Skip to content

[clang] Don't warn on __COUNTER__ in system macros - #196689

Merged
ojhunt merged 1 commit into
mainfrom
users/ojhunt/permit-counter-in-system-macros
May 12, 2026
Merged

ojhunt merged 1 commit into
mainfrom
users/ojhunt/permit-counter-in-system-macros

Conversation

@ojhunt

@ojhunt ojhunt commented May 9, 2026 •

Copy link
Copy Markdown
Contributor

The introduction of extension and compatibility warnings means that __COUNTER__ has started causing warnings (and -Werror= build failures) due to use of system APIs.

This PR simply ensures that these diagnostics don't get reported for system macro expansions as well.

@ojhunt
ojhunt requested review from AaronBallman and cor3ntin May 9, 2026 05:11
@llvmorg-github-actions llvmorg-github-actions Bot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels May 9, 2026
@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-clang

Author: Oliver Hunt (ojhunt)

Changes

The introduction of extension and compatibility warnings means that COUNTER has started causing warnings (and -Werror=) build failures due to use of system APIs.

This PR simply ensures that these diagnostics don't get reported for system macro expansions as well.


Full diff: https://github.com/llvm/llvm-project/pull/196689.diff

3 Files Affected:

  • (modified) clang/include/clang/Basic/DiagnosticLexKinds.td (+2-2)
  • (added) clang/test/Lexer/Inputs/counter-system-header.h (+7)
  • (added) clang/test/Lexer/counter-system-include.c (+15)
diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td b/clang/include/clang/Basic/DiagnosticLexKinds.td
index 85fa290de6fd9..0ac7ac27a0271 100644
--- a/clang/include/clang/Basic/DiagnosticLexKinds.td
+++ b/clang/include/clang/Basic/DiagnosticLexKinds.td
@@ -93,10 +93,10 @@ def err_conflict_marker : Error<"version control conflict marker in file">;
 def err_counter_overflow : Error<
   "'__COUNTER__' value cannot exceed 2'147'483'647">;
 def ext_counter : Extension<
-  "'__COUNTER__' is a C2y extension">, InGroup<C2y>;
+  "'__COUNTER__' is a C2y extension">, InGroup<C2y>, SuppressInSystemMacro;
 def warn_counter : Warning<
   "'__COUNTER__' is incompatible with standards before C2y">,
-  InGroup<CPre2yCompat>, DefaultIgnore;
+  InGroup<CPre2yCompat>, DefaultIgnore, SuppressInSystemMacro;
 
 def err_raw_delim_too_long : Error<
   "raw string delimiter longer than 16 characters"
diff --git a/clang/test/Lexer/Inputs/__counter__-system-header.h b/clang/test/Lexer/Inputs/__counter__-system-header.h
new file mode 100644
index 0000000000000..60619215abd27
--- /dev/null
+++ b/clang/test/Lexer/Inputs/__counter__-system-header.h
@@ -0,0 +1,7 @@
+#define COUNTER_ALIAS __COUNTER__
+#define COUNTER_MACRO() __COUNTER__
+
+int header_counter_value = __COUNTER__;
+int header_counter_alias = COUNTER_ALIAS;
+int header_counter_macro = COUNTER_MACRO();
+
diff --git a/clang/test/Lexer/__counter__-system-include.c b/clang/test/Lexer/__counter__-system-include.c
new file mode 100644
index 0000000000000..6574190258d47
--- /dev/null
+++ b/clang/test/Lexer/__counter__-system-include.c
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1                  -Wpedantic %s -fsyntax-only -isystem %S/Inputs -verify=ext
+// RUN: %clang_cc1 -std=c2y         -Wpedantic %s -fsyntax-only -isystem %S/Inputs -verify
+// RUN: %clang_cc1 -std=c2y -Wpre-c2y-compat   %s -fsyntax-only -isystem %S/Inputs -verify=pre
+// RUN: %clang_cc1                            -pedantic %s -fsyntax-only -isystem %S/Inputs -verify=ext
+// RUN: %clang_cc1 -std=c2y -Wpre-c2y-compat  -pedantic %s -fsyntax-only -isystem %S/Inputs -verify=pre
+
+#include <__counter__-system-header.h>
+
+// expected-no-diagnostics
+
+int tu_direct_reference = __COUNTER__;
+// ext-warning@-1 {{'__COUNTER__' is a C2y extension}}
+// pre-warning@-2 {{'__COUNTER__' is incompatible with standards before C2y}}
+int tu_counter_alias = COUNTER_ALIAS;
+int tu_counter_macro = COUNTER_MACRO();

@ojhunt
ojhunt force-pushed the users/ojhunt/permit-counter-in-system-macros branch from 6bb9cd1 to bb174f4 Compare May 9, 2026 05:12
@cor3ntin

cor3ntin commented May 9, 2026

Copy link
Copy Markdown
Contributor

@AaronBallman The disruption continues

@AaronBallman AaronBallman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should come with a release note eventually but I'd like to backport this to Clang 22.x. One testing request, but I think this generally LGTM

@ojhunt

ojhunt commented May 11, 2026

Copy link
Copy Markdown
Contributor Author

I'm no longer convinced that this is the correct fix. No other compiler warns on __COUNTER__ in any mode. We should not be warning either.

We should not be papering over a bug by trying to suppress it in one of the many places it is triggered.

@AaronBallman

Copy link
Copy Markdown
Contributor

I'm no longer convinced that this is the correct fix. No other compiler warns on __COUNTER__ in any mode. We should not be warning either.

Strong -1; we absolutely should be telling users about extensions when they enable the flag documented to tell them about extensions. There are other compilers which do not support __COUNTER__, so this isn't a theoretical portability issue either:

https://godbolt.org/z/8s1EYo9zM
https://godbolt.org/z/fnonKWbWW

That said, I'm strongly in favor of reducing contention in practice. I think we should introduce -Wcounter-extension or similar so users can do -pedantic -Wno-counter-extension if they want extension diagnostics except for ones related to __COUNTER__.

(The only folks who should be seeing this diagnostic are people who explicitly enabled extension diagnostics, so if this is being enabled through other, that's a different situation and possibly a bug. But the current behavior is intentional in that users asking for extension diagnostics will get extension diagnostics and that sometimes means extensions we've supported for a long time will start being diagnosed, same as any other fix for a false negative.)

@ojhunt
ojhunt force-pushed the users/ojhunt/permit-counter-in-system-macros branch from bb174f4 to 4be40f7 Compare May 11, 2026 20:12
The introduction of extension and compatibility warnings means
that __COUNTER__ has started causing warnings (and -Werror= build
failures) due to use of system APIs.

This PR simply ensures that these diagnostics don't get reported
for system macro expansions as well.
@ojhunt
ojhunt force-pushed the users/ojhunt/permit-counter-in-system-macros branch from 4be40f7 to fbb6e2e Compare May 11, 2026 20:12
@ojhunt

ojhunt commented May 11, 2026

Copy link
Copy Markdown
Contributor Author

@AaronBallman @cor3ntin now with pedantic-errors tests -- I'm more willing to accept a system macro only extension here as the non-system macro failures involved projects doing questionable things

@ojhunt
ojhunt requested a review from AaronBallman May 11, 2026 20:15
@shafik

shafik commented May 11, 2026

Copy link
Copy Markdown
Contributor

That said, I'm strongly in favor of reducing contention in practice. I think we should introduce -Wcounter-extension or similar so users can do -pedantic -Wno-counter-extension if they want extension diagnostics except for ones related to __COUNTER__.

(The only folks who should be seeing this diagnostic are people who explicitly enabled extension diagnostics, so if this is being enabled through other, that's a different situation and possibly a bug. But the current behavior is intentional in that users asking for extension diagnostics will get extension diagnostics and that sometimes means extensions we've supported for a long time will start being diagnosed, same as any other fix for a false negative.)

Agreed, someone could easily make the counter argument, "I want warning on all extensions and I am not happy you carved out an exception, I want what is written on the label". The simplest path is stick with consistent approach and provide -Wno-counter-extension for those who this is a problem.

@ojhunt

ojhunt commented May 11, 2026

Copy link
Copy Markdown
Contributor Author

That said, I'm strongly in favor of reducing contention in practice. I think we should introduce -Wcounter-extension or similar so users can do -pedantic -Wno-counter-extension if they want extension diagnostics except for ones related to __COUNTER__.
(The only folks who should be seeing this diagnostic are people who explicitly enabled extension diagnostics, so if this is being enabled through other, that's a different situation and possibly a bug. But the current behavior is intentional in that users asking for extension diagnostics will get extension diagnostics and that sometimes means extensions we've supported for a long time will start being diagnosed, same as any other fix for a false negative.)

Agreed, someone could easily make the counter argument, "I want warning on all extensions and I am not happy you carved out an exception, I want what is written on the label". The simplest path is stick with consistent approach and provide -Wno-counter-extension for those who this is a problem.

We currently support warnings that don't get error promoted with -Werror, but that doesn't work for extensions as it asserts the diagnostic is a warning. I can't tell if the reason for the assertion is to explicitly say "this only applies to warnings" or to prevent an actual error diagnostic being downgraded to a warning.

The other question for such approach is the intended user. While I disagree about this being captured by pedantic-errors is actually expected by users in this case, it does feel like maybe warning->error promotion (and promotion suppression) might be worth separating -Werror and -pedantic-error. On the other hand I just searched for DefaultWarnNoWerror and it's mentioned four times: the declaration of the class, the test case, and the two diagnostics that actually use it :D

@ojhunt

ojhunt commented May 12, 2026

Copy link
Copy Markdown
Contributor Author

(sigh very nearly landed w/o review because of GH's terrible UI)

@AaronBallman

Copy link
Copy Markdown
Contributor

We currently support warnings that don't get error promoted with -Werror, but that doesn't work for extensions as it asserts the diagnostic is a warning. I can't tell if the reason for the assertion is to explicitly say "this only applies to warnings" or to prevent an actual error diagnostic being downgraded to a warning.

I believe it's to prevent actual error diagnostics from being downgraded into warnings rather than something specific to not applying to extensions or errors.

The other question for such approach is the intended user. While I disagree about this being captured by pedantic-errors is actually expected by users in this case, it does feel like maybe warning->error promotion (and promotion suppression) might be worth separating -Werror and -pedantic-error. On the other hand I just searched for DefaultWarnNoWerror and it's mentioned four times: the declaration of the class, the test case, and the two diagnostics that actually use it :D

I would not recommend DefaultWarnNoWerror in general, that should really only be for extremely special cases (like compatibility with the extension behavior in another compiler). __COUNTER__ isn't special in that regard IMO.

@AaronBallman AaronBallman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM though I think this is worth a backport and add a release note at that time.

@ojhunt
ojhunt merged commit 3724713 into main May 12, 2026
8 checks passed
@ojhunt
ojhunt deleted the users/ojhunt/permit-counter-in-system-macros branch May 12, 2026 19:14
lialan pushed a commit to lialan/llvm-project that referenced this pull request May 13, 2026
The introduction of extension and compatibility warnings means that
`__COUNTER__` has started causing warnings (and -Werror= build failures)
due to use of system APIs.

This PR simply ensures that these diagnostics don't get reported for
system macro expansions as well.
EuphoricThinking pushed a commit to EuphoricThinking/llvm-project that referenced this pull request May 14, 2026
The introduction of extension and compatibility warnings means that
`__COUNTER__` has started causing warnings (and -Werror= build failures)
due to use of system APIs.

This PR simply ensures that these diagnostics don't get reported for
system macro expansions as well.
pedroMVicente pushed a commit to pedroMVicente/llvm-project that referenced this pull request May 19, 2026
The introduction of extension and compatibility warnings means that
`__COUNTER__` has started causing warnings (and -Werror= build failures)
due to use of system APIs.

This PR simply ensures that these diagnostics don't get reported for
system macro expansions as well.
fanquake pushed a commit to bitcoin-core/libmultiprocess that referenced this pull request Oct 28, 2026
`vmactions/openbsd-vm` now provides OpenBSD 8.0 by default with Clang
22.1.6, which triggers `-Wc2y-extensions` on `__COUNTER__`.

Disable the warning temporarily until the fix becomes available in
Clang.

See upstream LLVM items:
- llvm/llvm-project#196557
- llvm/llvm-project#196689
- llvm/llvm-project#218669
fanquake added a commit to bitcoin-core/libmultiprocess that referenced this pull request Oct 28, 2026
1b742cf ci: Fix OpenBSD job (Hennadii Stepanov)

Pull request description:

  `vmactions/openbsd-vm` now [provides](https://github.com/vmactions/openbsd-vm/releases/tag/v1.4.9) OpenBSD 8.0 by default with Clang 22.1.6, which triggers `-Wc2y-extensions` on `__COUNTER__`.

  Disable the warning temporarily until the fix becomes available in Clang.

  See upstream LLVM items:
  - llvm/llvm-project#196557
  - llvm/llvm-project#196689
  - llvm/llvm-project#218669

  Related:
  - #371
  - https://github.com/bitcoin-core/libmultiprocess/actions/runs/39768415425/job/119789556263?pr=380

ACKs for top commit:
  maflcko:
    lgtm ACK 1b742cf
  xyzconstant:
    ACK 1b742cf
  ViniciusCestarii:
    ACK 1b742cf

Tree-SHA512: 673e5b61073ae1df8f577b6f12b31a33b7c9de1df64b5d57bb2bc61434d52ff3920be77cbcdb105bc72a610018ea9b5dc8eb1ad50a0a355842b38c7b3b0e95bf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants