3 ms·
That’s because the preprocessor adds too much flexibility to make it easy to write such tools. Once you have #ifdef FOO ... #else ... #endif y
by Someone 5y ago
That’s because the preprocessor adds too much flexibility to make it easy to write such tools.
Once you have
#ifdef FOO
...
#else
...
#endif
you have to completely preprocess the compilation unit to find out which branch is taken. That means you have to interpret the build script (which may do -DFOO, possibly only for some compilation targets)
You have to do that even for such simple things as syntax coloring. Now, you say “I simply parse both branches, check what things they define, somehow merge the two, and we’re good to go”. That won’t work, as the not taken branch(es) need not contain valid C/C++, and their validity may depend on whether other branches are taken.
the preprocessor also has concatenation (https://gcc.gnu.org/onlinedocs/cpp/Concatenation.html https://gcc.gnu.org/onlinedocs/cpp/Concatenation.html). That makes writing a refactoring tool as good as impossible (for example, how do you rename quit_command to commandExit in the example on that page?)
Now, lots of code is fairly reasonable in its use of the preprocessor, but writing a reliable tool is a lot harder, if it’s possible at all, than with Java.
- marcodiego 5y agoThanks for the reasonable view and explanation.