Clang ASTContext.getParents always returns an empty list

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?

+4
source share
1

:

const Stmt  currentStmt = *s;

. Context->getParents() , , . currentStmt , , node.

currentStmt node, s:

const Stmt& currentStmt = *s;

node, , clang .

+3

Source: https://habr.com/ru/post/1662308/


All Articles