ostream.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. // Libraries should include this header instead of raw_ostream.
  7. #include <concepts>
  8. #include <ostream>
  9. #include <type_traits>
  10. #include "llvm/Support/Compiler.h"
  11. #include "llvm/Support/raw_os_ostream.h"
  12. #include "llvm/Support/raw_ostream.h" // IWYU pragma: export
  13. namespace Carbon {
  14. // CRTP base class for printable types. Children (DerivedT) must implement:
  15. // - auto Print(llvm::raw_ostream& out) const -> void
  16. template <typename DerivedT>
  17. // NOLINTNEXTLINE(bugprone-crtp-constructor-accessibility)
  18. class Printable {
  19. // Provides simple printing for debuggers.
  20. LLVM_DUMP_METHOD auto Dump() const -> std::string {
  21. std::string buffer;
  22. llvm::raw_string_ostream stream(buffer);
  23. static_cast<const DerivedT*>(this)->Print(stream);
  24. return buffer;
  25. }
  26. // Supports printing to llvm::raw_ostream.
  27. friend auto operator<<(llvm::raw_ostream& out, const DerivedT& obj)
  28. -> llvm::raw_ostream& {
  29. obj.Print(out);
  30. return out;
  31. }
  32. // Supports printing to std::ostream.
  33. friend auto operator<<(std::ostream& out, const DerivedT& obj)
  34. -> std::ostream& {
  35. llvm::raw_os_ostream raw_os(out);
  36. obj.Print(raw_os);
  37. return out;
  38. }
  39. // Allows GoogleTest and GoogleMock to print pointers by dereferencing them.
  40. // This is important to allow automatic printing of arguments of mocked
  41. // APIs.
  42. friend auto PrintTo(DerivedT* p, std::ostream* out) -> void {
  43. *out << static_cast<const void*>(p);
  44. // Also print the object if non-null.
  45. if (p) {
  46. *out << " pointing to " << *p;
  47. }
  48. }
  49. };
  50. // Helper class for printing strings with escapes.
  51. //
  52. // For example:
  53. // stream << FormatEscaped(str);
  54. // Is equivalent to:
  55. // stream.write_escaped(str);
  56. class FormatEscaped : public Printable<FormatEscaped> {
  57. public:
  58. explicit FormatEscaped(llvm::StringRef str, bool use_hex_escapes = false)
  59. : str_(str), use_hex_escapes_(use_hex_escapes) {}
  60. auto Print(llvm::raw_ostream& out) const -> void {
  61. out.write_escaped(str_, use_hex_escapes_);
  62. }
  63. private:
  64. llvm::StringRef str_;
  65. bool use_hex_escapes_;
  66. };
  67. // Returns the result of printing the value.
  68. template <typename T>
  69. requires std::derived_from<T, Printable<T>>
  70. inline auto PrintToString(const T& val) -> std::string {
  71. std::string str;
  72. llvm::raw_string_ostream stream(str);
  73. stream << val;
  74. return str;
  75. }
  76. } // namespace Carbon
  77. namespace llvm {
  78. // Injects an `operator<<` overload into the `llvm` namespace which detects LLVM
  79. // types with `raw_ostream` overloads and uses that to map to a `std::ostream`
  80. // overload. This allows LLVM types to be printed to `std::ostream` via their
  81. // `raw_ostream` operator overloads, which is needed both for logging and
  82. // testing.
  83. //
  84. // To make this overload be unusually low priority, it is designed to take even
  85. // the `std::ostream` parameter as a template, and SFINAE disable itself unless
  86. // that template parameter is derived from `std::ostream`. This ensures that an
  87. // *explicit* operator will be preferred when provided. Some LLVM types may have
  88. // this, and so we want to prioritize accordingly.
  89. //
  90. // It would be slightly cleaner for LLVM itself to provide this overload in
  91. // `raw_os_ostream.h` so that we wouldn't need to inject into LLVM's namespace,
  92. // but supporting `std::ostream` isn't a priority for LLVM so we handle it
  93. // locally instead.
  94. template <typename StreamT, typename ClassT>
  95. requires std::derived_from<std::decay_t<StreamT>, std::ostream> &&
  96. (!std::same_as<std::decay_t<ClassT>, raw_ostream>) &&
  97. requires(raw_ostream& os, const ClassT& value) { os << value; }
  98. auto operator<<(StreamT& standard_out, const ClassT& value) -> StreamT& {
  99. raw_os_ostream(standard_out) << value;
  100. return standard_out;
  101. }
  102. } // namespace llvm
  103. #endif // CARBON_COMMON_OSTREAM_H_