Skip to content

Commit 41fd167

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) (cherry picked from commit 925cc8d)
1 parent b7ab938 commit 41fd167

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
@@ -421,7 +421,7 @@ class DependencyScanningAction : public tooling::ToolAction {
421421
}
422422

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

480-
ScanInstance.setFileManager(FileMgr);
481480
// Support for virtual file system overlays.
482-
FileMgr->setVirtualFileSystem(createVFSFromCompilerInvocation(
481+
auto FS = createVFSFromCompilerInvocation(
483482
ScanInstance.getInvocation(), ScanInstance.getDiagnostics(),
484-
FileMgr->getVirtualFileSystemPtr()));
483+
DriverFileMgr->getVirtualFileSystemPtr());
485484

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

488489
// Store the list of prebuilt module files into header search options. This
@@ -845,9 +846,8 @@ bool DependencyScanningWorker::computeDependencies(
845846
ModifiedCommandLine ? *ModifiedCommandLine : CommandLine;
846847
auto &FinalFS = ModifiedFS ? ModifiedFS : BaseFS;
847848

848-
FileSystemOptions FSOpts;
849-
FSOpts.WorkingDir = WorkingDirectory.str();
850-
auto FileMgr = llvm::makeIntrusiveRefCnt<FileManager>(FSOpts, FinalFS);
849+
auto FileMgr =
850+
llvm::makeIntrusiveRefCnt<FileManager>(FileSystemOptions{}, FinalFS);
851851

852852
std::vector<const char *> FinalCCommandLine(FinalCommandLine.size(), nullptr);
853853
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)