Skip to content

Commit a8896e5

Browse files
[clang-format][NFC] Annotate control statement r_braces (#68621)
Annotating switch braces for the first time. Also in preparation of #67906.
1 parent 1bc4871 commit a8896e5

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

clang/lib/Format/FormatToken.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ namespace format {
5252
TYPE(ConflictStart) \
5353
/* l_brace of if/for/while */ \
5454
TYPE(ControlStatementLBrace) \
55+
TYPE(ControlStatementRBrace) \
5556
TYPE(CppCastLParen) \
5657
TYPE(CSharpGenericTypeConstraint) \
5758
TYPE(CSharpGenericTypeConstraintColon) \
@@ -67,6 +68,7 @@ namespace format {
6768
TYPE(DesignatedInitializerPeriod) \
6869
TYPE(DictLiteral) \
6970
TYPE(ElseLBrace) \
71+
TYPE(ElseRBrace) \
7072
TYPE(EnumLBrace) \
7173
TYPE(EnumRBrace) \
7274
TYPE(FatArrow) \

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,14 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
640640
FormatTok = Tokens->setPosition(StoredPosition);
641641
}
642642

643+
// Sets the token type of the directly previous right brace.
644+
void UnwrappedLineParser::setPreviousRBraceType(TokenType Type) {
645+
if (auto Prev = FormatTok->getPreviousNonComment();
646+
Prev && Prev->is(tok::r_brace)) {
647+
Prev->setFinalizedType(Type);
648+
}
649+
}
650+
643651
template <class T>
644652
static inline void hash_combine(std::size_t &seed, const T &v) {
645653
std::hash<T> hasher;
@@ -2756,6 +2764,7 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
27562764
CompoundStatementIndenter Indenter(this, Style, Line->Level);
27572765
parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
27582766
/*MunchSemi=*/true, KeepIfBraces, &IfBlockKind);
2767+
setPreviousRBraceType(TT_ControlStatementRBrace);
27592768
if (Style.BraceWrapping.BeforeElse)
27602769
addUnwrappedLine();
27612770
else
@@ -2794,6 +2803,7 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
27942803
FormatToken *IfLBrace =
27952804
parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
27962805
/*MunchSemi=*/true, KeepElseBraces, &ElseBlockKind);
2806+
setPreviousRBraceType(TT_ElseRBrace);
27972807
if (FormatTok->is(tok::kw_else)) {
27982808
KeepElseBraces = KeepElseBraces ||
27992809
ElseBlockKind == IfStmtKind::IfOnly ||
@@ -3057,12 +3067,12 @@ void UnwrappedLineParser::parseLoopBody(bool KeepBraces, bool WrapRightBrace) {
30573067
keepAncestorBraces();
30583068

30593069
if (isBlockBegin(*FormatTok)) {
3060-
if (!KeepBraces)
3061-
FormatTok->setFinalizedType(TT_ControlStatementLBrace);
3070+
FormatTok->setFinalizedType(TT_ControlStatementLBrace);
30623071
FormatToken *LeftBrace = FormatTok;
30633072
CompoundStatementIndenter Indenter(this, Style, Line->Level);
30643073
parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
30653074
/*MunchSemi=*/true, KeepBraces);
3075+
setPreviousRBraceType(TT_ControlStatementRBrace);
30663076
if (!KeepBraces) {
30673077
assert(!NestedTooDeep.empty());
30683078
if (!NestedTooDeep.back())
@@ -3196,7 +3206,9 @@ void UnwrappedLineParser::parseSwitch() {
31963206

31973207
if (FormatTok->is(tok::l_brace)) {
31983208
CompoundStatementIndenter Indenter(this, Style, Line->Level);
3209+
FormatTok->setFinalizedType(TT_ControlStatementLBrace);
31993210
parseBlock();
3211+
setPreviousRBraceType(TT_ControlStatementRBrace);
32003212
addUnwrappedLine();
32013213
} else {
32023214
addUnwrappedLine();
@@ -3713,10 +3725,7 @@ bool UnwrappedLineParser::parseEnum() {
37133725
nextToken();
37143726
addUnwrappedLine();
37153727
}
3716-
if (auto Prev = FormatTok->getPreviousNonComment();
3717-
Prev && Prev->is(tok::r_brace)) {
3718-
Prev->setFinalizedType(TT_EnumRBrace);
3719-
}
3728+
setPreviousRBraceType(TT_EnumRBrace);
37203729
return true;
37213730

37223731
// There is no addUnwrappedLine() here so that we fall through to parsing a
@@ -3950,10 +3959,7 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
39503959
unsigned AddLevels = Style.IndentAccessModifiers ? 2u : 1u;
39513960
parseBlock(/*MustBeDeclaration=*/true, AddLevels, /*MunchSemi=*/false);
39523961
}
3953-
if (auto Prev = FormatTok->getPreviousNonComment();
3954-
Prev && Prev->is(tok::r_brace)) {
3955-
Prev->setFinalizedType(ClosingBraceType);
3956-
}
3962+
setPreviousRBraceType(ClosingBraceType);
39573963
}
39583964
// There is no addUnwrappedLine() here so that we fall through to parsing a
39593965
// structural element afterwards. Thus, in "class A {} n, m;",

clang/lib/Format/UnwrappedLineParser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ class UnwrappedLineParser {
243243
void flushComments(bool NewlineBeforeNext);
244244
void pushToken(FormatToken *Tok);
245245
void calculateBraceTypes(bool ExpectClassBody = false);
246+
void setPreviousRBraceType(TokenType Type);
246247

247248
// Marks a conditional compilation edge (for example, an '#if', '#ifdef',
248249
// '#else' or merge conflict marker). If 'Unreachable' is true, assumes

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,6 +2151,37 @@ TEST_F(TokenAnnotatorTest, UnderstandsAttributes) {
21512151
EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_AttributeRParen);
21522152
}
21532153

2154+
TEST_F(TokenAnnotatorTest, UnderstandsControlStatements) {
2155+
auto Tokens = annotate("while (true) {}");
2156+
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
2157+
EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_ControlStatementLBrace);
2158+
EXPECT_TOKEN(Tokens[5], tok::r_brace, TT_ControlStatementRBrace);
2159+
2160+
Tokens = annotate("for (;;) {}");
2161+
ASSERT_EQ(Tokens.size(), 8u) << Tokens;
2162+
EXPECT_TOKEN(Tokens[5], tok::l_brace, TT_ControlStatementLBrace);
2163+
EXPECT_TOKEN(Tokens[6], tok::r_brace, TT_ControlStatementRBrace);
2164+
2165+
Tokens = annotate("do {} while (true);");
2166+
ASSERT_EQ(Tokens.size(), 9u) << Tokens;
2167+
EXPECT_TOKEN(Tokens[1], tok::l_brace, TT_ControlStatementLBrace);
2168+
EXPECT_TOKEN(Tokens[2], tok::r_brace, TT_ControlStatementRBrace);
2169+
2170+
Tokens = annotate("if (true) {} else if (false) {} else {}");
2171+
ASSERT_EQ(Tokens.size(), 17u) << Tokens;
2172+
EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_ControlStatementLBrace);
2173+
EXPECT_TOKEN(Tokens[5], tok::r_brace, TT_ControlStatementRBrace);
2174+
EXPECT_TOKEN(Tokens[11], tok::l_brace, TT_ControlStatementLBrace);
2175+
EXPECT_TOKEN(Tokens[12], tok::r_brace, TT_ControlStatementRBrace);
2176+
EXPECT_TOKEN(Tokens[14], tok::l_brace, TT_ElseLBrace);
2177+
EXPECT_TOKEN(Tokens[15], tok::r_brace, TT_ElseRBrace);
2178+
2179+
Tokens = annotate("switch (foo) {}");
2180+
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
2181+
EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_ControlStatementLBrace);
2182+
EXPECT_TOKEN(Tokens[5], tok::r_brace, TT_ControlStatementRBrace);
2183+
}
2184+
21542185
} // namespace
21552186
} // namespace format
21562187
} // namespace clang

0 commit comments

Comments
 (0)