[llvm-commits] PATCH: Teach googletest to use raw_ostream.

Jeffrey Yasskin jyasskin at google.com
Sat Sep 5 21:35:23 PDT 2009


On Fri, Sep 4, 2009 at 4:19 PM, Jeffrey Yasskin<jyasskin at google.com> wrote:
> This is a little tricky, and can break when there are implicit
> conversions from types raw_ostream understands but std::ostream
> doesn't, but it should increase the number of cases that Just Work.

I pinged the googletest list to let them know what we needed and how
I'd worked around it, in case they want to develop a more general
solution. Zhanyong replied with the following. Personally, I think
what I've done is better than either of the options he proposes, but
if you guys like one of his options better, I can switch to it.

---------- Forwarded message ----------
From: Zhanyong Wan (λx.x x) <wan at google.com>
Date: 2009/9/5
Subject: Re: [googletest] Using streams other than std::ostream
To: Jeffrey Yasskin <jyasskin at gmail.com>
Cc: Google C++ Testing Framework <googletestframework at googlegroups.com>

Instead of maintaining a local hack, would you be happy with an iomap
that turns std::ostream to an llvm::raw_ostream?

 EXPECT_FOO(...) << "Ordinary object " << x << " and llvm objects "
                 << from_llvm << some_llvm_object
                 << some_other_llvm_object;

where:

 struct from_llvm_t {};
 const from_llvm_t from_llvm = {};

 class OstreamAdaptor : public llvm::raw_ostream {
  public:
   explicit OstreamAdaptor(std::ostream* os) : os_(os) {}
   ...
  private:
   std::ostream* const os_;
 };

 OstreamAdaptor operator<<(std::ostream& os, from_llvm_t) {
   return OstreamAdaptor(os);
 }

This would require you to write "<< from_llvm" before you can stream
llvm objects, but it may not be too bad - and I kind of like the
explicitness.

An even easier-to-implement approach would be to define a Show()
function that returns string, s.t. you write

 EXPECT_FOO(...) << "Ordinary object " << x << " and llvm objects "
                 << Show(some_llvm_object)
                 << Show(some_other_llvm_object);




More information about the llvm-commits mailing list