Skip to content

Commit 040d72e

Browse files
authored
Merge pull request github#10857 from geoffw0/locationstring
Swift: Give Location a useful toString
2 parents 3a99b98 + dcf254a commit 040d72e

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed
Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
private import codeql.swift.generated.Location
22

3+
/**
4+
* A location of a program element.
5+
*/
36
class Location extends LocationBase {
4-
predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
5-
path = getFile().getFullName() and
6-
sl = getStartLine() and
7-
sc = getStartColumn() and
8-
el = getEndLine() and
9-
ec = getEndColumn()
7+
/**
8+
* Holds if this location is described by `path`, `startLine`, `startColumn`, `endLine` and `endColumn`.
9+
*/
10+
predicate hasLocationInfo(string path, int startLine, int startColumn, int endLine, int endColumn) {
11+
path = this.getFile().getFullName() and
12+
startLine = this.getStartLine() and
13+
startColumn = this.getStartColumn() and
14+
endLine = this.getEndLine() and
15+
endColumn = this.getEndColumn()
16+
}
17+
18+
/**
19+
* Gets a textual representation of this location.
20+
*/
21+
override string toString() {
22+
exists(string filePath, int startLine, int startColumn, int endLine, int endColumn |
23+
this.hasLocationInfo(filePath, startLine, startColumn, endLine, endColumn)
24+
|
25+
toUrl(filePath, startLine, startColumn, endLine, endColumn, result)
26+
)
1027
}
1128
}

swift/ql/lib/codeql/swift/elements/UnknownLocation.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ private import codeql.swift.generated.UnknownLocation
22
private import codeql.swift.elements.UnknownFile
33
private import codeql.swift.elements.File
44

5+
/**
6+
* A `Location` that is given to something that is not associated with any position in the source code.
7+
*/
58
class UnknownLocation extends UnknownLocationBase {
69
override File getImmediateFile() { result instanceof UnknownFile }
710

@@ -12,4 +15,6 @@ class UnknownLocation extends UnknownLocationBase {
1215
override int getEndLine() { result = 0 }
1316

1417
override int getEndColumn() { result = 0 }
18+
19+
override string toString() { result = "UnknownLocation" }
1520
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| file://:0:0:0:0 | UnknownLocation |
2+
| location.swift:2:1:3:1 | location.swift:2:1:3:1 |
3+
| location.swift:2:11:2:14 | location.swift:2:11:2:14 |
4+
| location.swift:2:19:3:1 | location.swift:2:19:3:1 |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import swift
2+
3+
from Location l
4+
select l
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
func test(x: Int) {
3+
}

0 commit comments

Comments
 (0)