Skip to content

[clang][deps][cas] Fix include-tree contents with -working-directory #8367

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

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 @@ -476,12 +476,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 @@ -868,9 +869,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
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,18 @@ IncludeTreeBuilder::getObjectForBuffer(const SrcMgr::FileInfo &FI) {

Expected<cas::ObjectRef> IncludeTreeBuilder::addToFileList(FileManager &FM,
FileEntryRef FE) {
SmallString<128> PathStorage;
StringRef Filename = FE.getName();
// Apply -working-directory to relative paths. This option causes filesystem
// lookups to use absolute paths, so make paths in the include-tree filesystem
// absolute to match.
if (!llvm::sys::path::is_absolute(Filename) &&
!FM.getFileSystemOpts().WorkingDir.empty()) {
PathStorage = Filename;
FM.FixupRelativePath(PathStorage);
Filename = PathStorage;
}

llvm::ErrorOr<std::optional<cas::ObjectRef>> CASContents =
FM.getObjectRefForFileContent(Filename);
if (!CASContents)
Expand Down
38 changes: 38 additions & 0 deletions clang/test/ClangScanDeps/include-tree-working-directory.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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-include-tree-full -cas-path %t/cas \
// RUN: > %t/deps.json

// Build the include-tree command
// RUN: %deps-to-rsp %t/deps.json --tu-index 0 > %t/tu.rsp
// RUN: %clang @%t/tu.rsp -Rcompile-job-cache 2>&1 | FileCheck %s -check-prefix=CACHE-MISS
// RUN: %clang @%t/tu.rsp -Rcompile-job-cache 2>&1 | FileCheck %s -check-prefix=CACHE-HIT
// RUN: ls %t/t.o

// CACHE-MISS: remark: compile job cache miss
// CACHE-HIT: remark: compile job cache hit

// RUN: cat %t/tu.rsp | sed -E 's|.*"-fcas-include-tree" "(llvmcas://[[:xdigit:]]+)".*|\1|' > %t/tu.casid
// RUN: clang-cas-test -cas %t/cas -print-include-tree @%t/tu.casid | FileCheck %s -DPREFIX=%/t

// CHECK: [[PREFIX]]/t.c llvmcas://
// CHECK: 1:1 <built-in> llvmcas://
// CHECK: 2:1 [[PREFIX]]/relative/h1.h llvmcas://
// CHECK: Files:
// CHECK: [[PREFIX]]/t.c llvmcas://
// CHECK: [[PREFIX]]/relative/h1.h llvmcas://

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

//--- relative/h1.h

//--- t.c
#include "h1.h"
41 changes: 41 additions & 0 deletions clang/test/ClangScanDeps/modules-include-tree-working-directory.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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-include-tree-full -cas-path %t/cas \
// RUN: > %t/deps.json

// Build the include-tree command
// RUN: %deps-to-rsp %t/deps.json --module H > %t/H.rsp
// RUN: %deps-to-rsp %t/deps.json --tu-index 0 > %t/tu.rsp
// RUN: %clang @%t/H.rsp -Rcompile-job-cache 2>&1 | FileCheck %s -check-prefix=CACHE-MISS
// RUN: %clang @%t/H.rsp -Rcompile-job-cache 2>&1 | FileCheck %s -check-prefix=CACHE-HIT
// RUN: %clang @%t/tu.rsp -Rcompile-job-cache 2>&1 | FileCheck %s -check-prefix=CACHE-MISS
// RUN: %clang @%t/tu.rsp -Rcompile-job-cache 2>&1 | FileCheck %s -check-prefix=CACHE-HIT

// CACHE-MISS: remark: compile job cache miss
// CACHE-HIT: remark: compile job cache hit

// RUN: cat %t/H.rsp | sed -E 's|.*"-fcas-include-tree" "(llvmcas://[[:xdigit:]]+)".*|\1|' > %t/H.casid
// RUN: clang-cas-test -cas %t/cas -print-include-tree @%t/H.casid | FileCheck %s -DPREFIX=%/t

// CHEK:C <module-includes>
// CHECK: 2:1 [[PREFIX]]/relative/h1.h llvmcas://
// CHECK: Files:
// CHECK: [[PREFIX]]/relative/h1.h llvmcas://

//--- cdb.json.template
[{
"directory": "DIR/other",
"command": "clang -fsyntax-only t.c -I relative -working-directory DIR -fmodules -fimplicit-modules -fimplicit-module-maps",
"file": "DIR/t.c"
}]

//--- relative/h1.h

//--- relative/module.modulemap
module H { header "h1.h" }

//--- t.c
#include "h1.h"
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"
2 changes: 1 addition & 1 deletion clang/unittests/Tooling/DependencyScannerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,5 +355,5 @@ TEST(DependencyScanner, ScanDepsWithModuleLookup) {

EXPECT_TRUE(llvm::find(InterceptFS->StatPaths, OtherPath) ==
InterceptFS->StatPaths.end());
EXPECT_EQ(InterceptFS->ReadFiles, std::vector<std::string>{"/root/test.m"});
EXPECT_EQ(InterceptFS->ReadFiles, std::vector<std::string>{"test.m"});
}