opnsense-src/include/clang/Checker/PathSensitive/ExplodedGraph.h

433 lines
12 KiB
C
Raw Normal View History

2009-06-02 13:58:47 -04:00
//=-- ExplodedGraph.h - Local, Path-Sens. "Exploded Graph" -*- C++ -*-------==//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the template classes ExplodedNode and ExplodedGraph,
// which represent a path-sensitive, intra-procedural "exploded graph."
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_ANALYSIS_EXPLODEDGRAPH
#define LLVM_CLANG_ANALYSIS_EXPLODEDGRAPH
#include "clang/Analysis/ProgramPoint.h"
2010-02-16 04:31:36 -05:00
#include "clang/Analysis/AnalysisContext.h"
2009-06-02 13:58:47 -04:00
#include "clang/AST/Decl.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Support/Allocator.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/GraphTraits.h"
#include "llvm/ADT/DepthFirstIterator.h"
#include "llvm/Support/Casting.h"
2009-10-14 14:03:49 -04:00
#include "clang/Analysis/Support/BumpVector.h"
2009-06-02 13:58:47 -04:00
namespace clang {
2009-10-14 14:03:49 -04:00
class GRState;
2009-06-02 13:58:47 -04:00
class CFG;
class ASTContext;
2009-10-14 14:03:49 -04:00
class ExplodedGraph;
2009-06-02 13:58:47 -04:00
//===----------------------------------------------------------------------===//
// ExplodedGraph "implementation" classes. These classes are not typed to
// contain a specific kind of state. Typed-specialized versions are defined
// on top of these classes.
//===----------------------------------------------------------------------===//
2009-10-14 14:03:49 -04:00
class ExplodedNode : public llvm::FoldingSetNode {
friend class ExplodedGraph;
friend class GRCoreEngine;
friend class GRStmtNodeBuilder;
friend class GRBranchNodeBuilder;
friend class GRIndirectGotoNodeBuilder;
friend class GRSwitchNodeBuilder;
friend class GREndPathNodeBuilder;
2009-06-02 13:58:47 -04:00
class NodeGroup {
enum { Size1 = 0x0, SizeOther = 0x1, AuxFlag = 0x2, Mask = 0x3 };
uintptr_t P;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
unsigned getKind() const {
return P & 0x1;
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
void* getPtr() const {
assert (!getFlag());
return reinterpret_cast<void*>(P & ~Mask);
}
2009-10-14 14:03:49 -04:00
ExplodedNode *getNode() const {
return reinterpret_cast<ExplodedNode*>(getPtr());
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
public:
NodeGroup() : P(0) {}
2009-10-14 14:03:49 -04:00
ExplodedNode **begin() const;
ExplodedNode **end() const;
2009-06-02 13:58:47 -04:00
unsigned size() const;
2009-10-14 14:03:49 -04:00
bool empty() const { return (P & ~Mask) == 0; }
void addNode(ExplodedNode* N, ExplodedGraph &G);
2009-06-02 13:58:47 -04:00
void setFlag() {
2009-10-14 14:03:49 -04:00
assert(P == 0);
2009-06-02 13:58:47 -04:00
P = AuxFlag;
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
bool getFlag() const {
return P & AuxFlag ? true : false;
}
2009-10-14 14:03:49 -04:00
};
2009-06-02 13:58:47 -04:00
/// Location - The program location (within a function body) associated
/// with this node.
const ProgramPoint Location;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
/// State - The state associated with this node.
2009-10-14 14:03:49 -04:00
const GRState* State;
2009-06-02 13:58:47 -04:00
/// Preds - The predecessors of this node.
NodeGroup Preds;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
/// Succs - The successors of this node.
NodeGroup Succs;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
public:
2009-10-14 14:03:49 -04:00
explicit ExplodedNode(const ProgramPoint& loc, const GRState* state)
: Location(loc), State(state) {}
2009-06-02 13:58:47 -04:00
/// getLocation - Returns the edge associated with the given node.
ProgramPoint getLocation() const { return Location; }
2009-10-14 14:03:49 -04:00
const LocationContext *getLocationContext() const {
return getLocation().getLocationContext();
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
const Decl &getCodeDecl() const { return *getLocationContext()->getDecl(); }
2009-06-02 13:58:47 -04:00
2009-10-14 14:03:49 -04:00
CFG &getCFG() const { return *getLocationContext()->getCFG(); }
ParentMap &getParentMap() const {return getLocationContext()->getParentMap();}
LiveVariables &getLiveVariables() const {
return *getLocationContext()->getLiveVariables();
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
const GRState* getState() const { return State; }
template <typename T>
const T* getLocationAs() const { return llvm::dyn_cast<T>(&Location); }
static void Profile(llvm::FoldingSetNodeID &ID,
const ProgramPoint& Loc, const GRState* state) {
2009-06-02 13:58:47 -04:00
ID.Add(Loc);
2009-10-14 14:03:49 -04:00
ID.AddPointer(state);
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
void Profile(llvm::FoldingSetNodeID& ID) const {
2009-06-02 13:58:47 -04:00
Profile(ID, getLocation(), getState());
}
2009-10-14 14:03:49 -04:00
/// addPredeccessor - Adds a predecessor to the current node, and
/// in tandem add this node as a successor of the other node.
void addPredecessor(ExplodedNode* V, ExplodedGraph &G);
unsigned succ_size() const { return Succs.size(); }
unsigned pred_size() const { return Preds.size(); }
bool succ_empty() const { return Succs.empty(); }
bool pred_empty() const { return Preds.empty(); }
bool isSink() const { return Succs.getFlag(); }
void markAsSink() { Succs.setFlag(); }
2009-06-02 13:58:47 -04:00
ExplodedNode* getFirstPred() {
return pred_empty() ? NULL : *(pred_begin());
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
const ExplodedNode* getFirstPred() const {
return const_cast<ExplodedNode*>(this)->getFirstPred();
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// Iterators over successor and predecessor vertices.
typedef ExplodedNode** succ_iterator;
typedef const ExplodedNode* const * const_succ_iterator;
typedef ExplodedNode** pred_iterator;
typedef const ExplodedNode* const * const_pred_iterator;
2009-10-14 14:03:49 -04:00
pred_iterator pred_begin() { return Preds.begin(); }
pred_iterator pred_end() { return Preds.end(); }
2009-06-02 13:58:47 -04:00
const_pred_iterator pred_begin() const {
return const_cast<ExplodedNode*>(this)->pred_begin();
2009-10-14 14:03:49 -04:00
}
2009-06-02 13:58:47 -04:00
const_pred_iterator pred_end() const {
return const_cast<ExplodedNode*>(this)->pred_end();
}
2009-10-14 14:03:49 -04:00
succ_iterator succ_begin() { return Succs.begin(); }
succ_iterator succ_end() { return Succs.end(); }
2009-06-02 13:58:47 -04:00
const_succ_iterator succ_begin() const {
return const_cast<ExplodedNode*>(this)->succ_begin();
}
const_succ_iterator succ_end() const {
return const_cast<ExplodedNode*>(this)->succ_end();
}
2009-10-14 14:03:49 -04:00
// For debugging.
public:
class Auditor {
public:
virtual ~Auditor();
virtual void AddEdge(ExplodedNode* Src, ExplodedNode* Dst) = 0;
};
static void SetAuditor(Auditor* A);
2009-06-02 13:58:47 -04:00
};
2009-10-14 14:03:49 -04:00
// FIXME: Is this class necessary?
class InterExplodedGraphMap {
llvm::DenseMap<const ExplodedNode*, ExplodedNode*> M;
friend class ExplodedGraph;
2009-06-02 13:58:47 -04:00
2009-10-14 14:03:49 -04:00
public:
ExplodedNode* getMappedNode(const ExplodedNode* N) const;
2010-01-01 05:34:51 -05:00
InterExplodedGraphMap() {}
2009-10-14 14:03:49 -04:00
virtual ~InterExplodedGraphMap() {}
};
class ExplodedGraph {
2009-06-02 13:58:47 -04:00
protected:
2009-10-14 14:03:49 -04:00
friend class GRCoreEngine;
2009-06-02 13:58:47 -04:00
// Type definitions.
2009-10-14 14:03:49 -04:00
typedef llvm::SmallVector<ExplodedNode*,2> RootsTy;
typedef llvm::SmallVector<ExplodedNode*,10> EndNodesTy;
2009-06-02 13:58:47 -04:00
/// Roots - The roots of the simulation graph. Usually there will be only
/// one, but clients are free to establish multiple subgraphs within a single
/// SimulGraph. Moreover, these subgraphs can often merge when paths from
/// different roots reach the same state at the same program location.
RootsTy Roots;
/// EndNodes - The nodes in the simulation graph which have been
/// specially marked as the endpoint of an abstract simulation path.
EndNodesTy EndNodes;
2009-10-14 14:03:49 -04:00
/// Nodes - The nodes in the graph.
llvm::FoldingSet<ExplodedNode> Nodes;
/// BVC - Allocator and context for allocating nodes and their predecessor
/// and successor groups.
BumpVectorContext BVC;
2009-06-02 13:58:47 -04:00
/// Ctx - The ASTContext used to "interpret" CodeDecl.
ASTContext& Ctx;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
/// NumNodes - The number of nodes in the graph.
unsigned NumNodes;
2009-10-14 14:03:49 -04:00
public:
/// getNode - Retrieve the node associated with a (Location,State) pair,
/// where the 'Location' is a ProgramPoint in the CFG. If no node for
/// this pair exists, it is created. IsNew is set to true if
/// the node was freshly created.
ExplodedNode* getNode(const ProgramPoint& L, const GRState *State,
bool* IsNew = 0);
ExplodedGraph* MakeEmptyGraph() const {
return new ExplodedGraph(Ctx);
}
2009-06-02 13:58:47 -04:00
/// addRoot - Add an untyped node to the set of roots.
2009-10-14 14:03:49 -04:00
ExplodedNode* addRoot(ExplodedNode* V) {
2009-06-02 13:58:47 -04:00
Roots.push_back(V);
return V;
}
/// addEndOfPath - Add an untyped node to the set of EOP nodes.
2009-10-14 14:03:49 -04:00
ExplodedNode* addEndOfPath(ExplodedNode* V) {
2009-06-02 13:58:47 -04:00
EndNodes.push_back(V);
return V;
}
2009-10-14 14:03:49 -04:00
ExplodedGraph(ASTContext& ctx) : Ctx(ctx), NumNodes(0) {}
~ExplodedGraph() {}
2009-06-02 13:58:47 -04:00
unsigned num_roots() const { return Roots.size(); }
unsigned num_eops() const { return EndNodes.size(); }
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
bool empty() const { return NumNodes == 0; }
unsigned size() const { return NumNodes; }
// Iterators.
2009-10-14 14:03:49 -04:00
typedef ExplodedNode NodeTy;
typedef llvm::FoldingSet<ExplodedNode> AllNodesTy;
2009-06-02 13:58:47 -04:00
typedef NodeTy** roots_iterator;
2009-10-14 14:03:49 -04:00
typedef NodeTy* const * const_roots_iterator;
2009-06-02 13:58:47 -04:00
typedef NodeTy** eop_iterator;
2009-10-14 14:03:49 -04:00
typedef NodeTy* const * const_eop_iterator;
typedef AllNodesTy::iterator node_iterator;
typedef AllNodesTy::const_iterator const_node_iterator;
2009-06-02 13:58:47 -04:00
2009-10-14 14:03:49 -04:00
node_iterator nodes_begin() { return Nodes.begin(); }
2009-06-02 13:58:47 -04:00
2009-10-14 14:03:49 -04:00
node_iterator nodes_end() { return Nodes.end(); }
const_node_iterator nodes_begin() const { return Nodes.begin(); }
const_node_iterator nodes_end() const { return Nodes.end(); }
roots_iterator roots_begin() { return Roots.begin(); }
roots_iterator roots_end() { return Roots.end(); }
const_roots_iterator roots_begin() const { return Roots.begin(); }
const_roots_iterator roots_end() const { return Roots.end(); }
eop_iterator eop_begin() { return EndNodes.begin(); }
eop_iterator eop_end() { return EndNodes.end(); }
const_eop_iterator eop_begin() const { return EndNodes.begin(); }
const_eop_iterator eop_end() const { return EndNodes.end(); }
llvm::BumpPtrAllocator & getAllocator() { return BVC.getAllocator(); }
BumpVectorContext &getNodeAllocator() { return BVC; }
ASTContext& getContext() { return Ctx; }
typedef llvm::DenseMap<const ExplodedNode*, ExplodedNode*> NodeMap;
std::pair<ExplodedGraph*, InterExplodedGraphMap*>
2009-06-02 13:58:47 -04:00
Trim(const NodeTy* const* NBeg, const NodeTy* const* NEnd,
2009-10-14 14:03:49 -04:00
llvm::DenseMap<const void*, const void*> *InverseMap = 0) const;
ExplodedGraph* TrimInternal(const ExplodedNode* const * NBeg,
const ExplodedNode* const * NEnd,
InterExplodedGraphMap *M,
llvm::DenseMap<const void*, const void*> *InverseMap) const;
2009-06-02 13:58:47 -04:00
};
class ExplodedNodeSet {
2009-10-14 14:03:49 -04:00
typedef llvm::SmallPtrSet<ExplodedNode*,5> ImplTy;
2009-06-02 13:58:47 -04:00
ImplTy Impl;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
public:
2009-10-14 14:03:49 -04:00
ExplodedNodeSet(ExplodedNode* N) {
assert (N && !static_cast<ExplodedNode*>(N)->isSink());
2009-06-02 13:58:47 -04:00
Impl.insert(N);
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
ExplodedNodeSet() {}
2009-10-14 14:03:49 -04:00
inline void Add(ExplodedNode* N) {
if (N && !static_cast<ExplodedNode*>(N)->isSink()) Impl.insert(N);
}
ExplodedNodeSet& operator=(const ExplodedNodeSet &X) {
Impl = X.Impl;
return *this;
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
typedef ImplTy::iterator iterator;
typedef ImplTy::const_iterator const_iterator;
2009-06-02 13:58:47 -04:00
2009-12-01 06:08:04 -05:00
unsigned size() const { return Impl.size(); }
bool empty() const { return Impl.empty(); }
void clear() { Impl.clear(); }
void insert(const ExplodedNodeSet &S) {
if (empty())
Impl = S.Impl;
else
Impl.insert(S.begin(), S.end());
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
inline iterator begin() { return Impl.begin(); }
inline iterator end() { return Impl.end(); }
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
inline const_iterator begin() const { return Impl.begin(); }
inline const_iterator end() const { return Impl.end(); }
2009-10-14 14:03:49 -04:00
};
2009-06-02 13:58:47 -04:00
} // end clang namespace
// GraphTraits
namespace llvm {
2009-10-14 14:03:49 -04:00
template<> struct GraphTraits<clang::ExplodedNode*> {
typedef clang::ExplodedNode NodeType;
typedef NodeType::succ_iterator ChildIteratorType;
2009-06-02 13:58:47 -04:00
typedef llvm::df_iterator<NodeType*> nodes_iterator;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
static inline NodeType* getEntryNode(NodeType* N) {
return N;
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
static inline ChildIteratorType child_begin(NodeType* N) {
return N->succ_begin();
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
static inline ChildIteratorType child_end(NodeType* N) {
return N->succ_end();
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
static inline nodes_iterator nodes_begin(NodeType* N) {
return df_begin(N);
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
static inline nodes_iterator nodes_end(NodeType* N) {
return df_end(N);
}
};
2009-10-14 14:03:49 -04:00
template<> struct GraphTraits<const clang::ExplodedNode*> {
typedef const clang::ExplodedNode NodeType;
typedef NodeType::const_succ_iterator ChildIteratorType;
2009-06-02 13:58:47 -04:00
typedef llvm::df_iterator<NodeType*> nodes_iterator;
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
static inline NodeType* getEntryNode(NodeType* N) {
return N;
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
static inline ChildIteratorType child_begin(NodeType* N) {
return N->succ_begin();
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
static inline ChildIteratorType child_end(NodeType* N) {
return N->succ_end();
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
static inline nodes_iterator nodes_begin(NodeType* N) {
return df_begin(N);
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
static inline nodes_iterator nodes_end(NodeType* N) {
return df_end(N);
}
};
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
} // end llvm namespace
#endif