clang递归遍历recorddecl

    void VisitUnknownType(QualType qtype, Displayer* displayer,
                          Displayer::Node* current_node){
        if(qtype->isPointerType()){
        }
        else if(qtype->isStructureType()){
            const RecordType* rtype = qtype->getAsStructureType();
            DisplayerVisitRecordType(rtype, displayer, current_node);
        }
        else if(qtype->isArrayType()){
            auto atype = dyn_cast_or_null<ArrayType>(qtype.getTypePtr());
            DisplayerVisitArrayType(atype, displayer, current_node);
        }
        else if(qtype->isPointerType()){
            auto ptype=dyn_cast_or_null<PointerType>(qtype.getTypePtr());
            DisplayerVisitPointerType(ptype,displayer, current_node);
        }
        else{
            qtype->dump();
        }
    }

    void DisplayerVisitPointerType(const PointerType* ptype, Displayer* displayer, Displayer::Node* current_node) {
        QualType qualType = ptype->getPointeeType();//child type
        Displayer::Node* child_node=new Displayer::Node;
        child_node->qualType=qualType;

        {
            std::unique_ptr<Displayer::Edge> edge = std::make_unique<Displayer::Edge>(
                    child_node, Displayer::EdgeType::PointEdge);
            current_node->edges.push_back(std::move(edge));
        }

        VisitUnknownType(qualType, displayer, child_node);
    }

    void DisplayerVisitArrayType(const ArrayType* atype, Displayer* displayer, Displayer::Node* current_node) {
        QualType qualType = atype->getElementType();//child type
        Displayer::Node* child_node=new Displayer::Node;
        child_node->qualType=qualType;

        {
            std::unique_ptr<Displayer::Edge> edge = std::make_unique<Displayer::Edge>(
                    child_node, Displayer::EdgeType::ArrayEdge, fieldDecl->getNameAsString());
            current_node->edges.push_back(std::move(edge));
        }

        VisitUnknownType(qualType, displayer, child_node);
    }

    void DisplayerVisitRecordType(const RecordType* rtype, Displayer* displayer, Displayer::Node* current_node){
        RecordDecl* recordDecl=rtype->getDecl();
        for(auto fieldDecl : recordDecl->fields()){
            QualType qualType = fieldDecl->getType();//child type
            Displayer::Node* child_node=new Displayer::Node;
            child_node->qualType=qualType;
            {
                std::unique_ptr<Displayer::Edge> edge = std::make_unique<Displayer::Edge>(
                        child_node, Displayer::EdgeType::StructEdge, fieldDecl->getNameAsString());
                current_node->edges.push_back(std::move(edge));
            }

            VisitUnknownType(qualType, displayer, child_node);
        }
    }

    void DisplayerStartDrawStruct(const RecordDecl* RD, Displayer* displayer){
        const RecordType* recordType = RD->getTypeForDecl()->getAsStructureType();
        DisplayerVisitRecordType(recordType, displayer, displayer->GetRootNode());
    }

推荐阅读更多精彩内容