[clang] [clang] Fix assertion failures involving code completion with delayed default arguments and exception specifications (PR #203794)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Jun 14 14:03:56 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Harlen Batagelo (hbatagelo)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/203794.diff
3 Files Affected:
- (modified) clang/docs/ReleaseNotes.rst (+1)
- (modified) clang/lib/Parse/ParseCXXInlineMethods.cpp (+2-2)
- (added) clang/test/CodeCompletion/GH200879.cpp (+10)
``````````diff
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
``````````
</details>
https://github.com/llvm/llvm-project/pull/203794
More information about the cfe-commits
mailing list