[C PATCH] warn for empty struct -Wc++-compat

Prathamesh Kulkarni bilbotheelffriend@gmail.com
Tue Jun 2 21:30:00 GMT 2015


On Sat, Nov 15, 2014 at 3:56 AM, Prathamesh Kulkarni
<bilbotheelffriend@gmail.com> wrote:
> On Sat, Nov 15, 2014 at 3:06 AM, Joseph Myers <joseph@codesourcery.com> wrote:
>> On Wed, 12 Nov 2014, Prathamesh Kulkarni wrote:
>>
>>> Is this version okay ?
>>> [gcc/c]
>>>   * c-decl.c (warn_cxx_compat_finish_struct): New parameters code, record_loc.
>>>         Warn for empty struct.
>>>     (finish_struct): Pass TREE_CODE (t) and loc to
>>> warn_cxx_compat_finish_struct.
>>>
>>> [gcc/testsuite/gcc.dg]
>>>   * Wcxx-compat-22.c: New test-case.
>>
>> OK.
> Thanks. I will perform complete regression testing, and post the patch
>  with modifications to test cases that
> may possibly break due to this warning.
Sorry I had forgotten about this.
The attached patch adds warning for empty struct.
Bootstrapped on x86_64-unknown-linux-gnu, testing in progress.
OK for trunk ?

Thank you,
Prathamesh
>
> Regards,
> Prathamesh
>>
>> --
>> Joseph S. Myers
>> joseph@codesourcery.com
-------------- next part --------------
2015-06-02  Prathamesh Kulkarni  <prathamesh.kulkarni@linaro.org>

c/
	* c-decl.c (warn_cxx_compat_finish_struct): New parameters code, record_loc.
	Warn for empty struct.
	(finish_struct): Pass TREE_CODE(t) and loc to warn_cxx_compat_finish_struct.

testsuite/
	* gcc.dg/Wcxx-compat-22.c: New testcase.
	* c-c++-common/Wsizeof-pointer-memaccess1.c: Pass -Wno-c++-compat.
	* c-c++-common/Wsizeof-pointer-memaccess2.c: Likewise.
	* c-c++-common/pr58346-1.c: Likewise.
	* c-c++-common/transparent-union-1.c: Likewise.
-------------- next part --------------
Index: gcc/c/c-decl.c
===================================================================
--- gcc/c/c-decl.c	(revision 224033)
+++ gcc/c/c-decl.c	(working copy)
@@ -7525,12 +7525,23 @@
 /* Finish up struct info used by -Wc++-compat.  */
 
 static void
-warn_cxx_compat_finish_struct (tree fieldlist)
+warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
+			       location_t record_loc)
 {
   unsigned int ix;
   tree x;
   struct c_binding *b;
 
+  if (fieldlist == NULL_TREE)
+    {
+      if (code == RECORD_TYPE)
+	warning_at (record_loc, OPT_Wc___compat,
+		    "empty struct has size 0 in C, size 1 in C++");
+      else
+	warning_at (record_loc, OPT_Wc___compat,
+		    "empty union has size 0 in C, size 1 in C++");
+    }
+
   /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
      the current struct.  We do this now at the end of the struct
      because the flag is used to issue visibility warnings, and we
@@ -7863,7 +7874,7 @@
 			  DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
 
   if (warn_cxx_compat)
-    warn_cxx_compat_finish_struct (fieldlist);
+    warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
 
   struct_parse_info->struct_types.release ();
   struct_parse_info->fields.release ();
Index: gcc/testsuite/c-c++-common/Wsizeof-pointer-memaccess1.c
===================================================================
--- gcc/testsuite/c-c++-common/Wsizeof-pointer-memaccess1.c	(revision 224031)
+++ gcc/testsuite/c-c++-common/Wsizeof-pointer-memaccess1.c	(working copy)
@@ -1,6 +1,7 @@
 /* Test -Wsizeof-pointer-memaccess warnings.  */
 /* { dg-do compile } */
 /* { dg-options "-Wall -Wno-sizeof-array-argument" } */
+/* { dg-options "-Wall -Wno-sizeof-array-argument -Wno-c++-compat" { target c } } */
 
 typedef __SIZE_TYPE__ size_t;
 #ifdef __cplusplus
Index: gcc/testsuite/c-c++-common/Wsizeof-pointer-memaccess2.c
===================================================================
--- gcc/testsuite/c-c++-common/Wsizeof-pointer-memaccess2.c	(revision 224031)
+++ gcc/testsuite/c-c++-common/Wsizeof-pointer-memaccess2.c	(working copy)
@@ -1,6 +1,7 @@
 /* Test -Wsizeof-pointer-memaccess warnings.  */
 /* { dg-do compile } */
 /* { dg-options "-Wall -O2 -Wno-sizeof-array-argument" } */
+/* { dg-options "-Wall -O2 -Wno-sizeof-array-argument -Wno-c++-compat" {target c} } */
 
 #define bos(ptr) __builtin_object_size (ptr, 1)
 #define bos0(ptr) __builtin_object_size (ptr, 0)
Index: gcc/testsuite/c-c++-common/pr58346-1.c
===================================================================
--- gcc/testsuite/c-c++-common/pr58346-1.c	(revision 224031)
+++ gcc/testsuite/c-c++-common/pr58346-1.c	(working copy)
@@ -1,5 +1,6 @@
 /* PR c/58346 */
 /* { dg-do compile } */
+/* { dg-options "-Wno-c++-compat" { target c } } */
 
 struct U {
 #ifdef __cplusplus
Index: gcc/testsuite/c-c++-common/transparent-union-1.c
===================================================================
--- gcc/testsuite/c-c++-common/transparent-union-1.c	(revision 224031)
+++ gcc/testsuite/c-c++-common/transparent-union-1.c	(working copy)
@@ -1,4 +1,5 @@
 /* PR c++/51228 */
+/* { dg-options "-Wno-c++-compat" { target c } } */
 
 typedef union {} U __attribute__((transparent_union)); /* { dg-warning "ignored" } */
 
Index: gcc/testsuite/gcc.dg/Wcxx-compat-22.c
===================================================================
--- gcc/testsuite/gcc.dg/Wcxx-compat-22.c	(revision 0)
+++ gcc/testsuite/gcc.dg/Wcxx-compat-22.c	(working copy)
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+/* { dg-options "-Wc++-compat" } */
+struct A {}; /* { dg-warning "empty struct has size 0 in C" } */
+union B {}; /* { dg-warning "empty union has size 0 in C" } */
+struct C { struct D {}; int x; }; /* { dg-warning "empty struct has size 0 in C|declaration does not declare anything" } */
+struct E { union F {}; int x; }; /* { dg-warning "empty union has size 0 in C|declaration does not declare anything" } */
+union G { union H {}; int x; }; /* { dg-warning "empty union has size 0 in C|declaration does not declare anything" } */
+union I { struct J {}; int x; }; /* { dg-warning "empty struct has size 0 in C|declaration does not declare anything" } */


More information about the Gcc-patches mailing list