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

后端 未结 1 1656
春和景丽
春和景丽 2021-01-26 12:51

I have a cpp file and the bitcode using clang 9. Now, I select a cpp line number from the source code and I want to get the LLVM bit code line number for this source code line i

相关标签:
1条回答
  • 2021-01-26 13:12

    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.

    0 讨论(0)
提交回复
热议问题