11#pragma once
22
3- #include < swift/AST/SourceFile.h>
43#include < swift/Basic/SourceManager.h>
4+ #include < unordered_map>
5+ #include < filesystem>
56
6- #include " swift/extractor/trap/TrapDomain.h"
77#include " swift/extractor/trap/generated/TrapEntries.h"
8- #include " swift/extractor/trap/generated/TrapClasses.h"
98#include " swift/extractor/infra/file/PathHash.h"
109
1110namespace codeql {
@@ -19,52 +18,12 @@ class SwiftLocationExtractor {
1918 void attachLocation (const swift::SourceManager& sourceManager,
2019 swift::SourceLoc start,
2120 swift::SourceLoc end,
22- TrapLabel<LocatableTag> locatableLabel) {
23- if (!start.isValid () || !end.isValid ()) {
24- // invalid locations seem to come from entities synthesized by the compiler
25- return ;
26- }
27- auto file = getFilePath (sourceManager.getDisplayNameForLoc (start));
28- DbLocation entry{{}};
29- entry.file = fetchFileLabel (file);
30- std::tie (entry.start_line , entry.start_column ) = sourceManager.getLineAndColumnInBuffer (start);
31- std::tie (entry.end_line , entry.end_column ) = sourceManager.getLineAndColumnInBuffer (end);
32- entry.id = trap.createLabel <DbLocationTag>(' {' , entry.file , " }:" , entry.start_line , ' :' ,
33- entry.start_column , ' :' , entry.end_line , ' :' ,
34- entry.end_column );
35- trap.emit (entry);
36- trap.emit (LocatableLocationsTrap{locatableLabel, entry.id });
37- }
21+ TrapLabel<LocatableTag> locatableLabel);
3822
39- void emitFile (llvm::StringRef path) { fetchFileLabel ( getFilePath (path)); }
23+ void emitFile (llvm::StringRef path);
4024
4125 private:
42- TrapLabel<FileTag> fetchFileLabel (const std::filesystem::path& file) {
43- if (store.count (file)) {
44- return store[file];
45- }
46-
47- DbFile entry ({});
48- entry.id = trap.createLabel <DbFileTag>(file.string ());
49- entry.name = file.string ();
50- trap.emit (entry);
51- store[file] = entry.id ;
52- return entry.id ;
53- }
54-
55- static std::filesystem::path getFilePath (std::string_view path) {
56- // TODO: this needs more testing
57- // TODO: check canonicalization of names on a case insensitive filesystems
58- // TODO: make symlink resolution conditional on CODEQL_PRESERVE_SYMLINKS=true
59- std::error_code ec;
60- auto ret = std::filesystem::canonical (path, ec);
61- if (ec) {
62- std::cerr << " Cannot get real path: " << std::quoted (path) << " : " << ec.message () << " \n " ;
63- return {};
64- }
65- return ret;
66- }
67-
26+ TrapLabel<FileTag> fetchFileLabel (const std::filesystem::path& file);
6827 TrapDomain& trap;
6928 std::unordered_map<std::filesystem::path, TrapLabel<FileTag>> store;
7029};
0 commit comments