mirror of
https://github.com/intel/llvm.git
synced 2026-02-03 10:39:35 +08:00
[analyzer] Add scope information to CFG
This patch adds two new CFG elements CFGScopeBegin and CFGScopeEnd that indicate when a local scope begins and ends respectively. We use first VarDecl declared in a scope to uniquely identify it and add CFGScopeBegin and CFGScopeEnd elements into corresponding basic blocks. Differential Revision: https://reviews.llvm.org/D16403 llvm-svn: 327258
This commit is contained in:
@@ -435,7 +435,9 @@ public:
|
||||
bool addImplicitDtors = false,
|
||||
bool addInitializers = false,
|
||||
bool addTemporaryDtors = false,
|
||||
bool addLifetime = false, bool addLoopExit = false,
|
||||
bool addLifetime = false,
|
||||
bool addLoopExit = false,
|
||||
bool addScopes = false,
|
||||
bool synthesizeBodies = false,
|
||||
bool addStaticInitBranches = false,
|
||||
bool addCXXNewAllocator = true,
|
||||
|
||||
@@ -57,6 +57,8 @@ public:
|
||||
enum Kind {
|
||||
// main kind
|
||||
Initializer,
|
||||
ScopeBegin,
|
||||
ScopeEnd,
|
||||
NewAllocator,
|
||||
LifetimeEnds,
|
||||
LoopExit,
|
||||
@@ -260,6 +262,55 @@ private:
|
||||
}
|
||||
};
|
||||
|
||||
/// Represents beginning of a scope implicitly generated
|
||||
/// by the compiler on encountering a CompoundStmt
|
||||
class CFGScopeBegin : public CFGElement {
|
||||
public:
|
||||
CFGScopeBegin() {}
|
||||
CFGScopeBegin(const VarDecl *VD, const Stmt *S)
|
||||
: CFGElement(ScopeBegin, VD, S) {}
|
||||
|
||||
// Get statement that triggered a new scope.
|
||||
const Stmt *getTriggerStmt() const {
|
||||
return static_cast<Stmt*>(Data2.getPointer());
|
||||
}
|
||||
|
||||
// Get VD that triggered a new scope.
|
||||
const VarDecl *getVarDecl() const {
|
||||
return static_cast<VarDecl *>(Data1.getPointer());
|
||||
}
|
||||
|
||||
private:
|
||||
friend class CFGElement;
|
||||
static bool isKind(const CFGElement &E) {
|
||||
Kind kind = E.getKind();
|
||||
return kind == ScopeBegin;
|
||||
}
|
||||
};
|
||||
|
||||
/// Represents end of a scope implicitly generated by
|
||||
/// the compiler after the last Stmt in a CompoundStmt's body
|
||||
class CFGScopeEnd : public CFGElement {
|
||||
public:
|
||||
CFGScopeEnd() {}
|
||||
CFGScopeEnd(const VarDecl *VD, const Stmt *S) : CFGElement(ScopeEnd, VD, S) {}
|
||||
|
||||
const VarDecl *getVarDecl() const {
|
||||
return static_cast<VarDecl *>(Data1.getPointer());
|
||||
}
|
||||
|
||||
const Stmt *getTriggerStmt() const {
|
||||
return static_cast<Stmt *>(Data2.getPointer());
|
||||
}
|
||||
|
||||
private:
|
||||
friend class CFGElement;
|
||||
static bool isKind(const CFGElement &E) {
|
||||
Kind kind = E.getKind();
|
||||
return kind == ScopeEnd;
|
||||
}
|
||||
};
|
||||
|
||||
/// CFGImplicitDtor - Represents C++ object destructor implicitly generated
|
||||
/// by compiler on various occasions.
|
||||
class CFGImplicitDtor : public CFGElement {
|
||||
@@ -799,6 +850,24 @@ public:
|
||||
Elements.push_back(CFGNewAllocator(NE), C);
|
||||
}
|
||||
|
||||
void appendScopeBegin(const VarDecl *VD, const Stmt *S,
|
||||
BumpVectorContext &C) {
|
||||
Elements.push_back(CFGScopeBegin(VD, S), C);
|
||||
}
|
||||
|
||||
void prependScopeBegin(const VarDecl *VD, const Stmt *S,
|
||||
BumpVectorContext &C) {
|
||||
Elements.insert(Elements.rbegin(), 1, CFGScopeBegin(VD, S), C);
|
||||
}
|
||||
|
||||
void appendScopeEnd(const VarDecl *VD, const Stmt *S, BumpVectorContext &C) {
|
||||
Elements.push_back(CFGScopeEnd(VD, S), C);
|
||||
}
|
||||
|
||||
void prependScopeEnd(const VarDecl *VD, const Stmt *S, BumpVectorContext &C) {
|
||||
Elements.insert(Elements.rbegin(), 1, CFGScopeEnd(VD, S), C);
|
||||
}
|
||||
|
||||
void appendBaseDtor(const CXXBaseSpecifier *BS, BumpVectorContext &C) {
|
||||
Elements.push_back(CFGBaseDtor(BS), C);
|
||||
}
|
||||
@@ -852,6 +921,19 @@ public:
|
||||
*I = CFGLifetimeEnds(VD, S);
|
||||
return ++I;
|
||||
}
|
||||
|
||||
// Scope leaving must be performed in reversed order. So insertion is in two
|
||||
// steps. First we prepare space for some number of elements, then we insert
|
||||
// the elements beginning at the last position in prepared space.
|
||||
iterator beginScopeEndInsert(iterator I, size_t Cnt, BumpVectorContext &C) {
|
||||
return iterator(
|
||||
Elements.insert(I.base(), Cnt, CFGScopeEnd(nullptr, nullptr), C));
|
||||
}
|
||||
iterator insertScopeEnd(iterator I, VarDecl *VD, Stmt *S) {
|
||||
*I = CFGScopeEnd(VD, S);
|
||||
return ++I;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
/// \brief CFGCallback defines methods that should be called when a logical
|
||||
@@ -894,6 +976,7 @@ public:
|
||||
bool AddLifetime = false;
|
||||
bool AddLoopExit = false;
|
||||
bool AddTemporaryDtors = false;
|
||||
bool AddScopes = false;
|
||||
bool AddStaticInitBranches = false;
|
||||
bool AddCXXNewAllocator = false;
|
||||
bool AddCXXDefaultInitExprInCtors = false;
|
||||
|
||||
@@ -240,6 +240,9 @@ private:
|
||||
/// \sa mayInlineCXXStandardLibrary
|
||||
Optional<bool> InlineCXXStandardLibrary;
|
||||
|
||||
/// \sa includeScopesInCFG
|
||||
Optional<bool> IncludeScopesInCFG;
|
||||
|
||||
/// \sa mayInlineTemplateFunctions
|
||||
Optional<bool> InlineTemplateFunctions;
|
||||
|
||||
@@ -481,6 +484,12 @@ public:
|
||||
/// which accepts the values "true" and "false".
|
||||
bool includeRichConstructorsInCFG();
|
||||
|
||||
/// Returns whether or not scope information should be included in the CFG.
|
||||
///
|
||||
/// This is controlled by the 'cfg-scope-info' config option, which accepts
|
||||
/// the values "true" and "false".
|
||||
bool includeScopesInCFG();
|
||||
|
||||
/// Returns whether or not C++ standard library functions may be considered
|
||||
/// for inlining.
|
||||
///
|
||||
|
||||
@@ -66,9 +66,9 @@ AnalysisDeclContext::AnalysisDeclContext(AnalysisDeclContextManager *Mgr,
|
||||
AnalysisDeclContextManager::AnalysisDeclContextManager(
|
||||
ASTContext &ASTCtx, bool useUnoptimizedCFG, bool addImplicitDtors,
|
||||
bool addInitializers, bool addTemporaryDtors, bool addLifetime,
|
||||
bool addLoopExit, bool synthesizeBodies, bool addStaticInitBranch,
|
||||
bool addCXXNewAllocator, bool addRichCXXConstructors,
|
||||
CodeInjector *injector)
|
||||
bool addLoopExit, bool addScopes, bool synthesizeBodies,
|
||||
bool addStaticInitBranch, bool addCXXNewAllocator,
|
||||
bool addRichCXXConstructors, CodeInjector *injector)
|
||||
: Injector(injector), FunctionBodyFarm(ASTCtx, injector),
|
||||
SynthesizeBodies(synthesizeBodies) {
|
||||
cfgBuildOptions.PruneTriviallyFalseEdges = !useUnoptimizedCFG;
|
||||
@@ -77,6 +77,7 @@ AnalysisDeclContextManager::AnalysisDeclContextManager(
|
||||
cfgBuildOptions.AddTemporaryDtors = addTemporaryDtors;
|
||||
cfgBuildOptions.AddLifetime = addLifetime;
|
||||
cfgBuildOptions.AddLoopExit = addLoopExit;
|
||||
cfgBuildOptions.AddScopes = addScopes;
|
||||
cfgBuildOptions.AddStaticInitBranches = addStaticInitBranch;
|
||||
cfgBuildOptions.AddCXXNewAllocator = addCXXNewAllocator;
|
||||
cfgBuildOptions.AddRichCXXConstructors = addRichCXXConstructors;
|
||||
|
||||
@@ -234,6 +234,13 @@ public:
|
||||
assert(VarIter != 0 && "Iterator has invalid value of VarIter member");
|
||||
return &Scope->Vars[VarIter - 1];
|
||||
}
|
||||
|
||||
const VarDecl *getFirstVarInScope() const {
|
||||
assert(Scope && "Dereferencing invalid iterator is not allowed");
|
||||
assert(VarIter != 0 && "Iterator has invalid value of VarIter member");
|
||||
return Scope->Vars[0];
|
||||
}
|
||||
|
||||
VarDecl *operator*() const {
|
||||
return *this->operator->();
|
||||
}
|
||||
@@ -267,6 +274,7 @@ public:
|
||||
|
||||
int distance(const_iterator L);
|
||||
const_iterator shared_parent(const_iterator L);
|
||||
bool pointsToFirstDeclaredVar() { return VarIter == 1; }
|
||||
};
|
||||
|
||||
private:
|
||||
@@ -479,6 +487,9 @@ class CFGBuilder {
|
||||
llvm::DenseMap<CXXConstructExpr *, const ConstructionContextLayer *>
|
||||
ConstructionContextMap;
|
||||
|
||||
using DeclsWithEndedScopeSetTy = llvm::SmallSetVector<VarDecl *, 16>;
|
||||
DeclsWithEndedScopeSetTy DeclsWithEndedScope;
|
||||
|
||||
bool badCFG = false;
|
||||
const CFG::BuildOptions &BuildOpts;
|
||||
|
||||
@@ -576,6 +587,12 @@ private:
|
||||
CFGBlock *VisitChildren(Stmt *S);
|
||||
CFGBlock *VisitNoRecurse(Expr *E, AddStmtChoice asc);
|
||||
|
||||
void maybeAddScopeBeginForVarDecl(CFGBlock *B, const VarDecl *VD,
|
||||
const Stmt *S) {
|
||||
if (ScopePos && (VD == ScopePos.getFirstVarInScope()))
|
||||
appendScopeBegin(B, VD, S);
|
||||
}
|
||||
|
||||
/// When creating the CFG for temporary destructors, we want to mirror the
|
||||
/// branch structure of the corresponding constructor calls.
|
||||
/// Thus, while visiting a statement for temporary destructors, we keep a
|
||||
@@ -688,6 +705,11 @@ private:
|
||||
void addAutomaticObjHandling(LocalScope::const_iterator B,
|
||||
LocalScope::const_iterator E, Stmt *S);
|
||||
void addImplicitDtorsForDestructor(const CXXDestructorDecl *DD);
|
||||
void addScopesEnd(LocalScope::const_iterator B, LocalScope::const_iterator E,
|
||||
Stmt *S);
|
||||
|
||||
void getDeclsWithEndedScope(LocalScope::const_iterator B,
|
||||
LocalScope::const_iterator E, Stmt *S);
|
||||
|
||||
// Local scopes creation.
|
||||
LocalScope* createOrReuseLocalScope(LocalScope* Scope);
|
||||
@@ -770,6 +792,11 @@ private:
|
||||
LocalScope::const_iterator B,
|
||||
LocalScope::const_iterator E);
|
||||
|
||||
const VarDecl *
|
||||
prependAutomaticObjScopeEndWithTerminator(CFGBlock *Blk,
|
||||
LocalScope::const_iterator B,
|
||||
LocalScope::const_iterator E);
|
||||
|
||||
void addSuccessor(CFGBlock *B, CFGBlock *S, bool IsReachable = true) {
|
||||
B->addSuccessor(CFGBlock::AdjacentBlock(S, IsReachable),
|
||||
cfg->getBumpVectorContext());
|
||||
@@ -782,6 +809,26 @@ private:
|
||||
cfg->getBumpVectorContext());
|
||||
}
|
||||
|
||||
void appendScopeBegin(CFGBlock *B, const VarDecl *VD, const Stmt *S) {
|
||||
if (BuildOpts.AddScopes)
|
||||
B->appendScopeBegin(VD, S, cfg->getBumpVectorContext());
|
||||
}
|
||||
|
||||
void prependScopeBegin(CFGBlock *B, const VarDecl *VD, const Stmt *S) {
|
||||
if (BuildOpts.AddScopes)
|
||||
B->prependScopeBegin(VD, S, cfg->getBumpVectorContext());
|
||||
}
|
||||
|
||||
void appendScopeEnd(CFGBlock *B, const VarDecl *VD, const Stmt *S) {
|
||||
if (BuildOpts.AddScopes)
|
||||
B->appendScopeEnd(VD, S, cfg->getBumpVectorContext());
|
||||
}
|
||||
|
||||
void prependScopeEnd(CFGBlock *B, const VarDecl *VD, const Stmt *S) {
|
||||
if (BuildOpts.AddScopes)
|
||||
B->prependScopeEnd(VD, S, cfg->getBumpVectorContext());
|
||||
}
|
||||
|
||||
/// \brief Find a relational comparison with an expression evaluating to a
|
||||
/// boolean and a constant other than 0 and 1.
|
||||
/// e.g. if ((x < y) == 10)
|
||||
@@ -1299,6 +1346,9 @@ std::unique_ptr<CFG> CFGBuilder::buildCFG(const Decl *D, Stmt *Statement) {
|
||||
JT.scopePosition);
|
||||
prependAutomaticObjDtorsWithTerminator(B, I->scopePosition,
|
||||
JT.scopePosition);
|
||||
const VarDecl *VD = prependAutomaticObjScopeEndWithTerminator(
|
||||
B, I->scopePosition, JT.scopePosition);
|
||||
appendScopeBegin(JT.block, VD, G);
|
||||
addSuccessor(B, JT.block);
|
||||
}
|
||||
|
||||
@@ -1456,9 +1506,34 @@ void CFGBuilder::addLoopExit(const Stmt *LoopStmt){
|
||||
appendLoopExit(Block, LoopStmt);
|
||||
}
|
||||
|
||||
void CFGBuilder::getDeclsWithEndedScope(LocalScope::const_iterator B,
|
||||
LocalScope::const_iterator E, Stmt *S) {
|
||||
if (!BuildOpts.AddScopes)
|
||||
return;
|
||||
|
||||
if (B == E)
|
||||
return;
|
||||
|
||||
// To go from B to E, one first goes up the scopes from B to P
|
||||
// then sideways in one scope from P to P' and then down
|
||||
// the scopes from P' to E.
|
||||
// The lifetime of all objects between B and P end.
|
||||
LocalScope::const_iterator P = B.shared_parent(E);
|
||||
int Dist = B.distance(P);
|
||||
if (Dist <= 0)
|
||||
return;
|
||||
|
||||
for (LocalScope::const_iterator I = B; I != P; ++I)
|
||||
if (I.pointsToFirstDeclaredVar())
|
||||
DeclsWithEndedScope.insert(*I);
|
||||
}
|
||||
|
||||
void CFGBuilder::addAutomaticObjHandling(LocalScope::const_iterator B,
|
||||
LocalScope::const_iterator E,
|
||||
Stmt *S) {
|
||||
getDeclsWithEndedScope(B, E, S);
|
||||
if (BuildOpts.AddScopes)
|
||||
addScopesEnd(B, E, S);
|
||||
if (BuildOpts.AddImplicitDtors)
|
||||
addAutomaticObjDtors(B, E, S);
|
||||
if (BuildOpts.AddLifetime)
|
||||
@@ -1510,6 +1585,23 @@ void CFGBuilder::addLifetimeEnds(LocalScope::const_iterator B,
|
||||
appendLifetimeEnds(Block, *I, S);
|
||||
}
|
||||
|
||||
/// Add to current block markers for ending scopes.
|
||||
void CFGBuilder::addScopesEnd(LocalScope::const_iterator B,
|
||||
LocalScope::const_iterator E, Stmt *S) {
|
||||
// If implicit destructors are enabled, we'll add scope ends in
|
||||
// addAutomaticObjDtors.
|
||||
if (BuildOpts.AddImplicitDtors)
|
||||
return;
|
||||
|
||||
autoCreateBlock();
|
||||
|
||||
for (auto I = DeclsWithEndedScope.rbegin(), E = DeclsWithEndedScope.rend();
|
||||
I != E; ++I)
|
||||
appendScopeEnd(Block, *I, S);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/// addAutomaticObjDtors - Add to current block automatic objects destructors
|
||||
/// for objects in range of local scope positions. Use S as trigger statement
|
||||
/// for destructors.
|
||||
@@ -1533,6 +1625,15 @@ void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B,
|
||||
for (SmallVectorImpl<VarDecl*>::reverse_iterator I = Decls.rbegin(),
|
||||
E = Decls.rend();
|
||||
I != E; ++I) {
|
||||
if (hasTrivialDestructor(*I)) {
|
||||
// If AddScopes is enabled and *I is a first variable in a scope, add a
|
||||
// ScopeEnd marker in a Block.
|
||||
if (BuildOpts.AddScopes && DeclsWithEndedScope.count(*I)) {
|
||||
autoCreateBlock();
|
||||
appendScopeEnd(Block, *I, S);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
// If this destructor is marked as a no-return destructor, we need to
|
||||
// create a new block for the destructor which does not have as a successor
|
||||
// anything built thus far: control won't flow out of this block.
|
||||
@@ -1547,6 +1648,9 @@ void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B,
|
||||
else
|
||||
autoCreateBlock();
|
||||
|
||||
// Add ScopeEnd just after automatic obj destructor.
|
||||
if (BuildOpts.AddScopes && DeclsWithEndedScope.count(*I))
|
||||
appendScopeEnd(Block, *I, S);
|
||||
appendAutomaticObjDtor(Block, *I, S);
|
||||
}
|
||||
}
|
||||
@@ -1609,7 +1713,8 @@ LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) {
|
||||
/// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement
|
||||
/// that should create implicit scope (e.g. if/else substatements).
|
||||
void CFGBuilder::addLocalScopeForStmt(Stmt *S) {
|
||||
if (!BuildOpts.AddImplicitDtors && !BuildOpts.AddLifetime)
|
||||
if (!BuildOpts.AddImplicitDtors && !BuildOpts.AddLifetime &&
|
||||
!BuildOpts.AddScopes)
|
||||
return;
|
||||
|
||||
LocalScope *Scope = nullptr;
|
||||
@@ -1634,7 +1739,8 @@ void CFGBuilder::addLocalScopeForStmt(Stmt *S) {
|
||||
/// reuse Scope if not NULL.
|
||||
LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt *DS,
|
||||
LocalScope* Scope) {
|
||||
if (!BuildOpts.AddImplicitDtors && !BuildOpts.AddLifetime)
|
||||
if (!BuildOpts.AddImplicitDtors && !BuildOpts.AddLifetime &&
|
||||
!BuildOpts.AddScopes)
|
||||
return Scope;
|
||||
|
||||
for (auto *DI : DS->decls())
|
||||
@@ -1686,7 +1792,8 @@ LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl *VD,
|
||||
LocalScope* Scope) {
|
||||
assert(!(BuildOpts.AddImplicitDtors && BuildOpts.AddLifetime) &&
|
||||
"AddImplicitDtors and AddLifetime cannot be used at the same time");
|
||||
if (!BuildOpts.AddImplicitDtors && !BuildOpts.AddLifetime)
|
||||
if (!BuildOpts.AddImplicitDtors && !BuildOpts.AddLifetime &&
|
||||
!BuildOpts.AddScopes)
|
||||
return Scope;
|
||||
|
||||
// Check if variable is local.
|
||||
@@ -1699,7 +1806,7 @@ LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl *VD,
|
||||
}
|
||||
|
||||
if (BuildOpts.AddImplicitDtors) {
|
||||
if (!hasTrivialDestructor(VD)) {
|
||||
if (!hasTrivialDestructor(VD) || BuildOpts.AddScopes) {
|
||||
// Add the variable to scope
|
||||
Scope = createOrReuseLocalScope(Scope);
|
||||
Scope->addVar(VD);
|
||||
@@ -1759,6 +1866,26 @@ void CFGBuilder::prependAutomaticObjLifetimeWithTerminator(
|
||||
InsertPos = Blk->insertLifetimeEnds(InsertPos, *I, Blk->getTerminator());
|
||||
}
|
||||
|
||||
/// prependAutomaticObjScopeEndWithTerminator - Prepend scope end CFGElements for
|
||||
/// variables with automatic storage duration to CFGBlock's elements vector.
|
||||
/// Elements will be prepended to physical beginning of the vector which
|
||||
/// happens to be logical end. Use blocks terminator as statement that specifies
|
||||
/// where scope ends.
|
||||
const VarDecl *
|
||||
CFGBuilder::prependAutomaticObjScopeEndWithTerminator(
|
||||
CFGBlock *Blk, LocalScope::const_iterator B, LocalScope::const_iterator E) {
|
||||
if (!BuildOpts.AddScopes)
|
||||
return nullptr;
|
||||
BumpVectorContext &C = cfg->getBumpVectorContext();
|
||||
CFGBlock::iterator InsertPos =
|
||||
Blk->beginScopeEndInsert(Blk->end(), 1, C);
|
||||
LocalScope::const_iterator PlaceToInsert = B;
|
||||
for (LocalScope::const_iterator I = B; I != E; ++I)
|
||||
PlaceToInsert = I;
|
||||
Blk->insertScopeEnd(InsertPos, *PlaceToInsert, Blk->getTerminator());
|
||||
return *PlaceToInsert;
|
||||
}
|
||||
|
||||
/// Visit - Walk the subtree of a statement and add extra
|
||||
/// blocks for ternary operators, &&, and ||. We also process "," and
|
||||
/// DeclStmts (which may contain nested control-flow).
|
||||
@@ -2492,6 +2619,8 @@ CFGBlock *CFGBuilder::VisitDeclSubExpr(DeclStmt *DS) {
|
||||
LastBlock = newBlock;
|
||||
}
|
||||
|
||||
maybeAddScopeBeginForVarDecl(Block, VD, DS);
|
||||
|
||||
// Remove variable from local scope.
|
||||
if (ScopePos && VD == *ScopePos)
|
||||
++ScopePos;
|
||||
@@ -2956,6 +3085,7 @@ CFGBlock *CFGBuilder::VisitForStmt(ForStmt *F) {
|
||||
|
||||
do {
|
||||
Expr *C = F->getCond();
|
||||
SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
|
||||
|
||||
// Specially handle logical operators, which have a slightly
|
||||
// more optimal CFG representation.
|
||||
@@ -2989,6 +3119,7 @@ CFGBlock *CFGBuilder::VisitForStmt(ForStmt *F) {
|
||||
appendStmt(Block, F->getConditionVariableDeclStmt());
|
||||
EntryConditionBlock = addStmt(Init);
|
||||
assert(Block == EntryConditionBlock);
|
||||
maybeAddScopeBeginForVarDecl(EntryConditionBlock, VD, C);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3015,6 +3146,8 @@ CFGBlock *CFGBuilder::VisitForStmt(ForStmt *F) {
|
||||
// If the loop contains initialization, create a new block for those
|
||||
// statements. This block can also contain statements that precede the loop.
|
||||
if (Stmt *I = F->getInit()) {
|
||||
SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
|
||||
ScopePos = LoopBeginScopePos;
|
||||
Block = createBlock();
|
||||
return addStmt(I);
|
||||
}
|
||||
@@ -3311,6 +3444,7 @@ CFGBlock *CFGBuilder::VisitWhileStmt(WhileStmt *W) {
|
||||
appendStmt(Block, W->getConditionVariableDeclStmt());
|
||||
EntryConditionBlock = addStmt(Init);
|
||||
assert(Block == EntryConditionBlock);
|
||||
maybeAddScopeBeginForVarDecl(EntryConditionBlock, VD, C);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3636,6 +3770,7 @@ CFGBlock *CFGBuilder::VisitSwitchStmt(SwitchStmt *Terminator) {
|
||||
autoCreateBlock();
|
||||
appendStmt(Block, Terminator->getConditionVariableDeclStmt());
|
||||
LastBlock = addStmt(Init);
|
||||
maybeAddScopeBeginForVarDecl(LastBlock, VD, Init);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4381,6 +4516,8 @@ CFGImplicitDtor::getDestructorDecl(ASTContext &astContext) const {
|
||||
case CFGElement::LifetimeEnds:
|
||||
case CFGElement::Statement:
|
||||
case CFGElement::Constructor:
|
||||
case CFGElement::ScopeBegin:
|
||||
case CFGElement::ScopeEnd:
|
||||
llvm_unreachable("getDestructorDecl should only be used with "
|
||||
"ImplicitDtors");
|
||||
case CFGElement::AutomaticObjectDtor: {
|
||||
@@ -4841,6 +4978,16 @@ static void print_elem(raw_ostream &OS, StmtPrinterHelper &Helper,
|
||||
} else if (Optional<CFGLoopExit> LE = E.getAs<CFGLoopExit>()) {
|
||||
const Stmt *LoopStmt = LE->getLoopStmt();
|
||||
OS << LoopStmt->getStmtClassName() << " (LoopExit)\n";
|
||||
} else if (Optional<CFGScopeBegin> SB = E.getAs<CFGScopeBegin>()) {
|
||||
OS << "CFGScopeBegin(";
|
||||
if (const VarDecl *VD = SB->getVarDecl())
|
||||
OS << VD->getQualifiedNameAsString();
|
||||
OS << ")\n";
|
||||
} else if (Optional<CFGScopeEnd> SE = E.getAs<CFGScopeEnd>()) {
|
||||
OS << "CFGScopeEnd(";
|
||||
if (const VarDecl *VD = SE->getVarDecl())
|
||||
OS << VD->getQualifiedNameAsString();
|
||||
OS << ")\n";
|
||||
} else if (Optional<CFGNewAllocator> NE = E.getAs<CFGNewAllocator>()) {
|
||||
OS << "CFGNewAllocator(";
|
||||
if (const CXXNewExpr *AllocExpr = NE->getAllocatorExpr())
|
||||
|
||||
@@ -26,6 +26,7 @@ AnalysisManager::AnalysisManager(
|
||||
// Adding LoopExit elements to the CFG is a requirement for loop
|
||||
// unrolling.
|
||||
Options.includeLoopExitInCFG() || Options.shouldUnrollLoops(),
|
||||
Options.includeScopesInCFG(),
|
||||
Options.shouldSynthesizeBodies(),
|
||||
Options.shouldConditionalizeStaticInitializers(),
|
||||
/*addCXXNewAllocator=*/true,
|
||||
|
||||
@@ -224,6 +224,12 @@ bool AnalyzerOptions::includeRichConstructorsInCFG() {
|
||||
/* Default = */ true);
|
||||
}
|
||||
|
||||
bool AnalyzerOptions::includeScopesInCFG() {
|
||||
return getBooleanOption(IncludeScopesInCFG,
|
||||
"cfg-scopes",
|
||||
/* Default = */ false);
|
||||
}
|
||||
|
||||
bool AnalyzerOptions::mayInlineCXXStandardLibrary() {
|
||||
return getBooleanOption(InlineCXXStandardLibrary,
|
||||
"c++-stdlib-inlining",
|
||||
|
||||
@@ -632,6 +632,8 @@ void ExprEngine::processCFGElement(const CFGElement E, ExplodedNode *Pred,
|
||||
ProcessLoopExit(E.castAs<CFGLoopExit>().getLoopStmt(), Pred);
|
||||
return;
|
||||
case CFGElement::LifetimeEnds:
|
||||
case CFGElement::ScopeBegin:
|
||||
case CFGElement::ScopeEnd:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -614,6 +614,9 @@ getLocationForCaller(const StackFrameContext *SFC,
|
||||
return PathDiagnosticLocation::createEnd(Dtor.getBindTemporaryExpr(), SM,
|
||||
CallerCtx);
|
||||
}
|
||||
case CFGElement::ScopeBegin:
|
||||
case CFGElement::ScopeEnd:
|
||||
llvm_unreachable("not yet implemented!");
|
||||
case CFGElement::LifetimeEnds:
|
||||
case CFGElement::LoopExit:
|
||||
llvm_unreachable("CFGElement kind should not be on callsite!");
|
||||
|
||||
@@ -16,6 +16,7 @@ void foo() {
|
||||
// CHECK-NEXT: cfg-lifetime = false
|
||||
// CHECK-NEXT: cfg-loopexit = false
|
||||
// CHECK-NEXT: cfg-rich-constructors = true
|
||||
// CHECK-NEXT: cfg-scopes = false
|
||||
// CHECK-NEXT: cfg-temporary-dtors = true
|
||||
// CHECK-NEXT: exploration_strategy = unexplored_first_queue
|
||||
// CHECK-NEXT: faux-bodies = true
|
||||
@@ -34,4 +35,4 @@ void foo() {
|
||||
// CHECK-NEXT: unroll-loops = false
|
||||
// CHECK-NEXT: widen-loops = false
|
||||
// CHECK-NEXT: [stats]
|
||||
// CHECK-NEXT: num-entries = 22
|
||||
// CHECK-NEXT: num-entries = 23
|
||||
|
||||
@@ -30,6 +30,7 @@ public:
|
||||
// CHECK-NEXT: cfg-lifetime = false
|
||||
// CHECK-NEXT: cfg-loopexit = false
|
||||
// CHECK-NEXT: cfg-rich-constructors = true
|
||||
// CHECK-NEXT: cfg-scopes = false
|
||||
// CHECK-NEXT: cfg-temporary-dtors = true
|
||||
// CHECK-NEXT: experimental-enable-naive-ctu-analysis = false
|
||||
// CHECK-NEXT: exploration_strategy = unexplored_first_queue
|
||||
@@ -49,4 +50,4 @@ public:
|
||||
// CHECK-NEXT: unroll-loops = false
|
||||
// CHECK-NEXT: widen-loops = false
|
||||
// CHECK-NEXT: [stats]
|
||||
// CHECK-NEXT: num-entries = 29
|
||||
// CHECK-NEXT: num-entries = 30
|
||||
|
||||
1171
clang/test/Analysis/scopes-cfg-output.cpp
Normal file
1171
clang/test/Analysis/scopes-cfg-output.cpp
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user