-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLocationManager.cpp
96 lines (82 loc) · 3.19 KB
/
LocationManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "LocationManager.h"
#include "../Utils.h"
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/YAMLTraits.h>
template <> struct llvm::yaml::MappingTraits<HeaderEntry> {
static void mapping(llvm::yaml::IO &io, HeaderEntry &headerEntry) {
io.mapRequired("path", headerEntry.path);
io.mapRequired("object", headerEntry.object);
io.mapOptional("names", headerEntry.names);
}
};
template <> struct llvm::yaml::MappingTraits<HeaderEntryName> {
static void mapping(llvm::yaml::IO &io, HeaderEntryName &headerEntryName) {
// Dynamically look up the available keys when only one key is given.
//
// ```yaml
// - struct point: Point
// ```
if (io.keys().size() == 1) {
for (auto key : io.keys()) {
headerEntryName.original = key;
io.mapRequired(headerEntryName.original.c_str(),
headerEntryName.target);
}
} else {
io.mapRequired("original", headerEntryName.original);
io.mapRequired("target", headerEntryName.target);
}
}
};
LLVM_YAML_IS_SEQUENCE_VECTOR(HeaderEntry)
LLVM_YAML_IS_SEQUENCE_VECTOR(HeaderEntryName)
LocationManager::LocationManager(std::string mainHeaderPath)
: headerEntries(), mainHeaderPath(std::move(mainHeaderPath)) {}
bool LocationManager::loadConfig(const std::string &path) {
llvm::SmallString<4096> realPath;
if (llvm::sys::fs::real_path(path, realPath))
return false;
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> mb =
llvm::MemoryBuffer::getFile(realPath);
if (!mb)
return false;
llvm::yaml::Input input(mb->get()->getBuffer());
input >> headerEntries;
return true;
}
bool LocationManager::inMainFile(const Location &location) const {
return location.getPath() == mainHeaderPath;
}
bool LocationManager::isImported(const Location &location) const {
if (location.getPath().empty()) {
return false;
}
return getHeaderEntryIndex(location) != headerEntries.size();
}
std::vector<HeaderEntry>::size_type
LocationManager::getHeaderEntryIndex(const Location &location) const {
auto isHeader = [&](const HeaderEntry &headerEntry) {
if (startsWith(headerEntry.path, "/") &&
location.getPath() == headerEntry.path)
return true;
return endsWith(location.getPath(), "/" + headerEntry.path);
};
auto it =
std::find_if(headerEntries.begin(), headerEntries.end(), isHeader);
return static_cast<std::vector<HeaderEntry>::size_type>(
it - headerEntries.begin());
}
std::string LocationManager::getImportedType(const Location &location,
const std::string &name) const {
auto index = getHeaderEntryIndex(location);
if (index == headerEntries.size())
return name;
auto headerEntry = headerEntries[index];
for (auto const &headerEntryName : headerEntry.names) {
if (headerEntryName.original == name) {
return headerEntry.object + "." + headerEntryName.target;
}
}
return headerEntry.object + "." +
handleReservedWords(replaceChar(name, " ", "_"));
}