set line number debug info

Hi,
I am new to LLVM infrastructure. Recently I am trying to set the debug info for an instruction. The main aim is to set the source line number of an instruction. Can anyone please show how to do that?
I think that setMetadata method in Instruction class is to be used. But how do I create MDNode* consisting of desired source line number. Source line number can be extracted from the instruction-debug-info using getMetadata method and then using getLineNumber method from DILocation class. Unfortunately DILocation class does not have any function like setLinenumber.

Abhirup Ghosh
M. Tech
Department of Computer Science & Engg.
IIT, Bombay
email - abhirupju@gmail.com , abhirup@cse.iitb.ac.in
Contact - 9920735181

You may want to check out Instruction::setDebugLoc().
llvm/Support/DebugLoc.h and llvm/Analysis/DIBuilder.h will likely be
helpful as well.

Thanks for the help.
I thought I’d solved the problem (setting the line number information in the instruction metadata) by inserting my own function for setting line number in llvm/Support/DebugLoc.h. I have added a function setLine(unsigned) in DebugLoc class. The function simply sets the LineCol variable as needed. But there is a problem.

Whenever I’m checking the line number(calling getLine()) from the function where I’ve called setLine(…) function, it returns the changed line number but whenever I call the getLine() function from other functions then it is returning the previous value not the changed. Is the change in the LineCol variable is not permanent or is this variable not at all linked with the debug information?

How can I set the line number information for the instructions?

Abhirup Ghosh
M. Tech
Department of Computer Science & Engg.
IIT, Bombay
email - abhirupju@gmail.com , abhirup@cse.iitb.ac.in
Contact - 9920735181

Oh! I made a mistake by not using setDebugInfo() function present in the Instruction class. Now it’s working fully.
So, the solution is:-
To set the line number for an instruction -

  1. Add a function in DebugLoc class located in llvm/Support/DebugLoc.h. Set the line number there to the private variable called LineCol.
  2. Call the added function. And then pass the modified DebugLoc object to the seDebugInfo method for the instruction.

Abhirup Ghosh
M. Tech
Department of Computer Science & Engg.
IIT, Bombay
email - abhirupju@gmail.com , abhirup@cse.iitb.ac.in
Contact - 9920735181 / 9434362120

Abhirup,