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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.util.*;
import liquidjava.rj_language.Predicate;
import liquidjava.utils.constants.Formats;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.reference.CtTypeReference;

public class Context {
Expand All @@ -18,6 +20,8 @@ public class Context {
private int counter;
private static Context instance;

private Map<Integer, Map<String, Integer>> lineVariables;

private Context() {
ctxVars = new Stack<>();
ctxVars.add(new ArrayList<>());
Expand All @@ -29,6 +33,7 @@ private Context() {
ghosts = new ArrayList<>();
classStates = new HashMap<>();
counter = 0;
lineVariables = new HashMap<>();
}

public static Context getInstance() {
Expand All @@ -50,6 +55,7 @@ public void reinitializeAllContext() {
ghosts = new ArrayList<>();
classStates = new HashMap<>();
counter = 0;
lineVariables = new HashMap<>();
}

public void enterContext() {
Expand Down Expand Up @@ -363,6 +369,31 @@ public List<AliasWrapper> getAliases() {
return aliases;
}

// ---------------------- Variable Name ----------------------
public String getVariableName(String varName, CtElement element) {
boolean isValidPosition = element.getPosition().isValidPosition();
int line = isValidPosition ? element.getPosition().getLine() : getCounter();
CtMethod<?> method = element.getParent(CtMethod.class);

if (!isValidPosition) {
return String.format(Formats.INSTANCE, method != null ? method.getSimpleName() + "_" + varName : varName,
line);
}

Map<String, Integer> variables = lineVariables.getOrDefault(line, new HashMap<>());

String key = method != null ? method.getSimpleName() + varName + line : varName + line;

int count = variables.getOrDefault(varName, 0);
variables.put(key, count + 1);

return count == 0
? String.format(Formats.INSTANCE, method != null ? method.getSimpleName() + "_" + varName : varName,
line)
: String.format(Formats.INSTANCE + "_%d",
method != null ? method.getSimpleName() + "_" + varName : varName, line, count);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import liquidjava.smt.SMTEvaluator;
import liquidjava.smt.SMTResult;
import liquidjava.utils.Utils;
import liquidjava.utils.constants.Formats;
import liquidjava.utils.constants.Keys;
import liquidjava.utils.constants.Types;
import spoon.reflect.code.CtExpression;
Expand Down Expand Up @@ -351,7 +350,7 @@ public void checkVariableRefinements(Predicate refinementFound, String simpleNam
cEt = cEt.substituteVariable(Keys.WILDCARD, simpleName);
Predicate cet = cEt.substituteVariable(Keys.WILDCARD, simpleName);

String newName = String.format(Formats.INSTANCE, simpleName, context.getCounter());
String newName = context.getVariableName(simpleName, usage);
Predicate correctNewRefinement = refinementFound.substituteVariable(Keys.WILDCARD, newName);
correctNewRefinement = correctNewRefinement.substituteVariable(Keys.THIS, newName);
cEt = cEt.substituteVariable(simpleName, newName);
Expand Down