From 2f5eb4e9a55b06cec0516a2e6a371afd46f0fd12 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Thu, 9 May 2002 03:56:52 +0000 Subject: [PATCH] * Remove dead function * Print C strings correctly * Expand C escape sequences nicely (ie \n \t, etc get generated instead of hex escapes) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2572 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/CBackend/CBackend.cpp | 29 ++++++++++++++++++++--------- lib/Target/CBackend/Writer.cpp | 29 ++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 18 deletions(-) diff --git a/lib/Target/CBackend/CBackend.cpp b/lib/Target/CBackend/CBackend.cpp index bb5f75de3ea..630abecbd0c 100644 --- a/lib/Target/CBackend/CBackend.cpp +++ b/lib/Target/CBackend/CBackend.cpp @@ -72,11 +72,6 @@ static string makeNameProper(string x) { return tmp; } -static string getConstantName(const Constant *CPV) { - return CPV->getName(); -} - - static std::string getConstArrayStrValue(const Constant* CPV) { std::string Result; @@ -94,10 +89,17 @@ static std::string getConstArrayStrValue(const Constant* CPV) { break; } } + if (isString) { + // Make sure the last character is a null char, as automatically added by C + if (CPV->getNumOperands() == 0 || + !cast(*(CPV->op_end()-1))->isNullValue()) + isString = false; + } if (isString) { Result = "\""; - for (unsigned i = 0; i < CPV->getNumOperands(); ++i) { + // Do not include the last character, which we know is null + for (unsigned i = 0, e = CPV->getNumOperands()-1; i != e; ++i) { unsigned char C = (ETy == Type::SByteTy) ? (unsigned char)cast(CPV->getOperand(i))->getValue() : (unsigned char)cast(CPV->getOperand(i))->getValue(); @@ -105,9 +107,18 @@ static std::string getConstArrayStrValue(const Constant* CPV) { if (isprint(C)) { Result += C; } else { - Result += "\\x"; - Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'); - Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'); + switch (C) { + case '\n': Result += "\\n"; break; + case '\t': Result += "\\t"; break; + case '\r': Result += "\\r"; break; + case '\v': Result += "\\v"; break; + case '\a': Result += "\\a"; break; + default: + Result += "\\x"; + Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'); + Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'); + break; + } } } Result += "\""; diff --git a/lib/Target/CBackend/Writer.cpp b/lib/Target/CBackend/Writer.cpp index bb5f75de3ea..630abecbd0c 100644 --- a/lib/Target/CBackend/Writer.cpp +++ b/lib/Target/CBackend/Writer.cpp @@ -72,11 +72,6 @@ static string makeNameProper(string x) { return tmp; } -static string getConstantName(const Constant *CPV) { - return CPV->getName(); -} - - static std::string getConstArrayStrValue(const Constant* CPV) { std::string Result; @@ -94,10 +89,17 @@ static std::string getConstArrayStrValue(const Constant* CPV) { break; } } + if (isString) { + // Make sure the last character is a null char, as automatically added by C + if (CPV->getNumOperands() == 0 || + !cast(*(CPV->op_end()-1))->isNullValue()) + isString = false; + } if (isString) { Result = "\""; - for (unsigned i = 0; i < CPV->getNumOperands(); ++i) { + // Do not include the last character, which we know is null + for (unsigned i = 0, e = CPV->getNumOperands()-1; i != e; ++i) { unsigned char C = (ETy == Type::SByteTy) ? (unsigned char)cast(CPV->getOperand(i))->getValue() : (unsigned char)cast(CPV->getOperand(i))->getValue(); @@ -105,9 +107,18 @@ static std::string getConstArrayStrValue(const Constant* CPV) { if (isprint(C)) { Result += C; } else { - Result += "\\x"; - Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'); - Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'); + switch (C) { + case '\n': Result += "\\n"; break; + case '\t': Result += "\\t"; break; + case '\r': Result += "\\r"; break; + case '\v': Result += "\\v"; break; + case '\a': Result += "\\a"; break; + default: + Result += "\\x"; + Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'); + Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'); + break; + } } } Result += "\"";