<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body><div>Hi,<br></div>
<div> </div>
<div>The best way currently indeed is to add state to your visitor class - addings a stack to keep track of parents is a fairly typical use.<br></div>
<div> </div>
<div>The reason why it's (probably) not part of Clang, is probably because it's trivial to implement yourself - with some caveats, like calling the base class' Traverse* method and noting that for an AST node, the Traverse is only called for the root of its base hierarchy (i.e. Stmt, Decl, etc.) and the most specific type (e.g. CXXOperatorCallExpr), but not any intermediate base classes.<br></div>
<div> </div>
<div>Furthermore, what you actually need to track varies a lot. In a lot of uses, I had to keep track only of the parent FunctionDecl-s, and I didn't even need to go down into the Stmt level. Very often, what I actually need to keep track of is not only the AST nodes themselves, but to some data related to them...<br></div>
<div> </div>
<div>Covering any one of the possible use cases would be too narrow, and trying to cover them all would have to be so generic that it's basically useless.<br></div>
<div> </div>
<div id="sig19426265"><div class="signature">---<br></div>
<div class="signature">Best regards,<br></div>
<div class="signature"> </div>
<div class="signature">Gábor 'ShdNx' Kozár<br></div>
<div class="signature"><a href="http://gaborkozar.me">http://gaborkozar.me</a><br></div>
</div>
<div> </div>
<div> </div>
<div>On Tue, Apr 28, 2015, at 00:01, Saswat Padhi wrote:<br></div>
<blockquote type="cite"><div style="font-family:arial,helvetica,sans-serif;font-size:12pt;color:rgb(0, 0, 0);"><div>Hi Mats,<br></div>
<div> </div>
<div>Thanks for the reply.<br></div>
<div> </div>
<div>That's something I had thought about. I was thinking of adding a stack to my Visitor class, and the Traverse* functions would push and pop on to the stack to store and fetch the argument. I was hoping there would be a cleaner way to do this.<br></div>
<div>If not, I think it would definitely be a good improvement to Clang. The functions within the Visitor classes should accept one more parameter that could point to any extra data the programmer wants to send.<br></div>
<div> </div>
<div><hr><br></div>
<div><b>From: </b>"mats petersson" <mats@planetcatfish.com><br></div>
<div><b>To: </b>"Saswat Padhi" <padhi@cs.ucla.edu><br></div>
<div><b>Cc: </b>"cfe-dev" <cfe-dev@cs.uiuc.edu><br></div>
<div><b>Sent: </b>Monday, 27 April, 2015 02:03:13<br></div>
<div><b>Subject: </b>Re: [cfe-dev] Extra parameters to Traverse* functions in ASTVisitor<br></div>
<div> </div>
<div><div dir="ltr"><div>I've not worked directly with RecurisiveASTVisitor, but in my own compiler, I have my own ASTVisitor class, and the way I pass "extra information" is to add it to the class that is doing the visiting. In the RecursiveASTVisitor, that would mean adding it to the `Derived` class that is the template parameter. You then call `getDerived()` to get the derived class and dig out your extra argument that way. Would that work for your case?<br></div>
<div> </div>
<div>--<br></div>
<div>Mats<br></div>
</div>
<div><div> </div>
<div><div>On 27 April 2015 at 07:04, Saswat Padhi <span dir="ltr"><<a href="mailto:padhi@cs.ucla.edu">padhi@cs.ucla.edu</a>></span> wrote:<br></div>
<blockquote style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;margin-left-ltr-source:physical;margin-left-rtl-source:physical;margin-right-ltr-source:physical;margin-right-rtl-source:physical;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204, 204, 204);border-left-width-ltr-source:physical;border-left-width-rtl-source:physical;border-left-style-ltr-source:physical;border-left-style-rtl-source:physical;border-left-color-ltr-source:physical;border-left-color-rtl-source:physical;padding-left:1ex;padding-left-ltr-source:physical;padding-left-rtl-source:physical;">Hello everyone,<br> <br>
I am new to Clang and am trying to build a Static Checker for one of my projects. I am currently using a RecursiveASTVisitor to walk through the AST. But I also need to pass some state to the Traverse* functions, while walking through the AST. For example, when I call TraverseStmt recursively, I need to pass some extra arguments, but the Traverse* functions only accept single arguments. Is there any way I could achieve this?<br> <br>
I can keep a local stack within the ASTVisitor, but I was wondering if there would be a cleaner way to do it.<br> <br>
Thanks,<br>
Saswat<br>
_______________________________________________<br>
cfe-dev mailing list<br> <a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a></blockquote></div>
</div>
<div> </div>
</div>
</div>
<div><u>_______________________________________________</u><br></div>
<div>cfe-dev mailing list<br></div>
<div><a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br></div>
<div><a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br></div>
</blockquote><div> </div>
</body>
</html>