[PATCH] D140835: [clang-format] Improve UnwrappedLineParser::mightFitOnOneLine()

Owen Pan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Jan 1 19:21:43 PST 2023


owenpan created this revision.
owenpan added reviewers: HazardyKnusperkeks, MyDeveloperDay, rymiel.
owenpan added a project: clang-format.
Herald added a project: All.
owenpan requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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 https://github.com/llvm/llvm-project/issues/59778.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D140835

Files:
  clang/lib/Format/UnwrappedLineParser.cpp
  clang/unittests/Format/BracesRemoverTest.cpp


Index: clang/unittests/Format/BracesRemoverTest.cpp
===================================================================
--- clang/unittests/Format/BracesRemoverTest.cpp
+++ clang/unittests/Format/BracesRemoverTest.cpp
@@ -828,6 +828,17 @@
                "}",
                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 = "
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -836,6 +836,11 @@
     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++];


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140835.485838.patch
Type: text/x-patch
Size: 1293 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230102/d28694ce/attachment.bin>


More information about the cfe-commits mailing list