[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:19 PDT 2026


https://github.com/Serosh-commits created https://github.com/llvm/llvm-project/pull/194097

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


>From 44ecbdf4b5a1712762765311577698c5cf15e139 Mon Sep 17 00:00:00 2001
From: Serosh-commits <janmejayapanda400 at gmail.com>
Date: Sat, 25 Apr 2026 06:19:24 +0530
Subject: [PATCH] [Clang][Parser] Fix assertion failure in ::template operator
 parsing (#GH186582)

---
 clang/docs/ReleaseNotes.rst      | 1 +
 clang/lib/Parse/ParseExprCXX.cpp | 8 ++++----
 clang/test/Parser/gh186582.cpp   | 8 ++++++++
 3 files changed, 13 insertions(+), 4 deletions(-)
 create mode 100644 clang/test/Parser/gh186582.cpp

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}}



More information about the cfe-commits mailing list