How to get label of a LabelStmt?
e.g. from code below, I want to get label MYASSERT1 and MYASSERT2, how to do
int x, y;
x = 10;
y = 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?
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.
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 ";
std::cerr << "\n";
std::cerr << "StmtClassName = " << s->getStmtClassName();
if (strcmp(s->getStmtClassName(), "LabelStmt") == 0)
std::cerr << "\n found LabelStmt\n";
// Returning false from one of the overridden visitor functions
// will abort the entire traversal.
bool MyProcessStmtBox::VisitLabelStmt(Stmt* s)
std::cerr << "\tLabelStmt: \n";
s->dump(); // TODO: remove
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.
We need more context than this. Are you deriving from StmtVisitor? RecursiveASTVisitor? What code are you using to visit the body of the function?