如何从LLVM指令获取文件名和目录?

问题描述 投票:3回答:1

我需要在llvm传递期间提取目录和文件名。当前版本的llvm将getFilenamegetDirectoryDebugLoc移动到DebugInfoMetadata。我在getFilename标题中找不到类成员DebugLoc。那么,如何从指令转到源代码文件名和目录?

http://llvm.org/docs/doxygen/html/classllvm_1_1DebugLoc.html

此外,有一个打印功能可能会有所帮助,但它只需要一个llvm::raw_ostream,不能重定向到std::string

void print (raw_ostream &OS) const
// prints source location /path/to/file.exe:line:col @[inlined at]

下面的代码是给出错误的原因

const DebugLoc &location = an_instruction_iter->getDebugLoc()
StringRef File = location->getFilename() // Gives an error

---我几分钟前发现的解决方案----

const DebugLoc &location = i_iter->getDebugLoc();
const DILocation *test =location.get();
test->getFilename();`
clang llvm llvm-clang llvm-c++-api
1个回答
3
投票

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();
© www.soinside.com 2019 - 2024. All rights reserved.