2018-03-21 08:14:43 +08:00
|
|
|
//===- AnalysisDeclContext.cpp - Analysis context for Path Sens analysis --===//
|
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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2018-03-21 08:14:43 +08:00
|
|
|
// This file defines AnalysisDeclContext, a class that manages the analysis
|
|
|
|
// context data for path sensitive analysis.
|
2009-07-30 09:17:21 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-09-07 05:45:03 +08:00
|
|
|
#include "clang/Analysis/AnalysisDeclContext.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"
|
2018-03-21 08:14:43 +08:00
|
|
|
#include "clang/AST/DeclBase.h"
|
|
|
|
#include "clang/AST/DeclCXX.h"
|
2009-07-30 09:17:21 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2010-01-13 10:59:54 +08:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2018-03-21 08:14:43 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "clang/AST/LambdaCapture.h"
|
2009-07-30 09:17:21 +08:00
|
|
|
#include "clang/AST/ParentMap.h"
|
2018-03-21 08:14:43 +08:00
|
|
|
#include "clang/AST/PrettyPrinter.h"
|
|
|
|
#include "clang/AST/Stmt.h"
|
|
|
|
#include "clang/AST/StmtCXX.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"
|
2017-10-24 07:59:52 +08:00
|
|
|
#include "clang/Analysis/BodyFarm.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"
|
2018-03-21 08:14:43 +08:00
|
|
|
#include "clang/Basic/LLVM.h"
|
|
|
|
#include "clang/Basic/SourceLocation.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/FoldingSet.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2012-03-10 23:08:09 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2018-03-21 08:14:43 +08:00
|
|
|
#include "llvm/ADT/iterator_range.h"
|
|
|
|
#include "llvm/Support/Allocator.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/Compiler.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"
|
2018-03-21 08:14:43 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <memory>
|
2012-09-21 08:09:11 +08:00
|
|
|
|
2009-07-30 09:17:21 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2018-03-21 08:14:43 +08:00
|
|
|
using ManagedAnalysisMap = llvm::DenseMap<const void *, ManagedAnalysis *>;
|
2011-10-08 06:21:02 +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,
|
|
|
|
const CFG::BuildOptions &buildOptions)
|
2018-07-31 03:24:48 +08:00
|
|
|
: Manager(Mgr), D(d), cfgBuildOptions(buildOptions) {
|
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)
|
2018-07-31 03:24:48 +08:00
|
|
|
: Manager(Mgr), D(d) {
|
2011-07-21 13:22:47 +08:00
|
|
|
cfgBuildOptions.forcedBlkExprs = &forcedBlkExprs;
|
|
|
|
}
|
|
|
|
|
2017-10-24 07:59:52 +08:00
|
|
|
AnalysisDeclContextManager::AnalysisDeclContextManager(
|
|
|
|
ASTContext &ASTCtx, bool useUnoptimizedCFG, bool addImplicitDtors,
|
|
|
|
bool addInitializers, bool addTemporaryDtors, bool addLifetime,
|
2018-03-12 20:26:15 +08:00
|
|
|
bool addLoopExit, bool addScopes, bool synthesizeBodies,
|
|
|
|
bool addStaticInitBranch, bool addCXXNewAllocator,
|
2018-06-28 08:04:54 +08:00
|
|
|
bool addRichCXXConstructors, bool markElidedCXXConstructors,
|
|
|
|
CodeInjector *injector)
|
2017-11-01 10:29:04 +08:00
|
|
|
: Injector(injector), FunctionBodyFarm(ASTCtx, injector),
|
2017-11-01 09:36:01 +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;
|
2017-07-12 15:04:19 +08:00
|
|
|
cfgBuildOptions.AddLifetime = addLifetime;
|
2017-08-19 19:19:16 +08:00
|
|
|
cfgBuildOptions.AddLoopExit = addLoopExit;
|
2018-03-12 20:26:15 +08:00
|
|
|
cfgBuildOptions.AddScopes = addScopes;
|
2013-03-29 08:09:22 +08:00
|
|
|
cfgBuildOptions.AddStaticInitBranches = addStaticInitBranch;
|
2014-01-14 01:59:19 +08:00
|
|
|
cfgBuildOptions.AddCXXNewAllocator = addCXXNewAllocator;
|
[CFG] Add extra context to C++ constructor statement elements.
This patch adds a new CFGStmt sub-class, CFGConstructor, which replaces
the regular CFGStmt with CXXConstructExpr in it whenever the CFG has additional
information to provide regarding what sort of object is being constructed.
It is useful for figuring out what memory is initialized in client of the
CFG such as the Static Analyzer, which do not operate by recursive AST
traversal, but instead rely on the CFG to provide all the information when they
need it. Otherwise, the statement that triggers the construction and defines
what memory is being initialized would normally occur after the
construct-expression, and the client would need to peek to the next CFG element
or use statement parent map to understand the necessary facts about
the construct-expression.
As a proof of concept, CFGConstructors are added for new-expressions
and the respective test cases are provided to demonstrate how it works.
For now, the only additional data contained in the CFGConstructor element is
the "trigger statement", such as new-expression, which is the parent of the
constructor. It will be significantly expanded in later commits. The additional
data is organized as an auxiliary structure - the "construction context",
which is allocated separately from the CFGElement.
Differential Revision: https://reviews.llvm.org/D42672
llvm-svn: 324668
2018-02-09 06:58:15 +08:00
|
|
|
cfgBuildOptions.AddRichCXXConstructors = addRichCXXConstructors;
|
2018-06-28 08:04:54 +08:00
|
|
|
cfgBuildOptions.MarkElidedCXXConstructors = markElidedCXXConstructors;
|
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
|
|
|
|
2013-02-02 08:30:04 +08:00
|
|
|
Stmt *AnalysisDeclContext::getBody(bool &IsAutosynthesized) const {
|
2013-02-04 13:06:21 +08:00
|
|
|
IsAutosynthesized = false;
|
2018-03-21 08:14:43 +08:00
|
|
|
if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
|
2012-09-21 08:09:11 +08:00
|
|
|
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()) {
|
2017-11-01 09:36:01 +08:00
|
|
|
Stmt *SynthesizedBody = Manager->getBodyFarm().getBody(FD);
|
2016-03-29 07:55:58 +08:00
|
|
|
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;
|
|
|
|
}
|
2018-03-21 08:14:43 +08:00
|
|
|
else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
|
2014-01-11 04:06:06 +08:00
|
|
|
Stmt *Body = MD->getBody();
|
2016-03-29 07:55:58 +08:00
|
|
|
if (Manager && Manager->synthesizeBodies()) {
|
2017-11-01 09:36:01 +08:00
|
|
|
Stmt *SynthesizedBody = Manager->getBodyFarm().getBody(MD);
|
2016-03-29 07:55:58 +08:00
|
|
|
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;
|
2018-03-21 08:14:43 +08:00
|
|
|
} else if (const auto *BD = dyn_cast<BlockDecl>(D))
|
2009-12-05 04:34:55 +08:00
|
|
|
return BD->getBody();
|
2018-03-21 08:14:43 +08:00
|
|
|
else if (const auto *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D))
|
2010-01-13 10:59:54 +08:00
|
|
|
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);
|
2018-08-10 05:08:08 +08:00
|
|
|
return Tmp && Body->getBeginLoc().isValid();
|
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
|
|
|
}
|
|
|
|
|
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 {
|
2018-03-21 08:14:43 +08:00
|
|
|
if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
|
2009-08-22 07:25:54 +08:00
|
|
|
return MD->getSelfDecl();
|
2018-03-21 08:14:43 +08:00
|
|
|
if (const auto *BD = dyn_cast<BlockDecl>(D)) {
|
2011-11-15 03:36:08 +08:00
|
|
|
// 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);
|
2018-07-31 03:24:48 +08:00
|
|
|
}
|
2011-11-15 03:36:08 +08:00
|
|
|
}
|
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;
|
|
|
|
|
2018-03-21 08:14:43 +08:00
|
|
|
for (const auto &LC : parent->captures()) {
|
2015-11-16 01:48:22 +08:00
|
|
|
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'.
|
2018-03-21 08:14:43 +08:00
|
|
|
if (const auto *e = dyn_cast<Expr>(stmt))
|
2011-06-10 16:49:37 +08:00
|
|
|
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);
|
2018-03-21 08:14:43 +08:00
|
|
|
if (const auto *e = dyn_cast<Expr>(stmt))
|
2011-06-10 16:49:37 +08:00
|
|
|
stmt = e->IgnoreParens();
|
2018-07-31 03:24:48 +08:00
|
|
|
CFG::BuildOptions::ForcedBlkExprs::const_iterator itr =
|
2011-03-10 11:50:34 +08:00
|
|
|
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();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
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();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
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) {
|
2018-03-21 08:14:43 +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()));
|
2018-03-21 08:14:43 +08:00
|
|
|
if (const auto *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;
|
|
|
|
}
|
|
|
|
|
2012-04-28 09:58:08 +08:00
|
|
|
AnalysisDeclContext *AnalysisDeclContextManager::getContext(const Decl *D) {
|
2018-03-21 08:14:43 +08:00
|
|
|
if (const auto *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
|
|
|
|
2017-11-01 09:36:01 +08:00
|
|
|
BodyFarm &AnalysisDeclContextManager::getBodyFarm() { return FunctionBodyFarm; }
|
2017-10-24 07:59:52 +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,
|
2018-03-21 08:14:43 +08:00
|
|
|
const BlockDecl *BD,
|
2012-06-02 04:04:04 +08:00
|
|
|
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();
|
2018-03-21 08:14:43 +08:00
|
|
|
const auto *ND = dyn_cast<NamespaceDecl>(DC);
|
2016-02-08 00:55:44 +08:00
|
|
|
if (!ND)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while (const DeclContext *Parent = ND->getParent()) {
|
|
|
|
if (!isa<NamespaceDecl>(Parent))
|
|
|
|
break;
|
|
|
|
ND = cast<NamespaceDecl>(Parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ND->isStdNamespace();
|
|
|
|
}
|
|
|
|
|
2018-03-21 08:14:43 +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!");
|
2018-07-31 03:24:48 +08:00
|
|
|
return Manager->getLocationContextManager();
|
2011-10-23 10:31:52 +08:00
|
|
|
}
|
|
|
|
|
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;
|
2018-03-21 08:14:43 +08:00
|
|
|
auto *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;
|
2018-03-21 08:14:43 +08:00
|
|
|
auto *L =
|
2012-06-02 04:04:04 +08:00
|
|
|
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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-06-27 09:51:55 +08:00
|
|
|
const StackFrameContext *LocationContext::getStackFrame() const {
|
2009-12-08 06:05:27 +08:00
|
|
|
const LocationContext *LC = this;
|
|
|
|
while (LC) {
|
2018-03-21 08:14:43 +08:00
|
|
|
if (const auto *SFC = dyn_cast<StackFrameContext>(LC))
|
2009-12-08 06:05:27 +08:00
|
|
|
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 {
|
2018-06-27 09:51:55 +08:00
|
|
|
return getStackFrame()->inTopFrame();
|
2012-11-03 10:54:16 +08:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-02-09 06:24:38 +08:00
|
|
|
static void printLocation(raw_ostream &OS, const SourceManager &SM,
|
|
|
|
SourceLocation SLoc) {
|
|
|
|
if (SLoc.isFileID() && SM.isInMainFile(SLoc))
|
|
|
|
OS << "line " << SM.getExpansionLineNumber(SLoc);
|
|
|
|
else
|
|
|
|
SLoc.print(OS, SM);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocationContext::dumpStack(
|
|
|
|
raw_ostream &OS, StringRef Indent, const char *NL, const char *Sep,
|
|
|
|
std::function<void(const LocationContext *)> printMoreInfoPerContext) const {
|
2013-03-30 09:31:35 +08:00
|
|
|
ASTContext &Ctx = getAnalysisDeclContext()->getASTContext();
|
|
|
|
PrintingPolicy PP(Ctx.getLangOpts());
|
|
|
|
PP.TerseOutput = 1;
|
|
|
|
|
2018-02-09 06:24:38 +08:00
|
|
|
const SourceManager &SM =
|
|
|
|
getAnalysisDeclContext()->getASTContext().getSourceManager();
|
|
|
|
|
2013-03-30 09:31:35 +08:00
|
|
|
unsigned Frame = 0;
|
|
|
|
for (const LocationContext *LCtx = this; LCtx; LCtx = LCtx->getParent()) {
|
|
|
|
switch (LCtx->getKind()) {
|
|
|
|
case StackFrame:
|
2018-02-09 06:24:38 +08:00
|
|
|
OS << Indent << '#' << Frame << ' ';
|
|
|
|
++Frame;
|
2018-03-21 08:14:43 +08:00
|
|
|
if (const auto *D = dyn_cast<NamedDecl>(LCtx->getDecl()))
|
2018-02-09 06:24:38 +08:00
|
|
|
OS << "Calling " << D->getQualifiedNameAsString();
|
|
|
|
else
|
|
|
|
OS << "Calling anonymous code";
|
|
|
|
if (const Stmt *S = cast<StackFrameContext>(LCtx)->getCallSite()) {
|
|
|
|
OS << " at ";
|
2018-08-10 05:08:08 +08:00
|
|
|
printLocation(OS, SM, S->getBeginLoc());
|
2018-02-09 06:24:38 +08:00
|
|
|
}
|
2013-03-30 09:31:35 +08:00
|
|
|
break;
|
|
|
|
case Scope:
|
2018-02-09 06:24:38 +08:00
|
|
|
OS << "Entering scope";
|
2013-03-30 09:31:35 +08:00
|
|
|
break;
|
|
|
|
case Block:
|
2018-02-09 06:24:38 +08:00
|
|
|
OS << "Invoking block";
|
|
|
|
if (const Decl *D = cast<BlockInvocationContext>(LCtx)->getDecl()) {
|
|
|
|
OS << " defined at ";
|
2018-08-10 05:08:08 +08:00
|
|
|
printLocation(OS, SM, D->getBeginLoc());
|
2018-02-09 06:24:38 +08:00
|
|
|
}
|
2013-03-30 09:31:35 +08:00
|
|
|
break;
|
|
|
|
}
|
2018-02-09 06:24:38 +08:00
|
|
|
OS << NL;
|
|
|
|
|
|
|
|
printMoreInfoPerContext(LCtx);
|
2013-03-30 09:31:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-03-21 08:14:43 +08:00
|
|
|
|
2009-11-26 10:31:33 +08:00
|
|
|
class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
|
2018-03-21 08:14:43 +08:00
|
|
|
BumpVector<const VarDecl *> &BEVals;
|
2009-11-26 10:31:33 +08:00
|
|
|
BumpVectorContext &BC;
|
2018-03-21 08:14:43 +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)
|
2018-03-21 08:14:43 +08:00
|
|
|
: BEVals(bevals), BC(bc) {}
|
2010-03-10 08:18:11 +08:00
|
|
|
|
2009-11-26 10:31:33 +08:00
|
|
|
void VisitStmt(Stmt *S) {
|
2018-03-21 08:14:43 +08:00
|
|
|
for (auto *Child : S->children())
|
2015-07-03 05:03:14 +08:00
|
|
|
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.
|
2018-03-21 08:14:43 +08:00
|
|
|
if (const auto *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());
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-12-22 09:30:46 +08:00
|
|
|
void VisitPseudoObjectExpr(PseudoObjectExpr *PE) {
|
2018-07-31 03:24:48 +08:00
|
|
|
for (PseudoObjectExpr::semantics_iterator it = PE->semantics_begin(),
|
2011-12-22 09:30:46 +08:00
|
|
|
et = PE->semantics_end(); it != et; ++it) {
|
|
|
|
Expr *Semantic = *it;
|
2018-03-21 08:14:43 +08:00
|
|
|
if (auto *OVE = dyn_cast<OpaqueValueExpr>(Semantic))
|
2011-12-22 09:30:46 +08:00
|
|
|
Semantic = OVE->getSourceExpr();
|
|
|
|
Visit(Semantic);
|
|
|
|
}
|
|
|
|
}
|
2010-03-23 08:13:23 +08:00
|
|
|
};
|
2009-11-26 10:31:33 +08:00
|
|
|
|
2018-03-21 08:14:43 +08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
using DeclVec = BumpVector<const VarDecl *>;
|
2009-11-26 10:31:33 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-03-21 08:14:43 +08:00
|
|
|
ManagedAnalysis::~ManagedAnalysis() = default;
|
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
|
|
|
}
|
|
|
|
|
2018-03-21 08:14:43 +08:00
|
|
|
LocationContext::~LocationContext() = default;
|
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;
|
|
|
|
}
|
|
|
|
Contexts.clear();
|
|
|
|
}
|