Improve import handling for proto conversion (#5673)

* Keep include prefix when converting from proto.

This change preserves the include prefix when generating flatbuffers
from proto (with FBS_GEN_INCLUDES) defined.

* Improve handling of imports in proto conversion.

Previously, there was no runtime flag to make proto->fbs conversion keep
the import structure of a collection of files. This change makes proto
conversion respect the --no-gen-includes flag and skip the output of
"generated" symbols.
This commit is contained in:
Michael Beardsworth
2019-12-23 08:50:29 -08:00
committed by Wouter van Oortmerssen
parent ce3a1c43a2
commit 13c05f4da3
5 changed files with 195 additions and 4 deletions

View File

@@ -69,19 +69,22 @@ std::string GenerateFBS(const Parser &parser, const std::string &file_name) {
schema += "// Generated from " + file_name + ".proto\n\n";
if (parser.opts.include_dependence_headers) {
// clang-format off
#ifdef FBS_GEN_INCLUDES // TODO: currently all in one file.
int num_includes = 0;
for (auto it = parser.included_files_.begin();
it != parser.included_files_.end(); ++it) {
if (it->second.empty())
continue;
auto basename = flatbuffers::StripPath(
flatbuffers::StripExtension(it->second));
std::string basename;
if(parser.opts.keep_include_path) {
basename = flatbuffers::StripPath(
flatbuffers::StripExtension(it->second));
} else {
basename = flatbuffers::StripExtension(it->second);
}
schema += "include \"" + basename + ".fbs\";\n";
num_includes++;
}
if (num_includes) schema += "\n";
#endif
// clang-format on
}
// Generate code for all the enum declarations.
@@ -89,6 +92,7 @@ std::string GenerateFBS(const Parser &parser, const std::string &file_name) {
for (auto enum_def_it = parser.enums_.vec.begin();
enum_def_it != parser.enums_.vec.end(); ++enum_def_it) {
EnumDef &enum_def = **enum_def_it;
if (parser.opts.include_dependence_headers && enum_def.generated) { continue; }
GenNameSpace(*enum_def.defined_namespace, &schema, &last_namespace);
GenComment(enum_def.doc_comment, &schema, nullptr);
if (enum_def.is_union)
@@ -110,6 +114,7 @@ std::string GenerateFBS(const Parser &parser, const std::string &file_name) {
for (auto it = parser.structs_.vec.begin(); it != parser.structs_.vec.end();
++it) {
StructDef &struct_def = **it;
if (parser.opts.include_dependence_headers && struct_def.generated) { continue; }
GenNameSpace(*struct_def.defined_namespace, &schema, &last_namespace);
GenComment(struct_def.doc_comment, &schema, nullptr);
schema += "table " + struct_def.name + " {\n";