I am writing a tool for clang 4.0 using LibTooling using the Clang ASTFrontendActions example . Given the current stmt statement, I want to get the immediate parent in AST. So I will try the code below to reset all stmt parents (for testing purposes):
bool VisitStmt(Stmt *s) {
cout <<"Trying to get parents \n";
const Stmt currentStmt = *s;
const auto& parents = Context->getParents(currentStmt);
auto it = Context->getParents(currentStmt).begin();
if(it == Context->getParents(currentStmt).end())
cout<< "parents not found\n";
cout<<"parents size "<< parents.size() <<": \n";
if (!parents.empty()){
for (int i = 0; i< parents.size(); i++ ){
cout<<"parent at "<< i <<": \n";
const Stmt* parentStmt = parents[i].get<Stmt>();
parentStmt->dump();
}
}
}
Contextis ASTContextand works fine when I use its other functions, such as: Context->getSourceManager()
For all protrusions, the result is always (no matter what I put in the input):
Trying to get parents
parents not found
parents size 0:
Skip anything (initialization, setup) to use getParents?
source
share