[cfe-dev] Attach additional information to AST nodes

Benedikt Lukas Huber via cfe-dev cfe-dev at lists.llvm.org
Tue Sep 25 08:46:04 PDT 2018


Hi Clang list,

I am implementing a RecursiveASTVisitor so I can rely on a preorder traversal.
In some of the Visit* callbacks I would like to pass some information downwards
and do something like this:

  bool VisitFunctionDecl(const FunctionDecl *d) {
    ...
    d->getBody()->attachToContext(myAdditionalData);
    ...
  }

So that I can access it when the callback for the body is called:

  bool VisitCompoundStmt(const CompoundStmt *n) {
    ...
    MyAdditionalData *myData = n->getFromContext();
    ...
  }

Right now I am using a std::map with the pointer as key, i.e.

  bool VisitFunctionDecl(const FunctionDecl *d) {
    ...
    myMap[d->getBody()] = myAdditionalData;
    ...
  }

and then

  bool VisitCompoundStmt(const CompoundStmt *n) {
    ...
    MyAdditionalData *myData = myMap[n];
    ...
  }

That works, but it does not seem to be the right thing.
Is there a way to attach additional information to AST nodes?
Or should I approach that in a completely different way?

Thank you and best regards,
Benedikt

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180925/627d39c8/attachment.html>


More information about the cfe-dev mailing list