如何使用 Clang 的 C++ API 从文件名/行/列三元组中获取源位置

How can I get a SourceLocation from a filename/line/column triplet using Clang's C++ API

本文关键字:三元组 获取 位置 文件名 Clang 何使用 C++ API      更新时间:2023-10-16

libclang C API具有以下功能:

CXSourceLocation clang_getLocation( CXTranslationUnit tu, CXFile file, unsigned line, unsigned column )

我找不到一个等价的c++ API。有许多getLocation函数,但没有一个接受这组参数。

我最终试图在给定的源位置获得DeclRef,如果它存在。

通过SourceManager查找file:line:column三元组的SourceLocation可能有些笨拙,具体如下:

SourceManager& srcmgr = astctx.getSourceManager();
FileManager& filemgr = srcmgr.getFileManager();
const FileEntry* file_entry_ptr = filemgr.getFile(filename);
SourceLocation loc = srcmgr.translateFileLineCol(file_entry_ptr, line, column);

我仍然不知道如何找到Stmt在这一点上,虽然