Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2633,12 +2633,20 @@ class AnnotatingParser {
PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
}

if ((PreviousNotConst->is(tok::r_paren) &&
PreviousNotConst->is(TT_TypeDeclarationParen)) ||
PreviousNotConst->is(TT_AttributeRParen)) {
if (PreviousNotConst->is(tok::r_paren) &&
PreviousNotConst->is(TT_TypeDeclarationParen)) {
return true;
}

auto InTypeDecl = [&]() {
for (auto Next = Tok.Next; Next; Next = Next->Next)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more robust and efficient way would be to annotate the class name in UnwrappedLineParser as most of the work has already been done there. I'll submit a patch soon.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #124891.

if (Next->isOneOf(TT_ClassLBrace, TT_StructLBrace))
return true;
return false;
};
if (PreviousNotConst->is(TT_AttributeRParen) && (!IsCpp || !InTypeDecl()))
return true;

// If is a preprocess keyword like #define.
if (IsPPKeyword)
return false;
Expand Down
9 changes: 9 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3123,6 +3123,15 @@ TEST_F(TokenAnnotatorTest, UnderstandsAttributes) {
EXPECT_TOKEN(Tokens[7], tok::identifier, TT_FunctionDeclarationName);
EXPECT_TOKEN(Tokens[8], tok::l_paren, TT_FunctionDeclarationLParen);

Tokens = annotate("struct __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_Unknown);
EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_StructLBrace);

FormatStyle Style = getLLVMStyle();
Style.AttributeMacros.push_back("FOO");
Tokens = annotate("bool foo FOO(unused);", Style);
Expand Down