public CallGraphTest() { source = FileUtil.ReadAllText(path); tree = ASTUtil.GetSyntaxTreeFromSource(source); classDeclaration = tree.GetRoot().DescendantNodes().OfType<ClassDeclarationSyntax>(). First(c => c.Identifier.Value.Equals("CallGraphTest")); graph = new CallGraphBuilder(classDeclaration, tree).BuildCallGraph(); }
/* Get the list of method declarations that are in this call graph but not in another. */ public IList<MethodDeclarationSyntax> getVerticesNotIn(CallGraph another) { IList<MethodDeclarationSyntax> methods = new List<MethodDeclarationSyntax>(); foreach(var m in vertices) { if(!another.hasVertice(m)) methods.Add(m); } return methods; }
/* Build the graph. */ public CallGraph BuildCallGraph() { // Get all the method declarations in the class. var methods = classDeclaration.DescendantNodes().Where(n => n.Kind == SyntaxKind.MethodDeclaration); var callGraph = new CallGraph(); // Add all the methods to the call graph as vertices. foreach (MethodDeclarationSyntax m in methods) { callGraph.addVertice(m); } // For each pair of method, check if they have caller/callee relationship and adds to // the call graph as edges if true. foreach (var m1 in methods) { foreach (var m2 in methods) { if (ASTUtil.IsInvoking(m1, m2, tree)) { callGraph.addEdge((MethodDeclarationSyntax)m1, (MethodDeclarationSyntax)m2); } } } return callGraph; }