r371903 - [clang-scan-deps] Fix for headers having the same name as a directory

Alex Lorenz via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 13 15:12:02 PDT 2019


Author: arphaman
Date: Fri Sep 13 15:12:02 2019
New Revision: 371903

URL: http://llvm.org/viewvc/llvm-project?rev=371903&view=rev
Log:
[clang-scan-deps] Fix for headers having the same name as a directory

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

Added:
    cfe/trunk/test/ClangScanDeps/Inputs/foodir
    cfe/trunk/test/ClangScanDeps/Inputs/headerwithdirname.json
    cfe/trunk/test/ClangScanDeps/headerwithdirname.cpp
Modified:
    cfe/trunk/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h
    cfe/trunk/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp

Modified: cfe/trunk/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h?rev=371903&r1=371902&r2=371903&view=diff
==============================================================================
--- cfe/trunk/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h (original)
+++ cfe/trunk/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h Fri Sep 13 15:12:02 2019
@@ -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)

Modified: cfe/trunk/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp?rev=371903&r1=371902&r2=371903&view=diff
==============================================================================
--- cfe/trunk/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp (original)
+++ cfe/trunk/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp Fri Sep 13 15:12:02 2019
@@ -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();

Added: cfe/trunk/test/ClangScanDeps/Inputs/foodir
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ClangScanDeps/Inputs/foodir?rev=371903&view=auto
==============================================================================
--- cfe/trunk/test/ClangScanDeps/Inputs/foodir (added)
+++ cfe/trunk/test/ClangScanDeps/Inputs/foodir Fri Sep 13 15:12:02 2019
@@ -0,0 +1 @@
+// A C++ header with same name as that of a directory in the include path.

Added: cfe/trunk/test/ClangScanDeps/Inputs/headerwithdirname.json
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ClangScanDeps/Inputs/headerwithdirname.json?rev=371903&view=auto
==============================================================================
--- cfe/trunk/test/ClangScanDeps/Inputs/headerwithdirname.json (added)
+++ cfe/trunk/test/ClangScanDeps/Inputs/headerwithdirname.json Fri Sep 13 15:12:02 2019
@@ -0,0 +1,7 @@
+[
+    {
+      "directory": "DIR",
+      "command": "clang -c -IDIR -IDIR/foodir -IInputs DIR/headerwithdirname_input.cpp",
+      "file": "DIR/headerwithdirname_input.cpp"
+    }
+]

Added: cfe/trunk/test/ClangScanDeps/headerwithdirname.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ClangScanDeps/headerwithdirname.cpp?rev=371903&view=auto
==============================================================================
--- cfe/trunk/test/ClangScanDeps/headerwithdirname.cpp (added)
+++ cfe/trunk/test/ClangScanDeps/headerwithdirname.cpp Fri Sep 13 15:12:02 2019
@@ -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




More information about the cfe-commits mailing list