How to get the macro and its expansion with clang libtooling?

喜你入骨 提交于 2020-03-05 04:07:09

问题


I am working on clang libtooling.

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

#define abc ab

#define sum 0

int main()
{
int ab;
abc  = abc + 0;
return 0;
}

can I get the output like the following

abc -- ab

sum -- 0

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.


回答1:


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.

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.

I hope this information is useful. Happy hacking with Clang!



来源:https://stackoverflow.com/questions/58620022/how-to-get-the-macro-and-its-expansion-with-clang-libtooling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!