Skip to content

[Clang] Suppressing -Wc2y-extensions does not work as expected #189645

Open

Description

@horenmar

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.

Activity

  1. added
    clang
    on Mar 31, 2026
  2. added
    clang:diagnostics
    confirmed
    on Mar 31, 2026
  3. AaronBallman commented on Mar 31, 2026

    @AaronBallman
    Contributor

    Possibly related: #184250
    Using a #pragma works: https://godbolt.org/z/5EjhhYcnK

  4. horenmar commented on Mar 31, 2026

    @horenmar
    Author

    Using #pragma works, but it causes the warning to be suppressed for the rest of the TU, which is... suboptimal.

  5. AaronBallman commented on Mar 31, 2026

    @AaronBallman
    Contributor

    Using #pragma works, but it causes the warning to be suppressed for the rest of the TU, which is... suboptimal.

    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 BLAH is outside of the pragma region. I'm not certain if that's intentional behavior or not.

  6. cor3ntin commented on Mar 31, 2026

    @cor3ntin
    Contributor

    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)

  7. removed
    clang
    on Mar 31, 2026
  8. horenmar commented on Mar 31, 2026

    @horenmar
    Author

    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 BLAH is outside of the pragma region.

    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 _Pragma so that the suppression happens in the expansion location.

  9. AaronBallman commented on Mar 31, 2026

    @AaronBallman
    Contributor

    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.

    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.)

    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)

    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.

  10. AaronBallman commented on Mar 31, 2026

    @AaronBallman
    Contributor

    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 BLAH is outside of the pragma region.

    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 _Pragma so that the suppression happens in the expansion location.

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    c2yclang:diagnosticsconfirmedquality-of-implementation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions