[llvm-commits] [PATCH] StringRef class.

Daniel Dunbar daniel at zuster.org
Tue Jul 21 02:33:15 PDT 2009


On Mon, Jul 20, 2009 at 11:36 PM, Chris Lattner<clattner at apple.com> wrote:
> Otherwise, looks great.  Thanks Daniel!  Bonus points if you actually
> change the Value "name" apis to use it. :)

Oh dear. I had no idea how std::string happy you backend folks were! :)

The change to the Value API is straightforward, but the problem is the
getName() API gets used a lot in the following two situations:
--
... "hello" + Foo->getName() + "bar" ...
--
and
--
cerr << ... << Foo->getName() << ...
--

Support for writing strings to output streams is pretty desirable.
Concatenation is actually quite useful, as the number of uses of it in
LLVM would suggest, but I'm not sure its something StringRef should
support directly.

FWIW, if I add stream support then with the following additions the
API change to Value is almost transparent (there are a few places that
rely on being able to call c_str on the result):
--
  // Support string concatenation using StringRef. Clients should use .str()
  // instead, but this is pervasive enough in LLVM that we provide these as a
  // convenience.

  inline std::string operator+(const std::string &LHS, const StringRef &RHS) {
    return LHS + RHS.str();
  }

  inline std::string operator+(const StringRef &LHS, const std::string &RHS) {
    return LHS.str() + RHS;
  }

  inline bool operator==(const std::string &LHS, const StringRef &RHS) {
    return RHS == LHS;
  }

  inline bool operator!=(const std::string &LHS, const StringRef &RHS) {
    return RHS != LHS;
  }
--

 - Daniel




More information about the llvm-commits mailing list