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