0
votes

We have a code base that relies on lots of generated code generated by C macros.

If something goes wrong and there is a error or a warning, the compiler points at the line of the first macro expansion without telling more about where it went wrong inside the expanded code. I my particular case they are those /analyze warnings in Visual Studio.

Are there any tricks and tips that help find problems in complex preprocessor macros?

EDIT:

If you wonder why this code base have complex macros.

This is an emulator project where the decoding phase and execution phase is separated. For example instead of finding out during the execution of each instruction what addressing mode or operand size, etc is used, we generate a function for each combination with a DEFINE_INSTRUCTION macro which in turn generate the functions for all combinations. And chain these functions.

1
This must be the reason they don't support preprocessor tricks in C#DrKoch
em... could you elaborate a little bit more? how complex it can be? are you talking about macro expanded function or something similar?Jason Hu
@DrKoch people should blame themselves when the tools are misused instead of blaming the tools are not behaving as they think.Jason Hu
There are options to most compilers (see /E and /P for Microsoft) for generating the pre-processed version of the source. Either inspect visually or compile that to see what it doesn't like.TripeHound
@HuStmpHrr Anyway, I consider a tool which is designed to prevent misuse the better tool ;)DrKoch

1 Answers

1
votes
  1. idea: dont ;) don't use macros that are complicated as you loose a lot of IDE support / compiler support

=> if you have such macros, refactor them into functions... maybe even inline functions

but seriously. to help you with the bad macros you're stuck with: As TripeHound said, there are flags to 'compile' C files only to the stage of preprocessed C files --

On the command line, clang -E foo.m will show you the preprocessed output.