Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 89ea36c

Browse files
committed
[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
1 parent f991e38 commit 89ea36c

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

include/llvm/ADT/StringExtras.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <iterator>
2020

2121
namespace llvm {
22+
class raw_ostream;
2223
template<typename T> class SmallVectorImpl;
2324

2425
/// hexdigit - Return the hexadecimal character for the
@@ -150,6 +151,10 @@ static inline StringRef getOrdinalSuffix(unsigned Val) {
150151
}
151152
}
152153

154+
/// PrintEscapedString - Print each character of the specified string, escaping
155+
/// it if it is not printable or if it is an escape char.
156+
void PrintEscapedString(StringRef Name, raw_ostream &Out);
157+
153158
template <typename IteratorT>
154159
inline std::string join_impl(IteratorT Begin, IteratorT End,
155160
StringRef Separator, std::input_iterator_tag) {

lib/IR/AsmWriter.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,7 @@ static void PrintCallingConv(unsigned cc, raw_ostream &Out) {
337337
}
338338
}
339339

340-
// PrintEscapedString - Print each character of the specified string, escaping
341-
// it if it is not printable or if it is an escape char.
342-
static void PrintEscapedString(StringRef Name, raw_ostream &Out) {
340+
void llvm::PrintEscapedString(StringRef Name, raw_ostream &Out) {
343341
for (unsigned i = 0, e = Name.size(); i != e; ++i) {
344342
unsigned char C = Name[i];
345343
if (isprint(C) && C != '\\' && C != '"')

lib/IR/Attributes.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,18 @@ std::string Attribute::getAsString(bool InAttrGrp) const {
381381
std::string Result;
382382
Result += (Twine('"') + getKindAsString() + Twine('"')).str();
383383

384-
StringRef Val = pImpl->getValueAsString();
385-
if (Val.empty()) return Result;
386-
387-
Result += ("=\"" + Val + Twine('"')).str();
384+
std::string AttrVal = pImpl->getValueAsString();
385+
if (AttrVal.empty()) return Result;
386+
387+
// Since some attribute strings contain special characters that cannot be
388+
// printable, those have to be escaped to make the attribute value printable
389+
// as is. e.g. "\01__gnu_mcount_nc"
390+
{
391+
raw_string_ostream OS(Result);
392+
OS << "=\"";
393+
PrintEscapedString(AttrVal, OS);
394+
OS << "\"";
395+
}
388396
return Result;
389397
}
390398

0 commit comments

Comments
 (0)