Author: Juan Manuel Martinez CaamaƱo
Date: 2025-05-01T09:55:46+02:00
New Revision: 0d3d2f639c42b22fc1b9453c7b834dbb0f16c0dc

URL: 
https://github.com/llvm/llvm-project/commit/0d3d2f639c42b22fc1b9453c7b834dbb0f16c0dc
DIFF: 
https://github.com/llvm/llvm-project/commit/0d3d2f639c42b22fc1b9453c7b834dbb0f16c0dc.diff

LOG: [Clang] Do not warn for serialized builtin or command-line definitions 
(#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.

Added: 
    clang/test/Preprocessor/macro_reserved.i

Modified: 
    clang/lib/Lex/PPDirectives.cpp

Removed: 
    


################################################################################
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


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to