Rename variables.

This commit is contained in:
Kui LIU
2018-02-21 17:34:21 +01:00
parent a1e9627566
commit 832fdc0bd6
2 changed files with 102 additions and 54 deletions
@@ -2,17 +2,17 @@ package edu.lu.uni.serval.FixPatternParser.violations;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import com.github.gumtreediff.tree.ITree; import com.github.gumtreediff.tree.ITree;
import edu.lu.uni.serval.FixPatternParser.Tokenizer;
import edu.lu.uni.serval.config.Configuration; import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.diffentry.DiffEntryHunk; import edu.lu.uni.serval.diffentry.DiffEntryHunk;
import edu.lu.uni.serval.diffentry.DiffEntryReader; import edu.lu.uni.serval.diffentry.DiffEntryReader;
import edu.lu.uni.serval.gumtree.regroup.HierarchicalActionSet; import edu.lu.uni.serval.gumtree.regroup.HierarchicalActionSet;
import edu.lu.uni.serval.gumtree.regroup.HunkActionFilter; import edu.lu.uni.serval.gumtree.regroup.HunkActionFilter;
import edu.lu.uni.serval.gumtree.regroup.SimpleTree;
import edu.lu.uni.serval.gumtree.regroup.SimplifyTree; import edu.lu.uni.serval.gumtree.regroup.SimplifyTree;
import edu.lu.uni.serval.violation.code.parser.ViolationSourceCodeTree; import edu.lu.uni.serval.violation.code.parser.ViolationSourceCodeTree;
@@ -145,13 +145,13 @@ public class FixedViolationHunkParser extends FixedViolationParser {
// if (noUpdate(editScriptTokens)) { // if (noUpdate(editScriptTokens)) {
// } // }
String canonicalVariableNames = getBuggyCodeTree(patchHunk, prevFile, revFile);
this.patchesSourceCode += info; this.patchesSourceCode += info;
this.sizes += size + "\n"; this.patchesSourceCode += "\nRenamed_Variables###:" + canonicalVariableNames;
this.astEditScripts += astEditScripts + "\n"; // this.sizes += size + "\n";
// this.astEditScripts += astEditScripts + "\n";
SimpleTree simpleTree = getBuggyCodeTree(patchHunk, bugEndPosition, prevFile, bugStartLine, bugEndLine); // String tokens = Tokenizer.getTokensDeepFirst(simpleTree).trim();
String tokens = Tokenizer.getTokensDeepFirst(simpleTree).trim(); // this.tokensOfSourceCode += tokens + "\n";
this.tokensOfSourceCode += tokens + "\n";
} }
} }
} }
@@ -164,53 +164,44 @@ public class FixedViolationHunkParser extends FixedViolationParser {
return scripts; return scripts;
} }
private SimpleTree getBuggyCodeTree(DiffEntryHunk patchHunk, int bugEndPosition, File prevFile, int bugStartLine, int bugEndLine) { private String getBuggyCodeTree(DiffEntryHunk patchHunk, File prevFile, File revFile) {
SimpleTree simpleTree = new SimpleTree(); int bugStartLine = patchHunk.getBugLineStartNum();
simpleTree.setLabel("Block"); int bugEndLine = bugStartLine + patchHunk.getBugRange() - 1;
simpleTree.setNodeType("Block");
List<SimpleTree> children = new ArrayList<>();
int vStartLine = patchHunk.getBugLineStartNum(); ViolationSourceCodeTree parser = new ViolationSourceCodeTree(prevFile, bugStartLine, bugEndLine);
int vEndLine = vStartLine + patchHunk.getBuggyHunkSize() + 1; parser.extract();
if (bugStartLine < vStartLine && vEndLine < bugEndLine) { List<ITree> matchedTrees = parser.getViolationSourceCodeTrees();
ViolationSourceCodeTree parser = new ViolationSourceCodeTree(prevFile, vStartLine, vEndLine); Map<String, String> renamedVariablesMap = new HashMap<>();
parser.extract(); Map<String, Integer> canonicalVariables = new HashMap<>();
List<ITree> matchedTrees = parser.getViolationSourceCodeTrees(); if (matchedTrees.size() > 0) {
if (matchedTrees.size() > 0) { SimplifyTree st = new SimplifyTree();
for (ITree matchedTree : matchedTrees) { for (ITree matchedTree : matchedTrees) {
SimpleTree simpleT = new SimplifyTree().canonicalizeSourceCodeTree(matchedTree, simpleTree); st.canonicalizeSourceCodeTree(matchedTree);
children.add(simpleT);
}
} }
renamedVariablesMap = st.canonicalVariableMap;
canonicalVariables = st.canonicalVariables;
} }
if (children.size() == 0) {
List<HierarchicalActionSet> hunkActionSets = patchHunk.getActionSets(); int fixStartLine = patchHunk.getFixLineStartNum();
/* int fixEndLine = fixStartLine + patchHunk.getFixRange() - 1;
* Convert the ITree of buggy code to a simple tree. ViolationSourceCodeTree fixedParser = new ViolationSourceCodeTree(revFile, fixStartLine, fixEndLine);
* It will be used to compute the similarity. fixedParser.extract();
*/ matchedTrees = fixedParser.getViolationSourceCodeTrees();
for (HierarchicalActionSet hunkActionSet : hunkActionSets) { if (matchedTrees.size() > 0) {
// TODO simplify buggy tree with buggy code. SimplifyTree st = new SimplifyTree();
/** st.canonicalVariableMap = renamedVariablesMap;
* Select edit scripts for deep learning. st.canonicalVariables = canonicalVariables;
* Edit scripts will be used to mine common fix patterns. for (ITree matchedTree : matchedTrees) {
*/ st.canonicalizeSourceCodeTree(matchedTree);
// // 1. First level: AST node type.
// // 2. source code: raw tokens
// // 3. abstract identifiers:
// // 4. semi-source code:
SimplifyTree abstractIdentifier = new SimplifyTree();
abstractIdentifier.abstractTree(hunkActionSet, bugEndPosition);
SimpleTree simpleT = hunkActionSet.getSimpleTree();
if (simpleT == null) { // Failed to get the simple tree for INS actions.
continue;
}
children.add(simpleT);
} }
renamedVariablesMap = st.canonicalVariableMap;
} }
simpleTree.setChildren(children);
simpleTree.setParent(null); StringBuilder builder = new StringBuilder();
return simpleTree; for (Map.Entry<String, String> entry : renamedVariablesMap.entrySet()) {
builder.append(entry.getKey()).append(" = ").append(entry.getValue()).append("\n");
}
return builder.toString();
} }
private void removeOverlapperdUPD(List<HierarchicalActionSet> actionSets) { private void removeOverlapperdUPD(List<HierarchicalActionSet> actionSets) {
@@ -254,6 +254,44 @@ public class SimplifyTree {
return simpleTree; return simpleTree;
} }
public void canonicalizeSourceCodeTree(ITree tree) {
String label = tree.getLabel();
String astNode = ASTNodeMap.map.get(tree.getType());
List<ITree> children = tree.getChildren();
if (children.size() > 0) {
if (astNode.endsWith("Type")) {
} else {
if ((astNode.equals("SimpleName") || astNode.equals("MethodInvocation"))) {
if (label.startsWith("MethodName:")) {
} else if (label.startsWith("ClassName:")) {
}
} else {
}
for (ITree child : children) {
canonicalizeSourceCodeTree(child);
}
}
} else {
if (astNode.endsWith("Name")) {
// variableName, methodName, QualifiedName
if (label.startsWith("MethodName:")) { // <MethodName, name>
} else if (label.startsWith("ClassName:")) {
} else if (label.startsWith("Name:")) {
label = label.substring(5);
char firstChar = label.charAt(0);
if (Character.isUpperCase(firstChar)) {
// simpleTree.setNodeType("Name");
} else {// variableName: <VariableName, canonicalName>
canonicalVariableName(label, tree);
}
} else {// variableName: <VariableName, canonicalName>
canonicalVariableName(label, tree);
}
}
}
}
public SimpleTree canonicalizeSourceCodeTree(ITree tree, SimpleTree parent, int bugEndPosition) { public SimpleTree canonicalizeSourceCodeTree(ITree tree, SimpleTree parent, int bugEndPosition) {
SimpleTree simpleTree = new SimpleTree(); SimpleTree simpleTree = new SimpleTree();
@@ -430,6 +468,9 @@ public class SimplifyTree {
return null; return null;
} }
public Map<String, String> canonicalVariableMap = new HashMap<>();
public Map<String, Integer> canonicalVariables = new HashMap<>();
private String matchVariableDeclarationExpression(ITree variable, String label) { private String matchVariableDeclarationExpression(ITree variable, String label) {
List<ITree> children = variable.getChildren(); List<ITree> children = variable.getChildren();
ITree type = null; ITree type = null;
@@ -442,8 +483,7 @@ public class SimplifyTree {
ITree simpleName = child.getChild(0); ITree simpleName = child.getChild(0);
if (simpleName.getLabel().equals(label)) { if (simpleName.getLabel().equals(label)) {
String typeStr = canonicalizeTypeStr(type.getLabel()); String typeStr = canonicalizeTypeStr(type.getLabel());
label = typeStr.toLowerCase() + "Var"; return canonicalizeVariable(label, typeStr);
return label;
} }
} }
} }
@@ -458,8 +498,7 @@ public class SimplifyTree {
if (child.getLabel().equals(label)) { if (child.getLabel().equals(label)) {
ITree type = children.get(i - 1); ITree type = children.get(i - 1);
String typeStr = canonicalizeTypeStr(type.getLabel()); String typeStr = canonicalizeTypeStr(type.getLabel());
label = typeStr.toLowerCase() + "Var"; return canonicalizeVariable(label, typeStr);
return label;
} }
break; break;
} }
@@ -467,6 +506,24 @@ public class SimplifyTree {
return null; return null;
} }
private String canonicalizeVariable(String label, String typeStr) {
String key = label + "_" + typeStr;
if (canonicalVariableMap.containsKey(key)) {
label = canonicalVariableMap.get(key);
} else {
label = typeStr.toLowerCase() + "Var";
Integer num = canonicalVariables.get(label);
if (num == null) {
num = 0;
}
num ++;
canonicalVariables.put(label, num);
label += "" + num;
canonicalVariableMap.put(key, label);
}
return label;
}
private String canonicalizeTypeStr(String label) { private String canonicalizeTypeStr(String label) {
String typeStr = label; String typeStr = label;
int index1 = typeStr.indexOf("<"); int index1 = typeStr.indexOf("<");