[Swan] uncrustify vs. DBG
D. Hugh Redelmeier
hugh at mimosa.com
Tue May 28 21:01:12 EEST 2013
The C preprocessor is a magical monster. It makes things very difficult
or impossible for programs that try to understand C source code. C macros
and conditional compilation can cut across grammatical structures.
The *swan DBG macro takes a sequence of C statements as its second, as
long as no comma appears "at the top level".
Here's the definition:
#define DBG(cond, action) do { if (DBGP(cond)) { action ; } } while (0)
The "do ... while (0)" is to prevent surprises, just the way the body of
a function-like macro should be wrapped in ( ). Why not the simpler { }?
Because this would not work
if (....)
DBG(...);
else
...
Notice the way the "action" parameter is used. It can be a sequence of
statements, and the last one need not end with ";" because that is
supplied.
Uncrustify doesn't understand all invocations of DBG in *swan.
I've found a compromise that seems like reasonable coding style and that
uncrustify understands:
In each case where the action is more complicated than a simple
statement, put braces around it.
It turns out that only some of these examples confuse uncrustify, but I
think applying this simple rule universally would be good.
I suggest that we adopt this style rule. Unfortunately, we don't have a
tool to enforce this (i.e. warn when we break the rule).
More information about the Swan
mailing list