1 year ago

#317156

test-img

Mah35h

clang DeclContext::getParent() not returning parent RecordDecl for nested structs

I'm using the following code get parent struct of a nested struct using clang libtooling.

bool VisitRecordDecl(clang::RecordDecl *RD) {
    llvm::outs() <<"\n\tTrying to get parents of "<<RD->getNameAsString()<<"\n\n";
    clang::RecordDecl *Parent = dyn_cast<clang::RecordDecl>(RD->getParent()); 
    if(!Parent) {
      llvm::outs()<<"NULL RecordDecl with dyn_cast\n";
      clang::DeclContext *DParent = RD->getParent(); // trying to find what did getParent return
      llvm::outs()<<DParent->getDeclKindName()<<"\n"; // prints TransaltionUnit
      DParent->dumpDeclContext(); // prints whole source file
    }
    else{
      Parent->dump();
    }
    return true;
}

This is the source file in which I run the code

struct outer{
        struct inner{
                int ia;
                int ib;
                struct deep{
                        int da;
                }dobj;
        }iobj;
        int oa;
}oboj;
int main(){
        return 0;
}

For each invocation of VisitRecordDecl, getParent returns a TranslationUnit which cannot be casted into RecordDecl to get parent struct of inner or deep struct. getLexicalParent() returns RecordDecl correctly but why is getParent() not working?

clang

llvm

libtooling

0 Answers

Your Answer

Accepted video resources