How to get LLVM bitcode line number from CPP source code line number?

我的未来我决定 提交于 2019-12-02 07:56:57

There may not be a set of IR instructions that correspond clearly to that exact line... but it's possible, mostly. There is a function called Instruction::getDebugLoc() that returns the file name and line number for that particular instruction, if it returns anything at all. You can use that.

But you need to be prepared for a bit of guesswork, for two reasons.

  • If one instruction is from line 42, and the next two instructions don't have a marked origin, and then there's one from line 43, you have to decide what to do about the two instructions in between. There is no general answer, it depends on your needs.

  • If a particular C++ line calls an inline function or macro, then the reported line may well be in the inlined function or macro. This may suit you, or not.

getDebugLoc() requires that you compile with debug info. Even if you compile with full debug info, it cannot always return an origin, because an instruction doesn't always have a clear and unique origin in the source code. For example, in C++ this code requires that the line } calls Bar::~Bar():

if(foo) {
    Bar b(42);
    b.quuz();
}

But { and } are optional and this is legal:

if(foo)
    Bar b(42);

The compiler has to call Bar::~Bar() even though there's no line of code for that call. You could say that the origin of the ~Bar() call is a language rule rather than any location in the source code.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!