[cfe-dev] Matching std::set::iterator

Daniel Jasper djasper at google.com
Wed Oct 10 04:55:52 PDT 2012


+cfe-dev, please remember to include

I don't see anything generally wrong with your approach to match
iterators. What I would do to debug this is locally editing
ASTMatchers.h to add a "llvm::outs() << FullName;" debug output into
the matchesName matcher. That way, you can see what it is actually
trying to match.

The second question should work like:

  varDecl(hasType(recordDecl(
    hasDescendent(classTemplateSpecialization(hasAnyTemplateArgument(
      refersToType(hasName("std::auto_ptr")))))))).bind("id")

This is just as a general idea, it might not yet be correct.

Cheers,
Daniel

On Wed, Oct 10, 2012 at 1:23 PM, Gábor Horváth <xazax.hun at gmail.com> wrote:
> Hi!
>
> I want to create a matcher to match things like std::set<int>::iterator.
>
> I come up with this one: ...
> hasType(namedDecl(matchesName("std::set.*iterator"))) ...
> however it did not give me any match.
>
> Using the internal name of the iterator class like
> std::_Rb_tree_const_iterator would work, however that is implementation
> defined, so I do not want to rely on that one.
>
> Do you have any idea what am I doing wrong?
>
> My another question is, for example if I want to search for auto_ptr as
> template arguments in containers, I want to match vector<auto_ptr<int>> and
> also vector<vector<auto_ptr<int>>> ... etc.
>
> Is there any proper way to do it? My current solution:
>
> MatcherProxy StlCOAPPred::getMatcher()
> {
>   TypeMatcher type = unless(anything());
>
>   for(const auto& e : gContainers)
>     type = anyOf(hasDeclaration(recordDecl(hasName(e))), type);
>
>   auto templateSpecWithArgument = [](DeclarationMatcher decl) ->
> DeclarationMatcher {
>     return
> classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(hasDeclaration(decl))));
>   };
>
>   // 1, 2, 3 times embedded
>   DeclarationMatcher decl =
> anyOf(templateSpecWithArgument(recordDecl(hasName("std::auto_ptr"))),
>
> templateSpecWithArgument(templateSpecWithArgument(recordDecl(hasName("std::auto_ptr")))),
>
> templateSpecWithArgument(templateSpecWithArgument(templateSpecWithArgument(recordDecl(hasName("std::auto_ptr"))))));
>
>
>   return id("id",varDecl(allOf(hasType(decl),hasType(type))));
> }
>
> I use templateSpecWithArgument, and nesting it. I could write a loop to do
> this nesting several times, however I think that could degrade the
> performance significantly (I did not measure yet).
>
> Thanks
> Gábor




More information about the cfe-dev mailing list