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

Honggyu Kim via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 24 17:03:08 PDT 2016


honggyu.kim added a comment.

In https://reviews.llvm.org/D23792#524043, @rjmccall wrote:

> In https://reviews.llvm.org/D23792#522872, @honggyu.kim wrote:
>
> > In `Attribute::getAsString()`, it is expected to return `std::string` as the name describes. I think `getAsString()` function should not print the string directly with `PrintEscapedString()`. If I directly print attribute string here, we have to modify many functions in the execution paths above. The only problem here is printing `Val`.  That's why I added a new function `GetEscapedString()`.  Please correct me if there's a better approach.
>
>
> PrintEscapedString prints to a raw_ostream that's passed in as a parameter, and there's a raw_string_ostream that will just append to a std::string.
>
> John.


Thanks for pointing out this. But there is one more tiny but tricky issue here. The implementation of PrintEscapedString and GetEscapedString are a bit different. PrintEscapedString just prints an escaped character '\' itself, but GetEscapedString has to put double quoted string "\\" for later usage.  For this purpose, PrintEscapedString has to also be modified as below in that case.

  --- a/lib/IR/AsmWriter.cpp
  +++ b/lib/IR/AsmWriter.cpp
  @@ -345,7 +345,7 @@ static void PrintEscapedString(StringRef Name, raw_ostream &Out) {
       if (isprint(C) && C != '\\' && C != '"')
         Out << C;
       else
  -      Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
  +      Out << "\\" << hexdigit(C >> 4) << hexdigit(C & 0x0F);
     }
   }

But it will change the behaviour of other functions that use PrintEscapedString, that's why I thought it'd be better to add a new function GetEscapedString to make it simple.


https://reviews.llvm.org/D23792





More information about the llvm-commits mailing list