Line data Source code
1 : /* Various declarations for language-independent pretty-print subroutines.
2 : Copyright (C) 2003-2026 Free Software Foundation, Inc.
3 : Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it under
8 : the terms of the GNU General Public License as published by the Free
9 : Software Foundation; either version 3, or (at your option) any later
10 : version.
11 :
12 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #include "config.h"
22 : #define INCLUDE_VECTOR
23 : #include "system.h"
24 : #include "coretypes.h"
25 : #include "intl.h"
26 : #include "pretty-print.h"
27 : #include "pretty-print-format-impl.h"
28 : #include "pretty-print-markup.h"
29 : #include "pretty-print-urlifier.h"
30 : #include "diagnostics/color.h"
31 : #include "diagnostics/event-id.h"
32 : #include "diagnostics/dumping.h"
33 : #include "diagnostic-highlight-colors.h"
34 : #include "auto-obstack.h"
35 : #include "selftest.h"
36 :
37 : #if HAVE_ICONV
38 : #include <iconv.h>
39 : #endif
40 :
41 : static int
42 : decode_utf8_char (const unsigned char *, size_t len, unsigned int *);
43 :
44 : #ifdef __MINGW32__
45 :
46 : /* Replacement for fputs() that handles ANSI escape codes on Windows NT.
47 : Contributed by: Liu Hao (lh_mouse at 126 dot com)
48 :
49 : Extended by: Peter Damianov
50 : Converts UTF-8 to UTF-16 if outputting to a console, so that emojis and
51 : various other unicode characters don't get mojibak'd.
52 :
53 : XXX: This file is compiled into libcommon.a that will be self-contained.
54 : It looks like that these functions can be put nowhere else. */
55 :
56 : #include <io.h>
57 : #define WIN32_LEAN_AND_MEAN 1
58 : #include <windows.h>
59 :
60 : /* Convert UTF-8 string to UTF-16.
61 : Returns true if conversion was performed, false if string is pure ASCII.
62 :
63 : If the string contains only ASCII characters, returns false
64 : without allocating any memory. Otherwise, a buffer that the caller
65 : must free is allocated and the string is converted into it. */
66 : static bool
67 : mingw_utf8_str_to_utf16_str (const char *utf8_str, size_t utf8_len, wchar_t **utf16_str,
68 : size_t *utf16_len)
69 : {
70 : if (utf8_len == 0)
71 : {
72 : *utf16_str = NULL;
73 : *utf16_len = 0;
74 : return false; /* No conversion needed for empty string. */
75 : }
76 :
77 : /* First pass: scan for non-ASCII and count UTF-16 code units needed. */
78 : size_t utf16_count = 0;
79 : const unsigned char *p = (const unsigned char *) utf8_str;
80 : const unsigned char *end = p + utf8_len;
81 : bool found_non_ascii = false;
82 :
83 : while (p < end)
84 : {
85 : if (*p <= 127)
86 : {
87 : /* ASCII character - count as 1 UTF-16 unit and advance. */
88 : utf16_count++;
89 : p++;
90 : }
91 : else
92 : {
93 : /* Non-ASCII character - decode UTF-8 sequence. */
94 : found_non_ascii = true;
95 : unsigned int codepoint;
96 : int utf8_char_len = decode_utf8_char (p, end - p, &codepoint);
97 :
98 : if (utf8_char_len == 0)
99 : return false; /* Invalid UTF-8. */
100 :
101 : if (codepoint <= 0xFFFF)
102 : utf16_count += 1; /* Single UTF-16 unit. */
103 : else
104 : utf16_count += 2; /* Surrogate pair. */
105 :
106 : p += utf8_char_len;
107 : }
108 : }
109 :
110 : /* If string is pure ASCII, no conversion needed. */
111 : if (!found_non_ascii)
112 : return false;
113 :
114 : *utf16_str = (wchar_t *) xmalloc (utf16_count * sizeof (wchar_t));
115 : *utf16_len = utf16_count;
116 :
117 : /* Second pass: convert UTF-8 to UTF-16. */
118 : wchar_t *out = *utf16_str;
119 : p = (const unsigned char *) utf8_str;
120 :
121 : while (p < end)
122 : {
123 : if (*p <= 127)
124 : {
125 : /* ASCII character. */
126 : *out++ = (wchar_t) *p++;
127 : }
128 : else
129 : {
130 : /* Non-ASCII character - decode and convert. */
131 : unsigned int codepoint;
132 : int utf8_char_len = decode_utf8_char (p, end - p, &codepoint);
133 :
134 : if (codepoint <= 0xFFFF)
135 : {
136 : *out++ = (wchar_t) codepoint;
137 : }
138 : else
139 : {
140 : /* Convert to UTF-16 surrogate pair. */
141 : codepoint -= 0x10000;
142 : *out++ = (wchar_t) (0xD800 + (codepoint >> 10));
143 : *out++ = (wchar_t) (0xDC00 + (codepoint & 0x3FF));
144 : }
145 :
146 : p += utf8_char_len;
147 : }
148 : }
149 :
150 : return true;
151 : }
152 :
153 : /* Check if the handle is a console. */
154 : static bool
155 : is_console_handle (HANDLE h)
156 : {
157 : DWORD mode;
158 : return GetConsoleMode (h, &mode);
159 : }
160 :
161 : /* Write all bytes in [s,s+n) into the specified stream.
162 : If outputting to a Windows console, convert UTF-8 to UTF-16 if needed.
163 : Errors are ignored. */
164 : static void
165 : write_all (HANDLE h, const char *s, size_t n)
166 : {
167 : /* If writing to console, try to convert from UTF-8 to UTF-16 and use
168 : WriteConsoleW. utf8_to_utf16 will return false if the string is pure
169 : ASCII, in which case we fall back to the regular WriteFile path. */
170 : if (is_console_handle (h))
171 : {
172 : wchar_t *utf16_str;
173 : size_t utf16_len;
174 :
175 : if (mingw_utf8_str_to_utf16_str (s, n, &utf16_str, &utf16_len))
176 : {
177 : DWORD written;
178 : WriteConsoleW (h, utf16_str, utf16_len, &written, NULL);
179 : free (utf16_str);
180 : return;
181 : }
182 : /* If UTF-8 conversion returned false, fall back to WriteFile. */
183 : }
184 :
185 : /* WriteFile for regular files or when UTF-16 conversion is not needed. */
186 : size_t rem = n;
187 : DWORD step;
188 :
189 : while (rem != 0)
190 : {
191 : if (rem <= UINT_MAX)
192 : step = rem;
193 : else
194 : step = UINT_MAX;
195 : if (!WriteFile (h, s + n - rem, step, &step, NULL))
196 : break;
197 : rem -= step;
198 : }
199 : }
200 :
201 : /* Find the beginning of an escape sequence.
202 : There are two cases:
203 : 1. If the sequence begins with an ESC character (0x1B) and a second
204 : character X in [0x40,0x5F], returns X and stores a pointer to
205 : the third character into *head.
206 : 2. If the sequence begins with a character X in [0x80,0x9F], returns
207 : (X-0x40) and stores a pointer to the second character into *head.
208 : Stores the number of ESC character(s) in *prefix_len.
209 : Returns 0 if no such sequence can be found. */
210 : static int
211 : find_esc_head (int *prefix_len, const char **head, const char *str)
212 : {
213 : int c;
214 : const char *r = str;
215 : int escaped = 0;
216 :
217 : for (;;)
218 : {
219 : c = (unsigned char) *r;
220 : if (c == 0)
221 : {
222 : /* Not found. */
223 : return 0;
224 : }
225 : if (escaped && 0x40 <= c && c <= 0x5F)
226 : {
227 : /* Found (case 1). */
228 : *prefix_len = 2;
229 : *head = r + 1;
230 : return c;
231 : }
232 : if (0x80 <= c && c <= 0x9F)
233 : {
234 : /* Found (case 2). */
235 : *prefix_len = 1;
236 : *head = r + 1;
237 : return c - 0x40;
238 : }
239 : ++r;
240 : escaped = c == 0x1B;
241 : }
242 : }
243 :
244 : /* Find the terminator of an escape sequence.
245 : str should be the value stored in *head by a previous successful
246 : call to find_esc_head().
247 : Returns 0 if no such sequence can be found. */
248 : static int
249 : find_esc_terminator (const char **term, const char *str)
250 : {
251 : int c;
252 : const char *r = str;
253 :
254 : for (;;)
255 : {
256 : c = (unsigned char) *r;
257 : if (c == 0)
258 : {
259 : /* Not found. */
260 : return 0;
261 : }
262 : if (0x40 <= c && c <= 0x7E)
263 : {
264 : /* Found. */
265 : *term = r;
266 : return c;
267 : }
268 : ++r;
269 : }
270 : }
271 :
272 : /* Handle a sequence of codes. Sequences that are invalid, reserved,
273 : unrecognized or unimplemented are ignored silently.
274 : There isn't much we can do because of lameness of Windows consoles. */
275 : static void
276 : eat_esc_sequence (HANDLE h, int esc_code,
277 : const char *esc_head, const char *esc_term)
278 : {
279 : /* Numbers in an escape sequence cannot be negative, because
280 : a minus sign in the middle of it would have terminated it. */
281 : long n1, n2;
282 : char *eptr;
283 : const char *delim;
284 : CONSOLE_SCREEN_BUFFER_INFO sb;
285 : COORD cr;
286 : /* ED and EL parameters. */
287 : DWORD cnt, step;
288 : long rows;
289 : /* SGR parameters. */
290 : WORD attrib_add, attrib_rm;
291 : const char *param;
292 :
293 : switch (MAKEWORD (esc_code, *esc_term))
294 : {
295 : /* ESC [ n1 'A'
296 : Move the cursor up by n1 characters. */
297 : case MAKEWORD ('[', 'A'):
298 : if (esc_head == esc_term)
299 : n1 = 1;
300 : else
301 : {
302 : n1 = strtol (esc_head, &eptr, 10);
303 : if (eptr != esc_term)
304 : break;
305 : }
306 :
307 : if (GetConsoleScreenBufferInfo (h, &sb))
308 : {
309 : cr = sb.dwCursorPosition;
310 : /* Stop at the topmost boundary. */
311 : if (cr.Y > n1)
312 : cr.Y -= n1;
313 : else
314 : cr.Y = 0;
315 : SetConsoleCursorPosition (h, cr);
316 : }
317 : break;
318 :
319 : /* ESC [ n1 'B'
320 : Move the cursor down by n1 characters. */
321 : case MAKEWORD ('[', 'B'):
322 : if (esc_head == esc_term)
323 : n1 = 1;
324 : else
325 : {
326 : n1 = strtol (esc_head, &eptr, 10);
327 : if (eptr != esc_term)
328 : break;
329 : }
330 :
331 : if (GetConsoleScreenBufferInfo (h, &sb))
332 : {
333 : cr = sb.dwCursorPosition;
334 : /* Stop at the bottommost boundary. */
335 : if (sb.dwSize.Y - cr.Y > n1)
336 : cr.Y += n1;
337 : else
338 : cr.Y = sb.dwSize.Y;
339 : SetConsoleCursorPosition (h, cr);
340 : }
341 : break;
342 :
343 : /* ESC [ n1 'C'
344 : Move the cursor right by n1 characters. */
345 : case MAKEWORD ('[', 'C'):
346 : if (esc_head == esc_term)
347 : n1 = 1;
348 : else
349 : {
350 : n1 = strtol (esc_head, &eptr, 10);
351 : if (eptr != esc_term)
352 : break;
353 : }
354 :
355 : if (GetConsoleScreenBufferInfo (h, &sb))
356 : {
357 : cr = sb.dwCursorPosition;
358 : /* Stop at the rightmost boundary. */
359 : if (sb.dwSize.X - cr.X > n1)
360 : cr.X += n1;
361 : else
362 : cr.X = sb.dwSize.X;
363 : SetConsoleCursorPosition (h, cr);
364 : }
365 : break;
366 :
367 : /* ESC [ n1 'D'
368 : Move the cursor left by n1 characters. */
369 : case MAKEWORD ('[', 'D'):
370 : if (esc_head == esc_term)
371 : n1 = 1;
372 : else
373 : {
374 : n1 = strtol (esc_head, &eptr, 10);
375 : if (eptr != esc_term)
376 : break;
377 : }
378 :
379 : if (GetConsoleScreenBufferInfo (h, &sb))
380 : {
381 : cr = sb.dwCursorPosition;
382 : /* Stop at the leftmost boundary. */
383 : if (cr.X > n1)
384 : cr.X -= n1;
385 : else
386 : cr.X = 0;
387 : SetConsoleCursorPosition (h, cr);
388 : }
389 : break;
390 :
391 : /* ESC [ n1 'E'
392 : Move the cursor to the beginning of the n1-th line downwards. */
393 : case MAKEWORD ('[', 'E'):
394 : if (esc_head == esc_term)
395 : n1 = 1;
396 : else
397 : {
398 : n1 = strtol (esc_head, &eptr, 10);
399 : if (eptr != esc_term)
400 : break;
401 : }
402 :
403 : if (GetConsoleScreenBufferInfo (h, &sb))
404 : {
405 : cr = sb.dwCursorPosition;
406 : cr.X = 0;
407 : /* Stop at the bottommost boundary. */
408 : if (sb.dwSize.Y - cr.Y > n1)
409 : cr.Y += n1;
410 : else
411 : cr.Y = sb.dwSize.Y;
412 : SetConsoleCursorPosition (h, cr);
413 : }
414 : break;
415 :
416 : /* ESC [ n1 'F'
417 : Move the cursor to the beginning of the n1-th line upwards. */
418 : case MAKEWORD ('[', 'F'):
419 : if (esc_head == esc_term)
420 : n1 = 1;
421 : else
422 : {
423 : n1 = strtol (esc_head, &eptr, 10);
424 : if (eptr != esc_term)
425 : break;
426 : }
427 :
428 : if (GetConsoleScreenBufferInfo (h, &sb))
429 : {
430 : cr = sb.dwCursorPosition;
431 : cr.X = 0;
432 : /* Stop at the topmost boundary. */
433 : if (cr.Y > n1)
434 : cr.Y -= n1;
435 : else
436 : cr.Y = 0;
437 : SetConsoleCursorPosition (h, cr);
438 : }
439 : break;
440 :
441 : /* ESC [ n1 'G'
442 : Move the cursor to the (1-based) n1-th column. */
443 : case MAKEWORD ('[', 'G'):
444 : if (esc_head == esc_term)
445 : n1 = 1;
446 : else
447 : {
448 : n1 = strtol (esc_head, &eptr, 10);
449 : if (eptr != esc_term)
450 : break;
451 : }
452 :
453 : if (GetConsoleScreenBufferInfo (h, &sb))
454 : {
455 : cr = sb.dwCursorPosition;
456 : n1 -= 1;
457 : /* Stop at the leftmost or rightmost boundary. */
458 : if (n1 < 0)
459 : cr.X = 0;
460 : else if (n1 > sb.dwSize.X)
461 : cr.X = sb.dwSize.X;
462 : else
463 : cr.X = n1;
464 : SetConsoleCursorPosition (h, cr);
465 : }
466 : break;
467 :
468 : /* ESC [ n1 ';' n2 'H'
469 : ESC [ n1 ';' n2 'f'
470 : Move the cursor to the (1-based) n1-th row and
471 : (also 1-based) n2-th column. */
472 : case MAKEWORD ('[', 'H'):
473 : case MAKEWORD ('[', 'f'):
474 : if (esc_head == esc_term)
475 : {
476 : /* Both parameters are omitted and set to 1 by default. */
477 : n1 = 1;
478 : n2 = 1;
479 : }
480 : else if (!(delim = (const char *) memchr (esc_head, ';',
481 : esc_term - esc_head)))
482 : {
483 : /* Only the first parameter is given. The second one is
484 : set to 1 by default. */
485 : n1 = strtol (esc_head, &eptr, 10);
486 : if (eptr != esc_term)
487 : break;
488 : n2 = 1;
489 : }
490 : else
491 : {
492 : /* Both parameters are given. The first one shall be
493 : terminated by the semicolon. */
494 : n1 = strtol (esc_head, &eptr, 10);
495 : if (eptr != delim)
496 : break;
497 : n2 = strtol (delim + 1, &eptr, 10);
498 : if (eptr != esc_term)
499 : break;
500 : }
501 :
502 : if (GetConsoleScreenBufferInfo (h, &sb))
503 : {
504 : cr = sb.dwCursorPosition;
505 : n1 -= 1;
506 : n2 -= 1;
507 : /* The cursor position shall be relative to the view coord of
508 : the console window, which is usually smaller than the actual
509 : buffer. FWIW, the 'appropriate' solution will be shrinking
510 : the buffer to match the size of the console window,
511 : destroying scrollback in the process. */
512 : n1 += sb.srWindow.Top;
513 : n2 += sb.srWindow.Left;
514 : /* Stop at the topmost or bottommost boundary. */
515 : if (n1 < 0)
516 : cr.Y = 0;
517 : else if (n1 > sb.dwSize.Y)
518 : cr.Y = sb.dwSize.Y;
519 : else
520 : cr.Y = n1;
521 : /* Stop at the leftmost or rightmost boundary. */
522 : if (n2 < 0)
523 : cr.X = 0;
524 : else if (n2 > sb.dwSize.X)
525 : cr.X = sb.dwSize.X;
526 : else
527 : cr.X = n2;
528 : SetConsoleCursorPosition (h, cr);
529 : }
530 : break;
531 :
532 : /* ESC [ n1 'J'
533 : Erase display. */
534 : case MAKEWORD ('[', 'J'):
535 : if (esc_head == esc_term)
536 : /* This is one of the very few codes whose parameters have
537 : a default value of zero. */
538 : n1 = 0;
539 : else
540 : {
541 : n1 = strtol (esc_head, &eptr, 10);
542 : if (eptr != esc_term)
543 : break;
544 : }
545 :
546 : if (GetConsoleScreenBufferInfo (h, &sb))
547 : {
548 : /* The cursor is not necessarily in the console window, which
549 : makes the behavior of this code harder to define. */
550 : switch (n1)
551 : {
552 : case 0:
553 : /* If the cursor is in or above the window, erase from
554 : it to the bottom of the window; otherwise, do nothing. */
555 : cr = sb.dwCursorPosition;
556 : cnt = sb.dwSize.X - sb.dwCursorPosition.X;
557 : rows = sb.srWindow.Bottom - sb.dwCursorPosition.Y;
558 : break;
559 : case 1:
560 : /* If the cursor is in or under the window, erase from
561 : it to the top of the window; otherwise, do nothing. */
562 : cr.X = 0;
563 : cr.Y = sb.srWindow.Top;
564 : cnt = sb.dwCursorPosition.X + 1;
565 : rows = sb.dwCursorPosition.Y - sb.srWindow.Top;
566 : break;
567 : case 2:
568 : /* Erase the entire window. */
569 : cr.X = sb.srWindow.Left;
570 : cr.Y = sb.srWindow.Top;
571 : cnt = 0;
572 : rows = sb.srWindow.Bottom - sb.srWindow.Top + 1;
573 : break;
574 : default:
575 : /* Erase the entire buffer. */
576 : cr.X = 0;
577 : cr.Y = 0;
578 : cnt = 0;
579 : rows = sb.dwSize.Y;
580 : break;
581 : }
582 : if (rows < 0)
583 : break;
584 : cnt += rows * sb.dwSize.X;
585 : FillConsoleOutputCharacterW (h, L' ', cnt, cr, &step);
586 : FillConsoleOutputAttribute (h, sb.wAttributes, cnt, cr, &step);
587 : }
588 : break;
589 :
590 : /* ESC [ n1 'K'
591 : Erase line. */
592 : case MAKEWORD ('[', 'K'):
593 : if (esc_head == esc_term)
594 : /* This is one of the very few codes whose parameters have
595 : a default value of zero. */
596 : n1 = 0;
597 : else
598 : {
599 : n1 = strtol (esc_head, &eptr, 10);
600 : if (eptr != esc_term)
601 : break;
602 : }
603 :
604 : if (GetConsoleScreenBufferInfo (h, &sb))
605 : {
606 : switch (n1)
607 : {
608 : case 0:
609 : /* Erase from the cursor to the end. */
610 : cr = sb.dwCursorPosition;
611 : cnt = sb.dwSize.X - sb.dwCursorPosition.X;
612 : break;
613 : case 1:
614 : /* Erase from the cursor to the beginning. */
615 : cr = sb.dwCursorPosition;
616 : cr.X = 0;
617 : cnt = sb.dwCursorPosition.X + 1;
618 : break;
619 : default:
620 : /* Erase the entire line. */
621 : cr = sb.dwCursorPosition;
622 : cr.X = 0;
623 : cnt = sb.dwSize.X;
624 : break;
625 : }
626 : FillConsoleOutputCharacterW (h, L' ', cnt, cr, &step);
627 : FillConsoleOutputAttribute (h, sb.wAttributes, cnt, cr, &step);
628 : }
629 : break;
630 :
631 : /* ESC [ n1 ';' n2 'm'
632 : Set SGR parameters. Zero or more parameters will follow. */
633 : case MAKEWORD ('[', 'm'):
634 : attrib_add = 0;
635 : attrib_rm = 0;
636 : if (esc_head == esc_term)
637 : {
638 : /* When no parameter is given, reset the console. */
639 : attrib_add |= (FOREGROUND_RED | FOREGROUND_GREEN
640 : | FOREGROUND_BLUE);
641 : attrib_rm = -1; /* Removes everything. */
642 : goto sgr_set_it;
643 : }
644 : param = esc_head;
645 : do
646 : {
647 : /* Parse a parameter. */
648 : n1 = strtol (param, &eptr, 10);
649 : if (*eptr != ';' && eptr != esc_term)
650 : goto sgr_set_it;
651 :
652 : switch (n1)
653 : {
654 : case 0:
655 : /* Reset. */
656 : attrib_add |= (FOREGROUND_RED | FOREGROUND_GREEN
657 : | FOREGROUND_BLUE);
658 : attrib_rm = -1; /* Removes everything. */
659 : break;
660 : case 1:
661 : /* Bold. */
662 : attrib_add |= FOREGROUND_INTENSITY;
663 : break;
664 : case 4:
665 : /* Underline. */
666 : attrib_add |= COMMON_LVB_UNDERSCORE;
667 : break;
668 : case 5:
669 : /* Blink. */
670 : /* XXX: It is not BLINKING at all! */
671 : attrib_add |= BACKGROUND_INTENSITY;
672 : break;
673 : case 7:
674 : /* Reverse. */
675 : attrib_add |= COMMON_LVB_REVERSE_VIDEO;
676 : break;
677 : case 22:
678 : /* No bold. */
679 : attrib_add &= ~FOREGROUND_INTENSITY;
680 : attrib_rm |= FOREGROUND_INTENSITY;
681 : break;
682 : case 24:
683 : /* No underline. */
684 : attrib_add &= ~COMMON_LVB_UNDERSCORE;
685 : attrib_rm |= COMMON_LVB_UNDERSCORE;
686 : break;
687 : case 25:
688 : /* No blink. */
689 : /* XXX: It is not BLINKING at all! */
690 : attrib_add &= ~BACKGROUND_INTENSITY;
691 : attrib_rm |= BACKGROUND_INTENSITY;
692 : break;
693 : case 27:
694 : /* No reverse. */
695 : attrib_add &= ~COMMON_LVB_REVERSE_VIDEO;
696 : attrib_rm |= COMMON_LVB_REVERSE_VIDEO;
697 : break;
698 : case 30:
699 : case 31:
700 : case 32:
701 : case 33:
702 : case 34:
703 : case 35:
704 : case 36:
705 : case 37:
706 : /* Foreground color. */
707 : attrib_add &= ~(FOREGROUND_RED | FOREGROUND_GREEN
708 : | FOREGROUND_BLUE);
709 : n1 -= 30;
710 : if (n1 & 1)
711 : attrib_add |= FOREGROUND_RED;
712 : if (n1 & 2)
713 : attrib_add |= FOREGROUND_GREEN;
714 : if (n1 & 4)
715 : attrib_add |= FOREGROUND_BLUE;
716 : attrib_rm |= (FOREGROUND_RED | FOREGROUND_GREEN
717 : | FOREGROUND_BLUE);
718 : break;
719 : case 38:
720 : /* Reserved for extended foreground color.
721 : Don't know how to handle parameters remaining.
722 : Bail out. */
723 : goto sgr_set_it;
724 : case 39:
725 : /* Reset foreground color. */
726 : /* Set to grey. */
727 : attrib_add |= (FOREGROUND_RED | FOREGROUND_GREEN
728 : | FOREGROUND_BLUE);
729 : attrib_rm |= (FOREGROUND_RED | FOREGROUND_GREEN
730 : | FOREGROUND_BLUE);
731 : break;
732 : case 40:
733 : case 41:
734 : case 42:
735 : case 43:
736 : case 44:
737 : case 45:
738 : case 46:
739 : case 47:
740 : /* Background color. */
741 : attrib_add &= ~(BACKGROUND_RED | BACKGROUND_GREEN
742 : | BACKGROUND_BLUE);
743 : n1 -= 40;
744 : if (n1 & 1)
745 : attrib_add |= BACKGROUND_RED;
746 : if (n1 & 2)
747 : attrib_add |= BACKGROUND_GREEN;
748 : if (n1 & 4)
749 : attrib_add |= BACKGROUND_BLUE;
750 : attrib_rm |= (BACKGROUND_RED | BACKGROUND_GREEN
751 : | BACKGROUND_BLUE);
752 : break;
753 : case 48:
754 : /* Reserved for extended background color.
755 : Don't know how to handle parameters remaining.
756 : Bail out. */
757 : goto sgr_set_it;
758 : case 49:
759 : /* Reset background color. */
760 : /* Set to black. */
761 : attrib_add &= ~(BACKGROUND_RED | BACKGROUND_GREEN
762 : | BACKGROUND_BLUE);
763 : attrib_rm |= (BACKGROUND_RED | BACKGROUND_GREEN
764 : | BACKGROUND_BLUE);
765 : break;
766 : }
767 :
768 : /* Prepare the next parameter. */
769 : param = eptr + 1;
770 : }
771 : while (param != esc_term);
772 :
773 : sgr_set_it:
774 : /* 0xFFFF removes everything. If it is not the case,
775 : care must be taken to preserve old attributes. */
776 : if (attrib_rm != 0xFFFF && GetConsoleScreenBufferInfo (h, &sb))
777 : {
778 : attrib_add |= sb.wAttributes & ~attrib_rm;
779 : }
780 : if (attrib_add & COMMON_LVB_REVERSE_VIDEO)
781 : {
782 : /* COMMON_LVB_REVERSE_VIDEO is only effective for DBCS.
783 : * Swap foreground and background colors by hand.
784 : */
785 : attrib_add = (attrib_add & 0xFF00)
786 : | ((attrib_add & 0x00F0) >> 4)
787 : | ((attrib_add & 0x000F) << 4);
788 : attrib_add &= ~COMMON_LVB_REVERSE_VIDEO;
789 : }
790 : SetConsoleTextAttribute (h, attrib_add);
791 : break;
792 : }
793 : }
794 :
795 : int
796 : mingw_ansi_fputs (const char *str, FILE *fp)
797 : {
798 : const char *read = str;
799 : HANDLE h;
800 : DWORD mode;
801 : int esc_code, prefix_len;
802 : const char *esc_head, *esc_term;
803 :
804 : h = (HANDLE) _get_osfhandle (_fileno (fp));
805 : if (h == INVALID_HANDLE_VALUE)
806 : return EOF;
807 :
808 : /* Don't mess up stdio functions with Windows APIs. */
809 : fflush (fp);
810 :
811 : if (GetConsoleMode (h, &mode)
812 : #ifdef ENABLE_VIRTUAL_TERMINAL_PROCESSING
813 : && !(mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING)
814 : #endif
815 : )
816 : /* If it is a console, and doesn't support ANSI escape codes, translate
817 : them as needed. */
818 : for (;;)
819 : {
820 : if ((esc_code = find_esc_head (&prefix_len, &esc_head, read)) == 0)
821 : {
822 : /* Write all remaining characters, then exit. */
823 : write_all (h, read, strlen (read));
824 : break;
825 : }
826 : if (find_esc_terminator (&esc_term, esc_head) == 0)
827 : /* Ignore incomplete escape sequences at the moment.
828 : FIXME: The escape state shall be cached for further calls
829 : to this function. */
830 : break;
831 : write_all (h, read, esc_head - prefix_len - read);
832 : eat_esc_sequence (h, esc_code, esc_head, esc_term);
833 : read = esc_term + 1;
834 : }
835 : else
836 : /* If it is not a console, write everything as-is. */
837 : write_all (h, read, strlen (read));
838 :
839 : return 1;
840 : }
841 :
842 : #endif /* __MINGW32__ */
843 :
844 : static void pp_quoted_string (pretty_printer *, const char *, size_t = -1);
845 :
846 : extern void
847 : default_token_printer (pretty_printer *pp,
848 : const pp_token_list &tokens);
849 :
850 : /* Overwrite the given location/range within this text_info's rich_location.
851 : For use e.g. when implementing "+" in client format decoders. */
852 :
853 : void
854 1284241 : text_info::set_location (unsigned int idx, location_t loc,
855 : enum range_display_kind range_display_kind)
856 : {
857 1284241 : gcc_checking_assert (m_richloc);
858 1284241 : m_richloc->set_range (idx, loc, range_display_kind);
859 1284241 : }
860 :
861 : location_t
862 113551357 : text_info::get_location (unsigned int index_of_location) const
863 : {
864 113551357 : gcc_checking_assert (m_richloc);
865 :
866 113551357 : if (index_of_location == 0)
867 113551357 : return m_richloc->get_loc ();
868 : else
869 : return UNKNOWN_LOCATION;
870 : }
871 :
872 : // Default construct an output buffer.
873 :
874 952556842 : output_buffer::output_buffer ()
875 952556842 : : m_formatted_obstack (),
876 952556842 : m_chunk_obstack (),
877 952556842 : m_obstack (&m_formatted_obstack),
878 952556842 : m_cur_formatted_chunks (nullptr),
879 952556842 : m_stream (stderr),
880 952556842 : m_line_length (),
881 952556842 : m_digit_buffer (),
882 952556842 : m_flush_p (true)
883 : {
884 952556842 : obstack_init (&m_formatted_obstack);
885 952556842 : obstack_init (&m_chunk_obstack);
886 952556842 : }
887 :
888 : // Release resources owned by an output buffer at the end of lifetime.
889 :
890 951698979 : output_buffer::~output_buffer ()
891 : {
892 951698979 : obstack_free (&m_chunk_obstack, NULL);
893 951698979 : obstack_free (&m_formatted_obstack, NULL);
894 951698979 : }
895 :
896 : /* Allocate a new pp_formatted_chunks from chunk_obstack and push
897 : it onto this buffer's stack.
898 : This represents the result of phases 1 and 2 of formatting. */
899 :
900 : pp_formatted_chunks *
901 14459271 : output_buffer::push_formatted_chunks ()
902 : {
903 : /* Allocate a new chunk structure. */
904 14459271 : pp_formatted_chunks *new_chunk_array
905 14459271 : = XOBNEW (&m_chunk_obstack, pp_formatted_chunks);
906 14459271 : new_chunk_array->m_prev = m_cur_formatted_chunks;
907 14459271 : m_cur_formatted_chunks = new_chunk_array;
908 14459271 : return new_chunk_array;
909 : }
910 :
911 : /* Deallocate the current pp_formatted_chunks structure and everything after it
912 : (i.e. the associated series of formatted strings, pp_token_lists, and
913 : pp_tokens). */
914 :
915 : void
916 14459271 : output_buffer::pop_formatted_chunks ()
917 : {
918 14459271 : pp_formatted_chunks *old_top = m_cur_formatted_chunks;
919 14459271 : gcc_assert (old_top);
920 14459271 : m_cur_formatted_chunks = old_top->m_prev;
921 14459271 : obstack_free (&m_chunk_obstack, old_top);
922 14459271 : }
923 :
924 : static const int bytes_per_hexdump_line = 16;
925 :
926 : static void
927 0 : print_hexdump_line (FILE *out, int indent,
928 : const void *buf, size_t size, size_t line_start_idx)
929 : {
930 0 : fprintf (out, "%*s%08lx: ", indent, "", (unsigned long)line_start_idx);
931 0 : for (size_t offset = 0; offset < bytes_per_hexdump_line; ++offset)
932 : {
933 0 : const size_t idx = line_start_idx + offset;
934 0 : if (idx < size)
935 0 : fprintf (out, "%02x ", ((const unsigned char *)buf)[idx]);
936 : else
937 0 : fprintf (out, " ");
938 : }
939 0 : fprintf (out, "| ");
940 0 : for (size_t offset = 0; offset < bytes_per_hexdump_line; ++offset)
941 : {
942 0 : const size_t idx = line_start_idx + offset;
943 0 : if (idx < size)
944 : {
945 0 : unsigned char ch = ((const unsigned char *)buf)[idx];
946 0 : if (!ISPRINT (ch))
947 0 : ch = '.';
948 0 : fputc (ch, out);
949 : }
950 : else
951 : break;
952 : }
953 0 : fprintf (out, "\n");
954 :
955 0 : }
956 :
957 : static void
958 0 : print_hexdump (FILE *out, int indent, const void *buf, size_t size)
959 : {
960 0 : for (size_t idx = 0; idx < size; idx += bytes_per_hexdump_line)
961 0 : print_hexdump_line (out, indent, buf, size, idx);
962 0 : }
963 :
964 : /* Dump state of this output_buffer to OUT, for debugging. */
965 :
966 : void
967 0 : output_buffer::dump (FILE *out, int indent) const
968 : {
969 0 : {
970 0 : size_t obj_size = obstack_object_size (&m_formatted_obstack);
971 0 : fprintf (out, "%*sm_formatted_obstack current object: length %li:\n",
972 : indent, "", (long)obj_size);
973 0 : print_hexdump (out, indent + 2,
974 0 : m_formatted_obstack.object_base, obj_size);
975 : }
976 0 : {
977 0 : size_t obj_size = obstack_object_size (&m_chunk_obstack);
978 0 : fprintf (out, "%*sm_chunk_obstack current object: length %li:\n",
979 : indent, "", (long)obj_size);
980 0 : print_hexdump (out, indent + 2,
981 0 : m_chunk_obstack.object_base, obj_size);
982 : }
983 :
984 0 : int depth = 0;
985 0 : for (pp_formatted_chunks *iter = m_cur_formatted_chunks;
986 0 : iter;
987 0 : iter = iter->m_prev, depth++)
988 : {
989 0 : fprintf (out, "%*spp_formatted_chunks: depth %i\n",
990 : indent, "",
991 : depth);
992 0 : iter->dump (out, indent + 2);
993 : }
994 0 : }
995 :
996 : #ifndef PTRDIFF_MAX
997 : #define PTRDIFF_MAX INTTYPE_MAXIMUM (ptrdiff_t)
998 : #endif
999 :
1000 : /* Format an integer given by va_arg (ARG, type-specifier T) where
1001 : type-specifier is a precision modifier as indicated by PREC. F is
1002 : a string used to construct the appropriate format-specifier. */
1003 : #define pp_integer_with_precision(PP, ARG, PREC, T, F) \
1004 : do \
1005 : switch (PREC) \
1006 : { \
1007 : case 0: \
1008 : pp_scalar (PP, "%" F, va_arg (ARG, T)); \
1009 : break; \
1010 : \
1011 : case 1: \
1012 : pp_scalar (PP, "%l" F, va_arg (ARG, long T)); \
1013 : break; \
1014 : \
1015 : case 2: \
1016 : pp_scalar (PP, "%" HOST_LONG_LONG_FORMAT F, \
1017 : va_arg (ARG, long long T)); \
1018 : break; \
1019 : \
1020 : case 3: \
1021 : if (T (-1) < T (0)) \
1022 : pp_scalar (PP, "%" GCC_PRISZ F, \
1023 : (fmt_size_t) va_arg (ARG, ssize_t)); \
1024 : else \
1025 : pp_scalar (PP, "%" GCC_PRISZ F, \
1026 : (fmt_size_t) va_arg (ARG, size_t)); \
1027 : break; \
1028 : \
1029 : case 4: \
1030 : if (T (-1) >= T (0)) \
1031 : { \
1032 : unsigned long long a = va_arg (ARG, ptrdiff_t); \
1033 : unsigned long long m = PTRDIFF_MAX; \
1034 : m = 2 * m + 1; \
1035 : pp_scalar (PP, "%" HOST_LONG_LONG_FORMAT F, \
1036 : a & m); \
1037 : } \
1038 : else if (sizeof (ptrdiff_t) <= sizeof (int)) \
1039 : pp_scalar (PP, "%" F, \
1040 : (int) va_arg (ARG, ptrdiff_t)); \
1041 : else if (sizeof (ptrdiff_t) <= sizeof (long)) \
1042 : pp_scalar (PP, "%l" F, \
1043 : (long int) va_arg (ARG, ptrdiff_t)); \
1044 : else \
1045 : pp_scalar (PP, "%" HOST_LONG_LONG_FORMAT F, \
1046 : (long long int) \
1047 : va_arg (ARG, ptrdiff_t)); \
1048 : break; \
1049 : \
1050 : default: \
1051 : break; \
1052 : } \
1053 : while (0)
1054 :
1055 :
1056 : /* Subroutine of pp_set_maximum_length. Set up PRETTY-PRINTER's
1057 : internal maximum characters per line. */
1058 :
1059 : void
1060 955373422 : pretty_printer::set_real_maximum_length ()
1061 : {
1062 : /* If we're told not to wrap lines then do the obvious thing. In case
1063 : we'll emit prefix only once per message, it is appropriate
1064 : not to increase unnecessarily the line-length cut-off. */
1065 955373422 : if (!pp_is_wrapping_line (this)
1066 16 : || pp_prefixing_rule (this) == DIAGNOSTICS_SHOW_PREFIX_ONCE
1067 955373430 : || pp_prefixing_rule (this) == DIAGNOSTICS_SHOW_PREFIX_NEVER)
1068 955373418 : m_maximum_length = pp_line_cutoff (this);
1069 : else
1070 : {
1071 4 : int prefix_length = m_prefix ? strlen (m_prefix) : 0;
1072 : /* If the prefix is ridiculously too long, output at least
1073 : 32 characters. */
1074 4 : if (pp_line_cutoff (this) - prefix_length < 32)
1075 4 : m_maximum_length = pp_line_cutoff (this) + 32;
1076 : else
1077 0 : m_maximum_length = pp_line_cutoff (this);
1078 : }
1079 955373422 : }
1080 :
1081 : /* Clear this pretty_printer's output state. */
1082 : inline void
1083 25971118 : pretty_printer::clear_state ()
1084 : {
1085 25971118 : m_emitted_prefix = false;
1086 25971118 : pp_indentation (this) = 0;
1087 : }
1088 :
1089 : /* Print X to PP in decimal. */
1090 : template<unsigned int N, typename T>
1091 : void
1092 1065702 : pp_wide_integer (pretty_printer *pp, const poly_int<N, T> &x)
1093 : {
1094 : if (x.is_constant ())
1095 1065702 : pp_wide_integer (pp, x.coeffs[0]);
1096 : else
1097 : {
1098 : pp_left_bracket (pp);
1099 : for (unsigned int i = 0; i < N; ++i)
1100 : {
1101 : if (i != 0)
1102 : pp_comma (pp);
1103 : pp_wide_integer (pp, x.coeffs[i]);
1104 : }
1105 : pp_right_bracket (pp);
1106 : }
1107 1065702 : }
1108 :
1109 : template void pp_wide_integer (pretty_printer *, const poly_uint16 &);
1110 : template void pp_wide_integer (pretty_printer *, const poly_int64 &);
1111 : template void pp_wide_integer (pretty_printer *, const poly_uint64 &);
1112 :
1113 : /* Flush the formatted text of PRETTY-PRINTER onto the attached stream. */
1114 : void
1115 10547840 : pp_write_text_to_stream (pretty_printer *pp)
1116 : {
1117 10547840 : const char *text = pp_formatted_text (pp);
1118 : #ifdef __MINGW32__
1119 : mingw_ansi_fputs (text, pp_buffer (pp)->m_stream);
1120 : #else
1121 10547840 : fputs (text, pp_buffer (pp)->m_stream);
1122 : #endif
1123 10547840 : pp_clear_output_area (pp);
1124 10547840 : }
1125 :
1126 : /* As pp_write_text_to_stream, but for GraphViz label output.
1127 :
1128 : Flush the formatted text of pretty-printer PP onto the attached stream.
1129 : Replace characters in PPF that have special meaning in a GraphViz .dot
1130 : file.
1131 :
1132 : This routine is not very fast, but it doesn't have to be as this is only
1133 : be used by routines dumping intermediate representations in graph form. */
1134 :
1135 : void
1136 4493 : pp_write_text_as_dot_label_to_stream (pretty_printer *pp, bool for_record)
1137 : {
1138 4493 : const char *text = pp_formatted_text (pp);
1139 4493 : const char *p = text;
1140 4493 : FILE *fp = pp_buffer (pp)->m_stream;
1141 :
1142 183173 : for (;*p; p++)
1143 : {
1144 178680 : bool escape_char;
1145 178680 : switch (*p)
1146 : {
1147 : /* Print newlines as a left-aligned newline. */
1148 6479 : case '\n':
1149 6479 : fputs ("\\l", fp);
1150 6479 : escape_char = true;
1151 6479 : break;
1152 :
1153 : /* The following characters are only special for record-shape nodes. */
1154 : case '|':
1155 : case '{':
1156 : case '}':
1157 : case '<':
1158 : case '>':
1159 : case ' ':
1160 : escape_char = for_record;
1161 : break;
1162 :
1163 : /* The following characters always have to be escaped
1164 : for use in labels. */
1165 5 : case '\\':
1166 : /* There is a bug in some (f.i. 2.36.0) versions of graphiz
1167 : ( http://www.graphviz.org/mantisbt/view.php?id=2524 ) related to
1168 : backslash as last char in label. Let's avoid triggering it. */
1169 5 : gcc_assert (*(p + 1) != '\0');
1170 : /* Fall through. */
1171 : case '"':
1172 : escape_char = true;
1173 : break;
1174 :
1175 : default:
1176 : escape_char = false;
1177 : break;
1178 : }
1179 :
1180 45911 : if (escape_char)
1181 39335 : fputc ('\\', fp);
1182 :
1183 178680 : fputc (*p, fp);
1184 : }
1185 :
1186 4493 : pp_clear_output_area (pp);
1187 4493 : }
1188 :
1189 : /* As pp_write_text_to_stream, but for GraphViz HTML-like strings.
1190 :
1191 : Flush the formatted text of pretty-printer PP onto the attached stream,
1192 : escaping these characters
1193 : " & < >
1194 : using XML escape sequences.
1195 :
1196 : http://www.graphviz.org/doc/info/lang.html#html states:
1197 : special XML escape sequences for ", &, <, and > may be necessary in
1198 : order to embed these characters in attribute values or raw text
1199 : This doesn't list "'" (which would normally be escaped in XML
1200 : as "'" or in HTML as "'");.
1201 :
1202 : Experiments show that escaping "'" doesn't seem to be necessary. */
1203 :
1204 : void
1205 1119 : pp_write_text_as_html_like_dot_to_stream (pretty_printer *pp)
1206 : {
1207 1119 : const char *text = pp_formatted_text (pp);
1208 1119 : const char *p = text;
1209 1119 : FILE *fp = pp_buffer (pp)->m_stream;
1210 :
1211 52878 : for (;*p; p++)
1212 : {
1213 51759 : switch (*p)
1214 : {
1215 48 : case '"':
1216 48 : fputs (""", fp);
1217 48 : break;
1218 0 : case '&':
1219 0 : fputs ("&", fp);
1220 0 : break;
1221 80 : case '<':
1222 80 : fputs ("<", fp);
1223 80 : break;
1224 0 : case '>':
1225 0 : fputs (">",fp);
1226 0 : break;
1227 :
1228 51631 : default:
1229 51631 : fputc (*p, fp);
1230 51631 : break;
1231 : }
1232 : }
1233 :
1234 1119 : pp_clear_output_area (pp);
1235 1119 : }
1236 :
1237 : /* Wrap a text delimited by START and END into PRETTY-PRINTER. */
1238 : static void
1239 24 : pp_wrap_text (pretty_printer *pp, const char *start, const char *end)
1240 : {
1241 24 : bool wrapping_line = pp_is_wrapping_line (pp);
1242 :
1243 216 : while (start != end)
1244 : {
1245 : /* Dump anything bordered by whitespaces. */
1246 : {
1247 : const char *p = start;
1248 840 : while (p != end && !ISBLANK (*p) && *p != '\n')
1249 648 : ++p;
1250 192 : if (wrapping_line
1251 192 : && p - start >= pp->remaining_character_count_for_line ())
1252 28 : pp_newline (pp);
1253 192 : pp_append_text (pp, start, p);
1254 192 : start = p;
1255 : }
1256 :
1257 192 : if (start != end && ISBLANK (*start))
1258 : {
1259 168 : pp_space (pp);
1260 168 : ++start;
1261 : }
1262 192 : if (start != end && *start == '\n')
1263 : {
1264 0 : pp_newline (pp);
1265 0 : ++start;
1266 : }
1267 : }
1268 24 : }
1269 :
1270 : /* Same as pp_wrap_text but wrap text only when in line-wrapping mode. */
1271 : static inline void
1272 912362981 : pp_maybe_wrap_text (pretty_printer *pp, const char *start, const char *end)
1273 : {
1274 912362981 : if (pp_is_wrapping_line (pp))
1275 24 : pp_wrap_text (pp, start, end);
1276 : else
1277 912362957 : pp_append_text (pp, start, end);
1278 912362981 : }
1279 :
1280 : /* Append to the output area of PRETTY-PRINTER a string specified by its
1281 : STARTing character and LENGTH. */
1282 : static inline void
1283 945986017 : pp_append_r (pretty_printer *pp, const char *start, int length)
1284 : {
1285 945986017 : output_buffer_append_r (pp_buffer (pp), start, length);
1286 : }
1287 :
1288 : /* Insert enough spaces into the output area of PRETTY-PRINTER to bring
1289 : the column position to the current indentation level, assuming that a
1290 : newline has just been written to the buffer. */
1291 : void
1292 33761 : pp_indent (pretty_printer *pp)
1293 : {
1294 33761 : int n = pp_indentation (pp);
1295 33761 : int i;
1296 :
1297 2042488 : for (i = 0; i < n; ++i)
1298 2008727 : pp_space (pp);
1299 33761 : }
1300 :
1301 : static const char *get_end_url_string (pretty_printer *);
1302 :
1303 : /* struct pp_token. */
1304 :
1305 51986604 : pp_token::pp_token (enum kind k)
1306 51986604 : : m_kind (k),
1307 51986604 : m_prev (nullptr),
1308 51986604 : m_next (nullptr)
1309 : {
1310 51986604 : }
1311 :
1312 : void
1313 0 : pp_token::dump (FILE *out) const
1314 : {
1315 0 : switch (m_kind)
1316 : {
1317 0 : default:
1318 0 : gcc_unreachable ();
1319 0 : case kind::text:
1320 0 : {
1321 0 : const pp_token_text *sub = as_a <const pp_token_text *> (this);
1322 0 : gcc_assert (sub->m_value.get ());
1323 0 : fprintf (out, "TEXT(\"%s\")", sub->m_value.get ());
1324 : }
1325 0 : break;
1326 0 : case kind::begin_color:
1327 0 : {
1328 0 : const pp_token_begin_color *sub
1329 0 : = as_a <const pp_token_begin_color *> (this);
1330 0 : gcc_assert (sub->m_value.get ());
1331 0 : fprintf (out, "BEGIN_COLOR(\"%s\")", sub->m_value.get ());
1332 0 : break;
1333 : }
1334 0 : case kind::end_color:
1335 0 : fprintf (out, "END_COLOR");
1336 0 : break;
1337 0 : case kind::begin_quote:
1338 0 : fprintf (out, "BEGIN_QUOTE");
1339 0 : break;
1340 0 : case kind::end_quote:
1341 0 : fprintf (out, "END_QUOTE");
1342 0 : break;
1343 0 : case kind::begin_url:
1344 0 : {
1345 0 : const pp_token_begin_url *sub
1346 0 : = as_a <const pp_token_begin_url *> (this);
1347 0 : gcc_assert (sub->m_value.get ());
1348 0 : fprintf (out, "BEGIN_URL(\"%s\")", sub->m_value.get ());
1349 : }
1350 0 : break;
1351 0 : case kind::end_url:
1352 0 : fprintf (out, "END_URL");
1353 0 : break;
1354 :
1355 0 : case kind::event_id:
1356 0 : {
1357 0 : const pp_token_event_id *sub
1358 0 : = as_a <const pp_token_event_id *> (this);
1359 0 : gcc_assert (sub->m_event_id.known_p ());
1360 0 : fprintf (out, "EVENT((%i))", sub->m_event_id.one_based ());
1361 : }
1362 0 : break;
1363 :
1364 0 : case kind::custom_data:
1365 0 : {
1366 0 : const pp_token_custom_data *sub
1367 0 : = as_a <const pp_token_custom_data *> (this);
1368 0 : gcc_assert (sub->m_value.get ());
1369 0 : fprintf (out, "CUSTOM(");
1370 0 : sub->m_value->dump (out);
1371 0 : fprintf (out, ")");
1372 : }
1373 0 : break;
1374 : }
1375 0 : }
1376 :
1377 : /* Allocate SZ bytes within S, which must not be half-way through
1378 : building another object. */
1379 :
1380 : static void *
1381 110727550 : allocate_object (size_t sz, obstack &s)
1382 : {
1383 : /* We must not be half-way through an object. */
1384 110727550 : gcc_assert (obstack_base (&s) == obstack_next_free (&s));
1385 :
1386 110727550 : obstack_blank (&s, sz);
1387 110727550 : void *buf = obstack_finish (&s);
1388 110727550 : return buf;
1389 : }
1390 :
1391 : /* Make room for a pp_token instance within obstack S. */
1392 :
1393 : void *
1394 51986604 : pp_token::operator new (size_t sz, obstack &s)
1395 : {
1396 51986604 : return allocate_object (sz, s);
1397 : }
1398 :
1399 : void
1400 51986604 : pp_token::operator delete (void *)
1401 : {
1402 : /* No-op: pp_tokens are allocated within obstacks, so
1403 : the memory will be reclaimed when the obstack is freed. */
1404 51986604 : }
1405 :
1406 : /* class pp_token_list. */
1407 :
1408 : /* Make room for a pp_token_list instance within obstack S. */
1409 :
1410 : void *
1411 53361866 : pp_token_list::operator new (size_t sz, obstack &s)
1412 : {
1413 53361866 : return allocate_object (sz, s);
1414 : }
1415 :
1416 : void
1417 53361866 : pp_token_list::operator delete (void *)
1418 : {
1419 : /* No-op: pp_token_list allocated within obstacks don't
1420 : need their own reclaim the memory will be reclaimed when
1421 : the obstack is freed. */
1422 53361866 : }
1423 :
1424 71957107 : pp_token_list::pp_token_list (obstack &s)
1425 71957107 : : m_obstack (s),
1426 71957107 : m_first (nullptr),
1427 71957107 : m_end (nullptr)
1428 : {
1429 71957107 : }
1430 :
1431 104 : pp_token_list::pp_token_list (pp_token_list &&other)
1432 104 : : m_obstack (other.m_obstack),
1433 104 : m_first (other.m_first),
1434 104 : m_end (other.m_end)
1435 : {
1436 104 : other.m_first = nullptr;
1437 104 : other.m_end = nullptr;
1438 104 : }
1439 :
1440 71957211 : pp_token_list::~pp_token_list ()
1441 : {
1442 110304849 : for (auto iter = m_first; iter; )
1443 : {
1444 38347638 : pp_token *next = iter->m_next;
1445 38347638 : delete iter;
1446 38347638 : iter = next;
1447 : }
1448 71957211 : }
1449 :
1450 : void
1451 62047821 : pp_token_list::push_back_text (label_text &&text)
1452 : {
1453 62047821 : if (text.get ()[0] == '\0')
1454 : return; // pushing empty string is a no-op
1455 45374570 : push_back<pp_token_text> (std::move (text));
1456 : }
1457 :
1458 : void
1459 0 : pp_token_list::push_back_byte (char ch)
1460 : {
1461 0 : char buf[2];
1462 0 : buf[0] = ch;
1463 0 : buf[1] = '\0';
1464 0 : push_back_text (label_text::take (xstrdup (buf)));
1465 0 : }
1466 :
1467 : void
1468 88794011 : pp_token_list::push_back (std::unique_ptr<pp_token> tok)
1469 : {
1470 88794011 : if (!m_first)
1471 : {
1472 63365582 : gcc_assert (m_end == nullptr);
1473 63365582 : m_first = tok.get ();
1474 63365582 : m_end = tok.get ();
1475 : }
1476 : else
1477 : {
1478 25428429 : gcc_assert (m_end != nullptr);
1479 25428429 : m_end->m_next = tok.get ();
1480 25428429 : tok->m_prev = m_end;
1481 25428429 : m_end = tok.get ();
1482 : }
1483 88794011 : tok.release ();
1484 88794011 : }
1485 :
1486 : void
1487 38302693 : pp_token_list::push_back_list (pp_token_list &&list)
1488 : {
1489 75148368 : while (auto tok = list.pop_front ())
1490 75148368 : push_back (std::move (tok));
1491 38302693 : }
1492 :
1493 : std::unique_ptr<pp_token>
1494 75191416 : pp_token_list::pop_front ()
1495 : {
1496 75191416 : pp_token *result = m_first;
1497 75191416 : if (result == nullptr)
1498 38302701 : return nullptr;
1499 :
1500 36888715 : gcc_assert (result->m_prev == nullptr);
1501 36888715 : m_first = result->m_next;
1502 36888715 : if (result->m_next)
1503 : {
1504 2534372 : gcc_assert (result != m_end);
1505 2534372 : m_first->m_prev = nullptr;
1506 : }
1507 : else
1508 : {
1509 34354343 : gcc_assert (result == m_end);
1510 34354343 : m_end = nullptr;
1511 : }
1512 36888715 : result->m_next = nullptr;
1513 36888715 : return std::unique_ptr<pp_token> (result);
1514 : }
1515 :
1516 : std::unique_ptr<pp_token>
1517 13595934 : pp_token_list::remove_token (pp_token *tok)
1518 : {
1519 13595934 : gcc_assert (tok);
1520 13595934 : if (tok->m_prev)
1521 : {
1522 13595934 : gcc_assert (tok != m_first);
1523 13595934 : tok->m_prev->m_next = tok->m_next;
1524 : }
1525 : else
1526 : {
1527 0 : gcc_assert (tok == m_first);
1528 0 : m_first = tok->m_next;
1529 : }
1530 13595934 : if (tok->m_next)
1531 : {
1532 9330488 : gcc_assert (tok != m_end);
1533 9330488 : tok->m_next->m_prev = tok->m_prev;
1534 : }
1535 : else
1536 : {
1537 4265446 : gcc_assert (tok == m_end);
1538 4265446 : m_end = tok->m_prev;
1539 : }
1540 13595934 : tok->m_prev = nullptr;
1541 13595934 : tok->m_next = nullptr;
1542 13595934 : gcc_assert (m_first != tok);
1543 13595934 : gcc_assert (m_end != tok);
1544 13595934 : return std::unique_ptr<pp_token> (tok);
1545 : }
1546 :
1547 : /* Insert NEW_TOK after RELATIVE_TOK. */
1548 :
1549 : void
1550 38276 : pp_token_list::insert_after (std::unique_ptr<pp_token> new_tok_up,
1551 : pp_token *relative_tok)
1552 : {
1553 38276 : pp_token *new_tok = new_tok_up.release ();
1554 :
1555 38276 : gcc_assert (new_tok);
1556 38276 : gcc_assert (new_tok->m_prev == nullptr);
1557 38276 : gcc_assert (new_tok->m_next == nullptr);
1558 38276 : gcc_assert (relative_tok);
1559 :
1560 38276 : if (relative_tok->m_next)
1561 : {
1562 38276 : gcc_assert (relative_tok != m_end);
1563 38276 : relative_tok->m_next->m_prev = new_tok;
1564 : }
1565 : else
1566 : {
1567 0 : gcc_assert (relative_tok == m_end);
1568 0 : m_end = new_tok;
1569 : }
1570 38276 : new_tok->m_prev = relative_tok;
1571 38276 : new_tok->m_next = relative_tok->m_next;
1572 38276 : relative_tok->m_next = new_tok;
1573 38276 : }
1574 :
1575 : void
1576 14459271 : pp_token_list::replace_custom_tokens ()
1577 : {
1578 14459271 : pp_token *iter = m_first;
1579 51304946 : while (iter)
1580 : {
1581 36845675 : pp_token *next = iter->m_next;
1582 36845675 : if (iter->m_kind == pp_token::kind::custom_data)
1583 : {
1584 4135906 : pp_token_list tok_list (m_obstack);
1585 4135906 : pp_token_custom_data *sub = as_a <pp_token_custom_data *> (iter);
1586 4135906 : if (sub->m_value->as_standard_tokens (tok_list))
1587 : {
1588 16 : while (auto tok = tok_list.pop_front ())
1589 : {
1590 : /* The resulting token list must not contain any
1591 : custom data. */
1592 8 : gcc_assert (tok->m_kind != pp_token::kind::custom_data);
1593 8 : insert_after (std::move (tok), iter);
1594 16 : }
1595 8 : remove_token (iter);
1596 : }
1597 4135906 : }
1598 36845675 : iter = next;
1599 : }
1600 14459271 : }
1601 :
1602 : /* Merge any runs of consecutive text tokens within this list
1603 : into individual text tokens. */
1604 :
1605 : void
1606 14459275 : pp_token_list::merge_consecutive_text_tokens ()
1607 : {
1608 14459275 : pp_token *start_of_run = m_first;
1609 37709040 : while (start_of_run)
1610 : {
1611 23249765 : if (start_of_run->m_kind != pp_token::kind::text)
1612 : {
1613 6530494 : start_of_run = start_of_run->m_next;
1614 6530494 : continue;
1615 : }
1616 : pp_token *end_of_run = start_of_run;
1617 30315197 : while (end_of_run->m_next
1618 30315197 : && end_of_run->m_next->m_kind == pp_token::kind::text)
1619 : end_of_run = end_of_run->m_next;
1620 16719271 : if (end_of_run != start_of_run)
1621 : {
1622 : /* start_of_run through end_of_run are a run of consecutive
1623 : text tokens. */
1624 :
1625 : /* Calculate size of buffer for merged text. */
1626 : size_t sz = 0;
1627 24354086 : for (auto iter = start_of_run; iter != end_of_run->m_next;
1628 18975006 : iter = iter->m_next)
1629 : {
1630 18975006 : pp_token_text *iter_text = static_cast<pp_token_text *> (iter);
1631 18975006 : sz += strlen (iter_text->m_value.get ());
1632 : }
1633 :
1634 : /* Allocate and populate buffer for merged text
1635 : (within m_obstack). */
1636 5379080 : char * const buf = (char *)allocate_object (sz + 1, m_obstack);
1637 5379080 : char *p = buf;
1638 29733166 : for (auto iter = start_of_run; iter != end_of_run->m_next;
1639 18975006 : iter = iter->m_next)
1640 : {
1641 18975006 : pp_token_text *iter_text = static_cast<pp_token_text *> (iter);
1642 18975006 : size_t iter_sz = strlen (iter_text->m_value.get ());
1643 18975006 : memcpy (p, iter_text->m_value.get (), iter_sz);
1644 18975006 : p += iter_sz;
1645 : }
1646 5379080 : *p = '\0';
1647 :
1648 : /* Replace start_of_run's buffer pointer with the new buffer. */
1649 5379080 : static_cast<pp_token_text *> (start_of_run)->m_value
1650 5379080 : = label_text::borrow (buf);
1651 :
1652 : /* Remove all the other text tokens in the run. */
1653 5379080 : pp_token * const next = end_of_run->m_next;
1654 18975006 : while (start_of_run->m_next != next)
1655 13595926 : remove_token (start_of_run->m_next);
1656 : start_of_run = next;
1657 : }
1658 : else
1659 : start_of_run = end_of_run->m_next;
1660 : }
1661 14459275 : }
1662 :
1663 : /* Apply URLIFIER to this token list.
1664 : Find BEGIN_QUOTE, TEXT, END_QUOTE triples, and if URLIFIER has a url
1665 : for the value of TEXT, then wrap TEXT in a {BEGIN,END}_URL pair. */
1666 :
1667 : void
1668 1654114 : pp_token_list::apply_urlifier (const urlifier &urlifier)
1669 : {
1670 5165708 : for (pp_token *iter = m_first; iter; )
1671 : {
1672 3511594 : if (iter->m_kind == pp_token::kind::begin_quote
1673 998410 : && iter->m_next
1674 998410 : && iter->m_next->m_kind == pp_token::kind::text
1675 990507 : && iter->m_next->m_next
1676 965524 : && iter->m_next->m_next->m_kind == pp_token::kind::end_quote)
1677 : {
1678 950240 : pp_token *begin_quote = iter;
1679 950240 : pp_token_text *text = as_a <pp_token_text *> (begin_quote->m_next);
1680 950240 : pp_token *end_quote = text->m_next;
1681 1900480 : if (char *url = urlifier.get_url_for_quoted_text
1682 950240 : (text->m_value.get (),
1683 : strlen (text->m_value.get ())))
1684 : {
1685 19134 : auto begin_url
1686 19134 : = make_token<pp_token_begin_url> (label_text::take (url));
1687 19134 : auto end_url = make_token<pp_token_end_url> ();
1688 19134 : insert_after (std::move (begin_url), begin_quote);
1689 19134 : insert_after (std::move (end_url), text);
1690 19134 : }
1691 950240 : iter = end_quote->m_next;
1692 950240 : }
1693 : else
1694 2561354 : iter = iter->m_next;
1695 : }
1696 1654114 : }
1697 :
1698 : void
1699 0 : pp_token_list::dump (FILE *out) const
1700 : {
1701 0 : for (auto iter = m_first; iter; iter = iter->m_next)
1702 : {
1703 0 : iter->dump (out);
1704 0 : if (iter->m_next)
1705 0 : fprintf (out, ", ");
1706 : }
1707 0 : fprintf (out, "]\n");
1708 0 : }
1709 :
1710 :
1711 : /* Adds a chunk to the end of formatted output, so that it
1712 : will be printed by pp_output_formatted_text. */
1713 :
1714 : void
1715 74 : pp_formatted_chunks::append_formatted_chunk (obstack &s, const char *content)
1716 : {
1717 74 : unsigned int chunk_idx;
1718 518 : for (chunk_idx = 0; m_args[chunk_idx]; chunk_idx++)
1719 : ;
1720 74 : pp_token_list *tokens = pp_token_list::make (s);
1721 74 : tokens->push_back_text (label_text::borrow (content));
1722 74 : m_args[chunk_idx++] = tokens;
1723 74 : m_args[chunk_idx] = nullptr;
1724 74 : }
1725 :
1726 : void
1727 0 : pp_formatted_chunks::dump (FILE *out, int indent) const
1728 : {
1729 0 : for (size_t idx = 0; m_args[idx]; ++idx)
1730 : {
1731 0 : diagnostics::dumping::emit_indent (out, indent);
1732 0 : fprintf (out, "%i: ", (int)idx);
1733 0 : m_args[idx]->dump (out);
1734 : }
1735 0 : }
1736 :
1737 : /* Finish any text accumulating within CUR_OBSTACK,
1738 : terminating it.
1739 : Push a text pp_token to the end of TOK_LIST containing
1740 : a borrowed copy of the text in CUR_OBSTACK. */
1741 :
1742 : static void
1743 61994241 : push_back_any_text (pp_token_list *tok_list,
1744 : obstack *cur_obstack)
1745 : {
1746 61994241 : obstack_1grow (cur_obstack, '\0');
1747 61994241 : tok_list->push_back_text
1748 61994241 : (label_text::borrow (XOBFINISH (cur_obstack,
1749 : const char *)));
1750 61994241 : }
1751 :
1752 : /* The following format specifiers are recognized as being client independent:
1753 : %d, %i: (signed) integer in base ten.
1754 : %u: unsigned integer in base ten.
1755 : %o: unsigned integer in base eight.
1756 : %x: unsigned integer in base sixteen.
1757 : %ld, %li, %lo, %lu, %lx: long versions of the above.
1758 : %lld, %lli, %llo, %llu, %llx: long long versions.
1759 : %wd, %wi, %wo, %wu, %wx: HOST_WIDE_INT versions.
1760 : %zd, %zi, %zo, %zu, %zx: size_t versions.
1761 : %td, %ti, %to, %tu, %tx: ptrdiff_t versions.
1762 : %f: double
1763 : %c: character.
1764 : %s: string.
1765 : %p: pointer (printed in a host-dependent manner).
1766 : %r: if pp_show_color(pp), switch to color identified by const char *.
1767 : %R: if pp_show_color(pp), reset color.
1768 : %m: strerror(text->err_no) - does not consume a value from args_ptr.
1769 : %%: '%'.
1770 : %<: opening quote.
1771 : %>: closing quote.
1772 : %{: URL start. Consumes a const char * argument for the URL.
1773 : %}: URL end. Does not consume any arguments.
1774 : %': apostrophe (should only be used in untranslated messages;
1775 : translations should use appropriate punctuation directly).
1776 : %@: diagnostic_event_id_ptr, for which event_id->known_p () must be true.
1777 : %.*s: a substring the length of which is specified by an argument
1778 : integer.
1779 : %.Ns: likewise, but length specified as constant in the format string.
1780 : Flag 'q': quote formatted text (must come immediately after '%').
1781 : %Z: Requires two arguments - array of int, and len. Prints elements
1782 : of the array.
1783 :
1784 : %e: Consumes a pp_element * argument.
1785 :
1786 : Arguments can be used sequentially, or through %N$ resp. *N$
1787 : notation Nth argument after the format string. If %N$ / *N$
1788 : notation is used, it must be used for all arguments, except %m, %%,
1789 : %<, %>, %} and %', which may not have a number, as they do not consume
1790 : an argument. When %M$.*N$s is used, M must be N + 1. (This may
1791 : also be written %M$.*s, provided N is not otherwise used.) The
1792 : format string must have conversion specifiers with argument numbers
1793 : 1 up to highest argument; each argument may only be used once.
1794 : A format string can have at most 30 arguments. */
1795 :
1796 : /* Implementation of pp_format.
1797 : Formatting phases 1 and 2:
1798 : - push a pp_formatted_chunks instance.
1799 : - render TEXT->format_spec plus text->m_args_ptr into the pp_formatted_chunks
1800 : instance as pp_token_lists.
1801 : Phase 3 is in pp_output_formatted_text, which pops the pp_formatted_chunks
1802 : instance. */
1803 :
1804 : static void
1805 : format_phase_1 (const text_info &text,
1806 : obstack &chunk_obstack,
1807 : pp_token_list **args,
1808 : pp_token_list ***formatters);
1809 :
1810 : static void
1811 : format_phase_2 (pretty_printer *pp,
1812 : text_info &text,
1813 : obstack &chunk_obstack,
1814 : pp_token_list ***formatters);
1815 :
1816 : void
1817 14459271 : pretty_printer::format (text_info &text)
1818 : {
1819 14459271 : pp_formatted_chunks *new_chunk_array = m_buffer->push_formatted_chunks ();
1820 14459271 : pp_token_list **args = new_chunk_array->m_args;
1821 :
1822 14459271 : pp_token_list **formatters[PP_NL_ARGMAX];
1823 14459271 : memset (formatters, 0, sizeof formatters);
1824 :
1825 : /* Formatting phase 1: split up TEXT->format_spec into chunks in
1826 : pp_buffer (PP)->args[]. Even-numbered chunks are to be output
1827 : verbatim, odd-numbered chunks are format specifiers.
1828 : %m, %%, %<, %>, %} and %' are replaced with the appropriate text at
1829 : this point. */
1830 14459271 : format_phase_1 (text, m_buffer->m_chunk_obstack, args, formatters);
1831 :
1832 : /* Note that you can debug the state of the chunk arrays here using
1833 : (gdb) call m_buffer->cur_chunk_array->dump()
1834 : which, given e.g. "foo: %s bar: %s" might print:
1835 : 0: [TEXT("foo: ")]
1836 : 1: [TEXT("s")]
1837 : 2: [TEXT(" bar: ")]
1838 : 3: [TEXT("s")]
1839 : */
1840 :
1841 : /* Set output to the argument obstack, and switch line-wrapping and
1842 : prefixing off. */
1843 14459271 : m_buffer->m_obstack = &m_buffer->m_chunk_obstack;
1844 14459271 : const int old_line_length = m_buffer->m_line_length;
1845 14459271 : const pp_wrapping_mode_t old_wrapping_mode = pp_set_verbatim_wrapping (this);
1846 :
1847 14459271 : format_phase_2 (this, text, m_buffer->m_chunk_obstack, formatters);
1848 :
1849 : /* If the client supplied a postprocessing object, call its "handle"
1850 : hook here. */
1851 14459271 : if (m_format_postprocessor)
1852 397728 : m_format_postprocessor->handle (this);
1853 :
1854 : /* Revert to normal obstack and wrapping mode. */
1855 14459271 : m_buffer->m_obstack = &m_buffer->m_formatted_obstack;
1856 14459271 : m_buffer->m_line_length = old_line_length;
1857 14459271 : pp_wrapping_mode (this) = old_wrapping_mode;
1858 14459271 : clear_state ();
1859 14459271 : }
1860 :
1861 : static void
1862 14459271 : format_phase_1 (const text_info &text,
1863 : obstack &chunk_obstack,
1864 : pp_token_list **args,
1865 : pp_token_list ***formatters)
1866 : {
1867 14459271 : unsigned chunk = 0;
1868 14459271 : unsigned int curarg = 0;
1869 14459271 : bool any_unnumbered = false, any_numbered = false;
1870 14459271 : pp_token_list *cur_token_list;
1871 14459271 : args[chunk++] = cur_token_list = pp_token_list::make (chunk_obstack);
1872 23558218 : for (const char *p = text.m_format_spec; *p; )
1873 : {
1874 271809443 : while (*p != '\0' && *p != '%')
1875 : {
1876 248307880 : obstack_1grow (&chunk_obstack, *p);
1877 248307880 : p++;
1878 : }
1879 :
1880 23501563 : if (*p == '\0')
1881 : break;
1882 :
1883 15373945 : switch (*++p)
1884 : {
1885 0 : case '\0':
1886 0 : gcc_unreachable ();
1887 :
1888 13067 : case '%':
1889 13067 : obstack_1grow (&chunk_obstack, '%');
1890 13067 : p++;
1891 13067 : continue;
1892 :
1893 130260 : case '<':
1894 130260 : {
1895 130260 : push_back_any_text (cur_token_list, &chunk_obstack);
1896 130260 : cur_token_list->push_back<pp_token_begin_quote> ();
1897 130260 : p++;
1898 130260 : continue;
1899 : }
1900 :
1901 130260 : case '>':
1902 130260 : {
1903 130260 : push_back_any_text (cur_token_list, &chunk_obstack);
1904 130260 : cur_token_list->push_back<pp_token_end_quote> ();
1905 130260 : p++;
1906 130260 : continue;
1907 : }
1908 18411 : case '\'':
1909 18411 : {
1910 18411 : push_back_any_text (cur_token_list, &chunk_obstack);
1911 18411 : cur_token_list->push_back<pp_token_end_quote> ();
1912 18411 : p++;
1913 : }
1914 18411 : continue;
1915 :
1916 36 : case '}':
1917 36 : {
1918 36 : push_back_any_text (cur_token_list, &chunk_obstack);
1919 36 : cur_token_list->push_back<pp_token_end_url> ();
1920 36 : p++;
1921 : }
1922 36 : continue;
1923 :
1924 22726 : case 'R':
1925 22726 : {
1926 22726 : push_back_any_text (cur_token_list, &chunk_obstack);
1927 22726 : cur_token_list->push_back<pp_token_end_color> ();
1928 22726 : p++;
1929 22726 : continue;
1930 : }
1931 :
1932 12 : case 'm':
1933 12 : {
1934 12 : const char *errstr = xstrerror (text.m_err_no);
1935 12 : obstack_grow (&chunk_obstack, errstr, strlen (errstr));
1936 : }
1937 12 : p++;
1938 12 : continue;
1939 :
1940 15059173 : default:
1941 : /* Handled in phase 2. Terminate the plain chunk here. */
1942 15059173 : push_back_any_text (cur_token_list, &chunk_obstack);
1943 15059173 : break;
1944 314772 : }
1945 :
1946 : /* Start a new token list for the formatting args. */
1947 15059173 : args[chunk] = cur_token_list = pp_token_list::make (chunk_obstack);
1948 :
1949 15059173 : unsigned argno;
1950 15059173 : if (ISDIGIT (*p))
1951 : {
1952 48 : char *end;
1953 48 : argno = strtoul (p, &end, 10) - 1;
1954 48 : p = end;
1955 48 : gcc_assert (*p == '$');
1956 48 : p++;
1957 :
1958 48 : any_numbered = true;
1959 48 : gcc_assert (!any_unnumbered);
1960 : }
1961 : else
1962 : {
1963 15059125 : argno = curarg++;
1964 15059125 : any_unnumbered = true;
1965 15059125 : gcc_assert (!any_numbered);
1966 : }
1967 15059173 : gcc_assert (argno < PP_NL_ARGMAX);
1968 15059173 : gcc_assert (!formatters[argno]);
1969 15059173 : formatters[argno] = &args[chunk++];
1970 16223927 : do
1971 : {
1972 16223927 : obstack_1grow (&chunk_obstack, *p);
1973 16223927 : p++;
1974 : }
1975 16223927 : while (strchr ("qwlzt+#", p[-1]));
1976 :
1977 15059173 : if (p[-1] == '.')
1978 : {
1979 : /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
1980 : (where M == N + 1). */
1981 6764 : if (ISDIGIT (*p))
1982 : {
1983 16 : do
1984 : {
1985 16 : obstack_1grow (&chunk_obstack, *p);
1986 16 : p++;
1987 : }
1988 16 : while (ISDIGIT (p[-1]));
1989 8 : gcc_assert (p[-1] == 's');
1990 : }
1991 : else
1992 : {
1993 6756 : gcc_assert (*p == '*');
1994 6756 : obstack_1grow (&chunk_obstack, '*');
1995 6756 : p++;
1996 :
1997 6756 : if (ISDIGIT (*p))
1998 : {
1999 4 : char *end;
2000 4 : unsigned int argno2 = strtoul (p, &end, 10) - 1;
2001 4 : p = end;
2002 4 : gcc_assert (argno2 == argno - 1);
2003 4 : gcc_assert (!any_unnumbered);
2004 4 : gcc_assert (*p == '$');
2005 :
2006 4 : p++;
2007 4 : formatters[argno2] = formatters[argno];
2008 : }
2009 : else
2010 : {
2011 6752 : gcc_assert (!any_numbered);
2012 6752 : formatters[argno+1] = formatters[argno];
2013 6752 : curarg++;
2014 : }
2015 6756 : gcc_assert (*p == 's');
2016 6756 : obstack_1grow (&chunk_obstack, 's');
2017 6756 : p++;
2018 : }
2019 : }
2020 15059173 : if (*p == '\0')
2021 : {
2022 6274998 : push_back_any_text (cur_token_list, &chunk_obstack);
2023 6274998 : break;
2024 : }
2025 :
2026 8784175 : obstack_1grow (&chunk_obstack, '\0');
2027 8784175 : push_back_any_text (cur_token_list, &chunk_obstack);
2028 :
2029 : /* Start a new token list for the next (non-formatted) text. */
2030 8784175 : gcc_assert (chunk < PP_NL_ARGMAX * 2);
2031 8784175 : args[chunk++] = cur_token_list = pp_token_list::make (chunk_obstack);
2032 : }
2033 :
2034 14459271 : obstack_1grow (&chunk_obstack, '\0');
2035 14459271 : push_back_any_text (cur_token_list, &chunk_obstack);
2036 14459271 : gcc_assert (chunk < PP_NL_ARGMAX * 2);
2037 14459271 : args[chunk] = nullptr;
2038 14459271 : }
2039 :
2040 : /* Second phase. Replace each formatter with pp_tokens for the formatted
2041 : text it corresponds to, consuming va_args from TEXT->m_args_ptr. */
2042 :
2043 : static void
2044 14459271 : format_phase_2 (pretty_printer *pp,
2045 : text_info &text,
2046 : obstack &chunk_obstack,
2047 : pp_token_list ***formatters)
2048 : {
2049 14459271 : unsigned argno;
2050 29518444 : for (argno = 0; formatters[argno]; argno++)
2051 : {
2052 15059173 : int precision = 0;
2053 15059173 : bool wide = false;
2054 15059173 : bool plus = false;
2055 15059173 : bool hash = false;
2056 15059173 : bool quote = false;
2057 :
2058 : /* We expect a single text token containing the formatter. */
2059 15059173 : pp_token_list *tok_list = *(formatters[argno]);
2060 15059173 : gcc_assert (tok_list);
2061 15059173 : gcc_assert (tok_list->m_first == tok_list->m_end);
2062 15059173 : gcc_assert (tok_list->m_first->m_kind == pp_token::kind::text);
2063 :
2064 : /* Accumulate the value of the formatted text into here. */
2065 15059173 : pp_token_list *formatted_tok_list
2066 15059173 : = pp_token_list::make (chunk_obstack);
2067 :
2068 : /* We do not attempt to enforce any ordering on the modifier
2069 : characters. */
2070 :
2071 15059173 : const char *p;
2072 16223927 : for (p = as_a <pp_token_text *> (tok_list->m_first)->m_value.get ();; p++)
2073 : {
2074 16223927 : switch (*p)
2075 : {
2076 1050561 : case 'q':
2077 1050561 : gcc_assert (!quote);
2078 1050561 : quote = true;
2079 1050561 : continue;
2080 :
2081 3660 : case '+':
2082 3660 : gcc_assert (!plus);
2083 3660 : plus = true;
2084 3660 : continue;
2085 :
2086 43132 : case '#':
2087 43132 : gcc_assert (!hash);
2088 43132 : hash = true;
2089 43132 : continue;
2090 :
2091 63631 : case 'w':
2092 63631 : gcc_assert (!wide);
2093 63631 : wide = true;
2094 63631 : continue;
2095 :
2096 62 : case 'z':
2097 62 : gcc_assert (!precision);
2098 62 : precision = 3;
2099 62 : continue;
2100 :
2101 27 : case 't':
2102 27 : gcc_assert (!precision);
2103 27 : precision = 4;
2104 27 : continue;
2105 :
2106 3681 : case 'l':
2107 : /* We don't support precision beyond that of "long long". */
2108 3681 : gcc_assert (precision < 2);
2109 3681 : precision++;
2110 3681 : continue;
2111 : }
2112 15059173 : break;
2113 : }
2114 :
2115 15059173 : gcc_assert (!wide || precision == 0);
2116 :
2117 15059173 : if (quote)
2118 : {
2119 1050561 : push_back_any_text (formatted_tok_list, &chunk_obstack);
2120 1050561 : formatted_tok_list->push_back<pp_token_begin_quote> ();
2121 : }
2122 :
2123 15059173 : switch (*p)
2124 : {
2125 22726 : case 'r':
2126 22726 : {
2127 22726 : const char *color = va_arg (*text.m_args_ptr, const char *);
2128 22726 : formatted_tok_list->push_back<pp_token_begin_color>
2129 22726 : (label_text::borrow (color));
2130 : }
2131 22726 : break;
2132 :
2133 3549 : case 'c':
2134 3549 : {
2135 : /* When quoting, print alphanumeric, punctuation, and the space
2136 : character unchanged, and all others in hexadecimal with the
2137 : "\x" prefix. Otherwise print them all unchanged. */
2138 3549 : char chr = (char) va_arg (*text.m_args_ptr, int);
2139 3549 : if (ISPRINT (chr) || !quote)
2140 3505 : pp_character (pp, chr);
2141 : else
2142 : {
2143 44 : const char str [2] = { chr, '\0' };
2144 44 : pp_quoted_string (pp, str, 1);
2145 : }
2146 : break;
2147 : }
2148 :
2149 1226490 : case 'd':
2150 1226490 : case 'i':
2151 1226490 : if (wide)
2152 21424 : pp_wide_integer (pp, va_arg (*text.m_args_ptr, HOST_WIDE_INT));
2153 : else
2154 1205066 : pp_integer_with_precision (pp, *text.m_args_ptr, precision,
2155 : int, "d");
2156 : break;
2157 :
2158 144 : case 'o':
2159 144 : if (wide)
2160 4 : pp_scalar (pp, "%" HOST_WIDE_INT_PRINT "o",
2161 : va_arg (*text.m_args_ptr, unsigned HOST_WIDE_INT));
2162 : else
2163 140 : pp_integer_with_precision (pp, *text.m_args_ptr, precision,
2164 : unsigned, "o");
2165 : break;
2166 :
2167 4807660 : case 's':
2168 4807660 : if (quote)
2169 756910 : pp_quoted_string (pp, va_arg (*text.m_args_ptr, const char *));
2170 : else
2171 4050750 : pp_string (pp, va_arg (*text.m_args_ptr, const char *));
2172 : break;
2173 :
2174 0 : case 'B':
2175 0 : {
2176 0 : string_slice s = *va_arg (*text.m_args_ptr, string_slice *);
2177 0 : if (quote)
2178 0 : pp_quoted_string (pp, s.begin (), s.size ());
2179 : else
2180 0 : pp_string_n (pp, s.begin (), s.size ());
2181 0 : break;
2182 : }
2183 :
2184 1460331 : case 'p':
2185 1460331 : pp_pointer (pp, va_arg (*text.m_args_ptr, void *));
2186 1460331 : break;
2187 :
2188 1651809 : case 'u':
2189 1651809 : if (wide)
2190 42137 : pp_scalar (pp, HOST_WIDE_INT_PRINT_UNSIGNED,
2191 : va_arg (*text.m_args_ptr, unsigned HOST_WIDE_INT));
2192 : else
2193 1609672 : pp_integer_with_precision (pp, *text.m_args_ptr, precision,
2194 : unsigned, "u");
2195 : break;
2196 :
2197 82050 : case 'f':
2198 82050 : pp_double (pp, va_arg (*text.m_args_ptr, double));
2199 82050 : break;
2200 :
2201 341 : case 'Z':
2202 341 : {
2203 341 : int *v = va_arg (*text.m_args_ptr, int *);
2204 341 : unsigned len = va_arg (*text.m_args_ptr, unsigned);
2205 :
2206 1041 : for (unsigned i = 0; i < len; ++i)
2207 : {
2208 359 : pp_scalar (pp, "%i", v[i]);
2209 359 : if (i < len - 1)
2210 : {
2211 18 : pp_comma (pp);
2212 18 : pp_space (pp);
2213 : }
2214 : }
2215 : break;
2216 : }
2217 :
2218 4794 : case 'x':
2219 4794 : if (wide)
2220 66 : pp_scalar (pp, HOST_WIDE_INT_PRINT_HEX,
2221 : va_arg (*text.m_args_ptr, unsigned HOST_WIDE_INT));
2222 : else
2223 4728 : pp_integer_with_precision (pp, *text.m_args_ptr, precision,
2224 : unsigned, "x");
2225 : break;
2226 :
2227 6764 : case '.':
2228 6764 : {
2229 6764 : int n;
2230 6764 : const char *s;
2231 :
2232 : /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
2233 : (where M == N + 1). The format string should be verified
2234 : already from the first phase. */
2235 6764 : p++;
2236 6764 : if (ISDIGIT (*p))
2237 : {
2238 8 : char *end;
2239 8 : n = strtoul (p, &end, 10);
2240 8 : p = end;
2241 8 : gcc_assert (*p == 's');
2242 : }
2243 : else
2244 : {
2245 6756 : gcc_assert (*p == '*');
2246 6756 : p++;
2247 6756 : gcc_assert (*p == 's');
2248 6756 : n = va_arg (*text.m_args_ptr, int);
2249 :
2250 : /* This consumes a second entry in the formatters array. */
2251 6756 : gcc_assert (formatters[argno] == formatters[argno+1]);
2252 : argno++;
2253 : }
2254 :
2255 6764 : s = va_arg (*text.m_args_ptr, const char *);
2256 :
2257 : /* Append the lesser of precision and strlen (s) characters
2258 : from the array (which need not be a nul-terminated string).
2259 : Negative precision is treated as if it were omitted. */
2260 6764 : size_t len = n < 0 ? strlen (s) : strnlen (s, n);
2261 :
2262 6764 : pp_append_text (pp, s, s + len);
2263 : }
2264 6764 : break;
2265 :
2266 24889 : case '@':
2267 24889 : {
2268 : /* diagnostic_event_id_t *. */
2269 24889 : diagnostic_event_id_ptr event_id
2270 24889 : = va_arg (*text.m_args_ptr, diagnostic_event_id_ptr);
2271 24889 : gcc_assert (event_id->known_p ());
2272 24889 : formatted_tok_list->push_back<pp_token_event_id> (*event_id);
2273 : }
2274 24889 : break;
2275 :
2276 36 : case '{':
2277 36 : {
2278 36 : const char *url = va_arg (*text.m_args_ptr, const char *);
2279 36 : formatted_tok_list->push_back<pp_token_begin_url>
2280 36 : (label_text::borrow (url));
2281 : }
2282 36 : break;
2283 :
2284 27380 : case 'e':
2285 27380 : {
2286 27380 : pp_element *element = va_arg (*text.m_args_ptr, pp_element *);
2287 27380 : pp_markup::context ctxt (*pp,
2288 : quote, /* by reference */
2289 27380 : formatted_tok_list);
2290 27380 : element->add_to_phase_2 (ctxt);
2291 : }
2292 27380 : break;
2293 :
2294 5740210 : default:
2295 5740210 : {
2296 : /* Call the format decoder.
2297 : Pass the address of "quote" so that format decoders can
2298 : potentially disable printing of the closing quote
2299 : (e.g. when printing "'TYPEDEF' aka 'TYPE'" in the C family
2300 : of frontends). */
2301 5740210 : printer_fn format_decoder = pp_format_decoder (pp);
2302 5740210 : gcc_assert (format_decoder);
2303 5740210 : gcc_assert (formatted_tok_list);
2304 5740210 : bool ok = format_decoder (pp, &text, p,
2305 : precision, wide, plus, hash, "e,
2306 : *formatted_tok_list);
2307 5740210 : gcc_assert (ok);
2308 : }
2309 : }
2310 :
2311 15059173 : if (quote)
2312 : {
2313 1005197 : push_back_any_text (formatted_tok_list, &chunk_obstack);
2314 1005197 : formatted_tok_list->push_back<pp_token_end_quote> ();
2315 : }
2316 :
2317 15059173 : push_back_any_text (formatted_tok_list, &chunk_obstack);
2318 15059173 : delete *formatters[argno];
2319 15059173 : *formatters[argno] = formatted_tok_list;
2320 : }
2321 :
2322 : if (CHECKING_P)
2323 433171472 : for (; argno < PP_NL_ARGMAX; argno++)
2324 418712201 : gcc_assert (!formatters[argno]);
2325 14459271 : }
2326 :
2327 : /* Phase 3 of formatting a message (phases 1 and 2 done by pp_format).
2328 :
2329 : Pop a pp_formatted_chunks from chunk_obstack, collecting all the tokens from
2330 : phases 1 and 2 of formatting, and writing into text in formatted_obstack.
2331 :
2332 : If URLIFIER is non-null then use it on any quoted text that was not
2333 : handled in phases 1 or 2 to potentially add URLs. */
2334 :
2335 : void
2336 14459271 : pp_output_formatted_text (pretty_printer *pp,
2337 : const urlifier *urlifier)
2338 : {
2339 14459271 : output_buffer * const buffer = pp_buffer (pp);
2340 14459271 : gcc_assert (buffer->m_obstack == &buffer->m_formatted_obstack);
2341 :
2342 14459271 : pp_formatted_chunks *chunk_array = buffer->m_cur_formatted_chunks;
2343 14459271 : pp_token_list * const *token_lists = chunk_array->get_token_lists ();
2344 :
2345 14459271 : {
2346 : /* Consolidate into one token list. */
2347 14459271 : pp_token_list tokens (buffer->m_chunk_obstack);
2348 67221235 : for (unsigned chunk = 0; token_lists[chunk]; chunk++)
2349 : {
2350 38302693 : tokens.push_back_list (std::move (*token_lists[chunk]));
2351 38302693 : delete token_lists[chunk];
2352 : }
2353 :
2354 14459271 : tokens.replace_custom_tokens ();
2355 :
2356 14459271 : tokens.merge_consecutive_text_tokens ();
2357 :
2358 14459271 : if (urlifier)
2359 1654114 : tokens.apply_urlifier (*urlifier);
2360 :
2361 : /* This is a third phase, first 2 phases done in pp_format_args.
2362 : Now we actually print it. */
2363 14459271 : if (pp->m_token_printer)
2364 11938093 : pp->m_token_printer->print_tokens (pp, tokens);
2365 : else
2366 2521178 : default_token_printer (pp, tokens);
2367 :
2368 : /* Close the scope here to ensure that "tokens" above is fully cleared up
2369 : before popping the current pp_formatted_chunks, since that latter will pop
2370 : the chunk_obstack, and "tokens" may be using blocks within
2371 : the current pp_formatted_chunks's chunk_obstack level. */
2372 14459271 : }
2373 :
2374 14459271 : buffer->pop_formatted_chunks ();
2375 14459271 : }
2376 :
2377 : /* Default implementation of token printing. */
2378 :
2379 : void
2380 2521178 : default_token_printer (pretty_printer *pp,
2381 : const pp_token_list &tokens)
2382 : {
2383 : /* Convert to text, possibly with colorization, URLs, etc. */
2384 9350764 : for (auto iter = tokens.m_first; iter; iter = iter->m_next)
2385 6829586 : switch (iter->m_kind)
2386 : {
2387 0 : default:
2388 0 : gcc_unreachable ();
2389 :
2390 4469603 : case pp_token::kind::text:
2391 4469603 : {
2392 4469603 : pp_token_text *sub = as_a <pp_token_text *> (iter);
2393 4469603 : pp_string (pp, sub->m_value.get ());
2394 : }
2395 4469603 : break;
2396 :
2397 30463 : case pp_token::kind::begin_color:
2398 30463 : {
2399 30463 : pp_token_begin_color *sub = as_a <pp_token_begin_color *> (iter);
2400 30463 : pp_string (pp, colorize_start (pp_show_color (pp),
2401 : sub->m_value.get ()));
2402 : }
2403 30463 : break;
2404 30463 : case pp_token::kind::end_color:
2405 30463 : pp_string (pp, colorize_stop (pp_show_color (pp)));
2406 30463 : break;
2407 :
2408 1140798 : case pp_token::kind::begin_quote:
2409 1140798 : pp_begin_quote (pp, pp_show_color (pp));
2410 1140798 : break;
2411 1095207 : case pp_token::kind::end_quote:
2412 1095207 : pp_end_quote (pp, pp_show_color (pp));
2413 1095207 : break;
2414 :
2415 19144 : case pp_token::kind::begin_url:
2416 19144 : {
2417 19144 : pp_token_begin_url *sub = as_a <pp_token_begin_url *> (iter);
2418 19144 : pp_begin_url (pp, sub->m_value.get ());
2419 : }
2420 19144 : break;
2421 19144 : case pp_token::kind::end_url:
2422 19144 : pp_end_url (pp);
2423 19144 : break;
2424 :
2425 24764 : case pp_token::kind::event_id:
2426 24764 : {
2427 24764 : pp_token_event_id *sub = as_a <pp_token_event_id *> (iter);
2428 24764 : gcc_assert (sub->m_event_id.known_p ());
2429 24764 : pp_string (pp, colorize_start (pp_show_color (pp), "path"));
2430 24764 : pp_character (pp, '(');
2431 24764 : pp_decimal_int (pp, sub->m_event_id.one_based ());
2432 24764 : pp_character (pp, ')');
2433 24764 : pp_string (pp, colorize_stop (pp_show_color (pp)));
2434 : }
2435 24764 : break;
2436 :
2437 0 : case pp_token::kind::custom_data:
2438 : /* These should have been eliminated by replace_custom_tokens. */
2439 0 : gcc_unreachable ();
2440 6829586 : break;
2441 : }
2442 2521178 : }
2443 :
2444 : /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
2445 : settings needed by BUFFER for a verbatim formatting. */
2446 : void
2447 78243 : pp_format_verbatim (pretty_printer *pp, text_info *text)
2448 : {
2449 : /* Set verbatim mode. */
2450 78243 : pp_wrapping_mode_t oldmode = pp_set_verbatim_wrapping (pp);
2451 :
2452 : /* Do the actual formatting. */
2453 78243 : pp_format (pp, text);
2454 78243 : pp_output_formatted_text (pp);
2455 :
2456 : /* Restore previous settings. */
2457 78243 : pp_wrapping_mode (pp) = oldmode;
2458 78243 : }
2459 :
2460 : /* Flush the content of BUFFER onto the attached stream. This
2461 : function does nothing unless pp->output_buffer->flush_p. */
2462 : void
2463 11504276 : pp_flush (pretty_printer *pp)
2464 : {
2465 11504276 : pp->clear_state ();
2466 11504276 : if (!pp_buffer (pp)->m_flush_p)
2467 : return;
2468 10235086 : pp_write_text_to_stream (pp);
2469 10235086 : fflush (pp_buffer (pp)->m_stream);
2470 : }
2471 :
2472 : /* Flush the content of BUFFER onto the attached stream independently
2473 : of the value of pp->output_buffer->flush_p. */
2474 : void
2475 7571 : pp_really_flush (pretty_printer *pp)
2476 : {
2477 7571 : pp->clear_state ();
2478 7571 : pp_write_text_to_stream (pp);
2479 7571 : fflush (pp_buffer (pp)->m_stream);
2480 7571 : }
2481 :
2482 : /* Sets the number of maximum characters per line PRETTY-PRINTER can
2483 : output in line-wrapping mode. A LENGTH value 0 suppresses
2484 : line-wrapping. */
2485 : void
2486 108014 : pp_set_line_maximum_length (pretty_printer *pp, int length)
2487 : {
2488 108014 : pp_line_cutoff (pp) = length;
2489 108014 : pp->set_real_maximum_length ();
2490 108014 : }
2491 :
2492 : /* Clear PRETTY-PRINTER output area text info. */
2493 : void
2494 258112529 : pp_clear_output_area (pretty_printer *pp)
2495 : {
2496 258112529 : obstack_free (pp_buffer (pp)->m_obstack,
2497 : obstack_base (pp_buffer (pp)->m_obstack));
2498 258112529 : pp_buffer (pp)->m_line_length = 0;
2499 258112529 : }
2500 :
2501 : /* Set PREFIX for PRETTY-PRINTER, taking ownership of PREFIX, which
2502 : will eventually be free-ed. */
2503 :
2504 : void
2505 955265408 : pretty_printer::set_prefix (char *prefix)
2506 : {
2507 955265408 : free (m_prefix);
2508 955265408 : m_prefix = prefix;
2509 955265408 : set_real_maximum_length ();
2510 955265408 : m_emitted_prefix = false;
2511 955265408 : pp_indentation (this) = 0;
2512 955265408 : }
2513 :
2514 : /* Take ownership of PP's prefix, setting it to NULL.
2515 : This allows clients to save, override, and then restore an existing
2516 : prefix, without it being free-ed. */
2517 :
2518 : char *
2519 439944 : pp_take_prefix (pretty_printer *pp)
2520 : {
2521 439944 : char *result = pp->m_prefix;
2522 439944 : pp->m_prefix = nullptr;
2523 439944 : return result;
2524 : }
2525 :
2526 : /* Free PRETTY-PRINTER's prefix, a previously malloc()'d string. */
2527 : void
2528 1296003 : pp_destroy_prefix (pretty_printer *pp)
2529 : {
2530 1296003 : if (pp->m_prefix)
2531 : {
2532 1296003 : free (pp->m_prefix);
2533 1296003 : pp->m_prefix = nullptr;
2534 : }
2535 1296003 : }
2536 :
2537 : /* Write out this pretty_printer's prefix. */
2538 : void
2539 367078808 : pretty_printer::emit_prefix ()
2540 : {
2541 367078808 : if (m_prefix)
2542 : {
2543 2106991 : switch (pp_prefixing_rule (this))
2544 : {
2545 : default:
2546 : case DIAGNOSTICS_SHOW_PREFIX_NEVER:
2547 : break;
2548 :
2549 1709124 : case DIAGNOSTICS_SHOW_PREFIX_ONCE:
2550 1709124 : if (m_emitted_prefix)
2551 : {
2552 24 : pp_indent (this);
2553 24 : break;
2554 : }
2555 1709100 : pp_indentation (this) += 3;
2556 : /* Fall through. */
2557 :
2558 1729224 : case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
2559 1729224 : {
2560 1729224 : int prefix_length = strlen (m_prefix);
2561 1729224 : pp_append_r (this, m_prefix, prefix_length);
2562 1729224 : m_emitted_prefix = true;
2563 : }
2564 1729224 : break;
2565 : }
2566 : }
2567 367078808 : }
2568 :
2569 : /* Construct a PRETTY-PRINTER of MAXIMUM_LENGTH characters per line. */
2570 :
2571 951144857 : pretty_printer::pretty_printer (int maximum_length)
2572 951144857 : : m_buffer (new (XCNEW (output_buffer)) output_buffer ()),
2573 951144857 : m_prefix (nullptr),
2574 951144857 : m_padding (pp_none),
2575 951144857 : m_maximum_length (0),
2576 951144857 : m_indent_skip (0),
2577 951144857 : m_wrapping (),
2578 951144857 : m_format_decoder (nullptr),
2579 951144857 : m_format_postprocessor (nullptr),
2580 951144857 : m_token_printer (nullptr),
2581 951144857 : m_emitted_prefix (false),
2582 951144857 : m_need_newline (false),
2583 951144857 : m_translate_identifiers (true),
2584 951144857 : m_show_color (false),
2585 951144857 : m_show_highlight_colors (false),
2586 951144857 : m_url_format (URL_FORMAT_NONE),
2587 951144857 : m_skipping_null_url (false)
2588 : {
2589 951144857 : pp_line_cutoff (this) = maximum_length;
2590 : /* By default, we emit prefixes once per message. */
2591 951144857 : pp_prefixing_rule (this) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
2592 951144857 : pp_set_prefix (this, NULL);
2593 951144857 : }
2594 :
2595 : /* Copy constructor for pretty_printer. */
2596 :
2597 1311287 : pretty_printer::pretty_printer (const pretty_printer &other)
2598 1311287 : : m_buffer (new (XCNEW (output_buffer)) output_buffer ()),
2599 1311287 : m_prefix (nullptr),
2600 1311287 : m_padding (other.m_padding),
2601 1311287 : m_maximum_length (other.m_maximum_length),
2602 1311287 : m_indent_skip (other.m_indent_skip),
2603 1311287 : m_wrapping (other.m_wrapping),
2604 1311287 : m_format_decoder (other.m_format_decoder),
2605 1311287 : m_format_postprocessor (nullptr),
2606 1311287 : m_token_printer (other.m_token_printer),
2607 1311287 : m_emitted_prefix (other.m_emitted_prefix),
2608 1311287 : m_need_newline (other.m_need_newline),
2609 1311287 : m_translate_identifiers (other.m_translate_identifiers),
2610 1311287 : m_show_color (other.m_show_color),
2611 1311287 : m_show_highlight_colors (other.m_show_highlight_colors),
2612 1311287 : m_url_format (other.m_url_format),
2613 1311287 : m_skipping_null_url (false)
2614 : {
2615 1311287 : pp_line_cutoff (this) = m_maximum_length;
2616 : /* By default, we emit prefixes once per message. */
2617 1311287 : pp_prefixing_rule (this) = pp_prefixing_rule (&other);
2618 1311287 : pp_set_prefix (this, NULL);
2619 :
2620 1311287 : if (other.m_format_postprocessor)
2621 226173 : m_format_postprocessor = other.m_format_postprocessor->clone ();
2622 1311287 : }
2623 :
2624 952321914 : pretty_printer::~pretty_printer ()
2625 : {
2626 951600607 : m_buffer->~output_buffer ();
2627 951600607 : XDELETE (m_buffer);
2628 951600607 : free (m_prefix);
2629 952321914 : }
2630 :
2631 : /* Base class implementation of pretty_printer::clone vfunc. */
2632 :
2633 : std::unique_ptr<pretty_printer>
2634 822872 : pretty_printer::clone () const
2635 : {
2636 822872 : return std::make_unique<pretty_printer> (*this);
2637 : }
2638 :
2639 : /* Append a string delimited by START and END to the output area of
2640 : PRETTY-PRINTER. No line wrapping is done. However, if beginning a
2641 : new line then emit PRETTY-PRINTER's prefix and skip any leading
2642 : whitespace if appropriate. The caller must ensure that it is
2643 : safe to do so. */
2644 : void
2645 929156723 : pp_append_text (pretty_printer *pp, const char *start, const char *end)
2646 : {
2647 : /* Emit prefix and skip whitespace if we're starting a new line. */
2648 929156723 : if (pp_buffer (pp)->m_line_length == 0)
2649 : {
2650 366969544 : pp->emit_prefix ();
2651 366969544 : if (pp_is_wrapping_line (pp))
2652 52 : while (start != end && *start == ' ')
2653 0 : ++start;
2654 : }
2655 929156723 : pp_append_r (pp, start, end - start);
2656 929156723 : }
2657 :
2658 : /* Finishes constructing a NULL-terminated character string representing
2659 : the PRETTY-PRINTED text. */
2660 : const char *
2661 271557066 : pp_formatted_text (pretty_printer *pp)
2662 : {
2663 271557066 : return output_buffer_formatted_text (pp_buffer (pp));
2664 : }
2665 :
2666 : /* Return a pointer to the last character emitted in PRETTY-PRINTER's
2667 : output area. A NULL pointer means no character available. */
2668 : const char *
2669 412815835 : pp_last_position_in_text (const pretty_printer *pp)
2670 : {
2671 412815835 : return output_buffer_last_position_in_text (pp_buffer (pp));
2672 : }
2673 :
2674 : /* Return the amount of characters PRETTY-PRINTER can accept to
2675 : make a full line. Meaningful only in line-wrapping mode. */
2676 : int
2677 452 : pretty_printer::remaining_character_count_for_line ()
2678 : {
2679 452 : return m_maximum_length - pp_buffer (this)->m_line_length;
2680 : }
2681 :
2682 : /* Format a message into BUFFER a la printf. */
2683 : void
2684 745808 : pp_printf (pretty_printer *pp, const char *msg, ...)
2685 : {
2686 745808 : va_list ap;
2687 :
2688 745808 : va_start (ap, msg);
2689 745808 : text_info text (msg, &ap, errno);
2690 745808 : pp_format (pp, &text);
2691 745808 : pp_output_formatted_text (pp);
2692 745808 : va_end (ap);
2693 745808 : }
2694 :
2695 : /* Format a message into PP using ngettext to handle
2696 : singular vs plural. */
2697 :
2698 : void
2699 1677 : pp_printf_n (pretty_printer *pp,
2700 : unsigned HOST_WIDE_INT n,
2701 : const char *singular_gmsgid, const char *plural_gmsgid, ...)
2702 : {
2703 1677 : va_list ap;
2704 :
2705 1677 : va_start (ap, plural_gmsgid);
2706 :
2707 1677 : unsigned long gtn;
2708 1677 : if (sizeof n <= sizeof gtn)
2709 1677 : gtn = n;
2710 : else
2711 : /* Use the largest number ngettext can handle, otherwise
2712 : preserve the six least significant decimal digits for
2713 : languages where the plural form depends on them. */
2714 : gtn = n <= ULONG_MAX ? n : n % 1000000LU + 1000000LU;
2715 1677 : const char *msg = ngettext (singular_gmsgid, plural_gmsgid, gtn);
2716 1677 : text_info text (msg, &ap, errno);
2717 1677 : pp_format (pp, &text);
2718 1677 : pp_output_formatted_text (pp);
2719 1677 : va_end (ap);
2720 1677 : }
2721 :
2722 : /* Output MESSAGE verbatim into BUFFER. */
2723 : void
2724 78243 : pp_verbatim (pretty_printer *pp, const char *msg, ...)
2725 : {
2726 78243 : va_list ap;
2727 :
2728 78243 : va_start (ap, msg);
2729 78243 : text_info text (msg, &ap, errno);
2730 78243 : pp_format_verbatim (pp, &text);
2731 78243 : va_end (ap);
2732 78243 : }
2733 :
2734 :
2735 :
2736 : /* Have PRETTY-PRINTER start a new line. */
2737 : void
2738 9145697 : pp_newline (pretty_printer *pp)
2739 : {
2740 9145697 : obstack_1grow (pp_buffer (pp)->m_obstack, '\n');
2741 9145697 : pp_needs_newline (pp) = false;
2742 9145697 : pp_buffer (pp)->m_line_length = 0;
2743 9145697 : }
2744 :
2745 : /* Have PRETTY-PRINTER add a CHARACTER. */
2746 : void
2747 522993047 : pp_character (pretty_printer *pp, int c)
2748 : {
2749 522993047 : if (pp_is_wrapping_line (pp)
2750 : /* If printing UTF-8, don't wrap in the middle of a sequence. */
2751 284 : && (((unsigned int) c) & 0xC0) != 0x80
2752 522993307 : && pp->remaining_character_count_for_line () <= 0)
2753 : {
2754 8 : pp_newline (pp);
2755 8 : if (ISSPACE (c))
2756 : return;
2757 : }
2758 522993047 : obstack_1grow (pp_buffer (pp)->m_obstack, c);
2759 522993047 : ++pp_buffer (pp)->m_line_length;
2760 : }
2761 :
2762 : /* Append a STRING to the output area of PRETTY-PRINTER; the STRING may
2763 : be line-wrapped if in appropriate mode. */
2764 : void
2765 911604816 : pp_string (pretty_printer *pp, const char *str)
2766 : {
2767 911604816 : gcc_checking_assert (str);
2768 911604816 : pp_maybe_wrap_text (pp, str, str + strlen (str));
2769 911604816 : }
2770 :
2771 : /* As per pp_string, but only append the first LEN of STR. */
2772 :
2773 : void
2774 1131 : pp_string_n (pretty_printer *pp, const char *str, size_t len)
2775 : {
2776 1131 : gcc_checking_assert (str);
2777 1131 : pp_maybe_wrap_text (pp, str, str + len);
2778 1131 : }
2779 :
2780 : /* Append code point C to the output area of PRETTY-PRINTER, encoding it
2781 : as UTF-8. */
2782 :
2783 : void
2784 15100070 : pp_unicode_character (pretty_printer *pp, unsigned c)
2785 : {
2786 15100070 : static const uchar masks[6] = { 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
2787 15100070 : static const uchar limits[6] = { 0x80, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE };
2788 15100070 : size_t nbytes;
2789 15100070 : uchar buf[6], *p = &buf[6];
2790 :
2791 15100070 : nbytes = 1;
2792 15100070 : if (c < 0x80)
2793 14927344 : *--p = c;
2794 : else
2795 : {
2796 345462 : do
2797 : {
2798 345462 : *--p = ((c & 0x3F) | 0x80);
2799 345462 : c >>= 6;
2800 345462 : nbytes++;
2801 : }
2802 345462 : while (c >= 0x3F || (c & limits[nbytes-1]));
2803 172726 : *--p = (c | masks[nbytes-1]);
2804 : }
2805 :
2806 15100070 : pp_append_r (pp, (const char *)p, nbytes);
2807 15100070 : }
2808 :
2809 : /* Append the leading N characters of STRING to the output area of
2810 : PRETTY-PRINTER, quoting in hexadecimal non-printable characters.
2811 : Setting N = -1 is as if N were set to strlen (STRING). The STRING
2812 : may be line-wrapped if in appropriate mode. */
2813 : static void
2814 756962 : pp_quoted_string (pretty_printer *pp, const char *str, size_t n /* = -1 */)
2815 : {
2816 756962 : gcc_checking_assert (str);
2817 :
2818 756962 : const char *last = str;
2819 756962 : const char *ps;
2820 :
2821 : /* Compute the length if not specified. */
2822 756962 : if (n == (size_t) -1)
2823 756918 : n = strlen (str);
2824 :
2825 6320621 : for (ps = str; n; ++ps, --n)
2826 : {
2827 5563659 : if (ISPRINT (*ps))
2828 5563595 : continue;
2829 :
2830 : /* Don't escape a valid UTF-8 extended char. */
2831 82 : const unsigned char *ups = (const unsigned char *) ps;
2832 82 : if (*ups & 0x80)
2833 : {
2834 39 : unsigned int extended_char;
2835 39 : const int valid_utf8_len = decode_utf8_char (ups, n, &extended_char);
2836 39 : if (valid_utf8_len > 0)
2837 : {
2838 18 : ps += valid_utf8_len - 1;
2839 18 : n -= valid_utf8_len - 1;
2840 18 : continue;
2841 : }
2842 : }
2843 :
2844 64 : if (last < ps)
2845 8 : pp_maybe_wrap_text (pp, last, ps);
2846 :
2847 : /* Append the hexadecimal value of the character. Allocate a buffer
2848 : that's large enough for a 32-bit char plus the hex prefix. */
2849 64 : char buf [11];
2850 64 : int n = sprintf (buf, "\\x%02x", (unsigned char)*ps);
2851 64 : pp_maybe_wrap_text (pp, buf, buf + n);
2852 64 : last = ps + 1;
2853 : }
2854 :
2855 756962 : pp_maybe_wrap_text (pp, last, ps);
2856 756962 : }
2857 :
2858 : /* Maybe print out a whitespace if needed. */
2859 :
2860 : void
2861 985374 : pretty_printer::maybe_space ()
2862 : {
2863 985374 : if (m_padding != pp_none)
2864 : {
2865 801323 : pp_space (this);
2866 801323 : m_padding = pp_none;
2867 : }
2868 985374 : }
2869 :
2870 : // Add a newline to the pretty printer PP and flush formatted text.
2871 :
2872 : void
2873 3691347 : pp_newline_and_flush (pretty_printer *pp)
2874 : {
2875 3691347 : pp_newline (pp);
2876 3691347 : pp_flush (pp);
2877 3691347 : pp_needs_newline (pp) = false;
2878 3691347 : }
2879 :
2880 : // Add a newline to the pretty printer PP, followed by indentation.
2881 :
2882 : void
2883 8550 : pp_newline_and_indent (pretty_printer *pp, int n)
2884 : {
2885 8550 : pp_indentation (pp) += n;
2886 8550 : pp_newline (pp);
2887 8550 : pp_indent (pp);
2888 8550 : pp_needs_newline (pp) = false;
2889 8550 : }
2890 :
2891 : // Add separator C, followed by a single whitespace.
2892 :
2893 : void
2894 57876532 : pp_separate_with (pretty_printer *pp, char c)
2895 : {
2896 57876532 : pp_character (pp, c);
2897 57876532 : pp_space (pp);
2898 57876532 : }
2899 :
2900 : /* Add a localized open quote, and if SHOW_COLOR is true, begin colorizing
2901 : using the "quote" color. */
2902 :
2903 : void
2904 1200365 : pp_begin_quote (pretty_printer *pp, bool show_color)
2905 : {
2906 1200365 : pp_string (pp, open_quote);
2907 1200365 : pp_string (pp, colorize_start (show_color, "quote"));
2908 1200365 : }
2909 :
2910 : /* If SHOW_COLOR is true, stop colorizing.
2911 : Add a localized close quote. */
2912 :
2913 : void
2914 1219537 : pp_end_quote (pretty_printer *pp, bool show_color)
2915 : {
2916 1219537 : pp_string (pp, colorize_stop (show_color));
2917 1219537 : pp_string (pp, close_quote);
2918 1219537 : }
2919 :
2920 :
2921 : /* The string starting at P has LEN (at least 1) bytes left; if they
2922 : start with a valid UTF-8 sequence, return the length of that
2923 : sequence and set *VALUE to the value of that sequence, and
2924 : otherwise return 0 and set *VALUE to (unsigned int) -1. */
2925 :
2926 : static int
2927 4598851230 : decode_utf8_char (const unsigned char *p, size_t len, unsigned int *value)
2928 : {
2929 4598851230 : unsigned int t = *p;
2930 :
2931 4598851230 : if (len == 0)
2932 0 : abort ();
2933 4598851230 : if (t & 0x80)
2934 : {
2935 : size_t utf8_len = 0;
2936 : unsigned int ch;
2937 : size_t i;
2938 21624 : for (t = *p; t & 0x80; t <<= 1)
2939 16240 : utf8_len++;
2940 :
2941 5384 : if (utf8_len > len || utf8_len < 2 || utf8_len > 6)
2942 : {
2943 18 : *value = (unsigned int) -1;
2944 18 : return 0;
2945 : }
2946 5366 : ch = *p & ((1 << (7 - utf8_len)) - 1);
2947 16184 : for (i = 1; i < utf8_len; i++)
2948 : {
2949 10822 : unsigned int u = p[i];
2950 10822 : if ((u & 0xC0) != 0x80)
2951 : {
2952 4 : *value = (unsigned int) -1;
2953 4 : return 0;
2954 : }
2955 10818 : ch = (ch << 6) | (u & 0x3F);
2956 : }
2957 5362 : if ( (ch <= 0x7F && utf8_len > 1)
2958 5362 : || (ch <= 0x7FF && utf8_len > 2)
2959 5362 : || (ch <= 0xFFFF && utf8_len > 3)
2960 5362 : || (ch <= 0x1FFFFF && utf8_len > 4)
2961 5362 : || (ch <= 0x3FFFFFF && utf8_len > 5)
2962 5362 : || (ch >= 0xD800 && ch <= 0xDFFF))
2963 : {
2964 0 : *value = (unsigned int) -1;
2965 0 : return 0;
2966 : }
2967 5362 : *value = ch;
2968 5362 : return utf8_len;
2969 : }
2970 : else
2971 : {
2972 4598845846 : *value = t;
2973 4598845846 : return 1;
2974 : }
2975 : }
2976 :
2977 : /* Allocator for identifier_to_locale and corresponding function to
2978 : free memory. */
2979 :
2980 : void *(*identifier_to_locale_alloc) (size_t) = xmalloc;
2981 : void (*identifier_to_locale_free) (void *) = free;
2982 :
2983 : /* Given IDENT, an identifier in the internal encoding, return a
2984 : version of IDENT suitable for diagnostics in the locale character
2985 : set: either IDENT itself, or a string, allocated using
2986 : identifier_to_locale_alloc, converted to the locale character set
2987 : and using escape sequences if not representable in the locale
2988 : character set or containing control characters or invalid byte
2989 : sequences. Existing backslashes in IDENT are not doubled, so the
2990 : result may not uniquely specify the contents of an arbitrary byte
2991 : sequence identifier. */
2992 :
2993 : const char *
2994 621914484 : identifier_to_locale (const char *ident)
2995 : {
2996 621914484 : const unsigned char *uid = (const unsigned char *) ident;
2997 621914484 : size_t idlen = strlen (ident);
2998 621914484 : bool valid_printable_utf8 = true;
2999 621914484 : bool all_ascii = true;
3000 621914484 : size_t i;
3001 :
3002 5220758986 : for (i = 0; i < idlen;)
3003 : {
3004 4598844505 : unsigned int c;
3005 4598844505 : size_t utf8_len = decode_utf8_char (&uid[i], idlen - i, &c);
3006 4598844505 : if (utf8_len == 0 || c <= 0x1F || (c >= 0x7F && c <= 0x9F))
3007 : {
3008 3 : valid_printable_utf8 = false;
3009 3 : break;
3010 : }
3011 4598844502 : if (utf8_len > 1)
3012 2672 : all_ascii = false;
3013 4598844502 : i += utf8_len;
3014 : }
3015 :
3016 : /* If IDENT contains invalid UTF-8 sequences (which may occur with
3017 : attributes putting arbitrary byte sequences in identifiers), or
3018 : control characters, we use octal escape sequences for all bytes
3019 : outside printable ASCII. */
3020 621914484 : if (!valid_printable_utf8)
3021 : {
3022 3 : char *ret = (char *) identifier_to_locale_alloc (4 * idlen + 1);
3023 3 : char *p = ret;
3024 11 : for (i = 0; i < idlen; i++)
3025 : {
3026 5 : if (uid[i] > 0x1F && uid[i] < 0x7F)
3027 2 : *p++ = uid[i];
3028 : else
3029 : {
3030 3 : sprintf (p, "\\%03o", uid[i]);
3031 3 : p += 4;
3032 : }
3033 : }
3034 3 : *p = 0;
3035 3 : return ret;
3036 : }
3037 :
3038 : /* Otherwise, if it is valid printable ASCII, or printable UTF-8
3039 : with the locale character set being UTF-8, IDENT is used. */
3040 621914481 : if (all_ascii || locale_utf8)
3041 : return ident;
3042 :
3043 : /* Otherwise IDENT is converted to the locale character set if
3044 : possible. */
3045 : #if defined ENABLE_NLS && defined HAVE_LANGINFO_CODESET && HAVE_ICONV
3046 779 : if (locale_encoding != NULL)
3047 : {
3048 779 : iconv_t cd = iconv_open (locale_encoding, "UTF-8");
3049 779 : bool conversion_ok = true;
3050 779 : char *ret = NULL;
3051 779 : if (cd != (iconv_t) -1)
3052 : {
3053 779 : size_t ret_alloc = 4 * idlen + 1;
3054 779 : for (;;)
3055 : {
3056 : /* Repeat the whole conversion process as needed with
3057 : larger buffers so non-reversible transformations can
3058 : always be detected. */
3059 779 : ICONV_CONST char *inbuf = const_cast<char *> (ident);
3060 779 : char *outbuf;
3061 779 : size_t inbytesleft = idlen;
3062 779 : size_t outbytesleft = ret_alloc - 1;
3063 779 : size_t iconv_ret;
3064 :
3065 779 : ret = (char *) identifier_to_locale_alloc (ret_alloc);
3066 779 : outbuf = ret;
3067 :
3068 779 : if (iconv (cd, 0, 0, 0, 0) == (size_t) -1)
3069 : {
3070 : conversion_ok = false;
3071 779 : break;
3072 : }
3073 :
3074 779 : iconv_ret = iconv (cd, &inbuf, &inbytesleft,
3075 : &outbuf, &outbytesleft);
3076 779 : if (iconv_ret == (size_t) -1 || inbytesleft != 0)
3077 : {
3078 779 : if (errno == E2BIG)
3079 : {
3080 0 : ret_alloc *= 2;
3081 0 : identifier_to_locale_free (ret);
3082 0 : ret = NULL;
3083 0 : continue;
3084 : }
3085 : else
3086 : {
3087 : conversion_ok = false;
3088 : break;
3089 : }
3090 : }
3091 0 : else if (iconv_ret != 0)
3092 : {
3093 : conversion_ok = false;
3094 : break;
3095 : }
3096 : /* Return to initial shift state. */
3097 0 : if (iconv (cd, 0, 0, &outbuf, &outbytesleft) == (size_t) -1)
3098 : {
3099 0 : if (errno == E2BIG)
3100 : {
3101 0 : ret_alloc *= 2;
3102 0 : identifier_to_locale_free (ret);
3103 0 : ret = NULL;
3104 0 : continue;
3105 : }
3106 : else
3107 : {
3108 : conversion_ok = false;
3109 : break;
3110 : }
3111 : }
3112 0 : *outbuf = 0;
3113 0 : break;
3114 0 : }
3115 779 : iconv_close (cd);
3116 779 : if (conversion_ok)
3117 : return ret;
3118 : }
3119 : }
3120 : #endif
3121 :
3122 : /* Otherwise, convert non-ASCII characters in IDENT to UCNs. */
3123 779 : {
3124 779 : char *ret = (char *) identifier_to_locale_alloc (10 * idlen + 1);
3125 779 : char *p = ret;
3126 8244 : for (i = 0; i < idlen;)
3127 : {
3128 6686 : unsigned int c;
3129 6686 : size_t utf8_len = decode_utf8_char (&uid[i], idlen - i, &c);
3130 6686 : if (utf8_len == 1)
3131 4014 : *p++ = uid[i];
3132 : else
3133 : {
3134 2672 : sprintf (p, "\\U%08x", c);
3135 2672 : p += 10;
3136 : }
3137 6686 : i += utf8_len;
3138 : }
3139 779 : *p = 0;
3140 779 : return ret;
3141 : }
3142 : }
3143 :
3144 : /* Support for encoding URLs.
3145 : See egmontkob/Hyperlinks_in_Terminal_Emulators.md
3146 : ( https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda ).
3147 :
3148 : > A hyperlink is opened upon encountering an OSC 8 escape sequence with
3149 : > the target URI. The syntax is
3150 : >
3151 : > OSC 8 ; params ; URI ST
3152 : >
3153 : > A hyperlink is closed with the same escape sequence, omitting the
3154 : > parameters and the URI but keeping the separators:
3155 : >
3156 : > OSC 8 ; ; ST
3157 : >
3158 : > OSC (operating system command) is typically ESC ].
3159 :
3160 : Use BEL instead of ST, as that is currently rendered better in some
3161 : terminal emulators that don't support OSC 8, like konsole. */
3162 :
3163 : /* If URL-printing is enabled, write an "open URL" escape sequence to PP
3164 : for the given URL. */
3165 :
3166 : void
3167 19168 : pretty_printer::begin_url (const char *url)
3168 : {
3169 19168 : if (!url)
3170 : {
3171 : /* Handle null URL by skipping all output here,
3172 : and in the next pp_end_url. */
3173 12 : m_skipping_null_url = true;
3174 12 : return;
3175 : }
3176 19156 : switch (m_url_format)
3177 : {
3178 : case URL_FORMAT_NONE:
3179 : break;
3180 56 : case URL_FORMAT_ST:
3181 56 : pp_string (this, "\33]8;;");
3182 56 : pp_string (this, url);
3183 56 : pp_string (this, "\33\\");
3184 56 : break;
3185 12 : case URL_FORMAT_BEL:
3186 12 : pp_string (this, "\33]8;;");
3187 12 : pp_string (this, url);
3188 12 : pp_string (this, "\a");
3189 12 : break;
3190 0 : default:
3191 0 : gcc_unreachable ();
3192 : }
3193 : }
3194 :
3195 : /* Helper function for pp_end_url and pp_format, return the "close URL" escape
3196 : sequence string. */
3197 :
3198 : static const char *
3199 84 : get_end_url_string (pretty_printer *pp)
3200 : {
3201 84 : switch (pp->get_url_format ())
3202 : {
3203 : case URL_FORMAT_NONE:
3204 : return "";
3205 64 : case URL_FORMAT_ST:
3206 64 : return "\33]8;;\33\\";
3207 20 : case URL_FORMAT_BEL:
3208 20 : return "\33]8;;\a";
3209 0 : default:
3210 0 : gcc_unreachable ();
3211 : }
3212 : }
3213 :
3214 : /* If URL-printing is enabled, write a "close URL" escape sequence to PP. */
3215 :
3216 : void
3217 19192 : pretty_printer::end_url ()
3218 : {
3219 19192 : if (m_skipping_null_url)
3220 : {
3221 : /* We gracefully handle pp_begin_url (NULL) by omitting output for
3222 : both begin and end. Here we handle the latter. */
3223 12 : m_skipping_null_url = false;
3224 12 : return;
3225 : }
3226 19180 : if (m_url_format != URL_FORMAT_NONE)
3227 84 : pp_string (this, get_end_url_string (this));
3228 : }
3229 :
3230 : static const char *
3231 0 : get_url_format_as_string (diagnostic_url_format url_format)
3232 : {
3233 0 : switch (url_format)
3234 : {
3235 : case URL_FORMAT_NONE:
3236 : return "none";
3237 0 : case URL_FORMAT_ST:
3238 0 : return "st";
3239 0 : case URL_FORMAT_BEL:
3240 0 : return "bel";
3241 0 : default:
3242 0 : gcc_unreachable ();
3243 : }
3244 : }
3245 :
3246 : /* Dump state of this pretty_printer to OUT, for debugging. */
3247 :
3248 : void
3249 0 : pretty_printer::dump (FILE *outfile, int indent) const
3250 : {
3251 0 : namespace dumping = diagnostics::dumping;
3252 :
3253 0 : DIAGNOSTICS_DUMPING_EMIT_BOOL_FIELD (m_show_color);
3254 0 : dumping::emit_string_field
3255 0 : (outfile, indent,
3256 : "m_url_format",
3257 0 : get_url_format_as_string (m_url_format));
3258 0 : dumping::emit_heading (outfile, indent, "m_buffer");
3259 0 : if (m_buffer)
3260 0 : m_buffer->dump (outfile, indent + 2);
3261 : else
3262 0 : dumping::emit_none (outfile, indent + 2);
3263 0 : }
3264 :
3265 : /* class pp_markup::context. */
3266 :
3267 : void
3268 8418 : pp_markup::context::begin_quote ()
3269 : {
3270 8418 : gcc_assert (!m_quoted);
3271 8418 : gcc_assert (m_formatted_token_list);
3272 8418 : push_back_any_text ();
3273 8418 : m_formatted_token_list->push_back<pp_token_begin_quote> ();
3274 8418 : m_quoted = true;
3275 8418 : }
3276 :
3277 : void
3278 8418 : pp_markup::context::end_quote ()
3279 : {
3280 : /* Bail out if the quotes have already been ended, such as by
3281 : printing a type emitting "TYPEDEF' {aka `TYPE'}". */
3282 8418 : if (!m_quoted)
3283 : return;
3284 7594 : gcc_assert (m_formatted_token_list);
3285 7594 : push_back_any_text ();
3286 7594 : m_formatted_token_list->push_back<pp_token_end_quote> ();
3287 7594 : m_quoted = false;
3288 : }
3289 :
3290 : void
3291 7877 : pp_markup::context::begin_highlight_color (const char *color_name)
3292 : {
3293 7877 : if (!pp_show_highlight_colors (&m_pp))
3294 : return;
3295 :
3296 7869 : push_back_any_text ();
3297 7869 : m_formatted_token_list->push_back <pp_token_begin_color>
3298 7869 : (label_text::borrow (color_name));
3299 : }
3300 :
3301 : void
3302 7877 : pp_markup::context::end_highlight_color ()
3303 : {
3304 7877 : if (!pp_show_highlight_colors (&m_pp))
3305 : return;
3306 :
3307 7869 : push_back_any_text ();
3308 7869 : m_formatted_token_list->push_back<pp_token_end_color> ();
3309 : }
3310 :
3311 : void
3312 0 : pp_markup::context::begin_url (const char *url)
3313 : {
3314 0 : push_back_any_text ();
3315 0 : m_formatted_token_list->push_back<pp_token_begin_url>
3316 0 : (label_text::take (xstrdup (url)));
3317 0 : }
3318 :
3319 : void
3320 0 : pp_markup::context::end_url ()
3321 : {
3322 0 : push_back_any_text ();
3323 0 : m_formatted_token_list->push_back<pp_token_end_url> ();
3324 0 : }
3325 :
3326 : void
3327 0 : pp_markup::context::add_event_id (diagnostic_event_id_t event_id)
3328 : {
3329 0 : gcc_assert (event_id.known_p ());
3330 0 : push_back_any_text ();
3331 0 : m_formatted_token_list->push_back<pp_token_event_id> (event_id);
3332 0 : }
3333 :
3334 : void
3335 31766 : pp_markup::context::push_back_any_text ()
3336 : {
3337 31766 : obstack *cur_obstack = m_buf.m_obstack;
3338 31766 : obstack_1grow (cur_obstack, '\0');
3339 31766 : m_formatted_token_list->push_back_text
3340 31766 : (label_text::borrow (XOBFINISH (cur_obstack,
3341 : const char *)));
3342 31766 : }
3343 :
3344 : void
3345 199 : pp_markup::comma_separated_quoted_strings::add_to_phase_2 (context &ctxt)
3346 : {
3347 688 : for (unsigned i = 0; i < m_strings.length (); i++)
3348 : {
3349 489 : if (i > 0)
3350 294 : pp_string (&ctxt.m_pp, ", ");
3351 489 : ctxt.begin_quote ();
3352 489 : pp_string (&ctxt.m_pp, m_strings[i]);
3353 489 : ctxt.end_quote ();
3354 : }
3355 199 : }
3356 :
3357 : /* Color names for expressing "expected" vs "actual" values. */
3358 : const char *const highlight_colors::expected = "highlight-a";
3359 : const char *const highlight_colors::actual = "highlight-b";
3360 :
3361 : /* Color names for expressing "LHS" vs "RHS" values in a binary operation. */
3362 : const char *const highlight_colors::lhs = "highlight-a";
3363 : const char *const highlight_colors::rhs = "highlight-b";
3364 :
3365 : #if CHECKING_P
3366 :
3367 : namespace selftest {
3368 :
3369 : /* Smoketest for pretty_printer. */
3370 :
3371 : static void
3372 4 : test_basic_printing ()
3373 : {
3374 4 : pretty_printer pp;
3375 4 : pp_string (&pp, "hello");
3376 4 : pp_space (&pp);
3377 4 : pp_string (&pp, "world");
3378 :
3379 4 : ASSERT_STREQ ("hello world", pp_formatted_text (&pp));
3380 4 : }
3381 :
3382 : /* Helper function for testing pp_format.
3383 : Verify that pp_format (FMT, ...) followed by pp_output_formatted_text
3384 : prints EXPECTED, assuming that pp_show_color is SHOW_COLOR. */
3385 :
3386 : static void
3387 241 : assert_pp_format_va (const location &loc, const char *expected,
3388 : bool show_color, const char *fmt, va_list *ap)
3389 : {
3390 241 : pretty_printer pp;
3391 241 : rich_location rich_loc (line_table, UNKNOWN_LOCATION);
3392 :
3393 241 : text_info ti (fmt, ap, 0, nullptr, &rich_loc);
3394 :
3395 241 : pp_show_color (&pp) = show_color;
3396 241 : pp_format (&pp, &ti);
3397 241 : pp_output_formatted_text (&pp);
3398 241 : ASSERT_STREQ_AT (loc, expected, pp_formatted_text (&pp));
3399 241 : }
3400 :
3401 : /* Verify that pp_format (FMT, ...) followed by pp_output_formatted_text
3402 : prints EXPECTED, with show_color disabled. */
3403 :
3404 : static void
3405 236 : assert_pp_format (const location &loc, const char *expected,
3406 : const char *fmt, ...)
3407 : {
3408 236 : va_list ap;
3409 :
3410 236 : va_start (ap, fmt);
3411 236 : assert_pp_format_va (loc, expected, false, fmt, &ap);
3412 236 : va_end (ap);
3413 236 : }
3414 :
3415 : /* As above, but with colorization enabled. */
3416 :
3417 : static void
3418 20 : assert_pp_format_colored (const location &loc, const char *expected,
3419 : const char *fmt, ...)
3420 : {
3421 : /* The tests of colorization assume the default color scheme.
3422 : If GCC_COLORS is set, then the colors have potentially been
3423 : overridden; skip the test. */
3424 20 : if (getenv ("GCC_COLORS"))
3425 : return;
3426 :
3427 5 : va_list ap;
3428 :
3429 5 : va_start (ap, fmt);
3430 5 : assert_pp_format_va (loc, expected, true, fmt, &ap);
3431 5 : va_end (ap);
3432 : }
3433 :
3434 : /* Helper function for calling testing pp_format,
3435 : by calling assert_pp_format with various numbers of arguments.
3436 : These exist mostly to avoid having to write SELFTEST_LOCATION
3437 : throughout test_pp_format. */
3438 :
3439 : #define ASSERT_PP_FORMAT_1(EXPECTED, FMT, ARG1) \
3440 : SELFTEST_BEGIN_STMT \
3441 : assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
3442 : (ARG1)); \
3443 : SELFTEST_END_STMT
3444 :
3445 : #define ASSERT_PP_FORMAT_2(EXPECTED, FMT, ARG1, ARG2) \
3446 : SELFTEST_BEGIN_STMT \
3447 : assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
3448 : (ARG1), (ARG2)); \
3449 : SELFTEST_END_STMT
3450 :
3451 : #define ASSERT_PP_FORMAT_3(EXPECTED, FMT, ARG1, ARG2, ARG3) \
3452 : SELFTEST_BEGIN_STMT \
3453 : assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
3454 : (ARG1), (ARG2), (ARG3)); \
3455 : SELFTEST_END_STMT
3456 :
3457 : /* Verify that pp_format works, for various format codes. */
3458 :
3459 : static void
3460 4 : test_pp_format ()
3461 : {
3462 : /* Avoid introducing locale-specific differences in the results
3463 : by hardcoding open_quote and close_quote. */
3464 4 : auto_fix_quotes fix_quotes;
3465 :
3466 : /* Verify that plain text is passed through unchanged. */
3467 4 : assert_pp_format (SELFTEST_LOCATION, "unformatted", "unformatted");
3468 :
3469 : /* Verify various individual format codes, in the order listed in the
3470 : comment for pp_format above. For each code, we append a second
3471 : argument with a known bit pattern (0x12345678), to ensure that we
3472 : are consuming arguments correctly. */
3473 4 : ASSERT_PP_FORMAT_2 ("-27 12345678", "%d %x", -27, 0x12345678);
3474 4 : ASSERT_PP_FORMAT_2 ("-5 12345678", "%i %x", -5, 0x12345678);
3475 4 : ASSERT_PP_FORMAT_2 ("10 12345678", "%u %x", 10, 0x12345678);
3476 4 : ASSERT_PP_FORMAT_2 ("17 12345678", "%o %x", 15, 0x12345678);
3477 4 : ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%x %x", 0xcafebabe, 0x12345678);
3478 4 : ASSERT_PP_FORMAT_2 ("-27 12345678", "%ld %x", (long)-27, 0x12345678);
3479 4 : ASSERT_PP_FORMAT_2 ("-5 12345678", "%li %x", (long)-5, 0x12345678);
3480 4 : ASSERT_PP_FORMAT_2 ("10 12345678", "%lu %x", (long)10, 0x12345678);
3481 4 : ASSERT_PP_FORMAT_2 ("17 12345678", "%lo %x", (long)15, 0x12345678);
3482 4 : ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%lx %x", (long)0xcafebabe,
3483 : 0x12345678);
3484 4 : ASSERT_PP_FORMAT_2 ("-27 12345678", "%lld %x", (long long)-27, 0x12345678);
3485 4 : ASSERT_PP_FORMAT_2 ("-5 12345678", "%lli %x", (long long)-5, 0x12345678);
3486 4 : ASSERT_PP_FORMAT_2 ("10 12345678", "%llu %x", (long long)10, 0x12345678);
3487 4 : ASSERT_PP_FORMAT_2 ("17 12345678", "%llo %x", (long long)15, 0x12345678);
3488 4 : ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%llx %x", (long long)0xcafebabe,
3489 : 0x12345678);
3490 4 : ASSERT_PP_FORMAT_2 ("-27 12345678", "%wd %x", HOST_WIDE_INT_C (-27),
3491 : 0x12345678);
3492 4 : ASSERT_PP_FORMAT_2 ("-5 12345678", "%wi %x", HOST_WIDE_INT_C (-5),
3493 : 0x12345678);
3494 4 : ASSERT_PP_FORMAT_2 ("10 12345678", "%wu %x", HOST_WIDE_INT_UC (10),
3495 : 0x12345678);
3496 4 : ASSERT_PP_FORMAT_2 ("17 12345678", "%wo %x", HOST_WIDE_INT_C (15),
3497 : 0x12345678);
3498 4 : ASSERT_PP_FORMAT_2 ("0xcafebabe 12345678", "%wx %x",
3499 : HOST_WIDE_INT_C (0xcafebabe), 0x12345678);
3500 4 : ASSERT_PP_FORMAT_2 ("-27 12345678", "%zd %x", (ssize_t)-27, 0x12345678);
3501 4 : ASSERT_PP_FORMAT_2 ("-5 12345678", "%zi %x", (ssize_t)-5, 0x12345678);
3502 4 : ASSERT_PP_FORMAT_2 ("10 12345678", "%zu %x", (size_t)10, 0x12345678);
3503 4 : ASSERT_PP_FORMAT_2 ("17 12345678", "%zo %x", (size_t)15, 0x12345678);
3504 4 : ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%zx %x", (size_t)0xcafebabe,
3505 : 0x12345678);
3506 4 : ASSERT_PP_FORMAT_2 ("-27 12345678", "%td %x", (ptrdiff_t)-27, 0x12345678);
3507 4 : ASSERT_PP_FORMAT_2 ("-5 12345678", "%ti %x", (ptrdiff_t)-5, 0x12345678);
3508 4 : ASSERT_PP_FORMAT_2 ("10 12345678", "%tu %x", (ptrdiff_t)10, 0x12345678);
3509 4 : ASSERT_PP_FORMAT_2 ("17 12345678", "%to %x", (ptrdiff_t)15, 0x12345678);
3510 4 : ASSERT_PP_FORMAT_2 ("1afebabe 12345678", "%tx %x", (ptrdiff_t)0x1afebabe,
3511 : 0x12345678);
3512 4 : ASSERT_PP_FORMAT_2 ("1.000000 12345678", "%f %x", 1.0, 0x12345678);
3513 4 : ASSERT_PP_FORMAT_2 ("A 12345678", "%c %x", 'A', 0x12345678);
3514 4 : ASSERT_PP_FORMAT_2 ("hello world 12345678", "%s %x", "hello world",
3515 : 0x12345678);
3516 :
3517 : /* Not nul-terminated. */
3518 4 : char arr[5] = { '1', '2', '3', '4', '5' };
3519 4 : ASSERT_PP_FORMAT_3 ("123 12345678", "%.*s %x", 3, arr, 0x12345678);
3520 4 : ASSERT_PP_FORMAT_3 ("1234 12345678", "%.*s %x", -1, "1234", 0x12345678);
3521 4 : ASSERT_PP_FORMAT_3 ("12345 12345678", "%.*s %x", 7, "12345", 0x12345678);
3522 :
3523 : /* We can't test for %p; the pointer is printed in an implementation-defined
3524 : manner. */
3525 4 : ASSERT_PP_FORMAT_2 ("normal colored normal 12345678",
3526 : "normal %rcolored%R normal %x",
3527 : "error", 0x12345678);
3528 4 : assert_pp_format_colored
3529 4 : (SELFTEST_LOCATION,
3530 : "normal \33[01;31m\33[Kcolored\33[m\33[K normal 12345678",
3531 : "normal %rcolored%R normal %x", "error", 0x12345678);
3532 : /* TODO:
3533 : %m: strerror(text->err_no) - does not consume a value from args_ptr. */
3534 4 : ASSERT_PP_FORMAT_1 ("% 12345678", "%% %x", 0x12345678);
3535 4 : ASSERT_PP_FORMAT_1 ("` 12345678", "%< %x", 0x12345678);
3536 4 : ASSERT_PP_FORMAT_1 ("' 12345678", "%> %x", 0x12345678);
3537 4 : ASSERT_PP_FORMAT_1 ("' 12345678", "%' %x", 0x12345678);
3538 4 : ASSERT_PP_FORMAT_3 ("abc 12345678", "%.*s %x", 3, "abcdef", 0x12345678);
3539 4 : ASSERT_PP_FORMAT_2 ("abc 12345678", "%.3s %x", "abcdef", 0x12345678);
3540 :
3541 : /* Verify flag 'q'. */
3542 4 : ASSERT_PP_FORMAT_2 ("`foo' 12345678", "%qs %x", "foo", 0x12345678);
3543 4 : assert_pp_format_colored (SELFTEST_LOCATION,
3544 : "`\33[01m\33[Kfoo\33[m\33[K' 12345678", "%qs %x",
3545 : "foo", 0x12345678);
3546 : /* Verify "%@". */
3547 4 : {
3548 4 : diagnostics::paths::event_id_t first (2);
3549 4 : diagnostics::paths::event_id_t second (7);
3550 :
3551 4 : ASSERT_PP_FORMAT_2 ("first `free' at (3); second `free' at (8)",
3552 : "first %<free%> at %@; second %<free%> at %@",
3553 : &first, &second);
3554 4 : assert_pp_format_colored
3555 4 : (SELFTEST_LOCATION,
3556 : "first `[01m[Kfree[m[K' at [01;36m[K(3)[m[K;"
3557 : " second `[01m[Kfree[m[K' at [01;36m[K(8)[m[K",
3558 : "first %<free%> at %@; second %<free%> at %@",
3559 : &first, &second);
3560 : }
3561 :
3562 : /* Verify %Z. */
3563 4 : int v[] = { 1, 2, 3 };
3564 4 : ASSERT_PP_FORMAT_3 ("1, 2, 3 12345678", "%Z %x", v, 3, 0x12345678);
3565 :
3566 4 : int v2[] = { 0 };
3567 4 : ASSERT_PP_FORMAT_3 ("0 12345678", "%Z %x", v2, 1, 0x12345678);
3568 :
3569 : /* Verify %e. */
3570 4 : {
3571 4 : pp_element_quoted_string foo ("foo");
3572 4 : pp_element_quoted_string bar ("bar");
3573 4 : ASSERT_PP_FORMAT_2 ("before `foo' `bar' after",
3574 : "before %e %e after",
3575 : &foo, &bar);
3576 4 : }
3577 :
3578 : /* Verify that combinations work, along with unformatted text. */
3579 4 : assert_pp_format (SELFTEST_LOCATION,
3580 : "the quick brown fox jumps over the lazy dog",
3581 : "the %s %s %s jumps over the %s %s",
3582 : "quick", "brown", "fox", "lazy", "dog");
3583 4 : assert_pp_format (SELFTEST_LOCATION, "item 3 of 7", "item %i of %i", 3, 7);
3584 4 : assert_pp_format (SELFTEST_LOCATION, "problem with `bar' at line 10",
3585 : "problem with %qs at line %i", "bar", 10);
3586 :
3587 : /* Verified numbered args. */
3588 4 : assert_pp_format (SELFTEST_LOCATION,
3589 : "foo: second bar: first",
3590 : "foo: %2$s bar: %1$s",
3591 : "first", "second");
3592 4 : assert_pp_format (SELFTEST_LOCATION,
3593 : "foo: 1066 bar: 1776",
3594 : "foo: %2$i bar: %1$i",
3595 : 1776, 1066);
3596 4 : assert_pp_format (SELFTEST_LOCATION,
3597 : "foo: second bar: 1776",
3598 : "foo: %2$s bar: %1$i",
3599 : 1776, "second");
3600 4 : assert_pp_format (SELFTEST_LOCATION,
3601 : "foo: sec bar: 3360",
3602 : "foo: %3$.*2$s bar: %1$o",
3603 : 1776, 3, "second");
3604 4 : assert_pp_format (SELFTEST_LOCATION,
3605 : "foo: seco bar: 3360",
3606 : "foo: %2$.4s bar: %1$o",
3607 : 1776, "second");
3608 4 : }
3609 :
3610 : static void
3611 4 : test_merge_consecutive_text_tokens ()
3612 : {
3613 4 : auto_obstack s;
3614 4 : pp_token_list list (s);
3615 4 : list.push_back_text (label_text::borrow ("hello"));
3616 4 : list.push_back_text (label_text::borrow (" "));
3617 4 : list.push_back_text (label_text::take (xstrdup ("world")));
3618 4 : list.push_back_text (label_text::borrow ("!"));
3619 :
3620 4 : list.merge_consecutive_text_tokens ();
3621 : // We expect a single text token, with concatenated text
3622 4 : ASSERT_EQ (list.m_first, list.m_end);
3623 4 : pp_token *tok = list.m_first;
3624 4 : ASSERT_NE (tok, nullptr);
3625 4 : ASSERT_EQ (tok->m_kind, pp_token::kind::text);
3626 4 : ASSERT_STREQ (as_a <pp_token_text *> (tok)->m_value.get (), "hello world!");
3627 4 : }
3628 :
3629 : /* Verify that we can create custom tokens that can be lowered
3630 : in phase 3. */
3631 :
3632 : static void
3633 4 : test_custom_tokens_1 ()
3634 : {
3635 8 : struct custom_token_adder : public pp_element
3636 : {
3637 : public:
3638 : struct value : public pp_token_custom_data::value
3639 : {
3640 8 : value (custom_token_adder &adder)
3641 8 : : m_adder (adder)
3642 : {
3643 8 : m_adder.m_num_living_values++;
3644 : }
3645 : value (const value &other)
3646 : : m_adder (other.m_adder)
3647 : {
3648 : m_adder.m_num_living_values++;
3649 : }
3650 : value (value &&other)
3651 : : m_adder (other.m_adder)
3652 : {
3653 : m_adder.m_num_living_values++;
3654 : }
3655 : value &operator= (const value &other) = delete;
3656 : value &operator= (value &&other) = delete;
3657 8 : ~value ()
3658 : {
3659 8 : m_adder.m_num_living_values--;
3660 8 : }
3661 :
3662 0 : void dump (FILE *out) const final override
3663 : {
3664 0 : fprintf (out, "\"%s\"", m_adder.m_name);
3665 0 : }
3666 :
3667 8 : bool as_standard_tokens (pp_token_list &out) final override
3668 : {
3669 8 : ASSERT_TRUE (m_adder.m_num_living_values > 0);
3670 8 : out.push_back<pp_token_text> (label_text::borrow (m_adder.m_name));
3671 8 : return true;
3672 : }
3673 :
3674 : custom_token_adder &m_adder;
3675 : };
3676 :
3677 4 : custom_token_adder (const char *name)
3678 4 : : m_name (name),
3679 4 : m_num_living_values (0)
3680 : {
3681 : }
3682 :
3683 8 : void add_to_phase_2 (pp_markup::context &ctxt) final override
3684 : {
3685 8 : auto val_ptr = std::make_unique<value> (*this);
3686 8 : ctxt.m_formatted_token_list->push_back<pp_token_custom_data>
3687 8 : (std::move (val_ptr));
3688 8 : }
3689 :
3690 : const char *m_name;
3691 : int m_num_living_values;
3692 : };
3693 :
3694 4 : custom_token_adder e1 ("foo");
3695 4 : custom_token_adder e2 ("bar");
3696 4 : ASSERT_EQ (e1.m_num_living_values, 0);
3697 4 : ASSERT_EQ (e2.m_num_living_values, 0);
3698 :
3699 4 : pretty_printer pp;
3700 4 : pp_printf (&pp, "before %e middle %e after", &e1, &e2);
3701 :
3702 : /* Verify that instances were cleaned up. */
3703 4 : ASSERT_EQ (e1.m_num_living_values, 0);
3704 4 : ASSERT_EQ (e2.m_num_living_values, 0);
3705 :
3706 4 : ASSERT_STREQ (pp_formatted_text (&pp),
3707 : "before foo middle bar after");
3708 4 : }
3709 :
3710 : /* Verify that we can create custom tokens that aren't lowered
3711 : in phase 3, but instead are handled by a custom token_printer.
3712 : Use this to verify the inputs seen by such token_printers. */
3713 :
3714 : static void
3715 4 : test_custom_tokens_2 ()
3716 : {
3717 4 : struct custom_token_adder : public pp_element
3718 : {
3719 : struct value : public pp_token_custom_data::value
3720 : {
3721 : public:
3722 8 : value (custom_token_adder &adder)
3723 8 : : m_adder (adder)
3724 : {
3725 8 : m_adder.m_num_living_values++;
3726 : }
3727 : value (const value &other)
3728 : : m_adder (other.m_adder)
3729 : {
3730 : m_adder.m_num_living_values++;
3731 : }
3732 : value (value &&other)
3733 : : m_adder (other.m_adder)
3734 : {
3735 : m_adder.m_num_living_values++;
3736 : }
3737 : value &operator= (const value &other) = delete;
3738 : value &operator= (value &&other) = delete;
3739 8 : ~value ()
3740 : {
3741 8 : m_adder.m_num_living_values--;
3742 8 : }
3743 :
3744 0 : void dump (FILE *out) const final override
3745 : {
3746 0 : fprintf (out, "\"%s\"", m_adder.m_name);
3747 0 : }
3748 :
3749 8 : bool as_standard_tokens (pp_token_list &) final override
3750 : {
3751 8 : return false;
3752 : }
3753 :
3754 : custom_token_adder &m_adder;
3755 : };
3756 :
3757 4 : custom_token_adder (const char *name)
3758 4 : : m_name (name),
3759 4 : m_num_living_values (0)
3760 : {
3761 : }
3762 :
3763 8 : void add_to_phase_2 (pp_markup::context &ctxt) final override
3764 : {
3765 8 : auto val_ptr = std::make_unique<value> (*this);
3766 8 : ctxt.m_formatted_token_list->push_back<pp_token_custom_data>
3767 8 : (std::move (val_ptr));
3768 8 : }
3769 :
3770 : const char *m_name;
3771 : int m_num_living_values;
3772 : };
3773 :
3774 8 : class custom_token_printer : public token_printer
3775 : {
3776 4 : void print_tokens (pretty_printer *pp,
3777 : const pp_token_list &tokens) final override
3778 : {
3779 : /* Verify that TOKENS has:
3780 : [TEXT("before "), CUSTOM("foo"), TEXT(" middle "), CUSTOM("bar"),
3781 : TEXT(" after")] */
3782 4 : pp_token *tok_0 = tokens.m_first;
3783 4 : ASSERT_NE (tok_0, nullptr);
3784 4 : ASSERT_EQ (tok_0->m_kind, pp_token::kind::text);
3785 4 : ASSERT_STREQ (as_a<pp_token_text *> (tok_0)->m_value.get (),
3786 : "before ");
3787 :
3788 4 : pp_token *tok_1 = tok_0->m_next;
3789 4 : ASSERT_NE (tok_1, nullptr);
3790 4 : ASSERT_EQ (tok_1->m_prev, tok_0);
3791 4 : ASSERT_EQ (tok_1->m_kind, pp_token::kind::custom_data);
3792 :
3793 4 : custom_token_adder::value *v1
3794 : = static_cast <custom_token_adder::value *>
3795 4 : (as_a<pp_token_custom_data *> (tok_1)->m_value.get ());
3796 4 : ASSERT_STREQ (v1->m_adder.m_name, "foo");
3797 4 : ASSERT_TRUE (v1->m_adder.m_num_living_values > 0);
3798 :
3799 4 : pp_token *tok_2 = tok_1->m_next;
3800 4 : ASSERT_NE (tok_2, nullptr);
3801 4 : ASSERT_EQ (tok_2->m_prev, tok_1);
3802 4 : ASSERT_EQ (tok_2->m_kind, pp_token::kind::text);
3803 4 : ASSERT_STREQ (as_a<pp_token_text *> (tok_2)->m_value.get (),
3804 : " middle ");
3805 :
3806 4 : pp_token *tok_3 = tok_2->m_next;
3807 4 : ASSERT_NE (tok_3, nullptr);
3808 4 : ASSERT_EQ (tok_3->m_prev, tok_2);
3809 4 : ASSERT_EQ (tok_3->m_kind, pp_token::kind::custom_data);
3810 4 : custom_token_adder::value *v3
3811 : = static_cast <custom_token_adder::value *>
3812 4 : (as_a<pp_token_custom_data *> (tok_3)->m_value.get ());
3813 4 : ASSERT_STREQ (v3->m_adder.m_name, "bar");
3814 4 : ASSERT_TRUE (v3->m_adder.m_num_living_values > 0);
3815 :
3816 4 : pp_token *tok_4 = tok_3->m_next;
3817 4 : ASSERT_NE (tok_4, nullptr);
3818 4 : ASSERT_EQ (tok_4->m_prev, tok_3);
3819 4 : ASSERT_EQ (tok_4->m_kind, pp_token::kind::text);
3820 4 : ASSERT_STREQ (as_a<pp_token_text *> (tok_4)->m_value.get (),
3821 : " after");
3822 4 : ASSERT_EQ (tok_4->m_next, nullptr);
3823 :
3824 : /* Normally we'd loop over the tokens, printing them to PP
3825 : and handling the custom tokens.
3826 : Instead, print a message to PP to verify that we were called. */
3827 4 : pp_string (pp, "print_tokens was called");
3828 4 : }
3829 : };
3830 :
3831 4 : custom_token_adder e1 ("foo");
3832 4 : custom_token_adder e2 ("bar");
3833 4 : ASSERT_EQ (e1.m_num_living_values, 0);
3834 4 : ASSERT_EQ (e2.m_num_living_values, 0);
3835 :
3836 4 : custom_token_printer tp;
3837 4 : pretty_printer pp;
3838 4 : pp.set_token_printer (&tp);
3839 4 : pp_printf (&pp, "before %e middle %e after", &e1, &e2);
3840 :
3841 : /* Verify that instances were cleaned up. */
3842 4 : ASSERT_EQ (e1.m_num_living_values, 0);
3843 4 : ASSERT_EQ (e2.m_num_living_values, 0);
3844 :
3845 4 : ASSERT_STREQ (pp_formatted_text (&pp),
3846 : "print_tokens was called");
3847 4 : }
3848 :
3849 : /* Helper subroutine for test_pp_format_stack.
3850 : Call pp_format (phases 1 and 2), without calling phase 3. */
3851 :
3852 : static void
3853 8 : push_pp_format (pretty_printer *pp, const char *msg, ...)
3854 : {
3855 8 : va_list ap;
3856 :
3857 8 : va_start (ap, msg);
3858 8 : rich_location rich_loc (line_table, UNKNOWN_LOCATION);
3859 8 : text_info ti (msg, &ap, 0, nullptr, &rich_loc);
3860 8 : pp_format (pp, &ti);
3861 8 : va_end (ap);
3862 8 : }
3863 :
3864 : #define ASSERT_TEXT_TOKEN(TOKEN, EXPECTED_TEXT) \
3865 : SELFTEST_BEGIN_STMT \
3866 : ASSERT_NE ((TOKEN), nullptr); \
3867 : ASSERT_EQ ((TOKEN)->m_kind, pp_token::kind::text); \
3868 : ASSERT_STREQ \
3869 : (as_a <const pp_token_text *> (TOKEN)->m_value.get (), \
3870 : (EXPECTED_TEXT)); \
3871 : SELFTEST_END_STMT
3872 :
3873 :
3874 : /* Verify that the stack of pp_formatted_chunks works as expected. */
3875 :
3876 : static void
3877 4 : test_pp_format_stack ()
3878 : {
3879 4 : auto_fix_quotes fix_quotes;
3880 :
3881 4 : pretty_printer pp;
3882 4 : push_pp_format (&pp, "unexpected foo: %i bar: %qs", 42, "test");
3883 4 : push_pp_format (&pp, "In function: %qs", "test_fn");
3884 :
3885 : /* Expect the top of the stack to have:
3886 : (gdb) call top->dump()
3887 : 0: [TEXT("In function: ")]
3888 : 1: [BEGIN_QUOTE, TEXT("test_fn"), END_QUOTE]. */
3889 :
3890 4 : pp_formatted_chunks *top = pp_buffer (&pp)->m_cur_formatted_chunks;
3891 4 : ASSERT_NE (top, nullptr);
3892 4 : ASSERT_TEXT_TOKEN (top->get_token_lists ()[0]->m_first, "In function: ");
3893 4 : ASSERT_EQ (top->get_token_lists ()[1]->m_first->m_kind,
3894 : pp_token::kind::begin_quote);
3895 4 : ASSERT_EQ (top->get_token_lists ()[2], nullptr);
3896 :
3897 : /* Expect an entry in the stack below it with:
3898 : 0: [TEXT("unexpected foo: ")]
3899 : 1: [TEXT("42")]
3900 : 2: [TEXT(" bar: ")]
3901 : 3: [BEGIN_QUOTE, TEXT("test"), END_QUOTE]. */
3902 4 : pp_formatted_chunks *prev = top->get_prev ();
3903 4 : ASSERT_NE (prev, nullptr);
3904 4 : ASSERT_TEXT_TOKEN (prev->get_token_lists ()[0]->m_first, "unexpected foo: ");
3905 4 : ASSERT_TEXT_TOKEN (prev->get_token_lists ()[1]->m_first, "42");
3906 4 : ASSERT_TEXT_TOKEN (prev->get_token_lists ()[2]->m_first, " bar: ");
3907 4 : ASSERT_EQ (prev->get_token_lists ()[3]->m_first->m_kind,
3908 : pp_token::kind::begin_quote);
3909 4 : ASSERT_EQ (prev->get_token_lists ()[4], nullptr);
3910 :
3911 4 : ASSERT_EQ (prev->get_prev (), nullptr);
3912 :
3913 : /* Pop the top of the stack. */
3914 4 : pp_output_formatted_text (&pp);
3915 4 : ASSERT_EQ (pp_buffer (&pp)->m_cur_formatted_chunks, prev);
3916 4 : pp_newline (&pp);
3917 :
3918 : /* Pop the remaining entry from the stack. */
3919 4 : pp_output_formatted_text (&pp);
3920 4 : ASSERT_EQ (pp_buffer (&pp)->m_cur_formatted_chunks, nullptr);
3921 :
3922 4 : ASSERT_STREQ (pp_formatted_text (&pp),
3923 : "In function: `test_fn'\nunexpected foo: 42 bar: `test'");
3924 4 : }
3925 :
3926 : /* Verify usage of pp_printf from within a pp_element's
3927 : add_to_phase_2 vfunc. */
3928 : static void
3929 4 : test_pp_printf_within_pp_element ()
3930 : {
3931 8 : class kv_element : public pp_element
3932 : {
3933 : public:
3934 4 : kv_element (const char *key, int value)
3935 4 : : m_key (key), m_value (value)
3936 : {
3937 : }
3938 :
3939 10 : void add_to_phase_2 (pp_markup::context &ctxt) final override
3940 : {
3941 : /* We can't call pp_printf directly on ctxt.m_pp from within
3942 : formatting. As a workaround, work with a clone of the pp. */
3943 10 : std::unique_ptr<pretty_printer> pp (ctxt.m_pp.clone ());
3944 10 : pp_printf (pp.get (), "(%qs: %qi)", m_key, m_value);
3945 10 : pp_string (&ctxt.m_pp, pp_formatted_text (pp.get ()));
3946 10 : }
3947 :
3948 : private:
3949 : const char *m_key;
3950 : int m_value;
3951 : };
3952 :
3953 4 : auto_fix_quotes fix_quotes;
3954 :
3955 4 : kv_element e1 ("foo", 42);
3956 4 : kv_element e2 ("bar", 1066);
3957 4 : ASSERT_PP_FORMAT_2 ("before (`foo': `42') (`bar': `1066') after",
3958 : "before %e %e after",
3959 : &e1, &e2);
3960 4 : assert_pp_format_colored (SELFTEST_LOCATION,
3961 : ("before "
3962 : "(`\33[01m\33[Kfoo\33[m\33[K'"
3963 : ": "
3964 : "`\33[01m\33[K42\33[m\33[K')"
3965 : " "
3966 : "(`\33[01m\33[Kbar\33[m\33[K'"
3967 : ": "
3968 : "`\33[01m\33[K1066\33[m\33[K')"
3969 : " after"),
3970 : "before %e %e after",
3971 : &e1, &e2);
3972 4 : }
3973 :
3974 : /* A subclass of pretty_printer for use by test_prefixes_and_wrapping. */
3975 :
3976 24 : class test_pretty_printer : public pretty_printer
3977 : {
3978 : public:
3979 24 : test_pretty_printer (enum diagnostic_prefixing_rule_t rule,
3980 : int max_line_length)
3981 24 : {
3982 24 : pp_set_prefix (this, xstrdup ("PREFIX: "));
3983 24 : pp_prefixing_rule (this) = rule;
3984 24 : pp_set_line_maximum_length (this, max_line_length);
3985 24 : }
3986 : };
3987 :
3988 : /* Verify that the various values of enum diagnostic_prefixing_rule_t work
3989 : as expected, with and without line wrapping. */
3990 :
3991 : static void
3992 4 : test_prefixes_and_wrapping ()
3993 : {
3994 : /* Tests of the various prefixing rules, without wrapping.
3995 : Newlines embedded in pp_string don't affect it; we have to
3996 : explicitly call pp_newline. */
3997 4 : {
3998 4 : test_pretty_printer pp (DIAGNOSTICS_SHOW_PREFIX_ONCE, 0);
3999 4 : pp_string (&pp, "the quick brown fox");
4000 4 : pp_newline (&pp);
4001 4 : pp_string (&pp, "jumps over the lazy dog");
4002 4 : pp_newline (&pp);
4003 4 : ASSERT_STREQ (pp_formatted_text (&pp),
4004 : "PREFIX: the quick brown fox\n"
4005 : " jumps over the lazy dog\n");
4006 4 : }
4007 4 : {
4008 4 : test_pretty_printer pp (DIAGNOSTICS_SHOW_PREFIX_NEVER, 0);
4009 4 : pp_string (&pp, "the quick brown fox");
4010 4 : pp_newline (&pp);
4011 4 : pp_string (&pp, "jumps over the lazy dog");
4012 4 : pp_newline (&pp);
4013 4 : ASSERT_STREQ (pp_formatted_text (&pp),
4014 : "the quick brown fox\n"
4015 : "jumps over the lazy dog\n");
4016 4 : }
4017 4 : {
4018 4 : test_pretty_printer pp (DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE, 0);
4019 4 : pp_string (&pp, "the quick brown fox");
4020 4 : pp_newline (&pp);
4021 4 : pp_string (&pp, "jumps over the lazy dog");
4022 4 : pp_newline (&pp);
4023 4 : ASSERT_STREQ (pp_formatted_text (&pp),
4024 : "PREFIX: the quick brown fox\n"
4025 : "PREFIX: jumps over the lazy dog\n");
4026 4 : }
4027 :
4028 : /* Tests of the various prefixing rules, with wrapping. */
4029 4 : {
4030 4 : test_pretty_printer pp (DIAGNOSTICS_SHOW_PREFIX_ONCE, 20);
4031 4 : pp_string (&pp, "the quick brown fox jumps over the lazy dog");
4032 4 : pp_newline (&pp);
4033 4 : pp_string (&pp, "able was I ere I saw elba");
4034 4 : pp_newline (&pp);
4035 4 : ASSERT_STREQ (pp_formatted_text (&pp),
4036 : "PREFIX: the quick \n"
4037 : " brown fox jumps \n"
4038 : " over the lazy \n"
4039 : " dog\n"
4040 : " able was I ere I \n"
4041 : " saw elba\n");
4042 4 : }
4043 4 : {
4044 4 : test_pretty_printer pp (DIAGNOSTICS_SHOW_PREFIX_NEVER, 20);
4045 4 : pp_string (&pp, "the quick brown fox jumps over the lazy dog");
4046 4 : pp_newline (&pp);
4047 4 : pp_string (&pp, "able was I ere I saw elba");
4048 4 : pp_newline (&pp);
4049 4 : ASSERT_STREQ (pp_formatted_text (&pp),
4050 : "the quick brown fox \n"
4051 : "jumps over the lazy \n"
4052 : "dog\n"
4053 : "able was I ere I \n"
4054 : "saw elba\n");
4055 4 : }
4056 4 : {
4057 4 : test_pretty_printer pp (DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE, 20);
4058 4 : pp_string (&pp, "the quick brown fox jumps over the lazy dog");
4059 4 : pp_newline (&pp);
4060 4 : pp_string (&pp, "able was I ere I saw elba");
4061 4 : pp_newline (&pp);
4062 4 : ASSERT_STREQ (pp_formatted_text (&pp),
4063 : "PREFIX: the quick brown fox jumps over the lazy dog\n"
4064 : "PREFIX: able was I ere I saw elba\n");
4065 4 : }
4066 :
4067 4 : }
4068 :
4069 : /* Verify that URL-printing works as expected. */
4070 :
4071 : static void
4072 4 : test_urls ()
4073 : {
4074 4 : {
4075 4 : pretty_printer pp;
4076 4 : pp.set_url_format (URL_FORMAT_NONE);
4077 4 : pp_begin_url (&pp, "http://example.com");
4078 4 : pp_string (&pp, "This is a link");
4079 4 : pp_end_url (&pp);
4080 4 : ASSERT_STREQ ("This is a link",
4081 : pp_formatted_text (&pp));
4082 4 : }
4083 :
4084 4 : {
4085 4 : pretty_printer pp;
4086 4 : pp.set_url_format (URL_FORMAT_ST);
4087 4 : pp_begin_url (&pp, "http://example.com");
4088 4 : pp_string (&pp, "This is a link");
4089 4 : pp_end_url (&pp);
4090 4 : ASSERT_STREQ ("\33]8;;http://example.com\33\\This is a link\33]8;;\33\\",
4091 : pp_formatted_text (&pp));
4092 4 : }
4093 :
4094 4 : {
4095 4 : pretty_printer pp;
4096 4 : pp.set_url_format (URL_FORMAT_BEL);
4097 4 : pp_begin_url (&pp, "http://example.com");
4098 4 : pp_string (&pp, "This is a link");
4099 4 : pp_end_url (&pp);
4100 4 : ASSERT_STREQ ("\33]8;;http://example.com\aThis is a link\33]8;;\a",
4101 : pp_formatted_text (&pp));
4102 4 : }
4103 4 : }
4104 :
4105 : static void
4106 4 : test_urls_from_braces ()
4107 : {
4108 4 : {
4109 4 : pretty_printer pp;
4110 4 : pp.set_url_format (URL_FORMAT_NONE);
4111 4 : pp_printf (&pp, "before %{text%} after",
4112 : "http://example.com");
4113 4 : ASSERT_STREQ ("before text after",
4114 : pp_formatted_text (&pp));
4115 4 : }
4116 :
4117 4 : {
4118 4 : pretty_printer pp;
4119 4 : pp.set_url_format (URL_FORMAT_ST);
4120 4 : pp_printf (&pp, "before %{text%} after",
4121 : "http://example.com");
4122 4 : ASSERT_STREQ ("before \33]8;;http://example.com\33\\text\33]8;;\33\\ after",
4123 : pp_formatted_text (&pp));
4124 4 : }
4125 :
4126 4 : {
4127 4 : pretty_printer pp;
4128 4 : pp.set_url_format (URL_FORMAT_BEL);
4129 4 : pp_printf (&pp, "before %{text%} after",
4130 : "http://example.com");
4131 4 : ASSERT_STREQ ("before \33]8;;http://example.com\atext\33]8;;\a after",
4132 : pp_formatted_text (&pp));
4133 4 : }
4134 4 : }
4135 :
4136 : /* Verify that we gracefully reject null URLs. */
4137 :
4138 : static void
4139 4 : test_null_urls ()
4140 : {
4141 4 : {
4142 4 : pretty_printer pp;
4143 4 : pp.set_url_format (URL_FORMAT_NONE);
4144 4 : pp_begin_url (&pp, nullptr);
4145 4 : pp_string (&pp, "This isn't a link");
4146 4 : pp_end_url (&pp);
4147 4 : ASSERT_STREQ ("This isn't a link",
4148 : pp_formatted_text (&pp));
4149 4 : }
4150 :
4151 4 : {
4152 4 : pretty_printer pp;
4153 4 : pp.set_url_format (URL_FORMAT_ST);
4154 4 : pp_begin_url (&pp, nullptr);
4155 4 : pp_string (&pp, "This isn't a link");
4156 4 : pp_end_url (&pp);
4157 4 : ASSERT_STREQ ("This isn't a link",
4158 : pp_formatted_text (&pp));
4159 4 : }
4160 :
4161 4 : {
4162 4 : pretty_printer pp;
4163 4 : pp.set_url_format (URL_FORMAT_BEL);
4164 4 : pp_begin_url (&pp, nullptr);
4165 4 : pp_string (&pp, "This isn't a link");
4166 4 : pp_end_url (&pp);
4167 4 : ASSERT_STREQ ("This isn't a link",
4168 : pp_formatted_text (&pp));
4169 4 : }
4170 4 : }
4171 :
4172 : /* Verify that URLification works as expected. */
4173 :
4174 : static void
4175 48 : pp_printf_with_urlifier (pretty_printer *pp,
4176 : const urlifier *urlifier,
4177 : const char *msg, ...)
4178 : {
4179 48 : va_list ap;
4180 :
4181 48 : va_start (ap, msg);
4182 48 : text_info text (msg, &ap, errno);
4183 48 : pp_format (pp, &text);
4184 48 : pp_output_formatted_text (pp, urlifier);
4185 48 : va_end (ap);
4186 48 : }
4187 :
4188 : static void
4189 4 : test_urlification ()
4190 : {
4191 4 : class test_urlifier : public urlifier
4192 : {
4193 : public:
4194 : char *
4195 72 : get_url_for_quoted_text (const char *p, size_t sz) const final override
4196 : {
4197 72 : if (!strncmp (p, "-foption", sz))
4198 56 : return xstrdup ("http://example.com");
4199 : return nullptr;
4200 : }
4201 : };
4202 :
4203 4 : auto_fix_quotes fix_quotes;
4204 4 : const test_urlifier urlifier;
4205 :
4206 : /* Uses of "%<" and "%>". */
4207 4 : {
4208 4 : {
4209 4 : pretty_printer pp;
4210 4 : pp.set_url_format (URL_FORMAT_NONE);
4211 4 : pp_printf_with_urlifier (&pp, &urlifier,
4212 : "foo %<-foption%> %<unrecognized%> bar");
4213 4 : ASSERT_STREQ ("foo `-foption' `unrecognized' bar",
4214 : pp_formatted_text (&pp));
4215 4 : }
4216 4 : {
4217 4 : pretty_printer pp;
4218 4 : pp.set_url_format (URL_FORMAT_ST);
4219 4 : pp_printf_with_urlifier (&pp, &urlifier,
4220 : "foo %<-foption%> %<unrecognized%> bar");
4221 4 : ASSERT_STREQ
4222 : ("foo `\33]8;;http://example.com\33\\-foption\33]8;;\33\\'"
4223 : " `unrecognized' bar",
4224 : pp_formatted_text (&pp));
4225 4 : }
4226 4 : {
4227 4 : pretty_printer pp;
4228 4 : pp.set_url_format (URL_FORMAT_BEL);
4229 4 : pp_printf_with_urlifier (&pp, &urlifier,
4230 : "foo %<-foption%> %<unrecognized%> bar");
4231 4 : ASSERT_STREQ
4232 : ("foo `\33]8;;http://example.com\a-foption\33]8;;\a'"
4233 : " `unrecognized' bar",
4234 : pp_formatted_text (&pp));
4235 4 : }
4236 : }
4237 :
4238 : /* Use of "%qs". */
4239 4 : {
4240 4 : pretty_printer pp;
4241 4 : pp.set_url_format (URL_FORMAT_ST);
4242 4 : pp_printf_with_urlifier (&pp, &urlifier,
4243 : "foo %qs %qs bar",
4244 : "-foption", "unrecognized");
4245 4 : ASSERT_STREQ
4246 : ("foo `\33]8;;http://example.com\33\\-foption\33]8;;\33\\'"
4247 : " `unrecognized' bar",
4248 : pp_formatted_text (&pp));
4249 4 : }
4250 :
4251 : /* Mixed usage of %< and %s, where the quoted string is built between
4252 : a mixture of phase 1 and phase 2. */
4253 4 : {
4254 4 : pretty_printer pp;
4255 4 : pp.set_url_format (URL_FORMAT_ST);
4256 4 : pp_printf_with_urlifier (&pp, &urlifier,
4257 : "foo %<-f%s%> bar",
4258 : "option");
4259 4 : ASSERT_STREQ
4260 : ("foo `\33]8;;http://example.com\33\\-foption\33]8;;\33\\' bar",
4261 : pp_formatted_text (&pp));
4262 4 : }
4263 :
4264 : /* Likewise, where there is trailing phase 1 content within the
4265 : quoted region. */
4266 4 : {
4267 4 : pretty_printer pp;
4268 4 : pp.set_url_format (URL_FORMAT_ST);
4269 4 : pp_printf_with_urlifier (&pp, &urlifier,
4270 : "foo %<-f%sion%> bar %<-f%sion%> baz",
4271 : "opt", "opt");
4272 4 : ASSERT_STREQ
4273 : ("foo `\33]8;;http://example.com\33\\-foption\33]8;;\33\\' bar `\33]8;;http://example.com\33\\-foption\33]8;;\33\\' baz",
4274 : pp_formatted_text (&pp));
4275 4 : }
4276 :
4277 : /* Likewise. */
4278 4 : {
4279 4 : pretty_printer pp;
4280 4 : pp.set_url_format (URL_FORMAT_ST);
4281 4 : pp_printf_with_urlifier (&pp, &urlifier,
4282 : "foo %<%sption%> bar %<-f%sion%> baz",
4283 : "-fo", "opt");
4284 4 : ASSERT_STREQ
4285 : ("foo `\33]8;;http://example.com\33\\-foption\33]8;;\33\\' bar `\33]8;;http://example.com\33\\-foption\33]8;;\33\\' baz",
4286 : pp_formatted_text (&pp));
4287 4 : }
4288 :
4289 : /* Another mixed usage of %< and %s, where the quoted string is built
4290 : between a mixture of phase 1 and multiple phase 2. */
4291 4 : {
4292 4 : pretty_printer pp;
4293 4 : pp.set_url_format (URL_FORMAT_ST);
4294 4 : pp_printf_with_urlifier (&pp, &urlifier,
4295 : "foo %<-f%s%s%> bar",
4296 : "opt", "ion");
4297 4 : ASSERT_STREQ
4298 : ("foo `\33]8;;http://example.com\33\\-foption\33]8;;\33\\' bar",
4299 : pp_formatted_text (&pp));
4300 4 : }
4301 :
4302 : /* Mixed usage of %< and %s with a prefix. */
4303 4 : {
4304 4 : pretty_printer pp;
4305 4 : pp.set_url_format (URL_FORMAT_ST);
4306 4 : pp_set_prefix (&pp, xstrdup ("PREFIX"));
4307 4 : pp_printf_with_urlifier (&pp, &urlifier,
4308 : "foo %<-f%s%> bar",
4309 : "option");
4310 4 : ASSERT_STREQ
4311 : ("PREFIXfoo `\33]8;;http://example.com\33\\-foption\33]8;;\33\\' bar",
4312 : pp_formatted_text (&pp));
4313 4 : }
4314 :
4315 : /* Example of mixed %< and %s with numbered args. */
4316 4 : {
4317 4 : pretty_printer pp;
4318 4 : pp.set_url_format (URL_FORMAT_ST);
4319 4 : pp_printf_with_urlifier (&pp, &urlifier,
4320 : "foo %<-f%2$st%1$sn%> bar",
4321 : "io", "op");
4322 4 : ASSERT_STREQ
4323 : ("foo `\33]8;;http://example.com\33\\-foption\33]8;;\33\\' bar",
4324 : pp_formatted_text (&pp));
4325 4 : }
4326 :
4327 : /* Example of %e. */
4328 4 : {
4329 4 : pretty_printer pp;
4330 4 : pp.set_url_format (URL_FORMAT_ST);
4331 4 : pp_element_quoted_string elem ("-foption");
4332 4 : pp_printf_with_urlifier (&pp, &urlifier,
4333 : "foo %e bar",
4334 : &elem);
4335 4 : ASSERT_STREQ
4336 : ("foo `\33]8;;http://example.com\33\\-foption\33]8;;\33\\' bar",
4337 : pp_formatted_text (&pp));
4338 4 : }
4339 :
4340 : /* Test the example from pretty-print-format-impl.h. */
4341 4 : {
4342 4 : pretty_printer pp;
4343 4 : pp.set_url_format (URL_FORMAT_ST);
4344 4 : pp_printf_with_urlifier (&pp, &urlifier,
4345 : "foo: %i, bar: %s, option: %qs",
4346 : 42, "baz", "-foption");
4347 4 : ASSERT_STREQ (pp_formatted_text (&pp),
4348 : "foo: 42, bar: baz, option:"
4349 : " `]8;;http://example.com\\-foption]8;;\\'");
4350 4 : }
4351 4 : }
4352 :
4353 : /* Test multibyte awareness. */
4354 4 : static void test_utf8 ()
4355 : {
4356 :
4357 : /* Check that pp_quoted_string leaves valid UTF-8 alone. */
4358 4 : {
4359 4 : pretty_printer pp;
4360 4 : const char *s = "\xf0\x9f\x98\x82";
4361 4 : pp_quoted_string (&pp, s);
4362 4 : ASSERT_STREQ (pp_formatted_text (&pp), s);
4363 4 : }
4364 :
4365 : /* Check that pp_quoted_string escapes non-UTF-8 nonprintable bytes. */
4366 4 : {
4367 4 : pretty_printer pp;
4368 4 : pp_quoted_string (&pp, "\xf0!\x9f\x98\x82");
4369 4 : ASSERT_STREQ (pp_formatted_text (&pp),
4370 : "\\xf0!\\x9f\\x98\\x82");
4371 4 : }
4372 :
4373 : /* Check that pp_character will line-wrap at the beginning of a UTF-8
4374 : sequence, but not in the middle. */
4375 4 : {
4376 4 : pretty_printer pp (3);
4377 4 : const char s[] = "---\xf0\x9f\x98\x82";
4378 32 : for (int i = 0; i != sizeof (s) - 1; ++i)
4379 28 : pp_character (&pp, s[i]);
4380 4 : pp_newline (&pp);
4381 32 : for (int i = 1; i != sizeof (s) - 1; ++i)
4382 24 : pp_character (&pp, s[i]);
4383 4 : pp_character (&pp, '-');
4384 4 : ASSERT_STREQ (pp_formatted_text (&pp),
4385 : "---\n"
4386 : "\xf0\x9f\x98\x82\n"
4387 : "--\xf0\x9f\x98\x82\n"
4388 : "-");
4389 4 : }
4390 :
4391 4 : }
4392 :
4393 : /* Verify that class comma_separated_quoted_strings works as expected. */
4394 :
4395 : static void
4396 4 : test_comma_separated_quoted_strings ()
4397 : {
4398 4 : auto_fix_quotes fix_quotes;
4399 :
4400 4 : auto_vec<const char *> none;
4401 4 : pp_markup::comma_separated_quoted_strings e_none (none);
4402 :
4403 4 : auto_vec<const char *> one;
4404 4 : one.safe_push ("one");
4405 4 : pp_markup::comma_separated_quoted_strings e_one (one);
4406 :
4407 4 : auto_vec<const char *> many;
4408 4 : many.safe_push ("0");
4409 4 : many.safe_push ("1");
4410 4 : many.safe_push ("2");
4411 4 : pp_markup::comma_separated_quoted_strings e_many (many);
4412 :
4413 4 : ASSERT_PP_FORMAT_3 ("none: () one: (`one') many: (`0', `1', `2')",
4414 : "none: (%e) one: (%e) many: (%e)",
4415 : &e_none, &e_one, &e_many);
4416 4 : assert_pp_format_colored (SELFTEST_LOCATION,
4417 : "one: (`[01m[Kone[m[K')",
4418 : "one: (%e)",
4419 : &e_one);
4420 4 : }
4421 :
4422 : /* Run all of the selftests within this file. */
4423 :
4424 : void
4425 4 : pretty_print_cc_tests ()
4426 : {
4427 4 : test_basic_printing ();
4428 4 : test_pp_format ();
4429 4 : test_merge_consecutive_text_tokens ();
4430 4 : test_custom_tokens_1 ();
4431 4 : test_custom_tokens_2 ();
4432 4 : test_pp_format_stack ();
4433 4 : test_pp_printf_within_pp_element ();
4434 4 : test_prefixes_and_wrapping ();
4435 4 : test_urls ();
4436 4 : test_urls_from_braces ();
4437 4 : test_null_urls ();
4438 4 : test_urlification ();
4439 4 : test_utf8 ();
4440 4 : test_comma_separated_quoted_strings ();
4441 4 : }
4442 :
4443 : } // namespace selftest
4444 :
4445 : #endif /* CHECKING_P */
|