diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -3025,8 +3025,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 = diff --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp --- a/clang/unittests/Format/FormatTestComments.cpp +++ b/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) {