]> granicus.if.org Git - clang/commit
Push a function scope when parsing function bodies without a declaration
authorReid Kleckner <rnk@google.com>
Wed, 7 Mar 2018 18:55:10 +0000 (18:55 +0000)
committerReid Kleckner <rnk@google.com>
Wed, 7 Mar 2018 18:55:10 +0000 (18:55 +0000)
commitc8bc50c0d95eeabd3f43d342acab96bf4809d1a7
tree05be1268dcb76c18ab85ef4e8297d949cd053c9a
parenteea47794b2e8ab41dc2146663ab3a3d3aea7dcef
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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@326926 91177308-0d34-0410-b5e6-96231b3b80d8
lib/Sema/SemaDecl.cpp
test/SemaCXX/pr36536.cpp [new file with mode: 0644]