blob: aa70fbc23e3c0505cb5e9f0282291b5d626eeb00 [file] [log] [blame]
Alex Lorenze82d89c2014-08-22 22:56:03 +00001//===- 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
10#ifndef LLVM_COV_RENDERINGSUPPORT_H
11#define LLVM_COV_RENDERINGSUPPORT_H
12
13#include "llvm/Support/raw_ostream.h"
14#include <utility>
15
16namespace llvm {
17
18/// \brief A helper class that resets the output stream's color if needed
19/// when destroyed.
20class ColoredRawOstream {
Aaron Ballmanf9a18972015-02-15 22:54:22 +000021 ColoredRawOstream(const ColoredRawOstream &OS) = delete;
Alex Lorenze82d89c2014-08-22 22:56:03 +000022
23public:
24 raw_ostream &OS;
25 bool IsColorUsed;
26
27 ColoredRawOstream(raw_ostream &OS, bool IsColorUsed)
28 : OS(OS), IsColorUsed(IsColorUsed) {}
29
30 ColoredRawOstream(ColoredRawOstream &&Other)
31 : OS(Other.OS), IsColorUsed(Other.IsColorUsed) {
32 // Reset the other IsColorUsed so that the other object won't reset the
33 // color when destroyed.
34 Other.IsColorUsed = false;
35 }
36
37 ~ColoredRawOstream() {
38 if (IsColorUsed)
39 OS.resetColor();
40 }
41};
42
43template <typename T>
44inline raw_ostream &operator<<(const ColoredRawOstream &OS, T &&Value) {
45 return OS.OS << std::forward<T>(Value);
46}
47
48/// \brief Change the color of the output stream if the `IsColorUsed` flag
49/// is true. Returns an object that resets the color when destroyed.
50inline ColoredRawOstream colored_ostream(raw_ostream &OS,
51 raw_ostream::Colors Color,
Justin Bognerfe357c02014-09-17 18:23:47 +000052 bool IsColorUsed = true,
53 bool Bold = false, bool BG = false) {
Alex Lorenze82d89c2014-08-22 22:56:03 +000054 if (IsColorUsed)
Justin Bognerfe357c02014-09-17 18:23:47 +000055 OS.changeColor(Color, Bold, BG);
Alex Lorenze82d89c2014-08-22 22:56:03 +000056 return ColoredRawOstream(OS, IsColorUsed);
57}
Vedant Kumarc2983412016-06-23 16:27:08 +000058
59} // namespace llvm
Alex Lorenze82d89c2014-08-22 22:56:03 +000060
61#endif // LLVM_COV_RENDERINGSUPPORT_H