Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expand C/C++ function macros without preprocessor

How would I test/expand all the function macros, in a C/C++ file, without running it through a preprocessor? For example, is there a program or method which would change this:

#include <iostream>
#define AAA(a) cout << "function "  << a << endl
using namespace std;
int main(){
AAA(12);
}

into this?

#include <iostream>
using namespace std;
int main(){
cout << "function " << 12 << endl;
}

I don't want to run through preprocessor because all the includes in the files make the "gcc -E <>" output really ugly, I just want a couple simple macro expansions without all the overhead.

like image 378
Involution Avatar asked Mar 08 '15 22:03

Involution


People also ask

What is expand in C?

The values 10 and 20 are called macro expansions. When the program run and if the C preprocessor sees an instance of a macro within the program code, it will do the macro expansion. It replaces the macro template with the value of macro expansion.

What is macro expansion in C?

In programming languages, such as C or assembly language, a name that defines a set of commands that are substituted for the macro name wherever the name appears in a program (a process called macro expansion) when the program is compiled or assembled.

What is macro and function in C?

A macro is a name given to a block of C statements as a pre-processor directive. Being a pre-processor, the block of code is communicated to the compiler before entering into the actual coding (main () function). A macro is defined with the pre-processor directive.


Video Answer


2 Answers

No, it's not possible. Your included headers could include macros that you want to expand in the body, for example. Or did you mean to not expand any macros that come from headers? The preprocessor has absolutely no way of distinguishing what you want from what you don't want in this case.

If you know in advance this is not the case, then I recommend simply writing a script to remove the includes and then run that through the preprocessor.

like image 176
Puppy Avatar answered Oct 26 '22 08:10

Puppy


I heard all possible negative answers on the topic:

  • macros can only be expanded not evaluated
  • processor should parse also include files
  • nested macros can be over-complicated
  • conditional preprocessing can be tricky
  • macros are evil just avoid them
  • etc etc....

They are all true, but IMO they collide with the reality of everydays programming.

In fact, working on old C project where macros were mostly simply used as functions this became of crucial importance for me. Generating all preprocessed files with /P works but is overkilling and time taking. I just needed a tool that expands a simple macro defined a few lines above or at maximum in other file.

How to do that?

1 Onl,inux simply use GDB and his expand macros capabilities 2 On windows I use https://www.jetbrains.com/resharper-cpp/ integrated into Visual Studio

So, Yes, in a practical sense, it is possible.

like image 43
OttoVonBrak Avatar answered Oct 26 '22 09:10

OttoVonBrak