eresonance
eresonance

Reputation: 1401

How to remove private code before publishing?

We have a medium-sized C codebase that provides an API for using our hardware products. We provide the source for this API so that customers can compile it into whatever environment they choose. It's important that the source is legible and easily navigated as many people do just that. We don't obfuscate it in any way.

In our code we have a number of functions and sections that are for internal testing only, stuff like wrapper methods, low level checks, etc. None of it is particularly interesting nor harmful in any respect but we don't really want people using them or getting confused about what they are. This has happened numerous times before despite having warnings in the code about using them.

Is there any tool or method by which we can filter out these sections before publishing the source?

Right now we use a custom method with comments, however it's hard to parse and maintain:

/* $if : INTERNAL : some comment */
debug_code();
/* $endif : INTERNAL */

I've looked at using #ifdef and the preprocessor but there doesn't seem to be a way to preprocess specific defines/macros and not the whole file at once. I would only want the specific blocks preprocessed (and therefore removed) and not all the defines and in the code.

EDIT: From my comment below, the stuff we would like to remove from the published code should not be located in separate files. Like wrappers, we prefer to keep those next to the functions they wrap to make it easy to find. Other times we have detailed comments about internal implementation that we want in the code for our reference but don't want others seeing it.

Upvotes: 4

Views: 158

Answers (3)

rici
rici

Reputation: 241881

One tool I've found useful for applications like this is coan, which is still actively maintained (although apparently it currently doesn't compile cleanly on Mac OS X, if that is a concern).

Coan is designed for code analysis and it can perform quite a few tasks which you probably don't need. As long as you don't explicitly #define the preprocessor macros which surround the code to be removed, you should be able to just -Define or -Undefine as appropriate.

Coan wants to keep the code consistent, and can evaluate preprocessor macros so as to correctly evaluate and possibly eliminate preprocessor conditionals. Even if you don't ask it to do the evaluation, it will normally interpret and simplify #ifdef blocks if the definition is visible. Even that behaviour can be altered with the --no-transients option, but it will emit a warning.

Upvotes: 1

Claudio
Claudio

Reputation: 10947

You should have put your testing code in different files, even better using proper testing frameworks (like Google Test or CUnit). In that case, removing the tests would have been matter of just removing some files. I suggest to go towrads heavy refactoring to isolate testing code in specific files.

Upvotes: 2

Norman Gray
Norman Gray

Reputation: 12514

The two options I would consider are awk and m4.

Something like sed '/\/\* *\$if/,/\/\* *\$endif/d' (untested) would delete all the lines between the comments you've illustrated. That's a bit crude, but if it's sufficient, it's simple enough that it would probably be robust.

If you need something more elaborate, then m4 would be worth looking at (see the documentation of the GNU implementation of m4). M4 is largely for this sort of text manipulation job. It's powerful, but that includes being powerful enough that you can tie yourself in knots with it (don't get carried away!). Hint: the default quote characters in m4 are left and right quote -- this isn't always convenient, and so the changequote function can be a lifesaver; thus changequote([[,]]) makes [[square brackets]] the quote characters.

Upvotes: 1

Related Questions