[PATCH] D23792: IR: Properly handle escape characters in Attribute::getAsString()

Honggyu Kim via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 22 21:07:40 PDT 2016


honggyu.kim created this revision.
honggyu.kim added reviewers: hfinkel, rjmccall, compnerd, rengolin.
honggyu.kim added subscribers: llvm-commits, majnemer, shenhan, hans, mcrosier, nemanjai.

If an attribute name has special characters such as '\01', it is not
properly printed in LLVM assembly language format.  Since LLVM assembly
language format expects the special characters are printed as it is, it
has to be handled in a special way.

Currently, PrintEscapedString() function does this purpose by printing
those characters in a different way.  But it is also required to handle
those cases for Attribute::getAsString() to print attribute string as it
is.  So this patch adds GetEscapedString() for this purpose.

Before:
  attributes #0 = { ... "counting-function"="^A__gnu_mcount_nc" ...

After:
  attributes #0 = { ... "counting-function"="\01__gnu_mcount_nc" ...

Signed-off-by: Honggyu Kim <hong.gyu.kim at lge.com>

https://reviews.llvm.org/D23792

Files:
  lib/IR/Attributes.cpp

Index: lib/IR/Attributes.cpp
===================================================================
--- lib/IR/Attributes.cpp
+++ lib/IR/Attributes.cpp
@@ -223,6 +223,21 @@
   return unpackAllocSizeArgs(pImpl->getValueAsInt());
 }
 
+static std::string GetEscapedString(StringRef Name) {
+  std::string EscapedName;
+  for (unsigned i = 0, e = Name.size(); i != e; ++i) {
+    unsigned char C = Name[i];
+    if (isprint(C) && C != '\\' && C != '"')
+      EscapedName += C;
+    else {
+      EscapedName += "\\";
+      EscapedName += hexdigit(C >> 4);
+      EscapedName += hexdigit(C & 0x0F);
+    }
+  }
+  return EscapedName;
+}
+
 std::string Attribute::getAsString(bool InAttrGrp) const {
   if (!pImpl) return "";
 
@@ -384,7 +399,9 @@
     StringRef Val = pImpl->getValueAsString();
     if (Val.empty()) return Result;
 
-    Result += ("=\"" + Val + Twine('"')).str();
+    Result += "=\"";
+    Result += GetEscapedString(Val);
+    Result += "\"";
     return Result;
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D23792.68948.patch
Type: text/x-patch
Size: 986 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160823/837564ea/attachment.bin>


More information about the llvm-commits mailing list