|
1 | | -#include <iostream> |
2 | 1 | #include <string> |
3 | 2 |
|
4 | 3 | #include "codspeed.h" |
5 | 4 |
|
6 | 5 | namespace codspeed { |
7 | 6 |
|
| 7 | +// The anonymous namespace is not a part of any identifier the benchmark can be referred to by, |
| 8 | +// and its spelling differs between the compilers, so it is dropped from the namespace path. |
| 9 | +// Example: {anonymous}::outer:: (GCC) or (anonymous namespace)::outer:: (clang) |
| 10 | +// Returns: outer:: |
| 11 | +std::string strip_anonymous_namespace(std::string ns) { |
| 12 | + for (const std::string anon : {"{anonymous}::", "(anonymous namespace)::"}) { |
| 13 | + for (auto pos = ns.find(anon); pos != std::string::npos; pos = ns.find(anon)) { |
| 14 | + ns.erase(pos, anon.size()); |
| 15 | + } |
| 16 | + } |
| 17 | + return ns; |
| 18 | +} |
| 19 | + |
8 | 20 | // Example: auto outer::test12::(anonymous class)::operator()() const |
9 | 21 | // Returns: outer::test12:: |
10 | 22 | std::string extract_namespace_clang(const std::string &pretty_func) { |
@@ -36,16 +48,10 @@ std::string extract_namespace_gcc(const std::string &pretty_func) { |
36 | 48 | // Returns: An empty string if the namespace could not be extracted, |
37 | 49 | // otherwise the namespace with a trailing "::" |
38 | 50 | std::string extract_lambda_namespace(const std::string &pretty_func) { |
39 | | - if (pretty_func.find("(anonymous namespace)") != std::string::npos) { |
40 | | - std::cerr << "[ERROR] Anonymous namespace not supported in " << pretty_func |
41 | | - << std::endl; |
42 | | - return {}; |
43 | | - } |
44 | | - |
45 | 51 | #ifdef __clang__ |
46 | | - return extract_namespace_clang(pretty_func); |
| 52 | + return strip_anonymous_namespace(extract_namespace_clang(pretty_func)); |
47 | 53 | #elif __GNUC__ |
48 | | - return extract_namespace_gcc(pretty_func); |
| 54 | + return strip_anonymous_namespace(extract_namespace_gcc(pretty_func)); |
49 | 55 | #elif _MSC_VER |
50 | 56 | // MSVC doesn't support __PRETTY_FUNCTION__ in the same way |
51 | 57 | // Return empty string as fallback for Windows |
|
0 commit comments