如何从LLVM指令获取文件名和目录? [英] How to get the filename and directory from a LLVM Instruction?
问题描述
我需要在llvm传递过程中提取目录和文件名.
当前版本的llvm将getFilename
和getDirectory
从DebugLoc
移到了DebugInfoMetadata
.我无法直接在DebugLoc
标头中找到类成员getFilename
.因此,如何从指令转到源代码的文件名和目录?
I need to extract the directory and filename during a llvm pass.
The current version of llvm moved getFilename
and getDirectory
from DebugLoc
to DebugInfoMetadata
. I can't find a class member getFilename
directly in the DebugLoc
header. Thus, how to do I go from an instruction to source code filename and directory?
http://llvm.org/docs/doxygen/html/classllvm_1_1DebugLoc.html
此外,还有一个打印功能可能会有所帮助,但它仅需使用llvm::raw_ostream
且不能重定向到std::string
.
Additionally, there is a print function that might help but it only takes a llvm::raw_ostream
and can't be redirected to a std::string
.
void print (raw_ostream &OS) const
// prints source location /path/to/file.exe:line:col @[inlined at]
下面的代码是导致错误的原因
The code below is what gives the error
const DebugLoc &location = an_instruction_iter->getDebugLoc()
StringRef File = location->getFilename() // Gives an error
---我几分钟前想出的解决方案----
---solution I figured out a few minutes ago----
const DebugLoc &location = i_iter->getDebugLoc();
const DILocation *test =location.get();
test->getFilename();`
推荐答案
1)
std::string dbgInfo;
llvm::raw_string_ostream rso(dbgInfo);
location->print(rso);
std::sting dbgStr = rso.str()
2)
auto *Scope = cast<DIScope>(location->getScope());
std::string fileName = Scope->getFilename();
这篇关于如何从LLVM指令获取文件名和目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!