RenderingSupport.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //===- RenderingSupport.h - output stream rendering support functions ----===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef LLVM_COV_RENDERINGSUPPORT_H
  10. #define LLVM_COV_RENDERINGSUPPORT_H
  11. #include "llvm/Support/raw_ostream.h"
  12. #include <utility>
  13. namespace llvm {
  14. /// \brief A helper class that resets the output stream's color if needed
  15. /// when destroyed.
  16. class ColoredRawOstream {
  17. ColoredRawOstream(const ColoredRawOstream &OS) = delete;
  18. public:
  19. raw_ostream &OS;
  20. bool IsColorUsed;
  21. ColoredRawOstream(raw_ostream &OS, bool IsColorUsed)
  22. : OS(OS), IsColorUsed(IsColorUsed) {}
  23. ColoredRawOstream(ColoredRawOstream &&Other)
  24. : OS(Other.OS), IsColorUsed(Other.IsColorUsed) {
  25. // Reset the other IsColorUsed so that the other object won't reset the
  26. // color when destroyed.
  27. Other.IsColorUsed = false;
  28. }
  29. ~ColoredRawOstream() {
  30. if (IsColorUsed)
  31. OS.resetColor();
  32. }
  33. };
  34. template <typename T>
  35. inline raw_ostream &operator<<(const ColoredRawOstream &OS, T &&Value) {
  36. return OS.OS << std::forward<T>(Value);
  37. }
  38. /// \brief Change the color of the output stream if the `IsColorUsed` flag
  39. /// is true. Returns an object that resets the color when destroyed.
  40. inline ColoredRawOstream colored_ostream(raw_ostream &OS,
  41. raw_ostream::Colors Color,
  42. bool IsColorUsed = true,
  43. bool Bold = false, bool BG = false) {
  44. if (IsColorUsed)
  45. OS.changeColor(Color, Bold, BG);
  46. return ColoredRawOstream(OS, IsColorUsed);
  47. }
  48. }
  49. #endif // LLVM_COV_RENDERINGSUPPORT_H