[clang] [clang-format] Fix parsing of `operator<() {}` (PR #75144)

via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 13 04:36:15 PST 2023


https://github.com/XDeme updated https://github.com/llvm/llvm-project/pull/75144

>From dddc20d967498c739baedb8d67303a28596f09e0 Mon Sep 17 00:00:00 2001
From: XDeme <fernando.tagawa.gamail.com at gmail.com>
Date: Tue, 12 Dec 2023 03:06:56 -0300
Subject: [PATCH 1/3] Fix operator<() parsing

---
 clang/lib/Format/TokenAnnotator.cpp           |  9 +++++++++
 clang/unittests/Format/FormatTest.cpp         |  7 +++++++
 clang/unittests/Format/TokenAnnotatorTest.cpp | 11 +++++++++++
 3 files changed, 27 insertions(+)

diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index eaccb5881ca30f..957612088c7bb0 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -23,6 +23,9 @@
 
 namespace clang {
 namespace format {
+static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
+                                      const AnnotatedLine &Line,
+                                      FormatToken *&ClosingParen);
 
 static bool mustBreakAfterAttributes(const FormatToken &Tok,
                                      const FormatStyle &Style) {
@@ -164,6 +167,12 @@ class AnnotatingParser {
                TT_OverloadedOperatorLParen))) {
         return false;
       }
+      FormatToken *ClosingParen = nullptr;
+      if (Previous.Previous->is(tok::kw_operator) &&
+          isFunctionDeclarationName(Style.isCpp(), *Previous.Previous, Line,
+                                    ClosingParen)) {
+        return false;
+      }
     }
 
     FormatToken *Left = CurrentToken->Previous;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 24b2fd599dc397..79013a473a7c2e 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -11727,6 +11727,13 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
                "  void func(type &a) { a & member; }\n"
                "  anotherType &member;\n"
                "}");
+
+  Style.ReferenceAlignment = FormatStyle::RAS_Left;
+  verifyFormat("class Foo {\n"
+               "  void operator<(Foo&) {}\n"
+               "  Foo& f;\n"
+               "};",
+               Style);
 }
 
 TEST_F(FormatTest, UnderstandsAttributes) {
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 65b1f0f4b57659..58a782f909d6aa 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -298,6 +298,17 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
   ASSERT_EQ(Tokens.size(), 12u) << Tokens;
   EXPECT_TOKEN(Tokens[2], tok::identifier, TT_TypeName);
   EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
+
+  Tokens = annotate("class Foo {\n"
+                    "void operator<(Foo&) {}\n"
+                    "Foo& f;\n"
+                    "};");
+  ASSERT_EQ(Tokens.size(), 19u) << Tokens;
+  EXPECT_TOKEN(Tokens[4], tok::kw_operator, TT_FunctionDeclarationName);
+  EXPECT_TOKEN(Tokens[5], tok::less, TT_OverloadedOperator);
+  EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_OverloadedOperatorLParen);
+  EXPECT_TOKEN(Tokens[10], tok::l_brace, TT_FunctionLBrace);
+  EXPECT_TOKEN(Tokens[13], tok::amp, TT_PointerOrReference);
 }
 
 TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {

>From 8e5f57d51f4d542ff6bb4bf5faab04ceb709f8d0 Mon Sep 17 00:00:00 2001
From: XDeme <fernando.tagawa.gamail.com at gmail.com>
Date: Tue, 12 Dec 2023 23:39:52 -0300
Subject: [PATCH 2/3] Addresses comments

---
 clang/lib/Format/TokenAnnotator.cpp | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 957612088c7bb0..bcf46d7ef46029 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -23,9 +23,6 @@
 
 namespace clang {
 namespace format {
-static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
-                                      const AnnotatedLine &Line,
-                                      FormatToken *&ClosingParen);
 
 static bool mustBreakAfterAttributes(const FormatToken &Tok,
                                      const FormatStyle &Style) {
@@ -167,10 +164,8 @@ class AnnotatingParser {
                TT_OverloadedOperatorLParen))) {
         return false;
       }
-      FormatToken *ClosingParen = nullptr;
       if (Previous.Previous->is(tok::kw_operator) &&
-          isFunctionDeclarationName(Style.isCpp(), *Previous.Previous, Line,
-                                    ClosingParen)) {
+          CurrentToken->is(tok::l_paren)) {
         return false;
       }
     }

>From 188958463343f930c7b0a7f350356873664d556c Mon Sep 17 00:00:00 2001
From: XDeme <fernando.tagawa.gamail.com at gmail.com>
Date: Wed, 13 Dec 2023 09:34:53 -0300
Subject: [PATCH 3/3] Addresses comments

---
 clang/unittests/Format/FormatTest.cpp         |  7 -------
 clang/unittests/Format/TokenAnnotatorTest.cpp | 10 +++++-----
 2 files changed, 5 insertions(+), 12 deletions(-)

diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 79013a473a7c2e..24b2fd599dc397 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -11727,13 +11727,6 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
                "  void func(type &a) { a & member; }\n"
                "  anotherType &member;\n"
                "}");
-
-  Style.ReferenceAlignment = FormatStyle::RAS_Left;
-  verifyFormat("class Foo {\n"
-               "  void operator<(Foo&) {}\n"
-               "  Foo& f;\n"
-               "};",
-               Style);
 }
 
 TEST_F(FormatTest, UnderstandsAttributes) {
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 58a782f909d6aa..8e6935319b2f3d 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -300,15 +300,15 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
   EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
 
   Tokens = annotate("class Foo {\n"
-                    "void operator<(Foo&) {}\n"
-                    "Foo& f;\n"
+                    "  void operator<() {}\n"
+                    "  Foo &f;\n"
                     "};");
-  ASSERT_EQ(Tokens.size(), 19u) << Tokens;
+  ASSERT_EQ(Tokens.size(), 17u) << Tokens;
   EXPECT_TOKEN(Tokens[4], tok::kw_operator, TT_FunctionDeclarationName);
   EXPECT_TOKEN(Tokens[5], tok::less, TT_OverloadedOperator);
   EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_OverloadedOperatorLParen);
-  EXPECT_TOKEN(Tokens[10], tok::l_brace, TT_FunctionLBrace);
-  EXPECT_TOKEN(Tokens[13], tok::amp, TT_PointerOrReference);
+  EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_FunctionLBrace);
+  EXPECT_TOKEN(Tokens[11], tok::amp, TT_PointerOrReference);
 }
 
 TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {



More information about the cfe-commits mailing list