diff options
author | Jay Foad <jay.foad@gmail.com> | 2011-06-28 08:24:19 +0000 |
---|---|---|
committer | Jay Foad <jay.foad@gmail.com> | 2011-06-28 08:24:19 +0000 |
commit | 2a31eb49c7be60db75b2ebac16699cde1f06d0c8 (patch) | |
tree | a2b8124b9ab37e33d931f1ba5cee94d9ba0568fe /llvm/lib | |
parent | 9083ef191808d7fcddd275ede15437b33447b522 (diff) | |
download | bcm5719-llvm-2a31eb49c7be60db75b2ebac16699cde1f06d0c8.tar.gz bcm5719-llvm-2a31eb49c7be60db75b2ebac16699cde1f06d0c8.zip |
PR10210: New method ConstantArray::getAsCString(). Use it in LTO to
avoid getting embedded trailing null bytes in std::strings.
llvm-svn: 133999
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/VMCore/Constants.cpp | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/llvm/lib/VMCore/Constants.cpp b/llvm/lib/VMCore/Constants.cpp index 87f2fe624e5..4e6e64d1928 100644 --- a/llvm/lib/VMCore/Constants.cpp +++ b/llvm/lib/VMCore/Constants.cpp @@ -1011,17 +1011,32 @@ bool ConstantArray::isCString() const { } -/// getAsString - If the sub-element type of this array is i8 -/// then this method converts the array to an std::string and returns it. -/// Otherwise, it asserts out. +/// convertToString - Helper function for getAsString() and getAsCString(). +static std::string convertToString(const User *U, unsigned len) +{ + 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 { assert(isString() && "Not a string!"); - std::string Result; - Result.reserve(getNumOperands()); - for (unsigned i = 0, e = getNumOperands(); i != e; ++i) - Result.push_back((char)cast<ConstantInt>(getOperand(i))->getZExtValue()); - return Result; + return convertToString(this, getNumOperands()); +} + + +/// 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); } |