[C++ Patch] Fix some simple location issues​ (2)

Paolo Carlini paolo.carlini@oracle.com
Sat Jun 11 00:36:00 GMT 2016


Hi,

yesterday I noticed a case, a variant of testcase array43.C which I had 
just committed, where using EXPR_LOR_OR_LOC (init, input_location) 
actually made a difference because init had an usable location. The 
patchlet represents also a nano clean-up/fix-up: elsewhere in the same 
function EXPR_LOC_OR_LOC was already used and a permerror had an 
explicit input_location as first argument. Tested x86_64-linux.

Thanks,
Paolo.

//////////////////////////
-------------- next part --------------
/cp
2016-06-12  Paolo Carlini  <paolo.carlini@oracle.com>

	* typeck2.c (digest_init_r): Use EXPR_LOC_OR_LOC on init.

/testsuite
2016-06-12  Paolo Carlini  <paolo.carlini@oracle.com>

	* g++.dg/init/array46.C: New.
-------------- next part --------------
Index: cp/typeck2.c
===================================================================
--- cp/typeck2.c	(revision 237318)
+++ cp/typeck2.c	(working copy)
@@ -1009,6 +1009,8 @@ digest_init_r (tree type, tree init, bool nested,
   if (TREE_CODE (init) == NON_LVALUE_EXPR)
     init = TREE_OPERAND (init, 0);
 
+  location_t loc = EXPR_LOC_OR_LOC (init, input_location);
+
   /* Initialization of an array of chars from a string constant. The initializer
      can be optionally enclosed in braces, but reshape_init has already removed
      them if they were present.  */
@@ -1017,7 +1019,7 @@ digest_init_r (tree type, tree init, bool nested,
       if (nested && !TYPE_DOMAIN (type))
 	{
 	  /* C++ flexible array members have a null domain.  */
-	  pedwarn (EXPR_LOC_OR_LOC (init, input_location), OPT_Wpedantic,
+	  pedwarn (loc, OPT_Wpedantic,
 		   "initialization of a flexible array member");
 	}
 
@@ -1033,7 +1035,7 @@ digest_init_r (tree type, tree init, bool nested,
 	      if (char_type != char_type_node)
 		{
 		  if (complain & tf_error)
-		    error ("char-array initialized from wide string");
+		    error_at (loc, "char-array initialized from wide string");
 		  return error_mark_node;
 		}
 	    }
@@ -1042,14 +1044,15 @@ digest_init_r (tree type, tree init, bool nested,
 	      if (char_type == char_type_node)
 		{
 		  if (complain & tf_error)
-		    error ("int-array initialized from non-wide string");
+		    error_at (loc,
+			      "int-array initialized from non-wide string");
 		  return error_mark_node;
 		}
 	      else if (char_type != typ1)
 		{
 		  if (complain & tf_error)
-		    error ("int-array initialized from incompatible "
-			   "wide string");
+		    error_at (loc, "int-array initialized from incompatible "
+			      "wide string");
 		  return error_mark_node;
 		}
 	    }
@@ -1069,7 +1072,7 @@ digest_init_r (tree type, tree init, bool nested,
 		 counted in the length of the constant, but in C++ this would
 		 be invalid.  */
 	      if (size < TREE_STRING_LENGTH (init))
-		permerror (input_location, "initializer-string for array "
+		permerror (loc, "initializer-string for array "
 			   "of chars is too long");
 	    }
 	  return init;
@@ -1122,8 +1125,8 @@ digest_init_r (tree type, tree init, bool nested,
       if (COMPOUND_LITERAL_P (init) && TREE_CODE (type) == ARRAY_TYPE)
 	{
 	  if (complain & tf_error)
-	    error ("cannot initialize aggregate of type %qT with "
-		   "a compound literal", type);
+	    error_at (loc, "cannot initialize aggregate of type %qT with "
+		      "a compound literal", type);
 
 	  return error_mark_node;
 	}
@@ -1140,8 +1143,8 @@ digest_init_r (tree type, tree init, bool nested,
 	    return init;
 
 	  if (complain & tf_error)
-	    error ("array must be initialized with a brace-enclosed"
-		   " initializer");
+	    error_at (loc, "array must be initialized with a brace-enclosed"
+		      " initializer");
 	  return error_mark_node;
 	}
 
Index: testsuite/g++.dg/init/array46.C
===================================================================
--- testsuite/g++.dg/init/array46.C	(revision 0)
+++ testsuite/g++.dg/init/array46.C	(working copy)
@@ -0,0 +1,3 @@
+int foo();
+int a[] = foo();  // { dg-error "14:initializer fails to determine size" }
+// { dg-error "14:array must be initialized" "" { target *-*-* } 2 }


More information about the Gcc-patches mailing list