GCC Code Coverage Report


Directory: ./
File: modules/core/util/include/util.hpp
Date: 2025-06-27 00:53:24
Exec Total Coverage
Lines: 14 17 82.4%
Functions: 1 1 100.0%
Branches: 22 30 73.3%

Line Branch Exec Source
1 #pragma once
2
3 #include <cstdint>
4 #include <cstdlib>
5 #include <memory>
6 #include <source_location>
7 #include <string>
8 #include <string_view>
9
10 #include "nlohmann/json_fwd.hpp"
11
12 #ifdef _MSC_VER
13 #pragma warning(push)
14 #pragma warning(disable : 4459)
15 #endif
16
17 #include <nlohmann/json.hpp>
18
19 /// @brief JSON namespace used for settings and config parsing.
20 using NlohmannJsonParseError = nlohmann::json::parse_error;
21 /// @brief JSON namespace used for settings and config typing.
22 using NlohmannJsonTypeError = nlohmann::json::type_error;
23 #ifdef _MSC_VER
24 #pragma warning(pop)
25 #endif
26
27 namespace ppc::util {
28
29 /**
30 * @brief Returns the unqualified name of the current function.
31 *
32 * @param loc Source location, defaults to the current function.
33 * @return Function name without namespaces or parameters.
34 */
35
19/24
✓ Branch 1 taken 346 times.
✓ Branch 2 taken 10 times.
✓ Branch 7 taken 336 times.
✓ Branch 8 taken 10 times.
✓ Branch 12 taken 2016 times.
✓ Branch 13 taken 10 times.
✓ Branch 14 taken 70 times.
✓ Branch 19 taken 20 times.
✓ Branch 20 taken 70 times.
✓ Branch 25 taken 10 times.
✓ Branch 26 taken 70 times.
✓ Branch 31 taken 10 times.
✓ Branch 32 taken 70 times.
✓ Branch 37 taken 20 times.
✗ Branch 38 not taken.
✓ Branch 43 taken 20 times.
✗ Branch 44 not taken.
✓ Branch 48 taken 760 times.
✓ Branch 49 taken 30 times.
✗ Branch 50 not taken.
✓ Branch 55 taken 30 times.
✗ Branch 56 not taken.
✓ Branch 60 taken 400 times.
✗ Branch 61 not taken.
8686 inline std::string FuncName(const std::source_location& loc = std::source_location::current()) {
36 3176 std::string s{loc.function_name()};
37
1/2
✓ Branch 0 taken 3176 times.
✗ Branch 1 not taken.
3176 if (auto p = s.find('('); p != std::string::npos) {
38 s.resize(p); // drop “(…)”
39 }
40
1/2
✓ Branch 0 taken 3176 times.
✗ Branch 1 not taken.
3176 if (auto p = s.rfind("::"); p != std::string::npos) {
41
1/2
✓ Branch 1 taken 3176 times.
✗ Branch 2 not taken.
3176 s.erase(0, p + 2); // drop namespaces
42 }
43 3176 return s;
44 }
45
46 enum GTestParamIndex : uint8_t { kTaskGetter, kNameTest, kTestParams };
47
48 std::string GetAbsoluteTaskPath(const std::string& id_path, const std::string& relative_path);
49 int GetNumThreads();
50
51 template <typename T>
52 588 constexpr std::string_view GetNamespace() {
53 #if defined(__clang__) || defined(__GNUC__)
54 588 constexpr std::string_view kFunc = __PRETTY_FUNCTION__;
55 constexpr std::string_view kKey = "T = ";
56
57 auto start = kFunc.find(kKey);
58 588 if (start == std::string_view::npos) {
59 return {};
60 }
61 588 start += kKey.size();
62
63 auto end = kFunc.find_first_of(";]> ,", start);
64 588 if (end == std::string_view::npos) {
65 return {};
66 }
67
68 588 auto full_type = kFunc.substr(start, end - start);
69
70 auto ns_end = full_type.rfind("::");
71 588 if (ns_end == std::string_view::npos) {
72 return {};
73 }
74
75 588 return full_type.substr(0, ns_end);
76
77 #elif defined(_MSC_VER)
78 constexpr std::string_view kFunc = __FUNCSIG__;
79 constexpr std::string_view kKey = "GetNamespace<";
80
81 auto start = kFunc.find(kKey);
82 if (start == std::string_view::npos) return {};
83 start += kKey.size();
84
85 constexpr std::string_view prefixes[] = {"class ", "struct ", "enum ", "union "};
86 for (auto prefix : prefixes) {
87 if (kFunc.substr(start, prefix.size()) == prefix) {
88 start += prefix.size();
89 break;
90 }
91 }
92
93 auto end = kFunc.find('>', start);
94 if (end == std::string_view::npos) return {};
95
96 auto full_type = kFunc.substr(start, end - start);
97
98 auto ns_end = full_type.rfind("::");
99 if (ns_end == std::string_view::npos) return {};
100
101 return full_type.substr(0, ns_end);
102
103 #else
104 static_assert([] { return false; }(), "Unsupported compiler");
105 return {};
106 #endif
107 }
108
109 inline std::shared_ptr<nlohmann::json> InitJSONPtr() { return std::make_shared<nlohmann::json>(); }
110
111 bool IsUnderMpirun();
112
113 } // namespace ppc::util
114