[clang-scan-deps] Fix for headers having the same name as a directory
authorAlex Lorenz <arphaman@gmail.com>
Fri, 13 Sep 2019 22:12:02 +0000 (22:12 +0000)
committerAlex Lorenz <arphaman@gmail.com>
Fri, 13 Sep 2019 22:12:02 +0000 (22:12 +0000)
Scan deps tool crashes when called on a C++ file, containing an include
that has the same name as a directory.
The tool crashes since it finds foo/dir and tries to read that as a file and fails.

Patch by: kousikk (Kousik Kumar)

Differential Revision: https://reviews.llvm.org/D67091

llvm-svn: 371903

clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h
clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp
clang/test/ClangScanDeps/Inputs/foodir [new file with mode: 0644]
clang/test/ClangScanDeps/Inputs/headerwithdirname.json [new file with mode: 0644]
clang/test/ClangScanDeps/headerwithdirname.cpp [new file with mode: 0644]

index 94b3705..1d0d265 100644 (file)
@@ -56,6 +56,9 @@ public:
   /// \returns True if the entry is valid.
   bool isValid() const { return !MaybeStat || MaybeStat->isStatusKnown(); }
 
+  /// \returns True if the current entry points to a directory.
+  bool isDirectory() const { return MaybeStat && MaybeStat->isDirectory(); }
+
   /// \returns The error or the file's contents.
   llvm::ErrorOr<StringRef> getContents() const {
     if (!MaybeStat)
index 7a3d189..35ecbd4 100644 (file)
@@ -193,6 +193,9 @@ private:
 llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
 createFile(const CachedFileSystemEntry *Entry,
            ExcludedPreprocessorDirectiveSkipMapping *PPSkipMappings) {
+  if (Entry->isDirectory())
+    return llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>(
+        std::make_error_code(std::errc::is_a_directory));
   llvm::ErrorOr<StringRef> Contents = Entry->getContents();
   if (!Contents)
     return Contents.getError();
diff --git a/clang/test/ClangScanDeps/Inputs/foodir b/clang/test/ClangScanDeps/Inputs/foodir
new file mode 100644 (file)
index 0000000..c2e511e
--- /dev/null
@@ -0,0 +1 @@
+// A C++ header with same name as that of a directory in the include path.
diff --git a/clang/test/ClangScanDeps/Inputs/headerwithdirname.json b/clang/test/ClangScanDeps/Inputs/headerwithdirname.json
new file mode 100644 (file)
index 0000000..2ae5619
--- /dev/null
@@ -0,0 +1,7 @@
+[
+    {
+      "directory": "DIR",
+      "command": "clang -c -IDIR -IDIR/foodir -IInputs DIR/headerwithdirname_input.cpp",
+      "file": "DIR/headerwithdirname_input.cpp"
+    }
+]
diff --git a/clang/test/ClangScanDeps/headerwithdirname.cpp b/clang/test/ClangScanDeps/headerwithdirname.cpp
new file mode 100644 (file)
index 0000000..b0f6033
--- /dev/null
@@ -0,0 +1,17 @@
+// RUN: rm -rf %t.dir
+// RUN: rm -rf %t.dir/foodir
+// RUN: rm -rf %t.cdb
+// RUN: mkdir -p %t.dir
+// RUN: mkdir -p %t.dir/foodir
+// RUN: cp %s %t.dir/headerwithdirname_input.cpp
+// RUN: mkdir %t.dir/Inputs
+// RUN: cp %S/Inputs/foodir %t.dir/Inputs/foodir
+// RUN: sed -e "s|DIR|%/t.dir|g" %S/Inputs/headerwithdirname.json > %t.cdb
+//
+// RUN: clang-scan-deps -compilation-database %t.cdb -j 1 | FileCheck %s
+
+#include <foodir>
+
+// CHECK: headerwithdirname_input.o
+// CHECK-NEXT: headerwithdirname_input.cpp
+// CHECK-NEXT: Inputs{{/|\\}}foodir