comparison gcc/pretty-print.c @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
comparison
equal deleted inserted replaced
111:04ced10e8804 131:84e7813d76e9
1 /* Various declarations for language-independent pretty-print subroutines. 1 /* Various declarations for language-independent pretty-print subroutines.
2 Copyright (C) 2003-2017 Free Software Foundation, Inc. 2 Copyright (C) 2003-2018 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net> 3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
4 4
5 This file is part of GCC. 5 This file is part of GCC.
6 6
7 GCC is free software; you can redistribute it and/or modify it under 7 GCC is free software; you can redistribute it and/or modify it under
638 care must be taken to preserve old attributes. */ 638 care must be taken to preserve old attributes. */
639 if (attrib_rm != 0xFFFF && GetConsoleScreenBufferInfo (h, &sb)) 639 if (attrib_rm != 0xFFFF && GetConsoleScreenBufferInfo (h, &sb))
640 { 640 {
641 attrib_add |= sb.wAttributes & ~attrib_rm; 641 attrib_add |= sb.wAttributes & ~attrib_rm;
642 } 642 }
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 }
643 SetConsoleTextAttribute (h, attrib_add); 653 SetConsoleTextAttribute (h, attrib_add);
644 break; 654 break;
645 } 655 }
646 } 656 }
647 657
682 } 692 }
683 else 693 else
684 /* If it is not a console, write everything as-is. */ 694 /* If it is not a console, write everything as-is. */
685 write_all (h, read, strlen (read)); 695 write_all (h, read, strlen (read));
686 696
687 _close ((intptr_t) h);
688 return 1; 697 return 1;
689 } 698 }
690 699
691 #endif /* __MINGW32__ */ 700 #endif /* __MINGW32__ */
692 701
694 703
695 /* Overwrite the given location/range within this text_info's rich_location. 704 /* Overwrite the given location/range within this text_info's rich_location.
696 For use e.g. when implementing "+" in client format decoders. */ 705 For use e.g. when implementing "+" in client format decoders. */
697 706
698 void 707 void
699 text_info::set_location (unsigned int idx, location_t loc, bool show_caret_p) 708 text_info::set_location (unsigned int idx, location_t loc,
709 enum range_display_kind range_display_kind)
700 { 710 {
701 gcc_checking_assert (m_richloc); 711 gcc_checking_assert (m_richloc);
702 m_richloc->set_range (line_table, idx, loc, show_caret_p); 712 m_richloc->set_range (idx, loc, range_display_kind);
703 } 713 }
704 714
705 location_t 715 location_t
706 text_info::get_location (unsigned int index_of_location) const 716 text_info::get_location (unsigned int index_of_location) const
707 { 717 {
792 pp_clear_state (pretty_printer *pp) 802 pp_clear_state (pretty_printer *pp)
793 { 803 {
794 pp->emitted_prefix = false; 804 pp->emitted_prefix = false;
795 pp_indentation (pp) = 0; 805 pp_indentation (pp) = 0;
796 } 806 }
807
808 /* Print X to PP in decimal. */
809 template<unsigned int N, typename T>
810 void
811 pp_wide_integer (pretty_printer *pp, const poly_int_pod<N, T> &x)
812 {
813 if (x.is_constant ())
814 pp_wide_integer (pp, x.coeffs[0]);
815 else
816 {
817 pp_left_bracket (pp);
818 for (unsigned int i = 0; i < N; ++i)
819 {
820 if (i != 0)
821 pp_comma (pp);
822 pp_wide_integer (pp, x.coeffs[i]);
823 }
824 pp_right_bracket (pp);
825 }
826 }
827
828 template void pp_wide_integer (pretty_printer *, const poly_uint16_pod &);
829 template void pp_wide_integer (pretty_printer *, const poly_int64_pod &);
830 template void pp_wide_integer (pretty_printer *, const poly_uint64_pod &);
797 831
798 /* Flush the formatted text of PRETTY-PRINTER onto the attached stream. */ 832 /* Flush the formatted text of PRETTY-PRINTER onto the attached stream. */
799 void 833 void
800 pp_write_text_to_stream (pretty_printer *pp) 834 pp_write_text_to_stream (pretty_printer *pp)
801 { 835 {
1207 } 1241 }
1208 1242
1209 gcc_assert (!wide || precision == 0); 1243 gcc_assert (!wide || precision == 0);
1210 1244
1211 if (quote) 1245 if (quote)
1212 { 1246 pp_begin_quote (pp, pp_show_color (pp));
1213 pp_string (pp, open_quote);
1214 pp_string (pp, colorize_start (pp_show_color (pp), "quote"));
1215 }
1216 1247
1217 switch (*p) 1248 switch (*p)
1218 { 1249 {
1219 case 'r': 1250 case 'r':
1220 pp_string (pp, colorize_start (pp_show_color (pp), 1251 pp_string (pp, colorize_start (pp_show_color (pp),
1343 1374
1344 default: 1375 default:
1345 { 1376 {
1346 bool ok; 1377 bool ok;
1347 1378
1379 /* Call the format decoder.
1380 Pass the address of "quote" so that format decoders can
1381 potentially disable printing of the closing quote
1382 (e.g. when printing "'TYPEDEF' aka 'TYPE'" in the C family
1383 of frontends). */
1348 gcc_assert (pp_format_decoder (pp)); 1384 gcc_assert (pp_format_decoder (pp));
1349 ok = pp_format_decoder (pp) (pp, text, p, 1385 ok = pp_format_decoder (pp) (pp, text, p,
1350 precision, wide, plus, hash, quote, 1386 precision, wide, plus, hash, &quote,
1351 formatters[argno]); 1387 formatters[argno]);
1352 gcc_assert (ok); 1388 gcc_assert (ok);
1353 } 1389 }
1354 } 1390 }
1355 1391
1356 if (quote) 1392 if (quote)
1357 { 1393 pp_end_quote (pp, pp_show_color (pp));
1358 pp_string (pp, colorize_stop (pp_show_color (pp)));
1359 pp_string (pp, close_quote);
1360 }
1361 1394
1362 obstack_1grow (&buffer->chunk_obstack, '\0'); 1395 obstack_1grow (&buffer->chunk_obstack, '\0');
1363 *formatters[argno] = XOBFINISH (&buffer->chunk_obstack, const char *); 1396 *formatters[argno] = XOBFINISH (&buffer->chunk_obstack, const char *);
1364 } 1397 }
1365 1398
1457 obstack_free (pp_buffer (pp)->obstack, 1490 obstack_free (pp_buffer (pp)->obstack,
1458 obstack_base (pp_buffer (pp)->obstack)); 1491 obstack_base (pp_buffer (pp)->obstack));
1459 pp_buffer (pp)->line_length = 0; 1492 pp_buffer (pp)->line_length = 0;
1460 } 1493 }
1461 1494
1462 /* Set PREFIX for PRETTY-PRINTER. */ 1495 /* Set PREFIX for PRETTY-PRINTER, taking ownership of PREFIX, which
1463 void 1496 will eventually be free-ed. */
1464 pp_set_prefix (pretty_printer *pp, const char *prefix) 1497
1465 { 1498 void
1499 pp_set_prefix (pretty_printer *pp, char *prefix)
1500 {
1501 free (pp->prefix);
1466 pp->prefix = prefix; 1502 pp->prefix = prefix;
1467 pp_set_real_maximum_length (pp); 1503 pp_set_real_maximum_length (pp);
1468 pp->emitted_prefix = false; 1504 pp->emitted_prefix = false;
1469 pp_indentation (pp) = 0; 1505 pp_indentation (pp) = 0;
1470 } 1506 }
1471 1507
1508 /* Take ownership of PP's prefix, setting it to NULL.
1509 This allows clients to save, overide, and then restore an existing
1510 prefix, without it being free-ed. */
1511
1512 char *
1513 pp_take_prefix (pretty_printer *pp)
1514 {
1515 char *result = pp->prefix;
1516 pp->prefix = NULL;
1517 return result;
1518 }
1519
1472 /* Free PRETTY-PRINTER's prefix, a previously malloc()'d string. */ 1520 /* Free PRETTY-PRINTER's prefix, a previously malloc()'d string. */
1473 void 1521 void
1474 pp_destroy_prefix (pretty_printer *pp) 1522 pp_destroy_prefix (pretty_printer *pp)
1475 { 1523 {
1476 if (pp->prefix != NULL) 1524 if (pp->prefix != NULL)
1477 { 1525 {
1478 free (CONST_CAST (char *, pp->prefix)); 1526 free (pp->prefix);
1479 pp->prefix = NULL; 1527 pp->prefix = NULL;
1480 } 1528 }
1481 } 1529 }
1482 1530
1483 /* Write out PRETTY-PRINTER's prefix. */ 1531 /* Write out PRETTY-PRINTER's prefix. */
1510 break; 1558 break;
1511 } 1559 }
1512 } 1560 }
1513 } 1561 }
1514 1562
1515 /* Construct a PRETTY-PRINTER with PREFIX and of MAXIMUM_LENGTH 1563 /* Construct a PRETTY-PRINTER of MAXIMUM_LENGTH characters per line. */
1516 characters per line. */ 1564
1517 1565 pretty_printer::pretty_printer (int maximum_length)
1518 pretty_printer::pretty_printer (const char *p, int l)
1519 : buffer (new (XCNEW (output_buffer)) output_buffer ()), 1566 : buffer (new (XCNEW (output_buffer)) output_buffer ()),
1520 prefix (), 1567 prefix (),
1521 padding (pp_none), 1568 padding (pp_none),
1522 maximum_length (), 1569 maximum_length (),
1523 indent_skip (), 1570 indent_skip (),
1527 emitted_prefix (), 1574 emitted_prefix (),
1528 need_newline (), 1575 need_newline (),
1529 translate_identifiers (true), 1576 translate_identifiers (true),
1530 show_color () 1577 show_color ()
1531 { 1578 {
1532 pp_line_cutoff (this) = l; 1579 pp_line_cutoff (this) = maximum_length;
1533 /* By default, we emit prefixes once per message. */ 1580 /* By default, we emit prefixes once per message. */
1534 pp_prefixing_rule (this) = DIAGNOSTICS_SHOW_PREFIX_ONCE; 1581 pp_prefixing_rule (this) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
1535 pp_set_prefix (this, p); 1582 pp_set_prefix (this, NULL);
1536 } 1583 }
1537 1584
1538 pretty_printer::~pretty_printer () 1585 pretty_printer::~pretty_printer ()
1539 { 1586 {
1540 if (m_format_postprocessor) 1587 if (m_format_postprocessor)
1541 delete m_format_postprocessor; 1588 delete m_format_postprocessor;
1542 buffer->~output_buffer (); 1589 buffer->~output_buffer ();
1543 XDELETE (buffer); 1590 XDELETE (buffer);
1591 free (prefix);
1544 } 1592 }
1545 1593
1546 /* Append a string delimited by START and END to the output area of 1594 /* Append a string delimited by START and END to the output area of
1547 PRETTY-PRINTER. No line wrapping is done. However, if beginning a 1595 PRETTY-PRINTER. No line wrapping is done. However, if beginning a
1548 new line then emit PRETTY-PRINTER's prefix and skip any leading 1596 new line then emit PRETTY-PRINTER's prefix and skip any leading
1727 void 1775 void
1728 pp_separate_with (pretty_printer *pp, char c) 1776 pp_separate_with (pretty_printer *pp, char c)
1729 { 1777 {
1730 pp_character (pp, c); 1778 pp_character (pp, c);
1731 pp_space (pp); 1779 pp_space (pp);
1780 }
1781
1782 /* Add a localized open quote, and if SHOW_COLOR is true, begin colorizing
1783 using the "quote" color. */
1784
1785 void
1786 pp_begin_quote (pretty_printer *pp, bool show_color)
1787 {
1788 pp_string (pp, open_quote);
1789 pp_string (pp, colorize_start (show_color, "quote"));
1790 }
1791
1792 /* If SHOW_COLOR is true, stop colorizing.
1793 Add a localized close quote. */
1794
1795 void
1796 pp_end_quote (pretty_printer *pp, bool show_color)
1797 {
1798 pp_string (pp, colorize_stop (show_color));
1799 pp_string (pp, close_quote);
1732 } 1800 }
1733 1801
1734 1802
1735 /* The string starting at P has LEN (at least 1) bytes left; if they 1803 /* The string starting at P has LEN (at least 1) bytes left; if they
1736 start with a valid UTF-8 sequence, return the length of that 1804 start with a valid UTF-8 sequence, return the length of that
2057 static void 2125 static void
2058 test_pp_format () 2126 test_pp_format ()
2059 { 2127 {
2060 /* Avoid introducing locale-specific differences in the results 2128 /* Avoid introducing locale-specific differences in the results
2061 by hardcoding open_quote and close_quote. */ 2129 by hardcoding open_quote and close_quote. */
2062 const char *old_open_quote = open_quote; 2130 auto_fix_quotes fix_quotes;
2063 const char *old_close_quote = close_quote;
2064 open_quote = "`";
2065 close_quote = "'";
2066 2131
2067 /* Verify that plain text is passed through unchanged. */ 2132 /* Verify that plain text is passed through unchanged. */
2068 assert_pp_format (SELFTEST_LOCATION, "unformatted", "unformatted"); 2133 assert_pp_format (SELFTEST_LOCATION, "unformatted", "unformatted");
2069 2134
2070 /* Verify various individual format codes, in the order listed in the 2135 /* Verify various individual format codes, in the order listed in the
2142 "the %s %s %s jumps over the %s %s", 2207 "the %s %s %s jumps over the %s %s",
2143 "quick", "brown", "fox", "lazy", "dog"); 2208 "quick", "brown", "fox", "lazy", "dog");
2144 assert_pp_format (SELFTEST_LOCATION, "item 3 of 7", "item %i of %i", 3, 7); 2209 assert_pp_format (SELFTEST_LOCATION, "item 3 of 7", "item %i of %i", 3, 7);
2145 assert_pp_format (SELFTEST_LOCATION, "problem with `bar' at line 10", 2210 assert_pp_format (SELFTEST_LOCATION, "problem with `bar' at line 10",
2146 "problem with %qs at line %i", "bar", 10); 2211 "problem with %qs at line %i", "bar", 10);
2147
2148 /* Restore old values of open_quote and close_quote. */
2149 open_quote = old_open_quote;
2150 close_quote = old_close_quote;
2151 } 2212 }
2152 2213
2153 /* Run all of the selftests within this file. */ 2214 /* Run all of the selftests within this file. */
2154 2215
2155 void 2216 void