Skip to content

Commit 9ee4f8b

Browse files
committed
Swift: extract remaining Stmts
`FailStmt` are `return nil` in fallible initializers. `PoundAssertStmt` are an experimental feature for compile time assertions.
1 parent 6fb021a commit 9ee4f8b

File tree

15 files changed

+81
-10
lines changed

15 files changed

+81
-10
lines changed

swift/extractor/translators/StmtTranslator.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,16 @@ void StmtTranslator::fillLabeledConditionalStmt(const swift::LabeledConditionalS
174174
fillLabeledStmt(stmt, entry);
175175
}
176176

177+
codeql::FailStmt StmtTranslator::translateFailStmt(const swift::FailStmt& stmt) {
178+
return dispatcher.createEntry(stmt);
179+
}
180+
181+
codeql::PoundAssertStmt StmtTranslator::translatePoundAssertStmt(
182+
const swift::PoundAssertStmt& stmt) {
183+
auto entry = dispatcher.createEntry(stmt);
184+
entry.condition = dispatcher.fetchLabel(stmt.getCondition());
185+
entry.message = stmt.getMessage();
186+
return entry;
187+
}
188+
177189
} // namespace codeql

swift/extractor/translators/StmtTranslator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class StmtTranslator : public AstTranslatorBase<StmtTranslator> {
3131
codeql::SwitchStmt translateSwitchStmt(const swift::SwitchStmt& stmt);
3232
codeql::FallthroughStmt translateFallthroughStmt(const swift::FallthroughStmt& stmt);
3333
codeql::YieldStmt translateYieldStmt(const swift::YieldStmt& stmt);
34+
codeql::FailStmt translateFailStmt(const swift::FailStmt& stmt);
35+
codeql::PoundAssertStmt translatePoundAssertStmt(const swift::PoundAssertStmt& stmt);
3436

3537
private:
3638
void fillLabeledStmt(const swift::LabeledStmt& stmt, codeql::LabeledStmt& entry);

swift/ql/lib/codeql/swift/generated/Raw.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,10 @@ module Raw {
11161116

11171117
class PoundAssertStmt extends @pound_assert_stmt, Stmt {
11181118
override string toString() { result = "PoundAssertStmt" }
1119+
1120+
Expr getCondition() { pound_assert_stmts(this, result, _) }
1121+
1122+
string getMessage() { pound_assert_stmts(this, _, result) }
11191123
}
11201124

11211125
class ReturnStmt extends @return_stmt, Stmt {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,36 @@
11
// generated by codegen/codegen.py
22
private import codeql.swift.generated.Synth
33
private import codeql.swift.generated.Raw
4+
import codeql.swift.elements.expr.Expr
45
import codeql.swift.elements.stmt.Stmt
56

67
module Generated {
78
class PoundAssertStmt extends Synth::TPoundAssertStmt, Stmt {
89
override string getAPrimaryQlClass() { result = "PoundAssertStmt" }
10+
11+
/**
12+
* Gets the condition of this pound assert statement.
13+
*
14+
* This includes nodes from the "hidden" AST. It can be overridden in subclasses to change the
15+
* behavior of both the `Immediate` and non-`Immediate` versions.
16+
*/
17+
Expr getImmediateCondition() {
18+
result =
19+
Synth::convertExprFromRaw(Synth::convertPoundAssertStmtToRaw(this)
20+
.(Raw::PoundAssertStmt)
21+
.getCondition())
22+
}
23+
24+
/**
25+
* Gets the condition of this pound assert statement.
26+
*/
27+
final Expr getCondition() { result = getImmediateCondition().resolve() }
28+
29+
/**
30+
* Gets the message of this pound assert statement.
31+
*/
32+
string getMessage() {
33+
result = Synth::convertPoundAssertStmtToRaw(this).(Raw::PoundAssertStmt).getMessage()
34+
}
935
}
1036
}

swift/ql/lib/swift.dbscheme

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1696,7 +1696,9 @@ labeled_stmt_labels( //dir=stmt
16961696
);
16971697

16981698
pound_assert_stmts( //dir=stmt
1699-
unique int id: @pound_assert_stmt
1699+
unique int id: @pound_assert_stmt,
1700+
int condition: @expr_or_none ref,
1701+
string message: string ref
17001702
);
17011703

17021704
return_stmts( //dir=stmt
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| fail.swift:2:15:2:22 | fail |
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// generated by codegen/codegen.py
2+
import codeql.swift.elements
3+
import TestUtils
4+
5+
from FailStmt x
6+
where toBeTested(x) and not x.isUnknown()
7+
select x

swift/ql/test/extractor-tests/generated/stmt/FailStmt/MISSING_SOURCE.txt

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
struct S {
2+
init?() { return nil }
3+
}

swift/ql/test/extractor-tests/generated/stmt/PoundAssertStmt/MISSING_SOURCE.txt

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)