Author: mitchell
Date: 2026-01-25T16:37:23+08:00
New Revision: a06a0e2780ea50f87cbe56be7754cd6866dece0a

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

LOG: [clang-tidy] Improve diagnostics of bugprone-macro-parentheses (#177841)

Closes #177594

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/bugprone/MacroParenthesesCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    
clang-tools-extra/test/clang-tidy/checkers/bugprone/macro-parentheses-cmdline.cpp

Removed: 
    


################################################################################
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 6c91d34c5539c..7f55341e2995b 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -122,6 +122,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