[cfe-dev] Matcher with expressions

Pedro Delgado Perez pedro.delgado at uca.es
Wed Oct 23 04:39:36 PDT 2013


 
Hi,

Now that I have updated my clang version, I have been testing the matcher you suggested me to find invocations to a constructor method of a parent class:

constructExpr(hasDeclaration(methodDecl(ofClass(recordDecl().bind("constructedType")))), hasAncestor(methodDecl(ofClass(recordDecl(isDerivedFrom(equalsBoundNode("constructedType"))))))).bind("constructExpr")

However, it seems that the "hasAncestor" is not functioning as expected. If I test only:

constructExpr(hasDeclaration(methodDecl(ofClass(recordDecl().bind("constructedType"))))).bind("constructExpr")

the matcher finds every CXXConstructorExpr in the code, so the problem is in the hasAncestor(). I have tested as well with hasParent(), but it didn't work neither. By the way, what's the difference between hasAncestor and hasParent?

In addition, what I'm looking for exactly are these cases marked in bold, but not the one marked in red:

class A1{
  A1(int a) {...}
  ...
};

class A2{
  A2() {...}
  A2(int a) {...}
  A2(int a, int b) {...}
  ...
};

class B: public A1, public A2{
    B (): A1(4) { A2(3,3); A2(3); }
    void b() { A1 a1(3); }
}

I've been analyzing these cases and I can't understand why A2(3,3) is a CXXBindTemporaryBindExpr while A2(3); is a CXXFunctionalCastExpr with a CXXConstructExprinside. All this is pretty confusing...and I would like to be sure that I'm binding all the possible cases...

Thanks in advance,

Pedro.



El día 27 sep 2013 12:13, Manuel Klimek <klimek at google.com> escribió:
> On Fri, Sep 27, 2013 at 11:57 AM, Pedro Delgado Perez <pedro.delgadoperez at mail.uca.es [ mailto:pedro.delgadoperez at mail.uca.es ]> wrote:
> 
> 
> 
> 
> Thanks a lot, Manuel. It looks great, but unfortunately, I can't test it as my clang version doesn't recognize equalsBoundNode... I have to solve this problem as my system is not able to identify lots of the matchers in the reference...
> 
Don't use released clang versions, use svn head...
 

Regards,

Pedro.


El día 27 sep 2013 08:33, Manuel Klimek <klimek at google.com [ mailto:klimek at google.com ]> escribió:



Something like (untested):
constructExpr(hasDeclaration(methodDecl(ofClass(recordDecl().bind("constructedType")))), hasAncestor(methodDecl(ofClass(recordDecl(isDerivedFrom(equalsBoundNode("constructedType"))))))).bind("constructExpr")

Cheers,
/Manuel



On Thu, Sep 26, 2013 at 11:40 AM, Pedro Delgado Perez <pedro.delgado at uca.es [ mailto:pedro.delgado at uca.es ]> wrote:




I'm not sure what the question is. You do not want definitions of what?

What I need is to find every call to a parent class constructor. Regarding the example I put, I want to match the invokation "A()" and I'm pretty lost in this case...

Use hasType to match on the type of an expression.

Ok, that's good. But, I don't know how could I express that the type is a constructorDecl from a parent class...

Thanks,

Pedro.


El día 26 sep 2013 11:19, Manuel Klimek <klimek at google.com [ mailto:klimek at google.com ]> escribió:
On Thu, Sep 26, 2013 at 9:26 AM, Pedro Delgado Perez <pedro.delgadoperez at mail.uca.es [ mailto:pedro.delgadoperez at mail.uca.es ]> wrote:




Hello,

Sorry to write you directly, but I have been sending this message since two days ago to cfe-dev but my message is not posted and I need help as soon as possible.

This is the message:

"Up to now, I have been using matchers to match declarations, but now, I need to match an expression and I am having some problems.
I am looking for a CXXConstructExpr like the one marked in bold in the next example:
class A{
  A() {...}
  ...
};
class B: public A{
    B () { A (); }
}
I think the matcher should follow this structure:
StatementMatcher CE = constructExpr(...);

Then:

1. If I put nothing within the parentheses, "A()" in class A, "B()" and "A()" in class B are all retrieved. Therefore, I need to indicate that I do not want the definitions. Thus, I thought about the matcher "isDefinition", but it is only for the type "Decl", how can I solve this problem? 
The only thing that I have found is the matcher "hasDeclaration", but I do not know how this matcher could help me in this task. 

I'm not sure what the question is. You do not want definitions of what?

2. I also need to ensure that the constructor invoked belongs to a parent class. In the same way, when matching declarations I can ask if the CXXConstructDecl "B()" is "ofClass(recordDecl(isDerivedFrom(...)))". But, with an expression...

Use hasType to match on the type of an expression.
Cheers,
/Manuel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20131023/1905d6d0/attachment.html>


More information about the cfe-dev mailing list