[LLVMdev] Variable name from metadata

Pankaj Gode godepankaj at yahoo.com
Mon Oct 17 05:08:42 PDT 2011


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<CallInst>(Insn);
              const Value* LLVMIValue  = cast<CallInst>(Insn)->getCalledValue();
              const Function *LLVMIFunc= dyn_cast<Function>(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

3. 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20111017/79346b44/attachment.html>


More information about the llvm-dev mailing list