Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get C/C++ source code data from LLVM IR

Tags:

c++

c

llvm

As described in http://llvm.org/docs/SourceLevelDebugging.html,
I can find the line & column number of source code from LLVM IR using the following piece of code.

if (MDNode *N = I->getMetadata("dbg")) {  // Here I is an LLVM instruction
  DILocation Loc(N);                      // DILocation is in DebugInfo.h
  unsigned Line = Loc.getLineNumber();
  StringRef File = Loc.getFilename();
  StringRef Dir = Loc.getDirectory();
}

Howerver, I want more precise information.
In AST level, clang provides the FullSourceLoc API (getCharaterData()) so that I can find the mapping between AST node and the original source code. And I want to find such mapping between LLVM IR and source code.
Is it possible for me to get the exact charater data from IR's debug information?

Thanks.

like image 879
YeJiabin Avatar asked Aug 19 '13 03:08

YeJiabin


1 Answers

It is impossible to get the exact character data from the IR debug information alone. The best you can do is manually save the source code somewhere, then use the line and column information you got from the debug information.

By the way, there's a simpler way to get the debug info for Instructions: Instruction::getDebugLoc() returns a DebugLoc instance, which you can then query with getLine() and getCol() (but make sure to check it with its isUnknown method first).

like image 184
Oak Avatar answered Oct 08 '22 13:10

Oak