Skip to content

Commit a95d4b7

Browse files
[clang-format] Annotate do while while
So we can differentiate on the while keyword between a do-while-loop and a normal while-loop.
1 parent 4896238 commit a95d4b7

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

clang/lib/Format/FormatToken.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ namespace format {
6767
TYPE(DesignatedInitializerLSquare) \
6868
TYPE(DesignatedInitializerPeriod) \
6969
TYPE(DictLiteral) \
70+
TYPE(DoWhile) \
7071
TYPE(ElseLBrace) \
7172
TYPE(ElseRBrace) \
7273
TYPE(EnumLBrace) \

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3137,6 +3137,8 @@ void UnwrappedLineParser::parseDoWhile() {
31373137
return;
31383138
}
31393139

3140+
FormatTok->setFinalizedType(TT_DoWhile);
3141+
31403142
// If in Whitesmiths mode, the line with the while() needs to be indented
31413143
// to the same level as the block.
31423144
if (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths)

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,6 +2334,16 @@ TEST_F(TokenAnnotatorTest, UnderstandsControlStatements) {
23342334
EXPECT_TOKEN(Tokens[5], tok::r_brace, TT_ControlStatementRBrace);
23352335
}
23362336

2337+
TEST_F(TokenAnnotatorTest, UnderstandsDoWhile) {
2338+
auto Tokens = annotate("do { ++i; } while ( i > 5 );");
2339+
ASSERT_EQ(Tokens.size(), 14u) << Tokens;
2340+
EXPECT_TOKEN(Tokens[6], tok::kw_while, TT_DoWhile);
2341+
2342+
Tokens = annotate("do ++i; while ( i > 5 );");
2343+
ASSERT_EQ(Tokens.size(), 12u) << Tokens;
2344+
EXPECT_TOKEN(Tokens[4], tok::kw_while, TT_DoWhile);
2345+
}
2346+
23372347
} // namespace
23382348
} // namespace format
23392349
} // namespace clang

0 commit comments

Comments
 (0)