<div dir="ltr">Thank you. Just one more thing: How do I restrict the AST to a single source file (passed as an argument to the clang tool) excluding the functions from headers and other third party libraries (if any). </div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jun 4, 2016 at 7:26 PM, Jonathan Roelofs <span dir="ltr"><<a href="mailto:jonathan@codesourcery.com" target="_blank">jonathan@codesourcery.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
<br>
On 6/4/16 6:38 AM, Dhriti Khanna via cfe-dev wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Here is my sample code:<br>
<br>
int x=0;<br>
if(x == 0)<br>
cout << "Hey" << endl;<br>
<br>
I want to get 'x'.<br>
</blockquote>
<br></span>
getConditionVariable() will only return x if it is written this way:<br>
<br>
if (int x = ... )<br>
...<br>
<br>
You need to do something more like this:<span class=""><br>
<br>
if (IfStmt *ifS = dyn_cast<IfStmt>(st))<br>
{<br></span>
if (BinaryOperator *binOp = dyn_cast<BinaryOperator>(ifS->getCond()))<br>
{<br>
if (binOp->getOpcode() == BO_Assign)<br>
{<br>
if (DeclRefExpr *Decl = dyn_cast<DeclRefExpr*>(binOp->getLHS()->IgnoreParenCasts()))<br>
{<br>
...<br>
}<br>
}<br>
}<br>
}<br>
<br>
It helps a *lot* to look at the AST dump for the examples you're interested in matching. To do that, run:<br>
<br>
$ ./bin/clang-check -ast-dump foo.c --<br>
<br>
If you're going to be doing heavy matching of ASTs, I recommend looking into using this: <a href="http://clang.llvm.org/docs/LibASTMatchersReference.html" rel="noreferrer" target="_blank">http://clang.llvm.org/docs/LibASTMatchersReference.html</a> instead of open-coding them.<br>
<br>
<br>
Jon<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
<br>
On Sat, Jun 4, 2016 at 6:06 PM, Miklos Vajna via cfe-dev<br></span><span class="">
<<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a> <mailto:<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>>> wrote:<br>
<br>
On Sat, Jun 04, 2016 at 05:32:37PM +0530, Dhriti Khanna via cfe-dev<br></span><span class="">
<<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a> <mailto:<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>>> wrote:<br>
> This code:<br>
><br>
> if (IfStmt* ifS = dyn_cast<IfStmt>(st)) {<br>
> errs() << "Found an IF statement ";<br>
> VarDecl* var = ifS->getConditionVariable();<br>
> errs() << var->getNameAsString();<br>
> }<br>
><br>
> produces cannot initialize object parameter of type 'const<br>
> clang::NamedDecl' with an expression of type 'clang::VarDecl' error on errs()<br>
> << var->getNameAsString(); line and the program crashes with a seg fault. I<br>
> can't find what's wrong with this? Please help.<br>
<br>
Please post some sample source code (from which the AST is generated),<br>
without that it's quite hard to help. But in general I guess you can't<br>
assume that you can get a single variable out of an if statement, if you<br>
have "if (Foo && Bar)", which one would be returned by the API?<br>
<br>
_______________________________________________<br>
cfe-dev mailing list<br></span>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a> <mailto:<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>><span class=""><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
<br>
<br>
<br>
<br>
--<br>
Regards,<br>
Dhriti Khanna<br>
PhD Scholar<br>
IIIT Delhi<br>
<br>
<br>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
<br>
</span></blockquote><span class="HOEnZb"><font color="#888888">
<br>
-- <br>
Jon Roelofs<br>
<a href="mailto:jonathan@codesourcery.com" target="_blank">jonathan@codesourcery.com</a><br>
CodeSourcery / Mentor Embedded<br>
</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><font color="#f1c232">Regards,</font><div><font color="#f1c232">Dhriti Khanna</font></div><div><font color="#f1c232">PhD Scholar</font></div><div><font color="#f1c232">IIIT Delhi</font></div></div></div></div></div>
</div>