C
Chris Croughton
I have a requirement to partially preprocess C code. By 'partially' I
mean that I need to define some macros (to the utility) and have it
preprocess only those macros, leaving everything else intact. A
compiler's preprocessor won't do this, since it will also process macros
defined in the code (and in standard header files) and strip out
everything. For instance, given source like:
#include <stdio.h>
#if defined(AAA)
void doSomeCode(void)
{
# if AAA > 1
printf("do something %d\n", AAA);
# else
printf("do something else\n");
# endif
}
#else
#include <math.h>
#define PI (4 * atan(1.0))
void doSomeCode(void)
{
printf("something completely different - PI = %f\n", PI);
}
#endif
I would want the output to be:
AAA undefined:
#include <stdio.h>
#include <math.h>
#define PI (4 * atan(1.0))
void doSomeCode(void)
{
printf("something completely different - PI = %f\n", PI);
}
AAA = 0 or 1
#include <stdio.h>
void doSomeCode(void)
{
printf("do something else\n");
}
AAA = 2
#include <stdio.h>
void doSomeCode(void)
{
printf("do something %d\n", 2);
}
OK, it's a silly example, but the idea should show. Note that using the
C preprocessor would have expanded the header files, which is not
wanted...
One purpose is to take some code which has got horribly crufty, with
lots of specific conditional compilation for things which no longer
exist (in particular things which were needed for pre-standard and
non-standard compilers), and reduce it for a simpler case. Yes, this
can be done "by hand" but this is prone to error and very time-consuming
with a large amount of code.
Another purpose is as a general utility for pre-parsing header and code
files for distribution, so that the destination system does not have to
include "config.h" (or equivalent) every time.
My question is:
Does such a utility exist as Free Software? Licence is unimportant as
long as it is compatible with other Free Software -- (L)GPL, BSD and
'Infozip' licences particularly. Source code as Standard C so it will
compile anywhere (so can't depend on yacc or lex, unless the generated
code from them is Standard C).
If not, I need to write my own. If it does exist already, I would
rather not re-invent the wheel...
Chris C
mean that I need to define some macros (to the utility) and have it
preprocess only those macros, leaving everything else intact. A
compiler's preprocessor won't do this, since it will also process macros
defined in the code (and in standard header files) and strip out
everything. For instance, given source like:
#include <stdio.h>
#if defined(AAA)
void doSomeCode(void)
{
# if AAA > 1
printf("do something %d\n", AAA);
# else
printf("do something else\n");
# endif
}
#else
#include <math.h>
#define PI (4 * atan(1.0))
void doSomeCode(void)
{
printf("something completely different - PI = %f\n", PI);
}
#endif
I would want the output to be:
AAA undefined:
#include <stdio.h>
#include <math.h>
#define PI (4 * atan(1.0))
void doSomeCode(void)
{
printf("something completely different - PI = %f\n", PI);
}
AAA = 0 or 1
#include <stdio.h>
void doSomeCode(void)
{
printf("do something else\n");
}
AAA = 2
#include <stdio.h>
void doSomeCode(void)
{
printf("do something %d\n", 2);
}
OK, it's a silly example, but the idea should show. Note that using the
C preprocessor would have expanded the header files, which is not
wanted...
One purpose is to take some code which has got horribly crufty, with
lots of specific conditional compilation for things which no longer
exist (in particular things which were needed for pre-standard and
non-standard compilers), and reduce it for a simpler case. Yes, this
can be done "by hand" but this is prone to error and very time-consuming
with a large amount of code.
Another purpose is as a general utility for pre-parsing header and code
files for distribution, so that the destination system does not have to
include "config.h" (or equivalent) every time.
My question is:
Does such a utility exist as Free Software? Licence is unimportant as
long as it is compatible with other Free Software -- (L)GPL, BSD and
'Infozip' licences particularly. Source code as Standard C so it will
compile anywhere (so can't depend on yacc or lex, unless the generated
code from them is Standard C).
If not, I need to write my own. If it does exist already, I would
rather not re-invent the wheel...
Chris C