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)
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)
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits