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

时间:2017-04-19 18:47:26

标签: clang llvm llvm-clang llvm-c++-api

我需要在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();`

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:1)

F.getParent()->getSourceFileName();

其中F是要为其获取源文件名的函数。