Author: Owen Pan Date: 2023-01-05T13:23:23-08:00 New Revision: 5751c439be7d00427991e8503e618d0c27a56f89
URL: https://github.com/llvm/llvm-project/commit/5751c439be7d00427991e8503e618d0c27a56f89 DIFF: https://github.com/llvm/llvm-project/commit/5751c439be7d00427991e8503e618d0c27a56f89.diff LOG: [clang-format] Improve UnwrappedLineParser::mightFitOnOneLine() Account for an r_brace that precedes an "else if" statement when calculating whether the line might fit on one line if the r_brace is removed. Fixes #59778. Differential Revision: https://reviews.llvm.org/D140835 Added: Modified: clang/lib/Format/UnwrappedLineParser.cpp clang/unittests/Format/BracesRemoverTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 3be4ef7ca6a1a..8e1ea06779022 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -836,6 +836,11 @@ bool UnwrappedLineParser::mightFitOnOneLine( Length -= OpeningBrace->TokenText.size() + 1; } + if (const auto *FirstToken = Line.First; FirstToken->is(tok::r_brace)) { + assert(!OpeningBrace || OpeningBrace->is(TT_ControlStatementLBrace)); + Length -= FirstToken->TokenText.size() + 1; + } + Index = 0; for (auto &Token : Tokens) { const auto &SavedToken = SavedTokens[Index++]; diff --git a/clang/unittests/Format/BracesRemoverTest.cpp b/clang/unittests/Format/BracesRemoverTest.cpp index 57cf40d4c9e95..ba0b3eb53d155 100644 --- a/clang/unittests/Format/BracesRemoverTest.cpp +++ b/clang/unittests/Format/BracesRemoverTest.cpp @@ -828,6 +828,17 @@ TEST_F(BracesRemoverTest, RemoveBraces) { "}", Style); + verifyFormat("if (foo)\n" + " f();\n" + "else if (bar || baz)\n" + " g();", + "if (foo) {\n" + " f();\n" + "} else if (bar || baz) {\n" + " g();\n" + "}", + Style); + Style.ColumnLimit = 0; verifyFormat("if (a)\n" " b234567890223456789032345678904234567890 = " _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits