From bd3dd10a8b489ce50823b4cc0049f16610adeee2 Mon Sep 17 00:00:00 2001 From: Emilia Dreamer Date: Mon, 5 Sep 2022 12:33:59 +0200 Subject: [PATCH] [clang-format] Concepts: allow identifiers after negation Previously, the formatter would refuse to treat identifiers within a compound concept definition as actually part of the definition, if they were after the negation operator !. It is now made consistent with the likes of && and ||. Fixes https://github.com/llvm/llvm-project/issues/55898 Differential Revision: https://reviews.llvm.org/D131978 --- clang/lib/Format/UnwrappedLineParser.cpp | 3 ++- clang/unittests/Format/FormatTest.cpp | 9 +++++++++ clang/unittests/Format/TokenAnnotatorTest.cpp | 7 +++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 1e775f7..02b13be 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -3544,7 +3544,8 @@ void UnwrappedLineParser::parseConstraintExpression() { switch (FormatTok->Previous->Tok.getKind()) { case tok::coloncolon: // Nested identifier. case tok::ampamp: // Start of a function or variable for the - case tok::pipepipe: // constraint expression. + case tok::pipepipe: // constraint expression. (binary) + case tok::exclaim: // The same as above, but unary. case tok::kw_requires: // Initial identifier of a requires clause. case tok::equal: // Initial identifier of a concept declaration. break; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 4383a90..88c1688 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -24218,6 +24218,15 @@ TEST_F(FormatTest, Concepts) { "sizeof(T) <= 8;"); verifyFormat("template \n" + "concept DelayedCheck = Unit && !DerivedUnit;"); + + verifyFormat("template \n" + "concept DelayedCheck = Unit && !(DerivedUnit);"); + + verifyFormat("template \n" + "concept DelayedCheck = Unit && !!DerivedUnit;"); + + verifyFormat("template \n" "concept DelayedCheck = !!false || requires(T t) { t.bar(); } " "&& sizeof(T) <= 8;"); diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 60684d7..a839fb2 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -375,6 +375,13 @@ TEST_F(TokenAnnotatorTest, UnderstandsRequiresClausesAndConcepts) { EXPECT_TOKEN(Tokens[16], tok::ampamp, TT_BinaryOperator); Tokens = annotate("template \n" + "concept C = Foo && !Bar;"); + + ASSERT_EQ(Tokens.size(), 14u) << Tokens; + EXPECT_TOKEN(Tokens[9], tok::ampamp, TT_BinaryOperator); + EXPECT_TOKEN(Tokens[10], tok::exclaim, TT_UnaryOperator); + + Tokens = annotate("template \n" "concept C = requires(T t) {\n" " { t.foo() };\n" "} && Bar && Baz;"); -- 2.7.4