https://github.com/danix800 created https://github.com/llvm/llvm-project/pull/77434
Invalid (direct) initializer would invalid `VarDecl` so `InitializerScopeRAII` cannot restore scope stack balance. As with other kind of initializer, `InitializerScopeRAII::pop()` is moved up before `Sema::ActOnInitializerError()` which invalidates the `VarDecl`, so scope can be balanced and current `DeclContext` can be restored. Fixes #30908 >From 6934d23d9276a0a0997a31d206ffaaa146243db1 Mon Sep 17 00:00:00 2001 From: dingfei <fd...@feysh.com> Date: Tue, 9 Jan 2024 16:40:23 +0800 Subject: [PATCH] [clang][Parser] Pop scope prior VarDecl invalidating by invalid direct Init Invalid (direct) initializer would invalid VarDecl so InitializerScopeRAII cannot restore scope stack balance. As with other kind of initializer, InitializerScopeRAII::pop() is moved up before Sema::ActOnInitializerError() which invalidates the VarDecl, so scope can be balanced and current DeclContext can be restored. Fixes #30908 --- clang/lib/Parse/ParseDecl.cpp | 8 +++++-- ...ope-balance-on-invalid-var-direct-init.cpp | 22 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 clang/test/Parser/gh30908-scope-balance-on-invalid-var-direct-init.cpp diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp index b60ae293ef8c20..ed684c5d57b1ee 100644 --- a/clang/lib/Parse/ParseDecl.cpp +++ b/clang/lib/Parse/ParseDecl.cpp @@ -2661,7 +2661,12 @@ Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes( // ProduceConstructorSignatureHelp only on VarDecls. ExpressionStarts = SetPreferredType; } - if (ParseExpressionList(Exprs, ExpressionStarts)) { + + bool SawError = ParseExpressionList(Exprs, ExpressionStarts); + + InitScope.pop(); + + if (SawError) { if (ThisVarDecl && PP.isCodeCompletionReached() && !CalledSignatureHelp) { Actions.ProduceConstructorSignatureHelp( ThisVarDecl->getType()->getCanonicalTypeInternal(), @@ -2674,7 +2679,6 @@ Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes( } else { // Match the ')'. T.consumeClose(); - InitScope.pop(); ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), T.getCloseLocation(), diff --git a/clang/test/Parser/gh30908-scope-balance-on-invalid-var-direct-init.cpp b/clang/test/Parser/gh30908-scope-balance-on-invalid-var-direct-init.cpp new file mode 100644 index 00000000000000..02200ce4f34a75 --- /dev/null +++ b/clang/test/Parser/gh30908-scope-balance-on-invalid-var-direct-init.cpp @@ -0,0 +1,22 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +#include <non-exist-header> // expected-error {{file not found}} + +class S {}; + +template <typename T> +class E { +public: + E(S* scope) {} + S &getS(); +}; + +class Z { + private: + static E<Z> e; + static S& s(); +}; + +E<Z> Z::e(&__UNKNOWN_ID__); + +S& Z::s() { return Z::e.getS(); } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits