Skip to content

[Clang] Do not warn for serialized builtin or command-line definitions #137306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jmmartinez
Copy link
Contributor

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.

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.
@jmmartinez jmmartinez requested review from spavloff and lamb-j April 25, 2025 10:11
@jmmartinez jmmartinez self-assigned this Apr 25, 2025
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Apr 25, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 25, 2025

@llvm/pr-subscribers-clang

Author: Juan Manuel Martinez Caamaño (jmmartinez)

Changes

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants