https://github.com/owenca created 
https://github.com/llvm/llvm-project/pull/154787

Fixes #154683

>From 04dab5e8cf9646e88970350ee602cc82535322bc Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpi...@gmail.com>
Date: Thu, 21 Aug 2025 08:58:12 -0700
Subject: [PATCH] [clang-format] Fix a bug in SkipMacroDefinitionBody

Fixes #154683
---
 clang/lib/Format/UnwrappedLineParser.cpp | 9 ++++++---
 clang/unittests/Format/FormatTest.cpp    | 4 ++++
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp 
b/clang/lib/Format/UnwrappedLineParser.cpp
index 91b8fdc8a3c38..23938f8cb9d00 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1182,10 +1182,8 @@ void UnwrappedLineParser::parsePPDefine() {
   if (MaybeIncludeGuard && !eof())
     IncludeGuard = IG_Rejected;
 
-  if (FormatTok->Tok.getKind() == tok::l_paren &&
-      !FormatTok->hasWhitespaceBefore()) {
+  if (FormatTok->is(tok::l_paren) && !FormatTok->hasWhitespaceBefore())
     parseParens();
-  }
   if (Style.IndentPPDirectives != FormatStyle::PPDIS_None)
     Line->Level += PPBranchLevel + 1;
   addUnwrappedLine();
@@ -1196,6 +1194,11 @@ void UnwrappedLineParser::parsePPDefine() {
   Line->InMacroBody = true;
 
   if (Style.SkipMacroDefinitionBody) {
+    if (auto *Prev = Tokens->getPreviousToken(); Prev->is(tok::comment) &&
+                                                 Prev->NewlinesBefore > 0 &&
+                                                 !Prev->HasUnescapedNewline) {
+      Prev->Finalized = true;
+    }
     while (!eof()) {
       FormatTok->Finalized = true;
       FormatTok = Tokens->getNextToken();
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 83c664c3b81f3..1ef6877941c7d 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -26022,6 +26022,10 @@ TEST_F(FormatTest, SkipMacroDefinitionBody) {
                  "  A  a \\\n "
                  " A  a",
                  Style);
+  verifyNoChange("#define MY_MACRO          \\\n"
+                 " /* this is a comment */ \\\n"
+                 "   1",
+                 Style);
 }
 
 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to