diff --git a/include/flatbuffers/flatbuffer_builder.h b/include/flatbuffers/flatbuffer_builder.h index b9015d85020..caf9a3d1566 100644 --- a/include/flatbuffers/flatbuffer_builder.h +++ b/include/flatbuffers/flatbuffer_builder.h @@ -40,8 +40,8 @@ namespace flatbuffers { // Converts a Field ID to a virtual table offset. inline voffset_t FieldIndexToOffset(voffset_t field_id) { // Should correspond to what EndTable() below builds up. - const int fixed_fields = 2; // Vtable size and Object Size. - return static_cast((field_id + fixed_fields) * sizeof(voffset_t)); + const voffset_t fixed_fields = 2 * sizeof(voffset_t); // Vtable size and Object Size. + return fixed_fields + field_id * sizeof(voffset_t); } template> @@ -360,7 +360,7 @@ class FlatBufferBuilder { FLATBUFFERS_ASSERT(nested); // Write the vtable offset, which is the start of any Table. // We fill its value later. - auto vtableoffsetloc = PushElement(0); + const uoffset_t vtableoffsetloc = PushElement(0); // Write a vtable, which consists entirely of voffset_t elements. // It starts with the number of offsets, followed by a type id, followed // by the offsets themselves. In reverse: @@ -400,7 +400,7 @@ class FlatBufferBuilder { auto vt2_size = ReadScalar(vt2); if (vt1_size != vt2_size || 0 != memcmp(vt2, vt1, vt1_size)) continue; vt_use = *vt_offset_ptr; - buf_.pop(GetSize() - vtableoffsetloc); + buf_.pop(GetSize() - static_cast(vtableoffsetloc)); break; } } @@ -525,7 +525,7 @@ class FlatBufferBuilder { FLATBUFFERS_ASSERT(FLATBUFFERS_GENERAL_HEAP_ALLOC_OK); if (!string_pool) string_pool = new StringOffsetMap(StringOffsetCompare(buf_)); - auto size_before_string = buf_.size(); + const size_t size_before_string = buf_.size(); // Must first serialize the string, since the set is all offsets into // buffer. auto off = CreateString(str, len); diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 67f228a250b..1b33016e2bb 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include "flatbuffers/base.h" #include "flatbuffers/code_generators.h" @@ -965,9 +966,9 @@ class CppGenerator : public BaseGenerator { std::string GetUnionElement(const EnumVal &ev, bool native_type, const IDLOptions &opts) { if (ev.union_type.base_type == BASE_TYPE_STRUCT) { - auto name = ev.union_type.struct_def->name; + std::string name = ev.union_type.struct_def->name; if (native_type) { - name = NativeName(name, ev.union_type.struct_def, opts); + name = NativeName(std::move(name), ev.union_type.struct_def, opts); } return WrapInNameSpace(ev.union_type.struct_def->defined_namespace, name); } else if (IsString(ev.union_type)) { @@ -985,8 +986,8 @@ class CppGenerator : public BaseGenerator { } std::string UnionVectorVerifySignature(const EnumDef &enum_def) { - auto name = Name(enum_def); - auto type = opts_.scoped_enums ? name : "uint8_t"; + const std::string name = Name(enum_def); + const std::string & type = opts_.scoped_enums ? name : "uint8_t"; return "bool Verify" + name + "Vector" + "(::flatbuffers::Verifier &verifier, " + "const ::flatbuffers::Vector<::flatbuffers::Offset> " @@ -1806,7 +1807,7 @@ class CppGenerator : public BaseGenerator { field.value.type.element != BASE_TYPE_UTYPE)) { auto type = GenTypeNative(field.value.type, false, field); auto cpp_type = field.attributes.Lookup("cpp_type"); - auto full_type = + const std::string & full_type = (cpp_type ? (IsVector(field.value.type) ? "std::vector<" + @@ -1953,7 +1954,7 @@ class CppGenerator : public BaseGenerator { if (!initializer_list.empty()) { initializer_list += ",\n "; } const auto cpp_type = field->attributes.Lookup("cpp_type"); const auto cpp_ptr_type = field->attributes.Lookup("cpp_ptr_type"); - auto type_name = (cpp_type) ? cpp_type->constant + const std::string & type_name = (cpp_type) ? cpp_type->constant : GenTypeNative(type, /*invector*/ false, *field, /*forcopy*/ true); const bool is_ptr = !(IsStruct(type) && field->native_inline) || @@ -1975,7 +1976,7 @@ class CppGenerator : public BaseGenerator { if (vec_type.base_type == BASE_TYPE_UTYPE) continue; const auto cpp_type = field->attributes.Lookup("cpp_type"); const auto cpp_ptr_type = field->attributes.Lookup("cpp_ptr_type"); - const auto type_name = (cpp_type) + const std::string & type_name = (cpp_type) ? cpp_type->constant : GenTypeNative(vec_type, /*invector*/ true, *field, /*forcopy*/ true); @@ -2834,7 +2835,7 @@ class CppGenerator : public BaseGenerator { // Generate code to do force_align for the vector. if (align > 1) { const auto vtype = field.value.type.VectorType(); - const auto type = IsStruct(vtype) ? WrapInNameSpace(*vtype.struct_def) + const std::string & type = IsStruct(vtype) ? WrapInNameSpace(*vtype.struct_def) : GenTypeWire(vtype, "", false); return "_fbb.ForceVectorAlignment(" + field_size + ", sizeof(" + type + "), " + std::to_string(static_cast(align)) + ");"; @@ -3356,7 +3357,7 @@ class CppGenerator : public BaseGenerator { } case BASE_TYPE_UTYPE: { value = StripUnionType(value); - auto type = opts_.scoped_enums ? Name(*field.value.type.enum_def) + const std::string & type = opts_.scoped_enums ? Name(*field.value.type.enum_def) : "uint8_t"; auto enum_value = "__va->_" + value + "[i].type"; if (!opts_.scoped_enums) @@ -3423,7 +3424,7 @@ class CppGenerator : public BaseGenerator { } } else { // _o->field ? CreateT(_fbb, _o->field.get(), _rehasher); - const auto type = field.value.type.struct_def->name; + const std::string & type = field.value.type.struct_def->name; code += value + " ? Create" + type; code += "(_fbb, " + value; if (!field.native_inline) code += GenPtrGet(field); @@ -3809,7 +3810,7 @@ class CppGenerator : public BaseGenerator { const auto field_type = GenTypeGet(type, " ", is_array ? "" : "const ", is_array ? "" : " &", true); auto member = Name(*field) + "_"; - auto value = + const std::string & value = is_scalar ? "::flatbuffers::EndianScalar(" + member + ")" : member; code_.SetValue("FIELD_NAME", Name(*field)); @@ -3918,7 +3919,7 @@ bool GenerateCPP(const Parser &parser, const std::string &path, cpp::IDLOptionsCpp opts(parser.opts); // The '--cpp_std' argument could be extended (like ASAN): // Example: "flatc --cpp_std c++17:option1:option2". - auto cpp_std = !opts.cpp_std.empty() ? opts.cpp_std : "C++11"; + std::string cpp_std = !opts.cpp_std.empty() ? opts.cpp_std : "C++11"; std::transform(cpp_std.begin(), cpp_std.end(), cpp_std.begin(), CharToUpper); if (cpp_std == "C++0X") { opts.g_cpp_std = cpp::CPP_STD_X0; diff --git a/tests/monster_test.cpp b/tests/monster_test.cpp index f081dd92f51..ed6d55bf687 100644 --- a/tests/monster_test.cpp +++ b/tests/monster_test.cpp @@ -422,7 +422,7 @@ void MutateFlatBuffersTest(uint8_t *flatbuf, std::size_t length) { // Mutate structs. auto pos = monster->mutable_pos(); - auto test3 = pos->mutable_test3(); // Struct inside a struct. + auto & test3 = pos->mutable_test3(); // Struct inside a struct. test3.mutate_a(50); // Struct fields never fail. TEST_EQ(test3.a(), 50); test3.mutate_a(10); @@ -508,7 +508,7 @@ void ObjectFlatBuffersTest(uint8_t *flatbuf) { CheckMonsterObject(monster2.get()); // Test object copy. - auto monster3 = *monster2; + MonsterT monster3 = *monster2; flatbuffers::FlatBufferBuilder fbb3; fbb3.Finish(CreateMonster(fbb3, &monster3, &rehasher), MonsterIdentifier()); const auto len3 = fbb3.GetSize();