]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/llvm/tools/clang/include/clang/Index/CallGraph.h
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / contrib / llvm / tools / clang / include / clang / Index / CallGraph.h
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 "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/GraphTraits.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include <vector>
24 #include <map>
25
26 namespace clang {
27
28 class CallGraphNode {
29   idx::Entity F;
30   typedef std::pair<idx::ASTLocation, CallGraphNode*> CallRecord;
31   std::vector<CallRecord> CalledFunctions;
32
33 public:
34   CallGraphNode(idx::Entity f) : F(f) {}
35
36   typedef std::vector<CallRecord>::iterator iterator;
37   typedef std::vector<CallRecord>::const_iterator const_iterator;
38
39   iterator begin() { return CalledFunctions.begin(); }
40   iterator end()   { return CalledFunctions.end(); }
41   const_iterator begin() const { return CalledFunctions.begin(); }
42   const_iterator end()   const { return CalledFunctions.end();   }
43
44   void addCallee(idx::ASTLocation L, CallGraphNode *Node) {
45     CalledFunctions.push_back(std::make_pair(L, Node));
46   }
47
48   bool hasCallee() const { return begin() != end(); }
49
50   std::string getName() const { return F.getPrintableName(); }
51
52   Decl *getDecl(ASTContext &Ctx) const { return F.getDecl(Ctx); }
53 };
54
55 class CallGraph {
56   /// Program manages all Entities.
57   idx::Program &Prog;
58
59   typedef std::map<idx::Entity, CallGraphNode *> FunctionMapTy;
60
61   /// FunctionMap owns all CallGraphNodes.
62   FunctionMapTy FunctionMap;
63
64   /// CallerCtx maps a caller to its ASTContext.
65   llvm::DenseMap<CallGraphNode *, ASTContext *> CallerCtx;
66
67   /// Root node is the 'main' function or 0.
68   CallGraphNode *Root;
69
70   /// ExternalCallingNode has edges to all external functions.
71   CallGraphNode *ExternalCallingNode;
72
73 public:
74   CallGraph(idx::Program &P);
75   ~CallGraph();
76
77   typedef FunctionMapTy::iterator iterator;
78   typedef FunctionMapTy::const_iterator const_iterator;
79
80   iterator begin() { return FunctionMap.begin(); }
81   iterator end()   { return FunctionMap.end();   }
82   const_iterator begin() const { return FunctionMap.begin(); }
83   const_iterator end()   const { return FunctionMap.end();   }
84
85   CallGraphNode *getRoot() { return Root; }
86
87   CallGraphNode *getExternalCallingNode() { return ExternalCallingNode; }
88
89   void addTU(ASTContext &AST);
90
91   idx::Program &getProgram() { return Prog; }
92
93   CallGraphNode *getOrInsertFunction(idx::Entity F);
94
95   Decl *getDecl(CallGraphNode *Node);
96
97   void print(raw_ostream &os);
98   void dump();
99
100   void ViewCallGraph() const;
101 };
102
103 } // end clang namespace
104
105 namespace llvm {
106
107 template <> struct GraphTraits<clang::CallGraph> {
108   typedef clang::CallGraph GraphType;
109   typedef clang::CallGraphNode NodeType;
110
111   typedef std::pair<clang::idx::ASTLocation, NodeType*> CGNPairTy;
112   typedef std::pointer_to_unary_function<CGNPairTy, NodeType*> CGNDerefFun;
113
114   typedef mapped_iterator<NodeType::iterator, CGNDerefFun> ChildIteratorType;
115
116   static NodeType *getEntryNode(GraphType *CG) {
117     return CG->getExternalCallingNode();
118   }
119
120   static ChildIteratorType child_begin(NodeType *N) {
121     return map_iterator(N->begin(), CGNDerefFun(CGNDeref));
122   }
123   static ChildIteratorType child_end(NodeType *N) {
124     return map_iterator(N->end(), CGNDerefFun(CGNDeref));
125   }
126
127   typedef std::pair<clang::idx::Entity, NodeType*> PairTy;
128   typedef std::pointer_to_unary_function<PairTy, NodeType*> DerefFun;
129
130   typedef mapped_iterator<GraphType::const_iterator, DerefFun> nodes_iterator;
131
132   static nodes_iterator nodes_begin(const GraphType &CG) {
133     return map_iterator(CG.begin(), DerefFun(CGDeref));
134   }
135   static nodes_iterator nodes_end(const GraphType &CG) {
136     return map_iterator(CG.end(), DerefFun(CGDeref));
137   }
138
139   static NodeType *CGNDeref(CGNPairTy P) { return P.second; }
140
141   static NodeType *CGDeref(PairTy P) { return P.second; }
142 };
143
144 } // end llvm namespace
145
146 #endif