YeJiabin
YeJiabin

Reputation: 1038

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

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.

Upvotes: 7

Views: 2189

Answers (1)

Oak
Oak

Reputation: 26868

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).

Upvotes: 7

Related Questions