public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: [patch] struct member warning
@ 1998-03-31  0:46 Bruno Haible
  0 siblings, 0 replies; 3+ messages in thread
From: Bruno Haible @ 1998-03-31  0:46 UTC (permalink / raw)
  To: Joern Rennecke, egcs, law

> > Here is a patch which adds a warning (triggered by
> > "-Wuninitialized").
> 
> I think that is the wrong switch.  Initialized to zero is considerably
> different from uninitialized.

You're right, but I would like to avoid too many -W options.
Let's have the warning triggered by "-W" (extra_warnings). Here is the
patch.


gcc/ChangeLog:

Mon Mar 30 02:40:32 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:

Mon Mar 30 02:40:32 1998  Bruno Haible  <bruno@linuix.mathematik.uni-karlsruhe.de>

        * typeck2.c (process_init_constructor): Warn about uninitialized
          members in struct initializers.

*** egcs-980321/gcc/c-typeck.c.bak	Tue Mar 24 01:31:06 1998
--- egcs-980321/gcc/c-typeck.c	Tue Mar 24 03:21:43 1998
***************
*** 5595,5600 ****
--- 5595,5611 ----
    if (constructor_type != 0)
      size = int_size_in_bytes (constructor_type);
  
+   /* Warn when some struct elements are uninitialized.  */
+   if (extra_warnings
+       && 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-980321/gcc/cp/typeck2.c.bak	Tue Mar 24 01:31:47 1998
--- egcs-980321/gcc/cp/typeck2.c	Tue Mar 24 03:21:34 1998
***************
*** 1155,1160 ****
--- 1155,1163 ----
  	  else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
  	    error ("member `%s' is uninitialized reference",
  		   IDENTIFIER_POINTER (DECL_NAME (field)));
+ 	  else if (extra_warnings)
+ 	    warning ("missing initializer for member `%s'",
+ 		     IDENTIFIER_POINTER (DECL_NAME (field)));
  	}
      }
  
*** egcs-980321/gcc/testsuite/gcc.misc-tests/m-un-2.c.bak	Tue Mar 24 02:28:40 1998
--- egcs-980321/gcc/testsuite/gcc.misc-tests/m-un-2.c	Tue Mar 24 02:28:40 1998
***************
*** 0 ****
--- 1,29 ----
+ /* { dg-do compile } */
+ /* { dg-options "-W -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-980321/gcc/testsuite/g++.old-deja/g++.other/warn01.C.bak	Tue Mar 24 02:28:40 1998
--- egcs-980321/gcc/testsuite/g++.old-deja/g++.other/warn01.C	Wed Mar 25 03:04:34 1998
***************
*** 0 ****
--- 1,15 ----
+ // Build don't link:
+ // Special g++ Options: -W -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

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

* Re: [patch] struct member warning
  1998-03-02  6:46 Bruno Haible
@ 1998-03-02 20:02 ` Joern Rennecke
  0 siblings, 0 replies; 3+ messages in thread
From: Joern Rennecke @ 1998-03-02 20:02 UTC (permalink / raw)
  To: Bruno Haible; +Cc: egcs

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

I think that is the wrong switch.  Initialized to zero is considerably
different from uninitialized.  If you must have a warning for this
construct (which is sometimes essential for portable code, i.e. to
initialize structs with union members), then IMO it should have a
more fitting name, like -Wimplicit-initialization.

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

* [patch] struct member warning
@ 1998-03-02  6:46 Bruno Haible
  1998-03-02 20:02 ` Joern Rennecke
  0 siblings, 1 reply; 3+ messages in thread
From: Bruno Haible @ 1998-03-02  6:46 UTC (permalink / raw)
  To: egcs

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

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

end of thread, other threads:[~1998-03-31  0:46 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-03-31  0:46 [patch] struct member warning Bruno Haible
  -- strict thread matches above, loose matches on Subject: below --
1998-03-02  6:46 Bruno Haible
1998-03-02 20:02 ` Joern Rennecke

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