Skip to content

Commit 1808850

Browse files
authored
Merge pull request #65436 from artemcm/DepScanPCHWorkingDir
[Dependency Scanner] Ensure the Clang dependency scanner working directory matches the invocation when querying bridging header dependencies
2 parents d18b2ca + 9d4e831 commit 1808850

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

lib/ClangImporter/ClangModuleDependencyScanner.cpp

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -200,19 +200,15 @@ void ClangImporter::recordModuleDependencies(
200200
}
201201
}
202202

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) {
216212
std::string workingDir;
217213
auto clangWorkingDirPos = std::find(
218214
commandLineArgs.rbegin(), commandLineArgs.rend(), "-working-directory");
@@ -227,6 +223,23 @@ Optional<const ModuleDependencyInfo*> ClangImporter::getModuleDependencies(
227223
}
228224
workingDir = *(clangWorkingDirPos - 1);
229225
}
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();
230243

231244
auto moduleCachePath = getModuleCachePathFromClang(getClangInstance());
232245
auto lookupModuleOutput =
@@ -285,8 +298,13 @@ bool ClangImporter::addBridgingHeaderDependencies(
285298
// Determine the command-line arguments for dependency scanning.
286299
std::vector<std::string> commandLineArgs =
287300
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();
290308

291309
auto moduleCachePath = getModuleCachePathFromClang(getClangInstance());
292310
auto lookupModuleOutput =

0 commit comments

Comments
 (0)