Coverage Report

Created: 2022-01-22 13:19

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/include/lldb/Utility/AnsiTerminal.h
Line
Count
Source (jump to first uncovered line)
1
#ifndef LLDB_UTILITY_ANSITERMINAL_H
2
3
#define LLDB_UTILITY_ANSITERMINAL_H
4
5
//===---------------------AnsiTerminal.h ------------------------*- C++ -*-===//
6
//
7
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
8
// See https://llvm.org/LICENSE.txt for license information.
9
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
10
//
11
//===----------------------------------------------------------------------===//
12
13
#define ANSI_FG_COLOR_BLACK 30
14
#define ANSI_FG_COLOR_RED 31
15
#define ANSI_FG_COLOR_GREEN 32
16
#define ANSI_FG_COLOR_YELLOW 33
17
#define ANSI_FG_COLOR_BLUE 34
18
#define ANSI_FG_COLOR_PURPLE 35
19
#define ANSI_FG_COLOR_CYAN 36
20
#define ANSI_FG_COLOR_WHITE 37
21
22
#define ANSI_BG_COLOR_BLACK 40
23
#define ANSI_BG_COLOR_RED 41
24
#define ANSI_BG_COLOR_GREEN 42
25
#define ANSI_BG_COLOR_YELLOW 43
26
#define ANSI_BG_COLOR_BLUE 44
27
#define ANSI_BG_COLOR_PURPLE 45
28
#define ANSI_BG_COLOR_CYAN 46
29
#define ANSI_BG_COLOR_WHITE 47
30
31
#define ANSI_SPECIAL_FRAMED 51
32
#define ANSI_SPECIAL_ENCIRCLED 52
33
34
#define ANSI_CTRL_NORMAL 0
35
#define ANSI_CTRL_BOLD 1
36
#define ANSI_CTRL_FAINT 2
37
#define ANSI_CTRL_ITALIC 3
38
#define ANSI_CTRL_UNDERLINE 4
39
#define ANSI_CTRL_SLOW_BLINK 5
40
#define ANSI_CTRL_FAST_BLINK 6
41
#define ANSI_CTRL_IMAGE_NEGATIVE 7
42
#define ANSI_CTRL_CONCEAL 8
43
#define ANSI_CTRL_CROSSED_OUT 9
44
45
251k
#define ANSI_ESC_START "\033["
46
251k
#define ANSI_ESC_END "m"
47
48
#define ANSI_STR(s) #s
49
#define ANSI_DEF_STR(s) ANSI_STR(s)
50
51
0
#define ANSI_ESCAPE1(s) ANSI_ESC_START ANSI_DEF_STR(s) ANSI_ESC_END
52
53
#define ANSI_1_CTRL(ctrl1) "\033["##ctrl1 ANSI_ESC_END
54
#define ANSI_2_CTRL(ctrl1, ctrl2) "\033["##ctrl1 ";"##ctrl2 ANSI_ESC_END
55
56
#include "llvm/ADT/ArrayRef.h"
57
#include "llvm/ADT/STLExtras.h"
58
#include "llvm/ADT/StringRef.h"
59
60
#include <string>
61
62
namespace lldb_private {
63
64
namespace ansi {
65
66
inline std::string FormatAnsiTerminalCodes(llvm::StringRef format,
67
9.68k
                                           bool do_color = true) {
68
  // Convert "${ansi.XXX}" tokens to ansi values or clear them if do_color is
69
  // false.
70
9.68k
  static const struct {
71
9.68k
    const char *name;
72
9.68k
    const char *value;
73
9.68k
  } g_color_tokens[] = {
74
9.68k
#define _TO_STR2(_val) #_val
75
9.68k
#define _TO_STR(_val) _TO_STR2(_val)
76
9.68k
      {"fg.black}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_BLACK) ANSI_ESC_END},
77
9.68k
      {"fg.red}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_RED) ANSI_ESC_END},
78
9.68k
      {"fg.green}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_GREEN) ANSI_ESC_END},
79
9.68k
      {"fg.yellow}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_YELLOW) ANSI_ESC_END},
80
9.68k
      {"fg.blue}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_BLUE) ANSI_ESC_END},
81
9.68k
      {"fg.purple}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_PURPLE) ANSI_ESC_END},
82
9.68k
      {"fg.cyan}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_CYAN) ANSI_ESC_END},
83
9.68k
      {"fg.white}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_WHITE) ANSI_ESC_END},
84
9.68k
      {"bg.black}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_BLACK) ANSI_ESC_END},
85
9.68k
      {"bg.red}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_RED) ANSI_ESC_END},
86
9.68k
      {"bg.green}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_GREEN) ANSI_ESC_END},
87
9.68k
      {"bg.yellow}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_YELLOW) ANSI_ESC_END},
88
9.68k
      {"bg.blue}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_BLUE) ANSI_ESC_END},
89
9.68k
      {"bg.purple}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_PURPLE) ANSI_ESC_END},
90
9.68k
      {"bg.cyan}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_CYAN) ANSI_ESC_END},
91
9.68k
      {"bg.white}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_WHITE) ANSI_ESC_END},
92
9.68k
      {"normal}", ANSI_ESC_START _TO_STR(ANSI_CTRL_NORMAL) ANSI_ESC_END},
93
9.68k
      {"bold}", ANSI_ESC_START _TO_STR(ANSI_CTRL_BOLD) ANSI_ESC_END},
94
9.68k
      {"faint}", ANSI_ESC_START _TO_STR(ANSI_CTRL_FAINT) ANSI_ESC_END},
95
9.68k
      {"italic}", ANSI_ESC_START _TO_STR(ANSI_CTRL_ITALIC) ANSI_ESC_END},
96
9.68k
      {"underline}", ANSI_ESC_START _TO_STR(ANSI_CTRL_UNDERLINE) ANSI_ESC_END},
97
9.68k
      {"slow-blink}",
98
9.68k
       ANSI_ESC_START _TO_STR(ANSI_CTRL_SLOW_BLINK) ANSI_ESC_END},
99
9.68k
      {"fast-blink}",
100
9.68k
       ANSI_ESC_START _TO_STR(ANSI_CTRL_FAST_BLINK) ANSI_ESC_END},
101
9.68k
      {"negative}",
102
9.68k
       ANSI_ESC_START _TO_STR(ANSI_CTRL_IMAGE_NEGATIVE) ANSI_ESC_END},
103
9.68k
      {"conceal}", ANSI_ESC_START _TO_STR(ANSI_CTRL_CONCEAL) ANSI_ESC_END},
104
9.68k
      {"crossed-out}",
105
9.68k
       ANSI_ESC_START _TO_STR(ANSI_CTRL_CROSSED_OUT) ANSI_ESC_END},
106
9.68k
#undef _TO_STR
107
9.68k
#undef _TO_STR2
108
9.68k
  };
109
9.68k
  auto codes = llvm::makeArrayRef(g_color_tokens);
110
111
9.68k
  static const char tok_hdr[] = "${ansi.";
112
113
9.68k
  std::string fmt;
114
9.97k
  while (!format.empty()) {
115
9.72k
    llvm::StringRef left, right;
116
9.72k
    std::tie(left, right) = format.split(tok_hdr);
117
118
9.72k
    fmt += left;
119
120
9.72k
    if (left == format && 
right.empty()9.43k
) {
121
      // The header was not found.  Just exit.
122
9.43k
      break;
123
9.43k
    }
124
125
285
    bool found_code = false;
126
3.49k
    for (const auto &code : codes) {
127
3.49k
      if (!right.consume_front(code.name))
128
3.21k
        continue;
129
130
282
      if (do_color)
131
282
        fmt.append(code.value);
132
282
      found_code = true;
133
282
      break;
134
3.49k
    }
135
285
    format = right;
136
    // If we haven't found a valid replacement value, we just copy the string
137
    // to the result without any modifications.
138
285
    if (!found_code)
139
3
      fmt.append(tok_hdr);
140
285
  }
141
9.68k
  return fmt;
142
9.68k
}
143
}
144
} // namespace lldb_private
145
146
#endif