2009-09-11 12:13:42 +08:00
|
|
|
//===--- CallInliner.cpp - Transfer function that inlines callee ----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the callee inlining transfer function.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-01-25 12:41:41 +08:00
|
|
|
#include "clang/Checker/PathSensitive/CheckerVisitor.h"
|
|
|
|
#include "clang/Checker/PathSensitive/GRState.h"
|
2010-01-27 06:59:55 +08:00
|
|
|
#include "clang/Checker/Checkers/LocalCheckers.h"
|
2009-09-11 12:13:42 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
2009-12-23 16:56:18 +08:00
|
|
|
class CallInliner : public Checker {
|
2009-09-11 12:13:42 +08:00
|
|
|
public:
|
2009-12-23 16:56:18 +08:00
|
|
|
static void *getTag() {
|
|
|
|
static int x;
|
|
|
|
return &x;
|
|
|
|
}
|
2009-09-11 12:13:42 +08:00
|
|
|
|
2009-12-23 16:56:18 +08:00
|
|
|
virtual bool EvalCallExpr(CheckerContext &C, const CallExpr *CE);
|
2009-12-24 10:25:21 +08:00
|
|
|
virtual void EvalEndPath(GREndPathNodeBuilder &B,void *tag,GRExprEngine &Eng);
|
2009-09-11 12:13:42 +08:00
|
|
|
};
|
2009-12-23 16:56:18 +08:00
|
|
|
}
|
2009-09-11 12:13:42 +08:00
|
|
|
|
2009-12-23 16:56:18 +08:00
|
|
|
void clang::RegisterCallInliner(GRExprEngine &Eng) {
|
|
|
|
Eng.registerCheck(new CallInliner());
|
2009-09-11 12:13:42 +08:00
|
|
|
}
|
|
|
|
|
2009-12-23 16:56:18 +08:00
|
|
|
bool CallInliner::EvalCallExpr(CheckerContext &C, const CallExpr *CE) {
|
|
|
|
const GRState *state = C.getState();
|
|
|
|
const Expr *Callee = CE->getCallee();
|
|
|
|
SVal L = state->getSVal(Callee);
|
2009-12-24 11:34:38 +08:00
|
|
|
|
2009-12-23 16:56:18 +08:00
|
|
|
const FunctionDecl *FD = L.getAsFunctionDecl();
|
2009-10-13 10:36:42 +08:00
|
|
|
if (!FD)
|
2009-12-23 16:56:18 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!FD->isThisDeclarationADefinition())
|
|
|
|
return false;
|
2009-10-13 10:36:42 +08:00
|
|
|
|
2009-12-24 11:34:38 +08:00
|
|
|
GRStmtNodeBuilder &Builder = C.getNodeBuilder();
|
2009-10-13 10:36:42 +08:00
|
|
|
// Make a new LocationContext.
|
2009-12-23 16:56:18 +08:00
|
|
|
const StackFrameContext *LocCtx = C.getAnalysisManager().getStackFrame(FD,
|
2009-12-24 11:34:38 +08:00
|
|
|
C.getPredecessor()->getLocationContext(), CE,
|
|
|
|
Builder.getBlock(), Builder.getIndex());
|
2009-10-13 10:36:42 +08:00
|
|
|
|
|
|
|
CFGBlock const *Entry = &(LocCtx->getCFG()->getEntry());
|
|
|
|
|
|
|
|
assert (Entry->empty() && "Entry block must be empty.");
|
|
|
|
|
|
|
|
assert (Entry->succ_size() == 1 && "Entry block must have 1 successor.");
|
|
|
|
|
|
|
|
// Get the solitary successor.
|
|
|
|
CFGBlock const *SuccB = *(Entry->succ_begin());
|
|
|
|
|
|
|
|
// Construct an edge representing the starting location in the function.
|
|
|
|
BlockEdge Loc(Entry, SuccB, LocCtx);
|
|
|
|
|
2009-12-23 16:56:18 +08:00
|
|
|
state = C.getStoreManager().EnterStackFrame(state, LocCtx);
|
|
|
|
// This is a hack. We really should not use the GRStmtNodeBuilder.
|
2009-10-13 10:36:42 +08:00
|
|
|
bool isNew;
|
2009-12-23 16:56:18 +08:00
|
|
|
GRExprEngine &Eng = C.getEngine();
|
|
|
|
ExplodedNode *Pred = C.getPredecessor();
|
2009-12-24 11:34:38 +08:00
|
|
|
|
2009-10-13 10:36:42 +08:00
|
|
|
|
2009-12-23 16:56:18 +08:00
|
|
|
ExplodedNode *SuccN = Eng.getGraph().getNode(Loc, state, &isNew);
|
|
|
|
SuccN->addPredecessor(Pred, Eng.getGraph());
|
|
|
|
C.getNodeBuilder().Deferred.erase(Pred);
|
|
|
|
|
2009-10-13 10:36:42 +08:00
|
|
|
if (isNew)
|
|
|
|
Builder.getWorkList()->Enqueue(SuccN);
|
|
|
|
|
|
|
|
Builder.HasGeneratedNode = true;
|
2009-12-23 16:56:18 +08:00
|
|
|
|
|
|
|
return true;
|
2009-09-11 12:13:42 +08:00
|
|
|
}
|
2009-12-23 16:56:18 +08:00
|
|
|
|
2009-12-24 10:25:21 +08:00
|
|
|
void CallInliner::EvalEndPath(GREndPathNodeBuilder &B, void *tag,
|
|
|
|
GRExprEngine &Eng) {
|
|
|
|
const GRState *state = B.getState();
|
|
|
|
ExplodedNode *Pred = B.getPredecessor();
|
|
|
|
const StackFrameContext *LocCtx =
|
|
|
|
cast<StackFrameContext>(Pred->getLocationContext());
|
|
|
|
|
|
|
|
const Stmt *CE = LocCtx->getCallSite();
|
|
|
|
|
|
|
|
// Check if this is the top level stack frame.
|
|
|
|
if (!LocCtx->getParent())
|
|
|
|
return;
|
|
|
|
|
|
|
|
PostStmt NodeLoc(CE, LocCtx->getParent());
|
|
|
|
|
|
|
|
bool isNew;
|
|
|
|
ExplodedNode *Succ = Eng.getGraph().getNode(NodeLoc, state, &isNew);
|
|
|
|
Succ->addPredecessor(Pred, Eng.getGraph());
|
|
|
|
|
|
|
|
// When creating the new work list unit, increment the statement index to
|
|
|
|
// point to the statement after the CallExpr.
|
|
|
|
if (isNew)
|
2009-12-24 11:34:38 +08:00
|
|
|
B.getWorkList().Enqueue(Succ,
|
|
|
|
*const_cast<CFGBlock*>(LocCtx->getCallSiteBlock()),
|
|
|
|
LocCtx->getIndex() + 1);
|
2009-12-24 10:25:21 +08:00
|
|
|
|
|
|
|
B.HasGeneratedNode = true;
|
|
|
|
}
|