From ef8a997c999112bbb025cc376ca1a9d6ba35c229 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Mon, 11 Sep 2023 23:40:39 +0100 Subject: [PATCH 01/10] Swift: Extend the test. --- .../type/nominaltype/nominaltype.expected | 41 ++++++++++-------- .../type/nominaltype/nominaltype.swift | 43 +++++++++++++++++-- .../type/nominaltype/nominaltypedecl.expected | 31 +++++++------ .../type/nominaltype/nominaltypedecl.ql | 4 +- 4 files changed, 82 insertions(+), 37 deletions(-) diff --git a/swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.expected b/swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.expected index d4243b80f62..9a330aa415e 100644 --- a/swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.expected +++ b/swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.expected @@ -1,19 +1,22 @@ -| nominaltype.swift:54:6:54:6 | a | A | getFullName:A, getName:A, getUnderlyingType:A | -| nominaltype.swift:55:6:55:6 | a_alias | A_alias | getAliasedType:A, getFullName:A_alias, getName:A_alias, getUnderlyingType:A | -| nominaltype.swift:56:6:56:6 | a_optional_alias | A_optional_alias | getAliasedType:A?, getFullName:A_optional_alias, getName:A_optional_alias, getUnderlyingType:A? | -| nominaltype.swift:57:6:57:6 | b1 | B1 | getABaseType:A, getFullName:B1, getName:B1, getUnderlyingType:B1 | -| nominaltype.swift:58:6:58:6 | b2 | B2 | getABaseType:A_alias, getFullName:B2, getName:B2, getUnderlyingType:B2 | -| nominaltype.swift:59:6:59:6 | b1_alias | B1_alias | getABaseType:A, getAliasedType:B1, getFullName:B1_alias, getName:B1_alias, getUnderlyingType:B1 | -| nominaltype.swift:60:6:60:6 | b2_alias | B2_alias | getABaseType:A_alias, getAliasedType:B2, getFullName:B2_alias, getName:B2_alias, getUnderlyingType:B2 | -| nominaltype.swift:61:6:61:6 | p | P | getFullName:P, getName:P, getUnderlyingType:P | -| nominaltype.swift:62:6:62:6 | p_alias | P_alias | getFullName:P_alias, getName:P_alias, getUnderlyingType:P_alias | -| nominaltype.swift:63:6:63:6 | c1 | C1 | getABaseType:P, getFullName:C1, getName:C1, getUnderlyingType:C1 | -| nominaltype.swift:64:6:64:6 | c2 | C2 | getABaseType:P_alias, getFullName:C2, getName:C2, getUnderlyingType:C2 | -| nominaltype.swift:65:6:65:6 | c1_alias | C1_alias | getABaseType:P, getAliasedType:C1, getFullName:C1_alias, getName:C1_alias, getUnderlyingType:C1 | -| nominaltype.swift:66:6:66:6 | c2_alias | C2_alias | getABaseType:P_alias, getAliasedType:C2, getFullName:C2_alias, getName:C2_alias, getUnderlyingType:C2 | -| nominaltype.swift:67:6:67:6 | o | Outer | getFullName:Outer, getName:Outer, getUnderlyingType:Outer | -| nominaltype.swift:68:6:68:6 | oi | Outer.Inner | getFullName:Outer.Inner, getName:Inner, getUnderlyingType:Outer.Inner | -| nominaltype.swift:69:6:69:6 | oia | Outer.Inner.InnerAlias | getABaseType:FixedWidthInteger, getABaseType:SignedInteger, getABaseType:_ExpressibleByBuiltinIntegerLiteral, getAliasedType:Int, getFullName:Outer.Inner.InnerAlias, getName:InnerAlias, getUnderlyingType:Int | -| nominaltype.swift:70:6:70:6 | aa | Any? | getFullName:Any?, getName:Any?, getUnderlyingType:Any? | -| nominaltype.swift:71:6:71:6 | p1p2 | P1P2 | getFullName:P1P2, getName:P1P2, getUnderlyingType:P1P2 | -| nominaltype.swift:72:6:72:6 | boxInt | Box | getFullName:Box, getName:Box, getUnderlyingType:Box | +| nominaltype.swift:88:6:88:6 | i | Int | getABaseType:FixedWidthInteger, getABaseType:SignedInteger, getABaseType:_ExpressibleByBuiltinIntegerLiteral, getFullName:Int, getName:Int, getUnderlyingType:Int | +| nominaltype.swift:89:6:89:6 | j | Any? | getFullName:Any?, getName:Any?, getUnderlyingType:Any? | +| nominaltype.swift:90:6:90:6 | a | A | getFullName:A, getName:A, getUnderlyingType:A | +| nominaltype.swift:91:6:91:6 | a_alias | A_alias | getAliasedType:A, getFullName:A_alias, getName:A_alias, getUnderlyingType:A | +| nominaltype.swift:92:6:92:6 | a_optional_alias | A_optional_alias | getAliasedType:A?, getFullName:A_optional_alias, getName:A_optional_alias, getUnderlyingType:A? | +| nominaltype.swift:93:6:93:6 | b1 | B1 | getABaseType:A, getFullName:B1, getName:B1, getUnderlyingType:B1 | +| nominaltype.swift:94:6:94:6 | b2 | B2 | getABaseType:A_alias, getFullName:B2, getName:B2, getUnderlyingType:B2 | +| nominaltype.swift:95:6:95:6 | b1_alias | B1_alias | getABaseType:A, getAliasedType:B1, getFullName:B1_alias, getName:B1_alias, getUnderlyingType:B1 | +| nominaltype.swift:96:6:96:6 | b2_alias | B2_alias | getABaseType:A_alias, getAliasedType:B2, getFullName:B2_alias, getName:B2_alias, getUnderlyingType:B2 | +| nominaltype.swift:97:6:97:6 | p | P | getFullName:P, getName:P, getUnderlyingType:P | +| nominaltype.swift:98:6:98:6 | p_alias | P_alias | getFullName:P_alias, getName:P_alias, getUnderlyingType:P_alias | +| nominaltype.swift:99:6:99:6 | c1 | C1 | getABaseType:P, getFullName:C1, getName:C1, getUnderlyingType:C1 | +| nominaltype.swift:100:6:100:6 | c2 | C2 | getABaseType:P_alias, getFullName:C2, getName:C2, getUnderlyingType:C2 | +| nominaltype.swift:101:6:101:6 | c1_alias | C1_alias | getABaseType:P, getAliasedType:C1, getFullName:C1_alias, getName:C1_alias, getUnderlyingType:C1 | +| nominaltype.swift:102:6:102:6 | c2_alias | C2_alias | getABaseType:P_alias, getAliasedType:C2, getFullName:C2_alias, getName:C2_alias, getUnderlyingType:C2 | +| nominaltype.swift:103:6:103:6 | o | Outer | getFullName:Outer, getName:Outer, getUnderlyingType:Outer | +| nominaltype.swift:104:6:104:6 | oi | Outer.Inner | getFullName:Outer.Inner, getName:Inner, getUnderlyingType:Outer.Inner | +| nominaltype.swift:105:6:105:6 | oia | Outer.Inner.InnerAlias | getAliasedType:A, getFullName:Outer.Inner.InnerAlias, getName:InnerAlias, getUnderlyingType:A | +| nominaltype.swift:106:6:106:6 | p1p2 | P1P2 | getFullName:P1P2, getName:P1P2, getUnderlyingType:P1P2 | +| nominaltype.swift:107:6:107:6 | boxInt | Box | getFullName:Box, getName:Box, getUnderlyingType:Box | +| nominaltype.swift:108:6:108:6 | d1 | D1 | getFullName:D1, getName:D1, getUnderlyingType:D1 | +| nominaltype.swift:109:6:109:6 | d2 | D2_alias | getAliasedType:D2, getFullName:D2_alias, getName:D2_alias, getUnderlyingType:D2 | diff --git a/swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.swift b/swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.swift index 26f73c49f55..b674e13cd71 100644 --- a/swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.swift +++ b/swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.swift @@ -16,6 +16,8 @@ typealias B1_alias = B1 typealias B2_alias = B2 +// --- + protocol P { } @@ -31,12 +33,16 @@ typealias C1_alias = C1 typealias C2_alias = C2 +// --- + class Outer { class Inner { - typealias InnerAlias = Int + typealias InnerAlias = A } } +// --- + protocol P1 { } @@ -45,12 +51,42 @@ protocol P2 { typealias P1P2 = P1 & P2 +// --- + class Box { } +// --- +class D1 { +} + +protocol P3 { +} + +extension D1 : P3 { +} + +// --- + +class D2 { +} + +typealias D2_alias = D2 + +protocol P4 { +} + +typealias P4_alias = P4 + +extension D2_alias : P4_alias { +} + +// --- func test() { + var i : Int + var j : Any? var a : A var a_alias : A_alias var a_optional_alias : A_optional_alias @@ -67,7 +103,8 @@ func test() { var o : Outer var oi : Outer.Inner var oia : Outer.Inner.InnerAlias - var aa : Any? var p1p2 : P1P2 - var boxInt : Box + var boxInt : Box + var d1: D1 + var d2: D2_alias } diff --git a/swift/ql/test/library-tests/elements/type/nominaltype/nominaltypedecl.expected b/swift/ql/test/library-tests/elements/type/nominaltype/nominaltypedecl.expected index b62b789b1a8..f5039a03cb8 100644 --- a/swift/ql/test/library-tests/elements/type/nominaltype/nominaltypedecl.expected +++ b/swift/ql/test/library-tests/elements/type/nominaltype/nominaltypedecl.expected @@ -1,14 +1,17 @@ -| nominaltype.swift:54:6:54:6 | a | A | getFullName:A, getName:A | -| nominaltype.swift:55:6:55:6 | a_alias | A_alias | getAliasedType:A, getFullName:A_alias, getName:A_alias | -| nominaltype.swift:56:6:56:6 | a_optional_alias | A_optional_alias | getAliasedType:A?, getFullName:A_optional_alias, getName:A_optional_alias | -| nominaltype.swift:57:6:57:6 | b1 | B1 | getABaseType:A, getFullName:B1, getName:B1 | -| nominaltype.swift:58:6:58:6 | b2 | B2 | getABaseType:A_alias, getFullName:B2, getName:B2 | -| nominaltype.swift:59:6:59:6 | b1_alias | B1_alias | getAliasedType:B1, getFullName:B1_alias, getName:B1_alias | -| nominaltype.swift:60:6:60:6 | b2_alias | B2_alias | getAliasedType:B2, getFullName:B2_alias, getName:B2_alias | -| nominaltype.swift:63:6:63:6 | c1 | C1 | getABaseType:P, getFullName:C1, getName:C1 | -| nominaltype.swift:64:6:64:6 | c2 | C2 | getABaseType:P_alias, getFullName:C2, getName:C2 | -| nominaltype.swift:65:6:65:6 | c1_alias | C1_alias | getAliasedType:C1, getFullName:C1_alias, getName:C1_alias | -| nominaltype.swift:66:6:66:6 | c2_alias | C2_alias | getAliasedType:C2, getFullName:C2_alias, getName:C2_alias | -| nominaltype.swift:67:6:67:6 | o | Outer | getFullName:Outer, getName:Outer | -| nominaltype.swift:68:6:68:6 | oi | Inner | getFullName:Outer.Inner, getName:Inner | -| nominaltype.swift:69:6:69:6 | oia | InnerAlias | getAliasedType:Int, getFullName:Outer.Inner.InnerAlias, getName:InnerAlias | +| nominaltype.swift:88:6:88:6 | i | Int | getABaseType:FixedWidthInteger, getABaseType:SignedInteger, getABaseType:_ExpressibleByBuiltinIntegerLiteral, getABaseTypeDecl:FixedWidthInteger, getABaseTypeDecl:SignedInteger, getABaseTypeDecl:_ExpressibleByBuiltinIntegerLiteral, getFullName:Int, getName:Int | +| nominaltype.swift:90:6:90:6 | a | A | getFullName:A, getName:A | +| nominaltype.swift:91:6:91:6 | a_alias | A_alias | getAliasedType:A, getFullName:A_alias, getName:A_alias | +| nominaltype.swift:92:6:92:6 | a_optional_alias | A_optional_alias | getAliasedType:A?, getFullName:A_optional_alias, getName:A_optional_alias | +| nominaltype.swift:93:6:93:6 | b1 | B1 | getABaseType:A, getABaseTypeDecl:A, getFullName:B1, getName:B1 | +| nominaltype.swift:94:6:94:6 | b2 | B2 | getABaseType:A_alias, getFullName:B2, getName:B2 | +| nominaltype.swift:95:6:95:6 | b1_alias | B1_alias | getAliasedType:B1, getFullName:B1_alias, getName:B1_alias | +| nominaltype.swift:96:6:96:6 | b2_alias | B2_alias | getAliasedType:B2, getFullName:B2_alias, getName:B2_alias | +| nominaltype.swift:99:6:99:6 | c1 | C1 | getABaseType:P, getABaseTypeDecl:P, getFullName:C1, getName:C1 | +| nominaltype.swift:100:6:100:6 | c2 | C2 | getABaseType:P_alias, getFullName:C2, getName:C2 | +| nominaltype.swift:101:6:101:6 | c1_alias | C1_alias | getAliasedType:C1, getFullName:C1_alias, getName:C1_alias | +| nominaltype.swift:102:6:102:6 | c2_alias | C2_alias | getAliasedType:C2, getFullName:C2_alias, getName:C2_alias | +| nominaltype.swift:103:6:103:6 | o | Outer | getFullName:Outer, getName:Outer | +| nominaltype.swift:104:6:104:6 | oi | Inner | getFullName:Outer.Inner, getName:Inner | +| nominaltype.swift:105:6:105:6 | oia | InnerAlias | getAliasedType:A, getFullName:Outer.Inner.InnerAlias, getName:InnerAlias | +| nominaltype.swift:108:6:108:6 | d1 | D1 | getFullName:D1, getName:D1 | +| nominaltype.swift:109:6:109:6 | d2 | D2_alias | getAliasedType:D2, getFullName:D2_alias, getName:D2_alias | diff --git a/swift/ql/test/library-tests/elements/type/nominaltype/nominaltypedecl.ql b/swift/ql/test/library-tests/elements/type/nominaltype/nominaltypedecl.ql index be1e4c58efa..83fb434a7f7 100644 --- a/swift/ql/test/library-tests/elements/type/nominaltype/nominaltypedecl.ql +++ b/swift/ql/test/library-tests/elements/type/nominaltype/nominaltypedecl.ql @@ -7,7 +7,9 @@ string describe(TypeDecl td) { or result = "getAliasedType:" + td.(TypeAliasDecl).getAliasedType().toString() or - result = "getABaseType:" + td.(NominalTypeDecl).getABaseType().toString() + result = "getABaseType:" + td.getABaseType().toString() + or + result = "getABaseTypeDecl:" + td.getABaseTypeDecl().toString() } from VarDecl v, TypeDecl td From 4f39cb65c215111a69bf8ed8440e64b3db412b0c Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Mon, 18 Sep 2023 19:57:04 +0100 Subject: [PATCH 02/10] ed test --- .../type/nominaltype/nominaltype.expected | 42 +++++++++---------- .../type/nominaltype/nominaltype.swift | 10 +---- .../type/nominaltype/nominaltypedecl.expected | 32 +++++++------- 3 files changed, 37 insertions(+), 47 deletions(-) diff --git a/swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.expected b/swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.expected index 9a330aa415e..35b87914c18 100644 --- a/swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.expected +++ b/swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.expected @@ -1,22 +1,20 @@ -| nominaltype.swift:88:6:88:6 | i | Int | getABaseType:FixedWidthInteger, getABaseType:SignedInteger, getABaseType:_ExpressibleByBuiltinIntegerLiteral, getFullName:Int, getName:Int, getUnderlyingType:Int | -| nominaltype.swift:89:6:89:6 | j | Any? | getFullName:Any?, getName:Any?, getUnderlyingType:Any? | -| nominaltype.swift:90:6:90:6 | a | A | getFullName:A, getName:A, getUnderlyingType:A | -| nominaltype.swift:91:6:91:6 | a_alias | A_alias | getAliasedType:A, getFullName:A_alias, getName:A_alias, getUnderlyingType:A | -| nominaltype.swift:92:6:92:6 | a_optional_alias | A_optional_alias | getAliasedType:A?, getFullName:A_optional_alias, getName:A_optional_alias, getUnderlyingType:A? | -| nominaltype.swift:93:6:93:6 | b1 | B1 | getABaseType:A, getFullName:B1, getName:B1, getUnderlyingType:B1 | -| nominaltype.swift:94:6:94:6 | b2 | B2 | getABaseType:A_alias, getFullName:B2, getName:B2, getUnderlyingType:B2 | -| nominaltype.swift:95:6:95:6 | b1_alias | B1_alias | getABaseType:A, getAliasedType:B1, getFullName:B1_alias, getName:B1_alias, getUnderlyingType:B1 | -| nominaltype.swift:96:6:96:6 | b2_alias | B2_alias | getABaseType:A_alias, getAliasedType:B2, getFullName:B2_alias, getName:B2_alias, getUnderlyingType:B2 | -| nominaltype.swift:97:6:97:6 | p | P | getFullName:P, getName:P, getUnderlyingType:P | -| nominaltype.swift:98:6:98:6 | p_alias | P_alias | getFullName:P_alias, getName:P_alias, getUnderlyingType:P_alias | -| nominaltype.swift:99:6:99:6 | c1 | C1 | getABaseType:P, getFullName:C1, getName:C1, getUnderlyingType:C1 | -| nominaltype.swift:100:6:100:6 | c2 | C2 | getABaseType:P_alias, getFullName:C2, getName:C2, getUnderlyingType:C2 | -| nominaltype.swift:101:6:101:6 | c1_alias | C1_alias | getABaseType:P, getAliasedType:C1, getFullName:C1_alias, getName:C1_alias, getUnderlyingType:C1 | -| nominaltype.swift:102:6:102:6 | c2_alias | C2_alias | getABaseType:P_alias, getAliasedType:C2, getFullName:C2_alias, getName:C2_alias, getUnderlyingType:C2 | -| nominaltype.swift:103:6:103:6 | o | Outer | getFullName:Outer, getName:Outer, getUnderlyingType:Outer | -| nominaltype.swift:104:6:104:6 | oi | Outer.Inner | getFullName:Outer.Inner, getName:Inner, getUnderlyingType:Outer.Inner | -| nominaltype.swift:105:6:105:6 | oia | Outer.Inner.InnerAlias | getAliasedType:A, getFullName:Outer.Inner.InnerAlias, getName:InnerAlias, getUnderlyingType:A | -| nominaltype.swift:106:6:106:6 | p1p2 | P1P2 | getFullName:P1P2, getName:P1P2, getUnderlyingType:P1P2 | -| nominaltype.swift:107:6:107:6 | boxInt | Box | getFullName:Box, getName:Box, getUnderlyingType:Box | -| nominaltype.swift:108:6:108:6 | d1 | D1 | getFullName:D1, getName:D1, getUnderlyingType:D1 | -| nominaltype.swift:109:6:109:6 | d2 | D2_alias | getAliasedType:D2, getFullName:D2_alias, getName:D2_alias, getUnderlyingType:D2 | +| nominaltype.swift:84:6:84:6 | i | Int | getABaseType:FixedWidthInteger, getABaseType:SignedInteger, getABaseType:_ExpressibleByBuiltinIntegerLiteral, getFullName:Int, getName:Int, getUnderlyingType:Int | +| nominaltype.swift:85:6:85:6 | j | Any? | getFullName:Any?, getName:Any?, getUnderlyingType:Any? | +| nominaltype.swift:86:6:86:6 | a | A | getFullName:A, getName:A, getUnderlyingType:A | +| nominaltype.swift:87:6:87:6 | a_alias | A_alias | getAliasedType:A, getFullName:A_alias, getName:A_alias, getUnderlyingType:A | +| nominaltype.swift:88:6:88:6 | a_optional_alias | A_optional_alias | getAliasedType:A?, getFullName:A_optional_alias, getName:A_optional_alias, getUnderlyingType:A? | +| nominaltype.swift:89:6:89:6 | b1 | B1 | getABaseType:A, getFullName:B1, getName:B1, getUnderlyingType:B1 | +| nominaltype.swift:90:6:90:6 | b2 | B2 | getABaseType:A_alias, getFullName:B2, getName:B2, getUnderlyingType:B2 | +| nominaltype.swift:91:6:91:6 | b1_alias | B1_alias | getABaseType:A, getAliasedType:B1, getFullName:B1_alias, getName:B1_alias, getUnderlyingType:B1 | +| nominaltype.swift:92:6:92:6 | b2_alias | B2_alias | getABaseType:A_alias, getAliasedType:B2, getFullName:B2_alias, getName:B2_alias, getUnderlyingType:B2 | +| nominaltype.swift:93:6:93:6 | p | P | getFullName:P, getName:P, getUnderlyingType:P | +| nominaltype.swift:94:6:94:6 | p_alias | P_alias | getFullName:P_alias, getName:P_alias, getUnderlyingType:P_alias | +| nominaltype.swift:95:6:95:6 | c1 | C1 | getABaseType:P, getFullName:C1, getName:C1, getUnderlyingType:C1 | +| nominaltype.swift:96:6:96:6 | c2 | C2 | getABaseType:P_alias, getFullName:C2, getName:C2, getUnderlyingType:C2 | +| nominaltype.swift:97:6:97:6 | o | Outer | getFullName:Outer, getName:Outer, getUnderlyingType:Outer | +| nominaltype.swift:98:6:98:6 | oi | Outer.Inner | getFullName:Outer.Inner, getName:Inner, getUnderlyingType:Outer.Inner | +| nominaltype.swift:99:6:99:6 | oia | Outer.Inner.InnerAlias | getAliasedType:A, getFullName:Outer.Inner.InnerAlias, getName:InnerAlias, getUnderlyingType:A | +| nominaltype.swift:100:6:100:6 | p1p2 | P1P2 | getFullName:P1P2, getName:P1P2, getUnderlyingType:P1P2 | +| nominaltype.swift:101:6:101:6 | boxInt | Box | getFullName:Box, getName:Box, getUnderlyingType:Box | +| nominaltype.swift:102:6:102:6 | d1 | D1 | getFullName:D1, getName:D1, getUnderlyingType:D1 | +| nominaltype.swift:103:6:103:6 | d2 | D2 | getFullName:D2, getName:D2, getUnderlyingType:D2 | diff --git a/swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.swift b/swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.swift index b674e13cd71..0bc56d9c5da 100644 --- a/swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.swift +++ b/swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.swift @@ -29,10 +29,6 @@ class C1 : P { class C2 : P_alias { } -typealias C1_alias = C1 - -typealias C2_alias = C2 - // --- class Outer { @@ -79,7 +75,7 @@ protocol P4 { typealias P4_alias = P4 -extension D2_alias : P4_alias { +extension D2 : P4_alias { } // --- @@ -98,13 +94,11 @@ func test() { var p_alias : P_alias var c1 : C1 var c2 : C2 - var c1_alias : C1_alias - var c2_alias : C2_alias var o : Outer var oi : Outer.Inner var oia : Outer.Inner.InnerAlias var p1p2 : P1P2 var boxInt : Box var d1: D1 - var d2: D2_alias + var d2: D2 } diff --git a/swift/ql/test/library-tests/elements/type/nominaltype/nominaltypedecl.expected b/swift/ql/test/library-tests/elements/type/nominaltype/nominaltypedecl.expected index f5039a03cb8..9a49fe5c67f 100644 --- a/swift/ql/test/library-tests/elements/type/nominaltype/nominaltypedecl.expected +++ b/swift/ql/test/library-tests/elements/type/nominaltype/nominaltypedecl.expected @@ -1,17 +1,15 @@ -| nominaltype.swift:88:6:88:6 | i | Int | getABaseType:FixedWidthInteger, getABaseType:SignedInteger, getABaseType:_ExpressibleByBuiltinIntegerLiteral, getABaseTypeDecl:FixedWidthInteger, getABaseTypeDecl:SignedInteger, getABaseTypeDecl:_ExpressibleByBuiltinIntegerLiteral, getFullName:Int, getName:Int | -| nominaltype.swift:90:6:90:6 | a | A | getFullName:A, getName:A | -| nominaltype.swift:91:6:91:6 | a_alias | A_alias | getAliasedType:A, getFullName:A_alias, getName:A_alias | -| nominaltype.swift:92:6:92:6 | a_optional_alias | A_optional_alias | getAliasedType:A?, getFullName:A_optional_alias, getName:A_optional_alias | -| nominaltype.swift:93:6:93:6 | b1 | B1 | getABaseType:A, getABaseTypeDecl:A, getFullName:B1, getName:B1 | -| nominaltype.swift:94:6:94:6 | b2 | B2 | getABaseType:A_alias, getFullName:B2, getName:B2 | -| nominaltype.swift:95:6:95:6 | b1_alias | B1_alias | getAliasedType:B1, getFullName:B1_alias, getName:B1_alias | -| nominaltype.swift:96:6:96:6 | b2_alias | B2_alias | getAliasedType:B2, getFullName:B2_alias, getName:B2_alias | -| nominaltype.swift:99:6:99:6 | c1 | C1 | getABaseType:P, getABaseTypeDecl:P, getFullName:C1, getName:C1 | -| nominaltype.swift:100:6:100:6 | c2 | C2 | getABaseType:P_alias, getFullName:C2, getName:C2 | -| nominaltype.swift:101:6:101:6 | c1_alias | C1_alias | getAliasedType:C1, getFullName:C1_alias, getName:C1_alias | -| nominaltype.swift:102:6:102:6 | c2_alias | C2_alias | getAliasedType:C2, getFullName:C2_alias, getName:C2_alias | -| nominaltype.swift:103:6:103:6 | o | Outer | getFullName:Outer, getName:Outer | -| nominaltype.swift:104:6:104:6 | oi | Inner | getFullName:Outer.Inner, getName:Inner | -| nominaltype.swift:105:6:105:6 | oia | InnerAlias | getAliasedType:A, getFullName:Outer.Inner.InnerAlias, getName:InnerAlias | -| nominaltype.swift:108:6:108:6 | d1 | D1 | getFullName:D1, getName:D1 | -| nominaltype.swift:109:6:109:6 | d2 | D2_alias | getAliasedType:D2, getFullName:D2_alias, getName:D2_alias | +| nominaltype.swift:84:6:84:6 | i | Int | getABaseType:FixedWidthInteger, getABaseType:SignedInteger, getABaseType:_ExpressibleByBuiltinIntegerLiteral, getABaseTypeDecl:FixedWidthInteger, getABaseTypeDecl:SignedInteger, getABaseTypeDecl:_ExpressibleByBuiltinIntegerLiteral, getFullName:Int, getName:Int | +| nominaltype.swift:86:6:86:6 | a | A | getFullName:A, getName:A | +| nominaltype.swift:87:6:87:6 | a_alias | A_alias | getAliasedType:A, getFullName:A_alias, getName:A_alias | +| nominaltype.swift:88:6:88:6 | a_optional_alias | A_optional_alias | getAliasedType:A?, getFullName:A_optional_alias, getName:A_optional_alias | +| nominaltype.swift:89:6:89:6 | b1 | B1 | getABaseType:A, getABaseTypeDecl:A, getFullName:B1, getName:B1 | +| nominaltype.swift:90:6:90:6 | b2 | B2 | getABaseType:A_alias, getFullName:B2, getName:B2 | +| nominaltype.swift:91:6:91:6 | b1_alias | B1_alias | getAliasedType:B1, getFullName:B1_alias, getName:B1_alias | +| nominaltype.swift:92:6:92:6 | b2_alias | B2_alias | getAliasedType:B2, getFullName:B2_alias, getName:B2_alias | +| nominaltype.swift:95:6:95:6 | c1 | C1 | getABaseType:P, getABaseTypeDecl:P, getFullName:C1, getName:C1 | +| nominaltype.swift:96:6:96:6 | c2 | C2 | getABaseType:P_alias, getFullName:C2, getName:C2 | +| nominaltype.swift:97:6:97:6 | o | Outer | getFullName:Outer, getName:Outer | +| nominaltype.swift:98:6:98:6 | oi | Inner | getFullName:Outer.Inner, getName:Inner | +| nominaltype.swift:99:6:99:6 | oia | InnerAlias | getAliasedType:A, getFullName:Outer.Inner.InnerAlias, getName:InnerAlias | +| nominaltype.swift:102:6:102:6 | d1 | D1 | getFullName:D1, getName:D1 | +| nominaltype.swift:103:6:103:6 | d2 | D2 | getFullName:D2, getName:D2 | From b048268042dd458ed4fc9f7c7441b60013b655ea Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Mon, 18 Sep 2023 18:49:26 +0100 Subject: [PATCH 03/10] Swift: Add some QLDoc. --- swift/ql/lib/codeql/swift/elements/decl/TypeAliasDecl.qll | 7 ++++++- swift/ql/lib/codeql/swift/elements/type/NominalType.qll | 3 +++ swift/ql/lib/codeql/swift/elements/type/TypeAliasType.qll | 6 ++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/swift/ql/lib/codeql/swift/elements/decl/TypeAliasDecl.qll b/swift/ql/lib/codeql/swift/elements/decl/TypeAliasDecl.qll index 015ee32349e..0b410002b2b 100644 --- a/swift/ql/lib/codeql/swift/elements/decl/TypeAliasDecl.qll +++ b/swift/ql/lib/codeql/swift/elements/decl/TypeAliasDecl.qll @@ -1,4 +1,9 @@ -// generated by codegen/codegen.py, remove this comment if you wish to edit this file private import codeql.swift.generated.decl.TypeAliasDecl +/** + * A declaration of a type alias to another type. For example: + * ``` + * typealias MyInt = Int + * ``` + */ class TypeAliasDecl extends Generated::TypeAliasDecl { } diff --git a/swift/ql/lib/codeql/swift/elements/type/NominalType.qll b/swift/ql/lib/codeql/swift/elements/type/NominalType.qll index 0e65cdc2f94..30775064bff 100644 --- a/swift/ql/lib/codeql/swift/elements/type/NominalType.qll +++ b/swift/ql/lib/codeql/swift/elements/type/NominalType.qll @@ -2,6 +2,9 @@ private import codeql.swift.generated.type.NominalType private import codeql.swift.elements.decl.NominalTypeDecl private import codeql.swift.elements.type.Type +/** + * A class, struct, enum or protocol. + */ class NominalType extends Generated::NominalType { override Type getABaseType() { result = this.getDeclaration().(NominalTypeDecl).getABaseType() } diff --git a/swift/ql/lib/codeql/swift/elements/type/TypeAliasType.qll b/swift/ql/lib/codeql/swift/elements/type/TypeAliasType.qll index 1d8e8fb1fd2..42d943d564e 100644 --- a/swift/ql/lib/codeql/swift/elements/type/TypeAliasType.qll +++ b/swift/ql/lib/codeql/swift/elements/type/TypeAliasType.qll @@ -1,6 +1,12 @@ private import codeql.swift.elements.type.Type private import codeql.swift.generated.type.TypeAliasType +/** + * A type alias to another type. For example: + * ``` + * typealias MyInt = Int + * ``` + */ class TypeAliasType extends Generated::TypeAliasType { /** * Gets the aliased type of this type alias type. From c78f5ce4cc275508ccac95e08170c40fafdd45a1 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Tue, 12 Sep 2023 14:55:44 +0100 Subject: [PATCH 04/10] Swift: Move NominalType.getABaseType() implementation out from NominalType (inconsistent, difficult to find) to Type. --- swift/ql/lib/codeql/swift/elements/type/NominalType.qll | 8 +------- swift/ql/lib/codeql/swift/elements/type/Type.qll | 5 ++++- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/swift/ql/lib/codeql/swift/elements/type/NominalType.qll b/swift/ql/lib/codeql/swift/elements/type/NominalType.qll index 30775064bff..bea26b68486 100644 --- a/swift/ql/lib/codeql/swift/elements/type/NominalType.qll +++ b/swift/ql/lib/codeql/swift/elements/type/NominalType.qll @@ -1,12 +1,6 @@ private import codeql.swift.generated.type.NominalType -private import codeql.swift.elements.decl.NominalTypeDecl -private import codeql.swift.elements.type.Type /** * A class, struct, enum or protocol. */ -class NominalType extends Generated::NominalType { - override Type getABaseType() { result = this.getDeclaration().(NominalTypeDecl).getABaseType() } - - NominalType getADerivedType() { result.getABaseType() = this } -} +class NominalType extends Generated::NominalType { } diff --git a/swift/ql/lib/codeql/swift/elements/type/Type.qll b/swift/ql/lib/codeql/swift/elements/type/Type.qll index 9572229584a..00a1b467063 100644 --- a/swift/ql/lib/codeql/swift/elements/type/Type.qll +++ b/swift/ql/lib/codeql/swift/elements/type/Type.qll @@ -1,4 +1,5 @@ private import codeql.swift.generated.type.Type +private import codeql.swift.elements.type.AnyGenericType /** * A Swift type. @@ -53,5 +54,7 @@ class Type extends Generated::Type { * typealias B_alias = B * ``` */ - Type getABaseType() { none() } + Type getABaseType() { result = this.(AnyGenericType).getDeclaration().getABaseType() } + + Type getADerivedType() { result.getABaseType() = this } } From f604b28ab5b4e962dc7f9351eb57011016cdb9e5 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Mon, 18 Sep 2023 19:32:29 +0100 Subject: [PATCH 05/10] Swift: Resolve type aliases (1) in base class declarations, not before them and (2) consistently. This is not behaviour preserving. --- .../codeql/swift/elements/decl/TypeDecl.qll | 28 +++++++++++++++---- .../lib/codeql/swift/elements/type/Type.qll | 20 ++++++++----- .../swift/elements/type/TypeAliasType.qll | 2 -- .../type/nominaltype/nominaltype.expected | 8 +++--- .../type/nominaltype/nominaltypedecl.expected | 4 +-- 5 files changed, 42 insertions(+), 20 deletions(-) diff --git a/swift/ql/lib/codeql/swift/elements/decl/TypeDecl.qll b/swift/ql/lib/codeql/swift/elements/decl/TypeDecl.qll index e89bbb921b4..898935c349f 100644 --- a/swift/ql/lib/codeql/swift/elements/decl/TypeDecl.qll +++ b/swift/ql/lib/codeql/swift/elements/decl/TypeDecl.qll @@ -35,11 +35,17 @@ class TypeDecl extends Generated::TypeDecl { deprecated Type getBaseType(int index) { result = this.getInheritedType(index) } /** - * Gets any of the base types of this type declaration. + * Gets any of the base types of this type declaration. Expands type aliases, for example + * in the following code, `B` has base type `A`. + * ``` + * typealias A_alias = A + * + * class B : A_alias {} + * ``` */ Type getABaseType() { - // TODO generalize this to resolve `TypeAliasDecl`s and consider bases added by extensions - result = this.getAnInheritedType() + // TODO generalize this to consider bases added by extensions + result = this.getAnInheritedType().getUnderlyingType() } /** @@ -51,7 +57,13 @@ class TypeDecl extends Generated::TypeDecl { } /** - * Gets the declaration of any of the base types of this type declaration. + * Gets the declaration of any of the base types of this type declaration. Expands type + * aliases, for example in the following code, `B` has base type decl `A`. + * ``` + * typealias A_alias = A + * + * class B : A_alias {} + * ``` */ TypeDecl getABaseTypeDecl() { result = this.getABaseType().(AnyGenericType).getDeclaration() } @@ -63,7 +75,13 @@ class TypeDecl extends Generated::TypeDecl { deprecated TypeDecl getDerivedTypeDecl(int i) { result.getBaseTypeDecl(i) = this } /** - * Gets the declaration of any type derived from this type declaration. + * Gets the declaration of any type derived from this type declaration. Expands type aliases, + * for example in the following code, `B` is derived from `A`. + * ``` + * typealias A_alias = A + * + * class B : A_alias {} + * ``` */ TypeDecl getADerivedTypeDecl() { result.getABaseTypeDecl() = this } diff --git a/swift/ql/lib/codeql/swift/elements/type/Type.qll b/swift/ql/lib/codeql/swift/elements/type/Type.qll index 00a1b467063..74e8d1fccc5 100644 --- a/swift/ql/lib/codeql/swift/elements/type/Type.qll +++ b/swift/ql/lib/codeql/swift/elements/type/Type.qll @@ -43,18 +43,24 @@ class Type extends Generated::Type { Type getUnderlyingType() { result = this } /** - * Gets any base type of this type. For a `typealias`, this is a base type - * of the aliased type. For example in the following code, both `B` and - * `B_alias` have base type `A`. + * Gets any base type of this type. Expands type aliases, for example in the following + * code, `B` has base type `A`. * ``` - * class A {} + * typealias A_alias = A * - * class B : A {} - * - * typealias B_alias = B + * class B : A_alias {} * ``` */ Type getABaseType() { result = this.(AnyGenericType).getDeclaration().getABaseType() } + /** + * Gets a type derived from this type. Expands type aliases, for example in the following + * code, `B` derives from type `A`. + * ``` + * typealias A_alias = A + * + * class B : A_alias {} + * ``` + */ Type getADerivedType() { result.getABaseType() = this } } diff --git a/swift/ql/lib/codeql/swift/elements/type/TypeAliasType.qll b/swift/ql/lib/codeql/swift/elements/type/TypeAliasType.qll index 42d943d564e..d9e9e4e2cbe 100644 --- a/swift/ql/lib/codeql/swift/elements/type/TypeAliasType.qll +++ b/swift/ql/lib/codeql/swift/elements/type/TypeAliasType.qll @@ -19,6 +19,4 @@ class TypeAliasType extends Generated::TypeAliasType { Type getAliasedType() { result = this.getDecl().getAliasedType() } override Type getUnderlyingType() { result = this.getAliasedType().getUnderlyingType() } - - override Type getABaseType() { result = this.getAliasedType().getABaseType() } } diff --git a/swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.expected b/swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.expected index 35b87914c18..d3a59d9663b 100644 --- a/swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.expected +++ b/swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.expected @@ -4,13 +4,13 @@ | nominaltype.swift:87:6:87:6 | a_alias | A_alias | getAliasedType:A, getFullName:A_alias, getName:A_alias, getUnderlyingType:A | | nominaltype.swift:88:6:88:6 | a_optional_alias | A_optional_alias | getAliasedType:A?, getFullName:A_optional_alias, getName:A_optional_alias, getUnderlyingType:A? | | nominaltype.swift:89:6:89:6 | b1 | B1 | getABaseType:A, getFullName:B1, getName:B1, getUnderlyingType:B1 | -| nominaltype.swift:90:6:90:6 | b2 | B2 | getABaseType:A_alias, getFullName:B2, getName:B2, getUnderlyingType:B2 | -| nominaltype.swift:91:6:91:6 | b1_alias | B1_alias | getABaseType:A, getAliasedType:B1, getFullName:B1_alias, getName:B1_alias, getUnderlyingType:B1 | -| nominaltype.swift:92:6:92:6 | b2_alias | B2_alias | getABaseType:A_alias, getAliasedType:B2, getFullName:B2_alias, getName:B2_alias, getUnderlyingType:B2 | +| nominaltype.swift:90:6:90:6 | b2 | B2 | getABaseType:A, getFullName:B2, getName:B2, getUnderlyingType:B2 | +| nominaltype.swift:91:6:91:6 | b1_alias | B1_alias | getAliasedType:B1, getFullName:B1_alias, getName:B1_alias, getUnderlyingType:B1 | +| nominaltype.swift:92:6:92:6 | b2_alias | B2_alias | getAliasedType:B2, getFullName:B2_alias, getName:B2_alias, getUnderlyingType:B2 | | nominaltype.swift:93:6:93:6 | p | P | getFullName:P, getName:P, getUnderlyingType:P | | nominaltype.swift:94:6:94:6 | p_alias | P_alias | getFullName:P_alias, getName:P_alias, getUnderlyingType:P_alias | | nominaltype.swift:95:6:95:6 | c1 | C1 | getABaseType:P, getFullName:C1, getName:C1, getUnderlyingType:C1 | -| nominaltype.swift:96:6:96:6 | c2 | C2 | getABaseType:P_alias, getFullName:C2, getName:C2, getUnderlyingType:C2 | +| nominaltype.swift:96:6:96:6 | c2 | C2 | getABaseType:P, getFullName:C2, getName:C2, getUnderlyingType:C2 | | nominaltype.swift:97:6:97:6 | o | Outer | getFullName:Outer, getName:Outer, getUnderlyingType:Outer | | nominaltype.swift:98:6:98:6 | oi | Outer.Inner | getFullName:Outer.Inner, getName:Inner, getUnderlyingType:Outer.Inner | | nominaltype.swift:99:6:99:6 | oia | Outer.Inner.InnerAlias | getAliasedType:A, getFullName:Outer.Inner.InnerAlias, getName:InnerAlias, getUnderlyingType:A | diff --git a/swift/ql/test/library-tests/elements/type/nominaltype/nominaltypedecl.expected b/swift/ql/test/library-tests/elements/type/nominaltype/nominaltypedecl.expected index 9a49fe5c67f..dbee369405c 100644 --- a/swift/ql/test/library-tests/elements/type/nominaltype/nominaltypedecl.expected +++ b/swift/ql/test/library-tests/elements/type/nominaltype/nominaltypedecl.expected @@ -3,11 +3,11 @@ | nominaltype.swift:87:6:87:6 | a_alias | A_alias | getAliasedType:A, getFullName:A_alias, getName:A_alias | | nominaltype.swift:88:6:88:6 | a_optional_alias | A_optional_alias | getAliasedType:A?, getFullName:A_optional_alias, getName:A_optional_alias | | nominaltype.swift:89:6:89:6 | b1 | B1 | getABaseType:A, getABaseTypeDecl:A, getFullName:B1, getName:B1 | -| nominaltype.swift:90:6:90:6 | b2 | B2 | getABaseType:A_alias, getFullName:B2, getName:B2 | +| nominaltype.swift:90:6:90:6 | b2 | B2 | getABaseType:A, getABaseTypeDecl:A, getFullName:B2, getName:B2 | | nominaltype.swift:91:6:91:6 | b1_alias | B1_alias | getAliasedType:B1, getFullName:B1_alias, getName:B1_alias | | nominaltype.swift:92:6:92:6 | b2_alias | B2_alias | getAliasedType:B2, getFullName:B2_alias, getName:B2_alias | | nominaltype.swift:95:6:95:6 | c1 | C1 | getABaseType:P, getABaseTypeDecl:P, getFullName:C1, getName:C1 | -| nominaltype.swift:96:6:96:6 | c2 | C2 | getABaseType:P_alias, getFullName:C2, getName:C2 | +| nominaltype.swift:96:6:96:6 | c2 | C2 | getABaseType:P, getABaseTypeDecl:P, getFullName:C2, getName:C2 | | nominaltype.swift:97:6:97:6 | o | Outer | getFullName:Outer, getName:Outer | | nominaltype.swift:98:6:98:6 | oi | Inner | getFullName:Outer.Inner, getName:Inner | | nominaltype.swift:99:6:99:6 | oia | InnerAlias | getAliasedType:A, getFullName:Outer.Inner.InnerAlias, getName:InnerAlias | From 37edcad1b0dd83a824df361ab23791c7fe5f811b Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Mon, 18 Sep 2023 19:52:27 +0100 Subject: [PATCH 06/10] Swift: Go through protocols added in extensions also. --- .../codeql/swift/elements/decl/TypeDecl.qll | 23 +++++++++++++------ .../lib/codeql/swift/elements/type/Type.qll | 4 ++-- .../type/nominaltype/nominaltype.expected | 6 ++--- .../type/nominaltype/nominaltypedecl.expected | 6 ++--- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/swift/ql/lib/codeql/swift/elements/decl/TypeDecl.qll b/swift/ql/lib/codeql/swift/elements/decl/TypeDecl.qll index 898935c349f..e75e2ff9b40 100644 --- a/swift/ql/lib/codeql/swift/elements/decl/TypeDecl.qll +++ b/swift/ql/lib/codeql/swift/elements/decl/TypeDecl.qll @@ -35,8 +35,9 @@ class TypeDecl extends Generated::TypeDecl { deprecated Type getBaseType(int index) { result = this.getInheritedType(index) } /** - * Gets any of the base types of this type declaration. Expands type aliases, for example - * in the following code, `B` has base type `A`. + * Gets any of the base types of this type declaration. Expands protocols added in + * extensions and expands type aliases. For example in the following code, `B` has + * base type `A`: * ``` * typealias A_alias = A * @@ -44,8 +45,14 @@ class TypeDecl extends Generated::TypeDecl { * ``` */ Type getABaseType() { - // TODO generalize this to consider bases added by extensions + // direct base type result = this.getAnInheritedType().getUnderlyingType() + or + // protocol added in an extension of the type + exists(ExtensionDecl ed | + ed.getExtendedTypeDecl() = this and + ed.getAProtocol().getType() = result + ) } /** @@ -57,8 +64,9 @@ class TypeDecl extends Generated::TypeDecl { } /** - * Gets the declaration of any of the base types of this type declaration. Expands type - * aliases, for example in the following code, `B` has base type decl `A`. + * Gets the declaration of any of the base types of this type declaration. Expands + * protocols added in extensions and expands type aliases. For example in the following + * code, `B` has base type `A`. * ``` * typealias A_alias = A * @@ -75,8 +83,9 @@ class TypeDecl extends Generated::TypeDecl { deprecated TypeDecl getDerivedTypeDecl(int i) { result.getBaseTypeDecl(i) = this } /** - * Gets the declaration of any type derived from this type declaration. Expands type aliases, - * for example in the following code, `B` is derived from `A`. + * Gets the declaration of any type derived from this type declaration. Expands protocols + * added in extensions and expands type aliases. For example in the following code, `B` + * is derived from `A`. * ``` * typealias A_alias = A * diff --git a/swift/ql/lib/codeql/swift/elements/type/Type.qll b/swift/ql/lib/codeql/swift/elements/type/Type.qll index 74e8d1fccc5..b5d30140e83 100644 --- a/swift/ql/lib/codeql/swift/elements/type/Type.qll +++ b/swift/ql/lib/codeql/swift/elements/type/Type.qll @@ -43,8 +43,8 @@ class Type extends Generated::Type { Type getUnderlyingType() { result = this } /** - * Gets any base type of this type. Expands type aliases, for example in the following - * code, `B` has base type `A`. + * Gets any base type of this type. Expands protocols added in extensions and expands + * type aliases. For example in the following code, `B` has base type `A`: * ``` * typealias A_alias = A * diff --git a/swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.expected b/swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.expected index d3a59d9663b..4df2f5dcfdc 100644 --- a/swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.expected +++ b/swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.expected @@ -1,4 +1,4 @@ -| nominaltype.swift:84:6:84:6 | i | Int | getABaseType:FixedWidthInteger, getABaseType:SignedInteger, getABaseType:_ExpressibleByBuiltinIntegerLiteral, getFullName:Int, getName:Int, getUnderlyingType:Int | +| nominaltype.swift:84:6:84:6 | i | Int | getABaseType:CVarArg, getABaseType:CodingKeyRepresentable, getABaseType:CustomReflectable, getABaseType:Decodable, getABaseType:Encodable, getABaseType:Equatable, getABaseType:FixedWidthInteger, getABaseType:Hashable, getABaseType:MirrorPath, getABaseType:SIMDScalar, getABaseType:Sendable, getABaseType:SignedInteger, getABaseType:_CustomPlaygroundQuickLookable, getABaseType:_ExpressibleByBuiltinIntegerLiteral, getABaseType:_HasCustomAnyHashableRepresentation, getFullName:Int, getName:Int, getUnderlyingType:Int | | nominaltype.swift:85:6:85:6 | j | Any? | getFullName:Any?, getName:Any?, getUnderlyingType:Any? | | nominaltype.swift:86:6:86:6 | a | A | getFullName:A, getName:A, getUnderlyingType:A | | nominaltype.swift:87:6:87:6 | a_alias | A_alias | getAliasedType:A, getFullName:A_alias, getName:A_alias, getUnderlyingType:A | @@ -16,5 +16,5 @@ | nominaltype.swift:99:6:99:6 | oia | Outer.Inner.InnerAlias | getAliasedType:A, getFullName:Outer.Inner.InnerAlias, getName:InnerAlias, getUnderlyingType:A | | nominaltype.swift:100:6:100:6 | p1p2 | P1P2 | getFullName:P1P2, getName:P1P2, getUnderlyingType:P1P2 | | nominaltype.swift:101:6:101:6 | boxInt | Box | getFullName:Box, getName:Box, getUnderlyingType:Box | -| nominaltype.swift:102:6:102:6 | d1 | D1 | getFullName:D1, getName:D1, getUnderlyingType:D1 | -| nominaltype.swift:103:6:103:6 | d2 | D2 | getFullName:D2, getName:D2, getUnderlyingType:D2 | +| nominaltype.swift:102:6:102:6 | d1 | D1 | getABaseType:P3, getFullName:D1, getName:D1, getUnderlyingType:D1 | +| nominaltype.swift:103:6:103:6 | d2 | D2 | getABaseType:P4, getFullName:D2, getName:D2, getUnderlyingType:D2 | diff --git a/swift/ql/test/library-tests/elements/type/nominaltype/nominaltypedecl.expected b/swift/ql/test/library-tests/elements/type/nominaltype/nominaltypedecl.expected index dbee369405c..e4bdf96ab7f 100644 --- a/swift/ql/test/library-tests/elements/type/nominaltype/nominaltypedecl.expected +++ b/swift/ql/test/library-tests/elements/type/nominaltype/nominaltypedecl.expected @@ -1,4 +1,4 @@ -| nominaltype.swift:84:6:84:6 | i | Int | getABaseType:FixedWidthInteger, getABaseType:SignedInteger, getABaseType:_ExpressibleByBuiltinIntegerLiteral, getABaseTypeDecl:FixedWidthInteger, getABaseTypeDecl:SignedInteger, getABaseTypeDecl:_ExpressibleByBuiltinIntegerLiteral, getFullName:Int, getName:Int | +| nominaltype.swift:84:6:84:6 | i | Int | getABaseType:CVarArg, getABaseType:CodingKeyRepresentable, getABaseType:CustomReflectable, getABaseType:Decodable, getABaseType:Encodable, getABaseType:Equatable, getABaseType:FixedWidthInteger, getABaseType:Hashable, getABaseType:MirrorPath, getABaseType:SIMDScalar, getABaseType:Sendable, getABaseType:SignedInteger, getABaseType:_CustomPlaygroundQuickLookable, getABaseType:_ExpressibleByBuiltinIntegerLiteral, getABaseType:_HasCustomAnyHashableRepresentation, getABaseTypeDecl:CVarArg, getABaseTypeDecl:CodingKeyRepresentable, getABaseTypeDecl:CustomReflectable, getABaseTypeDecl:Decodable, getABaseTypeDecl:Encodable, getABaseTypeDecl:Equatable, getABaseTypeDecl:FixedWidthInteger, getABaseTypeDecl:Hashable, getABaseTypeDecl:MirrorPath, getABaseTypeDecl:SIMDScalar, getABaseTypeDecl:Sendable, getABaseTypeDecl:SignedInteger, getABaseTypeDecl:_CustomPlaygroundQuickLookable, getABaseTypeDecl:_ExpressibleByBuiltinIntegerLiteral, getABaseTypeDecl:_HasCustomAnyHashableRepresentation, getFullName:Int, getName:Int | | nominaltype.swift:86:6:86:6 | a | A | getFullName:A, getName:A | | nominaltype.swift:87:6:87:6 | a_alias | A_alias | getAliasedType:A, getFullName:A_alias, getName:A_alias | | nominaltype.swift:88:6:88:6 | a_optional_alias | A_optional_alias | getAliasedType:A?, getFullName:A_optional_alias, getName:A_optional_alias | @@ -11,5 +11,5 @@ | nominaltype.swift:97:6:97:6 | o | Outer | getFullName:Outer, getName:Outer | | nominaltype.swift:98:6:98:6 | oi | Inner | getFullName:Outer.Inner, getName:Inner | | nominaltype.swift:99:6:99:6 | oia | InnerAlias | getAliasedType:A, getFullName:Outer.Inner.InnerAlias, getName:InnerAlias | -| nominaltype.swift:102:6:102:6 | d1 | D1 | getFullName:D1, getName:D1 | -| nominaltype.swift:103:6:103:6 | d2 | D2 | getFullName:D2, getName:D2 | +| nominaltype.swift:102:6:102:6 | d1 | D1 | getABaseType:P3, getABaseTypeDecl:P3, getFullName:D1, getName:D1 | +| nominaltype.swift:103:6:103:6 | d2 | D2 | getABaseType:P4, getABaseTypeDecl:P4, getFullName:D2, getName:D2 | From df29f3974bbb522e830d8e9828aed51c543b60ee Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Mon, 18 Sep 2023 20:50:47 +0100 Subject: [PATCH 07/10] Swift: Update uses of getABaseType() and similar to do things correctly now. --- .../codeql/swift/dataflow/internal/TaintTrackingPublic.qll | 2 +- .../swift/security/CleartextStorageDatabaseExtensions.qll | 4 ++-- .../codeql/swift/security/CleartextStorageDatabaseQuery.qll | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/swift/ql/lib/codeql/swift/dataflow/internal/TaintTrackingPublic.qll b/swift/ql/lib/codeql/swift/dataflow/internal/TaintTrackingPublic.qll index f2faec07be9..b6170db1b2a 100644 --- a/swift/ql/lib/codeql/swift/dataflow/internal/TaintTrackingPublic.qll +++ b/swift/ql/lib/codeql/swift/dataflow/internal/TaintTrackingPublic.qll @@ -31,7 +31,7 @@ predicate defaultImplicitTaintRead(DataFlow::Node node, DataFlow::ContentSet cs) // So when the node is a `PostUpdateNode` we allow any sequence of implicit read steps of an appropriate // type to make sure we arrive at the sink with an empty access path. exists(NominalTypeDecl d, Decl cx | - node.(DataFlow::PostUpdateNode).getPreUpdateNode().asExpr().getType() = + node.(DataFlow::PostUpdateNode).getPreUpdateNode().asExpr().getType().getUnderlyingType() = d.getType().getABaseType*() and cx.asNominalTypeDecl() = d and cs.getAReadContent().(DataFlow::Content::FieldContent).getField() = cx.getAMember() diff --git a/swift/ql/lib/codeql/swift/security/CleartextStorageDatabaseExtensions.qll b/swift/ql/lib/codeql/swift/security/CleartextStorageDatabaseExtensions.qll index cb7e8c53ec8..b8d20b3a56b 100644 --- a/swift/ql/lib/codeql/swift/security/CleartextStorageDatabaseExtensions.qll +++ b/swift/ql/lib/codeql/swift/security/CleartextStorageDatabaseExtensions.qll @@ -49,7 +49,7 @@ private class CoreDataStore extends CleartextStorageDatabaseSink { // with `coreDataObj.data` is a sink. // (ideally this would be only members with the `@NSManaged` attribute) exists(NominalType t, Expr e | - t.getABaseType*().getUnderlyingType().getName() = "NSManagedObject" and + t.getUnderlyingType().getABaseType*().getName() = "NSManagedObject" and this.(DataFlow::PostUpdateNode).getPreUpdateNode().asExpr() = e and e.getFullyConverted().getType() = t and not e.(DeclRefExpr).getDecl() instanceof SelfParamDecl @@ -67,7 +67,7 @@ private class RealmStore extends CleartextStorageDatabaseSink instanceof DataFlo // example in `realmObj.data = sensitive` the post-update node corresponding // with `realmObj.data` is a sink. exists(NominalType t, Expr e | - t.getABaseType*().getUnderlyingType().getName() = "RealmSwiftObject" and + t.getUnderlyingType().getABaseType*().getName() = "RealmSwiftObject" and this.getPreUpdateNode().asExpr() = e and e.getFullyConverted().getType() = t and not e.(DeclRefExpr).getDecl() instanceof SelfParamDecl diff --git a/swift/ql/lib/codeql/swift/security/CleartextStorageDatabaseQuery.qll b/swift/ql/lib/codeql/swift/security/CleartextStorageDatabaseQuery.qll index fc8430ddc2e..9432887db9d 100644 --- a/swift/ql/lib/codeql/swift/security/CleartextStorageDatabaseQuery.qll +++ b/swift/ql/lib/codeql/swift/security/CleartextStorageDatabaseQuery.qll @@ -34,7 +34,7 @@ module CleartextStorageDatabaseConfig implements DataFlow::ConfigSig { // for example in `realmObj.data = sensitive`. isSink(node) and exists(NominalTypeDecl d, Decl cx | - d.getType().getABaseType*().getUnderlyingType().getName() = + d.getType().getUnderlyingType().getABaseType*().getName() = ["NSManagedObject", "RealmSwiftObject"] and cx.asNominalTypeDecl() = d and c.getAReadContent().(DataFlow::Content::FieldContent).getField() = cx.getAMember() From bcf990b362caa2e9367cdb0b52bea1dca7c9da14 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Mon, 18 Sep 2023 22:04:39 +0100 Subject: [PATCH 08/10] Swift: Add change note. --- swift/ql/lib/change-notes/2023-09-18-get-a-base-type.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 swift/ql/lib/change-notes/2023-09-18-get-a-base-type.md diff --git a/swift/ql/lib/change-notes/2023-09-18-get-a-base-type.md b/swift/ql/lib/change-notes/2023-09-18-get-a-base-type.md new file mode 100644 index 00000000000..482b8346b64 --- /dev/null +++ b/swift/ql/lib/change-notes/2023-09-18-get-a-base-type.md @@ -0,0 +1,9 @@ +--- +category: majorAnalysis +--- + +* The predicates `getABaseType`, `getABaseTypeDecl`, `getADerivedType` and `getADerivedTypeDecl` on `Type` and `TypeDecl` now behave more usefully and consistently. They now explore through type aliases used in base class declarations, and include protocols added in extensions. + +To examine base class declarations at a low level without these enhancements, use `TypeDecl.getInheritedType`. + +`Type.getABaseType` (only) previously resolved a type alias it was called directly on. This behaviour no longer exists. To find any base type of a type that could be an alias, the construct `Type.getUnderlyingType().getABaseType*()` is recommended. From 34240f7328efa72d7ca0ae0e9c38375a75731479 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Tue, 19 Sep 2023 10:08:08 +0100 Subject: [PATCH 09/10] Swift: Accept generated changes. --- swift/ql/.generated.list | 1 - swift/ql/.gitattributes | 1 - 2 files changed, 2 deletions(-) diff --git a/swift/ql/.generated.list b/swift/ql/.generated.list index 1510c1dae29..a9256547e83 100644 --- a/swift/ql/.generated.list +++ b/swift/ql/.generated.list @@ -57,7 +57,6 @@ lib/codeql/swift/elements/decl/StructDecl.qll 708711bf4236f32174caa256f3b19e00b6 lib/codeql/swift/elements/decl/StructDeclConstructor.qll 653fef1ce7a5924f9db110dfab4ebc191b6688fa14ebeb6cf2a09fe338f00646 c7ed15002c41b7dd11a5dd768e0f6f1fe241c680d155364404c64d6251adee5c lib/codeql/swift/elements/decl/SubscriptDeclConstructor.qll 3a88617b41f96827cb6edd596d6d95ebcf5baf99ba113bdd298276666c6aeadf 166e04fc72507cb27e2c16ad2d5217074f8678d286cb6d0980e5b84125648abe lib/codeql/swift/elements/decl/TopLevelCodeDeclConstructor.qll 6920a4e7aec45ae2a561cef95b9082b861f81c16c259698541f317481645e194 4bd65820b93a5ec7332dd1bbf59326fc19b77e94c122ad65d41393c84e6ac581 -lib/codeql/swift/elements/decl/TypeAliasDecl.qll 984c5802c35e595388f7652cef1a50fb963b32342ab4f9d813b7200a0e6a37ca 630dc9cbf20603855c599a9f86037ba0d889ad3d2c2b6f9ac17508d398bff9e3 lib/codeql/swift/elements/decl/TypeAliasDeclConstructor.qll ba70bb69b3a14283def254cc1859c29963838f624b3f1062a200a8df38f1edd5 96ac51d1b3156d4139e583f7f803e9eb95fe25cc61c12986e1b2972a781f9c8b lib/codeql/swift/elements/expr/AbiSafeConversionExpr.qll 39b856c89b8aff769b75051fd9e319f2d064c602733eaa6fed90d8f626516306 a87738539276438cef63145461adf25309d1938cfac367f53f53d33db9b12844 lib/codeql/swift/elements/expr/AbiSafeConversionExprConstructor.qll 7d70e7c47a9919efcb1ebcbf70e69cab1be30dd006297b75f6d72b25ae75502a e7a741c42401963f0c1da414b3ae779adeba091e9b8f56c9abf2a686e3a04d52 diff --git a/swift/ql/.gitattributes b/swift/ql/.gitattributes index 339595afe10..a487b874c15 100644 --- a/swift/ql/.gitattributes +++ b/swift/ql/.gitattributes @@ -59,7 +59,6 @@ /lib/codeql/swift/elements/decl/StructDeclConstructor.qll linguist-generated /lib/codeql/swift/elements/decl/SubscriptDeclConstructor.qll linguist-generated /lib/codeql/swift/elements/decl/TopLevelCodeDeclConstructor.qll linguist-generated -/lib/codeql/swift/elements/decl/TypeAliasDecl.qll linguist-generated /lib/codeql/swift/elements/decl/TypeAliasDeclConstructor.qll linguist-generated /lib/codeql/swift/elements/expr/AbiSafeConversionExpr.qll linguist-generated /lib/codeql/swift/elements/expr/AbiSafeConversionExprConstructor.qll linguist-generated From fef5a49fcba99cfbfb5075a756eba990b5375971 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Wed, 20 Sep 2023 15:36:15 +0100 Subject: [PATCH 10/10] Swift: Remove now duplicate extension logic. --- .../lib/codeql/swift/dataflow/ExternalFlow.qll | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/swift/ql/lib/codeql/swift/dataflow/ExternalFlow.qll b/swift/ql/lib/codeql/swift/dataflow/ExternalFlow.qll index 128883d125d..524e5f5720a 100644 --- a/swift/ql/lib/codeql/swift/dataflow/ExternalFlow.qll +++ b/swift/ql/lib/codeql/swift/dataflow/ExternalFlow.qll @@ -417,14 +417,6 @@ private Element interpretElement0( subtypes = true and declWithMethod.asNominalTypeDecl() = namedTypeDecl.getADerivedTypeDecl*() or - // member declared in a type that's extended with a protocol that is the named type - exists(ExtensionDecl e | - e.getExtendedTypeDecl().getADerivedTypeDecl*() = declWithMethod.asNominalTypeDecl() - | - subtypes = true and - e.getAProtocol() = namedTypeDecl.getADerivedTypeDecl*() - ) - or // member declared directly in the named type (or an extension of it) subtypes = false and declWithMethod.asNominalTypeDecl() = namedTypeDecl @@ -442,14 +434,6 @@ private Element interpretElement0( subtypes = true and declWithField.asNominalTypeDecl() = namedTypeDecl.getADerivedTypeDecl*() or - // field declared in a type that's extended with a protocol that is the named type - exists(ExtensionDecl e | - e.getExtendedTypeDecl().getADerivedTypeDecl*() = declWithField.asNominalTypeDecl() - | - subtypes = true and - e.getAProtocol() = namedTypeDecl.getADerivedTypeDecl*() - ) - or // field declared directly in the named type (or an extension of it) subtypes = false and declWithField.asNominalTypeDecl() = namedTypeDecl