Use llvm::sys::fs::UniqueID for windows and unix.

This unifies the unix and windows versions of FileManager::UniqueDirContainer
and FileManager::UniqueFileContainer by using UniqueID.

We cannot just replace "struct stat" with llvm::sys::fs::file_status, since we
want to be able to construct fake ones, and file_status has different members
on unix and windows.

What the patch does is:

* Record only the information that clang is actually using.
* Use llvm::sys::fs::status instead of stat and fstat.
* Use llvm::sys::fs::UniqueID
* Delete the old windows versions of UniqueDirContainer and
UniqueFileContainer since the "unix" one now works on windows too.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@187619 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Basic/FileManager.cpp b/lib/Basic/FileManager.cpp
index 079b7ff..af9b266 100644
--- a/lib/Basic/FileManager.cpp
+++ b/lib/Basic/FileManager.cpp
@@ -63,91 +63,16 @@
   if (FD != -1) ::close(FD);
 }
 
-bool FileEntry::isNamedPipe() const {
-  return S_ISFIFO(FileMode);
-}
-
-//===----------------------------------------------------------------------===//
-// Windows.
-//===----------------------------------------------------------------------===//
-
-#ifdef LLVM_ON_WIN32
-
-namespace {
-  static std::string GetFullPath(const char *relPath) {
-    char *absPathStrPtr = _fullpath(NULL, relPath, 0);
-    assert(absPathStrPtr && "_fullpath() returned NULL!");
-
-    std::string absPath(absPathStrPtr);
-
-    free(absPathStrPtr);
-    return absPath;
-  }
-}
-
-class FileManager::UniqueDirContainer {
-  /// UniqueDirs - Cache from full path to existing directories/files.
-  ///
-  llvm::StringMap<DirectoryEntry> UniqueDirs;
-
-public:
-  /// getDirectory - Return an existing DirectoryEntry with the given
-  /// name if there is already one; otherwise create and return a
-  /// default-constructed DirectoryEntry.
-  DirectoryEntry &getDirectory(const char *Name,
-                               const struct stat & /*StatBuf*/) {
-    std::string FullPath(GetFullPath(Name));
-    return UniqueDirs.GetOrCreateValue(FullPath).getValue();
-  }
-
-  size_t size() const { return UniqueDirs.size(); }
-};
-
-class FileManager::UniqueFileContainer {
-  /// UniqueFiles - Cache from full path to existing directories/files.
-  ///
-  llvm::StringMap<FileEntry, llvm::BumpPtrAllocator> UniqueFiles;
-
-public:
-  /// getFile - Return an existing FileEntry with the given name if
-  /// there is already one; otherwise create and return a
-  /// default-constructed FileEntry.
-  FileEntry &getFile(const char *Name, const struct stat & /*StatBuf*/) {
-    std::string FullPath(GetFullPath(Name));
-
-    // Lowercase string because Windows filesystem is case insensitive.
-    FullPath = StringRef(FullPath).lower();
-    return UniqueFiles.GetOrCreateValue(FullPath).getValue();
-  }
-
-  size_t size() const { return UniqueFiles.size(); }
-
-  void erase(const FileEntry *Entry) {
-    std::string FullPath(GetFullPath(Entry->getName()));
-
-    // Lowercase string because Windows filesystem is case insensitive.
-    FullPath = StringRef(FullPath).lower();
-    UniqueFiles.erase(FullPath);
-  }
-};
-
-//===----------------------------------------------------------------------===//
-// Unix-like Systems.
-//===----------------------------------------------------------------------===//
-
-#else
-
 class FileManager::UniqueDirContainer {
   /// UniqueDirs - Cache from ID's to existing directories/files.
-  std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
+  std::map<llvm::sys::fs::UniqueID, DirectoryEntry> UniqueDirs;
 
 public:
   /// getDirectory - Return an existing DirectoryEntry with the given
   /// ID's if there is already one; otherwise create and return a
   /// default-constructed DirectoryEntry.
-  DirectoryEntry &getDirectory(const char * /*Name*/,
-                               const struct stat &StatBuf) {
-    return UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
+  DirectoryEntry &getDirectory(const llvm::sys::fs::UniqueID &UniqueID) {
+    return UniqueDirs[UniqueID];
   }
 
   size_t size() const { return UniqueDirs.size(); }
@@ -161,12 +86,10 @@
   /// getFile - Return an existing FileEntry with the given ID's if
   /// there is already one; otherwise create and return a
   /// default-constructed FileEntry.
-  FileEntry &getFile(const char * /*Name*/, const struct stat &StatBuf) {
-    return
-      const_cast<FileEntry&>(
-                    *UniqueFiles.insert(FileEntry(StatBuf.st_dev,
-                                                  StatBuf.st_ino,
-                                                  StatBuf.st_mode)).first);
+  FileEntry &getFile(llvm::sys::fs::UniqueID UniqueID, bool IsNamedPipe,
+                     bool InPCH) {
+    return const_cast<FileEntry &>(
+        *UniqueFiles.insert(FileEntry(UniqueID, IsNamedPipe, InPCH)).first);
   }
 
   size_t size() const { return UniqueFiles.size(); }
@@ -174,8 +97,6 @@
   void erase(const FileEntry *Entry) { UniqueFiles.erase(*Entry); }
 };
 
-#endif
-
 //===----------------------------------------------------------------------===//
 // Common logic.
 //===----------------------------------------------------------------------===//
@@ -323,8 +244,8 @@
   const char *InterndDirName = NamedDirEnt.getKeyData();
 
   // Check to see if the directory exists.
-  struct stat StatBuf;
-  if (getStatValue(InterndDirName, StatBuf, false, 0/*directory lookup*/)) {
+  FileData Data;
+  if (getStatValue(InterndDirName, Data, false, 0 /*directory lookup*/)) {
     // There's no real directory at the given path.
     if (!CacheFailure)
       SeenDirEntries.erase(DirName);
@@ -335,7 +256,8 @@
   // same inode (this occurs on Unix-like systems when one dir is
   // symlinked to another, for example) or the same path (on
   // Windows).
-  DirectoryEntry &UDE = UniqueRealDirs.getDirectory(InterndDirName, StatBuf);
+  DirectoryEntry &UDE =
+      UniqueRealDirs.getDirectory(Data.UniqueID);
 
   NamedDirEnt.setValue(&UDE);
   if (!UDE.getName()) {
@@ -388,8 +310,8 @@
 
   // Nope, there isn't.  Check to see if the file exists.
   int FileDescriptor = -1;
-  struct stat StatBuf;
-  if (getStatValue(InterndFileName, StatBuf, true,
+  FileData Data;
+  if (getStatValue(InterndFileName, Data, true,
                    openFile ? &FileDescriptor : 0)) {
     // There's no real file at the given path.
     if (!CacheFailure)
@@ -405,7 +327,8 @@
 
   // It exists.  See if we have already opened a file with the same inode.
   // This occurs when one dir is symlinked to another, for example.
-  FileEntry &UFE = UniqueRealFiles.getFile(InterndFileName, StatBuf);
+  FileEntry &UFE =
+      UniqueRealFiles.getFile(Data.UniqueID, Data.IsNamedPipe, Data.InPCH);
 
   NamedFileEnt.setValue(&UFE);
   if (UFE.getName()) { // Already have an entry with this inode, return it.
@@ -420,8 +343,8 @@
   // FIXME: Change the name to be a char* that points back to the
   // 'SeenFileEntries' key.
   UFE.Name    = InterndFileName;
-  UFE.Size    = StatBuf.st_size;
-  UFE.ModTime = StatBuf.st_mtime;
+  UFE.Size = Data.Size;
+  UFE.ModTime = Data.ModTime;
   UFE.Dir     = DirInfo;
   UFE.UID     = NextFileUID++;
   UFE.FD      = FileDescriptor;
@@ -458,12 +381,12 @@
          "The directory of a virtual file should already be in the cache.");
 
   // Check to see if the file exists. If so, drop the virtual file
-  struct stat StatBuf;
+  FileData Data;
   const char *InterndFileName = NamedFileEnt.getKeyData();
-  if (getStatValue(InterndFileName, StatBuf, true, 0) == 0) {
-    StatBuf.st_size = Size;
-    StatBuf.st_mtime = ModificationTime;
-    UFE = &UniqueRealFiles.getFile(InterndFileName, StatBuf);
+  if (getStatValue(InterndFileName, Data, true, 0) == 0) {
+    Data.Size = Size;
+    Data.ModTime = ModificationTime;
+    UFE = &UniqueRealFiles.getFile(Data.UniqueID, Data.IsNamedPipe, Data.InPCH);
 
     NamedFileEnt.setValue(UFE);
 
@@ -572,19 +495,19 @@
 /// if the path points to a virtual file or does not exist, or returns
 /// false if it's an existent real file.  If FileDescriptor is NULL,
 /// do directory look-up instead of file look-up.
-bool FileManager::getStatValue(const char *Path, struct stat &StatBuf,
-                               bool isFile, int *FileDescriptor) {
+bool FileManager::getStatValue(const char *Path, FileData &Data, bool isFile,
+                               int *FileDescriptor) {
   // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
   // absolute!
   if (FileSystemOpts.WorkingDir.empty())
-    return FileSystemStatCache::get(Path, StatBuf, isFile, FileDescriptor,
+    return FileSystemStatCache::get(Path, Data, isFile, FileDescriptor,
                                     StatCache.get());
 
   SmallString<128> FilePath(Path);
   FixupRelativePath(FilePath);
 
-  return FileSystemStatCache::get(FilePath.c_str(), StatBuf,
-                                  isFile, FileDescriptor, StatCache.get());
+  return FileSystemStatCache::get(FilePath.c_str(), Data, isFile,
+                                  FileDescriptor, StatCache.get());
 }
 
 bool FileManager::getNoncachedStatValue(StringRef Path,
diff --git a/lib/Basic/FileSystemStatCache.cpp b/lib/Basic/FileSystemStatCache.cpp
index b71259e..7a01bff 100644
--- a/lib/Basic/FileSystemStatCache.cpp
+++ b/lib/Basic/FileSystemStatCache.cpp
@@ -30,6 +30,16 @@
 
 void FileSystemStatCache::anchor() { }
 
+static void copyStatusToFileData(const llvm::sys::fs::file_status &Status,
+                                 FileData &Data) {
+  Data.Size = Status.getSize();
+  Data.ModTime = Status.getLastModificationTime().toEpochTime();
+  Data.UniqueID = Status.getUniqueID();
+  Data.IsDirectory = is_directory(Status);
+  Data.IsNamedPipe = Status.type() == llvm::sys::fs::file_type::fifo_file;
+  Data.InPCH = false;
+}
+
 /// FileSystemStatCache::get - Get the 'stat' information for the specified
 /// path, using the cache to accelerate it if possible.  This returns true if
 /// the path does not exist or false if it exists.
@@ -39,19 +49,24 @@
 /// success for directories (not files).  On a successful file lookup, the
 /// implementation can optionally fill in FileDescriptor with a valid
 /// descriptor and the client guarantees that it will close it.
-bool FileSystemStatCache::get(const char *Path, struct stat &StatBuf,
-                              bool isFile, int *FileDescriptor,
-                              FileSystemStatCache *Cache) {
+bool FileSystemStatCache::get(const char *Path, FileData &Data, bool isFile,
+                              int *FileDescriptor, FileSystemStatCache *Cache) {
   LookupResult R;
   bool isForDir = !isFile;
 
   // If we have a cache, use it to resolve the stat query.
   if (Cache)
-    R = Cache->getStat(Path, StatBuf, isFile, FileDescriptor);
+    R = Cache->getStat(Path, Data, isFile, FileDescriptor);
   else if (isForDir || !FileDescriptor) {
     // If this is a directory or a file descriptor is not needed and we have
     // no cache, just go to the file system.
-    R = ::stat(Path, &StatBuf) != 0 ? CacheMissing : CacheExists;
+    llvm::sys::fs::file_status Status;
+    if (llvm::sys::fs::status(Path, Status)) {
+      R = CacheMissing;
+    } else {
+      R = CacheExists;
+      copyStatusToFileData(Status, Data);
+    }
   } else {
     // Otherwise, we have to go to the filesystem.  We can always just use
     // 'stat' here, but (for files) the client is asking whether the file exists
@@ -69,9 +84,11 @@
       // Otherwise, the open succeeded.  Do an fstat to get the information
       // about the file.  We'll end up returning the open file descriptor to the
       // client to do what they please with it.
-      if (::fstat(*FileDescriptor, &StatBuf) == 0)
+      llvm::sys::fs::file_status Status;
+      if (!llvm::sys::fs::status(*FileDescriptor, Status)) {
         R = CacheExists;
-      else {
+        copyStatusToFileData(Status, Data);
+      } else {
         // fstat rarely fails.  If it does, claim the initial open didn't
         // succeed.
         R = CacheMissing;
@@ -86,7 +103,7 @@
   
   // If the path exists, make sure that its "directoryness" matches the clients
   // demands.
-  if (S_ISDIR(StatBuf.st_mode) != isForDir) {
+  if (Data.IsDirectory != isForDir) {
     // If not, close the file if opened.
     if (FileDescriptor && *FileDescriptor != -1) {
       ::close(*FileDescriptor);
@@ -99,12 +116,11 @@
   return false;
 }
 
-
 MemorizeStatCalls::LookupResult
-MemorizeStatCalls::getStat(const char *Path, struct stat &StatBuf,
-                           bool isFile, int *FileDescriptor) {
-  LookupResult Result = statChained(Path, StatBuf, isFile, FileDescriptor);
-  
+MemorizeStatCalls::getStat(const char *Path, FileData &Data, bool isFile,
+                           int *FileDescriptor) {
+  LookupResult Result = statChained(Path, Data, isFile, FileDescriptor);
+
   // Do not cache failed stats, it is easy to construct common inconsistent
   // situations if we do, and they are not important for PCH performance (which
   // currently only needs the stats to construct the initial FileManager
@@ -113,8 +129,8 @@
     return Result;
   
   // Cache file 'stat' results and directories with absolutely paths.
-  if (!S_ISDIR(StatBuf.st_mode) || llvm::sys::path::is_absolute(Path))
-    StatCalls[Path] = StatBuf;
-  
+  if (!Data.IsDirectory || llvm::sys::path::is_absolute(Path))
+    StatCalls[Path] = Data;
+
   return Result;
 }