Skip to content

Commit f6888c7

Browse files
committed
PrintAst: Assign indices to annotation children that don't have source locations
This avoids them all tying at rank 1 as before.
1 parent 01a5345 commit f6888c7

File tree

3 files changed

+77
-75
lines changed

3 files changed

+77
-75
lines changed

java/ql/integration-tests/all-platforms/kotlin/annotation-id-consistency/PrintAst.expected

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -151,46 +151,46 @@ test.kt:
151151
# 21| 3: [Annotation] VarargAnnotation
152152
# 0| 1: [ArrayInit] {...}
153153
# 0| 1: [IntegerLiteral] 1
154-
# 0| 1: [IntegerLiteral] 2
154+
# 0| 2: [IntegerLiteral] 2
155155
# 22| 4: [Annotation] VarargAnnotation
156156
# 0| 1: [ArrayInit] {...}
157157
# 0| 1: [IntegerLiteral] 1
158-
# 0| 1: [IntegerLiteral] 2
159-
# 0| 1: [IntegerLiteral] 3
158+
# 0| 2: [IntegerLiteral] 2
159+
# 0| 3: [IntegerLiteral] 3
160160
# 23| 5: [Annotation] VarargAnnotation
161161
# 0| 1: [ArrayInit] {...}
162162
# 0| 1: [IntegerLiteral] 1
163-
# 0| 1: [IntegerLiteral] 2
164-
# 0| 1: [IntegerLiteral] 3
163+
# 0| 2: [IntegerLiteral] 2
164+
# 0| 3: [IntegerLiteral] 3
165165
# 17| 2: [Annotation] Ann1
166166
# 0| 1: [IntegerLiteral] 1
167-
# 0| 1: [Annotation] Ann2
167+
# 0| 2: [Annotation] Ann2
168168
# 0| 1: [StringLiteral] "Hello"
169-
# 0| 1: [TypeLiteral] String.class
169+
# 0| 2: [TypeLiteral] String.class
170170
# 0| 0: [TypeAccess] String
171-
# 0| 1: [ArrayInit] {...}
171+
# 0| 3: [ArrayInit] {...}
172172
# 0| 1: [IntegerLiteral] 1
173-
# 0| 1: [IntegerLiteral] 2
174-
# 0| 1: [IntegerLiteral] 3
175-
# 0| 1: [ArrayInit] {...}
173+
# 0| 2: [IntegerLiteral] 2
174+
# 0| 3: [IntegerLiteral] 3
175+
# 0| 4: [ArrayInit] {...}
176176
# 0| 1: [Annotation] Ann3
177177
# 0| 1: [IntegerLiteral] 1
178-
# 0| 1: [Annotation] Ann3
178+
# 0| 2: [Annotation] Ann3
179179
# 0| 1: [IntegerLiteral] 2
180-
# 0| 1: [ArrayInit] {...}
180+
# 0| 5: [ArrayInit] {...}
181181
# 0| 1: [TypeLiteral] String.class
182182
# 0| 0: [TypeAccess] String
183-
# 0| 1: [TypeLiteral] int.class
183+
# 0| 2: [TypeLiteral] int.class
184184
# 0| 0: [TypeAccess] int
185-
# 0| 1: [VarAccess] DayOfWeek.MONDAY
185+
# 0| 3: [VarAccess] DayOfWeek.MONDAY
186186
# 0| -1: [TypeAccess] DayOfWeek
187187
# 18| 3: [Annotation] GenericAnnotation<>
188188
# 24| 4: [Annotation] AnnWithDefaults
189189
# 0| 1: [IntegerLiteral] 1
190-
# 0| 1: [StringLiteral] "hello"
191-
# 0| 1: [VarAccess] DayOfWeek.TUESDAY
190+
# 0| 2: [StringLiteral] "hello"
191+
# 0| 3: [VarAccess] DayOfWeek.TUESDAY
192192
# 0| -1: [TypeAccess] DayOfWeek
193-
# 0| 1: [ArrayInit] {...}
193+
# 0| 4: [ArrayInit] {...}
194194
# 0| 1: [Annotation] Ann3
195195
# 0| 1: [IntegerLiteral] 1
196196
# 25| 1: [Constructor] Annotated
@@ -209,46 +209,46 @@ test.kt:
209209
# 31| 3: [Annotation] VarargAnnotation
210210
# 0| 1: [ArrayInit] {...}
211211
# 0| 1: [IntegerLiteral] 1
212-
# 0| 1: [IntegerLiteral] 2
212+
# 0| 2: [IntegerLiteral] 2
213213
# 32| 4: [Annotation] VarargAnnotation
214214
# 0| 1: [ArrayInit] {...}
215215
# 0| 1: [IntegerLiteral] 1
216-
# 0| 1: [IntegerLiteral] 2
217-
# 0| 1: [IntegerLiteral] 3
216+
# 0| 2: [IntegerLiteral] 2
217+
# 0| 3: [IntegerLiteral] 3
218218
# 33| 5: [Annotation] VarargAnnotation
219219
# 0| 1: [ArrayInit] {...}
220220
# 0| 1: [IntegerLiteral] 1
221-
# 0| 1: [IntegerLiteral] 2
222-
# 0| 1: [IntegerLiteral] 3
221+
# 0| 2: [IntegerLiteral] 2
222+
# 0| 3: [IntegerLiteral] 3
223223
# 27| 2: [Annotation] Ann1
224224
# 0| 1: [IntegerLiteral] 1
225-
# 0| 1: [Annotation] Ann2
225+
# 0| 2: [Annotation] Ann2
226226
# 0| 1: [StringLiteral] "Hello"
227-
# 0| 1: [TypeLiteral] String.class
227+
# 0| 2: [TypeLiteral] String.class
228228
# 0| 0: [TypeAccess] String
229-
# 0| 1: [ArrayInit] {...}
229+
# 0| 3: [ArrayInit] {...}
230230
# 0| 1: [IntegerLiteral] 1
231-
# 0| 1: [IntegerLiteral] 2
232-
# 0| 1: [IntegerLiteral] 3
233-
# 0| 1: [ArrayInit] {...}
231+
# 0| 2: [IntegerLiteral] 2
232+
# 0| 3: [IntegerLiteral] 3
233+
# 0| 4: [ArrayInit] {...}
234234
# 0| 1: [Annotation] Ann3
235235
# 0| 1: [IntegerLiteral] 1
236-
# 0| 1: [Annotation] Ann3
236+
# 0| 2: [Annotation] Ann3
237237
# 0| 1: [IntegerLiteral] 2
238-
# 0| 1: [ArrayInit] {...}
238+
# 0| 5: [ArrayInit] {...}
239239
# 0| 1: [TypeLiteral] String.class
240240
# 0| 0: [TypeAccess] String
241-
# 0| 1: [TypeLiteral] int.class
241+
# 0| 2: [TypeLiteral] int.class
242242
# 0| 0: [TypeAccess] int
243-
# 0| 1: [VarAccess] DayOfWeek.MONDAY
243+
# 0| 3: [VarAccess] DayOfWeek.MONDAY
244244
# 0| -1: [TypeAccess] DayOfWeek
245245
# 28| 3: [Annotation] GenericAnnotation<>
246246
# 34| 4: [Annotation] AnnWithDefaults
247247
# 0| 1: [IntegerLiteral] 1
248-
# 0| 1: [StringLiteral] "hello"
249-
# 0| 1: [VarAccess] DayOfWeek.TUESDAY
248+
# 0| 2: [StringLiteral] "hello"
249+
# 0| 3: [VarAccess] DayOfWeek.TUESDAY
250250
# 0| -1: [TypeAccess] DayOfWeek
251-
# 0| 1: [ArrayInit] {...}
251+
# 0| 4: [ArrayInit] {...}
252252
# 0| 1: [Annotation] Ann3
253253
# 0| 1: [IntegerLiteral] 1
254254
# 35| 1: [Constructor] AnnotatedUsedByKotlin
@@ -290,12 +290,12 @@ test.kt:
290290
# 0| [ArrayInit] {...}
291291
# 0| 1: [TypeLiteral] String.class
292292
# 0| 0: [TypeAccess] String
293-
# 0| 1: [TypeLiteral] String.class
293+
# 0| 2: [TypeLiteral] String.class
294294
# 0| 0: [TypeAccess] String
295295
# 0| [TypeLiteral] String.class
296296
# 0| 0: [TypeAccess] String
297297
# 0| [ArrayInit] {...}
298298
# 0| 1: [TypeLiteral] String.class
299299
# 0| 0: [TypeAccess] String
300-
# 0| 1: [TypeLiteral] String.class
300+
# 0| 2: [TypeLiteral] String.class
301301
# 0| 0: [TypeAccess] String

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -298,19 +298,21 @@ final class AnnotationPartNode extends ExprStmtNode {
298298

299299
override ElementNode getChild(int childIndex) {
300300
result.getElement() =
301-
rank[childIndex](Element ch, string file, int line, int column |
302-
ch = this.getAnAnnotationChild() and locationSortKeys(ch, file, line, column)
301+
rank[childIndex](Element ch, string file, int line, int column, int idx |
302+
ch = this.getAnnotationChild(idx) and locationSortKeys(ch, file, line, column)
303303
|
304-
ch order by file, line, column
304+
ch order by file, line, column, idx
305305
)
306306
}
307307

308-
private Expr getAnAnnotationChild() {
309-
result = element.(Annotation).getValue(_)
308+
private Expr getAnnotationChild(int index) {
309+
result = element.(Annotation).getValue(_) and
310+
index >= 0 and
311+
if exists(int x | x >= 0 | result.isNthChildOf(element, x))
312+
then result.isNthChildOf(element, index)
313+
else result.isNthChildOf(element, -(index + 1))
310314
or
311-
result = element.(ArrayInit).getAnInit()
312-
or
313-
result = element.(ArrayInit).(Annotatable).getAnAnnotation()
315+
result = element.(ArrayInit).getInit(index)
314316
}
315317
}
316318

java/ql/test/kotlin/library-tests/annotation_classes/PrintAst.expected

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ def.kt:
4141
# 47| -1: [TypeAccess] ConsoleKt
4242
# 47| 0: [MethodAccess] a(...)
4343
# 47| -1: [VarAccess] a
44-
# 49| 1: [LocalVariableDeclStmt] var ...;
45-
# 49| 1: [LocalVariableDeclExpr] x
44+
# 50| 1: [LocalVariableDeclStmt] var ...;
45+
# 50| 1: [LocalVariableDeclExpr] x
4646
# 50| 0: [IntegerLiteral] 10
4747
# 53| 3: [FieldDeclaration] int p;
4848
#-----| -2: (Annotations)
@@ -91,51 +91,51 @@ def.kt:
9191
# 0| 1: [ArrayInit] {...}
9292
# 0| 1: [VarAccess] ElementType.TYPE
9393
# 0| -1: [TypeAccess] ElementType
94-
# 0| 1: [VarAccess] ElementType.FIELD
94+
# 0| 2: [VarAccess] ElementType.FIELD
9595
# 0| -1: [TypeAccess] ElementType
96-
# 0| 1: [VarAccess] ElementType.METHOD
96+
# 0| 3: [VarAccess] ElementType.METHOD
9797
# 0| -1: [TypeAccess] ElementType
98-
# 0| 1: [VarAccess] ElementType.PARAMETER
98+
# 0| 4: [VarAccess] ElementType.PARAMETER
9999
# 0| -1: [TypeAccess] ElementType
100-
# 0| 1: [VarAccess] ElementType.CONSTRUCTOR
100+
# 0| 5: [VarAccess] ElementType.CONSTRUCTOR
101101
# 0| -1: [TypeAccess] ElementType
102-
# 0| 1: [VarAccess] ElementType.LOCAL_VARIABLE
102+
# 0| 6: [VarAccess] ElementType.LOCAL_VARIABLE
103103
# 0| -1: [TypeAccess] ElementType
104-
# 0| 1: [VarAccess] ElementType.ANNOTATION_TYPE
104+
# 0| 7: [VarAccess] ElementType.ANNOTATION_TYPE
105105
# 0| -1: [TypeAccess] ElementType
106-
# 0| 1: [VarAccess] ElementType.TYPE_PARAMETER
106+
# 0| 8: [VarAccess] ElementType.TYPE_PARAMETER
107107
# 0| -1: [TypeAccess] ElementType
108-
# 0| 1: [VarAccess] ElementType.TYPE_USE
108+
# 0| 9: [VarAccess] ElementType.TYPE_USE
109109
# 0| -1: [TypeAccess] ElementType
110110
# 5| 3: [Annotation] Target
111111
# 0| 1: [ArrayInit] {...}
112112
# 0| 1: [VarAccess] AnnotationTarget.CLASS
113113
# 0| -1: [TypeAccess] AnnotationTarget
114-
# 0| 1: [VarAccess] AnnotationTarget.ANNOTATION_CLASS
114+
# 0| 2: [VarAccess] AnnotationTarget.ANNOTATION_CLASS
115115
# 0| -1: [TypeAccess] AnnotationTarget
116-
# 0| 1: [VarAccess] AnnotationTarget.TYPE_PARAMETER
116+
# 0| 3: [VarAccess] AnnotationTarget.TYPE_PARAMETER
117117
# 0| -1: [TypeAccess] AnnotationTarget
118-
# 0| 1: [VarAccess] AnnotationTarget.PROPERTY
118+
# 0| 4: [VarAccess] AnnotationTarget.PROPERTY
119119
# 0| -1: [TypeAccess] AnnotationTarget
120-
# 0| 1: [VarAccess] AnnotationTarget.FIELD
120+
# 0| 5: [VarAccess] AnnotationTarget.FIELD
121121
# 0| -1: [TypeAccess] AnnotationTarget
122-
# 0| 1: [VarAccess] AnnotationTarget.LOCAL_VARIABLE
122+
# 0| 6: [VarAccess] AnnotationTarget.LOCAL_VARIABLE
123123
# 0| -1: [TypeAccess] AnnotationTarget
124-
# 0| 1: [VarAccess] AnnotationTarget.VALUE_PARAMETER
124+
# 0| 7: [VarAccess] AnnotationTarget.VALUE_PARAMETER
125125
# 0| -1: [TypeAccess] AnnotationTarget
126-
# 0| 1: [VarAccess] AnnotationTarget.CONSTRUCTOR
126+
# 0| 8: [VarAccess] AnnotationTarget.CONSTRUCTOR
127127
# 0| -1: [TypeAccess] AnnotationTarget
128-
# 0| 1: [VarAccess] AnnotationTarget.FUNCTION
128+
# 0| 9: [VarAccess] AnnotationTarget.FUNCTION
129129
# 0| -1: [TypeAccess] AnnotationTarget
130-
# 0| 1: [VarAccess] AnnotationTarget.PROPERTY_GETTER
130+
# 0| 10: [VarAccess] AnnotationTarget.PROPERTY_GETTER
131131
# 0| -1: [TypeAccess] AnnotationTarget
132-
# 0| 1: [VarAccess] AnnotationTarget.PROPERTY_SETTER
132+
# 0| 11: [VarAccess] AnnotationTarget.PROPERTY_SETTER
133133
# 0| -1: [TypeAccess] AnnotationTarget
134-
# 0| 1: [VarAccess] AnnotationTarget.TYPE
134+
# 0| 12: [VarAccess] AnnotationTarget.TYPE
135135
# 0| -1: [TypeAccess] AnnotationTarget
136-
# 0| 1: [VarAccess] AnnotationTarget.FILE
136+
# 0| 13: [VarAccess] AnnotationTarget.FILE
137137
# 0| -1: [TypeAccess] AnnotationTarget
138-
# 0| 1: [VarAccess] AnnotationTarget.TYPEALIAS
138+
# 0| 14: [VarAccess] AnnotationTarget.TYPEALIAS
139139
# 0| -1: [TypeAccess] AnnotationTarget
140140
# 21| 1: [Method] a
141141
#-----| 1: (Annotations)
@@ -204,17 +204,17 @@ def.kt:
204204
# 0| 1: [IntegerLiteral] 1
205205
# 39| 2: [Annotation] Annot1k
206206
# 0| 1: [IntegerLiteral] 2
207-
# 0| 1: [StringLiteral] "ab"
208-
# 0| 1: [TypeLiteral] X.class
207+
# 0| 2: [StringLiteral] "ab"
208+
# 0| 3: [TypeLiteral] X.class
209209
# 0| 0: [TypeAccess] X
210-
# 0| 1: [VarAccess] Y.B
210+
# 0| 4: [VarAccess] Y.B
211211
# 0| -1: [TypeAccess] Y
212-
# 0| 1: [ArrayInit] {...}
212+
# 0| 5: [ArrayInit] {...}
213213
# 0| 1: [VarAccess] Y.C
214214
# 0| -1: [TypeAccess] Y
215-
# 0| 1: [VarAccess] Y.A
215+
# 0| 2: [VarAccess] Y.A
216216
# 0| -1: [TypeAccess] Y
217-
# 0| 1: [Annotation] Annot0k
217+
# 0| 6: [Annotation] Annot0k
218218
# 0| 1: [IntegerLiteral] 1
219219
# 42| 1: [Constructor] Z
220220
#-----| 1: (Annotations)

0 commit comments

Comments
 (0)