Skip to content

Commit 01e1c13

Browse files
committed
Swift: add UnknownLocation
`getLocation()` will now exists for all entities. When there is no valid location, the location will still not be emitted in the DB, but on the QL side we will then assign a special `UnknownLocation` with empty filename and 0 for line/column start/end. This unknown location is currently emitted (with a unique `@` key) at the start of every extraction, but we can move it elsewhere (and possibly in a unique global trap file) at a later stage, possibly after or when we rework the trap file strategy. This should solve flakiness that was observed on the control flow tests, which is probably caused by the `nodes` predicate in the `TestOutput` class in `ControlFlowGraphImplShared.qll` not able to assign a proper rank when the node does not have a location.
1 parent 4a02505 commit 01e1c13

File tree

5 files changed

+23
-3
lines changed

5 files changed

+23
-3
lines changed

swift/extractor/SwiftExtractor.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ static void extractFile(const SwiftExtractorConfiguration& config,
8484
TrapOutput trap{trapStream};
8585
TrapArena arena{};
8686

87+
// TODO move default location emission elsewhere, possibly in a separate global trap file
88+
auto unknownFileLabel = arena.allocateLabel<FileTag>();
89+
// the following cannot conflict with actual files as those have an absolute path starting with /
90+
trap.assignKey(unknownFileLabel, "unknown");
91+
trap.emit(FilesTrap{unknownFileLabel});
92+
auto unknownLocationLabel = arena.allocateLabel<LocationTag>();
93+
trap.assignKey(unknownLocationLabel, "unknown");
94+
trap.emit(LocationsTrap{unknownLocationLabel, unknownFileLabel});
95+
8796
// In the case of emtpy files, the dispatcher is not called, but we still want to 'record' the
8897
// fact that the file was extracted
8998
// TODO: to be moved elsewhere
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
// generated by codegen/codegen.py, remove this comment if you wish to edit this file
21
private import codeql.swift.generated.Locatable
2+
private import codeql.swift.elements.Location
33

4-
class Locatable extends LocatableBase { }
4+
class Locatable extends LocatableBase {
5+
override Location getLocation() {
6+
result = LocatableBase.super.getLocation()
7+
or
8+
not exists(LocatableBase.super.getLocation()) and result instanceof UnknownLocation
9+
}
10+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ class Location extends LocationBase {
99
ec = getEndColumn()
1010
}
1111
}
12+
13+
class UnknownLocation extends Location {
14+
UnknownLocation() { hasLocationInfo("", 0, 0, 0, 0) }
15+
}

swift/ql/test/extractor-tests/declarations/accessor.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ predicate isKnownKind(AccessorDecl decl, string kind) {
1212

1313
from AccessorDecl decl, string kind
1414
where
15-
exists(decl.getLocation()) and
15+
decl.getLocation().getFile().getName().matches("%swift/ql/test%") and
1616
(
1717
isKnownKind(decl, kind)
1818
or
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
| file://:0:0:0:0 | |
12
| hello.swift:0:0:0:0 | hello.swift |

0 commit comments

Comments
 (0)