From 5cb5ee026c67ab7f0eb59040224bb63c4206d067 Mon Sep 17 00:00:00 2001 From: Chris Smowton Date: Fri, 22 Mar 2024 20:51:10 +0000 Subject: [PATCH] Fix pretty-printing of anonymous vars and multiple patterns; add test --- java/ql/lib/semmle/code/java/PrettyPrintAst.qll | 14 ++++++++++---- java/ql/test/library-tests/prettyprint/Test.java | 7 +++++++ java/ql/test/library-tests/prettyprint/options | 2 +- java/ql/test/library-tests/prettyprint/pp.expected | 10 ++++++++-- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/java/ql/lib/semmle/code/java/PrettyPrintAst.qll b/java/ql/lib/semmle/code/java/PrettyPrintAst.qll index b91d371b187..36f5ce1ed89 100644 --- a/java/ql/lib/semmle/code/java/PrettyPrintAst.qll +++ b/java/ql/lib/semmle/code/java/PrettyPrintAst.qll @@ -400,7 +400,8 @@ private class PpInstanceOfExpr extends PpAst, InstanceOfExpr { private class PpLocalVariableDeclExpr extends PpAst, LocalVariableDeclExpr { override string getPart(int i) { - i = 0 and result = this.getName() + i = 0 and + (if this.isAnonymous() then result = "_" else result = this.getName()) or i = 1 and result = " = " and exists(this.getInit()) } @@ -793,7 +794,11 @@ private class PpPatternCase extends PpAst, PatternCase { or i = base + 2 and this.getPatternAtIndex(n) instanceof LocalVariableDeclExpr and - not this.isAnonymousPattern(n) and + ( + exists(this.getPatternAtIndex(n).asBindingOrUnnamedPattern().getTypeAccess()) + or + not this.isAnonymousPattern(n) + ) and result = " " or i = base + 3 and @@ -815,9 +820,10 @@ private class PpPatternCase extends PpAst, PatternCase { override PpAst getChild(int i) { exists(int n, int base | exists(this.getPatternAtIndex(n)) and base = n * 4 | - i = 1 and result = this.getPatternAtIndex(n).asBindingOrUnnamedPattern().getTypeAccess() + i = base + 1 and + result = this.getPatternAtIndex(n).asBindingOrUnnamedPattern().getTypeAccess() or - i = 1 and result = this.getPatternAtIndex(n).asRecordPattern() + i = base + 1 and result = this.getPatternAtIndex(n).asRecordPattern() ) or exists(int base | base = (max(int n | exists(this.getPatternAtIndex(n))) + 1) * 4 | diff --git a/java/ql/test/library-tests/prettyprint/Test.java b/java/ql/test/library-tests/prettyprint/Test.java index c643955751e..1ffb1671459 100644 --- a/java/ql/test/library-tests/prettyprint/Test.java +++ b/java/ql/test/library-tests/prettyprint/Test.java @@ -45,6 +45,13 @@ public class Test { if (o instanceof R(S(var x), var y)) { } + switch(o) { + case String _, Integer _: + case R(S(_), _): + default: + break; + } + } } diff --git a/java/ql/test/library-tests/prettyprint/options b/java/ql/test/library-tests/prettyprint/options index a0d1b7e7002..5011824a5d7 100644 --- a/java/ql/test/library-tests/prettyprint/options +++ b/java/ql/test/library-tests/prettyprint/options @@ -1 +1 @@ -//semmle-extractor-options: --javac-args --release 21 +//semmle-extractor-options: --javac-args --release 22 diff --git a/java/ql/test/library-tests/prettyprint/pp.expected b/java/ql/test/library-tests/prettyprint/pp.expected index eb4fbaf0633..fb9608f8f41 100644 --- a/java/ql/test/library-tests/prettyprint/pp.expected +++ b/java/ql/test/library-tests/prettyprint/pp.expected @@ -84,5 +84,11 @@ | Test.java:1:14:1:17 | Test | 83 | } | | Test.java:1:14:1:17 | Test | 84 | if (o instanceof R(S(var x), var y)) { | | Test.java:1:14:1:17 | Test | 85 | } | -| Test.java:1:14:1:17 | Test | 86 | } | -| Test.java:1:14:1:17 | Test | 87 | } | +| Test.java:1:14:1:17 | Test | 86 | switch (o) { | +| Test.java:1:14:1:17 | Test | 87 | case String _, Integer _: | +| Test.java:1:14:1:17 | Test | 88 | case R(S(var _), var _): | +| Test.java:1:14:1:17 | Test | 89 | default: | +| Test.java:1:14:1:17 | Test | 90 | break; | +| Test.java:1:14:1:17 | Test | 91 | } | +| Test.java:1:14:1:17 | Test | 92 | } | +| Test.java:1:14:1:17 | Test | 93 | } |