<div dir="ltr">On Tue, Sep 17, 2013 at 2:24 PM, Jesper Eskilson <span dir="ltr"><<a href="mailto:jesper.eskilson@iar.com" target="_blank">jesper.eskilson@iar.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
Hello,<br>
<br>
I'm trying to write an AST matcher rule which is able to match method definitions. Given the following C++ source code:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
class B<br>
{<br>
void foo();<br>
void bar() {<br>
int a_local_variable_in_bar;<br>
}<br>
};<br>
<br>
void B::foo()<br>
{<br>
int a_local_variable_in_foo;<br>
}<br>
<br>
B b<br>
<br>
int main()<br>
{<br>
}<br>
</blockquote>
<br>
I would like a matcher which can tell me where instances of B are created and also give me the definitions of its methods (in this case "foo" and "bar").<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
StatementMatcher m = constructExpr(<br>
hasType(<br>
recordDecl(<u></u>isSameOrDerivedFrom("B"),<br>
hasMethod(methodDecl(<u></u>isDefinition()).bind("method")<u></u>))));<br>
</blockquote>
<br>
But it will only give me the definition of "bar", and not the defintion of "foo" which is declared outside the class.<br></blockquote><div><br></div><div>This is because hasMethod will not give you anything outside of the class definition. The reason is that you often don't see out-of-class method definitions of when you have a class definition.</div>
<div><br></div><div>If you already know the name of the class (as you specify it in your matcher), why do you want to write this matcher in the first place? You'll not be able to match all constructor calls and method definitions in general, as classes may be instantiated in a translation unit where not all method definitions are visible.</div>
<div><br></div><div>If you have more details on what you're actually trying to do (on a higher level) we might be able to help more...</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
If I cannot do this with matchers, is there a way to get to the definition of "foo" given the declaration of the class B?<br></blockquote><div><br></div><div>No, as you might see the class definition in a header where foo is not visible. Usually you'll want to go the other way - find all method definitions of class "B".</div>
<div><br></div><div>Cheers,</div><div>/Manuel</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Thanks,<span class="HOEnZb"><font color="#888888"><br>
<br>
-- <br>
*Jesper Eskilson* /Development Engineer/<br>
IAR Systems AB<br>
Box 23051, Strandbodgatan 1<br>
SE-750 23 Uppsala, SWEDEN<br>
E-mail: <a href="mailto:jesper.eskilson@iar.com" target="_blank">jesper.eskilson@iar.com</a> <mailto:<a href="mailto:jesper.eskilson@iar.com" target="_blank">jesper.eskilson@iar.<u></u>com</a>> Website: <a href="http://www.iar.com" target="_blank">www.iar.com</a><br>
<<a href="http://www.iar.com" target="_blank">http://www.iar.com</a>> Twitter: <a href="http://www.twitter.com/iarsystems" target="_blank">www.twitter.com/iarsystems</a> <<a href="http://www.twitter.com/iarsystems" target="_blank">http://www.twitter.com/<u></u>iarsystems</a>><br>
______________________________<u></u>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu" target="_blank">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/<u></u>mailman/listinfo/cfe-dev</a><br>
</font></span></blockquote></div><br></div></div>