[clang] [clang-format] Fix a bug in annotating FunctionDeclarationName (PR #85361)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 14 22:29:56 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Owen Pan (owenca)
<details>
<summary>Changes</summary>
A name is not a FunctionDeclarationName if it's preceded by an Objective-C keyword.
---
Full diff: https://github.com/llvm/llvm-project/pull/85361.diff
2 Files Affected:
- (modified) clang/lib/Format/TokenAnnotator.cpp (+14-8)
- (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+5)
``````````diff
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index e464c2b5731a35..1342d37a147915 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3595,6 +3595,13 @@ static bool isFunctionDeclarationName(const FormatToken &Current,
if (!Current.Tok.getIdentifierInfo())
return false;
+ const auto &Previous = *Current.Previous;
+
+ if (const auto *PrevPrev = Previous.Previous;
+ PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
+ return false;
+ }
+
auto skipOperatorName = [](const FormatToken *Next) -> const FormatToken * {
for (; Next; Next = Next->Next) {
if (Next->is(TT_OverloadedOperatorLParen))
@@ -3633,18 +3640,17 @@ static bool isFunctionDeclarationName(const FormatToken &Current,
// Find parentheses of parameter list.
const FormatToken *Next = Current.Next;
if (Current.is(tok::kw_operator)) {
- const auto *Previous = Current.Previous;
- if (Previous->Tok.getIdentifierInfo() &&
- !Previous->isOneOf(tok::kw_return, tok::kw_co_return)) {
+ if (Previous.Tok.getIdentifierInfo() &&
+ !Previous.isOneOf(tok::kw_return, tok::kw_co_return)) {
return true;
}
- if (Previous->is(tok::r_paren) && Previous->is(TT_TypeDeclarationParen)) {
- assert(Previous->MatchingParen);
- assert(Previous->MatchingParen->is(tok::l_paren));
- assert(Previous->MatchingParen->is(TT_TypeDeclarationParen));
+ if (Previous.is(tok::r_paren) && Previous.is(TT_TypeDeclarationParen)) {
+ assert(Previous.MatchingParen);
+ assert(Previous.MatchingParen->is(tok::l_paren));
+ assert(Previous.MatchingParen->is(TT_TypeDeclarationParen));
return true;
}
- if (!Previous->isPointerOrReference() && Previous->isNot(TT_TemplateCloser))
+ if (!Previous.isPointerOrReference() && Previous.isNot(TT_TemplateCloser))
return false;
Next = skipOperatorName(Next);
} else {
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 21c18a03a4fc7f..b30ea64201bf8d 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2645,6 +2645,11 @@ TEST_F(TokenAnnotatorTest, StartOfName) {
EXPECT_TOKEN(Tokens[2], tok::identifier, TT_Unknown);
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_Unknown);
EXPECT_TOKEN(Tokens[4], tok::identifier, TT_StartOfName);
+
+ Tokens = annotate("@interface NSCoder (TestCoder)");
+ ASSERT_EQ(Tokens.size(), 7u) << Tokens;
+ EXPECT_TOKEN(Tokens[0], tok::at, TT_ObjCDecl);
+ EXPECT_TOKEN(Tokens[2], tok::identifier, TT_StartOfName);
}
TEST_F(TokenAnnotatorTest, BraceKind) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/85361
More information about the cfe-commits
mailing list