This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [C++ PATCH] warning about empty extern "C" structures


On 05/17/2006 10:08, DJ Delorie wrote:
+ warning (0,"size of empty %q+#D is different in C language",t);

Please pass OPT_Wempty_c_struct here, rather than testing it in the if().


Thank you

This patch adds an option for g++ to emit a warning when it sees
  extern "C" { struct foo {}; }
Such empty structures have different sizes in C++ and C modules. Linked
together the modules make the life painful. Supplying -Wempty-c-struct
option tells g++ to warn. Default is not to warn.

Regards,

Roman Kononov
------------------
Testcase 1: ./gcc/testsuite/g++.dg/warn/empty-c-struct.C

// { dg-options "-Wempty-c-struct" }
// { dg-do compile }
extern "C" { struct CS {}; }  // { dg-warning "" }
extern "C" { union  CU {}; }  // { dg-warning "" }
struct S {};
union  U {};
template<int inst> struct Z {};
typedef Z<0> Q;
Q q;
extern "C" { Q g; }
------------------
Testcase 2: ./gcc/testsuite/g++.dg/warn/empty-c-struct.C

// { dg-do compile }
extern "C" { struct CS {}; }
extern "C" { union  CU {}; }
struct S {};
union  U {};
template<int inst> struct Z {};
typedef Z<0> Q;
Q q;
extern "C" { Q g; }
------------------
Tested on x86_64-redhat-linux and i686-pc-cygwin
------------------
2006-05-17  Roman Kononov <kononov195-qwe@yahoo.com>

    New g++ warning about empty extern "C" structures
    * class.c (layout_class_type): Warning added
    * c.opt: -Wempty-c-struct option added
------------------
Index: gcc/gcc/cp/class.c
===================================================================
--- gcc/gcc/cp/class.c  (revision 113830)
+++ gcc/gcc/cp/class.c  (working copy)
@@ -4889,8 +4889,13 @@ layout_class_type (tree t, tree *virtual

   /* Make sure not to create any structures with zero size.  */
   if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
-    place_field (rli,
-                build_decl (FIELD_DECL, NULL_TREE, char_type_node));
+    {
+      if (current_lang_name == lang_name_c)
+        warning (OPT_Wempty_c_struct,
+                 "size of empty %q+#D is different in C language",t);
+      place_field (rli,
+                   build_decl (FIELD_DECL, NULL_TREE, char_type_node));
+    }

   /* Let the back-end lay out the type.  */
   finish_record_layout (rli, /*free_p=*/true);
Index: gcc/gcc/c.opt
===================================================================
--- gcc/gcc/c.opt       (revision 113830)
+++ gcc/gcc/c.opt       (working copy)
@@ -173,6 +173,10 @@ Weffc++
 C++ ObjC++ Var(warn_ecpp)
 Warn about violations of Effective C++ style rules

+Wempty-c-struct
+C++ ObjC++ Var(warn_empty_c_struct) Init(0)
+Warn about empty extern \"C\" structures
+
 Wendif-labels
 C ObjC C++ ObjC++
 Warn about stray tokens after #elif and #endif


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]