Java: Improve PrintAst tests and rename things

Add tests for `EnhcancedForStmt`s and `InstanceOfExpr`s.
Rename LocalVarDeclParent to SingleLocalVarDeclParent
This commit is contained in:
Joe
2020-09-25 11:19:13 +01:00
parent 9c8a468237
commit 5256c0ba39
5 changed files with 74 additions and 22 deletions

View File

@@ -115,7 +115,7 @@ private newtype TPrintAstNode =
TElementNode(Element el) { shouldPrint(el, _) } or
TForInitNode(ForStmt fs) { shouldPrint(fs, _) and exists(fs.getAnInit()) } or
TLocalVarDeclNode(LocalVariableDeclExpr lvde) {
shouldPrint(lvde, _) and lvde.getParent() instanceof LocalVarDeclParent
shouldPrint(lvde, _) and lvde.getParent() instanceof SingleLocalVarDeclParent
} or
TAnnotationsNode(Annotatable ann) {
shouldPrint(ann, _) and ann.hasAnnotation() and not partOfAnnotation(ann)
@@ -266,13 +266,11 @@ final class AnnotationPartNode extends ExprStmtNode {
}
private Expr getAnAnnotationChild() {
(
result = element.(Annotation).getValue(_)
or
result = element.(ArrayInit).getAnInit()
or
result = element.(ArrayInit).(Annotatable).getAnAnnotation()
)
result = element.(Annotation).getValue(_)
or
result = element.(ArrayInit).getAnInit()
or
result = element.(ArrayInit).(Annotatable).getAnAnnotation()
}
}
@@ -334,11 +332,11 @@ final class ForStmtNode extends ExprStmtNode {
}
/**
* An element that can be the parent of a `LocalVariableDeclExpr` for which we want
* An element that can be the parent of up to one `LocalVariableDeclExpr` for which we want
* to use a synthetic node to hold the variable declaration and its `TypeAccess`.
*/
private class LocalVarDeclParent extends ExprOrStmt {
LocalVarDeclParent() {
private class SingleLocalVarDeclParent extends ExprOrStmt {
SingleLocalVarDeclParent() {
this instanceof EnhancedForStmt or
this instanceof CatchClause or
this.(InstanceOfExpr).isPattern()
@@ -352,17 +350,16 @@ private class LocalVarDeclParent extends ExprOrStmt {
}
/**
* A node representing an element that can be the parent of a `LocalVariableDeclExpr` for which we
* A node representing an element that can be the parent of up to one `LocalVariableDeclExpr` for which we
* want to use a synthetic node to variable declaration and its type access.
*
* Excludes:
* - `LocalVariableDeclStmt` because a synthetic node isn't needed
* - `ForStmt` becasue a different synthetic node is already used
* Excludes `LocalVariableDeclStmt` and `ForStmt`, as they can hold multiple declarations.
* For these cases, either a synthetic node is not necassary or a different synthetic node is used.
*/
final class LocalVarDeclParentNode extends ExprStmtNode {
LocalVarDeclParent lvdp;
final class SingleLocalVarDeclParentNode extends ExprStmtNode {
SingleLocalVarDeclParent lvdp;
LocalVarDeclParentNode() { lvdp = element }
SingleLocalVarDeclParentNode() { lvdp = element }
override PrintAstNode getChild(int childIndex) {
result = super.getChild(childIndex) and
@@ -560,7 +557,7 @@ final class LocalVarDeclSynthNode extends PrintAstNode, TLocalVarDeclNode {
LocalVarDeclSynthNode() { this = TLocalVarDeclNode(lvde) }
override string toString() { result = "(Local Variable Declaration)" }
override string toString() { result = "(Single Local Variable Declaration)" }
override ElementNode getChild(int childIndex) {
childIndex = 0 and

View File

@@ -21,7 +21,7 @@ MultiCatch.java:
# 14| 0: [ClassInstanceExpr] new SQLException(...)
# 14| -3: [TypeAccess] SQLException
# 15| 0: [CatchClause] stmt
#-----| 0: (Local Variable Declaration)
#-----| 0: (Single Local Variable Declaration)
# 15| 0: [UnionTypeAccess] ...|...
# 15| 0: [TypeAccess] IOException
# 15| 1: [TypeAccess] SQLException
@@ -56,7 +56,7 @@ MultiCatch.java:
# 30| 0: [ClassInstanceExpr] new Exception(...)
# 30| -3: [TypeAccess] Exception
# 31| 0: [CatchClause] stmt
#-----| 0: (Local Variable Declaration)
#-----| 0: (Single Local Variable Declaration)
# 31| 0: [UnionTypeAccess] ...|...
# 31| 0: [TypeAccess] IOException
# 31| 1: [TypeAccess] SQLException
@@ -71,7 +71,7 @@ MultiCatch.java:
# 39| 0: [ClassInstanceExpr] new IOException(...)
# 39| -3: [TypeAccess] IOException
# 40| 0: [CatchClause] stmt
#-----| 0: (Local Variable Declaration)
#-----| 0: (Single Local Variable Declaration)
# 40| 0: [TypeAccess] Exception
# 40| 1: [LocalVariableDeclExpr] e
# 41| 1: [BlockStmt] stmt

View File

@@ -40,4 +40,20 @@ class A {
@Ann2(7)
})
String doSomethingElse() { return "c"; }
void varDecls(Object[] things) {
try {
for(Object thing : things) {
if (thing instanceof Integer) {
return;
}
if (thing instanceof String s) {
throw new RuntimeException(s);
}
}
}
catch (RuntimeException rte) {
return;
}
}
}

View File

@@ -87,3 +87,41 @@ A.java:
# 42| 5: [BlockStmt] stmt
# 42| 0: [ReturnStmt] stmt
# 42| 0: [StringLiteral] "c"
# 44| 9: [Method] varDecls
# 44| 3: [TypeAccess] void
#-----| 4: (Parameters)
# 44| 0: [Parameter] things
# 44| 0: [ArrayTypeAccess] ...[]
# 44| 0: [TypeAccess] Object
# 44| 5: [BlockStmt] stmt
# 45| 0: [TryStmt] stmt
# 45| -1: [BlockStmt] stmt
# 46| 0: [EnhancedForStmt] stmt
#-----| 0: (Single Local Variable Declaration)
# 46| 0: [TypeAccess] Object
# 46| 1: [LocalVariableDeclExpr] thing
# 46| 1: [VarAccess] things
# 46| 2: [BlockStmt] stmt
# 47| 0: [IfStmt] stmt
# 47| 0: [InstanceOfExpr] ...instanceof...
# 47| 0: [VarAccess] thing
# 47| 1: [TypeAccess] Integer
# 47| 1: [BlockStmt] stmt
# 48| 0: [ReturnStmt] stmt
# 50| 1: [IfStmt] stmt
# 50| 0: [InstanceOfExpr] ...instanceof...
#-----| 0: (Single Local Variable Declaration)
# 50| 0: [TypeAccess] String
# 50| 1: [LocalVariableDeclExpr] s
# 50| 0: [VarAccess] thing
# 50| 1: [BlockStmt] stmt
# 51| 0: [ThrowStmt] stmt
# 51| 0: [ClassInstanceExpr] new RuntimeException(...)
# 51| -3: [TypeAccess] RuntimeException
# 51| 0: [VarAccess] s
# 55| 0: [CatchClause] stmt
#-----| 0: (Single Local Variable Declaration)
# 55| 0: [TypeAccess] RuntimeException
# 55| 1: [LocalVariableDeclExpr] rte
# 55| 1: [BlockStmt] stmt
# 56| 0: [ReturnStmt] stmt

View File

@@ -0,0 +1 @@
//semmle-extractor-options: --javac-args --enable-preview -source 14 -target 14