This revision was landed with ongoing or failed builds. This revision was automatically updated to reflect the committed changes. Closed by commit rGca7f47158581: [clang-format] Fix a bug that indents else-comment-if incorrectly (authored by owenca <owe...@users.noreply.github.com>).
Changed prior to commit: https://reviews.llvm.org/D104774?vs=353909&id=353933#toc Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D104774/new/ https://reviews.llvm.org/D104774 Files: clang/lib/Format/UnwrappedLineParser.cpp clang/unittests/Format/FormatTest.cpp Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ 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" Index: clang/lib/Format/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ 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;
Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ 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" Index: clang/lib/Format/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ 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;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits