how to get label from a statement and VisitLabelExpr

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

how to get label from a statement and VisitLabelExpr

Rajendra
How to get label of a LabelStmt?

e.g. from code below, I want to get label MYASSERT1 and MYASSERT2, how to do that?

int main()
{
  int x, y;
  x = 10;
  MYASSERT1: x>0;
  y = 0;
  MYASSERT2: x==0;
  return 0;
}

I have written VisitLabelStmt(Stmt*) but control doesn't come to this function. Also in VisitStmt(Stmt*) when I check stmt class as "LabelStmt", I dont't get label. Any help?
Reply | Threaded
Open this post in threaded view
|

Re: how to get label from a statement and VisitLabelExpr

Gabor Kozar-2
Hi,

That seems to be correct, I'm not sure what could be the issue. Could you show us some code?

(I think that VisitLabelStmt's parameter type is LabelStmt*, but I do not think that could be the issue here.)

The only thing I can imagine is that you abort the tree traversal in one of your override methods. To quote the documentation of RecursiveASTVisitor:

> "Advanced users may override Traverse* and WalkUpFrom* to implement custom traversal strategies. Returning false from one of these overridden functions will abort the entire traversal."

Try commenting out your entire visitor, and replacing it with a trivial one that ONLY overrides VisitLabelStmt, and returns true - I don't remember what the effect of returning false in a Visit* method is, but my guess would be that it causes any derived-type Visit* methods not to be called (i.e. if for a LabelStmt*, you return false from VisitStmt, then VisitLabelStmt won't be called) - but again, that's just my guess.

Hope this helps.

Gabor


2013/5/14 Rajendra <[hidden email]>
How to get label of a LabelStmt?

e.g. from code below, I want to get label MYASSERT1 and MYASSERT2, how to do
that?

int main()
{
  int x, y;
  x = 10;
  MYASSERT1: x>0;
  y = 0;
  MYASSERT2: x==0;
  return 0;
}

I have written VisitLabelStmt(Stmt*) but control doesn't come to this
function. Also in VisitStmt(Stmt*) when I check stmt class as "LabelStmt", I
dont't get label. Any help?



--
View this message in context: http://clang-developers.42468.n3.nabble.com/how-to-get-label-from-a-statement-and-VisitLabelExpr-tp4032103.html
Sent from the Clang Developers mailing list archive at Nabble.com.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: how to get label from a statement and VisitLabelExpr

Rajendra
Here below is my code of VisitStmt() and VisitLabelStmt(), I know when to return true//false from visitor method and I have tried VisitLabelStmt(LabelStmt* s) and VisitLabelDecl(LabelDecl* d) too. No luck.

bool MyProcessStmtBox::VisitStmt(Stmt *s)
{
  clang::SourceManager & sm = this->m_compilerInstance->getSourceManager();
  clang::SourceLocation locStart = s->getLocStart();

  std::cerr << "\n found stmt \n";
  std::cerr << "\t" << stmtToStr(s) << " at ";
  locStart.dump(sm);
  std::cerr << "\n";
  std::cerr << "StmtClassName = " << s->getStmtClassName();

  if (strcmp(s->getStmtClassName(), "LabelStmt") == 0)
  {
      std::cerr << "\n found LabelStmt\n";
      s->dump();
  }
  // Returning false from one of the overridden visitor functions
  // will abort the entire traversal.
  return true;
}

bool MyProcessStmtBox::VisitLabelStmt(Stmt* s)
{
  std::cerr << "\tLabelStmt: \n";
  s->dump(); // TODO: remove

  return false;
}
Reply | Threaded
Open this post in threaded view
|

Re: how to get label from a statement and VisitLabelExpr

Gabor Kozar-2

2013/5/14 Rajendra <[hidden email]>
if (strcmp(s->getStmtClassName(), "LabelStmt") == 0)

Ough, don't do this. Use llvm::isa<> and llvm::dyn_cast<>.

Another idea: I think you're supposed to call the base class' VisitStmt method after you're done with your custom logic. Try doing that instead of returning with just true.

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: how to get label from a statement and VisitLabelExpr

Richard Smith
In reply to this post by Rajendra
On Tue, May 14, 2013 at 2:04 AM, Rajendra <[hidden email]> wrote:
Here below is my code of VisitStmt() and VisitLabelStmt(), I know when to
return true//false from visitor method and I have tried
VisitLabelStmt(LabelStmt* s) and VisitLabelDecl(LabelDecl* d) too. No luck.

bool MyProcessStmtBox::VisitStmt(Stmt *s)
{
  clang::SourceManager & sm = this->m_compilerInstance->getSourceManager();
  clang::SourceLocation locStart = s->getLocStart();

  std::cerr << "\n found stmt \n";
  std::cerr << "\t" << stmtToStr(s) << " at ";
  locStart.dump(sm);
  std::cerr << "\n";
  std::cerr << "StmtClassName = " << s->getStmtClassName();

  if (strcmp(s->getStmtClassName(), "LabelStmt") == 0)
  {
      std::cerr << "\n found LabelStmt\n";
      s->dump();
  }
  // Returning false from one of the overridden visitor functions
  // will abort the entire traversal.
  return true;
}

bool MyProcessStmtBox::VisitLabelStmt(Stmt* s)
{
  std::cerr << "\tLabelStmt: \n";
  s->dump(); // TODO: remove

  return false;
}

We need more context than this. Are you deriving from StmtVisitor? RecursiveASTVisitor? What code are you using to visit the body of the function? 

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev