Skip to content

Commit a11c5c7

Browse files
committed
Fixup pretty-printer and add test
1 parent 3bde66a commit a11c5c7

File tree

5 files changed

+140
-11
lines changed

5 files changed

+140
-11
lines changed

java/ql/lib/semmle/code/java/PrettyPrintAst.qll

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -745,13 +745,17 @@ private class PpSwitchStmt extends PpAst, SwitchStmt {
745745
}
746746
}
747747

748+
private predicate isNonNullDefaultCase(SwitchCase sc) {
749+
sc instanceof DefaultCase and not sc instanceof NullDefaultCase
750+
}
751+
748752
private class PpSwitchCase extends PpAst, SwitchCase {
749753
PpSwitchCase() { not this instanceof PatternCase }
750754

751755
override string getPart(int i) {
752-
i = 0 and result = "default" and this instanceof DefaultCase
756+
i = 0 and result = "default" and isNonNullDefaultCase(this)
753757
or
754-
i = 0 and result = "case " and not this instanceof DefaultCase
758+
i = 0 and result = "case " and not isNonNullDefaultCase(this)
755759
or
756760
i = this.lastConstCaseValueIndex() and result = "default" and this instanceof NullDefaultCase
757761
or
@@ -792,25 +796,21 @@ private class PpPatternCase extends PpAst, PatternCase {
792796
or
793797
i = 3 and result = this.getPattern().asBindingPattern().getName()
794798
or
795-
i = 2 + this.getPatternOffset() and result = ":" and not this.isRule()
799+
i = 4 and result = ":" and not this.isRule()
796800
or
797-
i = 2 + this.getPatternOffset() and result = " -> " and this.isRule()
801+
i = 4 and result = " -> " and this.isRule()
798802
or
799-
i = 4 + this.getPatternOffset() and result = ";" and exists(this.getRuleExpression())
800-
}
801-
802-
private int getPatternOffset() {
803-
if this.getPattern() instanceof LocalVariableDeclExpr then result = 2 else result = 0
803+
i = 6 and result = ";" and exists(this.getRuleExpression())
804804
}
805805

806806
override PpAst getChild(int i) {
807807
i = 1 and result = this.getPattern().asBindingPattern().getTypeAccess()
808808
or
809809
i = 1 and result = this.getPattern().asRecordPattern()
810810
or
811-
i = 4 and result = this.getRuleExpression()
811+
i = 5 and result = this.getRuleExpression()
812812
or
813-
i = 4 and result = this.getRuleStatement()
813+
i = 5 and result = this.getRuleStatement()
814814
}
815815
}
816816

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
public class Test {
2+
3+
record S(int x) { }
4+
record R(S s, String y) { }
5+
6+
public static void test(Object o) {
7+
8+
switch(o) {
9+
case String s:
10+
break;
11+
case R(S(int x), String y):
12+
break;
13+
default:
14+
break;
15+
}
16+
17+
switch(o) {
18+
case String s -> { }
19+
case R(S(int x), String y) -> { }
20+
case null, default -> { }
21+
}
22+
23+
var a = switch(o) {
24+
case String s:
25+
yield 1;
26+
case R(S(int x), String y):
27+
yield x;
28+
case null, default:
29+
yield 2;
30+
};
31+
32+
var b = switch(o) {
33+
case String s -> 1;
34+
case R(S(int x), String y) -> x;
35+
default -> 2;
36+
};
37+
38+
if (o instanceof String s) { }
39+
if (o instanceof R(S(int x), String y)) { }
40+
41+
}
42+
43+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//semmle-extractor-options: --javac-args --release 21
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
| Test.java:1:14:1:17 | Test | 0 | public class Test { |
2+
| Test.java:1:14:1:17 | Test | 1 | public Test() { |
3+
| Test.java:1:14:1:17 | Test | 2 | super(); |
4+
| Test.java:1:14:1:17 | Test | 3 | } |
5+
| Test.java:1:14:1:17 | Test | 4 | |
6+
| Test.java:1:14:1:17 | Test | 5 | static final class S { |
7+
| Test.java:1:14:1:17 | Test | 6 | public final boolean equals(Object p0) { <missing body> } |
8+
| Test.java:1:14:1:17 | Test | 7 | |
9+
| Test.java:1:14:1:17 | Test | 8 | public final int hashCode() { <missing body> } |
10+
| Test.java:1:14:1:17 | Test | 9 | |
11+
| Test.java:1:14:1:17 | Test | 10 | public final String toString() { <missing body> } |
12+
| Test.java:1:14:1:17 | Test | 11 | |
13+
| Test.java:1:14:1:17 | Test | 12 | public int x() { <missing body> } |
14+
| Test.java:1:14:1:17 | Test | 13 | |
15+
| Test.java:1:14:1:17 | Test | 14 | S(int x) { |
16+
| Test.java:1:14:1:17 | Test | 15 | super(); |
17+
| Test.java:1:14:1:17 | Test | 16 | this.x = x; |
18+
| Test.java:1:14:1:17 | Test | 17 | } |
19+
| Test.java:1:14:1:17 | Test | 18 | |
20+
| Test.java:1:14:1:17 | Test | 19 | private final int x; |
21+
| Test.java:1:14:1:17 | Test | 20 | } |
22+
| Test.java:1:14:1:17 | Test | 21 | |
23+
| Test.java:1:14:1:17 | Test | 22 | static final class R { |
24+
| Test.java:1:14:1:17 | Test | 23 | public final boolean equals(Object p0) { <missing body> } |
25+
| Test.java:1:14:1:17 | Test | 24 | |
26+
| Test.java:1:14:1:17 | Test | 25 | public final int hashCode() { <missing body> } |
27+
| Test.java:1:14:1:17 | Test | 26 | |
28+
| Test.java:1:14:1:17 | Test | 27 | public S s() { <missing body> } |
29+
| Test.java:1:14:1:17 | Test | 28 | |
30+
| Test.java:1:14:1:17 | Test | 29 | public final String toString() { <missing body> } |
31+
| Test.java:1:14:1:17 | Test | 30 | |
32+
| Test.java:1:14:1:17 | Test | 31 | public String y() { <missing body> } |
33+
| Test.java:1:14:1:17 | Test | 32 | |
34+
| Test.java:1:14:1:17 | Test | 33 | R(S s, String y) { |
35+
| Test.java:1:14:1:17 | Test | 34 | super(); |
36+
| Test.java:1:14:1:17 | Test | 35 | this.s = s; |
37+
| Test.java:1:14:1:17 | Test | 36 | this.y = y; |
38+
| Test.java:1:14:1:17 | Test | 37 | } |
39+
| Test.java:1:14:1:17 | Test | 38 | |
40+
| Test.java:1:14:1:17 | Test | 39 | private final S s; |
41+
| Test.java:1:14:1:17 | Test | 40 | |
42+
| Test.java:1:14:1:17 | Test | 41 | private final String y; |
43+
| Test.java:1:14:1:17 | Test | 42 | } |
44+
| Test.java:1:14:1:17 | Test | 43 | |
45+
| Test.java:1:14:1:17 | Test | 44 | public static void test(Object o) { |
46+
| Test.java:1:14:1:17 | Test | 45 | switch (o) { |
47+
| Test.java:1:14:1:17 | Test | 46 | case String s: |
48+
| Test.java:1:14:1:17 | Test | 47 | break; |
49+
| Test.java:1:14:1:17 | Test | 48 | case R(S(x), y): |
50+
| Test.java:1:14:1:17 | Test | 49 | break; |
51+
| Test.java:1:14:1:17 | Test | 50 | default: |
52+
| Test.java:1:14:1:17 | Test | 51 | break; |
53+
| Test.java:1:14:1:17 | Test | 52 | } |
54+
| Test.java:1:14:1:17 | Test | 53 | switch (o) { |
55+
| Test.java:1:14:1:17 | Test | 54 | case String s -> { |
56+
| Test.java:1:14:1:17 | Test | 55 | } |
57+
| Test.java:1:14:1:17 | Test | 56 | case R(S(x), y) -> { |
58+
| Test.java:1:14:1:17 | Test | 57 | } |
59+
| Test.java:1:14:1:17 | Test | 58 | case default -> { |
60+
| Test.java:1:14:1:17 | Test | 59 | } |
61+
| Test.java:1:14:1:17 | Test | 60 | } |
62+
| Test.java:1:14:1:17 | Test | 61 | var a = switch (o) { |
63+
| Test.java:1:14:1:17 | Test | 62 | case String s: |
64+
| Test.java:1:14:1:17 | Test | 63 | yield 1; |
65+
| Test.java:1:14:1:17 | Test | 64 | case R(S(x), y): |
66+
| Test.java:1:14:1:17 | Test | 65 | yield x; |
67+
| Test.java:1:14:1:17 | Test | 66 | case default: |
68+
| Test.java:1:14:1:17 | Test | 67 | yield 2; |
69+
| Test.java:1:14:1:17 | Test | 68 | }; |
70+
| Test.java:1:14:1:17 | Test | 69 | var b = switch (o) { |
71+
| Test.java:1:14:1:17 | Test | 70 | case String s -> 1; |
72+
| Test.java:1:14:1:17 | Test | 71 | case R(S(x), y) -> x; |
73+
| Test.java:1:14:1:17 | Test | 72 | default -> 2; |
74+
| Test.java:1:14:1:17 | Test | 73 | }; |
75+
| Test.java:1:14:1:17 | Test | 74 | if (o instanceof String s) { |
76+
| Test.java:1:14:1:17 | Test | 75 | } |
77+
| Test.java:1:14:1:17 | Test | 76 | if (o instanceof R(S(x), y)) { |
78+
| Test.java:1:14:1:17 | Test | 77 | } |
79+
| Test.java:1:14:1:17 | Test | 78 | } |
80+
| Test.java:1:14:1:17 | Test | 79 | } |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import semmle.code.java.PrettyPrintAst
2+
3+
from ClassOrInterface cori, string s, int line
4+
where pp(cori, s, line) and cori.fromSource()
5+
select cori, line, s order by line

0 commit comments

Comments
 (0)