]> granicus.if.org Git - clang/commitdiff
Store FileEntry::Filename as a StringRef instead of raw pointer (NFC)
authorMehdi Amini <mehdi.amini@apple.com>
Mon, 10 Oct 2016 22:52:47 +0000 (22:52 +0000)
committerMehdi Amini <mehdi.amini@apple.com>
Mon, 10 Oct 2016 22:52:47 +0000 (22:52 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@283815 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/FileManager.h
lib/Basic/FileManager.cpp
lib/CodeGen/CGObjCGNU.cpp
lib/Lex/HeaderSearch.cpp
lib/Lex/PTHLexer.cpp
lib/Serialization/ASTReader.cpp
lib/Serialization/ASTReaderInternals.h
lib/Serialization/ASTWriter.cpp
tools/libclang/CXSourceLocation.cpp
unittests/Basic/FileManagerTest.cpp

index b6a9ca702842c38d08fed41387b2cba23826a2b0..d16543ff831a9ef6d8ec503d648789abd8deb2dc 100644 (file)
@@ -51,7 +51,7 @@ public:
 /// If the 'File' member is valid, then this FileEntry has an open file
 /// descriptor for the file.
 class FileEntry {
-  const char *Name;           // Name of the file.
+  StringRef Name;             // Name of the file.
   std::string RealPathName;   // Real path to the file; could be empty.
   off_t Size;                 // File size in bytes.
   time_t ModTime;             // Modification time of file.
@@ -82,7 +82,7 @@ public:
     assert(!isValid() && "Cannot copy an initialized FileEntry");
   }
 
-  const char *getName() const { return Name; }
+  StringRef getName() const { return Name; }
   StringRef tryGetRealPathName() const { return RealPathName; }
   bool isValid() const { return IsValid; }
   off_t getSize() const { return Size; }
index 94286e2817eaf54b95e8f4ce3a83ece3b4152c8b..29b8f382206d472cbd54f570d86fded944c2699a 100644 (file)
@@ -423,7 +423,7 @@ FileManager::getBufferForFile(const FileEntry *Entry, bool isVolatile,
   if (isVolatile)
     FileSize = -1;
 
-  const char *Filename = Entry->getName();
+  StringRef Filename = Entry->getName();
   // If the file is already open, use the open file descriptor.
   if (Entry->File) {
     auto Result =
index caafef84c3339dec45aead0657fc0a091431583c..94d7ac336b1221df505cefefc68328cbd9226bda 100644 (file)
@@ -2582,7 +2582,7 @@ llvm::Function *CGObjCGNU::ModuleInitFunction() {
   SourceManager &SM = CGM.getContext().getSourceManager();
   const FileEntry *mainFile = SM.getFileEntryForID(SM.getMainFileID());
   std::string path =
-    std::string(mainFile->getDir()->getName()) + '/' + mainFile->getName();
+      (Twine(mainFile->getDir()->getName()) + "/" + mainFile->getName()).str();
   Elements.push_back(MakeConstantString(path, ".objc_source_file_name"));
   Elements.push_back(SymTab);
 
index c2c909e361d6563dff30c19e8ae419bead97ffca..cf315767b61c89c18e1de8d6d2df16bc67ec3ac3 100644 (file)
@@ -847,17 +847,19 @@ LookupSubframeworkHeader(StringRef Filename,
   if (SlashPos == StringRef::npos) return nullptr;
 
   // Look up the base framework name of the ContextFileEnt.
-  const char *ContextName = ContextFileEnt->getName();
+  StringRef ContextName = ContextFileEnt->getName();
 
   // If the context info wasn't a framework, couldn't be a subframework.
   const unsigned DotFrameworkLen = 10;
-  const char *FrameworkPos = strstr(ContextName, ".framework");
-  if (FrameworkPos == nullptr ||
-      (FrameworkPos[DotFrameworkLen] != '/' && 
-       FrameworkPos[DotFrameworkLen] != '\\'))
+  auto FrameworkPos = ContextName.find(".framework");
+  if (FrameworkPos == StringRef::npos ||
+      (ContextName[FrameworkPos + DotFrameworkLen] != '/' &&
+       ContextName[FrameworkPos + DotFrameworkLen] != '\\'))
     return nullptr;
 
-  SmallString<1024> FrameworkName(ContextName, FrameworkPos+DotFrameworkLen+1);
+  SmallString<1024> FrameworkName(ContextName.data(), ContextName.data() +
+                                                          FrameworkPos +
+                                                          DotFrameworkLen + 1);
 
   // Append Frameworks/HIToolbox.framework/
   FrameworkName += "Frameworks/";
@@ -1449,7 +1451,7 @@ std::string HeaderSearch::suggestPathToFileForDiagnostics(const FileEntry *File,
   // FIXME: We assume that the path name currently cached in the FileEntry is
   // the most appropriate one for this analysis (and that it's spelled the same
   // way as the corresponding header search path).
-  const char *Name = File->getName();
+  StringRef Name = File->getName();
 
   unsigned BestPrefixLength = 0;
   unsigned BestSearchDir;
@@ -1492,5 +1494,5 @@ std::string HeaderSearch::suggestPathToFileForDiagnostics(const FileEntry *File,
 
   if (IsSystem)
     *IsSystem = BestPrefixLength ? BestSearchDir >= SystemDirIdx : false;
-  return Name + BestPrefixLength;
+  return Name.drop_front(BestPrefixLength);
 }
index 102aaf9133700709074f50ac4c2b7b7665923f7c..2ccfcc4131fdf95d14d728b751e0530c01019e19 100644 (file)
@@ -317,7 +317,7 @@ public:
 
 class PTHFileLookupCommonTrait {
 public:
-  typedef std::pair<unsigned char, const char*> internal_key_type;
+  typedef std::pair<unsigned char, StringRef> internal_key_type;
   typedef unsigned hash_value_type;
   typedef unsigned offset_type;
 
@@ -352,7 +352,7 @@ public:
   }
 
   static bool EqualKey(internal_key_type a, internal_key_type b) {
-    return a.first == b.first && strcmp(a.second, b.second) == 0;
+    return a.first == b.first && a.second == b.second;
   }
 
   static PTHFileData ReadData(const internal_key_type& k,
@@ -655,7 +655,7 @@ public:
   static bool EqualKey(internal_key_type a, internal_key_type b) {
     // When doing 'stat' lookups we don't care about the kind of 'a' and 'b',
     // just the paths.
-    return strcmp(a.second, b.second) == 0;
+    return a.second == b.second;
   }
 
   static data_type ReadData(const internal_key_type& k, const unsigned char* d,
index 52e06e482e1e883292f9203f5c02f891ae75dc83..8ce118ce7100fc0adbb033fd7e8ed0d28e3486cd 100644 (file)
@@ -1600,8 +1600,7 @@ bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
   if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
     return false;
 
-  if (llvm::sys::path::is_absolute(a.Filename) &&
-      strcmp(a.Filename, b.Filename) == 0)
+  if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
     return true;
   
   // Determine whether the actual files are equivalent.
index 250c8b180d41ed1c44d298f9ca5ba106aba697ff..cdfed9bab74baf42ec289b81f81d044f8fb4d660 100644 (file)
@@ -257,7 +257,7 @@ public:
   struct internal_key_type {
     off_t Size;
     time_t ModTime;
-    const char *Filename;
+    StringRef Filename;
     bool Imported;
   };
   typedef const internal_key_type &internal_key_ref;
index 335a9320e304ba52377648910f45c1c4916c77ff..211d111fe04fe8a2553ef4cb04bd8b61bcffc1ab 100644 (file)
@@ -1808,7 +1808,7 @@ namespace {
     
     struct key_type {
       const FileEntry *FE;
-      const char *Filename;
+      StringRef Filename;
     };
     typedef const key_type &key_type_ref;
     
@@ -1829,7 +1829,7 @@ namespace {
     EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
       using namespace llvm::support;
       endian::Writer<little> LE(Out);
-      unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
+      unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
       LE.write<uint16_t>(KeyLen);
       unsigned DataLen = 1 + 2 + 4 + 4;
       for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE))
@@ -1846,7 +1846,7 @@ namespace {
       KeyLen -= 8;
       LE.write<uint64_t>(Writer.getTimestampForOutput(key.FE));
       KeyLen -= 8;
-      Out.write(key.Filename, KeyLen);
+      Out.write(key.Filename.data(), KeyLen);
     }
     
     void EmitData(raw_ostream &Out, key_type_ref key,
@@ -1935,13 +1935,13 @@ void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
       continue;
 
     // Massage the file path into an appropriate form.
-    const char *Filename = File->getName();
+    StringRef Filename = File->getName();
     SmallString<128> FilenameTmp(Filename);
     if (PreparePathForOutput(FilenameTmp)) {
       // If we performed any translation on the file name at all, we need to
       // save this string, since the generator will refer to it later.
-      Filename = strdup(FilenameTmp.c_str());
-      SavedStrings.push_back(Filename);
+      Filename = StringRef(strdup(FilenameTmp.c_str()));
+      SavedStrings.push_back(Filename.data());
     }
 
     HeaderFileInfoTrait::key_type key = { File, Filename };
index 1b7464b25af9b152cf89643306ff83ae6ac4df5d..e75f6a757f467ed36543bae4d04ae33a1f229628 100644 (file)
@@ -139,16 +139,17 @@ CXSourceLocation clang_getLocation(CXTranslationUnit TU,
   if (SLoc.isInvalid()) {
     if (Log)
       *Log << llvm::format("(\"%s\", %d, %d) = invalid",
-                           File->getName(), line, column);
+                           File->getName().str().c_str(), line, column);
     return clang_getNullLocation();
   }
   
   CXSourceLocation CXLoc =
       cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc);
   if (Log)
-    *Log << llvm::format("(\"%s\", %d, %d) = ", File->getName(), line, column)
+    *Log << llvm::format("(\"%s\", %d, %d) = ", File->getName().str().c_str(),
+                         line, column)
          << CXLoc;
-  
+
   return CXLoc;
 }
   
index d8d85dd76c382d436b6b6f38626c0ae89d5e6530..2c606715c219f906a9062183cb81476e7e1bb1d5 100644 (file)
@@ -140,7 +140,7 @@ TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingRealFile) {
 
   const FileEntry *file = manager.getFile("/tmp/test");
   ASSERT_TRUE(file != nullptr);
-  EXPECT_STREQ("/tmp/test", file->getName());
+  EXPECT_EQ("/tmp/test", file->getName());
 
   const DirectoryEntry *dir = file->getDir();
   ASSERT_TRUE(dir != nullptr);
@@ -164,7 +164,7 @@ TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingVirtualFile) {
   manager.getVirtualFile("virtual/dir/bar.h", 100, 0);
   const FileEntry *file = manager.getFile("virtual/dir/bar.h");
   ASSERT_TRUE(file != nullptr);
-  EXPECT_STREQ("virtual/dir/bar.h", file->getName());
+  EXPECT_EQ("virtual/dir/bar.h", file->getName());
 
   const DirectoryEntry *dir = file->getDir();
   ASSERT_TRUE(dir != nullptr);