Skip to content

Commit 325d6f7

Browse files
committed
Swift: add expr types to for-in upgrade script
1 parent 5b77e7d commit 325d6f7

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

swift/ql/lib/upgrades/147e087e57e51b2eb41e75c9c97380d0e6c20ecb/for_in_upgrade.ql

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,17 @@ Element getParent(Element type) {
136136
)
137137
}
138138

139+
Element getNextMethod(ForEachStmt foreach) {
140+
exists(@element sequence, @element exprType, @element parentType, @element typeDecl |
141+
sequence = foreach.getSequence() and
142+
expr_types(sequence, exprType) and
143+
parentType = getParent*(exprType) and
144+
any_generic_types(parentType, typeDecl) and
145+
decl_members(typeDecl, _, result) and
146+
callable_names(result, "next()")
147+
)
148+
}
149+
139150
// TODO: do we need a new_apply_expr_arguments
140151
query predicate new_decl_ref_exprs(NewElement id, NewElement decl) {
141152
decl_ref_exprs(id, decl)
@@ -145,18 +156,9 @@ query predicate new_decl_ref_exprs(NewElement id, NewElement decl) {
145156
Fresh::map(TIteratorVarConcreteDecl(foreach)) = decl
146157
)
147158
or
148-
exists(
149-
ForEachStmt foreach, @element sequence, @element exprType, @element parentType,
150-
@element typeDecl
151-
|
159+
exists(ForEachStmt foreach |
152160
Fresh::map(TNextCallFuncRef(foreach)) = id and
153-
// ForEachStmt.getSequence().getType().getMember(next)
154-
sequence = foreach.getSequence() and
155-
expr_types(sequence, exprType) and
156-
parentType = getParent*(exprType) and
157-
any_generic_types(parentType, typeDecl) and
158-
decl_members(typeDecl, _, decl) and
159-
callable_names(decl, "next()")
161+
decl = getNextMethod(foreach)
160162
)
161163
}
162164

@@ -217,4 +219,25 @@ query predicate new_expr_types(NewElement expr, NewElement type) {
217219
var_decl_parent_patterns(var_decl, pattern) and
218220
var_decls(var_decl, _, type)
219221
)
222+
or
223+
exists(ForEachStmt stmt |
224+
expr = Fresh::map(TNextCallMethodLookup(stmt)) and
225+
value_decls(getNextMethod(stmt), type)
226+
)
227+
or
228+
exists(ForEachStmt stmt |
229+
expr = Fresh::map(TNextCallVarRef(stmt)) and
230+
expr_types(stmt.getSequence(), type)
231+
)
232+
or
233+
exists(ForEachStmt stmt |
234+
expr = Fresh::map(TNextCallFuncRef(stmt)) and
235+
value_decls(getNextMethod(stmt), type)
236+
)
237+
or
238+
exists(ForEachStmt stmt, NewElement plainType|
239+
expr = Fresh::map(TNextCallInOutConversion(stmt)) and
240+
expr_types(stmt.getSequence(), plainType) and
241+
in_out_types(type, plainType)
242+
)
220243
}

swift/ql/lib/upgrades/147e087e57e51b2eb41e75c9c97380d0e6c20ecb/upgrade.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ in_out_exprs.rel: run for_in_upgrade.qlo new_in_out_exprs
1616
call_exprs.rel: run for_in_upgrade.qlo new_call_exprs
1717
locatable_locations.rel: run for_in_upgrade.qlo new_locatable_locations
1818
var_decls.rel: run for_in_upgrade.qlo new_var_decls
19-
concrete_var_decls.rel: run for_in_upgrade.qlo new_concrete_var_decls
19+
concrete_var_decls.rel: run for_in_upgrade.qlo new_concrete_var_decls
20+
expr_types.rel: run for_in_upgrade.qlo new_expr_types

0 commit comments

Comments
 (0)