r175455 - libclang: don't store nul-terminated strings as StringRefs,
Dmitri Gribenko
gribozavr at gmail.com
Mon Feb 18 11:50:38 PST 2013
Author: gribozavr
Date: Mon Feb 18 13:50:38 2013
New Revision: 175455
URL: http://llvm.org/viewvc/llvm-project?rev=175455&view=rev
Log:
libclang: don't store nul-terminated strings as StringRefs,
if the nul-terminatedness property is important for clients.
Also, don't return the same CXString multiple times. This did not create a
correctness issue in practice because the CXString was of an CXS_Unmanaged
kind, and destruction was a no-op.
Modified:
cfe/trunk/tools/libclang/CXLoadedDiagnostic.cpp
cfe/trunk/tools/libclang/CXLoadedDiagnostic.h
Modified: cfe/trunk/tools/libclang/CXLoadedDiagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXLoadedDiagnostic.cpp?rev=175455&r1=175454&r2=175455&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXLoadedDiagnostic.cpp (original)
+++ cfe/trunk/tools/libclang/CXLoadedDiagnostic.cpp Mon Feb 18 13:50:38 2013
@@ -29,7 +29,7 @@ using namespace clang;
// Extend CXDiagnosticSetImpl which contains strings for diagnostics.
//===----------------------------------------------------------------------===//
-typedef llvm::DenseMap<unsigned, llvm::StringRef> Strings;
+typedef llvm::DenseMap<unsigned, const char *> Strings;
namespace {
class CXLoadedDiagnosticSetImpl : public CXDiagnosticSetImpl {
@@ -45,14 +45,13 @@ public:
FileSystemOptions FO;
FileManager FakeFiles;
llvm::DenseMap<unsigned, const FileEntry *> Files;
-
- llvm::StringRef makeString(StringRef Blob) {
+
+ /// \brief Copy the string into our own allocator.
+ const char *copyString(StringRef Blob) {
char *mem = Alloc.Allocate<char>(Blob.size() + 1);
memcpy(mem, Blob.data(), Blob.size());
- // Add a null terminator for those clients accessing the buffer
- // like a c-string.
mem[Blob.size()] = '\0';
- return llvm::StringRef(mem, Blob.size());
+ return mem;
}
};
}
@@ -135,7 +134,7 @@ CXString CXLoadedDiagnostic::getFixIt(un
assert(FixIt < FixIts.size());
if (ReplacementRange)
*ReplacementRange = FixIts[FixIt].first;
- return FixIts[FixIt].second;
+ return cxstring::createRef(FixIts[FixIt].second);
}
void CXLoadedDiagnostic::decodeLocation(CXSourceLocation location,
@@ -220,7 +219,7 @@ class DiagLoader {
bool allowEmptyString = false);
LoadResult readString(CXLoadedDiagnosticSetImpl &TopDiags,
- llvm::StringRef &RetStr,
+ const char *&RetStr,
llvm::StringRef errorContext,
RecordData &Record,
StringRef Blob,
@@ -434,7 +433,7 @@ LoadResult DiagLoader::readMetaBlock(llv
}
LoadResult DiagLoader::readString(CXLoadedDiagnosticSetImpl &TopDiags,
- llvm::StringRef &RetStr,
+ const char *&RetStr,
llvm::StringRef errorContext,
RecordData &Record,
StringRef Blob,
@@ -458,7 +457,7 @@ LoadResult DiagLoader::readString(CXLoad
return Failure;
}
- RetStr = TopDiags.makeString(Blob);
+ RetStr = TopDiags.copyString(Blob);
return Success;
}
@@ -468,7 +467,7 @@ LoadResult DiagLoader::readString(CXLoad
RecordData &Record,
StringRef Blob,
bool allowEmptyString) {
- llvm::StringRef RetStr;
+ const char *RetStr;
if (readString(TopDiags, RetStr, errorContext, Record, Blob,
allowEmptyString))
return Failure;
@@ -622,11 +621,11 @@ LoadResult DiagLoader::readDiagnosticBlo
CXSourceRange SR;
if (readRange(TopDiags, Record, 0, SR))
return Failure;
- llvm::StringRef RetStr;
+ const char *RetStr;
if (readString(TopDiags, RetStr, "FIXIT", Record, Blob,
/* allowEmptyString */ true))
return Failure;
- D->FixIts.push_back(std::make_pair(SR, cxstring::createRef(RetStr)));
+ D->FixIts.push_back(std::make_pair(SR, RetStr));
continue;
}
@@ -639,7 +638,7 @@ LoadResult DiagLoader::readDiagnosticBlo
unsigned diagFlag = Record[offset++];
D->DiagOption = diagFlag ? TopDiags.WarningFlags[diagFlag] : "";
D->CategoryText = D->category ? TopDiags.Categories[D->category] : "";
- D->Spelling = TopDiags.makeString(Blob);
+ D->Spelling = TopDiags.copyString(Blob);
continue;
}
}
Modified: cfe/trunk/tools/libclang/CXLoadedDiagnostic.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXLoadedDiagnostic.h?rev=175455&r1=175454&r2=175455&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXLoadedDiagnostic.h (original)
+++ cfe/trunk/tools/libclang/CXLoadedDiagnostic.h Mon Feb 18 13:50:38 2013
@@ -82,8 +82,8 @@ public:
Location DiagLoc;
std::vector<CXSourceRange> Ranges;
- std::vector<std::pair<CXSourceRange, CXString> > FixIts;
- llvm::StringRef Spelling;
+ std::vector<std::pair<CXSourceRange, const char *> > FixIts;
+ const char *Spelling;
llvm::StringRef DiagOption;
llvm::StringRef CategoryText;
unsigned severity;
More information about the cfe-commits
mailing list