[LLVMdev] Programmatic compilation of C++ file into bitcode

Duncan Sands baldrick at free.fr
Thu Feb 25 01:04:30 PST 2010


Hi Trevor,

> I'm building a static analysis tool on top of LLVM. It needs to take
> in a C++ source file and have LLVM translate it into bitcode. In other
> words, it basically needs to do this:
>
>     llvmc hello.cpp -emit-llvm -O0 -S -g

behind the scenes it's actually llvm-gcc that is generating the
bitcode.

> Except that instead of writing the bitcode to a file, it needs to load
> it into memory (presumably as an instance of Module) for further
> processing and analysis.

You could just pipe it to your program:

llvm-gcc hello.cpp -emit-llvm -c -o - | analysis_program

So my goal is to do essentially what llvmc
> does, but programmatically by invoking the LLVM API directly.

You can add your static analysis to llvm-gcc as an LLVM pass.
If you write it as an LLVM pass then you can also use it from
"opt", which would be convenient.

> I thought I could use lib/CompilerDriver/Main.cpp as a guide, but
> after studying the code and associated docs, I'm stumped. Much of the
> logic is woven through TableGen'd drivers, so I can't even figure out
> how the command line options are fed into the LLVM API.
>
> Any suggestions? Thanks,

I don't think you should bother with llvmc: it's a compiler driver,
that launches the real compiler.

Did you take a look at the clang static analyser?

Ciao,

Duncan.



More information about the llvm-dev mailing list