rymiel created this revision. rymiel added a project: clang-format. rymiel added reviewers: HazardyKnusperkeks, owenpan, MyDeveloperDay. Herald added projects: All, clang. Herald added a subscriber: cfe-commits. rymiel requested review of this revision.
When in ColumnLimit 0, the formatter looks for MustBreakBefore in the line in order to check if a line is allowed to be merged onto one line. However, since MustBreakBefore is really a property of the gap between the token and the one previously, I belive the check is erroneous in checking all the tokens in a line, since whether the previous line ended with a forced line break should have no effect on whether the current line is allowed to merge with the next one. This patch changes the check to skip the first token in `LineJoiner.containsMustBreak`. This patch also changes a test, which is not ideal, but I believe the test also suffered from this bug. The test case in question sets AllowShortFunctionsOnASingleLine to "Empty", but the empty function in said test case isn't merged to a single line, because of the very same bug this patch fixes. Fixes https://github.com/llvm/llvm-project/issues/62721 Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D150614 Files: clang/lib/Format/UnwrappedLineFormatter.cpp clang/unittests/Format/FormatTest.cpp Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -13831,6 +13831,20 @@ "}", format("A()\n:b(0)\n{\n}", NoColumnLimit)); + FormatStyle NoColumnLimitWrapAfterFunction = NoColumnLimit; + NoColumnLimitWrapAfterFunction.BreakBeforeBraces = FormatStyle::BS_Custom; + NoColumnLimitWrapAfterFunction.BraceWrapping.AfterFunction = true; + verifyFormat("class C {\n" + "#pragma foo\n" + " int foo { return 0; }\n" + "};", + NoColumnLimitWrapAfterFunction); + verifyFormat("class C {\n" + "#pragma foo\n" + " void foo {}\n" + "};", + NoColumnLimitWrapAfterFunction); + FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; @@ -20119,9 +20133,7 @@ " int i = 5;\n" " }\n" "#ifdef _DEBUG\n" - "void bar()\n" - " {\n" - " }\n" + "void bar() {}\n" "#else\n" "void bar()\n" " {\n" Index: clang/lib/Format/UnwrappedLineFormatter.cpp =================================================================== --- clang/lib/Format/UnwrappedLineFormatter.cpp +++ clang/lib/Format/UnwrappedLineFormatter.cpp @@ -888,9 +888,14 @@ } bool containsMustBreak(const AnnotatedLine *Line) { - for (const FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) + for (const FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) { + // Ignore the first token, because in this situation, it applies more + // to the last token of the previous line. + if (Tok == Line->First) + continue; if (Tok->MustBreakBefore) return true; + } return false; }
Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -13831,6 +13831,20 @@ "}", format("A()\n:b(0)\n{\n}", NoColumnLimit)); + FormatStyle NoColumnLimitWrapAfterFunction = NoColumnLimit; + NoColumnLimitWrapAfterFunction.BreakBeforeBraces = FormatStyle::BS_Custom; + NoColumnLimitWrapAfterFunction.BraceWrapping.AfterFunction = true; + verifyFormat("class C {\n" + "#pragma foo\n" + " int foo { return 0; }\n" + "};", + NoColumnLimitWrapAfterFunction); + verifyFormat("class C {\n" + "#pragma foo\n" + " void foo {}\n" + "};", + NoColumnLimitWrapAfterFunction); + FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; @@ -20119,9 +20133,7 @@ " int i = 5;\n" " }\n" "#ifdef _DEBUG\n" - "void bar()\n" - " {\n" - " }\n" + "void bar() {}\n" "#else\n" "void bar()\n" " {\n" Index: clang/lib/Format/UnwrappedLineFormatter.cpp =================================================================== --- clang/lib/Format/UnwrappedLineFormatter.cpp +++ clang/lib/Format/UnwrappedLineFormatter.cpp @@ -888,9 +888,14 @@ } bool containsMustBreak(const AnnotatedLine *Line) { - for (const FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) + for (const FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) { + // Ignore the first token, because in this situation, it applies more + // to the last token of the previous line. + if (Tok == Line->First) + continue; if (Tok->MustBreakBefore) return true; + } return false; }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits