diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -1989,7 +1989,9 @@ } else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept, tok::kw_requires) && Current.Previous && - !Current.Previous->isOneOf(tok::equal, tok::at) && + !Current.Previous->isOneOf(tok::equal, tok::at, + TT_CtorInitializerComma, + TT_CtorInitializerColon) && Line.MightBeFunctionDecl && Contexts.size() == 1) { // Line.MightBeFunctionDecl can only be true after the parentheses of a // function declaration have been found. diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -1124,6 +1124,35 @@ EXPECT_TOKEN(Tokens[9], tok::colon, TT_GotoLabelColon); } +TEST_F(TokenAnnotatorTest, UnderstandConstructors) { + auto Tokens = annotate("Class::Class() : BaseClass(), Member() {}"); + + // The TT_Unknown is clearly not binding for the future, please adapt if those + // tokens get annotated. + ASSERT_EQ(Tokens.size(), 16u) << Tokens; + EXPECT_TOKEN(Tokens[5], tok::colon, TT_CtorInitializerColon); + EXPECT_TOKEN(Tokens[6], tok::identifier, TT_Unknown); + EXPECT_TOKEN(Tokens[7], tok::l_paren, TT_Unknown); + EXPECT_TOKEN(Tokens[8], tok::r_paren, TT_Unknown); + EXPECT_TOKEN(Tokens[9], tok::comma, TT_CtorInitializerComma); + EXPECT_TOKEN(Tokens[10], tok::identifier, TT_Unknown); + EXPECT_TOKEN(Tokens[11], tok::l_paren, TT_Unknown); + EXPECT_TOKEN(Tokens[12], tok::r_paren, TT_Unknown); + EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace); + + Tokens = annotate("Class::Class() : BaseClass{}, Member{} {}"); + ASSERT_EQ(Tokens.size(), 16u) << Tokens; + EXPECT_TOKEN(Tokens[5], tok::colon, TT_CtorInitializerColon); + EXPECT_TOKEN(Tokens[6], tok::identifier, TT_Unknown); + EXPECT_TOKEN(Tokens[7], tok::l_brace, TT_Unknown); + EXPECT_TOKEN(Tokens[8], tok::r_brace, TT_Unknown); + EXPECT_TOKEN(Tokens[9], tok::comma, TT_CtorInitializerComma); + EXPECT_TOKEN(Tokens[10], tok::identifier, TT_Unknown); + EXPECT_TOKEN(Tokens[11], tok::l_brace, TT_Unknown); + EXPECT_TOKEN(Tokens[12], tok::r_brace, TT_Unknown); + EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace); +} + } // namespace } // namespace format } // namespace clang