From b2da08610327d32474a931502c03f486ccbe36ac Mon Sep 17 00:00:00 2001 From: Reid Kleckner Date: Wed, 7 Mar 2018 18:55:10 +0000 Subject: [PATCH] Push a function scope when parsing function bodies without a declaration Summary: This is PR36536. There are a few ways to reach Sema::ActOnStartOfFunctionDef with a null Decl. Currently, the parser continues on to attempt to parse the statements in the function body without pushing a function scope or declaration context. However, lots of statement parsing logic relies on getCurFunction() returning something reasonable. It turns out that getCurFunction() will never return null today because of an optimization where Sema pre-allocates one FunctionScopeInfo and reuses it when possible. This goes wrong when something inside the function body causes us to push another function scope, such as requiring an implicit definition of a special member function. Reusing the state clears it out, which will lead to bugs. In PR36536, we found that the SwitchStack gets unbalanced, because we push a switch, clear out the stack, and then try to pop a switch that isn't there. As a follow-up, I plan to move the pre-allocated FunctionScopeInfo out of the FunctionScopes stack. This means the FunctionScopes stack will often be empty, and callers of getCurFunction() will need to check for null. Reviewers: thakis Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D43980 llvm-svn: 326926 --- clang/lib/Sema/SemaDecl.cpp | 10 +++++++++- clang/test/SemaCXX/pr36536.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 clang/test/SemaCXX/pr36536.cpp diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index c2e3da3..03dd929 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -12406,8 +12406,13 @@ static void RebuildLambdaScopeInfo(CXXMethodDecl *CallOperator, Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D, SkipBodyInfo *SkipBody) { - if (!D) + if (!D) { + // Parsing the function declaration failed in some way. Push on a fake scope + // anyway so we can try to parse the function body. + PushFunctionScope(); return D; + } + FunctionDecl *FD = nullptr; if (FunctionTemplateDecl *FunTmpl = dyn_cast(D)) @@ -12816,6 +12821,9 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body, getCurFunction()->ObjCWarnForNoInitDelegation = false; } } else { + // Parsing the function declaration failed in some way. Pop the fake scope + // we pushed on. + PopFunctionScopeInfo(ActivePolicy, dcl); return nullptr; } diff --git a/clang/test/SemaCXX/pr36536.cpp b/clang/test/SemaCXX/pr36536.cpp new file mode 100644 index 0000000..702b9cc --- /dev/null +++ b/clang/test/SemaCXX/pr36536.cpp @@ -0,0 +1,44 @@ +// RUN: %clang_cc1 -std=c++11 %s -verify -fno-spell-checking + +// These test cases are constructed to make clang call ActOnStartOfFunctionDef +// with nullptr. + +struct ImplicitDefaultCtor1 {}; +struct Foo { + typedef int NameInClass; + void f(); +}; +namespace bar { +// FIXME: Improved our recovery to make this a redeclaration of Foo::f, +// even though this is in the wrong namespace. That will allow name lookup to +// find NameInClass below. Users are likely to hit this when they forget to +// close namespaces. +// expected-error@+1 {{cannot define or redeclare 'f' here}} +void Foo::f() { + switch (0) { case 0: ImplicitDefaultCtor1 o; } + // expected-error@+1 {{unknown type name 'NameInClass'}} + NameInClass var; +} +} // namespace bar + +struct ImplicitDefaultCtor2 {}; +template class TFoo { void f(); }; +// expected-error@+1 {{nested name specifier 'decltype(TFoo())::'}} +template void decltype(TFoo())::f() { + switch (0) { case 0: ImplicitDefaultCtor1 o; } +} + +namespace tpl2 { +struct ImplicitDefaultCtor3 {}; +template class A { + template class B { + void mf2(); + }; +}; +template +template <> +// expected-error@+1 {{nested name specifier 'A::B::'}} +void A::B::mf2() { + switch (0) { case 0: ImplicitDefaultCtor3 o; } +} +} -- 2.7.4