1 /* Various declarations for language-independent pretty-print subroutines. 2 Copyright (C) 2003-2015 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 #include "system.h" 23 #include "coretypes.h" 24 #include "intl.h" 25 #include "pretty-print.h" 26 #include "diagnostic-color.h" 27 28 #if HAVE_ICONV 29 #include <iconv.h> 30 #endif 31 32 // Default construct an output buffer. 33 34 output_buffer::output_buffer () 35 : formatted_obstack (), 36 chunk_obstack (), 37 obstack (&formatted_obstack), 38 cur_chunk_array (), 39 stream (stderr), 40 line_length (), 41 digit_buffer (), 42 flush_p (true) 43 { 44 obstack_init (&formatted_obstack); 45 obstack_init (&chunk_obstack); 46 } 47 48 // Release resources owned by an output buffer at the end of lifetime. 49 50 output_buffer::~output_buffer () 51 { 52 obstack_free (&chunk_obstack, NULL); 53 obstack_free (&formatted_obstack, NULL); 54 } 55 56 57 /* Format an integer given by va_arg (ARG, type-specifier T) where 58 type-specifier is a precision modifier as indicated by PREC. F is 59 a string used to construct the appropriate format-specifier. */ 60 #define pp_integer_with_precision(PP, ARG, PREC, T, F) \ 61 do \ 62 switch (PREC) \ 63 { \ 64 case 0: \ 65 pp_scalar (PP, "%" F, va_arg (ARG, T)); \ 66 break; \ 67 \ 68 case 1: \ 69 pp_scalar (PP, "%l" F, va_arg (ARG, long T)); \ 70 break; \ 71 \ 72 case 2: \ 73 pp_scalar (PP, "%" HOST_LONG_LONG_FORMAT F, va_arg (ARG, long long T)); \ 74 break; \ 75 \ 76 default: \ 77 break; \ 78 } \ 79 while (0) 80 81 82 /* Subroutine of pp_set_maximum_length. Set up PRETTY-PRINTER's 83 internal maximum characters per line. */ 84 static void 85 pp_set_real_maximum_length (pretty_printer *pp) 86 { 87 /* If we're told not to wrap lines then do the obvious thing. In case 88 we'll emit prefix only once per message, it is appropriate 89 not to increase unnecessarily the line-length cut-off. */ 90 if (!pp_is_wrapping_line (pp) 91 || pp_prefixing_rule (pp) == DIAGNOSTICS_SHOW_PREFIX_ONCE 92 || pp_prefixing_rule (pp) == DIAGNOSTICS_SHOW_PREFIX_NEVER) 93 pp->maximum_length = pp_line_cutoff (pp); 94 else 95 { 96 int prefix_length = pp->prefix ? strlen (pp->prefix) : 0; 97 /* If the prefix is ridiculously too long, output at least 98 32 characters. */ 99 if (pp_line_cutoff (pp) - prefix_length < 32) 100 pp->maximum_length = pp_line_cutoff (pp) + 32; 101 else 102 pp->maximum_length = pp_line_cutoff (pp); 103 } 104 } 105 106 /* Clear PRETTY-PRINTER's output state. */ 107 static inline void 108 pp_clear_state (pretty_printer *pp) 109 { 110 pp->emitted_prefix = false; 111 pp_indentation (pp) = 0; 112 } 113 114 /* Flush the formatted text of PRETTY-PRINTER onto the attached stream. */ 115 void 116 pp_write_text_to_stream (pretty_printer *pp) 117 { 118 const char *text = pp_formatted_text (pp); 119 fputs (text, pp_buffer (pp)->stream); 120 pp_clear_output_area (pp); 121 } 122 123 /* As pp_write_text_to_stream, but for GraphViz label output. 124 125 Flush the formatted text of pretty-printer PP onto the attached stream. 126 Replace characters in PPF that have special meaning in a GraphViz .dot 127 file. 128 129 This routine is not very fast, but it doesn't have to be as this is only 130 be used by routines dumping intermediate representations in graph form. */ 131 132 void 133 pp_write_text_as_dot_label_to_stream (pretty_printer *pp, bool for_record) 134 { 135 const char *text = pp_formatted_text (pp); 136 const char *p = text; 137 FILE *fp = pp_buffer (pp)->stream; 138 139 while (*p) 140 { 141 switch (*p) 142 { 143 /* Print newlines as a left-aligned newline. */ 144 case '\n': 145 fputs ("\\l\\\n", fp); 146 break; 147 148 /* A pipe is only special for record-shape nodes. */ 149 case '|': 150 if (for_record) 151 fputc ('\\', fp); 152 fputc (*p, fp); 153 break; 154 155 /* The following characters always have to be escaped 156 for use in labels. */ 157 case '{': 158 case '}': 159 case '<': 160 case '>': 161 case '"': 162 case ' ': 163 fputc ('\\', fp); 164 /* fall through */ 165 default: 166 fputc (*p, fp); 167 break; 168 } 169 p++; 170 } 171 172 pp_clear_output_area (pp); 173 } 174 175 /* Wrap a text delimited by START and END into PRETTY-PRINTER. */ 176 static void 177 pp_wrap_text (pretty_printer *pp, const char *start, const char *end) 178 { 179 bool wrapping_line = pp_is_wrapping_line (pp); 180 181 while (start != end) 182 { 183 /* Dump anything bordered by whitespaces. */ 184 { 185 const char *p = start; 186 while (p != end && !ISBLANK (*p) && *p != '\n') 187 ++p; 188 if (wrapping_line 189 && p - start >= pp_remaining_character_count_for_line (pp)) 190 pp_newline (pp); 191 pp_append_text (pp, start, p); 192 start = p; 193 } 194 195 if (start != end && ISBLANK (*start)) 196 { 197 pp_space (pp); 198 ++start; 199 } 200 if (start != end && *start == '\n') 201 { 202 pp_newline (pp); 203 ++start; 204 } 205 } 206 } 207 208 /* Same as pp_wrap_text but wrap text only when in line-wrapping mode. */ 209 static inline void 210 pp_maybe_wrap_text (pretty_printer *pp, const char *start, const char *end) 211 { 212 if (pp_is_wrapping_line (pp)) 213 pp_wrap_text (pp, start, end); 214 else 215 pp_append_text (pp, start, end); 216 } 217 218 /* Append to the output area of PRETTY-PRINTER a string specified by its 219 STARTing character and LENGTH. */ 220 static inline void 221 pp_append_r (pretty_printer *pp, const char *start, int length) 222 { 223 output_buffer_append_r (pp_buffer (pp), start, length); 224 } 225 226 /* Insert enough spaces into the output area of PRETTY-PRINTER to bring 227 the column position to the current indentation level, assuming that a 228 newline has just been written to the buffer. */ 229 void 230 pp_indent (pretty_printer *pp) 231 { 232 int n = pp_indentation (pp); 233 int i; 234 235 for (i = 0; i < n; ++i) 236 pp_space (pp); 237 } 238 239 /* The following format specifiers are recognized as being client independent: 240 %d, %i: (signed) integer in base ten. 241 %u: unsigned integer in base ten. 242 %o: unsigned integer in base eight. 243 %x: unsigned integer in base sixteen. 244 %ld, %li, %lo, %lu, %lx: long versions of the above. 245 %lld, %lli, %llo, %llu, %llx: long long versions. 246 %wd, %wi, %wo, %wu, %wx: HOST_WIDE_INT versions. 247 %c: character. 248 %s: string. 249 %p: pointer. 250 %r: if pp_show_color(pp), switch to color identified by const char *. 251 %R: if pp_show_color(pp), reset color. 252 %m: strerror(text->err_no) - does not consume a value from args_ptr. 253 %%: '%'. 254 %<: opening quote. 255 %>: closing quote. 256 %': apostrophe (should only be used in untranslated messages; 257 translations should use appropriate punctuation directly). 258 %.*s: a substring the length of which is specified by an argument 259 integer. 260 %Ns: likewise, but length specified as constant in the format string. 261 Flag 'q': quote formatted text (must come immediately after '%'). 262 263 Arguments can be used sequentially, or through %N$ resp. *N$ 264 notation Nth argument after the format string. If %N$ / *N$ 265 notation is used, it must be used for all arguments, except %m, %%, 266 %<, %> and %', which may not have a number, as they do not consume 267 an argument. When %M$.*N$s is used, M must be N + 1. (This may 268 also be written %M$.*s, provided N is not otherwise used.) The 269 format string must have conversion specifiers with argument numbers 270 1 up to highest argument; each argument may only be used once. 271 A format string can have at most 30 arguments. */ 272 273 /* Formatting phases 1 and 2: render TEXT->format_spec plus 274 TEXT->args_ptr into a series of chunks in pp_buffer (PP)->args[]. 275 Phase 3 is in pp_format_text. */ 276 277 void 278 pp_format (pretty_printer *pp, text_info *text) 279 { 280 output_buffer *buffer = pp_buffer (pp); 281 const char *p; 282 const char **args; 283 struct chunk_info *new_chunk_array; 284 285 unsigned int curarg = 0, chunk = 0, argno; 286 pp_wrapping_mode_t old_wrapping_mode; 287 bool any_unnumbered = false, any_numbered = false; 288 const char **formatters[PP_NL_ARGMAX]; 289 290 /* Allocate a new chunk structure. */ 291 new_chunk_array = XOBNEW (&buffer->chunk_obstack, struct chunk_info); 292 new_chunk_array->prev = buffer->cur_chunk_array; 293 buffer->cur_chunk_array = new_chunk_array; 294 args = new_chunk_array->args; 295 296 /* Formatting phase 1: split up TEXT->format_spec into chunks in 297 pp_buffer (PP)->args[]. Even-numbered chunks are to be output 298 verbatim, odd-numbered chunks are format specifiers. 299 %m, %%, %<, %>, and %' are replaced with the appropriate text at 300 this point. */ 301 302 memset (formatters, 0, sizeof formatters); 303 304 for (p = text->format_spec; *p; ) 305 { 306 while (*p != '\0' && *p != '%') 307 { 308 obstack_1grow (&buffer->chunk_obstack, *p); 309 p++; 310 } 311 312 if (*p == '\0') 313 break; 314 315 switch (*++p) 316 { 317 case '\0': 318 gcc_unreachable (); 319 320 case '%': 321 obstack_1grow (&buffer->chunk_obstack, '%'); 322 p++; 323 continue; 324 325 case '<': 326 { 327 obstack_grow (&buffer->chunk_obstack, 328 open_quote, strlen (open_quote)); 329 const char *colorstr 330 = colorize_start (pp_show_color (pp), "quote"); 331 obstack_grow (&buffer->chunk_obstack, colorstr, strlen (colorstr)); 332 p++; 333 continue; 334 } 335 336 case '>': 337 { 338 const char *colorstr = colorize_stop (pp_show_color (pp)); 339 obstack_grow (&buffer->chunk_obstack, colorstr, strlen (colorstr)); 340 } 341 /* FALLTHRU */ 342 case '\'': 343 obstack_grow (&buffer->chunk_obstack, 344 close_quote, strlen (close_quote)); 345 p++; 346 continue; 347 348 case 'R': 349 { 350 const char *colorstr = colorize_stop (pp_show_color (pp)); 351 obstack_grow (&buffer->chunk_obstack, colorstr, 352 strlen (colorstr)); 353 p++; 354 continue; 355 } 356 357 case 'm': 358 { 359 const char *errstr = xstrerror (text->err_no); 360 obstack_grow (&buffer->chunk_obstack, errstr, strlen (errstr)); 361 } 362 p++; 363 continue; 364 365 default: 366 /* Handled in phase 2. Terminate the plain chunk here. */ 367 obstack_1grow (&buffer->chunk_obstack, '\0'); 368 gcc_assert (chunk < PP_NL_ARGMAX * 2); 369 args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *); 370 break; 371 } 372 373 if (ISDIGIT (*p)) 374 { 375 char *end; 376 argno = strtoul (p, &end, 10) - 1; 377 p = end; 378 gcc_assert (*p == '$'); 379 p++; 380 381 any_numbered = true; 382 gcc_assert (!any_unnumbered); 383 } 384 else 385 { 386 argno = curarg++; 387 any_unnumbered = true; 388 gcc_assert (!any_numbered); 389 } 390 gcc_assert (argno < PP_NL_ARGMAX); 391 gcc_assert (!formatters[argno]); 392 formatters[argno] = &args[chunk]; 393 do 394 { 395 obstack_1grow (&buffer->chunk_obstack, *p); 396 p++; 397 } 398 while (strchr ("qwl+#", p[-1])); 399 400 if (p[-1] == '.') 401 { 402 /* We handle '%.Ns' and '%.*s' or '%M$.*N$s' 403 (where M == N + 1). */ 404 if (ISDIGIT (*p)) 405 { 406 do 407 { 408 obstack_1grow (&buffer->chunk_obstack, *p); 409 p++; 410 } 411 while (ISDIGIT (p[-1])); 412 gcc_assert (p[-1] == 's'); 413 } 414 else 415 { 416 gcc_assert (*p == '*'); 417 obstack_1grow (&buffer->chunk_obstack, '*'); 418 p++; 419 420 if (ISDIGIT (*p)) 421 { 422 char *end; 423 unsigned int argno2 = strtoul (p, &end, 10) - 1; 424 p = end; 425 gcc_assert (argno2 == argno - 1); 426 gcc_assert (!any_unnumbered); 427 gcc_assert (*p == '$'); 428 429 p++; 430 formatters[argno2] = formatters[argno]; 431 } 432 else 433 { 434 gcc_assert (!any_numbered); 435 formatters[argno+1] = formatters[argno]; 436 curarg++; 437 } 438 gcc_assert (*p == 's'); 439 obstack_1grow (&buffer->chunk_obstack, 's'); 440 p++; 441 } 442 } 443 if (*p == '\0') 444 break; 445 446 obstack_1grow (&buffer->chunk_obstack, '\0'); 447 gcc_assert (chunk < PP_NL_ARGMAX * 2); 448 args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *); 449 } 450 451 obstack_1grow (&buffer->chunk_obstack, '\0'); 452 gcc_assert (chunk < PP_NL_ARGMAX * 2); 453 args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *); 454 args[chunk] = 0; 455 456 /* Set output to the argument obstack, and switch line-wrapping and 457 prefixing off. */ 458 buffer->obstack = &buffer->chunk_obstack; 459 old_wrapping_mode = pp_set_verbatim_wrapping (pp); 460 461 /* Second phase. Replace each formatter with the formatted text it 462 corresponds to. */ 463 464 for (argno = 0; formatters[argno]; argno++) 465 { 466 int precision = 0; 467 bool wide = false; 468 bool plus = false; 469 bool hash = false; 470 bool quote = false; 471 472 /* We do not attempt to enforce any ordering on the modifier 473 characters. */ 474 475 for (p = *formatters[argno];; p++) 476 { 477 switch (*p) 478 { 479 case 'q': 480 gcc_assert (!quote); 481 quote = true; 482 continue; 483 484 case '+': 485 gcc_assert (!plus); 486 plus = true; 487 continue; 488 489 case '#': 490 gcc_assert (!hash); 491 hash = true; 492 continue; 493 494 case 'w': 495 gcc_assert (!wide); 496 wide = true; 497 continue; 498 499 case 'l': 500 /* We don't support precision beyond that of "long long". */ 501 gcc_assert (precision < 2); 502 precision++; 503 continue; 504 } 505 break; 506 } 507 508 gcc_assert (!wide || precision == 0); 509 510 if (quote) 511 { 512 pp_string (pp, open_quote); 513 pp_string (pp, colorize_start (pp_show_color (pp), "quote")); 514 } 515 516 switch (*p) 517 { 518 case 'r': 519 pp_string (pp, colorize_start (pp_show_color (pp), 520 va_arg (*text->args_ptr, 521 const char *))); 522 break; 523 524 case 'c': 525 pp_character (pp, va_arg (*text->args_ptr, int)); 526 break; 527 528 case 'd': 529 case 'i': 530 if (wide) 531 pp_wide_integer (pp, va_arg (*text->args_ptr, HOST_WIDE_INT)); 532 else 533 pp_integer_with_precision 534 (pp, *text->args_ptr, precision, int, "d"); 535 break; 536 537 case 'o': 538 if (wide) 539 pp_scalar (pp, "%" HOST_WIDE_INT_PRINT "o", 540 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT)); 541 else 542 pp_integer_with_precision 543 (pp, *text->args_ptr, precision, unsigned, "o"); 544 break; 545 546 case 's': 547 pp_string (pp, va_arg (*text->args_ptr, const char *)); 548 break; 549 550 case 'p': 551 pp_pointer (pp, va_arg (*text->args_ptr, void *)); 552 break; 553 554 case 'u': 555 if (wide) 556 pp_scalar (pp, HOST_WIDE_INT_PRINT_UNSIGNED, 557 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT)); 558 else 559 pp_integer_with_precision 560 (pp, *text->args_ptr, precision, unsigned, "u"); 561 break; 562 563 case 'x': 564 if (wide) 565 pp_scalar (pp, HOST_WIDE_INT_PRINT_HEX, 566 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT)); 567 else 568 pp_integer_with_precision 569 (pp, *text->args_ptr, precision, unsigned, "x"); 570 break; 571 572 case '.': 573 { 574 int n; 575 const char *s; 576 577 /* We handle '%.Ns' and '%.*s' or '%M$.*N$s' 578 (where M == N + 1). The format string should be verified 579 already from the first phase. */ 580 p++; 581 if (ISDIGIT (*p)) 582 { 583 char *end; 584 n = strtoul (p, &end, 10); 585 p = end; 586 gcc_assert (*p == 's'); 587 } 588 else 589 { 590 gcc_assert (*p == '*'); 591 p++; 592 gcc_assert (*p == 's'); 593 n = va_arg (*text->args_ptr, int); 594 595 /* This consumes a second entry in the formatters array. */ 596 gcc_assert (formatters[argno] == formatters[argno+1]); 597 argno++; 598 } 599 600 s = va_arg (*text->args_ptr, const char *); 601 pp_append_text (pp, s, s + n); 602 } 603 break; 604 605 default: 606 { 607 bool ok; 608 609 gcc_assert (pp_format_decoder (pp)); 610 ok = pp_format_decoder (pp) (pp, text, p, 611 precision, wide, plus, hash); 612 gcc_assert (ok); 613 } 614 } 615 616 if (quote) 617 { 618 pp_string (pp, colorize_stop (pp_show_color (pp))); 619 pp_string (pp, close_quote); 620 } 621 622 obstack_1grow (&buffer->chunk_obstack, '\0'); 623 *formatters[argno] = XOBFINISH (&buffer->chunk_obstack, const char *); 624 } 625 626 #ifdef ENABLE_CHECKING 627 for (; argno < PP_NL_ARGMAX; argno++) 628 gcc_assert (!formatters[argno]); 629 #endif 630 631 /* Revert to normal obstack and wrapping mode. */ 632 buffer->obstack = &buffer->formatted_obstack; 633 buffer->line_length = 0; 634 pp_wrapping_mode (pp) = old_wrapping_mode; 635 pp_clear_state (pp); 636 } 637 638 /* Format of a message pointed to by TEXT. */ 639 void 640 pp_output_formatted_text (pretty_printer *pp) 641 { 642 unsigned int chunk; 643 output_buffer *buffer = pp_buffer (pp); 644 struct chunk_info *chunk_array = buffer->cur_chunk_array; 645 const char **args = chunk_array->args; 646 647 gcc_assert (buffer->obstack == &buffer->formatted_obstack); 648 gcc_assert (buffer->line_length == 0); 649 650 /* This is a third phase, first 2 phases done in pp_format_args. 651 Now we actually print it. */ 652 for (chunk = 0; args[chunk]; chunk++) 653 pp_string (pp, args[chunk]); 654 655 /* Deallocate the chunk structure and everything after it (i.e. the 656 associated series of formatted strings). */ 657 buffer->cur_chunk_array = chunk_array->prev; 658 obstack_free (&buffer->chunk_obstack, chunk_array); 659 } 660 661 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate 662 settings needed by BUFFER for a verbatim formatting. */ 663 void 664 pp_format_verbatim (pretty_printer *pp, text_info *text) 665 { 666 /* Set verbatim mode. */ 667 pp_wrapping_mode_t oldmode = pp_set_verbatim_wrapping (pp); 668 669 /* Do the actual formatting. */ 670 pp_format (pp, text); 671 pp_output_formatted_text (pp); 672 673 /* Restore previous settings. */ 674 pp_wrapping_mode (pp) = oldmode; 675 } 676 677 /* Flush the content of BUFFER onto the attached stream. This 678 function does nothing unless pp->output_buffer->flush_p. */ 679 void 680 pp_flush (pretty_printer *pp) 681 { 682 pp_clear_state (pp); 683 if (!pp->buffer->flush_p) 684 return; 685 pp_write_text_to_stream (pp); 686 fflush (pp_buffer (pp)->stream); 687 } 688 689 /* Flush the content of BUFFER onto the attached stream independently 690 of the value of pp->output_buffer->flush_p. */ 691 void 692 pp_really_flush (pretty_printer *pp) 693 { 694 pp_clear_state (pp); 695 pp_write_text_to_stream (pp); 696 fflush (pp_buffer (pp)->stream); 697 } 698 699 /* Sets the number of maximum characters per line PRETTY-PRINTER can 700 output in line-wrapping mode. A LENGTH value 0 suppresses 701 line-wrapping. */ 702 void 703 pp_set_line_maximum_length (pretty_printer *pp, int length) 704 { 705 pp_line_cutoff (pp) = length; 706 pp_set_real_maximum_length (pp); 707 } 708 709 /* Clear PRETTY-PRINTER output area text info. */ 710 void 711 pp_clear_output_area (pretty_printer *pp) 712 { 713 obstack_free (pp_buffer (pp)->obstack, 714 obstack_base (pp_buffer (pp)->obstack)); 715 pp_buffer (pp)->line_length = 0; 716 } 717 718 /* Set PREFIX for PRETTY-PRINTER. */ 719 void 720 pp_set_prefix (pretty_printer *pp, const char *prefix) 721 { 722 pp->prefix = prefix; 723 pp_set_real_maximum_length (pp); 724 pp->emitted_prefix = false; 725 pp_indentation (pp) = 0; 726 } 727 728 /* Free PRETTY-PRINTER's prefix, a previously malloc()'d string. */ 729 void 730 pp_destroy_prefix (pretty_printer *pp) 731 { 732 if (pp->prefix != NULL) 733 { 734 free (CONST_CAST (char *, pp->prefix)); 735 pp->prefix = NULL; 736 } 737 } 738 739 /* Write out PRETTY-PRINTER's prefix. */ 740 void 741 pp_emit_prefix (pretty_printer *pp) 742 { 743 if (pp->prefix != NULL) 744 { 745 switch (pp_prefixing_rule (pp)) 746 { 747 default: 748 case DIAGNOSTICS_SHOW_PREFIX_NEVER: 749 break; 750 751 case DIAGNOSTICS_SHOW_PREFIX_ONCE: 752 if (pp->emitted_prefix) 753 { 754 pp_indent (pp); 755 break; 756 } 757 pp_indentation (pp) += 3; 758 /* Fall through. */ 759 760 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE: 761 { 762 int prefix_length = strlen (pp->prefix); 763 pp_append_r (pp, pp->prefix, prefix_length); 764 pp->emitted_prefix = true; 765 } 766 break; 767 } 768 } 769 } 770 771 /* Construct a PRETTY-PRINTER with PREFIX and of MAXIMUM_LENGTH 772 characters per line. */ 773 774 pretty_printer::pretty_printer (const char *p, int l) 775 : buffer (new (XCNEW (output_buffer)) output_buffer ()), 776 prefix (), 777 padding (pp_none), 778 maximum_length (), 779 indent_skip (), 780 wrapping (), 781 format_decoder (), 782 emitted_prefix (), 783 need_newline (), 784 translate_identifiers (true), 785 show_color () 786 { 787 pp_line_cutoff (this) = l; 788 /* By default, we emit prefixes once per message. */ 789 pp_prefixing_rule (this) = DIAGNOSTICS_SHOW_PREFIX_ONCE; 790 pp_set_prefix (this, p); 791 } 792 793 pretty_printer::~pretty_printer () 794 { 795 buffer->~output_buffer (); 796 XDELETE (buffer); 797 } 798 799 /* Append a string delimited by START and END to the output area of 800 PRETTY-PRINTER. No line wrapping is done. However, if beginning a 801 new line then emit PRETTY-PRINTER's prefix and skip any leading 802 whitespace if appropriate. The caller must ensure that it is 803 safe to do so. */ 804 void 805 pp_append_text (pretty_printer *pp, const char *start, const char *end) 806 { 807 /* Emit prefix and skip whitespace if we're starting a new line. */ 808 if (pp_buffer (pp)->line_length == 0) 809 { 810 pp_emit_prefix (pp); 811 if (pp_is_wrapping_line (pp)) 812 while (start != end && *start == ' ') 813 ++start; 814 } 815 pp_append_r (pp, start, end - start); 816 } 817 818 /* Finishes constructing a NULL-terminated character string representing 819 the PRETTY-PRINTED text. */ 820 const char * 821 pp_formatted_text (pretty_printer *pp) 822 { 823 return output_buffer_formatted_text (pp_buffer (pp)); 824 } 825 826 /* Return a pointer to the last character emitted in PRETTY-PRINTER's 827 output area. A NULL pointer means no character available. */ 828 const char * 829 pp_last_position_in_text (const pretty_printer *pp) 830 { 831 return output_buffer_last_position_in_text (pp_buffer (pp)); 832 } 833 834 /* Return the amount of characters PRETTY-PRINTER can accept to 835 make a full line. Meaningful only in line-wrapping mode. */ 836 int 837 pp_remaining_character_count_for_line (pretty_printer *pp) 838 { 839 return pp->maximum_length - pp_buffer (pp)->line_length; 840 } 841 842 843 /* Format a message into BUFFER a la printf. */ 844 void 845 pp_printf (pretty_printer *pp, const char *msg, ...) 846 { 847 text_info text; 848 va_list ap; 849 850 va_start (ap, msg); 851 text.err_no = errno; 852 text.args_ptr = ≈ 853 text.format_spec = msg; 854 text.locus = NULL; 855 pp_format (pp, &text); 856 pp_output_formatted_text (pp); 857 va_end (ap); 858 } 859 860 861 /* Output MESSAGE verbatim into BUFFER. */ 862 void 863 pp_verbatim (pretty_printer *pp, const char *msg, ...) 864 { 865 text_info text; 866 va_list ap; 867 868 va_start (ap, msg); 869 text.err_no = errno; 870 text.args_ptr = ≈ 871 text.format_spec = msg; 872 text.locus = NULL; 873 pp_format_verbatim (pp, &text); 874 va_end (ap); 875 } 876 877 878 879 /* Have PRETTY-PRINTER start a new line. */ 880 void 881 pp_newline (pretty_printer *pp) 882 { 883 obstack_1grow (pp_buffer (pp)->obstack, '\n'); 884 pp_needs_newline (pp) = false; 885 pp_buffer (pp)->line_length = 0; 886 } 887 888 /* Have PRETTY-PRINTER add a CHARACTER. */ 889 void 890 pp_character (pretty_printer *pp, int c) 891 { 892 if (pp_is_wrapping_line (pp) 893 && pp_remaining_character_count_for_line (pp) <= 0) 894 { 895 pp_newline (pp); 896 if (ISSPACE (c)) 897 return; 898 } 899 obstack_1grow (pp_buffer (pp)->obstack, c); 900 ++pp_buffer (pp)->line_length; 901 } 902 903 /* Append a STRING to the output area of PRETTY-PRINTER; the STRING may 904 be line-wrapped if in appropriate mode. */ 905 void 906 pp_string (pretty_printer *pp, const char *str) 907 { 908 pp_maybe_wrap_text (pp, str, str + (str ? strlen (str) : 0)); 909 } 910 911 /* Maybe print out a whitespace if needed. */ 912 913 void 914 pp_maybe_space (pretty_printer *pp) 915 { 916 if (pp->padding != pp_none) 917 { 918 pp_space (pp); 919 pp->padding = pp_none; 920 } 921 } 922 923 // Add a newline to the pretty printer PP and flush formatted text. 924 925 void 926 pp_newline_and_flush (pretty_printer *pp) 927 { 928 pp_newline (pp); 929 pp_flush (pp); 930 pp_needs_newline (pp) = false; 931 } 932 933 // Add a newline to the pretty printer PP, followed by indentation. 934 935 void 936 pp_newline_and_indent (pretty_printer *pp, int n) 937 { 938 pp_indentation (pp) += n; 939 pp_newline (pp); 940 pp_indent (pp); 941 pp_needs_newline (pp) = false; 942 } 943 944 // Add separator C, followed by a single whitespace. 945 946 void 947 pp_separate_with (pretty_printer *pp, char c) 948 { 949 pp_character (pp, c); 950 pp_space (pp); 951 } 952 953 954 /* The string starting at P has LEN (at least 1) bytes left; if they 955 start with a valid UTF-8 sequence, return the length of that 956 sequence and set *VALUE to the value of that sequence, and 957 otherwise return 0 and set *VALUE to (unsigned int) -1. */ 958 959 static int 960 decode_utf8_char (const unsigned char *p, size_t len, unsigned int *value) 961 { 962 unsigned int t = *p; 963 964 if (len == 0) 965 abort (); 966 if (t & 0x80) 967 { 968 size_t utf8_len = 0; 969 unsigned int ch; 970 size_t i; 971 for (t = *p; t & 0x80; t <<= 1) 972 utf8_len++; 973 974 if (utf8_len > len || utf8_len < 2 || utf8_len > 6) 975 { 976 *value = (unsigned int) -1; 977 return 0; 978 } 979 ch = *p & ((1 << (7 - utf8_len)) - 1); 980 for (i = 1; i < utf8_len; i++) 981 { 982 unsigned int u = p[i]; 983 if ((u & 0xC0) != 0x80) 984 { 985 *value = (unsigned int) -1; 986 return 0; 987 } 988 ch = (ch << 6) | (u & 0x3F); 989 } 990 if ( (ch <= 0x7F && utf8_len > 1) 991 || (ch <= 0x7FF && utf8_len > 2) 992 || (ch <= 0xFFFF && utf8_len > 3) 993 || (ch <= 0x1FFFFF && utf8_len > 4) 994 || (ch <= 0x3FFFFFF && utf8_len > 5) 995 || (ch >= 0xD800 && ch <= 0xDFFF)) 996 { 997 *value = (unsigned int) -1; 998 return 0; 999 } 1000 *value = ch; 1001 return utf8_len; 1002 } 1003 else 1004 { 1005 *value = t; 1006 return 1; 1007 } 1008 } 1009 1010 /* Allocator for identifier_to_locale and corresponding function to 1011 free memory. */ 1012 1013 void *(*identifier_to_locale_alloc) (size_t) = xmalloc; 1014 void (*identifier_to_locale_free) (void *) = free; 1015 1016 /* Given IDENT, an identifier in the internal encoding, return a 1017 version of IDENT suitable for diagnostics in the locale character 1018 set: either IDENT itself, or a string, allocated using 1019 identifier_to_locale_alloc, converted to the locale character set 1020 and using escape sequences if not representable in the locale 1021 character set or containing control characters or invalid byte 1022 sequences. Existing backslashes in IDENT are not doubled, so the 1023 result may not uniquely specify the contents of an arbitrary byte 1024 sequence identifier. */ 1025 1026 const char * 1027 identifier_to_locale (const char *ident) 1028 { 1029 const unsigned char *uid = (const unsigned char *) ident; 1030 size_t idlen = strlen (ident); 1031 bool valid_printable_utf8 = true; 1032 bool all_ascii = true; 1033 size_t i; 1034 1035 for (i = 0; i < idlen;) 1036 { 1037 unsigned int c; 1038 size_t utf8_len = decode_utf8_char (&uid[i], idlen - i, &c); 1039 if (utf8_len == 0 || c <= 0x1F || (c >= 0x7F && c <= 0x9F)) 1040 { 1041 valid_printable_utf8 = false; 1042 break; 1043 } 1044 if (utf8_len > 1) 1045 all_ascii = false; 1046 i += utf8_len; 1047 } 1048 1049 /* If IDENT contains invalid UTF-8 sequences (which may occur with 1050 attributes putting arbitrary byte sequences in identifiers), or 1051 control characters, we use octal escape sequences for all bytes 1052 outside printable ASCII. */ 1053 if (!valid_printable_utf8) 1054 { 1055 char *ret = (char *) identifier_to_locale_alloc (4 * idlen + 1); 1056 char *p = ret; 1057 for (i = 0; i < idlen; i++) 1058 { 1059 if (uid[i] > 0x1F && uid[i] < 0x7F) 1060 *p++ = uid[i]; 1061 else 1062 { 1063 sprintf (p, "\\%03o", uid[i]); 1064 p += 4; 1065 } 1066 } 1067 *p = 0; 1068 return ret; 1069 } 1070 1071 /* Otherwise, if it is valid printable ASCII, or printable UTF-8 1072 with the locale character set being UTF-8, IDENT is used. */ 1073 if (all_ascii || locale_utf8) 1074 return ident; 1075 1076 /* Otherwise IDENT is converted to the locale character set if 1077 possible. */ 1078 #if defined ENABLE_NLS && defined HAVE_LANGINFO_CODESET && HAVE_ICONV 1079 if (locale_encoding != NULL) 1080 { 1081 iconv_t cd = iconv_open (locale_encoding, "UTF-8"); 1082 bool conversion_ok = true; 1083 char *ret = NULL; 1084 if (cd != (iconv_t) -1) 1085 { 1086 size_t ret_alloc = 4 * idlen + 1; 1087 for (;;) 1088 { 1089 /* Repeat the whole conversion process as needed with 1090 larger buffers so non-reversible transformations can 1091 always be detected. */ 1092 ICONV_CONST char *inbuf = CONST_CAST (char *, ident); 1093 char *outbuf; 1094 size_t inbytesleft = idlen; 1095 size_t outbytesleft = ret_alloc - 1; 1096 size_t iconv_ret; 1097 1098 ret = (char *) identifier_to_locale_alloc (ret_alloc); 1099 outbuf = ret; 1100 1101 if (iconv (cd, 0, 0, 0, 0) == (size_t) -1) 1102 { 1103 conversion_ok = false; 1104 break; 1105 } 1106 1107 iconv_ret = iconv (cd, &inbuf, &inbytesleft, 1108 &outbuf, &outbytesleft); 1109 if (iconv_ret == (size_t) -1 || inbytesleft != 0) 1110 { 1111 if (errno == E2BIG) 1112 { 1113 ret_alloc *= 2; 1114 identifier_to_locale_free (ret); 1115 ret = NULL; 1116 continue; 1117 } 1118 else 1119 { 1120 conversion_ok = false; 1121 break; 1122 } 1123 } 1124 else if (iconv_ret != 0) 1125 { 1126 conversion_ok = false; 1127 break; 1128 } 1129 /* Return to initial shift state. */ 1130 if (iconv (cd, 0, 0, &outbuf, &outbytesleft) == (size_t) -1) 1131 { 1132 if (errno == E2BIG) 1133 { 1134 ret_alloc *= 2; 1135 identifier_to_locale_free (ret); 1136 ret = NULL; 1137 continue; 1138 } 1139 else 1140 { 1141 conversion_ok = false; 1142 break; 1143 } 1144 } 1145 *outbuf = 0; 1146 break; 1147 } 1148 iconv_close (cd); 1149 if (conversion_ok) 1150 return ret; 1151 } 1152 } 1153 #endif 1154 1155 /* Otherwise, convert non-ASCII characters in IDENT to UCNs. */ 1156 { 1157 char *ret = (char *) identifier_to_locale_alloc (10 * idlen + 1); 1158 char *p = ret; 1159 for (i = 0; i < idlen;) 1160 { 1161 unsigned int c; 1162 size_t utf8_len = decode_utf8_char (&uid[i], idlen - i, &c); 1163 if (utf8_len == 1) 1164 *p++ = uid[i]; 1165 else 1166 { 1167 sprintf (p, "\\U%08x", c); 1168 p += 10; 1169 } 1170 i += utf8_len; 1171 } 1172 *p = 0; 1173 return ret; 1174 } 1175 } 1176