aboutsummaryrefslogtreecommitdiffstats
path: root/lib/VMCore
diff options
context:
space:
mode:
authorReid Spencer <rspencer@reidspencer.com>2007-05-22 19:27:35 +0000
committerReid Spencer <rspencer@reidspencer.com>2007-05-22 19:27:35 +0000
commit4ad513ca153430c3a7f36be1ef43af04534e1f5d (patch)
treefd572f2109390bbe676d5cf871f2b6a75a8fd8e9 /lib/VMCore
parente2aa96140efea24e8356e20d6632b577d969220a (diff)
downloadexternal_llvm-4ad513ca153430c3a7f36be1ef43af04534e1f5d.zip
external_llvm-4ad513ca153430c3a7f36be1ef43af04534e1f5d.tar.gz
external_llvm-4ad513ca153430c3a7f36be1ef43af04534e1f5d.tar.bz2
Reinstate the patch for escaping non-printing characters and allow for
\\ to escape \. All these cases are now handled by the AsmParser. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@37295 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/VMCore')
-rw-r--r--lib/VMCore/AsmWriter.cpp55
1 files changed, 36 insertions, 19 deletions
diff --git a/lib/VMCore/AsmWriter.cpp b/lib/VMCore/AsmWriter.cpp
index 536b541..16b5510 100644
--- a/lib/VMCore/AsmWriter.cpp
+++ b/lib/VMCore/AsmWriter.cpp
@@ -33,6 +33,7 @@
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/Streams.h"
#include <algorithm>
+#include <cctype>
using namespace llvm;
namespace llvm {
@@ -178,17 +179,42 @@ static SlotMachine *createSlotMachine(const Value *V) {
/// NameNeedsQuotes - Return true if the specified llvm name should be wrapped
/// with ""'s.
-static bool NameNeedsQuotes(const std::string &Name) {
- if (Name[0] >= '0' && Name[0] <= '9') return true;
- // Scan to see if we have any characters that are not on the "white list"
+static std::string QuoteNameIfNeeded(const std::string &Name) {
+ std::string result;
+ bool needsQuotes = Name[0] >= '0' && Name[0] <= '9';
+ // Scan the name to see if it needs quotes and to replace funky chars with
+ // their octal equivalent.
for (unsigned i = 0, e = Name.size(); i != e; ++i) {
char C = Name[i];
assert(C != '"' && "Illegal character in LLVM value name!");
- if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
- C != '-' && C != '.' && C != '_')
- return true;
+ if (isalnum(C) || C == '-' || C == '.' || C == '_')
+ result += C;
+ else if (C == '\\') {
+ needsQuotes = true;
+ result += "\\\\";
+ } else if (isprint(C)) {
+ needsQuotes = true;
+ result += C;
+ } else {
+ needsQuotes = true;
+ result += "\\";
+ char hex1 = (C >> 4) & 0x0F;
+ if (hex1 < 10)
+ result += hex1 + '0';
+ else
+ result += hex1 - 10 + 'A';
+ char hex2 = C & 0x0F;
+ if (hex2 < 10)
+ result += hex2 + '0';
+ else
+ result += hex2 - 10 + 'A';
+ }
+ }
+ if (needsQuotes) {
+ result.insert(0,"\"");
+ result += '"';
}
- return false;
+ return result;
}
enum PrefixType {
@@ -202,20 +228,11 @@ enum PrefixType {
/// surrounded with ""'s (if it has special chars in it).
static std::string getLLVMName(const std::string &Name, PrefixType Prefix) {
assert(!Name.empty() && "Cannot get empty name!");
-
- // First character cannot start with a number...
- if (NameNeedsQuotes(Name)) {
- if (Prefix == GlobalPrefix)
- return "@\"" + Name + "\"";
- return "\"" + Name + "\"";
- }
-
- // If we get here, then the identifier is legal to use as a "VarID".
switch (Prefix) {
default: assert(0 && "Bad prefix!");
- case GlobalPrefix: return '@' + Name;
- case LabelPrefix: return Name;
- case LocalPrefix: return '%' + Name;
+ case GlobalPrefix: return '@' + QuoteNameIfNeeded(Name);
+ case LabelPrefix: return QuoteNameIfNeeded(Name);
+ case LocalPrefix: return '%' + QuoteNameIfNeeded(Name);
}
}