1010#include " swift/extractor/trap/TrapDomain.h"
1111#include " swift/extractor/infra/SwiftTagTraits.h"
1212#include " swift/extractor/trap/generated/TrapClasses.h"
13- #include " swift/extractor/infra/file/PathHash .h"
13+ #include " swift/extractor/infra/SwiftLocationExtractor .h"
1414
1515namespace codeql {
1616
@@ -29,8 +29,7 @@ class SwiftDispatcher {
2929 const swift::Expr*,
3030 const swift::Pattern*,
3131 const swift::TypeRepr*,
32- const swift::TypeBase*,
33- std::filesystem::path>;
32+ const swift::TypeBase*>;
3433
3534 template <typename E>
3635 static constexpr bool IsStorable = std::is_constructible_v<Store::Handle, const E&>;
@@ -48,10 +47,11 @@ class SwiftDispatcher {
4847 : sourceManager{sourceManager},
4948 trap{trap},
5049 currentModule{currentModule},
51- currentPrimarySourceFile{currentPrimarySourceFile} {
50+ currentPrimarySourceFile{currentPrimarySourceFile},
51+ locationExtractor (trap) {
5252 if (currentPrimarySourceFile) {
5353 // we make sure the file is in the trap output even if the source is empty
54- fetchLabel ( getFilePath ( currentPrimarySourceFile->getFilename () ));
54+ locationExtractor. emitFile ( currentPrimarySourceFile->getFilename ());
5555 }
5656 }
5757
@@ -325,20 +325,7 @@ class SwiftDispatcher {
325325 void attachLocation (swift::SourceLoc start,
326326 swift::SourceLoc end,
327327 TrapLabel<LocatableTag> locatableLabel) {
328- if (!start.isValid () || !end.isValid ()) {
329- // invalid locations seem to come from entities synthesized by the compiler
330- return ;
331- }
332- auto file = getFilePath (sourceManager.getDisplayNameForLoc (start));
333- DbLocation entry{{}};
334- entry.file = fetchLabel (file);
335- std::tie (entry.start_line , entry.start_column ) = sourceManager.getLineAndColumnInBuffer (start);
336- std::tie (entry.end_line , entry.end_column ) = sourceManager.getLineAndColumnInBuffer (end);
337- entry.id = trap.createLabel <DbLocationTag>(' {' , entry.file , " }:" , entry.start_line , ' :' ,
338- entry.start_column , ' :' , entry.end_line , ' :' ,
339- entry.end_column );
340- emit (entry);
341- emit (LocatableLocationsTrap{locatableLabel, entry.id });
328+ locationExtractor.attachLocation (sourceManager, start, end, locatableLabel);
342329 }
343330
344331 template <typename Tag, typename ... Ts>
@@ -391,19 +378,14 @@ class SwiftDispatcher {
391378 virtual void visit (const swift::TypeRepr* typeRepr, swift::Type type) = 0;
392379 virtual void visit (const swift::TypeBase* type) = 0;
393380
394- void visit (const std::filesystem::path& file) {
395- auto entry = createEntry (file, file.string ());
396- entry.name = file.string ();
397- emit (entry);
398- }
399-
400381 const swift::SourceManager& sourceManager;
401382 TrapDomain& trap;
402383 Store store;
403384 Store::Handle waitingForNewLabel{std::monostate{}};
404385 swift::ModuleDecl& currentModule;
405386 swift::SourceFile* currentPrimarySourceFile;
406387 std::unordered_set<swift::ModuleDecl*> encounteredModules;
388+ SwiftLocationExtractor locationExtractor;
407389};
408390
409391} // namespace codeql
0 commit comments