[cfe-commits] r125407 - /cfe/trunk/lib/Basic/FileManager.cpp

Zhanyong Wan wan at google.com
Fri Feb 11 13:25:35 PST 2011


Author: wan
Date: Fri Feb 11 15:25:35 2011
New Revision: 125407

URL: http://llvm.org/viewvc/llvm-project?rev=125407&view=rev
Log:
Uses llvm::sys::path instead of hand-rolled algorithm in FileManager.
Reviewed by dgregor.

Modified:
    cfe/trunk/lib/Basic/FileManager.cpp

Modified: cfe/trunk/lib/Basic/FileManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/FileManager.cpp?rev=125407&r1=125406&r2=125407&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/FileManager.cpp (original)
+++ cfe/trunk/lib/Basic/FileManager.cpp Fri Feb 11 15:25:35 2011
@@ -64,8 +64,6 @@
 
 #ifdef LLVM_ON_WIN32
 
-#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/' || (x) == '\\')
-
 namespace {
   static std::string GetFullPath(const char *relPath) {
     char *absPathStrPtr = _fullpath(NULL, relPath, 0);
@@ -122,8 +120,6 @@
 
 #else
 
-#define IS_DIR_SEPARATOR_CHAR(x) ((x) == '/')
-
 class FileManager::UniqueDirContainer {
   /// UniqueDirs - Cache from ID's to existing directories/files.
   std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
@@ -222,44 +218,27 @@
 /// Filename can point to either a real file or a virtual file.
 static const DirectoryEntry *getDirectoryFromFile(FileManager &FileMgr,
                                                   llvm::StringRef Filename) {
-  // Figure out what directory it is in.   If the string contains a / in it,
-  // strip off everything after it.
-  // FIXME: this logic should be in sys::Path.
-  size_t SlashPos = Filename.size();
-  if (SlashPos == 0 || IS_DIR_SEPARATOR_CHAR(Filename[SlashPos-1]))
-    return NULL;  // If Filename is empty or a directory.
+  if (Filename.empty())
+    return NULL;
 
-  while (SlashPos != 0 && !IS_DIR_SEPARATOR_CHAR(Filename[SlashPos-1]))
-    --SlashPos;
+  if (llvm::sys::path::is_separator(Filename[Filename.size() - 1]))
+    return NULL;  // If Filename is a directory.
 
+  llvm::StringRef DirName = llvm::sys::path::parent_path(Filename);
   // Use the current directory if file has no path component.
-  if (SlashPos == 0)
-    return FileMgr.getDirectory(".");
-
-  // Ignore repeated //'s.
-  while (SlashPos != 0 && IS_DIR_SEPARATOR_CHAR(Filename[SlashPos-1]))
-    --SlashPos;
+  if (DirName.empty())
+    DirName = ".";
 
-  return FileMgr.getDirectory(Filename.substr(0, SlashPos));
+  return FileMgr.getDirectory(DirName);
 }
 
 /// Add all ancestors of the given path (pointing to either a file or
 /// a directory) as virtual directories.
 void FileManager::addAncestorsAsVirtualDirs(llvm::StringRef Path) {
-  size_t SlashPos = Path.size();
-
-  // Find the beginning of the last segment in Path.
-  while (SlashPos != 0 && !IS_DIR_SEPARATOR_CHAR(Path[SlashPos-1]))
-    --SlashPos;
-
-  // Ignore repeated //'s.
-  while (SlashPos != 0 && IS_DIR_SEPARATOR_CHAR(Path[SlashPos-1]))
-    --SlashPos;
-
-  if (SlashPos == 0)
+  llvm::StringRef DirName = llvm::sys::path::parent_path(Path);
+  if (DirName.empty())
     return;
 
-  llvm::StringRef DirName = Path.substr(0, SlashPos);
   llvm::StringMapEntry<DirectoryEntry *> &NamedDirEnt =
     SeenDirEntries.GetOrCreateValue(DirName);
 
@@ -286,7 +265,7 @@
 ///
 const DirectoryEntry *FileManager::getDirectory(llvm::StringRef DirName) {
   // stat doesn't like trailing separators (at least on Windows).
-  if (DirName.size() > 1 && IS_DIR_SEPARATOR_CHAR(DirName.back()))
+  if (DirName.size() > 1 && llvm::sys::path::is_separator(DirName.back()))
     DirName = DirName.substr(0, DirName.size()-1);
 
   ++NumDirLookups;





More information about the cfe-commits mailing list