Remove useless actions.

This commit is contained in:
Kui LIU
2017-08-12 19:59:29 +02:00
parent 70c38e361b
commit 325d66d7a9
3 changed files with 144 additions and 11 deletions
@@ -196,14 +196,13 @@ public abstract class Parser implements ParserInterface {
while (actionSets.size() != 0) {
List<HierarchicalActionSet> subSets = new ArrayList<>();
for (HierarchicalActionSet set : actionSets) {
int bugS = set.getStartPosition();
int fixS = set.getFixStartLineNum();
int position = set.getAction().getPosition();
if (set.getActionString().startsWith("INS")) {
if (fixS > fixEndPosition) {
if (position > fixEndPosition) {
continue;
}
} else if (!set.getActionString().startsWith("MOV") && bugS > bugEndPosition) {
} else if (!set.getActionString().startsWith("MOV") && position > bugEndPosition) {
continue;
}
@@ -151,9 +151,11 @@ public class FixedViolationHunkParser extends FixedViolationParser {
fixEndLine = actionFixEnd;
fixEndPosition = hunkActionSet.getFixEndPosition();
}
}
for (HierarchicalActionSet hunkActionSet : hunkActionSets) {
SimplifyTree abstractIdentifier = new SimplifyTree();
abstractIdentifier.abstractTree(hunkActionSet);
abstractIdentifier.abstractTree(hunkActionSet, bugEndPosition);
SimpleTree simpleT = hunkActionSet.getSimpleTree();
if (simpleT == null) { // Failed to get the simple tree for INS actions.
continue;
@@ -190,7 +192,7 @@ public class FixedViolationHunkParser extends FixedViolationParser {
if (size == 1) continue;
counter ++;
String patchPosition = "";//"###:" + counter + "\n" + revFile.getName() + "\nPosition: " + violation.getStartLineNum() + " --> " + violation.getEndLineNum() + "\n@@ -" + bugStartLine + ", " + bugEndLine + " +" + fixStartLine + ", " + fixEndLine + "@@\n";
String patchPosition = "###:" + counter + "\n" + revFile.getName() + "\nPosition: " + violation.getStartLineNum() + " --> " + violation.getEndLineNum() + "\n@@ -" + bugStartLine + ", " + bugEndLine + " +" + fixStartLine + ", " + fixEndLine + "@@\n";
this.patchesSourceCode += Configuration.PATCH_SIGNAL + "\n" + patchPosition + patchSourceCode + "\nAST diff:\n" + getAstEditScripts(hunkActionSets, bugEndPosition, fixEndPosition) + "\n";
this.sizes += size + "\n";
this.astEditScripts += astEditScripts + "\n";
@@ -225,14 +227,13 @@ public class FixedViolationHunkParser extends FixedViolationParser {
editScripts += startStr + actionSet.getActionString() + "\n";
for (HierarchicalActionSet subActionSet : actionSet.getSubActions()) {
int bugS = subActionSet.getStartPosition();
int fixS = subActionSet.getFixStartLineNum();
int position = subActionSet.getAction().getPosition();
if (subActionSet.getActionString().startsWith("INS")) {
if (fixS > fixEndPosition) {
if (position > fixEndPosition) {
continue;
}
} else if (!subActionSet.getActionString().startsWith("MOV") && bugS > bugEndPosition) {
} else if (!subActionSet.getActionString().startsWith("MOV") && position > bugEndPosition) {
continue;
}
editScripts += getActionString(subActionSet, bugEndPosition, fixEndPosition, startStr + "---");
@@ -89,6 +89,69 @@ public class SimplifyTree {
actionSet.setOriginalTree(sourceCodeSimpleTree);
}
/**
* Convert ITree to a source code simple tree, an abstract identifier simple tree, and a semi-source code simple tree.
*
* @param actionSet
*/
public void abstractTree(HierarchicalActionSet actionSet, int bugEndPosition) {
SimpleTree sourceCodeSimpleTree = null; // source code tree and AST node type tree
SimpleTree abstractIdentifierTree = null; // abstract identifier tree
SimpleTree abstractSimpleTree = null; // semi-source code tree. and AST node type tree
SimpleTree simpleTree = null; // source code tree with canonical variable names.
if (actionSet.getActionString().startsWith("INS")) {
List<Action> allMoveActions = getAllMoveActions(actionSet);
if (allMoveActions != null) {
List<Action> actions = new ArrayList<>();
for (Action action : allMoveActions) {
boolean hasParent = false;
ITree parent = action.getNode().getParent();
for (Action act : allMoveActions) {
if (act == action) continue;
ITree actNode = act.getNode();
if (actNode.equals(parent)) {
hasParent = true;
break;
}
}
if (!hasParent) {
actions.add(action);
}
}
// sourceCodeSimpleTree = sourceCodeTree(actions);
simpleTree = canonicalizeSourceCodeTree(actions, null);
}
} else {
ITree tree = actionSet.getNode();
// String astNodeType = actionSet.getAstNodeType();
// if (Checker.containsBodyBlock(astNodeType)) {
// // delete the body block.
// List<ITree> children = tree.getChildren();
// List<ITree> newChildren = new ArrayList<>();
// for (ITree child : children) {
// if (!child.getLabel().endsWith("Body")) {
// newChildren.add(child);
// }
// }
// tree.setChildren(newChildren);
// }
// sourceCodeSimpleTree = originalSourceCodeTree(tree, null);
// abstractIdentifierTree = abstractIdentifierTree(actionSet, tree, null);
// abstractSimpleTree = semiSourceCodeTree(actionSet, tree, null);
if (actionSet.getActionString().startsWith("UPD")) {
simpleTree = canonicalizeSourceCodeTree(tree, null, bugEndPosition);
} else {
simpleTree = canonicalizeSourceCodeTree(tree, null);
}
}
actionSet.setAbstractSimpleTree(abstractSimpleTree);
actionSet.setAbstractIdentifierTree(abstractIdentifierTree);
actionSet.setSimpleTree(simpleTree);
actionSet.setOriginalTree(sourceCodeSimpleTree);
}
private SimpleTree canonicalizeSourceCodeTree(List<Action> actions, SimpleTree parent) {
SimpleTree simpleTree = new SimpleTree();
simpleTree.setLabel("Block");
@@ -171,6 +234,76 @@ public class SimplifyTree {
simpleTree.setParent(parent);
return simpleTree;
}
public SimpleTree canonicalizeSourceCodeTree(ITree tree, SimpleTree parent, int bugEndPosition) {
SimpleTree simpleTree = new SimpleTree();
String label = tree.getLabel();
String astNode = ASTNodeMap.map.get(tree.getType());
List<ITree> children = tree.getChildren();
if (children.size() > 0) {
simpleTree.setNodeType(astNode);
if (astNode.endsWith("Type")) {
simpleTree.setLabel(canonicalizeTypeStr(label).replaceAll(" ", ""));
} else {
if ((astNode.equals("SimpleName") || astNode.equals("MethodInvocation")) && label.startsWith("MethodName:")) {
simpleTree.setNodeType("MethodName");
label = label.substring(11);
label = label.substring(0, label.indexOf(":["));
simpleTree.setLabel(label);
} else {
simpleTree.setLabel(astNode);
}
List<SimpleTree> subTrees = new ArrayList<>();
for (ITree child : children) {
if (child.getPos() > bugEndPosition) continue;
subTrees.add(canonicalizeSourceCodeTree(child, simpleTree));
}
simpleTree.setChildren(subTrees);
}
} else {
if (astNode.endsWith("Name")) {
// variableName, methodName, QualifiedName
if (label.startsWith("MethodName:")) { // <MethodName, name>
simpleTree.setNodeType("MethodName");
label = label.substring(11);
label = label.substring(0, label.indexOf(":["));
simpleTree.setLabel(label);
} else if (label.startsWith("Name:")) {
label = label.substring(5);
char firstChar = label.charAt(0);
if (Character.isUpperCase(firstChar)) {
simpleTree.setNodeType("Name");
simpleTree.setLabel(label);
} else {// variableName: <VariableName, canonicalName>
simpleTree.setNodeType("VariableName");
simpleTree.setLabel(canonicalVariableName(label, tree));
}
} else {// variableName: <VariableName, canonicalName>
simpleTree.setNodeType("VariableName");
simpleTree.setLabel(canonicalVariableName(label, tree));
}
} else {
simpleTree.setNodeType(astNode);
if (astNode.endsWith("Type")) {
simpleTree.setLabel(canonicalizeTypeStr(label).replaceAll(" ", ""));
} else if (astNode.startsWith("Type")) {
simpleTree.setLabel(canonicalizeTypeStr(label).replaceAll(" ", ""));
} else if ((astNode.equals("SimpleName") || astNode.equals("MethodInvocation")) && label.startsWith("MethodName:")) {
simpleTree.setNodeType("MethodName");
label = label.substring(11);
label = label.substring(0, label.indexOf(":["));
simpleTree.setLabel(label);
} else {
simpleTree.setLabel(label.replaceAll(" ", ""));
}
}
}
simpleTree.setParent(parent);
return simpleTree;
}
private String canonicalVariableName(String label, ITree tree) {
ITree parent = tree.getParent();