Commit 66214243 authored by Paul Reimer's avatar Paul Reimer Committed by Wouter van Oortmerssen

Add --root-type option to flatc [C++, parser, JSON] (#4728)

* Add --root-type option to flatc

To select or override a root_type

* Add help text from flatc --root-type to Compiler.md doc
parent d215852f
...@@ -128,5 +128,7 @@ Additional options: ...@@ -128,5 +128,7 @@ Additional options:
- `--reflect-types` : Add minimal type reflection to code generation. - `--reflect-types` : Add minimal type reflection to code generation.
- `--reflect-names` : Add minimal type/name reflection. - `--reflect-names` : Add minimal type/name reflection.
- `--root-type T` : Select or override the default root_type.
NOTE: short-form options for generators are deprecated, use the long form NOTE: short-form options for generators are deprecated, use the long form
whenever possible. whenever possible.
...@@ -390,6 +390,7 @@ struct IDLOptions { ...@@ -390,6 +390,7 @@ struct IDLOptions {
bool reexport_ts_modules; bool reexport_ts_modules;
bool protobuf_ascii_alike; bool protobuf_ascii_alike;
bool size_prefixed; bool size_prefixed;
std::string root_type;
// Possible options for the more general generator below. // Possible options for the more general generator below.
enum Language { enum Language {
......
...@@ -120,6 +120,7 @@ std::string FlatCompiler::GetUsageString(const char *program_name) const { ...@@ -120,6 +120,7 @@ std::string FlatCompiler::GetUsageString(const char *program_name) const {
" --no-ts-reexport Don't re-export imported dependencies for TypeScript.\n" " --no-ts-reexport Don't re-export imported dependencies for TypeScript.\n"
" --reflect-types Add minimal type reflection to code generation.\n" " --reflect-types Add minimal type reflection to code generation.\n"
" --reflect-names Add minimal type/name reflection.\n" " --reflect-names Add minimal type/name reflection.\n"
" --root-type T Select or override the default root_type\n"
"FILEs may be schemas (must end in .fbs), or JSON files (conforming to preceding\n" "FILEs may be schemas (must end in .fbs), or JSON files (conforming to preceding\n"
"schema). FILEs after the -- must be binary flatbuffer format files.\n" "schema). FILEs after the -- must be binary flatbuffer format files.\n"
"Output files are named using the base file name of the input,\n" "Output files are named using the base file name of the input,\n"
...@@ -268,6 +269,9 @@ int FlatCompiler::Compile(int argc, const char **argv) { ...@@ -268,6 +269,9 @@ int FlatCompiler::Compile(int argc, const char **argv) {
opts.mini_reflect = IDLOptions::kTypes; opts.mini_reflect = IDLOptions::kTypes;
} else if (arg == "--reflect-names") { } else if (arg == "--reflect-names") {
opts.mini_reflect = IDLOptions::kTypesAndNames; opts.mini_reflect = IDLOptions::kTypesAndNames;
} else if (arg == "--root-type") {
if (++argi >= argc) Error("missing type following" + arg, true);
opts.root_type = argv[argi];
} else { } else {
for (size_t i = 0; i < params_.num_generators; ++i) { for (size_t i = 0; i < params_.num_generators; ++i) {
if (arg == params_.generators[i].generator_opt_long || if (arg == params_.generators[i].generator_opt_long ||
...@@ -407,6 +411,13 @@ int FlatCompiler::Compile(int argc, const char **argv) { ...@@ -407,6 +411,13 @@ int FlatCompiler::Compile(int argc, const char **argv) {
} }
} }
if (!opts.root_type.empty()) {
if (!parser->SetRootType(opts.root_type.c_str()))
Error("unknown root type: " + opts.root_type);
else if (parser->root_struct_def_->fixed)
Error("root type must be a table");
}
if (opts.proto_mode) GenerateFBS(*parser.get(), output_path, filebase); if (opts.proto_mode) GenerateFBS(*parser.get(), output_path, filebase);
// We do not want to generate code for the definitions in this file // We do not want to generate code for the definitions in this file
......
...@@ -2432,9 +2432,12 @@ CheckedError Parser::DoParse(const char *source, const char **include_paths, ...@@ -2432,9 +2432,12 @@ CheckedError Parser::DoParse(const char *source, const char **include_paths,
auto root_type = attribute_; auto root_type = attribute_;
EXPECT(kTokenIdentifier); EXPECT(kTokenIdentifier);
ECHECK(ParseNamespacing(&root_type, nullptr)); ECHECK(ParseNamespacing(&root_type, nullptr));
if (!SetRootType(root_type.c_str())) if (opts.root_type.empty()) {
return Error("unknown root type: " + root_type); if (!SetRootType(root_type.c_str()))
if (root_struct_def_->fixed) return Error("root type must be a table"); return Error("unknown root type: " + root_type);
if (root_struct_def_->fixed)
return Error("root type must be a table");
}
EXPECT(';'); EXPECT(';');
} else if (IsIdent("file_identifier")) { } else if (IsIdent("file_identifier")) {
NEXT(); NEXT();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment