Skip to content

Commit 53ab8da

Browse files
Java: PrintAst: Fix failing tests
1 parent 112b6d2 commit 53ab8da

File tree

17 files changed

+213
-73
lines changed

17 files changed

+213
-73
lines changed

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

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ private class ExprOrStmt extends Element {
4646
}
4747
}
4848

49-
/** Holds if the given element does not need to be rendered in the AST, due to being compiler-generated. */
49+
/** Holds if the given element does not need to be rendered in the AST, due to being compiler-generated. */
5050
private predicate isNotNeeded(Element el) {
5151
exists(InitializerMethod im |
5252
el = im
@@ -58,17 +58,33 @@ private predicate isNotNeeded(Element el) {
5858
)
5959
)
6060
or
61-
exists(Constructor c | c.isDefaultConstructor() |
61+
exists(Constructor c | c.isDefaultConstructor() |
6262
el = c
6363
or
6464
el.(ExprOrStmt).getEnclosingCallable() = c
6565
)
6666
or
67+
exists(Constructor c, int sline, int eline, int scol, int ecol |
68+
el.(ExprOrStmt).getEnclosingCallable() = c
69+
|
70+
el.getLocation().hasLocationInfo(_, sline, eline, scol, ecol) and
71+
c.getLocation().hasLocationInfo(_, sline, eline, scol, ecol)
72+
// simply comparing their getLocation() doesn't work as they have distinct but equivalent locations
73+
)
74+
or
6775
isNotNeeded(el.(Expr).getParent*().(Annotation).getAnnotatedElement())
6876
or
6977
isNotNeeded(el.(Parameter).getCallable())
7078
}
7179

80+
/** Holds if the given field would have the same javadoc and annotations as another field declared in the same declaration */
81+
private predicate duplicateMetadata(Field f) {
82+
exists(FieldDeclaration fd |
83+
f = fd.getAField() and
84+
not f = fd.getField(0)
85+
)
86+
}
87+
7288
/**
7389
* Retrieves the canonical QL class(es) for entity `el`
7490
*/
@@ -97,14 +113,18 @@ private predicate locationSortKeys(Element ast, string file, int line, int colum
97113
*/
98114
private newtype TPrintAstNode =
99115
TElementNode(Element el) { shouldPrint(el, _) } or
100-
TAnnotationsNode(Annotatable ann) { shouldPrint(ann, _) and ann.hasAnnotation() and not partOfAnnotation(ann)} or
116+
TAnnotationsNode(Annotatable ann) {
117+
shouldPrint(ann, _) and ann.hasAnnotation() and not partOfAnnotation(ann)
118+
} or
101119
TParametersNode(Callable c) { shouldPrint(c, _) and not c.hasNoParameters() } or
102120
TBaseTypesNode(ClassOrInterface ty) { shouldPrint(ty, _) } or
103-
TGenericTypeNode(GenericType ty) { shouldPrint(ty, _) } or
121+
TGenericTypeNode(GenericType ty) { shouldPrint(ty, _) } or
104122
TGenericCallableNode(GenericCallable c) { shouldPrint(c, _) } or
105123
TDocumentableNode(Documentable d) { shouldPrint(d, _) and exists(d.getJavadoc()) } or
106124
TJavadocNode(Javadoc jd) { exists(Documentable d | d.getJavadoc() = jd | shouldPrint(d, _)) } or
107-
TJavadocElementNode(JavadocElement jd) { exists(Documentable d | d.getJavadoc() = jd.getParent*() | shouldPrint(d, _)) }
125+
TJavadocElementNode(JavadocElement jd) {
126+
exists(Documentable d | d.getJavadoc() = jd.getParent*() | shouldPrint(d, _))
127+
}
108128

109129
/**
110130
* A node in the output tree.
@@ -201,7 +221,7 @@ abstract class ElementNode extends PrintAstNode, TElementNode {
201221
private predicate partOfAnnotation(Expr e) {
202222
e instanceof Annotation
203223
or
204-
e instanceof ArrayInit and
224+
e instanceof ArrayInit and
205225
partOfAnnotation(e.getParent())
206226
}
207227

@@ -236,11 +256,12 @@ final class ExprStmtNode extends ElementNode {
236256
el = element.(LocalClassDeclStmt).getLocalClass()
237257
or
238258
partOfAnnotation(element) and
239-
el = rank[childIndex](Element ch, string file, int line, int column |
240-
ch = getAnAnnotationChild(element) and locationSortKeys(ch, file, line, column)
241-
|
242-
ch order by file, line, column
243-
)
259+
el =
260+
rank[childIndex](Element ch, string file, int line, int column |
261+
ch = getAnAnnotationChild(element) and locationSortKeys(ch, file, line, column)
262+
|
263+
ch order by file, line, column
264+
)
244265
)
245266
or
246267
exists(Element el | result.(AnnotationsNode).getAnnotated() = el |
@@ -292,7 +313,7 @@ final class ParameterNode extends ElementNode {
292313
result.(AnnotationsNode).getAnnotated() = p
293314
or
294315
childIndex = 0 and
295-
result.(ElementNode).getElement().(Expr).getParent() = p
316+
result.(ElementNode).getElement().(Expr).isNthChildOf(p, -1) // type
296317
}
297318
}
298319

@@ -353,10 +374,10 @@ final class FieldDeclNode extends ElementNode {
353374

354375
override PrintAstNode getChild(int childIndex) {
355376
childIndex = -3 and
356-
result.(DocumentableNode).getDocumentable() = decl.getAField()
377+
result.(DocumentableNode).getDocumentable() = decl.getField(0)
357378
or
358379
childIndex = -2 and
359-
result.(AnnotationsNode).getAnnotated() = decl.getAField()
380+
result.(AnnotationsNode).getAnnotated() = decl.getField(0)
360381
or
361382
childIndex = -1 and
362383
result.(ElementNode).getElement() = decl.getTypeAccess()
@@ -398,7 +419,7 @@ final class ImportNode extends ElementNode {
398419
ImportNode() { element instanceof Import }
399420
}
400421

401-
/**
422+
/**
402423
* A node representing a `TypeVariable`.
403424
*/
404425
final class TypeVariableNode extends ElementNode {
@@ -416,7 +437,9 @@ final class TypeVariableNode extends ElementNode {
416437
final class AnnotationsNode extends PrintAstNode, TAnnotationsNode {
417438
Annotatable ann;
418439

419-
AnnotationsNode() { this = TAnnotationsNode(ann) and not isNotNeeded(ann) }
440+
AnnotationsNode() {
441+
this = TAnnotationsNode(ann) and not isNotNeeded(ann) and not duplicateMetadata(ann)
442+
}
420443

421444
override string toString() { result = "(Annotations)" }
422445

@@ -526,13 +549,13 @@ final class GenericCallableNode extends PrintAstNode, TGenericCallableNode {
526549
}
527550

528551
/**
529-
* A node representing the documentation of a `Documentable`.
552+
* A node representing the documentation of a `Documentable`.
530553
* Only rendered if there is at least one `Javadoc` attatched to it.
531554
*/
532555
final class DocumentableNode extends PrintAstNode, TDocumentableNode {
533556
Documentable d;
534557

535-
DocumentableNode() { this = TDocumentableNode(d) }
558+
DocumentableNode() { this = TDocumentableNode(d) and not duplicateMetadata(d) }
536559

537560
override string toString() { result = "(Javadoc)" }
538561

@@ -542,7 +565,7 @@ final class DocumentableNode extends PrintAstNode, TDocumentableNode {
542565
result.getJavadoc() =
543566
rank[childIndex](Javadoc jd, string file, int line, int column |
544567
jd.getCommentedElement() = d and jd.getLocation().hasLocationInfo(file, line, column, _, _)
545-
|
568+
|
546569
jd order by file, line, column
547570
)
548571
}
@@ -554,7 +577,7 @@ final class DocumentableNode extends PrintAstNode, TDocumentableNode {
554577
}
555578

556579
/**
557-
* A node representing a `Javadoc`.
580+
* A node representing a `Javadoc`.
558581
* Only rendered if it is the javadoc of some `Documentable`.
559582
*/
560583
final class JavadocNode extends PrintAstNode, TJavadocNode {
@@ -576,7 +599,8 @@ final class JavadocNode extends PrintAstNode, TJavadocNode {
576599
Javadoc getJavadoc() { result = jd }
577600
}
578601

579-
/** A node representing a `JavadocElement`.
602+
/**
603+
* A node representing a `JavadocElement`.
580604
* Only rendered if it is part of the javadoc of some `Documentable`.
581605
*/
582606
final class JavadocElementNode extends PrintAstNode, TJavadocElementNode {

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,31 @@ jdk/A.java:
33
# 3| 1: [Class] A
44
# 4| 2: [Method] main
55
# 4| 3: [TypeAccess] void
6-
# 4| 4: (Parameters)
6+
#-----| 4: (Parameters)
77
# 4| 0: [Parameter] args
88
# 4| 0: [ArrayTypeAccess] ...[]
99
# 4| 0: [TypeAccess] String
1010
# 4| 5: [BlockStmt] stmt
1111
# 7| 2: [Class] B
1212
# 8| 2: [Method] main
1313
# 8| 3: [TypeAccess] void
14-
# 8| 4: (Parameters)
14+
#-----| 4: (Parameters)
1515
# 8| 0: [Parameter] args
1616
# 8| 0: [ArrayTypeAccess] ...[]
1717
# 8| 0: [TypeAccess] String
1818
# 8| 5: [BlockStmt] stmt
1919
# 11| 3: [Class] C
2020
# 12| 2: [Method] main
2121
# 12| 3: [TypeAccess] void
22-
# 12| 4: (Parameters)
22+
#-----| 4: (Parameters)
2323
# 12| 0: [Parameter] args
2424
# 12| 0: [ArrayTypeAccess] ...[]
2525
# 12| 0: [TypeAccess] String
2626
# 12| 5: [BlockStmt] stmt
2727
# 15| 4: [Class] D
2828
# 16| 2: [Method] main
2929
# 16| 3: [TypeAccess] int
30-
# 16| 4: (Parameters)
30+
#-----| 4: (Parameters)
3131
# 16| 0: [Parameter] args
3232
# 16| 0: [ArrayTypeAccess] ...[]
3333
# 16| 0: [TypeAccess] String
@@ -37,7 +37,7 @@ jdk/A.java:
3737
# 19| 5: [Class] E
3838
# 20| 2: [Method] main
3939
# 20| 3: [TypeAccess] void
40-
# 20| 4: (Parameters)
40+
#-----| 4: (Parameters)
4141
# 20| 0: [Parameter] argc
4242
# 20| 0: [TypeAccess] int
4343
# 20| 1: [Parameter] args
@@ -47,14 +47,14 @@ jdk/A.java:
4747
# 23| 6: [Class] F
4848
# 24| 2: [Method] main
4949
# 24| 3: [TypeAccess] void
50-
# 24| 4: (Parameters)
50+
#-----| 4: (Parameters)
5151
# 24| 0: [Parameter] arg
5252
# 24| 0: [TypeAccess] String
5353
# 24| 5: [BlockStmt] stmt
5454
# 27| 7: [Class] G
5555
# 28| 2: [Method] main
5656
# 28| 3: [TypeAccess] void
57-
# 28| 4: (Parameters)
57+
#-----| 4: (Parameters)
5858
# 28| 0: [Parameter] args
5959
# 28| 0: [ArrayTypeAccess] ...[]
6060
# 28| 0: [ArrayTypeAccess] ...[]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ arrays/B.java:
1313
# 3| 1: [Class] B
1414
# 4| 2: [Method] m
1515
# 4| 3: [TypeAccess] void
16-
# 4| 4: (Parameters)
16+
#-----| 4: (Parameters)
1717
# 4| 0: [Parameter] a
1818
# 4| 0: [TypeAccess] A
1919
# 4| 5: [BlockStmt] stmt

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
Test.java:
22
# 0| [CompilationUnit] Test
33
# 5| 1: [Class] Test
4-
# 1| -4: [Javadoc] /** A JavaDoc comment ... */
5-
# 2| 0: [JavadocText] A JavaDoc comment
6-
# 3| 1: [JavadocText] with multiple lines.
4+
#-----| -4: (Javadoc)
5+
# 1| 1: [Javadoc] /** A JavaDoc comment ... */
6+
# 2| 0: [JavadocText] A JavaDoc comment
7+
# 3| 1: [JavadocText] with multiple lines.
78
# 7| 2: [Method] m
8-
# 6| 0: [Javadoc] /** A JavaDoc comment with a single line. */
9-
# 6| 0: [JavadocText] A JavaDoc comment with a single line.
9+
#-----| 0: (Javadoc)
10+
# 6| 1: [Javadoc] /** A JavaDoc comment with a single line. */
11+
# 6| 0: [JavadocText] A JavaDoc comment with a single line.
1012
# 7| 3: [TypeAccess] void
1113
# 7| 5: [BlockStmt] stmt
1214
# 21| 3: [Method] test
@@ -15,12 +17,14 @@ Test.java:
1517
TestWindows.java:
1618
# 0| [CompilationUnit] TestWindows
1719
# 5| 1: [Class] TestWindows
18-
# 1| -4: [Javadoc] /** A JavaDoc comment ... */
19-
# 2| 0: [JavadocText] A JavaDoc comment
20-
# 3| 1: [JavadocText] with multiple lines.
20+
#-----| -4: (Javadoc)
21+
# 1| 1: [Javadoc] /** A JavaDoc comment ... */
22+
# 2| 0: [JavadocText] A JavaDoc comment
23+
# 3| 1: [JavadocText] with multiple lines.
2124
# 7| 2: [Method] m
22-
# 6| 0: [Javadoc] /** A JavaDoc comment with a single line. */
23-
# 6| 0: [JavadocText] A JavaDoc comment with a single line.
25+
#-----| 0: (Javadoc)
26+
# 6| 1: [Javadoc] /** A JavaDoc comment with a single line. */
27+
# 6| 0: [JavadocText] A JavaDoc comment with a single line.
2428
# 7| 3: [TypeAccess] void
2529
# 7| 5: [BlockStmt] stmt
2630
# 21| 3: [Method] test

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ constants/Constants.java:
33
# 3| 1: [Class] Constants
44
# 4| 2: [Method] constants
55
# 4| 3: [TypeAccess] void
6-
# 4| 4: (Parameters)
6+
#-----| 4: (Parameters)
77
# 4| 0: [Parameter] notConstant
88
# 4| 0: [TypeAccess] int
99
# 4| 5: [BlockStmt] stmt
@@ -80,9 +80,6 @@ constants/Initializers.java:
8080
# 8| -1: [TypeAccess] int
8181
# 10| 6: [Constructor] Initializers
8282
# 10| 5: [BlockStmt] stmt
83-
# 10| 0: [SuperConstructorInvocationStmt] super(...)
84-
# 10| 1: [ExprStmt] stmt
85-
# 10| 0: [MethodAccess] <obinit>(...)
8683
# 12| 2: [ExprStmt] stmt
8784
# 12| 0: [AssignExpr] ...=...
8885
# 12| 0: [VarAccess] IFIELD2
@@ -135,14 +132,15 @@ constants/Initializers.java:
135132
constants/Values.java:
136133
# 0| [CompilationUnit] Values
137134
# 4| 1: [Class] Values
138-
# 3| -4: [Javadoc] /** Tests of the getIntValue() predicate */
139-
# 3| 0: [JavadocText] Tests of the getIntValue() predicate
135+
#-----| -4: (Javadoc)
136+
# 3| 1: [Javadoc] /** Tests of the getIntValue() predicate */
137+
# 3| 0: [JavadocText] Tests of the getIntValue() predicate
140138
# 6| 3: [FieldDeclaration] int final_field, ...;
141139
# 6| -1: [TypeAccess] int
142140
# 6| 0: [IntegerLiteral] 42
143141
# 8| 4: [Method] values
144142
# 8| 3: [TypeAccess] void
145-
# 8| 4: (Parameters)
143+
#-----| 4: (Parameters)
146144
# 8| 0: [Parameter] notConstant
147145
# 8| 0: [TypeAccess] int
148146
# 8| 5: [BlockStmt] stmt
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
constructors/A.java:
2+
# 0| [CompilationUnit] A
3+
# 3| 1: [Class] A
4+
# 4| 3: [Constructor] A
5+
# 4| 5: [BlockStmt] stmt
6+
# 5| 0: [ThisConstructorInvocationStmt] this(...)
7+
# 5| 0: [IntegerLiteral] 42
8+
# 8| 4: [Constructor] A
9+
#-----| 4: (Parameters)
10+
# 8| 0: [Parameter] i
11+
# 8| 0: [TypeAccess] int
12+
# 8| 5: [BlockStmt] stmt
13+
# 10| 5: [Method] main
14+
# 10| 3: [TypeAccess] void
15+
#-----| 4: (Parameters)
16+
# 10| 0: [Parameter] args
17+
# 10| 0: [ArrayTypeAccess] ...[]
18+
# 10| 0: [TypeAccess] String
19+
# 10| 5: [BlockStmt] stmt
20+
# 11| 0: [ExprStmt] stmt
21+
# 11| 0: [ClassInstanceExpr] new A(...)
22+
# 11| -3: [TypeAccess] A
23+
# 14| 6: [FieldDeclaration] String STATIC, ...;
24+
# 14| -1: [TypeAccess] String
25+
# 14| 0: [StringLiteral] "static string"
26+
# 15| 7: [FieldDeclaration] String INSTANCE, ...;
27+
# 15| -1: [TypeAccess] String
28+
# 15| 0: [StringLiteral] "instance string"
29+
# 17| 8: [BlockStmt] stmt
30+
# 18| 0: [ExprStmt] stmt
31+
# 18| 0: [MethodAccess] println(...)
32+
# 18| -1: [VarAccess] System.out
33+
# 18| -1: [TypeAccess] System
34+
# 18| 0: [StringLiteral] "<obinit>"
35+
# 21| 9: [BlockStmt] stmt
36+
# 22| 0: [ExprStmt] stmt
37+
# 22| 0: [MethodAccess] println(...)
38+
# 22| -1: [VarAccess] System.out
39+
# 22| -1: [TypeAccess] System
40+
# 22| 0: [StringLiteral] "<clinit>"

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ dependency/A.java:
1818
# 15| 2: [Class] E
1919
# 16| 2: [Method] m
2020
# 16| 3: [TypeAccess] C
21-
# 16| 4: (Parameters)
21+
#-----| 4: (Parameters)
2222
# 16| 0: [Parameter] f
2323
# 16| 0: [TypeAccess] A<F>
2424
# 16| 0: [TypeAccess] F
@@ -35,15 +35,15 @@ dependency/A.java:
3535
# 27| 0: [TypeVariable] T
3636
# 27| 0: [TypeAccess] String
3737
# 27| 3: [TypeAccess] T
38-
# 27| 4: (Parameters)
38+
#-----| 4: (Parameters)
3939
# 27| 0: [Parameter] t
4040
# 27| 0: [TypeAccess] T
4141
# 27| 5: [BlockStmt] stmt
4242
# 27| 0: [ReturnStmt] stmt
4343
# 27| 0: [VarAccess] t
4444
# 28| 3: [Method] test2
4545
# 28| 3: [TypeAccess] void
46-
# 28| 4: (Parameters)
46+
#-----| 4: (Parameters)
4747
# 28| 0: [Parameter] t
4848
# 28| 0: [TypeAccess] Collection<? extends Number>
4949
# 28| 0: [WildcardTypeAccess] ? ...

0 commit comments

Comments
 (0)