Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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,13 +2,16 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import liquidjava.api.CommandLineLauncher;
import liquidjava.processor.VCImplication;
import liquidjava.rj_language.Predicate;
import liquidjava.rj_language.ast.BinaryExpression;
import liquidjava.rj_language.ast.Expression;
import liquidjava.rj_language.ast.GroupExpression;
import liquidjava.rj_language.opt.derivation_node.ValDerivationNode;
import liquidjava.smt.Counterexample;
import liquidjava.utils.Utils;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.reference.CtTypeReference;
Expand All @@ -29,6 +32,7 @@ public final class DebugLog {

private static final String SMT_TAG = Colors.BLUE + "[SMT]" + Colors.RESET;
private static final String SMT_CHECK = Colors.SALMON + "[SMT CHECK]" + Colors.RESET;
private static final String SMP_TAG = Colors.YELLOW + "[SMP]" + Colors.RESET;

private DebugLog() {
}
Expand Down Expand Up @@ -123,6 +127,33 @@ public static void smtStart(VCImplication chain, Predicate extraPremise, Predica
System.out.println(SMT_TAG + " " + formatConclusion(conclusion));
}

/**
* Print the simplifier input and output side by side. This keeps the raw expression visible in debug traces while
* callers continue using the simplified expression for user-facing diagnostics.
*/
public static void simplification(Expression input, Expression output) {
if (!enabled()) {
return;
}
System.out.println(SMP_TAG + " Simplified " + Colors.CYAN + input + Colors.RESET + " to " + Colors.YELLOW
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

we might want this in two lines if its very big so its easier to parse

+ output + Colors.RESET);
}

/**
* Print every assignment returned by the solver before error reporting filters the user-facing counterexample down
* to the variables mentioned in the diagnostic.
*/
public static void counterexample(Counterexample counterexample) {
if (!enabled() || counterexample == null || counterexample.assignments().isEmpty()) {
return;
}
System.out
.println(SMP_TAG
+ " Counterexample: " + Colors.RED + counterexample.assignments().stream()
.map(a -> a.first() + " = " + a.second()).collect(Collectors.joining(" && "))
+ Colors.RESET);
}

private static String plainLabel(VCImplication node) {
return node.getName() + " : " + simpleType(node.getType());
}
Expand Down Expand Up @@ -215,14 +246,14 @@ public static void smtUnsat() {
if (!enabled()) {
return;
}
System.out.println(SMT_TAG + " result: " + Colors.GREEN + "UNSAT (subtype holds)" + Colors.RESET);
System.out.println(SMT_TAG + " Result: " + Colors.GREEN + "UNSAT (subtype holds)" + Colors.RESET);
}

public static void smtSat(Object counterexample) {
if (!enabled()) {
return;
}
String header = SMT_TAG + " result: " + Colors.RED + "SAT (subtype fails)" + Colors.RESET;
String header = SMT_TAG + " Result: " + Colors.RED + "SAT (subtype fails)" + Colors.RESET;
String pretty = formatCounterexample(counterexample);
if (pretty == null) {
System.out.println(header);
Expand Down Expand Up @@ -266,7 +297,7 @@ public static void smtUnknown() {
if (!enabled()) {
return;
}
System.out.println(SMT_TAG + " result: " + Colors.YELLOW + "UNKNOWN (treated as OK)" + Colors.RESET);
System.out.println(SMT_TAG + " Result: " + Colors.YELLOW + "UNKNOWN (treated as OK)" + Colors.RESET);
}

/**
Expand All @@ -292,7 +323,7 @@ public static void smtError(String message) {
if (!enabled()) {
return;
}
System.out.println(SMT_TAG + " result: " + Colors.RED + "ERROR" + Colors.RESET + " — "
System.out.println(SMT_TAG + " Result: " + Colors.RED + "ERROR" + Colors.RESET + " — "
+ (message == null ? "(no message)" : message));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.List;
import java.util.stream.Collectors;

import liquidjava.api.CommandLineLauncher;
import liquidjava.diagnostics.DebugLog;
import liquidjava.diagnostics.TranslationTable;
import liquidjava.rj_language.ast.Expression;
import liquidjava.rj_language.ast.formatter.VariableFormatter;
Expand Down Expand Up @@ -44,6 +44,8 @@ public String getCounterExampleString() {
if (counterexample == null || counterexample.assignments().isEmpty())
return null;

DebugLog.counterexample(counterexample);

List<String> foundVarNames = new ArrayList<>();
found.getValue().getVariableNames(foundVarNames);
// also keep resolved static-final constants (e.g. Integer.MAX_VALUE) referenced by either side of the
Expand All @@ -53,8 +55,8 @@ public String getCounterExampleString() {
List<String> foundAssignments = found.getValue().getConjuncts().stream().map(Expression::toString).toList();
String counterexampleString = counterexample.assignments().stream()
// only include variables that appear in the found value and are not already fixed there
.filter(a -> CommandLineLauncher.cmdArgs.debugMode || (foundVarNames.contains(a.first())
&& !foundAssignments.contains(a.first() + " == " + a.second())))
.filter(a -> foundVarNames.contains(a.first())
&& !foundAssignments.contains(a.first() + " == " + a.second()))
// format as "var == value"
.map(a -> VariableFormatter.format(a.first()) + " == " + a.second())
// join with "&&"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.Map;
import java.util.stream.Collectors;

import liquidjava.api.CommandLineLauncher;
import liquidjava.diagnostics.DebugLog;
import liquidjava.diagnostics.errors.LJError;
import liquidjava.diagnostics.errors.NotFoundError;
import liquidjava.processor.context.AliasWrapper;
Expand Down Expand Up @@ -257,11 +257,10 @@ public ValDerivationNode simplify(Context context) {
for (AliasWrapper aw : context.getAliases()) {
aliases.put(aw.getName(), aw.createAliasDTO());
}
if (CommandLineLauncher.cmdArgs.debugMode) {
return new ValDerivationNode(exp.clone(), null);
}
// simplify expression
return ExpressionSimplifier.simplify(exp.clone(), aliases);
ValDerivationNode result = ExpressionSimplifier.simplify(exp.clone(), aliases);
DebugLog.simplification(this.getExpression(), result.getValue());
return result;
}

private static boolean isBooleanLiteral(Expression expr, boolean value) {
Expand Down
Loading