From 7179a5a8baf478b09bb906ed0be030ade87f02cd Mon Sep 17 00:00:00 2001 From: Derek Bailey Date: Mon, 1 Jun 2020 08:40:55 -0700 Subject: [PATCH] General Codebase clean up (#5939) * Fixed refractoring issue in reflection/generate_code.sh. Also, mv deletes the original file, so I don't need to clean it up manually in that case. * Ensuring test/generate_code.sh was ran * Fixed Dart Tests by removing code-gen for included files. * General cleanup of codebase. --- include/flatbuffers/code_generators.h | 6 ++---- include/flatbuffers/flatbuffers.h | 5 +++-- include/flatbuffers/idl.h | 4 +--- include/flatbuffers/reflection.h | 3 +-- src/idl_gen_js_ts.cpp | 25 +++++++++++++------------ src/idl_gen_rust.cpp | 6 +++--- src/idl_parser.cpp | 7 ++----- src/reflection.cpp | 20 +++++++++----------- 8 files changed, 34 insertions(+), 42 deletions(-) diff --git a/include/flatbuffers/code_generators.h b/include/flatbuffers/code_generators.h index a411c83d4..d64ab03c1 100644 --- a/include/flatbuffers/code_generators.h +++ b/include/flatbuffers/code_generators.h @@ -75,10 +75,8 @@ class CodeWriter { void DecrementIdentLevel() { if (cur_ident_lvl_) cur_ident_lvl_--; } - - void SetPadding(const std::string &padding) { - pad_ = padding; - } + + void SetPadding(const std::string &padding) { pad_ = padding; } private: std::map value_map_; diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index f596e4268..7c882127a 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -587,7 +587,7 @@ static inline const char *GetCstring(const String *str) { static inline flatbuffers::string_view GetStringView(const String *str) { return str ? str->string_view() : flatbuffers::string_view(); } -#endif // FLATBUFFERS_HAS_STRING_VIEW +#endif // FLATBUFFERS_HAS_STRING_VIEW // Allocator interface. This is flatbuffers-specific and meant only for // `vector_downward` usage. @@ -1803,7 +1803,8 @@ class FlatBufferBuilder { return a.KeyCompareLessThan(&b); } - FLATBUFFERS_DELETE_FUNC(StructKeyComparator &operator=(const StructKeyComparator &)) + FLATBUFFERS_DELETE_FUNC( + StructKeyComparator &operator=(const StructKeyComparator &)) }; /// @endcond diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index d73c92db7..8c02f2827 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -410,9 +410,7 @@ struct EnumDef : public Definition { size_t size() const { return vals.vec.size(); } - const std::vector &Vals() const { - return vals.vec; - } + const std::vector &Vals() const { return vals.vec; } const EnumVal *Lookup(const std::string &enum_name) const { return vals.Lookup(enum_name); diff --git a/include/flatbuffers/reflection.h b/include/flatbuffers/reflection.h index 1c49234fc..160625deb 100644 --- a/include/flatbuffers/reflection.h +++ b/include/flatbuffers/reflection.h @@ -469,8 +469,7 @@ Offset CopyTable(FlatBufferBuilder &fbb, // buf should point to the start of flatbuffer data. // length specifies the size of the flatbuffer data. bool Verify(const reflection::Schema &schema, const reflection::Object &root, - const uint8_t *buf, size_t length, - uoffset_t max_depth = 64, + const uint8_t *buf, size_t length, uoffset_t max_depth = 64, uoffset_t max_tables = 1000000); } // namespace flatbuffers diff --git a/src/idl_gen_js_ts.cpp b/src/idl_gen_js_ts.cpp index a32e03583..b09de5a5e 100644 --- a/src/idl_gen_js_ts.cpp +++ b/src/idl_gen_js_ts.cpp @@ -15,10 +15,10 @@ */ // independent from idl_parser, since this code is not needed for most clients +#include #include #include #include -#include #include "flatbuffers/code_generators.h" #include "flatbuffers/flatbuffers.h" @@ -1224,14 +1224,15 @@ class JsTsGenerator : public BaseGenerator { } bool CanCreateFactoryMethod(const StructDef &struct_def) { - // to preserve backwards compatibility, we allow the first field to be a struct - return struct_def.fields.vec.size() < 2 || std::all_of( - std::begin(struct_def.fields.vec) + 1, - std::end(struct_def.fields.vec), - [](const FieldDef *f) -> bool { - FLATBUFFERS_ASSERT(f != nullptr); - return f->value.type.base_type != BASE_TYPE_STRUCT; - }); + // to preserve backwards compatibility, we allow the first field to be a + // struct + return struct_def.fields.vec.size() < 2 || + std::all_of(std::begin(struct_def.fields.vec) + 1, + std::end(struct_def.fields.vec), + [](const FieldDef *f) -> bool { + FLATBUFFERS_ASSERT(f != nullptr); + return f->value.type.base_type != BASE_TYPE_STRUCT; + }); } // Generate an accessor struct with constructor for a flatbuffers struct. @@ -1876,7 +1877,7 @@ class JsTsGenerator : public BaseGenerator { std::string paramDoc = GenTypeAnnotation(kParam, "flatbuffers.Builder", "builder"); for (auto it = struct_def.fields.vec.begin(); - it != struct_def.fields.vec.end(); ++it) { + it != struct_def.fields.vec.end(); ++it) { const auto &field = **it; if (field.deprecated) continue; paramDoc += @@ -1896,7 +1897,7 @@ class JsTsGenerator : public BaseGenerator { code += " = function(builder"; } for (auto it = struct_def.fields.vec.begin(); - it != struct_def.fields.vec.end(); ++it) { + it != struct_def.fields.vec.end(); ++it) { const auto &field = **it; if (field.deprecated) continue; @@ -1920,7 +1921,7 @@ class JsTsGenerator : public BaseGenerator { std::string methodPrefix = lang_.language == IDLOptions::kTs ? struct_def.name : object_name; for (auto it = struct_def.fields.vec.begin(); - it != struct_def.fields.vec.end(); ++it) { + it != struct_def.fields.vec.end(); ++it) { const auto &field = **it; if (field.deprecated) continue; diff --git a/src/idl_gen_rust.cpp b/src/idl_gen_rust.cpp index 0369188d1..45d5faff1 100644 --- a/src/idl_gen_rust.cpp +++ b/src/idl_gen_rust.cpp @@ -1358,8 +1358,8 @@ class RustGenerator : public BaseGenerator { code_ += " Some({{U_ELEMENT_TABLE_TYPE}}::init_from_table(u))"; } else { code_ += - " self.{{FIELD_NAME}}().map(|u| " - "{{U_ELEMENT_TABLE_TYPE}}::init_from_table(u))"; + " self.{{FIELD_NAME}}().map(|u| " + "{{U_ELEMENT_TABLE_TYPE}}::init_from_table(u))"; } code_ += " } else {"; code_ += " None"; @@ -1788,7 +1788,7 @@ class RustGenerator : public BaseGenerator { code_ += ""; if (!parser_.opts.generate_all) { for (auto it = parser_.included_files_.begin(); - it != parser_.included_files_.end(); ++it) { + it != parser_.included_files_.end(); ++it) { if (it->second.empty()) continue; auto noext = flatbuffers::StripExtension(it->second); auto basename = flatbuffers::StripPath(noext); diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index 6cbe5c7cc..d56a20920 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -1704,9 +1704,7 @@ CheckedError Parser::ParseSingleValue(const std::string *name, Value &e, const auto is_tok_string = (token_ == kTokenStringConstant); // First see if this could be a conversion function: - if (is_tok_ident && *cursor_ == '(') { - return ParseFunction(name, e); - } + if (is_tok_ident && *cursor_ == '(') { return ParseFunction(name, e); } // clang-format off auto match = false; @@ -1757,8 +1755,7 @@ CheckedError Parser::ParseSingleValue(const std::string *name, Value &e, if (!match && is_tok_string && IsScalar(in_type)) { // Strip trailing whitespaces from attribute_. auto last_non_ws = attribute_.find_last_not_of(' '); - if (std::string::npos != last_non_ws) - attribute_.resize(last_non_ws + 1); + if (std::string::npos != last_non_ws) attribute_.resize(last_non_ws + 1); if (IsFloat(e.type.base_type)) { // The functions strtod() and strtof() accept both 'nan' and // 'nan(number)' literals. While 'nan(number)' is rejected by the parser diff --git a/src/reflection.cpp b/src/reflection.cpp index 77ffa79f2..fcb225b54 100644 --- a/src/reflection.cpp +++ b/src/reflection.cpp @@ -396,17 +396,17 @@ Offset CopyTable(FlatBufferBuilder &fbb, case reflection::Obj: { auto &subobjectdef = *schema.objects()->Get(fielddef.type()->index()); if (!subobjectdef.is_struct()) { - offset = - CopyTable(fbb, schema, subobjectdef, - *GetFieldT(table, fielddef), use_string_pooling).o; + offset = CopyTable(fbb, schema, subobjectdef, + *GetFieldT(table, fielddef), use_string_pooling) + .o; } break; } case reflection::Union: { auto &subobjectdef = GetUnionType(schema, objectdef, fielddef, table); - offset = - CopyTable(fbb, schema, subobjectdef, *GetFieldT(table, fielddef), - use_string_pooling).o; + offset = CopyTable(fbb, schema, subobjectdef, + *GetFieldT(table, fielddef), use_string_pooling) + .o; break; } case reflection::Vector: { @@ -433,9 +433,8 @@ Offset CopyTable(FlatBufferBuilder &fbb, if (!elemobjectdef->is_struct()) { std::vector> elements(vec->size()); for (uoffset_t i = 0; i < vec->size(); i++) { - elements[i] = - CopyTable(fbb, schema, *elemobjectdef, *vec->Get(i), - use_string_pooling); + elements[i] = CopyTable(fbb, schema, *elemobjectdef, + *vec->Get(i), use_string_pooling); } offset = fbb.CreateVector(elements).o; break; @@ -705,8 +704,7 @@ bool VerifyObject(flatbuffers::Verifier &v, const reflection::Schema &schema, } bool Verify(const reflection::Schema &schema, const reflection::Object &root, - const uint8_t *buf, size_t length, - uoffset_t max_depth /*= 64*/, + const uint8_t *buf, size_t length, uoffset_t max_depth /*= 64*/, uoffset_t max_tables /*= 1000000*/) { Verifier v(buf, length, max_depth, max_tables); return VerifyObject(v, schema, root, flatbuffers::GetAnyRoot(buf), true);