[cfe-dev] Preprocessor/Parser interaction

Eli Friedman eli.friedman at gmail.com
Fri Feb 17 11:34:57 PST 2012

On Fri, Feb 17, 2012 at 10:38 AM, Aditya Kumar <hiraditya at msn.com> wrote:
> For my project(related to source code analysis of C++ program) I  need to
> traverse the clang AST to figure out all the places
> where the preprocessor macros are called in the program.
> I am fairly new to clang to be honest.
> 1. What i want to know is whether the preprocessor gets called before the
> parser or
> it gets called from the parser as each token is seen by it.

The Parser library calls into the Lex library, which turns the input
file into a stream of tokens.

> 2. How can I obtain the AST of a C++ program which has been generated
> without any preprocessing.
> Even if there is some syntax-error in doing so, it is okay.

This won't work; don't try it.  C++ parsing is way too complicated for
that sort of substitution to result in anything sane.

Depending on what you are doing, you might be able to use source
locations instead; clang very precisely tracks where every expression
is written in the source code, including complete information about
macro expansions.


More information about the cfe-dev mailing list