* [C PATCH] Fix up diagnostics (PR c/25801)
@ 2014-04-18 20:30 Marek Polacek
2014-05-01 22:54 ` Joseph S. Myers
0 siblings, 1 reply; 4+ messages in thread
From: Marek Polacek @ 2014-04-18 20:30 UTC (permalink / raw)
To: GCC Patches; +Cc: Joseph S. Myers
Here's a patch that tidies up diagnostics given when there's
an arithmetic on pointer to an incomplete type. Firstly, we ought not
to say "unknown structure" when the type is not a structure; secondly,
we shouldn't report identical error twice.
(It might make sense to instead "increment/decrement of" just say
"arithmetic on", but I'm not changing that now.)
Regtested on x86_64-unknown-linux-gnu, bootstrapped on
powerpc64-unknown-linux-gnu, ok for trunk?
2014-04-18 Marek Polacek <polacek@redhat.com>
PR c/25801
* c-typeck.c (c_size_in_bytes): Don't call error.
(pointer_diff): Remove comment.
(build_unary_op): Improve error messages.
* gcc.dg/pr25801.c: New test.
diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c
index 65aad45..bc977a7 100644
--- gcc/c/c-typeck.c
+++ gcc/c/c-typeck.c
@@ -1760,15 +1760,10 @@ c_size_in_bytes (const_tree type)
{
enum tree_code code = TREE_CODE (type);
- if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
+ if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
+ || !COMPLETE_OR_VOID_TYPE_P (type))
return size_one_node;
- if (!COMPLETE_OR_VOID_TYPE_P (type))
- {
- error ("arithmetic on pointer to an incomplete type");
- return size_one_node;
- }
-
/* Convert in case a char is more than one unit. */
return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
size_int (TYPE_PRECISION (char_type_node)
@@ -3528,7 +3523,6 @@ pointer_diff (location_t loc, tree op0, tree op1)
if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
error_at (loc, "arithmetic on pointer to an incomplete type");
- /* This generates an error if op0 is pointer to incomplete type. */
op1 = c_size_in_bytes (target_type);
if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
@@ -4002,16 +3996,18 @@ build_unary_op (location_t location,
if (typecode == POINTER_TYPE)
{
- /* If pointer target is an undefined struct,
+ /* If pointer target is an incomplete type,
we just cannot know how to do the arithmetic. */
if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
{
if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
error_at (location,
- "increment of pointer to unknown structure");
+ "increment of pointer to an incomplete type %qT",
+ TREE_TYPE (argtype));
else
error_at (location,
- "decrement of pointer to unknown structure");
+ "decrement of pointer to an incomplete type %qT",
+ TREE_TYPE (argtype));
}
else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
|| TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
diff --git gcc/testsuite/gcc.dg/pr25801.c gcc/testsuite/gcc.dg/pr25801.c
index e69de29..70b4ef8 100644
--- gcc/testsuite/gcc.dg/pr25801.c
+++ gcc/testsuite/gcc.dg/pr25801.c
@@ -0,0 +1,19 @@
+/* PR c/25801 */
+/* { dg-do compile } */
+
+int (*a)[];
+struct S *s;
+
+void
+f (void)
+{
+ a++; /* { dg-error "increment of pointer to an incomplete type" } */
+ ++a; /* { dg-error "increment of pointer to an incomplete type" } */
+ a--; /* { dg-error "decrement of pointer to an incomplete type" } */
+ --a; /* { dg-error "decrement of pointer to an incomplete type" } */
+
+ s++; /* { dg-error "increment of pointer to an incomplete type" } */
+ ++s; /* { dg-error "increment of pointer to an incomplete type" } */
+ s--; /* { dg-error "decrement of pointer to an incomplete type" } */
+ --s; /* { dg-error "decrement of pointer to an incomplete type" } */
+}
Marek
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [C PATCH] Fix up diagnostics (PR c/25801)
2014-04-18 20:30 [C PATCH] Fix up diagnostics (PR c/25801) Marek Polacek
@ 2014-05-01 22:54 ` Joseph S. Myers
2014-05-02 7:44 ` Marek Polacek
0 siblings, 1 reply; 4+ messages in thread
From: Joseph S. Myers @ 2014-05-01 22:54 UTC (permalink / raw)
To: Marek Polacek; +Cc: GCC Patches
On Fri, 18 Apr 2014, Marek Polacek wrote:
> @@ -1760,15 +1760,10 @@ c_size_in_bytes (const_tree type)
I think the comment on this function should be updated to explain the
interface contract when an incomplete (or function) type is passed (i.e.
return size_one_node, caller is responsible for any diagnostics).
> diff --git gcc/testsuite/gcc.dg/pr25801.c gcc/testsuite/gcc.dg/pr25801.c
> index e69de29..70b4ef8 100644
> --- gcc/testsuite/gcc.dg/pr25801.c
> +++ gcc/testsuite/gcc.dg/pr25801.c
> @@ -0,0 +1,19 @@
> +/* PR c/25801 */
> +/* { dg-do compile } */
> +
> +int (*a)[];
> +struct S *s;
> +
> +void
> +f (void)
> +{
> + a++; /* { dg-error "increment of pointer to an incomplete type" } */
> + ++a; /* { dg-error "increment of pointer to an incomplete type" } */
> + a--; /* { dg-error "decrement of pointer to an incomplete type" } */
> + --a; /* { dg-error "decrement of pointer to an incomplete type" } */
> +
> + s++; /* { dg-error "increment of pointer to an incomplete type" } */
> + ++s; /* { dg-error "increment of pointer to an incomplete type" } */
> + s--; /* { dg-error "decrement of pointer to an incomplete type" } */
> + --s; /* { dg-error "decrement of pointer to an incomplete type" } */
> +}
The test should I think also cover incomplete union types, and pointer
subtraction for all the different cases of incomplete types, and the +=
and -= operators (i.e. cover invalid arithmetic on pointers to incomplete
types more thoroughly, even if not actually affected by this patch).
--
Joseph S. Myers
joseph@codesourcery.com
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [C PATCH] Fix up diagnostics (PR c/25801)
2014-05-01 22:54 ` Joseph S. Myers
@ 2014-05-02 7:44 ` Marek Polacek
2014-05-02 17:51 ` Joseph S. Myers
0 siblings, 1 reply; 4+ messages in thread
From: Marek Polacek @ 2014-05-02 7:44 UTC (permalink / raw)
To: Joseph S. Myers; +Cc: GCC Patches
On Thu, May 01, 2014 at 10:54:18PM +0000, Joseph S. Myers wrote:
> I think the comment on this function should be updated to explain the
> interface contract when an incomplete (or function) type is passed (i.e.
> return size_one_node, caller is responsible for any diagnostics).
Done.
> The test should I think also cover incomplete union types, and pointer
> subtraction for all the different cases of incomplete types, and the +=
> and -= operators (i.e. cover invalid arithmetic on pointers to incomplete
> types more thoroughly, even if not actually affected by this patch).
Done.
Another change is that I used COMPLETE_TYPE_P instead of
COMPLETE_OR_VOID_TYPE_P, that would be a bug and before it didn't make
much sense: we checked VOID_TYPE before.
Tested x86_64-linux, ok now?
2014-05-02 Marek Polacek <polacek@redhat.com>
PR c/25801
* c-typeck.c (c_size_in_bytes): Update comment. Don't call error.
Return size_one_node when the type is not complete.
(pointer_diff): Remove comment.
(build_unary_op): Improve error messages.
* gcc.dg/pr25801.c: New test.
diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c
index 98567b4..41770bc 100644
--- gcc/c/c-typeck.c
+++ gcc/c/c-typeck.c
@@ -1754,22 +1754,20 @@ type_lists_compatible_p (const_tree args1, const_tree args2,
}
}
\f
-/* Compute the size to increment a pointer by. */
+/* Compute the size to increment a pointer by. When a function type or void
+ type or incomplete type is passed, size_one_node is returned.
+ This function does not emit any diagnostics; the caller is responsible
+ for that. */
static tree
c_size_in_bytes (const_tree type)
{
enum tree_code code = TREE_CODE (type);
- if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
+ if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
+ || !COMPLETE_TYPE_P (type))
return size_one_node;
- if (!COMPLETE_OR_VOID_TYPE_P (type))
- {
- error ("arithmetic on pointer to an incomplete type");
- return size_one_node;
- }
-
/* Convert in case a char is more than one unit. */
return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
size_int (TYPE_PRECISION (char_type_node)
@@ -3530,7 +3528,6 @@ pointer_diff (location_t loc, tree op0, tree op1)
if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
error_at (loc, "arithmetic on pointer to an incomplete type");
- /* This generates an error if op0 is pointer to incomplete type. */
op1 = c_size_in_bytes (target_type);
if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
@@ -4004,16 +4001,18 @@ build_unary_op (location_t location,
if (typecode == POINTER_TYPE)
{
- /* If pointer target is an undefined struct,
+ /* If pointer target is an incomplete type,
we just cannot know how to do the arithmetic. */
if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
{
if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
error_at (location,
- "increment of pointer to unknown structure");
+ "increment of pointer to an incomplete type %qT",
+ TREE_TYPE (argtype));
else
error_at (location,
- "decrement of pointer to unknown structure");
+ "decrement of pointer to an incomplete type %qT",
+ TREE_TYPE (argtype));
}
else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
|| TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
diff --git gcc/testsuite/gcc.dg/pr25801.c gcc/testsuite/gcc.dg/pr25801.c
index e69de29..10b53d3 100644
--- gcc/testsuite/gcc.dg/pr25801.c
+++ gcc/testsuite/gcc.dg/pr25801.c
@@ -0,0 +1,44 @@
+/* PR c/25801 */
+/* { dg-do compile } */
+/* { dg-options "-std=c99" } */
+
+int (*a)[];
+struct S *s;
+union U *u;
+enum E *e;
+
+void
+f (void)
+{
+ a++; /* { dg-error "increment of pointer to an incomplete type" } */
+ ++a; /* { dg-error "increment of pointer to an incomplete type" } */
+ a--; /* { dg-error "decrement of pointer to an incomplete type" } */
+ --a; /* { dg-error "decrement of pointer to an incomplete type" } */
+ a += 1; /* { dg-error "invalid use of array with unspecified bounds" } */
+ a -= 1; /* { dg-error "invalid use of array with unspecified bounds" } */
+ a - a; /* { dg-error "arithmetic on pointer to an incomplete type" } */
+
+ s++; /* { dg-error "increment of pointer to an incomplete type" } */
+ ++s; /* { dg-error "increment of pointer to an incomplete type" } */
+ s--; /* { dg-error "decrement of pointer to an incomplete type" } */
+ --s; /* { dg-error "decrement of pointer to an incomplete type" } */
+ s += 1; /* { dg-error "invalid use of undefined type" } */
+ s -= 1; /* { dg-error "invalid use of undefined type" } */
+ s - s; /* { dg-error "arithmetic on pointer to an incomplete type" } */
+
+ u++; /* { dg-error "increment of pointer to an incomplete type" } */
+ ++u; /* { dg-error "increment of pointer to an incomplete type" } */
+ u--; /* { dg-error "decrement of pointer to an incomplete type" } */
+ --u; /* { dg-error "decrement of pointer to an incomplete type" } */
+ u += 1; /* { dg-error "invalid use of undefined type" } */
+ u -= 1; /* { dg-error "invalid use of undefined type" } */
+ u - u; /* { dg-error "arithmetic on pointer to an incomplete type" } */
+
+ e++; /* { dg-error "increment of pointer to an incomplete type" } */
+ ++e; /* { dg-error "increment of pointer to an incomplete type" } */
+ e--; /* { dg-error "decrement of pointer to an incomplete type" } */
+ --e; /* { dg-error "decrement of pointer to an incomplete type" } */
+ e += 1; /* { dg-error "invalid use of undefined type" } */
+ e -= 1; /* { dg-error "invalid use of undefined type" } */
+ e - e; /* { dg-error "arithmetic on pointer to an incomplete type" } */
+}
Marek
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [C PATCH] Fix up diagnostics (PR c/25801)
2014-05-02 7:44 ` Marek Polacek
@ 2014-05-02 17:51 ` Joseph S. Myers
0 siblings, 0 replies; 4+ messages in thread
From: Joseph S. Myers @ 2014-05-02 17:51 UTC (permalink / raw)
To: Marek Polacek; +Cc: GCC Patches
On Fri, 2 May 2014, Marek Polacek wrote:
> On Thu, May 01, 2014 at 10:54:18PM +0000, Joseph S. Myers wrote:
> > I think the comment on this function should be updated to explain the
> > interface contract when an incomplete (or function) type is passed (i.e.
> > return size_one_node, caller is responsible for any diagnostics).
>
> Done.
>
> > The test should I think also cover incomplete union types, and pointer
> > subtraction for all the different cases of incomplete types, and the +=
> > and -= operators (i.e. cover invalid arithmetic on pointers to incomplete
> > types more thoroughly, even if not actually affected by this patch).
>
> Done.
>
> Another change is that I used COMPLETE_TYPE_P instead of
> COMPLETE_OR_VOID_TYPE_P, that would be a bug and before it didn't make
> much sense: we checked VOID_TYPE before.
>
> Tested x86_64-linux, ok now?
OK.
--
Joseph S. Myers
joseph@codesourcery.com
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2014-05-02 17:51 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-04-18 20:30 [C PATCH] Fix up diagnostics (PR c/25801) Marek Polacek
2014-05-01 22:54 ` Joseph S. Myers
2014-05-02 7:44 ` Marek Polacek
2014-05-02 17:51 ` Joseph S. Myers
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).