[clang] [clang-format] Fix a bug in annotating TrailingReturnArrow (PR #69249)

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 16 14:48:25 PDT 2023


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

>From 25e0a84bd254969fb6930001f6203d4dc56f9871 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Mon, 16 Oct 2023 13:38:09 -0700
Subject: [PATCH 1/2] [clang-format] Fix a bug in annotating
 TrailingReturnArrow

Skip TrailingAnnotation when looking for TrailingReturnArrow.

Fixes #69234.
---
 clang/lib/Format/TokenAnnotator.cpp           | 9 +++++++++
 clang/unittests/Format/TokenAnnotatorTest.cpp | 4 ++++
 2 files changed, 13 insertions(+)

diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 3dd537272e9dad0..9f007125e82c430 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3497,6 +3497,15 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
           Tok->setType(TT_TrailingReturnArrow);
           break;
         }
+        if (Tok->isNot(TT_TrailingAnnotation))
+          continue;
+        const auto *Next = Tok->Next;
+        if (!Next || Next->isNot(tok::l_paren))
+          continue;
+        Tok = Next->MatchingParen;
+        if (Tok)
+          continue;
+        break;
       }
     }
   }
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index e5cc3ed3686b3d3..d386ae9aae4ca61 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -1788,6 +1788,10 @@ TEST_F(TokenAnnotatorTest, UnderstandsTrailingReturnArrow) {
   ASSERT_EQ(Tokens.size(), 15u) << Tokens;
   EXPECT_TOKEN(Tokens[12], tok::arrow, TT_Unknown);
 
+  Tokens = annotate("void f() FOO(foo->bar);");
+  ASSERT_EQ(Tokens.size(), 12u) << Tokens;
+  EXPECT_TOKEN(Tokens[7], tok::arrow, TT_Unknown);
+
   // Mixed
   Tokens = annotate("auto f() -> int { auto a = b()->c; }");
   ASSERT_EQ(Tokens.size(), 18u) << Tokens;

>From c1d6bf4c45482d0eabcf55ea7719366bc0fb8f1c Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Mon, 16 Oct 2023 14:47:57 -0700
Subject: [PATCH 2/2] Simplify the logic at the end of the for loop

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

diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 9f007125e82c430..293f7286abe4202 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3503,9 +3503,8 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
         if (!Next || Next->isNot(tok::l_paren))
           continue;
         Tok = Next->MatchingParen;
-        if (Tok)
-          continue;
-        break;
+        if (!Tok)
+          break;
       }
     }
   }



More information about the cfe-commits mailing list