[cfe-dev] get source code with LLVM pass

Eli Friedman eli.friedman at gmail.com
Fri Nov 5 11:16:53 PDT 2010


On Thu, Nov 4, 2010 at 11:19 PM, Neng Chen <cnisme.cn at gmail.com> wrote:
>
>
> 2010/11/5 Eli Friedman <eli.friedman at gmail.com>
>>
>> On Thu, Nov 4, 2010 at 10:22 PM, Neng Chen <cnisme.cn at gmail.com> wrote:
>> > I wonder if there is any method to get the source code of a program with
>> > LLVM pass in the bytecode file(*.ibc), Recently I have written an LLVM
>> > Pass
>> > to iterate through functions, basic blocks and instructions
>> >
>> >
>> > for (Module::iterator F = M.begin(), ME = M.end(); F != ME; ++F) {
>> >
>> >     for (Function::iterator B = F->begin(), FE = F->end(); B != FE; ++B)
>> > {
>> >
>> >         for (BasicBlock::iterator I = B->begin(), BE = B->end(); I !=
>> > BE;
>> > I++) {
>> >
>> >              //code
>> >              //how can I get the source code here
>> >
>> >         }
>> >     }
>> > }
>> >
>> >
>> > if we can get the source code, and how to get ?
>>
>> Debug metadata is really your only option at that point in the
>> pipeline.  See http://llvm.org/docs/SourceLevelDebugging.html .  For
>> access to the AST, you need to write an ASTConsumer, not an LLVM pass;
>> look in clang/examples for some guidance for how to do that (clang
>> documentation is still relatively immature).
>>
>> -Eli
>
> Thank you very much for your help.
> You means I must use Clang's API, such as ASTConsumer, to get the source
> code from the AST, do I get your method?
> But, I hope just use LLVM Pass to get some source code, no means all source
> code. For example, I just want to get how many and where "while"
> expressions appear in the program. and maybe I also like to get some
> variables' values. I use llvm-gcc and llvm on Ubuntu 9.10,  have not
> installed clang. so, does LLVM Pass work?

You can't use an LLVM pass to access that sort of information; you
need access to the proper clang AST.  Again, see examples/ in the
clang source tree for how to do that.

-Eli




More information about the cfe-dev mailing list