<div dir="ltr">Hi Piotr,<div><br></div><div>I think I found a working matcher: match ifStmt(hasCondition(implicitCastExpr(hasImplicitDestinationType(isInteger()), has(cxxMemberCallExpr(callee(cxxMethodDecl(hasName("compare"))), hasArgument(0, declRefExpr().bind("str2")), callee(memberExpr(has(declRefExpr().bind("str1"))))))))).bind("case1")</div><div><br></div><div>This one bind to both str1 and str2 in str1.compare(str2). I have included a code segment below. I have attached a screenshot of this matcher working from clang-query.</div><div>HOWEVER - when I try to use the matcher in clang-tidy it will not work. It is because of the <span style="line-height:1.5">callee(memberExpr(has(declRefExpr().bind("str1")))) part of the matcher. If I remove that, I can match on the str1.compare(str2) from the code. But I need to bind str1 as well to test if it is a string.</span></div><div><span style="line-height:1.5"><br></span></div><div><span style="line-height:1.5">Do you have any idea why a matcher would work in clang-query, but not in clang-tidy? It's the same piece of code it is working on. Is there something wrong with my matcher (undefined behavior or something)?</span></div><div><br></div><div>Code: </div><div><div>#include <iostream></div><div>#include <string></div><div><br></div><div>int main() {</div><div> std::string str1{"aa"};</div><div> std::string str2{"bb"};</div><div><br></div><div> if(str1.compare(str2)) {</div><div> std::cout << "Strings not equal" << std::endl;</div><div> }</div><div><br></div><div> return 0;</div><div><br></div><div>}</div></div><div><br></div><div><br></div><div><br></div><br><div class="gmail_quote"><div dir="ltr">On Sun, Nov 27, 2016 at 10:39 PM Piotr Padlewski <<a href="mailto:piotr.padlewski@gmail.com">piotr.padlewski@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class="gmail_msg">Sorry for writing 3 emails,<div class="gmail_msg">I looked into <a href="http://clang.llvm.org/docs/LibASTMatchersReference.html" class="gmail_msg" target="_blank">http://clang.llvm.org/docs/LibASTMatchersReference.html</a>? and it seems that matcher </div><div class="gmail_msg">on : </div><table style="font-family:"lucida grande","lucida sans unicode",arial,verdana,helvetica,sans-serif" class="gmail_msg"><tbody class="gmail_msg"><tr style="vertical-align:top" class="gmail_msg"><td style="padding:0.33em" class="gmail_msg">Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1CXXMemberCallExpr.html" class="gmail_msg" target="_blank">CXXMemberCallExpr</a>></td><td class="m_-7096765933453598891gmail-name gmail_msg" style="padding:0.33em"><font color="#1155cc" class="gmail_msg">-></font></td><td style="padding:0.33em" class="gmail_msg">Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Expr.html" class="gmail_msg" target="_blank">Expr</a>> InnerMatcher</td></tr></tbody></table><br class="gmail_msg"><div class="gmail_msg">would be my candidate to check. If it won't gonna work then send the test and AST dump and I will try to help.</div></div><div dir="ltr" class="gmail_msg"><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Piotr</div></div><div class="gmail_extra gmail_msg"><br class="gmail_msg"><div class="gmail_quote gmail_msg">2016-11-27 22:35 GMT+01:00 Piotr Padlewski <span dir="ltr" class="gmail_msg"><<a href="mailto:piotr.padlewski@gmail.com" class="gmail_msg" target="_blank">piotr.padlewski@gmail.com</a>></span>:<br class="gmail_msg"><blockquote class="gmail_quote gmail_msg" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class="gmail_msg">Adding cfe-dev, because it is related to clang, not LLVM.</div><div class="m_-7096765933453598891HOEnZb gmail_msg"><div class="m_-7096765933453598891h5 gmail_msg"><div class="gmail_extra gmail_msg"><br class="gmail_msg"><div class="gmail_quote gmail_msg">2016-11-27 22:34 GMT+01:00 Piotr Padlewski <span dir="ltr" class="gmail_msg"><<a href="mailto:piotr.padlewski@gmail.com" class="gmail_msg" target="_blank">piotr.padlewski@gmail.com</a>></span>:<br class="gmail_msg"><blockquote class="gmail_quote gmail_msg" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class="gmail_msg">Hi Mads,<div class="gmail_msg">Can you provide the code that you run clang-query on, or at least AST for the fragment you want to match?</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Piotr</div></div><div class="gmail_extra gmail_msg"><br class="gmail_msg"><div class="gmail_quote gmail_msg"><div class="gmail_msg"><div class="m_-7096765933453598891m_-1636274675611907480h5 gmail_msg">2016-11-26 22:27 GMT+01:00 Mads Ravn via llvm-dev <span dir="ltr" class="gmail_msg"><<a href="mailto:llvm-dev@lists.llvm.org" class="gmail_msg" target="_blank">llvm-dev@lists.llvm.org</a>></span>:<br class="gmail_msg"></div></div><blockquote class="gmail_quote gmail_msg" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="gmail_msg"><div class="m_-7096765933453598891m_-1636274675611907480h5 gmail_msg"><div dir="ltr" class="gmail_msg">Hi,<div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Hope this is the right channel for this question. I am trying to make an ast matcher for clang-tidy to find str1.compare(str2), where both str1 and str2 are std::string. I have this so far: <a href="http://i.imgur.com/sUma9WC.png" class="gmail_msg" target="_blank">http://i.imgur.com/sUma9WC.png</a> . But I am having a hard time finding a way to bind an id to the str1 part of the expression.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Can anyone help me out with an idea?</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Best regards,</div><div class="gmail_msg">Mads Ravn</div></div>
<br class="gmail_msg"></div></div>_______________________________________________<br class="gmail_msg">
LLVM Developers mailing list<br class="gmail_msg">
<a href="mailto:llvm-dev@lists.llvm.org" class="gmail_msg" target="_blank">llvm-dev@lists.llvm.org</a><br class="gmail_msg">
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" class="gmail_msg" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br class="gmail_msg">
<br class="gmail_msg"></blockquote></div><br class="gmail_msg"></div>
</blockquote></div><br class="gmail_msg"></div>
</div></div></blockquote></div><br class="gmail_msg"></div>
</blockquote></div></div>