Skip to content

Commit 108e9da

Browse files
committed
[Dependency Scanning] Make scanner invocation context (argc, argv, working_dir) an opaque type
1 parent cff741b commit 108e9da

File tree

3 files changed

+85
-20
lines changed

3 files changed

+85
-20
lines changed

include/swift-c/DependencyScan/DependencyScan.h

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ typedef struct {
7474
swiftscan_dependency_result_t *results;
7575
} swiftscan_batch_scan_result_t;
7676

77+
//=== Scanner Invocation Specification ------------------------------------===//
78+
79+
/// Opaque container of all relevant context required to launch a dependency
80+
/// scan (command line arguments, working directory, etc.)
81+
typedef void *swiftscan_scan_invocation_t;
82+
7783
//=== Dependency Result Functions -----------------------------------------===//
7884

7985
SWIFTSCAN_PUBLIC swiftscan_string_t
@@ -197,6 +203,18 @@ swiftscan_batch_scan_entry_get_is_swift(swiftscan_batch_scan_entry_t entry);
197203
SWIFTSCAN_PUBLIC swiftscan_string_set_t *
198204
swiftscan_prescan_result_get_import_set(swiftscan_prescan_result_t result);
199205

206+
//=== Scanner Invocation Functions ----------------------------------------===//
207+
208+
SWIFTSCAN_PUBLIC swiftscan_string_t
209+
swiftscan_scan_invocation_get_working_directory(
210+
swiftscan_scan_invocation_t invocation);
211+
212+
SWIFTSCAN_PUBLIC int
213+
swiftscan_scan_invocation_get_argc(swiftscan_scan_invocation_t invocation);
214+
215+
SWIFTSCAN_PUBLIC swiftscan_string_set_t *
216+
swiftscan_scan_invocation_get_argv(swiftscan_scan_invocation_t invocation);
217+
200218
//=== Cleanup Functions ---------------------------------------------------===//
201219

202220
SWIFTSCAN_PUBLIC void
@@ -223,6 +241,9 @@ swiftscan_batch_scan_input_dispose(swiftscan_batch_scan_input_t *input);
223241
SWIFTSCAN_PUBLIC void
224242
swiftscan_batch_scan_result_dispose(swiftscan_batch_scan_result_t *result);
225243

244+
SWIFTSCAN_PUBLIC void
245+
swiftscan_scan_invocation_dispose(swiftscan_scan_invocation_t invocation);
246+
226247
//=== Scanner Functions ---------------------------------------------------===//
227248

228249
/// Container of the configuration state and shared cache for dependency
@@ -233,21 +254,16 @@ SWIFTSCAN_PUBLIC swiftscan_scanner_t swiftscan_scanner_create(void);
233254

234255
SWIFTSCAN_PUBLIC void swiftscan_scanner_dispose(swiftscan_scanner_t);
235256

236-
SWIFTSCAN_PUBLIC swiftscan_dependency_result_t
237-
swiftscan_scan_dependencies(swiftscan_scanner_t *scanner,
238-
const char *working_directory, int argc,
239-
const char *const *argv);
257+
SWIFTSCAN_PUBLIC swiftscan_dependency_result_t swiftscan_scan_dependencies(
258+
swiftscan_scanner_t scanner, swiftscan_scan_invocation_t invocation);
240259

241260
SWIFTSCAN_PUBLIC swiftscan_batch_scan_result_t *
242-
swiftscan_batch_scan_dependencies(swiftscan_scanner_t *scanner,
243-
const char *working_directory,
261+
swiftscan_batch_scan_dependencies(swiftscan_scanner_t scanner,
244262
swiftscan_batch_scan_input_t *batch_input,
245-
int argc, const char *const *argv);
263+
swiftscan_scan_invocation_t invocation);
246264

247-
SWIFTSCAN_PUBLIC swiftscan_prescan_result_t
248-
swiftscan_prescan_dependencies(swiftscan_scanner_t *scanner,
249-
const char *working_directory, int argc,
250-
const char *const *argv);
265+
SWIFTSCAN_PUBLIC swiftscan_prescan_result_t swiftscan_prescan_dependencies(
266+
swiftscan_scanner_t scanner, swiftscan_scan_invocation_t invocation);
251267

252268
SWIFTSCAN_END_DECLS
253269

include/swift/DependencyScan/DependencyScanImpl.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ typedef struct {
146146
swiftscan_string_set_t *import_set;
147147
} swiftscan_impl_prescan_result_t;
148148

149+
typedef struct {
150+
swiftscan_string_t working_directory;
151+
swiftscan_string_set_t *argv;
152+
} swiftscan_impl_scan_invocation_t;
153+
149154
inline swift::dependencies::DependencyScanningTool *
150155
unwrap_scanner(swiftscan_scanner_t P) {
151156
return reinterpret_cast<swift::dependencies::DependencyScanningTool *>(P);
@@ -212,4 +217,15 @@ wrap_batch_entry(const swiftscan_impl_batch_scan_entry_t *P) {
212217
const_cast<swiftscan_impl_batch_scan_entry_t *>(P));
213218
}
214219

220+
inline swiftscan_impl_scan_invocation_t *
221+
unwrap_scan_invocation(swiftscan_scan_invocation_t P) {
222+
return reinterpret_cast<swiftscan_impl_scan_invocation_t *>(P);
223+
}
224+
225+
inline swiftscan_batch_scan_entry_t
226+
wrap_scan_invocation(const swiftscan_impl_scan_invocation_t *P) {
227+
return reinterpret_cast<swiftscan_scan_invocation_t>(
228+
const_cast<swiftscan_impl_scan_invocation_t *>(P));
229+
}
230+
215231
#endif // SWIFT_C_DEPENDENCY_SCAN_IMPL_H

lib/DependencyScan/DependencyScanImpl.cpp

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@ void swiftscan_scanner_dispose(swiftscan_scanner_t c_scanner) {
3030

3131
swiftscan_dependency_result_t
3232
swiftscan_scan_dependencies(swiftscan_scanner_t *scanner,
33-
const char *working_directory, int argc,
34-
const char *const *argv) {
33+
swiftscan_scan_invocation_t invocation) {
3534
DependencyScanningTool *ScanningTool = unwrap_scanner(scanner);
35+
swiftscan_impl_scan_invocation_t *InvocationImpl =
36+
unwrap_scan_invocation(invocation);
37+
int argc = InvocationImpl->argv->count;
3638
std::vector<const char *> Compilation;
3739
for (int i = 0; i < argc; ++i)
38-
Compilation.push_back(argv[i]);
40+
Compilation.push_back(
41+
swiftscan_get_C_string(InvocationImpl->argv->strings[i]));
3942

4043
// Execute the scan and bridge the result
4144
auto ScanResult = ScanningTool->getDependencies(Compilation, {});
@@ -47,13 +50,16 @@ swiftscan_scan_dependencies(swiftscan_scanner_t *scanner,
4750

4851
swiftscan_batch_scan_result_t *
4952
swiftscan_batch_scan_dependencies(swiftscan_scanner_t *scanner,
50-
const char *working_directory,
5153
swiftscan_batch_scan_input_t *batch_input,
52-
int argc, const char *const *argv) {
54+
swiftscan_scan_invocation_t invocation) {
5355
DependencyScanningTool *ScanningTool = unwrap_scanner(scanner);
56+
swiftscan_impl_scan_invocation_t *InvocationImpl =
57+
unwrap_scan_invocation(invocation);
58+
int argc = InvocationImpl->argv->count;
5459
std::vector<const char *> Compilation;
5560
for (int i = 0; i < argc; ++i)
56-
Compilation.push_back(argv[i]);
61+
Compilation.push_back(
62+
swiftscan_get_C_string(InvocationImpl->argv->strings[i]));
5763

5864
std::vector<BatchScanInput> BatchInput;
5965
for (int i = 0; i < batch_input->count; ++i) {
@@ -83,12 +89,15 @@ swiftscan_batch_scan_dependencies(swiftscan_scanner_t *scanner,
8389

8490
swiftscan_prescan_result_t
8591
swiftscan_prescan_dependencies(swiftscan_scanner_t *scanner,
86-
const char *working_directory, int argc,
87-
const char *const *argv) {
92+
swiftscan_scan_invocation_t invocation) {
8893
DependencyScanningTool *ScanningTool = unwrap_scanner(scanner);
94+
swiftscan_impl_scan_invocation_t *InvocationImpl =
95+
unwrap_scan_invocation(invocation);
96+
int argc = InvocationImpl->argv->count;
8997
std::vector<const char *> Compilation;
9098
for (int i = 0; i < argc; ++i)
91-
Compilation.push_back(argv[i]);
99+
Compilation.push_back(
100+
swiftscan_get_C_string(InvocationImpl->argv->strings[i]));
92101

93102
// Execute the scan and bridge the result
94103
auto PreScanResult = ScanningTool->getImports(Compilation);
@@ -261,6 +270,22 @@ swiftscan_prescan_result_get_import_set(swiftscan_prescan_result_t result) {
261270
return unwrap_prescan_result(result)->import_set;
262271
}
263272

273+
//=== Scanner Invocation Functions ----------------------------------------===//
274+
275+
swiftscan_string_t swiftscan_scan_invocation_get_working_directory(
276+
swiftscan_scan_invocation_t invocation) {
277+
return unwrap_scan_invocation(invocation)->working_directory;
278+
}
279+
280+
int swiftscan_scan_invocation_get_argc(swiftscan_scan_invocation_t invocation) {
281+
return unwrap_scan_invocation(invocation)->argv->count;
282+
}
283+
284+
swiftscan_string_set_t *
285+
swiftscan_scan_invocation_get_argv(swiftscan_scan_invocation_t invocation) {
286+
return unwrap_scan_invocation(invocation)->argv;
287+
}
288+
264289
//=== Cleanup Functions ---------------------------------------------------===//
265290

266291
void swiftscan_dependency_info_details_dispose(
@@ -363,3 +388,11 @@ void swiftscan_batch_scan_result_dispose(
363388
delete[] result->results;
364389
delete result;
365390
}
391+
392+
void swiftscan_scan_invocation_dispose(swiftscan_scan_invocation_t invocation) {
393+
swiftscan_impl_scan_invocation_t *invocation_impl =
394+
unwrap_scan_invocation(invocation);
395+
swiftscan_string_dispose(invocation_impl->working_directory);
396+
swiftscan_string_set_dispose(invocation_impl->argv);
397+
delete invocation_impl;
398+
}

0 commit comments

Comments
 (0)