[clang] [Clang] Do not warn for serialized builtin or command-line definitions (PR #137306)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 25 03:12:22 PDT 2025
Juan Manuel Martinez =?utf-8?q?Caamaño?= <juamarti at amd.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/137306 at github.com>
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Juan Manuel Martinez Caamaño (jmmartinez)
<details>
<summary>Changes</summary>
When using `-dD` to generate a preprocessed output, the `#define` directives
are preserved. This includes built-in and command-line definitions.
Before, clang would warn for reserved identifiers for serialized built-in
and command-line definitions.
This patch adds an exception to these cases.
---
Full diff: https://github.com/llvm/llvm-project/pull/137306.diff
2 Files Affected:
- (modified) clang/lib/Lex/PPDirectives.cpp (+6-2)
- (added) clang/test/Preprocessor/macro_reserved.i (+11)
``````````diff
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 6c337801a8435..6468e62889413 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -371,8 +371,12 @@ bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
SourceLocation MacroNameLoc = MacroNameTok.getLocation();
if (ShadowFlag)
*ShadowFlag = false;
- if (!SourceMgr.isInSystemHeader(MacroNameLoc) &&
- (SourceMgr.getBufferName(MacroNameLoc) != "<built-in>")) {
+ // Macro names with reserved identifiers are accepted if built-in or passed
+ // through the command line (the later may be present if -dD was used to
+ // generate the preprocessed file).
+ bool IsBuiltinOrCmd = SourceMgr.isWrittenInBuiltinFile(MacroNameLoc) ||
+ SourceMgr.isWrittenInCommandLineFile(MacroNameLoc);
+ if (!IsBuiltinOrCmd && !SourceMgr.isInSystemHeader(MacroNameLoc)) {
MacroDiag D = MD_NoWarn;
if (isDefineUndef == MU_Define) {
D = shouldWarnOnMacroDef(*this, II);
diff --git a/clang/test/Preprocessor/macro_reserved.i b/clang/test/Preprocessor/macro_reserved.i
new file mode 100644
index 0000000000000..c9ed044e4c931
--- /dev/null
+++ b/clang/test/Preprocessor/macro_reserved.i
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -x cpp-output %s
+#pragma clang diagnostic push
+#pragma clang diagnostic warning "-Wreserved-macro-identifier"
+# 1 "<built-in>" 1
+#define __BUILTIN__
+# 2 "<command line>" 1
+#define __CMD__
+# 3 "biz.cpp" 1
+#define __SOME_FILE__ // expected-warning {{macro name is a reserved identifier}}
+int v;
+#pragma clang diagnostic pop
``````````
</details>
https://github.com/llvm/llvm-project/pull/137306
More information about the cfe-commits
mailing list