https://github.com/ojhunt created https://github.com/llvm/llvm-project/pull/196689
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. >From 6bb9cd1601ddcbf698512289afb7ba0aa27b7158 Mon Sep 17 00:00:00 2001 From: Oliver Hunt <[email protected]> Date: Fri, 8 May 2026 21:20:44 -0700 Subject: [PATCH] [clang] Don't warn on __COUNTER__ in system macros 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. --- clang/include/clang/Basic/DiagnosticLexKinds.td | 4 ++-- .../test/Lexer/Inputs/__counter__-system-header.h | 7 +++++++ clang/test/Lexer/__counter__-system-include.c | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 clang/test/Lexer/Inputs/__counter__-system-header.h create mode 100644 clang/test/Lexer/__counter__-system-include.c 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(); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
