xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-cov/RenderingSupport.h (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===- RenderingSupport.h - output stream rendering support functions  ----===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #ifndef LLVM_COV_RENDERINGSUPPORT_H
10*0b57cec5SDimitry Andric #define LLVM_COV_RENDERINGSUPPORT_H
11*0b57cec5SDimitry Andric 
12*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
13*0b57cec5SDimitry Andric #include <utility>
14*0b57cec5SDimitry Andric 
15*0b57cec5SDimitry Andric namespace llvm {
16*0b57cec5SDimitry Andric 
17*0b57cec5SDimitry Andric /// A helper class that resets the output stream's color if needed
18*0b57cec5SDimitry Andric /// when destroyed.
19*0b57cec5SDimitry Andric class ColoredRawOstream {
20*0b57cec5SDimitry Andric   ColoredRawOstream(const ColoredRawOstream &OS) = delete;
21*0b57cec5SDimitry Andric 
22*0b57cec5SDimitry Andric public:
23*0b57cec5SDimitry Andric   raw_ostream &OS;
24*0b57cec5SDimitry Andric   bool IsColorUsed;
25*0b57cec5SDimitry Andric 
ColoredRawOstream(raw_ostream & OS,bool IsColorUsed)26*0b57cec5SDimitry Andric   ColoredRawOstream(raw_ostream &OS, bool IsColorUsed)
27*0b57cec5SDimitry Andric       : OS(OS), IsColorUsed(IsColorUsed) {}
28*0b57cec5SDimitry Andric 
ColoredRawOstream(ColoredRawOstream && Other)29*0b57cec5SDimitry Andric   ColoredRawOstream(ColoredRawOstream &&Other)
30*0b57cec5SDimitry Andric       : OS(Other.OS), IsColorUsed(Other.IsColorUsed) {
31*0b57cec5SDimitry Andric     // Reset the other IsColorUsed so that the other object won't reset the
32*0b57cec5SDimitry Andric     // color when destroyed.
33*0b57cec5SDimitry Andric     Other.IsColorUsed = false;
34*0b57cec5SDimitry Andric   }
35*0b57cec5SDimitry Andric 
~ColoredRawOstream()36*0b57cec5SDimitry Andric   ~ColoredRawOstream() {
37*0b57cec5SDimitry Andric     if (IsColorUsed)
38*0b57cec5SDimitry Andric       OS.resetColor();
39*0b57cec5SDimitry Andric   }
40*0b57cec5SDimitry Andric };
41*0b57cec5SDimitry Andric 
42*0b57cec5SDimitry Andric template <typename T>
43*0b57cec5SDimitry Andric inline raw_ostream &operator<<(const ColoredRawOstream &OS, T &&Value) {
44*0b57cec5SDimitry Andric   return OS.OS << std::forward<T>(Value);
45*0b57cec5SDimitry Andric }
46*0b57cec5SDimitry Andric 
47*0b57cec5SDimitry Andric /// Change the color of the output stream if the `IsColorUsed` flag
48*0b57cec5SDimitry Andric /// is true. Returns an object that resets the color when destroyed.
49*0b57cec5SDimitry Andric inline ColoredRawOstream colored_ostream(raw_ostream &OS,
50*0b57cec5SDimitry Andric                                          raw_ostream::Colors Color,
51*0b57cec5SDimitry Andric                                          bool IsColorUsed = true,
52*0b57cec5SDimitry Andric                                          bool Bold = false, bool BG = false) {
53*0b57cec5SDimitry Andric   if (IsColorUsed)
54*0b57cec5SDimitry Andric     OS.changeColor(Color, Bold, BG);
55*0b57cec5SDimitry Andric   return ColoredRawOstream(OS, IsColorUsed);
56*0b57cec5SDimitry Andric }
57*0b57cec5SDimitry Andric 
58*0b57cec5SDimitry Andric } // namespace llvm
59*0b57cec5SDimitry Andric 
60*0b57cec5SDimitry Andric #endif // LLVM_COV_RENDERINGSUPPORT_H
61