Given an AST object in clang, how can I get the code behind it? I tried editing the code in the tutorial, and added:
clang::SourceLocation _b = d->getLocStart(), _e = d->getLocEnd();
char *b = sourceManager->getCharacterData(_b),
e = sourceManager->getCharacterData(_E);
llvm:errs() << std::string(b, e-b) << "\n";
but alas, it didn't print the whole typedef declaration, only about half of it! The same phenomena happened when printing Expr
.
How can I print and see the whole original string constituting the declaration?
Elazar's method worked for me except when a macro was involved. The following correction resolved it:
Use the
Lexer
module:The following code works for me.