PR10210: New method ConstantArray::getAsCString(). Use it in LTO to

avoid getting embedded trailing null bytes in std::strings.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@133999 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Jay Foad 2011-06-28 08:24:19 +00:00
parent 4086bb5ba5
commit 4f91054fe4
3 changed files with 30 additions and 9 deletions

View File

@ -387,6 +387,12 @@ public:
/// ///
std::string getAsString() const; std::string getAsString() const;
/// getAsCString - If this array is isCString(), then this method converts the
/// array (without the trailing null byte) to an std::string and returns it.
/// Otherwise, it asserts out.
///
std::string getAsCString() const;
/// isNullValue - Return true if this is the value that would be returned by /// isNullValue - Return true if this is the value that would be returned by
/// getNullValue. This always returns false because zero arrays are always /// getNullValue. This always returns false because zero arrays are always
/// created as ConstantAggregateZero objects. /// created as ConstantAggregateZero objects.

View File

@ -1011,17 +1011,32 @@ bool ConstantArray::isCString() const {
} }
/// getAsString - If the sub-element type of this array is i8 /// convertToString - Helper function for getAsString() and getAsCString().
/// then this method converts the array to an std::string and returns it. static std::string convertToString(const User *U, unsigned len)
/// Otherwise, it asserts out. {
std::string Result;
Result.reserve(len);
for (unsigned i = 0; i != len; ++i)
Result.push_back((char)cast<ConstantInt>(U->getOperand(i))->getZExtValue());
return Result;
}
/// getAsString - If this array is isString(), then this method converts the
/// array to an std::string and returns it. Otherwise, it asserts out.
/// ///
std::string ConstantArray::getAsString() const { std::string ConstantArray::getAsString() const {
assert(isString() && "Not a string!"); assert(isString() && "Not a string!");
std::string Result; return convertToString(this, getNumOperands());
Result.reserve(getNumOperands()); }
for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
return Result; /// getAsCString - If this array is isCString(), then this method converts the
/// array (without the trailing null byte) to an std::string and returns it.
/// Otherwise, it asserts out.
///
std::string ConstantArray::getAsCString() const {
assert(isCString() && "Not a string!");
return convertToString(this, getNumOperands() - 1);
} }

View File

@ -191,7 +191,7 @@ bool LTOModule::objcClassNameFromExpression(Constant *c, std::string &name) {
Constant *cn = gvn->getInitializer(); Constant *cn = gvn->getInitializer();
if (ConstantArray *ca = dyn_cast<ConstantArray>(cn)) { if (ConstantArray *ca = dyn_cast<ConstantArray>(cn)) {
if (ca->isCString()) { if (ca->isCString()) {
name = ".objc_class_name_" + ca->getAsString(); name = ".objc_class_name_" + ca->getAsCString();
return true; return true;
} }
} }