Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion py-visualise/out/diff.json

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions src/main/java/org/pdgdiff/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.pdgdiff.graph.GraphExporter;
import org.pdgdiff.graph.GraphGenerator;
import org.pdgdiff.graph.model.MyPDG;
import org.pdgdiff.graph.model.MyPDGNode;
import org.pdgdiff.matching.PDGComparator;
import org.pdgdiff.util.SootInitializer;
import soot.Scene;
Expand Down Expand Up @@ -36,8 +38,8 @@ public static void main(String[] args) {
SootClass testAdder2 = Scene.v().getSootClass(class2Name);

// Generate PDGs for all methods in both classes and store in lists
List<HashMutablePDG> pdgsClass1 = generatePDGsForClass(testAdder1);
List<HashMutablePDG> pdgsClass2 = generatePDGsForClass(testAdder2);
List<MyPDG> pdgsClass1 = generatePDGsForClass(testAdder1);
List<MyPDG> pdgsClass2 = generatePDGsForClass(testAdder2);

// Print the number of PDGs generated for each class
System.out.println("PDGs generated for " + testAdder1.getName() + ": " + pdgsClass1.size());
Expand All @@ -57,8 +59,8 @@ public static void main(String[] args) {
}

// Method to generate PDGs for all methods in a given class and store them in a list
private static List<HashMutablePDG> generatePDGsForClass(SootClass sootClass) {
List<HashMutablePDG> pdgList = new ArrayList<>();
private static List<MyPDG> generatePDGsForClass(SootClass sootClass) {
List<MyPDG> pdgList = new ArrayList<>();
System.out.println("Generating PDGs for class: " + sootClass.getName());
// TODO investigate getting metadata from here.
// Iterate over each method in the class
Expand All @@ -70,7 +72,7 @@ private static List<HashMutablePDG> generatePDGsForClass(SootClass sootClass) {
System.out.println("Successfully retrieved active body for: " + method.getName() + " in " + sootClass.getName());

// Generate the PDG for the method
HashMutablePDG pdg = GraphGenerator.generatePDG(sootClass, method);
MyPDG pdg = GraphGenerator.generatePDG(sootClass, method);
if (pdg != null) {
pdgList.add(pdg);
System.out.println("PDG generated for method: " + method.getName());
Expand Down
59 changes: 31 additions & 28 deletions src/main/java/org/pdgdiff/edit/EditScriptGenerator.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.pdgdiff.edit;

import org.pdgdiff.edit.model.*;
import org.pdgdiff.graph.model.MyPDG;
import org.pdgdiff.graph.model.MyPDGNode;
import org.pdgdiff.graph.model.MyPDGNodeType;
import org.pdgdiff.matching.GraphMapping;
import org.pdgdiff.matching.NodeMapping;
import org.pdgdiff.util.CodeAnalysisUtils;
Expand All @@ -21,8 +24,8 @@
public class EditScriptGenerator {

public static List<EditOperation> generateEditScript(
HashMutablePDG srcPDG,
HashMutablePDG dstPDG,
MyPDG srcPDG,
MyPDG dstPDG,
GraphMapping graphMapping,
String srcSourceFilePath,
String dstSourceFilePath,
Expand All @@ -38,15 +41,15 @@ public static List<EditOperation> generateEditScript(

NodeMapping nodeMapping = graphMapping.getNodeMapping(srcPDG);

Map<PDGNode, PDGNode> mappings = nodeMapping.getNodeMapping();
Set<PDGNode> srcNodesMapped = mappings.keySet();
Set<PDGNode> dstNodesMapped = new HashSet<>(mappings.values());
Map<MyPDGNode, MyPDGNode> mappings = nodeMapping.getNodeMapping();
Set<MyPDGNode> srcNodesMapped = mappings.keySet();
Set<MyPDGNode> dstNodesMapped = new HashSet<>(mappings.values());

Set<PDGNode> visitedNodes = new HashSet<>();
Set<MyPDGNode> visitedNodes = new HashSet<>();

// process mapped nodes for updates or moves
for (PDGNode srcNode : srcNodesMapped) {
PDGNode dstNode = mappings.get(srcNode);
for (MyPDGNode srcNode : srcNodesMapped) {
MyPDGNode dstNode = mappings.get(srcNode);

if (!visitedNodes.contains(srcNode)) {
ComparisonResult compResult = nodesAreEqual(srcNode, dstNode, visitedNodes, srcCodeMapper, dstCodeMapper, nodeMapping);
Expand All @@ -63,7 +66,7 @@ public static List<EditOperation> generateEditScript(
int newLineNumber = syntaxDiff.getNewLineNumber();
String oldCodeSnippet = syntaxDiff.getOldCodeSnippet();
String newCodeSnippet = syntaxDiff.getNewCodeSnippet();
PDGNode node = srcNode;
MyPDGNode node = srcNode;
Update update = new Update(node, oldLineNumber, newLineNumber, oldCodeSnippet, newCodeSnippet, syntaxDiff);
editScriptSet.add(update);
}
Expand All @@ -73,7 +76,7 @@ public static List<EditOperation> generateEditScript(
}

// handle deletions
for (PDGNode srcNode : srcPDG) {
for (MyPDGNode srcNode : srcPDG) {
if (!srcNodesMapped.contains(srcNode) && !visitedNodes.contains(srcNode)) {
int lineNumber = getNodeLineNumber(srcNode);
String codeSnippet = srcCodeMapper.getCodeLine(lineNumber);
Expand All @@ -82,7 +85,7 @@ public static List<EditOperation> generateEditScript(
}

// handle insertions
for (PDGNode dstNode : dstPDG) {
for (MyPDGNode dstNode : dstPDG) {
if (!dstNodesMapped.contains(dstNode) && !visitedNodes.contains(dstNode)) {
int lineNumber = getNodeLineNumber(dstNode);
String codeSnippet = dstCodeMapper.getCodeLine(lineNumber);
Expand Down Expand Up @@ -134,20 +137,20 @@ public ComparisonResult(boolean isEqual, boolean isMove, Set<SyntaxDifference> s
}


public static int getNodeLineNumber(PDGNode node) {
if (node.getType() == PDGNode.Type.CFGNODE) {
public static int getNodeLineNumber(MyPDGNode node) {
if (node.getType() == MyPDGNodeType.CFGNODE) {
Block block = (Block) node.getNode();
Unit headUnit = block.getHead();
return getLineNumber(headUnit);
} else if (node.getType() == PDGNode.Type.REGION) {
} else if (node.getType() == MyPDGNodeType.REGION) {
IRegion region = (IRegion) node.getNode();
Unit firstUnit = region.getFirst();
return getLineNumber(firstUnit);
}
return -1;
}

private static ComparisonResult nodesAreEqual(PDGNode n1, PDGNode n2, Set<PDGNode> visitedNodes,
private static ComparisonResult nodesAreEqual(MyPDGNode n1, MyPDGNode n2, Set<MyPDGNode> visitedNodes,
SourceCodeMapper srcCodeMapper, SourceCodeMapper dstCodeMapper,
NodeMapping nodeMapping) {
if (visitedNodes.contains(n1)) {
Expand All @@ -160,16 +163,16 @@ private static ComparisonResult nodesAreEqual(PDGNode n1, PDGNode n2, Set<PDGNod
return new ComparisonResult(false);
}

if (n1.getType() == PDGNode.Type.CFGNODE) {
if (n1.getType() == MyPDGNodeType.CFGNODE) {
return compareCFGNodes(n1, n2, srcCodeMapper, dstCodeMapper);
} else if (n1.getType() == PDGNode.Type.REGION) {
} else if (n1.getType() == MyPDGNodeType.REGION) {
return compareRegionNodes(n1, n2, visitedNodes, srcCodeMapper, dstCodeMapper, nodeMapping);
}

return new ComparisonResult(true);
}

private static ComparisonResult compareRegionNodes(PDGNode n1, PDGNode n2, Set<PDGNode> visitedNodes,
private static ComparisonResult compareRegionNodes(MyPDGNode n1, MyPDGNode n2, Set<MyPDGNode> visitedNodes,
SourceCodeMapper srcCodeMapper, SourceCodeMapper dstCodeMapper,
NodeMapping nodeMapping) {
IRegion region1 = (IRegion) n1.getNode();
Expand All @@ -181,11 +184,11 @@ private static ComparisonResult compareRegionNodes(PDGNode n1, PDGNode n2, Set<P
Set<SyntaxDifference> differences = compareUnitLists(units1, units2, srcCodeMapper, dstCodeMapper);

// recurively compare child regions
List<PDGNode> childNodes1 = getRegionChildNodes(n1);
List<PDGNode> childNodes2 = getRegionChildNodes(n2);
List<MyPDGNode> childNodes1 = getRegionChildNodes(n1);
List<MyPDGNode> childNodes2 = getRegionChildNodes(n2);

for (PDGNode child1 : childNodes1) {
PDGNode child2 = nodeMapping.getMappedNode(child1);
for (MyPDGNode child1 : childNodes1) {
MyPDGNode child2 = nodeMapping.getMappedNode(child1);
if (child2 != null) {
ComparisonResult compResult = nodesAreEqual(child1, child2, visitedNodes, srcCodeMapper, dstCodeMapper, nodeMapping);
if (!compResult.isEqual) {
Expand All @@ -197,8 +200,8 @@ private static ComparisonResult compareRegionNodes(PDGNode n1, PDGNode n2, Set<P
}
}

for (PDGNode child2 : childNodes2) {
PDGNode child1 = nodeMapping.getReverseMappedNode(child2);
for (MyPDGNode child2 : childNodes2) {
MyPDGNode child1 = nodeMapping.getReverseMappedNode(child2);
if (child1 == null) {
// node has been inserted
differences.add(new SyntaxDifference(null, child2, srcCodeMapper, dstCodeMapper));
Expand All @@ -214,15 +217,15 @@ private static ComparisonResult compareRegionNodes(PDGNode n1, PDGNode n2, Set<P


// helper class cos soot classes is not modifiable
private static List<PDGNode> getRegionChildNodes(PDGNode regionNode) {
List<PDGNode> childNodes = new ArrayList<>();
for (PDGNode dependent : regionNode.getDependents()) {
private static List<MyPDGNode> getRegionChildNodes(MyPDGNode regionNode) {
List<MyPDGNode> childNodes = new ArrayList<>();
for (MyPDGNode dependent : regionNode.getDependents()) {
childNodes.add(dependent);
}
return childNodes;
}

private static ComparisonResult compareCFGNodes(PDGNode n1, PDGNode n2,
private static ComparisonResult compareCFGNodes(MyPDGNode n1, MyPDGNode n2,
SourceCodeMapper srcCodeMapper, SourceCodeMapper dstCodeMapper) {
Block block1 = (Block) n1.getNode();
Block block2 = (Block) n2.getNode();
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/pdgdiff/edit/model/Delete.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.pdgdiff.edit.model;

import org.pdgdiff.graph.model.MyPDGNode;
import soot.toolkits.graph.pdg.PDGNode;

import java.util.Objects;
Expand All @@ -8,7 +9,7 @@ public class Delete extends EditOperation {
private int lineNumber;
private String codeSnippet;

public Delete(PDGNode node, int lineNumber, String codeSnippet) {
public Delete(MyPDGNode node, int lineNumber, String codeSnippet) {
super(node);
this.lineNumber = lineNumber;
this.codeSnippet = codeSnippet;
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/pdgdiff/edit/model/EditOperation.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package org.pdgdiff.edit.model;

import org.pdgdiff.graph.model.MyPDGNode;
import soot.toolkits.graph.pdg.PDGNode;

public abstract class EditOperation {
protected PDGNode node;
protected MyPDGNode node;

public EditOperation(PDGNode node) {
public EditOperation(MyPDGNode node) {
this.node = node;
}

public PDGNode getNode() {
public MyPDGNode getNode() {
return node;
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/pdgdiff/edit/model/Insert.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.pdgdiff.edit.model;

import org.pdgdiff.graph.model.MyPDGNode;
import soot.toolkits.graph.pdg.PDGNode;

import java.util.Objects;
Expand All @@ -8,7 +9,7 @@ public class Insert extends EditOperation {
private int lineNumber;
private String codeSnippet;

public Insert(PDGNode node, int lineNumber, String codeSnippet) {
public Insert(MyPDGNode node, int lineNumber, String codeSnippet) {
super(node);
this.lineNumber = lineNumber;
this.codeSnippet = codeSnippet;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/pdgdiff/edit/model/Move.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.pdgdiff.edit.model;

import org.pdgdiff.graph.model.MyPDGNode;
import soot.toolkits.graph.pdg.PDGNode;

import java.util.Objects;
Expand All @@ -13,7 +14,7 @@ public class Move extends EditOperation {
private int newLineNumber;
private String codeSnippet;

public Move(PDGNode node, int oldLineNumber, int newLineNumber, String codeSnippet) {
public Move(MyPDGNode node, int oldLineNumber, int newLineNumber, String codeSnippet) {
super(node);
this.oldLineNumber = oldLineNumber;
this.newLineNumber = newLineNumber;
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/org/pdgdiff/edit/model/SyntaxDifference.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.pdgdiff.edit.model;

import org.pdgdiff.edit.EditScriptGenerator;
import org.pdgdiff.graph.model.MyPDGNode;
import org.pdgdiff.util.SourceCodeMapper;
import soot.Unit;
import soot.tagkit.LineNumberTag;
import soot.toolkits.graph.pdg.PDGNode;

import java.util.Objects;

Expand All @@ -14,8 +14,8 @@
public class SyntaxDifference {
private Unit oldUnit;
private Unit newUnit;
private PDGNode oldNode;
private PDGNode newNode;
private MyPDGNode oldNode;
private MyPDGNode newNode;
private String message;

private int oldLineNumber;
Expand All @@ -38,7 +38,7 @@ public SyntaxDifference(Unit oldUnit, Unit newUnit,
this.newJimpleCode = newUnit != null ? newUnit.toString() : null;
}

public SyntaxDifference(PDGNode oldNode, PDGNode newNode,
public SyntaxDifference(MyPDGNode oldNode, MyPDGNode newNode,
SourceCodeMapper oldSourceMapper, SourceCodeMapper newSourceMapper) {
this.oldNode = oldNode;
this.newNode = newNode;
Expand All @@ -62,11 +62,11 @@ public Unit getNewUnit() {
return newUnit;
}

public PDGNode getOldNode() {
public MyPDGNode getOldNode() {
return oldNode;
}

public PDGNode getNewNode() {
public MyPDGNode getNewNode() {
return newNode;
}

Expand Down Expand Up @@ -133,14 +133,14 @@ private int getLineNumber(Unit unit) {
return -1;
}

private int getNodeLineNumber(PDGNode node) {
private int getNodeLineNumber(MyPDGNode node) {
if (node == null) {
return -1;
}
return EditScriptGenerator.getNodeLineNumber(node);
}

private String getNodeCodeSnippet(PDGNode node, SourceCodeMapper codeMapper) {
private String getNodeCodeSnippet(MyPDGNode node, SourceCodeMapper codeMapper) {
int lineNumber = getNodeLineNumber(node);
if (lineNumber != -1) {
return codeMapper.getCodeLine(lineNumber);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/pdgdiff/edit/model/Update.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.pdgdiff.edit.model;

import soot.toolkits.graph.pdg.PDGNode;
import org.pdgdiff.graph.model.MyPDGNode;

import java.util.Objects;

Expand All @@ -14,7 +14,7 @@ public class Update extends EditOperation {
private String newCodeSnippet;
private SyntaxDifference syntaxDifference;

public Update(PDGNode node, int oldLineNumber, int newLineNumber,
public Update(MyPDGNode node, int oldLineNumber, int newLineNumber,
String oldCodeSnippet, String newCodeSnippet,
SyntaxDifference syntaxDifference) {
super(node);
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/pdgdiff/graph/GraphExporter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.pdgdiff.graph;

import org.pdgdiff.client.PDGDotVisualizer;
import org.pdgdiff.graph.model.MyPDG;
import soot.SootMethod;
import soot.toolkits.graph.pdg.HashMutablePDG;

Expand Down Expand Up @@ -36,9 +37,9 @@ public static void clearOutputFolder(String folderPath) {
* @param dotFileName The filename for the DOT file
* @param txtFileName The filename for the text file
*/
public static void exportPDG(HashMutablePDG pdg, String dotFileName, String txtFileName) throws IOException {
public static void exportPDG(MyPDG pdg, String dotFileName, String txtFileName) throws IOException {
// Get the method associated with the PDG via the UnitGraph in HashMutablePDG
SootMethod method = pdg.getCFG().getBody().getMethod();
SootMethod method = pdg.getMethod();

// Export the PDG to a DOT file
exportPDGToDot(method, dotFileName);
Expand All @@ -49,7 +50,7 @@ public static void exportPDG(HashMutablePDG pdg, String dotFileName, String txtF


// Method to export PDG to a file for each class
public static void exportPDGToFile(HashMutablePDG pdg, String fileName, String methodName) throws IOException {
public static void exportPDGToFile(MyPDG pdg, String fileName, String methodName) throws IOException {
PrintWriter writer = null;
try {
writer = new PrintWriter(new FileWriter(fileName, true));
Expand Down
Loading