[clang] [clang] Don't warn on __COUNTER__ in system macros (PR #196689)
via cfe-commits
cfe-commits at lists.llvm.org
Fri May 8 22:12:30 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Oliver Hunt (ojhunt)
<details>
<summary>Changes</summary>
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
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 at -1 {{'__COUNTER__' is a C2y extension}}
+// pre-warning at -2 {{'__COUNTER__' is incompatible with standards before C2y}}
+int tu_counter_alias = COUNTER_ALIAS;
+int tu_counter_macro = COUNTER_MACRO();
``````````
</details>
https://github.com/llvm/llvm-project/pull/196689
More information about the cfe-commits
mailing list