[FileManager] Don't crash if reading from stdin and stat(".") fails
authorDavid Majnemer <david.majnemer@gmail.com>
Tue, 12 Apr 2016 16:33:53 +0000 (16:33 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Tue, 12 Apr 2016 16:33:53 +0000 (16:33 +0000)
addAncestorsAsVirtualDirs("<stdin>") quickly returns without doing work
because "<stdin>" has no parent_path.  This violates the expectation
that a subsequent call to getDirectoryFromFile("<stdin>") would succeed.
Instead, it fails because it uses the "." if the file has no path
component.

Fix this by keeping the behavior between addAncestorsAsVirtualDirs and
getDirectoryFromFile symmetric.

llvm-svn: 266089

clang/lib/Basic/FileManager.cpp

index ba016db..c4cc8dc 100644 (file)
@@ -123,7 +123,7 @@ static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr,
 void FileManager::addAncestorsAsVirtualDirs(StringRef Path) {
   StringRef DirName = llvm::sys::path::parent_path(Path);
   if (DirName.empty())
-    return;
+    DirName = ".";
 
   auto &NamedDirEnt =
       *SeenDirEntries.insert(std::make_pair(DirName, nullptr)).first;