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

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children d34655255c78 1830386684a0
line wrap: on
line diff
--- a/gcc/c-family/c-pretty-print.c	Fri Oct 27 22:46:09 2017 +0900
+++ b/gcc/c-family/c-pretty-print.c	Thu Oct 25 07:37:49 2018 +0900
@@ -1,5 +1,5 @@
 /* Subroutines common to both C and C++ pretty-printers.
-   Copyright (C) 2002-2017 Free Software Foundation, Inc.
+   Copyright (C) 2002-2018 Free Software Foundation, Inc.
    Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
 
 This file is part of GCC.
@@ -28,6 +28,7 @@
 #include "attribs.h"
 #include "intl.h"
 #include "tree-pretty-print.h"
+#include "selftest.h"
 
 /* The pretty-printer code is primarily designed to closely follow
    (GNU) C and C++ grammars.  That is to be contrasted with spaghetti
@@ -191,7 +192,7 @@
 
 /* Pretty-print T using the type-cast notation '( type-name )'.  */
 
-static void
+void
 pp_c_type_cast (c_pretty_printer *pp, tree t)
 {
   pp_c_left_paren (pp);
@@ -907,7 +908,7 @@
 
 /* Pretty-print an INTEGER literal.  */
 
-static void
+void
 pp_c_integer_constant (c_pretty_printer *pp, tree i)
 {
   if (tree_fits_shwi_p (i))
@@ -967,21 +968,20 @@
     pp_unsupported_tree (pp, b);
 }
 
-/* Attempt to print out an ENUMERATOR.  Return true on success.  Else return
-   false; that means the value was obtained by a cast, in which case
-   print out the type-id part of the cast-expression -- the casted value
-   is then printed by pp_c_integer_literal.  */
+/* Given a value e of ENUMERAL_TYPE:
+   Print out the first ENUMERATOR id with value e, if one is found,
+   else print out the value as a C-style cast (type-id)value.  */
 
-static bool
+static void
 pp_c_enumeration_constant (c_pretty_printer *pp, tree e)
 {
-  bool value_is_named = true;
   tree type = TREE_TYPE (e);
   tree value;
 
   /* Find the name of this constant.  */
   for (value = TYPE_VALUES (type);
-       value != NULL_TREE && !tree_int_cst_equal (TREE_VALUE (value), e);
+       value != NULL_TREE
+	&& !tree_int_cst_equal (DECL_INITIAL (TREE_VALUE (value)), e);
        value = TREE_CHAIN (value))
     ;
 
@@ -991,10 +991,8 @@
     {
       /* Value must have been cast.  */
       pp_c_type_cast (pp, type);
-      value_is_named = false;
+      pp_c_integer_constant (pp, e);
     }
-
-  return value_is_named;
 }
 
 /* Print out a REAL value as a decimal-floating-constant.  */
@@ -1139,9 +1137,8 @@
 	  pp_c_bool_constant (this, e);
 	else if (type == char_type_node)
 	  pp_c_character_constant (this, e);
-	else if (TREE_CODE (type) == ENUMERAL_TYPE
-		 && pp_c_enumeration_constant (this, e))
-	  ;
+	else if (TREE_CODE (type) == ENUMERAL_TYPE)
+	  pp_c_enumeration_constant (this, e);
 	else
 	  pp_c_integer_constant (this, e);
       }
@@ -1379,8 +1376,9 @@
     case VECTOR_TYPE:
       if (TREE_CODE (e) == VECTOR_CST)
 	{
-	  unsigned i;
-	  for (i = 0; i < VECTOR_CST_NELTS (e); ++i)
+	  /* We don't create variable-length VECTOR_CSTs.  */
+	  unsigned int nunits = VECTOR_CST_NELTS (e).to_constant ();
+	  for (unsigned int i = 0; i < nunits; ++i)
 	    {
 	      if (i > 0)
 		pp_separate_with (pp, ',');
@@ -1482,17 +1480,6 @@
       pp_c_right_bracket (this);
       break;
 
-    case ARRAY_NOTATION_REF:
-      postfix_expression (ARRAY_NOTATION_ARRAY (e));
-      pp_c_left_bracket (this);
-      expression (ARRAY_NOTATION_START (e));
-      pp_colon (this);
-      expression (ARRAY_NOTATION_LENGTH (e));
-      pp_colon (this);
-      expression (ARRAY_NOTATION_STRIDE (e));
-      pp_c_right_bracket (this);
-      break;
-      
     case CALL_EXPR:
       {
 	call_expr_arg_iterator iter;
@@ -1819,7 +1806,8 @@
     case FIX_TRUNC_EXPR:
     CASE_CONVERT:
     case VIEW_CONVERT_EXPR:
-      pp_c_type_cast (pp, TREE_TYPE (e));
+      if (!location_wrapper_p (e))
+	pp_c_type_cast (pp, TREE_TYPE (e));
       pp_c_cast_expression (pp, TREE_OPERAND (e, 0));
       break;
 
@@ -1849,7 +1837,7 @@
       pp_c_whitespace (this);
       if (code == MULT_EXPR)
 	pp_c_star (this);
-      else if (code == TRUNC_DIV_EXPR)
+      else if (code != TRUNC_MOD_EXPR)
 	pp_slash (this);
       else
 	pp_modulo (this);
@@ -1876,6 +1864,7 @@
     {
     case POINTER_PLUS_EXPR:
     case PLUS_EXPR:
+    case POINTER_DIFF_EXPR:
     case MINUS_EXPR:
       pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
       pp_c_whitespace (pp);
@@ -2191,7 +2180,6 @@
     case POSTINCREMENT_EXPR:
     case POSTDECREMENT_EXPR:
     case ARRAY_REF:
-    case ARRAY_NOTATION_REF:
     case CALL_EXPR:
     case COMPONENT_REF:
     case BIT_FIELD_REF:
@@ -2292,6 +2280,7 @@
 
     case POINTER_PLUS_EXPR:
     case PLUS_EXPR:
+    case POINTER_DIFF_EXPR:
     case MINUS_EXPR:
       pp_c_additive_expression (this, e);
       break;
@@ -2348,7 +2337,7 @@
   if (pp_needs_newline (this))
     pp_newline_and_indent (this, 0);
 
-  dump_generic_node (this, stmt, pp_indentation (this), 0, true);
+  dump_generic_node (this, stmt, pp_indentation (this), TDF_NONE, true);
 }
 
 
@@ -2409,3 +2398,65 @@
 
   pp_c_identifier (pp, name);
 }
+
+#if CHECKING_P
+
+namespace selftest {
+
+/* Selftests for pretty-printing trees.  */
+
+/* Verify that EXPR printed by c_pretty_printer is EXPECTED, using
+   LOC as the effective location for any failures.  */
+
+static void
+assert_c_pretty_printer_output (const location &loc, const char *expected,
+				tree expr)
+{
+  c_pretty_printer pp;
+  pp.expression (expr);
+  ASSERT_STREQ_AT (loc, expected, pp_formatted_text (&pp));
+}
+
+/* Helper function for calling assert_c_pretty_printer_output.
+   This is to avoid having to write SELFTEST_LOCATION.  */
+
+#define ASSERT_C_PRETTY_PRINTER_OUTPUT(EXPECTED, EXPR) \
+  SELFTEST_BEGIN_STMT						\
+    assert_c_pretty_printer_output ((SELFTEST_LOCATION),	\
+				    (EXPECTED),		\
+				    (EXPR));			\
+  SELFTEST_END_STMT
+
+/* Verify that location wrappers don't show up in pretty-printed output.  */
+
+static void
+test_location_wrappers ()
+{
+  /* VAR_DECL.  */
+  tree id = get_identifier ("foo");
+  tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, id,
+			  integer_type_node);
+  tree wrapped_decl = maybe_wrap_with_location (decl, BUILTINS_LOCATION);
+  ASSERT_NE (wrapped_decl, decl);
+  ASSERT_C_PRETTY_PRINTER_OUTPUT ("foo", decl);
+  ASSERT_C_PRETTY_PRINTER_OUTPUT ("foo", wrapped_decl);
+
+  /* INTEGER_CST.  */
+  tree int_cst = build_int_cst (integer_type_node, 42);
+  tree wrapped_cst = maybe_wrap_with_location (int_cst, BUILTINS_LOCATION);
+  ASSERT_NE (wrapped_cst, int_cst);
+  ASSERT_C_PRETTY_PRINTER_OUTPUT ("42", int_cst);
+  ASSERT_C_PRETTY_PRINTER_OUTPUT ("42", wrapped_cst);
+}
+
+/* Run all of the selftests within this file.  */
+
+void
+c_pretty_print_c_tests ()
+{
+  test_location_wrappers ();
+}
+
+} // namespace selftest
+
+#endif /* CHECKING_P */