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

Roman Kononov kononov195-far@yahoo.com
Thu May 18 02:51:00 GMT 2006


On 05/17/2006 17:05, Mark Mitchell wrote:
> Change:
> Rename:

Changed and re-tested.

> The patch is OK with those changes, but you'll need a copyright
> assignment, if you don't already have one, and you'll need to wait for
> Stage 1 of GCC 4.3, as this is a new feature, not a bug-fix.

I don't have a copyright assignment. What do I do to get one? I am confused 
by info on the web.

Roman
---------------------
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-struct
option tells g++ to warn. Default is not to warn.
---------------------
Testcase 1: ./gcc/testsuite/g++.dg/warn/empty-c-struct.C

// { dg-options "-Wempty-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-struct option added
     * invoke.texi: new manual entry
---------------------
Index: gcc/gcc/doc/invoke.texi
===================================================================
--- gcc/gcc/doc/invoke.texi     (revision 113830)
+++ gcc/gcc/doc/invoke.texi     (working copy)
@@ -1911,6 +1911,14 @@ main ()

  In this example, G++ will synthesize a default @samp{A& operator =
  (const A&);}, while cfront will use the user-defined @samp{operator =}.
+
+@item -Wempty-struct @r{(C++ only)}
+@opindex Wempty-struct
+Warn when G++ encounters an empty structure within an extern "C" block.
+Empty structures (structures without non-static data members) have zero
+size in GNU C, but non-zero size in C++, which may cause problems when
+an empty structure is used in both C and C++ code.
+
  @end table

  @node Objective-C and Objective-C++ Dialect Options
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_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-struct
+C++ ObjC++ Var(warn_empty_struct) Init(0)
+Warn about empty extern \"C\" structures
+
  Wendif-labels
  C ObjC C++ ObjC++
  Warn about stray tokens after #elif and #endif



More information about the Gcc-patches mailing list