Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add type information for execution difference #240

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ private ProgramStateDiff.UniqueReturnSummary getFirstUniqueReturn(
firstUniqueReturnSummary.setDifferencingTest(executedTest);
firstUniqueReturnSummary.setFirstUniqueVarValLine(p.getLeft());
firstUniqueReturnSummary.setFirstUniqueVarVal(p.getRight());

RuntimeReturnedValue outerMostReturn = jsonStates.stream()
.filter(rt -> Integer.parseInt(rt.getLocation().split(":")[1]) == p.getLeft())
.findFirst()
.get();
firstUniqueReturnSummary.setFirstUniqueVarValType(
findType("{return-object}", p.getRight(), List.of(outerMostReturn)));
break;
}
}
Expand Down Expand Up @@ -215,13 +222,84 @@ private ProgramStateDiff.UniqueStateSummary getFirstDistinctStateOnRelevantLine(
firstUniqueStateSummary.setDifferencingTest(executedTest);
firstUniqueStateSummary.setFirstUniqueVarValLine(p.getLeft());
firstUniqueStateSummary.setFirstUniqueVarVal(p.getRight());

LineSnapshot outerMostLine = lineSnapshots.stream()
.filter(lineSnapshot -> lineSnapshot.getLineNumber() == p.getLeft())
.findFirst()
.get();
List<RuntimeValue> candidateRuntimeValues =
outerMostLine.getStackFrameContext().get(0).getRuntimeValueCollection();

firstUniqueStateSummary.setFirstUniqueVarValType(findType("", p.getRight(), candidateRuntimeValues));
break;
}
}

return firstUniqueStateSummary;
}

private String findType(String prefix, String expectedHash, List<RuntimeValue> candidateRuntimeValues) {
for (RuntimeValue runtimeValue : candidateRuntimeValues) {

RuntimeValue runtimeValueFromHash = getRuntimeValueFromHash(prefix, runtimeValue, expectedHash);

if (runtimeValueFromHash != null) {
return runtimeValueFromHash.getType();
}
}
return null;
}

private RuntimeValue getRuntimeValueFromHash(String prefix, RuntimeValue valueJo, String expectedHash) {
if (Constants.FILE_RELATED_CLASSES.stream().anyMatch(valueJo.getType()::contains)) {
return valueJo;
}

if (valueJo.getFields() != null && !valueJo.getFields().isEmpty()) {
if (valueJo.getKind() == RuntimeValue.Kind.RETURN) {
prefix += ".";
} else {
prefix += (valueJo.getName() == null ? "" : valueJo.getName() + ".");
}
List<RuntimeValue> nestedTypes = valueJo.getFields();

for (RuntimeValue nestedObj : nestedTypes) {
RuntimeValue runtimeValue = getRuntimeValueFromHash(prefix, nestedObj, expectedHash);
if (runtimeValue != null) {
return runtimeValue;
}
}
} else if (valueJo.getArrayElements() != null
&& !valueJo.getArrayElements().isEmpty()) {
List<RuntimeValue> nestedTypes = valueJo.getArrayElements();

prefix += (valueJo.getName() == null ? "" : valueJo.getName());

for (int i = 0; i < nestedTypes.size(); i++) {
RuntimeValue nestedObj = nestedTypes.get(i);
String currentPrefix = prefix + "[" + i + "].";
RuntimeValue runtimeValue = getRuntimeValueFromHash(currentPrefix, nestedObj, expectedHash);
if (runtimeValue != null) {
return runtimeValue;
}
}
} else {
// it's a leaf node
String currentPrefix;
if (valueJo.getKind() == RuntimeValue.Kind.RETURN) {
currentPrefix = prefix;
} else {
currentPrefix = prefix + (valueJo.getName() == null ? "" : valueJo.getName());
}
String value = String.valueOf(valueJo.getValue());
String actualHash = currentPrefix + "=" + value;
if (actualHash.equals(expectedHash)) {
return valueJo;
}
}
return null;
}

Comment on lines +241 to +302
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@khaes-kth I am not proud of the code I wrote. Can you help me refactor it? Especially this block. It is a duplicate of extractVarVals.

private int getVarValHash(int line, String varVal) {
return (line + ":" + varVal).hashCode();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public static class UniqueReturnSummary {
private Integer firstUniqueVarValLine;
private String firstUniqueVarVal, differencingTest;

private String firstUniqueVarValType;

public Integer getFirstUniqueVarValLine() {
return firstUniqueVarValLine;
}
Expand Down Expand Up @@ -71,12 +73,22 @@ public String getDifferencingTest() {
public void setDifferencingTest(String differencingTest) {
this.differencingTest = differencingTest;
}

public String getFirstUniqueVarValType() {
return firstUniqueVarValType;
}

public void setFirstUniqueVarValType(String firstUniqueVarValType) {
this.firstUniqueVarValType = firstUniqueVarValType;
}
}

public static class UniqueStateSummary {
private Integer firstUniqueVarValLine;
private String firstUniqueVarVal, differencingTest;

private String firstUniqueVarValType;

public Integer getFirstUniqueVarValLine() {
return firstUniqueVarValLine;
}
Expand Down Expand Up @@ -108,6 +120,14 @@ public String getDifferencingTest() {
public void setDifferencingTest(String differencingTest) {
this.differencingTest = differencingTest;
}

public String getFirstUniqueVarValType() {
return firstUniqueVarValType;
}

public void setFirstUniqueVarValType(String firstUniqueVarValType) {
this.firstUniqueVarValType = firstUniqueVarValType;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ private void addStateDiffToExecDiffUI(
ProgramStateDiff stateDiff, File ghFullDiff, String testLink, boolean isHitDataIncluded) throws Exception {
if (stateDiff.getFirstPatchedUniqueStateSummary().getFirstUniqueVarVal() != null) {
addStateDiffToExecDiffUI(
stateDiff.getFirstPatchedUniqueStateSummary().getFirstUniqueVarVal(),
stateDiff.getFirstPatchedUniqueStateSummary().getFirstUniqueVarVal() + " ("
+ stateDiff.getFirstPatchedUniqueStateSummary().getFirstUniqueVarValType() + ")",
stateDiff.getFirstPatchedUniqueStateSummary().getFirstUniqueVarValLine(),
"state",
false,
Expand All @@ -154,7 +155,8 @@ private void addStateDiffToExecDiffUI(
}
if (stateDiff.getFirstOriginalUniqueStateSummary().getFirstUniqueVarVal() != null) {
addStateDiffToExecDiffUI(
stateDiff.getFirstOriginalUniqueStateSummary().getFirstUniqueVarVal(),
stateDiff.getFirstOriginalUniqueStateSummary().getFirstUniqueVarVal() + " ("
+ stateDiff.getFirstOriginalUniqueStateSummary().getFirstUniqueVarValType() + ")",
stateDiff.getFirstOriginalUniqueStateSummary().getFirstUniqueVarValLine(),
"state",
true,
Expand All @@ -166,7 +168,8 @@ private void addStateDiffToExecDiffUI(

if (stateDiff.getPatchedUniqueReturn().getFirstUniqueVarVal() != null) {
addStateDiffToExecDiffUI(
stateDiff.getPatchedUniqueReturn().getFirstUniqueVarVal(),
stateDiff.getPatchedUniqueReturn().getFirstUniqueVarVal() + " ("
+ stateDiff.getPatchedUniqueReturn().getFirstUniqueVarValType() + ")",
stateDiff.getPatchedUniqueReturn().getFirstUniqueVarValLine(),
"return",
false,
Expand All @@ -177,7 +180,8 @@ private void addStateDiffToExecDiffUI(
}
if (stateDiff.getOriginalUniqueReturn().getFirstUniqueVarVal() != null) {
addStateDiffToExecDiffUI(
stateDiff.getOriginalUniqueReturn().getFirstUniqueVarVal(),
stateDiff.getOriginalUniqueReturn().getFirstUniqueVarVal() + " ("
+ stateDiff.getOriginalUniqueReturn().getFirstUniqueVarValType() + ")",
stateDiff.getOriginalUniqueReturn().getFirstUniqueVarValLine(),
"return",
true,
Expand Down