[clang] [Clang] Do not warn for serialized builtin or command-line definitions (PR #137306)
Juan Manuel Martinez CaamaƱo via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 25 03:11:47 PDT 2025
https://github.com/jmmartinez created https://github.com/llvm/llvm-project/pull/137306
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.
>From 5a0da33b72a220b483b68b7b5a89c7df95481a72 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?= <juamarti at amd.com>
Date: Fri, 25 Apr 2025 11:15:05 +0200
Subject: [PATCH 1/2] Pre-commit test: [Clang] Do not warn for serialized
builtin or command-line definitions
---
clang/test/Preprocessor/macro_reserved.i | 11 +++++++++++
1 file changed, 11 insertions(+)
create mode 100644 clang/test/Preprocessor/macro_reserved.i
diff --git a/clang/test/Preprocessor/macro_reserved.i b/clang/test/Preprocessor/macro_reserved.i
new file mode 100644
index 0000000000000..266d4e1a14ebb
--- /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__ // expected-warning {{macro name is a reserved identifier}}
+# 2 "<command line>" 1
+#define __CMD__ // expected-warning {{macro name is a reserved identifier}}
+# 3 "biz.cpp" 1
+#define __SOME_FILE__ // expected-warning {{macro name is a reserved identifier}}
+int v;
+#pragma clang diagnostic pop
>From 40812dfa678da6408bf5781abf1d051c0d3af4c4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?= <juamarti at amd.com>
Date: Fri, 25 Apr 2025 11:15:27 +0200
Subject: [PATCH 2/2] [Clang] Do not warn for serialized builtin or
command-line definitions
When using -dD to generte a preprocessed output, the #define directives
are preserved. This includes builtin and command line definitions.
Before, clang would warn for reserved identifiers for serialzied builtin
and command-line definitions.
This patch adds an exception to these cases.
---
clang/lib/Lex/PPDirectives.cpp | 8 ++++++--
clang/test/Preprocessor/macro_reserved.i | 4 ++--
2 files changed, 8 insertions(+), 4 deletions(-)
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
index 266d4e1a14ebb..c9ed044e4c931 100644
--- a/clang/test/Preprocessor/macro_reserved.i
+++ b/clang/test/Preprocessor/macro_reserved.i
@@ -2,9 +2,9 @@
#pragma clang diagnostic push
#pragma clang diagnostic warning "-Wreserved-macro-identifier"
# 1 "<built-in>" 1
-#define __BUILTIN__ // expected-warning {{macro name is a reserved identifier}}
+#define __BUILTIN__
# 2 "<command line>" 1
-#define __CMD__ // expected-warning {{macro name is a reserved identifier}}
+#define __CMD__
# 3 "biz.cpp" 1
#define __SOME_FILE__ // expected-warning {{macro name is a reserved identifier}}
int v;
More information about the cfe-commits
mailing list