1 //===- RenderingSupport.h - output stream rendering support functions ----===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_COV_RENDERINGSUPPORT_H
10 #define LLVM_COV_RENDERINGSUPPORT_H
12 #include "llvm/Support/raw_ostream.h"
17 /// A helper class that resets the output stream's color if needed
19 class ColoredRawOstream
{
20 ColoredRawOstream(const ColoredRawOstream
&OS
) = delete;
26 ColoredRawOstream(raw_ostream
&OS
, bool IsColorUsed
)
27 : OS(OS
), IsColorUsed(IsColorUsed
) {}
29 ColoredRawOstream(ColoredRawOstream
&&Other
)
30 : OS(Other
.OS
), IsColorUsed(Other
.IsColorUsed
) {
31 // Reset the other IsColorUsed so that the other object won't reset the
32 // color when destroyed.
33 Other
.IsColorUsed
= false;
36 ~ColoredRawOstream() {
43 inline raw_ostream
&operator<<(const ColoredRawOstream
&OS
, T
&&Value
) {
44 return OS
.OS
<< std::forward
<T
>(Value
);
47 /// Change the color of the output stream if the `IsColorUsed` flag
48 /// is true. Returns an object that resets the color when destroyed.
49 inline ColoredRawOstream
colored_ostream(raw_ostream
&OS
,
50 raw_ostream::Colors Color
,
51 bool IsColorUsed
= true,
52 bool Bold
= false, bool BG
= false) {
54 OS
.changeColor(Color
, Bold
, BG
);
55 return ColoredRawOstream(OS
, IsColorUsed
);
60 #endif // LLVM_COV_RENDERINGSUPPORT_H