Question

I want to have a C pre-preprocessor which is filtering some #define statements from the sourcecode without changing anything else.

Why? This should be used to remove some client specific code from the sources if the source is handed out to another client.

Does anyone know of an existing solution?

Thanks! Simon

Was it helpful?

Solution

You can use something like awk instead of CPP ? Add some flags in your code surrounding the piece of code to be removed. For example:

(...)
//BEGIN_REMOVE_THIS_CODE

printf("secret code");

//END_REMOVE_THIS_CODE
(...)

then write a awk script to remove this code, something like...

BEGIN { write=1;}
/^\/\/BEGIN_REMOVE_THIS_CODE/ { write=0; next;}
/^\/\/END_REMOVE_THIS_CODE/ { write=1; next;}
    {
    if(write==1) print $0;
    }

OTHER TIPS

This sounds like what I asked about in Is there a C pre-processor which eliminates ifdef blocks based on values defined. The best answer I got was sunifdef, or 'Son of unifdef', which has worked reliably for me on some excessively contorted conditional code (the accumulated crud from over 20 years of development on a wide variety of platforms with an inadequate theory of how to do platform-specific compilation).

I don't think you need a preprocessor for this. If you don't have nested #ifdef's in your code, any regex engine can remove anything that is located between #ifdef CLIENT and #endif (use non-greedy matching to match first #endif, not last).

I would put the client specific code in a separate directory or possibly part of a different project that would need to be checked out of the source control.

Put a function call that would be stubbed out or (I forget the proper term) loosely linked so that another function can be put in its place.

I recommend using an additional macro language layer for code filtering, like filepp. You may use a C preprocessor friendly syntax to express which parts belongs to which clients.

//%ifdef CLIENT_A
  code for client A
//%endif

//%ifdef CLIENT_B
  code for client B
//%endif

//%if "CLIENT_A" || "CLIENT_B"
  code for client A and B
//%endif

The '//%' prefix enables You to compile the code unmodified. You may run filepp before You giving out the code to a client.

If you're using gcc, then you can use:

gcc <insert files here> -E

The -E option tells gcc to only preprocess the sources, and not to compile them.

Or, you could use grep to filter out specific files and let the preprocessor loose on them only.

grep -r '#define CLIENT_CODE' ./*.h 

You can also try unifdef which is rather simpler than sunifdef.

as far as I know... the preprocessor can be run as a separate step (using the correct compiler optios) . This way you can do whatever you want with the processed code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top