Skip to content

Conversation

@owenca
Copy link
Contributor

@owenca owenca commented May 2, 2025

Fix #138097

@llvmbot
Copy link
Member

llvmbot commented May 2, 2025

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

Fix #138097


Full diff: https://github.com/llvm/llvm-project/pull/138230.diff

2 Files Affected:

  • (modified) clang/lib/Format/UnwrappedLineParser.cpp (+2-2)
  • (modified) clang/unittests/Format/FormatTest.cpp (+2)
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 837146f0c6483..863df987ab230 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1834,8 +1834,8 @@ void UnwrappedLineParser::parseStructuralElement(
       nextToken();
       if (FormatTok->is(tok::l_paren)) {
         parseParens();
-        assert(FormatTok->Previous);
-        if (FormatTok->Previous->endsSequence(tok::r_paren, tok::kw_auto,
+        if (FormatTok->Previous &&
+            FormatTok->Previous->endsSequence(tok::r_paren, tok::kw_auto,
                                               tok::l_paren)) {
           Line->SeenDecltypeAuto = true;
         }
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index c4fcc5506d152..5605777c20ae2 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -14259,6 +14259,8 @@ TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
   verifyNoCrash("struct Foo {\n"
                 "  operator foo(bar\n"
                 "};");
+  verifyNoCrash("decltype( {\n"
+                "  {");
 }
 
 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) {

@owenca owenca merged commit 79210fe into llvm:main May 3, 2025
13 checks passed
@owenca owenca deleted the 138097 branch May 3, 2025 02:20
@llvm llvm deleted a comment from llvm-ci May 3, 2025
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
owenca added a commit to owenca/llvm-project that referenced this pull request May 10, 2025
swift-ci pushed a commit to swiftlang/llvm-project that referenced this pull request May 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[clangd/clang-format] range formatting crashes at clang::format::FormatToken::is(clang::tok::TokenKind)

3 participants