[PATCH] D121756: [clang-format] Clean up code looking for if statements

Owen Pan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 22 02:57:36 PDT 2022


owenpan requested changes to this revision.
owenpan added inline comments.


================
Comment at: clang/lib/Format/ContinuationIndenter.cpp:749
+  if (Current.isNot(tok::comment) &&
+      Previous.isConditionLParen(/*IncludeSpecial=*/true)) {
     // Treat the condition inside an if as if it was a second function
----------------
We only checked `for` and `if` before. Now you are also checking `while` and `switch`?


================
Comment at: clang/lib/Format/FormatToken.h:529
+  bool isConditionLParen(bool IncludeSpecial) const {
+    if (!is(tok::l_paren))
+      return false;
----------------



================
Comment at: clang/lib/Format/FormatToken.h:534-539
+    // `for` and `catch` special handling.
+    return Prev &&
+           ((IncludeSpecial && Prev->isOneOf(TT_ForEachMacro, TT_ObjCForIn,
+                                             tok::kw_for, tok::kw_catch)) ||
+            Prev->isOneOf(tok::kw_if, tok::kw_while, tok::kw_switch,
+                          tok::kw_case, tok::kw_constexpr));
----------------
We prefer early returns and shorter conditionals.


================
Comment at: clang/lib/Format/UnwrappedLineParser.cpp:2425
       nextToken();
-    if (FormatTok->is(tok::l_paren))
+    if (FormatTok->Tok.is(tok::l_paren)) {
+      FormatTok->setFinalizedType(TT_ConditionLParen);
----------------



================
Comment at: clang/lib/Format/UnwrappedLineParser.cpp:2719-2721
+  // Those that begin with a for require special treatment because inside the
+  // parentheses is not an expression.
+  bool IsFor = FormatTok->is(tok::kw_for) || FormatTok->is(TT_ForEachMacro);
----------------
You can move the comment to above line 2729 below if you like.


================
Comment at: clang/lib/Format/UnwrappedLineParser.cpp:2729
+  if (FormatTok->is(tok::l_paren)) {
+    if (!IsFor)
+      FormatTok->setFinalizedType(TT_ConditionLParen);
----------------



Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D121756



More information about the cfe-commits mailing list