ostream.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_COMMON_OSTREAM_H_
  5. #define CARBON_COMMON_OSTREAM_H_
  6. #include <ostream>
  7. #include <type_traits>
  8. #include "llvm/Support/raw_os_ostream.h"
  9. // Libraries should include this header instead of raw_ostream.
  10. #include "llvm/Support/Compiler.h"
  11. #include "llvm/Support/raw_ostream.h" // IWYU pragma: export
  12. namespace Carbon {
  13. // CRTP base class for printable types. Children (DerivedT) must implement:
  14. // - auto Print(llvm::raw_ostream& out) -> void
  15. template <typename DerivedT>
  16. class Printable {
  17. // Provides simple printing for debuggers.
  18. LLVM_DUMP_METHOD void Dump() const {
  19. static_cast<const DerivedT*>(this)->Print(llvm::errs());
  20. }
  21. // Supports printing to llvm::raw_ostream.
  22. friend auto operator<<(llvm::raw_ostream& out, const DerivedT& obj)
  23. -> llvm::raw_ostream& {
  24. obj.Print(out);
  25. return out;
  26. }
  27. // Supports printing to std::ostream.
  28. friend auto operator<<(std::ostream& out, const DerivedT& obj)
  29. -> std::ostream& {
  30. llvm::raw_os_ostream raw_os(out);
  31. obj.Print(raw_os);
  32. return out;
  33. }
  34. // Allows GoogleTest and GoogleMock to print pointers by dereferencing them.
  35. // This is important to allow automatic printing of arguments of mocked
  36. // APIs.
  37. friend auto PrintTo(DerivedT* p, std::ostream* out) -> void {
  38. *out << static_cast<const void*>(p);
  39. // Also print the object if non-null.
  40. if (p) {
  41. *out << " pointing to " << *p;
  42. }
  43. }
  44. };
  45. // Returns the result of printing the value.
  46. template <typename T>
  47. inline auto PrintToString(const T& val) -> std::string {
  48. std::string str;
  49. llvm::raw_string_ostream stream(str);
  50. stream << val;
  51. return str;
  52. }
  53. } // namespace Carbon
  54. namespace llvm {
  55. // Injects an `operator<<` overload into the `llvm` namespace which detects LLVM
  56. // types with `raw_ostream` overloads and uses that to map to a `std::ostream`
  57. // overload. This allows LLVM types to be printed to `std::ostream` via their
  58. // `raw_ostream` operator overloads, which is needed both for logging and
  59. // testing.
  60. //
  61. // To make this overload be unusually low priority, it is designed to take even
  62. // the `std::ostream` parameter as a template, and SFINAE disable itself unless
  63. // that template parameter matches `std::ostream`. This ensures that an
  64. // *explicit* operator will be preferred when provided. Some LLVM types may have
  65. // this, and so we want to prioritize accordingly.
  66. //
  67. // It would be slightly cleaner for LLVM itself to provide this overload in
  68. // `raw_os_ostream.h` so that we wouldn't need to inject into LLVM's namespace,
  69. // but supporting `std::ostream` isn't a priority for LLVM so we handle it
  70. // locally instead.
  71. template <typename StreamT, typename ClassT,
  72. typename = std::enable_if_t<
  73. std::is_base_of_v<std::ostream, std::decay_t<StreamT>>>,
  74. typename = std::enable_if_t<
  75. !std::is_same_v<std::decay_t<ClassT>, raw_ostream>>>
  76. auto operator<<(StreamT& standard_out, const ClassT& value) -> StreamT& {
  77. raw_os_ostream(standard_out) << value;
  78. return standard_out;
  79. }
  80. } // namespace llvm
  81. #endif // CARBON_COMMON_OSTREAM_H_