llvm.org GIT mirror llvm / 89ea36c
[IR] Properly handle escape characters in Attribute::getAsString() If an attribute name has special characters such as '\01', it is not properly printed in LLVM assembly language format. Since the format expects the special characters are printed as it is, it has to contain escape characters to make it printable. Before: attributes #0 = { ... "counting-function"="^A__gnu_mcount_nc" ... After: attributes #0 = { ... "counting-function"="\01__gnu_mcount_nc" ... Reviewers: hfinkel, rengolin, rjmccall, compnerd Subscribers: nemanjai, mcrosier, hans, shenhan, majnemer, llvm-commits Differential Revision: https://reviews.llvm.org/D23792 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@280357 91177308-0d34-0410-b5e6-96231b3b80d8 Honggyu Kim 3 years ago
3 changed file(s) with 18 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
1818 #include
1919
2020 namespace llvm {
21 class raw_ostream;
2122 template class SmallVectorImpl;
2223
2324 /// hexdigit - Return the hexadecimal character for the
149150 }
150151 }
151152
153 /// PrintEscapedString - Print each character of the specified string, escaping
154 /// it if it is not printable or if it is an escape char.
155 void PrintEscapedString(StringRef Name, raw_ostream &Out);
156
152157 template
153158 inline std::string join_impl(IteratorT Begin, IteratorT End,
154159 StringRef Separator, std::input_iterator_tag) {
336336 }
337337 }
338338
339 // PrintEscapedString - Print each character of the specified string, escaping
340 // it if it is not printable or if it is an escape char.
341 static void PrintEscapedString(StringRef Name, raw_ostream &Out) {
339 void llvm::PrintEscapedString(StringRef Name, raw_ostream &Out) {
342340 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
343341 unsigned char C = Name[i];
344342 if (isprint(C) && C != '\\' && C != '"')
380380 std::string Result;
381381 Result += (Twine('"') + getKindAsString() + Twine('"')).str();
382382
383 StringRef Val = pImpl->getValueAsString();
384 if (Val.empty()) return Result;
385
386 Result += ("=\"" + Val + Twine('"')).str();
383 std::string AttrVal = pImpl->getValueAsString();
384 if (AttrVal.empty()) return Result;
385
386 // Since some attribute strings contain special characters that cannot be
387 // printable, those have to be escaped to make the attribute value printable
388 // as is. e.g. "\01__gnu_mcount_nc"
389 {
390 raw_string_ostream OS(Result);
391 OS << "=\"";
392 PrintEscapedString(AttrVal, OS);
393 OS << "\"";
394 }
387395 return Result;
388396 }
389397