Skip to content

Commit 5e940cd

Browse files
authored
Merge pull request #13829 from geoffw0/typegetname
Swift: Correct the behaviour of Type.getName
2 parents 0e9f8c4 + b6dc2ac commit 5e940cd

File tree

16 files changed

+129
-67
lines changed

16 files changed

+129
-67
lines changed

swift/ql/.generated.list

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

swift/ql/.gitattributes

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
5+
* `Type.getName` now gets the name of the type alone without any enclosing types. Use `Type.getFullName` for the old behaviour.

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,4 @@ class NominalType extends Generated::NominalType {
66
override Type getABaseType() { result = this.getDeclaration().(NominalTypeDecl).getABaseType() }
77

88
NominalType getADerivedType() { result.getABaseType() = this }
9-
10-
/**
11-
* Gets the full name of this `NominalType`. For example in:
12-
* ```swift
13-
* struct A {
14-
* struct B {
15-
* // ...
16-
* }
17-
* }
18-
* ```
19-
* The name and full name of `A` is `A`. The name of `B` is `B`, but the
20-
* full name of `B` is `A.B`.
21-
*/
22-
string getFullName() { result = this.getDeclaration().(NominalTypeDecl).getFullName() }
239
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
// generated by codegen/codegen.py, remove this comment if you wish to edit this file
21
private import codeql.swift.generated.type.TupleType
32

3+
/**
4+
* A tuple type, for example:
5+
* ```
6+
* (Int, String)
7+
* ```
8+
*/
49
class TupleType extends Generated::TupleType { }

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,31 @@ private import codeql.swift.generated.type.Type
66
* This QL class is the root of the Swift type hierarchy.
77
*/
88
class Type extends Generated::Type {
9-
override string toString() { result = this.getName() }
9+
override string toString() { result = this.getFullName() }
10+
11+
/**
12+
* Gets the name of this type.
13+
*/
14+
override string getName() {
15+
// replace anything that looks like a full name `a.b.c` with just the
16+
// short name `c`, by removing the `a.` and `b.` parts. Note that this
17+
// has to be robust for tuple type names such as `(a, b.c)`.
18+
result = super.getName().regexpReplaceAll("[^(),. ]++\\.(?!\\.)", "")
19+
}
20+
21+
/**
22+
* Gets the full name of this `Type`. For example in:
23+
* ```swift
24+
* struct A {
25+
* struct B {
26+
* // ...
27+
* }
28+
* }
29+
* ```
30+
* The name and full name of `A` is `A`. The name of `B` is `B`, but the
31+
* full name of `B` is `A.B`.
32+
*/
33+
string getFullName() { result = super.getName() }
1034

1135
/**
1236
* Gets this type after any type aliases have been resolved. For example in

swift/ql/lib/codeql/swift/security/StringLengthConflationExtensions.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private class ExtraStringLengthConflationSource extends StringLengthConflationSo
158158
or
159159
stringType = TStringUnicodeScalars()
160160
) and
161-
memberRef.getBase().getType().(NominalType).getName() = stringType.getName() and
161+
memberRef.getBase().getType().(NominalType).getFullName() = stringType.getName() and
162162
memberRef.getMember().(VarDecl).getName() = "count" and
163163
this.asExpr() = memberRef
164164
)
@@ -218,8 +218,8 @@ private class ExtraStringLengthConflationSink extends StringLengthConflationSink
218218
stringType = TStringUnicodeScalars()
219219
) and
220220
(
221-
call.getQualifier().getType().(NominalType).getName() = stringType.getName() or
222-
call.getQualifier().getType().(InOutType).getObjectType().(NominalType).getName() =
221+
call.getQualifier().getType().(NominalType).getFullName() = stringType.getName() or
222+
call.getQualifier().getType().(InOutType).getObjectType().(NominalType).getFullName() =
223223
stringType.getName()
224224
) and
225225
(

swift/ql/lib/codeql/swift/security/UnsafeJsEvalExtensions.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ private class DefaultUnsafeJsEvalAdditionalFlowStep extends UnsafeJsEvalAddition
127127
)
128128
or
129129
exists(MemberRefExpr e, Expr self, VarDecl member |
130-
self.getType().getName().matches(["Unsafe%Buffer%", "Unsafe%Pointer%"]) and
130+
self.getType().getFullName().matches(["Unsafe%Buffer%", "Unsafe%Pointer%"]) and
131131
member.getName() = "baseAddress"
132132
|
133133
e.getBase() = self and
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
| Builtin.Int8 | getName: | Builtin.Int8 | getCanonicalType: | Builtin.Int8 | hasWidth: | yes |
2-
| Builtin.Int16 | getName: | Builtin.Int16 | getCanonicalType: | Builtin.Int16 | hasWidth: | yes |
3-
| Builtin.Int32 | getName: | Builtin.Int32 | getCanonicalType: | Builtin.Int32 | hasWidth: | yes |
4-
| Builtin.Int64 | getName: | Builtin.Int64 | getCanonicalType: | Builtin.Int64 | hasWidth: | yes |
5-
| Builtin.Word | getName: | Builtin.Word | getCanonicalType: | Builtin.Word | hasWidth: | no |
1+
| Builtin.Int8 | getName: | Int8 | getCanonicalType: | Builtin.Int8 | hasWidth: | yes |
2+
| Builtin.Int16 | getName: | Int16 | getCanonicalType: | Builtin.Int16 | hasWidth: | yes |
3+
| Builtin.Int32 | getName: | Int32 | getCanonicalType: | Builtin.Int32 | hasWidth: | yes |
4+
| Builtin.Int64 | getName: | Int64 | getCanonicalType: | Builtin.Int64 | hasWidth: | yes |
5+
| Builtin.Word | getName: | Word | getCanonicalType: | Builtin.Word | hasWidth: | no |
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
| Builtin.BridgeObject | BuiltinBridgeObjectType | getName: | Builtin.BridgeObject | getCanonicalType: | Builtin.BridgeObject |
2-
| Builtin.Executor | BuiltinExecutorType | getName: | Builtin.Executor | getCanonicalType: | Builtin.Executor |
3-
| Builtin.FPIEEE32 | BuiltinFloatType | getName: | Builtin.FPIEEE32 | getCanonicalType: | Builtin.FPIEEE32 |
4-
| Builtin.FPIEEE64 | BuiltinFloatType | getName: | Builtin.FPIEEE64 | getCanonicalType: | Builtin.FPIEEE64 |
5-
| Builtin.IntLiteral | BuiltinIntegerLiteralType | getName: | Builtin.IntLiteral | getCanonicalType: | Builtin.IntLiteral |
6-
| Builtin.Job | BuiltinJobType | getName: | Builtin.Job | getCanonicalType: | Builtin.Job |
7-
| Builtin.NativeObject | BuiltinNativeObjectType | getName: | Builtin.NativeObject | getCanonicalType: | Builtin.NativeObject |
8-
| Builtin.RawPointer | BuiltinRawPointerType | getName: | Builtin.RawPointer | getCanonicalType: | Builtin.RawPointer |
9-
| Builtin.RawUnsafeContinuation | BuiltinRawUnsafeContinuationType | getName: | Builtin.RawUnsafeContinuation | getCanonicalType: | Builtin.RawUnsafeContinuation |
1+
| Builtin.BridgeObject | BuiltinBridgeObjectType | getName: | BridgeObject | getCanonicalType: | Builtin.BridgeObject |
2+
| Builtin.Executor | BuiltinExecutorType | getName: | Executor | getCanonicalType: | Builtin.Executor |
3+
| Builtin.FPIEEE32 | BuiltinFloatType | getName: | FPIEEE32 | getCanonicalType: | Builtin.FPIEEE32 |
4+
| Builtin.FPIEEE64 | BuiltinFloatType | getName: | FPIEEE64 | getCanonicalType: | Builtin.FPIEEE64 |
5+
| Builtin.IntLiteral | BuiltinIntegerLiteralType | getName: | IntLiteral | getCanonicalType: | Builtin.IntLiteral |
6+
| Builtin.Job | BuiltinJobType | getName: | Job | getCanonicalType: | Builtin.Job |
7+
| Builtin.NativeObject | BuiltinNativeObjectType | getName: | NativeObject | getCanonicalType: | Builtin.NativeObject |
8+
| Builtin.RawPointer | BuiltinRawPointerType | getName: | RawPointer | getCanonicalType: | Builtin.RawPointer |
9+
| Builtin.RawUnsafeContinuation | BuiltinRawUnsafeContinuationType | getName: | RawUnsafeContinuation | getCanonicalType: | Builtin.RawUnsafeContinuation |

0 commit comments

Comments
 (0)