owenpan created this revision.
owenpan added reviewers: MyDeveloperDay, HazardyKnusperkeks, rymiel.
owenpan added a project: clang-format.
Herald added projects: All, clang.
Herald added a subscriber: cfe-commits.
owenpan requested review of this revision.

Fixes https://github.com/llvm/llvm-project/issues/62107.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D148200

Files:
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTestComments.cpp


Index: clang/unittests/Format/FormatTestComments.cpp
===================================================================
--- clang/unittests/Format/FormatTestComments.cpp
+++ clang/unittests/Format/FormatTestComments.cpp
@@ -1059,6 +1059,13 @@
                    "#endif\n"
                    "  }\n"
                    "}"));
+
+  const StringRef Code("void func() {\n"
+                       "  // clang-format off\n"
+                       "  #define KV(value) #value, value\n"
+                       "  // clang-format on\n"
+                       "}");
+  EXPECT_EQ(Code, format(Code));
 }
 
 TEST_F(FormatTestComments, SplitsLongLinesInComments) {
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2975,8 +2975,8 @@
 
     // If the comment is currently aligned with the line immediately following
     // it, that's probably intentional and we should keep it.
-    if (NextNonCommentLine && Line->isComment() &&
-        NextNonCommentLine->First->NewlinesBefore <= 1 &&
+    if (NextNonCommentLine && !NextNonCommentLine->First->Finalized &&
+        Line->isComment() && NextNonCommentLine->First->NewlinesBefore <= 1 &&
         NextNonCommentLine->First->OriginalColumn ==
             Line->First->OriginalColumn) {
       const bool PPDirectiveOrImportStmt =


Index: clang/unittests/Format/FormatTestComments.cpp
===================================================================
--- clang/unittests/Format/FormatTestComments.cpp
+++ clang/unittests/Format/FormatTestComments.cpp
@@ -1059,6 +1059,13 @@
                    "#endif\n"
                    "  }\n"
                    "}"));
+
+  const StringRef Code("void func() {\n"
+                       "  // clang-format off\n"
+                       "  #define KV(value) #value, value\n"
+                       "  // clang-format on\n"
+                       "}");
+  EXPECT_EQ(Code, format(Code));
 }
 
 TEST_F(FormatTestComments, SplitsLongLinesInComments) {
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2975,8 +2975,8 @@
 
     // If the comment is currently aligned with the line immediately following
     // it, that's probably intentional and we should keep it.
-    if (NextNonCommentLine && Line->isComment() &&
-        NextNonCommentLine->First->NewlinesBefore <= 1 &&
+    if (NextNonCommentLine && !NextNonCommentLine->First->Finalized &&
+        Line->isComment() && NextNonCommentLine->First->NewlinesBefore <= 1 &&
         NextNonCommentLine->First->OriginalColumn ==
             Line->First->OriginalColumn) {
       const bool PPDirectiveOrImportStmt =
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to