mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-22 16:58:52 +00:00
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.
This commit is contained in:
@@ -75,10 +75,8 @@ class CodeWriter {
|
|||||||
void DecrementIdentLevel() {
|
void DecrementIdentLevel() {
|
||||||
if (cur_ident_lvl_) cur_ident_lvl_--;
|
if (cur_ident_lvl_) cur_ident_lvl_--;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetPadding(const std::string &padding) {
|
void SetPadding(const std::string &padding) { pad_ = padding; }
|
||||||
pad_ = padding;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::map<std::string, std::string> value_map_;
|
std::map<std::string, std::string> value_map_;
|
||||||
|
|||||||
@@ -587,7 +587,7 @@ static inline const char *GetCstring(const String *str) {
|
|||||||
static inline flatbuffers::string_view GetStringView(const String *str) {
|
static inline flatbuffers::string_view GetStringView(const String *str) {
|
||||||
return str ? str->string_view() : flatbuffers::string_view();
|
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
|
// Allocator interface. This is flatbuffers-specific and meant only for
|
||||||
// `vector_downward` usage.
|
// `vector_downward` usage.
|
||||||
@@ -1803,7 +1803,8 @@ class FlatBufferBuilder {
|
|||||||
return a.KeyCompareLessThan(&b);
|
return a.KeyCompareLessThan(&b);
|
||||||
}
|
}
|
||||||
|
|
||||||
FLATBUFFERS_DELETE_FUNC(StructKeyComparator &operator=(const StructKeyComparator &))
|
FLATBUFFERS_DELETE_FUNC(
|
||||||
|
StructKeyComparator &operator=(const StructKeyComparator &))
|
||||||
};
|
};
|
||||||
/// @endcond
|
/// @endcond
|
||||||
|
|
||||||
|
|||||||
@@ -410,9 +410,7 @@ struct EnumDef : public Definition {
|
|||||||
|
|
||||||
size_t size() const { return vals.vec.size(); }
|
size_t size() const { return vals.vec.size(); }
|
||||||
|
|
||||||
const std::vector<EnumVal *> &Vals() const {
|
const std::vector<EnumVal *> &Vals() const { return vals.vec; }
|
||||||
return vals.vec;
|
|
||||||
}
|
|
||||||
|
|
||||||
const EnumVal *Lookup(const std::string &enum_name) const {
|
const EnumVal *Lookup(const std::string &enum_name) const {
|
||||||
return vals.Lookup(enum_name);
|
return vals.Lookup(enum_name);
|
||||||
|
|||||||
@@ -469,8 +469,7 @@ Offset<const Table *> CopyTable(FlatBufferBuilder &fbb,
|
|||||||
// buf should point to the start of flatbuffer data.
|
// buf should point to the start of flatbuffer data.
|
||||||
// length specifies the size of the flatbuffer data.
|
// length specifies the size of the flatbuffer data.
|
||||||
bool Verify(const reflection::Schema &schema, const reflection::Object &root,
|
bool Verify(const reflection::Schema &schema, const reflection::Object &root,
|
||||||
const uint8_t *buf, size_t length,
|
const uint8_t *buf, size_t length, uoffset_t max_depth = 64,
|
||||||
uoffset_t max_depth = 64,
|
|
||||||
uoffset_t max_tables = 1000000);
|
uoffset_t max_tables = 1000000);
|
||||||
|
|
||||||
} // namespace flatbuffers
|
} // namespace flatbuffers
|
||||||
|
|||||||
@@ -15,10 +15,10 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// independent from idl_parser, since this code is not needed for most clients
|
// independent from idl_parser, since this code is not needed for most clients
|
||||||
|
#include <algorithm>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
#include "flatbuffers/code_generators.h"
|
#include "flatbuffers/code_generators.h"
|
||||||
#include "flatbuffers/flatbuffers.h"
|
#include "flatbuffers/flatbuffers.h"
|
||||||
@@ -1224,14 +1224,15 @@ class JsTsGenerator : public BaseGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool CanCreateFactoryMethod(const StructDef &struct_def) {
|
bool CanCreateFactoryMethod(const StructDef &struct_def) {
|
||||||
// to preserve backwards compatibility, we allow the first field to be a struct
|
// to preserve backwards compatibility, we allow the first field to be a
|
||||||
return struct_def.fields.vec.size() < 2 || std::all_of(
|
// struct
|
||||||
std::begin(struct_def.fields.vec) + 1,
|
return struct_def.fields.vec.size() < 2 ||
|
||||||
std::end(struct_def.fields.vec),
|
std::all_of(std::begin(struct_def.fields.vec) + 1,
|
||||||
[](const FieldDef *f) -> bool {
|
std::end(struct_def.fields.vec),
|
||||||
FLATBUFFERS_ASSERT(f != nullptr);
|
[](const FieldDef *f) -> bool {
|
||||||
return f->value.type.base_type != BASE_TYPE_STRUCT;
|
FLATBUFFERS_ASSERT(f != nullptr);
|
||||||
});
|
return f->value.type.base_type != BASE_TYPE_STRUCT;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate an accessor struct with constructor for a flatbuffers struct.
|
// Generate an accessor struct with constructor for a flatbuffers struct.
|
||||||
@@ -1876,7 +1877,7 @@ class JsTsGenerator : public BaseGenerator {
|
|||||||
std::string paramDoc =
|
std::string paramDoc =
|
||||||
GenTypeAnnotation(kParam, "flatbuffers.Builder", "builder");
|
GenTypeAnnotation(kParam, "flatbuffers.Builder", "builder");
|
||||||
for (auto it = struct_def.fields.vec.begin();
|
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;
|
const auto &field = **it;
|
||||||
if (field.deprecated) continue;
|
if (field.deprecated) continue;
|
||||||
paramDoc +=
|
paramDoc +=
|
||||||
@@ -1896,7 +1897,7 @@ class JsTsGenerator : public BaseGenerator {
|
|||||||
code += " = function(builder";
|
code += " = function(builder";
|
||||||
}
|
}
|
||||||
for (auto it = struct_def.fields.vec.begin();
|
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;
|
const auto &field = **it;
|
||||||
if (field.deprecated) continue;
|
if (field.deprecated) continue;
|
||||||
|
|
||||||
@@ -1920,7 +1921,7 @@ class JsTsGenerator : public BaseGenerator {
|
|||||||
std::string methodPrefix =
|
std::string methodPrefix =
|
||||||
lang_.language == IDLOptions::kTs ? struct_def.name : object_name;
|
lang_.language == IDLOptions::kTs ? struct_def.name : object_name;
|
||||||
for (auto it = struct_def.fields.vec.begin();
|
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;
|
const auto &field = **it;
|
||||||
if (field.deprecated) continue;
|
if (field.deprecated) continue;
|
||||||
|
|
||||||
|
|||||||
@@ -1358,8 +1358,8 @@ class RustGenerator : public BaseGenerator {
|
|||||||
code_ += " Some({{U_ELEMENT_TABLE_TYPE}}::init_from_table(u))";
|
code_ += " Some({{U_ELEMENT_TABLE_TYPE}}::init_from_table(u))";
|
||||||
} else {
|
} else {
|
||||||
code_ +=
|
code_ +=
|
||||||
" self.{{FIELD_NAME}}().map(|u| "
|
" self.{{FIELD_NAME}}().map(|u| "
|
||||||
"{{U_ELEMENT_TABLE_TYPE}}::init_from_table(u))";
|
"{{U_ELEMENT_TABLE_TYPE}}::init_from_table(u))";
|
||||||
}
|
}
|
||||||
code_ += " } else {";
|
code_ += " } else {";
|
||||||
code_ += " None";
|
code_ += " None";
|
||||||
@@ -1788,7 +1788,7 @@ class RustGenerator : public BaseGenerator {
|
|||||||
code_ += "";
|
code_ += "";
|
||||||
if (!parser_.opts.generate_all) {
|
if (!parser_.opts.generate_all) {
|
||||||
for (auto it = parser_.included_files_.begin();
|
for (auto it = parser_.included_files_.begin();
|
||||||
it != parser_.included_files_.end(); ++it) {
|
it != parser_.included_files_.end(); ++it) {
|
||||||
if (it->second.empty()) continue;
|
if (it->second.empty()) continue;
|
||||||
auto noext = flatbuffers::StripExtension(it->second);
|
auto noext = flatbuffers::StripExtension(it->second);
|
||||||
auto basename = flatbuffers::StripPath(noext);
|
auto basename = flatbuffers::StripPath(noext);
|
||||||
|
|||||||
@@ -1704,9 +1704,7 @@ CheckedError Parser::ParseSingleValue(const std::string *name, Value &e,
|
|||||||
const auto is_tok_string = (token_ == kTokenStringConstant);
|
const auto is_tok_string = (token_ == kTokenStringConstant);
|
||||||
|
|
||||||
// First see if this could be a conversion function:
|
// First see if this could be a conversion function:
|
||||||
if (is_tok_ident && *cursor_ == '(') {
|
if (is_tok_ident && *cursor_ == '(') { return ParseFunction(name, e); }
|
||||||
return ParseFunction(name, e);
|
|
||||||
}
|
|
||||||
|
|
||||||
// clang-format off
|
// clang-format off
|
||||||
auto match = false;
|
auto match = false;
|
||||||
@@ -1757,8 +1755,7 @@ CheckedError Parser::ParseSingleValue(const std::string *name, Value &e,
|
|||||||
if (!match && is_tok_string && IsScalar(in_type)) {
|
if (!match && is_tok_string && IsScalar(in_type)) {
|
||||||
// Strip trailing whitespaces from attribute_.
|
// Strip trailing whitespaces from attribute_.
|
||||||
auto last_non_ws = attribute_.find_last_not_of(' ');
|
auto last_non_ws = attribute_.find_last_not_of(' ');
|
||||||
if (std::string::npos != last_non_ws)
|
if (std::string::npos != last_non_ws) attribute_.resize(last_non_ws + 1);
|
||||||
attribute_.resize(last_non_ws + 1);
|
|
||||||
if (IsFloat(e.type.base_type)) {
|
if (IsFloat(e.type.base_type)) {
|
||||||
// The functions strtod() and strtof() accept both 'nan' and
|
// The functions strtod() and strtof() accept both 'nan' and
|
||||||
// 'nan(number)' literals. While 'nan(number)' is rejected by the parser
|
// 'nan(number)' literals. While 'nan(number)' is rejected by the parser
|
||||||
|
|||||||
@@ -396,17 +396,17 @@ Offset<const Table *> CopyTable(FlatBufferBuilder &fbb,
|
|||||||
case reflection::Obj: {
|
case reflection::Obj: {
|
||||||
auto &subobjectdef = *schema.objects()->Get(fielddef.type()->index());
|
auto &subobjectdef = *schema.objects()->Get(fielddef.type()->index());
|
||||||
if (!subobjectdef.is_struct()) {
|
if (!subobjectdef.is_struct()) {
|
||||||
offset =
|
offset = CopyTable(fbb, schema, subobjectdef,
|
||||||
CopyTable(fbb, schema, subobjectdef,
|
*GetFieldT(table, fielddef), use_string_pooling)
|
||||||
*GetFieldT(table, fielddef), use_string_pooling).o;
|
.o;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case reflection::Union: {
|
case reflection::Union: {
|
||||||
auto &subobjectdef = GetUnionType(schema, objectdef, fielddef, table);
|
auto &subobjectdef = GetUnionType(schema, objectdef, fielddef, table);
|
||||||
offset =
|
offset = CopyTable(fbb, schema, subobjectdef,
|
||||||
CopyTable(fbb, schema, subobjectdef, *GetFieldT(table, fielddef),
|
*GetFieldT(table, fielddef), use_string_pooling)
|
||||||
use_string_pooling).o;
|
.o;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case reflection::Vector: {
|
case reflection::Vector: {
|
||||||
@@ -433,9 +433,8 @@ Offset<const Table *> CopyTable(FlatBufferBuilder &fbb,
|
|||||||
if (!elemobjectdef->is_struct()) {
|
if (!elemobjectdef->is_struct()) {
|
||||||
std::vector<Offset<const Table *>> elements(vec->size());
|
std::vector<Offset<const Table *>> elements(vec->size());
|
||||||
for (uoffset_t i = 0; i < vec->size(); i++) {
|
for (uoffset_t i = 0; i < vec->size(); i++) {
|
||||||
elements[i] =
|
elements[i] = CopyTable(fbb, schema, *elemobjectdef,
|
||||||
CopyTable(fbb, schema, *elemobjectdef, *vec->Get(i),
|
*vec->Get(i), use_string_pooling);
|
||||||
use_string_pooling);
|
|
||||||
}
|
}
|
||||||
offset = fbb.CreateVector(elements).o;
|
offset = fbb.CreateVector(elements).o;
|
||||||
break;
|
break;
|
||||||
@@ -705,8 +704,7 @@ bool VerifyObject(flatbuffers::Verifier &v, const reflection::Schema &schema,
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool Verify(const reflection::Schema &schema, const reflection::Object &root,
|
bool Verify(const reflection::Schema &schema, const reflection::Object &root,
|
||||||
const uint8_t *buf, size_t length,
|
const uint8_t *buf, size_t length, uoffset_t max_depth /*= 64*/,
|
||||||
uoffset_t max_depth /*= 64*/,
|
|
||||||
uoffset_t max_tables /*= 1000000*/) {
|
uoffset_t max_tables /*= 1000000*/) {
|
||||||
Verifier v(buf, length, max_depth, max_tables);
|
Verifier v(buf, length, max_depth, max_tables);
|
||||||
return VerifyObject(v, schema, root, flatbuffers::GetAnyRoot(buf), true);
|
return VerifyObject(v, schema, root, flatbuffers::GetAnyRoot(buf), true);
|
||||||
|
|||||||
Reference in New Issue
Block a user