1
1
#pragma once
2
2
3
- #include < swift/AST/SourceFile.h>
4
3
#include < swift/Basic/SourceManager.h>
4
+ #include < unordered_map>
5
+ #include < filesystem>
5
6
6
- #include " swift/extractor/trap/TrapDomain.h"
7
7
#include " swift/extractor/trap/generated/TrapEntries.h"
8
- #include " swift/extractor/trap/generated/TrapClasses.h"
9
8
#include " swift/extractor/infra/file/PathHash.h"
10
9
11
10
namespace codeql {
@@ -19,52 +18,12 @@ class SwiftLocationExtractor {
19
18
void attachLocation (const swift::SourceManager& sourceManager,
20
19
swift::SourceLoc start,
21
20
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);
38
22
39
- void emitFile (llvm::StringRef path) { fetchFileLabel ( getFilePath (path)); }
23
+ void emitFile (llvm::StringRef path);
40
24
41
25
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);
68
27
TrapDomain& trap;
69
28
std::unordered_map<std::filesystem::path, TrapLabel<FileTag>> store;
70
29
};
0 commit comments