Clang-tidy matcher for pointers of a given type


I am trying to create a matcher that finds all declared objects of a given class name. So far I managed to create these matchers:

 Finder->addMatcher(varDecl(hasType(cxxRecordDecl(isSameOrDerivedFrom(hasAnyName(superClassNames))))).bind("qWidgetVar"), this);
  Finder->addMatcher(fieldDecl(hasType(cxxRecordDecl(isSameOrDerivedFrom(hasAnyName(superClassNames))))).bind("qWidgetField"), this);

The problem I am encountering is that for pointers to objects of the classes I’m interested in these checkers don’t work. I tried different matchers but I couldn’t get it working. An example of what I’m struggling with:

class InterestingClass

InterestingClass interestingClassVar; // works
InterestingClass* interestingClassPtrVar; // doesn't work

class DerivedInterestingClass : public InterestingClass

DerivedInterestingClass derivedInterestingClassVar; // works
DerivedInterestingClass* derivedInterestingClassVarPtr; // doesn't work

Any ideas for a matcher that does the trick?

A bit late to the party. Assuming you still need this, the following matcher should do the trick:


It is similar to the first matcher you wrote with the exception of the pointsTo part.
If you want a matcher that binds to both pointers and instances of the given type and you can use anyOf:

auto m = cxxRecordDecl(isSameOrDerivedFrom(hasName("InterestingClass")));

Hope this was helpful!