Skip to content

Commit 84d3c28

Browse files
owencaaokblast
authored andcommitted
[clang-format][NFC] Annotate attribute squares more effectively (llvm#164052)
Annotate left/right attribute squares distinctively and only annotate the outer pair of C++ attribute squares.
1 parent 423a1ea commit 84d3c28

File tree

5 files changed

+62
-37
lines changed

5 files changed

+62
-37
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
433433
}
434434
if ((startsNextParameter(Current, Style) || Previous.is(tok::semi) ||
435435
(Previous.is(TT_TemplateCloser) && Current.is(TT_StartOfName) &&
436-
State.Line->First->isNot(TT_AttributeSquare) && Style.isCpp() &&
436+
State.Line->First->isNot(TT_AttributeLSquare) && Style.isCpp() &&
437437
// FIXME: This is a temporary workaround for the case where clang-format
438438
// sets BreakBeforeParameter to avoid bin packing and this creates a
439439
// completely unnecessary line break after a template type that isn't
@@ -1374,7 +1374,8 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
13741374
}
13751375
if (Current.is(TT_LambdaArrow) &&
13761376
Previous.isOneOf(tok::kw_noexcept, tok::kw_mutable, tok::kw_constexpr,
1377-
tok::kw_consteval, tok::kw_static, TT_AttributeSquare)) {
1377+
tok::kw_consteval, tok::kw_static,
1378+
TT_AttributeRSquare)) {
13781379
return ContinuationIndent;
13791380
}
13801381
if ((Current.isOneOf(tok::r_brace, tok::r_square) ||
@@ -1499,9 +1500,10 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
14991500
Current.isNot(tok::l_paren) &&
15001501
!Current.endsSequence(TT_StartOfName, TT_AttributeMacro,
15011502
TT_PointerOrReference)) ||
1502-
PreviousNonComment->isOneOf(
1503-
TT_AttributeRParen, TT_AttributeSquare, TT_FunctionAnnotationRParen,
1504-
TT_JavaAnnotation, TT_LeadingJavaAnnotation))) ||
1503+
PreviousNonComment->isOneOf(TT_AttributeRParen, TT_AttributeRSquare,
1504+
TT_FunctionAnnotationRParen,
1505+
TT_JavaAnnotation,
1506+
TT_LeadingJavaAnnotation))) ||
15051507
(!Style.IndentWrappedFunctionNames &&
15061508
NextNonComment->isOneOf(tok::kw_operator, TT_FunctionDeclarationName))) {
15071509
return std::max(CurrentState.LastSpace, CurrentState.Indent);

clang/lib/Format/DefinitionBlockSeparator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ void DefinitionBlockSeparator::separateBlocks(
169169
}
170170
}
171171

172-
if (Style.isCSharp() && OperateLine->First->is(TT_AttributeSquare))
172+
if (Style.isCSharp() && OperateLine->First->is(TT_AttributeLSquare))
173173
return true;
174174
return false;
175175
};

clang/lib/Format/FormatToken.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ namespace format {
3030
TYPE(ArraySubscriptLSquare) \
3131
TYPE(AttributeColon) \
3232
TYPE(AttributeLParen) \
33+
TYPE(AttributeLSquare) \
3334
TYPE(AttributeMacro) \
3435
TYPE(AttributeRParen) \
35-
TYPE(AttributeSquare) \
36+
TYPE(AttributeRSquare) \
3637
TYPE(BinaryOperator) \
3738
TYPE(BitFieldColon) \
3839
TYPE(BlockComment) \

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -531,10 +531,6 @@ class AnnotatingParser {
531531
OpeningParen.Previous->is(TT_LeadingJavaAnnotation)) {
532532
CurrentToken->setType(TT_LeadingJavaAnnotation);
533533
}
534-
if (OpeningParen.Previous &&
535-
OpeningParen.Previous->is(TT_AttributeSquare)) {
536-
CurrentToken->setType(TT_AttributeSquare);
537-
}
538534

539535
if (!HasMultipleLines)
540536
OpeningParen.setPackingKind(PPK_Inconclusive);
@@ -722,9 +718,11 @@ class AnnotatingParser {
722718
} else if (InsideInlineASM) {
723719
Left->setType(TT_InlineASMSymbolicNameLSquare);
724720
} else if (IsCpp11AttributeSpecifier) {
725-
Left->setType(TT_AttributeSquare);
726-
if (!IsInnerSquare && Left->Previous)
727-
Left->Previous->EndsCppAttributeGroup = false;
721+
if (!IsInnerSquare) {
722+
Left->setType(TT_AttributeLSquare);
723+
if (Left->Previous)
724+
Left->Previous->EndsCppAttributeGroup = false;
725+
}
728726
} else if (Style.isJavaScript() && Parent &&
729727
Contexts.back().ContextKind == tok::l_brace &&
730728
Parent->isOneOf(tok::l_brace, tok::comma)) {
@@ -733,7 +731,7 @@ class AnnotatingParser {
733731
Parent && Parent->isOneOf(tok::l_brace, tok::comma)) {
734732
Left->setType(TT_DesignatedInitializerLSquare);
735733
} else if (IsCSharpAttributeSpecifier) {
736-
Left->setType(TT_AttributeSquare);
734+
Left->setType(TT_AttributeLSquare);
737735
} else if (CurrentToken->is(tok::r_square) && Parent &&
738736
Parent->is(TT_TemplateCloser)) {
739737
Left->setType(TT_ArraySubscriptLSquare);
@@ -797,13 +795,12 @@ class AnnotatingParser {
797795

798796
while (CurrentToken) {
799797
if (CurrentToken->is(tok::r_square)) {
800-
if (IsCpp11AttributeSpecifier) {
801-
CurrentToken->setType(TT_AttributeSquare);
802-
if (!IsInnerSquare)
803-
CurrentToken->EndsCppAttributeGroup = true;
798+
if (IsCpp11AttributeSpecifier && !IsInnerSquare) {
799+
CurrentToken->setType(TT_AttributeRSquare);
800+
CurrentToken->EndsCppAttributeGroup = true;
804801
}
805802
if (IsCSharpAttributeSpecifier) {
806-
CurrentToken->setType(TT_AttributeSquare);
803+
CurrentToken->setType(TT_AttributeRSquare);
807804
} else if (((CurrentToken->Next &&
808805
CurrentToken->Next->is(tok::l_paren)) ||
809806
(CurrentToken->Previous &&
@@ -1297,7 +1294,7 @@ class AnnotatingParser {
12971294
bool consumeToken() {
12981295
if (IsCpp) {
12991296
const auto *Prev = CurrentToken->getPreviousNonComment();
1300-
if (Prev && Prev->is(tok::r_square) && Prev->is(TT_AttributeSquare) &&
1297+
if (Prev && Prev->is(TT_AttributeRSquare) &&
13011298
CurrentToken->isOneOf(tok::kw_if, tok::kw_switch, tok::kw_case,
13021299
tok::kw_default, tok::kw_for, tok::kw_while) &&
13031300
mustBreakAfterAttributes(*CurrentToken, Style)) {
@@ -2850,7 +2847,7 @@ class AnnotatingParser {
28502847
T = Tok->Previous;
28512848
continue;
28522849
}
2853-
} else if (T->is(TT_AttributeSquare)) {
2850+
} else if (T->is(TT_AttributeRSquare)) {
28542851
// Handle `x = (foo *[[clang::foo]])&v;`:
28552852
if (T->MatchingParen && T->MatchingParen->Previous) {
28562853
T = T->MatchingParen->Previous;
@@ -3656,7 +3653,7 @@ static FormatToken *getFunctionName(const AnnotatedLine &Line,
36563653
for (FormatToken *Tok = Line.getFirstNonComment(), *Name = nullptr; Tok;
36573654
Tok = Tok->getNextNonComment()) {
36583655
// Skip C++11 attributes both before and after the function name.
3659-
if (Tok->is(tok::l_square) && Tok->is(TT_AttributeSquare)) {
3656+
if (Tok->is(TT_AttributeLSquare)) {
36603657
Tok = Tok->MatchingParen;
36613658
if (!Tok)
36623659
break;
@@ -4325,7 +4322,7 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
43254322
return 35;
43264323
if (Right.isNoneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
43274324
TT_ArrayInitializerLSquare,
4328-
TT_DesignatedInitializerLSquare, TT_AttributeSquare)) {
4325+
TT_DesignatedInitializerLSquare, TT_AttributeLSquare)) {
43294326
return 500;
43304327
}
43314328
}
@@ -4808,7 +4805,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
48084805
if (Right.is(tok::l_square) &&
48094806
Right.isNoneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
48104807
TT_DesignatedInitializerLSquare,
4811-
TT_StructuredBindingLSquare, TT_AttributeSquare) &&
4808+
TT_StructuredBindingLSquare, TT_AttributeLSquare) &&
48124809
Left.isNoneOf(tok::numeric_constant, TT_DictLiteral) &&
48134810
!(Left.isNot(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
48144811
Right.is(TT_ArraySubscriptLSquare))) {
@@ -4827,7 +4824,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
48274824

48284825
// Space between template and attribute.
48294826
// e.g. template <typename T> [[nodiscard]] ...
4830-
if (Left.is(TT_TemplateCloser) && Right.is(TT_AttributeSquare))
4827+
if (Left.is(TT_TemplateCloser) && Right.is(TT_AttributeLSquare))
48314828
return true;
48324829
// Space before parentheses common for all languages
48334830
if (Right.is(tok::l_paren)) {
@@ -4842,10 +4839,8 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
48424839
return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
48434840
spaceRequiredBeforeParens(Right);
48444841
}
4845-
if (Left.is(TT_AttributeRParen) ||
4846-
(Left.is(tok::r_square) && Left.is(TT_AttributeSquare))) {
4842+
if (Left.isOneOf(TT_AttributeRParen, TT_AttributeRSquare))
48474843
return true;
4848-
}
48494844
if (Left.is(TT_ForEachMacro)) {
48504845
return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
48514846
spaceRequiredBeforeParens(Right);
@@ -5663,16 +5658,14 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
56635658
}
56645659

56655660
// Break after C# [...] and before public/protected/private/internal.
5666-
if (Left.is(TT_AttributeSquare) && Left.is(tok::r_square) &&
5661+
if (Left.is(TT_AttributeRSquare) &&
56675662
(Right.isAccessSpecifier(/*ColonRequired=*/false) ||
56685663
Right.is(Keywords.kw_internal))) {
56695664
return true;
56705665
}
56715666
// Break between ] and [ but only when there are really 2 attributes.
5672-
if (Left.is(TT_AttributeSquare) && Right.is(TT_AttributeSquare) &&
5673-
Left.is(tok::r_square) && Right.is(tok::l_square)) {
5667+
if (Left.is(TT_AttributeRSquare) && Right.is(TT_AttributeLSquare))
56745668
return true;
5675-
}
56765669
} else if (Style.isJavaScript()) {
56775670
// FIXME: This might apply to other languages and token kinds.
56785671
if (Right.is(tok::string_literal) && Left.is(tok::plus) && BeforeLeft &&
@@ -6412,8 +6405,10 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
64126405
if (Right.isAttribute())
64136406
return true;
64146407

6415-
if (Right.is(tok::l_square) && Right.is(TT_AttributeSquare))
6416-
return Left.isNot(TT_AttributeSquare);
6408+
if (Right.is(TT_AttributeLSquare)) {
6409+
assert(Left.isNot(tok::l_square));
6410+
return true;
6411+
}
64176412

64186413
if (Left.is(tok::identifier) && Right.is(tok::string_literal))
64196414
return true;
@@ -6454,8 +6449,12 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
64546449
Left.getPrecedence() == prec::Assignment)) {
64556450
return true;
64566451
}
6457-
if ((Left.is(TT_AttributeSquare) && Right.is(tok::l_square)) ||
6458-
(Left.is(tok::r_square) && Right.is(TT_AttributeSquare))) {
6452+
if (Left.is(TT_AttributeLSquare) && Right.is(tok::l_square)) {
6453+
assert(Right.isNot(TT_AttributeLSquare));
6454+
return false;
6455+
}
6456+
if (Left.is(tok::r_square) && Right.is(TT_AttributeRSquare)) {
6457+
assert(Left.isNot(TT_AttributeRSquare));
64596458
return false;
64606459
}
64616460

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4237,6 +4237,29 @@ TEST_F(TokenAnnotatorTest, QtProperty) {
42374237
EXPECT_TOKEN(Tokens[12], tok::identifier, TT_QtProperty);
42384238
}
42394239

4240+
TEST_F(TokenAnnotatorTest, AttributeSquares) {
4241+
auto Tokens = annotate("[[maybe_unused]] const int i;");
4242+
ASSERT_EQ(Tokens.size(), 10u) << Tokens;
4243+
EXPECT_TOKEN(Tokens[0], tok::l_square, TT_AttributeLSquare);
4244+
EXPECT_TOKEN(Tokens[1], tok::l_square, TT_Unknown);
4245+
EXPECT_TOKEN(Tokens[3], tok::r_square, TT_Unknown);
4246+
EXPECT_TOKEN(Tokens[4], tok::r_square, TT_AttributeRSquare);
4247+
EXPECT_TRUE(Tokens[4]->EndsCppAttributeGroup);
4248+
4249+
Tokens = annotate("[[foo([[]])]] [[maybe_unused]] int j;");
4250+
ASSERT_EQ(Tokens.size(), 20u) << Tokens;
4251+
EXPECT_TOKEN(Tokens[0], tok::l_square, TT_AttributeLSquare);
4252+
EXPECT_TOKEN(Tokens[1], tok::l_square, TT_Unknown);
4253+
EXPECT_TOKEN(Tokens[9], tok::r_square, TT_Unknown);
4254+
EXPECT_TOKEN(Tokens[10], tok::r_square, TT_AttributeRSquare);
4255+
EXPECT_FALSE(Tokens[10]->EndsCppAttributeGroup);
4256+
EXPECT_TOKEN(Tokens[11], tok::l_square, TT_AttributeLSquare);
4257+
EXPECT_TOKEN(Tokens[12], tok::l_square, TT_Unknown);
4258+
EXPECT_TOKEN(Tokens[14], tok::r_square, TT_Unknown);
4259+
EXPECT_TOKEN(Tokens[15], tok::r_square, TT_AttributeRSquare);
4260+
EXPECT_TRUE(Tokens[15]->EndsCppAttributeGroup);
4261+
}
4262+
42404263
} // namespace
42414264
} // namespace format
42424265
} // namespace clang

0 commit comments

Comments
 (0)