[clang] [clang][Lex] Diagnose keywords in macro expansions (PR #242626)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 14 04:00:39 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Chen Miao (ChenMiaoi)
<details>
<summary>Changes</summary>
Macro expansion bypasses the keyword diagnostics performed by `HandleIdentifier()`, suppressing compatibility and extension warnings.
Share these diagnostics with the fast path. Defer diagnostics for macro arguments until rescanning to avoid duplicate warnings and warnings for discarded tokens.
For example, this C++98 code now produces a compatibility warning:
``` cc
int constexpr;
```
``` cc
<stdin>:2:5: warning: 'constexpr' is a keyword in C++11 [-Wc++11-compat]
2 | int constexpr;
| ^
<stdin>:1:19: note: expanded from macro 'constexpr'
1 | #define constexpr constexpr
| ^
1 warning generated.
```
---
Full diff: https://github.com/llvm/llvm-project/pull/242626.diff
5 Files Affected:
- (modified) clang/docs/ReleaseNotes.md (+4)
- (modified) clang/include/clang/Lex/Preprocessor.h (+3)
- (modified) clang/lib/Lex/PPMacroExpansion.cpp (+3)
- (modified) clang/lib/Lex/Preprocessor.cpp (+31-23)
- (modified) clang/test/Preprocessor/extension-warning.c (+45-2)
``````````diff
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index e805697aaee655..d1b8b17d4603fd 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -511,6 +511,10 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
### Improvements to Clang's diagnostics
+- Fixed missing keyword compatibility and extension warnings in single-token
+ macro expansions, and duplicate or spurious warnings from macro argument
+ expansion.
+
- Fixed `-Wheader-guard` warnings pointing at `if` instead of the macro name
in `#if !defined(...)` header guards.
diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h
index 8d15ed7e8afd85..323e1c4118cde7 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -2382,6 +2382,9 @@ class Preprocessor {
IdentifierInfo *LookUpIdentifierInfo(Token &Identifier) const;
private:
+ /// Diagnose keyword compatibility and extension uses.
+ void diagnoseKeywordUsage(const Token &Identifier, IdentifierInfo &II);
+
llvm::DenseMap<IdentifierInfo*,unsigned> PoisonReasons;
public:
diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp
index bce83b27298f88..4723445822c380 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -570,6 +570,9 @@ bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
if (NewMI != MI || MI->isFunctionLike())
Diag(Identifier, diag::pp_disabled_macro_expansion);
}
+ // Fast expansion bypasses TokenLexer and its call to HandleIdentifier.
+ if (NewII->isHandleIdentifierCase())
+ diagnoseKeywordUsage(Identifier, *NewII);
}
// Since this is not an identifier token, it can't be macro expanded, so
diff --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp
index d3a56f726b7b85..83cc78846bc3bb 100644
--- a/clang/lib/Lex/Preprocessor.cpp
+++ b/clang/lib/Lex/Preprocessor.cpp
@@ -879,6 +879,33 @@ void Preprocessor::updateOutOfDateIdentifier(const IdentifierInfo &II) const {
getExternalSource()->updateOutOfDateIdentifier(II);
}
+void Preprocessor::diagnoseKeywordUsage(const Token &Identifier,
+ IdentifierInfo &II) {
+ // Diagnose pre-expanded arguments when they are rescanned after substitution,
+ // since the tokens may be discarded by a later macro expansion.
+ if (InMacroArgPreExpansion)
+ return;
+
+ // If this identifier is a keyword in a newer Standard or proposed Standard,
+ // produce a warning.
+ if (II.isFutureCompatKeyword()) {
+ Diag(Identifier,
+ getIdentifierTable().getFutureCompatDiagKind(II, getLangOpts()))
+ << II.getName();
+ // Don't diagnose this keyword again in this translation unit.
+ II.setIsFutureCompatKeyword(false);
+ }
+
+ // If this identifier would be a keyword in C++, diagnose as a compatibility
+ // issue.
+ if (II.IsKeywordInCPlusPlus())
+ Diag(Identifier, diag::warn_pp_identifier_is_cpp_keyword) << &II;
+
+ // If this is an extension token, diagnose its use.
+ if (II.isExtensionToken())
+ Diag(Identifier, diag::ext_token_used);
+}
+
/// HandleIdentifier - This callback is invoked when the lexer reads an
/// identifier. This callback looks up the identifier in the map and/or
/// potentially macro expands it or turns it into a named token (like 'for').
@@ -939,29 +966,10 @@ bool Preprocessor::HandleIdentifier(Token &Identifier) {
}
}
- // If this identifier is a keyword in a newer Standard or proposed Standard,
- // produce a warning. Don't warn if we're not considering macro expansion,
- // since this identifier might be the name of a macro.
- // FIXME: This warning is disabled in cases where it shouldn't be, like
- // "#define constexpr constexpr", "int constexpr;"
- if (II.isFutureCompatKeyword() && !DisableMacroExpansion) {
- Diag(Identifier, getIdentifierTable().getFutureCompatDiagKind(II, getLangOpts()))
- << II.getName();
- // Don't diagnose this keyword again in this translation unit.
- II.setIsFutureCompatKeyword(false);
- }
-
- // If this identifier would be a keyword in C++, diagnose as a compatibility
- // issue.
- if (II.IsKeywordInCPlusPlus() && !DisableMacroExpansion)
- Diag(Identifier, diag::warn_pp_identifier_is_cpp_keyword) << &II;
-
- // If this is an extension token, diagnose its use.
- // We avoid diagnosing tokens that originate from macro definitions.
- // FIXME: This warning is disabled in cases where it shouldn't be,
- // like "#define TY typeof", "TY(1) x".
- if (II.isExtensionToken() && !DisableMacroExpansion)
- Diag(Identifier, diag::ext_token_used);
+ // Don't warn while reading unexpanded tokens, since this identifier might
+ // be the name of a macro.
+ if (!DisableMacroExpansion)
+ diagnoseKeywordUsage(Identifier, II);
// Handle module contextual keywords.
if (getLangOpts().CPlusPlusModules && CurLexer &&
diff --git a/clang/test/Preprocessor/extension-warning.c b/clang/test/Preprocessor/extension-warning.c
index 5fe905c8435dfd..0dac798c593872 100644
--- a/clang/test/Preprocessor/extension-warning.c
+++ b/clang/test/Preprocessor/extension-warning.c
@@ -1,4 +1,11 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -pedantic %s
+// RUN: %clang_cc1 -fsyntax-only -verify -pedantic -Wc++-keyword %s
+// RUN: %clang_cc1 -x c++ -std=c++98 -fsyntax-only -verify -Wc++11-compat %s
+
+#define ID(x) x
+#define DISCARD(x)
+#define EXPAND_AND_DISCARD(x) DISCARD(x)
+
+#ifndef __cplusplus
// The preprocessor shouldn't warn about extensions within macro bodies that
// aren't expanded.
@@ -7,7 +14,15 @@
// But we should warn here
TY1 x; // expected-warning {{extension}}
-TY(1) x; // FIXME: And we should warn here
+TY(1) x; // expected-warning {{extension}}
+
+#define TY_FN() typeof
+TY_FN()(1) y; // expected-warning {{extension}}
+
+ID(TY)(1) z; // expected-warning {{extension}}
+ID(TY1) w; // expected-warning {{extension}}
+
+EXPAND_AND_DISCARD(TY)
// Note: this warning intentionally doesn't trigger on keywords like
// __attribute; the standard allows implementation-defined extensions
@@ -16,3 +31,31 @@ TY(1) x; // FIXME: And we should warn here
// inline, restrict, asm, typeof, _asm
void whatever(void) {}
+
+#define CLASS class
+void macro_keyword(void) {
+ int CLASS; // expected-warning {{identifier 'class' conflicts with a C++ keyword}}
+}
+
+void macro_argument_keyword(void) {
+ int ID(CLASS); // expected-warning {{identifier 'class' conflicts with a C++ keyword}}
+}
+
+#else
+
+#define constexpr const
+constexpr int x = 0;
+#undef constexpr
+
+#define constexpr constexpr
+int constexpr; // expected-warning {{'constexpr' is a keyword in C++11}}
+namespace repeated {
+int constexpr; // already diagnosed in this TU
+}
+#undef constexpr
+
+#define NOEXCEPT noexcept
+EXPAND_AND_DISCARD(NOEXCEPT)
+int ID(NOEXCEPT); // expected-warning {{'noexcept' is a keyword in C++11}}
+
+#endif
``````````
</details>
https://github.com/llvm/llvm-project/pull/242626
More information about the cfe-commits
mailing list