Clang getFilename返回一个空字符串
我有一个文件,我想从中提取其文件名。显然,我应该可以通过使用的。然而,当我处理一些头文件时,结果似乎总是一个空字符串 我遵循源代码,发现问题在于Clang getFilename返回一个空字符串,clang,llvm,llvm-clang,Clang,Llvm,Llvm Clang,我有一个文件,我想从中提取其文件名。显然,我应该可以通过使用的。然而,当我处理一些头文件时,结果似乎总是一个空字符串 我遵循源代码,发现问题在于getFilename函数中,该函数的内容如下: /// Return the filename of the file containing a SourceLocation. StringRef getFilename(SourceLocation SpellingLoc) const { if (const FileEntry *F = get
getFilename
函数中,该函数的内容如下:
/// Return the filename of the file containing a SourceLocation.
StringRef getFilename(SourceLocation SpellingLoc) const {
if (const FileEntry *F = getFileEntryForID(getFileID(SpellingLoc)))
return F->getName();
return StringRef();
}
从某种意义上说,getFileID
的结果是无效的,由它构造的SLocEntry
将有isFile
返回false
。这会导致(在引擎盖下构造SLocEntry
)返回空指针
我有一个变通办法,即:
StringRef myGetFilename(SourceLocation SpellingLoc) const {
std::pair<FileID, unsigned> locInfo = getDecomposedExpansionLoc(SpellingLoc);
if (const FileEntry *F = srcManager.getFileEntryForID(locInfo.first))
return F->getName();
return StringRef();
}
StringRef myGetFilename(SourceLocation SpellingLoc)常量{
std::pair locInfo=getDecomposedExpansionLoc(SpellingLoc);
if(const FileEntry*F=srcManager.getFileEntryForID(locInfo.first))
返回F->getName();
返回StringRef();
}
也就是说,首先调用getDecomposedExpansionLoc
以获取原始FileID
并在getFileEntryForID
中使用它
从实验上看,这似乎很有效,但这是我第一天使用叮当,所以我很不确定它是否真的正确。所以我有两个问题:
谢谢 啊,所以问题似乎是
getFilename
需要一种特定的SourceLocation
,即“SpellingLoc”。因此,改变:
srcManager.getFilename(loc)
到
这将解决问题
srcManager.getFilename(srcManager.getSpellingLoc(loc))