Skip to content

Commit 4f05f08

Browse files
committed
Swift: split SwiftLocationExtractor into h/cpp
1 parent 609c7cf commit 4f05f08

File tree

3 files changed

+65
-46
lines changed

3 files changed

+65
-46
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <swift/AST/SourceFile.h>
2+
#include <swift/Basic/SourceManager.h>
3+
4+
#include "swift/extractor/trap/TrapDomain.h"
5+
#include "swift/extractor/trap/generated/TrapEntries.h"
6+
#include "swift/extractor/trap/generated/TrapClasses.h"
7+
#include "swift/extractor/infra/SwiftLocationExtractor.h"
8+
9+
using namespace codeql;
10+
11+
static std::filesystem::path getFilePath(std::string_view path) {
12+
// TODO: this needs more testing
13+
// TODO: check canonicalization of names on a case insensitive filesystems
14+
// TODO: make symlink resolution conditional on CODEQL_PRESERVE_SYMLINKS=true
15+
std::error_code ec;
16+
auto ret = std::filesystem::canonical(path, ec);
17+
if (ec) {
18+
std::cerr << "Cannot get real path: " << std::quoted(path) << ": " << ec.message() << "\n";
19+
return {};
20+
}
21+
return ret;
22+
}
23+
24+
void SwiftLocationExtractor::attachLocation(const swift::SourceManager& sourceManager,
25+
swift::SourceLoc start,
26+
swift::SourceLoc end,
27+
TrapLabel<LocatableTag> locatableLabel) {
28+
if (!start.isValid() || !end.isValid()) {
29+
// invalid locations seem to come from entities synthesized by the compiler
30+
return;
31+
}
32+
auto file = getFilePath(sourceManager.getDisplayNameForLoc(start));
33+
DbLocation entry{{}};
34+
entry.file = fetchFileLabel(file);
35+
std::tie(entry.start_line, entry.start_column) = sourceManager.getLineAndColumnInBuffer(start);
36+
std::tie(entry.end_line, entry.end_column) = sourceManager.getLineAndColumnInBuffer(end);
37+
entry.id = trap.createLabel<DbLocationTag>('{', entry.file, "}:", entry.start_line, ':',
38+
entry.start_column, ':', entry.end_line, ':',
39+
entry.end_column);
40+
trap.emit(entry);
41+
trap.emit(LocatableLocationsTrap{locatableLabel, entry.id});
42+
}
43+
44+
void SwiftLocationExtractor::emitFile(llvm::StringRef path) {
45+
fetchFileLabel(getFilePath(path));
46+
}
47+
48+
TrapLabel<FileTag> SwiftLocationExtractor::fetchFileLabel(const std::filesystem::path& file) {
49+
if (store.count(file)) {
50+
return store[file];
51+
}
52+
53+
DbFile entry({});
54+
entry.id = trap.createLabel<DbFileTag>(file.string());
55+
entry.name = file.string();
56+
trap.emit(entry);
57+
store[file] = entry.id;
58+
return entry.id;
59+
}

swift/extractor/infra/SwiftLocationExtractor.h

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
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

1110
namespace 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
};

swift/extractor/invocation/SwiftDiagnosticsConsumer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "swift/extractor/invocation/SwiftDiagnosticsConsumer.h"
22
#include "swift/extractor/trap/generated/TrapEntries.h"
3+
#include "swift/extractor/trap/TrapDomain.h"
34

45
#include <swift/AST/DiagnosticEngine.h>
56
#include <swift/Basic/SourceManager.h>

0 commit comments

Comments
 (0)