[cfe-dev] Lenient lexing/parsing of code snippets

Will Hawkins whh8b at virginia.edu
Sat Dec 6 18:55:42 PST 2014


Hello awesome clang community!

The combination of clang/llvm is so powerful that I'm sure what I'm
about to ask is simple. Unfortunately I've worked for about a month
and gotten nowhere. After RTFMing as much as possible, I'm reaching
out here.

I'm trying to use cfe to lex/parse code snippets. The snippets will be
complete functions but they will be taken mostly out of their original
context. I'd like to generate an AST for the code. I've tried several
different ways of doing this one my own. The major problem is that
when taken out of context, most of the variables are undefined. I've
tried iteratively compiling the code and modifying it at after each
compilation by adding declarations for the missing variables (which I
catch using a DiagnosticConsumer). This is cumbersome and, actually,
mostly just does not work.

Is there a way to do this? Did I miss some obvious set of
documentation somewhere? If this isn't already doable, can any of the
experts here recommend some best practices for something like this?

Any help would be greatly appreciated. Thanks for everything that you
are doing to make the CFE as great a toolset as it is!

Will



More information about the cfe-dev mailing list