diff options
author | Jordan Rose <jordan_rose@apple.com> | 2012-07-12 00:16:25 +0000 |
---|---|---|
committer | Jordan Rose <jordan_rose@apple.com> | 2012-07-12 00:16:25 +0000 |
commit | c36b30c92c78b95fd29fb5d9d6214d737b3bcb02 (patch) | |
tree | 715c5369b56b6b363095e1029133c079668fc9df /lib/StaticAnalyzer | |
parent | 198871cc90375246d8692680467ff6e810edac36 (diff) |
[analyzer] Don't inline virtual calls unless we can devirtualize properly.
Previously we were using the static type of the base object to inline
methods, whether virtual or non-virtual. Now, we try to see if the base
object has a known type, and if so ask for its implementation of the method.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@160094 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/StaticAnalyzer')
-rw-r--r-- | lib/StaticAnalyzer/Core/Calls.cpp | 95 | ||||
-rw-r--r-- | lib/StaticAnalyzer/Core/ExprEngineCallAndReturn.cpp | 18 | ||||
-rw-r--r-- | lib/StaticAnalyzer/Core/MemRegion.cpp | 26 |
3 files changed, 100 insertions, 39 deletions
diff --git a/lib/StaticAnalyzer/Core/Calls.cpp b/lib/StaticAnalyzer/Core/Calls.cpp index 94fc5fc8c0..cf4d188e43 100644 --- a/lib/StaticAnalyzer/Core/Calls.cpp +++ b/lib/StaticAnalyzer/Core/Calls.cpp @@ -221,7 +221,9 @@ bool CallEvent::mayBeInlined(const Stmt *S) { CallEvent::param_iterator AnyFunctionCall::param_begin(bool UseDefinitionParams) const { - const Decl *D = UseDefinitionParams ? getDefinition() : getDecl(); + bool IgnoredDynamicDispatch; + const Decl *D = UseDefinitionParams ? getDefinition(IgnoredDynamicDispatch) + : getDecl(); if (!D) return 0; @@ -230,7 +232,9 @@ AnyFunctionCall::param_begin(bool UseDefinitionParams) const { CallEvent::param_iterator AnyFunctionCall::param_end(bool UseDefinitionParams) const { - const Decl *D = UseDefinitionParams ? getDefinition() : getDecl(); + bool IgnoredDynamicDispatch; + const Decl *D = UseDefinitionParams ? getDefinition(IgnoredDynamicDispatch) + : getDecl(); if (!D) return 0; @@ -329,6 +333,56 @@ void CallEvent::dump(raw_ostream &Out) const { } +void CXXInstanceCall::addExtraInvalidatedRegions(RegionList &Regions) const { + if (const MemRegion *R = getCXXThisVal().getAsRegion()) + Regions.push_back(R); +} + +static const CXXMethodDecl *devirtualize(const CXXMethodDecl *MD, SVal ThisVal){ + const MemRegion *R = ThisVal.getAsRegion(); + if (!R) + return 0; + + const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R->StripCasts()); + if (!TR) + return 0; + + const CXXRecordDecl *RD = TR->getValueType()->getAsCXXRecordDecl(); + if (!RD) + return 0; + + RD = RD->getDefinition(); + if (!RD) + return 0; + + const CXXMethodDecl *Result = MD->getCorrespondingMethodInClass(RD); + const FunctionDecl *Definition; + if (!Result->hasBody(Definition)) + return 0; + + return cast<CXXMethodDecl>(Definition); +} + + +const Decl *CXXInstanceCall::getDefinition(bool &IsDynamicDispatch) const { + const Decl *D = SimpleCall::getDefinition(IsDynamicDispatch); + if (!D) + return 0; + + const CXXMethodDecl *MD = cast<CXXMethodDecl>(D); + if (!MD->isVirtual()) + return MD; + + // If the method is virtual, see if we can find the actual implementation + // based on context-sensitivity. + if (const CXXMethodDecl *Devirtualized = devirtualize(MD, getCXXThisVal())) + return Devirtualized; + + IsDynamicDispatch = true; + return MD; +} + + SVal CXXMemberCall::getCXXThisVal() const { const Expr *Base = getOriginExpr()->getImplicitObjectArgument(); @@ -340,23 +394,12 @@ SVal CXXMemberCall::getCXXThisVal() const { return getSVal(Base); } -void CXXMemberCall::addExtraInvalidatedRegions(RegionList &Regions) const { - if (const MemRegion *R = getCXXThisVal().getAsRegion()) - Regions.push_back(R); -} - SVal CXXMemberOperatorCall::getCXXThisVal() const { const Expr *Base = getOriginExpr()->getArg(0); return getSVal(Base); } -void -CXXMemberOperatorCall::addExtraInvalidatedRegions(RegionList &Regions) const { - if (const MemRegion *R = getCXXThisVal().getAsRegion()) - Regions.push_back(R); -} - const BlockDataRegion *BlockCall::getBlockRegion() const { const Expr *Callee = getOriginExpr()->getCallee(); @@ -425,10 +468,30 @@ void CXXDestructorCall::addExtraInvalidatedRegions(RegionList &Regions) const { Regions.push_back(Target); } +const Decl *CXXDestructorCall::getDefinition(bool &IsDynamicDispatch) const { + const Decl *D = AnyFunctionCall::getDefinition(IsDynamicDispatch); + if (!D) + return 0; + + const CXXMethodDecl *MD = cast<CXXMethodDecl>(D); + if (!MD->isVirtual()) + return MD; + + // If the method is virtual, see if we can find the actual implementation + // based on context-sensitivity. + if (const CXXMethodDecl *Devirtualized = devirtualize(MD, getCXXThisVal())) + return Devirtualized; + + IsDynamicDispatch = true; + return MD; +} + CallEvent::param_iterator ObjCMethodCall::param_begin(bool UseDefinitionParams) const { - const Decl *D = UseDefinitionParams ? getDefinition() : getDecl(); + bool IgnoredDynamicDispatch; + const Decl *D = UseDefinitionParams ? getDefinition(IgnoredDynamicDispatch) + : getDecl(); if (!D) return 0; @@ -437,7 +500,9 @@ ObjCMethodCall::param_begin(bool UseDefinitionParams) const { CallEvent::param_iterator ObjCMethodCall::param_end(bool UseDefinitionParams) const { - const Decl *D = UseDefinitionParams ? getDefinition() : getDecl(); + bool IgnoredDynamicDispatch; + const Decl *D = UseDefinitionParams ? getDefinition(IgnoredDynamicDispatch) + : getDecl(); if (!D) return 0; diff --git a/lib/StaticAnalyzer/Core/ExprEngineCallAndReturn.cpp b/lib/StaticAnalyzer/Core/ExprEngineCallAndReturn.cpp index 0d9a6d79c9..493ca91b48 100644 --- a/lib/StaticAnalyzer/Core/ExprEngineCallAndReturn.cpp +++ b/lib/StaticAnalyzer/Core/ExprEngineCallAndReturn.cpp @@ -230,10 +230,6 @@ static unsigned getNumberStackFrames(const LocationContext *LCtx) { // Determine if we should inline the call. bool ExprEngine::shouldInlineDecl(const Decl *D, ExplodedNode *Pred) { - // FIXME: default constructors don't have bodies. - if (!D->hasBody()) - return false; - AnalysisDeclContext *CalleeADC = AMgr.getAnalysisDeclContext(D); const CFG *CalleeCFG = CalleeADC->getCFG(); @@ -276,13 +272,13 @@ bool ExprEngine::inlineCall(ExplodedNodeSet &Dst, if (!getAnalysisManager().shouldInlineCall()) return false; - const StackFrameContext *CallerSFC = - Pred->getLocationContext()->getCurrentStackFrame(); - - const Decl *D = Call.getDefinition(); - if (!D) + bool IsDynamicDispatch; + const Decl *D = Call.getDefinition(IsDynamicDispatch); + if (!D || IsDynamicDispatch) return false; + const LocationContext *CurLC = Pred->getLocationContext(); + const StackFrameContext *CallerSFC = CurLC->getCurrentStackFrame(); const LocationContext *ParentOfCallee = 0; switch (Call.getKind()) { @@ -313,7 +309,7 @@ bool ExprEngine::inlineCall(ExplodedNodeSet &Dst, case CE_ObjCPropertyAccess: // These always use dynamic dispatch; enabling inlining means assuming // that a particular method will be called at runtime. - return false; + llvm_unreachable("Dynamic dispatch should be handled above."); } if (!shouldInlineDecl(D, Pred)) @@ -332,7 +328,7 @@ bool ExprEngine::inlineCall(ExplodedNodeSet &Dst, currentBuilderContext->getBlock(), currentStmtIdx); - CallEnter Loc(CallE, CalleeSFC, Pred->getLocationContext()); + CallEnter Loc(CallE, CalleeSFC, CurLC); // Construct a new state which contains the mapping from actual to // formal arguments. diff --git a/lib/StaticAnalyzer/Core/MemRegion.cpp b/lib/StaticAnalyzer/Core/MemRegion.cpp index bcf7c3fe7d..54090783e2 100644 --- a/lib/StaticAnalyzer/Core/MemRegion.cpp +++ b/lib/StaticAnalyzer/Core/MemRegion.cpp @@ -954,21 +954,21 @@ const MemRegion *MemRegion::getBaseRegion() const { const MemRegion *MemRegion::StripCasts() const { const MemRegion *R = this; while (true) { - if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { - // FIXME: generalize. Essentially we want to strip away ElementRegions - // that were layered on a symbolic region because of casts. We only - // want to strip away ElementRegions, however, where the index is 0. - SVal index = ER->getIndex(); - if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&index)) { - if (CI->getValue().getSExtValue() == 0) { - R = ER->getSuperRegion(); - continue; - } - } + switch (R->getKind()) { + case ElementRegionKind: { + const ElementRegion *ER = cast<ElementRegion>(R); + if (!ER->getIndex().isZeroConstant()) + return R; + R = ER->getSuperRegion(); + break; + } + case CXXBaseObjectRegionKind: + R = cast<CXXBaseObjectRegion>(R)->getSuperRegion(); + break; + default: + return R; } - break; } - return R; } // FIXME: Merge with the implementation of the same method in Store.cpp |