brintos

brintos / llvm-project-archived public Read only

0
0
Text · 9.3 KiB · 3ee4db3 Raw
261 lines · c
1// Copyright 2005, Google Inc.2// All rights reserved.3//4// Redistribution and use in source and binary forms, with or without5// modification, are permitted provided that the following conditions are6// met:7//8//     * Redistributions of source code must retain the above copyright9// notice, this list of conditions and the following disclaimer.10//     * Redistributions in binary form must reproduce the above11// copyright notice, this list of conditions and the following disclaimer12// in the documentation and/or other materials provided with the13// distribution.14//     * Neither the name of Google Inc. nor the names of its15// contributors may be used to endorse or promote products derived from16// this software without specific prior written permission.17//18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29 30// The Google C++ Testing and Mocking Framework (Google Test)31//32// This header file defines the Message class.33//34// IMPORTANT NOTE: Due to limitation of the C++ language, we have to35// leave some internal implementation details in this header file.36// They are clearly marked by comments like this:37//38//   // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.39//40// Such code is NOT meant to be used by a user directly, and is subject41// to CHANGE WITHOUT NOTICE.  Therefore DO NOT DEPEND ON IT in a user42// program!43 44// IWYU pragma: private, include "gtest/gtest.h"45// IWYU pragma: friend gtest/.*46// IWYU pragma: friend gmock/.*47 48#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_MESSAGE_H_49#define GOOGLETEST_INCLUDE_GTEST_GTEST_MESSAGE_H_50 51#include <limits>52#include <memory>53#include <ostream>54#include <sstream>55#include <string>56 57#include "gtest/internal/gtest-port.h"58 59#ifdef GTEST_HAS_ABSL60#include <type_traits>61 62#include "absl/strings/internal/has_absl_stringify.h"63#include "absl/strings/str_cat.h"64#endif  // GTEST_HAS_ABSL65 66GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \67/* class A needs to have dll-interface to be used by clients of class B */)68 69// Ensures that there is at least one operator<< in the global namespace.70// See Message& operator<<(...) below for why.71void operator<<(const testing::internal::Secret&, int);72 73namespace testing {74 75// The Message class works like an ostream repeater.76//77// Typical usage:78//79//   1. You stream a bunch of values to a Message object.80//      It will remember the text in a stringstream.81//   2. Then you stream the Message object to an ostream.82//      This causes the text in the Message to be streamed83//      to the ostream.84//85// For example;86//87//   testing::Message foo;88//   foo << 1 << " != " << 2;89//   std::cout << foo;90//91// will print "1 != 2".92//93// Message is not intended to be inherited from.  In particular, its94// destructor is not virtual.95//96// Note that stringstream behaves differently in gcc and in MSVC.  You97// can stream a NULL char pointer to it in the former, but not in the98// latter (it causes an access violation if you do).  The Message99// class hides this difference by treating a NULL char pointer as100// "(null)".101class GTEST_API_ Message {102 private:103  // The type of basic IO manipulators (endl, ends, and flush) for104  // narrow streams.105  typedef std::ostream& (*BasicNarrowIoManip)(std::ostream&);106 107 public:108  // Constructs an empty Message.109  Message();110 111  // Copy constructor.112  Message(const Message& msg) : ss_(new ::std::stringstream) {  // NOLINT113    *ss_ << msg.GetString();114  }115 116  // Constructs a Message from a C-string.117  explicit Message(const char* str) : ss_(new ::std::stringstream) {118    *ss_ << str;119  }120 121  // Streams a non-pointer value to this object. If building a version of122  // GoogleTest with ABSL, this overload is only enabled if the value does not123  // have an AbslStringify definition.124  template <typename T125#ifdef GTEST_HAS_ABSL126            ,127            typename std::enable_if<128                !absl::strings_internal::HasAbslStringify<T>::value,  // NOLINT129                int>::type = 0130#endif  // GTEST_HAS_ABSL131            >132  inline Message& operator<<(const T& val) {133        // Some libraries overload << for STL containers.  These134    // overloads are defined in the global namespace instead of ::std.135    //136    // C++'s symbol lookup rule (i.e. Koenig lookup) says that these137    // overloads are visible in either the std namespace or the global138    // namespace, but not other namespaces, including the testing139    // namespace which Google Test's Message class is in.140    //141    // To allow STL containers (and other types that has a << operator142    // defined in the global namespace) to be used in Google Test143    // assertions, testing::Message must access the custom << operator144    // from the global namespace.  With this using declaration,145    // overloads of << defined in the global namespace and those146    // visible via Koenig lookup are both exposed in this function.147    using ::operator<<;148    // LLVM local change to support llvm printables.149    //150    // *ss_ << val;151    *ss_ << llvm_gtest::printable(val);152    // LLVM local change end.153    return *this;154  }155 156#ifdef GTEST_HAS_ABSL157  // Streams a non-pointer value with an AbslStringify definition to this158  // object.159  template <typename T,160            typename std::enable_if<161                absl::strings_internal::HasAbslStringify<T>::value,  // NOLINT162                int>::type = 0>163  inline Message& operator<<(const T& val) {164    // ::operator<< is needed here for a similar reason as with the non-Abseil165    // version above166    using ::operator<<;167    *ss_ << absl::StrCat(val);168    return *this;169  }170#endif  // GTEST_HAS_ABSL171 172  // Streams a pointer value to this object.173  //174  // This function is an overload of the previous one.  When you175  // stream a pointer to a Message, this definition will be used as it176  // is more specialized.  (The C++ Standard, section177  // [temp.func.order].)  If you stream a non-pointer, then the178  // previous definition will be used.179  //180  // The reason for this overload is that streaming a NULL pointer to181  // ostream is undefined behavior.  Depending on the compiler, you182  // may get "0", "(nil)", "(null)", or an access violation.  To183  // ensure consistent result across compilers, we always treat NULL184  // as "(null)".185  template <typename T>186  inline Message& operator<<(T* const& pointer) {  // NOLINT187    if (pointer == nullptr) {188      *ss_ << "(null)";189    } else {190      // LLVM local change to support llvm printables.191      //192      // *ss_ << pointer;193      *ss_ << llvm_gtest::printable(pointer);194      // LLVM local change end.195    }196    return *this;197  }198 199  // Since the basic IO manipulators are overloaded for both narrow200  // and wide streams, we have to provide this specialized definition201  // of operator <<, even though its body is the same as the202  // templatized version above.  Without this definition, streaming203  // endl or other basic IO manipulators to Message will confuse the204  // compiler.205  Message& operator<<(BasicNarrowIoManip val) {206    *ss_ << val;207    return *this;208  }209 210  // Instead of 1/0, we want to see true/false for bool values.211  Message& operator<<(bool b) { return *this << (b ? "true" : "false"); }212 213  // These two overloads allow streaming a wide C string to a Message214  // using the UTF-8 encoding.215  Message& operator<<(const wchar_t* wide_c_str);216  Message& operator<<(wchar_t* wide_c_str);217 218#if GTEST_HAS_STD_WSTRING219  // Converts the given wide string to a narrow string using the UTF-8220  // encoding, and streams the result to this Message object.221  Message& operator<<(const ::std::wstring& wstr);222#endif  // GTEST_HAS_STD_WSTRING223 224  // Gets the text streamed to this object so far as an std::string.225  // Each '\0' character in the buffer is replaced with "\\0".226  //227  // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.228  std::string GetString() const;229 230 private:231  // We'll hold the text streamed to this object here.232  const std::unique_ptr< ::std::stringstream> ss_;233 234  // We declare (but don't implement) this to prevent the compiler235  // from implementing the assignment operator.236  void operator=(const Message&);237};238 239// Streams a Message to an ostream.240inline std::ostream& operator<<(std::ostream& os, const Message& sb) {241  return os << sb.GetString();242}243 244namespace internal {245 246// Converts a streamable value to an std::string.  A NULL pointer is247// converted to "(null)".  When the input value is a ::string,248// ::std::string, ::wstring, or ::std::wstring object, each NUL249// character in it is replaced with "\\0".250template <typename T>251std::string StreamableToString(const T& streamable) {252  return (Message() << streamable).GetString();253}254 255}  // namespace internal256}  // namespace testing257 258GTEST_DISABLE_MSC_WARNINGS_POP_()  //  4251259 260#endif  // GOOGLETEST_INCLUDE_GTEST_GTEST_MESSAGE_H_261