https://github.com/hbatagelo created 
https://github.com/llvm/llvm-project/pull/203794

Fixes #200879.

This patch fixes crashes when code completion is invoked immediately after the 
declaration of a top-level class containing methods with default arguments or 
exception specifications. 

Default-argument case: https://godbolt.org/z/4G9nTfnGd
Exception-specification case: https://godbolt.org/z/8frEv8von

When code completion is triggered right after the class body, the lookahead 
token is `tok::code_completion` when late parsing begins. The token is saved to 
the cached token stream and `ConsumeAnyToken()` is called to consume it, but 
`ConsumeAnyToken()` defaults to rejecting completion tokens, which cuts off 
parsing. In the default-arguments path (as reported in #200879), the assertion 
`Tok.is(tok::equal) && "Default argument not starting with '='"` is triggered 
because `cutOffParsing()` sets the current token to `eof`. 

Fix by using `ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true)` when 
consuming the pushed token.


>From 2b0992291c76e54a28eefc34c5235f33924f4427 Mon Sep 17 00:00:00 2001
From: Harlen Batagelo <[email protected]>
Date: Sun, 14 Jun 2026 17:47:09 -0300
Subject: [PATCH] Fix parser crash on code completion in late-parsed methods

---
 clang/docs/ReleaseNotes.rst               |  1 +
 clang/lib/Parse/ParseCXXInlineMethods.cpp |  4 ++--
 clang/test/CodeCompletion/GH200879.cpp    | 10 ++++++++++
 3 files changed, 13 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/CodeCompletion/GH200879.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7828135a6edbc..b90b0b24ef5e0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -706,6 +706,7 @@ Bug Fixes in This Version
 - Clang no longer handles a `" q-char-sequence "` header name as a string 
literal (#GH132643).
 - Fixed an assertion when ``__attribute__((alloc_size))`` is used with an 
argument type wider than the target's pointer width. (#GH190445)
 - Fixed an assertion where we improperly handled implicit conversions to 
integral types from an atomic-type with a conversion function. (#GH201770)
+- Fixed assertion failures involving code completion with delayed default 
arguments and exception specifications. (#GH200879)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Parse/ParseCXXInlineMethods.cpp 
b/clang/lib/Parse/ParseCXXInlineMethods.cpp
index 6189c854e5fbf..24b6adc260a56 100644
--- a/clang/lib/Parse/ParseCXXInlineMethods.cpp
+++ b/clang/lib/Parse/ParseCXXInlineMethods.cpp
@@ -421,7 +421,7 @@ void 
Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
       PP.EnterTokenStream(*Toks, true, /*IsReinject*/ true);
 
       // Consume the previously-pushed token.
-      ConsumeAnyToken();
+      ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
 
       // Consume the '='.
       assert(Tok.is(tok::equal) && "Default argument not starting with '='");
@@ -501,7 +501,7 @@ void 
Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
     PP.EnterTokenStream(*Toks, true, /*IsReinject*/true);
 
     // Consume the previously-pushed token.
-    ConsumeAnyToken();
+    ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
 
     // C++11 [expr.prim.general]p3:
     //   If a declaration declares a member function or member function
diff --git a/clang/test/CodeCompletion/GH200879.cpp 
b/clang/test/CodeCompletion/GH200879.cpp
new file mode 100644
index 0000000000000..62fa7cab03312
--- /dev/null
+++ b/clang/test/CodeCompletion/GH200879.cpp
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:6:2 %s
+// RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:10:2 %s
+
+struct A {
+  A(int = 0);
+}/*invoke completion here*/;
+
+struct B {
+  B() noexcept(false);
+}/*invoke completion here*/;
\ No newline at end of file

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to