Skip to content

Commit 814218c

Browse files
author
Paolo Tranquilli
committed
Swift: extract variables as children of ForEachStmt
1 parent 8bcc5f4 commit 814218c

24 files changed

+161
-14
lines changed

Diff for: swift/extractor/translators/StmtTranslator.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ codeql::ForEachStmt StmtTranslator::translateForEachStmt(const swift::ForEachStm
7777
entry.iteratorVar = dispatcher.fetchLabel(stmt.getIteratorVar());
7878
entry.where = dispatcher.fetchOptionalLabel(stmt.getWhere());
7979
entry.nextCall = dispatcher.fetchOptionalLabel(stmt.getNextCall());
80+
auto add_variable = [&](swift::VarDecl* var) {
81+
entry.variables.push_back(dispatcher.fetchLabel(var));
82+
};
83+
if (auto pattern = stmt.getPattern()) {
84+
pattern->forEachVariable(add_variable);
85+
}
86+
if (auto iteratorVar = stmt.getIteratorVar()) {
87+
for (auto i = 0u; i < iteratorVar->getNumPatternEntries(); ++i) {
88+
iteratorVar->getPattern(i)->forEachVariable(add_variable);
89+
}
90+
}
8091
return entry;
8192
}
8293

Diff for: swift/ql/.generated.list

+10-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: swift/ql/.gitattributes

+6-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: swift/ql/lib/codeql/swift/elements/stmt/ForEachStmt.qll

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: swift/ql/lib/codeql/swift/generated/ParentChild.qll

+10-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: swift/ql/lib/codeql/swift/generated/Raw.qll

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: swift/ql/lib/codeql/swift/generated/stmt/ForEachStmt.qll

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: swift/ql/lib/swift.dbscheme

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| for.swift:4:5:6:5 | for ... in ... where ... { ... } | hasLabel: | no | getNumberOfVariables: | 2 | getPattern: | for.swift:4:9:4:9 | x | hasWhere: | yes | hasIteratorVar: | yes | hasNextCall: | yes | getBody: | for.swift:4:32:6:5 | { ... } |
2+
| for.swift:7:5:9:5 | for ... in ... { ... } | hasLabel: | no | getNumberOfVariables: | 2 | getPattern: | for.swift:7:9:7:9 | s | hasWhere: | no | hasIteratorVar: | yes | hasNextCall: | yes | getBody: | for.swift:7:23:9:5 | { ... } |
3+
| for.swift:13:5:17:5 | for ... in ... { ... } | hasLabel: | no | getNumberOfVariables: | 1 | getPattern: | for.swift:13:9:13:9 | x | hasWhere: | no | hasIteratorVar: | no | hasNextCall: | no | getBody: | for.swift:13:32:17:5 | { ... } |

Diff for: swift/ql/test/extractor-tests/generated/stmt/ForEachStmt/ForEachStmt.ql

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| for.swift:4:5:6:5 | for ... in ... where ... { ... } | file://:0:0:0:0 | var ... = ... |
2+
| for.swift:7:5:9:5 | for ... in ... { ... } | file://:0:0:0:0 | var ... = ... |

Diff for: swift/ql/test/extractor-tests/generated/stmt/ForEachStmt/ForEachStmt_getIteratorVar.ql

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: swift/ql/test/extractor-tests/generated/stmt/ForEachStmt/ForEachStmt_getLabel.expected

Whitespace-only changes.

Diff for: swift/ql/test/extractor-tests/generated/stmt/ForEachStmt/ForEachStmt_getLabel.ql

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| for.swift:4:5:6:5 | for ... in ... where ... { ... } | for.swift:4:5:4:5 | call to next() |
2+
| for.swift:7:5:9:5 | for ... in ... { ... } | for.swift:7:5:7:5 | call to next() |

Diff for: swift/ql/test/extractor-tests/generated/stmt/ForEachStmt/ForEachStmt_getNextCall.ql

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
| for.swift:4:5:6:5 | for ... in ... where ... { ... } | 0 | for.swift:4:9:4:9 | x |
2+
| for.swift:4:5:6:5 | for ... in ... where ... { ... } | 1 | for.swift:4:14:4:14 | $x$generator |
3+
| for.swift:7:5:9:5 | for ... in ... { ... } | 0 | for.swift:7:9:7:9 | s |
4+
| for.swift:7:5:9:5 | for ... in ... { ... } | 1 | for.swift:7:14:7:14 | $s$generator |
5+
| for.swift:13:5:17:5 | for ... in ... { ... } | 0 | for.swift:13:9:13:9 | x |

Diff for: swift/ql/test/extractor-tests/generated/stmt/ForEachStmt/ForEachStmt_getVariable.ql

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| for.swift:4:5:6:5 | for ... in ... where ... { ... } | for.swift:4:25:4:30 | ... .!=(_:_:) ... |

Diff for: swift/ql/test/extractor-tests/generated/stmt/ForEachStmt/ForEachStmt_getWhere.ql

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: swift/ql/test/extractor-tests/generated/stmt/ForEachStmt/MISSING_SOURCE.txt

-4
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
struct S {}
2+
3+
func test_sequence(_ ints: [Int], _ elements: [S]) {
4+
for x in ints where x != 0 {
5+
print(x)
6+
}
7+
for s in elements {
8+
print(s)
9+
}
10+
}
11+
12+
func test_variadic_pack<each T>(_ array: repeat [each T]) -> Bool {
13+
for x in repeat each array {
14+
if !x.isEmpty {
15+
return false
16+
}
17+
}
18+
return true
19+
}

Diff for: swift/ql/test/library-tests/elements/expr/methodlookup/PrintAst.expected

+2
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ methodlookup.swift:
187187
# 40| getBase(): [TypeExpr] Bar.Type
188188
# 40| getTypeRepr(): [TypeRepr] Bar
189189
# 40| getMethodRef(): [DeclRefExpr] staticMethod()
190+
# 33| getExpr().getFullyConverted(): [FunctionConversionExpr] (@isolated(any) () async -> ()) ...
190191
# 33| [NilLiteralExpr] nil
191192
# 38| [Comment] // Bar.instanceMethod(bar2)() // error: actor-isolated instance method 'instanceMethod()' can not be referenced from a non-isolated context
192193
# 38|
@@ -262,6 +263,7 @@ methodlookup.swift:
262263
# 51| getMethodRef(): [DeclRefExpr] staticMethod()
263264
# 51| getMethodRef().getFullyConverted(): [FunctionConversionExpr] ((Baz.Type) -> @MainActor () -> ()) ...
264265
# 51| getElement(5).getFullyConverted(): [AwaitExpr] await ...
266+
# 43| getExpr().getFullyConverted(): [FunctionConversionExpr] (@isolated(any) () async -> ()) ...
265267
# 43| [NilLiteralExpr] nil
266268
# 47| [Comment] // DotSyntaxCallExpr
267269
# 47|

Diff for: swift/schema.py

+1
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,7 @@ class DoStmt(LabeledStmt):
10161016
body: BraceStmt | child
10171017

10181018
class ForEachStmt(LabeledStmt):
1019+
variables: list[VarDecl] | child
10191020
pattern: Pattern | child
10201021
where: optional[Expr] | child
10211022
iteratorVar: optional[PatternBindingDecl] | child

0 commit comments

Comments
 (0)