c++ - How to get the base class from this example with Clang ? -
here basic sample of code, , have :
class b{ // implementation of class b }; class d : public b{ // implementation of class d }; int main(){ try{ // code try statement } catch(d & d){ // handler d } catch(b & b){ // handler b } return 0; }
currently able cxxrecorddecl of class b , class d, in handlers (i can them getcaughttype
method in cxxcatchstmt
class).
what able access cxxrecorddecl
of class b class d, since have class d : public b
.
i have tried following methods available in class cxxrecorddecl
on cxxrecorddecl
of class d
:
- getcanonicaldecl() : returns class d
- getinstantiatedfrommemberclass() : returns nullptr
- getdefinition() : returns class d
i'm out of ideas right now. have idea ?
here implementation of answer given joachim pileborg in comments.
bool visitcxxtrystmt(cxxtrystmt * trystmt){ int nbcatch = trystmt->getnumhandlers(); for(int = 0 ; < nbcatch ; i++){ if(trystmt->gethandler(i)->getcaughttype().gettypeptr()->getpointeecxxrecorddecl() == nullptr){ cout << "the caught type not class" << endl; } else{ cout << "class caught : " << trystmt->gethandler(i)->getcaughttype().gettypeptr()->getpointeecxxrecorddecl()->getnameasstring() << endl; } if(trystmt->gethandler(i)->getcaughttype().gettypeptr()->getpointeecxxrecorddecl()->bases_begin() == nullptr){ cout << "this class base class" << endl; } else{ cout << "base class caught : " << trystmt->gethandler(i)->getcaughttype().gettypeptr()->getpointeecxxrecorddecl()->bases_begin()->gettype().getasstring() << endl; } cout << "\n \n end of loop \n \n" << endl; } return true; }
yields following output example given in question :
class caught : d
base class caught : class b
end of loop
class caught : b
this class base class
end of loop
Comments
Post a Comment