From 8d0d5df67909e7c992618cc87b66dea17502da87 Mon Sep 17 00:00:00 2001 From: Sven Konietzny Date: Fri, 14 Jun 2019 08:06:46 +0200 Subject: [PATCH] Optimizations --- src/idl_gen_cpp.cpp | 44 +++++++++++++++++------------------------ src/idl_gen_general.cpp | 2 +- src/idl_parser.cpp | 5 +++-- 3 files changed, 22 insertions(+), 29 deletions(-) diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 4238a26f757c..1325e067dc90 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -307,31 +307,13 @@ class CppGenerator : public BaseGenerator { } } - // Check if enum arrays are used without specifying --scoped-enums - if (!parser_.opts.scoped_enums) { - for (auto it_s = parser_.structs_.vec.begin(); - it_s != parser_.structs_.vec.end(); ++it_s) { - const auto &struct_def = **it_s; - for (auto it_f = struct_def.fields.vec.begin(); - it_f != struct_def.fields.vec.end(); ++it_f) { - const auto &field_def = **it_f; - const auto &field_type = field_def.value.type; - - if (IsArray(field_type) && IsEnum(field_type.VectorType())) { - printf("--scoped-enums must be enabled to use enum arrays\n"); - return false; - } - } - } - } - // Generate code for all structs, then all tables. for (auto it = parser_.structs_.vec.begin(); it != parser_.structs_.vec.end(); ++it) { const auto &struct_def = **it; if (struct_def.fixed && !struct_def.generated) { SetNameSpace(struct_def.defined_namespace); - GenStruct(struct_def); + if (!GenStruct(struct_def)) return false; } } for (auto it = parser_.structs_.vec.begin(); @@ -2724,7 +2706,7 @@ class CppGenerator : public BaseGenerator { } // Generate an accessor struct with constructor for a flatbuffers struct. - void GenStruct(const StructDef &struct_def) { + bool GenStruct(const StructDef &struct_def) { // Generate an accessor struct, with private variables of the form: // type name_; // Generates manual padding and alignment. @@ -2743,13 +2725,21 @@ class CppGenerator : public BaseGenerator { for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { const auto &field = **it; - code_.SetValue("FIELD_TYPE", - GenTypeGet(field.value.type, " ", "", " ", false)); + const auto &field_type = field.value.type; + + // Check if enum arrays are used without specifying --scoped-enums + if (!parser_.opts.scoped_enums && IsArray(field_type) && + IsEnum(field_type.VectorType())) { + printf("--scoped-enums must be enabled to use enum arrays\n"); + return false; + } + + code_.SetValue("FIELD_TYPE", GenTypeGet(field_type, " ", "", " ", false)); code_.SetValue("FIELD_NAME", Name(field)); - code_.SetValue( - "ARRAY", IsArray(field.value.type) - ? "[" + NumToString(field.value.type.fixed_length) + "]" - : ""); + code_.SetValue("ARRAY", + IsArray(field_type) + ? "[" + NumToString(field_type.fixed_length) + "]" + : ""); code_ += (" {{FIELD_TYPE}}{{FIELD_NAME}}_{{ARRAY}};"); if (field.padding) { @@ -2925,6 +2915,8 @@ class CppGenerator : public BaseGenerator { code_ += "FLATBUFFERS_STRUCT_END({{STRUCT_NAME}}, {{STRUCT_BYTE_SIZE}});"; if (parser_.opts.gen_compare) GenCompareOperator(struct_def, "()"); code_ += ""; + + return true; } // Set up the correct namespace. Only open a namespace if the existing one is diff --git a/src/idl_gen_general.cpp b/src/idl_gen_general.cpp index e641223b9bfb..18059e17d726 100644 --- a/src/idl_gen_general.cpp +++ b/src/idl_gen_general.cpp @@ -1280,7 +1280,7 @@ class GeneralGenerator : public BaseGenerator { // Generate mutators for scalar fields or vectors of scalars. if (parser_.opts.mutable_buffer) { auto is_series = (IsSeries(field.value.type)); - auto underlying_type = + const auto &underlying_type = is_series ? field.value.type.VectorType() : field.value.type; // Boolean parameters have to be explicitly converted to byte // representation. diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index 61b777ef834f..a5f3860c8d8b 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -1285,7 +1285,8 @@ CheckedError Parser::ParseArray(Value &array) { uoffset_t count = 0; auto err = ParseVectorDelimiters(count, [&](uoffset_t &) -> CheckedError { - Value val; + vector_emplace_back(&stack, Value()); + auto &val = stack.back(); val.type = type; if (IsStruct(type)) { ECHECK(ParseTable(*val.type.struct_def, &val.constant, nullptr)); @@ -1299,7 +1300,7 @@ CheckedError Parser::ParseArray(Value &array) { if (length != count) return Error("Fixed-length array size is incorrect."); - for (auto it = stack.rbegin(); it != stack.rend(); it++) { + for (auto it = stack.rbegin(); it != stack.rend(); ++it) { auto &val = *it; // clang-format off switch (val.type.base_type) {