Skip to content

Commit 8205751

Browse files
committed
Swift: Implement Type.getName() as different from Type.getFullName() (regex solution).
1 parent aa6d7c0 commit 8205751

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ private import codeql.swift.generated.type.Type
88
class Type extends Generated::Type {
99
override string toString() { result = this.getName() }
1010

11+
/**
12+
* Gets the name of this type.
13+
*/
14+
override string getName() {
15+
/*exists(string name, int lastDotPos |
16+
name = super.getName() and
17+
lastDotPos = max([-1, name.indexOf(".")]) and
18+
result = name.suffix(lastDotPos + 1)
19+
)*/
20+
// match as many characters as possible at the end that are not `.`.
21+
// (`*?` is lazy matching)
22+
result = super.getName().regexpCapture(".*?([^\\.]*)", 1)
23+
}
24+
1125
/**
1226
* Gets this type after any type aliases have been resolved. For example in
1327
* the following code, the underlying type of `MyInt` is `Int`:

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
| nominaltype.swift:65:6:65:6 | c1_alias | C1_alias | getABaseType:P, getAliasedType:C1, getName:C1_alias, getUnderlyingType:C1 |
1313
| nominaltype.swift:66:6:66:6 | c2_alias | C2_alias | getABaseType:P_alias, getAliasedType:C2, getName:C2_alias, getUnderlyingType:C2 |
1414
| nominaltype.swift:67:6:67:6 | o | Outer | getFullName:Outer, getName:Outer, getUnderlyingType:Outer |
15-
| nominaltype.swift:68:6:68:6 | oi | Outer.Inner | getFullName:Outer.Inner, getName:Outer.Inner, getUnderlyingType:Outer.Inner |
16-
| nominaltype.swift:69:6:69:6 | oia | Outer.Inner.InnerAlias | getABaseType:FixedWidthInteger, getABaseType:SignedInteger, getABaseType:_ExpressibleByBuiltinIntegerLiteral, getAliasedType:Int, getName:Outer.Inner.InnerAlias, getUnderlyingType:Int |
15+
| nominaltype.swift:68:6:68:6 | oi | Inner | getFullName:Outer.Inner, getName:Inner, getUnderlyingType:Inner |
16+
| nominaltype.swift:69:6:69:6 | oia | InnerAlias | getABaseType:FixedWidthInteger, getABaseType:SignedInteger, getABaseType:_ExpressibleByBuiltinIntegerLiteral, getAliasedType:Int, getName:InnerAlias, getUnderlyingType:Int |
1717
| nominaltype.swift:70:6:70:6 | aa | Any? | getName:Any?, getUnderlyingType:Any? |
1818
| nominaltype.swift:71:6:71:6 | p1p2 | P1P2 | getName:P1P2, getUnderlyingType:P1P2 |
1919
| nominaltype.swift:72:6:72:6 | boxInt | Box<Int> | getName:Box<Int>, getUnderlyingType:Box<Int> |

0 commit comments

Comments
 (0)