[PATCH] D156370: [clang-format] Fix bug with parsing of function/variable names.
Gedare Bloom via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 9 07:21:43 PST 2023
gedare updated this revision to Diff 558061.
gedare added a comment.
Add TokenAnnotator tests and light refactoring.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D156370/new/
https://reviews.llvm.org/D156370
Files:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
Index: clang/unittests/Format/TokenAnnotatorTest.cpp
===================================================================
--- clang/unittests/Format/TokenAnnotatorTest.cpp
+++ clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2313,6 +2313,7 @@
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 @@
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);
Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -16387,8 +16387,10 @@
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"
@@ -16422,8 +16424,10 @@
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"
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2220,7 +2220,8 @@
}
if (PreviousNotConst->is(tok::r_paren) &&
- PreviousNotConst->is(TT_TypeDeclarationParen)) {
+ (PreviousNotConst->isOneOf(TT_TypeDeclarationParen,
+ TT_AttributeRParen))) {
return true;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156370.558061.patch
Type: text/x-patch
Size: 3454 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20231109/5ab59771/attachment.bin>
More information about the cfe-commits
mailing list