Browse Source

Turn FileManager DirectoryEntry::Name from raw pointer to StringRef (NFC)

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@283856 91177308-0d34-0410-b5e6-96231b3b80d8
Mehdi Amini 8 years ago
parent
commit
b042fd3636

+ 3 - 4
include/clang/Basic/FileManager.h

@@ -38,11 +38,10 @@ class FileSystemStatCache;
 /// \brief Cached information about one directory (either on disk or in
 /// \brief Cached information about one directory (either on disk or in
 /// the virtual file system).
 /// the virtual file system).
 class DirectoryEntry {
 class DirectoryEntry {
-  const char *Name;   // Name of the directory.
+  StringRef Name; // Name of the directory.
   friend class FileManager;
   friend class FileManager;
 public:
 public:
-  DirectoryEntry() : Name(nullptr) {}
-  const char *getName() const { return Name; }
+  StringRef getName() const { return Name; }
 };
 };
 
 
 /// \brief Cached information about one file (either on disk
 /// \brief Cached information about one file (either on disk
@@ -165,7 +164,7 @@ class FileManager : public RefCountedBase<FileManager> {
   // Caching.
   // Caching.
   std::unique_ptr<FileSystemStatCache> StatCache;
   std::unique_ptr<FileSystemStatCache> StatCache;
 
 
-  bool getStatValue(const char *Path, FileData &Data, bool isFile,
+  bool getStatValue(StringRef Path, FileData &Data, bool isFile,
                     std::unique_ptr<vfs::File> *F);
                     std::unique_ptr<vfs::File> *F);
 
 
   /// Add all ancestors of the given path (pointing to either a file
   /// Add all ancestors of the given path (pointing to either a file

+ 4 - 4
include/clang/Basic/FileSystemStatCache.h

@@ -68,7 +68,7 @@ public:
   /// success for directories (not files).  On a successful file lookup, the
   /// success for directories (not files).  On a successful file lookup, the
   /// implementation can optionally fill in \p F with a valid \p File object and
   /// implementation can optionally fill in \p F with a valid \p File object and
   /// the client guarantees that it will close it.
   /// the client guarantees that it will close it.
-  static bool get(const char *Path, FileData &Data, bool isFile,
+  static bool get(StringRef Path, FileData &Data, bool isFile,
                   std::unique_ptr<vfs::File> *F, FileSystemStatCache *Cache,
                   std::unique_ptr<vfs::File> *F, FileSystemStatCache *Cache,
                   vfs::FileSystem &FS);
                   vfs::FileSystem &FS);
 
 
@@ -92,11 +92,11 @@ protected:
   // FIXME: The pointer here is a non-owning/optional reference to the
   // FIXME: The pointer here is a non-owning/optional reference to the
   // unique_ptr. Optional<unique_ptr<vfs::File>&> might be nicer, but
   // unique_ptr. Optional<unique_ptr<vfs::File>&> might be nicer, but
   // Optional needs some work to support references so this isn't possible yet.
   // Optional needs some work to support references so this isn't possible yet.
-  virtual LookupResult getStat(const char *Path, FileData &Data, bool isFile,
+  virtual LookupResult getStat(StringRef Path, FileData &Data, bool isFile,
                                std::unique_ptr<vfs::File> *F,
                                std::unique_ptr<vfs::File> *F,
                                vfs::FileSystem &FS) = 0;
                                vfs::FileSystem &FS) = 0;
 
 
-  LookupResult statChained(const char *Path, FileData &Data, bool isFile,
+  LookupResult statChained(StringRef Path, FileData &Data, bool isFile,
                            std::unique_ptr<vfs::File> *F, vfs::FileSystem &FS) {
                            std::unique_ptr<vfs::File> *F, vfs::FileSystem &FS) {
     if (FileSystemStatCache *Next = getNextStatCache())
     if (FileSystemStatCache *Next = getNextStatCache())
       return Next->getStat(Path, Data, isFile, F, FS);
       return Next->getStat(Path, Data, isFile, F, FS);
@@ -121,7 +121,7 @@ public:
   iterator begin() const { return StatCalls.begin(); }
   iterator begin() const { return StatCalls.begin(); }
   iterator end() const { return StatCalls.end(); }
   iterator end() const { return StatCalls.end(); }
 
 
-  LookupResult getStat(const char *Path, FileData &Data, bool isFile,
+  LookupResult getStat(StringRef Path, FileData &Data, bool isFile,
                        std::unique_ptr<vfs::File> *F,
                        std::unique_ptr<vfs::File> *F,
                        vfs::FileSystem &FS) override;
                        vfs::FileSystem &FS) override;
 };
 };

+ 6 - 6
lib/Basic/FileManager.cpp

@@ -140,7 +140,7 @@ void FileManager::addAncestorsAsVirtualDirs(StringRef Path) {
 
 
   // Add the virtual directory to the cache.
   // Add the virtual directory to the cache.
   auto UDE = llvm::make_unique<DirectoryEntry>();
   auto UDE = llvm::make_unique<DirectoryEntry>();
-  UDE->Name = NamedDirEnt.first().data();
+  UDE->Name = NamedDirEnt.first();
   NamedDirEnt.second = UDE.get();
   NamedDirEnt.second = UDE.get();
   VirtualDirectoryEntries.push_back(std::move(UDE));
   VirtualDirectoryEntries.push_back(std::move(UDE));
 
 
@@ -185,7 +185,7 @@ const DirectoryEntry *FileManager::getDirectory(StringRef DirName,
 
 
   // Get the null-terminated directory name as stored as the key of the
   // Get the null-terminated directory name as stored as the key of the
   // SeenDirEntries map.
   // SeenDirEntries map.
-  const char *InterndDirName = NamedDirEnt.first().data();
+  StringRef InterndDirName = NamedDirEnt.first();
 
 
   // Check to see if the directory exists.
   // Check to see if the directory exists.
   FileData Data;
   FileData Data;
@@ -203,7 +203,7 @@ const DirectoryEntry *FileManager::getDirectory(StringRef DirName,
   DirectoryEntry &UDE = UniqueRealDirs[Data.UniqueID];
   DirectoryEntry &UDE = UniqueRealDirs[Data.UniqueID];
 
 
   NamedDirEnt.second = &UDE;
   NamedDirEnt.second = &UDE;
-  if (!UDE.getName()) {
+  if (UDE.getName().empty()) {
     // We don't have this directory yet, add it.  We use the string
     // We don't have this directory yet, add it.  We use the string
     // key from the SeenDirEntries map as the string.
     // key from the SeenDirEntries map as the string.
     UDE.Name  = InterndDirName;
     UDE.Name  = InterndDirName;
@@ -232,7 +232,7 @@ const FileEntry *FileManager::getFile(StringRef Filename, bool openFile,
 
 
   // Get the null-terminated file name as stored as the key of the
   // Get the null-terminated file name as stored as the key of the
   // SeenFileEntries map.
   // SeenFileEntries map.
-  const char *InterndFileName = NamedFileEnt.first().data();
+  StringRef InterndFileName = NamedFileEnt.first();
 
 
   // Look up the directory for the file.  When looking up something like
   // Look up the directory for the file.  When looking up something like
   // sys/foo.h we'll discover all of the search directories that have a 'sys'
   // sys/foo.h we'll discover all of the search directories that have a 'sys'
@@ -463,7 +463,7 @@ FileManager::getBufferForFile(StringRef Filename) {
 /// if the path points to a virtual file or does not exist, or returns
 /// 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,
 /// false if it's an existent real file.  If FileDescriptor is NULL,
 /// do directory look-up instead of file look-up.
 /// do directory look-up instead of file look-up.
-bool FileManager::getStatValue(const char *Path, FileData &Data, bool isFile,
+bool FileManager::getStatValue(StringRef Path, FileData &Data, bool isFile,
                                std::unique_ptr<vfs::File> *F) {
                                std::unique_ptr<vfs::File> *F) {
   // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
   // FIXME: FileSystemOpts shouldn't be passed in here, all paths should be
   // absolute!
   // absolute!
@@ -535,7 +535,7 @@ StringRef FileManager::getCanonicalName(const DirectoryEntry *Dir) {
 
 
 #ifdef LLVM_ON_UNIX
 #ifdef LLVM_ON_UNIX
   char CanonicalNameBuf[PATH_MAX];
   char CanonicalNameBuf[PATH_MAX];
-  if (realpath(Dir->getName(), CanonicalNameBuf))
+  if (realpath(Dir->getName().str().c_str(), CanonicalNameBuf))
     CanonicalName = StringRef(CanonicalNameBuf).copy(CanonicalNameStorage);
     CanonicalName = StringRef(CanonicalNameBuf).copy(CanonicalNameStorage);
 #else
 #else
   SmallString<256> CanonicalNameBuf(CanonicalName);
   SmallString<256> CanonicalNameBuf(CanonicalName);

+ 2 - 2
lib/Basic/FileSystemStatCache.cpp

@@ -40,7 +40,7 @@ static void copyStatusToFileData(const vfs::Status &Status,
 /// success for directories (not files).  On a successful file lookup, the
 /// success for directories (not files).  On a successful file lookup, the
 /// implementation can optionally fill in FileDescriptor with a valid
 /// implementation can optionally fill in FileDescriptor with a valid
 /// descriptor and the client guarantees that it will close it.
 /// descriptor and the client guarantees that it will close it.
-bool FileSystemStatCache::get(const char *Path, FileData &Data, bool isFile,
+bool FileSystemStatCache::get(StringRef Path, FileData &Data, bool isFile,
                               std::unique_ptr<vfs::File> *F,
                               std::unique_ptr<vfs::File> *F,
                               FileSystemStatCache *Cache, vfs::FileSystem &FS) {
                               FileSystemStatCache *Cache, vfs::FileSystem &FS) {
   LookupResult R;
   LookupResult R;
@@ -107,7 +107,7 @@ bool FileSystemStatCache::get(const char *Path, FileData &Data, bool isFile,
 }
 }
 
 
 MemorizeStatCalls::LookupResult
 MemorizeStatCalls::LookupResult
-MemorizeStatCalls::getStat(const char *Path, FileData &Data, bool isFile,
+MemorizeStatCalls::getStat(StringRef Path, FileData &Data, bool isFile,
                            std::unique_ptr<vfs::File> *F, vfs::FileSystem &FS) {
                            std::unique_ptr<vfs::File> *F, vfs::FileSystem &FS) {
   LookupResult Result = statChained(Path, Data, isFile, F, FS);
   LookupResult Result = statChained(Path, Data, isFile, F, FS);
 
 

+ 9 - 6
lib/Frontend/CacheTokens.cpp

@@ -58,18 +58,21 @@ public:
 
 
 
 
 class PTHEntryKeyVariant {
 class PTHEntryKeyVariant {
-  union { const FileEntry* FE; const char* Path; };
+  union {
+    const FileEntry *FE;
+    StringRef Path;
+  };
   enum { IsFE = 0x1, IsDE = 0x2, IsNoExist = 0x0 } Kind;
   enum { IsFE = 0x1, IsDE = 0x2, IsNoExist = 0x0 } Kind;
   FileData *Data;
   FileData *Data;
 
 
 public:
 public:
   PTHEntryKeyVariant(const FileEntry *fe) : FE(fe), Kind(IsFE), Data(nullptr) {}
   PTHEntryKeyVariant(const FileEntry *fe) : FE(fe), Kind(IsFE), Data(nullptr) {}
 
 
-  PTHEntryKeyVariant(FileData *Data, const char *path)
-      : Path(path), Kind(IsDE), Data(new FileData(*Data)) {}
+  PTHEntryKeyVariant(FileData *Data, StringRef Path)
+      : Path(Path), Kind(IsDE), Data(new FileData(*Data)) {}
 
 
-  explicit PTHEntryKeyVariant(const char *path)
-      : Path(path), Kind(IsNoExist), Data(nullptr) {}
+  explicit PTHEntryKeyVariant(StringRef Path)
+      : Path(Path), Kind(IsNoExist), Data(nullptr) {}
 
 
   bool isFile() const { return Kind == IsFE; }
   bool isFile() const { return Kind == IsFE; }
 
 
@@ -549,7 +552,7 @@ public:
   StatListener(PTHMap &pm) : PM(pm) {}
   StatListener(PTHMap &pm) : PM(pm) {}
   ~StatListener() override {}
   ~StatListener() override {}
 
 
-  LookupResult getStat(const char *Path, FileData &Data, bool isFile,
+  LookupResult getStat(StringRef Path, FileData &Data, bool isFile,
                        std::unique_ptr<vfs::File> *F,
                        std::unique_ptr<vfs::File> *F,
                        vfs::FileSystem &FS) override {
                        vfs::FileSystem &FS) override {
     LookupResult Result = statChained(Path, Data, isFile, F, FS);
     LookupResult Result = statChained(Path, Data, isFile, F, FS);

+ 1 - 1
lib/Lex/HeaderSearch.cpp

@@ -1461,7 +1461,7 @@ std::string HeaderSearch::suggestPathToFileForDiagnostics(const FileEntry *File,
     if (!SearchDirs[I].isNormalDir())
     if (!SearchDirs[I].isNormalDir())
       continue;
       continue;
 
 
-    const char *Dir = SearchDirs[I].getDir()->getName();
+    StringRef Dir = SearchDirs[I].getDir()->getName();
     for (auto NI = llvm::sys::path::begin(Name),
     for (auto NI = llvm::sys::path::begin(Name),
               NE = llvm::sys::path::end(Name),
               NE = llvm::sys::path::end(Name),
               DI = llvm::sys::path::begin(Dir),
               DI = llvm::sys::path::begin(Dir),

+ 3 - 3
lib/Lex/PTHLexer.cpp

@@ -644,10 +644,10 @@ public:
 
 
 class PTHStatLookupTrait : public PTHFileLookupCommonTrait {
 class PTHStatLookupTrait : public PTHFileLookupCommonTrait {
 public:
 public:
-  typedef const char* external_key_type;  // const char*
+  typedef StringRef external_key_type; // const char*
   typedef PTHStatData data_type;
   typedef PTHStatData data_type;
 
 
-  static internal_key_type GetInternalKey(const char *path) {
+  static internal_key_type GetInternalKey(StringRef path) {
     // The key 'kind' doesn't matter here because it is ignored in EqualKey.
     // The key 'kind' doesn't matter here because it is ignored in EqualKey.
     return std::make_pair((unsigned char) 0x0, path);
     return std::make_pair((unsigned char) 0x0, path);
   }
   }
@@ -694,7 +694,7 @@ public:
       : Cache(FL.getNumBuckets(), FL.getNumEntries(), FL.getBuckets(),
       : Cache(FL.getNumBuckets(), FL.getNumEntries(), FL.getBuckets(),
               FL.getBase()) {}
               FL.getBase()) {}
 
 
-  LookupResult getStat(const char *Path, FileData &Data, bool isFile,
+  LookupResult getStat(StringRef Path, FileData &Data, bool isFile,
                        std::unique_ptr<vfs::File> *F,
                        std::unique_ptr<vfs::File> *F,
                        vfs::FileSystem &FS) override {
                        vfs::FileSystem &FS) override {
     // Do the lookup for the file's data in the PTH file.
     // Do the lookup for the file's data in the PTH file.

+ 8 - 8
unittests/Basic/FileManagerTest.cpp

@@ -52,7 +52,7 @@ public:
   }
   }
 
 
   // Implement FileSystemStatCache::getStat().
   // Implement FileSystemStatCache::getStat().
-  LookupResult getStat(const char *Path, FileData &Data, bool isFile,
+  LookupResult getStat(StringRef Path, FileData &Data, bool isFile,
                        std::unique_ptr<vfs::File> *F,
                        std::unique_ptr<vfs::File> *F,
                        vfs::FileSystem &FS) override {
                        vfs::FileSystem &FS) override {
     if (StatCalls.count(Path) != 0) {
     if (StatCalls.count(Path) != 0) {
@@ -82,14 +82,14 @@ TEST_F(FileManagerTest, getVirtualFileSetsTheDirFieldCorrectly) {
 
 
   const DirectoryEntry *dir = file->getDir();
   const DirectoryEntry *dir = file->getDir();
   ASSERT_TRUE(dir != nullptr);
   ASSERT_TRUE(dir != nullptr);
-  EXPECT_STREQ(".", dir->getName());
+  EXPECT_EQ(".", dir->getName());
 
 
   file = manager.getVirtualFile("x/y/z.cpp", 42, 0);
   file = manager.getVirtualFile("x/y/z.cpp", 42, 0);
   ASSERT_TRUE(file != nullptr);
   ASSERT_TRUE(file != nullptr);
 
 
   dir = file->getDir();
   dir = file->getDir();
   ASSERT_TRUE(dir != nullptr);
   ASSERT_TRUE(dir != nullptr);
-  EXPECT_STREQ("x/y", dir->getName());
+  EXPECT_EQ("x/y", dir->getName());
 }
 }
 
 
 // Before any virtual file is added, no virtual directory exists.
 // Before any virtual file is added, no virtual directory exists.
@@ -115,11 +115,11 @@ TEST_F(FileManagerTest, getVirtualFileCreatesDirectoryEntriesForAncestors) {
 
 
   const DirectoryEntry *dir = manager.getDirectory("virtual/dir");
   const DirectoryEntry *dir = manager.getDirectory("virtual/dir");
   ASSERT_TRUE(dir != nullptr);
   ASSERT_TRUE(dir != nullptr);
-  EXPECT_STREQ("virtual/dir", dir->getName());
+  EXPECT_EQ("virtual/dir", dir->getName());
 
 
   dir = manager.getDirectory("virtual");
   dir = manager.getDirectory("virtual");
   ASSERT_TRUE(dir != nullptr);
   ASSERT_TRUE(dir != nullptr);
-  EXPECT_STREQ("virtual", dir->getName());
+  EXPECT_EQ("virtual", dir->getName());
 }
 }
 
 
 // getFile() returns non-NULL if a real file exists at the given path.
 // getFile() returns non-NULL if a real file exists at the given path.
@@ -144,7 +144,7 @@ TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingRealFile) {
 
 
   const DirectoryEntry *dir = file->getDir();
   const DirectoryEntry *dir = file->getDir();
   ASSERT_TRUE(dir != nullptr);
   ASSERT_TRUE(dir != nullptr);
-  EXPECT_STREQ("/tmp", dir->getName());
+  EXPECT_EQ("/tmp", dir->getName());
 
 
 #ifdef LLVM_ON_WIN32
 #ifdef LLVM_ON_WIN32
   file = manager.getFile(FileName);
   file = manager.getFile(FileName);
@@ -152,7 +152,7 @@ TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingRealFile) {
 
 
   dir = file->getDir();
   dir = file->getDir();
   ASSERT_TRUE(dir != NULL);
   ASSERT_TRUE(dir != NULL);
-  EXPECT_STREQ(DirName, dir->getName());
+  EXPECT_EQ(DirName, dir->getName());
 #endif
 #endif
 }
 }
 
 
@@ -168,7 +168,7 @@ TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingVirtualFile) {
 
 
   const DirectoryEntry *dir = file->getDir();
   const DirectoryEntry *dir = file->getDir();
   ASSERT_TRUE(dir != nullptr);
   ASSERT_TRUE(dir != nullptr);
-  EXPECT_STREQ("virtual/dir", dir->getName());
+  EXPECT_EQ("virtual/dir", dir->getName());
 }
 }
 
 
 // getFile() returns different FileEntries for different paths when
 // getFile() returns different FileEntries for different paths when