[clang] [clang-format] Fix a bug in annotating TrailingReturnArrow (PR #69249)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 16 13:46:19 PDT 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Owen Pan (owenca)
<details>
<summary>Changes</summary>
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
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;
``````````
</details>
https://github.com/llvm/llvm-project/pull/69249
More information about the cfe-commits
mailing list