[cfe-dev] What's wrong with my AST matcher ?

Alan Davidson via cfe-dev cfe-dev at lists.llvm.org
Fri Jun 29 21:20:04 PDT 2018


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<Stmt>'*

  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::BindableMatcher<clang::Decl>' to 'const

      clang::ast_matchers::internal::Matcher<clang::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::BindableMatcher<clang::Decl>' to

      'ArrayRef<clang::ast_matchers::internal::Matcher<clang::Stmt> >' for
1st argument

  ResultT operator()(ArrayRef<ArgT> 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



Alan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180629/8536531c/attachment.html>


More information about the cfe-dev mailing list