How to limit the scope of preprocessing definitions used?

How to preprocess the code base using the clang (or gcc) preprocessor, limiting its text processing to use only #define entries from a single header file?

This is useful in general: imagine you want to see the direct result of some of the macros that you are currently working on ... without any mess that arises from the mountain of inclusions inherent in C.

Imagine a case where there are macros that provide a callback compatible or updated based on the availability of functions.

#if __has_feature(XYZ) # define JX_FOO(_o) new_foo(_o) # define JX_BAR(_o) // nop ... #else # define JX_FOO(_o) old_foo(_o) # define JX_BAR(_o) old_bar(_o) ... #endif 

A specific example is an Objective-C code set that has been ported as ARC-compatible (automatic reference counting) from manual memory management (not ARC) using a macro set ( https://github.com/JanX2/google-diff-match- patch-Objective-C / blob / master / JXArcCompatibilityMacros.h ) to subsequently compile both paths.

At some point, you want to give up support without ARC to improve readability and maintainability.

Edit: the base for receiving preprocessor output is described here: C, Objective-C preprocessor output

Edit 2: If someone has information on how the source-to-source conversion options are implemented in Xcode (Edit> Refactor> Convert To ...), this may help.

+4
source share
3 answers

This is a bit silly solution, but it works: apparently you can use AppCodes refactoring to remove macro usage.

This limits the OS X solution. It is also a bit tedious because you have to do it manually for each JX_FOO() and JX_BAR() .

0
source

If you are writing a file from scratch or all of its attachments are in one place, why not wrap them inside:

 #ifndef MACRO_DEBUG #include "someLib.h" /* ... */ #endif 

But, as I mentioned, this only works when the attachments are in consecutive lines, and at best you start writing the file yourself from scratch, so you do not need to go and look for incoming ones.

0
source

This is the perfect case for sed / awk. However, there is an even better tool available for the specific use case you mention. Checkout coan .

To pre-process the source file as if the <SYMBOL> character was defined,

 $ coan source -D<SYMBOL> sourcefile.c 

Similar to preprocessing the source file, as if the <SYMBOL> character is NOT defined,

 $ coan source -U<SYMBOL> source.c 
0
source

Source: https://habr.com/ru/post/1495499/


All Articles