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