[clang] [clang-tools-extra] [C++26] Implement P2843R3 - Preprocessing is never undefined (define/undef keyword part) (PR #218322)

Yihan Wang via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 10 21:03:11 PST 2026


https://github.com/yronglin updated https://github.com/llvm/llvm-project/pull/218322

>From e145bcda865b3a63616c94ce36fb9d0f06e32484 Mon Sep 17 00:00:00 2001
From: yronglin <yronglin777 at gmail.com>
Date: Sun, 23 Aug 2026 22:41:55 -0700
Subject: [PATCH 1/6] [C++26] Implement P2843R3 - Preprocessing is never
 undefined (define/undef keyword part)

Signed-off-by: yronglin <yronglin777 at gmail.com>
---
 clang/docs/ReleaseNotes.md                    |  6 +++
 clang/include/clang/Basic/DiagnosticGroups.td |  3 +-
 .../include/clang/Basic/DiagnosticLexKinds.td |  5 ++-
 clang/lib/Lex/PPDirectives.cpp                | 14 +++++--
 clang/test/C/drs/dr1xx.c                      |  2 +-
 clang/test/CXX/cpp/cpp.replace.general/p9.cpp | 38 +++++++++++++++++++
 clang/test/CXX/drs/cwg3088.cpp                | 14 +++----
 clang/test/Preprocessor/macro-reserved.c      | 30 +++++++--------
 clang/test/Preprocessor/macro-reserved.cpp    | 28 +++++++-------
 .../test/Preprocessor/warn-macro-undef-true.c |  8 +---
 clang/test/Sema/c++-keyword-in-c.c            |  8 ++--
 clang/test/SemaCUDA/noinline.cu               |  2 +-
 clang/test/SemaCXX/abstract.cpp               |  1 +
 clang/test/SemaCXX/consteval-builtin.cpp      |  2 +-
 clang/test/SemaTemplate/instantiate-array.cpp |  2 +-
 clang/www/cxx_status.html                     | 14 ++++++-
 16 files changed, 121 insertions(+), 56 deletions(-)
 create mode 100644 clang/test/CXX/cpp/cpp.replace.general/p9.cpp

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index b69f7cc3f5d60b..cb51a2c31fd011 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -795,6 +795,12 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
 - Added `-Wunsafe-buffer-usage-main-argv` as a diagnostic group under
   `-Wunsafe-buffer-usage` to control warnings on `main`'s `argv` parameter,
   allowing users to suppress them with `-Wno-unsafe-buffer-usage-main-argv`.
+- `-Wkeyword-macro` is now enabled by default for `#define` directives whose
+  macro name is a keyword or an identifier with special meaning. The
+  corresponding `#undef` diagnostic is not enabled by default; it is enabled by
+  `-pedantic`, `-Wkeyword-macro`, or its more specific
+  `-Wkeyword-macro-undef` subgroup.
+
 
 - `-Wno-unsafe-buffer-usage-in-libc-call` no longer turns off
   `-Wunsafe-buffer-usage-in-format-attr-call`, and
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td
index eb5a1280bf41ef..cd6da2f4eee752 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -1166,7 +1166,8 @@ def : DiagGroup<"sequence-point", [Unsequenced]>;
 
 // Preprocessor warnings.
 def AmbiguousMacro : DiagGroup<"ambiguous-macro">;
-def KeywordAsMacro : DiagGroup<"keyword-macro">;
+def KeywordAsMacroUndef : DiagGroup<"keyword-macro-undef">;
+def KeywordAsMacro : DiagGroup<"keyword-macro", [KeywordAsMacroUndef]>;
 def ReservedIdAsMacro : DiagGroup<"reserved-macro-identifier">;
 def ReservedIdAsMacroAlias : DiagGroup<"reserved-id-macro", [ReservedIdAsMacro]>;
 def ReservedAttributeIdentifier : DiagGroup<"reserved-attribute-identifier">;
diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td b/clang/include/clang/Basic/DiagnosticLexKinds.td
index ee3fb0e74d2209..f9abe2e65001c2 100644
--- a/clang/include/clang/Basic/DiagnosticLexKinds.td
+++ b/clang/include/clang/Basic/DiagnosticLexKinds.td
@@ -444,8 +444,11 @@ def note_pp_ambiguous_macro_chosen : Note<
   "expanding this definition of %0">;
 def note_pp_ambiguous_macro_other : Note<
   "other definition of %0">;
-def warn_pp_macro_hides_keyword : Extension<
+def warn_pp_macro_hides_keyword : ExtWarn<
   "keyword is hidden by macro definition">, InGroup<KeywordAsMacro>;
+def ext_pp_macro_name_is_keyword : Extension<
+  "keyword or identifier with special meaning is used as a macro name">,
+  InGroup<KeywordAsMacroUndef>;
 def warn_pp_macro_is_reserved_id : Warning<
   "macro name is a reserved identifier">, DefaultIgnore,
   InGroup<ReservedIdAsMacro>;
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 5ff165e3ddf81f..4bd61644348221 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -103,8 +103,10 @@ SourceRange Preprocessor::DiscardUntilEndOfDirective(
 
 /// Enumerates possible cases of #define/#undef a reserved identifier.
 enum MacroDiag {
-  MD_NoWarn,        //> Not a reserved identifier
-  MD_KeywordDef,    //> Macro hides keyword, enabled by default
+  MD_NoWarn,       //> Not a reserved identifier
+  MD_KeywordDef,   //> Macro hides keyword, enabled by default
+  MD_KeywordUnDef, //> Undef keyword,  It is generally harmless and widely used,
+                   //> enabled in pedantic mode.
   MD_ReservedMacro, //> #define of #undef reserved id, disabled by default
   MD_ReservedAttributeIdentifier
 };
@@ -213,7 +215,11 @@ static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) {
 
 static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II) {
   const LangOptions &Lang = PP.getLangOpts();
-  // Do not warn on keyword undef.  It is generally harmless and widely used.
+  StringRef Text = II->getName();
+  if (II->isKeyword(Lang))
+    return MD_KeywordUnDef;
+  if (Lang.CPlusPlus11 && (Text == "override" || Text == "final"))
+    return MD_KeywordUnDef;
   if (isReservedInAllContexts(II->isReserved(Lang)))
     return MD_ReservedMacro;
   if (isReservedCXXAttributeName(PP, II))
@@ -412,6 +418,8 @@ bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
       if (ShadowFlag)
         *ShadowFlag = true;
     }
+    if (D == MD_KeywordUnDef)
+      Diag(MacroNameTok, diag::ext_pp_macro_name_is_keyword);
     if (D == MD_ReservedMacro)
       Diag(MacroNameTok, diag::warn_pp_macro_is_reserved_id);
     if (D == MD_ReservedAttributeIdentifier)
diff --git a/clang/test/C/drs/dr1xx.c b/clang/test/C/drs/dr1xx.c
index 055a30cc9c4b8a..1704aa0a361d32 100644
--- a/clang/test/C/drs/dr1xx.c
+++ b/clang/test/C/drs/dr1xx.c
@@ -160,7 +160,7 @@ void dr106(void *p, int i) {
 void dr108(void) {
 #define const
   const int i = 12;
-#undef const
+#undef const /* expected-warning {{keyword or identifier with special meaning is used as a macro name}} */
   const int j = 12; /* expected-note {{variable 'j' declared const here}} */
 
   i = 100; /* Okay, the keyword was hidden by the macro. */
diff --git a/clang/test/CXX/cpp/cpp.replace.general/p9.cpp b/clang/test/CXX/cpp/cpp.replace.general/p9.cpp
new file mode 100644
index 00000000000000..7d775939063a26
--- /dev/null
+++ b/clang/test/CXX/cpp/cpp.replace.general/p9.cpp
@@ -0,0 +1,38 @@
+// RUN: %clang_cc1 -std=c++26 -fsyntax-only -verify=default %s
+// RUN: %clang_cc1 -std=c++26 -pedantic -fsyntax-only -verify=pedantic %s
+// RUN: %clang_cc1 -std=c++26 -pedantic -Wno-keyword-macro-undef -fsyntax-only -verify=default %s
+// RUN: %clang_cc1 -std=c++26 -pedantic-errors -fsyntax-only -verify=pedantic-errors %s
+
+// [cpp.replace.general]/p9: A translation unit shall not #define or #undef
+// macro names lexically identical to keywords ([lex.key]) or to the identifiers
+// listed in Table 4.
+#define for 0
+// default-warning at -1 {{keyword is hidden by macro definition}}
+// pedantic-warning at -2 {{keyword is hidden by macro definition}}
+// pedantic-errors-error at -3 {{keyword is hidden by macro definition}}
+#undef for
+// pedantic-warning at -1 {{keyword or identifier with special meaning is used as a macro name}}
+// pedantic-errors-error at -2 {{keyword or identifier with special meaning is used as a macro name}}
+
+#define final 1
+// default-warning at -1 {{keyword is hidden by macro definition}}
+// pedantic-warning at -2 {{keyword is hidden by macro definition}}
+// pedantic-errors-error at -3 {{keyword is hidden by macro definition}}
+#undef final
+// pedantic-warning at -1 {{keyword or identifier with special meaning is used as a macro name}}
+// pedantic-errors-error at -2 {{keyword or identifier with special meaning is used as a macro name}}
+
+#define override
+// default-warning at -1 {{keyword is hidden by macro definition}}
+// pedantic-warning at -2 {{keyword is hidden by macro definition}}
+// pedantic-errors-error at -3 {{keyword is hidden by macro definition}}
+#undef override
+// pedantic-warning at -1 {{keyword or identifier with special meaning is used as a macro name}}
+// pedantic-errors-error at -2 {{keyword or identifier with special meaning is used as a macro name}}
+
+// Empty definitions of qualifier keywords are accepted for compatibility with
+// configuration scripts, but #undef is still diagnosed in pedantic modes.
+#define const
+#undef const
+// pedantic-warning at -1 {{keyword or identifier with special meaning is used as a macro name}}
+// pedantic-errors-error at -2 {{keyword or identifier with special meaning is used as a macro name}}
diff --git a/clang/test/CXX/drs/cwg3088.cpp b/clang/test/CXX/drs/cwg3088.cpp
index 04333d8f91a46d..39c02dd31fc6c8 100644
--- a/clang/test/CXX/drs/cwg3088.cpp
+++ b/clang/test/CXX/drs/cwg3088.cpp
@@ -1,10 +1,10 @@
-// RUN: %clang_cc1 -std=c++98 -fexceptions -fcxx-exceptions -pedantic-errors %s -verify-directives -verify=expected
-// RUN: %clang_cc1 -std=c++11 -fexceptions -fcxx-exceptions -pedantic-errors %s -verify-directives -verify=expected,since-cxx11
-// RUN: %clang_cc1 -std=c++14 -fexceptions -fcxx-exceptions -pedantic-errors %s -verify-directives -verify=expected,since-cxx11
-// RUN: %clang_cc1 -std=c++17 -fexceptions -fcxx-exceptions -pedantic-errors %s -verify-directives -verify=expected,since-cxx11
-// RUN: %clang_cc1 -std=c++20 -fexceptions -fcxx-exceptions -pedantic-errors %s -verify-directives -verify=expected,since-cxx11,since-cxx20
-// RUN: %clang_cc1 -std=c++23 -fexceptions -fcxx-exceptions -pedantic-errors %s -verify-directives -verify=expected,since-cxx11,since-cxx20
-// RUN: %clang_cc1 -std=c++2c -fexceptions -fcxx-exceptions -pedantic-errors %s -verify-directives -verify=expected,since-cxx11,since-cxx20
+// RUN: %clang_cc1 -std=c++98 -fexceptions -fcxx-exceptions -pedantic-errors -Wno-keyword-macro-undef %s -verify-directives -verify=expected
+// RUN: %clang_cc1 -std=c++11 -fexceptions -fcxx-exceptions -pedantic-errors -Wno-keyword-macro-undef %s -verify-directives -verify=expected,since-cxx11
+// RUN: %clang_cc1 -std=c++14 -fexceptions -fcxx-exceptions -pedantic-errors -Wno-keyword-macro-undef %s -verify-directives -verify=expected,since-cxx11
+// RUN: %clang_cc1 -std=c++17 -fexceptions -fcxx-exceptions -pedantic-errors -Wno-keyword-macro-undef %s -verify-directives -verify=expected,since-cxx11
+// RUN: %clang_cc1 -std=c++20 -fexceptions -fcxx-exceptions -pedantic-errors -Wno-keyword-macro-undef %s -verify-directives -verify=expected,since-cxx11,since-cxx20
+// RUN: %clang_cc1 -std=c++23 -fexceptions -fcxx-exceptions -pedantic-errors -Wno-keyword-macro-undef %s -verify-directives -verify=expected,since-cxx11,since-cxx20
+// RUN: %clang_cc1 -std=c++2c -fexceptions -fcxx-exceptions -pedantic-errors -Wno-keyword-macro-undef %s -verify-directives -verify=expected,since-cxx11,since-cxx20
 
 // The list of keywords was taken fron [lex.key] for C++98, and the difference
 // between [lex.key] contents in N and N-1 revisions of the Standard otherwise.
diff --git a/clang/test/Preprocessor/macro-reserved.c b/clang/test/Preprocessor/macro-reserved.c
index 6026a9f60730e7..875fce19513c6d 100644
--- a/clang/test/Preprocessor/macro-reserved.c
+++ b/clang/test/Preprocessor/macro-reserved.c
@@ -9,7 +9,7 @@
 #define __STDC__ 1 // expected-warning {{redefining builtin macro}}
 #define __clang__ 1
 
-#undef for
+#undef for // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 #undef final
 #undef __HAVE_X
 #undef __cplusplus
@@ -30,30 +30,30 @@
 #define extern
 #define inline
 
-#undef while
-#undef const
-#undef static
-#undef extern
-#undef inline
+#undef while // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
+#undef const // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
+#undef static // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
+#undef extern // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
+#undef inline // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 
 #define inline __inline
-#undef  inline
+#undef  inline // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 #define inline __inline__
-#undef  inline
+#undef  inline // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 
 #define inline inline__  // expected-warning {{keyword is hidden by macro definition}}
-#undef  inline
+#undef  inline // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 #define extern __inline  // expected-warning {{keyword is hidden by macro definition}}
-#undef  extern
+#undef  extern // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 #define extern __extern	 // expected-warning {{keyword is hidden by macro definition}}
-#undef  extern
+#undef  extern // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 #define extern __extern__ // expected-warning {{keyword is hidden by macro definition}}
-#undef  extern
+#undef  extern // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 
 #define inline _inline   // expected-warning {{keyword is hidden by macro definition}}
-#undef  inline
+#undef  inline // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 #define volatile   // expected-warning {{keyword is hidden by macro definition}}
-#undef  volatile
+#undef  volatile // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 
 #pragma clang diagnostic warning "-Wreserved-macro-identifier"
 
@@ -64,7 +64,7 @@
 #define _HAVE_X 0  // expected-warning {{macro name is a reserved identifier}}
 #define X__Y
 
-#undef switch
+#undef switch // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 #undef final
 #undef __cplusplus // expected-warning {{macro name is a reserved identifier}}
 #undef _HAVE_X     // expected-warning {{macro name is a reserved identifier}}
diff --git a/clang/test/Preprocessor/macro-reserved.cpp b/clang/test/Preprocessor/macro-reserved.cpp
index 53bb3634bac4c2..a90966dc180dd4 100644
--- a/clang/test/Preprocessor/macro-reserved.cpp
+++ b/clang/test/Preprocessor/macro-reserved.cpp
@@ -6,7 +6,7 @@
 #define _HAVE_X 0
 #define X__Y
 
-#undef for
+#undef for // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 #undef final
 #undef __HAVE_X
 #undef _HAVE_X
@@ -22,30 +22,30 @@
 #define extern
 #define inline
 
-#undef while
-#undef const
-#undef static
-#undef extern
-#undef inline
+#undef while // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
+#undef const // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
+#undef static // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
+#undef extern // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
+#undef inline // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 
 #define inline __inline
-#undef  inline
+#undef  inline // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 #define inline __inline__
-#undef  inline
+#undef  inline // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 
 #define inline inline__  // expected-warning {{keyword is hidden by macro definition}}
-#undef  inline
+#undef  inline // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 #define extern __inline  // expected-warning {{keyword is hidden by macro definition}}
-#undef  extern
+#undef  extern // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 #define extern __extern	 // expected-warning {{keyword is hidden by macro definition}}
-#undef  extern
+#undef  extern // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 #define extern __extern__ // expected-warning {{keyword is hidden by macro definition}}
-#undef  extern
+#undef  extern // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 
 #define inline _inline   // expected-warning {{keyword is hidden by macro definition}}
-#undef  inline
+#undef  inline // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 #define volatile   // expected-warning {{keyword is hidden by macro definition}}
-#undef  volatile
+#undef  volatile // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 
 #pragma clang diagnostic warning "-Wreserved-macro-identifier"
 
diff --git a/clang/test/Preprocessor/warn-macro-undef-true.c b/clang/test/Preprocessor/warn-macro-undef-true.c
index 9a64d577a96ed4..e87b2d34f59cb9 100644
--- a/clang/test/Preprocessor/warn-macro-undef-true.c
+++ b/clang/test/Preprocessor/warn-macro-undef-true.c
@@ -2,11 +2,7 @@
 // RUN: %clang_cc1 %s -Eonly -std=c99 -verify=undef-true
 // RUN: %clang_cc1 %s -Eonly -std=c11 -verify=undef-true
 // RUN: %clang_cc1 %s -Eonly -std=c17 -verify=undef-true
-// RUN: %clang_cc1 %s -Eonly -std=c23 -verify=undef-true
-
-#if __STDC_VERSION__ >= 202311L
-/* undef-true-no-diagnostics */
-#endif
+// RUN: %clang_cc1 %s -Eonly -std=c23 -verify=undef-true,c23-keyword
 
 #define FOO true
 #if FOO /* #1 */
@@ -27,7 +23,7 @@
 /* undef-true-warning@#3 {{'true' is not defined, evaluates to 0}} */
 #endif
 
-#define true 1
+#define true 1 /* c23-keyword-warning {{keyword is hidden by macro definition}} */
 
 #define FOO true
 #if FOO
diff --git a/clang/test/Sema/c++-keyword-in-c.c b/clang/test/Sema/c++-keyword-in-c.c
index f4174694411145..89b5edbabbd9cb 100644
--- a/clang/test/Sema/c++-keyword-in-c.c
+++ b/clang/test/Sema/c++-keyword-in-c.c
@@ -200,10 +200,10 @@ int import;
 int override;
 int final;
 
-// We do not diagnose use of C++ keywords when used as a macro name because
-// that does not conflict with C++ (the macros will be replaced before the
-// keyword is seen by the parser).
-#define this 12
+// In C mode, we do not diagnose use of C++ keywords when used as a macro name
+// because that does not conflict with C++ (the macros will be replaced before
+// the keyword is seen by the parser).
+#define this 12 // cxx-warning {{keyword is hidden by macro definition}}
 
 // FIXME: These tests are disabled for C++ because it causes a crash.
 // See GH114815.
diff --git a/clang/test/SemaCUDA/noinline.cu b/clang/test/SemaCUDA/noinline.cu
index ce4dfe35ceaa83..e8e45c127eda27 100644
--- a/clang/test/SemaCUDA/noinline.cu
+++ b/clang/test/SemaCUDA/noinline.cu
@@ -13,7 +13,7 @@ __attribute__((__noinline__)) void fun3() { }
 #define __noinline__ __attribute__((__noinline__))
 __noinline__ void fun5() {}
 
-#undef __noinline__
+#undef __noinline__ // pedantic-warning {{keyword or identifier with special meaning is used as a macro name}}
 #10 "cuda.h" 3 // pedantic-warning {{this style of line directive is a GNU extension}}
 #define __noinline__ __attribute__((__noinline__))
 __noinline__ void fun6() {}
diff --git a/clang/test/SemaCXX/abstract.cpp b/clang/test/SemaCXX/abstract.cpp
index 730d7e390f1d0f..70c972261853c4 100644
--- a/clang/test/SemaCXX/abstract.cpp
+++ b/clang/test/SemaCXX/abstract.cpp
@@ -4,6 +4,7 @@
 #define __CONCAT(__X, __Y) __CONCAT1(__X, __Y)
 #define __CONCAT1(__X, __Y) __X ## __Y
 
+// expected-warning at +1 {{keyword is hidden by macro definition}}
 #define static_assert(__b, __m) \
   typedef int __CONCAT(__sa, __LINE__)[__b ? 1 : -1]
 #endif
diff --git a/clang/test/SemaCXX/consteval-builtin.cpp b/clang/test/SemaCXX/consteval-builtin.cpp
index 3ba95b4dbd9b50..2bb0d43ac3557a 100644
--- a/clang/test/SemaCXX/consteval-builtin.cpp
+++ b/clang/test/SemaCXX/consteval-builtin.cpp
@@ -26,7 +26,7 @@
 // c-error at -4 {{does not have the constexpr builtin}}
 
 #if __cplusplus < 201103L
-#define static_assert __extension__ _Static_assert
+#define static_assert __extension__ _Static_assert // c-warning {{keyword is hidden by macro definition}}
 #define CONSTEXPR11
 #else
 #define CONSTEXPR11 constexpr
diff --git a/clang/test/SemaTemplate/instantiate-array.cpp b/clang/test/SemaTemplate/instantiate-array.cpp
index 41d1cfe138ab5c..f317be4d22bac3 100644
--- a/clang/test/SemaTemplate/instantiate-array.cpp
+++ b/clang/test/SemaTemplate/instantiate-array.cpp
@@ -1,10 +1,10 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11
-// expected-no-diagnostics
 
 #ifndef __GXX_EXPERIMENTAL_CXX0X__
 #define __CONCAT(__X, __Y) __CONCAT1(__X, __Y)
 #define __CONCAT1(__X, __Y) __X ## __Y
 
+// expected-warning at +1 {{keyword is hidden by macro definition}}
 #define static_assert(__b, __m) \
   typedef int __CONCAT(__sa, __LINE__)[__b ? 1 : -1]
 #endif
diff --git a/clang/www/cxx_status.html b/clang/www/cxx_status.html
index 7ec483b26520c6..ab5dd45d59cb8a 100755
--- a/clang/www/cxx_status.html
+++ b/clang/www/cxx_status.html
@@ -452,7 +452,19 @@ <h2 id="cxx26">C++2c implementation status</h2>
  <tr>
    <td>Preprocessing is never undefined</td>
    <td><a href="https://wg21.link/P2843">P2843R3</a></td>
-   <td class="none" align="center">No</td>
+   <td class="partial" align="center">
+    <details>
+      <summary>Clang 24 (Partial)</summary>
+      Error-by-default warning for #define keyword;
+      Pedantic warning for #undef keyword;
+      #line is covered by P4136R2;
+      For attributes, there has been no discussions - The conservative approach is to do nothing;
+      For define in macros, the discussion doesn't seem to have reached its course yet.
+
+      The changes in this paper may break some legacy code, so we have tried to minimize the breakage
+      while providing users with clear diagnostic information to avoid introducing new undefined behaviors.
+    </details>
+  </td>
  </tr>
  <!-- Kona, Fall 2025-->
  <tr>

>From 9a187d5d5b25fd65ff67414957208c7ba5e88cb4 Mon Sep 17 00:00:00 2001
From: yronglin <yronglin777 at gmail.com>
Date: Sun, 23 Aug 2026 23:10:55 -0700
Subject: [PATCH 2/6] Fix cwg3088

Signed-off-by: yronglin <yronglin777 at gmail.com>
---
 clang/test/CXX/drs/cwg3088.cpp | 188 ++++++++++++++++-----------------
 1 file changed, 94 insertions(+), 94 deletions(-)

diff --git a/clang/test/CXX/drs/cwg3088.cpp b/clang/test/CXX/drs/cwg3088.cpp
index 39c02dd31fc6c8..5dc5aaab708227 100644
--- a/clang/test/CXX/drs/cwg3088.cpp
+++ b/clang/test/CXX/drs/cwg3088.cpp
@@ -1,10 +1,10 @@
-// RUN: %clang_cc1 -std=c++98 -fexceptions -fcxx-exceptions -pedantic-errors -Wno-keyword-macro-undef %s -verify-directives -verify=expected
-// RUN: %clang_cc1 -std=c++11 -fexceptions -fcxx-exceptions -pedantic-errors -Wno-keyword-macro-undef %s -verify-directives -verify=expected,since-cxx11
-// RUN: %clang_cc1 -std=c++14 -fexceptions -fcxx-exceptions -pedantic-errors -Wno-keyword-macro-undef %s -verify-directives -verify=expected,since-cxx11
-// RUN: %clang_cc1 -std=c++17 -fexceptions -fcxx-exceptions -pedantic-errors -Wno-keyword-macro-undef %s -verify-directives -verify=expected,since-cxx11
-// RUN: %clang_cc1 -std=c++20 -fexceptions -fcxx-exceptions -pedantic-errors -Wno-keyword-macro-undef %s -verify-directives -verify=expected,since-cxx11,since-cxx20
-// RUN: %clang_cc1 -std=c++23 -fexceptions -fcxx-exceptions -pedantic-errors -Wno-keyword-macro-undef %s -verify-directives -verify=expected,since-cxx11,since-cxx20
-// RUN: %clang_cc1 -std=c++2c -fexceptions -fcxx-exceptions -pedantic-errors -Wno-keyword-macro-undef %s -verify-directives -verify=expected,since-cxx11,since-cxx20
+// RUN: %clang_cc1 -std=c++98 -fexceptions -fcxx-exceptions -pedantic-errors %s -verify-directives -verify=expected
+// RUN: %clang_cc1 -std=c++11 -fexceptions -fcxx-exceptions -pedantic-errors %s -verify-directives -verify=expected,since-cxx11
+// RUN: %clang_cc1 -std=c++14 -fexceptions -fcxx-exceptions -pedantic-errors %s -verify-directives -verify=expected,since-cxx11
+// RUN: %clang_cc1 -std=c++17 -fexceptions -fcxx-exceptions -pedantic-errors %s -verify-directives -verify=expected,since-cxx11
+// RUN: %clang_cc1 -std=c++20 -fexceptions -fcxx-exceptions -pedantic-errors %s -verify-directives -verify=expected,since-cxx11,since-cxx20
+// RUN: %clang_cc1 -std=c++23 -fexceptions -fcxx-exceptions -pedantic-errors %s -verify-directives -verify=expected,since-cxx11,since-cxx20
+// RUN: %clang_cc1 -std=c++2c -fexceptions -fcxx-exceptions -pedantic-errors %s -verify-directives -verify=expected,since-cxx11,since-cxx20
 
 // The list of keywords was taken fron [lex.key] for C++98, and the difference
 // between [lex.key] contents in N and N-1 revisions of the Standard otherwise.
@@ -16,317 +16,317 @@ namespace cwg3088 { // cwg3088: partial
 
 #define asm
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef asm
+#undef asm // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define asm()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define auto
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef auto
+#undef auto // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define auto()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define bool
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef bool
+#undef bool // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define bool()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define break
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef break
+#undef break // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define break()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define case
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef case
+#undef case // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define case()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define catch
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef catch
+#undef catch // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define catch()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define char
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef char
+#undef char // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define char()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define class
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef class
+#undef class // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define class()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define const
 // FIXME-error at -1 {{keyword is hidden by macro definition}}
-#undef const
+#undef const // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define const()
 // FIXME-error at -1 {{keyword is hidden by macro definition}}
 #define const_cast
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef const_cast
+#undef const_cast // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define const_cast()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define continue
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef continue
+#undef continue // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define continue()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define default
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef default
+#undef default // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define default()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define delete
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef delete
+#undef delete // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define delete()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define do
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef do
+#undef do // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define do()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define double
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef double
+#undef double // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define double()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define dynamic_cast
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef dynamic_cast
+#undef dynamic_cast // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define dynamic_cast()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define else
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef else
+#undef else // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define else()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define enum
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef enum
+#undef enum // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define enum()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define explicit
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef explicit
+#undef explicit // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define explicit()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define export
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef export
+#undef export // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define export()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define extern
 // FIXME-error at -1 {{keyword is hidden by macro definition}}
-#undef extern
+#undef extern // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define extern()
 // FIXME-error at -1 {{keyword is hidden by macro definition}}
 #define false
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef false
+#undef false // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define false()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define float
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef float
+#undef float // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define float()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define for
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef for
+#undef for // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define for()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define friend
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef friend
+#undef friend // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define friend()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define goto
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef goto
+#undef goto // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define goto()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define if
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef if
+#undef if // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define if()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define inline
 // FIXME-error at -1 {{keyword is hidden by macro definition}}
-#undef inline
+#undef inline // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define inline()
 // FIXME-error at -1 {{keyword is hidden by macro definition}}
 #define int
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef int
+#undef int // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define int()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define long
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef long
+#undef long // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define long()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define mutable
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef mutable
+#undef mutable // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define mutable()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define namespace
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef namespace
+#undef namespace // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define namespace()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define new
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef new
+#undef new // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define new()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define operator
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef operator
+#undef operator // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define operator()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define private
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef private
+#undef private // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define private()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define protected
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef protected
+#undef protected // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define protected()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define public
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef public
+#undef public // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define public()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define register
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef register
+#undef register // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define register()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define reinterpret_cast
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef reinterpret_cast
+#undef reinterpret_cast // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define reinterpret_cast()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define return
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef return
+#undef return // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define return()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define short
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef short
+#undef short // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define short()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define signed
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef signed
+#undef signed // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define signed()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define sizeof
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef sizeof
+#undef sizeof // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define sizeof()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define static
 // FIXME-error at -1 {{keyword is hidden by macro definition}}
-#undef static
+#undef static // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define static()
 // FIXME-error at -1 {{keyword is hidden by macro definition}}
 #define static_cast
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef static_cast
+#undef static_cast // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define static_cast()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define struct
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef struct
+#undef struct // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define struct()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define switch
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef switch
+#undef switch // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define switch()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define template
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef template
+#undef template // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define template()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define this
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef this
+#undef this // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define this()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define throw
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef throw
+#undef throw // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define throw()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define true
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef true
+#undef true // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define true()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define try
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef try
+#undef try // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define try()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define typedef
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef typedef
+#undef typedef // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define typedef()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define typeid
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef typeid
+#undef typeid // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define typeid()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define typename
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef typename
+#undef typename // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define typename()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define union
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef union
+#undef union // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define union()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define unsigned
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef unsigned
+#undef unsigned // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define unsigned()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define using
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef using
+#undef using // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define using()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define virtual
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef virtual
+#undef virtual // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define virtual()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define void
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef void
+#undef void // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define void()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define volatile
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef volatile
+#undef volatile // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define volatile()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define wchar_t
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef wchar_t
+#undef wchar_t // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define wchar_t()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 #define while
 // expected-error at -1 {{keyword is hidden by macro definition}}
-#undef while
+#undef while // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define while()
 // expected-error at -1 {{keyword is hidden by macro definition}}
 
@@ -404,52 +404,52 @@ namespace cwg3088 { // cwg3088: partial
 
 #define alignas
 // since-cxx11-error at -1 {{keyword is hidden by macro definition}}
-#undef alignas
+#undef alignas // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define alignas()
 // since-cxx11-error at -1 {{keyword is hidden by macro definition}}
 #define alignof
 // since-cxx11-error at -1 {{keyword is hidden by macro definition}}
-#undef alignof
+#undef alignof // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define alignof()
 // since-cxx11-error at -1 {{keyword is hidden by macro definition}}
 #define char16_t
 // since-cxx11-error at -1 {{keyword is hidden by macro definition}}
-#undef char16_t
+#undef char16_t // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define char16_t()
 // since-cxx11-error at -1 {{keyword is hidden by macro definition}}
 #define char32_t
 // since-cxx11-error at -1 {{keyword is hidden by macro definition}}
-#undef char32_t
+#undef char32_t // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define char32_t()
 // since-cxx11-error at -1 {{keyword is hidden by macro definition}}
 #define constexpr
 // since-cxx11-error at -1 {{keyword is hidden by macro definition}}
-#undef constexpr
+#undef constexpr // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define constexpr()
 // since-cxx11-error at -1 {{keyword is hidden by macro definition}}
 #define decltype
 // since-cxx11-error at -1 {{keyword is hidden by macro definition}}
-#undef decltype
+#undef decltype // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define decltype()
 // since-cxx11-error at -1 {{keyword is hidden by macro definition}}
 #define noexcept
 // since-cxx11-error at -1 {{keyword is hidden by macro definition}}
-#undef noexcept
+#undef noexcept // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define noexcept()
 // since-cxx11-error at -1 {{keyword is hidden by macro definition}}
 #define nullptr
 // since-cxx11-error at -1 {{keyword is hidden by macro definition}}
-#undef nullptr
+#undef nullptr // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define nullptr()
 // since-cxx11-error at -1 {{keyword is hidden by macro definition}}
 #define static_assert
 // since-cxx11-error at -1 {{keyword is hidden by macro definition}}
-#undef static_assert
+#undef static_assert // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define static_assert()
 // since-cxx11-error at -1 {{keyword is hidden by macro definition}}
 #define thread_local
 // since-cxx11-error at -1 {{keyword is hidden by macro definition}}
-#undef thread_local
+#undef thread_local // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define thread_local()
 // since-cxx11-error at -1 {{keyword is hidden by macro definition}}
 
@@ -457,12 +457,12 @@ namespace cwg3088 { // cwg3088: partial
 
 #define final
 // since-cxx11-error at -1 {{keyword is hidden by macro definition}}
-#undef final
+#undef final // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define final()
 // since-cxx11-error at -1 {{keyword is hidden by macro definition}}
 #define override
 // since-cxx11-error at -1 {{keyword is hidden by macro definition}}
-#undef override
+#undef override // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define override()
 // since-cxx11-error at -1 {{keyword is hidden by macro definition}}
 
@@ -511,42 +511,42 @@ namespace cwg3088 { // cwg3088: partial
 
 #define char8_t
 // since-cxx20-error at -1 {{keyword is hidden by macro definition}}
-#undef char8_t
+#undef char8_t // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define char8_t()
 // since-cxx20-error at -1 {{keyword is hidden by macro definition}}
 #define concept
 // since-cxx20-error at -1 {{keyword is hidden by macro definition}}
-#undef concept
+#undef concept // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define concept()
 // since-cxx20-error at -1 {{keyword is hidden by macro definition}}
 #define consteval
 // since-cxx20-error at -1 {{keyword is hidden by macro definition}}
-#undef consteval
+#undef consteval // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define consteval()
 // since-cxx20-error at -1 {{keyword is hidden by macro definition}}
 #define constinit
 // since-cxx20-error at -1 {{keyword is hidden by macro definition}}
-#undef constinit
+#undef constinit // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define constinit()
 // since-cxx20-error at -1 {{keyword is hidden by macro definition}}
 #define co_await
 // since-cxx20-error at -1 {{keyword is hidden by macro definition}}
-#undef co_await
+#undef co_await // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define co_await()
 // since-cxx20-error at -1 {{keyword is hidden by macro definition}}
 #define co_return
 // since-cxx20-error at -1 {{keyword is hidden by macro definition}}
-#undef co_return
+#undef co_return // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define co_return()
 // since-cxx20-error at -1 {{keyword is hidden by macro definition}}
 #define co_yield
 // since-cxx20-error at -1 {{keyword is hidden by macro definition}}
-#undef co_yield
+#undef co_yield // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define co_yield
 // since-cxx20-error at -1 {{keyword is hidden by macro definition}}
 #define requires
 // since-cxx20-error at -1 {{keyword is hidden by macro definition}}
-#undef requires
+#undef requires // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define requires()
 // since-cxx20-error at -1 {{keyword is hidden by macro definition}}
 
@@ -595,7 +595,7 @@ namespace cwg3088 { // cwg3088: partial
 
 #define contract_assert
 // FIXME-error at -1 {{keyword is hidden by macro definition}}
-#undef contract_assert
+#undef contract_assert // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define contract_assert()
 // FIXME-error at -1 {{keyword is hidden by macro definition}}
 
@@ -603,12 +603,12 @@ namespace cwg3088 { // cwg3088: partial
 
 #define post
 // FIXME-error at -1 {{keyword is hidden by macro definition}}
-#undef post
+#undef post // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define post()
 // FIXME-error at -1 {{keyword is hidden by macro definition}}
 #define pre
 // FIXME-error at -1 {{keyword is hidden by macro definition}}
-#undef pre
+#undef pre // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define pre()
 // FIXME-error at -1 {{keyword is hidden by macro definition}}
 
@@ -616,7 +616,7 @@ namespace cwg3088 { // cwg3088: partial
 
 #define indeterminate
 // FIXME-since-cxx26-error at -1 {{attribute is hidden by macro definition}}
-#undef indeterminate
+#undef indeterminate // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define indeterminate()
 // FIXME-since-cxx26-error at -1 {{attribute is hidden by macro definition}}
 #endif

>From f7cdc17dce40681500fc6b53b939961e9185cd8c Mon Sep 17 00:00:00 2001
From: yronglin <yronglin777 at gmail.com>
Date: Mon, 24 Aug 2026 03:33:40 -0700
Subject: [PATCH 3/6] Add import and other special meaning keyword

Signed-off-by: yronglin <yronglin777 at gmail.com>
---
 clang/lib/Lex/PPDirectives.cpp | 15 ++++++++++-----
 clang/test/CXX/drs/cwg3088.cpp |  4 ++--
 2 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 4bd61644348221..1c7a5266cdfb1c 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -105,7 +105,7 @@ SourceRange Preprocessor::DiscardUntilEndOfDirective(
 enum MacroDiag {
   MD_NoWarn,       //> Not a reserved identifier
   MD_KeywordDef,   //> Macro hides keyword, enabled by default
-  MD_KeywordUnDef, //> Undef keyword,  It is generally harmless and widely used,
+  MD_KeywordUndef, //> Undef keyword,  It is generally harmless and widely used,
                    //> enabled in pedantic mode.
   MD_ReservedMacro, //> #define of #undef reserved id, disabled by default
   MD_ReservedAttributeIdentifier
@@ -217,9 +217,14 @@ static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II) {
   const LangOptions &Lang = PP.getLangOpts();
   StringRef Text = II->getName();
   if (II->isKeyword(Lang))
-    return MD_KeywordUnDef;
-  if (Lang.CPlusPlus11 && (Text == "override" || Text == "final"))
-    return MD_KeywordUnDef;
+    return MD_KeywordUndef;
+
+  // [lex.name]/p2: Identifiers with special meaning
+  if ((Lang.CPlusPlus26 && (Text == "post" || Text == "pre")) ||
+      (Lang.CPlusPlus20 && (Text == "module" || Text == "import")) ||
+      (Lang.CPlusPlus11 && (Text == "override" || Text == "final")))
+    return MD_KeywordUndef;
+
   if (isReservedInAllContexts(II->isReserved(Lang)))
     return MD_ReservedMacro;
   if (isReservedCXXAttributeName(PP, II))
@@ -418,7 +423,7 @@ bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
       if (ShadowFlag)
         *ShadowFlag = true;
     }
-    if (D == MD_KeywordUnDef)
+    if (D == MD_KeywordUndef)
       Diag(MacroNameTok, diag::ext_pp_macro_name_is_keyword);
     if (D == MD_ReservedMacro)
       Diag(MacroNameTok, diag::warn_pp_macro_is_reserved_id);
diff --git a/clang/test/CXX/drs/cwg3088.cpp b/clang/test/CXX/drs/cwg3088.cpp
index 5dc5aaab708227..aeb7d6e11dd721 100644
--- a/clang/test/CXX/drs/cwg3088.cpp
+++ b/clang/test/CXX/drs/cwg3088.cpp
@@ -554,12 +554,12 @@ namespace cwg3088 { // cwg3088: partial
 
 #define import
 // FIXME-error at -1 {{keyword is hidden by macro definition}}
-#undef import
+#undef import // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define import()
 // FIXME-error at -1 {{keyword is hidden by macro definition}}
 #define module
 // FIXME-error at -1 {{keyword is hidden by macro definition}}
-#undef module
+#undef module // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define module()
 // FIXME-error at -1 {{keyword is hidden by macro definition}}
 

>From ca449a8120191925cafeddd6fdcdb0a4181ad712 Mon Sep 17 00:00:00 2001
From: yronglin <yronglin777 at gmail.com>
Date: Thu, 27 Aug 2026 00:42:30 -0700
Subject: [PATCH 4/6] Address review comments

Signed-off-by: yronglin <yronglin777 at gmail.com>
---
 clang/lib/Lex/PPDirectives.cpp | 18 +++++++++++++-----
 clang/www/cxx_status.html      |  6 ------
 2 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 1c7a5266cdfb1c..ab2fc9184d460c 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -416,20 +416,28 @@ bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
     D = shouldWarnOnMacroUndef(*this, II);
   if (D != MD_NoWarn && !SourceMgr.isInSystemHeader(MacroNameLoc) &&
       !SourceMgr.isInPredefinedFile(MacroNameLoc)) {
-    if (D == MD_KeywordDef) {
+    switch (D) {
+    default:
+      llvm_unreachable("Unexpected MacroDiag kind");
+      break;
+    case MD_KeywordDef:
       // We do not want to warn on some patterns widely used in configuration
       // scripts.  This requires analyzing next tokens, so do not issue warnings
       // now, only inform caller.
       if (ShadowFlag)
         *ShadowFlag = true;
-    }
-    if (D == MD_KeywordUndef)
+      break;
+    case MD_KeywordUndef:
       Diag(MacroNameTok, diag::ext_pp_macro_name_is_keyword);
-    if (D == MD_ReservedMacro)
+      break;
+    case MD_ReservedMacro:
       Diag(MacroNameTok, diag::warn_pp_macro_is_reserved_id);
-    if (D == MD_ReservedAttributeIdentifier)
+      break;
+    case MD_ReservedAttributeIdentifier:
       Diag(MacroNameTok, diag::warn_pp_macro_is_reserved_attribute_id)
           << II->getName();
+      break;
+    }
   }
 
   // Okay, we got a good identifier.
diff --git a/clang/www/cxx_status.html b/clang/www/cxx_status.html
index ab5dd45d59cb8a..3ceb9a38406f49 100755
--- a/clang/www/cxx_status.html
+++ b/clang/www/cxx_status.html
@@ -457,12 +457,6 @@ <h2 id="cxx26">C++2c implementation status</h2>
       <summary>Clang 24 (Partial)</summary>
       Error-by-default warning for #define keyword;
       Pedantic warning for #undef keyword;
-      #line is covered by P4136R2;
-      For attributes, there has been no discussions - The conservative approach is to do nothing;
-      For define in macros, the discussion doesn't seem to have reached its course yet.
-
-      The changes in this paper may break some legacy code, so we have tried to minimize the breakage
-      while providing users with clear diagnostic information to avoid introducing new undefined behaviors.
     </details>
   </td>
  </tr>

>From d3599032ee0b93b21495026bcbbf74cedff3e365 Mon Sep 17 00:00:00 2001
From: yronglin <yronglin777 at gmail.com>
Date: Tue, 10 Nov 2026 07:04:42 -0800
Subject: [PATCH 5/6] Handle the contextual keyword only if it's a real keyword

Signed-off-by: yronglin <yronglin777 at gmail.com>
---
 clang/docs/ReleaseNotes.md                    |  4 +-
 .../include/clang/Basic/DiagnosticLexKinds.td |  2 +-
 clang/include/clang/Lex/Preprocessor.h        | 11 +++
 clang/include/clang/Parse/Parser.h            |  8 ++
 clang/lib/Lex/PPDirectives.cpp                |  2 -
 clang/lib/Lex/Preprocessor.cpp                | 44 +++++++---
 clang/lib/Parse/ParseDeclCXX.cpp              | 82 +++++++++++++++++++
 clang/test/CXX/cpp/cpp.replace.general/p9.cpp | 52 ++++++++++--
 clang/test/CXX/drs/cwg3088.cpp                |  4 -
 .../Preprocessor/macro-reserved-cxx11.cpp     |  8 +-
 clang/test/Preprocessor/macro-reserved.c      | 16 ++--
 clang/test/Preprocessor/macro-reserved.cpp    | 16 ++--
 clang/test/Preprocessor/macro_expand.c        |  2 +-
 .../static_assert-already-defined.c           |  8 +-
 .../test/Preprocessor/warn-macro-undef-true.c |  2 +-
 clang/test/Sema/c++-keyword-in-c.c            |  2 +-
 clang/test/SemaCXX/abstract.cpp               |  2 +-
 clang/test/SemaCXX/consteval-builtin.cpp      |  2 +-
 clang/test/SemaTemplate/instantiate-array.cpp |  2 +-
 19 files changed, 214 insertions(+), 55 deletions(-)

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index cb51a2c31fd011..7f757054992316 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -795,8 +795,8 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
 - Added `-Wunsafe-buffer-usage-main-argv` as a diagnostic group under
   `-Wunsafe-buffer-usage` to control warnings on `main`'s `argv` parameter,
   allowing users to suppress them with `-Wno-unsafe-buffer-usage-main-argv`.
-- `-Wkeyword-macro` is now enabled by default for `#define` directives whose
-  macro name is a keyword or an identifier with special meaning. The
+- `-Wkeyword-macro` is now an error-by-default warning for `#define` directives
+  whose macro name is a keyword or an identifier with special meaning. The
   corresponding `#undef` diagnostic is not enabled by default; it is enabled by
   `-pedantic`, `-Wkeyword-macro`, or its more specific
   `-Wkeyword-macro-undef` subgroup.
diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td b/clang/include/clang/Basic/DiagnosticLexKinds.td
index f9abe2e65001c2..2a1d9004b13133 100644
--- a/clang/include/clang/Basic/DiagnosticLexKinds.td
+++ b/clang/include/clang/Basic/DiagnosticLexKinds.td
@@ -445,7 +445,7 @@ def note_pp_ambiguous_macro_chosen : Note<
 def note_pp_ambiguous_macro_other : Note<
   "other definition of %0">;
 def warn_pp_macro_hides_keyword : ExtWarn<
-  "keyword is hidden by macro definition">, InGroup<KeywordAsMacro>;
+  "keyword is hidden by macro definition">, InGroup<KeywordAsMacro>, DefaultError;
 def ext_pp_macro_name_is_keyword : Extension<
   "keyword or identifier with special meaning is used as a macro name">,
   InGroup<KeywordAsMacroUndef>;
diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h
index 2d0c24b92334a3..90d103d4b17bb5 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -750,6 +750,13 @@ class Preprocessor {
   SourceLocation MaxTokensOverrideLoc;
 
 public:
+  /// Diagnose the definition \p MI of a macro named like a contextual keyword
+  /// (e.g. 'final', 'module') that was expanded at \p ExpandLoc in a position
+  /// where the identifier would have its special meaning. Each definition is
+  /// diagnosed at most once.
+  void diagnoseContextualKeywordMacro(IdentifierInfo *II, const MacroInfo *MI,
+                                      SourceLocation ExpandLoc);
+
   struct PreambleSkipInfo {
     SourceLocation HashTokenLoc;
     SourceLocation IfTokenLoc;
@@ -893,6 +900,10 @@ class Preprocessor {
   };
   SmallVector<MacroExpandsInfo, 2> DelayedMacroExpandsCallbacks;
 
+  /// Macro definitions of contextual keywords that have already been diagnosed
+  /// by diagnoseContextualKeywordMacro, to avoid duplicate diagnostics.
+  llvm::DenseSet<const MacroInfo *> DiagnosedContextualKeywordMacros;
+
   /// Information about a name that has been used to define a module macro.
   struct FullModuleMacroInfo {
     /// The most recent macro directive for this identifier.
diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h
index e4fa25ebc427f6..3244799f4ce6a9 100644
--- a/clang/include/clang/Parse/Parser.h
+++ b/clang/include/clang/Parse/Parser.h
@@ -3191,6 +3191,14 @@ class Parser : public CodeCompletionHandler {
     return isCXX11VirtSpecifier(Tok);
   }
 
+  /// Diagnose a macro named after \p VS ('final' or 'override'; other
+  /// specifiers are ignored) that was expanded where the current token is, or
+  /// immediately before it, i.e. where that identifier would have been a
+  /// contextual keyword ([cpp.replace.general]). \p VS is the specifier the
+  /// grammar accepts at this position, not the current token, which the macro
+  /// has usually replaced.
+  void DiagnoseVirtSpecifierMacro(VirtSpecifiers::Specifier VS);
+
   /// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
   ///
   /// \verbatim
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index ab2fc9184d460c..e202df6cde7910 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -206,8 +206,6 @@ static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) {
     return isFeatureTestMacro(Text) ? MD_NoWarn : MD_ReservedMacro;
   if (II->isKeyword(Lang))
     return MD_KeywordDef;
-  if (Lang.CPlusPlus11 && (Text == "override" || Text == "final"))
-    return MD_KeywordDef;
   if (isReservedCXXAttributeName(PP, II))
     return MD_ReservedAttributeIdentifier;
   return MD_NoWarn;
diff --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp
index d3a56f726b7b85..a714308534f6f3 100644
--- a/clang/lib/Lex/Preprocessor.cpp
+++ b/clang/lib/Lex/Preprocessor.cpp
@@ -918,6 +918,23 @@ bool Preprocessor::HandleIdentifier(Token &Identifier) {
     HandlePoisonedIdentifier(Identifier);
   }
 
+  // Handle module contextual keywords. [cpp.pre]: whether 'module' or 'import'
+  // introduces a directive is decided before macro replacement, so a macro
+  // with that name is not expanded there. Defining such a macro is ill-formed
+  // ([cpp.replace.general]); diagnose it here, where it matters.
+  if (getLangOpts().CPlusPlusModules && CurLexer &&
+      !CurLexer->isLexingRawMode() && !CurLexer->isPragmaLexer() &&
+      !CurLexer->ParsingPreprocessorDirective &&
+      Identifier.isModuleContextualKeyword() &&
+      HandleModuleContextualKeyword(Identifier)) {
+    if (const MacroDefinition MD = getMacroDefinition(&II))
+      diagnoseContextualKeywordMacro(&II, MD.getMacroInfo(),
+                                     Identifier.getLocation());
+    HandleDirective(Identifier);
+    // With a fatal failure in the module loader, we abort parsing.
+    return hadModuleLoaderFatalFailure();
+  }
+
   // If this is a macro to be expanded, do it.
   if (const MacroDefinition MD = getMacroDefinition(&II)) {
     const auto *MI = MD.getMacroInfo();
@@ -963,17 +980,6 @@ bool Preprocessor::HandleIdentifier(Token &Identifier) {
   if (II.isExtensionToken() && !DisableMacroExpansion)
     Diag(Identifier, diag::ext_token_used);
 
-  // Handle module contextual keywords.
-  if (getLangOpts().CPlusPlusModules && CurLexer &&
-      !CurLexer->isLexingRawMode() && !CurLexer->isPragmaLexer() &&
-      !CurLexer->ParsingPreprocessorDirective &&
-      Identifier.isModuleContextualKeyword() &&
-      HandleModuleContextualKeyword(Identifier)) {
-    HandleDirective(Identifier);
-    // With a fatal failure in the module loader, we abort parsing.
-    return hadModuleLoaderFatalFailure();
-  }
-
   return true;
 }
 
@@ -1104,6 +1110,22 @@ void Preprocessor::Lex(Token &Result) {
   }
 }
 
+void Preprocessor::diagnoseContextualKeywordMacro(IdentifierInfo *II,
+                                                  const MacroInfo *MI,
+                                                  SourceLocation ExpandLoc) {
+  if (!MI)
+    return;
+  SourceLocation DefLoc = MI->getDefinitionLoc();
+  if (DefLoc.isInvalid() || SourceMgr.isInSystemHeader(DefLoc) ||
+      SourceMgr.isInPredefinedFile(DefLoc))
+    return;
+  if (!DiagnosedContextualKeywordMacros.insert(MI).second)
+    return;
+  Diag(DefLoc, diag::warn_pp_macro_hides_keyword);
+  if (ExpandLoc.isValid())
+    Diag(ExpandLoc, diag::note_macro_expansion_here) << II;
+}
+
 void Preprocessor::LexTokensUntilEOF(std::vector<Token> *Tokens) {
   while (1) {
     Token Tok;
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index d78a630d0805f8..7cf405a1e287e2 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -1912,6 +1912,9 @@ void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
   // If there are attributes after class name, parse them.
   MaybeParseCXX11Attributes(Attributes);
 
+  if (Name || TemplateId)
+    DiagnoseVirtSpecifierMacro(VirtSpecifiers::VS_Final);
+
   const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
   TagUseKind TUK;
 
@@ -2508,10 +2511,89 @@ VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
   return VirtSpecifiers::VS_None;
 }
 
+void Parser::DiagnoseVirtSpecifierMacro(VirtSpecifiers::Specifier VS) {
+  if (!getLangOpts().CPlusPlus11 ||
+      (VS != VirtSpecifiers::VS_Final && VS != VirtSpecifiers::VS_Override))
+    return;
+
+  IdentifierInfo *Kw =
+      PP.getIdentifierInfo(VirtSpecifiers::getSpecifierName(VS));
+
+  // Fast path: in almost all code the keyword was never #defined.
+  if (!Kw->hadMacroDefinition() || Tok.getLocation().isInvalid())
+    return;
+
+  const SourceManager &SM = PP.getSourceManager();
+  SourceLocation TokLoc = Tok.getLocation();
+  SourceLocation TokExpansionLoc = SM.getExpansionLoc(TokLoc);
+
+  // The macro expanded to at least one token and the current token came
+  // from that expansion, e.g. '#define final 1' or '#define final final'.
+  for (SourceLocation L = TokLoc; L.isMacroID();
+       L = SM.getImmediateMacroCallerLoc(L)) {
+    if (PP.getImmediateMacroName(L) != Kw->getName())
+      continue;
+    SourceLocation ExpandLoc = SM.getExpansionLoc(L);
+    const MacroInfo *MI =
+        PP.getMacroDefinitionAtLoc(Kw, ExpandLoc).getMacroInfo();
+    PP.diagnoseContextualKeywordMacro(Kw, MI ? MI : PP.getMacroInfo(Kw),
+                                      ExpandLoc);
+    return;
+  }
+
+  // The macro expanded to nothing, e.g. '#define final' or
+  // '#define final EMPTY'. The parser never saw the identifier, but it is
+  // the raw token immediately preceding the current one, either in the file
+  // or in the body of the macro that produced the current token.
+  const MacroInfo *MI =
+      PP.getMacroDefinitionAtLoc(Kw, TokExpansionLoc).getMacroInfo();
+  if (!MI)
+    return;
+  auto PrecedesTokAfter = [&](SourceLocation Loc, SourceLocation Lower) {
+    std::optional<Token> Prev = Lexer::findPreviousToken(
+        Loc, SM, getLangOpts(), /*IncludeComments=*/false);
+    // findPreviousToken may walk back across lines, so the token must lie
+    // strictly after Lower (the previous parsed token, or the start of the
+    // macro definition the current token was spelled in).
+    if (!Prev || Prev->isNot(tok::raw_identifier) ||
+        Prev->getRawIdentifier() != Kw->getName() ||
+        !SM.isBeforeInTranslationUnit(Lower, Prev->getLocation()))
+      return SourceLocation();
+    return Prev->getLocation();
+  };
+
+  // The keyword was written in the file, e.g. 'struct S final {'.
+  if (PrevTokLocation.isValid()) {
+    if (SourceLocation L = PrecedesTokAfter(
+            TokExpansionLoc, SM.getExpansionLoc(PrevTokLocation));
+        L.isValid())
+      return PP.diagnoseContextualKeywordMacro(Kw, MI, L);
+  }
+
+  // The keyword was written in the body of the macro that produced the
+  // current token, e.g. '#define DECL(n) struct n final {}'.
+  if (TokLoc.isMacroID() && !SM.isMacroArgExpansion(TokLoc)) {
+    StringRef Outer = PP.getImmediateMacroName(TokLoc);
+    const MacroInfo *OuterMI =
+        PP.getMacroDefinitionAtLoc(PP.getIdentifierInfo(Outer),
+                                   SM.getExpansionLoc(TokLoc))
+            .getMacroInfo();
+    if (OuterMI &&
+        PrecedesTokAfter(SM.getSpellingLoc(TokLoc), OuterMI->getDefinitionLoc())
+            .isValid())
+      PP.diagnoseContextualKeywordMacro(Kw, MI, TokExpansionLoc);
+  }
+}
+
 void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
                                                 bool IsInterface,
                                                 SourceLocation FriendLoc) {
   while (true) {
+    // Check before testing the current token: a macro named 'final' or
+    // 'override' has replaced the virt-specifier, so the current token is
+    // usually not one.
+    DiagnoseVirtSpecifierMacro(VirtSpecifiers::VS_Final);
+    DiagnoseVirtSpecifierMacro(VirtSpecifiers::VS_Override);
     VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
     if (Specifier == VirtSpecifiers::VS_None)
       return;
diff --git a/clang/test/CXX/cpp/cpp.replace.general/p9.cpp b/clang/test/CXX/cpp/cpp.replace.general/p9.cpp
index 7d775939063a26..921dd1bc142075 100644
--- a/clang/test/CXX/cpp/cpp.replace.general/p9.cpp
+++ b/clang/test/CXX/cpp/cpp.replace.general/p9.cpp
@@ -7,29 +7,69 @@
 // macro names lexically identical to keywords ([lex.key]) or to the identifiers
 // listed in Table 4.
 #define for 0
-// default-warning at -1 {{keyword is hidden by macro definition}}
-// pedantic-warning at -2 {{keyword is hidden by macro definition}}
+// default-error at -1 {{keyword is hidden by macro definition}}
+// pedantic-error at -2 {{keyword is hidden by macro definition}}
 // pedantic-errors-error at -3 {{keyword is hidden by macro definition}}
 #undef for
 // pedantic-warning at -1 {{keyword or identifier with special meaning is used as a macro name}}
 // pedantic-errors-error at -2 {{keyword or identifier with special meaning is used as a macro name}}
 
+#define final
+// default-error at -1 {{keyword is hidden by macro definition}}
+// pedantic-error at -2 {{keyword is hidden by macro definition}}
+// pedantic-errors-error at -3 {{keyword is hidden by macro definition}}
+struct S final {
+// default-note at -1 {{expansion of macro 'final' requested here}}
+// pedantic-note at -2 {{expansion of macro 'final' requested here}}
+// pedantic-errors-note at -3 {{expansion of macro 'final' requested here}}
+  virtual void f();
+};
+#undef final
+// pedantic-warning at -1 {{keyword or identifier with special meaning is used as a macro name}}
+// pedantic-errors-error at -2 {{keyword or identifier with special meaning is used as a macro name}}
+
 #define final 1
-// default-warning at -1 {{keyword is hidden by macro definition}}
-// pedantic-warning at -2 {{keyword is hidden by macro definition}}
+// default-error at -1 {{keyword is hidden by macro definition}}
+// pedantic-error at -2 {{keyword is hidden by macro definition}}
 // pedantic-errors-error at -3 {{keyword is hidden by macro definition}}
+struct S2 final {};
+// default-note at -1 {{expansion of macro 'final' requested here}}
+// pedantic-note at -2 {{expansion of macro 'final' requested here}}
+// pedantic-errors-note at -3 {{expansion of macro 'final' requested here}}
+// default-error at -4 {{expected unqualified-id}}
+// pedantic-error at -5 {{expected unqualified-id}}
+// pedantic-errors-error at -6 {{expected unqualified-id}}
+#undef final
+// pedantic-warning at -1 {{keyword or identifier with special meaning is used as a macro name}}
+// pedantic-errors-error at -2 {{keyword or identifier with special meaning is used as a macro name}}
+
+// Contextual keywords not used in a contextual keyword position do not warn on #define.
+#define final 1
+int not_contextual_final = final;
 #undef final
 // pedantic-warning at -1 {{keyword or identifier with special meaning is used as a macro name}}
 // pedantic-errors-error at -2 {{keyword or identifier with special meaning is used as a macro name}}
 
 #define override
-// default-warning at -1 {{keyword is hidden by macro definition}}
-// pedantic-warning at -2 {{keyword is hidden by macro definition}}
+// default-error at -1 {{keyword is hidden by macro definition}}
+// pedantic-error at -2 {{keyword is hidden by macro definition}}
 // pedantic-errors-error at -3 {{keyword is hidden by macro definition}}
+struct D : S {
+  void f() override;
+// default-note at -1 {{expansion of macro 'override' requested here}}
+// pedantic-note at -2 {{expansion of macro 'override' requested here}}
+// pedantic-errors-note at -3 {{expansion of macro 'override' requested here}}
+};
 #undef override
 // pedantic-warning at -1 {{keyword or identifier with special meaning is used as a macro name}}
 // pedantic-errors-error at -2 {{keyword or identifier with special meaning is used as a macro name}}
 
+#define module 1
+int module_var = module;
+#undef module
+// pedantic-warning at -1 {{keyword or identifier with special meaning is used as a macro name}}
+// pedantic-errors-error at -2 {{keyword or identifier with special meaning is used as a macro name}}
+
 // Empty definitions of qualifier keywords are accepted for compatibility with
 // configuration scripts, but #undef is still diagnosed in pedantic modes.
 #define const
diff --git a/clang/test/CXX/drs/cwg3088.cpp b/clang/test/CXX/drs/cwg3088.cpp
index aeb7d6e11dd721..45a645566d1ed5 100644
--- a/clang/test/CXX/drs/cwg3088.cpp
+++ b/clang/test/CXX/drs/cwg3088.cpp
@@ -456,15 +456,11 @@ namespace cwg3088 { // cwg3088: partial
 // Identifiers with special meaning introduced in C++11
 
 #define final
-// since-cxx11-error at -1 {{keyword is hidden by macro definition}}
 #undef final // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define final()
-// since-cxx11-error at -1 {{keyword is hidden by macro definition}}
 #define override
-// since-cxx11-error at -1 {{keyword is hidden by macro definition}}
 #undef override // expected-error {{keyword or identifier with special meaning is used as a macro name}}
 #define override()
-// since-cxx11-error at -1 {{keyword is hidden by macro definition}}
 
 // Attributes introduced in C++11
 
diff --git a/clang/test/Preprocessor/macro-reserved-cxx11.cpp b/clang/test/Preprocessor/macro-reserved-cxx11.cpp
index 6daea953e8a600..aa7d8558d5110d 100644
--- a/clang/test/Preprocessor/macro-reserved-cxx11.cpp
+++ b/clang/test/Preprocessor/macro-reserved-cxx11.cpp
@@ -1,8 +1,10 @@
 // RUN: %clang_cc1 -fsyntax-only -std=c++11 -pedantic -verify %s
 // RUN: %clang_cc1 -fsyntax-only -std=c++14 -pedantic -verify %s
 
-#define for 0    // expected-warning {{keyword is hidden by macro definition}}
-#define final 1  // expected-warning {{keyword is hidden by macro definition}}
-#define override // expected-warning {{keyword is hidden by macro definition}}
+#define for 0    // expected-error {{keyword is hidden by macro definition}}
+
+// final and override are contextual keyword, only diagnose if it's a real keyword.
+#define final 1
+#define override
 
 int x;
diff --git a/clang/test/Preprocessor/macro-reserved.c b/clang/test/Preprocessor/macro-reserved.c
index 875fce19513c6d..c02e270cc342f5 100644
--- a/clang/test/Preprocessor/macro-reserved.c
+++ b/clang/test/Preprocessor/macro-reserved.c
@@ -1,6 +1,6 @@
 // RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s
 
-#define for 0    // expected-warning {{keyword is hidden by macro definition}}
+#define for 0    // expected-error {{keyword is hidden by macro definition}}
 #define final 1
 #define __HAVE_X 0
 #define __cplusplus
@@ -41,23 +41,23 @@
 #define inline __inline__
 #undef  inline // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 
-#define inline inline__  // expected-warning {{keyword is hidden by macro definition}}
+#define inline inline__  // expected-error {{keyword is hidden by macro definition}}
 #undef  inline // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
-#define extern __inline  // expected-warning {{keyword is hidden by macro definition}}
+#define extern __inline  // expected-error {{keyword is hidden by macro definition}}
 #undef  extern // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
-#define extern __extern	 // expected-warning {{keyword is hidden by macro definition}}
+#define extern __extern	 // expected-error {{keyword is hidden by macro definition}}
 #undef  extern // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
-#define extern __extern__ // expected-warning {{keyword is hidden by macro definition}}
+#define extern __extern__ // expected-error {{keyword is hidden by macro definition}}
 #undef  extern // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 
-#define inline _inline   // expected-warning {{keyword is hidden by macro definition}}
+#define inline _inline   // expected-error {{keyword is hidden by macro definition}}
 #undef  inline // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
-#define volatile   // expected-warning {{keyword is hidden by macro definition}}
+#define volatile   // expected-error {{keyword is hidden by macro definition}}
 #undef  volatile // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 
 #pragma clang diagnostic warning "-Wreserved-macro-identifier"
 
-#define switch if  // expected-warning {{keyword is hidden by macro definition}}
+#define switch if  // expected-error {{keyword is hidden by macro definition}}
 #define final 1
 #define __clusplus // expected-warning {{macro name is a reserved identifier}}
 #define __HAVE_X 0 // expected-warning {{macro name is a reserved identifier}}
diff --git a/clang/test/Preprocessor/macro-reserved.cpp b/clang/test/Preprocessor/macro-reserved.cpp
index a90966dc180dd4..0f862c12e5d4e7 100644
--- a/clang/test/Preprocessor/macro-reserved.cpp
+++ b/clang/test/Preprocessor/macro-reserved.cpp
@@ -1,6 +1,6 @@
 // RUN: %clang_cc1 -fsyntax-only -verify -pedantic -std=c++98 %s
 
-#define for 0    // expected-warning {{keyword is hidden by macro definition}}
+#define for 0    // expected-error {{keyword is hidden by macro definition}}
 #define final 1
 #define __HAVE_X 0
 #define _HAVE_X 0
@@ -33,23 +33,23 @@
 #define inline __inline__
 #undef  inline // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 
-#define inline inline__  // expected-warning {{keyword is hidden by macro definition}}
+#define inline inline__  // expected-error {{keyword is hidden by macro definition}}
 #undef  inline // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
-#define extern __inline  // expected-warning {{keyword is hidden by macro definition}}
+#define extern __inline  // expected-error {{keyword is hidden by macro definition}}
 #undef  extern // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
-#define extern __extern	 // expected-warning {{keyword is hidden by macro definition}}
+#define extern __extern	 // expected-error {{keyword is hidden by macro definition}}
 #undef  extern // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
-#define extern __extern__ // expected-warning {{keyword is hidden by macro definition}}
+#define extern __extern__ // expected-error {{keyword is hidden by macro definition}}
 #undef  extern // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 
-#define inline _inline   // expected-warning {{keyword is hidden by macro definition}}
+#define inline _inline   // expected-error {{keyword is hidden by macro definition}}
 #undef  inline // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
-#define volatile   // expected-warning {{keyword is hidden by macro definition}}
+#define volatile   // expected-error {{keyword is hidden by macro definition}}
 #undef  volatile // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
 
 #pragma clang diagnostic warning "-Wreserved-macro-identifier"
 
-#define switch if  // expected-warning {{keyword is hidden by macro definition}}
+#define switch if  // expected-error {{keyword is hidden by macro definition}}
 #define final 1
 #define __HAVE_X 0 // expected-warning {{macro name is a reserved identifier}}
 #define _HAVE_X 0  // expected-warning {{macro name is a reserved identifier}}
diff --git a/clang/test/Preprocessor/macro_expand.c b/clang/test/Preprocessor/macro_expand.c
index 892c7a7188a639..0a0c3bb9f04e53 100644
--- a/clang/test/Preprocessor/macro_expand.c
+++ b/clang/test/Preprocessor/macro_expand.c
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -E %s | FileCheck --strict-whitespace %s
+// RUN: %clang_cc1 -E -Wno-keyword-macro %s | FileCheck --strict-whitespace %s
 
 #define X() Y
 #define Y() X
diff --git a/clang/test/Preprocessor/static_assert-already-defined.c b/clang/test/Preprocessor/static_assert-already-defined.c
index 1308e123100c84..5deabf95943795 100644
--- a/clang/test/Preprocessor/static_assert-already-defined.c
+++ b/clang/test/Preprocessor/static_assert-already-defined.c
@@ -1,7 +1,7 @@
-// RUN: %clang_cc1 -DFIRST_WAY -E -dM %s | FileCheck --strict-whitespace %s
-// RUN: %clang_cc1 -DFIRST_WAY -fms-compatibility -E -dM %s | FileCheck --strict-whitespace %s
-// RUN: %clang_cc1 -E -dM %s | FileCheck --strict-whitespace %s
-// RUN: %clang_cc1 -fms-compatibility -E -dM %s | FileCheck --strict-whitespace %s
+// RUN: %clang_cc1 -DFIRST_WAY -Wno-keyword-macro -E -dM %s | FileCheck --strict-whitespace %s
+// RUN: %clang_cc1 -DFIRST_WAY -Wno-keyword-macro -fms-compatibility -E -dM %s | FileCheck --strict-whitespace %s
+// RUN: %clang_cc1 -Wno-keyword-macro -E -dM %s | FileCheck --strict-whitespace %s
+// RUN: %clang_cc1 -Wno-keyword-macro -fms-compatibility -E -dM %s | FileCheck --strict-whitespace %s
 
 // If the assert macro is defined in MS compatibility mode in C, we
 // automatically inject a macro definition for static_assert. Test that the
diff --git a/clang/test/Preprocessor/warn-macro-undef-true.c b/clang/test/Preprocessor/warn-macro-undef-true.c
index e87b2d34f59cb9..7b3340211fa55b 100644
--- a/clang/test/Preprocessor/warn-macro-undef-true.c
+++ b/clang/test/Preprocessor/warn-macro-undef-true.c
@@ -23,7 +23,7 @@
 /* undef-true-warning@#3 {{'true' is not defined, evaluates to 0}} */
 #endif
 
-#define true 1 /* c23-keyword-warning {{keyword is hidden by macro definition}} */
+#define true 1 /* c23-keyword-error {{keyword is hidden by macro definition}} */
 
 #define FOO true
 #if FOO
diff --git a/clang/test/Sema/c++-keyword-in-c.c b/clang/test/Sema/c++-keyword-in-c.c
index 89b5edbabbd9cb..698b231c0b5908 100644
--- a/clang/test/Sema/c++-keyword-in-c.c
+++ b/clang/test/Sema/c++-keyword-in-c.c
@@ -203,7 +203,7 @@ int final;
 // In C mode, we do not diagnose use of C++ keywords when used as a macro name
 // because that does not conflict with C++ (the macros will be replaced before
 // the keyword is seen by the parser).
-#define this 12 // cxx-warning {{keyword is hidden by macro definition}}
+#define this 12 // cxx-error {{keyword is hidden by macro definition}}
 
 // FIXME: These tests are disabled for C++ because it causes a crash.
 // See GH114815.
diff --git a/clang/test/SemaCXX/abstract.cpp b/clang/test/SemaCXX/abstract.cpp
index 70c972261853c4..5625b8b0efe699 100644
--- a/clang/test/SemaCXX/abstract.cpp
+++ b/clang/test/SemaCXX/abstract.cpp
@@ -4,7 +4,7 @@
 #define __CONCAT(__X, __Y) __CONCAT1(__X, __Y)
 #define __CONCAT1(__X, __Y) __X ## __Y
 
-// expected-warning at +1 {{keyword is hidden by macro definition}}
+// expected-error at +1 {{keyword is hidden by macro definition}}
 #define static_assert(__b, __m) \
   typedef int __CONCAT(__sa, __LINE__)[__b ? 1 : -1]
 #endif
diff --git a/clang/test/SemaCXX/consteval-builtin.cpp b/clang/test/SemaCXX/consteval-builtin.cpp
index 2bb0d43ac3557a..ad4a50511a744c 100644
--- a/clang/test/SemaCXX/consteval-builtin.cpp
+++ b/clang/test/SemaCXX/consteval-builtin.cpp
@@ -26,7 +26,7 @@
 // c-error at -4 {{does not have the constexpr builtin}}
 
 #if __cplusplus < 201103L
-#define static_assert __extension__ _Static_assert // c-warning {{keyword is hidden by macro definition}}
+#define static_assert __extension__ _Static_assert // c-error {{keyword is hidden by macro definition}}
 #define CONSTEXPR11
 #else
 #define CONSTEXPR11 constexpr
diff --git a/clang/test/SemaTemplate/instantiate-array.cpp b/clang/test/SemaTemplate/instantiate-array.cpp
index f317be4d22bac3..a018b15641e30a 100644
--- a/clang/test/SemaTemplate/instantiate-array.cpp
+++ b/clang/test/SemaTemplate/instantiate-array.cpp
@@ -4,7 +4,7 @@
 #define __CONCAT(__X, __Y) __CONCAT1(__X, __Y)
 #define __CONCAT1(__X, __Y) __X ## __Y
 
-// expected-warning at +1 {{keyword is hidden by macro definition}}
+// expected-error at +1 {{keyword is hidden by macro definition}}
 #define static_assert(__b, __m) \
   typedef int __CONCAT(__sa, __LINE__)[__b ? 1 : -1]
 #endif

>From ef7829f4f6ed0d4efd6475f3872d12fc635d9faf Mon Sep 17 00:00:00 2001
From: yronglin <yronglin777 at gmail.com>
Date: Tue, 10 Nov 2026 20:51:02 -0800
Subject: [PATCH 6/6] Relax Non-std C++ keywords

Signed-off-by: yronglin <yronglin777 at gmail.com>
---
 .../checkers/misc/static-assert.cpp           |  4 +-
 clang/docs/ReleaseNotes.md                    | 13 ++++--
 .../include/clang/Basic/DiagnosticLexKinds.td |  2 +
 clang/include/clang/Basic/IdentifierTable.h   |  5 ++
 clang/lib/Basic/IdentifierTable.cpp           | 33 +++++++++++++
 clang/lib/Lex/PPDirectives.cpp                | 11 ++++-
 clang/test/C/drs/dr1xx.c                      |  2 +-
 .../p9-extension-keywords.cpp                 | 34 ++++++++++++++
 clang/test/Preprocessor/macro-reserved.c      | 46 +++++++++----------
 clang/test/Preprocessor/macro_expand.c        |  2 +-
 .../static_assert-already-defined.c           |  8 ++--
 .../test/Preprocessor/warn-macro-undef-true.c |  8 +++-
 clang/test/SemaCUDA/noinline.cu               |  2 +-
 clang/test/SemaCXX/consteval-builtin.cpp      |  2 +-
 14 files changed, 130 insertions(+), 42 deletions(-)
 create mode 100644 clang/test/CXX/cpp/cpp.replace.general/p9-extension-keywords.cpp

diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/static-assert.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/static-assert.cpp
index f11069fe5983d4..7e053651271549 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/static-assert.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/static-assert.cpp
@@ -1,5 +1,5 @@
-// RUN: %check_clang_tidy -std=c++11,c++14 -check-suffixes=,CXX11 %s misc-static-assert %t
-// RUN: %check_clang_tidy -std=c++17-or-later -check-suffixes=,CXX17 %s misc-static-assert %t
+// RUN: %check_clang_tidy -std=c++11,c++14 -check-suffixes=,CXX11 %s misc-static-assert %t -- -- -Wno-keyword-macro
+// RUN: %check_clang_tidy -std=c++17-or-later -check-suffixes=,CXX17 %s misc-static-assert %t -- -- -Wno-keyword-macro
 
 void abort() {}
 #ifdef NDEBUG
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 7f757054992316..fd453915fff228 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -795,11 +795,14 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
 - Added `-Wunsafe-buffer-usage-main-argv` as a diagnostic group under
   `-Wunsafe-buffer-usage` to control warnings on `main`'s `argv` parameter,
   allowing users to suppress them with `-Wno-unsafe-buffer-usage-main-argv`.
-- `-Wkeyword-macro` is now an error-by-default warning for `#define` directives
-  whose macro name is a keyword or an identifier with special meaning. The
-  corresponding `#undef` diagnostic is not enabled by default; it is enabled by
-  `-pedantic`, `-Wkeyword-macro`, or its more specific
-  `-Wkeyword-macro-undef` subgroup.
+- In C++, `-Wkeyword-macro` is now an error-by-default warning for `#define`
+  directives whose macro name is a C++ keyword or an identifier with
+  special meaning. The corresponding `#undef` diagnostic is not enabled by
+  default; it is enabled by `-pedantic`, `-Wkeyword-macro`, or its more
+  specific `-Wkeyword-macro-undef` subgroup. Keywords that are only enabled by
+  an extension (such as `typeof` in GNU mode), C keywords such as
+  `_Static_assert`, and all keywords in C keep the previous pedantic-only
+  diagnostic.
 
 
 - `-Wno-unsafe-buffer-usage-in-libc-call` no longer turns off
diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td b/clang/include/clang/Basic/DiagnosticLexKinds.td
index 2a1d9004b13133..72ed85a49e8ca3 100644
--- a/clang/include/clang/Basic/DiagnosticLexKinds.td
+++ b/clang/include/clang/Basic/DiagnosticLexKinds.td
@@ -446,6 +446,8 @@ def note_pp_ambiguous_macro_other : Note<
   "other definition of %0">;
 def warn_pp_macro_hides_keyword : ExtWarn<
   "keyword is hidden by macro definition">, InGroup<KeywordAsMacro>, DefaultError;
+def ext_pp_macro_hides_keyword : Extension<
+  "keyword is hidden by macro definition">, InGroup<KeywordAsMacro>;
 def ext_pp_macro_name_is_keyword : Extension<
   "keyword or identifier with special meaning is used as a macro name">,
   InGroup<KeywordAsMacroUndef>;
diff --git a/clang/include/clang/Basic/IdentifierTable.h b/clang/include/clang/Basic/IdentifierTable.h
index ac1acb7ddac61b..45460d6b7180e2 100644
--- a/clang/include/clang/Basic/IdentifierTable.h
+++ b/clang/include/clang/Basic/IdentifierTable.h
@@ -478,6 +478,11 @@ class alignas(IdentifierInfoAlignment) IdentifierInfo {
   /// language.
   bool isCPlusPlusKeyword(const LangOptions &LangOpts) const;
 
+  /// Return true if this token is a keyword of standard C++ ([lex.key]) in
+  /// the specified language, as opposed to a keyword that is only enabled by
+  /// an extension (GNU, Microsoft, OpenCL, HLSL, ...).
+  bool isStandardCPlusPlusKeyword(const LangOptions &LangOpts) const;
+
   /// Get and set FETokenInfo. The language front-end is allowed to associate
   /// arbitrary metadata with this token.
   void *getFETokenInfo() const { return FETokenInfo; }
diff --git a/clang/lib/Basic/IdentifierTable.cpp b/clang/lib/Basic/IdentifierTable.cpp
index aadf224211508c..afd601a5afc08f 100644
--- a/clang/lib/Basic/IdentifierTable.cpp
+++ b/clang/lib/Basic/IdentifierTable.cpp
@@ -345,6 +345,39 @@ bool IdentifierInfo::isCPlusPlusKeyword(const LangOptions &LangOpts) const {
   return !isKeyword(LangOptsNoCPP);
 }
 
+/// Returns true if the identifier is a standard C++ keyword in the specified
+/// language.
+bool IdentifierInfo::isStandardCPlusPlusKeyword(
+    const LangOptions &LangOpts) const {
+  if (!LangOpts.CPlusPlus || !isKeyword(LangOpts))
+    return false;
+  // No standard C++ keyword starts with an underscore; such keywords (e.g.
+  // '_Static_assert', '__inline') are C keywords or extensions.
+  if (getName().starts_with("_"))
+    return false;
+  // Keyword aliases (e.g. OpenCL 'kernel' for '__kernel') are never standard.
+  tok::TokenKind K = getTokenID();
+  const char *Spelling = tok::getKeywordSpelling(K);
+  if (!Spelling || getName() != Spelling)
+    return false;
+  // Check the keyword against a strictly conforming C++ configuration of the
+  // same language version, with every keyword-enabling extension turned off.
+  LangOptions StdOpts;
+  StdOpts.CPlusPlus = true;
+  StdOpts.CPlusPlus11 = LangOpts.CPlusPlus11;
+  StdOpts.CPlusPlus14 = LangOpts.CPlusPlus14;
+  StdOpts.CPlusPlus17 = LangOpts.CPlusPlus17;
+  StdOpts.CPlusPlus20 = LangOpts.CPlusPlus20;
+  StdOpts.CPlusPlus23 = LangOpts.CPlusPlus23;
+  StdOpts.CPlusPlus26 = LangOpts.CPlusPlus26;
+  StdOpts.Bool = true;
+  StdOpts.WChar = true;
+  StdOpts.Char8 = LangOpts.CPlusPlus20;
+  StdOpts.Coroutines = LangOpts.CPlusPlus20;
+  StdOpts.GNUKeywords = false;
+  return getTokenKwStatus(StdOpts, K) == KS_Enabled;
+}
+
 ReservedIdentifierStatus
 IdentifierInfo::isReserved(const LangOptions &LangOpts) const {
   StringRef Name = getName();
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index e202df6cde7910..13848eab8523fb 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -214,7 +214,9 @@ static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) {
 static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II) {
   const LangOptions &Lang = PP.getLangOpts();
   StringRef Text = II->getName();
-  if (II->isKeyword(Lang))
+  // Undefining a C keyword, or a keyword that is only enabled by an extension,
+  // is generally harmless and widely used.
+  if (II->isStandardCPlusPlusKeyword(Lang))
     return MD_KeywordUndef;
 
   // [lex.name]/p2: Identifiers with special meaning
@@ -3353,7 +3355,12 @@ void Preprocessor::HandleDefineDirective(
 
   if (MacroShadowsKeyword &&
       !isConfigurationPattern(MacroNameTok, MI, getLangOpts())) {
-    Diag(MacroNameTok, diag::warn_pp_macro_hides_keyword);
+    // [cpp.replace.general]p9: A translation unit shall not #define a macro
+    // name lexically identical to a keyword ([lex.key]). C keywords and
+    // keywords enabled by extensions keep the pedantic warning.
+    Diag(MacroNameTok, II->isStandardCPlusPlusKeyword(getLangOpts())
+                           ? diag::warn_pp_macro_hides_keyword
+                           : diag::ext_pp_macro_hides_keyword);
   }
   // Check that there is no paste (##) operator at the beginning or end of the
   // replacement list.
diff --git a/clang/test/C/drs/dr1xx.c b/clang/test/C/drs/dr1xx.c
index 1704aa0a361d32..055a30cc9c4b8a 100644
--- a/clang/test/C/drs/dr1xx.c
+++ b/clang/test/C/drs/dr1xx.c
@@ -160,7 +160,7 @@ void dr106(void *p, int i) {
 void dr108(void) {
 #define const
   const int i = 12;
-#undef const /* expected-warning {{keyword or identifier with special meaning is used as a macro name}} */
+#undef const
   const int j = 12; /* expected-note {{variable 'j' declared const here}} */
 
   i = 100; /* Okay, the keyword was hidden by the macro. */
diff --git a/clang/test/CXX/cpp/cpp.replace.general/p9-extension-keywords.cpp b/clang/test/CXX/cpp/cpp.replace.general/p9-extension-keywords.cpp
new file mode 100644
index 00000000000000..b3a73a1a0f8450
--- /dev/null
+++ b/clang/test/CXX/cpp/cpp.replace.general/p9-extension-keywords.cpp
@@ -0,0 +1,34 @@
+// RUN: %clang_cc1 -std=gnu++26 -fms-extensions -fsyntax-only -verify=default %s
+// RUN: %clang_cc1 -std=gnu++26 -fms-extensions -pedantic -fsyntax-only -verify=pedantic %s
+// RUN: %clang_cc1 -std=gnu++26 -fms-extensions -pedantic-errors -fsyntax-only -verify=pedantic-errors %s
+
+// [cpp.replace.general]/p9 only covers the keywords listed in [lex.key].
+// Keywords that are enabled by an extension keep the pedantic-only diagnostic,
+// and #undef of them is not diagnosed.
+
+#define typeof decltype
+// pedantic-warning at -1 {{keyword is hidden by macro definition}}
+// pedantic-errors-error at -2 {{keyword is hidden by macro definition}}
+#undef typeof
+
+#define _cdecl
+// pedantic-warning at -1 {{keyword is hidden by macro definition}}
+// pedantic-errors-error at -2 {{keyword is hidden by macro definition}}
+#undef _cdecl
+
+// C keywords that clang accepts in C++ are reserved identifiers and are not
+// diagnosed as keywords.
+#define _Static_assert static_assert
+#undef _Static_assert
+#define _Alignof alignof
+#undef _Alignof
+
+// Standard C++ keywords are still ill-formed as macro names in GNU mode.
+#define decltype typeof
+// default-error at -1 {{keyword is hidden by macro definition}}
+// pedantic-error at -2 {{keyword is hidden by macro definition}}
+// pedantic-errors-error at -3 {{keyword is hidden by macro definition}}
+#undef decltype
+// pedantic-warning at -1 {{keyword or identifier with special meaning is used as a macro name}}
+// pedantic-errors-error at -2 {{keyword or identifier with special meaning is used as a macro name}}
+
diff --git a/clang/test/Preprocessor/macro-reserved.c b/clang/test/Preprocessor/macro-reserved.c
index c02e270cc342f5..6026a9f60730e7 100644
--- a/clang/test/Preprocessor/macro-reserved.c
+++ b/clang/test/Preprocessor/macro-reserved.c
@@ -1,6 +1,6 @@
 // RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s
 
-#define for 0    // expected-error {{keyword is hidden by macro definition}}
+#define for 0    // expected-warning {{keyword is hidden by macro definition}}
 #define final 1
 #define __HAVE_X 0
 #define __cplusplus
@@ -9,7 +9,7 @@
 #define __STDC__ 1 // expected-warning {{redefining builtin macro}}
 #define __clang__ 1
 
-#undef for // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
+#undef for
 #undef final
 #undef __HAVE_X
 #undef __cplusplus
@@ -30,41 +30,41 @@
 #define extern
 #define inline
 
-#undef while // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
-#undef const // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
-#undef static // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
-#undef extern // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
-#undef inline // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
+#undef while
+#undef const
+#undef static
+#undef extern
+#undef inline
 
 #define inline __inline
-#undef  inline // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
+#undef  inline
 #define inline __inline__
-#undef  inline // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
+#undef  inline
 
-#define inline inline__  // expected-error {{keyword is hidden by macro definition}}
-#undef  inline // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
-#define extern __inline  // expected-error {{keyword is hidden by macro definition}}
-#undef  extern // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
-#define extern __extern	 // expected-error {{keyword is hidden by macro definition}}
-#undef  extern // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
-#define extern __extern__ // expected-error {{keyword is hidden by macro definition}}
-#undef  extern // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
+#define inline inline__  // expected-warning {{keyword is hidden by macro definition}}
+#undef  inline
+#define extern __inline  // expected-warning {{keyword is hidden by macro definition}}
+#undef  extern
+#define extern __extern	 // expected-warning {{keyword is hidden by macro definition}}
+#undef  extern
+#define extern __extern__ // expected-warning {{keyword is hidden by macro definition}}
+#undef  extern
 
-#define inline _inline   // expected-error {{keyword is hidden by macro definition}}
-#undef  inline // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
-#define volatile   // expected-error {{keyword is hidden by macro definition}}
-#undef  volatile // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
+#define inline _inline   // expected-warning {{keyword is hidden by macro definition}}
+#undef  inline
+#define volatile   // expected-warning {{keyword is hidden by macro definition}}
+#undef  volatile
 
 #pragma clang diagnostic warning "-Wreserved-macro-identifier"
 
-#define switch if  // expected-error {{keyword is hidden by macro definition}}
+#define switch if  // expected-warning {{keyword is hidden by macro definition}}
 #define final 1
 #define __clusplus // expected-warning {{macro name is a reserved identifier}}
 #define __HAVE_X 0 // expected-warning {{macro name is a reserved identifier}}
 #define _HAVE_X 0  // expected-warning {{macro name is a reserved identifier}}
 #define X__Y
 
-#undef switch // expected-warning {{keyword or identifier with special meaning is used as a macro name}}
+#undef switch
 #undef final
 #undef __cplusplus // expected-warning {{macro name is a reserved identifier}}
 #undef _HAVE_X     // expected-warning {{macro name is a reserved identifier}}
diff --git a/clang/test/Preprocessor/macro_expand.c b/clang/test/Preprocessor/macro_expand.c
index 0a0c3bb9f04e53..892c7a7188a639 100644
--- a/clang/test/Preprocessor/macro_expand.c
+++ b/clang/test/Preprocessor/macro_expand.c
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -E -Wno-keyword-macro %s | FileCheck --strict-whitespace %s
+// RUN: %clang_cc1 -E %s | FileCheck --strict-whitespace %s
 
 #define X() Y
 #define Y() X
diff --git a/clang/test/Preprocessor/static_assert-already-defined.c b/clang/test/Preprocessor/static_assert-already-defined.c
index 5deabf95943795..1308e123100c84 100644
--- a/clang/test/Preprocessor/static_assert-already-defined.c
+++ b/clang/test/Preprocessor/static_assert-already-defined.c
@@ -1,7 +1,7 @@
-// RUN: %clang_cc1 -DFIRST_WAY -Wno-keyword-macro -E -dM %s | FileCheck --strict-whitespace %s
-// RUN: %clang_cc1 -DFIRST_WAY -Wno-keyword-macro -fms-compatibility -E -dM %s | FileCheck --strict-whitespace %s
-// RUN: %clang_cc1 -Wno-keyword-macro -E -dM %s | FileCheck --strict-whitespace %s
-// RUN: %clang_cc1 -Wno-keyword-macro -fms-compatibility -E -dM %s | FileCheck --strict-whitespace %s
+// RUN: %clang_cc1 -DFIRST_WAY -E -dM %s | FileCheck --strict-whitespace %s
+// RUN: %clang_cc1 -DFIRST_WAY -fms-compatibility -E -dM %s | FileCheck --strict-whitespace %s
+// RUN: %clang_cc1 -E -dM %s | FileCheck --strict-whitespace %s
+// RUN: %clang_cc1 -fms-compatibility -E -dM %s | FileCheck --strict-whitespace %s
 
 // If the assert macro is defined in MS compatibility mode in C, we
 // automatically inject a macro definition for static_assert. Test that the
diff --git a/clang/test/Preprocessor/warn-macro-undef-true.c b/clang/test/Preprocessor/warn-macro-undef-true.c
index 7b3340211fa55b..9a64d577a96ed4 100644
--- a/clang/test/Preprocessor/warn-macro-undef-true.c
+++ b/clang/test/Preprocessor/warn-macro-undef-true.c
@@ -2,7 +2,11 @@
 // RUN: %clang_cc1 %s -Eonly -std=c99 -verify=undef-true
 // RUN: %clang_cc1 %s -Eonly -std=c11 -verify=undef-true
 // RUN: %clang_cc1 %s -Eonly -std=c17 -verify=undef-true
-// RUN: %clang_cc1 %s -Eonly -std=c23 -verify=undef-true,c23-keyword
+// RUN: %clang_cc1 %s -Eonly -std=c23 -verify=undef-true
+
+#if __STDC_VERSION__ >= 202311L
+/* undef-true-no-diagnostics */
+#endif
 
 #define FOO true
 #if FOO /* #1 */
@@ -23,7 +27,7 @@
 /* undef-true-warning@#3 {{'true' is not defined, evaluates to 0}} */
 #endif
 
-#define true 1 /* c23-keyword-error {{keyword is hidden by macro definition}} */
+#define true 1
 
 #define FOO true
 #if FOO
diff --git a/clang/test/SemaCUDA/noinline.cu b/clang/test/SemaCUDA/noinline.cu
index e8e45c127eda27..ce4dfe35ceaa83 100644
--- a/clang/test/SemaCUDA/noinline.cu
+++ b/clang/test/SemaCUDA/noinline.cu
@@ -13,7 +13,7 @@ __attribute__((__noinline__)) void fun3() { }
 #define __noinline__ __attribute__((__noinline__))
 __noinline__ void fun5() {}
 
-#undef __noinline__ // pedantic-warning {{keyword or identifier with special meaning is used as a macro name}}
+#undef __noinline__
 #10 "cuda.h" 3 // pedantic-warning {{this style of line directive is a GNU extension}}
 #define __noinline__ __attribute__((__noinline__))
 __noinline__ void fun6() {}
diff --git a/clang/test/SemaCXX/consteval-builtin.cpp b/clang/test/SemaCXX/consteval-builtin.cpp
index ad4a50511a744c..3ba95b4dbd9b50 100644
--- a/clang/test/SemaCXX/consteval-builtin.cpp
+++ b/clang/test/SemaCXX/consteval-builtin.cpp
@@ -26,7 +26,7 @@
 // c-error at -4 {{does not have the constexpr builtin}}
 
 #if __cplusplus < 201103L
-#define static_assert __extension__ _Static_assert // c-error {{keyword is hidden by macro definition}}
+#define static_assert __extension__ _Static_assert
 #define CONSTEXPR11
 #else
 #define CONSTEXPR11 constexpr



More information about the cfe-commits mailing list