indent(1) support for gcc(1) 0b prefix
Joshua Isom
jrisom at gmail.com
Sun Apr 27 21:03:11 UTC 2008
On Apr 26, 2008, at 4:35 PM, Romain Tartière wrote:
> Hello FreeBSD hackers!
>
> I'm using avr-gcc from the ports and relying on the 0b prefix notation
> for binary constants, that is:
>
> foo = 0b00101010;
>
> Thanks to /usr/ports/devel/avr-gcc/files/patch-0b-constants this is
> possible :-)
>
> But I would like to use indent(1) to reformat contributed code
> automatically. Unfortunately, the 0b notation is not supported by that
> program, and the resulting code looks like this:
>
> foo = 0 b00101010;
>
> ... then compilation fails, bla bla bla...
>
> A quick look at indent(1) source code leaded me to tweak
> /usr/src/usr.bin/indent/lexi.c so that the 0b notation is supported
> (patch attached).
>
> I was so wondering how useful(less) it was to support this extension in
> FreeBSD indent(1) program. The version of gcc provided with the base
> system does not support this syntax, and AFAIK, only the avr-gcc port
> support this kind of constructs...
>
> So options are:
> - Add support for 0b notation to FreeBSD indent(1) (maybe requiring
> the use of an extra command line argument to support this feature);
> - Provide a patch for indent(1) that can be conditionally applied on
> the code when compiling the world;
> - Create another port, say avr-indent(1), that is not more than a
> copy
> of indent(1) with support of 0b constructs;
> - Do nothing: tweaking indent(1) for supporting this is so trivial
> that the few individuals interested in this can have their local
> version of indent.
>
> Can you please tell me your opinion about this?
>
> Thank you in advance,
> Romain
>
> PS: I also took a look at GNU indent (gindent(1) from the ports), but
> it
> does not support 0b notation too.
>
> --
> Romain Tartière <romain at blogreen.org>
> http://romain.blogreen.org/
> pgp: 8DAB A124 0DA4 7024 F82A E748 D8E9 A33F FF56 FF43 (ID:
> 0xFF56FF43)
> (plain text =non-HTML= PGP/GPG encrypted/signed e-mail much
> appreciated)
>
Why not just have a base2.h file that is a list of defines such as:
#define b00000001 1
#define b00000010 2
Then you just use a b00 syntax, perfectly valid, and all's well
everywhere. Considering that the 0b syntax isn't valid without a patch
that's only available with one compiler that's probably not regularly
used, it'd probably be much more maintainable and not require anyone
compiling the program to install that specific port with that specific
patch.
But of course, writing software that deals directly with bit fiddling
is annoying without using binary representation.
The man page for indent does say "it has a 'forgiving' parser" so I
don't see why it can't just treat any token starting with a zero as
special, treat 0b01, 0x0A, 0f4 and 010 as a number regardless of the
base, even if it doesn't know the base.
More information about the freebsd-hackers
mailing list