Skip to content

Commit

Permalink
Refactor FlatC to receive FlatCOptions (#7770)
Browse files Browse the repository at this point in the history
* Refactor FlatC to receive `FlatCOptions`

* switch to c++11 unique_ptr
  • Loading branch information
dbaileychess authored Jan 8, 2023
1 parent 5638a6a commit 641fbe4
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 147 deletions.
45 changes: 40 additions & 5 deletions include/flatbuffers/flatc.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <functional>
#include <limits>
#include <list>
#include <string>

#include "flatbuffers/bfbs_generator.h"
Expand All @@ -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<std::string> filenames;

std::list<std::string> include_directories_storage;
std::vector<const char *> include_directories;
std::vector<const char *> conform_include_directories;
std::vector<bool> generator_enabled;
size_t binary_files_from = std::numeric_limits<size_t>::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;
Expand Down Expand Up @@ -85,15 +110,18 @@ class FlatCompiler {

explicit FlatCompiler(const InitParams &params) : 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<const char *> &include_directories) const;
const std::vector<const char *> &include_directories) const;

void LoadBinarySchema(Parser &parser, const std::string &filename,
const std::string &contents);
Expand All @@ -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<std::string> &binary_files);

void ValidateOptions(const FlatCOptions &options);

Parser GetConformParser(const FlatCOptions &options);

std::unique_ptr<Parser> GenerateCode(const FlatCOptions &options,
Parser &conform_parser);

InitParams params_;
};

Expand Down
11 changes: 9 additions & 2 deletions include/flatbuffers/idl.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ struct FieldDef : public Definition {
presence(kDefault),
nested_flatbuffer(nullptr),
padding(0),
sibling_union_field(nullptr){}
sibling_union_field(nullptr) {}

Offset<reflection::Field> Serialize(FlatBufferBuilder *builder, uint16_t id,
const Parser &parser) const;
Expand Down Expand Up @@ -803,7 +803,7 @@ struct ParserState {
FLATBUFFERS_ASSERT(cursor_ && line_start_ && cursor_ >= line_start_);
return static_cast<int64_t>(cursor_ - line_start_);
}

const char *prev_cursor_;
const char *cursor_;
const char *line_start_;
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 641fbe4

Please sign in to comment.