This is the mail archive of the gcc@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]

-Wswitch vs -Wuninitialized


Hello,

I'm looking at the interaction between -Wswitch and -Wuninitialized. 
Given the ``buggy'' code:

  code#1
	enum e { E_A, E_B, E_C };
	int
	foo (enum e ev)
	{
	  int i;
	  switch (ev)
	    {
	    case E_A: i = 1; break;
	    case E_B: i = 2; break;
	    }
	  return i;
	}

GCC (2.95.2 19991024) reports:

	enumeration value `E_C' not handled in switch
	`i' might be used uninitialized in this function

Adding the missing enum vis:

  code#2
	...
	  switch (ev)
	    {
	    case E_A: i = 1; break;
	    case E_B: i = 2; break;
	    case E_C: i = 3; break;
	    }
	...

still gives the warning:

	`i' might be used uninitialized in this function

If instead a ``default:'' case had been present:

  code#3
	  switch (ev)
	    {
	    case E_A: i = 1; break;
	    case E_B: i = 2; break;
	    default: abort (); /* bad switch */
	    }

the -Wuninitialized issue is addressed (GCC knows that all branches are
covered).  Unfortunatly, it also supresses the -Wswitch warning - which
may still be a second bug.

Has anyone looked at refining the way -Wswitch works so that it is still
useful in cases such as the above?  Alternativly, is there a suggested
coding pratice that addreses this?

	enjoy,
		Andrew

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