Skip to content

Commit ee30b0e

Browse files
committed
[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 llvm-svn: 371903
1 parent 573863e commit ee30b0e

File tree

5 files changed

+31
-0
lines changed

5 files changed

+31
-0
lines changed

clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ class CachedFileSystemEntry {
5656
/// \returns True if the entry is valid.
5757
bool isValid() const { return !MaybeStat || MaybeStat->isStatusKnown(); }
5858

59+
/// \returns True if the current entry points to a directory.
60+
bool isDirectory() const { return MaybeStat && MaybeStat->isDirectory(); }
61+
5962
/// \returns The error or the file's contents.
6063
llvm::ErrorOr<StringRef> getContents() const {
6164
if (!MaybeStat)

clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ class MinimizedVFSFile final : public llvm::vfs::File {
193193
llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
194194
createFile(const CachedFileSystemEntry *Entry,
195195
ExcludedPreprocessorDirectiveSkipMapping *PPSkipMappings) {
196+
if (Entry->isDirectory())
197+
return llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>(
198+
std::make_error_code(std::errc::is_a_directory));
196199
llvm::ErrorOr<StringRef> Contents = Entry->getContents();
197200
if (!Contents)
198201
return Contents.getError();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// A C++ header with same name as that of a directory in the include path.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"directory": "DIR",
4+
"command": "clang -c -IDIR -IDIR/foodir -IInputs DIR/headerwithdirname_input.cpp",
5+
"file": "DIR/headerwithdirname_input.cpp"
6+
}
7+
]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: rm -rf %t.dir
2+
// RUN: rm -rf %t.dir/foodir
3+
// RUN: rm -rf %t.cdb
4+
// RUN: mkdir -p %t.dir
5+
// RUN: mkdir -p %t.dir/foodir
6+
// RUN: cp %s %t.dir/headerwithdirname_input.cpp
7+
// RUN: mkdir %t.dir/Inputs
8+
// RUN: cp %S/Inputs/foodir %t.dir/Inputs/foodir
9+
// RUN: sed -e "s|DIR|%/t.dir|g" %S/Inputs/headerwithdirname.json > %t.cdb
10+
//
11+
// RUN: clang-scan-deps -compilation-database %t.cdb -j 1 | FileCheck %s
12+
13+
#include <foodir>
14+
15+
// CHECK: headerwithdirname_input.o
16+
// CHECK-NEXT: headerwithdirname_input.cpp
17+
// CHECK-NEXT: Inputs{{/|\\}}foodir

0 commit comments

Comments
 (0)