Custom gcc preprocessor

Warning: dangerous and ugly hack. Close your eyes now You can hook your own preprocessor by adding the ‘-no-integrated-cpp’ and ‘-B’ switches to the gcc command line. ‘-no-integrated-cpp’ means that gcc does search in the ‘-B’ path for its preprocessors before it uses its internal search path. The invocations of the preprocessor can be identified if the ‘cc1’, ‘cc1plus’ or ‘cc1obj’ programs (these are the C, C++ and Objective-c compilers) are invoked with the ‘-E’ option. You can do your own preprocessing when you see this option. When there is no ‘-E’ option pass all the parameters to the original programs. When there is such an option, you can do your own preprocessing, and pass the manipulated file to the original compiler.

It looks like this:

> cat cc1
#!/bin/sh

echo "My own special preprocessor -- $@"

/usr/lib/gcc/i486-linux-gnu/4.3/cc1 $@
exit $?

> chmod 755 cc1
> gcc -no-integrated-cpp -B$PWD x.c
My own special preprocessor -- -E -quiet x.c -mtune=generic -o /tmp/cc68tIbc.i
My own special preprocessor -- -fpreprocessed /tmp/cc68tIbc.i -quiet -dumpbase x.c -mtune=generic -auxbase x -o /tmp/cc0WGHdh.s

This example calls the original preprocessor, but prints an additional message and the parameters. You can replace the script by your own preprocessor.

The bad hack is over. You can open your eyes now.

Leave a Comment