Swift: Implement Type.getName() as different from Type.getFullName() (regex solution).

This commit is contained in:
Geoffrey White
2023-07-27 18:30:42 +01:00
parent aa6d7c088b
commit 82057513c5
2 changed files with 16 additions and 2 deletions

View File

@@ -8,6 +8,20 @@ private import codeql.swift.generated.type.Type
class Type extends Generated::Type {
override string toString() { result = this.getName() }
/**
* Gets the name of this type.
*/
override string getName() {
/*exists(string name, int lastDotPos |
name = super.getName() and
lastDotPos = max([-1, name.indexOf(".")]) and
result = name.suffix(lastDotPos + 1)
)*/
// match as many characters as possible at the end that are not `.`.
// (`*?` is lazy matching)
result = super.getName().regexpCapture(".*?([^\\.]*)", 1)
}
/**
* Gets this type after any type aliases have been resolved. For example in
* the following code, the underlying type of `MyInt` is `Int`:

View File

@@ -12,8 +12,8 @@
| nominaltype.swift:65:6:65:6 | c1_alias | C1_alias | getABaseType:P, getAliasedType:C1, getName:C1_alias, getUnderlyingType:C1 |
| nominaltype.swift:66:6:66:6 | c2_alias | C2_alias | getABaseType:P_alias, getAliasedType:C2, 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:Outer.Inner, getUnderlyingType:Outer.Inner |
| nominaltype.swift:69:6:69:6 | oia | Outer.Inner.InnerAlias | getABaseType:FixedWidthInteger, getABaseType:SignedInteger, getABaseType:_ExpressibleByBuiltinIntegerLiteral, getAliasedType:Int, getName:Outer.Inner.InnerAlias, getUnderlyingType:Int |
| nominaltype.swift:68:6:68:6 | oi | Inner | getFullName:Outer.Inner, getName:Inner, getUnderlyingType:Inner |
| nominaltype.swift:69:6:69:6 | oia | InnerAlias | getABaseType:FixedWidthInteger, getABaseType:SignedInteger, getABaseType:_ExpressibleByBuiltinIntegerLiteral, getAliasedType:Int, getName:InnerAlias, getUnderlyingType:Int |
| nominaltype.swift:70:6:70:6 | aa | Any? | getName:Any?, getUnderlyingType:Any? |
| nominaltype.swift:71:6:71:6 | p1p2 | P1P2 | getName:P1P2, getUnderlyingType:P1P2 |
| nominaltype.swift:72:6:72:6 | boxInt | Box<Int> | getName:Box<Int>, getUnderlyingType:Box<Int> |