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
2827namespace 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+
3069enum GTestParamIndex : uint8_t { kTaskGetter , kNameTest , kTestParams };
3170
3271std::string GetAbsoluteTaskPath (const std::string& id_path, const std::string& relative_path);
0 commit comments