Getting variable names from LLVM Pass

Hi,

I want to retrieve the variable names used in a statement, I tried the following snippet, 
but it only gives me the variable named in llvm bitcode. I need the variable name in source code. 
for (auto op = I.op_begin(); op != I.op_end(); op++) {
                        Value* v = op->get();
                        StringRef name = v->getName();
}

Is there specific documentation I can refer to implement this?

Thanks

If the source language is something clang knows about, you may have to implement that on top of libTooling, part of clang. Unfortunately it’s not quite straight-forward to get to the source from the LLVM — you’re going to have to rely on a reverse-mapping from the LLVM IR to the higher-level source location, which *might* be doable if there’s debug information associated with the LLVM IR.

https://clang.llvm.org/docs/LibTooling.html

Cheers

-- Dean

Hi Michael,

Thanks for the help, it seems like you said its not going to be easy. But I will have a try at this, thank you for the link to LibTooling.

Best

Hi Michael,

Thanks for the help, it seems like you said its not going to be easy. But I will have a try at this, thank you for the link to LibTooling.

It’s not going to be easy if you’re trying to find the name of a variable from the LLVM IR, unless the debugging information is available in the LLVM IR.

If you have access to the original source that Clang knows about (i.e. if it’s C++, C, Objective-C, Objective-C++) then you can use LibTooling. If you have specific questions about using LibTooling, those are better sent to cfe-dev (the mailing list for Clang).

Cheers

Best

>
> Hi,
>
> I want to retrieve the variable names used in a statement, I tried the following snippet,
> but it only gives me the variable named in llvm bitcode. I need the variable name in source code.
>
> for (auto op = I.op_begin(); op != I.op_end(); op++) {
> Value* v = op->get();
> StringRef name = v->getName();
> }
>
> Is there specific documentation I can refer to implement this?
>

If the source language is something clang knows about, you may have to implement that on top of libTooling, part of clang. Unfortunately it’s not quite straight-forward to get to the source from the LLVM — you’re going to have to rely on a reverse-mapping from the LLVM IR to the higher-level source location, which *might* be doable if there’s debug information associated with the LLVM IR.

https://clang.llvm.org/docs/LibTooling.html

Cheers

-- Dean

--
Ridwan Shariffdeen
Graduate Student | National University of Singapore

-- Dean