Why protobuf's `source_code_info().location_size()` is zero?

Viewed 39

I wrote a tool to parse .proto file. While I use file_proto.source_code_info().location_size() to get the num, I always got 0. I print the file_proto's ShortDebugString() and it shows actually there are contents!

So what's wrong with the location_size(), or something I misunderstand?

The source code just like this:

bool PbConst::CheckFileAndGetDeclarationCode(const pb::FileDescriptor *file_desc, std::string &code_block) const {
    int ret = ErrorCode::PROCESS_SUCCESS;
    pb::FileDescriptorProto file_proto;
    file_desc->CopySourceCodeInfoTo(&file_proto);
    const pb::SourceCodeInfo *src_info = &file_proto.source_code_info();

    std::cout << file_proto.ShortDebugString() << std::endl;
    std::cout << "location_size: " << file_proto.source_code_info().location_size() << std::endl;

    for (int i = 0; i < src_info->location_size(); ++i) {
        const auto *loc = &src_info->location(i);
        if ((loc->path_size() == 1 && loc->path(0) == 2) ||  // import
            (loc->path_size() == 2 && loc->path(0) == 3)) {  // package
            if (!loc->leading_comments().empty()) {
                std::string src = loc->leading_comments();
                std::string attachment;
                ret = handle_common_cmd("@pbc_declare", src, attachment);
                COND_EXP(ret == ErrorCode::PROCESS_SUCCESS, std::swap(code_block, src); return true);
            }
        }
    }
    return false;
}

My protobuf's version is 3.7.0. And the code in my project is https://github.com/Modnars/ModTools/blob/master/pbc/src/pb_const.cc

0 Answers
Related