Skip to content

Commit 2dbacbc

Browse files
authored
Merge pull request #11841 from MathiasVP/swift-add-integral-types
Swift: Add integral type classes
2 parents c5038ed + cf9998b commit 2dbacbc

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
private import swift
2+
3+
/**
4+
* A floating-point type. This includes the `Float` type, the `Double`, and
5+
* builtin floating-point types.
6+
*/
7+
class FloatingPointType extends Type {
8+
FloatingPointType() {
9+
this.getName() = ["Float", "Double"] or
10+
this instanceof BuiltinFloatType
11+
}
12+
}
13+
14+
/** The `Character` type. */
15+
class CharacterType extends StructType {
16+
CharacterType() { this.getName() = "Character" }
17+
}
18+
19+
/**
20+
* An integer-like type. For example, `Int`, `Int16`, `Uint16`, etc.
21+
*/
22+
class IntegralType extends Type {
23+
IntegralType() {
24+
this.getName() =
25+
["Int", "Int8", "Int16", "Int32", "Int64", "UInt", "UInt8", "UInt16", "UInt32", "UInt64"]
26+
or
27+
this instanceof BuiltinIntegerType
28+
}
29+
}
30+
31+
/** The `Bool` type. */
32+
class BoolType extends Type {
33+
BoolType() { this.getName() = "Bool" }
34+
}
35+
36+
/**
37+
* A numeric type. This includes the integer and floating point types.
38+
*/
39+
class NumericType extends Type {
40+
NumericType() {
41+
this instanceof IntegralType or
42+
this instanceof FloatingPointType
43+
}
44+
}

swift/ql/lib/swift.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ import codeql.swift.elements.expr.InitializerCallExpr
1010
import codeql.swift.elements.expr.SelfRefExpr
1111
import codeql.swift.elements.decl.MethodDecl
1212
import codeql.swift.elements.decl.ClassOrStructDecl
13+
import codeql.swift.elements.type.NumericType
1314
import codeql.swift.Unit
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
typealias MyFloat = Float
3+
4+
func test() {
5+
var f1: Float
6+
let f2 = 123.456
7+
let f3 = f2
8+
var f4: Float?
9+
var f5: MyFloat
10+
var f6: MyFloat?
11+
12+
var d: Double
13+
14+
var c: Character
15+
16+
var i1: Int
17+
let i2 = 123
18+
let i3 = 0xFFFF
19+
var i4: Int8
20+
var i5: Int16
21+
var i6: Int32
22+
var i7: Int64
23+
24+
var u1: UInt
25+
var u2: UInt8
26+
var u3: UInt16
27+
var u4: UInt32
28+
var u5: UInt64
29+
30+
var b1: Bool
31+
let b2 = true
32+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
| numeric.swift:5:6:5:6 | f1 | Float | FloatingPointType, NumericType |
2+
| numeric.swift:6:6:6:6 | f2 | Double | FloatingPointType, NumericType |
3+
| numeric.swift:7:6:7:6 | f3 | Double | FloatingPointType, NumericType |
4+
| numeric.swift:8:6:8:6 | f4 | Float? | |
5+
| numeric.swift:9:6:9:6 | f5 | MyFloat | |
6+
| numeric.swift:10:6:10:6 | f6 | MyFloat? | |
7+
| numeric.swift:12:6:12:6 | d | Double | FloatingPointType, NumericType |
8+
| numeric.swift:14:6:14:6 | c | Character | CharacterType |
9+
| numeric.swift:16:6:16:6 | i1 | Int | IntegralType, NumericType |
10+
| numeric.swift:17:6:17:6 | i2 | Int | IntegralType, NumericType |
11+
| numeric.swift:18:6:18:6 | i3 | Int | IntegralType, NumericType |
12+
| numeric.swift:19:6:19:6 | i4 | Int8 | IntegralType, NumericType |
13+
| numeric.swift:20:6:20:6 | i5 | Int16 | IntegralType, NumericType |
14+
| numeric.swift:21:6:21:6 | i6 | Int32 | IntegralType, NumericType |
15+
| numeric.swift:22:6:22:6 | i7 | Int64 | IntegralType, NumericType |
16+
| numeric.swift:24:6:24:6 | u1 | UInt | IntegralType, NumericType |
17+
| numeric.swift:25:6:25:6 | u2 | UInt8 | IntegralType, NumericType |
18+
| numeric.swift:26:6:26:6 | u3 | UInt16 | IntegralType, NumericType |
19+
| numeric.swift:27:6:27:6 | u4 | UInt32 | IntegralType, NumericType |
20+
| numeric.swift:28:6:28:6 | u5 | UInt64 | IntegralType, NumericType |
21+
| numeric.swift:30:6:30:6 | b1 | Bool | BoolType |
22+
| numeric.swift:31:6:31:6 | b2 | Bool | BoolType |
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import swift
2+
3+
string describe(Type t) {
4+
t instanceof FloatingPointType and result = "FloatingPointType"
5+
or
6+
t instanceof CharacterType and result = "CharacterType"
7+
or
8+
t instanceof IntegralType and result = "IntegralType"
9+
or
10+
t instanceof BoolType and result = "BoolType"
11+
or
12+
t instanceof NumericType and result = "NumericType"
13+
}
14+
15+
from VarDecl v, Type t
16+
where
17+
v.getLocation().getFile().getBaseName() != "" and
18+
t = v.getType()
19+
select v, t.toString(), concat(describe(t), ", ")

0 commit comments

Comments
 (0)