This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
[patch] struct member warning
- To: egcs at cygnus dot com
- Subject: [patch] struct member warning
- From: Bruno Haible <haible at ilog dot fr>
- Date: Mon, 2 Mar 1998 15:45:50 +0100 (MET)
Hi,
It is a pitfall in C and C++ that a structure can have a static initializer
which lists less elements that the structure has. The remaining elements
are silently initialized with 0. This may or may not be what the programmer
intended. If the programmer intends this, she can easily write the 0es
explicitly. Here is a patch which adds a warning (triggered by
"-Wuninitialized").
gcc/ChangeLog:
Sat Feb 28 21:25:02 1998 Bruno Haible <bruno@linuix.mathematik.uni-karlsruhe.de>
* c-typeck.c (pop_init_level): Warn about uninitialized members in
struct initializers.
gcc/cp/ChangeLog:
Sat Feb 28 21:25:02 1998 Bruno Haible <bruno@linuix.mathematik.uni-karlsruhe.de>
* typeck2.c (process_init_constructor): Warn about uninitialized
members in struct initializers.
*** egcs-980214/gcc/c-typeck.c.bak Sun Feb 22 10:25:17 1998
--- egcs-980214/gcc/c-typeck.c Sat Feb 28 18:15:10 1998
***************
*** 5615,5620 ****
--- 5615,5631 ----
if (constructor_type != 0)
size = int_size_in_bytes (constructor_type);
+ /* Warn when some struct elements are uninitialized. */
+ if (warn_uninitialized
+ && constructor_type
+ && TREE_CODE (constructor_type) == RECORD_TYPE
+ && constructor_unfilled_fields)
+ {
+ push_member_name (constructor_unfilled_fields);
+ warning_init ("missing initializer%s", " for `%s'", NULL);
+ RESTORE_SPELLING_DEPTH (constructor_depth);
+ }
+
/* Now output all pending elements. */
output_pending_init_elements (1);
*** egcs-980214/gcc/cp/typeck2.c.bak Sun Feb 22 10:26:04 1998
--- egcs-980214/gcc/cp/typeck2.c Sat Feb 28 18:46:37 1998
***************
*** 1089,1094 ****
--- 1089,1097 ----
else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
error ("member `%s' is uninitialized reference",
IDENTIFIER_POINTER (DECL_NAME (field)));
+ else if (warn_uninitialized)
+ warning ("missing initializer for member `%s'",
+ IDENTIFIER_POINTER (DECL_NAME (field)));
}
}
*** egcs-980214/gcc/testsuite/gcc.misc-tests/m-un-2.c.bak Sat Feb 28 18:33:38 1998
--- egcs-980214/gcc/testsuite/gcc.misc-tests/m-un-2.c Sat Feb 28 18:37:33 1998
***************
*** 0 ****
--- 1,29 ----
+ /* { dg-do compile } */
+ /* { dg-options "-Wall" } */
+
+ typedef unsigned long size_t;
+ extern void* malloc (size_t);
+ extern void free (void*);
+ extern void* realloc (void*, size_t);
+
+ struct vtable {
+ void* (* _malloc) (size_t);
+ void (* _free) (void*);
+ void* (* _realloc) (void*, size_t);
+ };
+
+ struct vtable mtable = {
+ malloc,
+ free
+ }; /* { dg-warning "missing initializer for `mtable._realloc'" "warning regression" } */
+
+ struct vtable mtable2 = {
+ ._malloc = malloc,
+ ._realloc = realloc
+ }; /* { dg-warning "missing initializer for `mtable2._free'" "warning regression" } */
+
+ struct vtable mtable3 = {
+ ._free = free,
+ ._malloc = malloc,
+ ._realloc = realloc
+ };
*** egcs-980214/gcc/testsuite/g++.old-deja/g++.other/warn01.C.bak Sat Feb 28 18:51:42 1998
--- egcs-980214/gcc/testsuite/g++.old-deja/g++.other/warn01.C Sat Feb 28 18:52:40 1998
***************
*** 0 ****
--- 1,15 ----
+ // Build don't link:
+ // Special g++ Options: -Wall
+
+ typedef unsigned long size_t;
+ extern void* malloc (size_t);
+ extern void free (void*);
+ extern void* realloc (void*, size_t);
+
+ struct vtable {
+ void* (* _malloc) (size_t);
+ void (* _free) (void*);
+ void* (* _realloc) (void*, size_t);
+ };
+
+ struct vtable mtable = { malloc, free }; // WARNING - _realloc