From c09329e64bfc5c9bb432465fea8354028798835d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Fri, 14 Aug 2026 16:43:08 +0200 Subject: [PATCH] fix: Drop the anonymous namespace from the benchmark URI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The namespace was extracted from __PRETTY_FUNCTION__ and rejected only when spelled '(anonymous namespace)', as clang does, so with GCC, which spells it '{anonymous}', it ended up in the URI: file.cpp::{anonymous}::bench[…]. Strip the segment instead of bailing out, for both spellings, so the rest of the namespace path is kept and no error is reported for a construct that is merely unnameable. --- core/src/uri.cpp | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/core/src/uri.cpp b/core/src/uri.cpp index efb4e58..be60a34 100644 --- a/core/src/uri.cpp +++ b/core/src/uri.cpp @@ -1,10 +1,22 @@ -#include #include #include "codspeed.h" namespace codspeed { +// The anonymous namespace is not a part of any identifier the benchmark can be referred to by, +// and its spelling differs between the compilers, so it is dropped from the namespace path. +// Example: {anonymous}::outer:: (GCC) or (anonymous namespace)::outer:: (clang) +// Returns: outer:: +std::string strip_anonymous_namespace(std::string ns) { + for (const std::string anon : {"{anonymous}::", "(anonymous namespace)::"}) { + for (auto pos = ns.find(anon); pos != std::string::npos; pos = ns.find(anon)) { + ns.erase(pos, anon.size()); + } + } + return ns; +} + // Example: auto outer::test12::(anonymous class)::operator()() const // Returns: outer::test12:: std::string extract_namespace_clang(const std::string &pretty_func) { @@ -36,16 +48,10 @@ std::string extract_namespace_gcc(const std::string &pretty_func) { // Returns: An empty string if the namespace could not be extracted, // otherwise the namespace with a trailing "::" std::string extract_lambda_namespace(const std::string &pretty_func) { - if (pretty_func.find("(anonymous namespace)") != std::string::npos) { - std::cerr << "[ERROR] Anonymous namespace not supported in " << pretty_func - << std::endl; - return {}; - } - #ifdef __clang__ - return extract_namespace_clang(pretty_func); + return strip_anonymous_namespace(extract_namespace_clang(pretty_func)); #elif __GNUC__ - return extract_namespace_gcc(pretty_func); + return strip_anonymous_namespace(extract_namespace_gcc(pretty_func)); #elif _MSC_VER // MSVC doesn't support __PRETTY_FUNCTION__ in the same way // Return empty string as fallback for Windows