|
2 | 2 |
|
3 | 3 | import java.util.ArrayList; |
4 | 4 | import java.util.List; |
5 | | -import java.util.stream.Collectors; |
6 | | - |
7 | 5 | import liquidjava.api.CommandLineLauncher; |
8 | 6 | import liquidjava.processor.VCImplication; |
9 | 7 | import liquidjava.rj_language.Predicate; |
10 | 8 | import liquidjava.rj_language.ast.BinaryExpression; |
11 | 9 | import liquidjava.rj_language.ast.Expression; |
12 | 10 | import liquidjava.rj_language.ast.GroupExpression; |
13 | | -import liquidjava.smt.Counterexample; |
14 | 11 | import liquidjava.utils.Utils; |
15 | 12 | import spoon.reflect.cu.SourcePosition; |
16 | 13 | import spoon.reflect.reference.CtTypeReference; |
@@ -128,14 +125,152 @@ public static void smtStart(VCImplication chain, Predicate extraPremise, Predica |
128 | 125 |
|
129 | 126 | /** |
130 | 127 | * Print the simplifier input and output side by side. This keeps the raw expression visible in debug traces while |
131 | | - * callers continue using the simplified expression for user-facing diagnostics. |
| 128 | + * callers continue using the simplified expression for user-facing diagnostics. Long predicates are split on |
| 129 | + * top-level {@code &&} so each conjunct lands on its own line. |
132 | 130 | */ |
133 | 131 | public static void simplification(Expression input, Expression output) { |
134 | 132 | if (!enabled()) { |
135 | 133 | return; |
136 | 134 | } |
137 | | - System.out.println(SMP_TAG + " Before simplification: " + Colors.YELLOW + input + Colors.RESET); |
138 | | - System.out.println(SMP_TAG + " After simplification: " + Colors.BOLD_YELLOW + output + Colors.RESET); |
| 135 | + printSplitConjunction("Before simplification:", Colors.YELLOW, input); |
| 136 | + printSplitConjunction("After simplification: ", Colors.BOLD_YELLOW, output); |
| 137 | + } |
| 138 | + |
| 139 | + private static void printSplitConjunction(String header, String color, Expression exp) { |
| 140 | + List<Expression> conjuncts = new ArrayList<>(); |
| 141 | + flattenConjunction(exp, conjuncts); |
| 142 | + if (conjuncts.size() <= 1) { |
| 143 | + System.out.println(SMP_TAG + " " + header + " " + color + exp + Colors.RESET); |
| 144 | + return; |
| 145 | + } |
| 146 | + System.out.println(SMP_TAG + " " + header); |
| 147 | + String joiner = " " + Colors.GREY + "&&" + Colors.RESET; |
| 148 | + for (int i = 0; i < conjuncts.size(); i++) { |
| 149 | + String suffix = (i < conjuncts.size() - 1) ? joiner : ""; |
| 150 | + System.out.println(SMP_TAG + " " + color + conjuncts.get(i) + Colors.RESET + suffix); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private static final String PASS_NAME_COLOR = Colors.GOLD; |
| 155 | + private static final int PASS_NAME_WIDTH = 28; |
| 156 | + private static int simplificationPass; |
| 157 | + private static String previousSimplification; |
| 158 | + |
| 159 | + /** |
| 160 | + * Start a simplification log. DebugLog owns the running pass number and the previous expression snapshot because |
| 161 | + * both are only needed for debug output. |
| 162 | + */ |
| 163 | + public static void simplificationStart(Expression input) { |
| 164 | + if (!enabled()) { |
| 165 | + return; |
| 166 | + } |
| 167 | + previousSimplification = input.toString(); |
| 168 | + simplificationPass = 0; |
| 169 | + printSimplificationPass(simplificationPass, "initial expression", previousSimplification); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * One line per simplifier phase. |
| 174 | + */ |
| 175 | + public static void simplificationPass(String name, Expression result) { |
| 176 | + if (!enabled()) { |
| 177 | + return; |
| 178 | + } |
| 179 | + String resultStr = result.toString(); |
| 180 | + printSimplificationPass(++simplificationPass, name, previousSimplification, resultStr); |
| 181 | + previousSimplification = resultStr; |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Prints {@code (no change)} when the step left the expression unchanged, and otherwise emits a unified-diff-style |
| 186 | + * pair (red {@code -} for the previous expression with removed tokens highlighted, green {@code +} for the new one |
| 187 | + * with added tokens highlighted), so substitutions inside a long predicate are obvious at a glance. |
| 188 | + * |
| 189 | + * <p> |
| 190 | + * {@code previous} is taken as a string rather than an {@link Expression} because the simplifier mutates the AST in |
| 191 | + * place: caching an {@code Expression} reference and re-stringifying it after a later pass would yield the |
| 192 | + * already-mutated form, masking real changes as "no change". |
| 193 | + */ |
| 194 | + private static void printSimplificationPass(int pass, String name, String previous, String result) { |
| 195 | + if (previous != null && previous.equals(result)) { |
| 196 | + System.out.printf("%s pass %02d: %s %s(no change)%s%n", SMP_TAG, pass, paintPassName(name), Colors.GREY, |
| 197 | + Colors.RESET); |
| 198 | + return; |
| 199 | + } |
| 200 | + System.out.printf("%s pass %02d: %s%s%s%n", SMP_TAG, pass, PASS_NAME_COLOR, name, Colors.RESET); |
| 201 | + if (previous == null) { |
| 202 | + System.out.printf("%s %s%n", SMP_TAG, result); |
| 203 | + return; |
| 204 | + } |
| 205 | + String[] diff = wordDiff(previous, result); |
| 206 | + System.out.printf("%s %s-%s %s%n", SMP_TAG, Colors.RED, Colors.RESET, diff[0]); |
| 207 | + System.out.printf("%s %s+%s %s%n", SMP_TAG, Colors.GREEN, Colors.RESET, diff[1]); |
| 208 | + } |
| 209 | + |
| 210 | + private static void printSimplificationPass(int pass, String name, String result) { |
| 211 | + System.out.printf("%s pass %02d: %s%n %s%n", SMP_TAG, pass, paintPassName(name), result); |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Color the pass name without breaking column alignment: pad to {@link #PASS_NAME_WIDTH} first, then wrap only the |
| 216 | + * visible characters in {@link #PASS_NAME_COLOR}. The trailing spaces stay uncolored so {@code printf}'s width |
| 217 | + * accounting stays correct. |
| 218 | + */ |
| 219 | + private static String paintPassName(String name) { |
| 220 | + int pad = Math.max(0, PASS_NAME_WIDTH - name.length()); |
| 221 | + return PASS_NAME_COLOR + name + Colors.RESET + " ".repeat(pad); |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * Word-level LCS diff. Returns {@code [previousColored, currentColored]} where tokens that don't appear in the LCS |
| 226 | + * are wrapped in red (for the previous string) and green (for the current string). Splitting on a single space is |
| 227 | + * intentional — the {@link Expression#toString()} output spaces operators and operands. |
| 228 | + */ |
| 229 | + private static String[] wordDiff(String previous, String current) { |
| 230 | + String[] prev = previous.split(" "); |
| 231 | + String[] curr = current.split(" "); |
| 232 | + int[][] dp = new int[prev.length + 1][curr.length + 1]; |
| 233 | + for (int i = 1; i <= prev.length; i++) { |
| 234 | + for (int j = 1; j <= curr.length; j++) { |
| 235 | + if (prev[i - 1].equals(curr[j - 1])) { |
| 236 | + dp[i][j] = dp[i - 1][j - 1] + 1; |
| 237 | + } else { |
| 238 | + dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); |
| 239 | + } |
| 240 | + } |
| 241 | + } |
| 242 | + boolean[] prevKept = new boolean[prev.length]; |
| 243 | + boolean[] currKept = new boolean[curr.length]; |
| 244 | + int i = prev.length; |
| 245 | + int j = curr.length; |
| 246 | + while (i > 0 && j > 0) { |
| 247 | + if (prev[i - 1].equals(curr[j - 1])) { |
| 248 | + prevKept[i - 1] = true; |
| 249 | + currKept[j - 1] = true; |
| 250 | + i--; |
| 251 | + j--; |
| 252 | + } else if (dp[i - 1][j] >= dp[i][j - 1]) { |
| 253 | + i--; |
| 254 | + } else { |
| 255 | + j--; |
| 256 | + } |
| 257 | + } |
| 258 | + return new String[] { colorizeDiff(prev, prevKept, Colors.RED), colorizeDiff(curr, currKept, Colors.GREEN) }; |
| 259 | + } |
| 260 | + |
| 261 | + private static String colorizeDiff(String[] tokens, boolean[] kept, String color) { |
| 262 | + StringBuilder sb = new StringBuilder(); |
| 263 | + for (int k = 0; k < tokens.length; k++) { |
| 264 | + if (k > 0) { |
| 265 | + sb.append(' '); |
| 266 | + } |
| 267 | + if (kept[k]) { |
| 268 | + sb.append(tokens[k]); |
| 269 | + } else { |
| 270 | + sb.append(color).append(tokens[k]).append(Colors.RESET); |
| 271 | + } |
| 272 | + } |
| 273 | + return sb.toString(); |
139 | 274 | } |
140 | 275 |
|
141 | 276 | private static String plainLabel(VCImplication node) { |
|
0 commit comments