Skip to content

Commit 37edcad

Browse files
committed
Swift: Go through protocols added in extensions also.
1 parent f604b28 commit 37edcad

File tree

4 files changed

+24
-15
lines changed

4 files changed

+24
-15
lines changed

swift/ql/lib/codeql/swift/elements/decl/TypeDecl.qll

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,24 @@ class TypeDecl extends Generated::TypeDecl {
3535
deprecated Type getBaseType(int index) { result = this.getInheritedType(index) }
3636

3737
/**
38-
* Gets any of the base types of this type declaration. Expands type aliases, for example
39-
* in the following code, `B` has base type `A`.
38+
* Gets any of the base types of this type declaration. Expands protocols added in
39+
* extensions and expands type aliases. For example in the following code, `B` has
40+
* base type `A`:
4041
* ```
4142
* typealias A_alias = A
4243
*
4344
* class B : A_alias {}
4445
* ```
4546
*/
4647
Type getABaseType() {
47-
// TODO generalize this to consider bases added by extensions
48+
// direct base type
4849
result = this.getAnInheritedType().getUnderlyingType()
50+
or
51+
// protocol added in an extension of the type
52+
exists(ExtensionDecl ed |
53+
ed.getExtendedTypeDecl() = this and
54+
ed.getAProtocol().getType() = result
55+
)
4956
}
5057

5158
/**
@@ -57,8 +64,9 @@ class TypeDecl extends Generated::TypeDecl {
5764
}
5865

5966
/**
60-
* Gets the declaration of any of the base types of this type declaration. Expands type
61-
* aliases, for example in the following code, `B` has base type decl `A`.
67+
* Gets the declaration of any of the base types of this type declaration. Expands
68+
* protocols added in extensions and expands type aliases. For example in the following
69+
* code, `B` has base type `A`.
6270
* ```
6371
* typealias A_alias = A
6472
*
@@ -75,8 +83,9 @@ class TypeDecl extends Generated::TypeDecl {
7583
deprecated TypeDecl getDerivedTypeDecl(int i) { result.getBaseTypeDecl(i) = this }
7684

7785
/**
78-
* Gets the declaration of any type derived from this type declaration. Expands type aliases,
79-
* for example in the following code, `B` is derived from `A`.
86+
* Gets the declaration of any type derived from this type declaration. Expands protocols
87+
* added in extensions and expands type aliases. For example in the following code, `B`
88+
* is derived from `A`.
8089
* ```
8190
* typealias A_alias = A
8291
*

swift/ql/lib/codeql/swift/elements/type/Type.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ class Type extends Generated::Type {
4343
Type getUnderlyingType() { result = this }
4444

4545
/**
46-
* Gets any base type of this type. Expands type aliases, for example in the following
47-
* code, `B` has base type `A`.
46+
* Gets any base type of this type. Expands protocols added in extensions and expands
47+
* type aliases. For example in the following code, `B` has base type `A`:
4848
* ```
4949
* typealias A_alias = A
5050
*

swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
| nominaltype.swift:84:6:84:6 | i | Int | getABaseType:FixedWidthInteger, getABaseType:SignedInteger, getABaseType:_ExpressibleByBuiltinIntegerLiteral, getFullName:Int, getName:Int, getUnderlyingType:Int |
1+
| 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 |
22
| nominaltype.swift:85:6:85:6 | j | Any? | getFullName:Any?, getName:Any?, getUnderlyingType:Any? |
33
| nominaltype.swift:86:6:86:6 | a | A | getFullName:A, getName:A, getUnderlyingType:A |
44
| nominaltype.swift:87:6:87:6 | a_alias | A_alias | getAliasedType:A, getFullName:A_alias, getName:A_alias, getUnderlyingType:A |
@@ -16,5 +16,5 @@
1616
| nominaltype.swift:99:6:99:6 | oia | Outer.Inner.InnerAlias | getAliasedType:A, getFullName:Outer.Inner.InnerAlias, getName:InnerAlias, getUnderlyingType:A |
1717
| nominaltype.swift:100:6:100:6 | p1p2 | P1P2 | getFullName:P1P2, getName:P1P2, getUnderlyingType:P1P2 |
1818
| nominaltype.swift:101:6:101:6 | boxInt | Box<A> | getFullName:Box<A>, getName:Box<A>, getUnderlyingType:Box<A> |
19-
| nominaltype.swift:102:6:102:6 | d1 | D1 | getFullName:D1, getName:D1, getUnderlyingType:D1 |
20-
| nominaltype.swift:103:6:103:6 | d2 | D2 | getFullName:D2, getName:D2, getUnderlyingType:D2 |
19+
| nominaltype.swift:102:6:102:6 | d1 | D1 | getABaseType:P3, getFullName:D1, getName:D1, getUnderlyingType:D1 |
20+
| nominaltype.swift:103:6:103:6 | d2 | D2 | getABaseType:P4, getFullName:D2, getName:D2, getUnderlyingType:D2 |

swift/ql/test/library-tests/elements/type/nominaltype/nominaltypedecl.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
| nominaltype.swift:84:6:84:6 | i | Int | getABaseType:FixedWidthInteger, getABaseType:SignedInteger, getABaseType:_ExpressibleByBuiltinIntegerLiteral, getABaseTypeDecl:FixedWidthInteger, getABaseTypeDecl:SignedInteger, getABaseTypeDecl:_ExpressibleByBuiltinIntegerLiteral, getFullName:Int, getName:Int |
1+
| 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 |
22
| nominaltype.swift:86:6:86:6 | a | A | getFullName:A, getName:A |
33
| nominaltype.swift:87:6:87:6 | a_alias | A_alias | getAliasedType:A, getFullName:A_alias, getName:A_alias |
44
| 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 @@
1111
| nominaltype.swift:97:6:97:6 | o | Outer | getFullName:Outer, getName:Outer |
1212
| nominaltype.swift:98:6:98:6 | oi | Inner | getFullName:Outer.Inner, getName:Inner |
1313
| nominaltype.swift:99:6:99:6 | oia | InnerAlias | getAliasedType:A, getFullName:Outer.Inner.InnerAlias, getName:InnerAlias |
14-
| nominaltype.swift:102:6:102:6 | d1 | D1 | getFullName:D1, getName:D1 |
15-
| nominaltype.swift:103:6:103:6 | d2 | D2 | getFullName:D2, getName:D2 |
14+
| nominaltype.swift:102:6:102:6 | d1 | D1 | getABaseType:P3, getABaseTypeDecl:P3, getFullName:D1, getName:D1 |
15+
| nominaltype.swift:103:6:103:6 | d2 | D2 | getABaseType:P4, getABaseTypeDecl:P4, getFullName:D2, getName:D2 |

0 commit comments

Comments
 (0)