diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h index 606e9e790ad83..acd24f836199d 100644 --- a/clang/lib/Format/FormatToken.h +++ b/clang/lib/Format/FormatToken.h @@ -67,6 +67,7 @@ namespace format { TYPE(DesignatedInitializerLSquare) \ TYPE(DesignatedInitializerPeriod) \ TYPE(DictLiteral) \ + TYPE(DoWhile) \ TYPE(ElseLBrace) \ TYPE(ElseRBrace) \ TYPE(EnumLBrace) \ diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 7bb487d020ea6..488d8dc07b1ea 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -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) diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 4dbe2a532c5fd..290d0103bb3cf 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -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