2

I am looking to expand only macros in my C++ code. I understand that -E option runs only preprocessor, but I am not looking for complete preprocessed output. All I want is a C++ code with all macros expanded.

I need to submit this code to a analyser, who don't understand macros. But a preprocessed code will defeat the purpose, as it will add lot of third party code (via include files) to the code.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1765709
  • 441
  • 1
  • 4
  • 8
  • Why exactly can't you use the `-E` switch? Or use `cpp src_file`. – cadaniluk Oct 29 '15 at 08:59
  • 2
    Possible duplicate of [Run a "light" preprocessor for GCC](http://stackoverflow.com/questions/2615931/run-a-light-preprocessor-for-gcc) – m.s. Oct 29 '15 at 09:08
  • I had same issue and never found a solution, right now I delegated most of the work of Macros to a external tool (Cmake) so that I have minimal macro that don't really need expansion to get debugged. your best bet is having code where include files are really mimimal (in example using Pimpl idiom or pure virtual classes greatly reduces number of inclusions). I think certain IDES just show expanded macro if you put mouse over it – CoffeDeveloper Oct 29 '15 at 09:08
  • @cad That would strip comments and evaluate all `#include` and `#if` directives as well. – Angew is no longer proud of SO Oct 29 '15 at 09:08
  • Do you need to keep `#ifdef`s too? – Petr Oct 29 '15 at 09:09
  • The preprocessor outputs information about where the include files are. In GCC it looks like this: `# 1 ""`, where `1` is the line number. Microsofts preprocessor, AFAIK, includes `#line` directives. Maybe you can try and filter the preprocessor output based on that information. – cdonat Oct 29 '15 at 09:21

1 Answers1

0

If you just want macro expansions and skip the #include handling, this may help: https://github.com/ned14/pcpp/pull/34

smwikipedia
  • 61,609
  • 92
  • 309
  • 482