[PATCH] D121757: [clang-format] Take out common code for parsing blocks

Owen Pan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 24 19:32:10 PDT 2022


owenpan added inline comments.


================
Comment at: clang/lib/Format/UnwrappedLineParser.cpp:2707-2708
+  if (FormatTok->is(tok::l_brace)) {
+    CompoundStatementIndenter Indenter(this, Style, Line->Level);
+    FormatToken *LeftBrace = FormatTok;
+    parseBlock();
----------------
You swapped these two lines. We want to remember the `l_brace` before calling other functions in case they update `FormatTok`.


================
Comment at: clang/lib/Format/UnwrappedLineParser.cpp:2743-2744
 
-  keepAncestorBraces();
-
-  if (FormatTok->is(tok::l_brace)) {
-    FormatToken *LeftBrace = FormatTok;
-    CompoundStatementIndenter Indenter(this, Style, Line->Level);
-    parseBlock();
-    if (Style.RemoveBracesLLVM) {
-      assert(!NestedTooDeep.empty());
-      if (!NestedTooDeep.back())
-        markOptionalBraces(LeftBrace);
-    }
-    addUnwrappedLine();
-  } else {
-    parseUnbracedBody();
-  }
-
-  if (Style.RemoveBracesLLVM)
-    NestedTooDeep.pop_back();
+  parseLoopBody(/*TryRemoveBraces=*/Style.RemoveBracesLLVM,
+                /*WrapRightBrace=*/true);
 }
----------------
Or just the following as they are not default arguments now:
```
  parseLoopBody(Style.RemoveBracesLLVM, true);
```


================
Comment at: clang/lib/Format/UnwrappedLineParser.cpp:2751
 
-  keepAncestorBraces();
-
-  if (FormatTok->is(tok::l_brace)) {
-    CompoundStatementIndenter Indenter(this, Style, Line->Level);
-    parseBlock();
-    if (Style.BraceWrapping.BeforeWhile)
-      addUnwrappedLine();
-  } else {
-    parseUnbracedBody();
-  }
-
-  if (Style.RemoveBracesLLVM)
-    NestedTooDeep.pop_back();
+  parseLoopBody(/*BracesAreoptional=*/false, Style.BraceWrapping.BeforeWhile);
 
----------------
Or just the following:
```
  parseLoopBody(false, Style.BraceWrapping.BeforeWhile);
```


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D121757



More information about the cfe-commits mailing list