Skip to content

Commit

Permalink
[clang-format] Annotate do while while
Browse files Browse the repository at this point in the history
So we can differentiate on the while keyword between a do-while-loop and
a normal while-loop.
  • Loading branch information
HazardyKnusperkeks committed Oct 20, 2023
1 parent 4896238 commit a95d4b7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions clang/lib/Format/FormatToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ namespace format {
TYPE(DesignatedInitializerLSquare) \
TYPE(DesignatedInitializerPeriod) \
TYPE(DictLiteral) \
TYPE(DoWhile) \
TYPE(ElseLBrace) \
TYPE(ElseRBrace) \
TYPE(EnumLBrace) \
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3137,6 +3137,8 @@ void UnwrappedLineParser::parseDoWhile() {
return;
}

FormatTok->setFinalizedType(TT_DoWhile);

// If in Whitesmiths mode, the line with the while() needs to be indented
// to the same level as the block.
if (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths)
Expand Down
10 changes: 10 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2334,6 +2334,16 @@ TEST_F(TokenAnnotatorTest, UnderstandsControlStatements) {
EXPECT_TOKEN(Tokens[5], tok::r_brace, TT_ControlStatementRBrace);
}

TEST_F(TokenAnnotatorTest, UnderstandsDoWhile) {
auto Tokens = annotate("do { ++i; } while ( i > 5 );");
ASSERT_EQ(Tokens.size(), 14u) << Tokens;
EXPECT_TOKEN(Tokens[6], tok::kw_while, TT_DoWhile);

Tokens = annotate("do ++i; while ( i > 5 );");
ASSERT_EQ(Tokens.size(), 12u) << Tokens;
EXPECT_TOKEN(Tokens[4], tok::kw_while, TT_DoWhile);
}

} // namespace
} // namespace format
} // namespace clang

0 comments on commit a95d4b7

Please sign in to comment.