[clang] [clang-format][NFC] Annotate function decl l_paren (PR #97938)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Sat Jul 6 22:23:26 PDT 2024
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/97938
None
>From eb53318b3c5dec2452c4a0d5aed7944ebdd99380 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Sat, 6 Jul 2024 22:21:26 -0700
Subject: [PATCH] [clang-format][NFC] Annotate function decl l_paren
---
clang/lib/Format/FormatToken.h | 1 +
clang/lib/Format/TokenAnnotator.cpp | 6 ++++++
clang/unittests/Format/TokenAnnotatorTest.cpp | 18 ++++++++++++++++++
3 files changed, 25 insertions(+)
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..d453fd6d4d224 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3864,6 +3864,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..d109bf2ba5327 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);
}
@@ -2836,6 +2844,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 +2925,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 +2933,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 +2941,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 +2949,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 +2957,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 +2965,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 +2973,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 +2982,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 +2992,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);
More information about the cfe-commits
mailing list