Skip to content

Commit c692d8c

Browse files
Add qtil.swift
1 parent d12a59d commit c692d8c

18 files changed

Lines changed: 223 additions & 0 deletions

swift/src/codeql-pack.lock.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
lockVersion: 1.0.0
3+
dependencies:
4+
codeql/controlflow:
5+
version: 1.0.12
6+
codeql/dataflow:
7+
version: 1.1.6
8+
codeql/mad:
9+
version: 1.0.12
10+
codeql/regex:
11+
version: 1.0.12
12+
codeql/ssa:
13+
version: 1.0.12
14+
codeql/swift-all:
15+
version: 2.0.4
16+
codeql/tutorial:
17+
version: 1.0.12
18+
codeql/typetracking:
19+
version: 1.0.12
20+
codeql/util:
21+
version: 1.0.12
22+
compiled: false

swift/src/qlpack.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: mfairhurst/qtil-swift
2+
library: true
3+
warnOnImplicitThis: false
4+
version: 0.0.1
5+
license: MIT
6+
dependencies:
7+
codeql/swift-all: '>=0.0.1 <5.0.0'
8+
mfairhurst/qtil: 0.0.1

swift/src/qtil/Swift.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module Qtil {
2+
private import qtil.Qtil as Common
3+
// Importing qtil.Swift should import all of Qtil.
4+
import Common::Qtil
5+
import qtil.swift.ast.TwoOperands
6+
import qtil.swift.format.QlFormat
7+
import qtil.swift.graph.CustomPathProblem
8+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import swift as swift
2+
import qtil.parameterization.SignatureTypes
3+
4+
/**
5+
* A module for dealing with pairs of exclusive operands in C++ ASTs.
6+
*
7+
* For instance, to find cases where one operand is an integer and the other is a constant, you
8+
* will want to to perform checks on each operand separately and consistently without worrying about
9+
* order. This module makes this common pattern easy to implement.
10+
*
11+
* This module takes two type parameters:
12+
* - `Operand`: the type of the operands (e.g. `Expr`)
13+
* - `HasOperands`: a type that has operands of type `Operand` (e.g. `BinaryExpr`)
14+
*
15+
* ```ql
16+
* // Using this module:
17+
* predicate myBinaryTestNew(BinaryExpr e) {
18+
* exists(TwoOperands<BinaryExpr>::Set set |
19+
* set.getOperation() = e and
20+
* set.someOperand().isInteger() and
21+
* set.otherOperand().isConstant()
22+
* )
23+
* }
24+
*
25+
* // Is roughly equivalent to:
26+
* predicate myBinaryTestOld(BinaryExpr e) {
27+
* exists(Expr a, Expr b |
28+
* e.getAnOperand() = a and
29+
* e.getAnOperand() = b and
30+
* a != b and
31+
* a.isInteger() and
32+
* b.isConstant()
33+
* )
34+
* }
35+
* ```
36+
*
37+
* Some caution about using this module: for each use, two `Set` objects exst. If you do not
38+
* properly constrain the usage of `someOperand()` and `otherOperand()`, then these members could
39+
* hold for different `Set`s. Therefore, `someOperand()` and `otherOperand()` may be the same
40+
* operand. This will not happen if the `Set` is properly constrained across the two member
41+
* invocations.
42+
*
43+
* ```ql
44+
* predicate bug(BinaryExpr e) {
45+
* // Bad: the two sets are not constrained to the same instance, therefore the operands not
46+
* // guaranteed to be different.
47+
* TwoOperands<BinaryExpr>::getASet(e).someOperand().isInteger() and
48+
* TwoOperands<BinaryExpr>::getASet(e).otherOperand().isConstant()
49+
* }
50+
* ```
51+
*/
52+
module TwoOperands<Signature<swift::BinaryExpr>::Type BinOp> {
53+
private import qtil.ast.TwoOperands as Make
54+
import Make::TwoOperands<swift::Expr, BinOp>
55+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
private import qtil.format.QLFormat
2+
private import swift
3+
private import qtil.swift.locations.Locatable
4+
5+
import QlFormat<Location, SwiftLocatableConfig>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
private import qtil.locations.CustomPathProblem
2+
private import qtil.swift.locations.Locatable
3+
private import swift
4+
5+
// Import the swift specific configuration for making custom path problems.
6+
import PathProblem<Location, SwiftLocatableConfig>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
private import qtil.locations.Locatable
2+
private import swift as swift
3+
4+
/**
5+
* A module to declare `Locatable`s specific to swift for use in other qtil modules.
6+
*/
7+
module SwiftLocatableConfig implements LocatableConfig<swift::Location> {
8+
class Locatable = swift::Locatable;
9+
}

swift/test/codeql-pack.lock.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
lockVersion: 1.0.0
3+
dependencies:
4+
codeql/controlflow:
5+
version: 1.0.12
6+
codeql/dataflow:
7+
version: 1.1.6
8+
codeql/mad:
9+
version: 1.0.12
10+
codeql/regex:
11+
version: 1.0.12
12+
codeql/ssa:
13+
version: 1.0.12
14+
codeql/swift-all:
15+
version: 2.0.4
16+
codeql/tutorial:
17+
version: 1.0.12
18+
codeql/typetracking:
19+
version: 1.0.12
20+
codeql/util:
21+
version: 1.0.12
22+
compiled: false

swift/test/qlpack.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: mfairhurst/qtil-swift-test
2+
library: true
3+
warnOnImplicitThis: false
4+
version: 0.0.1
5+
license: MIT
6+
dependencies:
7+
mfairhurst/qtil-swift: "*"
8+
extractor: swift
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.swift:5:5:5:9 | ... .+(_:_:) ... | test.swift:5:5:5:5 | a | test.swift:5:9:5:9 | b |
2+
| test.swift:6:5:6:9 | ... .+(_:_:) ... | test.swift:6:9:6:9 | a | test.swift:6:5:6:5 | b |

0 commit comments

Comments
 (0)