Skip to content

Commit c0f9b11

Browse files
authored
Merge pull request github#12347 from github/alexdenisov/move-location-extraction
Swift: move location extraction logic into a separate class. NFC
2 parents 2db588f + 5701798 commit c0f9b11

File tree

4 files changed

+100
-61
lines changed

4 files changed

+100
-61
lines changed

swift/extractor/SwiftExtractor.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "swift/extractor/infra/TargetDomains.h"
1313
#include "swift/extractor/SwiftBuiltinSymbols.h"
1414
#include "swift/extractor/infra/file/Path.h"
15+
#include "swift/extractor/infra/SwiftLocationExtractor.h"
1516

1617
using namespace codeql;
1718
using namespace std::string_literals;
@@ -139,7 +140,9 @@ static std::unordered_set<swift::ModuleDecl*> extractDeclarations(
139140
}
140141
}
141142

142-
SwiftVisitor visitor(compiler.getSourceMgr(), *trap, module, primaryFile);
143+
SwiftLocationExtractor locationExtractor(*trap);
144+
locationExtractor.emitFile(primaryFile);
145+
SwiftVisitor visitor(compiler.getSourceMgr(), *trap, locationExtractor, module, primaryFile);
143146
auto topLevelDecls = getTopLevelDecls(module, primaryFile);
144147
for (auto decl : topLevelDecls) {
145148
visitor.extract(decl);

swift/extractor/infra/SwiftDispatcher.h

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,14 @@ class SwiftDispatcher {
4545
// the SwiftDispatcher
4646
SwiftDispatcher(const swift::SourceManager& sourceManager,
4747
TrapDomain& trap,
48+
SwiftLocationExtractor& locationExtractor,
4849
swift::ModuleDecl& currentModule,
4950
swift::SourceFile* currentPrimarySourceFile = nullptr)
5051
: sourceManager{sourceManager},
5152
trap{trap},
53+
locationExtractor{locationExtractor},
5254
currentModule{currentModule},
53-
currentPrimarySourceFile{currentPrimarySourceFile},
54-
locationExtractor(trap) {
55-
if (currentPrimarySourceFile) {
56-
// we make sure the file is in the trap output even if the source is empty
57-
locationExtractor.emitFile(currentPrimarySourceFile->getFilename());
58-
}
59-
}
55+
currentPrimarySourceFile{currentPrimarySourceFile} {}
6056

6157
const std::unordered_set<swift::ModuleDecl*> getEncounteredModules() && {
6258
return std::move(encounteredModules);
@@ -157,7 +153,7 @@ class SwiftDispatcher {
157153
// TODO when everything is moved to structured C++ classes, this should be moved to createEntry
158154
if (auto l = store.get(e)) {
159155
if constexpr (IsLocatable<E>) {
160-
attachLocation(e, *l);
156+
locationExtractor.attachLocation(sourceManager, e, *l);
161157
}
162158
return *l;
163159
}
@@ -206,50 +202,10 @@ class SwiftDispatcher {
206202
template <typename E, typename... Args>
207203
auto createUncachedEntry(const E& e, Args&&... args) {
208204
auto label = trap.createLabel<TrapTagOf<E>>(std::forward<Args>(args)...);
209-
attachLocation(&e, label);
205+
locationExtractor.attachLocation(sourceManager, &e, label);
210206
return TrapClassOf<E>{label};
211207
}
212208

213-
template <typename Locatable>
214-
void attachLocation(Locatable locatable, TrapLabel<LocatableTag> locatableLabel) {
215-
attachLocation(&locatable, locatableLabel);
216-
}
217-
218-
// Emits a Location TRAP entry and attaches it to a `Locatable` trap label
219-
template <typename Locatable>
220-
void attachLocation(Locatable* locatable, TrapLabel<LocatableTag> locatableLabel) {
221-
attachLocation(locatable->getStartLoc(), locatable->getEndLoc(), locatableLabel);
222-
}
223-
224-
void attachLocation(const swift::CapturedValue* capture, TrapLabel<LocatableTag> locatableLabel) {
225-
attachLocation(capture->getLoc(), locatableLabel);
226-
}
227-
228-
void attachLocation(const swift::IfConfigClause* clause, TrapLabel<LocatableTag> locatableLabel) {
229-
attachLocation(clause->Loc, clause->Loc, locatableLabel);
230-
}
231-
232-
void attachLocation(swift::AvailabilitySpec* spec, TrapLabel<LocatableTag> locatableLabel) {
233-
attachLocation(spec->getSourceRange().Start, spec->getSourceRange().End, locatableLabel);
234-
}
235-
236-
// Emits a Location TRAP entry and attaches it to a `Locatable` trap label for a given `SourceLoc`
237-
void attachLocation(swift::SourceLoc loc, TrapLabel<LocatableTag> locatableLabel) {
238-
attachLocation(loc, loc, locatableLabel);
239-
}
240-
241-
// Emits a Location TRAP entry for a list of swift entities and attaches it to a `Locatable` trap
242-
// label
243-
template <typename Locatable>
244-
void attachLocation(llvm::MutableArrayRef<Locatable>* locatables,
245-
TrapLabel<LocatableTag> locatableLabel) {
246-
if (locatables->empty()) {
247-
return;
248-
}
249-
attachLocation(locatables->front().getStartLoc(), locatables->back().getEndLoc(),
250-
locatableLabel);
251-
}
252-
253209
// return `std::optional(fetchLabel(arg))` if arg converts to true, otherwise std::nullopt
254210
// universal reference `Arg&&` is used to catch both temporary and non-const references, not
255211
// for perfect forwarding
@@ -317,7 +273,7 @@ class SwiftDispatcher {
317273
void emitComment(swift::Token& comment) {
318274
CommentsTrap entry{trap.createLabel<CommentTag>(), comment.getRawText().str()};
319275
trap.emit(entry);
320-
attachLocation(comment.getRange().getStart(), comment.getRange().getEnd(), entry.id);
276+
locationExtractor.attachLocation(sourceManager, comment, entry.id);
321277
}
322278

323279
private:
@@ -333,12 +289,6 @@ class SwiftDispatcher {
333289
template <typename T>
334290
struct HasId<T, decltype(std::declval<T>().id, void())> : std::true_type {};
335291

336-
void attachLocation(swift::SourceLoc start,
337-
swift::SourceLoc end,
338-
TrapLabel<LocatableTag> locatableLabel) {
339-
locationExtractor.attachLocation(sourceManager, start, end, locatableLabel);
340-
}
341-
342292
template <typename Tag, typename... Ts>
343293
TrapLabel<Tag> fetchLabelFromUnion(const llvm::PointerUnion<Ts...> u) {
344294
TrapLabel<Tag> ret{};
@@ -382,11 +332,11 @@ class SwiftDispatcher {
382332
const swift::SourceManager& sourceManager;
383333
TrapDomain& trap;
384334
Store store;
335+
SwiftLocationExtractor& locationExtractor;
385336
Store::Handle waitingForNewLabel{std::monostate{}};
386337
swift::ModuleDecl& currentModule;
387338
swift::SourceFile* currentPrimarySourceFile;
388339
std::unordered_set<swift::ModuleDecl*> encounteredModules;
389-
SwiftLocationExtractor locationExtractor;
390340
};
391341

392342
} // namespace codeql

swift/extractor/infra/SwiftLocationExtractor.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <swift/AST/SourceFile.h>
22
#include <swift/Basic/SourceManager.h>
3+
#include <swift/Parse/Token.h>
34

45
#include "swift/extractor/trap/TrapDomain.h"
56
#include "swift/extractor/trap/generated/TrapEntries.h"
@@ -29,8 +30,42 @@ void SwiftLocationExtractor::attachLocation(const swift::SourceManager& sourceMa
2930
trap.emit(LocatableLocationsTrap{locatableLabel, entry.id});
3031
}
3132

32-
void SwiftLocationExtractor::emitFile(llvm::StringRef path) {
33-
fetchFileLabel(resolvePath(path));
33+
void SwiftLocationExtractor::emitFile(swift::SourceFile* file) {
34+
if (file) {
35+
fetchFileLabel(resolvePath(file->getFilename()));
36+
}
37+
}
38+
39+
void SwiftLocationExtractor::attachLocation(const swift::SourceManager& sourceManager,
40+
const swift::CapturedValue* capture,
41+
TrapLabel<LocatableTag> locatableLabel) {
42+
attachLocation(sourceManager, capture->getLoc(), locatableLabel);
43+
}
44+
45+
void SwiftLocationExtractor::attachLocation(const swift::SourceManager& sourceManager,
46+
const swift::IfConfigClause* clause,
47+
TrapLabel<LocatableTag> locatableLabel) {
48+
attachLocation(sourceManager, clause->Loc, clause->Loc, locatableLabel);
49+
}
50+
51+
void SwiftLocationExtractor::attachLocation(const swift::SourceManager& sourceManager,
52+
swift::AvailabilitySpec* spec,
53+
TrapLabel<LocatableTag> locatableLabel) {
54+
attachLocation(sourceManager, spec->getSourceRange().Start, spec->getSourceRange().End,
55+
locatableLabel);
56+
}
57+
58+
void SwiftLocationExtractor::attachLocation(const swift::SourceManager& sourceManager,
59+
swift::Token& token,
60+
TrapLabel<LocatableTag> locatableLabel) {
61+
attachLocation(sourceManager, token.getRange().getStart(), token.getRange().getEnd(),
62+
locatableLabel);
63+
}
64+
65+
void SwiftLocationExtractor::attachLocation(const swift::SourceManager& sourceManager,
66+
swift::SourceLoc loc,
67+
TrapLabel<LocatableTag> locatableLabel) {
68+
attachLocation(sourceManager, loc, loc, locatableLabel);
3469
}
3570

3671
TrapLabel<FileTag> SwiftLocationExtractor::fetchFileLabel(const std::filesystem::path& file) {

swift/extractor/infra/SwiftLocationExtractor.h

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#pragma once
22

3+
#include <swift/AST/ASTAllocated.h>
4+
#include <swift/AST/AvailabilitySpec.h>
5+
#include <swift/AST/SourceFile.h>
36
#include <swift/Basic/SourceManager.h>
47
#include <unordered_map>
58
#include <filesystem>
@@ -15,12 +18,60 @@ class SwiftLocationExtractor {
1518
public:
1619
explicit SwiftLocationExtractor(TrapDomain& trap) : trap(trap) {}
1720

21+
void emitFile(swift::SourceFile* file);
22+
23+
template <typename Locatable>
24+
void attachLocation(const swift::SourceManager& sourceManager,
25+
Locatable locatable,
26+
TrapLabel<LocatableTag> locatableLabel) {
27+
attachLocation(sourceManager, &locatable, locatableLabel);
28+
}
29+
30+
// Emits a Location TRAP entry and attaches it to a `Locatable` trap label
31+
template <typename Locatable>
32+
void attachLocation(const swift::SourceManager& sourceManager,
33+
Locatable* locatable,
34+
TrapLabel<LocatableTag> locatableLabel) {
35+
attachLocation(sourceManager, locatable->getStartLoc(), locatable->getEndLoc(), locatableLabel);
36+
}
37+
38+
// Emits a Location TRAP entry for a list of swift entities and attaches it to a `Locatable` trap
39+
// label
40+
template <typename Locatable>
41+
void attachLocation(const swift::SourceManager& sourceManager,
42+
llvm::MutableArrayRef<Locatable>* locatables,
43+
TrapLabel<LocatableTag> locatableLabel) {
44+
if (locatables->empty()) {
45+
return;
46+
}
47+
attachLocation(sourceManager, locatables->front().getStartLoc(), locatables->back().getEndLoc(),
48+
locatableLabel);
49+
}
50+
1851
void attachLocation(const swift::SourceManager& sourceManager,
1952
swift::SourceLoc start,
2053
swift::SourceLoc end,
2154
TrapLabel<LocatableTag> locatableLabel);
2255

23-
void emitFile(llvm::StringRef path);
56+
void attachLocation(const swift::SourceManager& sourceManager,
57+
swift::SourceLoc loc,
58+
TrapLabel<LocatableTag> locatableLabel);
59+
60+
void attachLocation(const swift::SourceManager& sourceManager,
61+
const swift::CapturedValue* capture,
62+
TrapLabel<LocatableTag> locatableLabel);
63+
64+
void attachLocation(const swift::SourceManager& sourceManager,
65+
const swift::IfConfigClause* clause,
66+
TrapLabel<LocatableTag> locatableLabel);
67+
68+
void attachLocation(const swift::SourceManager& sourceManager,
69+
swift::AvailabilitySpec* spec,
70+
TrapLabel<LocatableTag> locatableLabel);
71+
72+
void attachLocation(const swift::SourceManager& sourceManager,
73+
swift::Token& token,
74+
TrapLabel<LocatableTag> locatableLabel);
2475

2576
private:
2677
TrapLabel<FileTag> fetchFileLabel(const std::filesystem::path& file);

0 commit comments

Comments
 (0)