Variable name from metadata

Hi All,

Can we extract name of variable name from “MDNode” ?

  1. Neither temp_MDNode->getName() nor temp_MDNode->getValueName() give me “global_int” which is name of a variable.

  2. I tried below ways as well.
    DIVariable DV(mdnode1);
    Value *v = mdnode1->getOperand(0);//gives add 0x69

  3. I have written below code to reach till variable name.

LLVMIname is

const CallInst CI = dyn_cast(Insn);
const Value
LLVMIValue = cast(Insn)->getCalledValue();
const Function *LLVMIFunc= dyn_cast(LLVMIValue);

if( LLVMIname.compare(“llvm.dbg.declare”) == 0 )
{
int x = 0;
int numoperands = CI->getNumArgOperands();
for( unsigned i = 0; i != numoperands; ++i)
{
Value *v = CI->getOperand(i);
if( i == 1) //points to metadata for variable name
{
MDNode *mdnode = (MDNode *)CI->getOperand(1);
int numoperds= mdnode->getNumOperands();//4 operands
for( unsigned x = 0; x != numoperds; ++x)
{
Value *v = mdnode->getOperand(x);
if( x == 2)//name of variable
{
MDNode *mdnode1 = (MDNode *)mdnode->getOperand(2);//mdnode1->dump() gives variable name.
std::pair<unsigned, MDNode *> mynode( Insn->getDebugLoc().getLine(), mdnode1);
linenum_varmdnode.push_back( mynode);

//DIVariable DV(mdnode1);//not working
//Value *v = mdnode1->getOperand(0);//, not working. gives add 0x69
}
}//for operands mdnode
}
}//for operands mdnode

  1. When I give “temp_MDNode->dump()” on gdb, it displays below output ==>

metadata !“global_int”
If I can collect this in a string, that too is OK for me.

Thanks & Regards,
Pankaj

Closing this, found below links to get variable name.

http://groups.google.com/group/llvm-dev/browse_thread/thread/1a239f0d24db2b5c

http://markmail.org/message/fj5qg44vyjsdde7k#query:+page:1+mid:5zhmyncisenomcga+state:results

I could do following, to get the required information.
const CallInst *CI = dyn_cast(Insn);
int numoperands = CI->getNumArgOperands();
for( unsigned i = 0; i != numoperands; ++i)
{
Value *v = CI->getOperand(i);
MDNode *temp = (MDNode *)CI->getOperand(1) ;
DIVariable DV(temp);//TO CHECK. remove later
StringRef varName = DV.getName();
const char *tmpvarname = varName.data();
if( i == 1) //points to metadata for variable name
{

Sorry for posting trivial question.

Regards,
Pankaj

Pankaj,

You could simply do

if (const CallInst *CI = dyn_cast(Insn))
if (const DbgDeclareInst *DDI = dyn_cast(Insn)) {
DIVariable DV = DDI->getVariable();
StringRef VarName = DV.getName();
}

Now, StringRef has various operators so you could most likely use it directly.