2010-04-19 16:51:02 +04:00
|
|
|
//===- GRCXXExprEngine.cpp - C++ expr evaluation engine ---------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the C++ expression evaluation engine.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Checker/PathSensitive/AnalysisManager.h"
|
|
|
|
#include "clang/Checker/PathSensitive/GRExprEngine.h"
|
|
|
|
#include "clang/AST/DeclCXX.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2010-09-23 09:14:51 +04:00
|
|
|
namespace {
|
|
|
|
class CallExprWLItem {
|
|
|
|
public:
|
|
|
|
CallExpr::const_arg_iterator I;
|
|
|
|
ExplodedNode *N;
|
|
|
|
|
|
|
|
CallExprWLItem(const CallExpr::const_arg_iterator &i, ExplodedNode *n)
|
|
|
|
: I(i), N(n) {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2010-07-20 10:22:24 +04:00
|
|
|
void GRExprEngine::EvalArguments(ConstExprIterator AI, ConstExprIterator AE,
|
2010-04-20 07:37:34 +04:00
|
|
|
const FunctionProtoType *FnType,
|
|
|
|
ExplodedNode *Pred, ExplodedNodeSet &Dst) {
|
2010-09-23 09:14:51 +04:00
|
|
|
|
|
|
|
|
2010-04-20 07:37:34 +04:00
|
|
|
llvm::SmallVector<CallExprWLItem, 20> WorkList;
|
|
|
|
WorkList.reserve(AE - AI);
|
|
|
|
WorkList.push_back(CallExprWLItem(AI, Pred));
|
|
|
|
|
|
|
|
while (!WorkList.empty()) {
|
|
|
|
CallExprWLItem Item = WorkList.back();
|
|
|
|
WorkList.pop_back();
|
|
|
|
|
|
|
|
if (Item.I == AE) {
|
|
|
|
Dst.insert(Item.N);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-09-23 09:14:51 +04:00
|
|
|
// Evaluate the argument.
|
2010-04-20 07:37:34 +04:00
|
|
|
ExplodedNodeSet Tmp;
|
|
|
|
const unsigned ParamIdx = Item.I - AI;
|
2010-09-23 09:14:51 +04:00
|
|
|
const bool VisitAsLvalue = FnType && ParamIdx < FnType->getNumArgs()
|
|
|
|
? FnType->getArgType(ParamIdx)->isReferenceType()
|
|
|
|
: false;
|
|
|
|
|
2010-04-20 07:37:34 +04:00
|
|
|
if (VisitAsLvalue)
|
|
|
|
VisitLValue(*Item.I, Item.N, Tmp);
|
|
|
|
else
|
|
|
|
Visit(*Item.I, Item.N, Tmp);
|
|
|
|
|
|
|
|
++(Item.I);
|
|
|
|
for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI != NE; ++NI)
|
|
|
|
WorkList.push_back(CallExprWLItem(Item.I, *NI));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-19 16:51:02 +04:00
|
|
|
const CXXThisRegion *GRExprEngine::getCXXThisRegion(const CXXMethodDecl *D,
|
|
|
|
const StackFrameContext *SFC) {
|
|
|
|
Type *T = D->getParent()->getTypeForDecl();
|
|
|
|
QualType PT = getContext().getPointerType(QualType(T,0));
|
|
|
|
return ValMgr.getRegionManager().getCXXThisRegion(PT, SFC);
|
|
|
|
}
|
|
|
|
|
2010-07-20 10:22:24 +04:00
|
|
|
void GRExprEngine::CreateCXXTemporaryObject(const Expr *Ex, ExplodedNode *Pred,
|
2010-04-19 16:51:02 +04:00
|
|
|
ExplodedNodeSet &Dst) {
|
|
|
|
ExplodedNodeSet Tmp;
|
|
|
|
Visit(Ex, Pred, Tmp);
|
|
|
|
for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
|
|
|
|
const GRState *state = GetState(*I);
|
|
|
|
|
|
|
|
// Bind the temporary object to the value of the expression. Then bind
|
|
|
|
// the expression to the location of the object.
|
|
|
|
SVal V = state->getSVal(Ex);
|
|
|
|
|
|
|
|
const MemRegion *R =
|
|
|
|
ValMgr.getRegionManager().getCXXObjectRegion(Ex,
|
|
|
|
Pred->getLocationContext());
|
|
|
|
|
|
|
|
state = state->bindLoc(loc::MemRegionVal(R), V);
|
|
|
|
MakeNode(Dst, Ex, Pred, state->BindExpr(Ex, loc::MemRegionVal(R)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-01 12:09:44 +03:00
|
|
|
void GRExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *E,
|
|
|
|
const MemRegion *Dest,
|
2010-04-19 16:51:02 +04:00
|
|
|
ExplodedNode *Pred,
|
|
|
|
ExplodedNodeSet &Dst) {
|
2010-11-01 12:09:44 +03:00
|
|
|
if (!Dest)
|
|
|
|
Dest = ValMgr.getRegionManager().getCXXObjectRegion(E,
|
|
|
|
Pred->getLocationContext());
|
|
|
|
|
2010-04-19 16:51:02 +04:00
|
|
|
if (E->isElidable()) {
|
|
|
|
VisitAggExpr(E->getArg(0), Dest, Pred, Dst);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CXXConstructorDecl *CD = E->getConstructor();
|
|
|
|
assert(CD);
|
|
|
|
|
2010-05-06 06:59:29 +04:00
|
|
|
if (!(CD->isThisDeclarationADefinition() && AMgr.shouldInlineCall()))
|
2010-04-19 16:51:02 +04:00
|
|
|
// FIXME: invalidate the object.
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
// Evaluate other arguments.
|
|
|
|
ExplodedNodeSet ArgsEvaluated;
|
2010-04-20 09:40:40 +04:00
|
|
|
const FunctionProtoType *FnType = CD->getType()->getAs<FunctionProtoType>();
|
2010-07-20 10:22:24 +04:00
|
|
|
EvalArguments(E->arg_begin(), E->arg_end(), FnType, Pred, ArgsEvaluated);
|
2010-04-19 16:51:02 +04:00
|
|
|
// The callee stack frame context used to create the 'this' parameter region.
|
|
|
|
const StackFrameContext *SFC = AMgr.getStackFrame(CD,
|
|
|
|
Pred->getLocationContext(),
|
|
|
|
E, Builder->getBlock(), Builder->getIndex());
|
|
|
|
|
|
|
|
const CXXThisRegion *ThisR = getCXXThisRegion(E->getConstructor(), SFC);
|
|
|
|
|
2010-07-19 05:31:21 +04:00
|
|
|
CallEnter Loc(E, SFC->getAnalysisContext(), Pred->getLocationContext());
|
2010-04-19 16:51:02 +04:00
|
|
|
for (ExplodedNodeSet::iterator NI = ArgsEvaluated.begin(),
|
|
|
|
NE = ArgsEvaluated.end(); NI != NE; ++NI) {
|
|
|
|
const GRState *state = GetState(*NI);
|
2010-08-24 08:26:55 +04:00
|
|
|
// Setup 'this' region, so that the ctor is evaluated on the object pointed
|
|
|
|
// by 'Dest'.
|
2010-11-01 12:09:44 +03:00
|
|
|
state = state->bindLoc(loc::MemRegionVal(ThisR), loc::MemRegionVal(Dest));
|
2010-04-19 16:51:02 +04:00
|
|
|
ExplodedNode *N = Builder->generateNode(Loc, state, Pred);
|
|
|
|
if (N)
|
|
|
|
Dst.Add(N);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GRExprEngine::VisitCXXMemberCallExpr(const CXXMemberCallExpr *MCE,
|
|
|
|
ExplodedNode *Pred,
|
|
|
|
ExplodedNodeSet &Dst) {
|
|
|
|
// Get the method type.
|
|
|
|
const FunctionProtoType *FnType =
|
|
|
|
MCE->getCallee()->getType()->getAs<FunctionProtoType>();
|
|
|
|
assert(FnType && "Method type not available");
|
|
|
|
|
|
|
|
// Evaluate explicit arguments with a worklist.
|
|
|
|
ExplodedNodeSet ArgsEvaluated;
|
2010-07-20 10:22:24 +04:00
|
|
|
EvalArguments(MCE->arg_begin(), MCE->arg_end(), FnType, Pred, ArgsEvaluated);
|
2010-04-20 09:40:40 +04:00
|
|
|
|
2010-04-19 16:51:02 +04:00
|
|
|
// Evaluate the implicit object argument.
|
|
|
|
ExplodedNodeSet AllArgsEvaluated;
|
|
|
|
const MemberExpr *ME = dyn_cast<MemberExpr>(MCE->getCallee()->IgnoreParens());
|
|
|
|
if (!ME)
|
|
|
|
return;
|
|
|
|
Expr *ObjArgExpr = ME->getBase();
|
|
|
|
for (ExplodedNodeSet::iterator I = ArgsEvaluated.begin(),
|
|
|
|
E = ArgsEvaluated.end(); I != E; ++I) {
|
|
|
|
if (ME->isArrow())
|
|
|
|
Visit(ObjArgExpr, *I, AllArgsEvaluated);
|
|
|
|
else
|
|
|
|
VisitLValue(ObjArgExpr, *I, AllArgsEvaluated);
|
|
|
|
}
|
|
|
|
|
2010-09-30 05:06:29 +04:00
|
|
|
// Allow checkers to pre-visit the member call.
|
|
|
|
ExplodedNodeSet PreVisitChecks;
|
|
|
|
CheckerVisit(MCE, PreVisitChecks, AllArgsEvaluated, PreVisitStmtCallback);
|
|
|
|
|
|
|
|
// Now evaluate the call itself.
|
2010-04-19 16:51:02 +04:00
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
|
|
|
|
assert(MD && "not a CXXMethodDecl?");
|
|
|
|
|
2010-09-30 05:06:29 +04:00
|
|
|
if (!(MD->isThisDeclarationADefinition() && AMgr.shouldInlineCall())) {
|
2010-04-19 16:51:02 +04:00
|
|
|
// FIXME: conservative method call evaluation.
|
2010-09-30 05:06:29 +04:00
|
|
|
CheckerVisit(MCE, Dst, PreVisitChecks, PostVisitStmtCallback);
|
2010-04-19 16:51:02 +04:00
|
|
|
return;
|
2010-09-30 05:06:29 +04:00
|
|
|
}
|
2010-04-19 16:51:02 +04:00
|
|
|
|
2010-09-30 05:06:29 +04:00
|
|
|
ExplodedNodeSet SetupThis;
|
2010-04-19 16:51:02 +04:00
|
|
|
const StackFrameContext *SFC = AMgr.getStackFrame(MD,
|
|
|
|
Pred->getLocationContext(),
|
|
|
|
MCE,
|
|
|
|
Builder->getBlock(),
|
|
|
|
Builder->getIndex());
|
|
|
|
const CXXThisRegion *ThisR = getCXXThisRegion(MD, SFC);
|
2010-07-19 05:31:21 +04:00
|
|
|
CallEnter Loc(MCE, SFC->getAnalysisContext(), Pred->getLocationContext());
|
2010-09-30 05:06:29 +04:00
|
|
|
for (ExplodedNodeSet::iterator I = PreVisitChecks.begin(),
|
|
|
|
E = PreVisitChecks.end(); I != E; ++I) {
|
2010-04-19 16:51:02 +04:00
|
|
|
// Set up 'this' region.
|
|
|
|
const GRState *state = GetState(*I);
|
|
|
|
state = state->bindLoc(loc::MemRegionVal(ThisR),state->getSVal(ObjArgExpr));
|
2010-09-30 05:06:29 +04:00
|
|
|
SetupThis.Add(Builder->generateNode(Loc, state, *I));
|
2010-04-19 16:51:02 +04:00
|
|
|
}
|
2010-09-30 05:06:29 +04:00
|
|
|
|
|
|
|
// FIXME: Perform the actual method call. Right now all we do is evaluate
|
|
|
|
// the arguments.
|
|
|
|
|
|
|
|
// Perform post-visit.
|
|
|
|
CheckerVisit(MCE, Dst, /* FIXME: don't forget to update later */ SetupThis,
|
|
|
|
PostVisitStmtCallback);
|
2010-04-19 16:51:02 +04:00
|
|
|
}
|
|
|
|
|
2010-07-20 10:22:24 +04:00
|
|
|
void GRExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred,
|
2010-04-19 16:51:02 +04:00
|
|
|
ExplodedNodeSet &Dst) {
|
|
|
|
if (CNE->isArray()) {
|
|
|
|
// FIXME: allocating an array has not been handled.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Count = Builder->getCurrentBlockCount();
|
2010-07-20 10:22:24 +04:00
|
|
|
DefinedOrUnknownSVal SymVal = getValueManager().getConjuredSymbolVal(NULL,CNE,
|
2010-04-19 16:51:02 +04:00
|
|
|
CNE->getType(), Count);
|
|
|
|
const MemRegion *NewReg = cast<loc::MemRegionVal>(SymVal).getRegion();
|
|
|
|
|
|
|
|
QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType();
|
|
|
|
|
|
|
|
const ElementRegion *EleReg =
|
|
|
|
getStoreManager().GetElementZeroRegion(NewReg, ObjTy);
|
|
|
|
|
2010-04-20 07:37:34 +04:00
|
|
|
// Evaluate constructor arguments.
|
|
|
|
const FunctionProtoType *FnType = NULL;
|
|
|
|
const CXXConstructorDecl *CD = CNE->getConstructor();
|
|
|
|
if (CD)
|
|
|
|
FnType = CD->getType()->getAs<FunctionProtoType>();
|
|
|
|
ExplodedNodeSet ArgsEvaluated;
|
|
|
|
EvalArguments(CNE->constructor_arg_begin(), CNE->constructor_arg_end(),
|
|
|
|
FnType, Pred, ArgsEvaluated);
|
2010-04-19 16:51:02 +04:00
|
|
|
|
2010-04-20 07:37:34 +04:00
|
|
|
// Initialize the object region and bind the 'new' expression.
|
|
|
|
for (ExplodedNodeSet::iterator I = ArgsEvaluated.begin(),
|
|
|
|
E = ArgsEvaluated.end(); I != E; ++I) {
|
|
|
|
const GRState *state = GetState(*I);
|
|
|
|
|
|
|
|
if (ObjTy->isRecordType()) {
|
2010-08-04 00:44:35 +04:00
|
|
|
state = state->InvalidateRegion(EleReg, CNE, Count);
|
2010-04-20 07:37:34 +04:00
|
|
|
} else {
|
|
|
|
if (CNE->hasInitializer()) {
|
|
|
|
SVal V = state->getSVal(*CNE->constructor_arg_begin());
|
|
|
|
state = state->bindLoc(loc::MemRegionVal(EleReg), V);
|
|
|
|
} else {
|
|
|
|
// Explicitly set to undefined, because currently we retrieve symbolic
|
|
|
|
// value from symbolic region.
|
|
|
|
state = state->bindLoc(loc::MemRegionVal(EleReg), UndefinedVal());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
state = state->BindExpr(CNE, loc::MemRegionVal(EleReg));
|
2010-04-21 06:20:10 +04:00
|
|
|
MakeNode(Dst, CNE, *I, state);
|
2010-04-20 07:37:34 +04:00
|
|
|
}
|
2010-04-19 16:51:02 +04:00
|
|
|
}
|
|
|
|
|
2010-07-20 10:22:24 +04:00
|
|
|
void GRExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE,
|
|
|
|
ExplodedNode *Pred,ExplodedNodeSet &Dst) {
|
2010-04-21 06:17:31 +04:00
|
|
|
// Should do more checking.
|
|
|
|
ExplodedNodeSet ArgEvaluated;
|
|
|
|
Visit(CDE->getArgument(), Pred, ArgEvaluated);
|
|
|
|
for (ExplodedNodeSet::iterator I = ArgEvaluated.begin(),
|
|
|
|
E = ArgEvaluated.end(); I != E; ++I) {
|
|
|
|
const GRState *state = GetState(*I);
|
|
|
|
MakeNode(Dst, CDE, *I, state);
|
|
|
|
}
|
|
|
|
}
|
2010-04-19 16:51:02 +04:00
|
|
|
|
2010-07-20 10:22:24 +04:00
|
|
|
void GRExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred,
|
2010-04-21 06:17:31 +04:00
|
|
|
ExplodedNodeSet &Dst) {
|
2010-04-19 16:51:02 +04:00
|
|
|
// Get the this object region from StoreManager.
|
|
|
|
const MemRegion *R =
|
|
|
|
ValMgr.getRegionManager().getCXXThisRegion(
|
|
|
|
getContext().getCanonicalType(TE->getType()),
|
|
|
|
Pred->getLocationContext());
|
|
|
|
|
|
|
|
const GRState *state = GetState(Pred);
|
|
|
|
SVal V = state->getSVal(loc::MemRegionVal(R));
|
|
|
|
MakeNode(Dst, TE, Pred, state->BindExpr(TE, V));
|
|
|
|
}
|