Skip to content

Commit 2a31eb4

Browse files
committed
PR10210: New method ConstantArray::getAsCString(). Use it in LTO to
avoid getting embedded trailing null bytes in std::strings. llvm-svn: 133999
1 parent 9083ef1 commit 2a31eb4

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

llvm/include/llvm/Constants.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,12 @@ class ConstantArray : public Constant {
387387
///
388388
std::string getAsString() const;
389389

390+
/// getAsCString - If this array is isCString(), then this method converts the
391+
/// array (without the trailing null byte) to an std::string and returns it.
392+
/// Otherwise, it asserts out.
393+
///
394+
std::string getAsCString() const;
395+
390396
/// isNullValue - Return true if this is the value that would be returned by
391397
/// getNullValue. This always returns false because zero arrays are always
392398
/// created as ConstantAggregateZero objects.

llvm/lib/VMCore/Constants.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,17 +1011,32 @@ bool ConstantArray::isCString() const {
10111011
}
10121012

10131013

1014-
/// getAsString - If the sub-element type of this array is i8
1015-
/// then this method converts the array to an std::string and returns it.
1016-
/// Otherwise, it asserts out.
1014+
/// convertToString - Helper function for getAsString() and getAsCString().
1015+
static std::string convertToString(const User *U, unsigned len)
1016+
{
1017+
std::string Result;
1018+
Result.reserve(len);
1019+
for (unsigned i = 0; i != len; ++i)
1020+
Result.push_back((char)cast<ConstantInt>(U->getOperand(i))->getZExtValue());
1021+
return Result;
1022+
}
1023+
1024+
/// getAsString - If this array is isString(), then this method converts the
1025+
/// array to an std::string and returns it. Otherwise, it asserts out.
10171026
///
10181027
std::string ConstantArray::getAsString() const {
10191028
assert(isString() && "Not a string!");
1020-
std::string Result;
1021-
Result.reserve(getNumOperands());
1022-
for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1023-
Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue());
1024-
return Result;
1029+
return convertToString(this, getNumOperands());
1030+
}
1031+
1032+
1033+
/// getAsCString - If this array is isCString(), then this method converts the
1034+
/// array (without the trailing null byte) to an std::string and returns it.
1035+
/// Otherwise, it asserts out.
1036+
///
1037+
std::string ConstantArray::getAsCString() const {
1038+
assert(isCString() && "Not a string!");
1039+
return convertToString(this, getNumOperands() - 1);
10251040
}
10261041

10271042

llvm/tools/lto/LTOModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ bool LTOModule::objcClassNameFromExpression(Constant *c, std::string &name) {
191191
Constant *cn = gvn->getInitializer();
192192
if (ConstantArray *ca = dyn_cast<ConstantArray>(cn)) {
193193
if (ca->isCString()) {
194-
name = ".objc_class_name_" + ca->getAsString();
194+
name = ".objc_class_name_" + ca->getAsCString();
195195
return true;
196196
}
197197
}

0 commit comments

Comments
 (0)