ostream.h 3.0 KB

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