Skip to content

Commit

Permalink
[Sema] Push and Pop Expression Evaluation Context Records at the star…
Browse files Browse the repository at this point in the history
…t and end of function definitions

This patch creates a new context for every function definition we enter.
Currently we do not push and pop on these, usually working off of the global
context record added in the Sema constructor, which never gets popped.

Differential Revision: https://reviews.llvm.org/D54014

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@348434 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
PiJoules committed Dec 6, 2018
1 parent 57dc61b commit 8a3afc3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
26 changes: 26 additions & 0 deletions lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12828,6 +12828,7 @@ Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *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();
PushExpressionEvaluationContext(ExprEvalContexts.back().Context);
return D;
}

Expand All @@ -12838,6 +12839,11 @@ Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D,
else
FD = cast<FunctionDecl>(D);

// Do not push if it is a lambda because one is already pushed when building
// the lambda in ActOnStartOfLambdaDefinition().
if (!isLambdaCallOperator(FD))
PushExpressionEvaluationContext(ExprEvalContexts.back().Context);

// Check for defining attributes before the check for redefinition.
if (const auto *Attr = FD->getAttr<AliasAttr>()) {
Diag(Attr->getLocation(), diag::err_alias_is_definition) << FD << 0;
Expand Down Expand Up @@ -13046,6 +13052,21 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *D, Stmt *BodyArg) {
return ActOnFinishFunctionBody(D, BodyArg, false);
}

/// RAII object that pops an ExpressionEvaluationContext when exiting a function
/// body.
class ExitFunctionBodyRAII {
public:
ExitFunctionBodyRAII(Sema &S, bool IsLambda) : S(S), IsLambda(IsLambda) {}
~ExitFunctionBodyRAII() {
if (!IsLambda)
S.PopExpressionEvaluationContext();
}

private:
Sema &S;
bool IsLambda = false;
};

Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body,
bool IsInstantiation) {
FunctionDecl *FD = dcl ? dcl->getAsFunction() : nullptr;
Expand All @@ -13056,6 +13077,11 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body,
if (getLangOpts().CoroutinesTS && getCurFunction()->isCoroutine())
CheckCompletedCoroutineBody(FD, Body);

// Do not call PopExpressionEvaluationContext() if it is a lambda because one
// is already popped when finishing the lambda in BuildLambdaExpr(). This is
// meant to pop the context added in ActOnStartOfFunctionDef().
ExitFunctionBodyRAII ExitRAII(*this, isLambdaCallOperator(FD));

if (FD) {
FD->setBody(Body);
FD->setWillHaveBody(false);
Expand Down
2 changes: 2 additions & 0 deletions lib/Sema/SemaDeclObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
assert((getCurMethodDecl() == nullptr) && "Methodparsing confused");
ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);

PushExpressionEvaluationContext(ExprEvalContexts.back().Context);

// If we don't have a valid method decl, simply return.
if (!MDecl)
return;
Expand Down
7 changes: 2 additions & 5 deletions lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14414,11 +14414,8 @@ void Sema::PopExpressionEvaluationContext() {
// Pop the current expression evaluation context off the stack.
ExprEvalContexts.pop_back();

if (!ExprEvalContexts.empty())
ExprEvalContexts.back().NumTypos += NumTypos;
else
assert(NumTypos == 0 && "There are outstanding typos after popping the "
"last ExpressionEvaluationContextRecord");
// The global expression evaluation context record is never popped.
ExprEvalContexts.back().NumTypos += NumTypos;
}

void Sema::DiscardCleanupsInEvaluationContext() {
Expand Down

0 comments on commit 8a3afc3

Please sign in to comment.