This is the mail archive of the
gdb@sources.redhat.com
mailing list for the GDB project.
Re: Preprocessor symbol style
On Sat, 14 Aug 2004 00:02:00 +0200 (CEST)
Mark Kettenis <kettenis@chello.nl> wrote:
> IIRC this has been discussed before, hopefully people forgive me
> raising the issue again.
>
> Currently in GDB we use the following style for preprocessor stuff:
>
> #ifdef HAVE_FOO_H
> #include <foo.h>
> #else
> #ifdef HAVE_BAR_H
> #include <bar.h>
> #ifndef HAVE_FOOBAR
> #define FOOBAR FOO(BAR)
> #endif
> #endif
> #endif
>
> I think this style has a serious problem; it's rather difficult to see
> how the #if's and #endif's balance. Personally I've been bitten by
> this more than once.
>
> Many GNU projects (GCC, glibc, autoconf, coreutils) use a somewhat
> different style:
>
> #ifdef HAVE_FOO_H
> # include <foo.h>
> #else
> # ifdef HAVE_BAR_H
> # include <bar.h>
> # ifndef HAVE_FOOBAR
> # define FOOBAR FOO(BAR)
> # endif
> # endif
> #endif
>
> This makes it much easier to see how the #if's and #endif's balance.
>
> Can we please adopt the second style for GDB? We can convert things
> incrementally, or if we want to do it all at once, I'll volunteer to
> provide the mamoth patch.
I agree that your proposed change is more readable than the current
convention. I am in favor of this reindentation.
But, since we've settled on ISO C, I think we can improve readability
even more. How about this?
#ifdef HAVE_FOO_H
# include <foo.h>
#elif defined(HAVE_BAR_H)
# include <bar.h>
# ifndef HAVE_FOOBAR
# define FOOBAR FOO(BAR)
# endif
#endif
One #ifdef / #endif pair has been removed by using #elif instead.
There are cases where the use of #elif improves readability a lot more
than is apparent from this example.
Kevin