如何获取我当前使用 Clang 访问的文件的名称?

How can I get the name of the file I'm currently visiting with Clang?

本文关键字:访问 Clang 文件 何获取 获取      更新时间:2023-10-16

当我访问,让我们说,一个声明(Clang库中的Decl),我怎么能得到这个Decl被写入的文件的名称?

有一个FileData类,但我找不到任何其他类允许我得到这个FileData

可以向SourceManager查询当前文件的FileEntry。

例如在匹配器回调:

void MyMatcher::run(const MatchFinder::MatchResult& Result) {
    ASTContext* Context = Result.Context;
    if (const Decl* D = Result.Nodes.getNodeAs<Decl>("MyDecl")) {
        SourceManager& SrcMgr = Context->getSourceManager();
        const FileEntry* Entry = SrcMgr.getFileEntryForID(SrcMgr.getFileID(D.getCaretLocation()));
        const char* FileName = Entry->getName();
    }
}