[PATCH] D120512: [clang-format] Fix requires related crash

Björn Schäpers via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 24 12:36:41 PST 2022


HazardyKnusperkeks created this revision.
HazardyKnusperkeks added reviewers: owenpan, MyDeveloperDay, curdeius.
HazardyKnusperkeks added a project: clang-format.
HazardyKnusperkeks requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

In the presence of pp branches we parse the token stream multiple times. Thus the token already has the type set. It's best just not to assert on any type in the parser.

Fixes https://github.com/llvm/llvm-project/issues/54019


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120512

Files:
  clang/lib/Format/UnwrappedLineParser.cpp
  clang/unittests/Format/FormatTest.cpp


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -23808,6 +23808,15 @@
 
   verifyFormat("template <typename T>\n"
                "concept C = [] && requires(T t) { typename T::size_type; };");
+
+  verifyFormat("template <typename T>\n"
+               "concept C =\n"
+               "#if X\n"
+               "    A &&\n"
+               "#else\n"
+               "    B &&\n"
+               "#endif\n"
+               "    requires C2<T>;");
 }
 
 TEST_F(FormatTest, RequiresClausesPositions) {
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -2995,7 +2995,6 @@
 void UnwrappedLineParser::parseRequiresClause(FormatToken *RequiresToken) {
   assert(FormatTok->getPreviousNonComment() == RequiresToken);
   assert(RequiresToken->is(tok::kw_requires) && "'requires' expected");
-  assert(RequiresToken->getType() == TT_Unknown);
 
   // If there is no previous token, we are within a requires expression,
   // otherwise we will always have the template or function declaration in front
@@ -3024,7 +3023,6 @@
 void UnwrappedLineParser::parseRequiresExpression(FormatToken *RequiresToken) {
   assert(FormatTok->getPreviousNonComment() == RequiresToken);
   assert(RequiresToken->is(tok::kw_requires) && "'requires' expected");
-  assert(RequiresToken->getType() == TT_Unknown);
 
   RequiresToken->setFinalizedType(TT_RequiresExpression);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D120512.411214.patch
Type: text/x-patch
Size: 1657 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220224/82653d39/attachment.bin>


More information about the cfe-commits mailing list