[llvm-commits] [llvm] r133999 - in /llvm/trunk: include/llvm/Constants.h lib/VMCore/Constants.cpp tools/lto/LTOModule.cpp

Chad Rosier mcrosier at apple.com
Tue Jun 28 10:38:36 PDT 2011


Cool, thanks Jay!

On Jun 28, 2011, at 1:24 AM, Jay Foad wrote:

> Author: foad
> Date: Tue Jun 28 03:24:19 2011
> New Revision: 133999
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=133999&view=rev
> Log:
> PR10210: New method ConstantArray::getAsCString(). Use it in LTO to
> avoid getting embedded trailing null bytes in std::strings.
> 
> Modified:
>    llvm/trunk/include/llvm/Constants.h
>    llvm/trunk/lib/VMCore/Constants.cpp
>    llvm/trunk/tools/lto/LTOModule.cpp
> 
> Modified: llvm/trunk/include/llvm/Constants.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Constants.h?rev=133999&r1=133998&r2=133999&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/Constants.h (original)
> +++ llvm/trunk/include/llvm/Constants.h Tue Jun 28 03:24:19 2011
> @@ -387,6 +387,12 @@
>   ///
>   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
>   /// getNullValue.  This always returns false because zero arrays are always
>   /// created as ConstantAggregateZero objects.
> 
> Modified: llvm/trunk/lib/VMCore/Constants.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Constants.cpp?rev=133999&r1=133998&r2=133999&view=diff
> ==============================================================================
> --- llvm/trunk/lib/VMCore/Constants.cpp (original)
> +++ llvm/trunk/lib/VMCore/Constants.cpp Tue Jun 28 03:24:19 2011
> @@ -1011,17 +1011,32 @@
> }
> 
> 
> -/// 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);
> }
> 
> 
> 
> Modified: llvm/trunk/tools/lto/LTOModule.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lto/LTOModule.cpp?rev=133999&r1=133998&r2=133999&view=diff
> ==============================================================================
> --- llvm/trunk/tools/lto/LTOModule.cpp (original)
> +++ llvm/trunk/tools/lto/LTOModule.cpp Tue Jun 28 03:24:19 2011
> @@ -191,7 +191,7 @@
>       Constant *cn = gvn->getInitializer();
>       if (ConstantArray *ca = dyn_cast<ConstantArray>(cn)) {
>         if (ca->isCString()) {
> -          name = ".objc_class_name_" + ca->getAsString();
> +          name = ".objc_class_name_" + ca->getAsCString();
>           return true;
>         }
>       }
> 
> 
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits




More information about the llvm-commits mailing list