[clang] b3aeca3 - [clang-format] Further improve requires clause detection
Björn Schäpers via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 24 19:43:44 PDT 2022
Author: Björn Schäpers
Date: 2022-06-25T04:43:38+02:00
New Revision: b3aeca3962bb39dfe110472ea033bda32044455d
URL: https://github.com/llvm/llvm-project/commit/b3aeca3962bb39dfe110472ea033bda32044455d
DIFF: https://github.com/llvm/llvm-project/commit/b3aeca3962bb39dfe110472ea033bda32044455d.diff
LOG: [clang-format] Further improve requires clause detection
If there is a typeish token before the requires, it can't be a requires
expression.
Fixes https://github.com/llvm/llvm-project/issues/56176
Differential Revision: https://reviews.llvm.org/D128496
Added:
Modified:
clang/lib/Format/UnwrappedLineParser.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 77a59d8585091..c1bfa4d573a72 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -3168,6 +3168,11 @@ bool clang::format::UnwrappedLineParser::parseRequires() {
break;
}
default:
+ if (PreviousNonComment->isTypeOrIdentifier()) {
+ // This is a requires clause.
+ parseRequiresClause(RequiresToken);
+ return true;
+ }
// It's an expression.
parseRequiresExpression(RequiresToken);
return false;
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index aa91f389a5d2e..2dbc5da07d4db 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -402,6 +402,33 @@ TEST_F(TokenAnnotatorTest, UnderstandsRequiresClausesAndConcepts) {
EXPECT_TOKEN(Tokens[25], tok::less, TT_TemplateOpener);
EXPECT_TOKEN(Tokens[27], tok::greater, TT_TemplateCloser);
EXPECT_TOKEN(Tokens[28], tok::greater, TT_TemplateCloser);
+
+ Tokens = annotate("auto bar() -> int requires(is_integral_v<T>) {}");
+ ASSERT_EQ(Tokens.size(), 16u) << Tokens;
+ EXPECT_TOKEN(Tokens[6], tok::kw_requires, TT_RequiresClause);
+
+ Tokens = annotate("auto bar() -> void requires(is_integral_v<T>) {}");
+ ASSERT_EQ(Tokens.size(), 16u) << Tokens;
+ EXPECT_TOKEN(Tokens[6], tok::kw_requires, TT_RequiresClause);
+
+ Tokens = annotate("auto bar() -> MyType requires(is_integral_v<T>) {}");
+ ASSERT_EQ(Tokens.size(), 16u) << Tokens;
+ EXPECT_TOKEN(Tokens[6], tok::kw_requires, TT_RequiresClause);
+
+ Tokens =
+ annotate("auto bar() -> SOME_MACRO_TYPE requires(is_integral_v<T>) {}");
+ ASSERT_EQ(Tokens.size(), 16u) << Tokens;
+ EXPECT_TOKEN(Tokens[6], tok::kw_requires, TT_RequiresClause);
+
+ Tokens =
+ annotate("auto bar() -> qualified::type requires(is_integral_v<T>) {}");
+ ASSERT_EQ(Tokens.size(), 18u) << Tokens;
+ EXPECT_TOKEN(Tokens[8], tok::kw_requires, TT_RequiresClause);
+
+ Tokens =
+ annotate("auto bar() -> Template<type> requires(is_integral_v<T>) {}");
+ ASSERT_EQ(Tokens.size(), 19u) << Tokens;
+ EXPECT_TOKEN(Tokens[9], tok::kw_requires, TT_RequiresClause);
}
TEST_F(TokenAnnotatorTest, UnderstandsRequiresExpressions) {
More information about the cfe-commits
mailing list