public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Handle trailing arrays in ODR warning (PR lto/81440).
@ 2018-01-23 13:09 Martin Liška
  2018-01-23 14:06 ` Jan Hubicka
  0 siblings, 1 reply; 2+ messages in thread
From: Martin Liška @ 2018-01-23 13:09 UTC (permalink / raw)
  To: gcc-patches; +Cc: Jan Hubicka

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

Hi.

This removes false positive warning when having trailing array at the end of a struct.

Patch can bootstrap on ppc64le-redhat-linux and survives regression tests.

Ready to be installed?
Martin

gcc/lto/ChangeLog:

2018-01-23  Martin Liska  <mliska@suse.cz>

	PR lto/81440
	* lto-symtab.c (lto_symtab_merge): Handle and do not warn about
	trailing arrays at the end of a struct.

gcc/testsuite/ChangeLog:

2018-01-23  Martin Liska  <mliska@suse.cz>

	PR lto/81440
	* gcc.dg/lto/pr81440.h: New test.
	* gcc.dg/lto/pr81440_0.c: New test.
	* gcc.dg/lto/pr81440_1.c: New test.
---
 gcc/lto/lto-symtab.c                 | 25 +++++++++++++++++++------
 gcc/testsuite/gcc.dg/lto/pr81440.h   |  4 ++++
 gcc/testsuite/gcc.dg/lto/pr81440_0.c |  9 +++++++++
 gcc/testsuite/gcc.dg/lto/pr81440_1.c |  6 ++++++
 4 files changed, 38 insertions(+), 6 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/lto/pr81440.h
 create mode 100644 gcc/testsuite/gcc.dg/lto/pr81440_0.c
 create mode 100644 gcc/testsuite/gcc.dg/lto/pr81440_1.c



[-- Attachment #2: 0001-Handle-trailing-arrays-in-ODR-warning-PR-lto-81440.patch --]
[-- Type: text/x-patch, Size: 2297 bytes --]

diff --git a/gcc/lto/lto-symtab.c b/gcc/lto/lto-symtab.c
index ee02a534714..0f0b958e98d 100644
--- a/gcc/lto/lto-symtab.c
+++ b/gcc/lto/lto-symtab.c
@@ -352,18 +352,31 @@ lto_symtab_merge (symtab_node *prevailing, symtab_node *entry)
     return false;
 
   if (DECL_SIZE (decl) && DECL_SIZE (prevailing_decl)
-      && !tree_int_cst_equal (DECL_SIZE (decl), DECL_SIZE (prevailing_decl))
+      && !tree_int_cst_equal (DECL_SIZE (decl), DECL_SIZE (prevailing_decl)))
+      {
+	if (!DECL_COMMON (decl) && !DECL_EXTERNAL (decl))
+	  return false;
+
+	tree type = TREE_TYPE (decl);
+
+	/* For record type, check for array at the end of the structure.  */
+	if (TREE_CODE (type) == RECORD_TYPE)
+	  {
+	    tree field = TYPE_FIELDS (type);
+	    while (DECL_CHAIN (field) != NULL_TREE)
+	      field = DECL_CHAIN (field);
+
+	    return TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE;
+	  }
       /* As a special case do not warn about merging
 	 int a[];
 	 and
 	 int a[]={1,2,3};
 	 here the first declaration is COMMON
 	 and sizeof(a) == sizeof (int).  */
-      && ((!DECL_COMMON (decl) && !DECL_EXTERNAL (decl))
-	  || TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE
-	  || TYPE_SIZE (TREE_TYPE (decl))
-	     != TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl)))))
-    return false;
+	else if (TREE_CODE (type) == ARRAY_TYPE)
+	  return (TYPE_SIZE (decl) == TYPE_SIZE (TREE_TYPE (type)));
+      }
 
   return true;
 }
diff --git a/gcc/testsuite/gcc.dg/lto/pr81440.h b/gcc/testsuite/gcc.dg/lto/pr81440.h
new file mode 100644
index 00000000000..d9e6c3da645
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/lto/pr81440.h
@@ -0,0 +1,4 @@
+typedef struct {
+  int i;
+  int ints[];
+} struct_t;
diff --git a/gcc/testsuite/gcc.dg/lto/pr81440_0.c b/gcc/testsuite/gcc.dg/lto/pr81440_0.c
new file mode 100644
index 00000000000..07f2a87da21
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/lto/pr81440_0.c
@@ -0,0 +1,9 @@
+/* { dg-lto-do link } */
+
+#include "pr81440.h"
+
+extern struct_t my_struct;
+
+int main() {
+ return my_struct.ints[0];
+}
diff --git a/gcc/testsuite/gcc.dg/lto/pr81440_1.c b/gcc/testsuite/gcc.dg/lto/pr81440_1.c
new file mode 100644
index 00000000000..d03533029c1
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/lto/pr81440_1.c
@@ -0,0 +1,6 @@
+#include "pr81440.h"
+
+struct_t my_struct = {
+ 20,
+ { 1, 2 }
+};


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

* Re: [PATCH] Handle trailing arrays in ODR warning (PR lto/81440).
  2018-01-23 13:09 [PATCH] Handle trailing arrays in ODR warning (PR lto/81440) Martin Liška
@ 2018-01-23 14:06 ` Jan Hubicka
  0 siblings, 0 replies; 2+ messages in thread
From: Jan Hubicka @ 2018-01-23 14:06 UTC (permalink / raw)
  To: Martin Liška; +Cc: gcc-patches

> Hi.
> 
> This removes false positive warning when having trailing array at the end of a struct.
> 
> Patch can bootstrap on ppc64le-redhat-linux and survives regression tests.
> 
> Ready to be installed?
> Martin
> 
> gcc/lto/ChangeLog:
> 
> 2018-01-23  Martin Liska  <mliska@suse.cz>
> 
> 	PR lto/81440
> 	* lto-symtab.c (lto_symtab_merge): Handle and do not warn about
> 	trailing arrays at the end of a struct.

OK, thanks!
Honza

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

end of thread, other threads:[~2018-01-23 13:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-01-23 13:09 [PATCH] Handle trailing arrays in ODR warning (PR lto/81440) Martin Liška
2018-01-23 14:06 ` Jan Hubicka

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).