Commit 52f3a6cd authored by Carlos Galindo's avatar Carlos Galindo
Browse files

Merge branch '43-use-a-specific-kind-of-set-with-javaparser-nodes' into 'develop'

Resolve "Use a specific kind of set with JavaParser nodes"

Closes #43

See merge request !50
parents fe0b9685 c51cc77b
Loading
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
package es.upv.mist.slicing.graphs;

import es.upv.mist.slicing.utils.ASTUtils;
import org.jgrapht.graph.AbstractGraph;

import java.util.*;
@@ -34,7 +35,7 @@ public abstract class BackwardDataFlowAnalysis<V, E, D> {
            List<V> newWorkList = new LinkedList<>();
            for (V vertex : workList) {
                Set<V> mayAffectVertex = graph.outgoingEdgesOf(vertex).stream()
                        .map(graph::getEdgeTarget).collect(Collectors.toCollection(() -> Collections.newSetFromMap(new IdentityHashMap<>())));
                        .map(graph::getEdgeTarget).collect(Collectors.toCollection(ASTUtils::newIdentityHashSet));
                D newValue = compute(vertex, mayAffectVertex);
                if (!Objects.equals(vertexDataMap.get(vertex), newValue)) {
                    vertexDataMap.put(vertex, newValue);
+5 −2
Original line number Diff line number Diff line
@@ -21,7 +21,10 @@ import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.DirectedPseudograph;
import org.jgrapht.nio.dot.DOTExporter;

import java.util.*;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;

/**
 * A directed graph which displays the available method declarations as nodes and their
@@ -37,7 +40,7 @@ import java.util.*;
 */
public class CallGraph extends DirectedPseudograph<CallGraph.Vertex, CallGraph.Edge<?>> implements Buildable<NodeList<CompilationUnit>> {
    private final Map<CallableDeclaration<?>, CFG> cfgMap;
    private final Map<CallableDeclaration<?>, Vertex> vertexDeclarationMap = new IdentityHashMap<>();
    private final Map<CallableDeclaration<?>, Vertex> vertexDeclarationMap = ASTUtils.newIdentityHashMap();

    private boolean built = false;

+9 −8
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ import es.upv.mist.slicing.nodes.exceptionsensitive.NormalReturnNode;
import es.upv.mist.slicing.nodes.exceptionsensitive.ReturnNode;
import es.upv.mist.slicing.slicing.PseudoPredicateSlicingAlgorithm;
import es.upv.mist.slicing.utils.ASTUtils;
import es.upv.mist.slicing.utils.NodeHashSet;
import es.upv.mist.slicing.utils.Utils;

import java.util.HashSet;
@@ -70,7 +71,7 @@ public class ConditionalControlDependencyBuilder {
    /** Create the {@link ConditionalControlDependencyArc.CC2 CC2}
     * arcs associated to a given {@link CatchClause}. */
    protected void buildCC2(GraphNode<CatchClause> cc) {
        Set<Node> tryBlockInstructs = getTryBlockInstructs(cc.getAstNode());
        NodeHashSet<Node> tryBlockInstructs = getTryBlockInstructs(cc.getAstNode());
        for (Node node : tryBlockInstructs)
            for (GraphNode<?> dst : pdg.findAllNodes(n -> ASTUtils.equalsWithRangeInCU(n.getAstNode(), node)))
                if (isExceptionSource(dst) && hasControlDependencePath(dst, cc, tryBlockInstructs))
@@ -78,13 +79,13 @@ public class ConditionalControlDependencyBuilder {
    }

    /** Obtains the set of AST nodes found within a given {@link CatchClause}. */
    protected static Set<Node> getBlockInstructs(CatchClause cc) {
    protected static NodeHashSet<Node> getBlockInstructs(CatchClause cc) {
        return childNodesOf(cc);
    }

    /** Obtains the set of AST nodes found within the {@link com.github.javaparser.ast.stmt.TryStmt try}
     * associated with the given {@link CatchClause}. */
    protected static Set<Node> getTryBlockInstructs(CatchClause cc) {
    protected static NodeHashSet<Node> getTryBlockInstructs(CatchClause cc) {
        Optional<Node> parent = cc.getParentNode();
        assert parent.isPresent();
        return childNodesOf(parent.get());
@@ -107,7 +108,7 @@ public class ConditionalControlDependencyBuilder {
     * following the rules of the PPDG traversal.
     * @see PseudoPredicateSlicingAlgorithm
     */
    protected boolean hasControlDependencePath(GraphNode<?> a, GraphNode<?> b, Set<Node> universe) {
    protected boolean hasControlDependencePath(GraphNode<?> a, GraphNode<?> b, NodeHashSet<Node> universe) {
        Set<GraphNode<?>> visited = new HashSet<>();
        Set<GraphNode<?>> pending = new HashSet<>();
        pending.add(b);
@@ -134,13 +135,13 @@ public class ConditionalControlDependencyBuilder {
    }

    /** Internal method to find all possible AST nodes that descend from the given argument. */
    protected static Set<Node> childNodesOf(Node parent) {
        Set<Node> result = new HashSet<>();
        Set<Node> pending = new HashSet<>();
    protected static NodeHashSet<Node> childNodesOf(Node parent) {
        NodeHashSet<Node> result = new NodeHashSet<>();
        Set<Node> pending = new NodeHashSet<>();
        pending.add(parent);

        while (!pending.isEmpty()) {
            Set<Node> newPending = new HashSet<>();
            Set<Node> newPending = new NodeHashSet<>();
            for (Node n : pending) {
                newPending.addAll(n.getChildNodes());
                result.add(n);
+5 −2
Original line number Diff line number Diff line
@@ -28,7 +28,10 @@ import es.upv.mist.slicing.nodes.io.CallNode;
import es.upv.mist.slicing.slicing.*;
import es.upv.mist.slicing.utils.ASTUtils;

import java.util.*;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

/**
 * The <b>System Dependence Graph</b> represents the statements of a program in
@@ -43,7 +46,7 @@ import java.util.*;
 * </ol>
 */
public class SDG extends Graph implements Sliceable, Buildable<NodeList<CompilationUnit>> {
    protected final Map<CallableDeclaration<?>, CFG> cfgMap = new IdentityHashMap<>();
    protected final Map<CallableDeclaration<?>, CFG> cfgMap = ASTUtils.newIdentityHashMap();

    protected boolean built = false;
    protected NodeList<CompilationUnit> compilationUnits;
+5 −3
Original line number Diff line number Diff line
@@ -5,6 +5,8 @@ import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.visitor.CloneVisitor;
import es.upv.mist.slicing.nodes.GraphNode;
import es.upv.mist.slicing.utils.ASTUtils;
import es.upv.mist.slicing.utils.NodeHashSet;

import java.util.*;

@@ -57,13 +59,13 @@ public class Slice {
    /** Organize all nodes pertaining to this slice in one or more CompilationUnits. CompilationUnits
     *  themselves need not be part of the slice to be included if any of their components are present. */
    public NodeList<CompilationUnit> toAst() {
        Map<CompilationUnit, Set<Node>> cuMap = new IdentityHashMap<>();
        Map<CompilationUnit, NodeHashSet<Node>> cuMap = ASTUtils.newIdentityHashMap();
        // Add each node to the corresponding bucket of the map
        // Nodes may not belong to a compilation unit (fictional nodes), and they are skipped for the slice.
        for (Node node : nodes) {
            Optional<CompilationUnit> cu = node.findCompilationUnit();
            if (cu.isEmpty()) continue;
            cuMap.putIfAbsent(cu.get(), new HashSet<>());
            cuMap.computeIfAbsent(cu.get(), compilationUnit -> new NodeHashSet<>());
            cuMap.get(cu.get()).add(node);
        }
        // Traverse the AST of each compilation unit, creating a copy and
@@ -71,7 +73,7 @@ public class Slice {
        NodeList<CompilationUnit> cus = new NodeList<>();
        SlicePruneVisitor sliceVisitor = new SlicePruneVisitor();
        CloneVisitor cloneVisitor = new CloneVisitor();
        for (Map.Entry<CompilationUnit, Set<Node>> entry : cuMap.entrySet()) {
        for (Map.Entry<CompilationUnit, NodeHashSet<Node>> entry : cuMap.entrySet()) {
            CompilationUnit clone = (CompilationUnit) entry.getKey().accept(cloneVisitor, null);
            if (entry.getKey().getStorage().isPresent())
                clone.setStorage(entry.getKey().getStorage().get().getPath(),
Loading