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

Owen Pan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 5 13:23:39 PST 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rG5751c439be7d: [clang-format] Improve UnwrappedLineParser::mightFitOnOneLine() (authored by owenpan).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D140835/new/

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.486665.patch
Type: text/x-patch
Size: 1293 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230105/1b11cbb7/attachment.bin>


More information about the cfe-commits mailing list