问题
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