[clang] 78ac867 - [clang-format] Fix requires related crash

Björn Schäpers via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 1 12:55:39 PST 2022


Author: Björn Schäpers
Date: 2022-03-01T21:55:31+01:00
New Revision: 78ac86701801663d700919e543f4e1129982993d

URL: https://github.com/llvm/llvm-project/commit/78ac86701801663d700919e543f4e1129982993d
DIFF: https://github.com/llvm/llvm-project/commit/78ac86701801663d700919e543f4e1129982993d.diff

LOG: [clang-format] Fix requires related crash

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

Differential Revision: https://reviews.llvm.org/D120512

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index c833cc97c6a4f..8f034a8ce1599 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -2994,7 +2994,6 @@ bool clang::format::UnwrappedLineParser::parseRequires() {
 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
@@ -3023,7 +3022,6 @@ void UnwrappedLineParser::parseRequiresClause(FormatToken *RequiresToken) {
 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);
 

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 9a0890f874dde..64807e144ae10 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -24053,6 +24053,13 @@ TEST_F(FormatTest, RequiresClauses) {
                "    return number_zero_v<T>;\n"
                "  }\n"
                "};");
+
+  // Not a clause, but we once hit an assert.
+  verifyFormat("#if 0\n"
+               "#else\n"
+               "foo();\n"
+               "#endif\n"
+               "bar(requires);");
 }
 
 TEST_F(FormatTest, StatementAttributeLikeMacros) {


        


More information about the cfe-commits mailing list