Repository navigation
Expand file tree
/
Copy pathscanner.h
More file actions
294 lines (256 loc) · 9.06 KB
/
Copy pathscanner.h
File metadata and controls
294 lines (256 loc) · 9.06 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#pragma once
#include <vector>
#include <string_view>
#include <memory>
#include "span.hpp"
#include "depinfo.h"
#include "strong_id.h"
namespace cppm {
DECL_STRONG_ID(target_idx_t)
DECL_STRONG_ID(cmd_idx_t)
DECL_STRONG_ID(scan_item_idx_t)
template<typename string_t>
struct ScanItemBase {
string_t path; // UTF_8
cmd_idx_t command_idx = {};
target_idx_t target_idx = {};
bool is_header_unit = false;
// if the item is not selected then it might not be scanned,
// even if it's out of date, unless it's a dependency of a selected item
bool is_selected = true;
template<typename other_string_t>
static ScanItemBase<string_t> from(const ScanItemBase<other_string_t> & item) {
return { item.path, item.command_idx, item.target_idx,
item.is_header_unit, item.is_selected };
}
explicit operator ScanItemBase<std::string_view>() const {
return { path, command_idx, target_idx, is_header_unit, is_selected };
}
};
using ScanItem = ScanItemBase<std::string>; // todo: use std::u8string(_view) everywhere
using ScanItemView = ScanItemBase<std::string_view>;
namespace detail {
template<typename T> struct is_view : std::false_type {};
template<> struct is_view<std::string_view> : std::true_type {};
template<> struct is_view<ScanItemView> : std::true_type {};
template<typename T> constexpr bool is_view_v = is_view<T>::value;
template<typename T> struct is_span_map : std::false_type {};
template<typename U, typename V> struct is_span_map < span_map<U, V> >: std::true_type {};
template<typename T> constexpr bool is_span_map_v = is_span_map<T>::value;
// convert between any of the following, when possible
// vector<object>
// vector<view>
// span<object>
// span<view>
// where object could be string/ScanItem
// and view could be string_view/ScanItemView
template<
template < typename, typename > typename dst_map_t, typename dst_idx_t, typename dst_val_t,
template < typename, typename > typename src_map_t, typename src_idx_t, typename src_val_t
>
static void conv(dst_map_t<dst_idx_t, dst_val_t>& dst, const src_map_t<src_idx_t, src_val_t>& src) {
using dst_t = dst_map_t<dst_idx_t, dst_val_t>;
if constexpr (is_span_map_v<dst_t>) {
if constexpr (is_view_v<dst_val_t>) {
static_assert(is_view_v<src_val_t>,
"cannot create e.g a span<string_view> from a *<string> directly");
dst = src;
} else { // !is_view_v<dst_val_t>
static_assert(!is_view_v<src_val_t>,
"cannot create e.g a span<string> from a *<string_view>");
dst = src;
}
} else { // is_vector_map<dst_t>
dst.resize(src.size()); // works for both vector/span
for (auto i : src.indices())
dst[i] = (dst_val_t)src[i]; // works for string(_view)<->string(_view)
}
}
} // namespace detail
template<
typename string_t,
template < typename, typename > typename map_t
>
struct ScanItemSetBase;
using ScanItemSet = ScanItemSetBase<std::string, vector_map>;
using ScanItemSetOwnedView = ScanItemSetBase<std::string_view, vector_map>;
using ScanItemSetView = ScanItemSetBase<std::string_view, span_map>;
template<
typename string_t,
template < typename, typename > typename map_t
>
struct ScanItemSetBase
{
// if not "" then relative item paths will be based on this:
string_t item_root_path;
// there may be far fewer unique commands than items:
map_t<cmd_idx_t, string_t> commands;
// if false: the item paths will be appeneded to the commands:
bool commands_contain_item_path = false;
// names used to distinguish mutiple configurations for the same file
map_t<target_idx_t, string_t> targets;
// what to scan:
// note: source files other than header units may appear in multiple targets
map_t<scan_item_idx_t, ScanItemBase<string_t>> items;
template<
typename other_string_t,
template < typename, typename > typename other_map_t
>
static auto from(const ScanItemSetBase<other_string_t, other_map_t>& other) {
ScanItemSetBase ret;
ret.item_root_path = other.item_root_path;
detail::conv(ret.commands, other.commands);
ret.commands_contain_item_path = other.commands_contain_item_path;
detail::conv(ret.targets, other.targets);
detail::conv(ret.items, other.items);
return ret;
}
};
ScanItemSet scan_item_set_from_comp_db(std::string_view comp_db_path, std::string_view item_root_path = "");
enum class ood_state {
unknown,
command_changed,
new_file,
file_changed,
deps_changed,
item_deps_changed,
up_to_date
};
enum class scan_state {
failed, // scanning failed
success // scanned successfully
};
struct DepInfoObserver {
struct RawDataBlockView {
std::string_view format;
tcb::span<int> code_units;
std::string_view readable_name;
};
struct IndexedStringView {
std::size_t idx;
std::string_view sv;
};
struct IndexedRawDataBlockView {
std::size_t idx;
RawDataBlockView data;
};
using DataBlockView = std::variant<std::string_view, IndexedStringView, IndexedRawDataBlockView, std::size_t, RawDataBlockView>;
// todo: maybe use a separate function_ref for each of these
// so the user can subscribe to only a subset of them ?
virtual void results_for_item(scan_item_idx_t /*item_idx*/, bool /*out_of_date*/) {}
virtual void export_module(DataBlockView /*name*/) {}
virtual void import_module(DataBlockView /*name*/) {}
virtual void include_header(DataBlockView /*path*/) {}
virtual void import_header(DataBlockView /*path*/) {}
virtual void other_file_dep(DataBlockView /*path*/) {}
virtual void item_finished() {}
};
struct CollatedModuleInfo
{
std::vector<scan_item_idx_t> imports_item_buf;
std::vector<char> modules_buf;
vector_map<scan_item_idx_t, std::string_view> exports;
vector_map<scan_item_idx_t, tcb::span<scan_item_idx_t>> imports_item;
bool collate_success = false;
};
// todo: refactor this
struct ModuleVisitor : public CollatedModuleInfo
{
std::vector<scan_item_idx_t> queue;
std::vector<bool> is_in_queue;
template<typename F>
void visit_transitive_imports(scan_item_idx_t root_idx, F&& visitor_func) {
queue.resize((std::size_t)imports_item.size());
if (queue.empty())
return;
is_in_queue.resize((std::size_t)imports_item.size());
queue[0] = root_idx;
is_in_queue[(std::size_t)root_idx] = true;
std::size_t s = 0, e = 0;
while (s <= e) {
auto idx = queue[s++];
if (idx != root_idx)
visitor_func(idx);
for (auto imp_idx : imports_item[idx]) {
if (!is_in_queue[(std::size_t)imp_idx]) {
is_in_queue[(std::size_t)imp_idx] = true;
queue[++e] = imp_idx;
}
}
}
is_in_queue.clear();
}
};
struct ScannerImpl;
class Scanner {
std::unique_ptr<ScannerImpl> impl;
public:
enum class Type {
CLANG_SCAN_DEPS
};
template<
typename string_t, // note: all of the input strings should be UTF-8
template < typename, typename > typename map_t
>
struct ConfigBase
{
// the type of scanner used:
Type tool_type = Type::CLANG_SCAN_DEPS;
// the path to the scanner tool to execute:
string_t tool_path;
// where to persist information for incremental scans:
// (this is intended to be the same for all targets)
string_t db_path;
// where to store other intermediate files unique to each (concurrently) scanned targets:
string_t int_dir;
// what to scan:
ScanItemSetBase<string_t, map_t> item_set;
// if false: must not invoke the tool on the same DB concurrently:
// if true: can use the same DB as long as all concurrent invocations are scanning different items // todo: too restrictive ?
bool concurrent_targets = true;
// if true: assume the stat times in the DB are up to date:
bool file_tracker_running = false;
// scan results are sent to this observer:
DepInfoObserver* observer = nullptr;
// submit scan results to the observer from previous scans for up-to-date items
bool submit_previous_results = false;
// the collated module dependency information will be stored here (if needed)
// note: requires submit_previous_results = true
CollatedModuleInfo* collated_results = nullptr;
template<
typename other_string_t,
template < typename, typename > typename other_map_t
>
static auto from(const ConfigBase<other_string_t, other_map_t>& conf) {
ConfigBase<string_t, map_t> ret;
ret.tool_type = conf.tool_type;
ret.tool_path = conf.tool_path;
ret.db_path = conf.db_path;
ret.int_dir = conf.int_dir;
ret.item_set = ScanItemSetBase<string_t, map_t>::from(conf.item_set);
ret.concurrent_targets = conf.concurrent_targets;
ret.file_tracker_running = conf.file_tracker_running;
ret.observer = conf.observer;
ret.submit_previous_results = conf.submit_previous_results;
ret.collated_results = conf.collated_results;
return ret;
}
};
using Config = ConfigBase<std::string, vector_map>;
using ConfigOwnedView = ConfigBase<std::string_view, vector_map>;
using ConfigView = ConfigBase<std::string_view, span_map>;
Scanner();
~Scanner();
struct Result {
ood_state ood;
scan_state scan;
};
vector_map<scan_item_idx_t, Result> scan(const ConfigView& config);
// clean the targets/items specified in the config's itemset
void clean(const ConfigView& config);
// clean everything in the db at the given path
void clean_all(std::string_view db_path);
// print everything in the db at the given path, for debugging
void print_db(std::string_view db_path);
};
} // namespace cppm