[clang] [clang-format] Add SpacesInParensOption for attributes and filtering for repeated parens (PR #77522)

Björn Schäpers via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 23 12:51:52 PST 2024


================
@@ -4006,11 +3993,58 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
   }
 
   if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) {
-    return (Right.is(TT_CastRParen) ||
-            (Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
-               ? Style.SpacesInParensOptions.InCStyleCasts
-               : Style.SpacesInParensOptions.Other;
+    const FormatToken *LeftParen =
+        Left.is(tok::l_paren) ? &Left : Right.MatchingParen;
+    const FormatToken *RightParen =
+        LeftParen ? LeftParen->MatchingParen : nullptr;
+    const auto IsAttributeParen = [](const FormatToken *Paren) {
+      return Paren && Paren->isOneOf(TT_AttributeLParen, TT_AttributeRParen);
+    };
+    auto AddSpaceInDoubleParens = [&]() {
+      const auto *RPrev = RightParen ? RightParen->Previous : nullptr;
+      const auto *LNext = LeftParen->Next;
+      const auto *LPrev = LeftParen->Previous;
+      const bool DoubleParens =
+          RPrev && RPrev->is(tok::r_paren) && LNext && LNext->is(tok::l_paren);
+      auto HasEqualBeforeNextParen = [&]() {
+        auto *Tok = LNext;
+        if (!Tok || !Tok->is(tok::l_paren))
+          return false;
+        while ((Tok = Tok->Next) && !Tok->isOneOf(tok::l_paren, tok::r_paren))
+          if (Tok->is(tok::equal))
+            return true;
+        return false;
+      };
+      const bool SuppressSpace =
+          IsAttributeParen(LeftParen) ||
+          (LPrev && (LPrev->isOneOf(tok::kw___attribute, tok::kw_decltype) ||
+                     (HasEqualBeforeNextParen() &&
+                      (LPrev->isOneOf(tok::kw_if, tok::kw_while) ||
+                       LPrev->endsSequence(tok::kw_constexpr, tok::kw_if)))));
+      return !(DoubleParens && SuppressSpace);
----------------
HazardyKnusperkeks wrote:

```suggestion
      return !DoubleParens || !SuppressSpace;
```

https://github.com/llvm/llvm-project/pull/77522


More information about the cfe-commits mailing list