如何从CPP源代码行号获取LLVM位代码行号? [英] How to get LLVM bitcode line number from CPP source code line number?

查看:132
本文介绍了如何从CPP源代码行号获取LLVM位代码行号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个cpp文件和使用clang 9的位代码.现在,我从源代码中选择一个cpp行号,我想获取此源代码行的LLVM位代码行号,而不是手动对位进行反汇编代码文件并逐行读取文件.请告诉我是否可行以及如何实现.

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 instead of manually doing disassembly of bit code file and reading the file line by line. Please tell me if it is possible and how.

推荐答案

可能没有一组与该确切行明显对应的IR指令...但是大多数情况下都是可能的.有一个名为 Instruction :: getDebugLoc()的函数,该函数返回文件名和行该特定指令的编号(如果它完全返回任何内容).您可以使用它.

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.

  • 如果一条指令来自第42行,而下两条指令没有明显的原点,那么第43行就有一条指令,则必须决定对这两条指令之间的处理方式.没有一般性的答案,这取决于您的需求.

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

如果特定的C ++行调用了内联函数或宏,则所报告的行很可能位于内联函数或宏中.这也许适合您.

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()要求您使用调试信息进行编译.即使您使用完整的调试信息进行编译,它也不能始终返回原点,因为指令在源代码中并不总是具有清晰唯一的原点.例如,在C ++中,此代码要求}行调用Bar::~Bar():

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

即使没有代码行,编译器也必须调用Bar::~Bar().您可以说~Bar()调用的起源是一种语言规则,而不是源代码中的任何位置.

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.

这篇关于如何从CPP源代码行号获取LLVM位代码行号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆