[cfe-dev] How to change typedef in AST
John McCall
rjmccall at apple.com
Thu Jan 3 11:54:08 PST 2013
On Dec 22, 2012, at 8:15 PM, Jaymie Strecker <jstrecker at kosada.com> wrote:
> 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);
The idiomatic way of writing this would be:
if (TypedefDecl *td = dyn_cast<TypedefDecl>(d)) {
or (if you're not working on the compiler proper and you can use C++11 features):
if (auto td = dyn_cast<TypedefDecl>(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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130103/254aeefb/attachment.html>
More information about the cfe-dev
mailing list