[LLVMdev] Variable name from metadata

Pankaj Gode godepankaj at yahoo.com
Mon Oct 17 05:47:04 PDT 2011


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<CallInst>(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
 


________________________________
From: Pankaj Gode <godepankaj at yahoo.com>
To: llvm Developers <llvmdev at cs.uiuc.edu>
Sent: Monday, October 17, 2011 5:38 PM
Subject: [LLVMdev] 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<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

_______________________________________________
LLVM Developers mailing list
LLVMdev at cs.uiuc.edu        http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20111017/d379b640/attachment.html>


More information about the llvm-dev mailing list