Skip to content

[clang][deps] Fix dependency scanning with -working-directory #84525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ class DependencyScanningAction : public tooling::ToolAction {
DisableFree(DisableFree), ModuleName(ModuleName) {}

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

ScanInstance.setFileManager(FileMgr);
// Support for virtual file system overlays.
FileMgr->setVirtualFileSystem(createVFSFromCompilerInvocation(
auto FS = createVFSFromCompilerInvocation(
ScanInstance.getInvocation(), ScanInstance.getDiagnostics(),
FileMgr->getVirtualFileSystemPtr()));
DriverFileMgr->getVirtualFileSystemPtr());

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

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

FileSystemOptions FSOpts;
FSOpts.WorkingDir = WorkingDirectory.str();
auto FileMgr = llvm::makeIntrusiveRefCnt<FileManager>(FSOpts, FinalFS);
auto FileMgr =
llvm::makeIntrusiveRefCnt<FileManager>(FileSystemOptions{}, FinalFS);

std::vector<const char *> FinalCCommandLine(FinalCommandLine.size(), nullptr);
llvm::transform(FinalCommandLine, FinalCCommandLine.begin(),
Expand Down
30 changes: 30 additions & 0 deletions clang/test/ClangScanDeps/working-directory-option.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Test that -working-directory works even when it differs from the working
// directory of the filesystem.

// RUN: rm -rf %t
// RUN: mkdir -p %t/other
// RUN: split-file %s %t
// RUN: sed -e "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json

// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-full \
// RUN: > %t/deps.json

// RUN: cat %t/deps.json | sed 's:\\\\\?:/:g' | FileCheck %s -DPREFIX=%/t

// CHECK: "file-deps": [
// CHECK-NEXT: "[[PREFIX]]/cwd/t.c"
// CHECK-NEXT: "[[PREFIX]]/cwd/relative/h1.h"
// CHECK-NEXT: ]
// CHECK-NEXT: "input-file": "[[PREFIX]]/cwd/t.c"

//--- cdb.json.template
[{
"directory": "DIR/other",
"command": "clang -c t.c -I relative -working-directory DIR/cwd",
"file": "DIR/cwd/t.c"
}]

//--- cwd/relative/h1.h

//--- cwd/t.c
#include "h1.h"