|
2 | 2 | #include <string>
|
3 | 3 | #include <iostream>
|
4 | 4 |
|
5 |
| -std::string extract_lambda_namespace(const std::string& pretty_func) { |
6 |
| - if (pretty_func.find("(anonymous namespace)") != std::string::npos) { |
7 |
| - std::cerr << "[ERROR] Anonymous namespace not supported in " << pretty_func << std::endl; |
8 |
| - return {}; |
9 |
| - } |
10 |
| - |
11 |
| -#ifdef __clang__ |
12 |
| - // Example: auto outer::test12::(anonymous class)::operator()() const |
13 |
| - |
14 |
| - std::size_t anon_class_pos = pretty_func.find("(anonymous class)"); |
| 5 | +// Example: auto outer::test12::(anonymous class)::operator()() const |
| 6 | +// Returns: outer::test12:: |
| 7 | +std::string extract_namespace_clang(const std::string& pretty_func) { |
| 8 | + std::size_t anon_class_pos = pretty_func.find("::(anonymous class)"); |
15 | 9 | std::size_t space_pos = pretty_func.find(' ');
|
16 | 10 |
|
17 | 11 | if (space_pos == std::string::npos || anon_class_pos == std::string::npos) {
|
18 | 12 | return {};
|
19 | 13 | }
|
| 14 | + space_pos += 1; // Skip the space |
20 | 15 |
|
21 | 16 | return pretty_func.substr(space_pos, anon_class_pos - space_pos) + "::";
|
22 |
| -#elif __GNUC__ |
23 |
| - // Example: outer::test12::<lambda()> |
| 17 | +} |
24 | 18 |
|
| 19 | +// Example: outer::test12::<lambda()> |
| 20 | +// Returns: outer::test12:: |
| 21 | +std::string extract_namespace_gcc(const std::string& pretty_func) { |
25 | 22 | auto lambda_pos = pretty_func.find("::<lambda()>");
|
26 | 23 | if (lambda_pos == std::string::npos) {
|
27 | 24 | return {};
|
28 | 25 | }
|
29 | 26 |
|
30 | 27 | return pretty_func.substr(0, lambda_pos) + "::";
|
| 28 | +} |
| 29 | + |
| 30 | +// Has to pass the pretty function from a lambda: |
| 31 | +// (([]() { return __PRETTY_FUNCTION__; })()) |
| 32 | +// |
| 33 | +// Returns: An empty string if the namespace could not be extracted, |
| 34 | +// otherwise the namespace with a trailing "::" |
| 35 | +std::string extract_lambda_namespace(const std::string& pretty_func) { |
| 36 | + if (pretty_func.find("(anonymous namespace)") != std::string::npos) { |
| 37 | + std::cerr << "[ERROR] Anonymous namespace not supported in " << pretty_func << std::endl; |
| 38 | + return {}; |
| 39 | + } |
| 40 | + |
| 41 | +#ifdef __clang__ |
| 42 | + return extract_namespace_clang(pretty_func); |
| 43 | +#elif __GNUC__ |
| 44 | + return extract_namespace_gcc(pretty_func); |
31 | 45 | #else
|
32 | 46 | #error "Unsupported compiler"
|
33 | 47 | #endif
|
|
0 commit comments