Clang Library

I am currently working on a personal project that would use on demand compilation of C code to load in new modules. I have been looking at clang and llvm and it seems like it has the capability to provide this ability. Is there anywhere i can look to see how i would be able to accomplish this programmatically? I have gone through the documentation on the Clang site as well as downloading and reviewing the source code from SVN. I have not seen anything directly on accessing the C preprocessing/compiler functionality programmatically.

Thanks.

Glenn R. Martin

Glenn R. Martin wrote:

I am currently working on a personal project that would use on demand
compilation of C code to load in new modules. I have been looking at
clang and llvm and it seems like it has the capability to provide this
ability. Is there anywhere i can look to see how i would be able to
accomplish this programmatically? I have gone through the documentation
on the Clang site as well as downloading and reviewing the source code
from SVN. I have not seen anything directly on accessing the C
preprocessing/compiler functionality programmatically.

There's several different objects you need to look into creating:

- Preprocessing is handled by a Preprocessor object.
- If you want to compile C code, you need to create an ASTContext (in
addition to a Preprocessor).
- The Sema (SEMAntic analyzer) object handles semantic checking, etc.
- The CodeGen library turns a fully-annotated AST into LLVM IR, which
you can then pass to LLVM libraries (like LLVM CodeGen and the Target
family) to generate executable code.
- The Frontend library handles much of the tedious process of setting up
these objects for you. In particular, look into the "CompilerInvocation"
and "CompilerInstance" classes.

HTH

Chip

Chip,
Thank you for the insight. I shall look in to it shortly.

Glenn R. Martin

And you may also have a look at lib/Driver. It contains the code that take care of all of this when you use the command line clang tool.

-- Jean-Daniel