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

问题描述:

我需要在llvm传递期间提取目录和文件名。 当前版本的llvm将getFilename和getDirectory从DebugLoc移动到DebugInfoMetadata。我无法直接在DebugLoc头文件中找到类成员getFilename。因此,我该如何从指令转到源代码文件名和目录。如何从LLVM指令获取文件名和目录?

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

此外,还有打印功能,可以帮助,但只需要LLVM :: raw_ostream,不能被重定向到一个std ::字符串。

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

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(); 
+0

二的解决方案工作。第一个解决方案给出了一个错误 '没有匹配的成员函数调用'打印' location-> print(stream);' –

+0

@QuentinMayo我已经更新了答案。 – hailinzeng