[clang] Code implementing SpacesInParensOptions.ExceptDoubleParentheses logic (PR #93439)

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Mon May 27 15:14:45 PDT 2024


https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/93439

>From ac03e1506b5ea0d00038501c4f41d5b30c8fa2b3 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Sun, 26 May 2024 22:01:48 -0700
Subject: [PATCH 1/2] Code implementing the
 SpacesInParensOptions.ExceptDoubleParentheses logic

---
 clang/lib/Format/TokenAnnotator.cpp | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 7c4c76a91f2c5..c204d107b12b7 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4346,6 +4346,14 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
        Right.is(tok::r_brace) && Right.isNot(BK_Block))) {
     return Style.SpacesInParensOptions.InEmptyParentheses;
   }
+  if (Style.SpacesInParens == FormatStyle::SIPO_Custom &&
+      Style.SpacesInParensOptions.ExceptDoubleParentheses &&
+      ((Left.is(tok::l_paren) && Right.is(tok::l_paren)) ||
+       (Left.is(tok::r_paren) && Right.is(tok::r_paren)))) {
+    const auto *Tok = Left.MatchingParen;
+    if (Tok && Tok->Previous == Right.MatchingParen)
+      return false;
+  }
   if (Style.SpacesInParensOptions.InConditionalStatements) {
     const FormatToken *LeftParen = nullptr;
     if (Left.is(tok::l_paren))

>From 1e086440c350644ec50a72d1ad3a15d877024ec3 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Mon, 27 May 2024 15:14:38 -0700
Subject: [PATCH 2/2] Improve the logic.

---
 clang/lib/Format/TokenAnnotator.cpp | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index c204d107b12b7..a26900383b256 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4348,11 +4348,12 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
   }
   if (Style.SpacesInParens == FormatStyle::SIPO_Custom &&
       Style.SpacesInParensOptions.ExceptDoubleParentheses &&
-      ((Left.is(tok::l_paren) && Right.is(tok::l_paren)) ||
-       (Left.is(tok::r_paren) && Right.is(tok::r_paren)))) {
-    const auto *Tok = Left.MatchingParen;
-    if (Tok && Tok->Previous == Right.MatchingParen)
+      Left.is(tok::r_paren) && Right.is(tok::r_paren)) {
+    auto *InnerLParen = Left.MatchingParen;
+    if (InnerLParen && InnerLParen->Previous == Right.MatchingParen) {
+      InnerLParen->SpacesRequiredBefore = 0;
       return false;
+    }
   }
   if (Style.SpacesInParensOptions.InConditionalStatements) {
     const FormatToken *LeftParen = nullptr;



More information about the cfe-commits mailing list