[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:56:37 PDT 2016


honggyu.kim added a comment.

Hi John,

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

> I'm not sure what you're saying.  The existing function PrintEscapedString in AsmWriter.cpp does exactly what you want; you just need to call it from getAsString as well. You'll need to expose it somewhere, of course.


Let me show you the execution sequence to print attribute list as below.

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

`llvm/lib/IR/AsmWriter.cpp`

  void AssemblyWriter::writeAllAttributeGroups() {
    std::vector<std::pair<AttributeSet, unsigned> > asVec;
      ...
    for (const auto &I : asVec)
      Out << "attributes #" << I.second << " = { "
          << I.first.getAsString(AttributeSet::FunctionIndex, true) << " }\n";
      ...
  }

`llvm/lib/IR/AttributeSetNode.h`

  typedef const Attribute *iterator;

`llvm/lib/IR/Attributes.cpp`

  std::string AttributeSet::getAsString(unsigned Index, bool InAttrGrp) const {
    AttributeSetNode *ASN = getAttributes(Index);
    return ASN ? ASN->getAsString(InAttrGrp) : std::string("");
  }
  
  std::string AttributeSetNode::getAsString(bool InAttrGrp) const {
    std::string Str;
    for (iterator I = begin(), E = end(); I != E; ++I) {
      if (I != begin())
        Str += ' ';
      Str += I->getAsString(InAttrGrp);
    }
    return Str;
  }
  
  std::string Attribute::getAsString(bool InAttrGrp) const {
      ...
    // Convert target-dependent attributes to strings of the form:
    //
    //   "kind"
    //   "kind" = "value"
    //
    if (isStringAttribute()) {
      std::string Result;
      Result += (Twine('"') + getKindAsString() + Twine('"')).str();
  
      StringRef Val = pImpl->getValueAsString();
      if (Val.empty()) return Result;
  
      Result += ("=\"" + Val + Twine('"')).str();
      return Result;
    }
  
    llvm_unreachable("Unknown attribute");
  }

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 the 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.


https://reviews.llvm.org/D23792





More information about the llvm-commits mailing list