ostream.h 3.2 KB

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