Thanks to visit codestin.com
Credit goes to github.com

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

Merged
merged 2 commits into from
May 1, 2025

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

@jmmartinez jmmartinez requested a review from a-tarasyuk April 29, 2025 15:14
Copy link
Collaborator

@spavloff spavloff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@jmmartinez jmmartinez merged commit 0d3d2f6 into llvm:main May 1, 2025
14 checks passed
@jmmartinez
Copy link
Contributor Author

Thanks !

@llvm-ci
Copy link
Collaborator

llvm-ci commented May 1, 2025

LLVM Buildbot has detected a new failure on builder lldb-remote-linux-win running on as-builder-10 while building clang at step 17 "test-check-lldb-api".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/197/builds/4754

Here is the relevant piece of the build log for the reference
Step 17 (test-check-lldb-api) failure: Test just built components: check-lldb-api completed (failure)
******************** TEST 'lldb-api :: functionalities/data-formatter/data-formatter-python-synth/TestDataFormatterPythonSynth.py' FAILED ********************
Script:
--
C:/Python312/python.exe C:/buildbot/as-builder-10/lldb-x-aarch64/llvm-project/lldb\test\API\dotest.py -u CXXFLAGS -u CFLAGS --env LLVM_LIBS_DIR=C:/buildbot/as-builder-10/lldb-x-aarch64/build/./lib --env LLVM_INCLUDE_DIR=C:/buildbot/as-builder-10/lldb-x-aarch64/build/include --env LLVM_TOOLS_DIR=C:/buildbot/as-builder-10/lldb-x-aarch64/build/./bin --arch aarch64 --build-dir C:/buildbot/as-builder-10/lldb-x-aarch64/build/lldb-test-build.noindex --lldb-module-cache-dir C:/buildbot/as-builder-10/lldb-x-aarch64/build/lldb-test-build.noindex/module-cache-lldb\lldb-api --clang-module-cache-dir C:/buildbot/as-builder-10/lldb-x-aarch64/build/lldb-test-build.noindex/module-cache-clang\lldb-api --executable C:/buildbot/as-builder-10/lldb-x-aarch64/build/./bin/lldb.exe --compiler C:/buildbot/as-builder-10/lldb-x-aarch64/build/./bin/clang.exe --dsymutil C:/buildbot/as-builder-10/lldb-x-aarch64/build/./bin/dsymutil.exe --make C:/ninja/make.exe --llvm-tools-dir C:/buildbot/as-builder-10/lldb-x-aarch64/build/./bin --lldb-obj-root C:/buildbot/as-builder-10/lldb-x-aarch64/build/tools/lldb --lldb-libs-dir C:/buildbot/as-builder-10/lldb-x-aarch64/build/./lib --platform-url connect://jetson-agx-0086.lab.llvm.org:1234 --platform-working-dir /home/ubuntu/lldb-tests --sysroot c:/buildbot/fs/jetson-agx-ubuntu --env ARCH_CFLAGS=-mcpu=cortex-a78 --platform-name remote-linux --skip-category=lldb-server C:\buildbot\as-builder-10\lldb-x-aarch64\llvm-project\lldb\test\API\functionalities\data-formatter\data-formatter-python-synth -p TestDataFormatterPythonSynth.py
--
Exit Code: 3221226356

Command Output (stdout):
--
lldb version 21.0.0git (https://github.com/llvm/llvm-project.git revision 0d3d2f639c42b22fc1b9453c7b834dbb0f16c0dc)
  clang revision 0d3d2f639c42b22fc1b9453c7b834dbb0f16c0dc
  llvm revision 0d3d2f639c42b22fc1b9453c7b834dbb0f16c0dc
-----------------------
Category: default
-----------------------
foo:  Python class fooSynthProvider
wrapfoo:  Python class wrapfooSynthProvider
-----------------------
Category: VectorTypes
-----------------------
-----------------------
Category: system
-----------------------
-----------------------
Category: cplusplus
-----------------------
-----------------------
Category: objc
-----------------------
(foo) f00_1 = (a = 1, fake_a = 33554432, r = 34)
(foo) f00_1 = (a = 1, fake_a = 33554432, r = 34)
(int) f00_1.fake_a = 33554432
(int) f00_1[1] = 33554432
(foo) f00_1 = fake_a=33554432
(foo) f00_1 = fake_a=33554432
Process 1829746 stopped
* thread #1, name = 'a.out', stop reason = step over
    frame #0: 0x0000aaaabae0a874 a.out`main at main.cpp:62:13
   59  	
   60  	    f00_1.a++; // Set break point at this line.
   61  	    
-> 62  	    wrapint test_cast('A' +
    	            ^
   63  	               256*'B' +
   64  	               256*256*'C'+
   65  	               256*256*256*'D');
(foo) f00_1 = (a = 2, fake_a = 33554432, r = 34)
(int) $0 = 280
(foo) f00_1 = (a = 280, fake_a = 33554433, r = 34)
(foo *) f00_ptr = 0x0000aaaadce3c2a0 {
...

@nikic
Copy link
Contributor

nikic commented May 1, 2025

// 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can at least combine these to avoid the duplicate getPresumedLoc(), but possibly these can be more efficient in general? E.g. isInSystemHeader has a much more optimized implementation.

@tbaederr
Copy link
Contributor

tbaederr commented May 6, 2025

@jmmartinez Ping.

IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
llvm#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.
@AaronBallman
Copy link
Collaborator

Yeah, those perf drops are pretty unfortunate. Nikita's idea of trying to combine the checks makes sense to me as a good approach to try.

IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
llvm#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.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
llvm#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.
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
llvm#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.
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.

7 participants