Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 18, 2023

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Oct 16, 2023

Skip TrailingAnnotation when looking for TrailingReturnArrow.

Fixes #69234.

Skip TrailingAnnotation when looking for TrailingReturnArrow.

Fixes llvm#69234.
@llvmbot
Copy link
Member

llvmbot commented Oct 16, 2023

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

Skip TrailingAnnotation when looking for TrailingReturnArrow.

Fixes #69234.


Full diff: https://github.com/llvm/llvm-project/pull/69249.diff

2 Files Affected:

  • (modified) clang/lib/Format/TokenAnnotator.cpp (+9)
  • (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+4)
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;

@owenca
Copy link
Contributor Author

owenca commented Oct 17, 2023

@HazardyKnusperkeks any other concerns?

@mydeveloperday
Copy link
Contributor

mydeveloperday commented Oct 18, 2023

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Clang-Format misformats Thread Safety Analysis annotations that include an arrow
4 participants