| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2025 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #include "CommonUtil.h" | ||
| 4 | |||
| 5 | #include <SourceFile.h> // Must be included before windows.h due to symbol name ambiguities | ||
| 6 | |||
| 7 | #include <cxxabi.h> | ||
| 8 | #if OS_UNIX | ||
| 9 | #include <unistd.h> | ||
| 10 | #elif OS_WINDOWS | ||
| 11 | #include <windows.h> | ||
| 12 | #else | ||
| 13 | #error "Unsupported platform" | ||
| 14 | #endif | ||
| 15 | |||
| 16 | #include <llvm/TargetParser/Triple.h> | ||
| 17 | |||
| 18 | namespace spice::compiler { | ||
| 19 | |||
| 20 | /** | ||
| 21 | * Search all occurrences of needle in haystack and replace them with the replacement | ||
| 22 | * | ||
| 23 | * @param haystack Input string | ||
| 24 | * @param needle String to search | ||
| 25 | * @param replacement String to replace | ||
| 26 | */ | ||
| 27 | 1171040 | void CommonUtil::replaceAll(std::string &haystack, const std::string &needle, const std::string &replacement) { | |
| 28 | 1171040 | size_t start_pos = 0; | |
| 29 |
2/2✓ Branch 8 → 3 taken 7159 times.
✓ Branch 8 → 9 taken 1171040 times.
|
1178199 | while ((start_pos = haystack.find(needle, start_pos)) != std::string::npos) { |
| 30 | 7159 | haystack.replace(start_pos, needle.length(), replacement); | |
| 31 | 7159 | start_pos += replacement.length(); | |
| 32 | } | ||
| 33 | 1171040 | } | |
| 34 | |||
| 35 | /** | ||
| 36 | * Split the given haystack by the needle and return the last fragment | ||
| 37 | * | ||
| 38 | * @param haystack Input string | ||
| 39 | * @param needle String to search | ||
| 40 | * @return Last fragment | ||
| 41 | */ | ||
| 42 | 26630 | std::string CommonUtil::getLastFragment(const std::string &haystack, const std::string &needle) { | |
| 43 | 26630 | const size_t index = haystack.rfind(needle); | |
| 44 |
2/2✓ Branch 3 → 4 taken 26627 times.
✓ Branch 3 → 5 taken 3 times.
|
26630 | return index == std::string::npos ? haystack : haystack.substr(index + needle.length()); |
| 45 | } | ||
| 46 | |||
| 47 | /** | ||
| 48 | * Trim the given input string | ||
| 49 | * | ||
| 50 | * @param input Input string | ||
| 51 | * @return Trimmed string | ||
| 52 | */ | ||
| 53 | 547 | std::string CommonUtil::trim(const std::string &input) { | |
| 54 | 547 | const size_t first = input.find_first_not_of(' '); | |
| 55 |
2/2✓ Branch 3 → 4 taken 6 times.
✓ Branch 3 → 5 taken 541 times.
|
547 | if (first == std::string::npos) |
| 56 | 6 | return input; | |
| 57 | 541 | const size_t last = input.find_last_not_of(' '); | |
| 58 | 541 | const size_t newLength = last - first + 1; | |
| 59 | 541 | return input.substr(first, newLength); | |
| 60 | } | ||
| 61 | |||
| 62 | /** | ||
| 63 | * Split the given input string by spaces | ||
| 64 | * | ||
| 65 | * @param input Input string | ||
| 66 | * @return Vector of fragments | ||
| 67 | */ | ||
| 68 | 24 | std::vector<std::string> CommonUtil::split(const std::string &input) { | |
| 69 | 24 | std::vector<std::string> result; | |
| 70 |
2/4✓ Branch 2 → 3 taken 24 times.
✗ Branch 2 → 20 not taken.
✓ Branch 3 → 4 taken 24 times.
✗ Branch 3 → 18 not taken.
|
24 | std::istringstream stream(trim(input)); |
| 71 | |||
| 72 | 24 | std::string token; | |
| 73 |
4/6✓ Branch 11 → 12 taken 63 times.
✗ Branch 11 → 24 not taken.
✓ Branch 12 → 13 taken 63 times.
✗ Branch 12 → 24 not taken.
✓ Branch 13 → 7 taken 39 times.
✓ Branch 13 → 14 taken 24 times.
|
63 | while (std::getline(stream, token, ' ')) |
| 74 |
2/4✓ Branch 7 → 8 taken 39 times.
✗ Branch 7 → 23 not taken.
✓ Branch 8 → 9 taken 39 times.
✗ Branch 8 → 21 not taken.
|
39 | result.push_back(trim(token)); |
| 75 | |||
| 76 | 24 | return result; | |
| 77 | 24 | } | |
| 78 | |||
| 79 | /** | ||
| 80 | * Get the memory page size of the current system | ||
| 81 | * | ||
| 82 | * @return Page size in bytes | ||
| 83 | */ | ||
| 84 | 438 | size_t CommonUtil::getSystemPageSize() { | |
| 85 | #if OS_UNIX | ||
| 86 | 438 | return static_cast<size_t>(sysconf(_SC_PAGESIZE)); | |
| 87 | #elif OS_WINDOWS | ||
| 88 | SYSTEM_INFO si; | ||
| 89 | GetSystemInfo(&si); | ||
| 90 | return static_cast<size_t>(si.dwPageSize); | ||
| 91 | #else | ||
| 92 | #error "Unsupported platform" | ||
| 93 | #endif | ||
| 94 | } | ||
| 95 | |||
| 96 | /** | ||
| 97 | * Return the given number of bytes in a human-readable format | ||
| 98 | * | ||
| 99 | * @return Human-readable size string | ||
| 100 | */ | ||
| 101 | 8 | std::string CommonUtil::formatBytes(const size_t bytes) { | |
| 102 | 8 | const char *units[] = {"B", "KB", "MB", "GB", "TB"}; | |
| 103 | |||
| 104 | 8 | auto size = static_cast<double>(bytes); | |
| 105 | 8 | unsigned int unitIndex = 0; | |
| 106 |
5/6✓ Branch 4 → 5 taken 15 times.
✓ Branch 4 → 9 taken 8 times.
✓ Branch 7 → 8 taken 15 times.
✗ Branch 7 → 9 not taken.
✓ Branch 10 → 3 taken 15 times.
✓ Branch 10 → 11 taken 8 times.
|
38 | while (size >= 1024 && unitIndex < std::size(units) - 1) { |
| 107 | 15 | size /= 1024; | |
| 108 | 15 | unitIndex++; | |
| 109 | } | ||
| 110 | |||
| 111 | char buffer[20]; | ||
| 112 | 8 | snprintf(buffer, sizeof(buffer), "%.2f %s", size, units[unitIndex]); | |
| 113 |
1/2✓ Branch 13 → 14 taken 8 times.
✗ Branch 13 → 17 not taken.
|
16 | return {buffer}; |
| 114 | } | ||
| 115 | |||
| 116 | /** | ||
| 117 | * Demangle CXX type name | ||
| 118 | * | ||
| 119 | * @param mangledName Mangled CXX type name | ||
| 120 | * @return Demangled name | ||
| 121 | */ | ||
| 122 | 1584267 | std::string CommonUtil::demangleTypeName(const char *mangledName) { | |
| 123 | int status; | ||
| 124 |
1/2✓ Branch 2 → 3 taken 1584267 times.
✗ Branch 2 → 25 not taken.
|
1584267 | char *demangled = abi::__cxa_demangle(mangledName, nullptr, nullptr, &status); |
| 125 |
1/2✓ Branch 3 → 4 taken 1584267 times.
✗ Branch 3 → 11 not taken.
|
1584267 | if (status == 0) { |
| 126 |
1/2✓ Branch 6 → 7 taken 1584267 times.
✗ Branch 6 → 19 not taken.
|
1584267 | std::string result(demangled); |
| 127 | 1584267 | free(demangled); | |
| 128 |
1/2✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 1584267 times.
|
1584267 | return result; |
| 129 | ✗ | } | |
| 130 | ✗ | return mangledName; | |
| 131 | } | ||
| 132 | |||
| 133 | /** | ||
| 134 | * Check if the given string is a valid, mangled name | ||
| 135 | * | ||
| 136 | * @return | ||
| 137 | */ | ||
| 138 | 30947 | bool CommonUtil::isValidMangledName(const std::string &mangledName) { | |
| 139 | int status; | ||
| 140 |
1/2✓ Branch 3 → 4 taken 30947 times.
✗ Branch 3 → 6 not taken.
|
30947 | char *demangled = abi::__cxa_demangle(mangledName.c_str(), nullptr, nullptr, &status); |
| 141 | 30947 | free(demangled); | |
| 142 | 30947 | return status == 0; | |
| 143 | } | ||
| 144 | |||
| 145 | /** | ||
| 146 | * Retrieve OS name without any version information from a LLVM target triple | ||
| 147 | * | ||
| 148 | * @param targetTriple LLVM target triple | ||
| 149 | * @return OS name | ||
| 150 | */ | ||
| 151 | ✗ | std::string CommonUtil::getOSNameFromTargetTriple(const llvm::Triple *targetTriple) { | |
| 152 | ✗ | return targetTriple->getOSTypeName(targetTriple->getOS()).str(); | |
| 153 | } | ||
| 154 | |||
| 155 | /** | ||
| 156 | * Generate a circular import message from the given source files | ||
| 157 | * | ||
| 158 | * @param sourceFiles Source files that form the circular dependency chain | ||
| 159 | * @return Error message | ||
| 160 | */ | ||
| 161 | 1 | std::string CommonUtil::getCircularImportMessage(std::stack<const SourceFile *> &sourceFiles) { | |
| 162 |
1/2✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 22 not taken.
|
1 | std::stringstream message; |
| 163 |
1/2✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 20 not taken.
|
1 | message << "*-----*\n"; |
| 164 |
1/2✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 20 not taken.
|
1 | message << "| |\n"; |
| 165 |
2/2✓ Branch 13 → 6 taken 3 times.
✓ Branch 13 → 14 taken 1 time.
|
4 | while (!sourceFiles.empty()) { |
| 166 |
3/6✓ Branch 6 → 7 taken 3 times.
✗ Branch 6 → 20 not taken.
✓ Branch 8 → 9 taken 3 times.
✗ Branch 8 → 20 not taken.
✓ Branch 9 → 10 taken 3 times.
✗ Branch 9 → 20 not taken.
|
3 | message << "| " << sourceFiles.top()->fileName << "\n"; |
| 167 |
1/2✓ Branch 10 → 11 taken 3 times.
✗ Branch 10 → 20 not taken.
|
3 | message << "| |\n"; |
| 168 | 3 | sourceFiles.pop(); | |
| 169 | } | ||
| 170 |
1/2✓ Branch 14 → 15 taken 1 time.
✗ Branch 14 → 20 not taken.
|
1 | message << "*-----*"; |
| 171 |
1/2✓ Branch 15 → 16 taken 1 time.
✗ Branch 15 → 20 not taken.
|
2 | return message.str(); |
| 172 | 1 | } | |
| 173 | |||
| 174 | /** | ||
| 175 | * Generate the version info string for the Spice driver | ||
| 176 | * | ||
| 177 | * @return Version info string | ||
| 178 | */ | ||
| 179 | 450 | std::string CommonUtil::buildVersionInfo() { | |
| 180 |
1/2✓ Branch 2 → 3 taken 450 times.
✗ Branch 2 → 27 not taken.
|
450 | std::stringstream versionString; |
| 181 |
7/14✓ Branch 3 → 4 taken 450 times.
✗ Branch 3 → 25 not taken.
✓ Branch 4 → 5 taken 450 times.
✗ Branch 4 → 25 not taken.
✓ Branch 5 → 6 taken 450 times.
✗ Branch 5 → 25 not taken.
✓ Branch 6 → 7 taken 450 times.
✗ Branch 6 → 25 not taken.
✓ Branch 7 → 8 taken 450 times.
✗ Branch 7 → 25 not taken.
✓ Branch 8 → 9 taken 450 times.
✗ Branch 8 → 25 not taken.
✓ Branch 9 → 10 taken 450 times.
✗ Branch 9 → 25 not taken.
|
450 | versionString << "Spice version: " << SPICE_VERSION << " " << SPICE_TARGET_OS << "/" << SPICE_TARGET_ARCH << "\n"; |
| 182 |
3/6✓ Branch 10 → 11 taken 450 times.
✗ Branch 10 → 25 not taken.
✓ Branch 11 → 12 taken 450 times.
✗ Branch 11 → 25 not taken.
✓ Branch 12 → 13 taken 450 times.
✗ Branch 12 → 25 not taken.
|
450 | versionString << "Git hash: " << SPICE_GIT_HASH << "\n"; |
| 183 |
3/6✓ Branch 13 → 14 taken 450 times.
✗ Branch 13 → 25 not taken.
✓ Branch 14 → 15 taken 450 times.
✗ Branch 14 → 25 not taken.
✓ Branch 15 → 16 taken 450 times.
✗ Branch 15 → 25 not taken.
|
450 | versionString << "LLVM version: " << LLVM_VERSION_STRING << "\n"; |
| 184 |
3/6✓ Branch 16 → 17 taken 450 times.
✗ Branch 16 → 25 not taken.
✓ Branch 17 → 18 taken 450 times.
✗ Branch 17 → 25 not taken.
✓ Branch 18 → 19 taken 450 times.
✗ Branch 18 → 25 not taken.
|
450 | versionString << "built by: " << SPICE_BUILT_BY << "\n\n"; |
| 185 |
1/2✓ Branch 19 → 20 taken 450 times.
✗ Branch 19 → 25 not taken.
|
450 | versionString << "(c) Marc Auberer 2021-2025"; |
| 186 |
1/2✓ Branch 20 → 21 taken 450 times.
✗ Branch 20 → 25 not taken.
|
900 | return versionString.str(); |
| 187 | 450 | } | |
| 188 | |||
| 189 | } // namespace spice::compiler | ||
| 190 |