rymiel created this revision.
rymiel added a project: clang-format.
rymiel added reviewers: HazardyKnusperkeks, MyDeveloperDay, owenpan.
Herald added a project: All.
rymiel requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Previously, the token annotator would get confused and annotate a member
function's ref qualifier as a binary operator, if said function also had
a requires clause after it.
This patch accounts for that, treating requires clauses more similarly
to `noexcept`, which also comes after the ref qualifier.
Fixes https://github.com/llvm/llvm-project/issues/61270
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D145656
Files:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
Index: clang/unittests/Format/TokenAnnotatorTest.cpp
===================================================================
--- clang/unittests/Format/TokenAnnotatorTest.cpp
+++ clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -744,10 +744,15 @@
"void S::bar() const & requires Baz<T> { }");
ASSERT_EQ(Tokens.size(), 85u) << Tokens;
EXPECT_TOKEN(Tokens[13], tok::kw_requires, TT_RequiresClause);
+ EXPECT_TOKEN(Tokens[24], tok::amp, TT_PointerOrReference);
EXPECT_TOKEN(Tokens[25], tok::kw_requires, TT_RequiresClause);
+ EXPECT_TOKEN(Tokens[35], tok::ampamp, TT_PointerOrReference);
EXPECT_TOKEN(Tokens[36], tok::kw_requires, TT_RequiresClause);
+ EXPECT_TOKEN(Tokens[47], tok::amp, TT_PointerOrReference);
EXPECT_TOKEN(Tokens[49], tok::kw_requires, TT_RequiresClause);
+ EXPECT_TOKEN(Tokens[59], tok::ampamp, TT_PointerOrReference);
EXPECT_TOKEN(Tokens[61], tok::kw_requires, TT_RequiresClause);
+ EXPECT_TOKEN(Tokens[76], tok::amp, TT_PointerOrReference);
EXPECT_TOKEN(Tokens[77], tok::kw_requires, TT_RequiresClause);
Tokens = annotate("void Class::member() && requires(Constant) {}");
Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -24148,6 +24148,14 @@
"}",
Style);
+ verifyFormat("template <typename T>\n"
+ "int S::bar(T t) &&\n"
+ " requires F<T>\n"
+ "{\n"
+ " return 5;\n"
+ "}",
+ Style);
+
verifyFormat("template <typename T>\n"
"int bar(T t)\n"
" requires F<T>;",
@@ -24161,6 +24169,14 @@
"}",
Style);
+ verifyFormat("template <typename T>\n"
+ "int S::bar(T t) &&\n"
+ "requires F<T>\n"
+ "{\n"
+ " return 5;\n"
+ "}",
+ Style);
+
verifyFormat("template <typename T>\n"
"int bar(T t)\n"
"requires F<T>\n"
@@ -24174,6 +24190,7 @@
"template <typename T> requires Foo<T> void bar() {}\n"
"template <typename T> void bar() requires Foo<T> {}\n"
"template <typename T> void bar() requires Foo<T>;\n"
+ "template <typename T> void S::bar() && requires Foo<T> {}\n"
"template <typename T> requires Foo<T> Bar(T) -> Bar<T>;",
Style);
@@ -24185,6 +24202,8 @@
"requires Foo<T> void bar() {}\n"
"template <typename AAAAAAA>\n"
"void bar() requires Foo<T> {}\n"
+ "template <typename T>\n"
+ "void S::bar() && requires Foo<T> {}\n"
"template <typename AAAAAAA>\n"
"requires Foo<T> Baz(T) -> Baz<T>;",
ColumnStyle);
@@ -24229,6 +24248,9 @@
"void bar()\n"
"requires Foo<T>;\n"
"template <typename T>\n"
+ "void S::bar() &&\n"
+ "requires Foo<T> {}\n"
+ "template <typename T>\n"
"requires Foo<T> Bar(T) -> Bar<T>;",
Style);
@@ -24259,6 +24281,9 @@
"void bar()\n"
" requires Foo<T> {}\n"
"template <typename T>\n"
+ "void S::bar() &&\n"
+ " requires Foo<T> {}\n"
+ "template <typename T>\n"
" requires Foo<T> Bar(T) -> Bar<T>;",
Style);
@@ -24289,6 +24314,9 @@
"void bar() requires Foo<T>\n"
"{}\n"
"template <typename T> void bar() requires Foo<T>;\n"
+ "template <typename T>\n"
+ "void S::bar() && requires Foo<T>\n"
+ "{}\n"
"template <typename T> requires Foo<T>\n"
"Bar(T) -> Bar<T>;",
Style);
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2400,7 +2400,7 @@
if (!NextToken ||
NextToken->isOneOf(tok::arrow, tok::equal, tok::kw_noexcept, tok::comma,
- tok::r_paren) ||
+ tok::r_paren, TT_RequiresClause) ||
NextToken->canBePointerOrReferenceQualifier() ||
(NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
return TT_PointerOrReference;
@@ -3758,8 +3758,9 @@
if (Right.is(TT_BlockComment))
return true;
// foo() -> const Bar * override/final
- if (Right.isOneOf(Keywords.kw_override, Keywords.kw_final,
- tok::kw_noexcept) &&
+ // S::foo() & noexcept/requires
+ if (Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
+ TT_RequiresClause) &&
!Right.is(TT_StartOfName)) {
return true;
}
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits