-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathswift-scan-test.cpp
246 lines (208 loc) · 8.36 KB
/
swift-scan-test.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//===--- swift-scan-test.cpp - Test libSwiftScan Dylib --------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// A simple program to test libSwiftScan interfaces.
//
//===----------------------------------------------------------------------===//
#include "swift-c/DependencyScan/DependencyScan.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/FileTypes.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/StringSaver.h"
#include "llvm/Support/ThreadPool.h"
using namespace llvm;
namespace {
enum Actions {
compute_cache_key,
compute_cache_key_from_index,
cache_query,
replay_result,
};
llvm::cl::OptionCategory Category("swift-scan-test Options");
llvm::cl::opt<std::string> CASPath("cas-path", llvm::cl::desc("<path>"),
llvm::cl::cat(Category));
llvm::cl::opt<std::string> CASID("id", llvm::cl::desc("<casid>"),
llvm::cl::cat(Category));
llvm::cl::opt<std::string> Input("input", llvm::cl::desc("<file|index>"),
llvm::cl::cat(Category));
llvm::cl::opt<unsigned> Threads("threads",
llvm::cl::desc("<number of threads>"),
llvm::cl::cat(Category), cl::init(1));
llvm::cl::opt<Actions>
Action("action", llvm::cl::desc("<action>"),
llvm::cl::values(clEnumVal(compute_cache_key, "compute cache key"),
clEnumVal(compute_cache_key_from_index,
"compute cache key from index"),
clEnumVal(cache_query, "cache query"),
clEnumVal(replay_result, "replay result")),
llvm::cl::cat(Category));
llvm::cl::list<std::string>
SwiftCommands(llvm::cl::Positional, llvm::cl::desc("<swift-frontend args>"),
llvm::cl::cat(Category));
} // namespace
static StringRef toString(swiftscan_string_ref_t str) {
return StringRef((const char *)str.data, str.length);
}
static int printError(swiftscan_string_ref_t err) {
llvm::errs() << toString(err) << "\n";
swiftscan_string_dispose(err);
return EXIT_FAILURE;
}
static int action_compute_cache_key(swiftscan_cas_t cas, StringRef input,
std::vector<const char *> &Args) {
if (input.empty()) {
llvm::errs() << "-input is not specified for compute_cache_key\n";
return EXIT_FAILURE;
}
swiftscan_string_ref_t err_msg;
auto key = swiftscan_cache_compute_key(cas, Args.size(), Args.data(),
input.str().c_str(), &err_msg);
if (key.length == 0)
return printError(err_msg);
llvm::outs() << toString(key) << "\n";
swiftscan_string_dispose(key);
return EXIT_SUCCESS;
}
static int
action_compute_cache_key_from_index(swiftscan_cas_t cas, StringRef index,
std::vector<const char *> &Args) {
unsigned inputIndex = 0;
if (!to_integer(index, inputIndex)) {
llvm::errs() << "-input is not a number for compute_cache_key_from_index\n";
return EXIT_FAILURE;
}
swiftscan_string_ref_t err_msg;
auto key = swiftscan_cache_compute_key_from_input_index(
cas, Args.size(), Args.data(), inputIndex, &err_msg);
if (key.length == 0)
return printError(err_msg);
llvm::outs() << toString(key) << "\n";
swiftscan_string_dispose(key);
return EXIT_SUCCESS;
}
static int print_cached_compilation(swiftscan_cached_compilation_t comp,
const char *key) {
auto numOutput = swiftscan_cached_compilation_get_num_outputs(comp);
llvm::outs() << "Cached Compilation for key \"" << key << "\" has "
<< numOutput << " outputs: \n";
for (unsigned i = 0; i < numOutput; ++i) {
swiftscan_cached_output_t out =
swiftscan_cached_compilation_get_output(comp, i);
swiftscan_string_ref_t id = swiftscan_cached_output_get_casid(out);
swiftscan_string_ref_t kind = swiftscan_cached_output_get_name(out);
SWIFT_DEFER { swiftscan_string_dispose(kind); };
llvm::outs() << toString(kind) << ": " << toString(id) << "\n";
swiftscan_string_dispose(id);
}
llvm::outs() << "\n";
return EXIT_SUCCESS;
}
static int action_cache_query(swiftscan_cas_t cas, const char *key) {
swiftscan_string_ref_t err_msg;
auto comp = swiftscan_cache_query(cas, key, /*globally=*/false, &err_msg);
if (err_msg.length != 0)
return printError(err_msg);
if (!comp) {
llvm::errs() << "cached output not found for \"" << key << "\n";
return EXIT_FAILURE;
}
SWIFT_DEFER { swiftscan_cached_compilation_dispose(comp); };
return print_cached_compilation(comp, key);
}
static int action_replay_result(swiftscan_cas_t cas, const char *key,
std::vector<const char *> &Args) {
swiftscan_string_ref_t err_msg;
auto comp = swiftscan_cache_query(cas, key, /*globally=*/false, &err_msg);
if (!comp)
return printError(err_msg);
SWIFT_DEFER { swiftscan_cached_compilation_dispose(comp); };
auto numOutput = swiftscan_cached_compilation_get_num_outputs(comp);
for (unsigned i = 0; i < numOutput; ++i) {
auto output = swiftscan_cached_compilation_get_output(comp, i);
SWIFT_DEFER { swiftscan_cached_output_dispose(output); };
if (swiftscan_cached_output_is_materialized(output))
continue;
auto load = swiftscan_cached_output_load(output, &err_msg);
if (err_msg.length != 0)
return printError(err_msg);
if (!load) {
llvm::errs() << "output at index " << i
<< " cannot be loaded for key: " << key << "\n";
return EXIT_FAILURE;
}
}
auto instance = swiftscan_cache_replay_instance_create(Args.size(),
Args.data(), &err_msg);
if (!instance)
return printError(err_msg);
SWIFT_DEFER { swiftscan_cache_replay_instance_dispose(instance); };
auto result = swiftscan_cache_replay_compilation(instance, comp, &err_msg);
if (err_msg.length != 0)
return printError(err_msg);
SWIFT_DEFER { swiftscan_cache_replay_result_dispose(result); };
llvm::outs() << toString(swiftscan_cache_replay_result_get_stdout(result));
llvm::errs() << toString(swiftscan_cache_replay_result_get_stderr(result));
return EXIT_SUCCESS;
}
static std::vector<const char *> createArgs(ArrayRef<std::string> Cmd,
StringSaver &Saver) {
if (!Cmd.empty() && StringRef(Cmd.front()).endswith("swift-frontend"))
Cmd = Cmd.drop_front();
std::vector<const char *> Args;
for (auto A : Cmd) {
StringRef Arg = Saver.save(A);
Args.push_back(Arg.data());
}
return Args;
}
int main(int argc, char *argv[]) {
llvm::cl::HideUnrelatedOptions(Category);
llvm::cl::ParseCommandLineOptions(argc, argv,
"Test libSwiftScan interfaces\n");
// Create CAS.
auto option = swiftscan_cas_options_create();
SWIFT_DEFER { swiftscan_cas_options_dispose(option); };
swiftscan_cas_options_set_ondisk_path(option, CASPath.c_str());
swiftscan_string_ref_t err_msg;
auto cas = swiftscan_cas_create_from_options(option, &err_msg);
if (!cas)
return printError(err_msg);
SWIFT_DEFER { swiftscan_cas_dispose(cas); };
// Convert commands.
llvm::BumpPtrAllocator Alloc;
llvm::StringSaver Saver(Alloc);
auto Args = createArgs(SwiftCommands, Saver);
std::atomic<int> Ret = 0;
llvm::ThreadPool Pool(llvm::hardware_concurrency(Threads));
for (unsigned i = 0; i < Threads; ++i) {
Pool.async([&]() {
switch (Action) {
case compute_cache_key:
Ret += action_compute_cache_key(cas, Input, Args);
break;
case compute_cache_key_from_index:
Ret += action_compute_cache_key_from_index(cas, Input, Args);
break;
case cache_query:
Ret += action_cache_query(cas, CASID.c_str());
break;
case replay_result:
Ret += action_replay_result(cas, CASID.c_str(), Args);
break;
}
});
}
Pool.wait();
return Ret;
}