From bf26a0eccc0739a076e7b5f83e64d91c4abb1256 Mon Sep 17 00:00:00 2001 From: daksenik Date: Mon, 4 Jul 2016 16:34:41 +0300 Subject: [PATCH 01/35] Fixed operator++. Added CreateXXX for vector types. --- include/flatbuffers/flatbuffers.h | 2 +- src/idl_gen_cpp.cpp | 77 +++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index 6755ec279..e3b10f6b1 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -288,7 +288,7 @@ public: } VectorIterator operator++(int) { - VectorIterator temp(data_); + VectorIterator temp(data_,0); data_ += IndirectHelper::element_stride; return temp; } diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index c690240d6..72a7dc519 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -719,6 +719,83 @@ class CppGenerator : public BaseGenerator { } } code += " return builder_.Finish();\n}\n\n"; + + //Generate a CreateX function with vector types as parameters + std::vectorvect_pars_ids; + code += "inline flatbuffers::Offset<" + struct_def.name + "> Create"; + code += struct_def.name; + code += "(flatbuffers::FlatBufferBuilder &_fbb"; + for (auto it = struct_def.fields.vec.begin(); + it != struct_def.fields.vec.end(); ++it) { + auto &field = **it; + if (!field.deprecated) { + if (field.value.type.base_type == BASE_TYPE_STRING) { + vect_pars_ids.push_back(*it); + code += ",\n const char *"; + code += field.name + " = \"\""; + } + else if (field.value.type.base_type == BASE_TYPE_VECTOR) { + vect_pars_ids.push_back(*it); + code += ",\n std::vector<" + GenTypeWire(field.value.type.VectorType(), "", false) + "> " + field.name; + code += " = std::vector<" + GenTypeWire(field.value.type.VectorType(), "", false) + ">()"; + } + else { + code += ",\n " + GenTypeWire(field.value.type, " ", true); + code += field.name + " = "; + if (field.value.type.enum_def && IsScalar(field.value.type.base_type)) { + auto ev = field.value.type.enum_def->ReverseLookup( + static_cast(StringToInt(field.value.constant.c_str())), + false); + if (ev) { + code += WrapInNameSpace( + field.value.type.enum_def->defined_namespace, + GetEnumVal(*field.value.type.enum_def, *ev, parser_.opts)); + } + else { + code += GenUnderlyingCast(field, true, field.value.constant); + } + } + else if (field.value.type.base_type == BASE_TYPE_BOOL) { + code += field.value.constant == "0" ? "false" : "true"; + } + else { + code += GenDefaultConstant(field); + } + } + } + } + code += ") {\n "; + //getting offsets + + for (auto it = vect_pars_ids.begin(); + it != vect_pars_ids.end(); ++it) { + auto&field = **it; + code += "auto " + field.name + "_off = "; + if (field.value.type.base_type == BASE_TYPE_STRING) { + code += "_fbb.CreateString(" + field.name + ");\n "; + } + else if (field.value.type.base_type == BASE_TYPE_VECTOR) { + code += "_fbb.CreateVector<" + GenTypeWire(field.value.type.VectorType(), "", false) + ">(" + field.name + ");\n "; + } + } + + //... + code += struct_def.name + "Builder builder_(_fbb);\n"; + for (size_t size = struct_def.sortbysize ? sizeof(largest_scalar_t) : 1; + size; size /= 2) { + for (auto it = struct_def.fields.vec.rbegin(); + it != struct_def.fields.vec.rend(); ++it) { + auto &field = **it; + if (!field.deprecated && (!struct_def.sortbysize || + size == SizeOf(field.value.type.base_type))) { + code += " builder_.add_" + field.name + "("; + code += (!IsScalar(field.value.type.base_type) && (!IsStruct(field.value.type))) ? //if vector or string + (field.name + "_off") : field.name; + code += ");\n"; + } + } + } + code += " return builder_.Finish();\n}\n\n"; } static void GenPadding(const FieldDef &field, std::string &code, From c2411e9c8c7a1128ec0afa93d93243e4910d352c Mon Sep 17 00:00:00 2001 From: daksenik Date: Mon, 4 Jul 2016 17:37:23 +0300 Subject: [PATCH 02/35] Fixed operator++. Added CreateXXX for vector types. --- src/idl_gen_cpp.cpp | 132 ++++++++++++++++++++++---------------------- 1 file changed, 67 insertions(+), 65 deletions(-) diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 72a7dc519..ba79fffd5 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -679,6 +679,9 @@ class CppGenerator : public BaseGenerator { // Generate a convenient CreateX function that uses the above builder // to create a table in one go. + + std::vectorvect_pars; + code += "inline flatbuffers::Offset<" + struct_def.name + "> Create"; code += struct_def.name; code += "(flatbuffers::FlatBufferBuilder &_fbb"; @@ -686,6 +689,7 @@ class CppGenerator : public BaseGenerator { it != struct_def.fields.vec.end(); ++it) { auto &field = **it; if (!field.deprecated) { + if (field.value.type.base_type == BASE_TYPE_STRING || field.value.type.base_type == BASE_TYPE_VECTOR) vect_pars.push_back(*it); code += ",\n " + GenTypeWire(field.value.type, " ", true); code += field.name + " = "; if (field.value.type.enum_def && IsScalar(field.value.type.base_type)) { @@ -721,81 +725,79 @@ class CppGenerator : public BaseGenerator { code += " return builder_.Finish();\n}\n\n"; //Generate a CreateX function with vector types as parameters - std::vectorvect_pars_ids; - code += "inline flatbuffers::Offset<" + struct_def.name + "> Create"; - code += struct_def.name; - code += "(flatbuffers::FlatBufferBuilder &_fbb"; - for (auto it = struct_def.fields.vec.begin(); - it != struct_def.fields.vec.end(); ++it) { - auto &field = **it; - if (!field.deprecated) { - if (field.value.type.base_type == BASE_TYPE_STRING) { - vect_pars_ids.push_back(*it); - code += ",\n const char *"; - code += field.name + " = \"\""; - } - else if (field.value.type.base_type == BASE_TYPE_VECTOR) { - vect_pars_ids.push_back(*it); - code += ",\n std::vector<" + GenTypeWire(field.value.type.VectorType(), "", false) + "> " + field.name; - code += " = std::vector<" + GenTypeWire(field.value.type.VectorType(), "", false) + ">()"; - } - else { - code += ",\n " + GenTypeWire(field.value.type, " ", true); - code += field.name + " = "; - if (field.value.type.enum_def && IsScalar(field.value.type.base_type)) { - auto ev = field.value.type.enum_def->ReverseLookup( - static_cast(StringToInt(field.value.constant.c_str())), - false); - if (ev) { - code += WrapInNameSpace( - field.value.type.enum_def->defined_namespace, - GetEnumVal(*field.value.type.enum_def, *ev, parser_.opts)); - } - else { - code += GenUnderlyingCast(field, true, field.value.constant); - } + if (vect_pars.size()) { + code += "inline flatbuffers::Offset<" + struct_def.name + "> Create"; + code += struct_def.name; + code += "(flatbuffers::FlatBufferBuilder &_fbb"; + for (auto it = struct_def.fields.vec.begin(); + it != struct_def.fields.vec.end(); ++it) { + auto &field = **it; + if (!field.deprecated) { + if (field.value.type.base_type == BASE_TYPE_STRING) { + code += ",\n const char *"; + code += field.name + " = \"\""; } - else if (field.value.type.base_type == BASE_TYPE_BOOL) { - code += field.value.constant == "0" ? "false" : "true"; + else if (field.value.type.base_type == BASE_TYPE_VECTOR) { + code += ",\n std::vector<" + GenTypeWire(field.value.type.VectorType(), "", false) + "> " + field.name; + code += " = std::vector<" + GenTypeWire(field.value.type.VectorType(), "", false) + ">()"; } else { - code += GenDefaultConstant(field); + code += ",\n " + GenTypeWire(field.value.type, " ", true); + code += field.name + " = "; + if (field.value.type.enum_def && IsScalar(field.value.type.base_type)) { + auto ev = field.value.type.enum_def->ReverseLookup( + static_cast(StringToInt(field.value.constant.c_str())), + false); + if (ev) { + code += WrapInNameSpace( + field.value.type.enum_def->defined_namespace, + GetEnumVal(*field.value.type.enum_def, *ev, parser_.opts)); + } + else { + code += GenUnderlyingCast(field, true, field.value.constant); + } + } + else if (field.value.type.base_type == BASE_TYPE_BOOL) { + code += field.value.constant == "0" ? "false" : "true"; + } + else { + code += GenDefaultConstant(field); + } } } } - } - code += ") {\n "; - //getting offsets + code += ") {\n "; + //getting offsets - for (auto it = vect_pars_ids.begin(); - it != vect_pars_ids.end(); ++it) { - auto&field = **it; - code += "auto " + field.name + "_off = "; - if (field.value.type.base_type == BASE_TYPE_STRING) { - code += "_fbb.CreateString(" + field.name + ");\n "; - } - else if (field.value.type.base_type == BASE_TYPE_VECTOR) { - code += "_fbb.CreateVector<" + GenTypeWire(field.value.type.VectorType(), "", false) + ">(" + field.name + ");\n "; - } - } - - //... - code += struct_def.name + "Builder builder_(_fbb);\n"; - for (size_t size = struct_def.sortbysize ? sizeof(largest_scalar_t) : 1; - size; size /= 2) { - for (auto it = struct_def.fields.vec.rbegin(); - it != struct_def.fields.vec.rend(); ++it) { - auto &field = **it; - if (!field.deprecated && (!struct_def.sortbysize || - size == SizeOf(field.value.type.base_type))) { - code += " builder_.add_" + field.name + "("; - code += (!IsScalar(field.value.type.base_type) && (!IsStruct(field.value.type))) ? //if vector or string - (field.name + "_off") : field.name; - code += ");\n"; + for (auto it = vect_pars.begin(); + it != vect_pars.end(); ++it) { + auto&field = **it; + code += "auto " + field.name + "_off = "; + if (field.value.type.base_type == BASE_TYPE_STRING) { + code += "_fbb.CreateString(" + field.name + ");\n "; + } + else if (field.value.type.base_type == BASE_TYPE_VECTOR) { + code += "_fbb.CreateVector<" + GenTypeWire(field.value.type.VectorType(), "", false) + ">(" + field.name + ");\n "; } } + + code += struct_def.name + "Builder builder_(_fbb);\n"; + for (size_t size = struct_def.sortbysize ? sizeof(largest_scalar_t) : 1; + size; size /= 2) { + for (auto it = struct_def.fields.vec.rbegin(); + it != struct_def.fields.vec.rend(); ++it) { + auto &field = **it; + if (!field.deprecated && (!struct_def.sortbysize || + size == SizeOf(field.value.type.base_type))) { + code += " builder_.add_" + field.name + "("; + code += (field.value.type.base_type == BASE_TYPE_STRING || field.value.type.base_type == BASE_TYPE_VECTOR) ? //if vector or string + (field.name + "_off") : field.name; + code += ");\n"; + } + } + } + code += " return builder_.Finish();\n}\n\n"; } - code += " return builder_.Finish();\n}\n\n"; } static void GenPadding(const FieldDef &field, std::string &code, From 7d84a4914fe4784369d0490fb2b8a45684ed1f07 Mon Sep 17 00:00:00 2001 From: Raman Date: Mon, 4 Jul 2016 21:34:42 +0200 Subject: [PATCH 03/35] Update idl_gen_fbs.cpp --- src/idl_gen_fbs.cpp | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/idl_gen_fbs.cpp b/src/idl_gen_fbs.cpp index 00afe25d4..241a78792 100644 --- a/src/idl_gen_fbs.cpp +++ b/src/idl_gen_fbs.cpp @@ -52,15 +52,18 @@ static void GenNameSpace(const Namespace &name_space, std::string *_schema, } // Generate a flatbuffer schema from the Parser's internal representation. -std::string GenerateFBS(const Parser &parser, const std::string &file_name) { - // Proto namespaces may clash with table names, so we have to prefix all: - for (auto it = parser.namespaces_.begin(); it != parser.namespaces_.end(); - ++it) { - for (auto comp = (*it)->components.begin(); comp != (*it)->components.end(); - ++comp) { - (*comp) = "_" + (*comp); - } - } +std::string GenerateFBS(const Parser &parser, const std::string &file_name, const bool &escape_proto_identifiers) { + // Proto namespaces may clash with table names, so we have to prefix all: + + if (!escape_proto_identifiers) { + for (auto it = parser.namespaces_.begin(); it != parser.namespaces_.end(); + ++it) { + for (auto comp = (*it)->components.begin(); comp != (*it)->components.end(); + ++comp) { + (*comp) = "_" + (*comp); + } + } + } std::string schema; schema += "// Generated from " + file_name + ".proto\n\n"; @@ -119,9 +122,10 @@ std::string GenerateFBS(const Parser &parser, const std::string &file_name) { bool GenerateFBS(const Parser &parser, const std::string &path, - const std::string &file_name) { + const std::string &file_name, + const bool &escape_proto_identifiers) { return SaveFile((path + file_name + ".fbs").c_str(), - GenerateFBS(parser, file_name), false); + GenerateFBS(parser, file_name, escape_proto_identifiers), false); } } // namespace flatbuffers From ff57f52b7229e70a7029c9c50b58a841768359a6 Mon Sep 17 00:00:00 2001 From: Raman Date: Mon, 4 Jul 2016 21:37:50 +0200 Subject: [PATCH 04/35] Update flatc.cpp --- src/flatc.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/flatc.cpp b/src/flatc.cpp index c7ba29e86..1b9559f6e 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -194,7 +194,9 @@ int main(int argc, const char *argv[]) { binary_files_from = filenames.size(); } else if(arg == "--proto") { opts.proto_mode = true; - } else if(arg == "--schema") { + } else if (arg == "--escape-proto-identifiers") { + opts.escape_proto_identifiers = true; + } else if (arg == "--schema") { schema_binary = true; } else if(arg == "-M") { print_make_rules = true; @@ -314,7 +316,7 @@ int main(int argc, const char *argv[]) { } } - if (opts.proto_mode) GenerateFBS(*parser, output_path, filebase); + if (opts.proto_mode) GenerateFBS(*parser, output_path, filebase, opts.escape_proto_identifiers) // We do not want to generate code for the definitions in this file // in any files coming up next. From aae48e3a9d09695b906da0113c410030dd26989b Mon Sep 17 00:00:00 2001 From: Raman Date: Mon, 4 Jul 2016 21:41:01 +0200 Subject: [PATCH 05/35] Update idl.h --- include/flatbuffers/idl.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index b22bc08a1..d5dee17af 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -337,7 +337,8 @@ struct IDLOptions { bool generate_all; bool skip_unexpected_fields_in_json; bool generate_name_strings; - + bool escape_proto_identifiers; + // Possible options for the more general generator below. enum Language { kJava, kCSharp, kGo, kMAX }; @@ -356,6 +357,7 @@ struct IDLOptions { generate_all(false), skip_unexpected_fields_in_json(false), generate_name_strings(false), + escape_proto_identifiers(false), lang(IDLOptions::kJava) {} }; @@ -644,10 +646,12 @@ extern bool GenerateGeneral(const Parser &parser, // Generate a schema file from the internal representation, useful after // parsing a .proto schema. extern std::string GenerateFBS(const Parser &parser, - const std::string &file_name); + const std::string &file_name, + const bool &escape_proto_identifiers); extern bool GenerateFBS(const Parser &parser, const std::string &path, - const std::string &file_name); + const std::string &file_name, + const bool &escape_proto_identifiers); // Generate a make rule for the generated JavaScript code. // See idl_gen_js.cpp. From 9fd4d664388853a93cdcea17bc51daed8cfb4322 Mon Sep 17 00:00:00 2001 From: Raman Date: Mon, 4 Jul 2016 21:51:34 +0200 Subject: [PATCH 06/35] Update flatc.cpp --- src/flatc.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/flatc.cpp b/src/flatc.cpp index 1b9559f6e..21dbd304d 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -120,8 +120,9 @@ static void Error(const std::string &err, bool usage, bool show_exe_name) { " --no-includes Don\'t generate include statements for included\n" " schemas the generated file depends on (C++).\n" " --gen-mutable Generate accessors that can mutate buffers in-place.\n" - " --gen-onefile Generate single output file for C#\n" - " --gen-name-strings Generate type name functions for C++.\n" + " --gen-onefile Generate single output file for C#.\n" + " --gen-name-strings Generate type name functions for C++.\n" + " --escape-proto-identifiers Disable appending '_' in namespaces names.\n" " --raw-binary Allow binaries without file_indentifier to be read.\n" " This may crash flatc given a mismatched schema.\n" " --proto Input is a .proto, translate to .fbs.\n" From 50437642477695d02ec8c5002cc50f89aa27dc50 Mon Sep 17 00:00:00 2001 From: Raman Date: Mon, 4 Jul 2016 21:55:44 +0200 Subject: [PATCH 07/35] Update test.cpp --- tests/test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test.cpp b/tests/test.cpp index 6f3b06299..15f8561ca 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -478,7 +478,7 @@ void ParseProtoTest() { TEST_EQ(parser.Parse(protofile.c_str(), include_directories), true); // Generate fbs. - auto fbs = flatbuffers::GenerateFBS(parser, "test"); + auto fbs = flatbuffers::GenerateFBS(parser, "test", false); // Ensure generated file is parsable. flatbuffers::Parser parser2; From 4f8abaaf10dc6f092960626ee88e47250aa55653 Mon Sep 17 00:00:00 2001 From: Raman Date: Mon, 4 Jul 2016 22:58:58 +0200 Subject: [PATCH 08/35] Update flatc.cpp --- src/flatc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/flatc.cpp b/src/flatc.cpp index 21dbd304d..a07fbcb7c 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -317,7 +317,7 @@ int main(int argc, const char *argv[]) { } } - if (opts.proto_mode) GenerateFBS(*parser, output_path, filebase, opts.escape_proto_identifiers) + if (opts.proto_mode) GenerateFBS(*parser, output_path, filebase, opts.escape_proto_identifiers); // We do not want to generate code for the definitions in this file // in any files coming up next. From 72e8219a65da19dc42874247b7ce49df141b48dc Mon Sep 17 00:00:00 2001 From: Raman Date: Tue, 5 Jul 2016 22:20:57 +0200 Subject: [PATCH 09/35] Update idl_parser.cpp --- src/idl_parser.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index b758e9592..8c0fdade3 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -423,6 +423,13 @@ CheckedError Parser::Next() { return NoError(); } else if (isdigit(static_cast(c)) || c == '-') { const char *start = cursor_ - 1; + const char *start_2 = cursor_ + 1; + if (c == '-' && *cursor_ == '0' && (*start_2 == 'x' || *start_2 == 'X')) { + ++start; + ++cursor_; + attribute_.append(&c, &c + 1); + c = '0'; + } if (c == '0' && (*cursor_ == 'x' || *cursor_ == 'X')) { cursor_++; while (isxdigit(static_cast(*cursor_))) cursor_++; From 4bb6ab3cd557d850b0e848261ff980ffd5918316 Mon Sep 17 00:00:00 2001 From: Raman Date: Tue, 5 Jul 2016 22:24:21 +0200 Subject: [PATCH 10/35] Update flatc.cpp --- src/flatc.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/flatc.cpp b/src/flatc.cpp index a07fbcb7c..b8c996372 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -195,9 +195,9 @@ int main(int argc, const char *argv[]) { binary_files_from = filenames.size(); } else if(arg == "--proto") { opts.proto_mode = true; - } else if (arg == "--escape-proto-identifiers") { - opts.escape_proto_identifiers = true; - } else if (arg == "--schema") { + } else if (arg == "--escape-proto-identifiers") { + opts.escape_proto_identifiers = true; + } else if (arg == "--schema") { schema_binary = true; } else if(arg == "-M") { print_make_rules = true; From f738981ed50840902eb9c422fca98272a8afffce Mon Sep 17 00:00:00 2001 From: Raman Date: Tue, 5 Jul 2016 22:26:21 +0200 Subject: [PATCH 11/35] Update idl_gen_fbs.cpp --- src/idl_gen_fbs.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/idl_gen_fbs.cpp b/src/idl_gen_fbs.cpp index 241a78792..719cb0cf7 100644 --- a/src/idl_gen_fbs.cpp +++ b/src/idl_gen_fbs.cpp @@ -55,16 +55,15 @@ static void GenNameSpace(const Namespace &name_space, std::string *_schema, std::string GenerateFBS(const Parser &parser, const std::string &file_name, const bool &escape_proto_identifiers) { // Proto namespaces may clash with table names, so we have to prefix all: - if (!escape_proto_identifiers) { - for (auto it = parser.namespaces_.begin(); it != parser.namespaces_.end(); - ++it) { - for (auto comp = (*it)->components.begin(); comp != (*it)->components.end(); - ++comp) { - (*comp) = "_" + (*comp); - } + if (!escape_proto_identifiers) { + for (auto it = parser.namespaces_.begin(); it != parser.namespaces_.end(); + ++it) { + for (auto comp = (*it)->components.begin(); comp != (*it)->components.end(); + ++comp) { + (*comp) = "_" + (*comp); } - } - + } + } std::string schema; schema += "// Generated from " + file_name + ".proto\n\n"; if (parser.opts.include_dependence_headers) { From 248432b92de15298f38e68ce8296b5b7905adafe Mon Sep 17 00:00:00 2001 From: Raman Date: Tue, 5 Jul 2016 23:23:17 +0200 Subject: [PATCH 12/35] Update idl_gen_fbs.cpp --- src/idl_gen_fbs.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/idl_gen_fbs.cpp b/src/idl_gen_fbs.cpp index 719cb0cf7..5bbf39073 100644 --- a/src/idl_gen_fbs.cpp +++ b/src/idl_gen_fbs.cpp @@ -122,7 +122,7 @@ std::string GenerateFBS(const Parser &parser, const std::string &file_name, cons bool GenerateFBS(const Parser &parser, const std::string &path, const std::string &file_name, - const bool &escape_proto_identifiers) { + const bool &escape_proto_identifiers) { return SaveFile((path + file_name + ".fbs").c_str(), GenerateFBS(parser, file_name, escape_proto_identifiers), false); } From dfbda986d7f387b0b36713df1a2e8420f8fcbb31 Mon Sep 17 00:00:00 2001 From: daksenik Date: Fri, 8 Jul 2016 12:35:57 +0300 Subject: [PATCH 13/35] New CreateXXX (with vectors and strings) calls old CreateXXX (with offsets). --- src/idl_gen_cpp.cpp | 56 +++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 33 deletions(-) diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index ba79fffd5..64dc15871 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -680,7 +680,7 @@ class CppGenerator : public BaseGenerator { // Generate a convenient CreateX function that uses the above builder // to create a table in one go. - std::vectorvect_pars; + bool gen_vector_pars = false; code += "inline flatbuffers::Offset<" + struct_def.name + "> Create"; code += struct_def.name; @@ -689,7 +689,7 @@ class CppGenerator : public BaseGenerator { it != struct_def.fields.vec.end(); ++it) { auto &field = **it; if (!field.deprecated) { - if (field.value.type.base_type == BASE_TYPE_STRING || field.value.type.base_type == BASE_TYPE_VECTOR) vect_pars.push_back(*it); + if (field.value.type.base_type == BASE_TYPE_STRING || field.value.type.base_type == BASE_TYPE_VECTOR) gen_vector_pars = true; code += ",\n " + GenTypeWire(field.value.type, " ", true); code += field.name + " = "; if (field.value.type.enum_def && IsScalar(field.value.type.base_type)) { @@ -725,7 +725,7 @@ class CppGenerator : public BaseGenerator { code += " return builder_.Finish();\n}\n\n"; //Generate a CreateX function with vector types as parameters - if (vect_pars.size()) { + if (gen_vector_pars) { code += "inline flatbuffers::Offset<" + struct_def.name + "> Create"; code += struct_def.name; code += "(flatbuffers::FlatBufferBuilder &_fbb"; @@ -735,11 +735,11 @@ class CppGenerator : public BaseGenerator { if (!field.deprecated) { if (field.value.type.base_type == BASE_TYPE_STRING) { code += ",\n const char *"; - code += field.name + " = \"\""; + code += field.name + " = nullptr"; } else if (field.value.type.base_type == BASE_TYPE_VECTOR) { - code += ",\n std::vector<" + GenTypeWire(field.value.type.VectorType(), "", false) + "> " + field.name; - code += " = std::vector<" + GenTypeWire(field.value.type.VectorType(), "", false) + ">()"; + code += ",\n std::vector<" + GenTypeWire(field.value.type.VectorType(), "", false) + "> *" + field.name; + code += " = nullptr"; } else { code += ",\n " + GenTypeWire(field.value.type, " ", true); @@ -767,36 +767,26 @@ class CppGenerator : public BaseGenerator { } } code += ") {\n "; - //getting offsets - for (auto it = vect_pars.begin(); - it != vect_pars.end(); ++it) { - auto&field = **it; - code += "auto " + field.name + "_off = "; - if (field.value.type.base_type == BASE_TYPE_STRING) { - code += "_fbb.CreateString(" + field.name + ");\n "; - } - else if (field.value.type.base_type == BASE_TYPE_VECTOR) { - code += "_fbb.CreateVector<" + GenTypeWire(field.value.type.VectorType(), "", false) + ">(" + field.name + ");\n "; + code += "return Create"; + code += struct_def.name; + code += "(_fbb"; + for (auto it = struct_def.fields.vec.begin(); + it != struct_def.fields.vec.end(); ++it) { + auto &field = **it; + if (!field.deprecated) { + if (field.value.type.base_type == BASE_TYPE_STRING) { + code += ", " + field.name + "==nullptr? 0 : "; + code += "_fbb.CreateString(" + field.name + ")"; + } else if (field.value.type.base_type == BASE_TYPE_VECTOR) { + code += ", " + field.name + "==nullptr? 0 : "; + code += "_fbb.CreateVector<" + GenTypeWire(field.value.type.VectorType(), "", false) + ">(*" + field.name + ")"; + } else code += ", " + field.name; } } - - code += struct_def.name + "Builder builder_(_fbb);\n"; - for (size_t size = struct_def.sortbysize ? sizeof(largest_scalar_t) : 1; - size; size /= 2) { - for (auto it = struct_def.fields.vec.rbegin(); - it != struct_def.fields.vec.rend(); ++it) { - auto &field = **it; - if (!field.deprecated && (!struct_def.sortbysize || - size == SizeOf(field.value.type.base_type))) { - code += " builder_.add_" + field.name + "("; - code += (field.value.type.base_type == BASE_TYPE_STRING || field.value.type.base_type == BASE_TYPE_VECTOR) ? //if vector or string - (field.name + "_off") : field.name; - code += ");\n"; - } - } - } - code += " return builder_.Finish();\n}\n\n"; + + + code += ");\n}\n\n"; } } From 084e5dbc4b170a828add67e81601f3c2c80cc481 Mon Sep 17 00:00:00 2001 From: lakedaemon Date: Fri, 8 Jul 2016 22:00:15 +0200 Subject: [PATCH 14/35] fixed initialization of member var for old make (hopefully) --- src/idl_gen_cpp.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index c690240d6..61ef6ff1b 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -39,7 +39,8 @@ class CppGenerator : public BaseGenerator { public: CppGenerator(const Parser &parser, const std::string &path, const std::string &file_name) - : BaseGenerator(parser, path, file_name, "", "::"){}; + : BaseGenerator(parser, path, file_name, "", "::"), + cur_name_space_(nullptr){}; // Iterate through all definitions we haven't generate code for (enums, // structs, // and tables) and output them to a single file. @@ -206,7 +207,7 @@ class CppGenerator : public BaseGenerator { private: // This tracks the current namespace so we can insert namespace declarations. - const Namespace *cur_name_space_ = nullptr; + const Namespace *cur_name_space_; const Namespace *CurrentNameSpace() { return cur_name_space_; } From 8e1aae0fd69c2bf4eb0ff5ed7af44983d04de9df Mon Sep 17 00:00:00 2001 From: lakedaemon Date: Fri, 8 Jul 2016 22:02:47 +0200 Subject: [PATCH 15/35] fix missing space (clang format) --- src/idl_gen_cpp.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 61ef6ff1b..6cdae16c9 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -39,8 +39,8 @@ class CppGenerator : public BaseGenerator { public: CppGenerator(const Parser &parser, const std::string &path, const std::string &file_name) - : BaseGenerator(parser, path, file_name, "", "::"), - cur_name_space_(nullptr){}; + : BaseGenerator(parser, path, file_name, "", "::"), + cur_name_space_(nullptr){}; // Iterate through all definitions we haven't generate code for (enums, // structs, // and tables) and output them to a single file. From a6764b9bf14215748af095dd9ef238a9dd258789 Mon Sep 17 00:00:00 2001 From: lakedaemon Date: Fri, 8 Jul 2016 22:51:31 +0200 Subject: [PATCH 16/35] same fix for general code generator --- src/idl_gen_general.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/idl_gen_general.cpp b/src/idl_gen_general.cpp index a96609c54..96e0e7bde 100644 --- a/src/idl_gen_general.cpp +++ b/src/idl_gen_general.cpp @@ -193,8 +193,9 @@ class GeneralGenerator : public BaseGenerator { public: GeneralGenerator(const Parser &parser, const std::string &path, const std::string &file_name) - : BaseGenerator(parser, path, file_name, "", "."){ - assert(parser_.opts.lang <= IDLOptions::kMAX); + : BaseGenerator(parser, path, file_name, "", "."), + lang_(language_parameters[parser_.opts.lang]) { + assert(parser_.opts.lang <= IDLOptions::kMAX); }; bool generate() { std::string one_file_code; @@ -1127,7 +1128,7 @@ void GenStruct(StructDef &struct_def, std::string *code_ptr) { } code += "};\n\n"; } - const LanguageParameters & lang_ = language_parameters[parser_.opts.lang]; + const LanguageParameters & lang_; }; } // namespace general From d9767b83156fa0fe9cc0d6dadb0b1be086b96fc7 Mon Sep 17 00:00:00 2001 From: Jonathan Tullett Date: Sat, 9 Jul 2016 09:16:26 +0100 Subject: [PATCH 17/35] Fix for issue #3922 Also, clean up redundant ';' at end of java classes. --- src/idl_gen_general.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/idl_gen_general.cpp b/src/idl_gen_general.cpp index 96e0e7bde..34ca2c3e4 100644 --- a/src/idl_gen_general.cpp +++ b/src/idl_gen_general.cpp @@ -545,7 +545,7 @@ void GenEnum(EnumDef &enum_def, std::string *code_ptr) { // "too sparse". Change at will. static const int kMaxSparseness = 5; if (range / static_cast(enum_def.vals.vec.size()) < kMaxSparseness) { - code += "\n private static"; + code += "\n public static"; code += lang_.const_decl; code += lang_.string_type; code += "[] names = { "; @@ -568,7 +568,10 @@ void GenEnum(EnumDef &enum_def, std::string *code_ptr) { } // Close the class - code += "};\n\n"; + code += "}"; + // Java does not need the closing semi-colon on class definitions. + code += (lang_.language != IDLOptions::kJava) ? ";" : ""; + code += "\n\n"; } // Returns the function name that is able to read a value of the given type. @@ -1126,7 +1129,10 @@ void GenStruct(StructDef &struct_def, std::string *code_ptr) { code += "); }\n"; } } - code += "};\n\n"; + code += "}"; + // Java does not need the closing semi-colon on class definitions. + code += (lang_.language != IDLOptions::kJava) ? ";" : ""; + code += "\n\n"; } const LanguageParameters & lang_; }; From 1661f3a2e8e6f3fafb844e8d5a72f4913a4630f7 Mon Sep 17 00:00:00 2001 From: daksenik Date: Sat, 9 Jul 2016 13:00:21 +0300 Subject: [PATCH 18/35] Added function GenSimpleParam. Tests added. --- src/idl_gen_cpp.cpp | 86 +++++++++++++++------------------- tests/monster_test_generated.h | 39 +++++++++++++++ 2 files changed, 77 insertions(+), 48 deletions(-) diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 64dc15871..07db13dbb 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -457,6 +457,30 @@ class CppGenerator : public BaseGenerator { : field.value.constant; } + void GenSimpleParam(std::string &code, FieldDef &field) { + code += ",\n " + GenTypeWire(field.value.type, " ", true); + code += field.name + " = "; + if (field.value.type.enum_def && IsScalar(field.value.type.base_type)) { + auto ev = field.value.type.enum_def->ReverseLookup( + static_cast(StringToInt(field.value.constant.c_str())), + false); + if (ev) { + code += WrapInNameSpace( + field.value.type.enum_def->defined_namespace, + GetEnumVal(*field.value.type.enum_def, *ev, parser_.opts)); + } + else { + code += GenUnderlyingCast(field, true, field.value.constant); + } + } + else if (field.value.type.base_type == BASE_TYPE_BOOL) { + code += field.value.constant == "0" ? "false" : "true"; + } + else { + code += GenDefaultConstant(field); + } + } + // Generate an accessor struct, builder structs & function for a table. void GenTable(StructDef &struct_def, std::string *code_ptr) { std::string &code = *code_ptr; @@ -679,9 +703,7 @@ class CppGenerator : public BaseGenerator { // Generate a convenient CreateX function that uses the above builder // to create a table in one go. - bool gen_vector_pars = false; - code += "inline flatbuffers::Offset<" + struct_def.name + "> Create"; code += struct_def.name; code += "(flatbuffers::FlatBufferBuilder &_fbb"; @@ -689,25 +711,11 @@ class CppGenerator : public BaseGenerator { it != struct_def.fields.vec.end(); ++it) { auto &field = **it; if (!field.deprecated) { - if (field.value.type.base_type == BASE_TYPE_STRING || field.value.type.base_type == BASE_TYPE_VECTOR) gen_vector_pars = true; - code += ",\n " + GenTypeWire(field.value.type, " ", true); - code += field.name + " = "; - if (field.value.type.enum_def && IsScalar(field.value.type.base_type)) { - auto ev = field.value.type.enum_def->ReverseLookup( - static_cast(StringToInt(field.value.constant.c_str())), - false); - if (ev) { - code += WrapInNameSpace( - field.value.type.enum_def->defined_namespace, - GetEnumVal(*field.value.type.enum_def, *ev, parser_.opts)); - } else { - code += GenUnderlyingCast(field, true, field.value.constant); - } - } else if (field.value.type.base_type == BASE_TYPE_BOOL) { - code += field.value.constant == "0" ? "false" : "true"; - } else { - code += GenDefaultConstant(field); + if (field.value.type.base_type == BASE_TYPE_STRING || + field.value.type.base_type == BASE_TYPE_VECTOR) { + gen_vector_pars = true; } + GenSimpleParam(code, field); } } code += ") {\n " + struct_def.name + "Builder builder_(_fbb);\n"; @@ -738,31 +746,11 @@ class CppGenerator : public BaseGenerator { code += field.name + " = nullptr"; } else if (field.value.type.base_type == BASE_TYPE_VECTOR) { - code += ",\n std::vector<" + GenTypeWire(field.value.type.VectorType(), "", false) + "> *" + field.name; - code += " = nullptr"; - } - else { - code += ",\n " + GenTypeWire(field.value.type, " ", true); - code += field.name + " = "; - if (field.value.type.enum_def && IsScalar(field.value.type.base_type)) { - auto ev = field.value.type.enum_def->ReverseLookup( - static_cast(StringToInt(field.value.constant.c_str())), - false); - if (ev) { - code += WrapInNameSpace( - field.value.type.enum_def->defined_namespace, - GetEnumVal(*field.value.type.enum_def, *ev, parser_.opts)); - } - else { - code += GenUnderlyingCast(field, true, field.value.constant); - } - } - else if (field.value.type.base_type == BASE_TYPE_BOOL) { - code += field.value.constant == "0" ? "false" : "true"; - } - else { - code += GenDefaultConstant(field); - } + code += ",\n std::vector<"; + code += GenTypeWire(field.value.type.VectorType(), "", false); + code += "> *" + field.name + " = nullptr"; + } else { + GenSimpleParam(code, field); } } } @@ -776,11 +764,13 @@ class CppGenerator : public BaseGenerator { auto &field = **it; if (!field.deprecated) { if (field.value.type.base_type == BASE_TYPE_STRING) { - code += ", " + field.name + "==nullptr? 0 : "; + code += ", " + field.name + " == nullptr ? 0 : "; code += "_fbb.CreateString(" + field.name + ")"; } else if (field.value.type.base_type == BASE_TYPE_VECTOR) { - code += ", " + field.name + "==nullptr? 0 : "; - code += "_fbb.CreateVector<" + GenTypeWire(field.value.type.VectorType(), "", false) + ">(*" + field.name + ")"; + code += ", " + field.name + " == nullptr ? 0 : "; + code += "_fbb.CreateVector<"; + code += GenTypeWire(field.value.type.VectorType(), "", false); + code += ">(*" + field.name + ")"; } else code += ", " + field.name; } } diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index 50fbc28ce..5a60eeb3c 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -215,6 +215,13 @@ inline flatbuffers::Offset CreateStat(flatbuffers::FlatBufferBuilder &_fbb return builder_.Finish(); } +inline flatbuffers::Offset CreateStat(flatbuffers::FlatBufferBuilder &_fbb, + const char *id = nullptr, + int64_t val = 0, + uint16_t count = 0) { + return CreateStat(_fbb, id == nullptr ? 0 : _fbb.CreateString(id), val, count); +} + /// an example documentation comment: monster object struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { enum { @@ -457,6 +464,38 @@ inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder return builder_.Finish(); } +inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, + const Vec3 *pos = 0, + int16_t mana = 150, + int16_t hp = 100, + const char *name = nullptr, + std::vector *inventory = nullptr, + Color color = Color_Blue, + Any test_type = Any_NONE, + flatbuffers::Offset test = 0, + std::vector *test4 = nullptr, + std::vector> *testarrayofstring = nullptr, + std::vector> *testarrayoftables = nullptr, + flatbuffers::Offset enemy = 0, + std::vector *testnestedflatbuffer = nullptr, + flatbuffers::Offset testempty = 0, + bool testbool = false, + int32_t testhashs32_fnv1 = 0, + uint32_t testhashu32_fnv1 = 0, + int64_t testhashs64_fnv1 = 0, + uint64_t testhashu64_fnv1 = 0, + int32_t testhashs32_fnv1a = 0, + uint32_t testhashu32_fnv1a = 0, + int64_t testhashs64_fnv1a = 0, + uint64_t testhashu64_fnv1a = 0, + std::vector *testarrayofbools = nullptr, + float testf = 3.14159f, + float testf2 = 3.0f, + float testf3 = 0.0f, + std::vector> *testarrayofstring2 = nullptr) { + return CreateMonster(_fbb, pos, mana, hp, name == nullptr ? 0 : _fbb.CreateString(name), inventory == nullptr ? 0 : _fbb.CreateVector(*inventory), color, test_type, test, test4 == nullptr ? 0 : _fbb.CreateVector(*test4), testarrayofstring == nullptr ? 0 : _fbb.CreateVector>(*testarrayofstring), testarrayoftables == nullptr ? 0 : _fbb.CreateVector>(*testarrayoftables), enemy, testnestedflatbuffer == nullptr ? 0 : _fbb.CreateVector(*testnestedflatbuffer), testempty, testbool, testhashs32_fnv1, testhashu32_fnv1, testhashs64_fnv1, testhashu64_fnv1, testhashs32_fnv1a, testhashu32_fnv1a, testhashs64_fnv1a, testhashu64_fnv1a, testarrayofbools == nullptr ? 0 : _fbb.CreateVector(*testarrayofbools), testf, testf2, testf3, testarrayofstring2 == nullptr ? 0 : _fbb.CreateVector>(*testarrayofstring2)); +} + inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, Any type) { switch (type) { case Any_NONE: return true; From 71d30d5c0282d23ebaaf2100f34c681ea9a1f071 Mon Sep 17 00:00:00 2001 From: Raman Date: Sat, 9 Jul 2016 12:28:02 +0200 Subject: [PATCH 19/35] Update idl.h --- include/flatbuffers/idl.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index d5dee17af..dddfab25c 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -646,12 +646,10 @@ extern bool GenerateGeneral(const Parser &parser, // Generate a schema file from the internal representation, useful after // parsing a .proto schema. extern std::string GenerateFBS(const Parser &parser, - const std::string &file_name, - const bool &escape_proto_identifiers); + const std::string &file_name); extern bool GenerateFBS(const Parser &parser, const std::string &path, - const std::string &file_name, - const bool &escape_proto_identifiers); + const std::string &file_name); // Generate a make rule for the generated JavaScript code. // See idl_gen_js.cpp. From d9d47a53bd254ad9855de4ff98336f0a9c257545 Mon Sep 17 00:00:00 2001 From: Raman Date: Sat, 9 Jul 2016 12:40:50 +0200 Subject: [PATCH 20/35] Update idl_gen_fbs.cpp --- src/idl_gen_fbs.cpp | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/idl_gen_fbs.cpp b/src/idl_gen_fbs.cpp index 5bbf39073..c391ed698 100644 --- a/src/idl_gen_fbs.cpp +++ b/src/idl_gen_fbs.cpp @@ -52,18 +52,18 @@ static void GenNameSpace(const Namespace &name_space, std::string *_schema, } // Generate a flatbuffer schema from the Parser's internal representation. -std::string GenerateFBS(const Parser &parser, const std::string &file_name, const bool &escape_proto_identifiers) { +std::string GenerateFBS(const Parser &parser, const std::string &file_name) { // Proto namespaces may clash with table names, so we have to prefix all: - - if (!escape_proto_identifiers) { - for (auto it = parser.namespaces_.begin(); it != parser.namespaces_.end(); - ++it) { - for (auto comp = (*it)->components.begin(); comp != (*it)->components.end(); - ++comp) { - (*comp) = "_" + (*comp); - } - } + if (!parser.opts.escape_proto_identifiers) { + for (auto it = parser.namespaces_.begin(); it != parser.namespaces_.end(); + ++it) { + for (auto comp = (*it)->components.begin(); comp != (*it)->components.end(); + ++comp) { + (*comp) = "_" + (*comp); + } + } } + std::string schema; schema += "// Generated from " + file_name + ".proto\n\n"; if (parser.opts.include_dependence_headers) { @@ -121,10 +121,9 @@ std::string GenerateFBS(const Parser &parser, const std::string &file_name, cons bool GenerateFBS(const Parser &parser, const std::string &path, - const std::string &file_name, - const bool &escape_proto_identifiers) { + const std::string &file_name) { return SaveFile((path + file_name + ".fbs").c_str(), - GenerateFBS(parser, file_name, escape_proto_identifiers), false); + GenerateFBS(parser, file_name), false); } } // namespace flatbuffers From ffbc93526ecc048bdb867ebcfae66e2bb851c145 Mon Sep 17 00:00:00 2001 From: Raman Date: Sat, 9 Jul 2016 12:47:14 +0200 Subject: [PATCH 21/35] Update flatc.cpp --- src/flatc.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/flatc.cpp b/src/flatc.cpp index b8c996372..7ee2e5749 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -121,8 +121,8 @@ static void Error(const std::string &err, bool usage, bool show_exe_name) { " schemas the generated file depends on (C++).\n" " --gen-mutable Generate accessors that can mutate buffers in-place.\n" " --gen-onefile Generate single output file for C#.\n" - " --gen-name-strings Generate type name functions for C++.\n" - " --escape-proto-identifiers Disable appending '_' in namespaces names.\n" + " --gen-name-strings Generate type name functions for C++.\n" + " --escape-proto-ids Disable appending '_' in namespaces names.\n" " --raw-binary Allow binaries without file_indentifier to be read.\n" " This may crash flatc given a mismatched schema.\n" " --proto Input is a .proto, translate to .fbs.\n" @@ -195,8 +195,8 @@ int main(int argc, const char *argv[]) { binary_files_from = filenames.size(); } else if(arg == "--proto") { opts.proto_mode = true; - } else if (arg == "--escape-proto-identifiers") { - opts.escape_proto_identifiers = true; + } else if (arg == "--escape-proto-ids") { + opts.escape_proto_identifiers = true; } else if (arg == "--schema") { schema_binary = true; } else if(arg == "-M") { @@ -317,7 +317,7 @@ int main(int argc, const char *argv[]) { } } - if (opts.proto_mode) GenerateFBS(*parser, output_path, filebase, opts.escape_proto_identifiers); + if (opts.proto_mode) GenerateFBS(*parser, output_path, filebase); // We do not want to generate code for the definitions in this file // in any files coming up next. From 22697722d94cb63b9d74744462cb0912005df271 Mon Sep 17 00:00:00 2001 From: Raman Date: Sat, 9 Jul 2016 12:51:06 +0200 Subject: [PATCH 22/35] Update test.cpp --- tests/test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test.cpp b/tests/test.cpp index 15f8561ca..6f3b06299 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -478,7 +478,7 @@ void ParseProtoTest() { TEST_EQ(parser.Parse(protofile.c_str(), include_directories), true); // Generate fbs. - auto fbs = flatbuffers::GenerateFBS(parser, "test", false); + auto fbs = flatbuffers::GenerateFBS(parser, "test"); // Ensure generated file is parsable. flatbuffers::Parser parser2; From 5f2b4e787231fd2f3dc09a2ab3dd50ca327319f2 Mon Sep 17 00:00:00 2001 From: Raman Date: Sat, 9 Jul 2016 12:57:07 +0200 Subject: [PATCH 23/35] Update idl_parser.cpp --- src/idl_parser.cpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index 8c0fdade3..b758e9592 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -423,13 +423,6 @@ CheckedError Parser::Next() { return NoError(); } else if (isdigit(static_cast(c)) || c == '-') { const char *start = cursor_ - 1; - const char *start_2 = cursor_ + 1; - if (c == '-' && *cursor_ == '0' && (*start_2 == 'x' || *start_2 == 'X')) { - ++start; - ++cursor_; - attribute_.append(&c, &c + 1); - c = '0'; - } if (c == '0' && (*cursor_ == 'x' || *cursor_ == 'X')) { cursor_++; while (isxdigit(static_cast(*cursor_))) cursor_++; From f0b2cc8f6eb884606dd4b3cf2ec0b6eb71736235 Mon Sep 17 00:00:00 2001 From: Raman Date: Sat, 9 Jul 2016 13:32:45 +0200 Subject: [PATCH 24/35] Update flatc.cpp --- src/flatc.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/flatc.cpp b/src/flatc.cpp index 7ee2e5749..d410718e3 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -195,9 +195,9 @@ int main(int argc, const char *argv[]) { binary_files_from = filenames.size(); } else if(arg == "--proto") { opts.proto_mode = true; - } else if (arg == "--escape-proto-ids") { + } else if(arg == "--escape-proto-ids") { opts.escape_proto_identifiers = true; - } else if (arg == "--schema") { + } else if(arg == "--schema") { schema_binary = true; } else if(arg == "-M") { print_make_rules = true; From df5575de1786502fb63ce3c6d6a53d3c467f021d Mon Sep 17 00:00:00 2001 From: Raman Date: Sat, 9 Jul 2016 13:33:30 +0200 Subject: [PATCH 25/35] Update idl_gen_fbs.cpp --- src/idl_gen_fbs.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/idl_gen_fbs.cpp b/src/idl_gen_fbs.cpp index c391ed698..237230e8e 100644 --- a/src/idl_gen_fbs.cpp +++ b/src/idl_gen_fbs.cpp @@ -63,7 +63,7 @@ std::string GenerateFBS(const Parser &parser, const std::string &file_name) { } } } - + std::string schema; schema += "// Generated from " + file_name + ".proto\n\n"; if (parser.opts.include_dependence_headers) { From aade31b2634ac31598d1a8ae51b671988acb3c94 Mon Sep 17 00:00:00 2001 From: daksenik Date: Mon, 11 Jul 2016 20:27:38 +0300 Subject: [PATCH 26/35] Fixed spaces. Removed redundant == nullptr. Vectors pointers made const. --- src/idl_gen_cpp.cpp | 13 ++-- tests/monster_test_generated.h | 130 ++++++++++++++++----------------- 2 files changed, 70 insertions(+), 73 deletions(-) diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 07db13dbb..b9d5e4c93 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -458,7 +458,7 @@ class CppGenerator : public BaseGenerator { } void GenSimpleParam(std::string &code, FieldDef &field) { - code += ",\n " + GenTypeWire(field.value.type, " ", true); + code += ",\n " + GenTypeWire(field.value.type, " ", true); code += field.name + " = "; if (field.value.type.enum_def && IsScalar(field.value.type.base_type)) { auto ev = field.value.type.enum_def->ReverseLookup( @@ -742,11 +742,11 @@ class CppGenerator : public BaseGenerator { auto &field = **it; if (!field.deprecated) { if (field.value.type.base_type == BASE_TYPE_STRING) { - code += ",\n const char *"; + code += ",\n const char *"; code += field.name + " = nullptr"; } else if (field.value.type.base_type == BASE_TYPE_VECTOR) { - code += ",\n std::vector<"; + code += ",\n const std::vector<"; code += GenTypeWire(field.value.type.VectorType(), "", false); code += "> *" + field.name + " = nullptr"; } else { @@ -755,7 +755,6 @@ class CppGenerator : public BaseGenerator { } } code += ") {\n "; - code += "return Create"; code += struct_def.name; code += "(_fbb"; @@ -764,18 +763,16 @@ class CppGenerator : public BaseGenerator { auto &field = **it; if (!field.deprecated) { if (field.value.type.base_type == BASE_TYPE_STRING) { - code += ", " + field.name + " == nullptr ? 0 : "; + code += ", " + field.name + " ? 0 : "; code += "_fbb.CreateString(" + field.name + ")"; } else if (field.value.type.base_type == BASE_TYPE_VECTOR) { - code += ", " + field.name + " == nullptr ? 0 : "; + code += ", " + field.name + " ? 0 : "; code += "_fbb.CreateVector<"; code += GenTypeWire(field.value.type.VectorType(), "", false); code += ">(*" + field.name + ")"; } else code += ", " + field.name; } } - - code += ");\n}\n\n"; } } diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index 5a60eeb3c..40e7c4089 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -162,7 +162,7 @@ struct TestSimpleTableWithEnumBuilder { }; inline flatbuffers::Offset CreateTestSimpleTableWithEnum(flatbuffers::FlatBufferBuilder &_fbb, - Color color = Color_Green) { + Color color = Color_Green) { TestSimpleTableWithEnumBuilder builder_(_fbb); builder_.add_color(color); return builder_.Finish(); @@ -205,9 +205,9 @@ struct StatBuilder { }; inline flatbuffers::Offset CreateStat(flatbuffers::FlatBufferBuilder &_fbb, - flatbuffers::Offset id = 0, - int64_t val = 0, - uint16_t count = 0) { + flatbuffers::Offset id = 0, + int64_t val = 0, + uint16_t count = 0) { StatBuilder builder_(_fbb); builder_.add_val(val); builder_.add_id(id); @@ -216,10 +216,10 @@ inline flatbuffers::Offset CreateStat(flatbuffers::FlatBufferBuilder &_fbb } inline flatbuffers::Offset CreateStat(flatbuffers::FlatBufferBuilder &_fbb, - const char *id = nullptr, - int64_t val = 0, - uint16_t count = 0) { - return CreateStat(_fbb, id == nullptr ? 0 : _fbb.CreateString(id), val, count); + const char *id = nullptr, + int64_t val = 0, + uint16_t count = 0) { + return CreateStat(_fbb, id ? 0 : _fbb.CreateString(id), val, count); } /// an example documentation comment: monster object @@ -404,34 +404,34 @@ struct MonsterBuilder { }; inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, - const Vec3 *pos = 0, - int16_t mana = 150, - int16_t hp = 100, - flatbuffers::Offset name = 0, - flatbuffers::Offset> inventory = 0, - Color color = Color_Blue, - Any test_type = Any_NONE, - flatbuffers::Offset test = 0, - flatbuffers::Offset> test4 = 0, - flatbuffers::Offset>> testarrayofstring = 0, - flatbuffers::Offset>> testarrayoftables = 0, - flatbuffers::Offset enemy = 0, - flatbuffers::Offset> testnestedflatbuffer = 0, - flatbuffers::Offset testempty = 0, - bool testbool = false, - int32_t testhashs32_fnv1 = 0, - uint32_t testhashu32_fnv1 = 0, - int64_t testhashs64_fnv1 = 0, - uint64_t testhashu64_fnv1 = 0, - int32_t testhashs32_fnv1a = 0, - uint32_t testhashu32_fnv1a = 0, - int64_t testhashs64_fnv1a = 0, - uint64_t testhashu64_fnv1a = 0, - flatbuffers::Offset> testarrayofbools = 0, - float testf = 3.14159f, - float testf2 = 3.0f, - float testf3 = 0.0f, - flatbuffers::Offset>> testarrayofstring2 = 0) { + const Vec3 *pos = 0, + int16_t mana = 150, + int16_t hp = 100, + flatbuffers::Offset name = 0, + flatbuffers::Offset> inventory = 0, + Color color = Color_Blue, + Any test_type = Any_NONE, + flatbuffers::Offset test = 0, + flatbuffers::Offset> test4 = 0, + flatbuffers::Offset>> testarrayofstring = 0, + flatbuffers::Offset>> testarrayoftables = 0, + flatbuffers::Offset enemy = 0, + flatbuffers::Offset> testnestedflatbuffer = 0, + flatbuffers::Offset testempty = 0, + bool testbool = false, + int32_t testhashs32_fnv1 = 0, + uint32_t testhashu32_fnv1 = 0, + int64_t testhashs64_fnv1 = 0, + uint64_t testhashu64_fnv1 = 0, + int32_t testhashs32_fnv1a = 0, + uint32_t testhashu32_fnv1a = 0, + int64_t testhashs64_fnv1a = 0, + uint64_t testhashu64_fnv1a = 0, + flatbuffers::Offset> testarrayofbools = 0, + float testf = 3.14159f, + float testf2 = 3.0f, + float testf3 = 0.0f, + flatbuffers::Offset>> testarrayofstring2 = 0) { MonsterBuilder builder_(_fbb); builder_.add_testhashu64_fnv1a(testhashu64_fnv1a); builder_.add_testhashs64_fnv1a(testhashs64_fnv1a); @@ -465,35 +465,35 @@ inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder } inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, - const Vec3 *pos = 0, - int16_t mana = 150, - int16_t hp = 100, - const char *name = nullptr, - std::vector *inventory = nullptr, - Color color = Color_Blue, - Any test_type = Any_NONE, - flatbuffers::Offset test = 0, - std::vector *test4 = nullptr, - std::vector> *testarrayofstring = nullptr, - std::vector> *testarrayoftables = nullptr, - flatbuffers::Offset enemy = 0, - std::vector *testnestedflatbuffer = nullptr, - flatbuffers::Offset testempty = 0, - bool testbool = false, - int32_t testhashs32_fnv1 = 0, - uint32_t testhashu32_fnv1 = 0, - int64_t testhashs64_fnv1 = 0, - uint64_t testhashu64_fnv1 = 0, - int32_t testhashs32_fnv1a = 0, - uint32_t testhashu32_fnv1a = 0, - int64_t testhashs64_fnv1a = 0, - uint64_t testhashu64_fnv1a = 0, - std::vector *testarrayofbools = nullptr, - float testf = 3.14159f, - float testf2 = 3.0f, - float testf3 = 0.0f, - std::vector> *testarrayofstring2 = nullptr) { - return CreateMonster(_fbb, pos, mana, hp, name == nullptr ? 0 : _fbb.CreateString(name), inventory == nullptr ? 0 : _fbb.CreateVector(*inventory), color, test_type, test, test4 == nullptr ? 0 : _fbb.CreateVector(*test4), testarrayofstring == nullptr ? 0 : _fbb.CreateVector>(*testarrayofstring), testarrayoftables == nullptr ? 0 : _fbb.CreateVector>(*testarrayoftables), enemy, testnestedflatbuffer == nullptr ? 0 : _fbb.CreateVector(*testnestedflatbuffer), testempty, testbool, testhashs32_fnv1, testhashu32_fnv1, testhashs64_fnv1, testhashu64_fnv1, testhashs32_fnv1a, testhashu32_fnv1a, testhashs64_fnv1a, testhashu64_fnv1a, testarrayofbools == nullptr ? 0 : _fbb.CreateVector(*testarrayofbools), testf, testf2, testf3, testarrayofstring2 == nullptr ? 0 : _fbb.CreateVector>(*testarrayofstring2)); + const Vec3 *pos = 0, + int16_t mana = 150, + int16_t hp = 100, + const char *name = nullptr, + const std::vector *inventory = nullptr, + Color color = Color_Blue, + Any test_type = Any_NONE, + flatbuffers::Offset test = 0, + const std::vector *test4 = nullptr, + const std::vector> *testarrayofstring = nullptr, + const std::vector> *testarrayoftables = nullptr, + flatbuffers::Offset enemy = 0, + const std::vector *testnestedflatbuffer = nullptr, + flatbuffers::Offset testempty = 0, + bool testbool = false, + int32_t testhashs32_fnv1 = 0, + uint32_t testhashu32_fnv1 = 0, + int64_t testhashs64_fnv1 = 0, + uint64_t testhashu64_fnv1 = 0, + int32_t testhashs32_fnv1a = 0, + uint32_t testhashu32_fnv1a = 0, + int64_t testhashs64_fnv1a = 0, + uint64_t testhashu64_fnv1a = 0, + const std::vector *testarrayofbools = nullptr, + float testf = 3.14159f, + float testf2 = 3.0f, + float testf3 = 0.0f, + const std::vector> *testarrayofstring2 = nullptr) { + return CreateMonster(_fbb, pos, mana, hp, name ? 0 : _fbb.CreateString(name), inventory ? 0 : _fbb.CreateVector(*inventory), color, test_type, test, test4 ? 0 : _fbb.CreateVector(*test4), testarrayofstring ? 0 : _fbb.CreateVector>(*testarrayofstring), testarrayoftables ? 0 : _fbb.CreateVector>(*testarrayoftables), enemy, testnestedflatbuffer ? 0 : _fbb.CreateVector(*testnestedflatbuffer), testempty, testbool, testhashs32_fnv1, testhashu32_fnv1, testhashs64_fnv1, testhashu64_fnv1, testhashs32_fnv1a, testhashu32_fnv1a, testhashs64_fnv1a, testhashu64_fnv1a, testarrayofbools ? 0 : _fbb.CreateVector(*testarrayofbools), testf, testf2, testf3, testarrayofstring2 ? 0 : _fbb.CreateVector>(*testarrayofstring2)); } inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, Any type) { From f6f88e567ef7f2d282991c761ad2c8d106f6b183 Mon Sep 17 00:00:00 2001 From: Raman Date: Tue, 12 Jul 2016 19:47:53 +0200 Subject: [PATCH 27/35] Update idl_parser.cpp --- src/idl_parser.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index b758e9592..a5f325de4 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -423,6 +423,12 @@ CheckedError Parser::Next() { return NoError(); } else if (isdigit(static_cast(c)) || c == '-') { const char *start = cursor_ - 1; + if (c == '-' && *cursor_ == '0' && (cursor_[1] == 'x' || cursor_[1] == 'X')) { + ++start; + ++cursor_; + attribute_.append(&c, &c + 1); + c = '0'; + } if (c == '0' && (*cursor_ == 'x' || *cursor_ == 'X')) { cursor_++; while (isxdigit(static_cast(*cursor_))) cursor_++; From db99c1aa6462ac50b5eed23b775b43c08afff599 Mon Sep 17 00:00:00 2001 From: Raman Date: Tue, 12 Jul 2016 19:55:35 +0200 Subject: [PATCH 28/35] Update test.cpp --- tests/test.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test.cpp b/tests/test.cpp index 6f3b06299..6cac364ae 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -837,6 +837,9 @@ void ValueTest() { // Test conversion functions. TEST_EQ(FloatCompare(TestValue("{ Y:cos(rad(180)) }"), -1), true); + + // Test negative hex constant. + TEST_EQ(TestValue("{ Y:-0x80 }") == -128, true); } void EnumStringsTest() { From 3d7b1a32a361681b0ea99f4fc4e4a323d70051ea Mon Sep 17 00:00:00 2001 From: Rushabh Yapuram Date: Wed, 13 Jul 2016 07:57:36 +0530 Subject: [PATCH 29/35] Rename CONTRIBUTING to CONTRIBUTING.md --- CONTRIBUTING => CONTRIBUTING.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename CONTRIBUTING => CONTRIBUTING.md (100%) diff --git a/CONTRIBUTING b/CONTRIBUTING.md similarity index 100% rename from CONTRIBUTING rename to CONTRIBUTING.md From 49c10bc2198dadd8538be11dc1a0320cc0f91b4f Mon Sep 17 00:00:00 2001 From: BogDan Vatra Date: Wed, 13 Jul 2016 21:04:26 +0300 Subject: [PATCH 30/35] Don't crash if str is null Is useful especially when we want to create a string from another message string that might be null. --- include/flatbuffers/flatbuffers.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index e3b10f6b1..4f5a4f012 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -876,7 +876,7 @@ FLATBUFFERS_FINAL_CLASS /// @param[in] str A const pointer to a `String` struct to add to the buffer. /// @return Returns the offset in the buffer where the string starts Offset CreateString(const String *str) { - return CreateString(str->c_str(), str->Length()); + return str ? CreateString(str->c_str(), str->Length()) : 0; } /// @brief Store a string in the buffer, which can contain any binary data. From 676f0712fd23932c8d823278a1457a184765ceeb Mon Sep 17 00:00:00 2001 From: Raman Date: Thu, 14 Jul 2016 10:01:08 +0200 Subject: [PATCH 31/35] Update test.cpp --- tests/test.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test.cpp b/tests/test.cpp index 6cac364ae..06fa79af7 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -814,14 +814,14 @@ void ErrorTest() { TestError("table X { Y:byte; } root_type X; { Y:1, Y:2 }", "more than once"); } -float TestValue(const char *json) { +template T TestValue(const char *json) { flatbuffers::Parser parser; // Simple schema. - TEST_EQ(parser.Parse("table X { Y:float; } root_type X;"), true); + TEST_EQ(parser.Parse(std::string("table X { Y:" + std::string(typeid(T).name()) + "; } root_type X;").c_str()), true); TEST_EQ(parser.Parse(json), true); - auto root = flatbuffers::GetRoot(parser.builder_.GetBufferPointer()); + auto root = flatbuffers::GetRoot(parser.builder_.GetBufferPointer()); // root will point to the table, which is a 32bit vtable offset followed // by a float: TEST_EQ(sizeof(flatbuffers::soffset_t), 4); // Test assumes 32bit offsets @@ -833,13 +833,13 @@ bool FloatCompare(float a, float b) { return fabs(a - b) < 0.001; } // Additional parser testing not covered elsewhere. void ValueTest() { // Test scientific notation numbers. - TEST_EQ(FloatCompare(TestValue("{ Y:0.0314159e+2 }"), 3.14159), true); + TEST_EQ(FloatCompare(TestValue("{ Y:0.0314159e+2 }"), 3.14159), true); // Test conversion functions. - TEST_EQ(FloatCompare(TestValue("{ Y:cos(rad(180)) }"), -1), true); + TEST_EQ(FloatCompare(TestValue("{ Y:cos(rad(180)) }"), -1), true); // Test negative hex constant. - TEST_EQ(TestValue("{ Y:-0x80 }") == -128, true); + TEST_EQ(TestValue("{ Y:-0x80 }") == -128, true); } void EnumStringsTest() { From 98c7a0c169809d5ef294d6e4cd2bf43e59d64d7b Mon Sep 17 00:00:00 2001 From: Raman Date: Thu, 14 Jul 2016 10:30:06 +0200 Subject: [PATCH 32/35] Update test.cpp --- tests/test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test.cpp b/tests/test.cpp index 06fa79af7..4f817f060 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -833,7 +833,7 @@ bool FloatCompare(float a, float b) { return fabs(a - b) < 0.001; } // Additional parser testing not covered elsewhere. void ValueTest() { // Test scientific notation numbers. - TEST_EQ(FloatCompare(TestValue("{ Y:0.0314159e+2 }"), 3.14159), true); + TEST_EQ(FloatCompare(TestValue("{ Y:0.0314159e+2 }"), (float)3.14159), true); // Test conversion functions. TEST_EQ(FloatCompare(TestValue("{ Y:cos(rad(180)) }"), -1), true); From 0d5627610261ee351aabfee626c2da87100ef08a Mon Sep 17 00:00:00 2001 From: Raman Date: Thu, 14 Jul 2016 18:51:23 +0200 Subject: [PATCH 33/35] Update test.cpp --- tests/test.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test.cpp b/tests/test.cpp index 4f817f060..209ec699f 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -814,11 +814,11 @@ void ErrorTest() { TestError("table X { Y:byte; } root_type X; { Y:1, Y:2 }", "more than once"); } -template T TestValue(const char *json) { +template T TestValue(const char *json, const char *type_name) { flatbuffers::Parser parser; // Simple schema. - TEST_EQ(parser.Parse(std::string("table X { Y:" + std::string(typeid(T).name()) + "; } root_type X;").c_str()), true); + TEST_EQ(parser.Parse(std::string("table X { Y:" + std::string(type_name) + "; } root_type X;").c_str()), true); TEST_EQ(parser.Parse(json), true); auto root = flatbuffers::GetRoot(parser.builder_.GetBufferPointer()); @@ -833,13 +833,13 @@ bool FloatCompare(float a, float b) { return fabs(a - b) < 0.001; } // Additional parser testing not covered elsewhere. void ValueTest() { // Test scientific notation numbers. - TEST_EQ(FloatCompare(TestValue("{ Y:0.0314159e+2 }"), (float)3.14159), true); + TEST_EQ(FloatCompare(TestValue("{ Y:0.0314159e+2 }","float"), (float)3.14159), true); // Test conversion functions. - TEST_EQ(FloatCompare(TestValue("{ Y:cos(rad(180)) }"), -1), true); + TEST_EQ(FloatCompare(TestValue("{ Y:cos(rad(180)) }","float"), -1), true); // Test negative hex constant. - TEST_EQ(TestValue("{ Y:-0x80 }") == -128, true); + TEST_EQ(TestValue("{ Y:-0x80 }","int") == -128, true); } void EnumStringsTest() { From 4b53762cf23f28a7b8491f56b66bb7b7130827b1 Mon Sep 17 00:00:00 2001 From: BogDan Vatra Date: Thu, 14 Jul 2016 20:15:44 +0300 Subject: [PATCH 34/35] Verifier computes the buffersize, useful for streaming Close #3898 --- include/flatbuffers/flatbuffers.h | 31 ++++++++++++++++++++++++++++++- tests/test.cpp | 18 ++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index 4f5a4f012..1b7bca486 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -1216,6 +1216,9 @@ class Verifier FLATBUFFERS_FINAL_CLASS { size_t _max_tables = 1000000) : buf_(buf), end_(buf + buf_len), depth_(0), max_depth_(_max_depth), num_tables_(0), max_tables_(_max_tables) + #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE + , upper_bound_(buf) + #endif {} // Central location where any verification failures register. @@ -1223,11 +1226,20 @@ class Verifier FLATBUFFERS_FINAL_CLASS { #ifdef FLATBUFFERS_DEBUG_VERIFICATION_FAILURE assert(ok); #endif + #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE + if (!ok) + upper_bound_ = buf_; + #endif return ok; } // Verify any range within the buffer. bool Verify(const void *elem, size_t elem_len) const { + #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE + auto upper_bound = reinterpret_cast(elem) + elem_len; + if (upper_bound_ < upper_bound) + upper_bound_ = upper_bound; + #endif return Check(elem_len <= (size_t) (end_ - buf_) && elem >= buf_ && elem <= end_ - elem_len); @@ -1306,7 +1318,11 @@ class Verifier FLATBUFFERS_FINAL_CLASS { // Call T::Verify, which must be in the generated code for this type. return Verify(buf_) && reinterpret_cast(buf_ + ReadScalar(buf_))-> - Verify(*this); + Verify(*this) + #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE + && GetComputedSize() + #endif + ; } // Called at the start of a table to increase counters measuring data @@ -1325,6 +1341,16 @@ class Verifier FLATBUFFERS_FINAL_CLASS { return true; } + #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE + // Returns the message size in bytes + size_t GetComputedSize() const { + uintptr_t size = upper_bound_ - buf_; + // Align the size to uoffset_t + size = (size - 1 + sizeof(uoffset_t)) & -uintptr_t(sizeof(uoffset_t)); + return (buf_ + size > end_) ? 0 : size; + } + #endif + private: const uint8_t *buf_; const uint8_t *end_; @@ -1332,6 +1358,9 @@ class Verifier FLATBUFFERS_FINAL_CLASS { size_t max_depth_; size_t num_tables_; size_t max_tables_; + #ifdef FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE + mutable const uint8_t *upper_bound_; + #endif }; // Convenient way to bundle a buffer and its length, to pass it around diff --git a/tests/test.cpp b/tests/test.cpp index 6f3b06299..4cdfbea16 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -15,6 +15,7 @@ */ #define FLATBUFFERS_DEBUG_VERIFICATION_FAILURE 1 +#define FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE #include "flatbuffers/flatbuffers.h" #include "flatbuffers/idl.h" @@ -160,6 +161,23 @@ void AccessFlatBufferTest(const uint8_t *flatbuf, size_t length) { flatbuffers::Verifier verifier(flatbuf, length); TEST_EQ(VerifyMonsterBuffer(verifier), true); + std::vector test_buff; + test_buff.resize(length * 2); + std::memcpy(&test_buff[0], flatbuf , length); + std::memcpy(&test_buff[length], flatbuf , length); + + flatbuffers::Verifier verifierl(&test_buff[0], length - 1); + TEST_EQ(VerifyMonsterBuffer(verifierl), false); + TEST_EQ(verifierl.GetComputedSize(), 0); + + flatbuffers::Verifier verifier1(&test_buff[0], length); + TEST_EQ(VerifyMonsterBuffer(verifier1), true); + TEST_EQ(verifier1.GetComputedSize(), length); + + flatbuffers::Verifier verifier2(&test_buff[length], length); + TEST_EQ(VerifyMonsterBuffer(verifier2), true); + TEST_EQ(verifier2.GetComputedSize(), length); + TEST_EQ(strcmp(MonsterIdentifier(), "MONS"), 0); TEST_EQ(MonsterBufferHasIdentifier(flatbuf), true); TEST_EQ(strcmp(MonsterExtension(), "mon"), 0); From 3a1f776132cf9377e3a85a888e6ff2bc12c7248c Mon Sep 17 00:00:00 2001 From: BogDan Vatra Date: Thu, 14 Jul 2016 20:15:06 +0300 Subject: [PATCH 35/35] Verify everything in one shot --- include/flatbuffers/flatbuffers.h | 7 ++- include/flatbuffers/reflection_generated.h | 4 +- samples/monster_generated.h | 43 +++++++++++++------ src/idl_gen_cpp.cpp | 22 ++++++---- tests/monster_test_generated.h | 4 +- .../namespace_test1_generated.h | 2 +- .../namespace_test2_generated.h | 12 +++--- 7 files changed, 62 insertions(+), 32 deletions(-) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index 1b7bca486..01ab6db04 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -1314,7 +1314,12 @@ class Verifier FLATBUFFERS_FINAL_CLASS { } // Verify this whole buffer, starting with root type T. - template bool VerifyBuffer() { + template bool VerifyBuffer(const char *identifier) { + if (identifier && (size_t(end_ - buf_) < 2 * sizeof(flatbuffers::uoffset_t) || + !BufferHasIdentifier(buf_, identifier))) { + return false; + } + // Call T::Verify, which must be in the generated code for this type. return Verify(buf_) && reinterpret_cast(buf_ + ReadScalar(buf_))-> diff --git a/include/flatbuffers/reflection_generated.h b/include/flatbuffers/reflection_generated.h index da1124939..eb981857b 100644 --- a/include/flatbuffers/reflection_generated.h +++ b/include/flatbuffers/reflection_generated.h @@ -478,12 +478,12 @@ inline flatbuffers::Offset CreateSchema(flatbuffers::FlatBufferBuilder & inline const reflection::Schema *GetSchema(const void *buf) { return flatbuffers::GetRoot(buf); } -inline bool VerifySchemaBuffer(flatbuffers::Verifier &verifier) { return verifier.VerifyBuffer(); } - inline const char *SchemaIdentifier() { return "BFBS"; } inline bool SchemaBufferHasIdentifier(const void *buf) { return flatbuffers::BufferHasIdentifier(buf, SchemaIdentifier()); } +inline bool VerifySchemaBuffer(flatbuffers::Verifier &verifier) { return verifier.VerifyBuffer(SchemaIdentifier()); } + inline const char *SchemaExtension() { return "bfbs"; } inline void FinishSchemaBuffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset root) { fbb.Finish(root, SchemaIdentifier()); } diff --git a/samples/monster_generated.h b/samples/monster_generated.h index 1a16126a5..62a23ad82 100644 --- a/samples/monster_generated.h +++ b/samples/monster_generated.h @@ -135,15 +135,15 @@ struct MonsterBuilder { }; inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, - const Vec3 *pos = 0, - int16_t mana = 150, - int16_t hp = 100, - flatbuffers::Offset name = 0, - flatbuffers::Offset> inventory = 0, - Color color = Color_Blue, - flatbuffers::Offset>> weapons = 0, - Equipment equipped_type = Equipment_NONE, - flatbuffers::Offset equipped = 0) { + const Vec3 *pos = 0, + int16_t mana = 150, + int16_t hp = 100, + flatbuffers::Offset name = 0, + flatbuffers::Offset> inventory = 0, + Color color = Color_Blue, + flatbuffers::Offset>> weapons = 0, + Equipment equipped_type = Equipment_NONE, + flatbuffers::Offset equipped = 0) { MonsterBuilder builder_(_fbb); builder_.add_equipped(equipped); builder_.add_weapons(weapons); @@ -157,6 +157,19 @@ inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder return builder_.Finish(); } +inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, + const Vec3 *pos = 0, + int16_t mana = 150, + int16_t hp = 100, + const char *name = nullptr, + const std::vector *inventory = nullptr, + Color color = Color_Blue, + const std::vector> *weapons = nullptr, + Equipment equipped_type = Equipment_NONE, + flatbuffers::Offset equipped = 0) { + return CreateMonster(_fbb, pos, mana, hp, name ? 0 : _fbb.CreateString(name), inventory ? 0 : _fbb.CreateVector(*inventory), color, weapons ? 0 : _fbb.CreateVector>(*weapons), equipped_type, equipped); +} + struct Weapon FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { enum { VT_NAME = 4, @@ -189,14 +202,20 @@ struct WeaponBuilder { }; inline flatbuffers::Offset CreateWeapon(flatbuffers::FlatBufferBuilder &_fbb, - flatbuffers::Offset name = 0, - int16_t damage = 0) { + flatbuffers::Offset name = 0, + int16_t damage = 0) { WeaponBuilder builder_(_fbb); builder_.add_name(name); builder_.add_damage(damage); return builder_.Finish(); } +inline flatbuffers::Offset CreateWeapon(flatbuffers::FlatBufferBuilder &_fbb, + const char *name = nullptr, + int16_t damage = 0) { + return CreateWeapon(_fbb, name ? 0 : _fbb.CreateString(name), damage); +} + inline bool VerifyEquipment(flatbuffers::Verifier &verifier, const void *union_obj, Equipment type) { switch (type) { case Equipment_NONE: return true; @@ -209,7 +228,7 @@ inline const MyGame::Sample::Monster *GetMonster(const void *buf) { return flatb inline Monster *GetMutableMonster(void *buf) { return flatbuffers::GetMutableRoot(buf); } -inline bool VerifyMonsterBuffer(flatbuffers::Verifier &verifier) { return verifier.VerifyBuffer(); } +inline bool VerifyMonsterBuffer(flatbuffers::Verifier &verifier) { return verifier.VerifyBuffer(nullptr); } inline void FinishMonsterBuffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset root) { fbb.Finish(root); } diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 463f3a0be..e12e8a34a 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -158,14 +158,6 @@ class CppGenerator : public BaseGenerator { code += name + ">(buf); }\n\n"; } - // The root verifier: - code += "inline bool Verify"; - code += name; - code += - "Buffer(flatbuffers::Verifier &verifier) { " - "return verifier.VerifyBuffer<"; - code += cpp_qualified_name + ">(); }\n\n"; - if (parser_.file_identifier_.length()) { // Return the identifier code += "inline const char *" + name; @@ -179,6 +171,20 @@ class CppGenerator : public BaseGenerator { code += name + "Identifier()); }\n\n"; } + // The root verifier: + code += "inline bool Verify"; + code += name; + code += + "Buffer(flatbuffers::Verifier &verifier) { " + "return verifier.VerifyBuffer<"; + code += cpp_qualified_name + ">("; + if (parser_.file_identifier_.length()) + code += name + "Identifier()"; + else + code += "nullptr"; + code += "); }\n\n"; + + if (parser_.file_extension_.length()) { // Return the extension code += "inline const char *" + name; diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index 40e7c4089..9f3d4de0f 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -510,12 +510,12 @@ inline const MyGame::Example::Monster *GetMonster(const void *buf) { return flat inline Monster *GetMutableMonster(void *buf) { return flatbuffers::GetMutableRoot(buf); } -inline bool VerifyMonsterBuffer(flatbuffers::Verifier &verifier) { return verifier.VerifyBuffer(); } - inline const char *MonsterIdentifier() { return "MONS"; } inline bool MonsterBufferHasIdentifier(const void *buf) { return flatbuffers::BufferHasIdentifier(buf, MonsterIdentifier()); } +inline bool VerifyMonsterBuffer(flatbuffers::Verifier &verifier) { return verifier.VerifyBuffer(MonsterIdentifier()); } + inline const char *MonsterExtension() { return "mon"; } inline void FinishMonsterBuffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset root) { fbb.Finish(root, MonsterIdentifier()); } diff --git a/tests/namespace_test/namespace_test1_generated.h b/tests/namespace_test/namespace_test1_generated.h index 9e10eb4c1..23a0964fb 100644 --- a/tests/namespace_test/namespace_test1_generated.h +++ b/tests/namespace_test/namespace_test1_generated.h @@ -69,7 +69,7 @@ struct TableInNestedNSBuilder { }; inline flatbuffers::Offset CreateTableInNestedNS(flatbuffers::FlatBufferBuilder &_fbb, - int32_t foo = 0) { + int32_t foo = 0) { TableInNestedNSBuilder builder_(_fbb); builder_.add_foo(foo); return builder_.Finish(); diff --git a/tests/namespace_test/namespace_test2_generated.h b/tests/namespace_test/namespace_test2_generated.h index 8e6b3ada8..885eae29b 100644 --- a/tests/namespace_test/namespace_test2_generated.h +++ b/tests/namespace_test/namespace_test2_generated.h @@ -60,9 +60,9 @@ struct TableInFirstNSBuilder { }; inline flatbuffers::Offset CreateTableInFirstNS(flatbuffers::FlatBufferBuilder &_fbb, - flatbuffers::Offset foo_table = 0, - NamespaceA::NamespaceB::EnumInNestedNS foo_enum = NamespaceA::NamespaceB::EnumInNestedNS_A, - const NamespaceA::NamespaceB::StructInNestedNS *foo_struct = 0) { + flatbuffers::Offset foo_table = 0, + NamespaceA::NamespaceB::EnumInNestedNS foo_enum = NamespaceA::NamespaceB::EnumInNestedNS_A, + const NamespaceA::NamespaceB::StructInNestedNS *foo_struct = 0) { TableInFirstNSBuilder builder_(_fbb); builder_.add_foo_struct(foo_struct); builder_.add_foo_table(foo_table); @@ -107,8 +107,8 @@ struct TableInCBuilder { }; inline flatbuffers::Offset CreateTableInC(flatbuffers::FlatBufferBuilder &_fbb, - flatbuffers::Offset refer_to_a1 = 0, - flatbuffers::Offset refer_to_a2 = 0) { + flatbuffers::Offset refer_to_a1 = 0, + flatbuffers::Offset refer_to_a2 = 0) { TableInCBuilder builder_(_fbb); builder_.add_refer_to_a2(refer_to_a2); builder_.add_refer_to_a1(refer_to_a1); @@ -146,7 +146,7 @@ struct SecondTableInABuilder { }; inline flatbuffers::Offset CreateSecondTableInA(flatbuffers::FlatBufferBuilder &_fbb, - flatbuffers::Offset refer_to_c = 0) { + flatbuffers::Offset refer_to_c = 0) { SecondTableInABuilder builder_(_fbb); builder_.add_refer_to_c(refer_to_c); return builder_.Finish();