4 ms·
Preprocessing is the easiest part unless you are working on IDE functionality and need to map it back to exact position in original source code at character ins
by Karliss 6y ago
Preprocessing is the easiest part unless you are working on IDE functionality and need to map it back to exact position in original source code at character instead of line level. Then it gets confusing because single expression can have position in the place calling macro, inside the macro definition and inside the preprocessed buffer.
It does mean that you have to know exact compiler invocation before you can start parsing. You can even invoke the original compiler to preprocess the file and treat it as completely separate phase that can be mostly ignored after it's done.
There is plenty of complexity making extracting AST difficult after preprocessing. You could argue of how much of analysis belongs to syntax and what's already semantic analysis. But in practice and possibly at least partially due nature of C++ language the parsers making AST also do a lot of type analysis. Due to features like constexpr it they even execute the logic of code.
- pfdietz 6y agoBut you aren't parsing the program. You're parsing a particular instantiation of the program for a particular set of -D defines and include files. Whole chunks of the program that were excluded by #ifdefs don't get parsed at all. This is fine if you just want to analyze that particular instance of what the program can become, but it's not fine if you're trying to do something to the program as a whole.