Index: clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp =================================================================== --- clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp +++ clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp @@ -44,28 +44,6 @@ DependencyConsumer &C; }; -/// A proxy file system that doesn't call `chdir` when changing the working -/// directory of a clang tool. -class ProxyFileSystemWithoutChdir : public llvm::vfs::ProxyFileSystem { -public: - ProxyFileSystemWithoutChdir( - llvm::IntrusiveRefCntPtr FS) - : ProxyFileSystem(std::move(FS)) {} - - llvm::ErrorOr getCurrentWorkingDirectory() const override { - assert(!CWD.empty() && "empty CWD"); - return CWD; - } - - std::error_code setCurrentWorkingDirectory(const Twine &Path) override { - CWD = Path.str(); - return {}; - } - -private: - std::string CWD; -}; - /// A clang tool that runs the preprocessor in a mode that's optimized for /// dependency scanning for the given compiler invocation. class DependencyScanningAction : public tooling::ToolAction { @@ -176,7 +154,10 @@ : Format(Service.getFormat()) { DiagOpts = new DiagnosticOptions(); PCHContainerOps = std::make_shared(); - RealFS = new ProxyFileSystemWithoutChdir(llvm::vfs::getRealFileSystem()); + + llvm::IntrusiveRefCntPtr PhysicalFileSystem( + llvm::vfs::createPhysicalFileSystem().release()); + RealFS = new llvm::vfs::ProxyFileSystem(PhysicalFileSystem); if (Service.canSkipExcludedPPRanges()) PPSkipMappings = std::make_unique(); Index: clang/test/ClangScanDeps/Inputs/relative_directory.json =================================================================== --- /dev/null +++ clang/test/ClangScanDeps/Inputs/relative_directory.json @@ -0,0 +1,12 @@ +[ +{ + "directory": "DIR", + "command": "clang -E Inputs/relative_directory_input1.cpp -IInputs", + "file": "DIR/Inputs/relative_directory_input1.cpp" +}, +{ + "directory": "DIR/Inputs", + "command": "clang -E relative_directory_input2.cpp -I.", + "file": "DIR/Inputs/relative_directory_input2.cpp" +} +] Index: clang/test/ClangScanDeps/relative_directory.cpp =================================================================== --- /dev/null +++ clang/test/ClangScanDeps/relative_directory.cpp @@ -0,0 +1,25 @@ +// RUN: rm -rf %t.dir +// RUN: rm -rf %t.cdb +// RUN: mkdir -p %t.dir +// RUN: mkdir %t.dir/Inputs +// RUN: cp %s %t.dir/Inputs/relative_directory_input1.cpp +// RUN: cp %s %t.dir/Inputs/relative_directory_input2.cpp +// RUN: touch %t.dir/Inputs/header.h +// RUN: sed -e "s|DIR|%/t.dir|g" %S/Inputs/relative_directory.json > %t.cdb +// +// RUN: clang-scan-deps -compilation-database %t.cdb -j 1 | FileCheck --check-prefixes=CHECK1,CHECK2 %s + +// The output order is non-deterministic when using more than one thread, +// so check the output using two runs. +// RUN: clang-scan-deps -compilation-database %t.cdb -j 2 | FileCheck --check-prefix=CHECK1 %s +// RUN: clang-scan-deps -compilation-database %t.cdb -j 2 | FileCheck --check-prefix=CHECK2 %s + +#include + +// CHECK1: relative_directory_input1.o: +// CHECK1-NEXT: relative_directory_input1.cpp +// CHECK1-NEXT: header.h + +// CHECK2: relative_directory_input2.o: +// CHECK2-NEXT: relative_directory_input2.cpp +// CHECK2-NEXT: header.h