Skip to content

Commit

Permalink
Merge pull request #18142 from github/redsun82/swift-6-for-each-vars
Browse files Browse the repository at this point in the history
Swift: extract variables as children of `ForEachStmt`
  • Loading branch information
redsun82 authored Dec 4, 2024
2 parents eeed2c2 + 814218c commit 9da7e8a
Show file tree
Hide file tree
Showing 24 changed files with 161 additions and 14 deletions.
11 changes: 11 additions & 0 deletions swift/extractor/translators/StmtTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ codeql::ForEachStmt StmtTranslator::translateForEachStmt(const swift::ForEachStm
entry.iteratorVar = dispatcher.fetchLabel(stmt.getIteratorVar());
entry.where = dispatcher.fetchOptionalLabel(stmt.getWhere());
entry.nextCall = dispatcher.fetchOptionalLabel(stmt.getNextCall());
auto add_variable = [&](swift::VarDecl* var) {
entry.variables.push_back(dispatcher.fetchLabel(var));
};
if (auto pattern = stmt.getPattern()) {
pattern->forEachVariable(add_variable);
}
if (auto iteratorVar = stmt.getIteratorVar()) {
for (auto i = 0u; i < iteratorVar->getNumPatternEntries(); ++i) {
iteratorVar->getPattern(i)->forEachVariable(add_variable);
}
}
return entry;
}

Expand Down
15 changes: 10 additions & 5 deletions swift/ql/.generated.list

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion swift/ql/.gitattributes

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions swift/ql/lib/codeql/swift/elements/stmt/ForEachStmt.qll

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions swift/ql/lib/codeql/swift/generated/ParentChild.qll

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions swift/ql/lib/codeql/swift/generated/Raw.qll

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions swift/ql/lib/codeql/swift/generated/stmt/ForEachStmt.qll

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions swift/ql/lib/swift.dbscheme

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
| 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 | { ... } |
| 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 | { ... } |
| 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 | { ... } |

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
| for.swift:4:5:6:5 | for ... in ... where ... { ... } | file://:0:0:0:0 | var ... = ... |
| for.swift:7:5:9:5 | for ... in ... { ... } | file://:0:0:0:0 | var ... = ... |

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
| for.swift:4:5:6:5 | for ... in ... where ... { ... } | for.swift:4:5:4:5 | call to next() |
| for.swift:7:5:9:5 | for ... in ... { ... } | for.swift:7:5:7:5 | call to next() |

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
| for.swift:4:5:6:5 | for ... in ... where ... { ... } | 0 | for.swift:4:9:4:9 | x |
| for.swift:4:5:6:5 | for ... in ... where ... { ... } | 1 | for.swift:4:14:4:14 | $x$generator |
| for.swift:7:5:9:5 | for ... in ... { ... } | 0 | for.swift:7:9:7:9 | s |
| for.swift:7:5:9:5 | for ... in ... { ... } | 1 | for.swift:7:14:7:14 | $s$generator |
| for.swift:13:5:17:5 | for ... in ... { ... } | 0 | for.swift:13:9:13:9 | x |

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| for.swift:4:5:6:5 | for ... in ... where ... { ... } | for.swift:4:25:4:30 | ... .!=(_:_:) ... |

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

19 changes: 19 additions & 0 deletions swift/ql/test/extractor-tests/generated/stmt/ForEachStmt/for.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
struct S {}

func test_sequence(_ ints: [Int], _ elements: [S]) {
for x in ints where x != 0 {
print(x)
}
for s in elements {
print(s)
}
}

func test_variadic_pack<each T>(_ array: repeat [each T]) -> Bool {
for x in repeat each array {
if !x.isEmpty {
return false
}
}
return true
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ methodlookup.swift:
# 40| getBase(): [TypeExpr] Bar.Type
# 40| getTypeRepr(): [TypeRepr] Bar
# 40| getMethodRef(): [DeclRefExpr] staticMethod()
# 33| getExpr().getFullyConverted(): [FunctionConversionExpr] (@isolated(any) () async -> ()) ...
# 33| [NilLiteralExpr] nil
# 38| [Comment] // Bar.instanceMethod(bar2)() // error: actor-isolated instance method 'instanceMethod()' can not be referenced from a non-isolated context
# 38|
Expand Down Expand Up @@ -262,6 +263,7 @@ methodlookup.swift:
# 51| getMethodRef(): [DeclRefExpr] staticMethod()
# 51| getMethodRef().getFullyConverted(): [FunctionConversionExpr] ((Baz.Type) -> @MainActor () -> ()) ...
# 51| getElement(5).getFullyConverted(): [AwaitExpr] await ...
# 43| getExpr().getFullyConverted(): [FunctionConversionExpr] (@isolated(any) () async -> ()) ...
# 43| [NilLiteralExpr] nil
# 47| [Comment] // DotSyntaxCallExpr
# 47|
Expand Down
1 change: 1 addition & 0 deletions swift/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,7 @@ class DoStmt(LabeledStmt):
body: BraceStmt | child

class ForEachStmt(LabeledStmt):
variables: list[VarDecl] | child
pattern: Pattern | child
where: optional[Expr] | child
iteratorVar: optional[PatternBindingDecl] | child
Expand Down

0 comments on commit 9da7e8a

Please sign in to comment.