[clang-tools-extra] [clang-tidy] Fix macro-parentheses locations around escaped newlines (PR #242940)

via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 15 18:40:02 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tools-extra

Author: Richard Thomson (LegalizeAdulthood)

<details>
<summary>Changes</summary>

Skip leading escaped newlines when locating diagnostics and opening parentheses. Use physical token ends for closing parentheses so fixes also handle tokens split across lines.

Add regression coverage for multiline macro arguments and replacement lists, and verify that the rewritten macros compile.

Fixes #<!-- -->26644.

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


3 Files Affected:

- (modified) clang-tools-extra/clang-tidy/bugprone/MacroParenthesesCheck.cpp (+15-15) 
- (modified) clang-tools-extra/docs/ReleaseNotes.md (+3-2) 
- (added) clang-tools-extra/test/clang-tidy/checkers/bugprone/macro-parentheses-multiline.cpp (+69) 


``````````diff
diff --git a/clang-tools-extra/clang-tidy/bugprone/MacroParenthesesCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/MacroParenthesesCheck.cpp
index 1de723cb87b5bc..11966cdbf27053 100644
--- a/clang-tools-extra/clang-tidy/bugprone/MacroParenthesesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/MacroParenthesesCheck.cpp
@@ -147,26 +147,26 @@ void MacroParenthesesPPCallbacks::replacementList(const Token &MacroNameTok,
       if (std::prev(TE)->is(tok::star))
         return;
 
-      Loc = Tok.getLocation();
+      Loc = Lexer::AdvanceToTokenCharacter(
+          Tok.getLocation(), 0, PP->getSourceManager(), PP->getLangOpts());
     }
   }
   if (Loc.isValid()) {
     const Token &Last = *std::prev(MI->tokens_end());
+    const SourceLocation Begin = Lexer::AdvanceToTokenCharacter(
+        MI->tokens_begin()->getLocation(), 0, PP->getSourceManager(),
+        PP->getLangOpts());
     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()),
-                                        ")");
+          << FixItHint::CreateInsertion(Begin, "(")
+          << FixItHint::CreateInsertion(Last.getEndLoc(), ")");
     } 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()),
-                                        ")");
+          << FixItHint::CreateInsertion(Begin, "(")
+          << FixItHint::CreateInsertion(Last.getEndLoc(), ")");
     }
   }
 }
@@ -278,12 +278,12 @@ void MacroParenthesesPPCallbacks::argument(const Token &MacroNameTok,
       continue;
 
     if (!FoundGoto) {
-      Check->diag(Tok.getLocation(), "macro argument should be enclosed in "
-                                     "parentheses")
-          << FixItHint::CreateInsertion(Tok.getLocation(), "(")
-          << FixItHint::CreateInsertion(Tok.getLocation().getLocWithOffset(
-                                            PP->getSpelling(Tok).length()),
-                                        ")");
+      // A token can start with an escaped newline or contain one internally.
+      const SourceLocation Begin = Lexer::AdvanceToTokenCharacter(
+          Tok.getLocation(), 0, PP->getSourceManager(), PP->getLangOpts());
+      Check->diag(Begin, "macro argument should be enclosed in parentheses")
+          << FixItHint::CreateInsertion(Begin, "(")
+          << FixItHint::CreateInsertion(Tok.getEndLoc(), ")");
     }
 
     FoundGoto = false;
diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md
index b6774370511feb..eefb14834b34f3 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -213,8 +213,9 @@ infrastructure are described first, followed by tool-specific sections.
   operands narrower than `int` is only signed due to integer promotion.
 
 - Improved {doc}`bugprone-macro-parentheses
-  <clang-tidy/checks/bugprone/macro-parentheses>` by fixing invalid fixes for
-  macro arguments used as names in alias declarations.
+  <clang-tidy/checks/bugprone/macro-parentheses>` by fixing invalid fixes
+  for macro arguments used as names in alias declarations, and correcting
+  diagnostic and fix-it locations for tokens containing escaped newlines.
 
 - Fixed a crash in {doc}`bugprone-misplaced-operator-in-strlen-in-alloc
   <clang-tidy/checks/bugprone/misplaced-operator-in-strlen-in-alloc>` when
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/macro-parentheses-multiline.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/macro-parentheses-multiline.cpp
new file mode 100644
index 00000000000000..29d04c614e0f53
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/macro-parentheses-multiline.cpp
@@ -0,0 +1,69 @@
+// RUN: %check_clang_tidy %s bugprone-macro-parentheses %t
+// RUN: %clang_cc1 -fsyntax-only %t.cpp
+
+// Preserve the indentation and line continuations being tested.
+// clang-format off
+#define UNINDENTED(b0, b1) \
+b0=b1; \
+b1++
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: macro argument should be enclosed in parentheses [bugprone-macro-parentheses]
+// CHECK-FIXES: #define UNINDENTED(b0, b1) \
+// CHECK-FIXES-NEXT: b0=b1; \
+// CHECK-FIXES-NEXT: (b1)++
+
+#define INDENTED(b0, b1) \
+  b0=b1; \
+  b1++
+// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: macro argument should be enclosed in parentheses [bugprone-macro-parentheses]
+// CHECK-FIXES: #define INDENTED(b0, b1) \
+// CHECK-FIXES-NEXT: b0=b1; \
+// CHECK-FIXES-NEXT: (b1)++
+
+#define SPLIT_ARGUMENT(b0, b1) \
+b0=b1; \
+b\
+1++
+// CHECK-MESSAGES: :[[@LINE-2]]:1: warning: macro argument should be enclosed in parentheses [bugprone-macro-parentheses]
+// CHECK-FIXES: #define SPLIT_ARGUMENT(b0, b1) \
+// CHECK-FIXES-NEXT: b0=b1; \
+// CHECK-FIXES-NEXT: (b\
+// CHECK-FIXES-NEXT: 1)++
+
+#define REPEATED_CONTINUATION(b0, b1) \
+b0=b1; \
+\
+b1++
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: macro argument should be enclosed in parentheses [bugprone-macro-parentheses]
+// CHECK-FIXES: #define REPEATED_CONTINUATION(b0, b1) \
+// CHECK-FIXES-NEXT: b0=b1; \
+// CHECK-FIXES-NEXT: \
+// CHECK-FIXES-NEXT: (b1)++
+
+#define REPLACEMENT_BEGIN \
+1+2
+// CHECK-MESSAGES: :[[@LINE-1]]:2: warning: macro replacement list should be enclosed in parentheses [bugprone-macro-parentheses]
+// CHECK-FIXES: #define REPLACEMENT_BEGIN \
+// CHECK-FIXES-NEXT: (1+2)
+
+#define REPLACEMENT_OPERATOR 1 \
++2
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: macro replacement list should be enclosed in parentheses [bugprone-macro-parentheses]
+// CHECK-FIXES: #define REPLACEMENT_OPERATOR (1 \
+// CHECK-FIXES-NEXT: +2)
+
+#define SPLIT_REPLACEMENT \
++1\
+2
+// CHECK-MESSAGES: :[[@LINE-2]]:1: warning: macro replacement list should be enclosed in parentheses [bugprone-macro-parentheses]
+// CHECK-FIXES: #define SPLIT_REPLACEMENT \
+// CHECK-FIXES-NEXT: (+1\
+// CHECK-FIXES-NEXT: 2)
+// clang-format on
+
+int use_macros(int a, int b) {
+  UNINDENTED(a, b);
+  INDENTED(a, b);
+  SPLIT_ARGUMENT(a, b);
+  REPEATED_CONTINUATION(a, b);
+  return REPLACEMENT_BEGIN + REPLACEMENT_OPERATOR + SPLIT_REPLACEMENT;
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/242940


More information about the cfe-commits mailing list