forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibSwiftScan.cpp
844 lines (705 loc) · 29.1 KB
/
libSwiftScan.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
//===------------ DependencyScanImpl.cpp - Swift Compiler -----------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 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
//
//===----------------------------------------------------------------------===//
//
// Implementation of the dependency scanning C API
//
//===----------------------------------------------------------------------===//
#include "swift/Basic/InitializeSwiftModules.h"
#include "swift/Basic/LLVMInitialize.h"
#include "swift/DependencyScan/DependencyScanImpl.h"
#include "swift/DependencyScan/DependencyScanningTool.h"
#include "swift/DependencyScan/StringUtils.h"
#include "swift/DriverTool/DriverTool.h"
#include "swift/Frontend/CompileJobCacheKey.h"
#include "swift/Option/Options.h"
#include "clang/CAS/CASOptions.h"
#include "llvm/CAS/ActionCache.h"
#include "llvm/CAS/BuiltinUnifiedCASDatabases.h"
#include "llvm/CAS/ObjectStore.h"
#include "llvm/Support/Error.h"
#include <memory>
using namespace swift::dependencies;
namespace {
/// Helper class to manage CAS/Caching from libSwiftScan C APIs.
class SwiftScanCAS {
public:
// Compute the CASID for PCH output from invocation.
llvm::Expected<std::string> computeCacheKey(llvm::ArrayRef<const char *> Args,
llvm::StringRef InputPath,
swift::file_types::ID OutputKind);
// Store content into CAS.
llvm::Expected<std::string> storeContent(llvm::StringRef Content);
// Construct SwiftScanCAS.
static llvm::Expected<SwiftScanCAS *>
createSwiftScanCAS(llvm::StringRef Path);
static llvm::Expected<SwiftScanCAS *>
createSwiftScanCAS(clang::CASOptions &CASOpts);
private:
SwiftScanCAS(std::shared_ptr<llvm::cas::ObjectStore> CAS,
std::shared_ptr<llvm::cas::ActionCache> Cache)
: CAS(CAS), Cache(Cache) {}
std::shared_ptr<llvm::cas::ObjectStore> CAS;
std::shared_ptr<llvm::cas::ActionCache> Cache;
};
} // namespace
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DependencyScanningTool, swiftscan_scanner_t)
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(clang::CASOptions, swiftscan_cas_options_t)
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(SwiftScanCAS, swiftscan_cas_t)
//=== Private Cleanup Functions -------------------------------------------===//
void swiftscan_dependency_info_details_dispose(
swiftscan_module_details_t details) {
swiftscan_module_details_s *details_impl = details;
switch (details_impl->kind) {
case SWIFTSCAN_DEPENDENCY_INFO_SWIFT_TEXTUAL:
swiftscan_string_dispose(
details_impl->swift_textual_details.module_interface_path);
swiftscan_string_set_dispose(
details_impl->swift_textual_details.compiled_module_candidates);
swiftscan_string_dispose(
details_impl->swift_textual_details.bridging_header_path);
swiftscan_string_set_dispose(
details_impl->swift_textual_details.bridging_source_files);
swiftscan_string_set_dispose(
details_impl->swift_textual_details.bridging_module_dependencies);
swiftscan_string_set_dispose(
details_impl->swift_textual_details.command_line);
swiftscan_string_set_dispose(
details_impl->swift_textual_details.extra_pcm_args);
swiftscan_string_dispose(details_impl->swift_textual_details.context_hash);
swiftscan_string_dispose(
details_impl->swift_textual_details.cas_fs_root_id);
swiftscan_string_dispose(
details_impl->swift_textual_details.bridging_header_include_tree);
swiftscan_string_dispose(
details_impl->swift_textual_details.module_cache_key);
break;
case SWIFTSCAN_DEPENDENCY_INFO_SWIFT_BINARY:
swiftscan_string_dispose(
details_impl->swift_binary_details.compiled_module_path);
swiftscan_string_dispose(
details_impl->swift_binary_details.module_doc_path);
swiftscan_string_dispose(
details_impl->swift_binary_details.module_source_info_path);
swiftscan_string_dispose(
details_impl->swift_binary_details.module_cache_key);
break;
case SWIFTSCAN_DEPENDENCY_INFO_SWIFT_PLACEHOLDER:
swiftscan_string_dispose(
details_impl->swift_placeholder_details.compiled_module_path);
swiftscan_string_dispose(
details_impl->swift_placeholder_details.module_doc_path);
swiftscan_string_dispose(
details_impl->swift_placeholder_details.module_source_info_path);
break;
case SWIFTSCAN_DEPENDENCY_INFO_CLANG:
swiftscan_string_dispose(details_impl->clang_details.module_map_path);
swiftscan_string_dispose(details_impl->clang_details.context_hash);
swiftscan_string_set_dispose(details_impl->clang_details.command_line);
swiftscan_string_set_dispose(details_impl->clang_details.captured_pcm_args);
swiftscan_string_dispose(details_impl->clang_details.cas_fs_root_id);
swiftscan_string_dispose(details_impl->clang_details.module_cache_key);
break;
}
delete details_impl;
}
void swiftscan_dependency_info_dispose(swiftscan_dependency_info_t info) {
swiftscan_string_dispose(info->module_name);
swiftscan_string_dispose(info->module_path);
swiftscan_string_set_dispose(info->source_files);
swiftscan_string_set_dispose(info->direct_dependencies);
swiftscan_dependency_info_details_dispose(info->details);
delete info;
}
void swiftscan_dependency_set_dispose(swiftscan_dependency_set_t *set) {
for (size_t i = 0; i < set->count; ++i) {
swiftscan_dependency_info_dispose(set->modules[i]);
}
delete[] set->modules;
delete set;
}
//=== Scanner Cache Operations --------------------------------------------===//
void swiftscan_scanner_cache_serialize(swiftscan_scanner_t scanner,
const char * path) {
DependencyScanningTool *ScanningTool = unwrap(scanner);
ScanningTool->serializeCache(path);
}
bool swiftscan_scanner_cache_load(swiftscan_scanner_t scanner,
const char * path) {
DependencyScanningTool *ScanningTool = unwrap(scanner);
return ScanningTool->loadCache(path);
}
void swiftscan_scanner_cache_reset(swiftscan_scanner_t scanner) {
DependencyScanningTool *ScanningTool = unwrap(scanner);
ScanningTool->resetCache();
}
//=== Scanner Functions ---------------------------------------------------===//
swiftscan_scanner_t swiftscan_scanner_create(void) {
INITIALIZE_LLVM();
// We must initialize the swift modules responsible for parsing functionality,
// such as parsing regex.
initializeSwiftParseModules();
return wrap(new DependencyScanningTool());
}
void swiftscan_scanner_dispose(swiftscan_scanner_t c_scanner) {
delete unwrap(c_scanner);
}
swiftscan_dependency_graph_t
swiftscan_dependency_graph_create(swiftscan_scanner_t scanner,
swiftscan_scan_invocation_t invocation) {
DependencyScanningTool *ScanningTool = unwrap(scanner);
int argc = invocation->argv->count;
std::vector<const char *> Compilation;
for (int i = 0; i < argc; ++i)
Compilation.push_back(swift::c_string_utils::get_C_string(invocation->argv->strings[i]));
// Execute the scan and bridge the result
auto ScanResult = ScanningTool->getDependencies(Compilation, {});
if (ScanResult.getError())
return nullptr;
auto DependencyGraph = std::move(*ScanResult);
return DependencyGraph;
}
swiftscan_batch_scan_result_t *
swiftscan_batch_scan_result_create(swiftscan_scanner_t scanner,
swiftscan_batch_scan_input_t *batch_input,
swiftscan_scan_invocation_t invocation) {
DependencyScanningTool *ScanningTool = unwrap(scanner);
int argc = invocation->argv->count;
std::vector<const char *> Compilation;
for (int i = 0; i < argc; ++i)
Compilation.push_back(swift::c_string_utils::get_C_string(invocation->argv->strings[i]));
std::vector<BatchScanInput> BatchInput;
for (size_t i = 0; i < batch_input->count; ++i) {
swiftscan_batch_scan_entry_s *Entry = batch_input->modules[i];
BatchInput.push_back({swift::c_string_utils::get_C_string(Entry->module_name),
swift::c_string_utils::get_C_string(Entry->arguments),
/*outputPath*/ "", Entry->is_swift});
}
// Execute the scan and bridge the result
auto BatchScanResult =
ScanningTool->getDependencies(Compilation, BatchInput, {});
swiftscan_batch_scan_result_t *Result = new swiftscan_batch_scan_result_t;
auto ResultGraphs = new swiftscan_dependency_graph_t[BatchScanResult.size()];
for (size_t i = 0; i < BatchScanResult.size(); ++i) {
auto &ResultOrErr = BatchScanResult[i];
if (ResultOrErr.getError()) {
ResultGraphs[i] = nullptr;
continue;
}
ResultGraphs[i] = ResultOrErr.get();
}
Result->results = ResultGraphs;
Result->count = BatchScanResult.size();
return Result;
}
swiftscan_import_set_t
swiftscan_import_set_create(swiftscan_scanner_t scanner,
swiftscan_scan_invocation_t invocation) {
DependencyScanningTool *ScanningTool = unwrap(scanner);
int argc = invocation->argv->count;
std::vector<const char *> Compilation;
for (int i = 0; i < argc; ++i)
Compilation.push_back(swift::c_string_utils::get_C_string(invocation->argv->strings[i]));
// Execute the scan and bridge the result
auto PreScanResult = ScanningTool->getImports(Compilation);
if (PreScanResult.getError())
return nullptr;
auto ImportSet = std::move(*PreScanResult);
return ImportSet;
}
//=== Dependency Result Functions -----------------------------------------===//
swiftscan_string_ref_t swiftscan_dependency_graph_get_main_module_name(
swiftscan_dependency_graph_t result) {
return result->main_module_name;
}
swiftscan_dependency_set_t *swiftscan_dependency_graph_get_dependencies(
swiftscan_dependency_graph_t result) {
return result->dependencies;
}
//=== Module Dependency Info query APIs -----------------------------------===//
swiftscan_string_ref_t
swiftscan_module_info_get_module_name(swiftscan_dependency_info_t info) {
return info->module_name;
}
swiftscan_string_ref_t
swiftscan_module_info_get_module_path(swiftscan_dependency_info_t info) {
return info->module_path;
}
swiftscan_string_set_t *
swiftscan_module_info_get_source_files(swiftscan_dependency_info_t info) {
return info->source_files;
}
swiftscan_string_set_t *swiftscan_module_info_get_direct_dependencies(
swiftscan_dependency_info_t info) {
return info->direct_dependencies;
}
swiftscan_module_details_t
swiftscan_module_info_get_details(swiftscan_dependency_info_t info) {
return info->details;
}
//=== Swift Textual Module Details query APIs -----------------------------===//
swiftscan_dependency_info_kind_t
swiftscan_module_detail_get_kind(swiftscan_module_details_t details) {
return details->kind;
}
swiftscan_string_ref_t swiftscan_swift_textual_detail_get_module_interface_path(
swiftscan_module_details_t details) {
return details->swift_textual_details.module_interface_path;
}
swiftscan_string_set_t *
swiftscan_swift_textual_detail_get_compiled_module_candidates(
swiftscan_module_details_t details) {
return details->swift_textual_details.compiled_module_candidates;
}
swiftscan_string_ref_t swiftscan_swift_textual_detail_get_bridging_header_path(
swiftscan_module_details_t details) {
return details->swift_textual_details.bridging_header_path;
}
swiftscan_string_set_t *
swiftscan_swift_textual_detail_get_bridging_source_files(
swiftscan_module_details_t details) {
return details->swift_textual_details.bridging_source_files;
}
swiftscan_string_set_t *
swiftscan_swift_textual_detail_get_bridging_module_dependencies(
swiftscan_module_details_t details) {
return details->swift_textual_details.bridging_module_dependencies;
}
swiftscan_string_set_t *swiftscan_swift_textual_detail_get_command_line(
swiftscan_module_details_t details) {
return details->swift_textual_details.command_line;
}
swiftscan_string_set_t *
swiftscan_swift_textual_detail_get_bridging_pch_command_line(
swiftscan_module_details_t details) {
return details->swift_textual_details.bridging_pch_command_line;
}
swiftscan_string_set_t *swiftscan_swift_textual_detail_get_extra_pcm_args(
swiftscan_module_details_t details) {
return details->swift_textual_details.extra_pcm_args;
}
swiftscan_string_ref_t swiftscan_swift_textual_detail_get_context_hash(
swiftscan_module_details_t details) {
return details->swift_textual_details.context_hash;
}
bool swiftscan_swift_textual_detail_get_is_framework(
swiftscan_module_details_t details) {
return details->swift_textual_details.is_framework;
}
swiftscan_string_set_t *swiftscan_swift_textual_detail_get_swift_overlay_dependencies(
swiftscan_module_details_t details) {
return details->swift_textual_details.swift_overlay_module_dependencies;
}
swiftscan_string_ref_t swiftscan_swift_textual_detail_get_cas_fs_root_id(
swiftscan_module_details_t details) {
return details->swift_textual_details.cas_fs_root_id;
}
swiftscan_string_ref_t swiftscan_swift_textual_detail_get_module_cache_key(
swiftscan_module_details_t details) {
return details->swift_textual_details.module_cache_key;
}
//=== Swift Binary Module Details query APIs ------------------------------===//
swiftscan_string_ref_t swiftscan_swift_binary_detail_get_compiled_module_path(
swiftscan_module_details_t details) {
return details->swift_binary_details.compiled_module_path;
}
swiftscan_string_ref_t swiftscan_swift_binary_detail_get_module_doc_path(
swiftscan_module_details_t details) {
return details->swift_binary_details.module_doc_path;
}
swiftscan_string_ref_t
swiftscan_swift_binary_detail_get_module_source_info_path(
swiftscan_module_details_t details) {
return details->swift_binary_details.module_source_info_path;
}
swiftscan_string_set_t *
swiftscan_swift_binary_detail_get_header_dependencies(
swiftscan_module_details_t details) {
return details->swift_binary_details.header_dependencies;
}
bool swiftscan_swift_binary_detail_get_is_framework(
swiftscan_module_details_t details) {
return details->swift_binary_details.is_framework;
}
swiftscan_string_ref_t swiftscan_swift_binary_detail_get_module_cache_key(
swiftscan_module_details_t details) {
return details->swift_binary_details.module_cache_key;
}
//=== Swift Placeholder Module Details query APIs -------------------------===//
swiftscan_string_ref_t
swiftscan_swift_placeholder_detail_get_compiled_module_path(
swiftscan_module_details_t details) {
return details->swift_placeholder_details.module_source_info_path;
}
swiftscan_string_ref_t swiftscan_swift_placeholder_detail_get_module_doc_path(
swiftscan_module_details_t details) {
return details->swift_placeholder_details.module_source_info_path;
}
swiftscan_string_ref_t
swiftscan_swift_placeholder_detail_get_module_source_info_path(
swiftscan_module_details_t details) {
return details->swift_placeholder_details.module_source_info_path;
}
//=== Clang Module Details query APIs -------------------------------------===//
swiftscan_string_ref_t
swiftscan_clang_detail_get_module_map_path(swiftscan_module_details_t details) {
return details->clang_details.module_map_path;
}
swiftscan_string_ref_t
swiftscan_clang_detail_get_context_hash(swiftscan_module_details_t details) {
return details->clang_details.context_hash;
}
swiftscan_string_set_t *
swiftscan_clang_detail_get_command_line(swiftscan_module_details_t details) {
return details->clang_details.command_line;
}
swiftscan_string_set_t *
swiftscan_clang_detail_get_captured_pcm_args(swiftscan_module_details_t details) {
return details->clang_details.captured_pcm_args;
}
swiftscan_string_ref_t
swiftscan_clang_detail_get_cas_fs_root_id(swiftscan_module_details_t details) {
return details->clang_details.cas_fs_root_id;
}
swiftscan_string_ref_t swiftscan_clang_detail_get_module_cache_key(
swiftscan_module_details_t details) {
return details->clang_details.module_cache_key;
}
//=== Batch Scan Input Functions ------------------------------------------===//
swiftscan_batch_scan_input_t *swiftscan_batch_scan_input_create() {
return new swiftscan_batch_scan_input_t;
}
void swiftscan_batch_scan_input_set_modules(
swiftscan_batch_scan_input_t *input, int count,
swiftscan_batch_scan_entry_t *modules) {
input->count = count;
input->modules = modules;
}
//=== Batch Scan Entry Functions ------------------------------------------===//
swiftscan_batch_scan_entry_t swiftscan_batch_scan_entry_create() {
return new swiftscan_batch_scan_entry_s;
}
void swiftscan_batch_scan_entry_set_module_name(
swiftscan_batch_scan_entry_t entry, const char *name) {
entry->module_name = swift::c_string_utils::create_clone(name);
}
void swiftscan_batch_scan_entry_set_arguments(
swiftscan_batch_scan_entry_t entry, const char *arguments) {
entry->arguments = swift::c_string_utils::create_clone(arguments);
}
void swiftscan_batch_scan_entry_set_is_swift(swiftscan_batch_scan_entry_t entry,
bool is_swift) {
entry->is_swift = is_swift;
}
swiftscan_string_ref_t
swiftscan_batch_scan_entry_get_module_name(swiftscan_batch_scan_entry_t entry) {
return entry->module_name;
}
swiftscan_string_ref_t
swiftscan_batch_scan_entry_get_arguments(swiftscan_batch_scan_entry_t entry) {
return entry->arguments;
}
bool swiftscan_batch_scan_entry_get_is_swift(
swiftscan_batch_scan_entry_t entry) {
return entry->is_swift;
}
//=== Prescan Result Functions --------------------------------------------===//
swiftscan_string_set_t *
swiftscan_import_set_get_imports(swiftscan_import_set_t result) {
return result->imports;
}
//=== Scanner Invocation Functions ----------------------------------------===//
swiftscan_scan_invocation_t swiftscan_scan_invocation_create() {
return new swiftscan_scan_invocation_s;
}
void swiftscan_scan_invocation_set_working_directory(
swiftscan_scan_invocation_t invocation, const char *working_directory) {
invocation->working_directory = swift::c_string_utils::create_clone(working_directory);
}
SWIFTSCAN_PUBLIC void
swiftscan_scan_invocation_set_argv(swiftscan_scan_invocation_t invocation,
int argc, const char **argv) {
invocation->argv = swift::c_string_utils::create_set(argc, argv);
}
swiftscan_string_ref_t swiftscan_scan_invocation_get_working_directory(
swiftscan_scan_invocation_t invocation) {
return invocation->working_directory;
}
int swiftscan_scan_invocation_get_argc(swiftscan_scan_invocation_t invocation) {
return invocation->argv->count;
}
swiftscan_string_set_t *
swiftscan_scan_invocation_get_argv(swiftscan_scan_invocation_t invocation) {
return invocation->argv;
}
//=== Public Cleanup Functions --------------------------------------------===//
void swiftscan_string_dispose(swiftscan_string_ref_t string) {
if (string.data)
free(const_cast<void *>(string.data));
}
void swiftscan_string_set_dispose(swiftscan_string_set_t *set) {
for (unsigned SI = 0, SE = set->count; SI < SE; ++SI)
swiftscan_string_dispose(set->strings[SI]);
if (set->count > 0)
delete[] set->strings;
delete set;
}
void swiftscan_dependency_graph_dispose(swiftscan_dependency_graph_t result) {
swiftscan_string_dispose(result->main_module_name);
swiftscan_dependency_set_dispose(result->dependencies);
delete result;
}
void swiftscan_import_set_dispose(swiftscan_import_set_t result) {
swiftscan_string_set_dispose(result->imports);
delete result;
}
void swiftscan_batch_scan_entry_dispose(swiftscan_batch_scan_entry_t entry) {
swiftscan_string_dispose(entry->module_name);
swiftscan_string_dispose(entry->arguments);
delete entry;
}
void swiftscan_batch_scan_input_dispose(swiftscan_batch_scan_input_t *input) {
for (size_t i = 0; i < input->count; ++i) {
swiftscan_batch_scan_entry_dispose(input->modules[i]);
}
delete[] input->modules;
delete input;
}
void swiftscan_batch_scan_result_dispose(
swiftscan_batch_scan_result_t *result) {
for (size_t i = 0; i < result->count; ++i) {
swiftscan_dependency_graph_dispose(result->results[i]);
}
delete[] result->results;
delete result;
}
void swiftscan_scan_invocation_dispose(swiftscan_scan_invocation_t invocation) {
swiftscan_string_dispose(invocation->working_directory);
swiftscan_string_set_dispose(invocation->argv);
delete invocation;
}
//=== Feature-Query Functions -----------------------------------------===//
static void addFrontendFlagOption(llvm::opt::OptTable &table,
swift::options::ID id,
std::vector<std::string> &frontendOptions) {
if (table.getOption(id).hasFlag(swift::options::FrontendOption)) {
auto name = table.getOptionName(id);
if (strlen(name) > 0) {
frontendOptions.push_back(std::string(name));
}
}
}
swiftscan_string_ref_t
swiftscan_compiler_target_info_query(swiftscan_scan_invocation_t invocation) {
return swiftscan_compiler_target_info_query_v2(invocation, nullptr);
}
swiftscan_string_ref_t
swiftscan_compiler_target_info_query_v2(swiftscan_scan_invocation_t invocation,
const char *main_executable_path) {
int argc = invocation->argv->count;
std::vector<const char *> Compilation;
for (int i = 0; i < argc; ++i)
Compilation.push_back(swift::c_string_utils::get_C_string(invocation->argv->strings[i]));
auto TargetInfo = swift::dependencies::getTargetInfo(Compilation, main_executable_path);
if (TargetInfo.getError())
return swift::c_string_utils::create_null();
return TargetInfo.get();
}
swiftscan_string_set_t *
swiftscan_compiler_supported_arguments_query() {
std::unique_ptr<llvm::opt::OptTable> table = swift::createSwiftOptTable();
std::vector<std::string> frontendFlags;
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
HELPTEXT, METAVAR, VALUES) \
addFrontendFlagOption(*table, swift::options::OPT_##ID, frontendFlags);
#include "swift/Option/Options.inc"
#undef OPTION
return swift::c_string_utils::create_set(frontendFlags);
}
swiftscan_string_set_t *
swiftscan_compiler_supported_features_query() {
std::vector<std::string> allFeatures;
allFeatures.emplace_back("library-level");
allFeatures.emplace_back("emit-abi-descriptor");
return swift::c_string_utils::create_set(allFeatures);
}
//=== Scanner Diagnostics -------------------------------------------------===//
swiftscan_diagnostic_set_t*
swiftscan_scanner_diagnostics_query(swiftscan_scanner_t scanner) {
DependencyScanningTool *ScanningTool = unwrap(scanner);
auto NumDiagnostics = ScanningTool->getDiagnostics().size();
swiftscan_diagnostic_set_t *Result = new swiftscan_diagnostic_set_t;
Result->count = NumDiagnostics;
Result->diagnostics = new swiftscan_diagnostic_info_t[NumDiagnostics];
for (size_t i = 0; i < NumDiagnostics; ++i) {
const auto &Diagnostic = ScanningTool->getDiagnostics()[i];
swiftscan_diagnostic_info_s *DiagnosticInfo = new swiftscan_diagnostic_info_s;
DiagnosticInfo->message = swift::c_string_utils::create_clone(Diagnostic.Message.c_str());
switch (Diagnostic.Severity) {
case llvm::SourceMgr::DK_Error:
DiagnosticInfo->severity = SWIFTSCAN_DIAGNOSTIC_SEVERITY_ERROR;
break;
case llvm::SourceMgr::DK_Warning:
DiagnosticInfo->severity = SWIFTSCAN_DIAGNOSTIC_SEVERITY_WARNING;
break;
case llvm::SourceMgr::DK_Note:
DiagnosticInfo->severity = SWIFTSCAN_DIAGNOSTIC_SEVERITY_NOTE;
break;
case llvm::SourceMgr::DK_Remark:
DiagnosticInfo->severity = SWIFTSCAN_DIAGNOSTIC_SEVERITY_REMARK;
break;
}
Result->diagnostics[i] = DiagnosticInfo;
}
return Result;
}
void
swiftscan_scanner_diagnostics_reset(swiftscan_scanner_t scanner) {
DependencyScanningTool *ScanningTool = unwrap(scanner);
ScanningTool->resetDiagnostics();
}
swiftscan_string_ref_t
swiftscan_diagnostic_get_message(swiftscan_diagnostic_info_t diagnostic) {
return diagnostic->message;
}
swiftscan_diagnostic_severity_t
swiftscan_diagnostic_get_severity(swiftscan_diagnostic_info_t diagnostic) {
return diagnostic->severity;
}
void swiftscan_diagnostic_dispose(swiftscan_diagnostic_info_t diagnostic) {
swiftscan_string_dispose(diagnostic->message);
delete diagnostic;
}
void
swiftscan_diagnostics_set_dispose(swiftscan_diagnostic_set_t* diagnostics){
for (size_t i = 0; i < diagnostics->count; ++i) {
swiftscan_diagnostic_dispose(diagnostics->diagnostics[i]);
}
delete[] diagnostics->diagnostics;
delete diagnostics;
}
//=== CAS Functions ----------------------------------------------------------//
swiftscan_cas_options_t swiftscan_cas_options_create() {
clang::CASOptions *CASOpts = new clang::CASOptions();
return wrap(CASOpts);
}
void swiftscan_cas_options_dispose(swiftscan_cas_options_t options) {
delete unwrap(options);
}
void swiftscan_cas_options_set_ondisk_path(swiftscan_cas_options_t options,
const char *path) {
unwrap(options)->CASPath = path;
}
void swiftscan_cas_options_set_plugin_path(swiftscan_cas_options_t options,
const char *path) {
unwrap(options)->PluginPath = path;
}
bool swiftscan_cas_options_set_option(swiftscan_cas_options_t options,
const char *name, const char *value,
swiftscan_string_ref_t *error) {
unwrap(options)->PluginOptions.emplace_back(name, value);
return false;
}
swiftscan_cas_t
swiftscan_cas_create_from_options(swiftscan_cas_options_t options,
swiftscan_string_ref_t *error) {
clang::CASOptions *opts = unwrap(options);
auto cas = SwiftScanCAS::createSwiftScanCAS(*opts);
if (!cas) {
*error =
swift::c_string_utils::create_clone(toString(cas.takeError()).c_str());
return nullptr;
}
return wrap(*cas);
}
void swiftscan_cas_dispose(swiftscan_cas_t cas) { delete unwrap(cas); }
swiftscan_string_ref_t swiftscan_cas_store(swiftscan_cas_t cas, uint8_t *data,
unsigned size,
swiftscan_string_ref_t *error) {
llvm::StringRef StrContent((char*)data, size);
auto ID = unwrap(cas)->storeContent(StrContent);
if (!ID) {
*error =
swift::c_string_utils::create_clone(toString(ID.takeError()).c_str());
return swift::c_string_utils::create_null();
}
return swift::c_string_utils::create_clone(ID->c_str());
}
static swift::file_types::ID
getFileTypeFromScanOutputKind(swiftscan_output_kind_t kind) {
switch (kind) {
case SWIFTSCAN_OUTPUT_TYPE_OBJECT:
return swift::file_types::ID::TY_Object;
case SWIFTSCAN_OUTPUT_TYPE_SWIFTMODULE:
return swift::file_types::ID::TY_SwiftModuleFile;
case SWIFTSCAN_OUTPUT_TYPE_SWIFTINTERFACE:
return swift::file_types::ID::TY_SwiftModuleInterfaceFile;
case SWIFTSCAN_OUTPUT_TYPE_SWIFTPRIVATEINTERFACE:
return swift::file_types::ID::TY_PrivateSwiftModuleInterfaceFile;
case SWIFTSCAN_OUTPUT_TYPE_CLANG_MODULE:
return swift::file_types::ID::TY_ClangModuleFile;
case SWIFTSCAN_OUTPUT_TYPE_CLANG_PCH:
return swift::file_types::ID::TY_PCH;
}
}
swiftscan_string_ref_t
swiftscan_compute_cache_key(swiftscan_cas_t cas, int argc, const char **argv,
const char *input, swiftscan_output_kind_t kind,
swiftscan_string_ref_t *error) {
std::vector<const char *> Compilation;
for (int i = 0; i < argc; ++i)
Compilation.push_back(argv[i]);
auto ID = unwrap(cas)->computeCacheKey(Compilation, input,
getFileTypeFromScanOutputKind(kind));
if (!ID) {
*error =
swift::c_string_utils::create_clone(toString(ID.takeError()).c_str());
return swift::c_string_utils::create_null();
}
return swift::c_string_utils::create_clone(ID->c_str());
}
llvm::Expected<SwiftScanCAS *>
SwiftScanCAS::createSwiftScanCAS(llvm::StringRef Path) {
clang::CASOptions Opts;
Opts.CASPath = Path;
return createSwiftScanCAS(Opts);
}
llvm::Expected<SwiftScanCAS *>
SwiftScanCAS::createSwiftScanCAS(clang::CASOptions &CASOpts) {
auto DB = CASOpts.getOrCreateDatabases();
if (!DB)
return DB.takeError();
return new SwiftScanCAS(std::move(DB->first), std::move(DB->second));
}
llvm::Expected<std::string>
SwiftScanCAS::computeCacheKey(llvm::ArrayRef<const char *> Args,
llvm::StringRef InputPath,
swift::file_types::ID OutputKind) {
auto BaseKey = swift::createCompileJobBaseCacheKey(*CAS, Args);
if (!BaseKey)
return BaseKey.takeError();
auto Key = swift::createCompileJobCacheKeyForOutput(*CAS, *BaseKey, InputPath,
OutputKind);
if (!Key)
return Key.takeError();
return CAS->getID(*Key).toString();
}
llvm::Expected<std::string>
SwiftScanCAS::storeContent(llvm::StringRef Content) {
auto Result = CAS->storeFromString({}, Content);
if (!Result)
return Result.takeError();
return CAS->getID(*Result).toString();
}
//=== Experimental Compiler Invocation Functions ------------------------===//
int invoke_swift_compiler(int argc, const char **argv) {
return swift::mainEntry(argc, argv);
}