diff --git a/include/llvm/LLVMContext.h b/include/llvm/LLVMContext.h index 134311e98af..0751ccd83a7 100644 --- a/include/llvm/LLVMContext.h +++ b/include/llvm/LLVMContext.h @@ -232,7 +232,7 @@ public: MDNode* getMDNode(Value* const* Vals, unsigned NumVals); // MDString accessors - MDString* getMDString(const char *StrBegin, const char *StrEnd); + MDString* getMDString(const char *StrBegin, unsigned Length); MDString* getMDString(const std::string &Str); // FunctionType accessors diff --git a/include/llvm/MDNode.h b/include/llvm/MDNode.h index 14bfe5798a6..06cc90931e3 100644 --- a/include/llvm/MDNode.h +++ b/include/llvm/MDNode.h @@ -64,16 +64,17 @@ public: /// class MDString : public MetadataBase { MDString(const MDString &); // DO NOT IMPLEMENT - const char *StrBegin, *StrEnd; + const char *StrBegin; + unsigned StrLength; friend class LLVMContextImpl; protected: - explicit MDString(const char *begin, const char *end) + explicit MDString(const char *begin, unsigned l) : MetadataBase(Type::MetadataTy, Value::MDStringVal), - StrBegin(begin), StrEnd(end) {} + StrBegin(begin), StrLength(l) {} public: - intptr_t size() const { return StrEnd - StrBegin; } + unsigned length() const { return StrLength; } /// begin() - Pointer to the first byte of the string. /// @@ -81,7 +82,7 @@ public: /// end() - Pointer to one byte past the end of the string. /// - const char *end() const { return StrEnd; } + const char *end() const { return StrBegin + length(); } /// Methods for support type inquiry through isa, cast, and dyn_cast: static inline bool classof(const MDString *) { return true; } diff --git a/lib/AsmParser/LLParser.cpp b/lib/AsmParser/LLParser.cpp index b09f23fbd1a..94224a6361c 100644 --- a/lib/AsmParser/LLParser.cpp +++ b/lib/AsmParser/LLParser.cpp @@ -373,7 +373,7 @@ bool LLParser::ParseNamedGlobal() { bool LLParser::ParseMDString(MetadataBase *&MDS) { std::string Str; if (ParseStringConstant(Str)) return true; - MDS = Context.getMDString(Str.data(), Str.data() + Str.size()); + MDS = Context.getMDString(Str.data(), Str.size()); return false; } diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index 8393cd42163..01b3eb3bced 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -758,7 +758,7 @@ bool BitcodeReader::ParseMetadata() { for (unsigned i = 0; i != MDStringLength; ++i) String[i] = Record[i]; Value *V = - Context.getMDString(String.c_str(), String.c_str() + MDStringLength); + Context.getMDString(String.c_str(), MDStringLength); ValueList.AssignValue(V, NextValueNo++); break; } diff --git a/lib/Bitcode/Writer/BitcodeWriter.cpp b/lib/Bitcode/Writer/BitcodeWriter.cpp index 7d745ffd50d..b3124204903 100644 --- a/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -512,7 +512,7 @@ static uint64_t GetOptimizationFlags(const Value *V) { // Code: [strchar x N] const char *StrBegin = MDS->begin(); - for (unsigned i = 0, e = MDS->size(); i != e; ++i) + for (unsigned i = 0, e = MDS->length(); i != e; ++i) Record.push_back(StrBegin[i]); // Emit the finished record. diff --git a/lib/VMCore/AsmWriter.cpp b/lib/VMCore/AsmWriter.cpp index ae55d5ba515..73310a35545 100644 --- a/lib/VMCore/AsmWriter.cpp +++ b/lib/VMCore/AsmWriter.cpp @@ -1139,7 +1139,7 @@ static void WriteAsOperandInternal(raw_ostream &Out, const Value *V, if (const MDString *MDS = dyn_cast(V)) { Out << "!\""; - PrintEscapedString(MDS->begin(), MDS->size(), Out); + PrintEscapedString(MDS->begin(), MDS->length(), Out); Out << '"'; return; } @@ -1983,7 +1983,7 @@ void Value::print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const { TypePrinter.print(MDS->getType(), OS); OS << ' '; OS << "!\""; - PrintEscapedString(MDS->begin(), MDS->size(), OS); + PrintEscapedString(MDS->begin(), MDS->length(), OS); OS << '"'; } else if (const MDNode *N = dyn_cast(this)) { SlotTracker SlotTable(N); diff --git a/lib/VMCore/LLVMContext.cpp b/lib/VMCore/LLVMContext.cpp index c88ecba72bf..d0abb2df626 100644 --- a/lib/VMCore/LLVMContext.cpp +++ b/lib/VMCore/LLVMContext.cpp @@ -551,12 +551,12 @@ MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) { } // MDString accessors -MDString* LLVMContext::getMDString(const char *StrBegin, const char *StrEnd) { - return pImpl->getMDString(StrBegin, StrEnd); +MDString* LLVMContext::getMDString(const char *StrBegin, unsigned StrLength) { + return pImpl->getMDString(StrBegin, StrLength); } MDString* LLVMContext::getMDString(const std::string &Str) { - return getMDString(Str.data(), Str.data()+Str.size()); + return getMDString(Str.data(), Str.size()); } // FunctionType accessors diff --git a/lib/VMCore/LLVMContextImpl.cpp b/lib/VMCore/LLVMContextImpl.cpp index 8ad46a040c5..79b33c14581 100644 --- a/lib/VMCore/LLVMContextImpl.cpp +++ b/lib/VMCore/LLVMContextImpl.cpp @@ -394,13 +394,13 @@ ConstantFP *LLVMContextImpl::getConstantFP(const APFloat &V) { } MDString *LLVMContextImpl::getMDString(const char *StrBegin, - const char *StrEnd) { + unsigned StrLength) { sys::SmartScopedWriter Writer(ConstantsLock); - StringMapEntry &Entry = MDStringCache.GetOrCreateValue( - StrBegin, StrEnd); + StringMapEntry &Entry = + MDStringCache.GetOrCreateValue(StrBegin, StrBegin + StrLength); MDString *&S = Entry.getValue(); if (!S) S = new MDString(Entry.getKeyData(), - Entry.getKeyData() + Entry.getKeyLength()); + Entry.getKeyLength()); return S; } @@ -460,7 +460,8 @@ Constant *LLVMContextImpl::getConstantArray(const ArrayType *Ty, void LLVMContextImpl::erase(MDString *M) { sys::SmartScopedWriter Writer(ConstantsLock); - MDStringCache.erase(MDStringCache.find(M->StrBegin, M->StrEnd)); + MDStringCache.erase(MDStringCache.find(M->StrBegin, + M->StrBegin + M->length())); } void LLVMContextImpl::erase(MDNode *M) { diff --git a/lib/VMCore/LLVMContextImpl.h b/lib/VMCore/LLVMContextImpl.h index 03f0e7b1414..392e500f128 100644 --- a/lib/VMCore/LLVMContextImpl.h +++ b/lib/VMCore/LLVMContextImpl.h @@ -133,7 +133,7 @@ public: ConstantFP *getConstantFP(const APFloat &V); - MDString *getMDString(const char *StrBegin, const char *StrEnd); + MDString *getMDString(const char *StrBegin, unsigned StrLength); MDNode *getMDNode(Value*const* Vals, unsigned NumVals); diff --git a/unittests/VMCore/MetadataTest.cpp b/unittests/VMCore/MetadataTest.cpp index 732719ec5c4..d612808b9f6 100644 --- a/unittests/VMCore/MetadataTest.cpp +++ b/unittests/VMCore/MetadataTest.cpp @@ -23,9 +23,9 @@ namespace { // MDString objects, even with the same string pointer and nulls in the string. TEST(MDStringTest, CreateDifferent) { char x[3] = { 'f', 0, 'A' }; - MDString *s1 = getGlobalContext().getMDString(&x[0], &x[3]); + MDString *s1 = getGlobalContext().getMDString(&x[0], 3); x[2] = 'B'; - MDString *s2 = getGlobalContext().getMDString(&x[0], &x[3]); + MDString *s2 = getGlobalContext().getMDString(&x[0], 3); EXPECT_NE(s1, s2); } @@ -35,8 +35,8 @@ TEST(MDStringTest, CreateSame) { char x[4] = { 'a', 'b', 'c', 'X' }; char y[4] = { 'a', 'b', 'c', 'Y' }; - MDString *s1 = getGlobalContext().getMDString(&x[0], &x[3]); - MDString *s2 = getGlobalContext().getMDString(&y[0], &y[3]); + MDString *s1 = getGlobalContext().getMDString(&x[0], 3); + MDString *s2 = getGlobalContext().getMDString(&y[0], 3); EXPECT_EQ(s1, s2); } @@ -44,7 +44,7 @@ TEST(MDStringTest, CreateSame) { TEST(MDStringTest, PrintingSimple) { char *str = new char[13]; strncpy(str, "testing 1 2 3", 13); - MDString *s = getGlobalContext().getMDString(str, str+13); + MDString *s = getGlobalContext().getMDString(str, 13); strncpy(str, "aaaaaaaaaaaaa", 13); delete[] str; @@ -56,7 +56,7 @@ TEST(MDStringTest, PrintingSimple) { // Test printing of MDString with non-printable characters. TEST(MDStringTest, PrintingComplex) { char str[5] = {0, '\n', '"', '\\', -1}; - MDString *s = getGlobalContext().getMDString(str+0, str+5); + MDString *s = getGlobalContext().getMDString(str+0, 5); std::ostringstream oss; s->print(oss); EXPECT_STREQ("metadata !\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str()); @@ -67,8 +67,8 @@ TEST(MDNodeTest, Simple) { char x[3] = { 'a', 'b', 'c' }; char y[3] = { '1', '2', '3' }; - MDString *s1 = getGlobalContext().getMDString(&x[0], &x[3]); - MDString *s2 = getGlobalContext().getMDString(&y[0], &y[3]); + MDString *s1 = getGlobalContext().getMDString(&x[0], 3); + MDString *s2 = getGlobalContext().getMDString(&y[0], 3); ConstantInt *CI = getGlobalContext().getConstantInt(APInt(8, 0)); std::vector V;