Skip to content
This repository has been archived by the owner on Apr 23, 2020. It is now read-only.

Commit

Permalink
[Analysis] Use unique_ptr for CallGraph::FunctionMap.
Browse files Browse the repository at this point in the history
Reviewers: timshen

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D25427

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@283775 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Justin Lebar committed Oct 10, 2016
1 parent 31365f6 commit b28828e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
15 changes: 10 additions & 5 deletions include/clang/Analysis/CallGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class CallGraphNode;
class CallGraph : public RecursiveASTVisitor<CallGraph> {
friend class CallGraphNode;

typedef llvm::DenseMap<const Decl *, CallGraphNode *> FunctionMapTy;
typedef llvm::DenseMap<const Decl *, std::unique_ptr<CallGraphNode>>
FunctionMapTy;

/// FunctionMap owns all CallGraphNodes.
FunctionMapTy FunctionMap;
Expand Down Expand Up @@ -198,9 +199,11 @@ template <> struct GraphTraits<clang::CallGraph*>
static NodeType *getEntryNode(clang::CallGraph *CGN) {
return CGN->getRoot(); // Start at the external node!
}
typedef std::pair<const clang::Decl*, clang::CallGraphNode*> PairTy;

static clang::CallGraphNode *CGGetValue(PairTy P) { return P.second; }
static clang::CallGraphNode *
CGGetValue(clang::CallGraph::const_iterator::value_type &P) {
return P.second.get();
}

// nodes_iterator/begin/end - Allow iteration over all nodes in the graph
typedef mapped_iterator<clang::CallGraph::iterator, decltype(&CGGetValue)>
Expand All @@ -223,9 +226,11 @@ template <> struct GraphTraits<const clang::CallGraph*> :
static NodeType *getEntryNode(const clang::CallGraph *CGN) {
return CGN->getRoot();
}
typedef std::pair<const clang::Decl*, clang::CallGraphNode*> PairTy;

static clang::CallGraphNode *CGGetValue(PairTy P) { return P.second; }
static clang::CallGraphNode *
CGGetValue(clang::CallGraph::const_iterator::value_type &P) {
return P.second.get();
}

// nodes_iterator/begin/end - Allow iteration over all nodes in the graph
typedef mapped_iterator<clang::CallGraph::const_iterator,
Expand Down
16 changes: 7 additions & 9 deletions lib/Analysis/CallGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ CallGraph::CallGraph() {
Root = getOrInsertNode(nullptr);
}

CallGraph::~CallGraph() {
llvm::DeleteContainerSeconds(FunctionMap);
}
CallGraph::~CallGraph() {}

bool CallGraph::includeInGraph(const Decl *D) {
assert(D);
Expand Down Expand Up @@ -142,22 +140,22 @@ void CallGraph::addNodeForDecl(Decl* D, bool IsGlobal) {
CallGraphNode *CallGraph::getNode(const Decl *F) const {
FunctionMapTy::const_iterator I = FunctionMap.find(F);
if (I == FunctionMap.end()) return nullptr;
return I->second;
return I->second.get();
}

CallGraphNode *CallGraph::getOrInsertNode(Decl *F) {
if (F && !isa<ObjCMethodDecl>(F))
F = F->getCanonicalDecl();

CallGraphNode *&Node = FunctionMap[F];
std::unique_ptr<CallGraphNode> &Node = FunctionMap[F];
if (Node)
return Node;
return Node.get();

Node = new CallGraphNode(F);
Node = llvm::make_unique<CallGraphNode>(F);
// Make Root node a parent of all functions to make sure all are reachable.
if (F)
Root->addCallee(Node, this);
return Node;
Root->addCallee(Node.get(), this);
return Node.get();
}

void CallGraph::print(raw_ostream &OS) const {
Expand Down

0 comments on commit b28828e

Please sign in to comment.