[PATCH] D67091: Fix for headers having the same name as a directory

Kousik Kumar via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 3 03:50:58 PDT 2019


kousikk created this revision.
kousikk added a reviewer: arphaman.
Herald added subscribers: cfe-commits, jfb, dexonsmith.
Herald added a project: clang.

Scan deps tool crashes when called on a C++ file, containing an include
that has the same name as a directory. For example:

test.cpp:
#include <dir>
int main() { return 0; }

In directory foo/ :
dir/ bar/dir(file)

Now if the compile command is:
clang++ -I foo/ -I foo/bar/ -c test.cpp

The tool crashes since it finds foo/dir and tries to read that as a file and fails.
In this change, I add a defence against that scenario in the Dependency Scanning
File system.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D67091

Files:
  clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h
  clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp


Index: clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp
===================================================================
--- clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp
+++ 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))
-    return createFile(Entry);
+  if (const CachedFileSystemEntry *Entry = getCachedEntry(Filename)) {
+    if (Entry->isDirectory()) {
+      return llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>(std::make_error_code(std::errc::is_a_directory));
+    }
+  }
 
   // FIXME: Handle PCM/PCH files.
   // FIXME: Handle module map files.
Index: clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h
===================================================================
--- clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h
+++ 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<StringRef> getContents() const {
     if (!MaybeStat)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D67091.218428.patch
Type: text/x-patch
Size: 1546 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190903/27577409/attachment.bin>


More information about the cfe-commits mailing list