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),
|
2014-05-20 12:30:07 +08:00
|
|
|
forcedBlkExprs(nullptr),
|
2011-07-21 13:22:47 +08:00
|
|
|
builtCFG(false),
|
|
|
|
builtCompleteCFG(false),
|
2014-05-20 12:30:07 +08:00
|
|
|
ReferencedBlockVars(nullptr),
|
|
|
|
ManagedAnalyses(nullptr)
|
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),
|
2014-05-20 12:30:07 +08:00
|
|
|
forcedBlkExprs(nullptr),
|
2011-07-21 13:22:47 +08:00
|
|
|
builtCFG(false),
|
|
|
|
builtCompleteCFG(false),
|
2014-05-20 12:30:07 +08:00
|
|
|
ReferencedBlockVars(nullptr),
|
|
|
|
ManagedAnalyses(nullptr)
|
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,
|
2017-07-12 15:04:19 +08:00
|
|
|
bool addLifetime,
|
2013-03-29 08:09:22 +08:00
|
|
|
bool synthesizeBodies,
|
2014-01-14 01:59:19 +08:00
|
|
|
bool addStaticInitBranch,
|
Add support for the static analyzer to synthesize function implementations from external model files.
Currently the analyzer lazily models some functions using 'BodyFarm',
which constructs a fake function implementation that the analyzer
can simulate that approximates the semantics of the function when
it is called. BodyFarm does this by constructing the AST for
such definitions on-the-fly. One strength of BodyFarm
is that all symbols and types referenced by synthesized function
bodies are contextual adapted to the containing translation unit.
The downside is that these ASTs are hardcoded in Clang's own
source code.
A more scalable model is to allow these models to be defined as source
code in separate "model" files and have the analyzer use those
definitions lazily when a function body is needed. Among other things,
it will allow more customization of the analyzer for specific APIs
and platforms.
This patch provides the initial infrastructure for this feature.
It extends BodyFarm to use an abstract API 'CodeInjector' that can be
used to synthesize function bodies. That 'CodeInjector' is
implemented using a new 'ModelInjector' in libFrontend, which lazily
parses a model file and injects the ASTs into the current translation
unit.
Models are currently found by specifying a 'model-path' as an
analyzer option; if no path is specified the CodeInjector is not
used, thus defaulting to the current behavior in the analyzer.
Models currently contain a single function definition, and can
be found by finding the file <function name>.model. This is an
initial starting point for something more rich, but it bootstraps
this feature for future evolution.
This patch was contributed by Gábor Horváth as part of his
Google Summer of Code project.
Some notes:
- This introduces the notion of a "model file" into
FrontendAction and the Preprocessor. This nomenclature
is specific to the static analyzer, but possibly could be
generalized. Essentially these are sources pulled in
exogenously from the principal translation.
Preprocessor gets a 'InitializeForModelFile' and
'FinalizeForModelFile' which could possibly be hoisted out
of Preprocessor if Preprocessor exposed a new API to
change the PragmaHandlers and some other internal pieces. This
can be revisited.
FrontendAction gets a 'isModelParsingAction()' predicate function
used to allow a new FrontendAction to recycle the Preprocessor
and ASTContext. This name could probably be made something
more general (i.e., not tied to 'model files') at the expense
of losing the intent of why it exists. This can be revisited.
- This is a moderate sized patch; it has gone through some amount of
offline code review. Most of the changes to the non-analyzer
parts are fairly small, and would make little sense without
the analyzer changes.
- Most of the analyzer changes are plumbing, with the interesting
behavior being introduced by ModelInjector.cpp and
ModelConsumer.cpp.
- The new functionality introduced by this change is off-by-default.
It requires an analyzer config option to enable.
llvm-svn: 216550
2014-08-27 23:14:15 +08:00
|
|
|
bool addCXXNewAllocator,
|
|
|
|
CodeInjector *injector)
|
|
|
|
: Injector(injector), SynthesizeBodies(synthesizeBodies)
|
2012-09-21 08:09:11 +08:00
|
|
|
{
|
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;
|
2017-07-12 15:04:19 +08:00
|
|
|
cfgBuildOptions.AddLifetime = addLifetime;
|
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
|
|
|
}
|
|
|
|
|
2016-10-11 00:26:44 +08:00
|
|
|
void AnalysisDeclContextManager::clear() { Contexts.clear(); }
|
2009-10-21 05:39:41 +08:00
|
|
|
|
Add support for the static analyzer to synthesize function implementations from external model files.
Currently the analyzer lazily models some functions using 'BodyFarm',
which constructs a fake function implementation that the analyzer
can simulate that approximates the semantics of the function when
it is called. BodyFarm does this by constructing the AST for
such definitions on-the-fly. One strength of BodyFarm
is that all symbols and types referenced by synthesized function
bodies are contextual adapted to the containing translation unit.
The downside is that these ASTs are hardcoded in Clang's own
source code.
A more scalable model is to allow these models to be defined as source
code in separate "model" files and have the analyzer use those
definitions lazily when a function body is needed. Among other things,
it will allow more customization of the analyzer for specific APIs
and platforms.
This patch provides the initial infrastructure for this feature.
It extends BodyFarm to use an abstract API 'CodeInjector' that can be
used to synthesize function bodies. That 'CodeInjector' is
implemented using a new 'ModelInjector' in libFrontend, which lazily
parses a model file and injects the ASTs into the current translation
unit.
Models are currently found by specifying a 'model-path' as an
analyzer option; if no path is specified the CodeInjector is not
used, thus defaulting to the current behavior in the analyzer.
Models currently contain a single function definition, and can
be found by finding the file <function name>.model. This is an
initial starting point for something more rich, but it bootstraps
this feature for future evolution.
This patch was contributed by Gábor Horváth as part of his
Google Summer of Code project.
Some notes:
- This introduces the notion of a "model file" into
FrontendAction and the Preprocessor. This nomenclature
is specific to the static analyzer, but possibly could be
generalized. Essentially these are sources pulled in
exogenously from the principal translation.
Preprocessor gets a 'InitializeForModelFile' and
'FinalizeForModelFile' which could possibly be hoisted out
of Preprocessor if Preprocessor exposed a new API to
change the PragmaHandlers and some other internal pieces. This
can be revisited.
FrontendAction gets a 'isModelParsingAction()' predicate function
used to allow a new FrontendAction to recycle the Preprocessor
and ASTContext. This name could probably be made something
more general (i.e., not tied to 'model files') at the expense
of losing the intent of why it exists. This can be revisited.
- This is a moderate sized patch; it has gone through some amount of
offline code review. Most of the changes to the non-analyzer
parts are fairly small, and would make little sense without
the analyzer changes.
- Most of the analyzer changes are plumbing, with the interesting
behavior being introduced by ModelInjector.cpp and
ModelConsumer.cpp.
- The new functionality introduced by this change is off-by-default.
It requires an analyzer config option to enable.
llvm-svn: 216550
2014-08-27 23:14:15 +08:00
|
|
|
static BodyFarm &getBodyFarm(ASTContext &C, CodeInjector *injector = nullptr) {
|
|
|
|
static BodyFarm *BF = new BodyFarm(C, injector);
|
2012-09-21 08:09:11 +08:00
|
|
|
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();
|
2017-05-25 10:16:53 +08:00
|
|
|
if (auto *CoroBody = dyn_cast_or_null<CoroutineBodyStmt>(Body))
|
|
|
|
Body = CoroBody->getBody();
|
2016-03-29 07:55:58 +08:00
|
|
|
if (Manager && Manager->synthesizeBodies()) {
|
|
|
|
Stmt *SynthesizedBody =
|
|
|
|
getBodyFarm(getASTContext(), Manager->Injector.get()).getBody(FD);
|
|
|
|
if (SynthesizedBody) {
|
|
|
|
Body = SynthesizedBody;
|
2014-01-11 04:06:06 +08:00
|
|
|
IsAutosynthesized = true;
|
2016-03-29 07:55:58 +08:00
|
|
|
}
|
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();
|
2016-03-29 07:55:58 +08:00
|
|
|
if (Manager && Manager->synthesizeBodies()) {
|
|
|
|
Stmt *SynthesizedBody =
|
|
|
|
getBodyFarm(getASTContext(), Manager->Injector.get()).getBody(MD);
|
|
|
|
if (SynthesizedBody) {
|
|
|
|
Body = SynthesizedBody;
|
2014-01-11 04:06:06 +08:00
|
|
|
IsAutosynthesized = true;
|
2016-03-29 07:55:58 +08:00
|
|
|
}
|
2014-01-11 04:06:06 +08:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
Add support for the static analyzer to synthesize function implementations from external model files.
Currently the analyzer lazily models some functions using 'BodyFarm',
which constructs a fake function implementation that the analyzer
can simulate that approximates the semantics of the function when
it is called. BodyFarm does this by constructing the AST for
such definitions on-the-fly. One strength of BodyFarm
is that all symbols and types referenced by synthesized function
bodies are contextual adapted to the containing translation unit.
The downside is that these ASTs are hardcoded in Clang's own
source code.
A more scalable model is to allow these models to be defined as source
code in separate "model" files and have the analyzer use those
definitions lazily when a function body is needed. Among other things,
it will allow more customization of the analyzer for specific APIs
and platforms.
This patch provides the initial infrastructure for this feature.
It extends BodyFarm to use an abstract API 'CodeInjector' that can be
used to synthesize function bodies. That 'CodeInjector' is
implemented using a new 'ModelInjector' in libFrontend, which lazily
parses a model file and injects the ASTs into the current translation
unit.
Models are currently found by specifying a 'model-path' as an
analyzer option; if no path is specified the CodeInjector is not
used, thus defaulting to the current behavior in the analyzer.
Models currently contain a single function definition, and can
be found by finding the file <function name>.model. This is an
initial starting point for something more rich, but it bootstraps
this feature for future evolution.
This patch was contributed by Gábor Horváth as part of his
Google Summer of Code project.
Some notes:
- This introduces the notion of a "model file" into
FrontendAction and the Preprocessor. This nomenclature
is specific to the static analyzer, but possibly could be
generalized. Essentially these are sources pulled in
exogenously from the principal translation.
Preprocessor gets a 'InitializeForModelFile' and
'FinalizeForModelFile' which could possibly be hoisted out
of Preprocessor if Preprocessor exposed a new API to
change the PragmaHandlers and some other internal pieces. This
can be revisited.
FrontendAction gets a 'isModelParsingAction()' predicate function
used to allow a new FrontendAction to recycle the Preprocessor
and ASTContext. This name could probably be made something
more general (i.e., not tied to 'model files') at the expense
of losing the intent of why it exists. This can be revisited.
- This is a moderate sized patch; it has gone through some amount of
offline code review. Most of the changes to the non-analyzer
parts are fairly small, and would make little sense without
the analyzer changes.
- Most of the analyzer changes are plumbing, with the interesting
behavior being introduced by ModelInjector.cpp and
ModelConsumer.cpp.
- The new functionality introduced by this change is off-by-default.
It requires an analyzer config option to enable.
llvm-svn: 216550
2014-08-27 23:14:15 +08:00
|
|
|
bool AnalysisDeclContext::isBodyAutosynthesizedFromModelFile() const {
|
|
|
|
bool Tmp;
|
|
|
|
Stmt *Body = getBody(Tmp);
|
|
|
|
return Tmp && Body->getLocStart().isValid();
|
|
|
|
}
|
|
|
|
|
2016-02-24 06:26:04 +08:00
|
|
|
/// Returns true if \param VD is an Objective-C implicit 'self' parameter.
|
|
|
|
static bool isSelfDecl(const VarDecl *VD) {
|
|
|
|
return isa<ImplicitParamDecl>(VD) && VD->getName() == "self";
|
|
|
|
}
|
Add support for the static analyzer to synthesize function implementations from external model files.
Currently the analyzer lazily models some functions using 'BodyFarm',
which constructs a fake function implementation that the analyzer
can simulate that approximates the semantics of the function when
it is called. BodyFarm does this by constructing the AST for
such definitions on-the-fly. One strength of BodyFarm
is that all symbols and types referenced by synthesized function
bodies are contextual adapted to the containing translation unit.
The downside is that these ASTs are hardcoded in Clang's own
source code.
A more scalable model is to allow these models to be defined as source
code in separate "model" files and have the analyzer use those
definitions lazily when a function body is needed. Among other things,
it will allow more customization of the analyzer for specific APIs
and platforms.
This patch provides the initial infrastructure for this feature.
It extends BodyFarm to use an abstract API 'CodeInjector' that can be
used to synthesize function bodies. That 'CodeInjector' is
implemented using a new 'ModelInjector' in libFrontend, which lazily
parses a model file and injects the ASTs into the current translation
unit.
Models are currently found by specifying a 'model-path' as an
analyzer option; if no path is specified the CodeInjector is not
used, thus defaulting to the current behavior in the analyzer.
Models currently contain a single function definition, and can
be found by finding the file <function name>.model. This is an
initial starting point for something more rich, but it bootstraps
this feature for future evolution.
This patch was contributed by Gábor Horváth as part of his
Google Summer of Code project.
Some notes:
- This introduces the notion of a "model file" into
FrontendAction and the Preprocessor. This nomenclature
is specific to the static analyzer, but possibly could be
generalized. Essentially these are sources pulled in
exogenously from the principal translation.
Preprocessor gets a 'InitializeForModelFile' and
'FinalizeForModelFile' which could possibly be hoisted out
of Preprocessor if Preprocessor exposed a new API to
change the PragmaHandlers and some other internal pieces. This
can be revisited.
FrontendAction gets a 'isModelParsingAction()' predicate function
used to allow a new FrontendAction to recycle the Preprocessor
and ASTContext. This name could probably be made something
more general (i.e., not tied to 'model files') at the expense
of losing the intent of why it exists. This can be revisited.
- This is a moderate sized patch; it has gone through some amount of
offline code review. Most of the changes to the non-analyzer
parts are fairly small, and would make little sense without
the analyzer changes.
- Most of the analyzer changes are plumbing, with the interesting
behavior being introduced by ModelInjector.cpp and
ModelConsumer.cpp.
- The new functionality introduced by this change is off-by-default.
It requires an analyzer config option to enable.
llvm-svn: 216550
2014-08-27 23:14:15 +08:00
|
|
|
|
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.
|
2014-03-15 02:34:04 +08:00
|
|
|
for (const auto &I : BD->captures()) {
|
|
|
|
const VarDecl *VD = I.getVariable();
|
2016-02-24 06:26:04 +08:00
|
|
|
if (isSelfDecl(VD))
|
2011-11-15 03:36:08 +08:00
|
|
|
return dyn_cast<ImplicitParamDecl>(VD);
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2015-11-16 01:48:22 +08:00
|
|
|
auto *CXXMethod = dyn_cast<CXXMethodDecl>(D);
|
|
|
|
if (!CXXMethod)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const CXXRecordDecl *parent = CXXMethod->getParent();
|
|
|
|
if (!parent->isLambda())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
for (const LambdaCapture &LC : parent->captures()) {
|
|
|
|
if (!LC.capturesVariable())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
VarDecl *VD = LC.getCapturedVar();
|
2016-02-24 06:26:04 +08:00
|
|
|
if (isSelfDecl(VD))
|
2015-11-16 01:48:22 +08:00
|
|
|
return dyn_cast<ImplicitParamDecl>(VD);
|
|
|
|
}
|
|
|
|
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2009-08-22 07:25:54 +08:00
|
|
|
}
|
|
|
|
|
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) {
|
2014-08-30 02:53:26 +08:00
|
|
|
cfg = 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);
|
2014-04-15 08:57:50 +08:00
|
|
|
|
2014-04-15 09:06:38 +08:00
|
|
|
// The Observer should only observe one build of the CFG.
|
2014-05-20 12:30:07 +08:00
|
|
|
getCFGBuildOptions().Observer = nullptr;
|
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);
|
2014-08-30 02:53:26 +08:00
|
|
|
completeCFG =
|
|
|
|
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);
|
2014-04-15 08:57:50 +08:00
|
|
|
|
2014-04-15 09:06:38 +08:00
|
|
|
// The Observer should only observe one build of the CFG.
|
2014-05-20 12:30:07 +08:00
|
|
|
getCFGBuildOptions().Observer = nullptr;
|
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
|
|
|
}
|
2014-05-20 12:30:07 +08:00
|
|
|
|
|
|
|
return nullptr;
|
2011-02-23 09:51:53 +08:00
|
|
|
}
|
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
|
|
|
}
|
2014-05-20 12:30:07 +08:00
|
|
|
|
|
|
|
return nullptr;
|
2011-02-23 09:51:59 +08:00
|
|
|
}
|
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())) {
|
2014-03-14 01:34:31 +08:00
|
|
|
for (const auto *I : C->inits()) {
|
|
|
|
PM->addStmt(I->getInit());
|
2013-05-18 10:26:50 +08:00
|
|
|
}
|
|
|
|
}
|
[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;
|
|
|
|
}
|
|
|
|
|
2016-10-11 00:26:44 +08:00
|
|
|
std::unique_ptr<AnalysisDeclContext> &AC = Contexts[D];
|
2009-08-22 07:58:43 +08:00
|
|
|
if (!AC)
|
2016-10-11 00:26:44 +08:00
|
|
|
AC = llvm::make_unique<AnalysisDeclContext>(this, D, cfgBuildOptions);
|
|
|
|
return AC.get();
|
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);
|
|
|
|
}
|
|
|
|
|
2016-02-08 00:55:44 +08:00
|
|
|
bool AnalysisDeclContext::isInStdNamespace(const Decl *D) {
|
|
|
|
const DeclContext *DC = D->getDeclContext()->getEnclosingNamespaceContext();
|
|
|
|
const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
|
|
|
|
if (!ND)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while (const DeclContext *Parent = ND->getParent()) {
|
|
|
|
if (!isa<NamespaceDecl>(Parent))
|
|
|
|
break;
|
|
|
|
ND = cast<NamespaceDecl>(Parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ND->isStdNamespace();
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2009-12-08 06:05:27 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2009-11-26 10:31:33 +08:00
|
|
|
void VisitStmt(Stmt *S) {
|
2015-07-03 05:03:14 +08:00
|
|
|
for (Stmt *Child : S->children())
|
|
|
|
if (Child)
|
|
|
|
Visit(Child);
|
2009-11-26 10:31:33 +08:00
|
|
|
}
|
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()) {
|
2014-11-19 15:49:47 +08:00
|
|
|
if (Visited.insert(VD).second)
|
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.
|
2014-03-15 02:34:04 +08:00
|
|
|
for (const auto &CI : BD->captures()) {
|
|
|
|
BV->push_back(CI.getVariable(), BC);
|
2012-12-06 15:17:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2015-02-07 01:25:10 +08:00
|
|
|
llvm::iterator_range<AnalysisDeclContext::referenced_decls_iterator>
|
2011-10-24 09:32:45 +08:00
|
|
|
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
|
|
|
|
2015-02-07 01:25:10 +08:00
|
|
|
const DeclVec *V =
|
|
|
|
LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
|
|
|
|
return llvm::make_range(V->begin(), V->end());
|
2009-11-26 10:31:33 +08:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-10-20 21:23:58 +08:00
|
|
|
ManagedAnalysis::~ManagedAnalysis() {}
|
2011-10-08 06:21:02 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-10-11 00:26:44 +08:00
|
|
|
AnalysisDeclContextManager::~AnalysisDeclContextManager() {}
|
2009-12-04 09:28:56 +08:00
|
|
|
|
2015-10-20 21:23:58 +08:00
|
|
|
LocationContext::~LocationContext() {}
|
2009-12-04 09:28:56 +08:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|