CallGraph.h revision 6bd8fb50acadeeafd923e98cd6a94efeb75693dc
1//== CallGraph.cpp - Call graph building ------------------------*- C++ -*--==//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file defined the CallGraph and CallGraphNode classes.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_ANALYSIS_CALLGRAPH
15#define LLVM_CLANG_ANALYSIS_CALLGRAPH
16
17#include "clang/Index/ASTLocation.h"
18#include "clang/Index/Entity.h"
19#include "clang/Index/Program.h"
20#include "clang/Frontend/ASTUnit.h"
21#include "llvm/ADT/DenseMap.h"
22#include <vector>
23
24namespace clang {
25
26class CallGraphNode {
27  idx::Entity *F;
28  typedef std::pair<idx::ASTLocation, CallGraphNode*> CallRecord;
29  std::vector<CallRecord> CalledFunctions;
30
31public:
32  CallGraphNode(idx::Entity *f) : F(f) {}
33
34  typedef std::vector<CallRecord>::iterator iterator;
35  typedef std::vector<CallRecord>::const_iterator const_iterator;
36
37  iterator begin() { return CalledFunctions.begin(); }
38  iterator end()   { return CalledFunctions.end(); }
39  const_iterator begin() const { return CalledFunctions.begin(); }
40  const_iterator end()   const { return CalledFunctions.end();   }
41
42  void addCallee(idx::ASTLocation L, CallGraphNode *Node) {
43    CalledFunctions.push_back(std::make_pair(L, Node));
44  }
45
46  const char *getName(ASTContext &Ctx) { return F->getName(Ctx); }
47};
48
49class CallGraph {
50  /// Program manages all Entities.
51  idx::Program Prog;
52
53  typedef llvm::DenseMap<idx::Entity *, CallGraphNode *> FunctionMapTy;
54
55  /// FunctionMap owns all CallGraphNodes.
56  FunctionMapTy FunctionMap;
57
58  /// CallerCtx maps a caller to its ASTContext.
59  llvm::DenseMap<CallGraphNode *, ASTContext *> CallerCtx;
60
61public:
62  ~CallGraph();
63
64  typedef FunctionMapTy::iterator iterator;
65  typedef FunctionMapTy::const_iterator const_iterator;
66
67  iterator begin() { return FunctionMap.begin(); }
68  iterator end()   { return FunctionMap.end();   }
69  const_iterator begin() const { return FunctionMap.begin(); }
70  const_iterator end()   const { return FunctionMap.end();   }
71
72  void addTU(ASTUnit &AST);
73
74  idx::Program &getProgram() { return Prog; }
75
76  CallGraphNode *getOrInsertFunction(idx::Entity *F);
77
78  void print(llvm::raw_ostream &os);
79  void dump();
80};
81
82}
83
84#endif
85