21
21
#include " swift/Basic/SourceManager.h"
22
22
#include " swift/DependencyScan/DependencyScanImpl.h"
23
23
#include " swift/DependencyScan/StringUtils.h"
24
+ #include " swift/Driver/FrontendUtil.h"
24
25
#include " swift/Frontend/CachedDiagnostics.h"
25
26
#include " swift/Frontend/CachingUtils.h"
26
27
#include " swift/Frontend/CompileJobCacheKey.h"
@@ -108,7 +109,7 @@ struct SwiftCachedOutputHandle {
108
109
struct SwiftScanReplayInstance {
109
110
swift::CompilerInvocation Invocation;
110
111
llvm::BumpPtrAllocator StringAlloc;
111
- std::vector <const char *> Args;
112
+ llvm::SmallVector <const char *> Args;
112
113
};
113
114
114
115
struct SwiftCachedReplayResult {
@@ -196,6 +197,25 @@ swiftscan_string_ref_t swiftscan_cas_store(swiftscan_cas_t cas, uint8_t *data,
196
197
CAS.getID (*Result).toString ().c_str ());
197
198
}
198
199
200
+ // Expand the invocation if there is repsonseFile into Args that are passed in
201
+ // the parameter. Return swift-frontend arguments in an ArrayRef, which has the
202
+ // first "-frontend" option dropped if needed.
203
+ static llvm::ArrayRef<const char *>
204
+ expandSwiftInvocation (int argc, const char **argv, llvm::StringSaver &Saver,
205
+ llvm::SmallVectorImpl<const char *> &Args) {
206
+ Args.reserve (argc);
207
+ for (int i = 0 ; i < argc; ++i)
208
+ Args.push_back (argv[i]);
209
+ swift::driver::ExpandResponseFilesWithRetry (Saver, Args);
210
+
211
+ // Drop the `-frontend` option if it is passed.
212
+ llvm::ArrayRef<const char *> FrontendArgs (Args);
213
+ if (!FrontendArgs.empty () &&
214
+ llvm::StringRef (FrontendArgs.front ()) == " -frontend" )
215
+ FrontendArgs = FrontendArgs.drop_front ();
216
+ return FrontendArgs;
217
+ }
218
+
199
219
static llvm::Expected<std::string>
200
220
computeCacheKey (llvm::cas::ObjectStore &CAS, llvm::ArrayRef<const char *> Args,
201
221
llvm::StringRef InputPath) {
@@ -215,10 +235,6 @@ computeCacheKey(llvm::cas::ObjectStore &CAS, llvm::ArrayRef<const char *> Args,
215
235
std::string MainExecutablePath = llvm::sys::fs::getMainExecutable (
216
236
" swift-frontend" , (void *)swiftscan_cache_replay_compilation);
217
237
218
- // Drop the `-frontend` option if it is passed.
219
- if (llvm::StringRef (Args.front ()) == " -frontend" )
220
- Args = Args.drop_front ();
221
-
222
238
if (Invocation.parseArgs (Args, Diags, &configurationFileBuffers,
223
239
workingDirectory, MainExecutablePath))
224
240
return llvm::createStringError (llvm::inconvertibleErrorCode (),
@@ -270,11 +286,12 @@ computeCacheKeyFromIndex(llvm::cas::ObjectStore &CAS,
270
286
swiftscan_string_ref_t
271
287
swiftscan_cache_compute_key (swiftscan_cas_t cas, int argc, const char **argv,
272
288
const char *input, swiftscan_string_ref_t *error) {
273
- std::vector<const char *> Compilation;
274
- for (int i = 0 ; i < argc; ++i)
275
- Compilation.push_back (argv[i]);
289
+ llvm::SmallVector<const char *> Compilation;
290
+ llvm::BumpPtrAllocator Alloc;
291
+ llvm::StringSaver Saver (Alloc);
292
+ auto Args = expandSwiftInvocation (argc, argv, Saver, Compilation);
276
293
277
- auto ID = computeCacheKey (unwrap (cas)->getCAS (), Compilation , input);
294
+ auto ID = computeCacheKey (unwrap (cas)->getCAS (), Args , input);
278
295
if (!ID) {
279
296
*error =
280
297
swift::c_string_utils::create_clone (toString (ID.takeError ()).c_str ());
@@ -289,12 +306,13 @@ swiftscan_cache_compute_key_from_input_index(swiftscan_cas_t cas, int argc,
289
306
const char **argv,
290
307
unsigned input_index,
291
308
swiftscan_string_ref_t *error) {
292
- std::vector<const char *> Compilation;
293
- for (int i = 0 ; i < argc; ++i)
294
- Compilation.push_back (argv[i]);
309
+ llvm::SmallVector<const char *> Compilation;
310
+ llvm::BumpPtrAllocator Alloc;
311
+ llvm::StringSaver Saver (Alloc);
312
+ auto Args = expandSwiftInvocation (argc, argv, Saver, Compilation);
295
313
296
314
auto ID =
297
- computeCacheKeyFromIndex (unwrap (cas)->getCAS (), Compilation , input_index);
315
+ computeCacheKeyFromIndex (unwrap (cas)->getCAS (), Args , input_index);
298
316
if (!ID) {
299
317
*error =
300
318
swift::c_string_utils::create_clone (toString (ID.takeError ()).c_str ());
@@ -747,11 +765,9 @@ swiftscan_cache_replay_instance_t
747
765
swiftscan_cache_replay_instance_create (int argc, const char **argv,
748
766
swiftscan_string_ref_t *error) {
749
767
auto *Instance = new SwiftScanReplayInstance ();
768
+ llvm::SmallVector<const char *> Compilation;
750
769
llvm::StringSaver Saver (Instance->StringAlloc );
751
- for (int i = 0 ; i < argc; ++i) {
752
- auto Str = Saver.save (argv[i]);
753
- Instance->Args .push_back (Str.data ());
754
- }
770
+ auto Args = expandSwiftInvocation (argc, argv, Saver, Instance->Args );
755
771
756
772
// Capture the diagnostics when creating invocation.
757
773
std::string err_msg;
@@ -764,7 +780,7 @@ swiftscan_cache_replay_instance_create(int argc, const char **argv,
764
780
std::string MainExecutablePath = llvm::sys::fs::getMainExecutable (
765
781
" swift-frontend" , (void *)swiftscan_cache_replay_compilation);
766
782
767
- if (Instance->Invocation .parseArgs (Instance-> Args , DE, nullptr , {},
783
+ if (Instance->Invocation .parseArgs (Args, DE, nullptr , {},
768
784
MainExecutablePath)) {
769
785
delete Instance;
770
786
*error = swift::c_string_utils::create_clone (err_msg.c_str ());
0 commit comments