Skip to content

Commit 5256c0b

Browse files
Java: Improve PrintAst tests and rename things
Add tests for `EnhcancedForStmt`s and `InstanceOfExpr`s. Rename LocalVarDeclParent to SingleLocalVarDeclParent
1 parent 9c8a468 commit 5256c0b

File tree

5 files changed

+74
-22
lines changed

5 files changed

+74
-22
lines changed

java/ql/src/semmle/code/java/PrintAst.qll

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ private newtype TPrintAstNode =
115115
TElementNode(Element el) { shouldPrint(el, _) } or
116116
TForInitNode(ForStmt fs) { shouldPrint(fs, _) and exists(fs.getAnInit()) } or
117117
TLocalVarDeclNode(LocalVariableDeclExpr lvde) {
118-
shouldPrint(lvde, _) and lvde.getParent() instanceof LocalVarDeclParent
118+
shouldPrint(lvde, _) and lvde.getParent() instanceof SingleLocalVarDeclParent
119119
} or
120120
TAnnotationsNode(Annotatable ann) {
121121
shouldPrint(ann, _) and ann.hasAnnotation() and not partOfAnnotation(ann)
@@ -266,13 +266,11 @@ final class AnnotationPartNode extends ExprStmtNode {
266266
}
267267

268268
private Expr getAnAnnotationChild() {
269-
(
270-
result = element.(Annotation).getValue(_)
271-
or
272-
result = element.(ArrayInit).getAnInit()
273-
or
274-
result = element.(ArrayInit).(Annotatable).getAnAnnotation()
275-
)
269+
result = element.(Annotation).getValue(_)
270+
or
271+
result = element.(ArrayInit).getAnInit()
272+
or
273+
result = element.(ArrayInit).(Annotatable).getAnAnnotation()
276274
}
277275
}
278276

@@ -334,11 +332,11 @@ final class ForStmtNode extends ExprStmtNode {
334332
}
335333

336334
/**
337-
* An element that can be the parent of a `LocalVariableDeclExpr` for which we want
335+
* An element that can be the parent of up to one `LocalVariableDeclExpr` for which we want
338336
* to use a synthetic node to hold the variable declaration and its `TypeAccess`.
339337
*/
340-
private class LocalVarDeclParent extends ExprOrStmt {
341-
LocalVarDeclParent() {
338+
private class SingleLocalVarDeclParent extends ExprOrStmt {
339+
SingleLocalVarDeclParent() {
342340
this instanceof EnhancedForStmt or
343341
this instanceof CatchClause or
344342
this.(InstanceOfExpr).isPattern()
@@ -352,17 +350,16 @@ private class LocalVarDeclParent extends ExprOrStmt {
352350
}
353351

354352
/**
355-
* A node representing an element that can be the parent of a `LocalVariableDeclExpr` for which we
353+
* A node representing an element that can be the parent of up to one `LocalVariableDeclExpr` for which we
356354
* want to use a synthetic node to variable declaration and its type access.
357355
*
358-
* Excludes:
359-
* - `LocalVariableDeclStmt` because a synthetic node isn't needed
360-
* - `ForStmt` becasue a different synthetic node is already used
356+
* Excludes `LocalVariableDeclStmt` and `ForStmt`, as they can hold multiple declarations.
357+
* For these cases, either a synthetic node is not necassary or a different synthetic node is used.
361358
*/
362-
final class LocalVarDeclParentNode extends ExprStmtNode {
363-
LocalVarDeclParent lvdp;
359+
final class SingleLocalVarDeclParentNode extends ExprStmtNode {
360+
SingleLocalVarDeclParent lvdp;
364361

365-
LocalVarDeclParentNode() { lvdp = element }
362+
SingleLocalVarDeclParentNode() { lvdp = element }
366363

367364
override PrintAstNode getChild(int childIndex) {
368365
result = super.getChild(childIndex) and
@@ -560,7 +557,7 @@ final class LocalVarDeclSynthNode extends PrintAstNode, TLocalVarDeclNode {
560557

561558
LocalVarDeclSynthNode() { this = TLocalVarDeclNode(lvde) }
562559

563-
override string toString() { result = "(Local Variable Declaration)" }
560+
override string toString() { result = "(Single Local Variable Declaration)" }
564561

565562
override ElementNode getChild(int childIndex) {
566563
childIndex = 0 and

java/ql/test/library-tests/java7/MultiCatch/PrintAst.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ MultiCatch.java:
2121
# 14| 0: [ClassInstanceExpr] new SQLException(...)
2222
# 14| -3: [TypeAccess] SQLException
2323
# 15| 0: [CatchClause] stmt
24-
#-----| 0: (Local Variable Declaration)
24+
#-----| 0: (Single Local Variable Declaration)
2525
# 15| 0: [UnionTypeAccess] ...|...
2626
# 15| 0: [TypeAccess] IOException
2727
# 15| 1: [TypeAccess] SQLException
@@ -56,7 +56,7 @@ MultiCatch.java:
5656
# 30| 0: [ClassInstanceExpr] new Exception(...)
5757
# 30| -3: [TypeAccess] Exception
5858
# 31| 0: [CatchClause] stmt
59-
#-----| 0: (Local Variable Declaration)
59+
#-----| 0: (Single Local Variable Declaration)
6060
# 31| 0: [UnionTypeAccess] ...|...
6161
# 31| 0: [TypeAccess] IOException
6262
# 31| 1: [TypeAccess] SQLException
@@ -71,7 +71,7 @@ MultiCatch.java:
7171
# 39| 0: [ClassInstanceExpr] new IOException(...)
7272
# 39| -3: [TypeAccess] IOException
7373
# 40| 0: [CatchClause] stmt
74-
#-----| 0: (Local Variable Declaration)
74+
#-----| 0: (Single Local Variable Declaration)
7575
# 40| 0: [TypeAccess] Exception
7676
# 40| 1: [LocalVariableDeclExpr] e
7777
# 41| 1: [BlockStmt] stmt

java/ql/test/library-tests/printAst/A.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,20 @@ static int doSomething(@SuppressWarnings("all") String text) {
4040
@Ann2(7)
4141
})
4242
String doSomethingElse() { return "c"; }
43+
44+
void varDecls(Object[] things) {
45+
try {
46+
for(Object thing : things) {
47+
if (thing instanceof Integer) {
48+
return;
49+
}
50+
if (thing instanceof String s) {
51+
throw new RuntimeException(s);
52+
}
53+
}
54+
}
55+
catch (RuntimeException rte) {
56+
return;
57+
}
58+
}
4359
}

java/ql/test/library-tests/printAst/PrintAst.expected

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,41 @@ A.java:
8787
# 42| 5: [BlockStmt] stmt
8888
# 42| 0: [ReturnStmt] stmt
8989
# 42| 0: [StringLiteral] "c"
90+
# 44| 9: [Method] varDecls
91+
# 44| 3: [TypeAccess] void
92+
#-----| 4: (Parameters)
93+
# 44| 0: [Parameter] things
94+
# 44| 0: [ArrayTypeAccess] ...[]
95+
# 44| 0: [TypeAccess] Object
96+
# 44| 5: [BlockStmt] stmt
97+
# 45| 0: [TryStmt] stmt
98+
# 45| -1: [BlockStmt] stmt
99+
# 46| 0: [EnhancedForStmt] stmt
100+
#-----| 0: (Single Local Variable Declaration)
101+
# 46| 0: [TypeAccess] Object
102+
# 46| 1: [LocalVariableDeclExpr] thing
103+
# 46| 1: [VarAccess] things
104+
# 46| 2: [BlockStmt] stmt
105+
# 47| 0: [IfStmt] stmt
106+
# 47| 0: [InstanceOfExpr] ...instanceof...
107+
# 47| 0: [VarAccess] thing
108+
# 47| 1: [TypeAccess] Integer
109+
# 47| 1: [BlockStmt] stmt
110+
# 48| 0: [ReturnStmt] stmt
111+
# 50| 1: [IfStmt] stmt
112+
# 50| 0: [InstanceOfExpr] ...instanceof...
113+
#-----| 0: (Single Local Variable Declaration)
114+
# 50| 0: [TypeAccess] String
115+
# 50| 1: [LocalVariableDeclExpr] s
116+
# 50| 0: [VarAccess] thing
117+
# 50| 1: [BlockStmt] stmt
118+
# 51| 0: [ThrowStmt] stmt
119+
# 51| 0: [ClassInstanceExpr] new RuntimeException(...)
120+
# 51| -3: [TypeAccess] RuntimeException
121+
# 51| 0: [VarAccess] s
122+
# 55| 0: [CatchClause] stmt
123+
#-----| 0: (Single Local Variable Declaration)
124+
# 55| 0: [TypeAccess] RuntimeException
125+
# 55| 1: [LocalVariableDeclExpr] rte
126+
# 55| 1: [BlockStmt] stmt
127+
# 56| 0: [ReturnStmt] stmt
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//semmle-extractor-options: --javac-args --enable-preview -source 14 -target 14

0 commit comments

Comments
 (0)