How to change typedef in AST

Hi, everyone.

Is it possible to parse some code containing a typedef, then modify that typedef in the AST? For example, the input code contains typedef int blah and I want to change it to typedef float blah.

I found some example code for traversing the AST, which I modified to visit each typedef declaration:

bool VisitDecl(Decl *d) {
d->dump();printf("\n");
if (TypedefDecl::classof(d)) {
TypedefDecl *td = static_cast<TypedefDecl *>(d);
td->getTypeForDecl()->dump(); // prints “NULL TYPE”
// Type *t = ???;
// td->setTypeForDecl(t); ???
}
return true;
}

But I wasn’t sure where to go next. Any hints?

The idiomatic way of writing this would be:
if (TypedefDecl *td = dyn_cast(d)) {
or (if you’re not working on the compiler proper and you can use C++11 features):
if (auto td = dyn_cast(d)) {

That said, the AST is not designed to be mutable like this, which is why
TypedefDecl doesn’t have a setter for the type.

John.