[clang] [Clang] fix assertion failure in ::template operator parsing (PR #194097)

via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 24 17:59:55 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Serosh (Serosh-commits)

<details>
<summary>Changes</summary>

when parsing an invalid `::template operator`, the parser incorrectly kept the consumed tokens on error. This caused the token cache to go out of sync and crash. This patch fixes it by reverting the tokens and properly returning the error 
fixes #<!-- -->186582


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


3 Files Affected:

- (modified) clang/docs/ReleaseNotes.rst (+1) 
- (modified) clang/lib/Parse/ParseExprCXX.cpp (+4-4) 
- (added) clang/test/Parser/gh186582.cpp (+8) 


``````````diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 899a4ee0dee0e..1f7b62503b92e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -483,6 +483,7 @@ Improvements to Coverage Mapping
 
 Bug Fixes in This Version
 -------------------------
+- Fixed a preprocessor assertion failure triggered when parsing an invalid template-id starting with `::template operator`. (#GH186582)
 - Fix a crash when marco name is empty in ``#pragma push_macro("")`` or
   ``#pragma pop_macro("")``. (#GH149762).
 - Fix a crash in variable length array (e.g. ``int a[*]``) function parameter type
diff --git a/clang/lib/Parse/ParseExprCXX.cpp b/clang/lib/Parse/ParseExprCXX.cpp
index 7a5d28caf8521..18d903e8bfe32 100644
--- a/clang/lib/Parse/ParseExprCXX.cpp
+++ b/clang/lib/Parse/ParseExprCXX.cpp
@@ -286,8 +286,8 @@ bool Parser::ParseOptionalCXXScopeSpecifier(
         // we already annotated the template-id.
         if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType,
                                        TemplateName)) {
-          TPA.Commit();
-          break;
+          TPA.Revert();
+          return true;
         }
 
         if (TemplateName.getKind() != UnqualifiedIdKind::IK_OperatorFunctionId &&
@@ -295,8 +295,8 @@ bool Parser::ParseOptionalCXXScopeSpecifier(
           Diag(TemplateName.getSourceRange().getBegin(),
                diag::err_id_after_template_in_nested_name_spec)
             << TemplateName.getSourceRange();
-          TPA.Commit();
-          break;
+          TPA.Revert();
+          return true;
         }
       } else {
         TPA.Revert();
diff --git a/clang/test/Parser/gh186582.cpp b/clang/test/Parser/gh186582.cpp
new file mode 100644
index 0000000000000..d5b1102586833
--- /dev/null
+++ b/clang/test/Parser/gh186582.cpp
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+a(   ::template operator
+// expected-error at -1 {{unknown type name 'a'}}
+// expected-error at -2 {{expected unqualified-id}}
+// expected-error at -3 {{expected ')'}}
+// expected-note at -4 {{to match this '('}}
+// expected-error@* 2{{expected a type}}

``````````

</details>


https://github.com/llvm/llvm-project/pull/194097


More information about the cfe-commits mailing list