[clang] 14178de - [clang-format] Fix a crash on parsing requires clause (#125021)

via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 30 18:03:08 PST 2025


Author: Owen Pan
Date: 2025-01-30T18:03:04-08:00
New Revision: 14178deab0334d9ce095ae7adce408868659faee

URL: https://github.com/llvm/llvm-project/commit/14178deab0334d9ce095ae7adce408868659faee
DIFF: https://github.com/llvm/llvm-project/commit/14178deab0334d9ce095ae7adce408868659faee.diff

LOG: [clang-format] Fix a crash on parsing requires clause (#125021)

Fixes #124921.

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 906fc11a07d5ee..120922d271aab7 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -3632,7 +3632,7 @@ void UnwrappedLineParser::parseRequiresClause(FormatToken *RequiresToken) {
   // It could be inlined into here.
   parseConstraintExpression();
 
-  if (!InRequiresExpression)
+  if (!InRequiresExpression && FormatTok->Previous)
     FormatTok->Previous->ClosesRequiresClause = true;
 }
 

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 57f12221cdc7e6..30f3533ac73f72 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -26478,6 +26478,9 @@ TEST_F(FormatTest, RequiresClauses) {
                "foo();\n"
                "#endif\n"
                "bar(requires);");
+
+  verifyNoCrash("template <class T>\n"
+                "    requires(requires { std::declval<T>()");
 }
 
 TEST_F(FormatTest, RequiresExpressionIndentation) {


        


More information about the cfe-commits mailing list