Author: Manuel Klimek Date: 2021-12-01T11:23:04+01:00 New Revision: 59788422609433c50c3d3c16ca531fa56b9acb4f
URL: https://github.com/llvm/llvm-project/commit/59788422609433c50c3d3c16ca531fa56b9acb4f DIFF: https://github.com/llvm/llvm-project/commit/59788422609433c50c3d3c16ca531fa56b9acb4f.diff LOG: Fix clang-format bug when handling conflict markers. Previously, clang-format would not correctly identify preprocessor directives directly following a conflict marker, which would result in violating the formatter's invariants. The provided test fails in assert mode before this change. Added: Modified: clang/lib/Format/UnwrappedLineParser.cpp clang/unittests/Format/FormatTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index da35648ef1ca0..a7e0422e3146c 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -3388,6 +3388,20 @@ void UnwrappedLineParser::readToken(int LevelDifference) { do { FormatTok = Tokens->getNextToken(); assert(FormatTok); + while (FormatTok->getType() == TT_ConflictStart || + FormatTok->getType() == TT_ConflictEnd || + FormatTok->getType() == TT_ConflictAlternative) { + if (FormatTok->getType() == TT_ConflictStart) { + conditionalCompilationStart(/*Unreachable=*/false); + } else if (FormatTok->getType() == TT_ConflictAlternative) { + conditionalCompilationAlternative(); + } else if (FormatTok->getType() == TT_ConflictEnd) { + conditionalCompilationEnd(); + } + FormatTok = Tokens->getNextToken(); + FormatTok->MustBreakBefore = true; + } + while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) && (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) { distributeComments(Comments, FormatTok); @@ -3409,19 +3423,6 @@ void UnwrappedLineParser::readToken(int LevelDifference) { flushComments(isOnNewLine(*FormatTok)); parsePPDirective(); } - while (FormatTok->getType() == TT_ConflictStart || - FormatTok->getType() == TT_ConflictEnd || - FormatTok->getType() == TT_ConflictAlternative) { - if (FormatTok->getType() == TT_ConflictStart) { - conditionalCompilationStart(/*Unreachable=*/false); - } else if (FormatTok->getType() == TT_ConflictAlternative) { - conditionalCompilationAlternative(); - } else if (FormatTok->getType() == TT_ConflictEnd) { - conditionalCompilationEnd(); - } - FormatTok = Tokens->getNextToken(); - FormatTok->MustBreakBefore = true; - } if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) && !Line->InPPDirective) { diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 39f1b835fb766..65809ab5e7579 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -21117,6 +21117,14 @@ TEST_F(FormatTest, HandleConflictMarkers) { ">>>>>>>\n" " End\n" "int i;\n")); + + verifyFormat(R"(==== +#ifdef A +a +#else +b +#endif +)"); } TEST_F(FormatTest, DisableRegions) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits