GCC Code Coverage Report


Directory: ../
File: src/util/CommonUtil.cpp
Date: 2025-03-05 01:50:32
Exec Total Coverage
Lines: 65 67 97.0%
Functions: 10 10 100.0%
Branches: 55 98 56.1%

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 951631 void CommonUtil::replaceAll(std::string &haystack, const std::string &needle, const std::string &replacement) {
26 951631 size_t start_pos = 0;
27
2/2
✓ Branch 0 (8→3) taken 6258 times.
✓ Branch 1 (8→9) taken 951631 times.
957889 while ((start_pos = haystack.find(needle, start_pos)) != std::string::npos) {
28 6258 haystack.replace(start_pos, needle.length(), replacement);
29 6258 start_pos += replacement.length();
30 }
31 951631 }
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 15245 std::string CommonUtil::getLastFragment(const std::string &haystack, const std::string &needle) {
41 15245 const size_t index = haystack.rfind(needle);
42
2/2
✓ Branch 0 (3→4) taken 15242 times.
✓ Branch 1 (3→5) taken 3 times.
15245 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 0 (3→4) taken 6 times.
✓ Branch 1 (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 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));
69
70 6 std::string token;
71
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, ' '))
72
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));
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 407 size_t CommonUtil::getSystemPageSize() {
83 #if OS_UNIX
84 407 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 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) {
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 0 (13→14) taken 8 times.
✗ Branch 1 (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 1291340 std::string CommonUtil::demangleTypeName(const char *mangledName) {
121 int status;
122
1/2
✓ Branch 0 (2→3) taken 1291340 times.
✗ Branch 1 (2→25) not taken.
1291340 char *demangled = abi::__cxa_demangle(mangledName, nullptr, nullptr, &status);
123
1/2
✓ Branch 0 (3→4) taken 1291340 times.
✗ Branch 1 (3→11) not taken.
1291340 if (status == 0) {
124
1/2
✓ Branch 0 (6→7) taken 1291340 times.
✗ Branch 1 (6→19) not taken.
1291340 std::string result(demangled);
125 1291340 free(demangled);
126
1/2
✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 1291340 times.
1291340 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 22317 bool CommonUtil::isValidMangledName(const std::string &mangledName) {
137 int status;
138
1/2
✓ Branch 0 (3→4) taken 22317 times.
✗ Branch 1 (3→6) not taken.
22317 char *demangled = abi::__cxa_demangle(mangledName.c_str(), nullptr, nullptr, &status);
139 22317 free(demangled);
140 22317 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 0 (2→3) taken 1 times.
✗ Branch 1 (2→22) not taken.
1 std::stringstream message;
151
1/2
✓ Branch 0 (3→4) taken 1 times.
✗ Branch 1 (3→20) not taken.
1 message << "*-----*\n";
152
1/2
✓ Branch 0 (4→5) taken 1 times.
✗ Branch 1 (4→20) not taken.
1 message << "| |\n";
153
2/2
✓ Branch 0 (13→6) taken 3 times.
✓ Branch 1 (13→14) taken 1 times.
4 while (!sourceFiles.empty()) {
154
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";
155
1/2
✓ Branch 0 (10→11) taken 3 times.
✗ Branch 1 (10→20) not taken.
3 message << "| |\n";
156 3 sourceFiles.pop();
157 }
158
1/2
✓ Branch 0 (14→15) taken 1 times.
✗ Branch 1 (14→20) not taken.
1 message << "*-----*";
159
1/2
✓ Branch 0 (15→16) taken 1 times.
✗ Branch 1 (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 0 (2→3) taken 9 times.
✗ Branch 1 (2→27) not taken.
9 std::stringstream versionString;
169
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";
170
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";
171
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";
172
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";
173
1/2
✓ Branch 0 (19→20) taken 9 times.
✗ Branch 1 (19→25) not taken.
9 versionString << "(c) Marc Auberer 2021-2025";
174
1/2
✓ Branch 0 (20→21) taken 9 times.
✗ Branch 1 (20→25) not taken.
18 return versionString.str();
175 9 }
176
177 } // namespace spice::compiler
178