From d0eb167d47b9b9ed6a21471ee3655434824f8523 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Thu, 12 Jan 2023 11:28:44 +0000 Subject: [PATCH 1/4] Swift: Merge FloatingPointType.qll into NumericOrCharType.qll, because it is a numeric type and other stuff like CharacterType is there. --- .../codeql/swift/elements/type/FloatingPointType.qll | 12 ------------ .../codeql/swift/elements/type/NumericOrCharType.qll | 11 +++++++++++ swift/ql/lib/swift.qll | 1 - 3 files changed, 11 insertions(+), 13 deletions(-) delete mode 100644 swift/ql/lib/codeql/swift/elements/type/FloatingPointType.qll diff --git a/swift/ql/lib/codeql/swift/elements/type/FloatingPointType.qll b/swift/ql/lib/codeql/swift/elements/type/FloatingPointType.qll deleted file mode 100644 index e5ae0ac3e7a0..000000000000 --- a/swift/ql/lib/codeql/swift/elements/type/FloatingPointType.qll +++ /dev/null @@ -1,12 +0,0 @@ -private import swift - -/** - * A floating-point type. This includes the `Float` type, the `Double`, and - * builtin floating-point types. - */ -class FloatingPointType extends Type { - FloatingPointType() { - this.getName() = ["Float", "Double"] or - this instanceof BuiltinFloatType - } -} diff --git a/swift/ql/lib/codeql/swift/elements/type/NumericOrCharType.qll b/swift/ql/lib/codeql/swift/elements/type/NumericOrCharType.qll index 217690039edc..31a825bd6cd1 100644 --- a/swift/ql/lib/codeql/swift/elements/type/NumericOrCharType.qll +++ b/swift/ql/lib/codeql/swift/elements/type/NumericOrCharType.qll @@ -1,5 +1,16 @@ private import swift +/** + * A floating-point type. This includes the `Float` type, the `Double`, and + * builtin floating-point types. + */ +class FloatingPointType extends Type { + FloatingPointType() { + this.getName() = ["Float", "Double"] or + this instanceof BuiltinFloatType + } +} + /** The `Character` type. */ class CharacterType extends StructType { CharacterType() { this.getName() = "Character" } diff --git a/swift/ql/lib/swift.qll b/swift/ql/lib/swift.qll index 552f2c99828d..4d79e4b73eb5 100644 --- a/swift/ql/lib/swift.qll +++ b/swift/ql/lib/swift.qll @@ -10,6 +10,5 @@ import codeql.swift.elements.expr.InitializerCallExpr import codeql.swift.elements.expr.SelfRefExpr import codeql.swift.elements.decl.MethodDecl import codeql.swift.elements.decl.ClassOrStructDecl -import codeql.swift.elements.type.FloatingPointType import codeql.swift.elements.type.NumericOrCharType import codeql.swift.Unit From 418d593a97d5666933146477b9e715f22c177e0c Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Thu, 12 Jan 2023 11:39:33 +0000 Subject: [PATCH 2/4] Swift: Replace NumericOrCharType with a more basic NumericType, and rename classes for consistency with other static languages. --- .../swift/elements/type/NumericOrCharType.qll | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/swift/ql/lib/codeql/swift/elements/type/NumericOrCharType.qll b/swift/ql/lib/codeql/swift/elements/type/NumericOrCharType.qll index 31a825bd6cd1..1becac4493b4 100644 --- a/swift/ql/lib/codeql/swift/elements/type/NumericOrCharType.qll +++ b/swift/ql/lib/codeql/swift/elements/type/NumericOrCharType.qll @@ -19,8 +19,8 @@ class CharacterType extends StructType { /** * An integer-like type. For example, `Int`, `Int16`, `Uint16`, etc. */ -class IntegerType extends Type { - IntegerType() { +class IntegralType extends Type { + IntegralType() { this.getName() = ["Int", "Int8", "Int16", "Int32", "Int64", "UInt", "UInt8", "UInt16", "UInt32", "UInt64"] or @@ -29,18 +29,16 @@ class IntegerType extends Type { } /** The `Bool` type. */ -class BooleanType extends Type { - BooleanType() { this.getName() = "Bool" } +class BoolType extends Type { + BoolType() { this.getName() = "Bool" } } /** - * A numeric-like type. This includes the types `Character`, `Bool`, and all - * the integer-like types. + * A numeric type. This includes the integer and floating point types. */ -class NumericOrCharType extends Type { - NumericOrCharType() { - this instanceof CharacterType or - this instanceof IntegerType or - this instanceof BooleanType +class NumericType extends Type { + NumericType() { + this instanceof IntegralType or + this instanceof FloatingPointType } } From 3d1b2fdbda06c77dafcb4e056908547e28f588f6 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Thu, 12 Jan 2023 11:46:51 +0000 Subject: [PATCH 3/4] Swift: Rename NumericOrCharType.qll -> Numer> NumericType.qll. --- .../elements/type/{NumericOrCharType.qll => NumericType.qll} | 0 swift/ql/lib/swift.qll | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename swift/ql/lib/codeql/swift/elements/type/{NumericOrCharType.qll => NumericType.qll} (100%) diff --git a/swift/ql/lib/codeql/swift/elements/type/NumericOrCharType.qll b/swift/ql/lib/codeql/swift/elements/type/NumericType.qll similarity index 100% rename from swift/ql/lib/codeql/swift/elements/type/NumericOrCharType.qll rename to swift/ql/lib/codeql/swift/elements/type/NumericType.qll diff --git a/swift/ql/lib/swift.qll b/swift/ql/lib/swift.qll index 4d79e4b73eb5..897bbf9b2f75 100644 --- a/swift/ql/lib/swift.qll +++ b/swift/ql/lib/swift.qll @@ -10,5 +10,5 @@ import codeql.swift.elements.expr.InitializerCallExpr import codeql.swift.elements.expr.SelfRefExpr import codeql.swift.elements.decl.MethodDecl import codeql.swift.elements.decl.ClassOrStructDecl -import codeql.swift.elements.type.NumericOrCharType +import codeql.swift.elements.type.NumericType import codeql.swift.Unit From 7f31c9c7e59270b09235448b05220d4a81171e5f Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Thu, 12 Jan 2023 14:48:40 +0000 Subject: [PATCH 4/4] Swift: Add a test. --- .../elements/type/numerictype/numeric.swift | 32 +++++++++++++++++++ .../type/numerictype/numerictype.expected | 22 +++++++++++++ .../elements/type/numerictype/numerictype.ql | 19 +++++++++++ 3 files changed, 73 insertions(+) create mode 100644 swift/ql/test/library-tests/elements/type/numerictype/numeric.swift create mode 100644 swift/ql/test/library-tests/elements/type/numerictype/numerictype.expected create mode 100644 swift/ql/test/library-tests/elements/type/numerictype/numerictype.ql diff --git a/swift/ql/test/library-tests/elements/type/numerictype/numeric.swift b/swift/ql/test/library-tests/elements/type/numerictype/numeric.swift new file mode 100644 index 000000000000..6da917e14110 --- /dev/null +++ b/swift/ql/test/library-tests/elements/type/numerictype/numeric.swift @@ -0,0 +1,32 @@ + +typealias MyFloat = Float + +func test() { + var f1: Float + let f2 = 123.456 + let f3 = f2 + var f4: Float? + var f5: MyFloat + var f6: MyFloat? + + var d: Double + + var c: Character + + var i1: Int + let i2 = 123 + let i3 = 0xFFFF + var i4: Int8 + var i5: Int16 + var i6: Int32 + var i7: Int64 + + var u1: UInt + var u2: UInt8 + var u3: UInt16 + var u4: UInt32 + var u5: UInt64 + + var b1: Bool + let b2 = true +} diff --git a/swift/ql/test/library-tests/elements/type/numerictype/numerictype.expected b/swift/ql/test/library-tests/elements/type/numerictype/numerictype.expected new file mode 100644 index 000000000000..5670aededb83 --- /dev/null +++ b/swift/ql/test/library-tests/elements/type/numerictype/numerictype.expected @@ -0,0 +1,22 @@ +| numeric.swift:5:6:5:6 | f1 | Float | FloatingPointType, NumericType | +| numeric.swift:6:6:6:6 | f2 | Double | FloatingPointType, NumericType | +| numeric.swift:7:6:7:6 | f3 | Double | FloatingPointType, NumericType | +| numeric.swift:8:6:8:6 | f4 | Float? | | +| numeric.swift:9:6:9:6 | f5 | MyFloat | | +| numeric.swift:10:6:10:6 | f6 | MyFloat? | | +| numeric.swift:12:6:12:6 | d | Double | FloatingPointType, NumericType | +| numeric.swift:14:6:14:6 | c | Character | CharacterType | +| numeric.swift:16:6:16:6 | i1 | Int | IntegralType, NumericType | +| numeric.swift:17:6:17:6 | i2 | Int | IntegralType, NumericType | +| numeric.swift:18:6:18:6 | i3 | Int | IntegralType, NumericType | +| numeric.swift:19:6:19:6 | i4 | Int8 | IntegralType, NumericType | +| numeric.swift:20:6:20:6 | i5 | Int16 | IntegralType, NumericType | +| numeric.swift:21:6:21:6 | i6 | Int32 | IntegralType, NumericType | +| numeric.swift:22:6:22:6 | i7 | Int64 | IntegralType, NumericType | +| numeric.swift:24:6:24:6 | u1 | UInt | IntegralType, NumericType | +| numeric.swift:25:6:25:6 | u2 | UInt8 | IntegralType, NumericType | +| numeric.swift:26:6:26:6 | u3 | UInt16 | IntegralType, NumericType | +| numeric.swift:27:6:27:6 | u4 | UInt32 | IntegralType, NumericType | +| numeric.swift:28:6:28:6 | u5 | UInt64 | IntegralType, NumericType | +| numeric.swift:30:6:30:6 | b1 | Bool | BoolType | +| numeric.swift:31:6:31:6 | b2 | Bool | BoolType | diff --git a/swift/ql/test/library-tests/elements/type/numerictype/numerictype.ql b/swift/ql/test/library-tests/elements/type/numerictype/numerictype.ql new file mode 100644 index 000000000000..7f3fb4591f84 --- /dev/null +++ b/swift/ql/test/library-tests/elements/type/numerictype/numerictype.ql @@ -0,0 +1,19 @@ +import swift + +string describe(Type t) { + t instanceof FloatingPointType and result = "FloatingPointType" + or + t instanceof CharacterType and result = "CharacterType" + or + t instanceof IntegralType and result = "IntegralType" + or + t instanceof BoolType and result = "BoolType" + or + t instanceof NumericType and result = "NumericType" +} + +from VarDecl v, Type t +where + v.getLocation().getFile().getBaseName() != "" and + t = v.getType() +select v, t.toString(), concat(describe(t), ", ")