google / flatbuffers

FlatBuffers: Memory Efficient Serialization Library
https://flatbuffers.dev/
Apache License 2.0
22.52k stars 3.19k forks source link

object.declaration_file is empty when use Parser to load .fbs schema file. #8342

Open jerrrykong opened 1 week ago

jerrrykong commented 1 week ago

flatbuffers version is 24.3.25 gcc: 7.3.1

code :

int Load(const char* filename)
{
    std::string str;
    if(!flatbuffers::LoadFile(filename, false, &str))
    {
        fprintf(stderr,  "load file error: %s \n", filename);
        return -1;
    }

    const char* p = strrchr(filename, '/');

    std::string path = p ?  std::string(filename, p - filename) : std::string(".");

    const char* includes[] = { path.c_str(),  nullptr};

    flatbuffers::IDLOptions options;
    //options.lang_to_generate = flatbuffers::IDLOptions::kCpp;

    flatbuffers::Parser parser(options);
    if(!parser.Parse(str.c_str(), includes, filename))
    {
        fprintf(stderr,  "parse error: \n%s\n", parser.error_.c_str());
        return -1;
    }

    parser.Serialize();

    auto schema = reflection::GetSchema(parser.builder_.GetBufferPointer());
    printf("\nList Objects: \n");
    auto objects = schema->objects();
    for(uint32_t i = 0; i < objects->size(); i++) {
        auto object= objects->Get(i);
        printf(" [%u] %s: %s, file: %s\n", i, object->is_struct() ? "struct" : "table", 
                object->name()->c_str(), object->declaration_file()->c_str());
    }

    // Enums
    printf("\nList Enums: \n");
    auto enums = schema->enums();
    for (uint32_t i = 0; i < enums->size(); i++) {
        auto eu = enums->Get(i);
        printf(" [%u] %s %s: %s, file: %s\n", i, eu->is_union() ? "Union" : "Enum",  
                eu->name()->c_str(), reflection::EnumNameBaseType(eu->underlying_type()->base_type()), 
                eu->declaration_file()->c_str());
    }
    return 0;
}

So, what is the correct way to determine in which schema file an object is defined?

jerrrykong commented 1 week ago

Output is like: List Objects: [0] struct: res.Area, file: [1] table: res.ResCard, file: [2] table: res.ResCardTable, file: [3] struct: res.WeaponSet, file:

List Enums: [0] Enum res.ArmorType: Byte, file: [1] Enum res.BuildingType: Byte, file: [2] Enum res.CardQuality: Byte, file: [3] Enum res.UnitType: Byte, file: