|
| 1 | +package tfm.graphs.sdg.sumarcs; |
| 2 | + |
| 3 | +import com.github.javaparser.ast.body.CallableDeclaration; |
| 4 | +import com.github.javaparser.ast.body.MethodDeclaration; |
| 5 | +import tfm.arcs.Arc; |
| 6 | +import tfm.arcs.sdg.CallArc; |
| 7 | +import tfm.graphs.CallGraph; |
| 8 | +import tfm.graphs.sdg.SDG; |
| 9 | +import tfm.nodes.ActualIONode; |
| 10 | +import tfm.nodes.FormalIONode; |
| 11 | +import tfm.nodes.GraphNode; |
| 12 | +import tfm.nodes.type.NodeType; |
| 13 | +import tfm.utils.Utils; |
| 14 | + |
| 15 | +import java.util.*; |
| 16 | +import java.util.stream.Collectors; |
| 17 | + |
| 18 | +/** |
| 19 | + * Performs a fixed point analysis over the call graph of a given SDG |
| 20 | + */ |
| 21 | +public class AnalysisSummaryArcsBuilder extends SummaryArcsBuilder { |
| 22 | + |
| 23 | + private static class SummaryArcPair { |
| 24 | + FormalIONode in; |
| 25 | + GraphNode<?> out; // out node is either FormalIONode or METHOD_OUTPUT |
| 26 | + |
| 27 | + SummaryArcPair(FormalIONode in, GraphNode<?> out) { |
| 28 | + this.in = in; this.out = out; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + private final SDG sdg; |
| 33 | + private final CallGraph callGraph; |
| 34 | + |
| 35 | + protected Map<CallableDeclaration<?>, Set<SummaryArcPair>> vertexDataMap = new HashMap<>(); |
| 36 | + protected boolean built = false; |
| 37 | + |
| 38 | + public AnalysisSummaryArcsBuilder(SDG sdg) { |
| 39 | + super(sdg); |
| 40 | + |
| 41 | + CallGraph callGraph = new CallGraph(); |
| 42 | + callGraph.build(sdg.getCompilationUnits()); |
| 43 | + |
| 44 | + this.sdg = sdg; |
| 45 | + this.callGraph = callGraph; |
| 46 | + } |
| 47 | + |
| 48 | + public AnalysisSummaryArcsBuilder(SDG sdg, CallGraph callGraph) { |
| 49 | + super(sdg); |
| 50 | + |
| 51 | + this.sdg = sdg; |
| 52 | + this.callGraph = callGraph; |
| 53 | + } |
| 54 | + |
| 55 | + public Set<SummaryArcPair> getResult(MethodDeclaration vertex) { |
| 56 | + return vertexDataMap.get(vertex); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void visit() { |
| 61 | + assert !built; |
| 62 | + List<CallableDeclaration<?>> workList = new LinkedList<>(callGraph.vertexSet()); |
| 63 | + callGraph.vertexSet().forEach(v -> vertexDataMap.put(v, computeSummaryArcs(v))); |
| 64 | + while (!workList.isEmpty()) { |
| 65 | + List<CallableDeclaration<?>> newWorkList = new LinkedList<>(); |
| 66 | + for (CallableDeclaration<?> vertex : workList) { |
| 67 | + updateVertex(vertex); |
| 68 | + Set<SummaryArcPair> newValue = computeSummaryArcs(vertex); // now with new arcs!!! |
| 69 | + if (!Objects.equals(vertexDataMap.get(vertex), newValue)) { |
| 70 | + vertexDataMap.put(vertex, newValue); |
| 71 | + newWorkList.addAll(callGraph.incomingEdgesOf(vertex).stream() |
| 72 | + .map(callGraph::getEdgeSource).collect(Collectors.toSet())); |
| 73 | + } |
| 74 | + } |
| 75 | + workList = newWorkList; |
| 76 | + } |
| 77 | + vertexDataMap = Collections.unmodifiableMap(vertexDataMap); |
| 78 | + built = true; |
| 79 | + } |
| 80 | + |
| 81 | + protected void updateVertex(CallableDeclaration<?> declaration) { |
| 82 | + if (!declaration.isMethodDeclaration()) { |
| 83 | + return; // Parse only method declarations |
| 84 | + } |
| 85 | + |
| 86 | + Optional<GraphNode<MethodDeclaration>> optionalMethodDeclarationNode = sdg.findNodeByASTNode(declaration.asMethodDeclaration()); |
| 87 | + |
| 88 | + if (optionalMethodDeclarationNode.isEmpty()) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + GraphNode<MethodDeclaration> methodDeclarationNode = optionalMethodDeclarationNode.get(); |
| 93 | + |
| 94 | + // Get call arcs from this declaration |
| 95 | + Set<CallArc> methodCallExprNodes = sdg.incomingEdgesOf(methodDeclarationNode).stream() |
| 96 | + .filter(Arc::isCallArc) |
| 97 | + .map(Arc::asCallArc) |
| 98 | + .collect(Collectors.toSet()); |
| 99 | + |
| 100 | + for (CallArc callArc : methodCallExprNodes) { |
| 101 | + GraphNode<?> methodCallNode = sdg.getEdgeSource(callArc); |
| 102 | + |
| 103 | + for (SummaryArcPair summaryArcPair : vertexDataMap.getOrDefault(declaration, Utils.emptySet())) { |
| 104 | + FormalIONode inFormalNode = summaryArcPair.in; |
| 105 | + GraphNode<?> outFormalNode = summaryArcPair.out; |
| 106 | + |
| 107 | + Optional<ActualIONode> optionalIn = sdg.outgoingEdgesOf(methodCallNode).stream() |
| 108 | + .filter(arc -> (sdg.getEdgeTarget(arc)).getNodeType().is(NodeType.ACTUAL_IN)) |
| 109 | + .map(arc -> (ActualIONode) sdg.getEdgeTarget(arc)) |
| 110 | + .filter(actualIONode -> actualIONode.matchesFormalIO(inFormalNode)) |
| 111 | + .findFirst(); |
| 112 | + |
| 113 | + Optional<? extends GraphNode<?>> optionalOut = sdg.outgoingEdgesOf(methodCallNode).stream() |
| 114 | + .map(sdg::getEdgeTarget) |
| 115 | + .filter(node -> node.getNodeType().is(NodeType.ACTUAL_OUT)) |
| 116 | + .filter(actualNode -> { |
| 117 | + if (actualNode instanceof ActualIONode) { |
| 118 | + return outFormalNode instanceof FormalIONode |
| 119 | + && ((ActualIONode) actualNode).matchesFormalIO((FormalIONode) outFormalNode); |
| 120 | + } |
| 121 | + // otherwise, actualNode must be METHOD_CALL_RETURN |
| 122 | + if (actualNode.getNodeType() != NodeType.METHOD_CALL_RETURN) { |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + return outFormalNode.getNodeType() == NodeType.METHOD_OUTPUT; |
| 127 | + }) |
| 128 | + .findFirst(); |
| 129 | + |
| 130 | + if (optionalIn.isEmpty() || optionalOut.isEmpty()) { |
| 131 | + continue; |
| 132 | + } |
| 133 | + |
| 134 | + sdg.addSummaryArc(optionalIn.get(), optionalOut.get()); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + protected Set<SummaryArcPair> computeSummaryArcs(CallableDeclaration<?> declaration) { |
| 140 | + Optional<GraphNode<MethodDeclaration>> optionalMethodDeclarationNode = sdg.findNodeByASTNode(declaration.asMethodDeclaration()); |
| 141 | + |
| 142 | + if (optionalMethodDeclarationNode.isEmpty()) { |
| 143 | + return Utils.emptySet(); |
| 144 | + } |
| 145 | + |
| 146 | + GraphNode<MethodDeclaration> methodDeclarationNode = optionalMethodDeclarationNode.get(); |
| 147 | + |
| 148 | + // Get formal out nodes from declaration |
| 149 | + Set<GraphNode<?>> formalOutNodes = sdg.outgoingEdgesOf(methodDeclarationNode).stream() |
| 150 | + .filter(Arc::isControlDependencyArc) |
| 151 | + .map(sdg::getEdgeTarget) |
| 152 | + .filter(node -> node.getNodeType().is(NodeType.FORMAL_OUT)) |
| 153 | + .collect(Collectors.toSet()); |
| 154 | + |
| 155 | + Set<SummaryArcPair> res = new HashSet<>(); |
| 156 | + |
| 157 | + for (GraphNode<?> formalOutNode : formalOutNodes) { |
| 158 | + for (FormalIONode formalInNode : findReachableFormalInNodes(formalOutNode)) { |
| 159 | + res.add(new SummaryArcPair(formalInNode, formalOutNode)); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + return res; |
| 164 | + } |
| 165 | + |
| 166 | + private Set<FormalIONode> findReachableFormalInNodes(GraphNode<?> formalOutNode) { |
| 167 | + return this.doFindReachableFormalInNodes(formalOutNode, Utils.emptySet()); |
| 168 | + } |
| 169 | + |
| 170 | + private Set<FormalIONode> doFindReachableFormalInNodes(GraphNode<?> root, Set<Long> visited) { |
| 171 | + visited.add(root.getId()); |
| 172 | + |
| 173 | + Set<FormalIONode> res = Utils.emptySet(); |
| 174 | + |
| 175 | + if (root.getNodeType().is(NodeType.FORMAL_IN)) { |
| 176 | + res.add((FormalIONode) root); |
| 177 | + } else { |
| 178 | + for (Arc arc : sdg.incomingEdgesOf(root)) { |
| 179 | + GraphNode<?> nextNode = sdg.getEdgeSource(arc); |
| 180 | + |
| 181 | + if (visited.contains(nextNode.getId())) { |
| 182 | + continue; |
| 183 | + } |
| 184 | + |
| 185 | + if (arc.isDataDependencyArc() || arc.isControlDependencyArc() || arc.isSummaryArc()) { |
| 186 | + res.addAll(this.doFindReachableFormalInNodes(nextNode, visited)); |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + return res; |
| 192 | + } |
| 193 | +} |
0 commit comments