ROSE 0.11.145.250
CallGraph.h
1
2#ifndef CALL_GRAPH_H
3#define CALL_GRAPH_H
4
5#include <GraphDotOutput.h>
6//~ #include <VirtualGraphCreate.h>
7
8#include "Rose/Diagnostics.h"
9
10//~ #include <sstream>
11//~ #include <iostream>
12#include <string>
13//~ #include <functional>
14#include <boost/unordered_map.hpp>
15
16#include "ClassHierarchyGraph.h"
17
18class FunctionData;
19
20// DQ (1/31/2006): Changed name and made global function type symbol table a static data member.
21// extern SgFunctionTypeTable Sgfunc_type_table;
22// This header has to be here since it uses type SgFunctionDeclarationPtrList
23
24//AS(090707) Added the CallTargetSet namespace to replace the CallGraphFunctionSolver class
25namespace CallTargetSet
26{
39 std::vector<SgFunctionDeclaration*> solveFunctionPointerCall ( SgPointerDerefExp *);
40
41 // returns the list of declarations of all functions that may get called via a member function pointer
42 std::vector<SgFunctionDeclaration*> solveMemberFunctionPointerCall ( SgExpression *,ClassHierarchyWrapper * );
43
58 Rose_STL_Container<SgFunctionDeclaration*> solveFunctionPointerCallsFunctional(SgNode* node, SgFunctionType* functionType );
59
60 // returns the list of declarations of all functions that may get called via a
61 // member function (non/polymorphic) call
62 std::vector<SgFunctionDeclaration*> solveMemberFunctionCall (
63 SgClassType *, ClassHierarchyWrapper *, SgMemberFunctionDeclaration *, bool , bool includePureVirtualFunc = false );
64
66 std::vector<SgFunctionDeclaration*> solveConstructorInitializer ( SgConstructorInitializer* sgCtorInit);
67
69 ROSE_DLL_API void getPropertiesForExpression(SgExpression* exp,
70 ClassHierarchyWrapper* classHierarchy,
71 Rose_STL_Container<SgFunctionDeclaration*>& propList,
72 bool includePureVirtualFunc = false);
73
78 void getDefinitionsForExpression(SgExpression* exp,
79 ClassHierarchyWrapper* classHierarchy,
80 Rose_STL_Container<SgFunctionDefinition*>& calleeList);
81
84 void getDeclarationsForExpression(SgExpression* exp,
85 ClassHierarchyWrapper* classHierarchy,
86 Rose_STL_Container<SgFunctionDeclaration*>& calleeList,
87 bool includePureVirtualFunc = false);
88
89 // Gets a vector of SgExpressions that are associated with the current SgFunctionDefinition.
90 // This functionality is necessary for virtual, interprocedural control flow graphs. However,
91 // it is costly and should be used infrequently (or optimized!).
92 void getExpressionsForDefinition(SgFunctionDefinition* targetDef,
93 ClassHierarchyWrapper* classHierarchy,
94 Rose_STL_Container<SgExpression*>& exps);
95
96 // Gets the latest implementation of the member function from the ancestor hierarchy
97 SgFunctionDeclaration * getFirstVirtualFunctionDefinitionFromAncestors(SgClassType *crtClass,
98 SgMemberFunctionDeclaration *memberFunctionDeclaration,
99 ClassHierarchyWrapper *classHierarchy);
100
101};
102
103class ROSE_DLL_API FunctionData
104{
105 public:
106
107 bool hasDefinition;
108
109 bool isDefined ();
110
111 FunctionData(SgFunctionDeclaration* functionDeclaration, SgProject *project, ClassHierarchyWrapper * );
112
114 Rose_STL_Container<SgFunctionDeclaration *> functionList;
115
116 SgFunctionDeclaration* functionDeclaration;
117
118 Rose_STL_Container<SgMemberFunctionDeclaration*> *findPointsToVirtualFunctions ( SgMemberFunctionDeclaration * );
119 bool compareFunctionDeclarations( SgFunctionDeclaration *f1, SgFunctionDeclaration *f2 );
120};
121
124{
125 using result_type = bool;
126 result_type operator() (SgFunctionDeclaration* node) const; // always return true
127};
128
130// Liao, 6/17/2012
131struct ROSE_DLL_API builtinFilter
132{
133 using result_type = bool;
134 result_type operator() (SgFunctionDeclaration* node) const;
135};
136
137class ROSE_DLL_API CallGraphBuilder
138{
139 public:
140 using GraphNodes = boost::unordered_map<SgFunctionDeclaration*, SgGraphNode*> ;
145 template<typename Predicate>
146 void buildCallGraph(Predicate pred);
149 //void classifyCallGraph();
150
151 //We map each function to the corresponding graph node
152 boost::unordered_map<SgFunctionDeclaration*, SgGraphNode*>& getGraphNodesMapping(){ return graphNodes; }
153
158
159 private:
160 SgProject *project;
162 //We map each function to the corresponding graph node
163 GraphNodes graphNodes;
164
165};
167//TODO this function is not defined? If so, need to be removed.
168// AstDOTGeneration::writeIncidenceGraphToDOTFile() is used instead in the tutorial. Liao 6/17/2012
169void GenerateDotGraph ( SgIncidenceDirectedGraph *graph, std::string fileName );
170
172{
173 using result_type = Rose_STL_Container<SgNode*>;
174 result_type operator()(SgNode* node);
175};
176
177template<typename Predicate>
178void
180{
182
183 // Adds additional constraints to the predicate. It makes no sense to analyze non-instantiated templates.
184 struct isSelected {
185 Predicate &pred;
186 isSelected(Predicate &pred): pred(pred) {}
187 bool operator()(SgNode *node) {
188 SgFunctionDeclaration *f = isSgFunctionDeclaration(node);
189 // TV (10/26/2018): FIXME ROSE-1487
190 // assert(!f || f==f->get_firstNondefiningDeclaration()); // node uniqueness test
191#if 0
192 // DQ (8/25/2016): This is not a meaningful test since all functions will be in the memory pool, including template functions and template member functions.
193 if(isSgTemplateFunctionDeclaration(f)||isSgTemplateMemberFunctionDeclaration(f)) {
194 std::cerr<<"Error: CallGraphBuilder: call referring to node "<<f->class_name()<<" :: function-name:"<<f->get_qualified_name()<<std::endl;
195 }
196#endif
197 return f && f==f->get_firstNondefiningDeclaration() && !isSgTemplateMemberFunctionDeclaration(f) && !isSgTemplateFunctionDeclaration(f) && pred(f);
198 }
199 };
200
201 // Add nodes to the graph by querying the memory pool for function declarations, mapping them to unique declarations
202 // that can be used as keys in a map (using get_firstNondefiningDeclaration()), and filtering according to the predicate.
203 graph = new SgIncidenceDirectedGraph();
204 std::vector<FunctionData> callGraphData;
205 ClassHierarchyWrapper classHierarchy(project);
206 graphNodes.clear();
207 VariantVector vv(V_SgFunctionDeclaration);
209 for(SgNode *node : NodeQuery::queryMemoryPool(defFunc, &vv)) {
210 SgFunctionDeclaration *fdecl = isSgFunctionDeclaration(node);
211 SgFunctionDeclaration *unique = isSgFunctionDeclaration(fdecl->get_firstNondefiningDeclaration());
212#if 0 //debug
213 printf ("In buildCallGraph(): loop over functions from memory pool: fdecl = %p = %s name = %s \n",fdecl,fdecl->class_name().c_str(),fdecl->get_name().str());
214 printf ("In buildCallGraph(): loop over functions from memory pool: unique = %p = %s name = %s \n",unique,unique->class_name().c_str(),unique->get_name().str());
215#endif
216 if (isSelected(pred)(unique) && hasGraphNodeFor(unique) == NULL)
217 {
218#if 0 //debug
219 printf ("Collect function calls in unique function: unique = %p \n",unique);
220#endif
221 FunctionData fdata(unique, project, &classHierarchy); // computes functions called by unique
222 callGraphData.push_back(fdata);
223 std::string functionName = unique->get_qualified_name().getString();
224 SgGraphNode *graphNode = new SgGraphNode(functionName);
225 graphNode->set_SgNode(unique);
226 graphNodes[unique] = graphNode;
227 graph->addNode(graphNode);
228 mprintf("Added function %s %p\n", functionName.c_str(), unique);
229 }
230 else
231 {
232#if 0 //debug
233 printf ("Function not selected for processing: unique = %p \n",unique);
234 printf (" --- isSelected(pred)(unique) = %s \n",isSelected(pred)(unique) ? "true" : "false");
235 printf (" --- graphNodes.find(unique)==graphNodes.end() = %s \n",graphNodes.find(unique)==graphNodes.end() ? "true" : "false");
236#endif
237 }
238 }
239
240 // Add edges to the graph
241 for (FunctionData& currentFunction: callGraphData) {
242 SgFunctionDeclaration* curFuncDecl = currentFunction.functionDeclaration;
243 std::string curFuncName = curFuncDecl->get_qualified_name().getString();
244 SgGraphNode * srcNode = hasGraphNodeFor(currentFunction.functionDeclaration);
245 ROSE_ASSERT(srcNode != NULL);
246 std::vector<SgFunctionDeclaration*> & callees = currentFunction.functionList;
247 for (SgFunctionDeclaration* callee: callees) {
248 if (isSelected(pred)(callee)) {
249 SgGraphNode * dstNode = getGraphNodeFor(callee); //getGraphNode here, see function comment
250 ROSE_ASSERT(dstNode != NULL);
251 if (graph->checkIfDirectedGraphEdgeExists(srcNode, dstNode) == false)
252 graph->addDirectedEdge(srcNode, dstNode);
253 }
254 }
255 }
256}
257
258// endif for CALL_GRAPH_H
259#endif
260
SgIncidenceDirectedGraph * getGraph()
Grab the call graph built.
SgGraphNode * hasGraphNodeFor(SgFunctionDeclaration *fdecl) const
Retrieve the node matching a function declaration using firstNondefiningDeclaration (does not work ac...
void buildCallGraph()
Default builder filtering nothing in the call graph.
SgGraphNode * getGraphNodeFor(SgFunctionDeclaration *fdecl) const
Retrieve the node matching a function declaration (using mangled name to resolve across translation u...
std::vector< SgFunctionDeclaration * > functionList
All the callees of this function.
Definition CallGraph.h:114
This class represents the call of a class constructor to initialize a variable. For example "Foo foo;...
SgDeclarationStatement * get_firstNondefiningDeclaration() const
This is an access function for the SgDeclarationStatement::p_firstNondefiningDeclaration data member ...
This class represents the notion of an expression. Expressions are derived from SgLocatedNodes,...
This class represents the concept of a function declaration statement.
virtual std::string class_name() const override
returns a string representing the class name
This class represents the concept of a scope in C++ (e.g. global scope, fuction scope,...
This class represents a type for all functions.
SgGraphNode * addNode(const std::string &name="", SgNode *sg_node=NULL)
Support for adding SgGraphNode to SgGraph.
SgDirectedGraphEdge * addDirectedEdge(SgGraphNode *a, SgGraphNode *b, const std::string &name="")
Support for adding SgGraphEdge to SgGraph.
This class represents the concept of a member function declaration statement.
This class represents the base class for all IR nodes within Sage III.
This class represents a source project, with a list of SgFile objects and global information about th...
ROSE_DLL_API Sawyer::Message::Facility mlog
Diagnostic facility for the ROSE library as a whole.
Definition sageBuilder.C:58
A function object to filter out builtin functions in a call graph (only non-builtin functions will be...
Definition CallGraph.h:132
A function object to be used as a predicate to filter out functions in a call graph: it does not filt...
Definition CallGraph.h:124