diff --git a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h --- a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h +++ b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h @@ -55,6 +55,11 @@ /// \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 getContents() const { if (!MaybeStat) diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp --- a/clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp +++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp @@ -190,8 +190,11 @@ StringRef Filename = Path.toStringRef(OwnedFilename); // Check the local cache first. - if (const CachedFileSystemEntry *Entry = getCachedEntry(Filename)) + if (const CachedFileSystemEntry *Entry = getCachedEntry(Filename)) { + if (Entry->isDirectory()) + return llvm::ErrorOr>(std::make_error_code(std::errc::is_a_directory)); return createFile(Entry); + } // FIXME: Handle PCM/PCH files. // FIXME: Handle module map files. diff --git a/clang/test/ClangScanDeps/Inputs/foodir b/clang/test/ClangScanDeps/Inputs/foodir new file mode 100644 --- /dev/null +++ b/clang/test/ClangScanDeps/Inputs/foodir @@ -0,0 +1 @@ +// A C++ header with same name as that of a directory in the include path. \ No newline at end of file diff --git a/clang/test/ClangScanDeps/Inputs/headerwithdirname.json b/clang/test/ClangScanDeps/Inputs/headerwithdirname.json new file mode 100644 --- /dev/null +++ b/clang/test/ClangScanDeps/Inputs/headerwithdirname.json @@ -0,0 +1,7 @@ +[ + { + "directory": "DIR", + "command": "clang -c -IDIR -IDIR/foodir -IInputs DIR/headerwithdirname.cpp", + "file": "DIR/headerwithdirname.cpp" + } +] diff --git a/clang/test/ClangScanDeps/headerwithdirname.cpp b/clang/test/ClangScanDeps/headerwithdirname.cpp new file mode 100644 --- /dev/null +++ b/clang/test/ClangScanDeps/headerwithdirname.cpp @@ -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.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 + +// CHECK: clang-scan-deps dependency +// CHECK-NEXT: headerwithdirname.cpp +// CHECK-NEXT: Inputs{{/|\\}}foodir \ No newline at end of file