ubsan: Avoid -Wpadded warnings [PR94641]
authorJakub Jelinek <jakub@redhat.com>
Tue, 21 Apr 2020 15:06:31 +0000 (17:06 +0200)
committerJakub Jelinek <jakub@redhat.com>
Tue, 21 Apr 2020 15:06:31 +0000 (17:06 +0200)
-Wpadded warnings aren't really useful for the artificial types that GCC
lays out for ubsan.

2020-04-21  Jakub Jelinek  <jakub@redhat.com>

PR c/94641
* stor-layout.c (place_field, finalize_record_size): Don't emit
-Wpadded warning on TYPE_ARTIFICIAL rli->t.
* ubsan.c (ubsan_get_type_descriptor_type,
ubsan_get_source_location_type, ubsan_create_data): Set
TYPE_ARTIFICIAL.
* asan.c (asan_global_struct): Likewise.

* c-c++-common/ubsan/pr94641.c: New test.

gcc/ChangeLog
gcc/asan.c
gcc/stor-layout.c
gcc/testsuite/ChangeLog
gcc/testsuite/c-c++-common/ubsan/pr94641.c [new file with mode: 0644]
gcc/ubsan.c

index dc831f6725b530a15c4c29728469c1dca9932e88..90c13ae209f7d1682f309afa1ff3137db13524b8 100644 (file)
@@ -1,3 +1,13 @@
+2020-04-21  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c/94641
+       * stor-layout.c (place_field, finalize_record_size): Don't emit
+       -Wpadded warning on TYPE_ARTIFICIAL rli->t.
+       * ubsan.c (ubsan_get_type_descriptor_type,
+       ubsan_get_source_location_type, ubsan_create_data): Set
+       TYPE_ARTIFICIAL.
+       * asan.c (asan_global_struct): Likewise.
+
 2020-04-21  Duan bo  <duanbo3@huawei.com>
 
        PR target/94577
index cc8f9122e3d24eb8238612310ee6a50ee98c16e7..ae14f7e543ccf2c73eaf0a3ba679521b5db83c40 100644 (file)
@@ -2661,6 +2661,7 @@ asan_global_struct (void)
   TYPE_FIELDS (ret) = fields[0];
   TYPE_NAME (ret) = type_decl;
   TYPE_STUB_DECL (ret) = type_decl;
+  TYPE_ARTIFICIAL (ret) = 1;
   layout_type (ret);
   return ret;
 }
index 8c9e034f6ec0513d72c174dbc217b7a4addcbd08..bde6fa22b58ac6dd17643f2f5ed67e26724826ae 100644 (file)
@@ -1341,7 +1341,8 @@ place_field (record_layout_info rli, tree field)
         Bump the cumulative size to multiple of field alignment.  */
 
       if (!targetm.ms_bitfield_layout_p (rli->t)
-          && DECL_SOURCE_LOCATION (field) != BUILTINS_LOCATION)
+         && DECL_SOURCE_LOCATION (field) != BUILTINS_LOCATION
+         && !TYPE_ARTIFICIAL (rli->t))
        warning (OPT_Wpadded, "padding struct to align %q+D", field);
 
       /* If the alignment is still within offset_align, just align
@@ -1775,7 +1776,8 @@ finalize_record_size (record_layout_info rli)
 
   if (TREE_CONSTANT (unpadded_size)
       && simple_cst_equal (unpadded_size, TYPE_SIZE (rli->t)) == 0
-      && input_location != BUILTINS_LOCATION)
+      && input_location != BUILTINS_LOCATION
+      && !TYPE_ARTIFICIAL (rli->t))
     warning (OPT_Wpadded, "padding struct size to alignment boundary");
 
   if (warn_packed && TREE_CODE (rli->t) == RECORD_TYPE
index 1f3fb60d6d5d2b60f481def9ec86c7f3d5297592..f3a3b69d155519db433fd2f4d45f6856f2b84afc 100644 (file)
@@ -1,3 +1,8 @@
+2020-04-21  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c/94641
+       * c-c++-common/ubsan/pr94641.c: New test.
+
 2020-04-21  Patrick Palka  <ppalka@redhat.com>
 
        PR c++/94597
diff --git a/gcc/testsuite/c-c++-common/ubsan/pr94641.c b/gcc/testsuite/c-c++-common/ubsan/pr94641.c
new file mode 100644 (file)
index 0000000..b41cb06
--- /dev/null
@@ -0,0 +1,11 @@
+/* PR c/94641 */
+/* { dg-do compile } */
+/* { dg-options "-fsanitize=undefined -Wpadded" } */
+
+void foo (void *) __attribute__((nonnull));
+
+void
+bar (void *p)
+{
+  foo (p);
+}
index da93831d3bde3723bce352fbb874118c4c9a0ad7..acd8a0b92b982c236722b0d6ad9dc9b135c38ad6 100644 (file)
@@ -229,6 +229,7 @@ ubsan_get_type_descriptor_type (void)
   TYPE_FIELDS (ret) = fields[0];
   TYPE_NAME (ret) = type_decl;
   TYPE_STUB_DECL (ret) = type_decl;
+  TYPE_ARTIFICIAL (ret) = 1;
   layout_type (ret);
   ubsan_type_descriptor_type = ret;
   return ret;
@@ -277,6 +278,7 @@ ubsan_get_source_location_type (void)
   TYPE_FIELDS (ret) = fields[0];
   TYPE_NAME (ret) = type_decl;
   TYPE_STUB_DECL (ret) = type_decl;
+  TYPE_ARTIFICIAL (ret) = 1;
   layout_type (ret);
   ubsan_source_location_type = ret;
   return ret;
@@ -593,6 +595,7 @@ ubsan_create_data (const char *name, int loccnt, const location_t *ploc, ...)
   TYPE_FIELDS (ret) = fields[0];
   TYPE_NAME (ret) = type_decl;
   TYPE_STUB_DECL (ret) = type_decl;
+  TYPE_ARTIFICIAL (ret) = 1;
   layout_type (ret);
 
   /* Now, fill in the type.  */