https://github.com/zeyi2 created 
https://github.com/llvm/llvm-project/pull/177841

Closes #177594

>From ee151ce597d5013fea960d54ea0ca67795d3131e Mon Sep 17 00:00:00 2001
From: mtx <[email protected]>
Date: Sat, 24 Jan 2026 17:36:51 +0800
Subject: [PATCH] [clang-tidy] Improve diagnostics of
 bugprone-macro-parentheses

---
 .../bugprone/MacroParenthesesCheck.cpp        | 30 +++++++++++++++----
 clang-tools-extra/docs/ReleaseNotes.rst       |  4 +++
 .../bugprone/macro-parentheses-cmdline.cpp    |  5 ++--
 3 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/MacroParenthesesCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/MacroParenthesesCheck.cpp
index 67cad02e5e2e5..e11b153e0250b 100644
--- a/clang-tools-extra/clang-tidy/bugprone/MacroParenthesesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/MacroParenthesesCheck.cpp
@@ -99,6 +99,15 @@ static bool possibleVarDecl(const MacroInfo *MI, const Token 
*Tok) {
          isVarDeclKeyword(*Tok);
 }
 
+static StringRef getMacroText(const MacroInfo *MI, Preprocessor *PP) {
+  if (MI->tokens_empty())
+    return {};
+  return Lexer::getSourceText(
+      CharSourceRange::getTokenRange(MI->tokens_begin()->getLocation(),
+                                     MI->tokens().back().getLocation()),
+      PP->getSourceManager(), PP->getLangOpts());
+}
+
 void MacroParenthesesPPCallbacks::replacementList(const Token &MacroNameTok,
                                                   const MacroInfo *MI) {
   // Make sure macro replacement isn't a variable declaration.
@@ -143,11 +152,22 @@ void MacroParenthesesPPCallbacks::replacementList(const 
Token &MacroNameTok,
   }
   if (Loc.isValid()) {
     const Token &Last = *std::prev(MI->tokens_end());
-    Check->diag(Loc, "macro replacement list should be enclosed in 
parentheses")
-        << FixItHint::CreateInsertion(MI->tokens_begin()->getLocation(), "(")
-        << FixItHint::CreateInsertion(Last.getLocation().getLocWithOffset(
-                                          PP->getSpelling(Last).length()),
-                                      ")");
+    if (PP->getSourceManager().isWrittenInCommandLineFile(Loc)) {
+      Check->diag(Loc, "macro replacement list should be enclosed in "
+                       "parentheses; macro '%0' defined as '%1'")
+          << PP->getSpelling(MacroNameTok) << getMacroText(MI, PP)
+          << FixItHint::CreateInsertion(MI->tokens_begin()->getLocation(), "(")
+          << FixItHint::CreateInsertion(Last.getLocation().getLocWithOffset(
+                                            PP->getSpelling(Last).length()),
+                                        ")");
+    } else {
+      Check->diag(Loc,
+                  "macro replacement list should be enclosed in parentheses")
+          << FixItHint::CreateInsertion(MI->tokens_begin()->getLocation(), "(")
+          << FixItHint::CreateInsertion(Last.getLocation().getLocWithOffset(
+                                            PP->getSpelling(Last).length()),
+                                        ")");
+    }
   }
 }
 
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 5af634c77f54d..40d479c2db9ec 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -109,6 +109,10 @@ New check aliases
 Changes in existing checks
 ^^^^^^^^^^^^^^^^^^^^^^^^^^
 
+- Improved :doc:`bugprone-macro-parentheses
+  <clang-tidy/checks/bugprone/macro-parentheses>` check by printing the macro
+  definition in the warning message if the macro is defined on command line.
+
 - Improved :doc:`bugprone-unsafe-functions
   <clang-tidy/checks/bugprone/unsafe-functions>` check by adding the function
   ``std::get_temporary_buffer`` to the default list of unsafe functions. (This
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/macro-parentheses-cmdline.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/macro-parentheses-cmdline.cpp
index 9ff757d13c62e..daee525275bfd 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/macro-parentheses-cmdline.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/macro-parentheses-cmdline.cpp
@@ -1,10 +1,9 @@
 // RUN: %check_clang_tidy %s bugprone-macro-parentheses %t -- -- -DVAL=0+0
 
-// The previous command-line is producing warnings and fixes with the source
-// locations from a virtual buffer. VAL is replaced by '0+0'.
-// Fixes could not be applied and should not be reported.
 int foo() { return VAL; }
+// CHECK-MESSAGES: warning: macro replacement list should be enclosed in 
parentheses; macro 'VAL' defined as '0+0' [bugprone-macro-parentheses]
 
 #define V 0+0
 int bar() { return V; }
+// CHECK-MESSAGES: :[[@LINE-2]]:12: warning: macro replacement list should be 
enclosed in parentheses [bugprone-macro-parentheses]
 // CHECK-FIXES: #define V (0+0)

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to