[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
Fri Jan 12 11:43:45 PST 2024


================
@@ -4007,26 +3992,45 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
     return true;
   }
 
-  // TODO: check consecutive parens
   if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) {
-    if (Right.is(TT_CastRParen) ||
-        (Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen))) {
-      return Style.SpacesInParensOptions.InCStyleCasts !=
-             FormatStyle::SIPCS_Never;
+    const FormatToken *LeftParen =
+        Left.is(tok::l_paren) ? &Left : Right.MatchingParen;
+    const FormatToken *RightParen =
+        LeftParen ? LeftParen->MatchingParen : nullptr;
+
+    auto NotConsecutiveParens = [&](auto *Left, auto *Right) {
+      return Left && Left->Next && Left->Next->isNot(tok::l_paren) && Right &&
+             Right->Previous && Right->Previous->isNot(tok::r_paren);
+    };
+    const auto AddSpace = [&](FormatStyle::SpacesInParensCustomStyle Option) {
+      if (Option == FormatStyle::SIPCS_Always)
+        return true;
+      if (Option == FormatStyle::SIPCS_NonConsecutive) {
+        if (NotConsecutiveParens(LeftParen, RightParen))
+          return true;
+      }
----------------
HazardyKnusperkeks wrote:

```suggestion
      if (Option == FormatStyle::SIPCS_NonConsecutive
        && NotConsecutiveParens(LeftParen, RightParen)) {
          return true;
      }
```

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


More information about the cfe-commits mailing list