[PATCH] D27207: Adds hasUnqualifiedDesugaredType to allow matching through type sugar.

Ɓukasz Anforowicz via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 29 14:14:30 PST 2016


lukasza added a comment.

I've tried replicating the deep-matching by saying qualType(hasType(hasUnqualifiedDesugaredType(hasDeclaration(... but this doesn't work because hasDeclaration only returns a matcher for a specific type from a subset of subclasses of Type - this is incompatible with expectations of the proposed hasUnqualifiedDesugaredType which takes a Matcher<Type>.

FWIW, the following matcher worked for me:

  AST_MATCHER_P(Type, hasUnqualifiedDesugaredType, internal::Matcher<QualType>,
                InnerMatcher) {
    const Type* type = Node.getUnqualifiedDesugaredType();
    QualType qualType(type, 0);
    return InnerMatcher.matches(qualType, Finder, Builder);
  }
  
  TEST(HasDeclaration, DeepTagType) {
    std::string input =
        "class Foo {};\n"
        "using Bar = Foo;\n"
        "using Baz = Bar;\n"
        "void Function(Baz param) {}\n";
  
    // Matcher for declaration of the Foo class.
    auto param_type_decl_matcher = cxxRecordDecl(hasName("Foo"));
  
    auto m1 = qualType(hasDeclaration(decl(param_type_decl_matcher)));
    // hasDeclaration / qualType-flavour.
    EXPECT_TRUE(matches(input, parmVarDecl(
        hasName("param"),
        hasType(hasUnqualifiedDesugaredType(m1)))));
  }

I think the above will work for my tool - thank you for providing the matcher example (for some reason I incorrectly thought that desugaring would only be done one step a time - this made me think that matching at an arbitrary depth will require tricky things to build a recursive matcher out of a single-step matcher).

BTW: Please note that I am fine with just copy&pasting the matcher above directly into my tool - I don't necessarily need this matcher to be part of the generic library of matchers.  I don't have high confidence that the matcher here is 1) applicable broadly (maybe) and 2) what to do about the Type vs QualType issue (yes, it says "unqualified" right in the name, but then it means that it cannot be used with hasDeclaration...).


https://reviews.llvm.org/D27207





More information about the cfe-commits mailing list