llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Ding Fei (danix800)

<details>
<summary>Changes</summary>

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

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


2 Files Affected:

- (modified) clang/lib/Parse/ParseDecl.cpp (+6-2) 
- (added) 
clang/test/Parser/gh30908-scope-balance-on-invalid-var-direct-init.cpp (+22) 


``````````diff
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(); }

``````````

</details>


https://github.com/llvm/llvm-project/pull/77434
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to