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.
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.
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.
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.
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.
I heard all possible negative answers on the topic:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With