Skip to content

Commit 51ec3b9

Browse files
committed
replace PPC_FUNC_NAME with ppc::util::FuncName() implementation
1 parent 0cdc845 commit 51ec3b9

2 files changed

Lines changed: 47 additions & 8 deletions

File tree

modules/core/task/include/task.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,33 +108,33 @@ class Task {
108108
/// @brief Validates input data and task attributes before execution.
109109
/// @return True if validation is successful.
110110
virtual bool Validation() final {
111-
InternalOrderTest(PPC_FUNC_NAME);
111+
InternalOrderTest(ppc::util::FuncName());
112112
return ValidationImpl();
113113
}
114114

115115
/// @brief Performs preprocessing on the input data.
116116
/// @return True if preprocessing is successful.
117117
virtual bool PreProcessing() final {
118-
InternalOrderTest(PPC_FUNC_NAME);
118+
InternalOrderTest(ppc::util::FuncName());
119119
if (state_of_testing_ == StateOfTesting::kFunc) {
120-
InternalTimeTest(PPC_FUNC_NAME);
120+
InternalTimeTest(ppc::util::FuncName());
121121
}
122122
return PreProcessingImpl();
123123
}
124124

125125
/// @brief Executes the main logic of the task.
126126
/// @return True if execution is successful.
127127
virtual bool Run() final {
128-
InternalOrderTest(PPC_FUNC_NAME);
128+
InternalOrderTest(ppc::util::FuncName());
129129
return RunImpl();
130130
}
131131

132132
/// @brief Performs postprocessing on the output data.
133133
/// @return True if postprocessing is successful.
134134
virtual bool PostProcessing() final {
135-
InternalOrderTest(PPC_FUNC_NAME);
135+
InternalOrderTest(ppc::util::FuncName());
136136
if (state_of_testing_ == StateOfTesting::kFunc) {
137-
InternalTimeTest(PPC_FUNC_NAME);
137+
InternalTimeTest(ppc::util::FuncName());
138138
}
139139
return PostProcessingImpl();
140140
}

modules/core/util/include/util.hpp

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
#include <cstdint>
44
#include <cstdlib>
55
#include <memory>
6+
#include <source_location>
67
#include <string>
78
#include <string_view>
89

910
#include "nlohmann/json_fwd.hpp"
1011

11-
#define PPC_FUNC_NAME __func__
12-
1312
#ifdef _MSC_VER
1413
#pragma warning(push)
1514
#pragma warning(disable : 4459)
@@ -27,6 +26,46 @@ using NlohmannJsonTypeError = nlohmann::json::type_error;
2726

2827
namespace ppc::util {
2928

29+
/**
30+
* @brief Extract the bare function name (e.g. "PreProcessing" or "foo")
31+
* from a full signature provided by std::source_location,
32+
* working both on GCC/Clang and MSVC.
33+
*
34+
* @param loc Source location (file, line, pretty-signature).
35+
* Defaults to the call site via std::source_location::current().
36+
* @return A std::string containing only the function name.
37+
*
38+
* @note On GCC/Clang, loc.function_name() yields a “pretty” signature
39+
* including namespaces, templates and parameter list.
40+
* On MSVC, it also includes return type and calling convention.
41+
* This function strips off everything before the last `::` (if any),
42+
* and any parameter list or qualifiers after the name.
43+
*/
44+
inline std::string FuncName(const std::source_location& loc = std::source_location::current()) {
45+
std::string_view full = loc.function_name();
46+
// 1) find end of name (just before '(' or end of string)
47+
size_t paren = full.find('(');
48+
size_t name_end = (paren == std::string_view::npos ? full.size() : paren);
49+
50+
// 2) try to strip namespaces/classes via '::'
51+
size_t colons = full.rfind("::", name_end);
52+
size_t start = (colons == std::string_view::npos ? 0 : colons + 2);
53+
54+
// 3) on MSVC there's often a return-type + "__cdecl " prefix before the name
55+
#ifdef _MSC_VER
56+
if (colons == std::string_view::npos) {
57+
// if no '::', drop everything up to last space before name_end
58+
size_t last_space = full.rfind(' ', name_end);
59+
if (last_space != std::string_view::npos) {
60+
start = last_space + 1;
61+
}
62+
}
63+
#endif
64+
65+
// 4) construct owning string of just the name
66+
return std::string{full.substr(start, name_end - start)};
67+
}
68+
3069
enum GTestParamIndex : uint8_t { kTaskGetter, kNameTest, kTestParams };
3170

3271
std::string GetAbsoluteTaskPath(const std::string& id_path, const std::string& relative_path);

0 commit comments

Comments
 (0)