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