This is the mail archive of the gdb@sourceware.org mailing list for the GDB 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: Switch -Wunused-variable on?


On Mon, 23 Apr 2012, Tom Tromey wrote:

> I'd say that the best approach would be to make the declarations
> conditional in the same way that the uses are.
> 
> If there are a lot of problem cases with this approach, or if it makes
> the code too ugly, then maybe it would be better just not to use this
> flag.

 That IMHO just asks to refrain from interspersing function bodies with 
preprocessor conditionals.  Lots if not all conditional stuff can be 
factored out to headers or definitions elsewhere.  Linux the kernel for 
one has had such a policy for years now.

 Example.  Do not write this:

int target_frobnicate (int count);

#ifdef TARGET_NEEDS_HARD_FROBNICATION
int target_frobnicate_harder (int count, int status);
#endif

int
handle_frobnication (int count)
{
  int status;
  int i;

  status = target_frobnicate (count);
#ifdef TARGET_NEEDS_HARD_FROBNICATION
  for (i = 0; i < count; i++)
    status = target_frobnicate_harder (i, status);
#endif

  return status;
}

Instead write this:

int target_frobnicate (int count);

#ifdef TARGET_NEEDS_HARD_FROBNICATION
int target_frobnicate_harder (int count, int status);
#define target_needs_hard_frobnication 1
#else
#define target_needs_hard_frobnication 0
endif

int
handle_frobnication (int count)
{
  int status;
  int i;

  status = target_frobnicate (count);
  if (target_needs_hard_frobnication)
    for (i = 0; i < count; i++)
      status = target_frobnicate_harder (i, status);

  return status;
}

-- "i" is now live in all cases and also IMHO handle_frobnication is more 
readable.

  Maciej


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