comparison gcc/c-family/c-pretty-print.c @ 132:d34655255c78

update gcc-8.2
author mir3636
date Thu, 25 Oct 2018 10:21:07 +0900
parents 367f9f4f266e 84e7813d76e9
children 351920fa3827
comparison
equal deleted inserted replaced
130:e108057fa461 132:d34655255c78
1 /* Subroutines common to both C and C++ pretty-printers. 1 /* Subroutines common to both C and C++ pretty-printers.
2 Copyright (C) 2002-2017 Free Software Foundation, Inc. 2 Copyright (C) 2002-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
26 #include "stor-layout.h" 26 #include "stor-layout.h"
27 #include "stringpool.h" 27 #include "stringpool.h"
28 #include "attribs.h" 28 #include "attribs.h"
29 #include "intl.h" 29 #include "intl.h"
30 #include "tree-pretty-print.h" 30 #include "tree-pretty-print.h"
31 #include "selftest.h"
31 #ifndef noCbC 32 #ifndef noCbC
32 #include "../c/cbc-tree.h" 33 #include "../c/cbc-tree.h"
33 #endif 34 #endif
34 35
35 /* The pretty-printer code is primarily designed to closely follow 36 /* The pretty-printer code is primarily designed to closely follow
192 ? "restrict" : "__restrict__")); 193 ? "restrict" : "__restrict__"));
193 } 194 }
194 195
195 /* Pretty-print T using the type-cast notation '( type-name )'. */ 196 /* Pretty-print T using the type-cast notation '( type-name )'. */
196 197
197 static void 198 void
198 pp_c_type_cast (c_pretty_printer *pp, tree t) 199 pp_c_type_cast (c_pretty_printer *pp, tree t)
199 { 200 {
200 pp_c_left_paren (pp); 201 pp_c_left_paren (pp);
201 pp->type_id (t); 202 pp->type_id (t);
202 pp_c_right_paren (pp); 203 pp_c_right_paren (pp);
908 pp_string (pp, "0"); 909 pp_string (pp, "0");
909 } 910 }
910 911
911 /* Pretty-print an INTEGER literal. */ 912 /* Pretty-print an INTEGER literal. */
912 913
913 static void 914 void
914 pp_c_integer_constant (c_pretty_printer *pp, tree i) 915 pp_c_integer_constant (c_pretty_printer *pp, tree i)
915 { 916 {
916 if (tree_fits_shwi_p (i)) 917 if (tree_fits_shwi_p (i))
917 pp_wide_integer (pp, tree_to_shwi (i)); 918 pp_wide_integer (pp, tree_to_shwi (i));
918 else if (tree_fits_uhwi_p (i)) 919 else if (tree_fits_uhwi_p (i))
968 pp_c_integer_constant (pp, b); 969 pp_c_integer_constant (pp, b);
969 else 970 else
970 pp_unsupported_tree (pp, b); 971 pp_unsupported_tree (pp, b);
971 } 972 }
972 973
973 /* Attempt to print out an ENUMERATOR. Return true on success. Else return 974 /* Given a value e of ENUMERAL_TYPE:
974 false; that means the value was obtained by a cast, in which case 975 Print out the first ENUMERATOR id with value e, if one is found,
975 print out the type-id part of the cast-expression -- the casted value 976 else print out the value as a C-style cast (type-id)value. */
976 is then printed by pp_c_integer_literal. */ 977
977 978 static void
978 static bool
979 pp_c_enumeration_constant (c_pretty_printer *pp, tree e) 979 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
980 { 980 {
981 bool value_is_named = true;
982 tree type = TREE_TYPE (e); 981 tree type = TREE_TYPE (e);
983 tree value; 982 tree value;
984 983
985 /* Find the name of this constant. */ 984 /* Find the name of this constant. */
986 for (value = TYPE_VALUES (type); 985 for (value = TYPE_VALUES (type);
987 value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e); 986 value != NULL_TREE
987 && !tree_int_cst_equal (DECL_INITIAL (TREE_VALUE (value)), e);
988 value = TREE_CHAIN (value)) 988 value = TREE_CHAIN (value))
989 ; 989 ;
990 990
991 if (value != NULL_TREE) 991 if (value != NULL_TREE)
992 pp->id_expression (TREE_PURPOSE (value)); 992 pp->id_expression (TREE_PURPOSE (value));
993 else 993 else
994 { 994 {
995 /* Value must have been cast. */ 995 /* Value must have been cast. */
996 pp_c_type_cast (pp, type); 996 pp_c_type_cast (pp, type);
997 value_is_named = false; 997 pp_c_integer_constant (pp, e);
998 } 998 }
999
1000 return value_is_named;
1001 } 999 }
1002 1000
1003 /* Print out a REAL value as a decimal-floating-constant. */ 1001 /* Print out a REAL value as a decimal-floating-constant. */
1004 1002
1005 static void 1003 static void
1140 tree type = TREE_TYPE (e); 1138 tree type = TREE_TYPE (e);
1141 if (type == boolean_type_node) 1139 if (type == boolean_type_node)
1142 pp_c_bool_constant (this, e); 1140 pp_c_bool_constant (this, e);
1143 else if (type == char_type_node) 1141 else if (type == char_type_node)
1144 pp_c_character_constant (this, e); 1142 pp_c_character_constant (this, e);
1145 else if (TREE_CODE (type) == ENUMERAL_TYPE 1143 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1146 && pp_c_enumeration_constant (this, e)) 1144 pp_c_enumeration_constant (this, e);
1147 ;
1148 else 1145 else
1149 pp_c_integer_constant (this, e); 1146 pp_c_integer_constant (this, e);
1150 } 1147 }
1151 break; 1148 break;
1152 1149
1380 return; 1377 return;
1381 1378
1382 case VECTOR_TYPE: 1379 case VECTOR_TYPE:
1383 if (TREE_CODE (e) == VECTOR_CST) 1380 if (TREE_CODE (e) == VECTOR_CST)
1384 { 1381 {
1385 unsigned i; 1382 /* We don't create variable-length VECTOR_CSTs. */
1386 for (i = 0; i < VECTOR_CST_NELTS (e); ++i) 1383 unsigned int nunits = VECTOR_CST_NELTS (e).to_constant ();
1384 for (unsigned int i = 0; i < nunits; ++i)
1387 { 1385 {
1388 if (i > 0) 1386 if (i > 0)
1389 pp_separate_with (pp, ','); 1387 pp_separate_with (pp, ',');
1390 pp->expression (VECTOR_CST_ELT (e, i)); 1388 pp->expression (VECTOR_CST_ELT (e, i));
1391 } 1389 }
1483 pp_c_left_bracket (this); 1481 pp_c_left_bracket (this);
1484 expression (TREE_OPERAND (e, 1)); 1482 expression (TREE_OPERAND (e, 1));
1485 pp_c_right_bracket (this); 1483 pp_c_right_bracket (this);
1486 break; 1484 break;
1487 1485
1488 case ARRAY_NOTATION_REF:
1489 postfix_expression (ARRAY_NOTATION_ARRAY (e));
1490 pp_c_left_bracket (this);
1491 expression (ARRAY_NOTATION_START (e));
1492 pp_colon (this);
1493 expression (ARRAY_NOTATION_LENGTH (e));
1494 pp_colon (this);
1495 expression (ARRAY_NOTATION_STRIDE (e));
1496 pp_c_right_bracket (this);
1497 break;
1498
1499 case CALL_EXPR: 1486 case CALL_EXPR:
1500 { 1487 {
1501 call_expr_arg_iterator iter; 1488 call_expr_arg_iterator iter;
1502 tree arg; 1489 tree arg;
1503 postfix_expression (CALL_EXPR_FN (e)); 1490 postfix_expression (CALL_EXPR_FN (e));
1820 { 1807 {
1821 case FLOAT_EXPR: 1808 case FLOAT_EXPR:
1822 case FIX_TRUNC_EXPR: 1809 case FIX_TRUNC_EXPR:
1823 CASE_CONVERT: 1810 CASE_CONVERT:
1824 case VIEW_CONVERT_EXPR: 1811 case VIEW_CONVERT_EXPR:
1825 pp_c_type_cast (pp, TREE_TYPE (e)); 1812 if (!location_wrapper_p (e))
1813 pp_c_type_cast (pp, TREE_TYPE (e));
1826 pp_c_cast_expression (pp, TREE_OPERAND (e, 0)); 1814 pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
1827 break; 1815 break;
1828 1816
1829 default: 1817 default:
1830 pp->unary_expression (e); 1818 pp->unary_expression (e);
1850 case RDIV_EXPR: 1838 case RDIV_EXPR:
1851 multiplicative_expression (TREE_OPERAND (e, 0)); 1839 multiplicative_expression (TREE_OPERAND (e, 0));
1852 pp_c_whitespace (this); 1840 pp_c_whitespace (this);
1853 if (code == MULT_EXPR) 1841 if (code == MULT_EXPR)
1854 pp_c_star (this); 1842 pp_c_star (this);
1855 else if (code == TRUNC_DIV_EXPR) 1843 else if (code != TRUNC_MOD_EXPR)
1856 pp_slash (this); 1844 pp_slash (this);
1857 else 1845 else
1858 pp_modulo (this); 1846 pp_modulo (this);
1859 pp_c_whitespace (this); 1847 pp_c_whitespace (this);
1860 pp_c_cast_expression (this, TREE_OPERAND (e, 1)); 1848 pp_c_cast_expression (this, TREE_OPERAND (e, 1));
1877 enum tree_code code = TREE_CODE (e); 1865 enum tree_code code = TREE_CODE (e);
1878 switch (code) 1866 switch (code)
1879 { 1867 {
1880 case POINTER_PLUS_EXPR: 1868 case POINTER_PLUS_EXPR:
1881 case PLUS_EXPR: 1869 case PLUS_EXPR:
1870 case POINTER_DIFF_EXPR:
1882 case MINUS_EXPR: 1871 case MINUS_EXPR:
1883 pp_c_additive_expression (pp, TREE_OPERAND (e, 0)); 1872 pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
1884 pp_c_whitespace (pp); 1873 pp_c_whitespace (pp);
1885 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR) 1874 if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
1886 pp_plus (pp); 1875 pp_plus (pp);
2192 break; 2181 break;
2193 2182
2194 case POSTINCREMENT_EXPR: 2183 case POSTINCREMENT_EXPR:
2195 case POSTDECREMENT_EXPR: 2184 case POSTDECREMENT_EXPR:
2196 case ARRAY_REF: 2185 case ARRAY_REF:
2197 case ARRAY_NOTATION_REF:
2198 case CALL_EXPR: 2186 case CALL_EXPR:
2199 case COMPONENT_REF: 2187 case COMPONENT_REF:
2200 case BIT_FIELD_REF: 2188 case BIT_FIELD_REF:
2201 case COMPLEX_CST: 2189 case COMPLEX_CST:
2202 case COMPLEX_EXPR: 2190 case COMPLEX_EXPR:
2293 conditional_expression (e); 2281 conditional_expression (e);
2294 break; 2282 break;
2295 2283
2296 case POINTER_PLUS_EXPR: 2284 case POINTER_PLUS_EXPR:
2297 case PLUS_EXPR: 2285 case PLUS_EXPR:
2286 case POINTER_DIFF_EXPR:
2298 case MINUS_EXPR: 2287 case MINUS_EXPR:
2299 pp_c_additive_expression (this, e); 2288 pp_c_additive_expression (this, e);
2300 break; 2289 break;
2301 2290
2302 case MODIFY_EXPR: 2291 case MODIFY_EXPR:
2349 return; 2338 return;
2350 2339
2351 if (pp_needs_newline (this)) 2340 if (pp_needs_newline (this))
2352 pp_newline_and_indent (this, 0); 2341 pp_newline_and_indent (this, 0);
2353 2342
2354 dump_generic_node (this, stmt, pp_indentation (this), 0, true); 2343 dump_generic_node (this, stmt, pp_indentation (this), TDF_NONE, true);
2355 } 2344 }
2356 2345
2357 2346
2358 /* Initialize the PRETTY-PRINTER for handling C codes. */ 2347 /* Initialize the PRETTY-PRINTER for handling C codes. */
2359 2348
2410 name = xname; 2399 name = xname;
2411 } 2400 }
2412 2401
2413 pp_c_identifier (pp, name); 2402 pp_c_identifier (pp, name);
2414 } 2403 }
2404
2405 #if CHECKING_P
2406
2407 namespace selftest {
2408
2409 /* Selftests for pretty-printing trees. */
2410
2411 /* Verify that EXPR printed by c_pretty_printer is EXPECTED, using
2412 LOC as the effective location for any failures. */
2413
2414 static void
2415 assert_c_pretty_printer_output (const location &loc, const char *expected,
2416 tree expr)
2417 {
2418 c_pretty_printer pp;
2419 pp.expression (expr);
2420 ASSERT_STREQ_AT (loc, expected, pp_formatted_text (&pp));
2421 }
2422
2423 /* Helper function for calling assert_c_pretty_printer_output.
2424 This is to avoid having to write SELFTEST_LOCATION. */
2425
2426 #define ASSERT_C_PRETTY_PRINTER_OUTPUT(EXPECTED, EXPR) \
2427 SELFTEST_BEGIN_STMT \
2428 assert_c_pretty_printer_output ((SELFTEST_LOCATION), \
2429 (EXPECTED), \
2430 (EXPR)); \
2431 SELFTEST_END_STMT
2432
2433 /* Verify that location wrappers don't show up in pretty-printed output. */
2434
2435 static void
2436 test_location_wrappers ()
2437 {
2438 /* VAR_DECL. */
2439 tree id = get_identifier ("foo");
2440 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, id,
2441 integer_type_node);
2442 tree wrapped_decl = maybe_wrap_with_location (decl, BUILTINS_LOCATION);
2443 ASSERT_NE (wrapped_decl, decl);
2444 ASSERT_C_PRETTY_PRINTER_OUTPUT ("foo", decl);
2445 ASSERT_C_PRETTY_PRINTER_OUTPUT ("foo", wrapped_decl);
2446
2447 /* INTEGER_CST. */
2448 tree int_cst = build_int_cst (integer_type_node, 42);
2449 tree wrapped_cst = maybe_wrap_with_location (int_cst, BUILTINS_LOCATION);
2450 ASSERT_NE (wrapped_cst, int_cst);
2451 ASSERT_C_PRETTY_PRINTER_OUTPUT ("42", int_cst);
2452 ASSERT_C_PRETTY_PRINTER_OUTPUT ("42", wrapped_cst);
2453 }
2454
2455 /* Run all of the selftests within this file. */
2456
2457 void
2458 c_pretty_print_c_tests ()
2459 {
2460 test_location_wrappers ();
2461 }
2462
2463 } // namespace selftest
2464
2465 #endif /* CHECKING_P */