Skip to content

Commit 5b77e7d

Browse files
committed
Swift: for-in upgrade script passing CFG test
1 parent 30b3069 commit 5b77e7d

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

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

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class NamedPattern extends Pattern, @named_pattern {
2424
newtype TAddedElement =
2525
TIteratorVar(ForEachStmt stmt) or
2626
TIteratorVarPattern(ForEachStmt stmt) or
27+
TIteratorVarConcreteDecl(ForEachStmt stmt) or
2728
TNextCall(ForEachStmt stmt) or
2829
TNextCallMethodLookup(ForEachStmt stmt) or
2930
TNextCallInOutConversion(ForEachStmt stmt) or
@@ -38,7 +39,7 @@ class NewElement extends TNewElement {
3839
string toString() { none() }
3940
}
4041

41-
query predicate new_for_each_stmts(ForEachStmt id, NamedPattern pattern, Element body) {
42+
query predicate new_for_each_stmts(ForEachStmt id, Pattern pattern, Element body) {
4243
for_each_stmts(id, pattern, _, body)
4344
}
4445

@@ -141,10 +142,13 @@ query predicate new_decl_ref_exprs(NewElement id, NewElement decl) {
141142
or
142143
exists(ForEachStmt foreach |
143144
Fresh::map(TNextCallVarRef(foreach)) = id and
144-
Fresh::map(TIteratorVarPattern(foreach)) = decl
145+
Fresh::map(TIteratorVarConcreteDecl(foreach)) = decl
145146
)
146147
or
147-
exists(ForEachStmt foreach, @element sequence, @element exprType, @element parentType, @element typeDecl |
148+
exists(
149+
ForEachStmt foreach, @element sequence, @element exprType, @element parentType,
150+
@element typeDecl
151+
|
148152
Fresh::map(TNextCallFuncRef(foreach)) = id and
149153
// ForEachStmt.getSequence().getType().getMember(next)
150154
sequence = foreach.getSequence() and
@@ -169,3 +173,48 @@ query predicate new_call_exprs(NewElement id) {
169173
call_exprs(id) or
170174
Fresh::map(TNextCall(_)) = id
171175
}
176+
177+
query predicate new_locatable_locations(NewElement locatable, NewElement location) {
178+
locatable_locations(locatable, location)
179+
or
180+
exists(ForEachStmt stmt |
181+
locatable = Fresh::map(TIteratorVarPattern(stmt)) or
182+
locatable = Fresh::map(TIteratorVarConcreteDecl(stmt)) or
183+
locatable = Fresh::map(TNextCall(stmt)) or
184+
locatable = Fresh::map(TNextCallMethodLookup(stmt)) or
185+
locatable = Fresh::map(TNextCallInOutConversion(stmt)) or
186+
locatable = Fresh::map(TNextCallVarRef(stmt))
187+
|
188+
locatable_locations(stmt, location)
189+
)
190+
}
191+
192+
query predicate new_concrete_var_decls(NewElement decl, int introducer_int) {
193+
concrete_var_decls(decl, introducer_int)
194+
or
195+
exists(ForEachStmt stmt |
196+
decl = Fresh::map(TIteratorVarConcreteDecl(stmt)) and
197+
introducer_int = 1
198+
)
199+
}
200+
201+
query predicate new_var_decls(NewElement decl, string name, Element type) {
202+
var_decls(decl, name, type)
203+
or
204+
exists(ForEachStmt stmt |
205+
decl = Fresh::map(TIteratorVarConcreteDecl(stmt)) and
206+
expr_types(stmt.getSequence(), type) and
207+
name = stmt.getPattern().getGeneratorString()
208+
)
209+
}
210+
211+
query predicate new_expr_types(NewElement expr, NewElement type) {
212+
expr_types(expr, type)
213+
or
214+
exists(ForEachStmt stmt, Element pattern, Element var_decl |
215+
expr = Fresh::map(TNextCall(stmt)) and
216+
for_each_stmts(stmt, pattern, _, _) and
217+
var_decl_parent_patterns(var_decl, pattern) and
218+
var_decls(var_decl, _, type)
219+
)
220+
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@ self_apply_exprs.rel: run for_in_upgrade.qlo new_self_apply_exprs
1313
apply_exprs.rel: run for_in_upgrade.qlo new_apply_exprs
1414
decl_ref_exprs.rel: run for_in_upgrade.qlo new_decl_ref_exprs
1515
in_out_exprs.rel: run for_in_upgrade.qlo new_in_out_exprs
16-
call_exprs.rel: run for_in_upgrade.ql new_call_exprs
16+
call_exprs.rel: run for_in_upgrade.qlo new_call_exprs
17+
locatable_locations.rel: run for_in_upgrade.qlo new_locatable_locations
18+
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

0 commit comments

Comments
 (0)