2011-10-24 09:32:45 +08:00
|
|
|
//== AnalysisDeclContext.cpp - Analysis context for Path Sens analysis -*- C++ -*-//
|
2009-07-30 09:17:21 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2011-10-24 09:32:45 +08:00
|
|
|
// This file defines AnalysisDeclContext, a class that manages the analysis context
|
2009-07-30 09:17:21 +08:00
|
|
|
// data for path sensitive analysis.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Analysis/AnalysisContext.h"
|
|
|
|
#include "BodyFarm.h"
|
2012-07-05 04:19:54 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-07-30 09:17:21 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/DeclObjC.h"
|
2010-01-13 10:59:54 +08:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2009-07-30 09:17:21 +08:00
|
|
|
#include "clang/AST/ParentMap.h"
|
2009-11-26 10:31:33 +08:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h"
|
2010-03-10 08:18:11 +08:00
|
|
|
#include "clang/Analysis/Analyses/LiveVariables.h"
|
2010-08-24 03:51:57 +08:00
|
|
|
#include "clang/Analysis/Analyses/PseudoConstantAnalysis.h"
|
2010-03-10 08:18:11 +08:00
|
|
|
#include "clang/Analysis/CFG.h"
|
2011-02-23 09:51:53 +08:00
|
|
|
#include "clang/Analysis/CFGStmtMap.h"
|
2009-11-26 10:31:33 +08:00
|
|
|
#include "clang/Analysis/Support/BumpVector.h"
|
2012-03-10 23:08:09 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2009-07-31 09:10:29 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/Support/SaveAndRestore.h"
|
2014-01-07 19:51:46 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-09-21 08:09:11 +08:00
|
|
|
|
2009-07-30 09:17:21 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2011-10-08 06:21:02 +08:00
|
|
|
typedef llvm::DenseMap<const void *, ManagedAnalysis *> ManagedAnalysisMap;
|
|
|
|
|
2011-10-24 09:32:45 +08:00
|
|
|
AnalysisDeclContext::AnalysisDeclContext(AnalysisDeclContextManager *Mgr,
|
2012-09-21 08:09:11 +08:00
|
|
|
const Decl *d,
|
|
|
|
const CFG::BuildOptions &buildOptions)
|
2011-10-23 10:31:52 +08:00
|
|
|
: Manager(Mgr),
|
|
|
|
D(d),
|
2011-07-21 13:22:47 +08:00
|
|
|
cfgBuildOptions(buildOptions),
|
2011-03-10 09:14:05 +08:00
|
|
|
forcedBlkExprs(0),
|
2011-07-21 13:22:47 +08:00
|
|
|
builtCFG(false),
|
|
|
|
builtCompleteCFG(false),
|
2011-10-08 06:21:02 +08:00
|
|
|
ReferencedBlockVars(0),
|
|
|
|
ManagedAnalyses(0)
|
2011-07-21 13:22:47 +08:00
|
|
|
{
|
2011-03-10 09:14:05 +08:00
|
|
|
cfgBuildOptions.forcedBlkExprs = &forcedBlkExprs;
|
2011-07-21 13:22:47 +08:00
|
|
|
}
|
|
|
|
|
2011-10-24 09:32:45 +08:00
|
|
|
AnalysisDeclContext::AnalysisDeclContext(AnalysisDeclContextManager *Mgr,
|
2012-09-21 08:09:11 +08:00
|
|
|
const Decl *d)
|
2011-10-23 10:31:52 +08:00
|
|
|
: Manager(Mgr),
|
|
|
|
D(d),
|
2011-07-21 13:22:47 +08:00
|
|
|
forcedBlkExprs(0),
|
|
|
|
builtCFG(false),
|
|
|
|
builtCompleteCFG(false),
|
2011-10-08 06:21:02 +08:00
|
|
|
ReferencedBlockVars(0),
|
|
|
|
ManagedAnalyses(0)
|
2011-07-21 13:22:47 +08:00
|
|
|
{
|
|
|
|
cfgBuildOptions.forcedBlkExprs = &forcedBlkExprs;
|
|
|
|
}
|
|
|
|
|
2011-10-24 09:32:45 +08:00
|
|
|
AnalysisDeclContextManager::AnalysisDeclContextManager(bool useUnoptimizedCFG,
|
[analyzer] Always include destructors in the analysis CFG.
While destructors will continue to not be inlined (unless the analyzer
config option 'c++-inlining' is set to 'destructors'), leaving them out
of the CFG is an incomplete model of the behavior of an object, and
can cause false positive warnings (like PR13751, now working).
Destructors for temporaries are still not on by default, since
(a) we haven't actually checked this code to be sure it's fully correct
(in particular, we probably need to be very careful with regard to
lifetime-extension when a temporary is bound to a reference,
C++11 [class.temporary]p5), and
(b) ExprEngine doesn't actually do anything when it sees a temporary
destructor in the CFG -- not even invalidate the object region.
To enable temporary destructors, set the 'cfg-temporary-dtors' analyzer
config option to '1'. The old -cfg-add-implicit-dtors cc1 option, which
controlled all implicit destructors, has been removed.
llvm-svn: 163264
2012-09-06 06:55:23 +08:00
|
|
|
bool addImplicitDtors,
|
|
|
|
bool addInitializers,
|
2012-09-21 08:09:11 +08:00
|
|
|
bool addTemporaryDtors,
|
2013-03-29 08:09:22 +08:00
|
|
|
bool synthesizeBodies,
|
2014-01-14 01:59:19 +08:00
|
|
|
bool addStaticInitBranch,
|
|
|
|
bool addCXXNewAllocator)
|
2012-09-21 08:09:11 +08:00
|
|
|
: SynthesizeBodies(synthesizeBodies)
|
|
|
|
{
|
2011-07-21 13:22:47 +08:00
|
|
|
cfgBuildOptions.PruneTriviallyFalseEdges = !useUnoptimizedCFG;
|
2011-03-10 09:14:05 +08:00
|
|
|
cfgBuildOptions.AddImplicitDtors = addImplicitDtors;
|
|
|
|
cfgBuildOptions.AddInitializers = addInitializers;
|
[analyzer] Always include destructors in the analysis CFG.
While destructors will continue to not be inlined (unless the analyzer
config option 'c++-inlining' is set to 'destructors'), leaving them out
of the CFG is an incomplete model of the behavior of an object, and
can cause false positive warnings (like PR13751, now working).
Destructors for temporaries are still not on by default, since
(a) we haven't actually checked this code to be sure it's fully correct
(in particular, we probably need to be very careful with regard to
lifetime-extension when a temporary is bound to a reference,
C++11 [class.temporary]p5), and
(b) ExprEngine doesn't actually do anything when it sees a temporary
destructor in the CFG -- not even invalidate the object region.
To enable temporary destructors, set the 'cfg-temporary-dtors' analyzer
config option to '1'. The old -cfg-add-implicit-dtors cc1 option, which
controlled all implicit destructors, has been removed.
llvm-svn: 163264
2012-09-06 06:55:23 +08:00
|
|
|
cfgBuildOptions.AddTemporaryDtors = addTemporaryDtors;
|
2013-03-29 08:09:22 +08:00
|
|
|
cfgBuildOptions.AddStaticInitBranches = addStaticInitBranch;
|
2014-01-14 01:59:19 +08:00
|
|
|
cfgBuildOptions.AddCXXNewAllocator = addCXXNewAllocator;
|
2011-03-10 09:14:05 +08:00
|
|
|
}
|
|
|
|
|
2011-10-24 09:32:45 +08:00
|
|
|
void AnalysisDeclContextManager::clear() {
|
2014-02-20 07:44:52 +08:00
|
|
|
llvm::DeleteContainerSeconds(Contexts);
|
2009-10-21 05:39:41 +08:00
|
|
|
}
|
|
|
|
|
2012-09-21 08:09:11 +08:00
|
|
|
static BodyFarm &getBodyFarm(ASTContext &C) {
|
|
|
|
static BodyFarm *BF = new BodyFarm(C);
|
|
|
|
return *BF;
|
|
|
|
}
|
|
|
|
|
2013-02-02 08:30:04 +08:00
|
|
|
Stmt *AnalysisDeclContext::getBody(bool &IsAutosynthesized) const {
|
2013-02-04 13:06:21 +08:00
|
|
|
IsAutosynthesized = false;
|
2012-09-21 08:09:11 +08:00
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
Stmt *Body = FD->getBody();
|
2013-02-02 08:30:04 +08:00
|
|
|
if (!Body && Manager && Manager->synthesizeBodies()) {
|
2014-01-11 04:06:06 +08:00
|
|
|
Body = getBodyFarm(getASTContext()).getBody(FD);
|
|
|
|
if (Body)
|
|
|
|
IsAutosynthesized = true;
|
2013-02-02 08:30:04 +08:00
|
|
|
}
|
2012-09-21 08:09:11 +08:00
|
|
|
return Body;
|
|
|
|
}
|
2014-01-11 04:06:06 +08:00
|
|
|
else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
|
|
|
|
Stmt *Body = MD->getBody();
|
|
|
|
if (!Body && Manager && Manager->synthesizeBodies()) {
|
|
|
|
Body = getBodyFarm(getASTContext()).getBody(MD);
|
|
|
|
if (Body)
|
|
|
|
IsAutosynthesized = true;
|
|
|
|
}
|
|
|
|
return Body;
|
|
|
|
} else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
|
2009-12-05 04:34:55 +08:00
|
|
|
return BD->getBody();
|
2010-01-13 10:59:54 +08:00
|
|
|
else if (const FunctionTemplateDecl *FunTmpl
|
|
|
|
= dyn_cast_or_null<FunctionTemplateDecl>(D))
|
|
|
|
return FunTmpl->getTemplatedDecl()->getBody();
|
2009-07-30 09:17:21 +08:00
|
|
|
|
2009-12-12 13:05:38 +08:00
|
|
|
llvm_unreachable("unknown code decl");
|
2009-07-30 09:17:21 +08:00
|
|
|
}
|
|
|
|
|
2013-02-02 08:30:04 +08:00
|
|
|
Stmt *AnalysisDeclContext::getBody() const {
|
|
|
|
bool Tmp;
|
|
|
|
return getBody(Tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AnalysisDeclContext::isBodyAutosynthesized() const {
|
|
|
|
bool Tmp;
|
|
|
|
getBody(Tmp);
|
|
|
|
return Tmp;
|
|
|
|
}
|
|
|
|
|
2011-10-24 09:32:45 +08:00
|
|
|
const ImplicitParamDecl *AnalysisDeclContext::getSelfDecl() const {
|
2009-08-22 07:25:54 +08:00
|
|
|
if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
|
|
|
|
return MD->getSelfDecl();
|
2011-11-15 03:36:08 +08:00
|
|
|
if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
|
|
|
|
// See if 'self' was captured by the block.
|
|
|
|
for (BlockDecl::capture_const_iterator it = BD->capture_begin(),
|
|
|
|
et = BD->capture_end(); it != et; ++it) {
|
|
|
|
const VarDecl *VD = it->getVariable();
|
|
|
|
if (VD->getName() == "self")
|
|
|
|
return dyn_cast<ImplicitParamDecl>(VD);
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-22 07:25:54 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-10-24 09:32:45 +08:00
|
|
|
void AnalysisDeclContext::registerForcedBlockExpression(const Stmt *stmt) {
|
2011-03-10 11:50:34 +08:00
|
|
|
if (!forcedBlkExprs)
|
|
|
|
forcedBlkExprs = new CFG::BuildOptions::ForcedBlkExprs();
|
|
|
|
// Default construct an entry for 'stmt'.
|
2011-06-10 16:49:37 +08:00
|
|
|
if (const Expr *e = dyn_cast<Expr>(stmt))
|
|
|
|
stmt = e->IgnoreParens();
|
2011-03-10 11:50:34 +08:00
|
|
|
(void) (*forcedBlkExprs)[stmt];
|
|
|
|
}
|
|
|
|
|
|
|
|
const CFGBlock *
|
2011-10-24 09:32:45 +08:00
|
|
|
AnalysisDeclContext::getBlockForRegisteredExpression(const Stmt *stmt) {
|
2011-03-10 11:50:34 +08:00
|
|
|
assert(forcedBlkExprs);
|
2011-06-10 16:49:37 +08:00
|
|
|
if (const Expr *e = dyn_cast<Expr>(stmt))
|
|
|
|
stmt = e->IgnoreParens();
|
2011-03-10 11:50:34 +08:00
|
|
|
CFG::BuildOptions::ForcedBlkExprs::const_iterator itr =
|
|
|
|
forcedBlkExprs->find(stmt);
|
|
|
|
assert(itr != forcedBlkExprs->end());
|
|
|
|
return itr->second;
|
|
|
|
}
|
|
|
|
|
[analyzer; new edges] Simplify edges in a C++11 for-range loop.
Previously our edges were completely broken here; now, the final result
is a very simple set of edges in most cases: one up to the "for" keyword
for context, and one into the body of the loop. This matches the behavior
for ObjC for-in loops.
In the AST, however, CXXForRangeStmts are handled very differently from
ObjCForCollectionStmts. Since they are specified in terms of equivalent
statements in the C++ standard, we actually have implicit AST nodes for
all of the semantic statements. This makes evaluation very easy, but
diagnostic locations a bit trickier. Fortunately, the problem can be
generally defined away by marking all of the implicit statements as
part of the top-level for-range statement.
One of the implicit statements in a for-range statement is the declaration
of implicit iterators __begin and __end. The CFG synthesizes two
separate DeclStmts to match each of these decls, but until now these
synthetic DeclStmts weren't in the function's ParentMap. Now, the CFG
keeps track of its synthetic statements, and the AnalysisDeclContext will
make sure to add them to the ParentMap.
<rdar://problem/14038483>
llvm-svn: 183449
2013-06-07 05:53:45 +08:00
|
|
|
/// Add each synthetic statement in the CFG to the parent map, using the
|
|
|
|
/// source statement's parent.
|
|
|
|
static void addParentsForSyntheticStmts(const CFG *TheCFG, ParentMap &PM) {
|
|
|
|
if (!TheCFG)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (CFG::synthetic_stmt_iterator I = TheCFG->synthetic_stmt_begin(),
|
|
|
|
E = TheCFG->synthetic_stmt_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
PM.setParent(I->first, PM.getParent(I->second));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-24 09:32:45 +08:00
|
|
|
CFG *AnalysisDeclContext::getCFG() {
|
2011-07-21 13:22:47 +08:00
|
|
|
if (!cfgBuildOptions.PruneTriviallyFalseEdges)
|
2010-08-03 08:09:51 +08:00
|
|
|
return getUnoptimizedCFG();
|
|
|
|
|
2010-03-23 08:13:23 +08:00
|
|
|
if (!builtCFG) {
|
2011-03-10 09:14:05 +08:00
|
|
|
cfg.reset(CFG::buildCFG(D, getBody(),
|
|
|
|
&D->getASTContext(), cfgBuildOptions));
|
2010-03-23 08:13:23 +08:00
|
|
|
// Even when the cfg is not successfully built, we don't
|
|
|
|
// want to try building it again.
|
|
|
|
builtCFG = true;
|
[analyzer; new edges] Simplify edges in a C++11 for-range loop.
Previously our edges were completely broken here; now, the final result
is a very simple set of edges in most cases: one up to the "for" keyword
for context, and one into the body of the loop. This matches the behavior
for ObjC for-in loops.
In the AST, however, CXXForRangeStmts are handled very differently from
ObjCForCollectionStmts. Since they are specified in terms of equivalent
statements in the C++ standard, we actually have implicit AST nodes for
all of the semantic statements. This makes evaluation very easy, but
diagnostic locations a bit trickier. Fortunately, the problem can be
generally defined away by marking all of the implicit statements as
part of the top-level for-range statement.
One of the implicit statements in a for-range statement is the declaration
of implicit iterators __begin and __end. The CFG synthesizes two
separate DeclStmts to match each of these decls, but until now these
synthetic DeclStmts weren't in the function's ParentMap. Now, the CFG
keeps track of its synthetic statements, and the AnalysisDeclContext will
make sure to add them to the ParentMap.
<rdar://problem/14038483>
llvm-svn: 183449
2013-06-07 05:53:45 +08:00
|
|
|
|
|
|
|
if (PM)
|
|
|
|
addParentsForSyntheticStmts(cfg.get(), *PM);
|
2010-03-23 08:13:23 +08:00
|
|
|
}
|
2011-03-10 09:14:05 +08:00
|
|
|
return cfg.get();
|
2009-07-30 09:17:21 +08:00
|
|
|
}
|
|
|
|
|
2011-10-24 09:32:45 +08:00
|
|
|
CFG *AnalysisDeclContext::getUnoptimizedCFG() {
|
2010-08-03 07:46:59 +08:00
|
|
|
if (!builtCompleteCFG) {
|
2011-07-21 13:22:47 +08:00
|
|
|
SaveAndRestore<bool> NotPrune(cfgBuildOptions.PruneTriviallyFalseEdges,
|
|
|
|
false);
|
|
|
|
completeCFG.reset(CFG::buildCFG(D, getBody(), &D->getASTContext(),
|
|
|
|
cfgBuildOptions));
|
2010-08-03 07:46:59 +08:00
|
|
|
// Even when the cfg is not successfully built, we don't
|
|
|
|
// want to try building it again.
|
|
|
|
builtCompleteCFG = true;
|
[analyzer; new edges] Simplify edges in a C++11 for-range loop.
Previously our edges were completely broken here; now, the final result
is a very simple set of edges in most cases: one up to the "for" keyword
for context, and one into the body of the loop. This matches the behavior
for ObjC for-in loops.
In the AST, however, CXXForRangeStmts are handled very differently from
ObjCForCollectionStmts. Since they are specified in terms of equivalent
statements in the C++ standard, we actually have implicit AST nodes for
all of the semantic statements. This makes evaluation very easy, but
diagnostic locations a bit trickier. Fortunately, the problem can be
generally defined away by marking all of the implicit statements as
part of the top-level for-range statement.
One of the implicit statements in a for-range statement is the declaration
of implicit iterators __begin and __end. The CFG synthesizes two
separate DeclStmts to match each of these decls, but until now these
synthetic DeclStmts weren't in the function's ParentMap. Now, the CFG
keeps track of its synthetic statements, and the AnalysisDeclContext will
make sure to add them to the ParentMap.
<rdar://problem/14038483>
llvm-svn: 183449
2013-06-07 05:53:45 +08:00
|
|
|
|
|
|
|
if (PM)
|
|
|
|
addParentsForSyntheticStmts(completeCFG.get(), *PM);
|
2010-08-03 07:46:59 +08:00
|
|
|
}
|
2011-03-10 09:14:05 +08:00
|
|
|
return completeCFG.get();
|
2010-08-03 07:46:59 +08:00
|
|
|
}
|
|
|
|
|
2011-10-24 09:32:45 +08:00
|
|
|
CFGStmtMap *AnalysisDeclContext::getCFGStmtMap() {
|
2011-02-23 09:51:53 +08:00
|
|
|
if (cfgStmtMap)
|
2011-03-10 09:14:05 +08:00
|
|
|
return cfgStmtMap.get();
|
2011-02-23 09:51:53 +08:00
|
|
|
|
|
|
|
if (CFG *c = getCFG()) {
|
2011-03-10 09:14:05 +08:00
|
|
|
cfgStmtMap.reset(CFGStmtMap::Build(c, &getParentMap()));
|
|
|
|
return cfgStmtMap.get();
|
2011-02-23 09:51:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2011-02-23 09:51:59 +08:00
|
|
|
|
2011-10-24 09:32:45 +08:00
|
|
|
CFGReverseBlockReachabilityAnalysis *AnalysisDeclContext::getCFGReachablityAnalysis() {
|
2011-02-23 09:51:59 +08:00
|
|
|
if (CFA)
|
2011-03-10 09:14:05 +08:00
|
|
|
return CFA.get();
|
2011-02-23 09:51:59 +08:00
|
|
|
|
|
|
|
if (CFG *c = getCFG()) {
|
2011-03-19 09:00:33 +08:00
|
|
|
CFA.reset(new CFGReverseBlockReachabilityAnalysis(*c));
|
2011-03-10 09:14:05 +08:00
|
|
|
return CFA.get();
|
2011-02-23 09:51:59 +08:00
|
|
|
}
|
2011-02-23 09:51:53 +08:00
|
|
|
|
2011-02-23 09:51:59 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2011-02-23 09:51:53 +08:00
|
|
|
|
2011-12-23 07:33:52 +08:00
|
|
|
void AnalysisDeclContext::dumpCFG(bool ShowColors) {
|
2012-03-11 15:00:24 +08:00
|
|
|
getCFG()->dump(getASTContext().getLangOpts(), ShowColors);
|
2011-01-17 06:05:23 +08:00
|
|
|
}
|
|
|
|
|
2011-10-24 09:32:45 +08:00
|
|
|
ParentMap &AnalysisDeclContext::getParentMap() {
|
2012-07-27 04:04:30 +08:00
|
|
|
if (!PM) {
|
2013-05-18 10:26:50 +08:00
|
|
|
PM.reset(new ParentMap(getBody()));
|
|
|
|
if (const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(getDecl())) {
|
|
|
|
for (CXXConstructorDecl::init_const_iterator I = C->init_begin(),
|
|
|
|
E = C->init_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
PM->addStmt((*I)->getInit());
|
|
|
|
}
|
|
|
|
}
|
[analyzer; new edges] Simplify edges in a C++11 for-range loop.
Previously our edges were completely broken here; now, the final result
is a very simple set of edges in most cases: one up to the "for" keyword
for context, and one into the body of the loop. This matches the behavior
for ObjC for-in loops.
In the AST, however, CXXForRangeStmts are handled very differently from
ObjCForCollectionStmts. Since they are specified in terms of equivalent
statements in the C++ standard, we actually have implicit AST nodes for
all of the semantic statements. This makes evaluation very easy, but
diagnostic locations a bit trickier. Fortunately, the problem can be
generally defined away by marking all of the implicit statements as
part of the top-level for-range statement.
One of the implicit statements in a for-range statement is the declaration
of implicit iterators __begin and __end. The CFG synthesizes two
separate DeclStmts to match each of these decls, but until now these
synthetic DeclStmts weren't in the function's ParentMap. Now, the CFG
keeps track of its synthetic statements, and the AnalysisDeclContext will
make sure to add them to the ParentMap.
<rdar://problem/14038483>
llvm-svn: 183449
2013-06-07 05:53:45 +08:00
|
|
|
if (builtCFG)
|
|
|
|
addParentsForSyntheticStmts(getCFG(), *PM);
|
|
|
|
if (builtCompleteCFG)
|
|
|
|
addParentsForSyntheticStmts(getUnoptimizedCFG(), *PM);
|
2012-07-27 04:04:30 +08:00
|
|
|
}
|
2009-07-30 09:17:21 +08:00
|
|
|
return *PM;
|
|
|
|
}
|
|
|
|
|
2011-10-24 09:32:45 +08:00
|
|
|
PseudoConstantAnalysis *AnalysisDeclContext::getPseudoConstantAnalysis() {
|
2010-08-19 05:17:24 +08:00
|
|
|
if (!PCA)
|
2011-03-10 09:14:05 +08:00
|
|
|
PCA.reset(new PseudoConstantAnalysis(getBody()));
|
|
|
|
return PCA.get();
|
2010-08-19 05:17:24 +08:00
|
|
|
}
|
|
|
|
|
2012-04-28 09:58:08 +08:00
|
|
|
AnalysisDeclContext *AnalysisDeclContextManager::getContext(const Decl *D) {
|
2012-09-21 08:09:11 +08:00
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
2012-09-25 05:17:14 +08:00
|
|
|
// Calling 'hasBody' replaces 'FD' in place with the FunctionDecl
|
|
|
|
// that has the body.
|
2012-09-21 08:09:11 +08:00
|
|
|
FD->hasBody(FD);
|
|
|
|
D = FD;
|
|
|
|
}
|
|
|
|
|
2011-10-24 09:32:45 +08:00
|
|
|
AnalysisDeclContext *&AC = Contexts[D];
|
2009-08-22 07:58:43 +08:00
|
|
|
if (!AC)
|
2012-04-28 09:58:08 +08:00
|
|
|
AC = new AnalysisDeclContext(this, D, cfgBuildOptions);
|
2009-08-22 07:58:43 +08:00
|
|
|
return AC;
|
2009-07-30 09:17:21 +08:00
|
|
|
}
|
2009-08-03 15:23:22 +08:00
|
|
|
|
2011-10-23 10:31:52 +08:00
|
|
|
const StackFrameContext *
|
2011-10-24 09:32:45 +08:00
|
|
|
AnalysisDeclContext::getStackFrame(LocationContext const *Parent, const Stmt *S,
|
2011-10-23 10:31:52 +08:00
|
|
|
const CFGBlock *Blk, unsigned Idx) {
|
|
|
|
return getLocationContextManager().getStackFrame(this, Parent, S, Blk, Idx);
|
|
|
|
}
|
|
|
|
|
2012-06-02 04:04:04 +08:00
|
|
|
const BlockInvocationContext *
|
|
|
|
AnalysisDeclContext::getBlockInvocationContext(const LocationContext *parent,
|
|
|
|
const clang::BlockDecl *BD,
|
|
|
|
const void *ContextData) {
|
|
|
|
return getLocationContextManager().getBlockInvocationContext(this, parent,
|
|
|
|
BD, ContextData);
|
|
|
|
}
|
|
|
|
|
2011-10-24 09:32:45 +08:00
|
|
|
LocationContextManager & AnalysisDeclContext::getLocationContextManager() {
|
2011-10-23 10:31:52 +08:00
|
|
|
assert(Manager &&
|
2011-10-24 09:32:45 +08:00
|
|
|
"Cannot create LocationContexts without an AnalysisDeclContextManager!");
|
2011-10-23 10:31:52 +08:00
|
|
|
return Manager->getLocationContextManager();
|
|
|
|
}
|
|
|
|
|
2009-12-04 08:50:10 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// FoldingSet profiling.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID,
|
|
|
|
ContextKind ck,
|
2011-10-24 09:32:45 +08:00
|
|
|
AnalysisDeclContext *ctx,
|
2009-12-04 08:50:10 +08:00
|
|
|
const LocationContext *parent,
|
2011-08-13 07:37:29 +08:00
|
|
|
const void *data) {
|
2009-12-04 09:28:56 +08:00
|
|
|
ID.AddInteger(ck);
|
|
|
|
ID.AddPointer(ctx);
|
|
|
|
ID.AddPointer(parent);
|
2009-12-04 08:50:10 +08:00
|
|
|
ID.AddPointer(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) {
|
2011-10-24 09:32:45 +08:00
|
|
|
Profile(ID, getAnalysisDeclContext(), getParent(), CallSite, Block, Index);
|
2009-08-03 15:23:22 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 08:50:10 +08:00
|
|
|
void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
|
2011-10-24 09:32:45 +08:00
|
|
|
Profile(ID, getAnalysisDeclContext(), getParent(), Enter);
|
2009-08-03 15:23:22 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 08:50:10 +08:00
|
|
|
void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
|
2012-06-02 04:04:04 +08:00
|
|
|
Profile(ID, getAnalysisDeclContext(), getParent(), BD, ContextData);
|
2009-12-04 08:50:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-12-04 09:28:56 +08:00
|
|
|
// LocationContext creation.
|
2009-12-04 08:50:10 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-12-04 09:28:56 +08:00
|
|
|
template <typename LOC, typename DATA>
|
|
|
|
const LOC*
|
2011-10-24 09:32:45 +08:00
|
|
|
LocationContextManager::getLocationContext(AnalysisDeclContext *ctx,
|
2009-12-04 09:28:56 +08:00
|
|
|
const LocationContext *parent,
|
|
|
|
const DATA *d) {
|
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
LOC::Profile(ID, ctx, parent, d);
|
|
|
|
void *InsertPos;
|
2010-03-23 08:13:23 +08:00
|
|
|
|
2009-12-04 09:28:56 +08:00
|
|
|
LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
|
2010-03-23 08:13:23 +08:00
|
|
|
|
2009-12-04 09:28:56 +08:00
|
|
|
if (!L) {
|
|
|
|
L = new LOC(ctx, parent, d);
|
|
|
|
Contexts.InsertNode(L, InsertPos);
|
|
|
|
}
|
|
|
|
return L;
|
2009-10-21 05:39:41 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 09:28:56 +08:00
|
|
|
const StackFrameContext*
|
2011-10-24 09:32:45 +08:00
|
|
|
LocationContextManager::getStackFrame(AnalysisDeclContext *ctx,
|
2009-08-22 07:39:58 +08:00
|
|
|
const LocationContext *parent,
|
2010-12-16 15:46:53 +08:00
|
|
|
const Stmt *s,
|
2010-11-24 21:08:51 +08:00
|
|
|
const CFGBlock *blk, unsigned idx) {
|
2009-12-24 11:34:38 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2010-12-16 15:46:53 +08:00
|
|
|
StackFrameContext::Profile(ID, ctx, parent, s, blk, idx);
|
2009-12-24 11:34:38 +08:00
|
|
|
void *InsertPos;
|
2010-03-23 08:13:23 +08:00
|
|
|
StackFrameContext *L =
|
2009-12-24 11:34:38 +08:00
|
|
|
cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
|
|
|
|
if (!L) {
|
2010-12-16 15:46:53 +08:00
|
|
|
L = new StackFrameContext(ctx, parent, s, blk, idx);
|
2009-12-24 11:34:38 +08:00
|
|
|
Contexts.InsertNode(L, InsertPos);
|
|
|
|
}
|
|
|
|
return L;
|
2009-08-03 15:23:22 +08:00
|
|
|
}
|
|
|
|
|
2009-12-04 09:28:56 +08:00
|
|
|
const ScopeContext *
|
2011-10-24 09:32:45 +08:00
|
|
|
LocationContextManager::getScope(AnalysisDeclContext *ctx,
|
2009-12-04 09:28:56 +08:00
|
|
|
const LocationContext *parent,
|
|
|
|
const Stmt *s) {
|
|
|
|
return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
|
|
|
|
}
|
2009-08-03 15:23:22 +08:00
|
|
|
|
2012-06-02 04:04:04 +08:00
|
|
|
const BlockInvocationContext *
|
|
|
|
LocationContextManager::getBlockInvocationContext(AnalysisDeclContext *ctx,
|
|
|
|
const LocationContext *parent,
|
|
|
|
const BlockDecl *BD,
|
|
|
|
const void *ContextData) {
|
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
BlockInvocationContext::Profile(ID, ctx, parent, BD, ContextData);
|
|
|
|
void *InsertPos;
|
|
|
|
BlockInvocationContext *L =
|
|
|
|
cast_or_null<BlockInvocationContext>(Contexts.FindNodeOrInsertPos(ID,
|
|
|
|
InsertPos));
|
|
|
|
if (!L) {
|
|
|
|
L = new BlockInvocationContext(ctx, parent, BD, ContextData);
|
|
|
|
Contexts.InsertNode(L, InsertPos);
|
|
|
|
}
|
|
|
|
return L;
|
|
|
|
}
|
|
|
|
|
2009-12-08 06:05:27 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// LocationContext methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
const StackFrameContext *LocationContext::getCurrentStackFrame() const {
|
|
|
|
const LocationContext *LC = this;
|
|
|
|
while (LC) {
|
|
|
|
if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC))
|
|
|
|
return SFC;
|
|
|
|
LC = LC->getParent();
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-11-03 10:54:16 +08:00
|
|
|
bool LocationContext::inTopFrame() const {
|
|
|
|
return getCurrentStackFrame()->inTopFrame();
|
|
|
|
}
|
|
|
|
|
2010-02-17 16:45:06 +08:00
|
|
|
bool LocationContext::isParentOf(const LocationContext *LC) const {
|
|
|
|
do {
|
|
|
|
const LocationContext *Parent = LC->getParent();
|
|
|
|
if (Parent == this)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
LC = Parent;
|
|
|
|
} while (LC);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-19 08:59:08 +08:00
|
|
|
void LocationContext::dumpStack(raw_ostream &OS, StringRef Indent) const {
|
2013-03-30 09:31:35 +08:00
|
|
|
ASTContext &Ctx = getAnalysisDeclContext()->getASTContext();
|
|
|
|
PrintingPolicy PP(Ctx.getLangOpts());
|
|
|
|
PP.TerseOutput = 1;
|
|
|
|
|
|
|
|
unsigned Frame = 0;
|
|
|
|
for (const LocationContext *LCtx = this; LCtx; LCtx = LCtx->getParent()) {
|
|
|
|
switch (LCtx->getKind()) {
|
|
|
|
case StackFrame:
|
2013-07-19 08:59:08 +08:00
|
|
|
OS << Indent << '#' << Frame++ << ' ';
|
|
|
|
cast<StackFrameContext>(LCtx)->getDecl()->print(OS, PP);
|
|
|
|
OS << '\n';
|
2013-03-30 09:31:35 +08:00
|
|
|
break;
|
|
|
|
case Scope:
|
2013-07-19 08:59:08 +08:00
|
|
|
OS << Indent << " (scope)\n";
|
2013-03-30 09:31:35 +08:00
|
|
|
break;
|
|
|
|
case Block:
|
2013-07-19 08:59:08 +08:00
|
|
|
OS << Indent << " (block context: "
|
2013-03-30 09:31:35 +08:00
|
|
|
<< cast<BlockInvocationContext>(LCtx)->getContextData()
|
|
|
|
<< ")\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-04 21:47:14 +08:00
|
|
|
LLVM_DUMP_METHOD void LocationContext::dumpStack() const {
|
2013-07-19 08:59:08 +08:00
|
|
|
dumpStack(llvm::errs());
|
|
|
|
}
|
|
|
|
|
2009-11-26 10:31:33 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Lazily generated map to query the external variables referenced by a Block.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
|
|
|
|
BumpVector<const VarDecl*> &BEVals;
|
|
|
|
BumpVectorContext &BC;
|
2012-03-10 23:08:09 +08:00
|
|
|
llvm::SmallPtrSet<const VarDecl*, 4> Visited;
|
|
|
|
llvm::SmallPtrSet<const DeclContext*, 4> IgnoredContexts;
|
2009-11-26 10:31:33 +08:00
|
|
|
public:
|
|
|
|
FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
|
|
|
|
BumpVectorContext &bc)
|
|
|
|
: BEVals(bevals), BC(bc) {}
|
2010-03-10 08:18:11 +08:00
|
|
|
|
|
|
|
bool IsTrackedDecl(const VarDecl *VD) {
|
|
|
|
const DeclContext *DC = VD->getDeclContext();
|
|
|
|
return IgnoredContexts.count(DC) == 0;
|
|
|
|
}
|
|
|
|
|
2009-11-26 10:31:33 +08:00
|
|
|
void VisitStmt(Stmt *S) {
|
2011-02-13 12:07:26 +08:00
|
|
|
for (Stmt::child_range I = S->children(); I; ++I)
|
2009-11-26 10:31:33 +08:00
|
|
|
if (Stmt *child = *I)
|
|
|
|
Visit(child);
|
|
|
|
}
|
2010-02-06 08:30:00 +08:00
|
|
|
|
2011-12-22 09:30:46 +08:00
|
|
|
void VisitDeclRefExpr(DeclRefExpr *DR) {
|
2010-02-06 08:30:00 +08:00
|
|
|
// Non-local variables are also directly modified.
|
2012-03-10 23:08:09 +08:00
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
|
2010-02-06 08:30:00 +08:00
|
|
|
if (!VD->hasLocalStorage()) {
|
2012-03-10 23:08:09 +08:00
|
|
|
if (Visited.insert(VD))
|
2010-02-06 08:30:00 +08:00
|
|
|
BEVals.push_back(VD, BC);
|
|
|
|
}
|
2012-03-10 23:08:09 +08:00
|
|
|
}
|
2010-02-06 08:30:00 +08:00
|
|
|
}
|
2010-03-10 08:18:11 +08:00
|
|
|
|
|
|
|
void VisitBlockExpr(BlockExpr *BR) {
|
|
|
|
// Blocks containing blocks can transitively capture more variables.
|
|
|
|
IgnoredContexts.insert(BR->getBlockDecl());
|
|
|
|
Visit(BR->getBlockDecl()->getBody());
|
|
|
|
}
|
2011-12-22 09:30:46 +08:00
|
|
|
|
|
|
|
void VisitPseudoObjectExpr(PseudoObjectExpr *PE) {
|
|
|
|
for (PseudoObjectExpr::semantics_iterator it = PE->semantics_begin(),
|
|
|
|
et = PE->semantics_end(); it != et; ++it) {
|
|
|
|
Expr *Semantic = *it;
|
|
|
|
if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Semantic))
|
|
|
|
Semantic = OVE->getSourceExpr();
|
|
|
|
Visit(Semantic);
|
|
|
|
}
|
|
|
|
}
|
2010-03-23 08:13:23 +08:00
|
|
|
};
|
2009-11-26 10:31:33 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
typedef BumpVector<const VarDecl*> DeclVec;
|
|
|
|
|
|
|
|
static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
|
|
|
|
void *&Vec,
|
|
|
|
llvm::BumpPtrAllocator &A) {
|
|
|
|
if (Vec)
|
|
|
|
return (DeclVec*) Vec;
|
2010-03-23 08:13:23 +08:00
|
|
|
|
2009-11-26 10:31:33 +08:00
|
|
|
BumpVectorContext BC(A);
|
|
|
|
DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
|
|
|
|
new (BV) DeclVec(BC, 10);
|
2010-03-23 08:13:23 +08:00
|
|
|
|
2012-12-06 15:17:26 +08:00
|
|
|
// Go through the capture list.
|
|
|
|
for (BlockDecl::capture_const_iterator CI = BD->capture_begin(),
|
|
|
|
CE = BD->capture_end(); CI != CE; ++CI) {
|
|
|
|
BV->push_back(CI->getVariable(), BC);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the referenced global/static variables.
|
2009-11-26 10:31:33 +08:00
|
|
|
FindBlockDeclRefExprsVals F(*BV, BC);
|
|
|
|
F.Visit(BD->getBody());
|
2010-03-23 08:13:23 +08:00
|
|
|
|
|
|
|
Vec = BV;
|
2009-11-26 10:31:33 +08:00
|
|
|
return BV;
|
|
|
|
}
|
|
|
|
|
2011-10-24 09:32:45 +08:00
|
|
|
std::pair<AnalysisDeclContext::referenced_decls_iterator,
|
|
|
|
AnalysisDeclContext::referenced_decls_iterator>
|
|
|
|
AnalysisDeclContext::getReferencedBlockVars(const BlockDecl *BD) {
|
2009-11-26 10:31:33 +08:00
|
|
|
if (!ReferencedBlockVars)
|
|
|
|
ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
|
2010-03-23 08:13:23 +08:00
|
|
|
|
2009-11-26 10:31:33 +08:00
|
|
|
DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
|
|
|
|
return std::make_pair(V->begin(), V->end());
|
|
|
|
}
|
|
|
|
|
2011-10-24 09:32:45 +08:00
|
|
|
ManagedAnalysis *&AnalysisDeclContext::getAnalysisImpl(const void *tag) {
|
2011-10-08 06:21:02 +08:00
|
|
|
if (!ManagedAnalyses)
|
|
|
|
ManagedAnalyses = new ManagedAnalysisMap();
|
|
|
|
ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses;
|
|
|
|
return (*M)[tag];
|
|
|
|
}
|
|
|
|
|
2009-11-26 10:31:33 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Cleanup.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-10-08 06:21:02 +08:00
|
|
|
ManagedAnalysis::~ManagedAnalysis() {}
|
|
|
|
|
2011-10-24 09:32:45 +08:00
|
|
|
AnalysisDeclContext::~AnalysisDeclContext() {
|
2011-03-10 09:14:05 +08:00
|
|
|
delete forcedBlkExprs;
|
2009-11-26 10:31:33 +08:00
|
|
|
delete ReferencedBlockVars;
|
2011-10-08 06:21:02 +08:00
|
|
|
// Release the managed analyses.
|
|
|
|
if (ManagedAnalyses) {
|
|
|
|
ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses;
|
2014-02-20 07:44:52 +08:00
|
|
|
llvm::DeleteContainerSeconds(*M);
|
2011-10-08 06:21:02 +08:00
|
|
|
delete M;
|
|
|
|
}
|
2009-11-26 10:31:33 +08:00
|
|
|
}
|
|
|
|
|
2011-10-24 09:32:45 +08:00
|
|
|
AnalysisDeclContextManager::~AnalysisDeclContextManager() {
|
2014-02-20 07:44:52 +08:00
|
|
|
llvm::DeleteContainerSeconds(Contexts);
|
2009-11-26 10:31:33 +08:00
|
|
|
}
|
2009-12-04 09:28:56 +08:00
|
|
|
|
|
|
|
LocationContext::~LocationContext() {}
|
|
|
|
|
|
|
|
LocationContextManager::~LocationContextManager() {
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocationContextManager::clear() {
|
|
|
|
for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
|
2010-03-23 08:13:23 +08:00
|
|
|
E = Contexts.end(); I != E; ) {
|
2009-12-04 09:28:56 +08:00
|
|
|
LocationContext *LC = &*I;
|
|
|
|
++I;
|
|
|
|
delete LC;
|
|
|
|
}
|
2010-03-23 08:13:23 +08:00
|
|
|
|
2009-12-04 09:28:56 +08:00
|
|
|
Contexts.clear();
|
|
|
|
}
|
|
|
|
|