2018-02-28 09:10:04 +08:00
|
|
|
//===- CallEvent.cpp - Wrapper for all function and method calls ----------===//
|
2012-07-03 03:27:35 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
/// \file This file defines CallEvent and its subclasses, which represent path-
|
|
|
|
/// sensitive instances of different kinds of function and method calls
|
|
|
|
/// (C, C++, and Objective-C).
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-07-27 05:39:41 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
|
2018-02-28 09:10:04 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/DeclBase.h"
|
|
|
|
#include "clang/AST/DeclCXX.h"
|
|
|
|
#include "clang/AST/DeclObjC.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "clang/AST/ExprCXX.h"
|
|
|
|
#include "clang/AST/ExprObjC.h"
|
2012-07-19 05:59:41 +08:00
|
|
|
#include "clang/AST/ParentMap.h"
|
2018-02-28 09:10:04 +08:00
|
|
|
#include "clang/AST/Stmt.h"
|
|
|
|
#include "clang/AST/Type.h"
|
|
|
|
#include "clang/Analysis/AnalysisDeclContext.h"
|
|
|
|
#include "clang/Analysis/CFG.h"
|
2018-08-01 09:58:15 +08:00
|
|
|
#include "clang/Analysis/CFGStmtMap.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Analysis/ProgramPoint.h"
|
2018-03-01 22:54:16 +08:00
|
|
|
#include "clang/CrossTU/CrossTranslationUnit.h"
|
2018-02-28 09:10:04 +08:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
|
|
|
#include "clang/Basic/LLVM.h"
|
|
|
|
#include "clang/Basic/SourceLocation.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Basic/Specifiers.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2018-02-28 09:10:04 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeInfo.h"
|
2015-09-12 01:19:57 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/DynamicTypeMap.h"
|
2018-02-28 09:10:04 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/Store.h"
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/None.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/ADT/PointerIntPair.h"
|
2012-07-03 03:27:35 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2018-02-28 09:10:04 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2012-07-03 03:28:04 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2018-02-28 09:10:04 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
2017-09-22 08:37:12 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2018-02-28 09:10:04 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <utility>
|
2017-09-22 08:37:12 +08:00
|
|
|
|
|
|
|
#define DEBUG_TYPE "static-analyzer-call-event"
|
2012-07-03 03:27:35 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
|
|
|
QualType CallEvent::getResultType() const {
|
2018-03-24 09:53:12 +08:00
|
|
|
ASTContext &Ctx = getState()->getStateManager().getContext();
|
[analyzer] Always derive a CallEvent's return type from its origin expr.
Previously, we preferred to get a result type by looking at the callee's
declared result type. This allowed us to handlereferences, which are
represented in the AST as lvalues of their pointee type. (That is, a call
to a function returning 'int &' has type 'int' and value kind 'lvalue'.)
However, this results in us preferring the original type of a function
over a casted type. This is a problem when a function pointer is casted
to another type, because the conjured result value will have the wrong
type. AdjustedReturnValueChecker is supposed to handle this, but still
doesn't handle the case where there is no "original function" at all,
i.e. where the callee is unknown.
Now, we instead look at the call expression's value kind (lvalue, xvalue,
or prvalue), and adjust the expr's type accordingly. This will have no
effect when the function is inlined, and will conjure the value that will
actually be used when it is not.
This makes AdjustedReturnValueChecker /nearly/ unnecessary; unfortunately,
the cases where it would still be useful are where we need to cast the
result of an inlined function or a checker-evaluated function, and in these
cases we don't know what we're casting /from/ by the time we can do post-
call checks. In light of that, remove AdjustedReturnValueChecker, which
was already not checking quite a few calls.
llvm-svn: 163065
2012-09-02 01:39:00 +08:00
|
|
|
const Expr *E = getOriginExpr();
|
2018-03-24 09:53:12 +08:00
|
|
|
if (!E)
|
|
|
|
return Ctx.VoidTy;
|
|
|
|
assert(E);
|
2012-07-03 03:27:35 +08:00
|
|
|
|
2018-03-24 09:53:12 +08:00
|
|
|
QualType ResultTy = E->getType();
|
[analyzer] Always derive a CallEvent's return type from its origin expr.
Previously, we preferred to get a result type by looking at the callee's
declared result type. This allowed us to handlereferences, which are
represented in the AST as lvalues of their pointee type. (That is, a call
to a function returning 'int &' has type 'int' and value kind 'lvalue'.)
However, this results in us preferring the original type of a function
over a casted type. This is a problem when a function pointer is casted
to another type, because the conjured result value will have the wrong
type. AdjustedReturnValueChecker is supposed to handle this, but still
doesn't handle the case where there is no "original function" at all,
i.e. where the callee is unknown.
Now, we instead look at the call expression's value kind (lvalue, xvalue,
or prvalue), and adjust the expr's type accordingly. This will have no
effect when the function is inlined, and will conjure the value that will
actually be used when it is not.
This makes AdjustedReturnValueChecker /nearly/ unnecessary; unfortunately,
the cases where it would still be useful are where we need to cast the
result of an inlined function or a checker-evaluated function, and in these
cases we don't know what we're casting /from/ by the time we can do post-
call checks. In light of that, remove AdjustedReturnValueChecker, which
was already not checking quite a few calls.
llvm-svn: 163065
2012-09-02 01:39:00 +08:00
|
|
|
|
|
|
|
// A function that returns a reference to 'int' will have a result type
|
|
|
|
// of simply 'int'. Check the origin expr's value kind to recover the
|
|
|
|
// proper type.
|
|
|
|
switch (E->getValueKind()) {
|
|
|
|
case VK_LValue:
|
|
|
|
ResultTy = Ctx.getLValueReferenceType(ResultTy);
|
|
|
|
break;
|
|
|
|
case VK_XValue:
|
|
|
|
ResultTy = Ctx.getRValueReferenceType(ResultTy);
|
|
|
|
break;
|
|
|
|
case VK_RValue:
|
|
|
|
// No adjustment is necessary.
|
|
|
|
break;
|
|
|
|
}
|
2012-07-03 03:27:35 +08:00
|
|
|
|
|
|
|
return ResultTy;
|
|
|
|
}
|
|
|
|
|
2015-10-28 04:19:45 +08:00
|
|
|
static bool isCallback(QualType T) {
|
2012-07-03 03:27:35 +08:00
|
|
|
// If a parameter is a block or a callback, assume it can modify pointer.
|
|
|
|
if (T->isBlockPointerType() ||
|
|
|
|
T->isFunctionPointerType() ||
|
|
|
|
T->isObjCSelType())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Check if a callback is passed inside a struct (for both, struct passed by
|
|
|
|
// reference and by value). Dig just one level into the struct for now.
|
|
|
|
|
2012-09-06 01:11:22 +08:00
|
|
|
if (T->isAnyPointerType() || T->isReferenceType())
|
2012-07-03 03:27:35 +08:00
|
|
|
T = T->getPointeeType();
|
|
|
|
|
|
|
|
if (const RecordType *RT = T->getAsStructureType()) {
|
|
|
|
const RecordDecl *RD = RT->getDecl();
|
2014-03-09 04:12:42 +08:00
|
|
|
for (const auto *I : RD->fields()) {
|
2012-07-03 03:27:35 +08:00
|
|
|
QualType FieldT = I->getType();
|
|
|
|
if (FieldT->isBlockPointerType() || FieldT->isFunctionPointerType())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-28 04:19:45 +08:00
|
|
|
static bool isVoidPointerToNonConst(QualType T) {
|
2018-02-28 09:10:04 +08:00
|
|
|
if (const auto *PT = T->getAs<PointerType>()) {
|
2015-10-28 04:19:45 +08:00
|
|
|
QualType PointeeTy = PT->getPointeeType();
|
|
|
|
if (PointeeTy.isConstQualified())
|
|
|
|
return false;
|
|
|
|
return PointeeTy->isVoidType();
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CallEvent::hasNonNullArgumentsWithType(bool (*Condition)(QualType)) const {
|
2012-07-03 03:27:35 +08:00
|
|
|
unsigned NumOfArgs = getNumArgs();
|
|
|
|
|
|
|
|
// If calling using a function pointer, assume the function does not
|
2015-10-28 04:19:45 +08:00
|
|
|
// satisfy the callback.
|
|
|
|
// TODO: We could check the types of the arguments here.
|
2012-07-03 03:27:35 +08:00
|
|
|
if (!getDecl())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned Idx = 0;
|
|
|
|
for (CallEvent::param_type_iterator I = param_type_begin(),
|
2015-10-28 04:19:45 +08:00
|
|
|
E = param_type_end();
|
2012-07-03 03:27:35 +08:00
|
|
|
I != E && Idx < NumOfArgs; ++I, ++Idx) {
|
2015-10-28 04:19:45 +08:00
|
|
|
// If the parameter is 0, it's harmless.
|
|
|
|
if (getArgSVal(Idx).isZeroConstant())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (Condition(*I))
|
2012-07-03 03:27:35 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-28 04:19:45 +08:00
|
|
|
bool CallEvent::hasNonZeroCallbackArg() const {
|
|
|
|
return hasNonNullArgumentsWithType(isCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CallEvent::hasVoidPointerToNonConstArg() const {
|
|
|
|
return hasNonNullArgumentsWithType(isVoidPointerToNonConst);
|
|
|
|
}
|
|
|
|
|
2012-11-03 07:49:29 +08:00
|
|
|
bool CallEvent::isGlobalCFunction(StringRef FunctionName) const {
|
2018-02-28 09:10:04 +08:00
|
|
|
const auto *FD = dyn_cast_or_null<FunctionDecl>(getDecl());
|
2012-11-03 07:49:29 +08:00
|
|
|
if (!FD)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return CheckerContext::isCLibraryFunction(FD, FunctionName);
|
|
|
|
}
|
|
|
|
|
2018-08-01 09:58:15 +08:00
|
|
|
AnalysisDeclContext *CallEvent::getCalleeAnalysisDeclContext() const {
|
|
|
|
const Decl *D = getDecl();
|
|
|
|
if (!D)
|
|
|
|
return nullptr;
|
|
|
|
|
2018-08-15 08:33:55 +08:00
|
|
|
// TODO: For now we skip functions without definitions, even if we have
|
|
|
|
// our own getDecl(), because it's hard to find out which re-declaration
|
|
|
|
// is going to be used, and usually clients don't really care about this
|
|
|
|
// situation because there's a loss of precision anyway because we cannot
|
|
|
|
// inline the call.
|
|
|
|
RuntimeDefinition RD = getRuntimeDefinition();
|
|
|
|
if (!RD.getDecl())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
AnalysisDeclContext *ADC =
|
|
|
|
LCtx->getAnalysisDeclContext()->getManager()->getContext(D);
|
|
|
|
|
|
|
|
// TODO: For now we skip virtual functions, because this also rises
|
|
|
|
// the problem of which decl to use, but now it's across different classes.
|
|
|
|
if (RD.mayHaveOtherDefinitions() || RD.getDecl() != ADC->getDecl())
|
|
|
|
return nullptr;
|
2018-08-01 09:58:15 +08:00
|
|
|
|
2018-08-15 08:33:55 +08:00
|
|
|
return ADC;
|
2018-08-01 09:58:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const StackFrameContext *CallEvent::getCalleeStackFrame() const {
|
|
|
|
AnalysisDeclContext *ADC = getCalleeAnalysisDeclContext();
|
|
|
|
if (!ADC)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const Expr *E = getOriginExpr();
|
|
|
|
if (!E)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Recover CFG block via reverse lookup.
|
|
|
|
// TODO: If we were to keep CFG element information as part of the CallEvent
|
|
|
|
// instead of doing this reverse lookup, we would be able to build the stack
|
|
|
|
// frame for non-expression-based calls, and also we wouldn't need the reverse
|
|
|
|
// lookup.
|
|
|
|
CFGStmtMap *Map = LCtx->getAnalysisDeclContext()->getCFGStmtMap();
|
|
|
|
const CFGBlock *B = Map->getBlock(E);
|
|
|
|
assert(B);
|
|
|
|
|
|
|
|
// Also recover CFG index by scanning the CFG block.
|
|
|
|
unsigned Idx = 0, Sz = B->size();
|
|
|
|
for (; Idx < Sz; ++Idx)
|
|
|
|
if (auto StmtElem = (*B)[Idx].getAs<CFGStmt>())
|
|
|
|
if (StmtElem->getStmt() == E)
|
|
|
|
break;
|
|
|
|
assert(Idx < Sz);
|
|
|
|
|
|
|
|
return ADC->getManager()->getStackFrame(ADC, LCtx, E, B, Idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
const VarRegion *CallEvent::getParameterLocation(unsigned Index) const {
|
|
|
|
const StackFrameContext *SFC = getCalleeStackFrame();
|
|
|
|
// We cannot construct a VarRegion without a stack frame.
|
|
|
|
if (!SFC)
|
|
|
|
return nullptr;
|
|
|
|
|
2018-08-15 08:33:55 +08:00
|
|
|
// Retrieve parameters of the definition, which are different from
|
|
|
|
// CallEvent's parameters() because getDecl() isn't necessarily
|
|
|
|
// the definition. SFC contains the definition that would be used
|
|
|
|
// during analysis.
|
|
|
|
const Decl *D = SFC->getDecl();
|
|
|
|
|
|
|
|
// TODO: Refactor into a virtual method of CallEvent, like parameters().
|
|
|
|
const ParmVarDecl *PVD = nullptr;
|
|
|
|
if (const auto *FD = dyn_cast<FunctionDecl>(D))
|
|
|
|
PVD = FD->parameters()[Index];
|
|
|
|
else if (const auto *BD = dyn_cast<BlockDecl>(D))
|
|
|
|
PVD = BD->parameters()[Index];
|
|
|
|
else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
|
|
|
|
PVD = MD->parameters()[Index];
|
|
|
|
else if (const auto *CD = dyn_cast<CXXConstructorDecl>(D))
|
|
|
|
PVD = CD->parameters()[Index];
|
|
|
|
assert(PVD && "Unexpected Decl kind!");
|
|
|
|
|
2018-08-01 09:58:15 +08:00
|
|
|
const VarRegion *VR =
|
|
|
|
State->getStateManager().getRegionManager().getVarRegion(PVD, SFC);
|
|
|
|
|
|
|
|
// This sanity check would fail if our parameter declaration doesn't
|
|
|
|
// correspond to the stack frame's function declaration.
|
|
|
|
assert(VR->getStackFrame() == SFC);
|
|
|
|
|
|
|
|
return VR;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns true if a type is a pointer-to-const or reference-to-const
|
2012-07-03 03:27:35 +08:00
|
|
|
/// with no further indirection.
|
|
|
|
static bool isPointerToConst(QualType Ty) {
|
|
|
|
QualType PointeeTy = Ty->getPointeeType();
|
|
|
|
if (PointeeTy == QualType())
|
|
|
|
return false;
|
|
|
|
if (!PointeeTy.isConstQualified())
|
|
|
|
return false;
|
|
|
|
if (PointeeTy->isAnyPointerType())
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to retrieve the function declaration and find the function parameter
|
|
|
|
// types which are pointers/references to a non-pointer const.
|
2012-07-03 03:27:51 +08:00
|
|
|
// We will not invalidate the corresponding argument regions.
|
2013-03-21 04:35:53 +08:00
|
|
|
static void findPtrToConstParams(llvm::SmallSet<unsigned, 4> &PreserveArgs,
|
2012-07-03 03:27:35 +08:00
|
|
|
const CallEvent &Call) {
|
|
|
|
unsigned Idx = 0;
|
|
|
|
for (CallEvent::param_type_iterator I = Call.param_type_begin(),
|
2012-07-03 03:27:51 +08:00
|
|
|
E = Call.param_type_end();
|
2012-07-03 03:27:35 +08:00
|
|
|
I != E; ++I, ++Idx) {
|
|
|
|
if (isPointerToConst(*I))
|
|
|
|
PreserveArgs.insert(Idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgramStateRef CallEvent::invalidateRegions(unsigned BlockCount,
|
2013-03-21 04:35:53 +08:00
|
|
|
ProgramStateRef Orig) const {
|
2012-07-19 05:59:41 +08:00
|
|
|
ProgramStateRef Result = (Orig ? Orig : getState());
|
2012-07-03 03:27:35 +08:00
|
|
|
|
2014-05-07 11:29:56 +08:00
|
|
|
// Don't invalidate anything if the callee is marked pure/const.
|
|
|
|
if (const Decl *callee = getDecl())
|
|
|
|
if (callee->hasAttr<PureAttr>() || callee->hasAttr<ConstAttr>())
|
|
|
|
return Result;
|
|
|
|
|
2013-04-02 09:28:24 +08:00
|
|
|
SmallVector<SVal, 8> ValuesToInvalidate;
|
2013-09-25 07:47:29 +08:00
|
|
|
RegionAndSymbolInvalidationTraits ETraits;
|
2013-04-02 09:28:24 +08:00
|
|
|
|
2015-10-14 06:20:52 +08:00
|
|
|
getExtraInvalidatedValues(ValuesToInvalidate, &ETraits);
|
2012-07-03 03:27:35 +08:00
|
|
|
|
|
|
|
// Indexes of arguments whose values will be preserved by the call.
|
2013-03-21 04:35:53 +08:00
|
|
|
llvm::SmallSet<unsigned, 4> PreserveArgs;
|
2012-07-03 03:27:51 +08:00
|
|
|
if (!argumentsMayEscape())
|
|
|
|
findPtrToConstParams(PreserveArgs, *this);
|
2012-07-03 03:27:35 +08:00
|
|
|
|
|
|
|
for (unsigned Idx = 0, Count = getNumArgs(); Idx != Count; ++Idx) {
|
2013-03-21 04:35:53 +08:00
|
|
|
// Mark this region for invalidation. We batch invalidate regions
|
|
|
|
// below for efficiency.
|
|
|
|
if (PreserveArgs.count(Idx))
|
2013-09-25 07:47:29 +08:00
|
|
|
if (const MemRegion *MR = getArgSVal(Idx).getAsRegion())
|
2016-04-25 22:44:25 +08:00
|
|
|
ETraits.setTrait(MR->getBaseRegion(),
|
2013-09-25 07:47:29 +08:00
|
|
|
RegionAndSymbolInvalidationTraits::TK_PreserveContents);
|
|
|
|
// TODO: Factor this out + handle the lower level const pointers.
|
|
|
|
|
|
|
|
ValuesToInvalidate.push_back(getArgSVal(Idx));
|
2018-08-15 08:33:55 +08:00
|
|
|
|
|
|
|
// If a function accepts an object by argument (which would of course be a
|
|
|
|
// temporary that isn't lifetime-extended), invalidate the object itself,
|
|
|
|
// not only other objects reachable from it. This is necessary because the
|
|
|
|
// destructor has access to the temporary object after the call.
|
|
|
|
// TODO: Support placement arguments once we start
|
|
|
|
// constructing them directly.
|
|
|
|
// TODO: This is unnecessary when there's no destructor, but that's
|
|
|
|
// currently hard to figure out.
|
|
|
|
if (getKind() != CE_CXXAllocator)
|
|
|
|
if (isArgumentConstructedDirectly(Idx))
|
|
|
|
if (auto AdjIdx = getAdjustedParameterIndex(Idx))
|
|
|
|
if (const VarRegion *VR = getParameterLocation(*AdjIdx))
|
|
|
|
ValuesToInvalidate.push_back(loc::MemRegionVal(VR));
|
2012-07-03 03:27:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Invalidate designated regions using the batch invalidation API.
|
|
|
|
// NOTE: Even if RegionsToInvalidate is empty, we may still invalidate
|
|
|
|
// global variables.
|
2013-04-02 09:28:24 +08:00
|
|
|
return Result->invalidateRegions(ValuesToInvalidate, getOriginExpr(),
|
2012-07-19 05:59:41 +08:00
|
|
|
BlockCount, getLocationContext(),
|
2012-12-21 09:50:14 +08:00
|
|
|
/*CausedByPointerEscape*/ true,
|
2014-05-27 10:45:47 +08:00
|
|
|
/*Symbols=*/nullptr, this, &ETraits);
|
2012-07-03 03:27:35 +08:00
|
|
|
}
|
|
|
|
|
2012-07-11 06:07:42 +08:00
|
|
|
ProgramPoint CallEvent::getProgramPoint(bool IsPreVisit,
|
|
|
|
const ProgramPointTag *Tag) const {
|
|
|
|
if (const Expr *E = getOriginExpr()) {
|
|
|
|
if (IsPreVisit)
|
2012-07-19 05:59:41 +08:00
|
|
|
return PreStmt(E, getLocationContext(), Tag);
|
|
|
|
return PostStmt(E, getLocationContext(), Tag);
|
2012-07-11 06:07:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Decl *D = getDecl();
|
2015-09-08 11:50:52 +08:00
|
|
|
assert(D && "Cannot get a program point without a statement or decl");
|
2012-07-11 06:07:42 +08:00
|
|
|
|
|
|
|
SourceLocation Loc = getSourceRange().getBegin();
|
|
|
|
if (IsPreVisit)
|
2012-07-19 05:59:41 +08:00
|
|
|
return PreImplicitCall(D, Loc, getLocationContext(), Tag);
|
|
|
|
return PostImplicitCall(D, Loc, getLocationContext(), Tag);
|
2012-07-11 06:07:42 +08:00
|
|
|
}
|
|
|
|
|
2016-01-23 06:32:46 +08:00
|
|
|
bool CallEvent::isCalled(const CallDescription &CD) const {
|
2017-10-30 16:47:13 +08:00
|
|
|
// FIXME: Add ObjC Message support.
|
|
|
|
if (getKind() == CE_ObjCMessage)
|
|
|
|
return false;
|
2017-02-15 23:35:56 +08:00
|
|
|
if (!CD.IsLookupDone) {
|
|
|
|
CD.IsLookupDone = true;
|
2016-01-23 06:32:46 +08:00
|
|
|
CD.II = &getState()->getStateManager().getContext().Idents.get(CD.FuncName);
|
2017-02-15 23:35:56 +08:00
|
|
|
}
|
|
|
|
const IdentifierInfo *II = getCalleeIdentifier();
|
|
|
|
if (!II || II != CD.II)
|
2016-01-23 06:32:46 +08:00
|
|
|
return false;
|
|
|
|
return (CD.RequiredArgs == CallDescription::NoArgRequirement ||
|
|
|
|
CD.RequiredArgs == getNumArgs());
|
|
|
|
}
|
|
|
|
|
2012-07-27 05:41:15 +08:00
|
|
|
SVal CallEvent::getArgSVal(unsigned Index) const {
|
|
|
|
const Expr *ArgE = getArgExpr(Index);
|
|
|
|
if (!ArgE)
|
|
|
|
return UnknownVal();
|
|
|
|
return getSVal(ArgE);
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceRange CallEvent::getArgSourceRange(unsigned Index) const {
|
|
|
|
const Expr *ArgE = getArgExpr(Index);
|
|
|
|
if (!ArgE)
|
2018-02-28 09:10:04 +08:00
|
|
|
return {};
|
2012-07-27 05:41:15 +08:00
|
|
|
return ArgE->getSourceRange();
|
|
|
|
}
|
|
|
|
|
2012-11-03 07:49:29 +08:00
|
|
|
SVal CallEvent::getReturnValue() const {
|
|
|
|
const Expr *E = getOriginExpr();
|
|
|
|
if (!E)
|
|
|
|
return UndefinedVal();
|
|
|
|
return getSVal(E);
|
|
|
|
}
|
|
|
|
|
2014-01-04 21:47:14 +08:00
|
|
|
LLVM_DUMP_METHOD void CallEvent::dump() const { dump(llvm::errs()); }
|
2012-08-14 22:50:32 +08:00
|
|
|
|
2012-07-27 05:41:15 +08:00
|
|
|
void CallEvent::dump(raw_ostream &Out) const {
|
|
|
|
ASTContext &Ctx = getState()->getStateManager().getContext();
|
|
|
|
if (const Expr *E = getOriginExpr()) {
|
2014-05-27 10:45:47 +08:00
|
|
|
E->printPretty(Out, nullptr, Ctx.getPrintingPolicy());
|
2012-07-27 05:41:15 +08:00
|
|
|
Out << "\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const Decl *D = getDecl()) {
|
|
|
|
Out << "Call to ";
|
|
|
|
D->print(Out, Ctx.getPrintingPolicy());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: a string representation of the kind would be nice.
|
|
|
|
Out << "Unknown call (type " << getKind() << ")";
|
|
|
|
}
|
|
|
|
|
2012-08-28 08:50:38 +08:00
|
|
|
bool CallEvent::isCallStmt(const Stmt *S) {
|
2012-07-27 05:41:15 +08:00
|
|
|
return isa<CallExpr>(S) || isa<ObjCMessageExpr>(S)
|
2012-08-28 08:50:38 +08:00
|
|
|
|| isa<CXXConstructExpr>(S)
|
|
|
|
|| isa<CXXNewExpr>(S);
|
2012-07-03 03:27:51 +08:00
|
|
|
}
|
2012-09-13 06:57:40 +08:00
|
|
|
|
|
|
|
QualType CallEvent::getDeclaredResultType(const Decl *D) {
|
|
|
|
assert(D);
|
2018-02-28 09:10:04 +08:00
|
|
|
if (const auto *FD = dyn_cast<FunctionDecl>(D))
|
2014-01-26 00:55:45 +08:00
|
|
|
return FD->getReturnType();
|
2018-02-28 09:10:04 +08:00
|
|
|
if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
|
2014-01-26 00:55:45 +08:00
|
|
|
return MD->getReturnType();
|
2018-02-28 09:10:04 +08:00
|
|
|
if (const auto *BD = dyn_cast<BlockDecl>(D)) {
|
2013-04-18 02:03:48 +08:00
|
|
|
// Blocks are difficult because the return type may not be stored in the
|
|
|
|
// BlockDecl itself. The AST should probably be enhanced, but for now we
|
|
|
|
// just do what we can.
|
2013-05-31 02:14:27 +08:00
|
|
|
// If the block is declared without an explicit argument list, the
|
|
|
|
// signature-as-written just includes the return type, not the entire
|
|
|
|
// function type.
|
2013-05-23 02:09:44 +08:00
|
|
|
// FIXME: All blocks should have signatures-as-written, even if the return
|
2013-05-31 02:14:27 +08:00
|
|
|
// type is inferred. (That's signified with a dependent result type.)
|
2013-05-23 02:09:44 +08:00
|
|
|
if (const TypeSourceInfo *TSI = BD->getSignatureAsWritten()) {
|
2013-05-31 02:14:27 +08:00
|
|
|
QualType Ty = TSI->getType();
|
|
|
|
if (const FunctionType *FT = Ty->getAs<FunctionType>())
|
2014-01-26 00:55:45 +08:00
|
|
|
Ty = FT->getReturnType();
|
2013-05-31 02:14:27 +08:00
|
|
|
if (!Ty->isDependentType())
|
|
|
|
return Ty;
|
2013-05-23 02:09:44 +08:00
|
|
|
}
|
2013-04-18 02:03:48 +08:00
|
|
|
|
2018-02-28 09:10:04 +08:00
|
|
|
return {};
|
2013-04-18 02:03:48 +08:00
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2013-09-28 10:04:19 +08:00
|
|
|
llvm_unreachable("unknown callable kind");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CallEvent::isVariadic(const Decl *D) {
|
|
|
|
assert(D);
|
|
|
|
|
2018-02-28 09:10:04 +08:00
|
|
|
if (const auto *FD = dyn_cast<FunctionDecl>(D))
|
2013-09-28 10:04:19 +08:00
|
|
|
return FD->isVariadic();
|
2018-02-28 09:10:04 +08:00
|
|
|
if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
|
2013-09-28 10:04:19 +08:00
|
|
|
return MD->isVariadic();
|
2018-02-28 09:10:04 +08:00
|
|
|
if (const auto *BD = dyn_cast<BlockDecl>(D))
|
2013-09-28 10:04:19 +08:00
|
|
|
return BD->isVariadic();
|
|
|
|
|
|
|
|
llvm_unreachable("unknown callable kind");
|
2012-09-13 06:57:40 +08:00
|
|
|
}
|
2012-07-03 03:27:51 +08:00
|
|
|
|
2012-07-31 09:07:55 +08:00
|
|
|
static void addParameterValuesToBindings(const StackFrameContext *CalleeCtx,
|
|
|
|
CallEvent::BindingsTy &Bindings,
|
|
|
|
SValBuilder &SVB,
|
|
|
|
const CallEvent &Call,
|
2014-01-17 15:15:31 +08:00
|
|
|
ArrayRef<ParmVarDecl*> parameters) {
|
2012-07-31 09:07:55 +08:00
|
|
|
MemRegionManager &MRMgr = SVB.getRegionManager();
|
2012-07-03 03:27:51 +08:00
|
|
|
|
2013-06-19 16:19:56 +08:00
|
|
|
// If the function has fewer parameters than the call has arguments, we simply
|
|
|
|
// do not bind any values to them.
|
|
|
|
unsigned NumArgs = Call.getNumArgs();
|
2012-07-31 09:07:55 +08:00
|
|
|
unsigned Idx = 0;
|
2014-01-17 15:15:31 +08:00
|
|
|
ArrayRef<ParmVarDecl*>::iterator I = parameters.begin(), E = parameters.end();
|
2013-06-19 16:19:56 +08:00
|
|
|
for (; I != E && Idx < NumArgs; ++I, ++Idx) {
|
2012-07-31 09:07:55 +08:00
|
|
|
const ParmVarDecl *ParamDecl = *I;
|
|
|
|
assert(ParamDecl && "Formal parameter has no decl?");
|
|
|
|
|
2018-08-15 08:33:55 +08:00
|
|
|
if (Call.getKind() != CE_CXXAllocator)
|
|
|
|
if (Call.isArgumentConstructedDirectly(Idx))
|
|
|
|
continue;
|
|
|
|
|
2012-07-31 09:07:55 +08:00
|
|
|
SVal ArgVal = Call.getArgSVal(Idx);
|
|
|
|
if (!ArgVal.isUnknown()) {
|
|
|
|
Loc ParamLoc = SVB.makeLoc(MRMgr.getVarRegion(ParamDecl, CalleeCtx));
|
|
|
|
Bindings.push_back(std::make_pair(ParamLoc, ArgVal));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Variadic arguments are not handled at all right now.
|
|
|
|
}
|
|
|
|
|
2014-01-17 15:15:31 +08:00
|
|
|
ArrayRef<ParmVarDecl*> AnyFunctionCall::parameters() const {
|
2012-07-31 09:07:55 +08:00
|
|
|
const FunctionDecl *D = getDecl();
|
2012-07-03 03:27:35 +08:00
|
|
|
if (!D)
|
2014-06-29 07:22:23 +08:00
|
|
|
return None;
|
2014-01-17 15:15:31 +08:00
|
|
|
return D->parameters();
|
2012-07-31 09:07:55 +08:00
|
|
|
}
|
|
|
|
|
2017-09-07 05:45:01 +08:00
|
|
|
RuntimeDefinition AnyFunctionCall::getRuntimeDefinition() const {
|
|
|
|
const FunctionDecl *FD = getDecl();
|
2018-04-13 20:36:08 +08:00
|
|
|
if (!FD)
|
|
|
|
return {};
|
|
|
|
|
2017-09-07 05:45:01 +08:00
|
|
|
// Note that the AnalysisDeclContext will have the FunctionDecl with
|
|
|
|
// the definition (if one exists).
|
2018-04-13 20:36:08 +08:00
|
|
|
AnalysisDeclContext *AD =
|
|
|
|
getLocationContext()->getAnalysisDeclContext()->
|
|
|
|
getManager()->getContext(FD);
|
|
|
|
bool IsAutosynthesized;
|
|
|
|
Stmt* Body = AD->getBody(IsAutosynthesized);
|
2018-05-15 21:30:56 +08:00
|
|
|
LLVM_DEBUG({
|
|
|
|
if (IsAutosynthesized)
|
|
|
|
llvm::dbgs() << "Using autosynthesized body for " << FD->getName()
|
|
|
|
<< "\n";
|
2018-04-13 20:36:08 +08:00
|
|
|
});
|
|
|
|
if (Body) {
|
|
|
|
const Decl* Decl = AD->getDecl();
|
|
|
|
return RuntimeDefinition(Decl);
|
2017-09-07 05:45:01 +08:00
|
|
|
}
|
|
|
|
|
2018-03-01 22:54:16 +08:00
|
|
|
SubEngine *Engine = getState()->getStateManager().getOwningEngine();
|
|
|
|
AnalyzerOptions &Opts = Engine->getAnalysisManager().options;
|
|
|
|
|
|
|
|
// Try to get CTU definition only if CTUDir is provided.
|
|
|
|
if (!Opts.naiveCTUEnabled())
|
2018-04-13 20:36:08 +08:00
|
|
|
return {};
|
2018-03-01 22:54:16 +08:00
|
|
|
|
|
|
|
cross_tu::CrossTranslationUnitContext &CTUCtx =
|
|
|
|
*Engine->getCrossTranslationUnitContext();
|
|
|
|
llvm::Expected<const FunctionDecl *> CTUDeclOrError =
|
|
|
|
CTUCtx.getCrossTUDefinition(FD, Opts.getCTUDir(), Opts.getCTUIndexName());
|
|
|
|
|
|
|
|
if (!CTUDeclOrError) {
|
|
|
|
handleAllErrors(CTUDeclOrError.takeError(),
|
|
|
|
[&](const cross_tu::IndexError &IE) {
|
|
|
|
CTUCtx.emitCrossTUDiagnostics(IE);
|
|
|
|
});
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return RuntimeDefinition(*CTUDeclOrError);
|
2017-09-07 05:45:01 +08:00
|
|
|
}
|
|
|
|
|
2012-07-31 09:07:55 +08:00
|
|
|
void AnyFunctionCall::getInitialStackFrameContents(
|
|
|
|
const StackFrameContext *CalleeCtx,
|
|
|
|
BindingsTy &Bindings) const {
|
2018-02-28 09:10:04 +08:00
|
|
|
const auto *D = cast<FunctionDecl>(CalleeCtx->getDecl());
|
2012-07-31 09:07:55 +08:00
|
|
|
SValBuilder &SVB = getState()->getStateManager().getSValBuilder();
|
|
|
|
addParameterValuesToBindings(CalleeCtx, Bindings, SVB, *this,
|
2014-01-17 15:15:31 +08:00
|
|
|
D->parameters());
|
2012-07-03 03:27:35 +08:00
|
|
|
}
|
|
|
|
|
2012-07-03 03:27:51 +08:00
|
|
|
bool AnyFunctionCall::argumentsMayEscape() const {
|
2015-10-28 04:19:45 +08:00
|
|
|
if (CallEvent::argumentsMayEscape() || hasVoidPointerToNonConstArg())
|
2012-07-03 03:27:51 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
const FunctionDecl *D = getDecl();
|
|
|
|
if (!D)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const IdentifierInfo *II = D->getIdentifier();
|
|
|
|
if (!II)
|
2012-10-31 09:18:26 +08:00
|
|
|
return false;
|
2012-07-03 03:27:51 +08:00
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
// This set of "escaping" APIs is
|
2012-07-03 03:27:51 +08:00
|
|
|
|
|
|
|
// - 'int pthread_setspecific(ptheread_key k, const void *)' stores a
|
|
|
|
// value into thread local storage. The value can later be retrieved with
|
|
|
|
// 'void *ptheread_getspecific(pthread_key)'. So even thought the
|
|
|
|
// parameter is 'const void *', the region escapes through the call.
|
|
|
|
if (II->isStr("pthread_setspecific"))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// - xpc_connection_set_context stores a value which can be retrieved later
|
|
|
|
// with xpc_connection_get_context.
|
|
|
|
if (II->isStr("xpc_connection_set_context"))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// - funopen - sets a buffer for future IO calls.
|
|
|
|
if (II->isStr("funopen"))
|
|
|
|
return true;
|
|
|
|
|
2016-12-16 06:55:18 +08:00
|
|
|
// - __cxa_demangle - can reallocate memory and can return the pointer to
|
|
|
|
// the input buffer.
|
|
|
|
if (II->isStr("__cxa_demangle"))
|
|
|
|
return true;
|
|
|
|
|
2012-07-03 03:27:51 +08:00
|
|
|
StringRef FName = II->getName();
|
|
|
|
|
|
|
|
// - CoreFoundation functions that end with "NoCopy" can free a passed-in
|
|
|
|
// buffer even if it is const.
|
|
|
|
if (FName.endswith("NoCopy"))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// - NSXXInsertXX, for example NSMapInsertIfAbsent, since they can
|
|
|
|
// be deallocated by NSMapRemove.
|
|
|
|
if (FName.startswith("NS") && (FName.find("Insert") != StringRef::npos))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// - Many CF containers allow objects to escape through custom
|
|
|
|
// allocators/deallocators upon container construction. (PR12101)
|
|
|
|
if (FName.startswith("CF") || FName.startswith("CG")) {
|
|
|
|
return StrInStrNoCase(FName, "InsertValue") != StringRef::npos ||
|
|
|
|
StrInStrNoCase(FName, "AddValue") != StringRef::npos ||
|
|
|
|
StrInStrNoCase(FName, "SetValue") != StringRef::npos ||
|
|
|
|
StrInStrNoCase(FName, "WithData") != StringRef::npos ||
|
|
|
|
StrInStrNoCase(FName, "AppendValue") != StringRef::npos ||
|
|
|
|
StrInStrNoCase(FName, "SetAttribute") != StringRef::npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-16 01:25:15 +08:00
|
|
|
const FunctionDecl *SimpleFunctionCall::getDecl() const {
|
2012-07-19 05:59:41 +08:00
|
|
|
const FunctionDecl *D = getOriginExpr()->getDirectCallee();
|
2012-07-03 03:27:35 +08:00
|
|
|
if (D)
|
|
|
|
return D;
|
|
|
|
|
2012-07-19 05:59:41 +08:00
|
|
|
return getSVal(getOriginExpr()->getCallee()).getAsFunctionDecl();
|
2012-07-03 03:27:35 +08:00
|
|
|
}
|
|
|
|
|
2012-08-14 07:46:05 +08:00
|
|
|
const FunctionDecl *CXXInstanceCall::getDecl() const {
|
2018-02-28 09:10:04 +08:00
|
|
|
const auto *CE = cast_or_null<CallExpr>(getOriginExpr());
|
2012-08-14 07:46:05 +08:00
|
|
|
if (!CE)
|
|
|
|
return AnyFunctionCall::getDecl();
|
|
|
|
|
|
|
|
const FunctionDecl *D = CE->getDirectCallee();
|
|
|
|
if (D)
|
|
|
|
return D;
|
|
|
|
|
|
|
|
return getSVal(CE->getCallee()).getAsFunctionDecl();
|
|
|
|
}
|
|
|
|
|
2015-11-10 19:48:55 +08:00
|
|
|
void CXXInstanceCall::getExtraInvalidatedValues(
|
|
|
|
ValueList &Values, RegionAndSymbolInvalidationTraits *ETraits) const {
|
2015-10-14 06:20:52 +08:00
|
|
|
SVal ThisVal = getCXXThisVal();
|
|
|
|
Values.push_back(ThisVal);
|
|
|
|
|
2015-10-16 16:54:23 +08:00
|
|
|
// Don't invalidate if the method is const and there are no mutable fields.
|
2018-02-28 09:10:04 +08:00
|
|
|
if (const auto *D = cast_or_null<CXXMethodDecl>(getDecl())) {
|
2015-10-14 06:20:52 +08:00
|
|
|
if (!D->isConst())
|
|
|
|
return;
|
|
|
|
// Get the record decl for the class of 'This'. D->getParent() may return a
|
|
|
|
// base class decl, rather than the class of the instance which needs to be
|
|
|
|
// checked for mutable fields.
|
2018-06-26 07:43:45 +08:00
|
|
|
// TODO: We might as well look at the dynamic type of the object.
|
2015-10-14 06:20:52 +08:00
|
|
|
const Expr *Ex = getCXXThisExpr()->ignoreParenBaseCasts();
|
2018-06-26 07:43:45 +08:00
|
|
|
QualType T = Ex->getType();
|
|
|
|
if (T->isPointerType()) // Arrow or implicit-this syntax?
|
|
|
|
T = T->getPointeeType();
|
|
|
|
const CXXRecordDecl *ParentRecord = T->getAsCXXRecordDecl();
|
|
|
|
assert(ParentRecord);
|
|
|
|
if (ParentRecord->hasMutableFields())
|
2015-10-14 06:20:52 +08:00
|
|
|
return;
|
|
|
|
// Preserve CXXThis.
|
|
|
|
const MemRegion *ThisRegion = ThisVal.getAsRegion();
|
2015-11-10 03:50:29 +08:00
|
|
|
if (!ThisRegion)
|
|
|
|
return;
|
|
|
|
|
2015-10-14 06:20:52 +08:00
|
|
|
ETraits->setTrait(ThisRegion->getBaseRegion(),
|
2015-11-10 19:48:55 +08:00
|
|
|
RegionAndSymbolInvalidationTraits::TK_PreserveContents);
|
2015-10-14 06:20:52 +08:00
|
|
|
}
|
2012-07-12 08:16:25 +08:00
|
|
|
}
|
|
|
|
|
2012-09-06 01:11:26 +08:00
|
|
|
SVal CXXInstanceCall::getCXXThisVal() const {
|
|
|
|
const Expr *Base = getCXXThisExpr();
|
|
|
|
// FIXME: This doesn't handle an overloaded ->* operator.
|
|
|
|
if (!Base)
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
SVal ThisVal = getSVal(Base);
|
2013-02-20 13:52:05 +08:00
|
|
|
assert(ThisVal.isUnknownOrUndef() || ThisVal.getAs<Loc>());
|
2012-09-06 01:11:26 +08:00
|
|
|
return ThisVal;
|
|
|
|
}
|
|
|
|
|
2012-09-21 14:13:13 +08:00
|
|
|
RuntimeDefinition CXXInstanceCall::getRuntimeDefinition() const {
|
2012-08-15 08:51:56 +08:00
|
|
|
// Do we have a decl at all?
|
2012-08-11 06:26:43 +08:00
|
|
|
const Decl *D = getDecl();
|
2012-07-12 08:16:25 +08:00
|
|
|
if (!D)
|
2018-02-28 09:10:04 +08:00
|
|
|
return {};
|
2012-07-12 08:16:25 +08:00
|
|
|
|
2012-08-15 08:51:56 +08:00
|
|
|
// If the method is non-virtual, we know we can inline it.
|
2018-02-28 09:10:04 +08:00
|
|
|
const auto *MD = cast<CXXMethodDecl>(D);
|
2012-07-12 08:16:25 +08:00
|
|
|
if (!MD->isVirtual())
|
2012-09-21 14:13:13 +08:00
|
|
|
return AnyFunctionCall::getRuntimeDefinition();
|
2012-07-12 08:16:25 +08:00
|
|
|
|
2012-08-15 08:51:56 +08:00
|
|
|
// Do we know the implicit 'this' object being called?
|
|
|
|
const MemRegion *R = getCXXThisVal().getAsRegion();
|
|
|
|
if (!R)
|
2018-02-28 09:10:04 +08:00
|
|
|
return {};
|
2012-08-15 08:51:56 +08:00
|
|
|
|
|
|
|
// Do we know anything about the type of 'this'?
|
2015-09-12 01:19:57 +08:00
|
|
|
DynamicTypeInfo DynType = getDynamicTypeInfo(getState(), R);
|
2012-08-15 08:51:56 +08:00
|
|
|
if (!DynType.isValid())
|
2018-02-28 09:10:04 +08:00
|
|
|
return {};
|
2012-08-15 08:51:56 +08:00
|
|
|
|
|
|
|
// Is the type a C++ class? (This is mostly a defensive check.)
|
|
|
|
QualType RegionType = DynType.getType()->getPointeeType();
|
2012-09-06 01:11:22 +08:00
|
|
|
assert(!RegionType.isNull() && "DynamicTypeInfo should always be a pointer.");
|
|
|
|
|
2012-08-15 08:51:56 +08:00
|
|
|
const CXXRecordDecl *RD = RegionType->getAsCXXRecordDecl();
|
2012-08-16 01:33:37 +08:00
|
|
|
if (!RD || !RD->hasDefinition())
|
2018-02-28 09:10:04 +08:00
|
|
|
return {};
|
2012-08-15 08:51:56 +08:00
|
|
|
|
2012-09-13 05:48:13 +08:00
|
|
|
// Find the decl for this method in that class.
|
|
|
|
const CXXMethodDecl *Result = MD->getCorrespondingMethodInClass(RD, true);
|
2012-09-07 09:19:42 +08:00
|
|
|
if (!Result) {
|
|
|
|
// We might not even get the original statically-resolved method due to
|
|
|
|
// some particularly nasty casting (e.g. casts to sister classes).
|
|
|
|
// However, we should at least be able to search up and down our own class
|
|
|
|
// hierarchy, and some real bugs have been caught by checking this.
|
|
|
|
assert(!RD->isDerivedFrom(MD->getParent()) && "Couldn't find known method");
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-09-13 05:48:17 +08:00
|
|
|
// FIXME: This is checking that our DynamicTypeInfo is at least as good as
|
|
|
|
// the static type. However, because we currently don't update
|
|
|
|
// DynamicTypeInfo when an object is cast, we can't actually be sure the
|
|
|
|
// DynamicTypeInfo is up to date. This assert should be re-enabled once
|
|
|
|
// this is fixed. <rdar://problem/12287087>
|
|
|
|
//assert(!MD->getParent()->isDerivedFrom(RD) && "Bad DynamicTypeInfo");
|
|
|
|
|
2018-02-28 09:10:04 +08:00
|
|
|
return {};
|
2012-09-07 09:19:42 +08:00
|
|
|
}
|
2012-08-15 08:51:56 +08:00
|
|
|
|
|
|
|
// Does the decl that we found have an implementation?
|
|
|
|
const FunctionDecl *Definition;
|
|
|
|
if (!Result->hasBody(Definition))
|
2018-02-28 09:10:04 +08:00
|
|
|
return {};
|
2012-07-12 08:16:25 +08:00
|
|
|
|
2012-08-15 08:51:56 +08:00
|
|
|
// We found a definition. If we're not sure that this devirtualization is
|
|
|
|
// actually what will happen at runtime, make sure to provide the region so
|
|
|
|
// that ExprEngine can decide what to do with it.
|
|
|
|
if (DynType.canBeASubClass())
|
|
|
|
return RuntimeDefinition(Definition, R->StripCasts());
|
2014-05-27 10:45:47 +08:00
|
|
|
return RuntimeDefinition(Definition, /*DispatchRegion=*/nullptr);
|
2012-07-12 08:16:25 +08:00
|
|
|
}
|
|
|
|
|
2012-07-31 09:07:55 +08:00
|
|
|
void CXXInstanceCall::getInitialStackFrameContents(
|
|
|
|
const StackFrameContext *CalleeCtx,
|
|
|
|
BindingsTy &Bindings) const {
|
|
|
|
AnyFunctionCall::getInitialStackFrameContents(CalleeCtx, Bindings);
|
|
|
|
|
2012-08-11 06:26:46 +08:00
|
|
|
// Handle the binding of 'this' in the new stack frame.
|
2012-07-31 09:07:55 +08:00
|
|
|
SVal ThisVal = getCXXThisVal();
|
|
|
|
if (!ThisVal.isUnknown()) {
|
2012-08-11 06:26:46 +08:00
|
|
|
ProgramStateManager &StateMgr = getState()->getStateManager();
|
|
|
|
SValBuilder &SVB = StateMgr.getSValBuilder();
|
2012-08-16 01:33:34 +08:00
|
|
|
|
2018-02-28 09:10:04 +08:00
|
|
|
const auto *MD = cast<CXXMethodDecl>(CalleeCtx->getDecl());
|
2012-07-31 09:07:55 +08:00
|
|
|
Loc ThisLoc = SVB.getCXXThis(MD, CalleeCtx);
|
2012-08-11 06:26:46 +08:00
|
|
|
|
2012-08-16 01:33:34 +08:00
|
|
|
// If we devirtualized to a different member function, we need to make sure
|
|
|
|
// we have the proper layering of CXXBaseObjectRegions.
|
|
|
|
if (MD->getCanonicalDecl() != getDecl()->getCanonicalDecl()) {
|
2012-08-14 07:46:01 +08:00
|
|
|
ASTContext &Ctx = SVB.getContext();
|
2012-08-11 06:26:46 +08:00
|
|
|
const CXXRecordDecl *Class = MD->getParent();
|
2012-08-14 07:46:01 +08:00
|
|
|
QualType Ty = Ctx.getPointerType(Ctx.getRecordType(Class));
|
2012-08-11 06:26:46 +08:00
|
|
|
|
2012-08-14 07:46:01 +08:00
|
|
|
// FIXME: CallEvent maybe shouldn't be directly accessing StoreManager.
|
|
|
|
bool Failed;
|
2016-08-08 17:22:59 +08:00
|
|
|
ThisVal = StateMgr.getStoreManager().attemptDownCast(ThisVal, Ty, Failed);
|
2018-02-28 04:54:40 +08:00
|
|
|
if (Failed) {
|
|
|
|
// We might have suffered some sort of placement new earlier, so
|
|
|
|
// we're constructing in a completely unexpected storage.
|
|
|
|
// Fall back to a generic pointer cast for this-value.
|
|
|
|
const CXXMethodDecl *StaticMD = cast<CXXMethodDecl>(getDecl());
|
|
|
|
const CXXRecordDecl *StaticClass = StaticMD->getParent();
|
|
|
|
QualType StaticTy = Ctx.getPointerType(Ctx.getRecordType(StaticClass));
|
|
|
|
ThisVal = SVB.evalCast(ThisVal, Ty, StaticTy);
|
|
|
|
}
|
2012-08-11 06:26:46 +08:00
|
|
|
}
|
|
|
|
|
2012-08-16 01:33:34 +08:00
|
|
|
if (!ThisVal.isUnknown())
|
|
|
|
Bindings.push_back(std::make_pair(ThisLoc, ThisVal));
|
2012-07-31 09:07:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-04 07:08:49 +08:00
|
|
|
const Expr *CXXMemberCall::getCXXThisExpr() const {
|
|
|
|
return getOriginExpr()->getImplicitObjectArgument();
|
2012-07-11 06:07:57 +08:00
|
|
|
}
|
|
|
|
|
2012-09-21 14:13:13 +08:00
|
|
|
RuntimeDefinition CXXMemberCall::getRuntimeDefinition() const {
|
2012-09-11 08:31:02 +08:00
|
|
|
// C++11 [expr.call]p1: ...If the selected function is non-virtual, or if the
|
|
|
|
// id-expression in the class member access expression is a qualified-id,
|
|
|
|
// that function is called. Otherwise, its final overrider in the dynamic type
|
|
|
|
// of the object expression is called.
|
2018-02-28 09:10:04 +08:00
|
|
|
if (const auto *ME = dyn_cast<MemberExpr>(getOriginExpr()->getCallee()))
|
2012-09-11 08:31:02 +08:00
|
|
|
if (ME->hasQualifier())
|
2012-09-21 14:13:13 +08:00
|
|
|
return AnyFunctionCall::getRuntimeDefinition();
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-09-21 14:13:13 +08:00
|
|
|
return CXXInstanceCall::getRuntimeDefinition();
|
2012-09-11 08:31:02 +08:00
|
|
|
}
|
|
|
|
|
2012-08-04 07:08:49 +08:00
|
|
|
const Expr *CXXMemberOperatorCall::getCXXThisExpr() const {
|
|
|
|
return getOriginExpr()->getArg(0);
|
2012-07-11 06:07:57 +08:00
|
|
|
}
|
|
|
|
|
2012-07-03 03:27:35 +08:00
|
|
|
const BlockDataRegion *BlockCall::getBlockRegion() const {
|
|
|
|
const Expr *Callee = getOriginExpr()->getCallee();
|
|
|
|
const MemRegion *DataReg = getSVal(Callee).getAsRegion();
|
|
|
|
|
2012-07-03 03:28:09 +08:00
|
|
|
return dyn_cast_or_null<BlockDataRegion>(DataReg);
|
2012-07-03 03:27:35 +08:00
|
|
|
}
|
|
|
|
|
2014-01-17 15:15:31 +08:00
|
|
|
ArrayRef<ParmVarDecl*> BlockCall::parameters() const {
|
2014-01-16 01:25:15 +08:00
|
|
|
const BlockDecl *D = getDecl();
|
2012-07-03 03:28:09 +08:00
|
|
|
if (!D)
|
2014-05-27 10:45:47 +08:00
|
|
|
return nullptr;
|
2014-01-17 15:15:31 +08:00
|
|
|
return D->parameters();
|
2012-07-03 03:27:35 +08:00
|
|
|
}
|
|
|
|
|
2015-10-14 06:20:52 +08:00
|
|
|
void BlockCall::getExtraInvalidatedValues(ValueList &Values,
|
|
|
|
RegionAndSymbolInvalidationTraits *ETraits) const {
|
2012-07-03 03:28:09 +08:00
|
|
|
// FIXME: This also needs to invalidate captured globals.
|
|
|
|
if (const MemRegion *R = getBlockRegion())
|
2013-04-02 09:28:24 +08:00
|
|
|
Values.push_back(loc::MemRegionVal(R));
|
2012-07-03 03:27:35 +08:00
|
|
|
}
|
|
|
|
|
2012-07-31 09:07:55 +08:00
|
|
|
void BlockCall::getInitialStackFrameContents(const StackFrameContext *CalleeCtx,
|
|
|
|
BindingsTy &Bindings) const {
|
|
|
|
SValBuilder &SVB = getState()->getStateManager().getSValBuilder();
|
2015-12-04 13:00:36 +08:00
|
|
|
ArrayRef<ParmVarDecl*> Params;
|
|
|
|
if (isConversionFromLambda()) {
|
|
|
|
auto *LambdaOperatorDecl = cast<CXXMethodDecl>(CalleeCtx->getDecl());
|
|
|
|
Params = LambdaOperatorDecl->parameters();
|
|
|
|
|
|
|
|
// For blocks converted from a C++ lambda, the callee declaration is the
|
2015-12-10 06:16:07 +08:00
|
|
|
// operator() method on the lambda so we bind "this" to
|
2015-12-04 13:00:36 +08:00
|
|
|
// the lambda captured by the block.
|
|
|
|
const VarRegion *CapturedLambdaRegion = getRegionStoringCapturedLambda();
|
|
|
|
SVal ThisVal = loc::MemRegionVal(CapturedLambdaRegion);
|
|
|
|
Loc ThisLoc = SVB.getCXXThis(LambdaOperatorDecl, CalleeCtx);
|
|
|
|
Bindings.push_back(std::make_pair(ThisLoc, ThisVal));
|
|
|
|
} else {
|
|
|
|
Params = cast<BlockDecl>(CalleeCtx->getDecl())->parameters();
|
|
|
|
}
|
|
|
|
|
2012-07-31 09:07:55 +08:00
|
|
|
addParameterValuesToBindings(CalleeCtx, Bindings, SVB, *this,
|
2015-12-04 13:00:36 +08:00
|
|
|
Params);
|
2012-07-31 09:07:55 +08:00
|
|
|
}
|
|
|
|
|
2012-07-11 06:07:57 +08:00
|
|
|
SVal CXXConstructorCall::getCXXThisVal() const {
|
2012-07-19 05:59:41 +08:00
|
|
|
if (Data)
|
|
|
|
return loc::MemRegionVal(static_cast<const MemRegion *>(Data));
|
2012-07-11 06:07:57 +08:00
|
|
|
return UnknownVal();
|
|
|
|
}
|
|
|
|
|
2015-10-14 06:20:52 +08:00
|
|
|
void CXXConstructorCall::getExtraInvalidatedValues(ValueList &Values,
|
|
|
|
RegionAndSymbolInvalidationTraits *ETraits) const {
|
[analyzer] Suppress "this" pointer escape during construction.
Pointer escape event notifies checkers that a pointer can no longer be reliably
tracked by the analyzer. For example, if a pointer is passed into a function
that has no body available, or written into a global, MallocChecker would
no longer report memory leaks for such pointer.
In case of operator new() under -analyzer-config c++-allocator-inlining=true,
MallocChecker would start tracking the pointer allocated by operator new()
only to immediately meet a pointer escape event notifying the checker that the
pointer has escaped into a constructor (assuming that the body of the
constructor is not available) and immediately stop tracking it. Even though
it is theoretically possible for such constructor to put "this" into
a global container that would later be freed, we prefer to preserve the old
behavior of MallocChecker, i.e. a memory leak warning, in order to
be able to find any memory leaks in C++ at all. In fact, c++-allocator-inlining
*reduces* the amount of false positives coming from this-pointers escaping in
constructors, because it'd be able to inline constructors in some cases.
With other checkers working similarly, we simply suppress the escape event for
this-value of the constructor, regardless of analyzer options.
Differential Revision: https://reviews.llvm.org/D41797
rdar://problem/12180598
llvm-svn: 322795
2018-01-18 08:44:41 +08:00
|
|
|
if (Data) {
|
|
|
|
loc::MemRegionVal MV(static_cast<const MemRegion *>(Data));
|
|
|
|
if (SymbolRef Sym = MV.getAsSymbol(true))
|
|
|
|
ETraits->setTrait(Sym,
|
|
|
|
RegionAndSymbolInvalidationTraits::TK_SuppressEscape);
|
|
|
|
Values.push_back(MV);
|
|
|
|
}
|
2012-07-03 03:27:35 +08:00
|
|
|
}
|
|
|
|
|
2012-07-31 09:07:55 +08:00
|
|
|
void CXXConstructorCall::getInitialStackFrameContents(
|
|
|
|
const StackFrameContext *CalleeCtx,
|
|
|
|
BindingsTy &Bindings) const {
|
|
|
|
AnyFunctionCall::getInitialStackFrameContents(CalleeCtx, Bindings);
|
|
|
|
|
|
|
|
SVal ThisVal = getCXXThisVal();
|
|
|
|
if (!ThisVal.isUnknown()) {
|
|
|
|
SValBuilder &SVB = getState()->getStateManager().getSValBuilder();
|
2018-02-28 09:10:04 +08:00
|
|
|
const auto *MD = cast<CXXMethodDecl>(CalleeCtx->getDecl());
|
2012-07-31 09:07:55 +08:00
|
|
|
Loc ThisLoc = SVB.getCXXThis(MD, CalleeCtx);
|
|
|
|
Bindings.push_back(std::make_pair(ThisLoc, ThisVal));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-11 06:07:57 +08:00
|
|
|
SVal CXXDestructorCall::getCXXThisVal() const {
|
2012-07-19 05:59:41 +08:00
|
|
|
if (Data)
|
2012-09-07 04:37:08 +08:00
|
|
|
return loc::MemRegionVal(DtorDataTy::getFromOpaqueValue(Data).getPointer());
|
2012-07-11 06:07:57 +08:00
|
|
|
return UnknownVal();
|
|
|
|
}
|
|
|
|
|
2012-09-21 14:13:13 +08:00
|
|
|
RuntimeDefinition CXXDestructorCall::getRuntimeDefinition() const {
|
2012-09-07 04:37:08 +08:00
|
|
|
// Base destructors are always called non-virtually.
|
|
|
|
// Skip CXXInstanceCall's devirtualization logic in this case.
|
|
|
|
if (isBaseDestructor())
|
2012-09-21 14:13:13 +08:00
|
|
|
return AnyFunctionCall::getRuntimeDefinition();
|
2012-09-07 04:37:08 +08:00
|
|
|
|
2012-09-21 14:13:13 +08:00
|
|
|
return CXXInstanceCall::getRuntimeDefinition();
|
2012-09-07 04:37:08 +08:00
|
|
|
}
|
|
|
|
|
2014-01-17 15:15:31 +08:00
|
|
|
ArrayRef<ParmVarDecl*> ObjCMethodCall::parameters() const {
|
2012-07-31 09:07:55 +08:00
|
|
|
const ObjCMethodDecl *D = getDecl();
|
2012-07-03 03:27:35 +08:00
|
|
|
if (!D)
|
2014-08-27 14:28:36 +08:00
|
|
|
return None;
|
2014-01-17 15:15:31 +08:00
|
|
|
return D->parameters();
|
2012-07-03 03:27:35 +08:00
|
|
|
}
|
|
|
|
|
2016-02-19 03:13:30 +08:00
|
|
|
void ObjCMethodCall::getExtraInvalidatedValues(
|
|
|
|
ValueList &Values, RegionAndSymbolInvalidationTraits *ETraits) const {
|
|
|
|
|
|
|
|
// If the method call is a setter for property known to be backed by
|
|
|
|
// an instance variable, don't invalidate the entire receiver, just
|
|
|
|
// the storage for that instance variable.
|
|
|
|
if (const ObjCPropertyDecl *PropDecl = getAccessedProperty()) {
|
|
|
|
if (const ObjCIvarDecl *PropIvar = PropDecl->getPropertyIvarDecl()) {
|
|
|
|
SVal IvarLVal = getState()->getLValue(PropIvar, getReceiverSVal());
|
2017-04-13 06:00:13 +08:00
|
|
|
if (const MemRegion *IvarRegion = IvarLVal.getAsRegion()) {
|
|
|
|
ETraits->setTrait(
|
2016-02-19 03:13:30 +08:00
|
|
|
IvarRegion,
|
|
|
|
RegionAndSymbolInvalidationTraits::TK_DoNotInvalidateSuperRegion);
|
2017-04-13 06:00:13 +08:00
|
|
|
ETraits->setTrait(
|
|
|
|
IvarRegion,
|
|
|
|
RegionAndSymbolInvalidationTraits::TK_SuppressEscape);
|
|
|
|
Values.push_back(IvarLVal);
|
|
|
|
}
|
2016-02-19 03:13:30 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-02 09:28:24 +08:00
|
|
|
Values.push_back(getReceiverSVal());
|
2012-07-03 03:27:35 +08:00
|
|
|
}
|
|
|
|
|
2012-08-24 08:06:12 +08:00
|
|
|
SVal ObjCMethodCall::getSelfSVal() const {
|
|
|
|
const LocationContext *LCtx = getLocationContext();
|
|
|
|
const ImplicitParamDecl *SelfDecl = LCtx->getSelfDecl();
|
|
|
|
if (!SelfDecl)
|
|
|
|
return SVal();
|
|
|
|
return getState()->getSVal(getState()->getRegion(SelfDecl, LCtx));
|
|
|
|
}
|
|
|
|
|
2012-07-03 03:27:56 +08:00
|
|
|
SVal ObjCMethodCall::getReceiverSVal() const {
|
2012-07-03 03:27:35 +08:00
|
|
|
// FIXME: Is this the best way to handle class receivers?
|
|
|
|
if (!isInstanceMessage())
|
|
|
|
return UnknownVal();
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-08-07 07:25:39 +08:00
|
|
|
if (const Expr *RecE = getOriginExpr()->getInstanceReceiver())
|
|
|
|
return getSVal(RecE);
|
2012-07-03 03:27:35 +08:00
|
|
|
|
|
|
|
// An instance message with no expression means we are sending to super.
|
|
|
|
// In this case the object reference is the same as 'self'.
|
2012-08-24 08:06:12 +08:00
|
|
|
assert(getOriginExpr()->getReceiverKind() == ObjCMessageExpr::SuperInstance);
|
|
|
|
SVal SelfVal = getSelfSVal();
|
|
|
|
assert(SelfVal.isValid() && "Calling super but not in ObjC method");
|
|
|
|
return SelfVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ObjCMethodCall::isReceiverSelfOrSuper() const {
|
|
|
|
if (getOriginExpr()->getReceiverKind() == ObjCMessageExpr::SuperInstance ||
|
|
|
|
getOriginExpr()->getReceiverKind() == ObjCMessageExpr::SuperClass)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!isInstanceMessage())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SVal RecVal = getSVal(getOriginExpr()->getInstanceReceiver());
|
|
|
|
|
|
|
|
return (RecVal == getSelfSVal());
|
2012-07-19 05:59:41 +08:00
|
|
|
}
|
|
|
|
|
2012-07-19 05:59:51 +08:00
|
|
|
SourceRange ObjCMethodCall::getSourceRange() const {
|
|
|
|
switch (getMessageKind()) {
|
|
|
|
case OCM_Message:
|
|
|
|
return getOriginExpr()->getSourceRange();
|
|
|
|
case OCM_PropertyAccess:
|
|
|
|
case OCM_Subscript:
|
|
|
|
return getContainingPseudoObjectExpr()->getSourceRange();
|
|
|
|
}
|
2012-07-19 11:08:07 +08:00
|
|
|
llvm_unreachable("unknown message kind");
|
2012-07-19 05:59:51 +08:00
|
|
|
}
|
|
|
|
|
2018-02-28 09:10:04 +08:00
|
|
|
using ObjCMessageDataTy = llvm::PointerIntPair<const PseudoObjectExpr *, 2>;
|
2012-07-19 05:59:51 +08:00
|
|
|
|
|
|
|
const PseudoObjectExpr *ObjCMethodCall::getContainingPseudoObjectExpr() const {
|
2014-05-27 10:45:47 +08:00
|
|
|
assert(Data && "Lazy lookup not yet performed.");
|
2012-07-19 05:59:51 +08:00
|
|
|
assert(getMessageKind() != OCM_Message && "Explicit message send.");
|
|
|
|
return ObjCMessageDataTy::getFromOpaqueValue(Data).getPointer();
|
|
|
|
}
|
|
|
|
|
2016-02-19 03:13:30 +08:00
|
|
|
static const Expr *
|
|
|
|
getSyntacticFromForPseudoObjectExpr(const PseudoObjectExpr *POE) {
|
|
|
|
const Expr *Syntactic = POE->getSyntacticForm();
|
|
|
|
|
|
|
|
// This handles the funny case of assigning to the result of a getter.
|
|
|
|
// This can happen if the getter returns a non-const reference.
|
2018-02-28 09:10:04 +08:00
|
|
|
if (const auto *BO = dyn_cast<BinaryOperator>(Syntactic))
|
2016-02-19 03:13:30 +08:00
|
|
|
Syntactic = BO->getLHS();
|
|
|
|
|
|
|
|
return Syntactic;
|
|
|
|
}
|
|
|
|
|
2012-07-19 05:59:51 +08:00
|
|
|
ObjCMessageKind ObjCMethodCall::getMessageKind() const {
|
2014-05-27 10:45:47 +08:00
|
|
|
if (!Data) {
|
2013-09-17 09:30:57 +08:00
|
|
|
// Find the parent, ignoring implicit casts.
|
2012-07-19 05:59:51 +08:00
|
|
|
ParentMap &PM = getLocationContext()->getParentMap();
|
2013-09-21 00:51:50 +08:00
|
|
|
const Stmt *S = PM.getParentIgnoreParenCasts(getOriginExpr());
|
2013-09-17 09:30:57 +08:00
|
|
|
|
|
|
|
// Check if parent is a PseudoObjectExpr.
|
2018-02-28 09:10:04 +08:00
|
|
|
if (const auto *POE = dyn_cast_or_null<PseudoObjectExpr>(S)) {
|
2016-02-19 03:13:30 +08:00
|
|
|
const Expr *Syntactic = getSyntacticFromForPseudoObjectExpr(POE);
|
2012-07-19 05:59:51 +08:00
|
|
|
|
|
|
|
ObjCMessageKind K;
|
|
|
|
switch (Syntactic->getStmtClass()) {
|
|
|
|
case Stmt::ObjCPropertyRefExprClass:
|
|
|
|
K = OCM_PropertyAccess;
|
|
|
|
break;
|
|
|
|
case Stmt::ObjCSubscriptRefExprClass:
|
|
|
|
K = OCM_Subscript;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// FIXME: Can this ever happen?
|
|
|
|
K = OCM_Message;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (K != OCM_Message) {
|
|
|
|
const_cast<ObjCMethodCall *>(this)->Data
|
|
|
|
= ObjCMessageDataTy(POE, K).getOpaqueValue();
|
|
|
|
assert(getMessageKind() == K);
|
|
|
|
return K;
|
|
|
|
}
|
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2012-07-19 05:59:51 +08:00
|
|
|
const_cast<ObjCMethodCall *>(this)->Data
|
2014-05-27 10:45:47 +08:00
|
|
|
= ObjCMessageDataTy(nullptr, 1).getOpaqueValue();
|
2012-07-19 05:59:51 +08:00
|
|
|
assert(getMessageKind() == OCM_Message);
|
|
|
|
return OCM_Message;
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjCMessageDataTy Info = ObjCMessageDataTy::getFromOpaqueValue(Data);
|
|
|
|
if (!Info.getPointer())
|
|
|
|
return OCM_Message;
|
|
|
|
return static_cast<ObjCMessageKind>(Info.getInt());
|
2012-07-03 03:27:35 +08:00
|
|
|
}
|
2012-07-26 08:27:51 +08:00
|
|
|
|
2016-02-19 03:13:30 +08:00
|
|
|
const ObjCPropertyDecl *ObjCMethodCall::getAccessedProperty() const {
|
|
|
|
// Look for properties accessed with property syntax (foo.bar = ...)
|
|
|
|
if ( getMessageKind() == OCM_PropertyAccess) {
|
|
|
|
const PseudoObjectExpr *POE = getContainingPseudoObjectExpr();
|
|
|
|
assert(POE && "Property access without PseudoObjectExpr?");
|
|
|
|
|
|
|
|
const Expr *Syntactic = getSyntacticFromForPseudoObjectExpr(POE);
|
|
|
|
auto *RefExpr = cast<ObjCPropertyRefExpr>(Syntactic);
|
|
|
|
|
|
|
|
if (RefExpr->isExplicitProperty())
|
|
|
|
return RefExpr->getExplicitProperty();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for properties accessed with method syntax ([foo setBar:...]).
|
|
|
|
const ObjCMethodDecl *MD = getDecl();
|
|
|
|
if (!MD || !MD->isPropertyAccessor())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Note: This is potentially quite slow.
|
|
|
|
return MD->findPropertyDecl();
|
|
|
|
}
|
2012-08-11 02:55:53 +08:00
|
|
|
|
|
|
|
bool ObjCMethodCall::canBeOverridenInSubclass(ObjCInterfaceDecl *IDecl,
|
|
|
|
Selector Sel) const {
|
|
|
|
assert(IDecl);
|
2018-04-26 05:51:26 +08:00
|
|
|
AnalysisManager &AMgr =
|
|
|
|
getState()->getStateManager().getOwningEngine()->getAnalysisManager();
|
2012-08-11 02:55:53 +08:00
|
|
|
// If the class interface is declared inside the main file, assume it is not
|
2015-09-08 11:50:52 +08:00
|
|
|
// subcassed.
|
2012-08-11 02:55:53 +08:00
|
|
|
// TODO: It could actually be subclassed if the subclass is private as well.
|
|
|
|
// This is probably very rare.
|
|
|
|
SourceLocation InterfLoc = IDecl->getEndOfDefinitionLoc();
|
2018-04-26 05:51:26 +08:00
|
|
|
if (InterfLoc.isValid() && AMgr.isInCodeFile(InterfLoc))
|
2012-08-11 02:55:53 +08:00
|
|
|
return false;
|
|
|
|
|
2012-08-15 03:19:18 +08:00
|
|
|
// Assume that property accessors are not overridden.
|
|
|
|
if (getMessageKind() == OCM_PropertyAccess)
|
|
|
|
return false;
|
2012-08-11 02:55:53 +08:00
|
|
|
|
|
|
|
// We assume that if the method is public (declared outside of main file) or
|
|
|
|
// has a parent which publicly declares the method, the method could be
|
|
|
|
// overridden in a subclass.
|
|
|
|
|
|
|
|
// Find the first declaration in the class hierarchy that declares
|
|
|
|
// the selector.
|
2014-05-27 10:45:47 +08:00
|
|
|
ObjCMethodDecl *D = nullptr;
|
2012-08-11 02:55:53 +08:00
|
|
|
while (true) {
|
|
|
|
D = IDecl->lookupMethod(Sel, true);
|
|
|
|
|
|
|
|
// Cannot find a public definition.
|
|
|
|
if (!D)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If outside the main file,
|
2018-04-26 05:51:26 +08:00
|
|
|
if (D->getLocation().isValid() && !AMgr.isInCodeFile(D->getLocation()))
|
2012-08-11 02:55:53 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if (D->isOverriding()) {
|
|
|
|
// Search in the superclass on the next iteration.
|
|
|
|
IDecl = D->getClassInterface();
|
|
|
|
if (!IDecl)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
IDecl = IDecl->getSuperClass();
|
|
|
|
if (!IDecl)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
llvm_unreachable("The while loop should always terminate.");
|
|
|
|
}
|
|
|
|
|
2017-01-13 08:50:47 +08:00
|
|
|
static const ObjCMethodDecl *findDefiningRedecl(const ObjCMethodDecl *MD) {
|
|
|
|
if (!MD)
|
|
|
|
return MD;
|
|
|
|
|
|
|
|
// Find the redeclaration that defines the method.
|
|
|
|
if (!MD->hasBody()) {
|
|
|
|
for (auto I : MD->redecls())
|
|
|
|
if (I->hasBody())
|
|
|
|
MD = cast<ObjCMethodDecl>(I);
|
|
|
|
}
|
|
|
|
return MD;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isCallToSelfClass(const ObjCMessageExpr *ME) {
|
|
|
|
const Expr* InstRec = ME->getInstanceReceiver();
|
|
|
|
if (!InstRec)
|
|
|
|
return false;
|
|
|
|
const auto *InstRecIg = dyn_cast<DeclRefExpr>(InstRec->IgnoreParenImpCasts());
|
|
|
|
|
|
|
|
// Check that receiver is called 'self'.
|
|
|
|
if (!InstRecIg || !InstRecIg->getFoundDecl() ||
|
|
|
|
!InstRecIg->getFoundDecl()->getName().equals("self"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check that the method name is 'class'.
|
|
|
|
if (ME->getSelector().getNumArgs() != 0 ||
|
|
|
|
!ME->getSelector().getNameForSlot(0).equals("class"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-21 14:13:13 +08:00
|
|
|
RuntimeDefinition ObjCMethodCall::getRuntimeDefinition() const {
|
2012-07-31 04:31:29 +08:00
|
|
|
const ObjCMessageExpr *E = getOriginExpr();
|
|
|
|
assert(E);
|
2012-07-31 07:48:36 +08:00
|
|
|
Selector Sel = E->getSelector();
|
2012-07-31 04:31:29 +08:00
|
|
|
|
|
|
|
if (E->isInstanceMessage()) {
|
2015-06-19 09:52:53 +08:00
|
|
|
// Find the receiver type.
|
2014-05-27 10:45:47 +08:00
|
|
|
const ObjCObjectPointerType *ReceiverT = nullptr;
|
2012-08-11 02:55:58 +08:00
|
|
|
bool CanBeSubClassed = false;
|
2012-07-31 07:48:36 +08:00
|
|
|
QualType SupersType = E->getSuperType();
|
2014-05-27 10:45:47 +08:00
|
|
|
const MemRegion *Receiver = nullptr;
|
2012-08-09 08:21:33 +08:00
|
|
|
|
2012-07-31 07:48:36 +08:00
|
|
|
if (!SupersType.isNull()) {
|
2017-01-13 08:50:47 +08:00
|
|
|
// The receiver is guaranteed to be 'super' in this case.
|
2012-08-09 08:21:33 +08:00
|
|
|
// Super always means the type of immediate predecessor to the method
|
|
|
|
// where the call occurs.
|
2012-08-07 13:12:24 +08:00
|
|
|
ReceiverT = cast<ObjCObjectPointerType>(SupersType);
|
2012-07-31 07:48:36 +08:00
|
|
|
} else {
|
2012-08-09 08:21:33 +08:00
|
|
|
Receiver = getReceiverSVal().getAsRegion();
|
2012-08-01 02:04:53 +08:00
|
|
|
if (!Receiver)
|
2018-02-28 09:10:04 +08:00
|
|
|
return {};
|
2012-08-01 02:04:53 +08:00
|
|
|
|
2015-09-12 01:19:57 +08:00
|
|
|
DynamicTypeInfo DTI = getDynamicTypeInfo(getState(), Receiver);
|
2017-06-13 01:59:50 +08:00
|
|
|
if (!DTI.isValid()) {
|
|
|
|
assert(isa<AllocaRegion>(Receiver) &&
|
|
|
|
"Unhandled untyped region class!");
|
2018-02-28 09:10:04 +08:00
|
|
|
return {};
|
2017-06-13 01:59:50 +08:00
|
|
|
}
|
|
|
|
|
2012-08-11 02:55:58 +08:00
|
|
|
QualType DynType = DTI.getType();
|
|
|
|
CanBeSubClassed = DTI.canBeASubClass();
|
2017-01-13 08:50:47 +08:00
|
|
|
ReceiverT = dyn_cast<ObjCObjectPointerType>(DynType.getCanonicalType());
|
2012-08-11 02:55:58 +08:00
|
|
|
|
|
|
|
if (ReceiverT && CanBeSubClassed)
|
|
|
|
if (ObjCInterfaceDecl *IDecl = ReceiverT->getInterfaceDecl())
|
|
|
|
if (!canBeOverridenInSubclass(IDecl, Sel))
|
|
|
|
CanBeSubClassed = false;
|
2012-07-26 08:27:51 +08:00
|
|
|
}
|
|
|
|
|
2017-01-13 08:50:47 +08:00
|
|
|
// Handle special cases of '[self classMethod]' and
|
|
|
|
// '[[self class] classMethod]', which are treated by the compiler as
|
|
|
|
// instance (not class) messages. We will statically dispatch to those.
|
|
|
|
if (auto *PT = dyn_cast_or_null<ObjCObjectPointerType>(ReceiverT)) {
|
|
|
|
// For [self classMethod], return the compiler visible declaration.
|
|
|
|
if (PT->getObjectType()->isObjCClass() &&
|
|
|
|
Receiver == getSelfSVal().getAsRegion())
|
|
|
|
return RuntimeDefinition(findDefiningRedecl(E->getMethodDecl()));
|
|
|
|
|
|
|
|
// Similarly, handle [[self class] classMethod].
|
|
|
|
// TODO: We are currently doing a syntactic match for this pattern with is
|
|
|
|
// limiting as the test cases in Analysis/inlining/InlineObjCClassMethod.m
|
|
|
|
// shows. A better way would be to associate the meta type with the symbol
|
|
|
|
// using the dynamic type info tracking and use it here. We can add a new
|
|
|
|
// SVal for ObjC 'Class' values that know what interface declaration they
|
|
|
|
// come from. Then 'self' in a class method would be filled in with
|
|
|
|
// something meaningful in ObjCMethodCall::getReceiverSVal() and we could
|
|
|
|
// do proper dynamic dispatch for class methods just like we do for
|
|
|
|
// instance methods now.
|
|
|
|
if (E->getInstanceReceiver())
|
|
|
|
if (const auto *M = dyn_cast<ObjCMessageExpr>(E->getInstanceReceiver()))
|
|
|
|
if (isCallToSelfClass(M))
|
|
|
|
return RuntimeDefinition(findDefiningRedecl(E->getMethodDecl()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup the instance method implementation.
|
2012-07-31 07:48:36 +08:00
|
|
|
if (ReceiverT)
|
2012-08-11 02:55:53 +08:00
|
|
|
if (ObjCInterfaceDecl *IDecl = ReceiverT->getInterfaceDecl()) {
|
2012-12-07 15:30:19 +08:00
|
|
|
// Repeatedly calling lookupPrivateMethod() is expensive, especially
|
|
|
|
// when in many cases it returns null. We cache the results so
|
|
|
|
// that repeated queries on the same ObjCIntefaceDecl and Selector
|
|
|
|
// don't incur the same cost. On some test cases, we can see the
|
|
|
|
// same query being issued thousands of times.
|
|
|
|
//
|
|
|
|
// NOTE: This cache is essentially a "global" variable, but it
|
|
|
|
// only gets lazily created when we get here. The value of the
|
|
|
|
// cache probably comes from it being global across ExprEngines,
|
|
|
|
// where the same queries may get issued. If we are worried about
|
|
|
|
// concurrency, or possibly loading/unloading ASTs, etc., we may
|
|
|
|
// need to revisit this someday. In terms of memory, this table
|
|
|
|
// stays around until clang quits, which also may be bad if we
|
|
|
|
// need to release memory.
|
2018-02-28 09:10:04 +08:00
|
|
|
using PrivateMethodKey = std::pair<const ObjCInterfaceDecl *, Selector>;
|
|
|
|
using PrivateMethodCache =
|
|
|
|
llvm::DenseMap<PrivateMethodKey, Optional<const ObjCMethodDecl *>>;
|
2012-12-07 15:30:19 +08:00
|
|
|
|
|
|
|
static PrivateMethodCache PMC;
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<const ObjCMethodDecl *> &Val = PMC[std::make_pair(IDecl, Sel)];
|
2012-12-07 15:30:19 +08:00
|
|
|
|
|
|
|
// Query lookupPrivateMethod() if the cache does not hit.
|
2014-01-11 04:06:06 +08:00
|
|
|
if (!Val.hasValue()) {
|
2012-12-07 15:30:19 +08:00
|
|
|
Val = IDecl->lookupPrivateMethod(Sel);
|
|
|
|
|
2014-01-11 04:06:06 +08:00
|
|
|
// If the method is a property accessor, we should try to "inline" it
|
|
|
|
// even if we don't actually have an implementation.
|
|
|
|
if (!*Val)
|
|
|
|
if (const ObjCMethodDecl *CompileTimeMD = E->getMethodDecl())
|
2016-04-01 11:24:13 +08:00
|
|
|
if (CompileTimeMD->isPropertyAccessor()) {
|
|
|
|
if (!CompileTimeMD->getSelfDecl() &&
|
|
|
|
isa<ObjCCategoryDecl>(CompileTimeMD->getDeclContext())) {
|
|
|
|
// If the method is an accessor in a category, and it doesn't
|
|
|
|
// have a self declaration, first
|
|
|
|
// try to find the method in a class extension. This
|
|
|
|
// works around a bug in Sema where multiple accessors
|
|
|
|
// are synthesized for properties in class
|
|
|
|
// extensions that are redeclared in a category and the
|
|
|
|
// the implicit parameters are not filled in for
|
|
|
|
// the method on the category.
|
|
|
|
// This ensures we find the accessor in the extension, which
|
|
|
|
// has the implicit parameters filled in.
|
|
|
|
auto *ID = CompileTimeMD->getClassInterface();
|
|
|
|
for (auto *CatDecl : ID->visible_extensions()) {
|
|
|
|
Val = CatDecl->getMethod(Sel,
|
|
|
|
CompileTimeMD->isInstanceMethod());
|
|
|
|
if (*Val)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!*Val)
|
|
|
|
Val = IDecl->lookupInstanceMethod(Sel);
|
|
|
|
}
|
2014-01-11 04:06:06 +08:00
|
|
|
}
|
|
|
|
|
2012-12-07 15:30:19 +08:00
|
|
|
const ObjCMethodDecl *MD = Val.getValue();
|
2012-08-11 02:55:58 +08:00
|
|
|
if (CanBeSubClassed)
|
|
|
|
return RuntimeDefinition(MD, Receiver);
|
2012-08-11 02:55:53 +08:00
|
|
|
else
|
2014-05-27 10:45:47 +08:00
|
|
|
return RuntimeDefinition(MD, nullptr);
|
2012-08-11 02:55:53 +08:00
|
|
|
}
|
2012-07-31 04:31:29 +08:00
|
|
|
} else {
|
|
|
|
// This is a class method.
|
|
|
|
// If we have type info for the receiver class, we are calling via
|
|
|
|
// class name.
|
|
|
|
if (ObjCInterfaceDecl *IDecl = E->getReceiverInterface()) {
|
|
|
|
// Find/Return the method implementation.
|
2012-08-10 02:43:00 +08:00
|
|
|
return RuntimeDefinition(IDecl->lookupPrivateClassMethod(Sel));
|
2012-07-31 04:31:29 +08:00
|
|
|
}
|
2012-07-26 08:27:51 +08:00
|
|
|
}
|
2012-07-31 04:31:29 +08:00
|
|
|
|
2018-02-28 09:10:04 +08:00
|
|
|
return {};
|
2012-07-26 08:27:51 +08:00
|
|
|
}
|
|
|
|
|
2014-01-08 05:39:48 +08:00
|
|
|
bool ObjCMethodCall::argumentsMayEscape() const {
|
|
|
|
if (isInSystemHeader() && !isInstanceMessage()) {
|
|
|
|
Selector Sel = getSelector();
|
|
|
|
if (Sel.getNumArgs() == 1 &&
|
|
|
|
Sel.getIdentifierInfoForSlot(0)->isStr("valueWithPointer"))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CallEvent::argumentsMayEscape();
|
|
|
|
}
|
|
|
|
|
2012-07-31 09:07:55 +08:00
|
|
|
void ObjCMethodCall::getInitialStackFrameContents(
|
|
|
|
const StackFrameContext *CalleeCtx,
|
|
|
|
BindingsTy &Bindings) const {
|
2018-02-28 09:10:04 +08:00
|
|
|
const auto *D = cast<ObjCMethodDecl>(CalleeCtx->getDecl());
|
2012-07-31 09:07:55 +08:00
|
|
|
SValBuilder &SVB = getState()->getStateManager().getSValBuilder();
|
|
|
|
addParameterValuesToBindings(CalleeCtx, Bindings, SVB, *this,
|
2014-01-17 15:15:31 +08:00
|
|
|
D->parameters());
|
2012-07-31 09:07:55 +08:00
|
|
|
|
|
|
|
SVal SelfVal = getReceiverSVal();
|
|
|
|
if (!SelfVal.isUnknown()) {
|
|
|
|
const VarDecl *SelfD = CalleeCtx->getAnalysisDeclContext()->getSelfDecl();
|
|
|
|
MemRegionManager &MRMgr = SVB.getRegionManager();
|
|
|
|
Loc SelfLoc = SVB.makeLoc(MRMgr.getVarRegion(SelfD, CalleeCtx));
|
|
|
|
Bindings.push_back(std::make_pair(SelfLoc, SelfVal));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-14 07:46:05 +08:00
|
|
|
CallEventRef<>
|
2012-07-31 04:22:09 +08:00
|
|
|
CallEventManager::getSimpleCall(const CallExpr *CE, ProgramStateRef State,
|
|
|
|
const LocationContext *LCtx) {
|
2018-02-28 09:10:04 +08:00
|
|
|
if (const auto *MCE = dyn_cast<CXXMemberCallExpr>(CE))
|
2012-07-31 04:22:09 +08:00
|
|
|
return create<CXXMemberCall>(MCE, State, LCtx);
|
|
|
|
|
2018-02-28 09:10:04 +08:00
|
|
|
if (const auto *OpCE = dyn_cast<CXXOperatorCallExpr>(CE)) {
|
2012-07-31 04:22:09 +08:00
|
|
|
const FunctionDecl *DirectCallee = OpCE->getDirectCallee();
|
2018-02-28 09:10:04 +08:00
|
|
|
if (const auto *MD = dyn_cast<CXXMethodDecl>(DirectCallee))
|
2012-07-31 04:22:09 +08:00
|
|
|
if (MD->isInstance())
|
|
|
|
return create<CXXMemberOperatorCall>(OpCE, State, LCtx);
|
|
|
|
|
|
|
|
} else if (CE->getCallee()->getType()->isBlockPointerType()) {
|
|
|
|
return create<BlockCall>(CE, State, LCtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, it's a normal function call, static member function call, or
|
|
|
|
// something we can't reason about.
|
2014-01-16 01:25:15 +08:00
|
|
|
return create<SimpleFunctionCall>(CE, State, LCtx);
|
2012-07-31 04:22:09 +08:00
|
|
|
}
|
2012-07-31 07:39:47 +08:00
|
|
|
|
|
|
|
CallEventRef<>
|
|
|
|
CallEventManager::getCaller(const StackFrameContext *CalleeCtx,
|
|
|
|
ProgramStateRef State) {
|
|
|
|
const LocationContext *ParentCtx = CalleeCtx->getParent();
|
2018-06-27 09:51:55 +08:00
|
|
|
const LocationContext *CallerCtx = ParentCtx->getStackFrame();
|
2012-07-31 07:39:47 +08:00
|
|
|
assert(CallerCtx && "This should not be used for top-level stack frames");
|
|
|
|
|
|
|
|
const Stmt *CallSite = CalleeCtx->getCallSite();
|
|
|
|
|
|
|
|
if (CallSite) {
|
|
|
|
if (const CallExpr *CE = dyn_cast<CallExpr>(CallSite))
|
|
|
|
return getSimpleCall(CE, State, CallerCtx);
|
|
|
|
|
|
|
|
switch (CallSite->getStmtClass()) {
|
2012-08-29 04:52:21 +08:00
|
|
|
case Stmt::CXXConstructExprClass:
|
|
|
|
case Stmt::CXXTemporaryObjectExprClass: {
|
2012-07-31 07:39:47 +08:00
|
|
|
SValBuilder &SVB = State->getStateManager().getSValBuilder();
|
2018-02-28 09:10:04 +08:00
|
|
|
const auto *Ctor = cast<CXXMethodDecl>(CalleeCtx->getDecl());
|
2012-07-31 07:39:47 +08:00
|
|
|
Loc ThisPtr = SVB.getCXXThis(Ctor, CalleeCtx);
|
|
|
|
SVal ThisVal = State->getSVal(ThisPtr);
|
|
|
|
|
|
|
|
return getCXXConstructorCall(cast<CXXConstructExpr>(CallSite),
|
|
|
|
ThisVal.getAsRegion(), State, CallerCtx);
|
|
|
|
}
|
|
|
|
case Stmt::CXXNewExprClass:
|
|
|
|
return getCXXAllocatorCall(cast<CXXNewExpr>(CallSite), State, CallerCtx);
|
|
|
|
case Stmt::ObjCMessageExprClass:
|
|
|
|
return getObjCMethodCall(cast<ObjCMessageExpr>(CallSite),
|
|
|
|
State, CallerCtx);
|
|
|
|
default:
|
|
|
|
llvm_unreachable("This is not an inlineable statement.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back to the CFG. The only thing we haven't handled yet is
|
|
|
|
// destructors, though this could change in the future.
|
|
|
|
const CFGBlock *B = CalleeCtx->getCallSiteBlock();
|
|
|
|
CFGElement E = (*B)[CalleeCtx->getIndex()];
|
2018-02-16 05:03:39 +08:00
|
|
|
assert((E.getAs<CFGImplicitDtor>() || E.getAs<CFGTemporaryDtor>()) &&
|
2013-02-22 04:58:29 +08:00
|
|
|
"All other CFG elements should have exprs");
|
2012-07-31 07:39:47 +08:00
|
|
|
|
|
|
|
SValBuilder &SVB = State->getStateManager().getSValBuilder();
|
2018-02-28 09:10:04 +08:00
|
|
|
const auto *Dtor = cast<CXXDestructorDecl>(CalleeCtx->getDecl());
|
2012-07-31 07:39:47 +08:00
|
|
|
Loc ThisPtr = SVB.getCXXThis(Dtor, CalleeCtx);
|
|
|
|
SVal ThisVal = State->getSVal(ThisPtr);
|
|
|
|
|
|
|
|
const Stmt *Trigger;
|
2013-02-23 08:29:34 +08:00
|
|
|
if (Optional<CFGAutomaticObjDtor> AutoDtor = E.getAs<CFGAutomaticObjDtor>())
|
|
|
|
Trigger = AutoDtor->getTriggerStmt();
|
2013-09-26 00:06:17 +08:00
|
|
|
else if (Optional<CFGDeleteDtor> DeleteDtor = E.getAs<CFGDeleteDtor>())
|
2018-03-01 13:43:23 +08:00
|
|
|
Trigger = DeleteDtor->getDeleteExpr();
|
2012-07-31 07:39:47 +08:00
|
|
|
else
|
|
|
|
Trigger = Dtor->getBody();
|
|
|
|
|
|
|
|
return getCXXDestructorCall(Dtor, Trigger, ThisVal.getAsRegion(),
|
2013-02-23 08:29:34 +08:00
|
|
|
E.getAs<CFGBaseDtor>().hasValue(), State,
|
|
|
|
CallerCtx);
|
2012-07-31 07:39:47 +08:00
|
|
|
}
|