[clang] acd17a2 - [clang-format] Fix crash on invalid requires expression
Björn Schäpers via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 14 04:45:23 PDT 2022
Author: Björn Schäpers
Date: 2022-03-14T12:44:46+01:00
New Revision: acd17a2be81a33abf4350e31ae1747dcb0f12332
URL: https://github.com/llvm/llvm-project/commit/acd17a2be81a33abf4350e31ae1747dcb0f12332
DIFF: https://github.com/llvm/llvm-project/commit/acd17a2be81a33abf4350e31ae1747dcb0f12332.diff
LOG: [clang-format] Fix crash on invalid requires expression
Fixes https://github.com/llvm/llvm-project/issues/54350
Differential Revision: https://reviews.llvm.org/D121550
Added:
Modified:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 9999120100b14..a918c0e7ab4da 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -421,8 +421,8 @@ class AnnotatingParser {
if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
return false;
- if (CurrentToken->is(tok::l_brace))
- OpeningParen.setType(TT_Unknown); // Not TT_ObjCBlockLParen
+ if (CurrentToken->is(tok::l_brace) && OpeningParen.is(TT_ObjCBlockLParen))
+ OpeningParen.setType(TT_Unknown);
if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
!CurrentToken->Next->HasUnescapedNewline &&
!CurrentToken->Next->isTrailingComment())
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 5ca6f6244c40c..aadd24a0d6ca6 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -24041,9 +24041,9 @@ TEST_F(FormatTest, Concepts) {
verifyFormat(
"template <typename T> concept C = decltype([]() -> std::true_type {\n"
" return {};\n"
- " }())::value\n"
- " && requires(T t) { t.bar(); } &&\n"
- " sizeof(T) <= 8;",
+ " }())::value &&\n"
+ " requires(T t) { t.bar(); } && "
+ "sizeof(T) <= 8;",
Style);
verifyFormat("template <typename T> concept Semiregular =\n"
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 7e753bc5a34d5..05cf000eadb71 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -356,6 +356,14 @@ TEST_F(TokenAnnotatorTest, UnderstandsRequiresExpressions) {
EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_RequiresExpressionLBrace);
EXPECT_TOKEN(Tokens[29], tok::kw_requires,
TT_RequiresClauseInARequiresExpression);
+
+ // Invalid Code, but we don't want to crash. See http://llvm.org/PR54350.
+ Tokens = annotate("bool r10 = requires (struct new_struct { int x; } s) { "
+ "requires true; };");
+ ASSERT_EQ(Tokens.size(), 21u) << Tokens;
+ EXPECT_TOKEN(Tokens[3], tok::kw_requires, TT_RequiresExpression);
+ EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_RequiresExpressionLParen);
+ EXPECT_TOKEN(Tokens[14], tok::l_brace, TT_RequiresExpressionLBrace);
}
TEST_F(TokenAnnotatorTest, RequiresDoesNotChangeParsingOfTheRest) {
More information about the cfe-commits
mailing list