From 641fbe46583c04c56bf33284ceb7971102ea4583 Mon Sep 17 00:00:00 2001 From: Derek Bailey Date: Sun, 8 Jan 2023 13:29:00 -0800 Subject: Refactor FlatC to receive `FlatCOptions` (#7770) * Refactor FlatC to receive `FlatCOptions` * switch to c++11 unique_ptr --- include/flatbuffers/flatc.h | 45 ++++++++++++++++++++++++++++++++++++++++----- include/flatbuffers/idl.h | 11 +++++++++-- 2 files changed, 49 insertions(+), 7 deletions(-) (limited to 'include') diff --git a/include/flatbuffers/flatc.h b/include/flatbuffers/flatc.h index af4ccae7..23eefaa0 100644 --- a/include/flatbuffers/flatc.h +++ b/include/flatbuffers/flatc.h @@ -19,6 +19,7 @@ #include #include +#include #include #include "flatbuffers/bfbs_generator.h" @@ -31,6 +32,30 @@ namespace flatbuffers { extern void LogCompilerWarn(const std::string &warn); extern void LogCompilerError(const std::string &err); +struct FlatCOptions { + IDLOptions opts; + + std::string program_name; + + std::string output_path; + + std::vector filenames; + + std::list include_directories_storage; + std::vector include_directories; + std::vector conform_include_directories; + std::vector generator_enabled; + size_t binary_files_from = std::numeric_limits::max(); + std::string conform_to_schema; + std::string annotate_schema; + bool any_generator = false; + bool print_make_rules = false; + bool raw_binary = false; + bool schema_binary = false; + bool grpc_enabled = false; + bool requires_bfbs = false; +}; + struct FlatCOption { std::string short_opt; std::string long_opt; @@ -85,15 +110,18 @@ class FlatCompiler { explicit FlatCompiler(const InitParams ¶ms) : params_(params) {} - int Compile(int argc, const char **argv); + int Compile(const FlatCOptions &options); + + std::string GetShortUsageString(const std::string& program_name) const; + std::string GetUsageString(const std::string& program_name) const; - std::string GetShortUsageString(const char *program_name) const; - std::string GetUsageString(const char *program_name) const; + // Parse the FlatC options from command line arguments. + FlatCOptions ParseFromCommandLineArguments(int argc, const char **argv); private: void ParseFile(flatbuffers::Parser &parser, const std::string &filename, const std::string &contents, - std::vector &include_directories) const; + const std::vector &include_directories) const; void LoadBinarySchema(Parser &parser, const std::string &filename, const std::string &contents); @@ -105,9 +133,16 @@ class FlatCompiler { void AnnotateBinaries(const uint8_t *binary_schema, uint64_t binary_schema_size, - const std::string & schema_filename, + const std::string &schema_filename, const std::vector &binary_files); + void ValidateOptions(const FlatCOptions &options); + + Parser GetConformParser(const FlatCOptions &options); + + std::unique_ptr GenerateCode(const FlatCOptions &options, + Parser &conform_parser); + InitParams params_; }; diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index 9e5bb25e..f6526bd5 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -298,7 +298,7 @@ struct FieldDef : public Definition { presence(kDefault), nested_flatbuffer(nullptr), padding(0), - sibling_union_field(nullptr){} + sibling_union_field(nullptr) {} Offset Serialize(FlatBufferBuilder *builder, uint16_t id, const Parser &parser) const; @@ -803,7 +803,7 @@ struct ParserState { FLATBUFFERS_ASSERT(cursor_ && line_start_ && cursor_ >= line_start_); return static_cast(cursor_ - line_start_); } - + const char *prev_cursor_; const char *cursor_; const char *line_start_; @@ -910,6 +910,13 @@ class Parser : public ParserState { known_attributes_["private"] = true; } + // Copying is not allowed + Parser(const Parser &) = delete; + Parser &operator=(const Parser &) = delete; + + Parser(Parser &&) = default; + Parser &operator=(Parser &&) = default; + ~Parser() { for (auto it = namespaces_.begin(); it != namespaces_.end(); ++it) { delete *it; -- cgit v1.2.3