[PATCH] D91204: [[clang-scan-deps] Fix for input file given as relative path in compilation database "command" entry
Sylvain Audi via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 10 13:25:55 PST 2020
saudi created this revision.
saudi added reviewers: arphaman, dexonsmith.
saudi added a project: clang.
Herald added subscribers: cfe-commits, tschuett.
saudi requested review of this revision.
The bug appeared when clang-scan-deps was run from a different directory than the one provided in the "directory" entry.
Bug: https://bugs.llvm.org/show_bug.cgi?id=47252
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D91204
Files:
clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
clang/test/ClangScanDeps/Inputs/relative_directory.json
clang/test/ClangScanDeps/relative_directory.cpp
Index: clang/test/ClangScanDeps/relative_directory.cpp
===================================================================
--- /dev/null
+++ clang/test/ClangScanDeps/relative_directory.cpp
@@ -0,0 +1,20 @@
+// 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 %s
+
+#include <header.h>
+
+// CHECK: relative_directory_input1.o:
+// CHECK-NEXT: relative_directory_input1.cpp
+// CHECK-NEXT: header.h
+
+// CHECK: relative_directory_input2.o:
+// CHECK-NEXT: relative_directory_input2.cpp
+// CHECK-NEXT: header.h
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/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
===================================================================
--- clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
+++ clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
@@ -58,11 +58,47 @@
}
std::error_code setCurrentWorkingDirectory(const Twine &Path) override {
+ assert(llvm::sys::path::is_absolute(Path) && "relative CWD");
CWD = Path.str();
return {};
}
+ // Provide absolute paths to the underlying FS, to prevent it from using
+ // the system's CWD.
+ llvm::ErrorOr<llvm::vfs::Status> status(const Twine &Path) override {
+ SmallString<256> Storage;
+ return ProxyFileSystem::status(adjustPath(Path, Storage));
+ }
+
+ llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
+ openFileForRead(const Twine &Path) override {
+ SmallString<256> Storage;
+ return ProxyFileSystem::openFileForRead(adjustPath(Path, Storage));
+ }
+ llvm::vfs::directory_iterator dir_begin(const Twine &Dir,
+ std::error_code &EC) override {
+ SmallString<256> Storage;
+ return ProxyFileSystem::dir_begin(adjustPath(Dir, Storage), EC);
+ }
+ std::error_code getRealPath(const Twine &Path,
+ SmallVectorImpl<char> &Output) const override {
+ SmallString<256> Storage;
+ return ProxyFileSystem::getRealPath(adjustPath(Path, Storage), Output);
+ }
+ std::error_code isLocal(const Twine &Path, bool &Result) override {
+ SmallString<256> Storage;
+ return ProxyFileSystem::isLocal(adjustPath(Path, Storage), Result);
+ }
+
private:
+ Twine adjustPath(const Twine &Path, SmallVectorImpl<char> &Storage) const {
+ assert(!CWD.empty() && "empty CWD");
+
+ Path.toVector(Storage);
+ llvm::sys::fs::make_absolute(CWD, Storage);
+ return Storage;
+ }
+
std::string CWD;
};
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D91204.304314.patch
Type: text/x-patch
Size: 3312 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201110/d1e22ae9/attachment.bin>
More information about the cfe-commits
mailing list