GCC Code Coverage Report


Directory: ../
File: src/util/CommonUtil.cpp
Date: 2025-10-27 22:48:14
Coverage Exec Excl Total
Lines: 97.0% 65 0 67
Functions: 100.0% 10 0 10
Branches: 56.1% 55 0 98

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 namespace spice::compiler {
17
18 /**
19 * Search all occurrences of needle in haystack and replace them with the replacement
20 *
21 * @param haystack Input string
22 * @param needle String to search
23 * @param replacement String to replace
24 */
25 1094910 void CommonUtil::replaceAll(std::string &haystack, const std::string &needle, const std::string &replacement) {
26 1094910 size_t start_pos = 0;
27
2/2
✓ Branch 8 → 3 taken 7017 times.
✓ Branch 8 → 9 taken 1094910 times.
1101927 while ((start_pos = haystack.find(needle, start_pos)) != std::string::npos) {
28 7017 haystack.replace(start_pos, needle.length(), replacement);
29 7017 start_pos += replacement.length();
30 }
31 1094910 }
32
33 /**
34 * Split the given haystack by the needle and return the last fragment
35 *
36 * @param haystack Input string
37 * @param needle String to search
38 * @return Last fragment
39 */
40 17439 std::string CommonUtil::getLastFragment(const std::string &haystack, const std::string &needle) {
41 17439 const size_t index = haystack.rfind(needle);
42
2/2
✓ Branch 3 → 4 taken 17436 times.
✓ Branch 3 → 5 taken 3 times.
17439 return index == std::string::npos ? haystack : haystack.substr(index + needle.length());
43 }
44
45 /**
46 * Trim the given input string
47 *
48 * @param input Input string
49 * @return Trimmed string
50 */
51 27 std::string CommonUtil::trim(const std::string &input) {
52 27 const size_t first = input.find_first_not_of(' ');
53
2/2
✓ Branch 3 → 4 taken 6 times.
✓ Branch 3 → 5 taken 21 times.
27 if (first == std::string::npos)
54 6 return input;
55 21 const size_t last = input.find_last_not_of(' ');
56 21 const size_t newLength = last - first + 1;
57 21 return input.substr(first, newLength);
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 2 → 3 taken 6 times.
✗ Branch 2 → 20 not taken.
✓ Branch 3 → 4 taken 6 times.
✗ Branch 3 → 18 not taken.
6 std::istringstream stream(trim(input));
69
70 6 std::string token;
71
4/6
✓ Branch 11 → 12 taken 21 times.
✗ Branch 11 → 24 not taken.
✓ Branch 12 → 13 taken 21 times.
✗ Branch 12 → 24 not taken.
✓ Branch 13 → 7 taken 15 times.
✓ Branch 13 → 14 taken 6 times.
21 while (std::getline(stream, token, ' '))
72
2/4
✓ Branch 7 → 8 taken 15 times.
✗ Branch 7 → 23 not taken.
✓ Branch 8 → 9 taken 15 times.
✗ Branch 8 → 21 not taken.
15 result.push_back(trim(token));
73
74 6 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 420 size_t CommonUtil::getSystemPageSize() {
83 #if OS_UNIX
84 420 return static_cast<size_t>(sysconf(_SC_PAGESIZE));
85 #elif OS_WINDOWS
86 SYSTEM_INFO si;
87 GetSystemInfo(&si);
88 return static_cast<size_t>(si.dwPageSize);
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 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) {
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 13 → 14 taken 8 times.
✗ Branch 13 → 17 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 1475616 std::string CommonUtil::demangleTypeName(const char *mangledName) {
121 int status;
122
1/2
✓ Branch 2 → 3 taken 1475616 times.
✗ Branch 2 → 25 not taken.
1475616 char *demangled = abi::__cxa_demangle(mangledName, nullptr, nullptr, &status);
123
1/2
✓ Branch 3 → 4 taken 1475616 times.
✗ Branch 3 → 11 not taken.
1475616 if (status == 0) {
124
1/2
✓ Branch 6 → 7 taken 1475616 times.
✗ Branch 6 → 19 not taken.
1475616 std::string result(demangled);
125 1475616 free(demangled);
126
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 1475616 times.
1475616 return result;
127 }
128 return mangledName;
129 }
130
131 /**
132 * Check if the given string is a valid, mangled name
133 *
134 * @return
135 */
136 27128 bool CommonUtil::isValidMangledName(const std::string &mangledName) {
137 int status;
138
1/2
✓ Branch 3 → 4 taken 27128 times.
✗ Branch 3 → 6 not taken.
27128 char *demangled = abi::__cxa_demangle(mangledName.c_str(), nullptr, nullptr, &status);
139 27128 free(demangled);
140 27128 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 2 → 3 taken 1 time.
✗ Branch 2 → 22 not taken.
1 std::stringstream message;
151
1/2
✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 20 not taken.
1 message << "*-----*\n";
152
1/2
✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 20 not taken.
1 message << "| |\n";
153
2/2
✓ Branch 13 → 6 taken 3 times.
✓ Branch 13 → 14 taken 1 time.
4 while (!sourceFiles.empty()) {
154
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";
155
1/2
✓ Branch 10 → 11 taken 3 times.
✗ Branch 10 → 20 not taken.
3 message << "| |\n";
156 3 sourceFiles.pop();
157 }
158
1/2
✓ Branch 14 → 15 taken 1 time.
✗ Branch 14 → 20 not taken.
1 message << "*-----*";
159
1/2
✓ Branch 15 → 16 taken 1 time.
✗ Branch 15 → 20 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 9 std::string CommonUtil::buildVersionInfo() {
168
1/2
✓ Branch 2 → 3 taken 9 times.
✗ Branch 2 → 27 not taken.
9 std::stringstream versionString;
169
7/14
✓ Branch 3 → 4 taken 9 times.
✗ Branch 3 → 25 not taken.
✓ Branch 4 → 5 taken 9 times.
✗ Branch 4 → 25 not taken.
✓ Branch 5 → 6 taken 9 times.
✗ Branch 5 → 25 not taken.
✓ Branch 6 → 7 taken 9 times.
✗ Branch 6 → 25 not taken.
✓ Branch 7 → 8 taken 9 times.
✗ Branch 7 → 25 not taken.
✓ Branch 8 → 9 taken 9 times.
✗ Branch 8 → 25 not taken.
✓ Branch 9 → 10 taken 9 times.
✗ Branch 9 → 25 not taken.
9 versionString << "Spice version: " << SPICE_VERSION << " " << SPICE_TARGET_OS << "/" << SPICE_TARGET_ARCH << "\n";
170
3/6
✓ Branch 10 → 11 taken 9 times.
✗ Branch 10 → 25 not taken.
✓ Branch 11 → 12 taken 9 times.
✗ Branch 11 → 25 not taken.
✓ Branch 12 → 13 taken 9 times.
✗ Branch 12 → 25 not taken.
9 versionString << "Git hash: " << SPICE_GIT_HASH << "\n";
171
3/6
✓ Branch 13 → 14 taken 9 times.
✗ Branch 13 → 25 not taken.
✓ Branch 14 → 15 taken 9 times.
✗ Branch 14 → 25 not taken.
✓ Branch 15 → 16 taken 9 times.
✗ Branch 15 → 25 not taken.
9 versionString << "LLVM version: " << LLVM_VERSION_STRING << "\n";
172
3/6
✓ Branch 16 → 17 taken 9 times.
✗ Branch 16 → 25 not taken.
✓ Branch 17 → 18 taken 9 times.
✗ Branch 17 → 25 not taken.
✓ Branch 18 → 19 taken 9 times.
✗ Branch 18 → 25 not taken.
9 versionString << "built by: " << SPICE_BUILT_BY << "\n\n";
173
1/2
✓ Branch 19 → 20 taken 9 times.
✗ Branch 19 → 25 not taken.
9 versionString << "(c) Marc Auberer 2021-2025";
174
1/2
✓ Branch 20 → 21 taken 9 times.
✗ Branch 20 → 25 not taken.
18 return versionString.str();
175 9 }
176
177 } // namespace spice::compiler
178