<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <div class="moz-cite-prefix">Hi Alan,<br>
      <br>
      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:<br>
      <br>
      return declStmt(containsDeclaration(test));<br>
      <br>
      Alternatively, if you only want to find a declaration, not a
      statement that declares it, you can just drop stmt() matcher.<br>
      <br>
      <br>
      <br>
      30.06.2018 07:20, Alan Davidson via cfe-dev пишет:<br>
    </div>
    <blockquote type="cite"
cite="mid:CAAGQa7J6wYwgjUSPq2S33Wg_8vgzwQu-2PNbxMR_Ng32KPc+Aw@mail.gmail.com">
      <div dir="ltr">
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures">Hi guys,</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures"><br>
          </span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures">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:</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures"><br>
          </span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures">The
            intent of this AST matching code is to detect this:</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures"><br>
          </span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures">int y =
            X;  <-- this should be matched</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures">int r =
            m;  <-- this shouldn't be matched</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures"><br>
          </span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures">After
            going through clang/ASTMatchers/ASTMatchers.h, I came up
            with a checker which - minus the boilerplate code - does
            this:</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures"><br>
          </span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures">...</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures">...</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures">static
            auto findTest() -> decltype(stmt()) {</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);min-height:13px"><span
            style="font-variant-ligatures:no-common-ligatures"></span><br>
        </p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures">  auto
            test =
varDecl(hasInitializer(ignoringParenImpCasts(declRefExpr(to(varDecl(hasName("X")))))));</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures">  return
            stmt(test);</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">}<br>
        </p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">..</p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">..</p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><br>
        </p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">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 ?</p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><br>
        </p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><br>
        </p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures">[ 87%]
            Built target clangIndex</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures">[ 87%]
            Built target clangStaticAnalyzerCore</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(200,20,201)"><span
            style="font-variant-ligatures:no-common-ligatures"><b>Scanning
              dependencies of target clangStaticAnalyzerCheckers</b></span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(47,180,29)"><span
style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">[
            87%] </span><span
            style="font-variant-ligatures:no-common-ligatures">Building
            CXX object
tools/clang/lib/StaticAnalyzer/Checkers/CMakeFiles/clangStaticAnalyzerCheckers.dir/testChecker.cpp.o</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures"><b>/Volumes/LoboElk17E202/Users/local/llvm/tools/clang/lib/StaticAnalyzer/Checkers/testChecker.cpp:40:10:
            </b></span><span
            style="font-variant-ligatures:no-common-ligatures;color:rgb(180,36,25)"><b>error:
            </b></span><span
            style="font-variant-ligatures:no-common-ligatures"><b>no
              matching function for</b></span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures"><b>     
              call to object of type 'const
              internal::VariadicAllOfMatcher<Stmt>'</b></span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures">  return
            stmt(test</span>);</p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(47,180,29)"><span
            style="font-variant-ligatures:no-common-ligatures"><b>      
                ^~~~</b></span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures"><b>/Volumes/LoboElk17E202/Users/local/llvm/tools/clang/include/clang/ASTMatchers/ASTMatchersInternal.h:95:11:
              note: </b>candidate function not</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures">     
            viable: no known conversion from
            'clang::ast_matchers::internal::BindableMatcher<clang::Decl>'
            to 'const</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures">     
            clang::ast_matchers::internal::Matcher<clang::Stmt>'
            for 1st argument</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures">  ResultT
            operator()(const ArgT &Arg1, const ArgsT &... Args)
            const {</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(47,180,29)"><span
            style="font-variant-ligatures:no-common-ligatures"><b>     
                  ^</b></span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures"><b>/Volumes/LoboElk17E202/Users/local/llvm/tools/clang/include/clang/ASTMatchers/ASTMatchersInternal.h:101:11:
              note: </b>candidate function not</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures">     
            viable: no known conversion from
            'clang::ast_matchers::internal::BindableMatcher<clang::Decl>'
            to</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures">     
            'ArrayRef<clang::ast_matchers::internal::Matcher<clang::Stmt>
            >' for 1st argument</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures">  ResultT
            operator()(ArrayRef<ArgT> Args) const {</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(47,180,29)"><span
            style="font-variant-ligatures:no-common-ligatures"><b>     
                  ^</b></span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures"><b>/Volumes/LoboElk17E202/Users/local/llvm/tools/clang/include/clang/ASTMatchers/ASTMatchersInternal.h:92:11:
              note: </b>candidate function not</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures">     
            viable: requires 0 arguments, but 1 was provided</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures">  ResultT
            operator()() const { return Func(None); }</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(47,180,29)"><span
            style="font-variant-ligatures:no-common-ligatures"><b>     
                  ^</b></span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures">1 error
            generated.</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures">make[2]:
            ***
[tools/clang/lib/StaticAnalyzer/Checkers/CMakeFiles/clangStaticAnalyzerCheckers.dir/testChecker.cpp.o]
            Error 1</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures">make[1]:
            ***
[tools/clang/lib/StaticAnalyzer/Checkers/CMakeFiles/clangStaticAnalyzerCheckers.dir/all]
            Error 2</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span
            style="font-variant-ligatures:no-common-ligatures">make: ***
            [all] Error 2</span></p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><br>
        </p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><br>
        </p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">Alan</p>
        <p
style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><br>
        </p>
      </div>
      <!--'"--><br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
cfe-dev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>
<a class="moz-txt-link-freetext" href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a>
</pre>
    </blockquote>
    <p><br>
    </p>
  </body>
</html>