Merge pull request #11169 from github/redsun82/swift-extract-last-stmts

Swift: extract remaining `Stmt`s
This commit is contained in:
Paolo Tranquilli
2022-11-08 20:51:10 +01:00
committed by GitHub
15 changed files with 81 additions and 10 deletions

View File

@@ -174,4 +174,16 @@ void StmtTranslator::fillLabeledConditionalStmt(const swift::LabeledConditionalS
fillLabeledStmt(stmt, entry);
}
codeql::FailStmt StmtTranslator::translateFailStmt(const swift::FailStmt& stmt) {
return dispatcher.createEntry(stmt);
}
codeql::PoundAssertStmt StmtTranslator::translatePoundAssertStmt(
const swift::PoundAssertStmt& stmt) {
auto entry = dispatcher.createEntry(stmt);
entry.condition = dispatcher.fetchLabel(stmt.getCondition());
entry.message = stmt.getMessage();
return entry;
}
} // namespace codeql

View File

@@ -31,6 +31,8 @@ class StmtTranslator : public AstTranslatorBase<StmtTranslator> {
codeql::SwitchStmt translateSwitchStmt(const swift::SwitchStmt& stmt);
codeql::FallthroughStmt translateFallthroughStmt(const swift::FallthroughStmt& stmt);
codeql::YieldStmt translateYieldStmt(const swift::YieldStmt& stmt);
codeql::FailStmt translateFailStmt(const swift::FailStmt& stmt);
codeql::PoundAssertStmt translatePoundAssertStmt(const swift::PoundAssertStmt& stmt);
private:
void fillLabeledStmt(const swift::LabeledStmt& stmt, codeql::LabeledStmt& entry);

View File

@@ -1116,6 +1116,10 @@ module Raw {
class PoundAssertStmt extends @pound_assert_stmt, Stmt {
override string toString() { result = "PoundAssertStmt" }
Expr getCondition() { pound_assert_stmts(this, result, _) }
string getMessage() { pound_assert_stmts(this, _, result) }
}
class ReturnStmt extends @return_stmt, Stmt {

View File

@@ -1,10 +1,36 @@
// generated by codegen/codegen.py
private import codeql.swift.generated.Synth
private import codeql.swift.generated.Raw
import codeql.swift.elements.expr.Expr
import codeql.swift.elements.stmt.Stmt
module Generated {
class PoundAssertStmt extends Synth::TPoundAssertStmt, Stmt {
override string getAPrimaryQlClass() { result = "PoundAssertStmt" }
/**
* Gets the condition of this pound assert statement.
*
* This includes nodes from the "hidden" AST. It can be overridden in subclasses to change the
* behavior of both the `Immediate` and non-`Immediate` versions.
*/
Expr getImmediateCondition() {
result =
Synth::convertExprFromRaw(Synth::convertPoundAssertStmtToRaw(this)
.(Raw::PoundAssertStmt)
.getCondition())
}
/**
* Gets the condition of this pound assert statement.
*/
final Expr getCondition() { result = getImmediateCondition().resolve() }
/**
* Gets the message of this pound assert statement.
*/
string getMessage() {
result = Synth::convertPoundAssertStmtToRaw(this).(Raw::PoundAssertStmt).getMessage()
}
}
}

View File

@@ -1696,7 +1696,9 @@ labeled_stmt_labels( //dir=stmt
);
pound_assert_stmts( //dir=stmt
unique int id: @pound_assert_stmt
unique int id: @pound_assert_stmt,
int condition: @expr_or_none ref,
string message: string ref
);
return_stmts( //dir=stmt

View File

@@ -0,0 +1 @@
| fail.swift:2:15:2:22 | fail |

View File

@@ -0,0 +1,7 @@
// generated by codegen/codegen.py
import codeql.swift.elements
import TestUtils
from FailStmt x
where toBeTested(x) and not x.isUnknown()
select x

View File

@@ -1,4 +0,0 @@
// generated by codegen/codegen.py
After a swift source file is added in this directory and codegen/codegen.py is run again, test queries
will appear and this file will be deleted

View File

@@ -0,0 +1,3 @@
struct S {
init?() { return nil }
}

View File

@@ -1,4 +0,0 @@
// generated by codegen/codegen.py
After a swift source file is added in this directory and codegen/codegen.py is run again, test queries
will appear and this file will be deleted

View File

@@ -0,0 +1,3 @@
| static_assert.swift:2:1:2:15 | #assert ... | getCondition: | static_assert.swift:2:9:2:14 | ... .==(_:_:) ... | getMessage: | |
| static_assert.swift:3:1:3:24 | #assert ... | getCondition: | static_assert.swift:3:9:3:14 | ... .!=(_:_:) ... | getMessage: | hello |
| static_failing_assert.swift:4:1:4:24 | #assert ... | getCondition: | static_failing_assert.swift:4:9:4:14 | ... .==(_:_:) ... | getMessage: | oh my |

View File

@@ -0,0 +1,11 @@
// generated by codegen/codegen.py
import codeql.swift.elements
import TestUtils
from PoundAssertStmt x, Expr getCondition, string getMessage
where
toBeTested(x) and
not x.isUnknown() and
getCondition = x.getCondition() and
getMessage = x.getMessage()
select x, "getCondition:", getCondition, "getMessage:", getMessage

View File

@@ -0,0 +1,3 @@
//codeql-extractor-options: -enable-experimental-static-assert
#assert(1 == 1)
#assert(1 != 0, "hello")

View File

@@ -0,0 +1,4 @@
//codeql-extractor-options: -enable-experimental-static-assert
//codeql-extractor-expected-status: 1
#assert(1 == 0, "oh my")

View File

@@ -742,7 +742,8 @@ class LabeledStmt(Stmt):
label: optional[string]
class PoundAssertStmt(Stmt):
pass
condition: Expr
message: string
class ReturnStmt(Stmt):
result: optional[Expr] | child