What's wrong with my AST matcher ?

Hi guys,

I’m trying to get the hang of AST matching in clang. I need a little help needed in understanding the error messages and what’s wrong with the code here:

The intent of this AST matching code is to detect this:

int y = X; ← this should be matched

int r = m; ← this shouldn’t be matched

After going through clang/ASTMatchers/ASTMatchers.h, I came up with a checker which - minus the boilerplate code - does this:

static auto findTest() → decltype(stmt()) {

auto test = varDecl(hasInitializer(ignoringParenImpCasts(declRefExpr(to(varDecl(hasName(“X”)))))));

return stmt(test);


The compiler is telling me something but I’m unable to figure out what’s the issue here. Why is it not able to create a stmt instance from test ? Also, is the checker logic sane ?

[ 87%] Built target clangIndex

[ 87%] Built target clangStaticAnalyzerCore

Scanning dependencies of target clangStaticAnalyzerCheckers

[ 87%] Building CXX object tools/clang/lib/StaticAnalyzer/Checkers/CMakeFiles/clangStaticAnalyzerCheckers.dir/testChecker.cpp.o

/Volumes/LoboElk17E202/Users/local/llvm/tools/clang/lib/StaticAnalyzer/Checkers/testChecker.cpp:40:10: error: no matching function for

call to object of type 'const internal::VariadicAllOfMatcher’

return stmt(test);


/Volumes/LoboElk17E202/Users/local/llvm/tools/clang/include/clang/ASTMatchers/ASTMatchersInternal.h:95:11: note: candidate function not

viable: no known conversion from ‘clang::ast_matchers::internal::BindableMatcherclang::Decl’ to 'const

clang::ast_matchers::internal::Matcherclang::Stmt’ for 1st argument

ResultT operator()(const ArgT &Arg1, const ArgsT &… Args) const {


/Volumes/LoboElk17E202/Users/local/llvm/tools/clang/include/clang/ASTMatchers/ASTMatchersInternal.h:101:11: note: candidate function not

viable: no known conversion from ‘clang::ast_matchers::internal::BindableMatcherclang::Decl’ to

‘ArrayRef<clang::ast_matchers::internal::Matcherclang::Stmt >’ for 1st argument

ResultT operator()(ArrayRef Args) const {


/Volumes/LoboElk17E202/Users/local/llvm/tools/clang/include/clang/ASTMatchers/ASTMatchersInternal.h:92:11: note: candidate function not

viable: requires 0 arguments, but 1 was provided

ResultT operator()() const { return Func(None); }


1 error generated.

make[2]: *** [tools/clang/lib/StaticAnalyzer/Checkers/CMakeFiles/clangStaticAnalyzerCheckers.dir/testChecker.cpp.o] Error 1

make[1]: *** [tools/clang/lib/StaticAnalyzer/Checkers/CMakeFiles/clangStaticAnalyzerCheckers.dir/all] Error 2

make: *** [all] Error 2


Hi Alan,

stmt() is a matcher for statements, but varDecl() is a matcher for declarations, so varDecl() cannot be used as an argument of stmt() matcher as-is. You should be more specific and point that you want to match not just any Stmt but DeclStmt that contains a declaration you want to match:

return declStmt(containsDeclaration(test));

Alternatively, if you only want to find a declaration, not a statement that declares it, you can just drop stmt() matcher.

30.06.2018 07:20, Alan Davidson via cfe-dev пишет: