public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* Make "assignment to read-only location" more useful for arrays/pointers
@ 2007-08-09  5:53 Daniel Berlin
  2007-08-09  9:22 ` Andrew Pinski
  2007-08-09 18:48 ` Ian Lance Taylor
  0 siblings, 2 replies; 3+ messages in thread
From: Daniel Berlin @ 2007-08-09  5:53 UTC (permalink / raw)
  To: GCC Patches, Mark Mitchell

[-- Attachment #1: Type: text/plain, Size: 1956 bytes --]

Prior to this patch, for something like
int func()
{
  const int *arr;
  const int arr2[5];
  arr[0] = 1;
  arr[1] = 1;
  arr2[0] = 1;
  arr2[1] = 1;
}


We would print
../../../gcc-clean/gcc/testsuite/gcc.dg/readonly-loc.c: In function 'func':
../../../gcc-clean/gcc/testsuite/gcc.dg/readonly-loc.c:7: error:
assignment of read-only location
../../../gcc-clean/gcc/testsuite/gcc.dg/readonly-loc.c:8: error:
assignment of read-only location
../../../gcc-clean/gcc/testsuite/gcc.dg/readonly-loc.c:9: error:
assignment of read-only location
../../../gcc-clean/gcc/testsuite/gcc.dg/readonly-loc.c:10: error:
assignment of read-only location

We now print
[dannyb@daniel-berlins-macbook-pro-15:~/gccstuff/gcc-clean/build/gcc]>
./cc1 ../../../gcc-clean/gcc/testsuite/gcc.dg/readonly-loc.c
 func
../../../gcc-clean/gcc/testsuite/gcc.dg/readonly-loc.c: In function 'func':
../../../gcc-clean/gcc/testsuite/gcc.dg/readonly-loc.c:7: error:
assignment of read-only location '*arr'
../../../gcc-clean/gcc/testsuite/gcc.dg/readonly-loc.c:8: error:
assignment of read-only location '*(arr + 4u)'
../../../gcc-clean/gcc/testsuite/gcc.dg/readonly-loc.c:9: error:
assignment of read-only location 'arr2[0]'
../../../gcc-clean/gcc/testsuite/gcc.dg/readonly-loc.c:10: error:
assignment of read-only location 'arr2[1]'



Note that the first two messages are due to lowering of array like
pointer accesses.  I'm not sure we can do better, and I still think
that giving the user *some* indication here is better


Bootstrapped and regtested on i686-darwin

Okay for mainline?

in gcc/
2007-08-08  Daniel Berlin  <dberlin@dberlin.org>

	* c-typeck.c (readonly_error): Improve error for assignment.
	* c-pretty-print.c (pp_c_additive_expression): Handle pointer-plus
	expression.
	(pp_c_expression): Ditto.

in gcc/cp/

2007-08-08  Daniel Berlin  <dberlin@dberlin.org>

	* typeck2.c (readonly_error): Handle general expressions.
	* error.c (dump_expr): Handle POINTER_PLUS_EXPR

[-- Attachment #2: readonly-loc.diff --]
[-- Type: text/x-patch, Size: 3090 bytes --]

Index: testsuite/gcc.dg/readonly-loc.c
===================================================================
--- testsuite/gcc.dg/readonly-loc.c	(revision 0)
+++ testsuite/gcc.dg/readonly-loc.c	(revision 0)
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-O" } */
+int func()
+{
+  const int *arr;
+  const int arr2[5];
+  arr[0] = 1; /* { dg-error "assignment of read-only location" "*(arr)" } */
+  arr[1] = 1; /* { dg-error "assignment of read-only location" "*(arr + 4u)" } */
+  arr2[0] = 1; /* { dg-error "assignment of read-only location" "arr2\[0\]" } */
+  arr2[1] = 1; /* { dg-error "assignment of read-only location" "arr2\[1\]" } */
+}
Index: cp/error.c
===================================================================
--- cp/error.c	(revision 127012)
+++ cp/error.c	(working copy)
@@ -1613,6 +1613,10 @@ dump_expr (tree t, int flags)
 	dump_expr (TREE_OPERAND (t, 1), flags | TFF_EXPR_IN_PARENS);
       break;
 
+    case POINTER_PLUS_EXPR:
+      dump_binary_op ("+", t, flags);
+      break;
+
     case INIT_EXPR:
     case MODIFY_EXPR:
     case PLUS_EXPR:
Index: cp/typeck2.c
===================================================================
--- cp/typeck2.c	(revision 127012)
+++ cp/typeck2.c	(working copy)
@@ -105,7 +105,7 @@ readonly_error (tree arg, const char* st
   else if (TREE_CODE (arg) == FUNCTION_DECL)
     error ("%s of function %qD", string, arg);
   else
-    error ("%s of read-only location", string);
+    error ("%s of read-only location %qE", string, arg);
 }
 
 \f
Index: c-pretty-print.c
===================================================================
--- c-pretty-print.c	(revision 127012)
+++ c-pretty-print.c	(working copy)
@@ -1570,11 +1570,12 @@ pp_c_additive_expression (c_pretty_print
   enum tree_code code = TREE_CODE (e);
   switch (code)
     {
+    case POINTER_PLUS_EXPR:
     case PLUS_EXPR:
     case MINUS_EXPR:
       pp_c_additive_expression (pp, TREE_OPERAND (e, 0));
       pp_c_whitespace (pp);
-      if (code == PLUS_EXPR)
+      if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR)
 	pp_plus (pp);
       else
 	pp_minus (pp);
@@ -1948,6 +1949,7 @@ pp_c_expression (c_pretty_printer *pp, t
       pp_conditional_expression (pp, e);
       break;
 
+    case POINTER_PLUS_EXPR:
     case PLUS_EXPR:
     case MINUS_EXPR:
       pp_c_additive_expression (pp, e);
Index: c-typeck.c
===================================================================
--- c-typeck.c	(revision 127012)
+++ c-typeck.c	(working copy)
@@ -3152,10 +3152,11 @@ readonly_error (tree arg, enum lvalue_us
 			 G_("read-only variable %qD used as %<asm%> output")),
 	   arg);
   else
-    error (READONLY_MSG (G_("assignment of read-only location"),
-			 G_("increment of read-only location"),
-			 G_("decrement of read-only location"),
-			 G_("read-only location used as %<asm%> output")));
+    error (READONLY_MSG (G_("assignment of read-only location %qE"),
+			 G_("increment of read-only location %qE"),
+			 G_("decrement of read-only location %qE"),
+			 G_("read-only location %qE used as %<asm%> output")),
+	   arg);
 }
 
 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Make "assignment to read-only location" more useful for arrays/pointers
  2007-08-09  5:53 Make "assignment to read-only location" more useful for arrays/pointers Daniel Berlin
@ 2007-08-09  9:22 ` Andrew Pinski
  2007-08-09 18:48 ` Ian Lance Taylor
  1 sibling, 0 replies; 3+ messages in thread
From: Andrew Pinski @ 2007-08-09  9:22 UTC (permalink / raw)
  To: Daniel Berlin; +Cc: GCC Patches, Mark Mitchell

On 8/9/07, Daniel Berlin <dberlin@dberlin.org> wrote:

>         * c-pretty-print.c (pp_c_additive_expression): Handle pointer-plus
>         expression.
>         (pp_c_expression): Ditto.

>         * error.c (dump_expr): Handle POINTER_PLUS_EXPR

Thanks for doing these two, I was going to handle them on my next
batch of patches for pointer plus anyways because C++ currently
produces funny errors in some cases already.

Thanks,
Andrew Pinski

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Make "assignment to read-only location" more useful for arrays/pointers
  2007-08-09  5:53 Make "assignment to read-only location" more useful for arrays/pointers Daniel Berlin
  2007-08-09  9:22 ` Andrew Pinski
@ 2007-08-09 18:48 ` Ian Lance Taylor
  1 sibling, 0 replies; 3+ messages in thread
From: Ian Lance Taylor @ 2007-08-09 18:48 UTC (permalink / raw)
  To: Daniel Berlin; +Cc: GCC Patches, Mark Mitchell

"Daniel Berlin" <dberlin@dberlin.org> writes:

> in gcc/
> 2007-08-08  Daniel Berlin  <dberlin@dberlin.org>
> 
> 	* c-typeck.c (readonly_error): Improve error for assignment.
> 	* c-pretty-print.c (pp_c_additive_expression): Handle pointer-plus
> 	expression.
> 	(pp_c_expression): Ditto.
> 
> in gcc/cp/
> 
> 2007-08-08  Daniel Berlin  <dberlin@dberlin.org>
> 
> 	* typeck2.c (readonly_error): Handle general expressions.
> 	* error.c (dump_expr): Handle POINTER_PLUS_EXPR

This is OK.

Thanks.

Ian

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2007-08-09 18:48 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-08-09  5:53 Make "assignment to read-only location" more useful for arrays/pointers Daniel Berlin
2007-08-09  9:22 ` Andrew Pinski
2007-08-09 18:48 ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).