Problem using ast matchers macros

I’m trying to implement an ast matcher for the IHD (hidden variable deletion) operator. For this purpose, i also define two matchers using the ast macros. The code is as follows:

namespace clang{
namespace ast_matchers{
using namespace clang::ast_matchers::internal;

// Checks that two FieldDecl nodes have the same type
AST_MATCHER_P(FieldDecl, SameType, FieldDecl, f)
{
return Node.getType() == f.getType();
}

// Checks that two FieldDecl nodes have the same name
AST_MATCHER_P(FieldDecl, SameName, FieldDecl, f)
{
return Node.getName() == f.getName();
}

}
}

And the code for the IHD operator is:

DeclarationMatcher IHD__Matcher = recordDecl(
has(
fieldDecl().bind(“IHD”)
),
isDerivedFrom(
SameName(
SameType(
recordDecl(
has(
fieldDecl()
)
)
)
)
)
);

The problem is that when i compile i get the following errors:

clang++ -I/usr/local/include -D_DEBUG -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -g -fvisibility-inlines-hidden -fno-exceptions -fno-rtti -fPIC -Woverloaded-virtual -Wcast-qual -fno-rtti -c -o IHD_operator.o IHD_operator.cpp
IHD_operator.cpp:83:6: error: no matching function for call to ‘SameType’
SameType(
^~~~~~~~
IHD_operator.cpp:39:28: note: candidate function not viable: no known conversion from ‘clang::ast_matchers::internal::BindableMatcherclang::Decl’ to
‘const clang::FieldDecl’ for 1st argument
AST_MATCHER_P(FieldDecl, SameType, FieldDecl, f)
^
/usr/local/include/clang/ASTMatchers/ASTMatchersMacros.h:86:32: note: expanded from macro ‘AST_MATCHER_P’
AST_MATCHER_P_OVERLOAD(Type, DefineMatcher, ParamType, Param, 0)
^
/usr/local/include/clang/ASTMatchers/ASTMatchersMacros.h:104:34: note: expanded from macro ‘AST_MATCHER_P_OVERLOAD’
inline internal::Matcher DefineMatcher(const ParamType &Param) {
^
1 error generated.
make: *** [IHD_operator.o] Error 1

I don’t know why is trying to make a conversion to FieldDecl from Decl if it is supose to be receiving a FieldDecl type param.

The source of the failure is where you have SameType(recordDecl(…)). You’ll notice in the reference that recordDecl() returns a Matcher. So your SameType() matcher is going to receive a ‘Decl’ as input. You’ll need to explicitly change this into a FieldDecl. I’m afraid I don’t know exactly what you’re trying to match or what IHD is so I can’t tell if there’s a more fundamental error here.

Actually, the problem is that you’re passing a matcher as argument, while you’ve defined your matchers to accept FieldDecl (the 3rd argument for the AST_MATCHER_P macro) - but the compiler already told you this.

So I don’t really see what you’re confused about. For that matter, nor can I really tell what you’re trying to do in your matcher expression… What is it that you’re trying to express here?

You can look at the equalsBoundNode matcher to get an idea of how something like you are trying to do can be implemented.