summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMehdi Amini <mehdi.amini@apple.com>2016-10-10 22:52:47 +0000
committerMehdi Amini <mehdi.amini@apple.com>2016-10-10 22:52:47 +0000
commit8bd553fc8d3074d54421845a80ea5ccc3a4c44a9 (patch)
tree997d123ec176455f52c46a2bd3b8b0789173a7ff
parent24a15d69fc8bff2dd1c53bd4ecf59b5b931326d2 (diff)
Store FileEntry::Filename as a StringRef instead of raw pointer (NFC)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@283815 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/clang/Basic/FileManager.h4
-rw-r--r--lib/Basic/FileManager.cpp2
-rw-r--r--lib/CodeGen/CGObjCGNU.cpp2
-rw-r--r--lib/Lex/HeaderSearch.cpp18
-rw-r--r--lib/Lex/PTHLexer.cpp6
-rw-r--r--lib/Serialization/ASTReader.cpp3
-rw-r--r--lib/Serialization/ASTReaderInternals.h2
-rw-r--r--lib/Serialization/ASTWriter.cpp12
-rw-r--r--tools/libclang/CXSourceLocation.cpp7
-rw-r--r--unittests/Basic/FileManagerTest.cpp4
10 files changed, 31 insertions, 29 deletions
diff --git a/include/clang/Basic/FileManager.h b/include/clang/Basic/FileManager.h
index b6a9ca7028..d16543ff83 100644
--- a/include/clang/Basic/FileManager.h
+++ b/include/clang/Basic/FileManager.h
@@ -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; }
diff --git a/lib/Basic/FileManager.cpp b/lib/Basic/FileManager.cpp
index 94286e2817..29b8f38220 100644
--- a/lib/Basic/FileManager.cpp
+++ b/lib/Basic/FileManager.cpp
@@ -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 =
diff --git a/lib/CodeGen/CGObjCGNU.cpp b/lib/CodeGen/CGObjCGNU.cpp
index caafef84c3..94d7ac336b 100644
--- a/lib/CodeGen/CGObjCGNU.cpp
+++ b/lib/CodeGen/CGObjCGNU.cpp
@@ -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);
diff --git a/lib/Lex/HeaderSearch.cpp b/lib/Lex/HeaderSearch.cpp
index c2c909e361..cf315767b6 100644
--- a/lib/Lex/HeaderSearch.cpp
+++ b/lib/Lex/HeaderSearch.cpp
@@ -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);
}
diff --git a/lib/Lex/PTHLexer.cpp b/lib/Lex/PTHLexer.cpp
index 102aaf9133..2ccfcc4131 100644
--- a/lib/Lex/PTHLexer.cpp
+++ b/lib/Lex/PTHLexer.cpp
@@ -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,
diff --git a/lib/Serialization/ASTReader.cpp b/lib/Serialization/ASTReader.cpp
index 52e06e482e..8ce118ce71 100644
--- a/lib/Serialization/ASTReader.cpp
+++ b/lib/Serialization/ASTReader.cpp
@@ -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.
diff --git a/lib/Serialization/ASTReaderInternals.h b/lib/Serialization/ASTReaderInternals.h
index 250c8b180d..cdfed9bab7 100644
--- a/lib/Serialization/ASTReaderInternals.h
+++ b/lib/Serialization/ASTReaderInternals.h
@@ -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;
diff --git a/lib/Serialization/ASTWriter.cpp b/lib/Serialization/ASTWriter.cpp
index 335a9320e3..211d111fe0 100644
--- a/lib/Serialization/ASTWriter.cpp
+++ b/lib/Serialization/ASTWriter.cpp
@@ -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 };
diff --git a/tools/libclang/CXSourceLocation.cpp b/tools/libclang/CXSourceLocation.cpp
index 1b7464b25a..e75f6a757f 100644
--- a/tools/libclang/CXSourceLocation.cpp
+++ b/tools/libclang/CXSourceLocation.cpp
@@ -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;
}
diff --git a/unittests/Basic/FileManagerTest.cpp b/unittests/Basic/FileManagerTest.cpp
index d8d85dd76c..2c606715c2 100644
--- a/unittests/Basic/FileManagerTest.cpp
+++ b/unittests/Basic/FileManagerTest.cpp
@@ -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);