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(qtype.getTypePtr());
            DisplayerVisitArrayType(atype, displayer, current_node);
        }
        else if(qtype->isPointerType()){
            auto ptype=dyn_cast_or_null(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 edge = std::make_unique(
                    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 edge = std::make_unique(
                    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 edge = std::make_unique(
                        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());
    }

你可能感兴趣的:(clang递归遍历recorddecl)