diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -2021,7 +2021,15 @@ parseBlock(/*MustBeDeclaration=*/false); addUnwrappedLine(); } else if (FormatTok->Tok.is(tok::kw_if)) { + FormatToken *Previous = AllTokens[Tokens->getPosition() - 1]; + bool PrecededByComment = Previous->is(tok::comment); + if (PrecededByComment) { + addUnwrappedLine(); + ++Line->Level; + } parseIfThenElse(); + if (PrecededByComment) + --Line->Level; } else { addUnwrappedLine(); ++Line->Level; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -1181,6 +1181,13 @@ " g();\n" "else\n" " h();"); + verifyFormat("if (a)\n" + " f();\n" + "else // comment\n" + " if (b) {\n" + " g();\n" + " h();\n" + " }"); verifyFormat("if constexpr (a)\n" " f();\n" "else if constexpr (b)\n"