[clang] 0bf63f0 - [clang-format] Disallow trailing return arrows to be operators
Emilia Dreamer via cfe-commits
cfe-commits at lists.llvm.org
Sun Sep 18 18:06:21 PDT 2022
Author: Emilia Dreamer
Date: 2022-09-19T04:04:31+03:00
New Revision: 0bf63f0d1b6b860a7134d668d3a28104c5c9afc1
URL: https://github.com/llvm/llvm-project/commit/0bf63f0d1b6b860a7134d668d3a28104c5c9afc1
DIFF: https://github.com/llvm/llvm-project/commit/0bf63f0d1b6b860a7134d668d3a28104c5c9afc1.diff
LOG: [clang-format] Disallow trailing return arrows to be operators
In the following construction:
`template <typename T> requires Foo<T> || Bar<T> auto func() -> int;`
The `->` of the trailing return type was actually considered as an
operator as part of the binary operation in the requires clause, with
the precedence level of `PrecedenceArrowAndPeriod`, leading to fake
parens being inserted in strange locations, that would never be closed.
Fixes one part of https://github.com/llvm/llvm-project/issues/56213
(the rest will probably be in a separate patch)
Reviewed By: HazardyKnusperkeks, owenpan
Differential Revision: https://reviews.llvm.org/D134049
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 d18adc308efb0..a81bbb99f5d9e 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2611,8 +2611,10 @@ class ExpressionParser {
}
if (Current->is(TT_BinaryOperator) || Current->is(tok::comma))
return Current->getPrecedence();
- if (Current->isOneOf(tok::period, tok::arrow))
+ if (Current->isOneOf(tok::period, tok::arrow) &&
+ Current->isNot(TT_TrailingReturnArrow)) {
return PrecedenceArrowAndPeriod;
+ }
if ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) &&
Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
Keywords.kw_throws)) {
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 1fe8bece722c4..f3007f85e91b0 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -440,6 +440,15 @@ TEST_F(TokenAnnotatorTest, UnderstandsRequiresClausesAndConcepts) {
ASSERT_EQ(Tokens.size(), 18u) << Tokens;
EXPECT_TOKEN(Tokens[11], tok::kw_requires, TT_RequiresClause);
+ Tokens = annotate("template <typename T>\n"
+ "requires Bar<T> || Baz<T>\n"
+ "auto foo(T) -> int;");
+ ASSERT_EQ(Tokens.size(), 24u) << Tokens;
+ EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresClause);
+ EXPECT_EQ(Tokens[11]->FakeLParens.size(), 0u);
+ EXPECT_TRUE(Tokens[14]->ClosesRequiresClause);
+ EXPECT_TOKEN(Tokens[20], tok::arrow, TT_TrailingReturnArrow);
+
Tokens = annotate("template <typename T>\n"
"struct S {\n"
" void foo() const requires Bar<T>;\n"
More information about the cfe-commits
mailing list