如何使用clang libtooling获取宏及其扩展? [英] How to get the macro and its expansion with clang libtooling?
问题描述
我正在使用clang libtooling.
I am working on clang libtooling.
到目前为止,我已经设法通过visitDeclRefExpr(DeclRefExpr *DR)
So far I am managed to get the macro where it is getting referred in the source file through visitDeclRefExpr(DeclRefExpr *DR)
但是我可以得到一个宏的列表,它的名称和扩展名是字符串.
But can I get a list of macros with its name and its expansion as string.
exampleprogram.c
exampleprogram.c
#define abc ab
#define sum 0
int main()
{
int ab;
abc = abc + 0;
return 0;
}
我能得到如下输出吗?
abc -- ab
sum -- 0
如何使用clang libtooling实现此输出.如何使用clang libtooling实现?
How can I achieve this output with clang libtooling. How can I implement with the clang libtooling?
请让我知道是否有解决此问题的方法.
Please let me know if solution is available for this problem.
推荐答案
不幸的是,在AST中,Clang在宏中存储的是最低限度的信息. SourceLocation
包含一些有关哪些宏已在此处扩展的信息.您可以找到有关位置和宏扩展的更多信息
In AST, unfortunately, Clang stores bare minimum information on macros. SourceLocation
contains some information on which macros had been expanded there. You can find more information on locations and macro expansions here.
为了访问宏定义和扩展(可以这么说,在访问AST节点时访问它们),您需要使用Clang的预处理器. PPCalbacks 是执行此操作的正确工具.您可以从CompilerInstance
获取Preprocessor
( CompilerInstance :: getPreprocessor )并将您的回调连接到其中.
In order to get access to macro definitions and expansions (visit them as you visit AST nodes, so to speak), you need to use Clang's preprocessor. PPCalbacks is the right tool to do it. You can get Preprocessor
from CompilerInstance
(CompilerInstance::getPreprocessor) and hook your callbacks into it.
我希望这些信息有用.用Clang进行快乐的黑客入侵!
I hope this information is useful. Happy hacking with Clang!
这篇关于如何使用clang libtooling获取宏及其扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!