Skip to content

Commit 547cecf

Browse files
authored
Merge pull request github#9385 from MathiasVP/swift-extract-yield-stmt
Swift: Extract `yield` statements
2 parents f70f769 + 1d12048 commit 547cecf

File tree

9 files changed

+61
-13
lines changed

9 files changed

+61
-13
lines changed

swift/codegen/schema.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ ThrowStmt:
601601

602602
YieldStmt:
603603
_extends: Stmt
604+
results: Expr*
604605

605606
BoundGenericType:
606607
_extends: NominalOrBoundGenericNominalType

swift/extractor/visitors/StmtVisitor.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,16 @@ class StmtVisitor : public AstVisitorBase<StmtVisitor> {
206206
dispatcher_.emit(FallthroughStmtsTrap{label, sourceLabel, destLabel});
207207
}
208208

209+
void visitYieldStmt(swift::YieldStmt* stmt) {
210+
auto label = dispatcher_.assignNewLabel(stmt);
211+
dispatcher_.emit(YieldStmtsTrap{label});
212+
auto i = 0u;
213+
for(auto* expr : stmt->getYields()) {
214+
auto exprLabel = dispatcher_.fetchLabel(expr);
215+
dispatcher_.emit(YieldStmtResultsTrap{label, i++, exprLabel});
216+
}
217+
}
218+
209219
private:
210220
void emitLabeledStmt(const swift::LabeledStmt* stmt, TrapLabel<LabeledStmtTag> label) {
211221
if (stmt->getLabelInfo()) {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
// generated by codegen/codegen.py
2+
import codeql.swift.elements.expr.Expr
23
import codeql.swift.elements.stmt.Stmt
34

45
class YieldStmtBase extends @yield_stmt, Stmt {
56
override string getAPrimaryQlClass() { result = "YieldStmt" }
7+
8+
Expr getResult(int index) {
9+
exists(Expr x |
10+
yield_stmt_results(this, index, x) and
11+
result = x.resolve()
12+
)
13+
}
14+
15+
Expr getAResult() { result = getResult(_) }
16+
17+
int getNumberOfResults() { result = count(getAResult()) }
618
}

swift/ql/lib/swift.dbscheme

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,13 @@ yield_stmts(
12731273
unique int id: @yield_stmt
12741274
);
12751275

1276+
#keyset[id, index]
1277+
yield_stmt_results(
1278+
int id: @yield_stmt ref,
1279+
int index: int ref,
1280+
int result: @expr ref
1281+
);
1282+
12761283
@bound_generic_type =
12771284
@bound_generic_class_type
12781285
| @bound_generic_enum_type

swift/ql/test/extractor-tests/declarations/all.expected

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
| test.swift:2:7:2:7 | get |
88
| test.swift:2:7:2:7 | self |
99
| test.swift:2:7:2:7 | self |
10+
| test.swift:2:7:2:7 | self |
1011
| test.swift:2:7:2:7 | set |
1112
| test.swift:2:7:2:7 | value |
1213
| test.swift:2:7:2:7 | x |
1314
| test.swift:3:3:6:3 | var ... = ... |
1415
| test.swift:3:7:3:7 | (unnamed function decl) |
1516
| test.swift:3:7:3:7 | next |
17+
| test.swift:3:7:3:7 | self |
1618
| test.swift:4:5:4:5 | self |
1719
| test.swift:4:5:4:24 | get |
1820
| test.swift:5:5:5:5 | self |
@@ -26,6 +28,7 @@
2628
| test.swift:9:17:9:17 | get |
2729
| test.swift:9:17:9:17 | self |
2830
| test.swift:9:17:9:17 | self |
31+
| test.swift:9:17:9:17 | self |
2932
| test.swift:9:17:9:17 | set |
3033
| test.swift:9:17:9:17 | value |
3134
| test.swift:9:17:9:17 | x |
@@ -79,6 +82,7 @@
7982
| test.swift:41:7:41:7 | get |
8083
| test.swift:41:7:41:7 | self |
8184
| test.swift:41:7:41:7 | self |
85+
| test.swift:41:7:41:7 | self |
8286
| test.swift:41:7:41:7 | set |
8387
| test.swift:41:7:41:7 | value |
8488
| test.swift:42:3:42:3 | self |
@@ -111,6 +115,7 @@
111115
| test.swift:81:8:81:8 | normalField |
112116
| test.swift:82:3:87:3 | var ... = ... |
113117
| test.swift:82:7:82:7 | (unnamed function decl) |
118+
| test.swift:82:7:82:7 | self |
114119
| test.swift:82:7:82:7 | settableField |
115120
| test.swift:83:5:83:5 | newValue |
116121
| test.swift:83:5:83:11 | set |
@@ -127,9 +132,11 @@
127132
| test.swift:102:7:102:7 | normalField |
128133
| test.swift:102:7:102:7 | self |
129134
| test.swift:102:7:102:7 | self |
135+
| test.swift:102:7:102:7 | self |
130136
| test.swift:102:7:102:7 | set |
131137
| test.swift:102:7:102:7 | value |
132138
| test.swift:104:3:104:3 | (unnamed function decl) |
139+
| test.swift:104:3:104:3 | self |
133140
| test.swift:104:3:109:3 | subscript ... |
134141
| test.swift:104:13:104:13 | x |
135142
| test.swift:104:13:104:13 | x |
@@ -149,6 +156,7 @@
149156
| test.swift:115:7:115:7 | hasWillSet1 |
150157
| test.swift:115:7:115:7 | self |
151158
| test.swift:115:7:115:7 | self |
159+
| test.swift:115:7:115:7 | self |
152160
| test.swift:115:7:115:7 | set |
153161
| test.swift:115:7:115:7 | value |
154162
| test.swift:116:5:116:25 | willSet |
@@ -159,6 +167,7 @@
159167
| test.swift:119:7:119:7 | hasWillSet2 |
160168
| test.swift:119:7:119:7 | self |
161169
| test.swift:119:7:119:7 | self |
170+
| test.swift:119:7:119:7 | self |
162171
| test.swift:119:7:119:7 | set |
163172
| test.swift:119:7:119:7 | value |
164173
| test.swift:120:5:120:5 | newValue |
@@ -169,6 +178,7 @@
169178
| test.swift:123:7:123:7 | hasDidSet1 |
170179
| test.swift:123:7:123:7 | self |
171180
| test.swift:123:7:123:7 | self |
181+
| test.swift:123:7:123:7 | self |
172182
| test.swift:123:7:123:7 | set |
173183
| test.swift:123:7:123:7 | value |
174184
| test.swift:124:5:124:24 | didSet |
@@ -189,6 +199,7 @@
189199
| test.swift:131:7:131:7 | hasBoth |
190200
| test.swift:131:7:131:7 | self |
191201
| test.swift:131:7:131:7 | self |
202+
| test.swift:131:7:131:7 | self |
192203
| test.swift:131:7:131:7 | set |
193204
| test.swift:131:7:131:7 | value |
194205
| test.swift:132:5:132:5 | newValue |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| main.swift:75:7:75:7 | yield ... | 0 | file://:0:0:0:0 | &... |
2+
| main.swift:78:7:78:14 | yield ... | 0 | main.swift:78:13:78:14 | &... |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import swift
2+
3+
from YieldStmt yield, int i
4+
where yield.getLocation().getFile().getName().matches("%swift/ql/test%")
5+
select yield, i, yield.getResult(i)

swift/ql/test/extractor-tests/statements/main.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,16 @@ if case .some(_) = x {
7070
let numbers = [1, 2, 3]
7171
for number in numbers where number % 2 == 0 {
7272
}
73+
74+
struct HasModifyAccessorDecl {
75+
var x : Int
76+
var hasModify : Int {
77+
_modify {
78+
yield &x
79+
}
80+
81+
get {
82+
return 0
83+
}
84+
}
85+
}

swift/ql/test/library-tests/controlflow/graph/Cfg.expected

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5129,8 +5129,6 @@ cfg.swift:
51295129
#-----| -> [...]
51305130

51315131
# 394| (unnamed function decl)
5132-
#-----| -> yield ...
5133-
#-----| -> TBD (YieldStmt)
51345132

51355133
# 394| enter (unnamed function decl)
51365134
#-----| -> (unnamed function decl)
@@ -5141,11 +5139,6 @@ cfg.swift:
51415139
# 394| enter set
51425140
#-----| -> set
51435141

5144-
# 394| exit (unnamed function decl)
5145-
5146-
# 394| exit (unnamed function decl) (normal)
5147-
#-----| -> exit (unnamed function decl)
5148-
51495142
# 394| exit get
51505143

51515144
# 394| exit get (normal)
@@ -5163,12 +5156,6 @@ cfg.swift:
51635156

51645157
# 394| value
51655158

5166-
# 394| yield ...
5167-
#-----| -> exit (unnamed function decl) (normal)
5168-
5169-
# 394| TBD (YieldStmt)
5170-
#-----| -> exit (unnamed function decl) (normal)
5171-
51725159
# 395| deinit
51735160
#-----| -> self
51745161

0 commit comments

Comments
 (0)