[clang] 2b04c41 - [clang-format] Fix misannotation of colon in presence of requires clause
Björn Schäpers via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 18 12:42:41 PDT 2022
Author: Björn Schäpers
Date: 2022-07-18T21:41:09+02:00
New Revision: 2b04c41b28320c1e399209fbe7a5a8d540578999
URL: https://github.com/llvm/llvm-project/commit/2b04c41b28320c1e399209fbe7a5a8d540578999
DIFF: https://github.com/llvm/llvm-project/commit/2b04c41b28320c1e399209fbe7a5a8d540578999.diff
LOG: [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
Added:
Modified:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index e0dc7a7f05c9..5991cf23d5dc 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -999,7 +999,8 @@ class AnnotatingParser {
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 a2474d5c10f8..ed6fb414d618 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<T>\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<T>)\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) {
More information about the cfe-commits
mailing list