forked from llvm/llvm-project
- Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathCMakeFileAPI.cpp
227 lines (202 loc) · 8.34 KB
/
CMakeFileAPI.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//===-- CMakeFileAPI.cpp - CMake File API ---------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include"CMakeFileAPI.h"
#include"llvm/Support/FileSystem.h"
#include"llvm/Support/MemoryBuffer.h"
#include"llvm/Support/Path.h"
usingnamespacellvm;
/// \returns true if \p Path is a nested directory in \p ParentPath.
/// \p Path should be absolute. Returns false if \p ParentPath is relative.
staticboolisPathNestedIn(StringRef Path, StringRef ParentPath) {
assert(sys::path::is_absolute(Path));
if (Path.size() <= ParentPath.size())
returnfalse;
if (!Path.startswith(ParentPath))
returnfalse;
returnsys::path::is_separator(Path.drop_front(ParentPath.size()).front());
}
/// Performs key lookups with \p KeyPath in the form of "key/subkey/anotherkey"
/// and returns the final nested value.
static Expected<const json::Value &> getValueFromPath(const json::Object &Root,
StringRef KeyPath,
StringRef ErrPrefix) {
assert(!KeyPath.empty());
StringRef Remaining = KeyPath;
size_t KeyPathPos = 0;
auto error = [&]() -> Error {
returncreateStringError(llvm::inconvertibleErrorCode(),
ErrPrefix + ": missing '" +
KeyPath.take_front(KeyPathPos - 1) +
"' entry");
};
const json::Object *Obj = &Root;
while (true) {
StringRef Key;
std::tie(Key, Remaining) = Remaining.split('/');
KeyPathPos += Key.size() + 1;
if (Remaining.empty()) {
const json::Value *Val = Obj->get(Key);
if (!Val)
returnerror();
return *Val;
}
Obj = Obj->getObject(Key);
if (!Obj)
returnerror();
}
}
/// Performs key lookups with \p KeyPath in the form of "key/subkey/anotherkey"
/// and returns the final nested string.
static Expected<StringRef> getStringFromPath(const json::Object &Root,
StringRef KeyPath,
StringRef ErrPrefix) {
auto Val = getValueFromPath(Root, KeyPath, ErrPrefix);
if (!Val)
return Val.takeError();
Optional<StringRef> Str = Val->getAsString();
if (!Str)
returncreateStringError(llvm::inconvertibleErrorCode(),
ErrPrefix + ": expected string '" + KeyPath +
"' entry");
return *Str;
}
Expected<cmake_file_api::Index>
cmake_file_api::Index::fromPath(StringRef CMakeBuildPath) {
// Detect presence of index file like
// '.cmake/api/v1/reply/index-2019-05-25T23-04-47-0494.json'
SmallString<128> ReplyPath(CMakeBuildPath);
sys::path::append(ReplyPath, ".cmake", "api", "v1", "reply");
std::error_code EC;
Optional<std::string> RecentIndexPath;
for (sys::fs::directory_iterator DirIt(ReplyPath, EC), DirE;
!EC && DirIt != DirE; DirIt.increment(EC)) {
StringRef Filename = sys::path::filename(DirIt->path());
if (Filename.startswith("index-") && Filename.endswith(".json")) {
if (!RecentIndexPath || sys::path::filename(*RecentIndexPath) < Filename)
RecentIndexPath = DirIt->path();
}
}
if (EC)
returncreateStringError(EC, "failed reading contents of '" + ReplyPath +
"': " + EC.message());
if (!RecentIndexPath)
returncreateStringError(llvm::inconvertibleErrorCode(),
"missing 'index' file");
StringRef IndexPath = *RecentIndexPath;
ErrorOr<std::unique_ptr<MemoryBuffer>> File =
MemoryBuffer::getFile(IndexPath);
if (!File)
returncreateStringError(File.getError(),
"failed opening '" + IndexPath +
"': " + File.getError().message());
Expected<json::Value> Val = json::parse((*File)->getBuffer());
if (!Val)
returncreateStringError(llvm::inconvertibleErrorCode(),
"json error parsing '" + IndexPath +
"': " + toString(Val.takeError()));
json::Object *Obj = Val->getAsObject();
if (!Obj)
returncreateStringError(llvm::inconvertibleErrorCode(),
"expected json dictionary for 'index' file");
returncmake_file_api::Index(std::move(*Obj), ReplyPath.str().str());
}
Expected<cmake_file_api::CodeModel>
cmake_file_api::Index::getCodeModel() const {
Expected<StringRef> CodeModelPath =
getStringFromPath(Obj, "reply/codemodel-v2/jsonFile", "index");
if (!CodeModelPath)
return CodeModelPath.takeError();
SmallString<128> FullCodeModelPath(CMakeFileAPIPath);
sys::path::append(FullCodeModelPath, *CodeModelPath);
ErrorOr<std::unique_ptr<MemoryBuffer>> File =
MemoryBuffer::getFile(FullCodeModelPath);
if (!File)
returncreateStringError(File.getError(),
"failed opening '" + FullCodeModelPath +
"': " + File.getError().message());
Expected<json::Value> Val = json::parse((*File)->getBuffer());
if (!Val)
returncreateStringError(llvm::inconvertibleErrorCode(),
"json error parsing '" + FullCodeModelPath +
"': " + toString(Val.takeError()));
json::Object *Obj = Val->getAsObject();
if (!Obj)
returncreateStringError(llvm::inconvertibleErrorCode(),
"expected json dictionary for 'codemodel' file");
returncmake_file_api::CodeModel(std::move(*Obj));
}
Expected<StringRef> cmake_file_api::CodeModel::getBuildPath() const {
Expected<StringRef> BuildPath =
getStringFromPath(Obj, "paths/build", "codemodel");
if (!BuildPath)
return BuildPath.takeError();
return *BuildPath;
}
Expected<StringRef> cmake_file_api::CodeModel::getSourcePath() const {
Expected<StringRef> SourcePath =
getStringFromPath(Obj, "paths/source", "codemodel");
if (!SourcePath)
return SourcePath.takeError();
return *SourcePath;
}
Error cmake_file_api::CodeModel::getExtraTopLevelSourcePaths(
SmallVectorImpl<StringRef> &TopLevelPaths) const {
const json::Array *Configs = Obj.getArray("configurations");
if (!Configs || Configs->empty())
returncreateStringError(llvm::inconvertibleErrorCode(),
"codemodel: missing 'configurations' entry");
const json::Object *Config = Configs->front().getAsObject();
if (!Config)
returncreateStringError(
llvm::inconvertibleErrorCode(),
"codemodel: expected 'configurations' as dictionary entries");
const json::Array *Dirs = Config->getArray("directories");
if (!Dirs)
returncreateStringError(
llvm::inconvertibleErrorCode(),
"codemodel: missing 'configurations[0]/directories' entry");
structDirInfoTy {
StringRef SourcePath;
Optional<int64_t> ParentIndex;
};
auto getDirInfo = [Dirs](unsigned Index) -> Expected<DirInfoTy> {
const json::Object *DirObj = (*Dirs)[Index].getAsObject();
if (!DirObj)
returncreateStringError(
llvm::inconvertibleErrorCode(),
"codemodel: expected 'directories' as dictionary entries");
Optional<StringRef> SourcePath = DirObj->getString("source");
if (!SourcePath)
returncreateStringError(
llvm::inconvertibleErrorCode(),
"codemodel: missing 'configurations[0]/directories/source' entry");
return DirInfoTy{*SourcePath, DirObj->getInteger("parentIndex")};
};
auto isTopLevelDir =
[&getDirInfo](const DirInfoTy &DirInfo) -> Expected<bool> {
if (sys::path::is_relative(DirInfo.SourcePath))
returnfalse;
if (!DirInfo.ParentIndex)
returntrue;
auto Parent = getDirInfo(*DirInfo.ParentIndex);
if (!Parent)
return Parent.takeError();
return !isPathNestedIn(DirInfo.SourcePath, Parent->SourcePath);
};
for (unsigned I = 0, E = Dirs->size(); I != E; ++I) {
auto DirInfo = getDirInfo(I);
if (!DirInfo)
return DirInfo.takeError();
Expected<bool> TopLevelCheck = isTopLevelDir(*DirInfo);
if (!TopLevelCheck)
return TopLevelCheck.takeError();
if (*TopLevelCheck)
TopLevelPaths.push_back(DirInfo->SourcePath);
}
returnError::success();
}