Skip to content

Commit

Permalink
[clang-scan-deps] Fix for headers having the same name as a directory
Browse files Browse the repository at this point in the history
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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@371903 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
hyp committed Sep 13, 2019
1 parent 7343eab commit bac35bd
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class CachedFileSystemEntry {
/// \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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ class MinimizedVFSFile final : public llvm::vfs::File {
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();
Expand Down
1 change: 1 addition & 0 deletions test/ClangScanDeps/Inputs/foodir
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// A C++ header with same name as that of a directory in the include path.
7 changes: 7 additions & 0 deletions test/ClangScanDeps/Inputs/headerwithdirname.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"directory": "DIR",
"command": "clang -c -IDIR -IDIR/foodir -IInputs DIR/headerwithdirname_input.cpp",
"file": "DIR/headerwithdirname_input.cpp"
}
]
17 changes: 17 additions & 0 deletions test/ClangScanDeps/headerwithdirname.cpp
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit bac35bd

Please sign in to comment.