[clang] a852869 - [clang-format] Fix a bug in parsing function/variable names
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 13 19:35:41 PST 2023
Author: Gedare Bloom
Date: 2023-11-13T19:35:28-08:00
New Revision: a852869398af71141d9422ab3e53a11433a3791f
URL: https://github.com/llvm/llvm-project/commit/a852869398af71141d9422ab3e53a11433a3791f
DIFF: https://github.com/llvm/llvm-project/commit/a852869398af71141d9422ab3e53a11433a3791f.diff
LOG: [clang-format] Fix a bug in parsing function/variable names
Function and variable names are not detected when there is a
__attribute__((x)) preceding the name.
Fixes #64137.
Differential Revision: https://reviews.llvm.org/D156370
Added:
Modified:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 03f3c3583f2ec44..aebea9d8695ffc3 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2246,8 +2246,9 @@ class AnnotatingParser {
PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
}
- if (PreviousNotConst->is(tok::r_paren) &&
- PreviousNotConst->is(TT_TypeDeclarationParen)) {
+ if ((PreviousNotConst->is(tok::r_paren) &&
+ PreviousNotConst->is(TT_TypeDeclarationParen)) ||
+ PreviousNotConst->is(TT_AttributeRParen)) {
return true;
}
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index f531e6da6a5c31b..a12a20359c2fad2 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -16394,8 +16394,10 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
verifyFormat("int f ();", SpaceFuncDecl);
verifyFormat("void f(int a, T b) {}", SpaceFuncDecl);
+ verifyFormat("void __attribute__((asdf)) f(int a, T b) {}", SpaceFuncDecl);
verifyFormat("A::A() : a(1) {}", SpaceFuncDecl);
verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl);
+ verifyFormat("void __attribute__((asdf)) f ();", SpaceFuncDecl);
verifyFormat("#define A(x) x", SpaceFuncDecl);
verifyFormat("#define A (x) x", SpaceFuncDecl);
verifyFormat("#if defined(x)\n"
@@ -16429,8 +16431,10 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
verifyFormat("int f();", SpaceFuncDef);
verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
+ verifyFormat("void __attribute__((asdf)) f (int a, T b) {}", SpaceFuncDef);
verifyFormat("A::A() : a(1) {}", SpaceFuncDef);
verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
+ verifyFormat("void __attribute__((asdf)) f();", SpaceFuncDef);
verifyFormat("#define A(x) x", SpaceFuncDef);
verifyFormat("#define A (x) x", SpaceFuncDef);
verifyFormat("#if defined(x)\n"
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index ed730307074963f..b4b42fc82238f8a 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2313,6 +2313,7 @@ TEST_F(TokenAnnotatorTest, UnderstandsJavaScript) {
TEST_F(TokenAnnotatorTest, UnderstandsAttributes) {
auto Tokens = annotate("bool foo __attribute__((unused));");
ASSERT_EQ(Tokens.size(), 10u) << Tokens;
+ EXPECT_TOKEN(Tokens[1], tok::identifier, TT_StartOfName);
EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_AttributeLParen);
EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_Unknown);
EXPECT_TOKEN(Tokens[6], tok::r_paren, TT_Unknown);
@@ -2323,6 +2324,22 @@ TEST_F(TokenAnnotatorTest, UnderstandsAttributes) {
EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_AttributeLParen);
EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_AttributeRParen);
+ Tokens = annotate("bool __attribute__((unused)) foo;");
+ ASSERT_EQ(Tokens.size(), 10u) << Tokens;
+ EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_AttributeLParen);
+ EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_Unknown);
+ 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_StartOfName);
+
+ Tokens = annotate("void __attribute__((x)) Foo();");
+ ASSERT_EQ(Tokens.size(), 12u) << Tokens;
+ EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_AttributeLParen);
+ EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_Unknown);
+ 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);
+
FormatStyle Style = getLLVMStyle();
Style.AttributeMacros.push_back("FOO");
Tokens = annotate("bool foo FOO(unused);", Style);
More information about the cfe-commits
mailing list