Fixup pretty-printer and add test

This commit is contained in:
Chris Smowton
2023-11-21 18:42:31 +00:00
parent 3bde66adfb
commit a11c5c7257
5 changed files with 140 additions and 11 deletions

View File

@@ -745,13 +745,17 @@ private class PpSwitchStmt extends PpAst, SwitchStmt {
}
}
private predicate isNonNullDefaultCase(SwitchCase sc) {
sc instanceof DefaultCase and not sc instanceof NullDefaultCase
}
private class PpSwitchCase extends PpAst, SwitchCase {
PpSwitchCase() { not this instanceof PatternCase }
override string getPart(int i) {
i = 0 and result = "default" and this instanceof DefaultCase
i = 0 and result = "default" and isNonNullDefaultCase(this)
or
i = 0 and result = "case " and not this instanceof DefaultCase
i = 0 and result = "case " and not isNonNullDefaultCase(this)
or
i = this.lastConstCaseValueIndex() and result = "default" and this instanceof NullDefaultCase
or
@@ -792,15 +796,11 @@ private class PpPatternCase extends PpAst, PatternCase {
or
i = 3 and result = this.getPattern().asBindingPattern().getName()
or
i = 2 + this.getPatternOffset() and result = ":" and not this.isRule()
i = 4 and result = ":" and not this.isRule()
or
i = 2 + this.getPatternOffset() and result = " -> " and this.isRule()
i = 4 and result = " -> " and this.isRule()
or
i = 4 + this.getPatternOffset() and result = ";" and exists(this.getRuleExpression())
}
private int getPatternOffset() {
if this.getPattern() instanceof LocalVariableDeclExpr then result = 2 else result = 0
i = 6 and result = ";" and exists(this.getRuleExpression())
}
override PpAst getChild(int i) {
@@ -808,9 +808,9 @@ private class PpPatternCase extends PpAst, PatternCase {
or
i = 1 and result = this.getPattern().asRecordPattern()
or
i = 4 and result = this.getRuleExpression()
i = 5 and result = this.getRuleExpression()
or
i = 4 and result = this.getRuleStatement()
i = 5 and result = this.getRuleStatement()
}
}

View File

@@ -0,0 +1,43 @@
public class Test {
record S(int x) { }
record R(S s, String y) { }
public static void test(Object o) {
switch(o) {
case String s:
break;
case R(S(int x), String y):
break;
default:
break;
}
switch(o) {
case String s -> { }
case R(S(int x), String y) -> { }
case null, default -> { }
}
var a = switch(o) {
case String s:
yield 1;
case R(S(int x), String y):
yield x;
case null, default:
yield 2;
};
var b = switch(o) {
case String s -> 1;
case R(S(int x), String y) -> x;
default -> 2;
};
if (o instanceof String s) { }
if (o instanceof R(S(int x), String y)) { }
}
}

View File

@@ -0,0 +1 @@
//semmle-extractor-options: --javac-args --release 21

View File

@@ -0,0 +1,80 @@
| Test.java:1:14:1:17 | Test | 0 | public class Test { |
| Test.java:1:14:1:17 | Test | 1 | public Test() { |
| Test.java:1:14:1:17 | Test | 2 | super(); |
| Test.java:1:14:1:17 | Test | 3 | } |
| Test.java:1:14:1:17 | Test | 4 | |
| Test.java:1:14:1:17 | Test | 5 | static final class S { |
| Test.java:1:14:1:17 | Test | 6 | public final boolean equals(Object p0) { <missing body> } |
| Test.java:1:14:1:17 | Test | 7 | |
| Test.java:1:14:1:17 | Test | 8 | public final int hashCode() { <missing body> } |
| Test.java:1:14:1:17 | Test | 9 | |
| Test.java:1:14:1:17 | Test | 10 | public final String toString() { <missing body> } |
| Test.java:1:14:1:17 | Test | 11 | |
| Test.java:1:14:1:17 | Test | 12 | public int x() { <missing body> } |
| Test.java:1:14:1:17 | Test | 13 | |
| Test.java:1:14:1:17 | Test | 14 | S(int x) { |
| Test.java:1:14:1:17 | Test | 15 | super(); |
| Test.java:1:14:1:17 | Test | 16 | this.x = x; |
| Test.java:1:14:1:17 | Test | 17 | } |
| Test.java:1:14:1:17 | Test | 18 | |
| Test.java:1:14:1:17 | Test | 19 | private final int x; |
| Test.java:1:14:1:17 | Test | 20 | } |
| Test.java:1:14:1:17 | Test | 21 | |
| Test.java:1:14:1:17 | Test | 22 | static final class R { |
| Test.java:1:14:1:17 | Test | 23 | public final boolean equals(Object p0) { <missing body> } |
| Test.java:1:14:1:17 | Test | 24 | |
| Test.java:1:14:1:17 | Test | 25 | public final int hashCode() { <missing body> } |
| Test.java:1:14:1:17 | Test | 26 | |
| Test.java:1:14:1:17 | Test | 27 | public S s() { <missing body> } |
| Test.java:1:14:1:17 | Test | 28 | |
| Test.java:1:14:1:17 | Test | 29 | public final String toString() { <missing body> } |
| Test.java:1:14:1:17 | Test | 30 | |
| Test.java:1:14:1:17 | Test | 31 | public String y() { <missing body> } |
| Test.java:1:14:1:17 | Test | 32 | |
| Test.java:1:14:1:17 | Test | 33 | R(S s, String y) { |
| Test.java:1:14:1:17 | Test | 34 | super(); |
| Test.java:1:14:1:17 | Test | 35 | this.s = s; |
| Test.java:1:14:1:17 | Test | 36 | this.y = y; |
| Test.java:1:14:1:17 | Test | 37 | } |
| Test.java:1:14:1:17 | Test | 38 | |
| Test.java:1:14:1:17 | Test | 39 | private final S s; |
| Test.java:1:14:1:17 | Test | 40 | |
| Test.java:1:14:1:17 | Test | 41 | private final String y; |
| Test.java:1:14:1:17 | Test | 42 | } |
| Test.java:1:14:1:17 | Test | 43 | |
| Test.java:1:14:1:17 | Test | 44 | public static void test(Object o) { |
| Test.java:1:14:1:17 | Test | 45 | switch (o) { |
| Test.java:1:14:1:17 | Test | 46 | case String s: |
| Test.java:1:14:1:17 | Test | 47 | break; |
| Test.java:1:14:1:17 | Test | 48 | case R(S(x), y): |
| Test.java:1:14:1:17 | Test | 49 | break; |
| Test.java:1:14:1:17 | Test | 50 | default: |
| Test.java:1:14:1:17 | Test | 51 | break; |
| Test.java:1:14:1:17 | Test | 52 | } |
| Test.java:1:14:1:17 | Test | 53 | switch (o) { |
| Test.java:1:14:1:17 | Test | 54 | case String s -> { |
| Test.java:1:14:1:17 | Test | 55 | } |
| Test.java:1:14:1:17 | Test | 56 | case R(S(x), y) -> { |
| Test.java:1:14:1:17 | Test | 57 | } |
| Test.java:1:14:1:17 | Test | 58 | case default -> { |
| Test.java:1:14:1:17 | Test | 59 | } |
| Test.java:1:14:1:17 | Test | 60 | } |
| Test.java:1:14:1:17 | Test | 61 | var a = switch (o) { |
| Test.java:1:14:1:17 | Test | 62 | case String s: |
| Test.java:1:14:1:17 | Test | 63 | yield 1; |
| Test.java:1:14:1:17 | Test | 64 | case R(S(x), y): |
| Test.java:1:14:1:17 | Test | 65 | yield x; |
| Test.java:1:14:1:17 | Test | 66 | case default: |
| Test.java:1:14:1:17 | Test | 67 | yield 2; |
| Test.java:1:14:1:17 | Test | 68 | }; |
| Test.java:1:14:1:17 | Test | 69 | var b = switch (o) { |
| Test.java:1:14:1:17 | Test | 70 | case String s -> 1; |
| Test.java:1:14:1:17 | Test | 71 | case R(S(x), y) -> x; |
| Test.java:1:14:1:17 | Test | 72 | default -> 2; |
| Test.java:1:14:1:17 | Test | 73 | }; |
| Test.java:1:14:1:17 | Test | 74 | if (o instanceof String s) { |
| Test.java:1:14:1:17 | Test | 75 | } |
| Test.java:1:14:1:17 | Test | 76 | if (o instanceof R(S(x), y)) { |
| Test.java:1:14:1:17 | Test | 77 | } |
| Test.java:1:14:1:17 | Test | 78 | } |
| Test.java:1:14:1:17 | Test | 79 | } |

View File

@@ -0,0 +1,5 @@
import semmle.code.java.PrettyPrintAst
from ClassOrInterface cori, string s, int line
where pp(cori, s, line) and cori.fromSource()
select cori, line, s order by line