]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/pretty-print.c
update-copyright.py: Add Mentor Graphics Corporation and Yoshinori Sato as external...
[thirdparty/gcc.git] / gcc / pretty-print.c
CommitLineData
b6fe0bb8 1/* Various declarations for language-independent pretty-print subroutines.
a5544970 2 Copyright (C) 2003-2019 Free Software Foundation, Inc.
b6fe0bb8
GDR
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9dcd6f09 9Software Foundation; either version 3, or (at your option) any later
b6fe0bb8
GDR
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
9dcd6f09
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
b6fe0bb8
GDR
20
21#include "config.h"
b6fe0bb8
GDR
22#include "system.h"
23#include "coretypes.h"
a668adb2 24#include "intl.h"
b6fe0bb8 25#include "pretty-print.h"
4b84d650 26#include "diagnostic-color.h"
4ccab56d 27#include "selftest.h"
a3af5087
JM
28
29#if HAVE_ICONV
30#include <iconv.h>
31#endif
b6fe0bb8 32
db0d1bae
LH
33#ifdef __MINGW32__
34
35/* Replacement for fputs() that handles ANSI escape codes on Windows NT.
36 Contributed by: Liu Hao (lh_mouse at 126 dot com)
37
38 XXX: This file is compiled into libcommon.a that will be self-contained.
39 It looks like that these functions can be put nowhere else. */
40
41#include <io.h>
42#define WIN32_LEAN_AND_MEAN 1
43#include <windows.h>
44
45/* Write all bytes in [s,s+n) into the specified stream.
46 Errors are ignored. */
47static void
48write_all (HANDLE h, const char *s, size_t n)
49{
50 size_t rem = n;
51 DWORD step;
52
53 while (rem != 0)
54 {
55 if (rem <= UINT_MAX)
56 step = rem;
57 else
58 step = UINT_MAX;
59 if (!WriteFile (h, s + n - rem, step, &step, NULL))
60 break;
61 rem -= step;
62 }
63}
64
65/* Find the beginning of an escape sequence.
66 There are two cases:
67 1. If the sequence begins with an ESC character (0x1B) and a second
68 character X in [0x40,0x5F], returns X and stores a pointer to
69 the third character into *head.
70 2. If the sequence begins with a character X in [0x80,0x9F], returns
71 (X-0x40) and stores a pointer to the second character into *head.
72 Stores the number of ESC character(s) in *prefix_len.
73 Returns 0 if no such sequence can be found. */
74static int
75find_esc_head (int *prefix_len, const char **head, const char *str)
76{
77 int c;
78 const char *r = str;
79 int escaped = 0;
80
81 for (;;)
82 {
83 c = (unsigned char) *r;
84 if (c == 0)
85 {
86 /* Not found. */
87 return 0;
88 }
89 if (escaped && 0x40 <= c && c <= 0x5F)
90 {
91 /* Found (case 1). */
92 *prefix_len = 2;
93 *head = r + 1;
94 return c;
95 }
96 if (0x80 <= c && c <= 0x9F)
97 {
98 /* Found (case 2). */
99 *prefix_len = 1;
100 *head = r + 1;
101 return c - 0x40;
102 }
103 ++r;
104 escaped = c == 0x1B;
105 }
106}
107
108/* Find the terminator of an escape sequence.
109 str should be the value stored in *head by a previous successful
110 call to find_esc_head().
111 Returns 0 if no such sequence can be found. */
112static int
113find_esc_terminator (const char **term, const char *str)
114{
115 int c;
116 const char *r = str;
117
118 for (;;)
119 {
120 c = (unsigned char) *r;
121 if (c == 0)
122 {
123 /* Not found. */
124 return 0;
125 }
126 if (0x40 <= c && c <= 0x7E)
127 {
128 /* Found. */
129 *term = r;
130 return c;
131 }
132 ++r;
133 }
134}
135
136/* Handle a sequence of codes. Sequences that are invalid, reserved,
137 unrecognized or unimplemented are ignored silently.
138 There isn't much we can do because of lameness of Windows consoles. */
139static void
140eat_esc_sequence (HANDLE h, int esc_code,
141 const char *esc_head, const char *esc_term)
142{
143 /* Numbers in an escape sequence cannot be negative, because
144 a minus sign in the middle of it would have terminated it. */
145 long n1, n2;
146 char *eptr, *delim;
147 CONSOLE_SCREEN_BUFFER_INFO sb;
148 COORD cr;
149 /* ED and EL parameters. */
150 DWORD cnt, step;
151 long rows;
152 /* SGR parameters. */
153 WORD attrib_add, attrib_rm;
154 const char *param;
155
156 switch (MAKEWORD (esc_code, *esc_term))
157 {
158 /* ESC [ n1 'A'
159 Move the cursor up by n1 characters. */
160 case MAKEWORD ('[', 'A'):
161 if (esc_head == esc_term)
162 n1 = 1;
163 else
164 {
165 n1 = strtol (esc_head, &eptr, 10);
166 if (eptr != esc_term)
167 break;
168 }
169
170 if (GetConsoleScreenBufferInfo (h, &sb))
171 {
172 cr = sb.dwCursorPosition;
173 /* Stop at the topmost boundary. */
174 if (cr.Y > n1)
175 cr.Y -= n1;
176 else
177 cr.Y = 0;
178 SetConsoleCursorPosition (h, cr);
179 }
180 break;
181
182 /* ESC [ n1 'B'
183 Move the cursor down by n1 characters. */
184 case MAKEWORD ('[', 'B'):
185 if (esc_head == esc_term)
186 n1 = 1;
187 else
188 {
189 n1 = strtol (esc_head, &eptr, 10);
190 if (eptr != esc_term)
191 break;
192 }
193
194 if (GetConsoleScreenBufferInfo (h, &sb))
195 {
196 cr = sb.dwCursorPosition;
197 /* Stop at the bottommost boundary. */
198 if (sb.dwSize.Y - cr.Y > n1)
199 cr.Y += n1;
200 else
201 cr.Y = sb.dwSize.Y;
202 SetConsoleCursorPosition (h, cr);
203 }
204 break;
205
206 /* ESC [ n1 'C'
207 Move the cursor right by n1 characters. */
208 case MAKEWORD ('[', 'C'):
209 if (esc_head == esc_term)
210 n1 = 1;
211 else
212 {
213 n1 = strtol (esc_head, &eptr, 10);
214 if (eptr != esc_term)
215 break;
216 }
217
218 if (GetConsoleScreenBufferInfo (h, &sb))
219 {
220 cr = sb.dwCursorPosition;
221 /* Stop at the rightmost boundary. */
222 if (sb.dwSize.X - cr.X > n1)
223 cr.X += n1;
224 else
225 cr.X = sb.dwSize.X;
226 SetConsoleCursorPosition (h, cr);
227 }
228 break;
229
230 /* ESC [ n1 'D'
231 Move the cursor left by n1 characters. */
232 case MAKEWORD ('[', 'D'):
233 if (esc_head == esc_term)
234 n1 = 1;
235 else
236 {
237 n1 = strtol (esc_head, &eptr, 10);
238 if (eptr != esc_term)
239 break;
240 }
241
242 if (GetConsoleScreenBufferInfo (h, &sb))
243 {
244 cr = sb.dwCursorPosition;
245 /* Stop at the leftmost boundary. */
246 if (cr.X > n1)
247 cr.X -= n1;
248 else
249 cr.X = 0;
250 SetConsoleCursorPosition (h, cr);
251 }
252 break;
253
254 /* ESC [ n1 'E'
255 Move the cursor to the beginning of the n1-th line downwards. */
256 case MAKEWORD ('[', 'E'):
257 if (esc_head == esc_term)
258 n1 = 1;
259 else
260 {
261 n1 = strtol (esc_head, &eptr, 10);
262 if (eptr != esc_term)
263 break;
264 }
265
266 if (GetConsoleScreenBufferInfo (h, &sb))
267 {
268 cr = sb.dwCursorPosition;
269 cr.X = 0;
270 /* Stop at the bottommost boundary. */
271 if (sb.dwSize.Y - cr.Y > n1)
272 cr.Y += n1;
273 else
274 cr.Y = sb.dwSize.Y;
275 SetConsoleCursorPosition (h, cr);
276 }
277 break;
278
279 /* ESC [ n1 'F'
280 Move the cursor to the beginning of the n1-th line upwards. */
281 case MAKEWORD ('[', 'F'):
282 if (esc_head == esc_term)
283 n1 = 1;
284 else
285 {
286 n1 = strtol (esc_head, &eptr, 10);
287 if (eptr != esc_term)
288 break;
289 }
290
291 if (GetConsoleScreenBufferInfo (h, &sb))
292 {
293 cr = sb.dwCursorPosition;
294 cr.X = 0;
295 /* Stop at the topmost boundary. */
296 if (cr.Y > n1)
297 cr.Y -= n1;
298 else
299 cr.Y = 0;
300 SetConsoleCursorPosition (h, cr);
301 }
302 break;
303
304 /* ESC [ n1 'G'
305 Move the cursor to the (1-based) n1-th column. */
306 case MAKEWORD ('[', 'G'):
307 if (esc_head == esc_term)
308 n1 = 1;
309 else
310 {
311 n1 = strtol (esc_head, &eptr, 10);
312 if (eptr != esc_term)
313 break;
314 }
315
316 if (GetConsoleScreenBufferInfo (h, &sb))
317 {
318 cr = sb.dwCursorPosition;
319 n1 -= 1;
320 /* Stop at the leftmost or rightmost boundary. */
321 if (n1 < 0)
322 cr.X = 0;
323 else if (n1 > sb.dwSize.X)
324 cr.X = sb.dwSize.X;
325 else
326 cr.X = n1;
327 SetConsoleCursorPosition (h, cr);
328 }
329 break;
330
331 /* ESC [ n1 ';' n2 'H'
332 ESC [ n1 ';' n2 'f'
333 Move the cursor to the (1-based) n1-th row and
334 (also 1-based) n2-th column. */
335 case MAKEWORD ('[', 'H'):
336 case MAKEWORD ('[', 'f'):
337 if (esc_head == esc_term)
338 {
339 /* Both parameters are omitted and set to 1 by default. */
340 n1 = 1;
341 n2 = 1;
342 }
343 else if (!(delim = (char *) memchr (esc_head, ';',
344 esc_term - esc_head)))
345 {
346 /* Only the first parameter is given. The second one is
347 set to 1 by default. */
348 n1 = strtol (esc_head, &eptr, 10);
349 if (eptr != esc_term)
350 break;
351 n2 = 1;
352 }
353 else
354 {
355 /* Both parameters are given. The first one shall be
356 terminated by the semicolon. */
357 n1 = strtol (esc_head, &eptr, 10);
358 if (eptr != delim)
359 break;
360 n2 = strtol (delim + 1, &eptr, 10);
361 if (eptr != esc_term)
362 break;
363 }
364
365 if (GetConsoleScreenBufferInfo (h, &sb))
366 {
367 cr = sb.dwCursorPosition;
368 n1 -= 1;
369 n2 -= 1;
370 /* The cursor position shall be relative to the view coord of
371 the console window, which is usually smaller than the actual
372 buffer. FWIW, the 'appropriate' solution will be shrinking
373 the buffer to match the size of the console window,
374 destroying scrollback in the process. */
375 n1 += sb.srWindow.Top;
376 n2 += sb.srWindow.Left;
377 /* Stop at the topmost or bottommost boundary. */
378 if (n1 < 0)
379 cr.Y = 0;
380 else if (n1 > sb.dwSize.Y)
381 cr.Y = sb.dwSize.Y;
382 else
383 cr.Y = n1;
384 /* Stop at the leftmost or rightmost boundary. */
385 if (n2 < 0)
386 cr.X = 0;
387 else if (n2 > sb.dwSize.X)
388 cr.X = sb.dwSize.X;
389 else
390 cr.X = n2;
391 SetConsoleCursorPosition (h, cr);
392 }
393 break;
394
395 /* ESC [ n1 'J'
396 Erase display. */
397 case MAKEWORD ('[', 'J'):
398 if (esc_head == esc_term)
399 /* This is one of the very few codes whose parameters have
400 a default value of zero. */
401 n1 = 0;
402 else
403 {
404 n1 = strtol (esc_head, &eptr, 10);
405 if (eptr != esc_term)
406 break;
407 }
408
409 if (GetConsoleScreenBufferInfo (h, &sb))
410 {
411 /* The cursor is not necessarily in the console window, which
412 makes the behavior of this code harder to define. */
413 switch (n1)
414 {
415 case 0:
416 /* If the cursor is in or above the window, erase from
417 it to the bottom of the window; otherwise, do nothing. */
418 cr = sb.dwCursorPosition;
419 cnt = sb.dwSize.X - sb.dwCursorPosition.X;
420 rows = sb.srWindow.Bottom - sb.dwCursorPosition.Y;
421 break;
422 case 1:
423 /* If the cursor is in or under the window, erase from
424 it to the top of the window; otherwise, do nothing. */
425 cr.X = 0;
426 cr.Y = sb.srWindow.Top;
427 cnt = sb.dwCursorPosition.X + 1;
428 rows = sb.dwCursorPosition.Y - sb.srWindow.Top;
429 break;
430 case 2:
431 /* Erase the entire window. */
432 cr.X = sb.srWindow.Left;
433 cr.Y = sb.srWindow.Top;
434 cnt = 0;
435 rows = sb.srWindow.Bottom - sb.srWindow.Top + 1;
436 break;
437 default:
438 /* Erase the entire buffer. */
439 cr.X = 0;
440 cr.Y = 0;
441 cnt = 0;
442 rows = sb.dwSize.Y;
443 break;
444 }
445 if (rows < 0)
446 break;
447 cnt += rows * sb.dwSize.X;
448 FillConsoleOutputCharacterW (h, L' ', cnt, cr, &step);
449 FillConsoleOutputAttribute (h, sb.wAttributes, cnt, cr, &step);
450 }
451 break;
452
453 /* ESC [ n1 'K'
454 Erase line. */
455 case MAKEWORD ('[', 'K'):
456 if (esc_head == esc_term)
457 /* This is one of the very few codes whose parameters have
458 a default value of zero. */
459 n1 = 0;
460 else
461 {
462 n1 = strtol (esc_head, &eptr, 10);
463 if (eptr != esc_term)
464 break;
465 }
466
467 if (GetConsoleScreenBufferInfo (h, &sb))
468 {
469 switch (n1)
470 {
471 case 0:
472 /* Erase from the cursor to the end. */
473 cr = sb.dwCursorPosition;
474 cnt = sb.dwSize.X - sb.dwCursorPosition.X;
475 break;
476 case 1:
477 /* Erase from the cursor to the beginning. */
478 cr = sb.dwCursorPosition;
479 cr.X = 0;
480 cnt = sb.dwCursorPosition.X + 1;
481 break;
482 default:
483 /* Erase the entire line. */
484 cr = sb.dwCursorPosition;
485 cr.X = 0;
486 cnt = sb.dwSize.X;
487 break;
488 }
489 FillConsoleOutputCharacterW (h, L' ', cnt, cr, &step);
490 FillConsoleOutputAttribute (h, sb.wAttributes, cnt, cr, &step);
491 }
492 break;
493
494 /* ESC [ n1 ';' n2 'm'
495 Set SGR parameters. Zero or more parameters will follow. */
496 case MAKEWORD ('[', 'm'):
497 attrib_add = 0;
498 attrib_rm = 0;
499 if (esc_head == esc_term)
500 {
501 /* When no parameter is given, reset the console. */
502 attrib_add |= (FOREGROUND_RED | FOREGROUND_GREEN
503 | FOREGROUND_BLUE);
504 attrib_rm = -1; /* Removes everything. */
505 goto sgr_set_it;
506 }
507 param = esc_head;
508 do
509 {
510 /* Parse a parameter. */
511 n1 = strtol (param, &eptr, 10);
512 if (*eptr != ';' && eptr != esc_term)
513 goto sgr_set_it;
514
515 switch (n1)
516 {
517 case 0:
518 /* Reset. */
519 attrib_add |= (FOREGROUND_RED | FOREGROUND_GREEN
520 | FOREGROUND_BLUE);
521 attrib_rm = -1; /* Removes everything. */
522 break;
523 case 1:
524 /* Bold. */
525 attrib_add |= FOREGROUND_INTENSITY;
526 break;
527 case 4:
528 /* Underline. */
529 attrib_add |= COMMON_LVB_UNDERSCORE;
530 break;
531 case 5:
532 /* Blink. */
533 /* XXX: It is not BLINKING at all! */
534 attrib_add |= BACKGROUND_INTENSITY;
535 break;
536 case 7:
537 /* Reverse. */
538 attrib_add |= COMMON_LVB_REVERSE_VIDEO;
539 break;
540 case 22:
541 /* No bold. */
542 attrib_add &= ~FOREGROUND_INTENSITY;
543 attrib_rm |= FOREGROUND_INTENSITY;
544 break;
545 case 24:
546 /* No underline. */
547 attrib_add &= ~COMMON_LVB_UNDERSCORE;
548 attrib_rm |= COMMON_LVB_UNDERSCORE;
549 break;
550 case 25:
551 /* No blink. */
552 /* XXX: It is not BLINKING at all! */
553 attrib_add &= ~BACKGROUND_INTENSITY;
554 attrib_rm |= BACKGROUND_INTENSITY;
555 break;
556 case 27:
557 /* No reverse. */
558 attrib_add &= ~COMMON_LVB_REVERSE_VIDEO;
559 attrib_rm |= COMMON_LVB_REVERSE_VIDEO;
560 break;
561 case 30:
562 case 31:
563 case 32:
564 case 33:
565 case 34:
566 case 35:
567 case 36:
568 case 37:
569 /* Foreground color. */
570 attrib_add &= ~(FOREGROUND_RED | FOREGROUND_GREEN
571 | FOREGROUND_BLUE);
572 n1 -= 30;
573 if (n1 & 1)
574 attrib_add |= FOREGROUND_RED;
575 if (n1 & 2)
576 attrib_add |= FOREGROUND_GREEN;
577 if (n1 & 4)
578 attrib_add |= FOREGROUND_BLUE;
579 attrib_rm |= (FOREGROUND_RED | FOREGROUND_GREEN
580 | FOREGROUND_BLUE);
581 break;
582 case 38:
583 /* Reserved for extended foreground color.
584 Don't know how to handle parameters remaining.
585 Bail out. */
586 goto sgr_set_it;
587 case 39:
588 /* Reset foreground color. */
589 /* Set to grey. */
590 attrib_add |= (FOREGROUND_RED | FOREGROUND_GREEN
591 | FOREGROUND_BLUE);
592 attrib_rm |= (FOREGROUND_RED | FOREGROUND_GREEN
593 | FOREGROUND_BLUE);
594 break;
595 case 40:
596 case 41:
597 case 42:
598 case 43:
599 case 44:
600 case 45:
601 case 46:
602 case 47:
603 /* Background color. */
604 attrib_add &= ~(BACKGROUND_RED | BACKGROUND_GREEN
605 | BACKGROUND_BLUE);
606 n1 -= 40;
607 if (n1 & 1)
608 attrib_add |= BACKGROUND_RED;
609 if (n1 & 2)
610 attrib_add |= BACKGROUND_GREEN;
611 if (n1 & 4)
612 attrib_add |= BACKGROUND_BLUE;
613 attrib_rm |= (BACKGROUND_RED | BACKGROUND_GREEN
614 | BACKGROUND_BLUE);
615 break;
616 case 48:
617 /* Reserved for extended background color.
618 Don't know how to handle parameters remaining.
619 Bail out. */
620 goto sgr_set_it;
621 case 49:
622 /* Reset background color. */
623 /* Set to black. */
624 attrib_add &= ~(BACKGROUND_RED | BACKGROUND_GREEN
625 | BACKGROUND_BLUE);
626 attrib_rm |= (BACKGROUND_RED | BACKGROUND_GREEN
627 | BACKGROUND_BLUE);
628 break;
629 }
630
631 /* Prepare the next parameter. */
632 param = eptr + 1;
633 }
634 while (param != esc_term);
635
636sgr_set_it:
637 /* 0xFFFF removes everything. If it is not the case,
638 care must be taken to preserve old attributes. */
639 if (attrib_rm != 0xFFFF && GetConsoleScreenBufferInfo (h, &sb))
640 {
641 attrib_add |= sb.wAttributes & ~attrib_rm;
642 }
5d3083dd
LH
643 if (attrib_add & COMMON_LVB_REVERSE_VIDEO)
644 {
645 /* COMMON_LVB_REVERSE_VIDEO is only effective for DBCS.
646 * Swap foreground and background colors by hand.
647 */
648 attrib_add = (attrib_add & 0xFF00)
649 | ((attrib_add & 0x00F0) >> 4)
650 | ((attrib_add & 0x000F) << 4);
651 attrib_add &= ~COMMON_LVB_REVERSE_VIDEO;
652 }
db0d1bae
LH
653 SetConsoleTextAttribute (h, attrib_add);
654 break;
655 }
656}
657
658int
659mingw_ansi_fputs (const char *str, FILE *fp)
660{
661 const char *read = str;
662 HANDLE h;
663 DWORD mode;
664 int esc_code, prefix_len;
665 const char *esc_head, *esc_term;
666
667 h = (HANDLE) _get_osfhandle (_fileno (fp));
668 if (h == INVALID_HANDLE_VALUE)
669 return EOF;
670
671 /* Don't mess up stdio functions with Windows APIs. */
672 fflush (fp);
673
674 if (GetConsoleMode (h, &mode))
675 /* If it is a console, translate ANSI escape codes as needed. */
676 for (;;)
677 {
678 if ((esc_code = find_esc_head (&prefix_len, &esc_head, read)) == 0)
679 {
680 /* Write all remaining characters, then exit. */
681 write_all (h, read, strlen (read));
682 break;
683 }
684 if (find_esc_terminator (&esc_term, esc_head) == 0)
685 /* Ignore incomplete escape sequences at the moment.
686 FIXME: The escape state shall be cached for further calls
687 to this function. */
688 break;
689 write_all (h, read, esc_head - prefix_len - read);
690 eat_esc_sequence (h, esc_code, esc_head, esc_term);
691 read = esc_term + 1;
692 }
693 else
694 /* If it is not a console, write everything as-is. */
695 write_all (h, read, strlen (read));
696
db0d1bae
LH
697 return 1;
698}
699
700#endif /* __MINGW32__ */
701
ddd0fd17
LH
702static int
703decode_utf8_char (const unsigned char *, size_t len, unsigned int *);
3f0177e7
MS
704static void pp_quoted_string (pretty_printer *, const char *, size_t = -1);
705
f79520bb 706/* Overwrite the given location/range within this text_info's rich_location.
8a645150
DM
707 For use e.g. when implementing "+" in client format decoders. */
708
709void
85204e23
DM
710text_info::set_location (unsigned int idx, location_t loc,
711 enum range_display_kind range_display_kind)
8a645150
DM
712{
713 gcc_checking_assert (m_richloc);
85204e23 714 m_richloc->set_range (idx, loc, range_display_kind);
8a645150
DM
715}
716
717location_t
718text_info::get_location (unsigned int index_of_location) const
719{
720 gcc_checking_assert (m_richloc);
721
722 if (index_of_location == 0)
723 return m_richloc->get_loc ();
724 else
725 return UNKNOWN_LOCATION;
726}
727
da6ca2b5
GDR
728// Default construct an output buffer.
729
730output_buffer::output_buffer ()
731 : formatted_obstack (),
732 chunk_obstack (),
733 obstack (&formatted_obstack),
734 cur_chunk_array (),
735 stream (stderr),
736 line_length (),
48749dbc
MLI
737 digit_buffer (),
738 flush_p (true)
da6ca2b5
GDR
739{
740 obstack_init (&formatted_obstack);
741 obstack_init (&chunk_obstack);
742}
743
025311c4
GDR
744// Release resources owned by an output buffer at the end of lifetime.
745
746output_buffer::~output_buffer ()
747{
65f5c720
RB
748 obstack_free (&chunk_obstack, NULL);
749 obstack_free (&formatted_obstack, NULL);
025311c4
GDR
750}
751
b6fe0bb8
GDR
752
753/* Format an integer given by va_arg (ARG, type-specifier T) where
754 type-specifier is a precision modifier as indicated by PREC. F is
755 a string used to construct the appropriate format-specifier. */
756#define pp_integer_with_precision(PP, ARG, PREC, T, F) \
757 do \
758 switch (PREC) \
759 { \
760 case 0: \
761 pp_scalar (PP, "%" F, va_arg (ARG, T)); \
762 break; \
763 \
764 case 1: \
765 pp_scalar (PP, "%l" F, va_arg (ARG, long T)); \
766 break; \
767 \
768 case 2: \
2a157700 769 pp_scalar (PP, "%" HOST_LONG_LONG_FORMAT F, va_arg (ARG, long long T)); \
b6fe0bb8
GDR
770 break; \
771 \
772 default: \
773 break; \
774 } \
775 while (0)
776
777
778/* Subroutine of pp_set_maximum_length. Set up PRETTY-PRINTER's
779 internal maximum characters per line. */
780static void
781pp_set_real_maximum_length (pretty_printer *pp)
782{
783 /* If we're told not to wrap lines then do the obvious thing. In case
784 we'll emit prefix only once per message, it is appropriate
785 not to increase unnecessarily the line-length cut-off. */
786 if (!pp_is_wrapping_line (pp)
787 || pp_prefixing_rule (pp) == DIAGNOSTICS_SHOW_PREFIX_ONCE
788 || pp_prefixing_rule (pp) == DIAGNOSTICS_SHOW_PREFIX_NEVER)
789 pp->maximum_length = pp_line_cutoff (pp);
790 else
791 {
792 int prefix_length = pp->prefix ? strlen (pp->prefix) : 0;
793 /* If the prefix is ridiculously too long, output at least
794 32 characters. */
795 if (pp_line_cutoff (pp) - prefix_length < 32)
796 pp->maximum_length = pp_line_cutoff (pp) + 32;
797 else
798 pp->maximum_length = pp_line_cutoff (pp);
799 }
800}
801
802/* Clear PRETTY-PRINTER's output state. */
803static inline void
804pp_clear_state (pretty_printer *pp)
805{
806 pp->emitted_prefix = false;
807 pp_indentation (pp) = 0;
808}
809
dc3f3805
RS
810/* Print X to PP in decimal. */
811template<unsigned int N, typename T>
812void
813pp_wide_integer (pretty_printer *pp, const poly_int_pod<N, T> &x)
814{
815 if (x.is_constant ())
816 pp_wide_integer (pp, x.coeffs[0]);
817 else
818 {
819 pp_left_bracket (pp);
820 for (unsigned int i = 0; i < N; ++i)
821 {
822 if (i != 0)
823 pp_comma (pp);
824 pp_wide_integer (pp, x.coeffs[i]);
825 }
826 pp_right_bracket (pp);
827 }
828}
829
830template void pp_wide_integer (pretty_printer *, const poly_uint16_pod &);
831template void pp_wide_integer (pretty_printer *, const poly_int64_pod &);
832template void pp_wide_integer (pretty_printer *, const poly_uint64_pod &);
833
b6fe0bb8 834/* Flush the formatted text of PRETTY-PRINTER onto the attached stream. */
6de9cd9a 835void
b6fe0bb8
GDR
836pp_write_text_to_stream (pretty_printer *pp)
837{
838 const char *text = pp_formatted_text (pp);
db0d1bae
LH
839#ifdef __MINGW32__
840 mingw_ansi_fputs (text, pp_buffer (pp)->stream);
841#else
025311c4 842 fputs (text, pp_buffer (pp)->stream);
db0d1bae 843#endif
b6fe0bb8
GDR
844 pp_clear_output_area (pp);
845}
846
7eba871a
SB
847/* As pp_write_text_to_stream, but for GraphViz label output.
848
849 Flush the formatted text of pretty-printer PP onto the attached stream.
850 Replace characters in PPF that have special meaning in a GraphViz .dot
851 file.
852
853 This routine is not very fast, but it doesn't have to be as this is only
854 be used by routines dumping intermediate representations in graph form. */
855
856void
857pp_write_text_as_dot_label_to_stream (pretty_printer *pp, bool for_record)
858{
859 const char *text = pp_formatted_text (pp);
860 const char *p = text;
025311c4 861 FILE *fp = pp_buffer (pp)->stream;
7eba871a 862
da3ebf2d 863 for (;*p; p++)
7eba871a 864 {
da3ebf2d 865 bool escape_char;
7eba871a
SB
866 switch (*p)
867 {
868 /* Print newlines as a left-aligned newline. */
869 case '\n':
da3ebf2d
TV
870 fputs ("\\l", fp);
871 escape_char = true;
7eba871a
SB
872 break;
873
795391fb 874 /* The following characters are only special for record-shape nodes. */
7eba871a 875 case '|':
795391fb
TV
876 case '{':
877 case '}':
878 case '<':
879 case '>':
880 case ' ':
da3ebf2d 881 escape_char = for_record;
7eba871a
SB
882 break;
883
884 /* The following characters always have to be escaped
885 for use in labels. */
b3de2446
TV
886 case '\\':
887 /* There is a bug in some (f.i. 2.36.0) versions of graphiz
888 ( http://www.graphviz.org/mantisbt/view.php?id=2524 ) related to
889 backslash as last char in label. Let's avoid triggering it. */
890 gcc_assert (*(p + 1) != '\0');
891 /* Fall through. */
7eba871a 892 case '"':
da3ebf2d
TV
893 escape_char = true;
894 break;
895
7eba871a 896 default:
da3ebf2d 897 escape_char = false;
7eba871a
SB
898 break;
899 }
da3ebf2d
TV
900
901 if (escape_char)
902 fputc ('\\', fp);
903
904 fputc (*p, fp);
7eba871a
SB
905 }
906
907 pp_clear_output_area (pp);
908}
909
a326a3de
DM
910/* As pp_write_text_to_stream, but for GraphViz HTML-like strings.
911
912 Flush the formatted text of pretty-printer PP onto the attached stream,
913 escaping these characters
914 " & < >
915 using XML escape sequences.
916
917 http://www.graphviz.org/doc/info/lang.html#html states:
918 special XML escape sequences for ", &, <, and > may be necessary in
919 order to embed these characters in attribute values or raw text
920 This doesn't list "'" (which would normally be escaped in XML
921 as "&apos;" or in HTML as "&#39;");.
922
923 Experiments show that escaping "'" doesn't seem to be necessary. */
924
925void
926pp_write_text_as_html_like_dot_to_stream (pretty_printer *pp)
927{
928 const char *text = pp_formatted_text (pp);
929 const char *p = text;
930 FILE *fp = pp_buffer (pp)->stream;
931
932 for (;*p; p++)
933 {
934 switch (*p)
935 {
936 case '"':
937 fputs ("&quot;", fp);
938 break;
939 case '&':
940 fputs ("&amp;", fp);
941 break;
942 case '<':
943 fputs ("&lt;", fp);
944 break;
945 case '>':
946 fputs ("&gt;",fp);
947 break;
948
949 default:
950 fputc (*p, fp);
951 break;
952 }
953 }
954
955 pp_clear_output_area (pp);
956}
957
b6fe0bb8
GDR
958/* Wrap a text delimited by START and END into PRETTY-PRINTER. */
959static void
960pp_wrap_text (pretty_printer *pp, const char *start, const char *end)
961{
962 bool wrapping_line = pp_is_wrapping_line (pp);
963
964 while (start != end)
965 {
966 /* Dump anything bordered by whitespaces. */
967 {
968 const char *p = start;
969 while (p != end && !ISBLANK (*p) && *p != '\n')
970 ++p;
971 if (wrapping_line
972 && p - start >= pp_remaining_character_count_for_line (pp))
973 pp_newline (pp);
974 pp_append_text (pp, start, p);
975 start = p;
976 }
977
978 if (start != end && ISBLANK (*start))
979 {
980 pp_space (pp);
981 ++start;
982 }
983 if (start != end && *start == '\n')
984 {
985 pp_newline (pp);
986 ++start;
987 }
988 }
989}
990
991/* Same as pp_wrap_text but wrap text only when in line-wrapping mode. */
992static inline void
993pp_maybe_wrap_text (pretty_printer *pp, const char *start, const char *end)
994{
995 if (pp_is_wrapping_line (pp))
996 pp_wrap_text (pp, start, end);
997 else
998 pp_append_text (pp, start, end);
999}
1000
1001/* Append to the output area of PRETTY-PRINTER a string specified by its
1002 STARTing character and LENGTH. */
1003static inline void
1004pp_append_r (pretty_printer *pp, const char *start, int length)
1005{
c4100eae 1006 output_buffer_append_r (pp_buffer (pp), start, length);
b6fe0bb8
GDR
1007}
1008
4b780675
GDR
1009/* Insert enough spaces into the output area of PRETTY-PRINTER to bring
1010 the column position to the current indentation level, assuming that a
1011 newline has just been written to the buffer. */
1012void
b066401f 1013pp_indent (pretty_printer *pp)
4b780675
GDR
1014{
1015 int n = pp_indentation (pp);
1016 int i;
1017
1018 for (i = 0; i < n; ++i)
1019 pp_space (pp);
1020}
1021
39ce81c9 1022/* The following format specifiers are recognized as being client independent:
b6fe0bb8
GDR
1023 %d, %i: (signed) integer in base ten.
1024 %u: unsigned integer in base ten.
1025 %o: unsigned integer in base eight.
1026 %x: unsigned integer in base sixteen.
1027 %ld, %li, %lo, %lu, %lx: long versions of the above.
1028 %lld, %lli, %llo, %llu, %llx: long long versions.
1029 %wd, %wi, %wo, %wu, %wx: HOST_WIDE_INT versions.
204839e7 1030 %f: double
b6fe0bb8
GDR
1031 %c: character.
1032 %s: string.
914bc2b9 1033 %p: pointer (printed in a host-dependent manner).
4b84d650
JJ
1034 %r: if pp_show_color(pp), switch to color identified by const char *.
1035 %R: if pp_show_color(pp), reset color.
b6fe0bb8 1036 %m: strerror(text->err_no) - does not consume a value from args_ptr.
a668adb2 1037 %%: '%'.
ca09cd34
JM
1038 %<: opening quote.
1039 %>: closing quote.
1040 %': apostrophe (should only be used in untranslated messages;
1041 translations should use appropriate punctuation directly).
39ce81c9
ZW
1042 %.*s: a substring the length of which is specified by an argument
1043 integer.
1044 %Ns: likewise, but length specified as constant in the format string.
39ce81c9 1045 Flag 'q': quote formatted text (must come immediately after '%').
975672f3
PK
1046 %Z: Requires two arguments - array of int, and len. Prints elements
1047 of the array.
39ce81c9
ZW
1048
1049 Arguments can be used sequentially, or through %N$ resp. *N$
1050 notation Nth argument after the format string. If %N$ / *N$
1051 notation is used, it must be used for all arguments, except %m, %%,
1052 %<, %> and %', which may not have a number, as they do not consume
1053 an argument. When %M$.*N$s is used, M must be N + 1. (This may
1054 also be written %M$.*s, provided N is not otherwise used.) The
1055 format string must have conversion specifiers with argument numbers
1056 1 up to highest argument; each argument may only be used once.
1057 A format string can have at most 30 arguments. */
1058
1059/* Formatting phases 1 and 2: render TEXT->format_spec plus
025311c4 1060 TEXT->args_ptr into a series of chunks in pp_buffer (PP)->args[].
4ccab56d 1061 Phase 3 is in pp_output_formatted_text. */
39ce81c9 1062
b6fe0bb8 1063void
b066401f 1064pp_format (pretty_printer *pp, text_info *text)
b6fe0bb8 1065{
025311c4 1066 output_buffer *buffer = pp_buffer (pp);
39ce81c9
ZW
1067 const char *p;
1068 const char **args;
1069 struct chunk_info *new_chunk_array;
1070
1071 unsigned int curarg = 0, chunk = 0, argno;
1072 pp_wrapping_mode_t old_wrapping_mode;
1073 bool any_unnumbered = false, any_numbered = false;
1074 const char **formatters[PP_NL_ARGMAX];
1075
1076 /* Allocate a new chunk structure. */
1077 new_chunk_array = XOBNEW (&buffer->chunk_obstack, struct chunk_info);
1078 new_chunk_array->prev = buffer->cur_chunk_array;
1079 buffer->cur_chunk_array = new_chunk_array;
1080 args = new_chunk_array->args;
1081
1082 /* Formatting phase 1: split up TEXT->format_spec into chunks in
025311c4 1083 pp_buffer (PP)->args[]. Even-numbered chunks are to be output
39ce81c9
ZW
1084 verbatim, odd-numbered chunks are format specifiers.
1085 %m, %%, %<, %>, and %' are replaced with the appropriate text at
1086 this point. */
1087
1088 memset (formatters, 0, sizeof formatters);
b8698a0f 1089
39ce81c9 1090 for (p = text->format_spec; *p; )
b6fe0bb8 1091 {
39ce81c9
ZW
1092 while (*p != '\0' && *p != '%')
1093 {
1094 obstack_1grow (&buffer->chunk_obstack, *p);
1095 p++;
1096 }
b6fe0bb8 1097
39ce81c9
ZW
1098 if (*p == '\0')
1099 break;
1100
1101 switch (*++p)
1102 {
1103 case '\0':
1104 gcc_unreachable ();
b8698a0f 1105
39ce81c9
ZW
1106 case '%':
1107 obstack_1grow (&buffer->chunk_obstack, '%');
1108 p++;
1109 continue;
b6fe0bb8 1110
39ce81c9 1111 case '<':
4b84d650
JJ
1112 {
1113 obstack_grow (&buffer->chunk_obstack,
1114 open_quote, strlen (open_quote));
1115 const char *colorstr
1116 = colorize_start (pp_show_color (pp), "quote");
1117 obstack_grow (&buffer->chunk_obstack, colorstr, strlen (colorstr));
1118 p++;
1119 continue;
1120 }
39ce81c9
ZW
1121
1122 case '>':
4b84d650
JJ
1123 {
1124 const char *colorstr = colorize_stop (pp_show_color (pp));
1125 obstack_grow (&buffer->chunk_obstack, colorstr, strlen (colorstr));
1126 }
1127 /* FALLTHRU */
39ce81c9
ZW
1128 case '\'':
1129 obstack_grow (&buffer->chunk_obstack,
241de8a0 1130 close_quote, strlen (close_quote));
39ce81c9
ZW
1131 p++;
1132 continue;
1133
4b84d650
JJ
1134 case 'R':
1135 {
1136 const char *colorstr = colorize_stop (pp_show_color (pp));
1137 obstack_grow (&buffer->chunk_obstack, colorstr,
1138 strlen (colorstr));
1139 p++;
1140 continue;
1141 }
1142
39ce81c9
ZW
1143 case 'm':
1144 {
1145 const char *errstr = xstrerror (text->err_no);
1146 obstack_grow (&buffer->chunk_obstack, errstr, strlen (errstr));
1147 }
1148 p++;
1149 continue;
1150
1151 default:
1152 /* Handled in phase 2. Terminate the plain chunk here. */
1153 obstack_1grow (&buffer->chunk_obstack, '\0');
1154 gcc_assert (chunk < PP_NL_ARGMAX * 2);
1155 args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
1156 break;
1157 }
1158
1159 if (ISDIGIT (*p))
1160 {
1161 char *end;
1162 argno = strtoul (p, &end, 10) - 1;
1163 p = end;
1164 gcc_assert (*p == '$');
1165 p++;
1166
1167 any_numbered = true;
1168 gcc_assert (!any_unnumbered);
1169 }
1170 else
1171 {
1172 argno = curarg++;
1173 any_unnumbered = true;
1174 gcc_assert (!any_numbered);
1175 }
1176 gcc_assert (argno < PP_NL_ARGMAX);
1177 gcc_assert (!formatters[argno]);
1178 formatters[argno] = &args[chunk];
1179 do
1180 {
1181 obstack_1grow (&buffer->chunk_obstack, *p);
1182 p++;
1183 }
1184 while (strchr ("qwl+#", p[-1]));
1185
1186 if (p[-1] == '.')
1187 {
1188 /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
1189 (where M == N + 1). */
1190 if (ISDIGIT (*p))
1191 {
1192 do
1193 {
1194 obstack_1grow (&buffer->chunk_obstack, *p);
1195 p++;
1196 }
1197 while (ISDIGIT (p[-1]));
1198 gcc_assert (p[-1] == 's');
1199 }
1200 else
1201 {
1202 gcc_assert (*p == '*');
1203 obstack_1grow (&buffer->chunk_obstack, '*');
1204 p++;
1205
1206 if (ISDIGIT (*p))
1207 {
1208 char *end;
1209 unsigned int argno2 = strtoul (p, &end, 10) - 1;
1210 p = end;
1211 gcc_assert (argno2 == argno - 1);
1212 gcc_assert (!any_unnumbered);
1213 gcc_assert (*p == '$');
1214
1215 p++;
1216 formatters[argno2] = formatters[argno];
1217 }
1218 else
1219 {
1220 gcc_assert (!any_numbered);
1221 formatters[argno+1] = formatters[argno];
1222 curarg++;
1223 }
1224 gcc_assert (*p == 's');
1225 obstack_1grow (&buffer->chunk_obstack, 's');
1226 p++;
1227 }
1228 }
1229 if (*p == '\0')
b6fe0bb8
GDR
1230 break;
1231
39ce81c9
ZW
1232 obstack_1grow (&buffer->chunk_obstack, '\0');
1233 gcc_assert (chunk < PP_NL_ARGMAX * 2);
1234 args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
1235 }
1236
1237 obstack_1grow (&buffer->chunk_obstack, '\0');
1238 gcc_assert (chunk < PP_NL_ARGMAX * 2);
1239 args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
1240 args[chunk] = 0;
b8698a0f 1241
39ce81c9
ZW
1242 /* Set output to the argument obstack, and switch line-wrapping and
1243 prefixing off. */
1244 buffer->obstack = &buffer->chunk_obstack;
e9c9a142 1245 const int old_line_length = buffer->line_length;
39ce81c9
ZW
1246 old_wrapping_mode = pp_set_verbatim_wrapping (pp);
1247
1248 /* Second phase. Replace each formatter with the formatted text it
1249 corresponds to. */
1250
1251 for (argno = 0; formatters[argno]; argno++)
1252 {
1253 int precision = 0;
1254 bool wide = false;
1255 bool plus = false;
1256 bool hash = false;
1257 bool quote = false;
1258
1259 /* We do not attempt to enforce any ordering on the modifier
1260 characters. */
1261
1262 for (p = *formatters[argno];; p++)
a668adb2 1263 {
39ce81c9
ZW
1264 switch (*p)
1265 {
1266 case 'q':
1267 gcc_assert (!quote);
1268 quote = true;
1269 continue;
1270
1271 case '+':
1272 gcc_assert (!plus);
1273 plus = true;
1274 continue;
1275
1276 case '#':
1277 gcc_assert (!hash);
1278 hash = true;
1279 continue;
1280
1281 case 'w':
1282 gcc_assert (!wide);
1283 wide = true;
1284 continue;
1285
1286 case 'l':
1287 /* We don't support precision beyond that of "long long". */
1288 gcc_assert (precision < 2);
1289 precision++;
1290 continue;
1291 }
1292 break;
a668adb2 1293 }
39ce81c9
ZW
1294
1295 gcc_assert (!wide || precision == 0);
1296
1297 if (quote)
ce95abc4 1298 pp_begin_quote (pp, pp_show_color (pp));
39ce81c9
ZW
1299
1300 switch (*p)
b6fe0bb8 1301 {
4b84d650
JJ
1302 case 'r':
1303 pp_string (pp, colorize_start (pp_show_color (pp),
1304 va_arg (*text->args_ptr,
1305 const char *)));
1306 break;
1307
b6fe0bb8 1308 case 'c':
3f0177e7
MS
1309 {
1310 /* When quoting, print alphanumeric, punctuation, and the space
1311 character unchanged, and all others in hexadecimal with the
1312 "\x" prefix. Otherwise print them all unchanged. */
1313 int chr = va_arg (*text->args_ptr, int);
1314 if (ISPRINT (chr) || !quote)
1315 pp_character (pp, chr);
1316 else
1317 {
1318 const char str [2] = { chr, '\0' };
1319 pp_quoted_string (pp, str, 1);
1320 }
1321 break;
1322 }
b6fe0bb8
GDR
1323
1324 case 'd':
1325 case 'i':
39ce81c9
ZW
1326 if (wide)
1327 pp_wide_integer (pp, va_arg (*text->args_ptr, HOST_WIDE_INT));
1328 else
1329 pp_integer_with_precision
1330 (pp, *text->args_ptr, precision, int, "d");
b6fe0bb8
GDR
1331 break;
1332
1333 case 'o':
39ce81c9
ZW
1334 if (wide)
1335 pp_scalar (pp, "%" HOST_WIDE_INT_PRINT "o",
1336 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
1337 else
1338 pp_integer_with_precision
1339 (pp, *text->args_ptr, precision, unsigned, "o");
b6fe0bb8
GDR
1340 break;
1341
1342 case 's':
3f0177e7
MS
1343 if (quote)
1344 pp_quoted_string (pp, va_arg (*text->args_ptr, const char *));
1345 else
1346 pp_string (pp, va_arg (*text->args_ptr, const char *));
b6fe0bb8
GDR
1347 break;
1348
39ce81c9
ZW
1349 case 'p':
1350 pp_pointer (pp, va_arg (*text->args_ptr, void *));
1351 break;
b6fe0bb8
GDR
1352
1353 case 'u':
39ce81c9
ZW
1354 if (wide)
1355 pp_scalar (pp, HOST_WIDE_INT_PRINT_UNSIGNED,
1356 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
1357 else
1358 pp_integer_with_precision
1359 (pp, *text->args_ptr, precision, unsigned, "u");
b6fe0bb8
GDR
1360 break;
1361
204839e7
DM
1362 case 'f':
1363 pp_double (pp, va_arg (*text->args_ptr, double));
1364 break;
1365
975672f3
PK
1366 case 'Z':
1367 {
1368 int *v = va_arg (*text->args_ptr, int *);
1369 unsigned len = va_arg (*text->args_ptr, unsigned);
1370
1371 for (unsigned i = 0; i < len; ++i)
1372 {
1373 pp_scalar (pp, "%i", v[i]);
1374 if (i < len - 1)
1375 {
1376 pp_comma (pp);
1377 pp_space (pp);
1378 }
1379 }
1380 break;
1381 }
1382
b6fe0bb8 1383 case 'x':
39ce81c9
ZW
1384 if (wide)
1385 pp_scalar (pp, HOST_WIDE_INT_PRINT_HEX,
1386 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
1387 else
1388 pp_integer_with_precision
1389 (pp, *text->args_ptr, precision, unsigned, "x");
b6fe0bb8
GDR
1390 break;
1391
b6fe0bb8
GDR
1392 case '.':
1393 {
1394 int n;
1395 const char *s;
d5706a1e 1396
39ce81c9
ZW
1397 /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
1398 (where M == N + 1). The format string should be verified
1399 already from the first phase. */
1400 p++;
1401 if (ISDIGIT (*p))
1402 {
1403 char *end;
1404 n = strtoul (p, &end, 10);
1405 p = end;
1406 gcc_assert (*p == 's');
1407 }
1408 else
1409 {
1410 gcc_assert (*p == '*');
1411 p++;
1412 gcc_assert (*p == 's');
1413 n = va_arg (*text->args_ptr, int);
1414
1415 /* This consumes a second entry in the formatters array. */
1416 gcc_assert (formatters[argno] == formatters[argno+1]);
1417 argno++;
1418 }
1419
b6fe0bb8 1420 s = va_arg (*text->args_ptr, const char *);
0a8923fa 1421
86ef85d3
MS
1422 /* Append the lesser of precision and strlen (s) characters
1423 from the array (which need not be a nul-terminated string).
1424 Negative precision is treated as if it were omitted. */
1425 size_t len = n < 0 ? strlen (s) : strnlen (s, n);
0a8923fa
MS
1426
1427 pp_append_text (pp, s, s + len);
b6fe0bb8
GDR
1428 }
1429 break;
1430
1431 default:
0e61db61
NS
1432 {
1433 bool ok;
39ce81c9 1434
ce95abc4
DM
1435 /* Call the format decoder.
1436 Pass the address of "quote" so that format decoders can
1437 potentially disable printing of the closing quote
1438 (e.g. when printing "'TYPEDEF' aka 'TYPE'" in the C family
1439 of frontends). */
0e61db61 1440 gcc_assert (pp_format_decoder (pp));
39ce81c9 1441 ok = pp_format_decoder (pp) (pp, text, p,
ce95abc4 1442 precision, wide, plus, hash, &quote,
f012c8ef 1443 formatters[argno]);
0e61db61
NS
1444 gcc_assert (ok);
1445 }
b6fe0bb8 1446 }
39ce81c9
ZW
1447
1448 if (quote)
ce95abc4 1449 pp_end_quote (pp, pp_show_color (pp));
39ce81c9
ZW
1450
1451 obstack_1grow (&buffer->chunk_obstack, '\0');
1452 *formatters[argno] = XOBFINISH (&buffer->chunk_obstack, const char *);
b6fe0bb8 1453 }
39ce81c9 1454
b2b29377
MM
1455 if (CHECKING_P)
1456 for (; argno < PP_NL_ARGMAX; argno++)
1457 gcc_assert (!formatters[argno]);
39ce81c9 1458
f012c8ef
DM
1459 /* If the client supplied a postprocessing object, call its "handle"
1460 hook here. */
1461 if (pp->m_format_postprocessor)
1462 pp->m_format_postprocessor->handle (pp);
1463
39ce81c9
ZW
1464 /* Revert to normal obstack and wrapping mode. */
1465 buffer->obstack = &buffer->formatted_obstack;
e9c9a142 1466 buffer->line_length = old_line_length;
39ce81c9
ZW
1467 pp_wrapping_mode (pp) = old_wrapping_mode;
1468 pp_clear_state (pp);
1469}
1470
1471/* Format of a message pointed to by TEXT. */
1472void
b066401f 1473pp_output_formatted_text (pretty_printer *pp)
39ce81c9
ZW
1474{
1475 unsigned int chunk;
1476 output_buffer *buffer = pp_buffer (pp);
1477 struct chunk_info *chunk_array = buffer->cur_chunk_array;
1478 const char **args = chunk_array->args;
1479
1480 gcc_assert (buffer->obstack == &buffer->formatted_obstack);
39ce81c9 1481
b066401f 1482 /* This is a third phase, first 2 phases done in pp_format_args.
39ce81c9
ZW
1483 Now we actually print it. */
1484 for (chunk = 0; args[chunk]; chunk++)
1485 pp_string (pp, args[chunk]);
1486
1487 /* Deallocate the chunk structure and everything after it (i.e. the
1488 associated series of formatted strings). */
1489 buffer->cur_chunk_array = chunk_array->prev;
1490 obstack_free (&buffer->chunk_obstack, chunk_array);
b6fe0bb8
GDR
1491}
1492
1493/* Helper subroutine of output_verbatim and verbatim. Do the appropriate
1494 settings needed by BUFFER for a verbatim formatting. */
1495void
b066401f 1496pp_format_verbatim (pretty_printer *pp, text_info *text)
b6fe0bb8 1497{
b6fe0bb8 1498 /* Set verbatim mode. */
39ce81c9
ZW
1499 pp_wrapping_mode_t oldmode = pp_set_verbatim_wrapping (pp);
1500
b6fe0bb8 1501 /* Do the actual formatting. */
39ce81c9
ZW
1502 pp_format (pp, text);
1503 pp_output_formatted_text (pp);
1504
b6fe0bb8 1505 /* Restore previous settings. */
39ce81c9 1506 pp_wrapping_mode (pp) = oldmode;
b6fe0bb8
GDR
1507}
1508
48749dbc
MLI
1509/* Flush the content of BUFFER onto the attached stream. This
1510 function does nothing unless pp->output_buffer->flush_p. */
b6fe0bb8 1511void
b066401f 1512pp_flush (pretty_printer *pp)
b6fe0bb8 1513{
48749dbc
MLI
1514 pp_clear_state (pp);
1515 if (!pp->buffer->flush_p)
1516 return;
b6fe0bb8 1517 pp_write_text_to_stream (pp);
48749dbc
MLI
1518 fflush (pp_buffer (pp)->stream);
1519}
1520
1521/* Flush the content of BUFFER onto the attached stream independently
1522 of the value of pp->output_buffer->flush_p. */
1523void
1524pp_really_flush (pretty_printer *pp)
1525{
b6fe0bb8 1526 pp_clear_state (pp);
48749dbc 1527 pp_write_text_to_stream (pp);
025311c4 1528 fflush (pp_buffer (pp)->stream);
b6fe0bb8
GDR
1529}
1530
1531/* Sets the number of maximum characters per line PRETTY-PRINTER can
1532 output in line-wrapping mode. A LENGTH value 0 suppresses
1533 line-wrapping. */
1534void
b066401f 1535pp_set_line_maximum_length (pretty_printer *pp, int length)
b6fe0bb8
GDR
1536{
1537 pp_line_cutoff (pp) = length;
1538 pp_set_real_maximum_length (pp);
1539}
1540
1541/* Clear PRETTY-PRINTER output area text info. */
1542void
b066401f 1543pp_clear_output_area (pretty_printer *pp)
b6fe0bb8 1544{
025311c4
GDR
1545 obstack_free (pp_buffer (pp)->obstack,
1546 obstack_base (pp_buffer (pp)->obstack));
1547 pp_buffer (pp)->line_length = 0;
b6fe0bb8
GDR
1548}
1549
653fee19
DM
1550/* Set PREFIX for PRETTY-PRINTER, taking ownership of PREFIX, which
1551 will eventually be free-ed. */
1552
b6fe0bb8 1553void
653fee19 1554pp_set_prefix (pretty_printer *pp, char *prefix)
b6fe0bb8 1555{
653fee19 1556 free (pp->prefix);
b6fe0bb8
GDR
1557 pp->prefix = prefix;
1558 pp_set_real_maximum_length (pp);
1559 pp->emitted_prefix = false;
1560 pp_indentation (pp) = 0;
1561}
1562
653fee19
DM
1563/* Take ownership of PP's prefix, setting it to NULL.
1564 This allows clients to save, overide, and then restore an existing
1565 prefix, without it being free-ed. */
1566
1567char *
1568pp_take_prefix (pretty_printer *pp)
1569{
1570 char *result = pp->prefix;
1571 pp->prefix = NULL;
1572 return result;
1573}
1574
b6fe0bb8
GDR
1575/* Free PRETTY-PRINTER's prefix, a previously malloc()'d string. */
1576void
b066401f 1577pp_destroy_prefix (pretty_printer *pp)
b6fe0bb8
GDR
1578{
1579 if (pp->prefix != NULL)
1580 {
653fee19 1581 free (pp->prefix);
b6fe0bb8
GDR
1582 pp->prefix = NULL;
1583 }
1584}
1585
1586/* Write out PRETTY-PRINTER's prefix. */
1587void
b066401f 1588pp_emit_prefix (pretty_printer *pp)
b6fe0bb8
GDR
1589{
1590 if (pp->prefix != NULL)
1591 {
1592 switch (pp_prefixing_rule (pp))
1593 {
1594 default:
1595 case DIAGNOSTICS_SHOW_PREFIX_NEVER:
1596 break;
1597
1598 case DIAGNOSTICS_SHOW_PREFIX_ONCE:
1599 if (pp->emitted_prefix)
1600 {
b066401f 1601 pp_indent (pp);
b6fe0bb8
GDR
1602 break;
1603 }
1604 pp_indentation (pp) += 3;
1605 /* Fall through. */
1606
1607 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
1608 {
1609 int prefix_length = strlen (pp->prefix);
1610 pp_append_r (pp, pp->prefix, prefix_length);
1611 pp->emitted_prefix = true;
1612 }
1613 break;
1614 }
1615 }
1616}
1617
653fee19 1618/* Construct a PRETTY-PRINTER of MAXIMUM_LENGTH characters per line. */
da6ca2b5 1619
653fee19 1620pretty_printer::pretty_printer (int maximum_length)
da6ca2b5
GDR
1621 : buffer (new (XCNEW (output_buffer)) output_buffer ()),
1622 prefix (),
1623 padding (pp_none),
1624 maximum_length (),
1625 indent_skip (),
1626 wrapping (),
1627 format_decoder (),
f012c8ef 1628 m_format_postprocessor (NULL),
da6ca2b5
GDR
1629 emitted_prefix (),
1630 need_newline (),
c3284718 1631 translate_identifiers (true),
d2608235
DM
1632 show_color (),
1633 show_urls (false)
b6fe0bb8 1634{
653fee19 1635 pp_line_cutoff (this) = maximum_length;
da6ca2b5
GDR
1636 /* By default, we emit prefixes once per message. */
1637 pp_prefixing_rule (this) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
653fee19 1638 pp_set_prefix (this, NULL);
b6fe0bb8
GDR
1639}
1640
368877a1
DM
1641/* Copy constructor for pretty_printer. */
1642
1643pretty_printer::pretty_printer (const pretty_printer &other)
1644: buffer (new (XCNEW (output_buffer)) output_buffer ()),
1645 prefix (),
1646 padding (other.padding),
1647 maximum_length (other.maximum_length),
1648 indent_skip (other.indent_skip),
1649 wrapping (other.wrapping),
1650 format_decoder (other.format_decoder),
1651 m_format_postprocessor (NULL),
1652 emitted_prefix (other.emitted_prefix),
1653 need_newline (other.need_newline),
1654 translate_identifiers (other.translate_identifiers),
1655 show_color (other.show_color),
1656 show_urls (other.show_urls)
1657{
1658 pp_line_cutoff (this) = maximum_length;
1659 /* By default, we emit prefixes once per message. */
1660 pp_prefixing_rule (this) = pp_prefixing_rule (&other);
1661 pp_set_prefix (this, NULL);
1662
1663 if (other.m_format_postprocessor)
1664 m_format_postprocessor = other.m_format_postprocessor->clone ();
1665}
1666
025311c4
GDR
1667pretty_printer::~pretty_printer ()
1668{
f012c8ef
DM
1669 if (m_format_postprocessor)
1670 delete m_format_postprocessor;
025311c4
GDR
1671 buffer->~output_buffer ();
1672 XDELETE (buffer);
653fee19 1673 free (prefix);
025311c4
GDR
1674}
1675
368877a1
DM
1676/* Base class implementation of pretty_printer::clone vfunc. */
1677
1678pretty_printer *
1679pretty_printer::clone () const
1680{
1681 return new pretty_printer (*this);
1682}
1683
b6fe0bb8
GDR
1684/* Append a string delimited by START and END to the output area of
1685 PRETTY-PRINTER. No line wrapping is done. However, if beginning a
1686 new line then emit PRETTY-PRINTER's prefix and skip any leading
1687 whitespace if appropriate. The caller must ensure that it is
1688 safe to do so. */
1689void
b066401f 1690pp_append_text (pretty_printer *pp, const char *start, const char *end)
b6fe0bb8
GDR
1691{
1692 /* Emit prefix and skip whitespace if we're starting a new line. */
025311c4 1693 if (pp_buffer (pp)->line_length == 0)
b6fe0bb8
GDR
1694 {
1695 pp_emit_prefix (pp);
1696 if (pp_is_wrapping_line (pp))
1697 while (start != end && *start == ' ')
1698 ++start;
1699 }
1700 pp_append_r (pp, start, end - start);
1701}
1702
1703/* Finishes constructing a NULL-terminated character string representing
1704 the PRETTY-PRINTED text. */
1705const char *
b066401f 1706pp_formatted_text (pretty_printer *pp)
b6fe0bb8 1707{
c4100eae 1708 return output_buffer_formatted_text (pp_buffer (pp));
b6fe0bb8
GDR
1709}
1710
1711/* Return a pointer to the last character emitted in PRETTY-PRINTER's
1712 output area. A NULL pointer means no character available. */
1713const char *
b066401f 1714pp_last_position_in_text (const pretty_printer *pp)
b6fe0bb8 1715{
c4100eae 1716 return output_buffer_last_position_in_text (pp_buffer (pp));
b6fe0bb8
GDR
1717}
1718
1719/* Return the amount of characters PRETTY-PRINTER can accept to
ba228239 1720 make a full line. Meaningful only in line-wrapping mode. */
b6fe0bb8 1721int
b066401f 1722pp_remaining_character_count_for_line (pretty_printer *pp)
b6fe0bb8 1723{
025311c4 1724 return pp->maximum_length - pp_buffer (pp)->line_length;
b6fe0bb8
GDR
1725}
1726
1727
1728/* Format a message into BUFFER a la printf. */
1729void
1730pp_printf (pretty_printer *pp, const char *msg, ...)
1731{
1732 text_info text;
1733 va_list ap;
1734
1735 va_start (ap, msg);
1736 text.err_no = errno;
1737 text.args_ptr = &ap;
1738 text.format_spec = msg;
39ce81c9
ZW
1739 pp_format (pp, &text);
1740 pp_output_formatted_text (pp);
b6fe0bb8
GDR
1741 va_end (ap);
1742}
1743
1744
1745/* Output MESSAGE verbatim into BUFFER. */
1746void
1747pp_verbatim (pretty_printer *pp, const char *msg, ...)
1748{
1749 text_info text;
1750 va_list ap;
1751
1752 va_start (ap, msg);
1753 text.err_no = errno;
1754 text.args_ptr = &ap;
1755 text.format_spec = msg;
1756 pp_format_verbatim (pp, &text);
1757 va_end (ap);
1758}
1759
1760
1761
1762/* Have PRETTY-PRINTER start a new line. */
1763void
b066401f 1764pp_newline (pretty_printer *pp)
b6fe0bb8 1765{
025311c4 1766 obstack_1grow (pp_buffer (pp)->obstack, '\n');
c4669594 1767 pp_needs_newline (pp) = false;
025311c4 1768 pp_buffer (pp)->line_length = 0;
b6fe0bb8
GDR
1769}
1770
1771/* Have PRETTY-PRINTER add a CHARACTER. */
1772void
b066401f 1773pp_character (pretty_printer *pp, int c)
b6fe0bb8
GDR
1774{
1775 if (pp_is_wrapping_line (pp)
ddd0fd17
LH
1776 /* If printing UTF-8, don't wrap in the middle of a sequence. */
1777 && (((unsigned int) c) & 0xC0) != 0x80
b6fe0bb8
GDR
1778 && pp_remaining_character_count_for_line (pp) <= 0)
1779 {
1780 pp_newline (pp);
1781 if (ISSPACE (c))
1782 return;
1783 }
025311c4
GDR
1784 obstack_1grow (pp_buffer (pp)->obstack, c);
1785 ++pp_buffer (pp)->line_length;
b6fe0bb8
GDR
1786}
1787
1788/* Append a STRING to the output area of PRETTY-PRINTER; the STRING may
1789 be line-wrapped if in appropriate mode. */
1790void
b066401f 1791pp_string (pretty_printer *pp, const char *str)
b6fe0bb8 1792{
41d9f1e0
MLI
1793 gcc_checking_assert (str);
1794 pp_maybe_wrap_text (pp, str, str + strlen (str));
b6fe0bb8
GDR
1795}
1796
3f0177e7
MS
1797/* Append the leading N characters of STRING to the output area of
1798 PRETTY-PRINTER, quoting in hexadecimal non-printable characters.
1799 Setting N = -1 is as if N were set to strlen (STRING). The STRING
1800 may be line-wrapped if in appropriate mode. */
1801static void
1802pp_quoted_string (pretty_printer *pp, const char *str, size_t n /* = -1 */)
1803{
1804 gcc_checking_assert (str);
1805
1806 const char *last = str;
1807 const char *ps;
1808
1809 /* Compute the length if not specified. */
1810 if (n == (size_t) -1)
1811 n = strlen (str);
1812
1813 for (ps = str; n; ++ps, --n)
1814 {
1815 if (ISPRINT (*ps))
1816 continue;
1817
ddd0fd17
LH
1818 /* Don't escape a valid UTF-8 extended char. */
1819 const unsigned char *ups = (const unsigned char *) ps;
1820 if (*ups & 0x80)
1821 {
1822 unsigned int extended_char;
1823 const int valid_utf8_len = decode_utf8_char (ups, n, &extended_char);
1824 if (valid_utf8_len > 0)
1825 {
1826 ps += valid_utf8_len - 1;
1827 n -= valid_utf8_len - 1;
1828 continue;
1829 }
1830 }
1831
3f0177e7 1832 if (last < ps)
ddd0fd17 1833 pp_maybe_wrap_text (pp, last, ps);
3f0177e7
MS
1834
1835 /* Append the hexadecimal value of the character. Allocate a buffer
1836 that's large enough for a 32-bit char plus the hex prefix. */
1837 char buf [11];
1838 int n = sprintf (buf, "\\x%02x", (unsigned char)*ps);
1839 pp_maybe_wrap_text (pp, buf, buf + n);
1840 last = ps + 1;
1841 }
1842
1843 pp_maybe_wrap_text (pp, last, ps);
1844}
1845
471854f8 1846/* Maybe print out a whitespace if needed. */
b6fe0bb8 1847
b9b44fb9 1848void
b066401f 1849pp_maybe_space (pretty_printer *pp)
b9b44fb9 1850{
b066401f 1851 if (pp->padding != pp_none)
b9b44fb9
GDR
1852 {
1853 pp_space (pp);
b066401f 1854 pp->padding = pp_none;
b9b44fb9
GDR
1855 }
1856}
0fc80001
GDR
1857
1858// Add a newline to the pretty printer PP and flush formatted text.
1859
1860void
1861pp_newline_and_flush (pretty_printer *pp)
1862{
1863 pp_newline (pp);
1864 pp_flush (pp);
1865 pp_needs_newline (pp) = false;
1866}
1867
1868// Add a newline to the pretty printer PP, followed by indentation.
1869
1870void
1871pp_newline_and_indent (pretty_printer *pp, int n)
1872{
1873 pp_indentation (pp) += n;
1874 pp_newline (pp);
1875 pp_indent (pp);
1876 pp_needs_newline (pp) = false;
1877}
1878
1879// Add separator C, followed by a single whitespace.
1880
1881void
1882pp_separate_with (pretty_printer *pp, char c)
1883{
1884 pp_character (pp, c);
1885 pp_space (pp);
1886}
1887
ce95abc4
DM
1888/* Add a localized open quote, and if SHOW_COLOR is true, begin colorizing
1889 using the "quote" color. */
1890
1891void
1892pp_begin_quote (pretty_printer *pp, bool show_color)
1893{
1894 pp_string (pp, open_quote);
1895 pp_string (pp, colorize_start (show_color, "quote"));
1896}
1897
1898/* If SHOW_COLOR is true, stop colorizing.
1899 Add a localized close quote. */
1900
1901void
1902pp_end_quote (pretty_printer *pp, bool show_color)
1903{
1904 pp_string (pp, colorize_stop (show_color));
1905 pp_string (pp, close_quote);
1906}
1907
a3af5087
JM
1908\f
1909/* The string starting at P has LEN (at least 1) bytes left; if they
1910 start with a valid UTF-8 sequence, return the length of that
1911 sequence and set *VALUE to the value of that sequence, and
1912 otherwise return 0 and set *VALUE to (unsigned int) -1. */
1913
1914static int
1915decode_utf8_char (const unsigned char *p, size_t len, unsigned int *value)
1916{
1917 unsigned int t = *p;
1918
1919 if (len == 0)
1920 abort ();
1921 if (t & 0x80)
1922 {
1923 size_t utf8_len = 0;
1924 unsigned int ch;
1925 size_t i;
1926 for (t = *p; t & 0x80; t <<= 1)
1927 utf8_len++;
1928
1929 if (utf8_len > len || utf8_len < 2 || utf8_len > 6)
1930 {
1931 *value = (unsigned int) -1;
1932 return 0;
1933 }
1934 ch = *p & ((1 << (7 - utf8_len)) - 1);
1935 for (i = 1; i < utf8_len; i++)
1936 {
1937 unsigned int u = p[i];
1938 if ((u & 0xC0) != 0x80)
1939 {
1940 *value = (unsigned int) -1;
1941 return 0;
1942 }
1943 ch = (ch << 6) | (u & 0x3F);
1944 }
1945 if ( (ch <= 0x7F && utf8_len > 1)
1946 || (ch <= 0x7FF && utf8_len > 2)
1947 || (ch <= 0xFFFF && utf8_len > 3)
1948 || (ch <= 0x1FFFFF && utf8_len > 4)
1949 || (ch <= 0x3FFFFFF && utf8_len > 5)
1950 || (ch >= 0xD800 && ch <= 0xDFFF))
1951 {
1952 *value = (unsigned int) -1;
1953 return 0;
1954 }
1955 *value = ch;
1956 return utf8_len;
1957 }
1958 else
1959 {
1960 *value = t;
1961 return 1;
1962 }
1963}
1964
ab9b814d
JM
1965/* Allocator for identifier_to_locale and corresponding function to
1966 free memory. */
1967
1968void *(*identifier_to_locale_alloc) (size_t) = xmalloc;
1969void (*identifier_to_locale_free) (void *) = free;
1970
a3af5087
JM
1971/* Given IDENT, an identifier in the internal encoding, return a
1972 version of IDENT suitable for diagnostics in the locale character
ab9b814d
JM
1973 set: either IDENT itself, or a string, allocated using
1974 identifier_to_locale_alloc, converted to the locale character set
1975 and using escape sequences if not representable in the locale
1976 character set or containing control characters or invalid byte
1977 sequences. Existing backslashes in IDENT are not doubled, so the
1978 result may not uniquely specify the contents of an arbitrary byte
1979 sequence identifier. */
a3af5087
JM
1980
1981const char *
1982identifier_to_locale (const char *ident)
1983{
1984 const unsigned char *uid = (const unsigned char *) ident;
1985 size_t idlen = strlen (ident);
1986 bool valid_printable_utf8 = true;
1987 bool all_ascii = true;
1988 size_t i;
1989
1990 for (i = 0; i < idlen;)
1991 {
1992 unsigned int c;
1993 size_t utf8_len = decode_utf8_char (&uid[i], idlen - i, &c);
1994 if (utf8_len == 0 || c <= 0x1F || (c >= 0x7F && c <= 0x9F))
1995 {
1996 valid_printable_utf8 = false;
1997 break;
1998 }
1999 if (utf8_len > 1)
2000 all_ascii = false;
2001 i += utf8_len;
2002 }
2003
2004 /* If IDENT contains invalid UTF-8 sequences (which may occur with
2005 attributes putting arbitrary byte sequences in identifiers), or
2006 control characters, we use octal escape sequences for all bytes
2007 outside printable ASCII. */
2008 if (!valid_printable_utf8)
2009 {
ab9b814d 2010 char *ret = (char *) identifier_to_locale_alloc (4 * idlen + 1);
a3af5087
JM
2011 char *p = ret;
2012 for (i = 0; i < idlen; i++)
2013 {
2014 if (uid[i] > 0x1F && uid[i] < 0x7F)
2015 *p++ = uid[i];
2016 else
2017 {
2018 sprintf (p, "\\%03o", uid[i]);
2019 p += 4;
2020 }
2021 }
2022 *p = 0;
2023 return ret;
2024 }
2025
2026 /* Otherwise, if it is valid printable ASCII, or printable UTF-8
2027 with the locale character set being UTF-8, IDENT is used. */
2028 if (all_ascii || locale_utf8)
2029 return ident;
2030
2031 /* Otherwise IDENT is converted to the locale character set if
2032 possible. */
2033#if defined ENABLE_NLS && defined HAVE_LANGINFO_CODESET && HAVE_ICONV
2034 if (locale_encoding != NULL)
2035 {
2036 iconv_t cd = iconv_open (locale_encoding, "UTF-8");
2037 bool conversion_ok = true;
2038 char *ret = NULL;
2039 if (cd != (iconv_t) -1)
2040 {
2041 size_t ret_alloc = 4 * idlen + 1;
2042 for (;;)
2043 {
2044 /* Repeat the whole conversion process as needed with
2045 larger buffers so non-reversible transformations can
2046 always be detected. */
2047 ICONV_CONST char *inbuf = CONST_CAST (char *, ident);
2048 char *outbuf;
2049 size_t inbytesleft = idlen;
2050 size_t outbytesleft = ret_alloc - 1;
2051 size_t iconv_ret;
2052
ab9b814d 2053 ret = (char *) identifier_to_locale_alloc (ret_alloc);
a3af5087
JM
2054 outbuf = ret;
2055
2056 if (iconv (cd, 0, 0, 0, 0) == (size_t) -1)
2057 {
2058 conversion_ok = false;
2059 break;
2060 }
2061
2062 iconv_ret = iconv (cd, &inbuf, &inbytesleft,
2063 &outbuf, &outbytesleft);
2064 if (iconv_ret == (size_t) -1 || inbytesleft != 0)
2065 {
2066 if (errno == E2BIG)
2067 {
2068 ret_alloc *= 2;
ab9b814d 2069 identifier_to_locale_free (ret);
a3af5087
JM
2070 ret = NULL;
2071 continue;
2072 }
2073 else
2074 {
2075 conversion_ok = false;
2076 break;
2077 }
2078 }
2079 else if (iconv_ret != 0)
2080 {
2081 conversion_ok = false;
2082 break;
2083 }
2084 /* Return to initial shift state. */
2085 if (iconv (cd, 0, 0, &outbuf, &outbytesleft) == (size_t) -1)
2086 {
2087 if (errno == E2BIG)
2088 {
2089 ret_alloc *= 2;
ab9b814d 2090 identifier_to_locale_free (ret);
a3af5087
JM
2091 ret = NULL;
2092 continue;
2093 }
2094 else
2095 {
2096 conversion_ok = false;
2097 break;
2098 }
2099 }
2100 *outbuf = 0;
2101 break;
2102 }
2103 iconv_close (cd);
2104 if (conversion_ok)
2105 return ret;
2106 }
2107 }
2108#endif
2109
2110 /* Otherwise, convert non-ASCII characters in IDENT to UCNs. */
2111 {
ab9b814d 2112 char *ret = (char *) identifier_to_locale_alloc (10 * idlen + 1);
a3af5087
JM
2113 char *p = ret;
2114 for (i = 0; i < idlen;)
2115 {
2116 unsigned int c;
2117 size_t utf8_len = decode_utf8_char (&uid[i], idlen - i, &c);
2118 if (utf8_len == 1)
2119 *p++ = uid[i];
2120 else
2121 {
2122 sprintf (p, "\\U%08x", c);
2123 p += 10;
2124 }
2125 i += utf8_len;
2126 }
2127 *p = 0;
2128 return ret;
2129 }
2130}
4ccab56d 2131
d2608235
DM
2132/* Support for encoding URLs.
2133 See egmontkob/Hyperlinks_in_Terminal_Emulators.md
2134 ( https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda ).
2135
2136 > A hyperlink is opened upon encountering an OSC 8 escape sequence with
2137 > the target URI. The syntax is
2138 >
2139 > OSC 8 ; params ; URI ST
2140 >
2141 > A hyperlink is closed with the same escape sequence, omitting the
2142 > parameters and the URI but keeping the separators:
2143 >
2144 > OSC 8 ; ; ST
2145 >
ae169f9e
TB
2146 > OSC (operating system command) is typically ESC ].
2147
2148 Use BEL instead of ST, as that is currently rendered better in some
2149 terminal emulators that don't support OSC 8, like konsole. */
d2608235
DM
2150
2151/* If URL-printing is enabled, write an "open URL" escape sequence to PP
2152 for the given URL. */
2153
2154void
2155pp_begin_url (pretty_printer *pp, const char *url)
2156{
2157 if (pp->show_urls)
ae169f9e 2158 pp_printf (pp, "\33]8;;%s\a", url);
d2608235
DM
2159}
2160
2161/* If URL-printing is enabled, write a "close URL" escape sequence to PP. */
2162
2163void
2164pp_end_url (pretty_printer *pp)
2165{
2166 if (pp->show_urls)
ae169f9e 2167 pp_string (pp, "\33]8;;\a");
d2608235
DM
2168}
2169
4ccab56d
DM
2170#if CHECKING_P
2171
2172namespace selftest {
2173
2174/* Smoketest for pretty_printer. */
2175
2176static void
2177test_basic_printing ()
2178{
2179 pretty_printer pp;
2180 pp_string (&pp, "hello");
2181 pp_space (&pp);
2182 pp_string (&pp, "world");
2183
2184 ASSERT_STREQ ("hello world", pp_formatted_text (&pp));
2185}
2186
2187/* Helper function for testing pp_format.
2188 Verify that pp_format (FMT, ...) followed by pp_output_formatted_text
2189 prints EXPECTED, assuming that pp_show_color is SHOW_COLOR. */
2190
2191static void
09765e3a
DM
2192assert_pp_format_va (const location &loc, const char *expected,
2193 bool show_color, const char *fmt, va_list *ap)
4ccab56d
DM
2194{
2195 pretty_printer pp;
2196 text_info ti;
2197 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
2198
2199 ti.format_spec = fmt;
2200 ti.args_ptr = ap;
2201 ti.err_no = 0;
2202 ti.x_data = NULL;
2203 ti.m_richloc = &rich_loc;
2204
2205 pp_show_color (&pp) = show_color;
2206 pp_format (&pp, &ti);
2207 pp_output_formatted_text (&pp);
09765e3a 2208 ASSERT_STREQ_AT (loc, expected, pp_formatted_text (&pp));
4ccab56d
DM
2209}
2210
2211/* Verify that pp_format (FMT, ...) followed by pp_output_formatted_text
2212 prints EXPECTED, with show_color disabled. */
2213
2214static void
09765e3a
DM
2215assert_pp_format (const location &loc, const char *expected,
2216 const char *fmt, ...)
4ccab56d
DM
2217{
2218 va_list ap;
2219
2220 va_start (ap, fmt);
09765e3a 2221 assert_pp_format_va (loc, expected, false, fmt, &ap);
4ccab56d
DM
2222 va_end (ap);
2223}
2224
2225/* As above, but with colorization enabled. */
2226
2227static void
09765e3a
DM
2228assert_pp_format_colored (const location &loc, const char *expected,
2229 const char *fmt, ...)
4ccab56d 2230{
2fe00b1f
DM
2231 /* The tests of colorization assume the default color scheme.
2232 If GCC_COLORS is set, then the colors have potentially been
2233 overridden; skip the test. */
2234 if (getenv ("GCC_COLORS"))
2235 return;
2236
4ccab56d
DM
2237 va_list ap;
2238
2239 va_start (ap, fmt);
09765e3a 2240 assert_pp_format_va (loc, expected, true, fmt, &ap);
4ccab56d
DM
2241 va_end (ap);
2242}
2243
09765e3a
DM
2244/* Helper function for calling testing pp_format,
2245 by calling assert_pp_format with various numbers of arguments.
2246 These exist mostly to avoid having to write SELFTEST_LOCATION
2247 throughout test_pp_format. */
2248
2249#define ASSERT_PP_FORMAT_1(EXPECTED, FMT, ARG1) \
2250 SELFTEST_BEGIN_STMT \
2251 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
2252 (ARG1)); \
2253 SELFTEST_END_STMT
2254
2255#define ASSERT_PP_FORMAT_2(EXPECTED, FMT, ARG1, ARG2) \
2256 SELFTEST_BEGIN_STMT \
2257 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
2258 (ARG1), (ARG2)); \
2259 SELFTEST_END_STMT
2260
2261#define ASSERT_PP_FORMAT_3(EXPECTED, FMT, ARG1, ARG2, ARG3) \
2262 SELFTEST_BEGIN_STMT \
2263 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
2264 (ARG1), (ARG2), (ARG3)); \
2265 SELFTEST_END_STMT
2266
4ccab56d
DM
2267/* Verify that pp_format works, for various format codes. */
2268
2269static void
2270test_pp_format ()
2271{
2272 /* Avoid introducing locale-specific differences in the results
2273 by hardcoding open_quote and close_quote. */
dbf96d49 2274 auto_fix_quotes fix_quotes;
4ccab56d
DM
2275
2276 /* Verify that plain text is passed through unchanged. */
09765e3a 2277 assert_pp_format (SELFTEST_LOCATION, "unformatted", "unformatted");
4ccab56d
DM
2278
2279 /* Verify various individual format codes, in the order listed in the
2280 comment for pp_format above. For each code, we append a second
2281 argument with a known bit pattern (0x12345678), to ensure that we
2282 are consuming arguments correctly. */
09765e3a
DM
2283 ASSERT_PP_FORMAT_2 ("-27 12345678", "%d %x", -27, 0x12345678);
2284 ASSERT_PP_FORMAT_2 ("-5 12345678", "%i %x", -5, 0x12345678);
2285 ASSERT_PP_FORMAT_2 ("10 12345678", "%u %x", 10, 0x12345678);
2286 ASSERT_PP_FORMAT_2 ("17 12345678", "%o %x", 15, 0x12345678);
2287 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%x %x", 0xcafebabe, 0x12345678);
2288 ASSERT_PP_FORMAT_2 ("-27 12345678", "%ld %x", (long)-27, 0x12345678);
2289 ASSERT_PP_FORMAT_2 ("-5 12345678", "%li %x", (long)-5, 0x12345678);
2290 ASSERT_PP_FORMAT_2 ("10 12345678", "%lu %x", (long)10, 0x12345678);
2291 ASSERT_PP_FORMAT_2 ("17 12345678", "%lo %x", (long)15, 0x12345678);
2292 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%lx %x", (long)0xcafebabe,
2293 0x12345678);
2294 ASSERT_PP_FORMAT_2 ("-27 12345678", "%lld %x", (long long)-27, 0x12345678);
2295 ASSERT_PP_FORMAT_2 ("-5 12345678", "%lli %x", (long long)-5, 0x12345678);
2296 ASSERT_PP_FORMAT_2 ("10 12345678", "%llu %x", (long long)10, 0x12345678);
2297 ASSERT_PP_FORMAT_2 ("17 12345678", "%llo %x", (long long)15, 0x12345678);
2298 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%llx %x", (long long)0xcafebabe,
2299 0x12345678);
2300 ASSERT_PP_FORMAT_2 ("-27 12345678", "%wd %x", (HOST_WIDE_INT)-27, 0x12345678);
2301 ASSERT_PP_FORMAT_2 ("-5 12345678", "%wi %x", (HOST_WIDE_INT)-5, 0x12345678);
2302 ASSERT_PP_FORMAT_2 ("10 12345678", "%wu %x", (unsigned HOST_WIDE_INT)10,
2303 0x12345678);
2304 ASSERT_PP_FORMAT_2 ("17 12345678", "%wo %x", (HOST_WIDE_INT)15, 0x12345678);
2305 ASSERT_PP_FORMAT_2 ("0xcafebabe 12345678", "%wx %x", (HOST_WIDE_INT)0xcafebabe,
2306 0x12345678);
204839e7 2307 ASSERT_PP_FORMAT_2 ("1.000000 12345678", "%f %x", 1.0, 0x12345678);
09765e3a
DM
2308 ASSERT_PP_FORMAT_2 ("A 12345678", "%c %x", 'A', 0x12345678);
2309 ASSERT_PP_FORMAT_2 ("hello world 12345678", "%s %x", "hello world",
2310 0x12345678);
86ef85d3
MS
2311
2312 /* Not nul-terminated. */
2313 char arr[5] = { '1', '2', '3', '4', '5' };
2314 ASSERT_PP_FORMAT_3 ("123 12345678", "%.*s %x", 3, arr, 0x12345678);
2315 ASSERT_PP_FORMAT_3 ("1234 12345678", "%.*s %x", -1, "1234", 0x12345678);
2316 ASSERT_PP_FORMAT_3 ("12345 12345678", "%.*s %x", 7, "12345", 0x12345678);
2317
914bc2b9
DM
2318 /* We can't test for %p; the pointer is printed in an implementation-defined
2319 manner. */
09765e3a
DM
2320 ASSERT_PP_FORMAT_2 ("normal colored normal 12345678",
2321 "normal %rcolored%R normal %x",
2322 "error", 0x12345678);
4ccab56d 2323 assert_pp_format_colored
09765e3a
DM
2324 (SELFTEST_LOCATION,
2325 "normal \33[01;31m\33[Kcolored\33[m\33[K normal 12345678",
4ccab56d
DM
2326 "normal %rcolored%R normal %x", "error", 0x12345678);
2327 /* TODO:
2328 %m: strerror(text->err_no) - does not consume a value from args_ptr. */
09765e3a
DM
2329 ASSERT_PP_FORMAT_1 ("% 12345678", "%% %x", 0x12345678);
2330 ASSERT_PP_FORMAT_1 ("` 12345678", "%< %x", 0x12345678);
2331 ASSERT_PP_FORMAT_1 ("' 12345678", "%> %x", 0x12345678);
2332 ASSERT_PP_FORMAT_1 ("' 12345678", "%' %x", 0x12345678);
2333 ASSERT_PP_FORMAT_3 ("abc 12345678", "%.*s %x", 3, "abcdef", 0x12345678);
2334 ASSERT_PP_FORMAT_2 ("abc 12345678", "%.3s %x", "abcdef", 0x12345678);
4ccab56d
DM
2335
2336 /* Verify flag 'q'. */
09765e3a
DM
2337 ASSERT_PP_FORMAT_2 ("`foo' 12345678", "%qs %x", "foo", 0x12345678);
2338 assert_pp_format_colored (SELFTEST_LOCATION,
2339 "`\33[01m\33[Kfoo\33[m\33[K' 12345678", "%qs %x",
4ccab56d
DM
2340 "foo", 0x12345678);
2341
975672f3
PK
2342 /* Verify %Z. */
2343 int v[] = { 1, 2, 3 };
2344 ASSERT_PP_FORMAT_3 ("1, 2, 3 12345678", "%Z %x", v, 3, 0x12345678);
2345
2346 int v2[] = { 0 };
2347 ASSERT_PP_FORMAT_3 ("0 12345678", "%Z %x", v2, 1, 0x12345678);
2348
4ccab56d 2349 /* Verify that combinations work, along with unformatted text. */
09765e3a
DM
2350 assert_pp_format (SELFTEST_LOCATION,
2351 "the quick brown fox jumps over the lazy dog",
4ccab56d
DM
2352 "the %s %s %s jumps over the %s %s",
2353 "quick", "brown", "fox", "lazy", "dog");
09765e3a
DM
2354 assert_pp_format (SELFTEST_LOCATION, "item 3 of 7", "item %i of %i", 3, 7);
2355 assert_pp_format (SELFTEST_LOCATION, "problem with `bar' at line 10",
4ccab56d 2356 "problem with %qs at line %i", "bar", 10);
4ccab56d
DM
2357}
2358
553a316b
DM
2359/* A subclass of pretty_printer for use by test_prefixes_and_wrapping. */
2360
2361class test_pretty_printer : public pretty_printer
2362{
2363 public:
2364 test_pretty_printer (enum diagnostic_prefixing_rule_t rule,
2365 int max_line_length)
2366 {
2367 pp_set_prefix (this, xstrdup ("PREFIX: "));
2368 wrapping.rule = rule;
2369 pp_set_line_maximum_length (this, max_line_length);
2370 }
2371};
2372
2373/* Verify that the various values of enum diagnostic_prefixing_rule_t work
2374 as expected, with and without line wrapping. */
2375
2376static void
2377test_prefixes_and_wrapping ()
2378{
2379 /* Tests of the various prefixing rules, without wrapping.
2380 Newlines embedded in pp_string don't affect it; we have to
2381 explicitly call pp_newline. */
2382 {
2383 test_pretty_printer pp (DIAGNOSTICS_SHOW_PREFIX_ONCE, 0);
2384 pp_string (&pp, "the quick brown fox");
2385 pp_newline (&pp);
2386 pp_string (&pp, "jumps over the lazy dog");
2387 pp_newline (&pp);
2388 ASSERT_STREQ (pp_formatted_text (&pp),
2389 "PREFIX: the quick brown fox\n"
2390 " jumps over the lazy dog\n");
2391 }
2392 {
2393 test_pretty_printer pp (DIAGNOSTICS_SHOW_PREFIX_NEVER, 0);
2394 pp_string (&pp, "the quick brown fox");
2395 pp_newline (&pp);
2396 pp_string (&pp, "jumps over the lazy dog");
2397 pp_newline (&pp);
2398 ASSERT_STREQ (pp_formatted_text (&pp),
2399 "the quick brown fox\n"
2400 "jumps over the lazy dog\n");
2401 }
2402 {
2403 test_pretty_printer pp (DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE, 0);
2404 pp_string (&pp, "the quick brown fox");
2405 pp_newline (&pp);
2406 pp_string (&pp, "jumps over the lazy dog");
2407 pp_newline (&pp);
2408 ASSERT_STREQ (pp_formatted_text (&pp),
2409 "PREFIX: the quick brown fox\n"
2410 "PREFIX: jumps over the lazy dog\n");
2411 }
2412
2413 /* Tests of the various prefixing rules, with wrapping. */
2414 {
2415 test_pretty_printer pp (DIAGNOSTICS_SHOW_PREFIX_ONCE, 20);
2416 pp_string (&pp, "the quick brown fox jumps over the lazy dog");
2417 pp_newline (&pp);
2418 pp_string (&pp, "able was I ere I saw elba");
2419 pp_newline (&pp);
2420 ASSERT_STREQ (pp_formatted_text (&pp),
2421 "PREFIX: the quick \n"
2422 " brown fox jumps \n"
2423 " over the lazy \n"
2424 " dog\n"
2425 " able was I ere I \n"
2426 " saw elba\n");
2427 }
2428 {
2429 test_pretty_printer pp (DIAGNOSTICS_SHOW_PREFIX_NEVER, 20);
2430 pp_string (&pp, "the quick brown fox jumps over the lazy dog");
2431 pp_newline (&pp);
2432 pp_string (&pp, "able was I ere I saw elba");
2433 pp_newline (&pp);
2434 ASSERT_STREQ (pp_formatted_text (&pp),
2435 "the quick brown fox \n"
2436 "jumps over the lazy \n"
2437 "dog\n"
2438 "able was I ere I \n"
2439 "saw elba\n");
2440 }
2441 {
2442 test_pretty_printer pp (DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE, 20);
2443 pp_string (&pp, "the quick brown fox jumps over the lazy dog");
2444 pp_newline (&pp);
2445 pp_string (&pp, "able was I ere I saw elba");
2446 pp_newline (&pp);
2447 ASSERT_STREQ (pp_formatted_text (&pp),
2448 "PREFIX: the quick brown fox jumps over the lazy dog\n"
2449 "PREFIX: able was I ere I saw elba\n");
2450 }
2451
2452}
2453
d2608235
DM
2454/* Verify that URL-printing works as expected. */
2455
2456void
2457test_urls ()
2458{
2459 {
2460 pretty_printer pp;
2461 pp.show_urls = false;
2462 pp_begin_url (&pp, "http://example.com");
2463 pp_string (&pp, "This is a link");
2464 pp_end_url (&pp);
2465 ASSERT_STREQ ("This is a link",
2466 pp_formatted_text (&pp));
2467 }
2468
2469 {
2470 pretty_printer pp;
2471 pp.show_urls = true;
2472 pp_begin_url (&pp, "http://example.com");
2473 pp_string (&pp, "This is a link");
2474 pp_end_url (&pp);
ae169f9e 2475 ASSERT_STREQ ("\33]8;;http://example.com\aThis is a link\33]8;;\a",
d2608235
DM
2476 pp_formatted_text (&pp));
2477 }
2478}
2479
ddd0fd17
LH
2480/* Test multibyte awareness. */
2481static void test_utf8 ()
2482{
2483
2484 /* Check that pp_quoted_string leaves valid UTF-8 alone. */
2485 {
2486 pretty_printer pp;
2487 const char *s = "\xf0\x9f\x98\x82";
2488 pp_quoted_string (&pp, s);
2489 ASSERT_STREQ (pp_formatted_text (&pp), s);
2490 }
2491
2492 /* Check that pp_quoted_string escapes non-UTF-8 nonprintable bytes. */
2493 {
2494 pretty_printer pp;
2495 pp_quoted_string (&pp, "\xf0!\x9f\x98\x82");
2496 ASSERT_STREQ (pp_formatted_text (&pp),
2497 "\\xf0!\\x9f\\x98\\x82");
2498 }
2499
2500 /* Check that pp_character will line-wrap at the beginning of a UTF-8
2501 sequence, but not in the middle. */
2502 {
2503 pretty_printer pp (3);
2504 const char s[] = "---\xf0\x9f\x98\x82";
2505 for (int i = 0; i != sizeof (s) - 1; ++i)
2506 pp_character (&pp, s[i]);
2507 pp_newline (&pp);
2508 for (int i = 1; i != sizeof (s) - 1; ++i)
2509 pp_character (&pp, s[i]);
2510 pp_character (&pp, '-');
2511 ASSERT_STREQ (pp_formatted_text (&pp),
2512 "---\n"
2513 "\xf0\x9f\x98\x82\n"
2514 "--\xf0\x9f\x98\x82\n"
2515 "-");
2516 }
2517
2518}
2519
4ccab56d
DM
2520/* Run all of the selftests within this file. */
2521
2522void
2523pretty_print_c_tests ()
2524{
2525 test_basic_printing ();
2526 test_pp_format ();
553a316b 2527 test_prefixes_and_wrapping ();
d2608235 2528 test_urls ();
ddd0fd17 2529 test_utf8 ();
4ccab56d
DM
2530}
2531
2532} // namespace selftest
2533
2534#endif /* CHECKING_P */