Skip to content

Commit cef9f40

Browse files
authored
[clang-format] Split TT_AttributeParen (#67396)
Replaced TT_AttributeParen with TT_AttributeLParen and TT_AttributeRParen.
1 parent 76ce473 commit cef9f40

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)) {
@@ -3993,7 +3997,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
39933997
// after pointer qualifiers.
39943998
if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_After ||
39953999
Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
3996-
(Left.is(TT_AttributeParen) ||
4000+
(Left.is(TT_AttributeRParen) ||
39974001
Left.canBePointerOrReferenceQualifier())) {
39984002
return true;
39994003
}
@@ -4186,7 +4190,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
41864190
return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
41874191
spaceRequiredBeforeParens(Right);
41884192
}
4189-
if ((Left.is(tok::r_paren) && Left.is(TT_AttributeParen)) ||
4193+
if (Left.is(TT_AttributeRParen) ||
41904194
(Left.is(tok::r_square) && Left.is(TT_AttributeSquare))) {
41914195
return true;
41924196
}
@@ -4365,7 +4369,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
43654369
return false;
43664370
}
43674371
// Space in __attribute__((attr)) ::type.
4368-
if (Left.is(TT_AttributeParen) && Right.is(tok::coloncolon))
4372+
if (Left.is(TT_AttributeRParen) && Right.is(tok::coloncolon))
43694373
return true;
43704374

43714375
if (Left.is(tok::kw_operator))
@@ -5194,7 +5198,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
51945198
}
51955199

51965200
// Ensure wrapping after __attribute__((XX)) and @interface etc.
5197-
if (Left.is(TT_AttributeParen) && Right.is(TT_ObjCDecl))
5201+
if (Left.is(TT_AttributeRParen) && Right.is(TT_ObjCDecl))
51985202
return true;
51995203

52005204
if (Left.is(TT_LambdaLBrace)) {
@@ -5556,8 +5560,8 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
55565560
!Style.Cpp11BracedListStyle) {
55575561
return false;
55585562
}
5559-
if (Left.is(tok::l_paren) &&
5560-
Left.isOneOf(TT_AttributeParen, TT_TypeDeclarationParen)) {
5563+
if (Left.is(TT_AttributeLParen) ||
5564+
(Left.is(tok::l_paren) && Left.is(TT_TypeDeclarationParen))) {
55615565
return false;
55625566
}
55635567
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
@@ -2072,6 +2072,15 @@ TEST_F(TokenAnnotatorTest, UnderstandsJavaScript) {
20722072
EXPECT_TOKEN(Tokens[11], tok::r_brace, TT_Unknown);
20732073
}
20742074

2075+
TEST_F(TokenAnnotatorTest, UnderstandsAttributes) {
2076+
auto Tokens = annotate("bool foo __attribute__((unused));");
2077+
ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2078+
EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_AttributeLParen);
2079+
EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_Unknown);
2080+
EXPECT_TOKEN(Tokens[6], tok::r_paren, TT_Unknown);
2081+
EXPECT_TOKEN(Tokens[7], tok::r_paren, TT_AttributeRParen);
2082+
}
2083+
20752084
} // namespace
20762085
} // namespace format
20772086
} // namespace clang

0 commit comments

Comments
 (0)