Patchwork [C] Add location to pedwarn_init

login
register
mail settings
Submitter Marek Polacek
Date May 10, 2014, 11:31 a.m.
Message ID <20140510113100.GC23554@redhat.com>
Download mbox | patch
Permalink /patch/347658/
State New
Headers show

Comments

Marek Polacek - May 10, 2014, 11:31 a.m.
As the subject says.  Along with some whitespace fixes.

Regtested/bootstrapped on x86_64-linux, ok for trunk?

2014-05-10  Marek Polacek  <polacek@redhat.com>

	* c-parser.c (c_parser_declaration_or_fndef): Pass init_loc to
	maybe_warn_string_init.
	(c_parser_postfix_expression_after_paren_type): Pass type_loc to
	maybe_warn_string_init.
	* c-tree.h (maybe_warn_string_init): Update declaration.
	* c-typeck.c (maybe_warn_string_init): Add location parameter.
	Call pedwarn_init with loc instead of with input_location.
	(digest_init): Pass init_loc to maybe_warn_string_init.
	(pop_init_level): Call pedwarn_init with loc instead of with
	input_location.
	(set_init_index): Likewise.
	(process_init_element): Likewise.

	* gcc.dg/pedwarn-init.c: New test.
	* gcc.dg/init-string-1.c: Adjust dg-error.


	Marek
Joseph S. Myers - May 10, 2014, 4:03 p.m.
On Sat, 10 May 2014, Marek Polacek wrote:

> As the subject says.  Along with some whitespace fixes.
> 
> Regtested/bootstrapped on x86_64-linux, ok for trunk?

OK.

Patch

diff --git gcc/c/c-parser.c gcc/c/c-parser.c
index 432bf96..6e8554d 100644
--- gcc/c/c-parser.c
+++ gcc/c/c-parser.c
@@ -1777,9 +1777,9 @@  c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
 		}
 	      if (d != error_mark_node)
 		{
-		  maybe_warn_string_init (TREE_TYPE (d), init);
+		  maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
 		  finish_decl (d, init_loc, init.value,
-		      	       init.original_type, asm_name);
+			       init.original_type, asm_name);
 		}
 	    }
 	  else
@@ -7599,7 +7599,7 @@  c_parser_postfix_expression_after_paren_type (c_parser *parser,
     }
   init = c_parser_braced_init (parser, type, false);
   finish_init ();
-  maybe_warn_string_init (type, init);
+  maybe_warn_string_init (type_loc, type, init);
 
   if (type != error_mark_node
       && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
diff --git gcc/c/c-tree.h gcc/c/c-tree.h
index ae932fa..e7dcb35 100644
--- gcc/c/c-tree.h
+++ gcc/c/c-tree.h
@@ -602,7 +602,7 @@  extern tree build_compound_expr (location_t, tree, tree);
 extern tree c_cast_expr (location_t, struct c_type_name *, tree);
 extern tree build_c_cast (location_t, tree, tree);
 extern void store_init_value (location_t, tree, tree, tree);
-extern void maybe_warn_string_init (tree, struct c_expr);
+extern void maybe_warn_string_init (location_t, tree, struct c_expr);
 extern void start_init (tree, tree, int);
 extern void finish_init (void);
 extern void really_start_incremental_init (tree);
diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c
index 6798801..4b1fe66 100644
--- gcc/c/c-typeck.c
+++ gcc/c/c-typeck.c
@@ -5596,13 +5596,13 @@  warning_init (location_t loc, int opt, const char *gmsgid)
    object of type TYPE.  */
 
 void
-maybe_warn_string_init (tree type, struct c_expr expr)
+maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
 {
   if (pedantic
       && TREE_CODE (type) == ARRAY_TYPE
       && TREE_CODE (expr.value) == STRING_CST
       && expr.original_code != STRING_CST)
-    pedwarn_init (input_location, OPT_Wpedantic,
+    pedwarn_init (loc, OPT_Wpedantic,
 		  "array initialized from parenthesized string constant");
 }
 
@@ -5657,14 +5657,14 @@  convert_for_assignment (location_t location, location_t expr_loc, tree type,
   /* This macro is used to emit diagnostics to ensure that all format
      strings are complete sentences, visible to gettext and checked at
      compile time.  */
-#define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE)             	 \
+#define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE)		 \
   do {                                                                   \
     switch (errtype)                                                     \
       {                                                                  \
       case ic_argpass:                                                   \
         if (pedwarn (LOCATION, OPT, AR, parmnum, rname))                 \
           inform ((fundecl && !DECL_IS_BUILTIN (fundecl))	         \
-	      	  ? DECL_SOURCE_LOCATION (fundecl) : LOCATION,		 \
+		  ? DECL_SOURCE_LOCATION (fundecl) : LOCATION,		 \
                   "expected %qT but argument is of type %qT",            \
                   type, rhstype);                                        \
         break;                                                           \
@@ -5675,7 +5675,7 @@  convert_for_assignment (location_t location, location_t expr_loc, tree type,
         pedwarn_init (LOCATION, OPT, IN);                                \
         break;                                                           \
       case ic_return:                                                    \
-        pedwarn (LOCATION, OPT, RE);                                 	 \
+        pedwarn (LOCATION, OPT, RE);					 \
         break;                                                           \
       default:                                                           \
         gcc_unreachable ();                                              \
@@ -6541,7 +6541,7 @@  digest_init (location_t init_loc, tree type, tree init, tree origtype,
 	  expr.value = inside_init;
 	  expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
 	  expr.original_type = NULL;
-	  maybe_warn_string_init (type, expr);
+	  maybe_warn_string_init (init_loc, type, expr);
 
 	  if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
 	    pedwarn_init (init_loc, OPT_Wpedantic,
@@ -7430,7 +7430,7 @@  pop_init_level (location_t loc, int implicit,
 	  if (constructor_depth > 2)
 	    error_init (loc, "initialization of flexible array member in a nested context");
 	  else
-	    pedwarn_init (input_location, OPT_Wpedantic,
+	    pedwarn_init (loc, OPT_Wpedantic,
 			  "initialization of a flexible array member");
 
 	  /* We have already issued an error message for the existence
@@ -7650,7 +7650,7 @@  push_range_stack (tree range_end, struct obstack * braced_init_obstack)
 
 void
 set_init_index (location_t loc, tree first, tree last,
-		struct obstack * braced_init_obstack)
+		struct obstack *braced_init_obstack)
 {
   if (set_designator (loc, 1, braced_init_obstack))
     return;
@@ -7668,7 +7668,7 @@  set_init_index (location_t loc, tree first, tree last,
     {
       first = c_fully_fold (first, false, NULL);
       if (TREE_CODE (first) == INTEGER_CST)
-	pedwarn_init (input_location, OPT_Wpedantic,
+	pedwarn_init (loc, OPT_Wpedantic,
 		      "array index in initializer is not "
 		      "an integer constant expression");
     }
@@ -7677,7 +7677,7 @@  set_init_index (location_t loc, tree first, tree last,
     {
       last = c_fully_fold (last, false, NULL);
       if (TREE_CODE (last) == INTEGER_CST)
-	pedwarn_init (input_location, OPT_Wpedantic,
+	pedwarn_init (loc, OPT_Wpedantic,
 		      "array index in initializer is not "
 		      "an integer constant expression");
     }
@@ -8745,7 +8745,7 @@  process_init_element (location_t loc, struct c_expr value, bool implicit,
 
 	  if (constructor_fields == 0)
 	    {
-	      pedwarn_init (input_location, 0,
+	      pedwarn_init (loc, 0,
 			    "excess elements in union initializer");
 	      break;
 	    }
@@ -8835,7 +8835,7 @@  process_init_element (location_t loc, struct c_expr value, bool implicit,
 	      && (tree_int_cst_lt (constructor_max_index, constructor_index)
 		  || integer_all_onesp (constructor_max_index)))
 	    {
-	      pedwarn_init (input_location, 0,
+	      pedwarn_init (loc, 0,
 			    "excess elements in array initializer");
 	      break;
 	    }
@@ -8869,7 +8869,7 @@  process_init_element (location_t loc, struct c_expr value, bool implicit,
 	    always have a fixed size derived from their type.  */
 	  if (tree_int_cst_lt (constructor_max_index, constructor_index))
 	    {
-	      pedwarn_init (input_location, 0,
+	      pedwarn_init (loc, 0,
 			    "excess elements in vector initializer");
 	      break;
 	    }
@@ -8901,7 +8901,7 @@  process_init_element (location_t loc, struct c_expr value, bool implicit,
       else if (constructor_type != error_mark_node
 	       && constructor_fields == 0)
 	{
-	  pedwarn_init (input_location, 0,
+	  pedwarn_init (loc, 0,
 			"excess elements in scalar initializer");
 	  break;
 	}
@@ -9413,7 +9413,7 @@  do_case (location_t loc, tree low_value, tree high_value)
     {
       low_value = c_fully_fold (low_value, false, NULL);
       if (TREE_CODE (low_value) == INTEGER_CST)
-	pedwarn (input_location, OPT_Wpedantic,
+	pedwarn (loc, OPT_Wpedantic,
 		 "case label is not an integer constant expression");
     }
 
diff --git gcc/testsuite/gcc.dg/init-string-1.c gcc/testsuite/gcc.dg/init-string-1.c
index ace3b34..a33f741 100644
--- gcc/testsuite/gcc.dg/init-string-1.c
+++ gcc/testsuite/gcc.dg/init-string-1.c
@@ -30,7 +30,7 @@  struct s j = {
   1,
   (L"j")
 }; /* { dg-bogus "warning" "warning in place of error" } */
-/* { dg-error "parenthesized|near init" "paren array" { target *-*-* } 32 } */
+/* { dg-error "parenthesized|near init" "paren array" { target *-*-* } 31 } */
 struct s k = {
   (("k")), /* { dg-bogus "warning" "warning in place of error" } */
   /* { dg-error "parenthesized|near init" "paren array" { target *-*-* } 35 } */
@@ -48,7 +48,7 @@  struct s m = {
   .c = L"m",
   .a = ("m")
 }; /* { dg-bogus "warning" "warning in place of error" } */
-/* { dg-error "parenthesized|near init" "paren array" { target *-*-* } 50 } */
+/* { dg-error "parenthesized|near init" "paren array" { target *-*-* } 49 } */
 
 char *n = (char []){ "n" };
 
diff --git gcc/testsuite/gcc.dg/pedwarn-init.c gcc/testsuite/gcc.dg/pedwarn-init.c
index e69de29..1261115 100644
--- gcc/testsuite/gcc.dg/pedwarn-init.c
+++ gcc/testsuite/gcc.dg/pedwarn-init.c
@@ -0,0 +1,19 @@ 
+/* { dg-do compile } */
+/* { dg-options "-std=gnu99 -Wpedantic" } */
+/* { dg-prune-output ".*near initialization for.*" } */
+
+typedef unsigned vec __attribute__ ((vector_size (2 * sizeof (int))));
+union u { int a; double d; };
+struct S { int i; char fam[]; };
+
+int i;
+vec v = { 1, 2, 3 }; /* { dg-warning "17:excess elements in vector initializer" } */
+int a1 = { 1, 2 }; /* { dg-warning "15:excess elements in scalar initializer" } */
+int a2[2] = { 1, 2, 3 }; /* { dg-warning "21:excess elements in array initializer" } */
+int a3[] = { [1 ? 1 : i] = 0 }; /* { dg-warning "15:array index in initializer is not an integer constant expression" } */
+int a4[] = { [1 ... 1 ? 2 : i] = 0 }; /* { dg-warning "15:array index in initializer is not an integer constant expression" } */
+char a5[] = ("lol"); /* { dg-warning "13:array initialized from parenthesized string constant" } */
+char a6[] = { ("foo") }; /* { dg-warning "13:array initialized from parenthesized string constant" } */
+char *a7 = (char []) { ("bar") }; /* { dg-warning "12:array initialized from parenthesized string constant" } */
+union u u = { 1, 1.0 }; /* { dg-warning "18:excess elements in union initializer" } */
+struct S s = { 1, 2 }; /* { dg-warning "14:initialization of a flexible array member" } */