Skip to content

Commit 3ae6475

Browse files
committed
[clang-format] Split TT_AttributeParen
Replaced TT_AttributeParen with TT_AttributeLParen and TT_AttributeRParen.
1 parent b0e28eb commit 3ae6475

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1336,7 +1336,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
13361336
(PreviousNonComment->ClosesTemplateDeclaration ||
13371337
PreviousNonComment->ClosesRequiresClause ||
13381338
PreviousNonComment->isOneOf(
1339-
TT_AttributeParen, TT_AttributeSquare, TT_FunctionAnnotationRParen,
1339+
TT_AttributeRParen, TT_AttributeSquare, TT_FunctionAnnotationRParen,
13401340
TT_JavaAnnotation, TT_LeadingJavaAnnotation))) ||
13411341
(!Style.IndentWrappedFunctionNames &&
13421342
NextNonComment->isOneOf(tok::kw_operator, TT_FunctionDeclarationName))) {

clang/lib/Format/FormatToken.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ namespace format {
3030
TYPE(ArrayInitializerLSquare) \
3131
TYPE(ArraySubscriptLSquare) \
3232
TYPE(AttributeColon) \
33+
TYPE(AttributeLParen) \
3334
TYPE(AttributeMacro) \
34-
TYPE(AttributeParen) \
35+
TYPE(AttributeRParen) \
3536
TYPE(AttributeSquare) \
3637
TYPE(BinaryOperator) \
3738
TYPE(BitFieldColon) \

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ class AnnotatingParser {
377377
// detected one yet.
378378
if (PrevNonComment && OpeningParen.is(TT_Unknown)) {
379379
if (PrevNonComment->is(tok::kw___attribute)) {
380-
OpeningParen.setType(TT_AttributeParen);
380+
OpeningParen.setType(TT_AttributeLParen);
381381
} else if (PrevNonComment->isOneOf(TT_TypenameMacro, tok::kw_decltype,
382382
tok::kw_typeof,
383383
#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) tok::kw___##Trait,
@@ -475,8 +475,8 @@ class AnnotatingParser {
475475
}
476476
}
477477

478-
if (OpeningParen.is(TT_AttributeParen))
479-
CurrentToken->setType(TT_AttributeParen);
478+
if (OpeningParen.is(TT_AttributeLParen))
479+
CurrentToken->setType(TT_AttributeRParen);
480480
if (OpeningParen.is(TT_TypeDeclarationParen))
481481
CurrentToken->setType(TT_TypeDeclarationParen);
482482
if (OpeningParen.Previous &&
@@ -2382,11 +2382,15 @@ class AnnotatingParser {
23822382
// Strip trailing qualifiers such as const or volatile when checking
23832383
// whether the parens could be a cast to a pointer/reference type.
23842384
while (T) {
2385-
if (T->is(TT_AttributeParen)) {
2385+
if (T->is(TT_AttributeRParen)) {
23862386
// Handle `x = (foo *__attribute__((foo)))&v;`:
2387-
if (T->MatchingParen && T->MatchingParen->Previous &&
2388-
T->MatchingParen->Previous->is(tok::kw___attribute)) {
2389-
T = T->MatchingParen->Previous->Previous;
2387+
assert(T->is(tok::r_paren));
2388+
assert(T->MatchingParen);
2389+
assert(T->MatchingParen->is(tok::l_paren));
2390+
assert(T->MatchingParen->is(TT_AttributeLParen));
2391+
if (const auto *Tok = T->MatchingParen->Previous;
2392+
Tok && Tok->is(tok::kw___attribute)) {
2393+
T = Tok->Previous;
23902394
continue;
23912395
}
23922396
} else if (T->is(TT_AttributeSquare)) {
@@ -3989,7 +3993,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
39893993
// after pointer qualifiers.
39903994
if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_After ||
39913995
Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
3992-
(Left.is(TT_AttributeParen) ||
3996+
(Left.is(TT_AttributeRParen) ||
39933997
Left.canBePointerOrReferenceQualifier())) {
39943998
return true;
39953999
}
@@ -4182,7 +4186,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
41824186
return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
41834187
spaceRequiredBeforeParens(Right);
41844188
}
4185-
if ((Left.is(tok::r_paren) && Left.is(TT_AttributeParen)) ||
4189+
if (Left.is(TT_AttributeRParen) ||
41864190
(Left.is(tok::r_square) && Left.is(TT_AttributeSquare))) {
41874191
return true;
41884192
}
@@ -4361,7 +4365,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
43614365
return false;
43624366
}
43634367
// Space in __attribute__((attr)) ::type.
4364-
if (Left.is(TT_AttributeParen) && Right.is(tok::coloncolon))
4368+
if (Left.is(TT_AttributeRParen) && Right.is(tok::coloncolon))
43654369
return true;
43664370

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

51925196
// Ensure wrapping after __attribute__((XX)) and @interface etc.
5193-
if (Left.is(TT_AttributeParen) && Right.is(TT_ObjCDecl))
5197+
if (Left.is(TT_AttributeRParen) && Right.is(TT_ObjCDecl))
51945198
return true;
51955199

51965200
if (Left.is(TT_LambdaLBrace)) {
@@ -5552,8 +5556,8 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
55525556
!Style.Cpp11BracedListStyle) {
55535557
return false;
55545558
}
5555-
if (Left.is(tok::l_paren) &&
5556-
Left.isOneOf(TT_AttributeParen, TT_TypeDeclarationParen)) {
5559+
if (Left.is(TT_AttributeLParen) ||
5560+
(Left.is(tok::l_paren) && Left.is(TT_TypeDeclarationParen))) {
55575561
return false;
55585562
}
55595563
if (Left.is(tok::l_paren) && Left.Previous &&

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,6 +2065,15 @@ TEST_F(TokenAnnotatorTest, UnderstandsJavaScript) {
20652065
EXPECT_TOKEN(Tokens[11], tok::r_brace, TT_Unknown);
20662066
}
20672067

2068+
TEST_F(TokenAnnotatorTest, UnderstandsAttributes) {
2069+
auto Tokens = annotate("bool foo __attribute__((unused));");
2070+
ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2071+
EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_AttributeLParen);
2072+
EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_Unknown);
2073+
EXPECT_TOKEN(Tokens[6], tok::r_paren, TT_Unknown);
2074+
EXPECT_TOKEN(Tokens[7], tok::r_paren, TT_AttributeRParen);
2075+
}
2076+
20682077
} // namespace
20692078
} // namespace format
20702079
} // namespace clang

0 commit comments

Comments
 (0)