Skip to content

Commit 925cc8d

Browse files
committed
[clang][deps] Fix dependency scanning with -working-directory (llvm#84525)
Stop overriding -working-directory to CWD during argument parsing, which should no longer necessary after we set the VFS working directory, and set FSOpts correctly after parsing arguments so that working-directory behaves correctly. (cherry picked from commit 083da46)
1 parent fb8d637 commit 925cc8d

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ class DependencyScanningAction : public tooling::ToolAction {
420420
}
421421

422422
bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation,
423-
FileManager *FileMgr,
423+
FileManager *DriverFileMgr,
424424
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
425425
DiagnosticConsumer *DiagConsumer) override {
426426
// Make a deep copy of the original Clang invocation.
@@ -476,12 +476,13 @@ class DependencyScanningAction : public tooling::ToolAction {
476476
ScanInstance.getHeaderSearchOpts().ModulesIncludeVFSUsage =
477477
any(OptimizeArgs & ScanningOptimizations::VFS);
478478

479-
ScanInstance.setFileManager(FileMgr);
480479
// Support for virtual file system overlays.
481-
FileMgr->setVirtualFileSystem(createVFSFromCompilerInvocation(
480+
auto FS = createVFSFromCompilerInvocation(
482481
ScanInstance.getInvocation(), ScanInstance.getDiagnostics(),
483-
FileMgr->getVirtualFileSystemPtr()));
482+
DriverFileMgr->getVirtualFileSystemPtr());
484483

484+
// Create a new FileManager to match the invocation's FileSystemOptions.
485+
auto *FileMgr = ScanInstance.createFileManager(FS);
485486
ScanInstance.createSourceManager(*FileMgr);
486487

487488
// Store the list of prebuilt module files into header search options. This
@@ -868,9 +869,8 @@ bool DependencyScanningWorker::computeDependencies(
868869
ModifiedCommandLine ? *ModifiedCommandLine : CommandLine;
869870
auto &FinalFS = ModifiedFS ? ModifiedFS : BaseFS;
870871

871-
FileSystemOptions FSOpts;
872-
FSOpts.WorkingDir = WorkingDirectory.str();
873-
auto FileMgr = llvm::makeIntrusiveRefCnt<FileManager>(FSOpts, FinalFS);
872+
auto FileMgr =
873+
llvm::makeIntrusiveRefCnt<FileManager>(FileSystemOptions{}, FinalFS);
874874

875875
std::vector<const char *> FinalCCommandLine(FinalCommandLine.size(), nullptr);
876876
llvm::transform(FinalCommandLine, FinalCCommandLine.begin(),
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Test that -working-directory works even when it differs from the working
2+
// directory of the filesystem.
3+
4+
// RUN: rm -rf %t
5+
// RUN: mkdir -p %t/other
6+
// RUN: split-file %s %t
7+
// RUN: sed -e "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json
8+
9+
// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-full \
10+
// RUN: > %t/deps.json
11+
12+
// RUN: cat %t/deps.json | sed 's:\\\\\?:/:g' | FileCheck %s -DPREFIX=%/t
13+
14+
// CHECK: "file-deps": [
15+
// CHECK-NEXT: "[[PREFIX]]/cwd/t.c"
16+
// CHECK-NEXT: "[[PREFIX]]/cwd/relative/h1.h"
17+
// CHECK-NEXT: ]
18+
// CHECK-NEXT: "input-file": "[[PREFIX]]/cwd/t.c"
19+
20+
//--- cdb.json.template
21+
[{
22+
"directory": "DIR/other",
23+
"command": "clang -c t.c -I relative -working-directory DIR/cwd",
24+
"file": "DIR/cwd/t.c"
25+
}]
26+
27+
//--- cwd/relative/h1.h
28+
29+
//--- cwd/t.c
30+
#include "h1.h"

0 commit comments

Comments
 (0)