| 1 |
/* { dg-options "-O" } */ |
| 2 |
|
| 3 |
/* This plugin exercises the text_art code. */ |
| 4 |
|
| 5 |
#define INCLUDE_VECTOR |
| 6 |
#include "gcc-plugin.h" |
| 7 |
#include "config.h" |
| 8 |
#include "system.h" |
| 9 |
#include "coretypes.h" |
| 10 |
#include "plugin-version.h" |
| 11 |
#include "diagnostic.h" |
| 12 |
#include "diagnostic-diagram.h" |
| 13 |
#include "text-art/canvas.h" |
| 14 |
#include "text-art/table.h" |
| 15 |
|
| 16 |
int plugin_is_GPL_compatible; |
| 17 |
|
| 18 |
using namespace text_art; |
| 19 |
|
| 20 |
/* Canvas tests. */ |
| 21 |
|
| 22 |
static void |
| 23 |
emit_canvas (const canvas &c, const char *alt_text) |
| 24 |
{ |
| 25 |
diagnostic_diagram diagram (c, alt_text); |
| 26 |
global_dc->emit_diagram (diagram); |
| 27 |
} |
| 28 |
|
| 29 |
static void |
| 30 |
test_abc () |
| 31 |
{ |
| 32 |
style_manager sm; |
| 33 |
canvas c (canvas::size_t (3, 3), sm); |
| 34 |
c.paint (canvas::coord_t (0, 0), styled_unichar ('A')); |
| 35 |
c.paint (canvas::coord_t (1, 1), styled_unichar ('B')); |
| 36 |
c.paint (canvas::coord_t (2, 2), styled_unichar ('C')); |
| 37 |
emit_canvas (c, "test_abc"); |
| 38 |
} |
| 39 |
|
| 40 |
/* Test of procedural art using 24-bit color: chess starting position. */ |
| 41 |
|
| 42 |
static void |
| 43 |
test_chessboard () |
| 44 |
{ |
| 45 |
/* With the exception of NONE, these are in order of the chess symbols |
| 46 |
in the Unicode Miscellaneous Symbols block. */ |
| 47 |
enum class piece { KING, QUEEN, ROOK, BISHOP, KNIGHT, PAWN, NONE }; |
| 48 |
enum class color { BLACK, WHITE, NONE }; |
| 49 |
|
| 50 |
style_manager sm; |
| 51 |
|
| 52 |
/* We assume double-column chars for the pieces, so allow two canvas |
| 53 |
columns per square. */ |
| 54 |
canvas canvas (canvas::size_t (16, 8), sm); |
| 55 |
|
| 56 |
for (int x = 0; x < 8; x++) |
| 57 |
for (int y = 0; y < 8; y++) |
| 58 |
{ |
| 59 |
enum piece piece_kind; |
| 60 |
enum color piece_color; |
| 61 |
switch (y) |
| 62 |
{ |
| 63 |
case 0: |
| 64 |
case 7: |
| 65 |
switch (x) |
| 66 |
{ |
| 67 |
default: |
| 68 |
gcc_unreachable (); |
| 69 |
case 0: |
| 70 |
piece_kind = piece::ROOK; |
| 71 |
break; |
| 72 |
case 1: |
| 73 |
piece_kind = piece::KNIGHT; |
| 74 |
break; |
| 75 |
case 2: |
| 76 |
piece_kind = piece::BISHOP; |
| 77 |
break; |
| 78 |
case 3: |
| 79 |
piece_kind = piece::QUEEN; |
| 80 |
break; |
| 81 |
case 4: |
| 82 |
piece_kind = piece::KING; |
| 83 |
break; |
| 84 |
case 5: |
| 85 |
piece_kind = piece::BISHOP; |
| 86 |
break; |
| 87 |
case 6: |
| 88 |
piece_kind = piece::KNIGHT; |
| 89 |
break; |
| 90 |
case 7: |
| 91 |
piece_kind = piece::ROOK; |
| 92 |
break; |
| 93 |
} |
| 94 |
piece_color = (y == 0) ? color::BLACK : color::WHITE; |
| 95 |
break; |
| 96 |
case 1: |
| 97 |
case 6: |
| 98 |
piece_kind = piece::PAWN; |
| 99 |
piece_color = (y == 1) ? color::BLACK : color::WHITE; |
| 100 |
break; |
| 101 |
default: |
| 102 |
piece_kind = piece::NONE; |
| 103 |
piece_color = color::NONE; |
| 104 |
break; |
| 105 |
} |
| 106 |
|
| 107 |
style s; |
| 108 |
const bool white_square = (x + y) % 2 == 0; |
| 109 |
if (white_square) |
| 110 |
s.m_bg_color = style::color (0xf0, 0xd9, 0xb5); |
| 111 |
else |
| 112 |
s.m_bg_color = style::color (0xb5, 0x88, 0x63); |
| 113 |
switch (piece_color) |
| 114 |
{ |
| 115 |
default: |
| 116 |
gcc_unreachable (); |
| 117 |
case color::WHITE: |
| 118 |
s.m_fg_color = style::color (0xff, 0xff, 0xff); |
| 119 |
break; |
| 120 |
case color::BLACK: |
| 121 |
s.m_fg_color = style::color (0x00, 0x00, 0x00); |
| 122 |
break; |
| 123 |
case color::NONE: |
| 124 |
break; |
| 125 |
} |
| 126 |
style::id_t style_id = sm.get_or_create_id (s); |
| 127 |
|
| 128 |
cppchar_t ch; |
| 129 |
if (piece_kind == piece::NONE) |
| 130 |
ch = ' '; |
| 131 |
else |
| 132 |
{ |
| 133 |
const cppchar_t WHITE_KING = 0x2654; |
| 134 |
const cppchar_t BLACK_KING = 0x265A; |
| 135 |
cppchar_t base ((piece_color == color::WHITE) |
| 136 |
? WHITE_KING : BLACK_KING); |
| 137 |
ch = base + ((int)piece_kind - (int)piece::KING); |
| 138 |
} |
| 139 |
canvas.paint (canvas::coord_t (x * 2, y), |
| 140 |
canvas::cell_t (ch, false, style_id)); |
| 141 |
canvas.paint (canvas::coord_t (x * 2 + 1, y), |
| 142 |
canvas::cell_t (' ', false, style_id)); |
| 143 |
} |
| 144 |
emit_canvas (canvas, "test_chessboard"); |
| 145 |
} |
| 146 |
|
| 147 |
/* Table tests. */ |
| 148 |
|
| 149 |
static void |
| 150 |
emit_table (const table &table, const style_manager &sm, const char *alt_text) |
| 151 |
{ |
| 152 |
const text_art::theme *theme = global_dc->get_diagram_theme (); |
| 153 |
if (!theme) |
| 154 |
return; |
| 155 |
canvas c (table.to_canvas (*theme, sm)); |
| 156 |
emit_canvas (c, alt_text); |
| 157 |
} |
| 158 |
|
| 159 |
static void |
| 160 |
test_double_width_chars () |
| 161 |
{ |
| 162 |
style_manager sm; |
| 163 |
table table (table::size_t (1, 1)); |
| 164 |
table.set_cell (table::coord_t (0,0), |
| 165 |
styled_string ((cppchar_t)0x1f642)); |
| 166 |
|
| 167 |
emit_table (table, sm, "test_double_width_chars"); |
| 168 |
} |
| 169 |
|
| 170 |
static void |
| 171 |
test_ipv4_header () |
| 172 |
{ |
| 173 |
style_manager sm; |
| 174 |
table table (table::size_t (34, 10)); |
| 175 |
table.set_cell (table::coord_t (0, 0), styled_string (sm, "Offsets")); |
| 176 |
table.set_cell (table::coord_t (1, 0), styled_string (sm, "Octet")); |
| 177 |
table.set_cell (table::coord_t (0, 1), styled_string (sm, "Octet")); |
| 178 |
for (int octet = 0; octet < 4; octet++) |
| 179 |
table.set_cell_span (table::rect_t (table::coord_t (2 + (octet * 8), 0), |
| 180 |
table::size_t (8, 1)), |
| 181 |
styled_string::from_fmt (sm, nullptr, "%i", octet)); |
| 182 |
table.set_cell (table::coord_t (1, 1), styled_string (sm, "Bit")); |
| 183 |
for (int bit = 0; bit < 32; bit++) |
| 184 |
table.set_cell (table::coord_t (bit + 2, 1), |
| 185 |
styled_string::from_fmt (sm, nullptr, "%i", bit)); |
| 186 |
for (int word = 0; word < 6; word++) |
| 187 |
{ |
| 188 |
table.set_cell (table::coord_t (0, word + 2), |
| 189 |
styled_string::from_fmt (sm, nullptr, "%i", word * 4)); |
| 190 |
table.set_cell (table::coord_t (1, word + 2), |
| 191 |
styled_string::from_fmt (sm, nullptr, "%i", word * 32)); |
| 192 |
} |
| 193 |
|
| 194 |
table.set_cell (table::coord_t (0, 8), styled_string (sm, "...")); |
| 195 |
table.set_cell (table::coord_t (1, 8), styled_string (sm, "...")); |
| 196 |
table.set_cell (table::coord_t (0, 9), styled_string (sm, "56")); |
| 197 |
table.set_cell (table::coord_t (1, 9), styled_string (sm, "448")); |
| 198 |
|
| 199 |
#define SET_BITS(FIRST, LAST, NAME) \ |
| 200 |
do { \ |
| 201 |
const int first = (FIRST); \ |
| 202 |
const int last = (LAST); \ |
| 203 |
const char *name = (NAME); \ |
| 204 |
const int row = first / 32; \ |
| 205 |
gcc_assert (last / 32 == row); \ |
| 206 |
table::rect_t rect (table::coord_t ((first % 32) + 2, row + 2), \ |
| 207 |
table::size_t (last + 1 - first , 1)); \ |
| 208 |
table.set_cell_span (rect, styled_string (sm, name)); \ |
| 209 |
} while (0) |
| 210 |
|
| 211 |
SET_BITS (0, 3, "Version"); |
| 212 |
SET_BITS (4, 7, "IHL"); |
| 213 |
SET_BITS (8, 13, "DSCP"); |
| 214 |
SET_BITS (14, 15, "ECN"); |
| 215 |
SET_BITS (16, 31, "Total Length"); |
| 216 |
|
| 217 |
SET_BITS (32 + 0, 32 + 15, "Identification"); |
| 218 |
SET_BITS (32 + 16, 32 + 18, "Flags"); |
| 219 |
SET_BITS (32 + 19, 32 + 31, "Fragment Offset"); |
| 220 |
|
| 221 |
SET_BITS (64 + 0, 64 + 7, "Time To Live"); |
| 222 |
SET_BITS (64 + 8, 64 + 15, "Protocol"); |
| 223 |
SET_BITS (64 + 16, 64 + 31, "Header Checksum"); |
| 224 |
|
| 225 |
SET_BITS (96 + 0, 96 + 31, "Source IP Address"); |
| 226 |
SET_BITS (128 + 0, 128 + 31, "Destination IP Address"); |
| 227 |
|
| 228 |
table.set_cell_span(table::rect_t (table::coord_t (2, 7), |
| 229 |
table::size_t (32, 3)), |
| 230 |
styled_string (sm, "Options")); |
| 231 |
|
| 232 |
emit_table (table, sm, "test_ipv4_header"); |
| 233 |
} |
| 234 |
|
| 235 |
static void |
| 236 |
show_diagrams () |
| 237 |
{ |
| 238 |
test_abc (); |
| 239 |
test_chessboard (); |
| 240 |
test_double_width_chars (); |
| 241 |
test_ipv4_header (); |
| 242 |
} |
| 243 |
|
| 244 |
int |
| 245 |
plugin_init (struct plugin_name_args *plugin_info, |
| 246 |
struct plugin_gcc_version *version) |
| 247 |
{ |
| 248 |
const char *plugin_name = plugin_info->base_name; |
| 249 |
int argc = plugin_info->argc; |
| 250 |
struct plugin_argument *argv = plugin_info->argv; |
| 251 |
|
| 252 |
if (!plugin_default_version_check (version, &gcc_version)) |
| 253 |
return 1; |
| 254 |
|
| 255 |
show_diagrams (); |
| 256 |
|
| 257 |
return 0; |
| 258 |
} |