[clang] b2ac7f5 - [clang-format][NFC] Annotate function/ctor/dtor declaration l_paren (#97938)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 10 00:10:28 PDT 2024
Author: Owen Pan
Date: 2024-07-10T00:10:24-07:00
New Revision: b2ac7f52fa95ccc478a478910ccd42730e8ea845
URL: https://github.com/llvm/llvm-project/commit/b2ac7f52fa95ccc478a478910ccd42730e8ea845
DIFF: https://github.com/llvm/llvm-project/commit/b2ac7f52fa95ccc478a478910ccd42730e8ea845.diff
LOG: [clang-format][NFC] Annotate function/ctor/dtor declaration l_paren (#97938)
Added:
Modified:
clang/lib/Format/FormatToken.h
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index 4ffd745bf9307..cc45d5a8c5c1e 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -77,6 +77,7 @@ namespace format {
TYPE(ForEachMacro) \
TYPE(FunctionAnnotationRParen) \
TYPE(FunctionDeclarationName) \
+ TYPE(FunctionDeclarationLParen) \
TYPE(FunctionLBrace) \
TYPE(FunctionLikeOrFreestandingMacro) \
TYPE(FunctionTypeLParen) \
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 0fd0214d16615..1fd309afd697e 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3539,7 +3539,8 @@ static unsigned maxNestingDepth(const AnnotatedLine &Line) {
// Returns the name of a function with no return type, e.g. a constructor or
// destructor.
-static FormatToken *getFunctionName(const AnnotatedLine &Line) {
+static FormatToken *getFunctionName(const AnnotatedLine &Line,
+ FormatToken *&OpeningParen) {
for (FormatToken *Tok = Line.getFirstNonComment(), *Name = nullptr; Tok;
Tok = Tok->getNextNonComment()) {
// Skip C++11 attributes both before and after the function name.
@@ -3552,10 +3553,12 @@ static FormatToken *getFunctionName(const AnnotatedLine &Line) {
// Make sure the name is followed by a pair of parentheses.
if (Name) {
- return Tok->is(tok::l_paren) && Tok->isNot(TT_FunctionTypeLParen) &&
- Tok->MatchingParen
- ? Name
- : nullptr;
+ if (Tok->is(tok::l_paren) && Tok->isNot(TT_FunctionTypeLParen) &&
+ Tok->MatchingParen) {
+ OpeningParen = Tok;
+ return Name;
+ }
+ return nullptr;
}
// Skip keywords that may precede the constructor/destructor name.
@@ -3632,10 +3635,13 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
ExprParser.parse();
if (IsCpp) {
- auto *Tok = getFunctionName(Line);
+ FormatToken *OpeningParen = nullptr;
+ auto *Tok = getFunctionName(Line, OpeningParen);
if (Tok && ((!Scopes.empty() && Scopes.back() == ST_Class) ||
Line.endsWith(TT_FunctionLBrace) || isCtorOrDtorName(Tok))) {
Tok->setFinalizedType(TT_CtorDtorDeclName);
+ assert(OpeningParen);
+ OpeningParen->setFinalizedType(TT_FunctionDeclarationLParen);
}
}
@@ -3864,6 +3870,12 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
Tok->setFinalizedType(TT_FunctionDeclarationName);
LineIsFunctionDeclaration = true;
SeenName = true;
+ if (ClosingParen) {
+ auto *OpeningParen = ClosingParen->MatchingParen;
+ assert(OpeningParen);
+ if (OpeningParen->is(TT_Unknown))
+ OpeningParen->setType(TT_FunctionDeclarationLParen);
+ }
break;
}
}
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 5d83d8a0c4429..c5e8aa72cd2cb 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -1037,6 +1037,7 @@ TEST_F(TokenAnnotatorTest, UnderstandsRequiresClausesAndConcepts) {
EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresClause);
EXPECT_TRUE(Tokens[9]->ClosesRequiresClause);
EXPECT_TOKEN(Tokens[11], tok::identifier, TT_FunctionDeclarationName);
+ EXPECT_TOKEN(Tokens[12], tok::l_paren, TT_FunctionDeclarationLParen);
Tokens = annotate("template <typename T>\n"
"requires Bar<T>\n"
@@ -1045,6 +1046,7 @@ TEST_F(TokenAnnotatorTest, UnderstandsRequiresClausesAndConcepts) {
EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresClause);
EXPECT_TRUE(Tokens[9]->ClosesRequiresClause);
EXPECT_TOKEN(Tokens[14], tok::identifier, TT_FunctionDeclarationName);
+ EXPECT_TOKEN(Tokens[15], tok::l_paren, TT_FunctionDeclarationLParen);
Tokens = annotate("template <typename T>\n"
"struct S {\n"
@@ -1914,15 +1916,18 @@ TEST_F(TokenAnnotatorTest, UnderstandsFunctionDeclarationNames) {
auto Tokens = annotate("void f [[noreturn]] ();");
ASSERT_EQ(Tokens.size(), 11u) << Tokens;
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
+ EXPECT_TOKEN(Tokens[7], tok::l_paren, TT_FunctionDeclarationLParen);
Tokens = annotate("void f [[noreturn]] () {}");
ASSERT_EQ(Tokens.size(), 12u) << Tokens;
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
+ EXPECT_TOKEN(Tokens[7], tok::l_paren, TT_FunctionDeclarationLParen);
Tokens = annotate("#define FOO Foo::\n"
"FOO Foo();");
ASSERT_EQ(Tokens.size(), 11u) << Tokens;
EXPECT_TOKEN(Tokens[6], tok::identifier, TT_FunctionDeclarationName);
+ EXPECT_TOKEN(Tokens[7], tok::l_paren, TT_FunctionDeclarationLParen);
Tokens = annotate("struct Foo {\n"
" Bar (*func)();\n"
@@ -1934,16 +1939,19 @@ TEST_F(TokenAnnotatorTest, UnderstandsFunctionDeclarationNames) {
Tokens = annotate("void instanceof();");
ASSERT_EQ(Tokens.size(), 6u);
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
+ EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_FunctionDeclarationLParen);
Tokens = annotate("int iso_time(time_t);");
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
+ EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_FunctionDeclarationLParen);
auto Style = getLLVMStyle();
Style.TypeNames.push_back("MyType");
Tokens = annotate("int iso_time(MyType);", Style);
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
+ EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_FunctionDeclarationLParen);
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_TypeName);
}
@@ -1951,49 +1959,59 @@ TEST_F(TokenAnnotatorTest, UnderstandsCtorAndDtorDeclNames) {
auto Tokens = annotate("class Foo { public: Foo(); };");
ASSERT_EQ(Tokens.size(), 12u) << Tokens;
EXPECT_TOKEN(Tokens[5], tok::identifier, TT_CtorDtorDeclName);
+ EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_FunctionDeclarationLParen);
Tokens = annotate("class Foo { public: ~Foo(); };");
ASSERT_EQ(Tokens.size(), 13u) << Tokens;
EXPECT_TOKEN(Tokens[6], tok::identifier, TT_CtorDtorDeclName);
+ EXPECT_TOKEN(Tokens[7], tok::l_paren, TT_FunctionDeclarationLParen);
Tokens = annotate("struct Foo { [[deprecated]] Foo() {} };");
ASSERT_EQ(Tokens.size(), 16u) << Tokens;
EXPECT_TOKEN(Tokens[8], tok::identifier, TT_CtorDtorDeclName);
+ EXPECT_TOKEN(Tokens[9], tok::l_paren, TT_FunctionDeclarationLParen);
EXPECT_TOKEN(Tokens[11], tok::l_brace, TT_FunctionLBrace);
Tokens = annotate("struct Foo { [[deprecated]] ~Foo() {} };");
ASSERT_EQ(Tokens.size(), 17u) << Tokens;
EXPECT_TOKEN(Tokens[9], tok::identifier, TT_CtorDtorDeclName);
+ EXPECT_TOKEN(Tokens[10], tok::l_paren, TT_FunctionDeclarationLParen);
EXPECT_TOKEN(Tokens[12], tok::l_brace, TT_FunctionLBrace);
Tokens = annotate("struct Foo { Foo() [[deprecated]] {} };");
ASSERT_EQ(Tokens.size(), 16u) << Tokens;
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_CtorDtorDeclName);
+ EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_FunctionDeclarationLParen);
EXPECT_TOKEN(Tokens[11], tok::l_brace, TT_FunctionLBrace);
Tokens = annotate("struct Foo { ~Foo() [[deprecated]] {} };");
ASSERT_EQ(Tokens.size(), 17u) << Tokens;
EXPECT_TOKEN(Tokens[4], tok::identifier, TT_CtorDtorDeclName);
+ EXPECT_TOKEN(Tokens[5], tok::l_paren, TT_FunctionDeclarationLParen);
EXPECT_TOKEN(Tokens[12], tok::l_brace, TT_FunctionLBrace);
Tokens = annotate("struct Foo { [[deprecated]] explicit Foo() {} };");
ASSERT_EQ(Tokens.size(), 17u) << Tokens;
EXPECT_TOKEN(Tokens[9], tok::identifier, TT_CtorDtorDeclName);
+ EXPECT_TOKEN(Tokens[10], tok::l_paren, TT_FunctionDeclarationLParen);
EXPECT_TOKEN(Tokens[12], tok::l_brace, TT_FunctionLBrace);
Tokens = annotate("struct Foo { virtual [[deprecated]] ~Foo() {} };");
ASSERT_EQ(Tokens.size(), 18u) << Tokens;
EXPECT_TOKEN(Tokens[10], tok::identifier, TT_CtorDtorDeclName);
+ EXPECT_TOKEN(Tokens[11], tok::l_paren, TT_FunctionDeclarationLParen);
EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace);
Tokens = annotate("Foo::Foo() {}");
ASSERT_EQ(Tokens.size(), 8u) << Tokens;
EXPECT_TOKEN(Tokens[2], tok::identifier, TT_CtorDtorDeclName);
+ EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_FunctionDeclarationLParen);
EXPECT_TOKEN(Tokens[5], tok::l_brace, TT_FunctionLBrace);
Tokens = annotate("Foo::~Foo() {}");
ASSERT_EQ(Tokens.size(), 9u) << Tokens;
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_CtorDtorDeclName);
+ EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_FunctionDeclarationLParen);
EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_FunctionLBrace);
Tokens = annotate("struct Test {\n"
@@ -2004,6 +2022,7 @@ TEST_F(TokenAnnotatorTest, UnderstandsCtorAndDtorDeclNames) {
"};");
ASSERT_EQ(Tokens.size(), 25u) << Tokens;
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_CtorDtorDeclName);
+ EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_FunctionDeclarationLParen);
EXPECT_TOKEN(Tokens[14], tok::identifier, TT_Unknown);
}
@@ -2836,6 +2855,7 @@ TEST_F(TokenAnnotatorTest, UnderstandsAttributes) {
EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_Unknown);
EXPECT_TOKEN(Tokens[6], tok::r_paren, TT_AttributeRParen);
EXPECT_TOKEN(Tokens[7], tok::identifier, TT_FunctionDeclarationName);
+ EXPECT_TOKEN(Tokens[8], tok::l_paren, TT_FunctionDeclarationLParen);
FormatStyle Style = getLLVMStyle();
Style.AttributeMacros.push_back("FOO");
@@ -2916,6 +2936,7 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
auto Tokens = annotate("void f() {};");
ASSERT_EQ(Tokens.size(), 8u) << Tokens;
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
+ EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_FunctionDeclarationLParen);
EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_FunctionLBrace);
EXPECT_BRACE_KIND(Tokens[4], BK_Block);
EXPECT_BRACE_KIND(Tokens[5], BK_Block);
@@ -2923,6 +2944,7 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
Tokens = annotate("class Foo<int> f() {}");
ASSERT_EQ(Tokens.size(), 11u) << Tokens;
EXPECT_TOKEN(Tokens[5], tok::identifier, TT_FunctionDeclarationName);
+ EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_FunctionDeclarationLParen);
EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_FunctionLBrace);
EXPECT_BRACE_KIND(Tokens[8], BK_Block);
EXPECT_BRACE_KIND(Tokens[9], BK_Block);
@@ -2930,6 +2952,7 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
Tokens = annotate("template <typename T> class Foo<T> f() {}");
ASSERT_EQ(Tokens.size(), 16u) << Tokens;
EXPECT_TOKEN(Tokens[10], tok::identifier, TT_FunctionDeclarationName);
+ EXPECT_TOKEN(Tokens[11], tok::l_paren, TT_FunctionDeclarationLParen);
EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace);
EXPECT_BRACE_KIND(Tokens[13], BK_Block);
EXPECT_BRACE_KIND(Tokens[14], BK_Block);
@@ -2937,6 +2960,7 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
Tokens = annotate("void f() override {};");
ASSERT_EQ(Tokens.size(), 9u) << Tokens;
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
+ EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_FunctionDeclarationLParen);
EXPECT_TOKEN(Tokens[5], tok::l_brace, TT_FunctionLBrace);
EXPECT_BRACE_KIND(Tokens[5], BK_Block);
EXPECT_BRACE_KIND(Tokens[6], BK_Block);
@@ -2944,6 +2968,7 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
Tokens = annotate("void f() noexcept(false) {};");
ASSERT_EQ(Tokens.size(), 12u) << Tokens;
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
+ EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_FunctionDeclarationLParen);
EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_FunctionLBrace);
EXPECT_BRACE_KIND(Tokens[8], BK_Block);
EXPECT_BRACE_KIND(Tokens[9], BK_Block);
@@ -2951,6 +2976,7 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
Tokens = annotate("auto f() -> void {};");
ASSERT_EQ(Tokens.size(), 10u) << Tokens;
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
+ EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_FunctionDeclarationLParen);
EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_FunctionLBrace);
EXPECT_BRACE_KIND(Tokens[6], BK_Block);
EXPECT_BRACE_KIND(Tokens[7], BK_Block);
@@ -2958,6 +2984,7 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
Tokens = annotate("void f() { /**/ };");
ASSERT_EQ(Tokens.size(), 9u) << Tokens;
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
+ EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_FunctionDeclarationLParen);
EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_FunctionLBrace);
EXPECT_BRACE_KIND(Tokens[4], BK_Block);
EXPECT_BRACE_KIND(Tokens[6], BK_Block);
@@ -2966,6 +2993,7 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
"};");
ASSERT_EQ(Tokens.size(), 9u) << Tokens;
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
+ EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_FunctionDeclarationLParen);
EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_FunctionLBrace);
EXPECT_BRACE_KIND(Tokens[4], BK_Block);
EXPECT_BRACE_KIND(Tokens[6], BK_Block);
@@ -2975,6 +3003,7 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
"};");
ASSERT_EQ(Tokens.size(), 9u) << Tokens;
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
+ EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_FunctionDeclarationLParen);
EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_FunctionLBrace);
EXPECT_BRACE_KIND(Tokens[4], BK_Block);
EXPECT_BRACE_KIND(Tokens[6], BK_Block);
@@ -2985,10 +3014,12 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
"};");
ASSERT_EQ(Tokens.size(), 19u) << Tokens;
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_CtorDtorDeclName);
+ EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_FunctionDeclarationLParen);
EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_FunctionLBrace);
EXPECT_BRACE_KIND(Tokens[6], BK_Block);
EXPECT_BRACE_KIND(Tokens[7], BK_Block);
EXPECT_TOKEN(Tokens[10], tok::identifier, TT_CtorDtorDeclName);
+ EXPECT_TOKEN(Tokens[11], tok::l_paren, TT_FunctionDeclarationLParen);
EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace);
EXPECT_BRACE_KIND(Tokens[13], BK_Block);
EXPECT_BRACE_KIND(Tokens[14], BK_Block);
@@ -3211,6 +3242,7 @@ TEST_F(TokenAnnotatorTest, FunctionTryBlock) {
"}");
ASSERT_EQ(Tokens.size(), 45u);
EXPECT_TOKEN(Tokens[2], tok::identifier, TT_CtorDtorDeclName);
+ EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_FunctionDeclarationLParen);
EXPECT_TOKEN(Tokens[11], tok::colon, TT_CtorInitializerColon);
EXPECT_TOKEN(Tokens[14], tok::l_square, TT_LambdaLSquare);
EXPECT_TOKEN(Tokens[16], tok::arrow, TT_TrailingReturnArrow);
More information about the cfe-commits
mailing list