Fix PR1525:
authorReid Spencer <rspencer@reidspencer.com>
Mon, 25 Jun 2007 16:45:54 +0000 (16:45 +0000)
committerReid Spencer <rspencer@reidspencer.com>
Mon, 25 Jun 2007 16:45:54 +0000 (16:45 +0000)
Use a better determinator for identifying constant array initializers that
are or are not zero terminated and generate code appropriately.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@37720 91177308-0d34-0410-b5e6-96231b3b80d8

tools/llvm2cpp/CppWriter.cpp

index 8e30c7990d18ce151cf733e11bfe90edbcb61182..7063b10cbe8c6bf68bedf15fe301227fbe231b7f 100644 (file)
@@ -720,12 +720,18 @@ void CppWriter::printConstant(const Constant *CV) {
   } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
     if (CA->isString() && CA->getType()->getElementType() == Type::Int8Ty) {
       Out << "Constant* " << constName << " = ConstantArray::get(\"";
-      printEscapedString(CA->getAsString());
+      std::string tmp = CA->getAsString();
+      bool nullTerminate = false;
+      if (tmp[tmp.length()-1] == 0) {
+        tmp.erase(tmp.length()-1);
+        nullTerminate = true;
+      }
+      printEscapedString(tmp);
       // Determine if we want null termination or not.
-      if (CA->getType()->getNumElements() <= CA->getAsString().length())
-        Out << "\", false";// No null terminator
-      else
+      if (nullTerminate)
         Out << "\", true"; // Indicate that the null terminator should be added.
+      else
+        Out << "\", false";// No null terminator
       Out << ");";
     } else { 
       Out << "std::vector<Constant*> " << constName << "_elems;";