Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate specific #ifdef branches

In short: I want to generate two different source trees from the current one, based only on one preprocessor macro being defined and another being undefined, with no other changes to the source.

If you are interested, here is my story...

In the beginning, my code was clean. Then we made a new product, and yea, it was better. But the code saw only the same peripheral devices, so we could keep the same code.

Well, almost.

There was one little condition that needed to be changed, so I added:

#if defined(PRODUCT_A)
condition = checkCat();
#elif defined(PRODUCT_B)
condition = checkCat() && checkHat();
#endif

...to one and only one source file. In the general all-source-files-include-this header file, I had:

#if !(defined(PRODUCT_A)||defined(PRODUCT_B))
#error "Don't make me replace you with a small shell script. RTFM."
#endif

...so that people couldn't compile it unless they explicitly defined a product type.

All was well. Oh... except that modifications were made, components changed, and since the new hardware worked better we could significantly re-write the control systems. Now when I look upon the face of the code, there are more than 60 separate areas delineated by either:

#ifdef PRODUCT_A
...
#else
...
#endif

...or the same, but for PRODUCT_B. Or even:

#if defined(PRODUCT_A)
...
#elif defined(PRODUCT_B)
...
#endif

And of course, sometimes sanity took a longer holiday and:

#ifdef PRODUCT_A
...
#endif
#ifdef PRODUCT_B
...
#endif

These conditions wrap anywhere from one to two hundred lines (you'd think that the last one could be done by switching header files, but the function names need to be the same).

This is insane. I would be better off maintaining two separate product-based branches in the source repo and porting any common changes. I realise this now.

Is there something that can generate the two different source trees I need, based only on PRODUCT_A being defined and PRODUCT_B being undefined (and vice-versa), without touching anything else (ie. no header inclusion, no macro expansion, etc)?

like image 519
detly Avatar asked May 07 '10 03:05

detly


People also ask

What is the formula to separate text in Excel?

In the first empty column, write =SPLIT(B1,"-") , with B1 being the cell you want to split and - the character you want the cell to split on.


1 Answers

I believe Coan will do what you're looking for. From the link:

Given a configuration and some source code, Coan can answer a range of questions about how the source code would appear to the C/C++ preprocessor if that configuration of symbols had been applied in advance.

And also:

Source code re-written by Coan is not preprocessed code as produced by the C preprocessor. It still contains comments, macro-invocations, and #-directives. It is still source code, but simplified in accordance with the chosen configuration.

So you could run it twice, first specifying product A and then product B.

like image 84
Dusty Avatar answered Sep 28 '22 08:09

Dusty