CallGraph.h revision 4c7c5a1d01d5e35aa6fb7724336c9506a88b6b62
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 bool hasCallee() const { return begin() != end(); } 47 48 std::string getName() { return F.getPrintableName(); } 49}; 50 51class CallGraph { 52 /// Program manages all Entities. 53 idx::Program Prog; 54 55 typedef llvm::DenseMap<idx::Entity, CallGraphNode *> FunctionMapTy; 56 57 /// FunctionMap owns all CallGraphNodes. 58 FunctionMapTy FunctionMap; 59 60 /// CallerCtx maps a caller to its ASTContext. 61 llvm::DenseMap<CallGraphNode *, ASTContext *> CallerCtx; 62 63public: 64 ~CallGraph(); 65 66 typedef FunctionMapTy::iterator iterator; 67 typedef FunctionMapTy::const_iterator const_iterator; 68 69 iterator begin() { return FunctionMap.begin(); } 70 iterator end() { return FunctionMap.end(); } 71 const_iterator begin() const { return FunctionMap.begin(); } 72 const_iterator end() const { return FunctionMap.end(); } 73 74 void addTU(ASTUnit &AST); 75 76 idx::Program &getProgram() { return Prog; } 77 78 CallGraphNode *getOrInsertFunction(idx::Entity F); 79 80 void print(llvm::raw_ostream &os); 81 void dump(); 82}; 83 84} 85 86#endif 87