[cfe-dev] Extracting variable name from VarDecl* Clang

Dhriti Khanna via cfe-dev cfe-dev at lists.llvm.org
Sat Jun 4 05:38:59 PDT 2016


Here is my sample code:

int x=0;
if(x == 0)
cout << "Hey" << endl;

I want to get 'x'.

On Sat, Jun 4, 2016 at 6:06 PM, Miklos Vajna via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> On Sat, Jun 04, 2016 at 05:32:37PM +0530, Dhriti Khanna via cfe-dev <
> cfe-dev at lists.llvm.org> wrote:
> > This code:
> >
> >  if (IfStmt* ifS = dyn_cast<IfStmt>(st)) {
> >    errs() << "Found an IF statement ";
> >    VarDecl* var = ifS->getConditionVariable();
> >    errs() << var->getNameAsString();
> >  }
> >
> > produces cannot initialize object parameter of type 'const
> > clang::NamedDecl' with an expression of type 'clang::VarDecl' error on
> errs()
> > << var->getNameAsString(); line and the program crashes with a seg
> fault. I
> > can't find what's wrong with this? Please help.
>
> Please post some sample source code (from which the AST is generated),
> without that it's quite hard to help. But in general I guess you can't
> assume that you can get a single variable out of an if statement, if you
> have "if (Foo && Bar)", which one would be returned by the API?
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
>


-- 
Regards,
Dhriti Khanna
PhD Scholar
IIIT Delhi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160604/98ffebd6/attachment.html>


More information about the cfe-dev mailing list