Skip to content

Commit 03136e0

Browse files
committed
[Dependency Scanner] Ensure the Clang dependency scanner working directory matches the invocation.
The Swift compiler does not have a concept of a working directory. It is instead handled by the Swift driver by resolving relative paths according to the driver's working directory argument. On the other hand, Clang does have a concept working directory which may be specified on this Clang invocation with '-working-directory'. If so, it is crucial that we use this directory as an argument to the Clang scanner API. Otherwiswe, we risk having a mismatch between the working directory specified on the scanner's Clang invocation and the one use from the scanner API entry-points, which leads to downstream inconsistencies and errors.
1 parent aec29cc commit 03136e0

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

lib/ClangImporter/ClangModuleDependencyScanner.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,25 @@ Optional<ModuleDependencies> ClangImporter::getModuleDependencies(
333333
// Determine the command-line arguments for dependency scanning.
334334
std::vector<std::string> commandLineArgs =
335335
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+
}
338355

339356
auto clangDependencies = clangImpl->tool.getFullDependencies(
340357
commandLineArgs, workingDir, clangImpl->alreadySeen);

0 commit comments

Comments
 (0)