From: Björn Schäpers Date: Mon, 4 Jul 2022 08:53:34 +0000 (+0200) Subject: [clang-format] Fix misannotation of colon in presence of requires clause X-Git-Tag: upstream/15.0.7~1287 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2b04c41b28320c1e399209fbe7a5a8d540578999;p=platform%2Fupstream%2Fllvm.git [clang-format] Fix misannotation of colon in presence of requires clause For clauses without parentheses it was annotated as TT_InheritanceColon. Relates to https://github.com/llvm/llvm-project/issues/56215 Differential Revision: https://reviews.llvm.org/D129940 --- diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index e0dc7a7..5991cf2 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -999,7 +999,8 @@ private: 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. diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index a2474d5..ed6fb41 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -802,6 +802,70 @@ TEST_F(TokenAnnotatorTest, RequiresDoesNotChangeParsingOfTheRest) { << 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) {