Repository navigation
Open
Labels
c2yclang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerconfirmedVerified by a second partyquality-of-implementation
Description
I have difficulty suppressing -Wc2y-extensions when using __COUNTER__ to generate unique C++ identifiers.
I minimized this reproducer from Catch2:
#if defined(REAL_SUPPRESS)
#define SUPPRESS_WARNING _Pragma( "clang diagnostic ignored \"-Wc2y-extensions\"" )
#else
#define SUPPRESS_WARNING
#endif
#define INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line ) name##line
#define INTERNAL_CATCH_UNIQUE_NAME_LINE( name, line ) \
INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line )
#define INTERNAL_CATCH_UNIQUE_NAME( name ) \
SUPPRESS_WARNING \
INTERNAL_CATCH_UNIQUE_NAME_LINE( name, __COUNTER__ )
#define INTERNAL_CATCH_TESTCASE2( TestName, ... ) \
static void TestName(); \
static void TestName()
#define INTERNAL_CATCH_TESTCASE( ... ) \
INTERNAL_CATCH_TESTCASE2( INTERNAL_CATCH_UNIQUE_NAME( CATCH2_INTERNAL_TEST_ ), __VA_ARGS__ )
#define TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE( __VA_ARGS__ )
TEST_CASE() {}Surprisingly, when preprocessing the input I can see the diagnostic pragma there before the identifier:
$ clang++-23 -Wc2y-extensions foo.cpp -E -fkeep-system-includes -DREAL_SUPPRESS
# 1 "foo.cpp"
# 1 "<built-in>" 1
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "foo.cpp" 2
foo.cpp:27:1: warning: '__COUNTER__' is a C2y extension [-Wc2y-extensions]
27 | TEST_CASE() {}
| ^
foo.cpp:24:26: note: expanded from macro 'TEST_CASE'
24 | #define TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE( __VA_ARGS__ )
| ^
foo.cpp:22:31: note: expanded from macro 'INTERNAL_CATCH_TESTCASE'
22 | INTERNAL_CATCH_TESTCASE2( INTERNAL_CATCH_UNIQUE_NAME( CATCH2_INTERNAL_TEST_ ), __VA_ARGS__ )
| ^
foo.cpp:14:44: note: expanded from macro 'INTERNAL_CATCH_UNIQUE_NAME'
14 | INTERNAL_CATCH_UNIQUE_NAME_LINE( name, __COUNTER__ )
| ^
# 27 "foo.cpp"
static void
# 27 "foo.cpp"
#pragma clang diagnostic ignored "-Wc2y-extensions"
# 27 "foo.cpp"
CATCH2_INTERNAL_TEST_0(); static void
# 27 "foo.cpp"
#pragma clang diagnostic ignored "-Wc2y-extensions"
# 27 "foo.cpp"
CATCH2_INTERNAL_TEST_0() {}
1 warning generated.
Notice the suppression pragma above the CATCH2_INTERNAL_TEST_0 identifier.
Since adding more lines with TEST_CASE(){} doesn't trigger more warnings with -DREAL_SUPPRESS, but does without it, we can see that the actual diagnostic suppression pragma works, but I guess that for some reason Clang issues the first warning before the actual __COUNTER__ usage.
Metadata
Metadata
Assignees
Labels
c2yclang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerconfirmedVerified by a second partyquality-of-implementation
Activity
AaronBallman commentedon Mar 31, 2026
Possibly related: #184250
Using a
#pragmaworks: https://godbolt.org/z/5EjhhYcnKhorenmar commentedon Mar 31, 2026
Using
#pragma works, but it causes the warning to be suppressed for the rest of the TU, which is... suboptimal.AaronBallman commentedon Mar 31, 2026
Agreed, I only pointed it out to others looking into the issue so that they know we do have the ability to suppress the diagnostics. I think the issue might be that the suppression is based on the location of the macro expansion rather than the lexical location of the pragmas. e.g., https://godbolt.org/z/K6YKr8aKf shows that we still emit the diagnostic when the expansion of
BLAHis outside of the pragma region. I'm not certain if that's intentional behavior or not.cor3ntin commentedon Mar 31, 2026
There is also the issue that this code used to be accepted in Clang 21 (and probably all previous version of clang) in all language modes without warning, with and without
-fpedantic. While we might want to tell C users about the C extension, we might consider making it a pedantic warning - and we probably don't want to tell C++ users about C2Y (Again, because__COUNTER__has been in use for almost 20 years and it was never considered a C only feature by Clang)horenmar commentedon Mar 31, 2026
FWIW, for me this is the expected behaviour of warnings in macro expansions. Every other warning Catch2 has to silence inside its own macros has to be handled with
_Pragmaso that the suppression happens in the expansion location.AaronBallman commentedon Mar 31, 2026
That's not an issue, that's routine work on the compiler as the language standards change and as we improve the implementation. We've always diagnosed extensions, but we've not always diagnosed them consistently. That's something we've been improving over time, so when this feature was standardized for C2y, it was appropriate to update the diagnostic behavior to correctly tell users about portability issue. (And this change did point out portability issues in practice.)
Quite possibly, except this feature is heading towards standardization in C++29 where it'll again end up being an extension we'd support in older language modes, so we'll still have to solve this issue.
AaronBallman commentedon Mar 31, 2026
Yeah, I think I agree as well when poking at other macro expansion related diagnostics. e.g., this behavior makes sense to me: https://godbolt.org/z/sWP94dYrY
Impl a temporary workaround for Clang 22.x
Impl a temporary workaround for Clang 22.x