Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -999,7 +999,8 @@ FormatToken *Prev = Tok->getPreviousNonComment(); if (!Prev) break; - if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept)) { + if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) || + Prev->ClosesRequiresClause) { Tok->setType(TT_CtorInitializerColon); } else if (Prev->is(tok::kw_try)) { // Member initializer list within function try block. Index: clang/unittests/Format/TokenAnnotatorTest.cpp =================================================================== --- clang/unittests/Format/TokenAnnotatorTest.cpp +++ clang/unittests/Format/TokenAnnotatorTest.cpp @@ -799,6 +799,70 @@ << I; } } + + BaseTokens = annotate("constexpr Foo(Foo const &other)\n" + " : value{other.value} {\n" + " do_magic();\n" + " do_more_magic();\n" + "}"); + + ConstrainedTokens = annotate("constexpr Foo(Foo const &other)\n" + " requires std::is_copy_constructible\n" + " : value{other.value} {\n" + " do_magic();\n" + " do_more_magic();\n" + "}"); + + NumberOfBaseTokens = 26u; + NumberOfAdditionalRequiresClauseTokens = 7u; + NumberOfTokensBeforeRequires = 8u; + + ASSERT_EQ(BaseTokens.size(), NumberOfBaseTokens) << BaseTokens; + ASSERT_EQ(ConstrainedTokens.size(), + NumberOfBaseTokens + NumberOfAdditionalRequiresClauseTokens) + << ConstrainedTokens; + + for (auto I = 0u; I < NumberOfBaseTokens; ++I) { + if (I < NumberOfTokensBeforeRequires) { + EXPECT_EQ(*BaseTokens[I], *ConstrainedTokens[I]) << I; + } else { + EXPECT_EQ(*BaseTokens[I], + *ConstrainedTokens[I + NumberOfAdditionalRequiresClauseTokens]) + << I; + } + } + + BaseTokens = annotate("constexpr Foo(Foo const &other)\n" + " : value{other.value} {\n" + " do_magic();\n" + " do_more_magic();\n" + "}"); + + ConstrainedTokens = annotate("constexpr Foo(Foo const &other)\n" + " requires (std::is_copy_constructible)\n" + " : value{other.value} {\n" + " do_magic();\n" + " do_more_magic();\n" + "}"); + + NumberOfBaseTokens = 26u; + NumberOfAdditionalRequiresClauseTokens = 9u; + NumberOfTokensBeforeRequires = 8u; + + ASSERT_EQ(BaseTokens.size(), NumberOfBaseTokens) << BaseTokens; + ASSERT_EQ(ConstrainedTokens.size(), + NumberOfBaseTokens + NumberOfAdditionalRequiresClauseTokens) + << ConstrainedTokens; + + for (auto I = 0u; I < NumberOfBaseTokens; ++I) { + if (I < NumberOfTokensBeforeRequires) { + EXPECT_EQ(*BaseTokens[I], *ConstrainedTokens[I]) << I; + } else { + EXPECT_EQ(*BaseTokens[I], + *ConstrainedTokens[I + NumberOfAdditionalRequiresClauseTokens]) + << I; + } + } } TEST_F(TokenAnnotatorTest, UnderstandsAsm) {