@@ -333,8 +333,25 @@ Optional<ModuleDependencies> ClangImporter::getModuleDependencies(
333
333
// Determine the command-line arguments for dependency scanning.
334
334
std::vector<std::string> commandLineArgs =
335
335
getClangDepScanningInvocationArguments (ctx, *importHackFile);
336
- std::string workingDir =
337
- ctx.SourceMgr .getFileSystem ()->getCurrentWorkingDirectory ().get ();
336
+ // The Swift compiler does not have a concept of a working directory.
337
+ // It is instead handled by the Swift driver by resolving relative paths
338
+ // according to the driver's notion of a working directory. On the other hand,
339
+ // Clang does have a concept working directory which may be specified on this
340
+ // Clang invocation with '-working-directory'. If so, it is crucial that we use
341
+ // this directory as an argument to the Clang scanner invocation below.
342
+ std::string workingDir;
343
+ auto clangWorkingDirPos = std::find (commandLineArgs.rbegin (),
344
+ commandLineArgs.rend (),
345
+ " -working-directory" );
346
+ if (clangWorkingDirPos == commandLineArgs.rend ())
347
+ workingDir = ctx.SourceMgr .getFileSystem ()->getCurrentWorkingDirectory ().get ();
348
+ else {
349
+ if (clangWorkingDirPos - 1 == commandLineArgs.rend ()) {
350
+ ctx.Diags .diagnose (SourceLoc (), diag::clang_dependency_scan_error, " Missing '-working-directory' argument" );
351
+ return None;
352
+ }
353
+ workingDir = *(clangWorkingDirPos - 1 );
354
+ }
338
355
339
356
auto clangDependencies = clangImpl->tool .getFullDependencies (
340
357
commandLineArgs, workingDir, clangImpl->alreadySeen );
0 commit comments