Skip to content

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Aug 26, 2025

All comments before the macro definition body should be skipped.

All comments before the macro definition body should be skipped.
@llvmbot
Copy link
Member

llvmbot commented Aug 26, 2025

@llvm/pr-subscribers-clang-format

Author: owenca (owenca)

Changes

All comments before the macro definition body should be skipped.


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

2 Files Affected:

  • (modified) clang/lib/Format/UnwrappedLineParser.cpp (+2-5)
  • (modified) clang/unittests/Format/FormatTest.cpp (+1)
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index ca7e2da3799c4..f4bbfcf8461bc 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1207,11 +1207,8 @@ void UnwrappedLineParser::parsePPDefine() {
     return;
   }
 
-  if (auto *Prev = Tokens->getPreviousToken(); Prev->is(tok::comment) &&
-                                               Prev->NewlinesBefore > 0 &&
-                                               !Prev->HasUnescapedNewline) {
-    Prev->Finalized = true;
-  }
+  for (auto *Comment : CommentsBeforeNextToken)
+    Comment->Finalized = true;
 
   do {
     FormatTok->Finalized = true;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 136039d255393..0d9b3ab1359cb 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -26023,6 +26023,7 @@ TEST_F(FormatTest, SkipMacroDefinitionBody) {
                  " A  a",
                  Style);
   verifyNoChange("#define MY_MACRO  \\\n"
+                 " /*foo*//*bar*/  \\\n"
                  " /* comment */  \\\n"
                  "   1",
                  Style);

@owenca owenca merged commit ac771f4 into llvm:main Aug 26, 2025
11 checks passed
@owenca owenca deleted the SkipMacroDefinitionBody branch August 26, 2025 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants