Skip to content

Commit 083da46

Browse files
authored
[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.
1 parent f32b04d commit 083da46

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
@@ -296,7 +296,7 @@ class DependencyScanningAction : public tooling::ToolAction {
296296
DisableFree(DisableFree), ModuleName(ModuleName) {}
297297

298298
bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation,
299-
FileManager *FileMgr,
299+
FileManager *DriverFileMgr,
300300
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
301301
DiagnosticConsumer *DiagConsumer) override {
302302
// Make a deep copy of the original Clang invocation.
@@ -342,12 +342,13 @@ class DependencyScanningAction : public tooling::ToolAction {
342342
ScanInstance.getHeaderSearchOpts().ModulesIncludeVFSUsage =
343343
any(OptimizeArgs & ScanningOptimizations::VFS);
344344

345-
ScanInstance.setFileManager(FileMgr);
346345
// Support for virtual file system overlays.
347-
FileMgr->setVirtualFileSystem(createVFSFromCompilerInvocation(
346+
auto FS = createVFSFromCompilerInvocation(
348347
ScanInstance.getInvocation(), ScanInstance.getDiagnostics(),
349-
FileMgr->getVirtualFileSystemPtr()));
348+
DriverFileMgr->getVirtualFileSystemPtr());
350349

350+
// Create a new FileManager to match the invocation's FileSystemOptions.
351+
auto *FileMgr = ScanInstance.createFileManager(FS);
351352
ScanInstance.createSourceManager(*FileMgr);
352353

353354
// Store the list of prebuilt module files into header search options. This
@@ -624,9 +625,8 @@ bool DependencyScanningWorker::computeDependencies(
624625
ModifiedCommandLine ? *ModifiedCommandLine : CommandLine;
625626
auto &FinalFS = ModifiedFS ? ModifiedFS : BaseFS;
626627

627-
FileSystemOptions FSOpts;
628-
FSOpts.WorkingDir = WorkingDirectory.str();
629-
auto FileMgr = llvm::makeIntrusiveRefCnt<FileManager>(FSOpts, FinalFS);
628+
auto FileMgr =
629+
llvm::makeIntrusiveRefCnt<FileManager>(FileSystemOptions{}, FinalFS);
630630

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