]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fix use of COMPLETE_TYPE_P for -Wstrict-aliasing=1
authorrsandifo <rsandifo@138bc75d-0d04-0410-961f-82ee72b054a4>
Fri, 26 Apr 2019 10:33:10 +0000 (10:33 +0000)
committerrsandifo <rsandifo@138bc75d-0d04-0410-961f-82ee72b054a4>
Fri, 26 Apr 2019 10:33:10 +0000 (10:33 +0000)
The handling of -Wstrict-aliasing=1 applied COMPLETE_TYPE_P to the
pointer type rather than the pointer target, so missed the warnings
for "struct incomplete" in the testcase.

I couldn't find any existing C tests for -Wstrict-aliasing=1,
so I added a few extra tests besides the ones fixed by the patch.
I'm sure there's lots more we could test -- this is just supposed
to be better than the status quo (i.e. nothing).

2019-04-26  Richard Sandiford  <richard.sandiford@arm.com>

gcc/c-family/
* c-warn.c (strict_aliasing_warning): Apply COMPLETE_TYPE_P to
the pointer target rather than the pointer itself.

gcc/testsuite/
* gcc.dg/alias-16.c: New test.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@270594 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/c-family/ChangeLog
gcc/c-family/c-warn.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/alias-16.c [new file with mode: 0644]

index 12d4587bb0d2b9260ddd20a63c76bef4879efe03..9a662f062f2e4018b127a84c497e7a188f0dbea8 100644 (file)
@@ -1,3 +1,8 @@
+2019-04-26  Richard Sandiford  <richard.sandiford@arm.com>
+
+       * c-warn.c (strict_aliasing_warning): Apply COMPLETE_TYPE_P to
+       the pointer target rather than the pointer itself.
+
 2019-04-19  Jakub Jelinek  <jakub@redhat.com>
 
        PR c/89888
index 322cf98eb02b669bd636396097048133db94bd65..f95eba96a70bb7cb2b54440e889e7e9d0b23996a 100644 (file)
@@ -746,7 +746,7 @@ strict_aliasing_warning (location_t loc, tree type, tree expr)
         are not revealed at higher levels.  */
       alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
       alias_set_type set2 = get_alias_set (TREE_TYPE (type));
-      if (!COMPLETE_TYPE_P (type)
+      if (!COMPLETE_TYPE_P (TREE_TYPE (type))
          || !alias_sets_must_conflict_p (set1, set2))
        {
          warning_at (loc, OPT_Wstrict_aliasing,
index 5ea04dc99dc2f7b828fc1abfcec7b3ad5f808fe5..90e8d8b2030df099a442fa5e140b9649a1927bb5 100644 (file)
@@ -1,3 +1,7 @@
+2019-04-26  Richard Sandiford  <richard.sandiford@arm.com>
+
+       * gcc.dg/alias-16.c: New test.
+
 2019-04-25  Martin Liska  <mliska@suse.cz>
            H.J. Lu  <hongjiu.lu@intel.com>
 
diff --git a/gcc/testsuite/gcc.dg/alias-16.c b/gcc/testsuite/gcc.dg/alias-16.c
new file mode 100644 (file)
index 0000000..8f5aa35
--- /dev/null
@@ -0,0 +1,46 @@
+/* { dg-do compile } */
+/* { dg-options "-Wstrict-aliasing=1 -fstrict-aliasing" } */
+
+struct incomplete;
+struct s1 { int i; };
+struct s2 { double d; };
+
+void
+f (int *i, double *d, struct s1 *s1, struct s2 *s2, char *c)
+{
+  (char *) i;
+  (char *) d;
+  (char *) s1;
+  (char *) s2;
+  (char *) c;
+
+  (int *) i;
+  (int *) d; /* { dg-warning "dereferencing type-punned pointer might break strict-aliasing rules" } */
+  (int *) s1; /* { dg-warning "dereferencing type-punned pointer might break strict-aliasing rules" } */
+  (int *) s2; /* { dg-warning "dereferencing type-punned pointer might break strict-aliasing rules" } */
+  (int *) c;
+
+  (double *) i; /* { dg-warning "dereferencing type-punned pointer might break strict-aliasing rules" } */
+  (double *) d;
+  (double *) s1; /* { dg-warning "dereferencing type-punned pointer might break strict-aliasing rules" } */
+  (double *) s2; /* { dg-warning "dereferencing type-punned pointer might break strict-aliasing rules" } */
+  (double *) c;
+
+  (struct incomplete *) i; /* { dg-warning "dereferencing type-punned pointer might break strict-aliasing rules" } */
+  (struct incomplete *) d; /* { dg-warning "dereferencing type-punned pointer might break strict-aliasing rules" } */
+  (struct incomplete *) s1; /* { dg-warning "dereferencing type-punned pointer might break strict-aliasing rules" } */
+  (struct incomplete *) s2; /* { dg-warning "dereferencing type-punned pointer might break strict-aliasing rules" } */
+  (struct incomplete *) c; /* { dg-warning "dereferencing type-punned pointer might break strict-aliasing rules" } */
+
+  (struct s1 *) i; /* { dg-warning "dereferencing type-punned pointer might break strict-aliasing rules" } */
+  (struct s1 *) d; /* { dg-warning "dereferencing type-punned pointer might break strict-aliasing rules" } */
+  (struct s1 *) s1;
+  (struct s1 *) s2; /* { dg-warning "dereferencing type-punned pointer might break strict-aliasing rules" } */
+  (struct s1 *) c;
+
+  (struct s2 *) i; /* { dg-warning "dereferencing type-punned pointer might break strict-aliasing rules" } */
+  (struct s2 *) d; /* { dg-warning "dereferencing type-punned pointer might break strict-aliasing rules" } */
+  (struct s2 *) s1; /* { dg-warning "dereferencing type-punned pointer might break strict-aliasing rules" } */
+  (struct s2 *) s2;
+  (struct s2 *) c;
+}