如何用clang获取变量定义的字面位置

2024-06-16 13:25:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下场景。在

一些头像

#define SOME_VARS \
  int first_var; \
  char other_var;

源文件.c

^{pr2}$

现在有了clang,我可以转储AST,看看它知道这些变量是在哪里定义的:

clang -Xclang -ast-dump -fsyntax-only source_file.c

TranslationUnitDecl 0x8007f4c0 <<invalid sloc>>
|-TypedefDecl 0x8007f790 <<invalid sloc>> __builtin_va_list 'char *'
|-VarDecl 0x8007f7d0 <./some_header.h:2:3, col:7> first_var 'int'
                      -------------------------- this is the part i'm looking for
`-VarDecl 0x8007f810 <line:3:3, col:8> other_var 'char'
                     ----------------- and this

我试图使用libclang和python来获取这些信息,但是到目前为止我还没有成功。在

我需要在哪里查找这些信息?这个信息在C接口中可用,但可能还没有移植到Python上吗?在


Tags: 信息var场景colthisintclangfirst