@@ -200,19 +200,15 @@ void ClangImporter::recordModuleDependencies(
200
200
}
201
201
}
202
202
203
- Optional<const ModuleDependencyInfo*> ClangImporter::getModuleDependencies (
204
- StringRef moduleName, ModuleDependenciesCache &cache,
205
- InterfaceSubContextDelegate &delegate, bool isTestableImport) {
206
- auto &ctx = Impl.SwiftContext ;
207
- // Determine the command-line arguments for dependency scanning.
208
- std::vector<std::string> commandLineArgs =
209
- getClangDepScanningInvocationArguments (ctx);
210
- // The Swift compiler does not have a concept of a working directory.
211
- // It is instead handled by the Swift driver by resolving relative paths
212
- // according to the driver's notion of a working directory. On the other hand,
213
- // Clang does have a concept working directory which may be specified on this
214
- // Clang invocation with '-working-directory'. If so, it is crucial that we
215
- // use this directory as an argument to the Clang scanner invocation below.
203
+ // The Swift compiler does not have a concept of a working directory.
204
+ // It is instead handled by the Swift driver by resolving relative paths
205
+ // according to the driver's notion of a working directory. On the other hand,
206
+ // Clang does have a concept working directory which may be specified on this
207
+ // Clang invocation with '-working-directory'. If so, it is crucial that we
208
+ // use this directory as an argument to the Clang scanner invocation below.
209
+ static Optional<std::string>
210
+ computeClangWorkingDirectory (const std::vector<std::string> &commandLineArgs,
211
+ const ASTContext &ctx) {
216
212
std::string workingDir;
217
213
auto clangWorkingDirPos = std::find (
218
214
commandLineArgs.rbegin (), commandLineArgs.rend (), " -working-directory" );
@@ -227,6 +223,23 @@ Optional<const ModuleDependencyInfo*> ClangImporter::getModuleDependencies(
227
223
}
228
224
workingDir = *(clangWorkingDirPos - 1 );
229
225
}
226
+ return workingDir;
227
+ }
228
+
229
+ Optional<const ModuleDependencyInfo*> ClangImporter::getModuleDependencies (
230
+ StringRef moduleName, ModuleDependenciesCache &cache,
231
+ InterfaceSubContextDelegate &delegate, bool isTestableImport) {
232
+ auto &ctx = Impl.SwiftContext ;
233
+ // Determine the command-line arguments for dependency scanning.
234
+ std::vector<std::string> commandLineArgs =
235
+ getClangDepScanningInvocationArguments (ctx);
236
+ auto optionalWorkingDir = computeClangWorkingDirectory (commandLineArgs, ctx);
237
+ if (!optionalWorkingDir.hasValue ()) {
238
+ ctx.Diags .diagnose (SourceLoc (), diag::clang_dependency_scan_error,
239
+ " Missing '-working-directory' argument" );
240
+ return None;
241
+ }
242
+ std::string workingDir = optionalWorkingDir.getValue ();
230
243
231
244
auto moduleCachePath = getModuleCachePathFromClang (getClangInstance ());
232
245
auto lookupModuleOutput =
@@ -285,8 +298,13 @@ bool ClangImporter::addBridgingHeaderDependencies(
285
298
// Determine the command-line arguments for dependency scanning.
286
299
std::vector<std::string> commandLineArgs =
287
300
getClangDepScanningInvocationArguments (ctx, StringRef (bridgingHeader));
288
- std::string workingDir =
289
- ctx.SourceMgr .getFileSystem ()->getCurrentWorkingDirectory ().get ();
301
+ auto optionalWorkingDir = computeClangWorkingDirectory (commandLineArgs, ctx);
302
+ if (!optionalWorkingDir.hasValue ()) {
303
+ ctx.Diags .diagnose (SourceLoc (), diag::clang_dependency_scan_error,
304
+ " Missing '-working-directory' argument" );
305
+ return true ;
306
+ }
307
+ std::string workingDir = optionalWorkingDir.getValue ();
290
308
291
309
auto moduleCachePath = getModuleCachePathFromClang (getClangInstance ());
292
310
auto lookupModuleOutput =
0 commit comments