Skip to content

Conversation

@saideepaksana
Copy link

For the issue #88882,
files changed clang/include/clang/Basic/DiagnosticLexKinds.td, clang/lib/Lex/PPDirectives.cpp. I tried adding %0 place holder in the .td file, so by passing the identifier info of the redefined builtin macro, so now it says clearly which macro affected, before it was warning: redefining builtin macro and now it is warning: redefining builtin macro '__FILE__'.

@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 10, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 10, 2025

@llvm/pr-subscribers-clang

Author: Sai Deepak Sana (saideepaksana)

Changes

For the issue #88882,
files changed clang/include/clang/Basic/DiagnosticLexKinds.td, clang/lib/Lex/PPDirectives.cpp. I tried adding %0 place holder in the .td file, so by passing the identifier info of the redefined builtin macro, so now it says clearly which macro affected, before it was warning: redefining builtin macro and now it is warning: redefining builtin macro '__FILE__'.


Full diff: https://github.com/llvm/llvm-project/pull/167299.diff

2 Files Affected:

  • (modified) clang/include/clang/Basic/DiagnosticLexKinds.td (+1-1)
  • (modified) clang/lib/Lex/PPDirectives.cpp (+2-2)
diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td b/clang/include/clang/Basic/DiagnosticLexKinds.td
index 417187222e448..e9339f473c67a 100644
--- a/clang/include/clang/Basic/DiagnosticLexKinds.td
+++ b/clang/include/clang/Basic/DiagnosticLexKinds.td
@@ -396,7 +396,7 @@ def pp_out_of_date_dependency : Warning<
   "current file is older than dependency %0">;
 def ext_pp_undef_builtin_macro : ExtWarn<"undefining builtin macro">,
   InGroup<BuiltinMacroRedefined>;
-def ext_pp_redef_builtin_macro : ExtWarn<"redefining builtin macro">,
+def ext_pp_redef_builtin_macro : ExtWarn<"redefining builtin macro '%0'">,
   InGroup<BuiltinMacroRedefined>;
 def pp_disabled_macro_expansion : Warning<
   "disabled expansion of recursive macro">, DefaultIgnore,
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 891c8ab7f3155..38780f4fc9a89 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -3289,7 +3289,7 @@ void Preprocessor::HandleDefineDirective(
       // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
       // C++ [cpp.predefined]p4, but allow it as an extension.
       if (isLanguageDefinedBuiltin(SourceMgr, OtherMI, II->getName()))
-        Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
+        Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro) << MacroNameTok.getIdentifierInfo(); // inserting this diagonstic message into stream.
       // Macros must be identical.  This means all tokens and whitespace
       // separation must be the same.  C99 6.10.3p2.
       else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
@@ -3354,7 +3354,7 @@ void Preprocessor::HandleUndefDirective() {
     // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4 and
     // C++ [cpp.predefined]p4, but allow it as an extension.
     if (isLanguageDefinedBuiltin(SourceMgr, MI, II->getName()))
-      Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro);
+      Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro) << MacroNameTok.getIdentifierInfo(); // inserting this diagnostic message into it
 
     if (MI->isWarnIfUnused())
       WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());

@zwuis
Copy link
Contributor

zwuis commented Nov 10, 2025

Thank you for your patch!

Did you run regression tests locally to ensure that this patch doesn't break anything?

@github-actions
Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff origin/main HEAD --extensions cpp -- clang/lib/Lex/PPDirectives.cpp --diff_from_common_commit

⚠️
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing origin/main to the base branch/commit you want to compare against.
⚠️

View the diff from clang-format here.
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 38780f4fc..88448f9b3 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -3289,7 +3289,9 @@ void Preprocessor::HandleDefineDirective(
       // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
       // C++ [cpp.predefined]p4, but allow it as an extension.
       if (isLanguageDefinedBuiltin(SourceMgr, OtherMI, II->getName()))
-        Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro) << MacroNameTok.getIdentifierInfo(); // inserting this diagonstic message into stream.
+        Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro)
+            << MacroNameTok.getIdentifierInfo(); // inserting this diagonstic
+                                                 // message into stream.
       // Macros must be identical.  This means all tokens and whitespace
       // separation must be the same.  C99 6.10.3p2.
       else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
@@ -3354,7 +3356,9 @@ void Preprocessor::HandleUndefDirective() {
     // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4 and
     // C++ [cpp.predefined]p4, but allow it as an extension.
     if (isLanguageDefinedBuiltin(SourceMgr, MI, II->getName()))
-      Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro) << MacroNameTok.getIdentifierInfo(); // inserting this diagnostic message into it
+      Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro)
+          << MacroNameTok.getIdentifierInfo(); // inserting this diagnostic
+                                               // message into it
 
     if (MI->isWarnIfUnused())
       WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());

Copy link
Contributor

@zwuis zwuis left a comment

Choose a reason for hiding this comment

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

Please use this syntax to reference the issue fixed by this patch.

Please update tests in "clang/test/" to verify that macros appear in diagnostic messages.

Please add a release note entry to "clang/docs/ReleaseNotes.rst".

// C++ [cpp.predefined]p4, but allow it as an extension.
if (isLanguageDefinedBuiltin(SourceMgr, OtherMI, II->getName()))
Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro) << MacroNameTok.getIdentifierInfo(); // inserting this diagonstic message into stream.
Copy link
Contributor

Choose a reason for hiding this comment

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

Unnecessary comment. Ditto below.

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.

3 participants