Skip to content

Commit e4837f7

Browse files
authored
Merge pull request github#12489 from geoffw0/typealiastests
Swift: Skeleton + tests for type alias support
2 parents 5461f94 + 0d1be22 commit e4837f7

File tree

13 files changed

+430
-273
lines changed

13 files changed

+430
-273
lines changed

swift/ql/.generated.list

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,6 @@ ql/lib/codeql/swift/elements/type/SugarType.qll 0833a0f1bd26b066817f55df7a58243d
352352
ql/lib/codeql/swift/elements/type/SyntaxSugarType.qll 699fe9b4805494b62416dc86098342a725020f59a649138e6f5ba405dd536db5 a7a002cf597c3e3d0fda67111116c61a80f1e66ab8db8ddb3e189c6f15cadda6
353353
ql/lib/codeql/swift/elements/type/TupleType.qll 1dc14882028be534d15e348fba318c0bb1b52e692ca833987e00c9a66a1921ad 0b34c17ce9db336d0be9a869da988f31f10f754d6ffab6fa88791e508044edd2
354354
ql/lib/codeql/swift/elements/type/TupleTypeConstructor.qll 060633b22ee9884cb98103b380963fac62a02799461d342372cfb9cc6303d693 c9a89f695c85e7e22947287bcc32909b1f701168fd89c3598a45c97909e879f4
355-
ql/lib/codeql/swift/elements/type/TypeAliasType.qll 8149cc01f6d47ab10c0e20f1892af5e5d778e11a76be5f4752ad349a4c5b4fa4 760c79b9b581cc645f1002ca892656d406d3d339267fd58e765738257dbb45ce
356355
ql/lib/codeql/swift/elements/type/TypeAliasTypeConstructor.qll f63ada921beb95d5f3484ab072aa4412e93adfc8e7c0b1637273f99356f5cb13 f90d2789f7c922bc8254a0d131e36b40db1e00f9b32518633520d5c3341cd70a
357356
ql/lib/codeql/swift/elements/type/TypeReprConstructor.qll 2bb9c5ece40c6caed9c3a614affc0efd47ad2309c09392800ad346bf369969bf 30429adc135eb8fc476bc9bc185cff0a4119ddc0e618368c44f4a43246b5287f
358357
ql/lib/codeql/swift/elements/type/UnarySyntaxSugarType.qll 712c7e75b8169a80a44a6609da7f5a39cc4f36773eb520c8824ea09295c6929c 0b113b1a7834779fabfa046a64c6d256cde0f510edb84da253e89d36f41f8241

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,13 @@ private import codeql.swift.generated.type.Type
22

33
class Type extends Generated::Type {
44
override string toString() { result = this.getName() }
5+
6+
/**
7+
* Gets this type after any type aliases have been resolved. For example in
8+
* the following code, the underlying type of `MyInt` is `Int`:
9+
* ```
10+
* typealias MyInt = Int
11+
* ```
12+
*/
13+
Type getUnderlyingType() { result = this }
514
}
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
// generated by codegen/codegen.py, remove this comment if you wish to edit this file
1+
private import codeql.swift.elements.type.Type
22
private import codeql.swift.generated.type.TypeAliasType
33

4-
class TypeAliasType extends Generated::TypeAliasType { }
4+
class TypeAliasType extends Generated::TypeAliasType {
5+
/**
6+
* Gets the aliased type of this type alias type.
7+
*
8+
* For example the aliased type of `MyInt` in the following code is `Int`:
9+
* ```
10+
* typealias MyInt = Int
11+
* ```
12+
*/
13+
Type getAliasedType() { none() } // TODO: not yet implemented.
14+
15+
override Type getUnderlyingType() { result = this } // TODO: not yet implemented.
16+
}

swift/ql/test/extractor-tests/declarations/all.expected

Lines changed: 276 additions & 269 deletions
Large diffs are not rendered by default.
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import swift
22

3+
string describe(Decl decl) {
4+
//result = "getAliasedType:" + decl.(TypeAliasDecl).getAliasedType().toString() TODO: not yet implemented
5+
none()
6+
}
7+
38
from Decl decl
49
where decl.getLocation().getFile().getName().matches("%swift/ql/test%")
5-
select decl
10+
select decl, concat(describe(decl), ", ")

swift/ql/test/extractor-tests/declarations/declarations.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,7 @@ func ifConfig() {
178178
12
179179
#endif
180180
}
181+
182+
class B {}
183+
typealias A = B
184+
typealias C = Int?

swift/ql/test/library-tests/ast/PrintAst.expected

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4218,6 +4218,20 @@ declarations.swift:
42184218
# 173| getElement(5): [IfConfigDecl] #if ...
42194219
# 174| getElement(6): [IntegerLiteralExpr] 9
42204220
# 175| getElement(7): [IntegerLiteralExpr] 10
4221+
# 182| [ClassDecl] B
4222+
# 182| getMember(0): [DestructorDecl] B.deinit()
4223+
# 182| InterfaceType = (B) -> () -> ()
4224+
# 182| getSelfParam(): [ParamDecl] self
4225+
# 182| Type = B
4226+
# 182| getBody(): [BraceStmt] { ... }
4227+
# 182| getMember(1): [ConstructorDecl] B.init()
4228+
# 182| InterfaceType = (B.Type) -> () -> B
4229+
# 182| getSelfParam(): [ParamDecl] self
4230+
# 182| Type = B
4231+
# 182| getBody(): [BraceStmt] { ... }
4232+
# 182| getElement(0): [ReturnStmt] return
4233+
# 183| [TypeAliasDecl] A
4234+
# 184| [TypeAliasDecl] C
42214235
expressions.swift:
42224236
# 1| [TopLevelCodeDecl] { ... }
42234237
# 1| getBody(): [BraceStmt] { ... }

swift/ql/test/library-tests/ast/declarations.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,7 @@ func ifConfig() {
178178
12
179179
#endif
180180
}
181+
182+
class B {}
183+
typealias A = B
184+
typealias C = Int?
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
| nominaltype.swift:35:6:35:6 | a | A | A | |
2+
| nominaltype.swift:36:6:36:6 | a_alias | A_alias | A_alias | |
3+
| nominaltype.swift:37:6:37:6 | a_optional_alias | A_optional_alias | A_optional_alias | |
4+
| nominaltype.swift:38:6:38:6 | b1 | B1 | B1 | getABaseType:A |
5+
| nominaltype.swift:39:6:39:6 | b2 | B2 | B2 | |
6+
| nominaltype.swift:40:6:40:6 | b1_alias | B1_alias | B1_alias | |
7+
| nominaltype.swift:41:6:41:6 | b2_alias | B2_alias | B2_alias | |
8+
| nominaltype.swift:42:6:42:6 | p | P | P | |
9+
| nominaltype.swift:43:6:43:6 | p_alias | P_alias | P_alias | |
10+
| nominaltype.swift:44:6:44:6 | c1 | C1 | C1 | getABaseType:P |
11+
| nominaltype.swift:45:6:45:6 | c2 | C2 | C2 | |
12+
| nominaltype.swift:46:6:46:6 | c1_alias | C1_alias | C1_alias | |
13+
| nominaltype.swift:47:6:47:6 | c2_alias | C2_alias | C2_alias | |
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import swift
2+
3+
string describe(Type t) {
4+
result = "getAliasedType:" + t.(TypeAliasType).getAliasedType()
5+
or
6+
result = "getABaseType:" + t.(NominalType).getABaseType()
7+
}
8+
9+
from VarDecl v, Type t
10+
where
11+
v.getLocation().getFile().getBaseName() != "" and
12+
not v.getName() = "self" and
13+
t = v.getType()
14+
select v, t.toString(), t.getUnderlyingType(), concat(describe(t), ", ")

0 commit comments

Comments
 (0)