如何从这个例子中使用Clang获取基类? [英] How to get the base class from this example with Clang ?

查看:187
本文介绍了如何从这个例子中使用Clang获取基类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常基本的代码示例,我想要的是:

  class B {
// B类的实现
};
class D:public B {
// D类的实现
};
int main(){
try {
// try语句的代码
}
catch(D& d){
// Handler for D
}
catch(B& b){
// B的处理程序b
}
return 0;
}

目前我可以获得 CXXRecordDecl B类和D类,在处理程序(我可以从 getCaughtType 方法在 CXXCatchStmt 类)。



我想做的是能够从D类访问B类的 CXXRecordDecl D类:public B



我已尝试在类CXXRecordDecl 中使用以下方法在 CXXRecordDecl class D





我现在不在想法。有人有想法吗?

解决方案

这里是 Joachim Pileborg 在评论中给出的答案的实现。

  bool VisitCXXTryStmt(CXXTryStmt * tryStmt){
int nbCatch = tryStmt-> getNumHandlers();
for(int i = 0; i if(tryStmt-> getHandler(i) - > getCaughtType()。getTypePtr() - > getPointeeCXXRecordDecl = nullptr){
cout<< 捕获类型不是类< endl;
}
else {
cout<< 捕获的类:< tryStmt-> getHandler(i) - > getCaughtType()。getTypePtr() - > getPointeeCXXRecordDecl() - > getNameAsString()< endl;
}
if(tryStmt-> getHandler(i) - > getCaughtType()。getTypePtr() - > getPointeeCXXRecordDecl() - > bases_begin()== nullptr){
cout < 这个类是基类<< endl;
}
else {
cout<< Base class caught:< tryStmt-> getHandler(i) - > getCaughtType()。getTypePtr() - > getPointeeCXXRecordDecl() - > bases_begin() - > getType()。getAsString()< endl;
}
cout<< \\\
\\\
END OF LOOP \\\
\\\
< endl;
}
return true;
}

针对问题中给出的示例生成以下输出:



捕获的类别:D





结束



>



此类是基类


Here is a very basic sample of code, and what I would like to have :

class B{
    // Implementation of class B
};
class D : public B{
    // Implementation of class D
};
int main(){
    try{
        // Code for try statement
    }
    catch(D & d){
        // Handler for D 
    } 
    catch(B & b){
        // Handler for B 
    } 
    return 0; 
} 

Currently I am able to get the CXXRecordDecl of class B and class D, in handlers (I can get them from the getCaughtType method in CXXCatchStmt class).

What I would like to do is to be able to access CXXRecordDecl of class B from class D, since we have class D : public B.

I have tried the following methods available in class CXXRecordDecl on my CXXRecordDecl of class D:

I'm out of ideas right now. Does someone have an idea ?

解决方案

Here is the implementation of the answer given by Joachim Pileborg in comments.

bool VisitCXXTryStmt(CXXTryStmt * tryStmt){
    int nbCatch = tryStmt->getNumHandlers(); 
    for(int i = 0 ; i < nbCatch ; i++){
        if(tryStmt->getHandler(i)->getCaughtType().getTypePtr()->getPointeeCXXRecordDecl() == nullptr){
            cout << "The caught type is not a 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 is the 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 the following output for the example given in the question :

Class caught : D

Base class caught : class B

END OF LOOP

Class caught : B

This class is the base class

END OF LOOP

这篇关于如何从这个例子中使用Clang获取基类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆