[clang] [clang-format] Allow decltype in requires clause (PR #78847)

Björn Schäpers via cfe-commits cfe-commits at lists.llvm.org
Sun Jan 21 12:27:31 PST 2024


================
@@ -1071,6 +1071,16 @@ TEST_F(TokenAnnotatorTest, UnderstandsRequiresClausesAndConcepts) {
                     "concept C = (!Foo<T>) && Bar;");
   ASSERT_EQ(Tokens.size(), 19u) << Tokens;
   EXPECT_TOKEN(Tokens[15], tok::ampamp, TT_BinaryOperator);
+
+  Tokens = annotate("void f() & requires(C<decltype(x)>) {}");
+  ASSERT_EQ(Tokens.size(), 18u) << Tokens;
+  EXPECT_TOKEN(Tokens[4], tok::amp, TT_PointerOrReference);
+  EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresClause);
+
+  Tokens = annotate("auto f() -> int& requires(C<decltype(x)>) {}");
+  ASSERT_EQ(Tokens.size(), 20u) << Tokens;
+  EXPECT_TOKEN(Tokens[6], tok::amp, TT_PointerOrReference);
+  EXPECT_TOKEN(Tokens[7], tok::kw_requires, TT_RequiresClause);
 }
----------------
HazardyKnusperkeks wrote:

I _think_ the idea was that if it is an expression we would detect the type before reaching the `r_paren` and thus it has to be a clause. But 2 years have passed and I don't know what I was thinking last week. ;) Had to reread the code and try to guess the conditions.

Which shows why one should comment complicated code...

If I read the code right my question is why didn't `Foo<decktype(t)>` suffice `FoundType && !LastWasColonColon && OpenAngles == 0` when hitting the `x`?

On a different note one could extend the look behind to see if one finds a `=` or a `(` for any conditional, or a requires expression `}`, then it should be an expression.

https://github.com/llvm/llvm-project/pull/78847


More information about the cfe-commits mailing list