Skip to content

[clang-format] Split TT_AttributeParen #67396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 27, 2023
Merged
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
2 changes: 1 addition & 1 deletion clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1336,7 +1336,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
(PreviousNonComment->ClosesTemplateDeclaration ||
PreviousNonComment->ClosesRequiresClause ||
PreviousNonComment->isOneOf(
TT_AttributeParen, TT_AttributeSquare, TT_FunctionAnnotationRParen,
TT_AttributeRParen, TT_AttributeSquare, TT_FunctionAnnotationRParen,
TT_JavaAnnotation, TT_LeadingJavaAnnotation))) ||
(!Style.IndentWrappedFunctionNames &&
NextNonComment->isOneOf(tok::kw_operator, TT_FunctionDeclarationName))) {
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Format/FormatToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ namespace format {
TYPE(ArrayInitializerLSquare) \
TYPE(ArraySubscriptLSquare) \
TYPE(AttributeColon) \
TYPE(AttributeLParen) \
TYPE(AttributeMacro) \
TYPE(AttributeParen) \
TYPE(AttributeRParen) \
TYPE(AttributeSquare) \
TYPE(BinaryOperator) \
TYPE(BitFieldColon) \
Expand Down
30 changes: 17 additions & 13 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ class AnnotatingParser {
// detected one yet.
if (PrevNonComment && OpeningParen.is(TT_Unknown)) {
if (PrevNonComment->is(tok::kw___attribute)) {
OpeningParen.setType(TT_AttributeParen);
OpeningParen.setType(TT_AttributeLParen);
} else if (PrevNonComment->isOneOf(TT_TypenameMacro, tok::kw_decltype,
tok::kw_typeof,
#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) tok::kw___##Trait,
Expand Down Expand Up @@ -475,8 +475,8 @@ class AnnotatingParser {
}
}

if (OpeningParen.is(TT_AttributeParen))
CurrentToken->setType(TT_AttributeParen);
if (OpeningParen.is(TT_AttributeLParen))
CurrentToken->setType(TT_AttributeRParen);
if (OpeningParen.is(TT_TypeDeclarationParen))
CurrentToken->setType(TT_TypeDeclarationParen);
if (OpeningParen.Previous &&
Expand Down Expand Up @@ -2382,11 +2382,15 @@ class AnnotatingParser {
// Strip trailing qualifiers such as const or volatile when checking
// whether the parens could be a cast to a pointer/reference type.
while (T) {
if (T->is(TT_AttributeParen)) {
if (T->is(TT_AttributeRParen)) {
// Handle `x = (foo *__attribute__((foo)))&v;`:
if (T->MatchingParen && T->MatchingParen->Previous &&
T->MatchingParen->Previous->is(tok::kw___attribute)) {
T = T->MatchingParen->Previous->Previous;
assert(T->is(tok::r_paren));
assert(T->MatchingParen);
assert(T->MatchingParen->is(tok::l_paren));
assert(T->MatchingParen->is(TT_AttributeLParen));
if (const auto *Tok = T->MatchingParen->Previous;
Tok && Tok->is(tok::kw___attribute)) {
T = Tok->Previous;
continue;
}
} else if (T->is(TT_AttributeSquare)) {
Expand Down Expand Up @@ -3989,7 +3993,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
// after pointer qualifiers.
if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_After ||
Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
(Left.is(TT_AttributeParen) ||
(Left.is(TT_AttributeRParen) ||
Left.canBePointerOrReferenceQualifier())) {
return true;
}
Expand Down Expand Up @@ -4182,7 +4186,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
spaceRequiredBeforeParens(Right);
}
if ((Left.is(tok::r_paren) && Left.is(TT_AttributeParen)) ||
if (Left.is(TT_AttributeRParen) ||
(Left.is(tok::r_square) && Left.is(TT_AttributeSquare))) {
return true;
}
Expand Down Expand Up @@ -4361,7 +4365,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
return false;
}
// Space in __attribute__((attr)) ::type.
if (Left.is(TT_AttributeParen) && Right.is(tok::coloncolon))
if (Left.is(TT_AttributeRParen) && Right.is(tok::coloncolon))
return true;

if (Left.is(tok::kw_operator))
Expand Down Expand Up @@ -5190,7 +5194,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
}

// Ensure wrapping after __attribute__((XX)) and @interface etc.
if (Left.is(TT_AttributeParen) && Right.is(TT_ObjCDecl))
if (Left.is(TT_AttributeRParen) && Right.is(TT_ObjCDecl))
return true;

if (Left.is(TT_LambdaLBrace)) {
Expand Down Expand Up @@ -5552,8 +5556,8 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
!Style.Cpp11BracedListStyle) {
return false;
}
if (Left.is(tok::l_paren) &&
Left.isOneOf(TT_AttributeParen, TT_TypeDeclarationParen)) {
if (Left.is(TT_AttributeLParen) ||
(Left.is(tok::l_paren) && Left.is(TT_TypeDeclarationParen))) {
return false;
}
if (Left.is(tok::l_paren) && Left.Previous &&
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 @@ -2065,6 +2065,15 @@ TEST_F(TokenAnnotatorTest, UnderstandsJavaScript) {
EXPECT_TOKEN(Tokens[11], tok::r_brace, TT_Unknown);
}

TEST_F(TokenAnnotatorTest, UnderstandsAttributes) {
auto Tokens = annotate("bool foo __attribute__((unused));");
ASSERT_EQ(Tokens.size(), 10u) << Tokens;
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);
EXPECT_TOKEN(Tokens[7], tok::r_paren, TT_AttributeRParen);
}

} // namespace
} // namespace format
} // namespace clang