[llvm-commits] CVS: llvm/lib/VMCore/Constants.cpp

Reid Spencer reid at x10sys.com
Tue May 30 01:23:32 PDT 2006



Changes in directory llvm/lib/VMCore:

Constants.cpp updated: 1.151 -> 1.152
---
Log message:

Adjust the interface to ConstantArray::get. The previous 
implementation always added a null byte to the end of the string. It turns
out that this is not always wanted. By adding a length parameter we preserve
this behavior when length==0 (default value) but also allow other lengths
(not null terminated) to be created.


---
Diffs of the changes:  (+15 -8)

 Constants.cpp |   23 +++++++++++++++--------
 1 files changed, 15 insertions(+), 8 deletions(-)


Index: llvm/lib/VMCore/Constants.cpp
diff -u llvm/lib/VMCore/Constants.cpp:1.151 llvm/lib/VMCore/Constants.cpp:1.152
--- llvm/lib/VMCore/Constants.cpp:1.151	Wed May 24 12:04:04 2006
+++ llvm/lib/VMCore/Constants.cpp	Tue May 30 03:23:18 2006
@@ -924,20 +924,27 @@
   destroyConstantImpl();
 }
 
-// ConstantArray::get(const string&) - Return an array that is initialized to
-// contain the specified string.  A null terminator is added to the specified
-// string so that it may be used in a natural way...
-//
-Constant *ConstantArray::get(const std::string &Str) {
+/// ConstantArray::get(const string&) - Return an array that is initialized to
+/// contain the specified string.  If length is zero then a null terminator is 
+/// added to the specified string so that it may be used in a natural way. 
+/// Otherwise, the length parameter specifies how much of the string to use 
+/// and it won't be null terminated.
+///
+Constant *ConstantArray::get(const std::string &Str, unsigned length) {
+  assert(length <= Str.length() && "Invalid length for string");
   std::vector<Constant*> ElementVals;
 
-  for (unsigned i = 0; i < Str.length(); ++i)
+  unsigned copy_len = (length == 0 ? Str.length() : length);
+  for (unsigned i = 0; i < copy_len; ++i)
     ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
 
   // Add a null terminator to the string...
-  ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
+  if (length == 0) {
+    ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
+    copy_len++;
+  }
 
-  ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
+  ArrayType *ATy = ArrayType::get(Type::SByteTy, copy_len);
   return ConstantArray::get(ATy, ElementVals);
 }
 






More information about the llvm-commits mailing list