<div dir="ltr">On Tue, Sep 17, 2013 at 3:27 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">On 09/17/2013 03:20 PM, Manuel Klimek wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
On Tue, Sep 17, 2013 at 3:17 PM, Jesper Eskilson <<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>>> wrote:<br>
<br>
On 09/17/2013 02:50 PM, Manuel Klimek wrote:<br>
<br>
On Tue, Sep 17, 2013 at 2:24 PM, Jesper Eskilson<br>
<<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>><br></div>
<mailto:<a href="mailto:jesper.eskilson@iar.com" target="_blank">jesper.eskilson@iar.<u></u>com</a><div><div class="h5"><br>
<mailto:<a href="mailto:jesper.eskilson@iar.com" target="_blank">jesper.eskilson@iar.<u></u>com</a>>>> wrote:<br>
<br>
<br>
Hello,<br>
<br>
I'm trying to write an AST matcher rule which is able to match<br>
method definitions. Given the following C++ source code:<br>
<br>
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>
<br>
<br>
I would like a matcher which can tell me where instances<br>
of B are<br>
created and also give me the definitions of its methods<br>
(in this<br>
case "foo" and "bar").<br>
<br>
StatementMatcher m = constructExpr(<br>
hasType(<br>
recordDecl(<u></u>isSameOrDerivedFrom("B"),<br>
hasMethod(methodDecl(<u></u>isDefinition()).bind("method")<u></u>))));<br>
<br>
<br>
But it will only give me the definition of "bar", and not the<br>
defintion of "foo" which is declared outside the class.<br>
<br>
<br>
This is because hasMethod will not give you anything outside<br>
of the class definition. The reason is that you often don't<br>
see out-of-class method definitions of when you have a class<br>
definition.<br>
<br>
If you already know the name of the class (as you specify it<br>
in your matcher), why do you want to write this matcher in the<br>
first place? You'll not be able to match all constructor calls<br>
and method definitions in general, as classes may be<br>
instantiated in a translation unit where not all method<br>
definitions are visible.<br>
<br>
If you have more details on what you're actually trying to do<br>
(on a higher level) we might be able to help more...<br>
<br>
If I cannot do this with matchers, is there a way to get<br>
to the<br>
definition of "foo" given the declaration of the class B?<br>
<br>
<br>
No, as you might see the class definition in a header where<br>
foo is not visible. Usually you'll want to go the other way -<br>
find all method definitions of class "B".<br>
<br>
<br>
I have a bunch of class instantiations in a large codebase which<br>
look like this<br>
<br>
A a1("banana", new B1);<br>
A a2("apple", new B2);<br>
A a3("ananas", new B2);<br>
<br>
B1, B2, B3, all inherit from B which defines a method "foo"<br>
(outside the declaration of B). This definition may or may not be<br>
overridden in B1/B2/B3.<br>
<br>
I want to be able to generate output on the form:<br>
<br>
banana:<br>
void foo() { /* this is the foo implementation in class B1 */ }<br>
apple:<br>
void foo() { /* this is the foo implementation in class B2 */ }<br>
ananas:<br>
void foo() { /* this is the foo implementation in class B3 */ }<br>
<br>
I was thinking about first matching out all the different<br>
definitions of foo, and then in a separate matcher match the<br>
construction expressions of a1-a3, but I'm not sure of the best<br>
way to "share" information between matchers.<br>
<br>
<br>
Create intermediate output:<br>
- output all paremeter combinations ("banana", "B1"), ("apple, "B2")<br>
- output all method definitions ("B1", "void foo() { ... }"), ...<br>
store the intermediate results. After running over all translation units, do a "reduce" step where you combine the information by using the class as the key<br>
</div></div></blockquote>
<br>
How do construct the key so that I get unique names for B1-B3, for example if they are qualified by namespaces? Typically it will look like this<br>
<br>
A a1("banana", new fruits::behavior);<br>
A a1("tomato", new vegetables::behavior);<br></blockquote><div><br></div><div>Sorry for the late answer, I was on vacation.</div><div><br></div><div>The solution we're using is to use a combination of the file name and the fully qualified name as key.</div>
<div> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
<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>><br></div>
<mailto:<a href="mailto:jesper.eskilson@iar.com" target="_blank">jesper.eskilson@iar.<u></u>com</a> <mailto:<a href="mailto:jesper.eskilson@iar.com" target="_blank">jesper.eskilson@iar.<u></u>com</a>>><div class="im">
<br>
Website: <a href="http://www.iar.com" target="_blank">www.iar.com</a> <<a href="http://www.iar.com" target="_blank">http://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><br>
<<a href="http://www.twitter.com/iarsystems" target="_blank">http://www.twitter.com/<u></u>iarsystems</a>><br></div>
<<a href="http://www.twitter.com/iarsystems" target="_blank">http://www.twitter.com/<u></u>iarsystems</a>><br>
<br>
<br>
</blockquote><div class="HOEnZb"><div class="h5">
<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>
</div></div></blockquote></div><br></div></div>