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

Honggyu Kim via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 1 04:52:29 PDT 2016


This revision was automatically updated to reflect the committed changes.
Closed by commit rL280357: [IR] Properly handle escape characters in Attribute::getAsString() (authored by honggyu.kim).

Changed prior to commit:
  https://reviews.llvm.org/D23792?vs=69468&id=69990#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D23792

Files:
  llvm/trunk/include/llvm/ADT/StringExtras.h
  llvm/trunk/lib/IR/AsmWriter.cpp
  llvm/trunk/lib/IR/Attributes.cpp

Index: llvm/trunk/include/llvm/ADT/StringExtras.h
===================================================================
--- llvm/trunk/include/llvm/ADT/StringExtras.h
+++ llvm/trunk/include/llvm/ADT/StringExtras.h
@@ -19,6 +19,7 @@
 #include <iterator>
 
 namespace llvm {
+class raw_ostream;
 template<typename T> class SmallVectorImpl;
 
 /// hexdigit - Return the hexadecimal character for the
@@ -150,6 +151,10 @@
   }
 }
 
+/// PrintEscapedString - Print each character of the specified string, escaping
+/// it if it is not printable or if it is an escape char.
+void PrintEscapedString(StringRef Name, raw_ostream &Out);
+
 template <typename IteratorT>
 inline std::string join_impl(IteratorT Begin, IteratorT End,
                              StringRef Separator, std::input_iterator_tag) {
Index: llvm/trunk/lib/IR/Attributes.cpp
===================================================================
--- llvm/trunk/lib/IR/Attributes.cpp
+++ llvm/trunk/lib/IR/Attributes.cpp
@@ -381,10 +381,18 @@
     std::string Result;
     Result += (Twine('"') + getKindAsString() + Twine('"')).str();
 
-    StringRef Val = pImpl->getValueAsString();
-    if (Val.empty()) return Result;
+    std::string AttrVal = pImpl->getValueAsString();
+    if (AttrVal.empty()) return Result;
 
-    Result += ("=\"" + Val + Twine('"')).str();
+    // Since some attribute strings contain special characters that cannot be
+    // printable, those have to be escaped to make the attribute value printable
+    // as is.  e.g. "\01__gnu_mcount_nc"
+    {
+      raw_string_ostream OS(Result);
+      OS << "=\"";
+      PrintEscapedString(AttrVal, OS);
+      OS << "\"";
+    }
     return Result;
   }
 
Index: llvm/trunk/lib/IR/AsmWriter.cpp
===================================================================
--- llvm/trunk/lib/IR/AsmWriter.cpp
+++ llvm/trunk/lib/IR/AsmWriter.cpp
@@ -337,9 +337,7 @@
   }
 }
 
-// PrintEscapedString - Print each character of the specified string, escaping
-// it if it is not printable or if it is an escape char.
-static void PrintEscapedString(StringRef Name, raw_ostream &Out) {
+void llvm::PrintEscapedString(StringRef Name, raw_ostream &Out) {
   for (unsigned i = 0, e = Name.size(); i != e; ++i) {
     unsigned char C = Name[i];
     if (isprint(C) && C != '\\' && C != '"')


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D23792.69990.patch
Type: text/x-patch
Size: 2306 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160901/3b099bf5/attachment.bin>


More information about the llvm-commits mailing list