Skip to content

Commit 40219aa

Browse files
committed
Swift: More robust Type.getName().
1 parent 655f238 commit 40219aa

File tree

2 files changed

+6
-9
lines changed

2 files changed

+6
-9
lines changed

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@ class Type extends Generated::Type {
1212
* Gets the name of this type.
1313
*/
1414
override string getName() {
15-
// note that `Generated::Type.getName()` gets the full name of the type, so
16-
// we have to compute a short name.
17-
exists(string name, int lastDotPos |
18-
name = super.getName() and
19-
lastDotPos = max([-1, name.indexOf(".")]) and
20-
result = name.suffix(lastDotPos + 1)
21-
)
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("[^(), ]*\\.", "")
2219
}
2320

2421
/**

swift/ql/test/extractor-tests/generated/type/TupleType/TupleType.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
| (Builtin.IntLiteral, Builtin.IntLiteral) | getName: | IntLiteral) | getCanonicalType: | (Builtin.IntLiteral, Builtin.IntLiteral) | getNumberOfTypes: | 2 |
2-
| (Builtin.IntLiteral, Builtin.IntLiteral) | getName: | IntLiteral) | getCanonicalType: | (Builtin.IntLiteral, Builtin.IntLiteral) | getNumberOfTypes: | 2 |
1+
| (Builtin.IntLiteral, Builtin.IntLiteral) | getName: | (IntLiteral, IntLiteral) | getCanonicalType: | (Builtin.IntLiteral, Builtin.IntLiteral) | getNumberOfTypes: | 2 |
2+
| (Builtin.IntLiteral, Builtin.IntLiteral) | getName: | (IntLiteral, IntLiteral) | getCanonicalType: | (Builtin.IntLiteral, Builtin.IntLiteral) | getNumberOfTypes: | 2 |
33
| (Int, Int, Int, Int, Int) | getName: | (Int, Int, Int, Int, Int) | getCanonicalType: | (Int, Int, Int, Int, Int) | getNumberOfTypes: | 5 |
44
| (Int, String, Double) | getName: | (Int, String, Double) | getCanonicalType: | (Int, String, Double) | getNumberOfTypes: | 3 |
55
| (Int, s: String, Double) | getName: | (Int, s: String, Double) | getCanonicalType: | (Int, s: String, Double) | getNumberOfTypes: | 3 |

0 commit comments

Comments
 (0)