[PATCH] D129940: [clang-format] Fix misannotation of colon in presence of requires clause
Björn Schäpers via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Sat Jul 16 13:10:05 PDT 2022
HazardyKnusperkeks created this revision.
HazardyKnusperkeks added reviewers: MyDeveloperDay, curdeius, owenpan, JohelEGP, eoanermine.
HazardyKnusperkeks added a project: clang-format.
Herald added a project: All.
HazardyKnusperkeks requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
For clauses without parentheses it was annotated as TT_InheritanceColon.
Relates to https://github.com/llvm/llvm-project/issues/56215
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D129940
Files:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
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<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) {
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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129940.445252.patch
Type: text/x-patch
Size: 3439 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220716/4fc14151/attachment.bin>
More information about the cfe-commits
mailing list