Let the compiler preprocess a source file by first removing #include lines which contain definitions of macros we don't want to see expanded (expanding only the definitions from the local file or from chosen headers, achieving less cluttered and more readable output). Say we have a file like:
#include "foo_defs.h"
#define BAR 42
FOO;
BAR;
We filter out the include line and then preprocess the file, getting the following output: FOO;
42; #include <stdio.h>
#include <assert.h>
#include "mydefs-control.h"
#include "mydefs-value.h"
int main(void) {
assert(1 == 1);
printf("%d\n", MYDEF_VALUE_MACRO);
MYDEF_MACRO_WITH_CONTROL_FLOW();
}
My goal is to produce output which doesn't include thousands of lines from stdio.h and assert.h, doesn't expand assert() or MYDEF_VALUE_MACRO, and only expands MYDEF_MACRO_WITH_CONTROL_FLOW: int main(void) {
assert(1 == 1);
printf("%d\n", MYDEF_VALUE_MACRO);
if (1) {
return 0;
} else {
return 1;
};
}
According to the author, having only the "interesting" stuff expanded makes it easier to reason about the control flow of the code.0. Anything involving file and line number.
1. Generating a string version of something alongside the value of something, without repeating myself.
2. Creating printing macros to keep me from typing out the entire asinine syntax for something like “std::cerr << foo << bar << baz << std::endl” so I only need PRINT(foo << bar << baz). Substituting code fragments is dead simple with a macro and absurdly complex or impossible with other C++ mechanisms. Ironically I’m only doing it because of the poor design of the entire “iostream” stack.
That would have been extremely useful when I was recently refactoring some old C code. I ended up writing a small script that can expand one macro at a time, but I wish I hadn't had to.
I'm sure that script doesn't work in the general case.
Can be used in atom: https://atom.io/packages/atom-clang-expand
For example, some good ones I use are isLandscape or isIPad, which are both variable depending on the user, but constant for the run of the app. Perfect for a PCH file I think.