clang format on codebase (#7058)

This commit is contained in:
Derek Bailey
2022-02-01 11:23:18 -08:00
committed by GitHub
parent 240be9b5ae
commit bc901436db
14 changed files with 105 additions and 136 deletions

View File

@@ -285,9 +285,7 @@ class FlatBufferBuilder {
FieldLoc fl = { off, field }; FieldLoc fl = { off, field };
buf_.scratch_push_small(fl); buf_.scratch_push_small(fl);
num_field_loc++; num_field_loc++;
if (field > max_voffset_) { if (field > max_voffset_) { max_voffset_ = field; }
max_voffset_ = field;
}
} }
// Like PushElement, but additionally tracks the field this represents. // Like PushElement, but additionally tracks the field this represents.
@@ -1074,7 +1072,7 @@ class FlatBufferBuilder {
void SwapBufAllocator(FlatBufferBuilder &other) { void SwapBufAllocator(FlatBufferBuilder &other) {
buf_.swap_allocator(other.buf_); buf_.swap_allocator(other.buf_);
} }
/// @brief The length of a FlatBuffer file header. /// @brief The length of a FlatBuffer file header.
static const size_t kFileIdentifierLength = static const size_t kFileIdentifierLength =
::flatbuffers::kFileIdentifierLength; ::flatbuffers::kFileIdentifierLength;

View File

@@ -226,12 +226,13 @@ struct TypeTable {
}; };
// String which identifies the current version of FlatBuffers. // String which identifies the current version of FlatBuffers.
inline const char * flatbuffers_version_string() { inline const char *flatbuffers_version_string() {
return "FlatBuffers " FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MAJOR) "." return "FlatBuffers " FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MAJOR) "."
FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MINOR) "." FLATBUFFERS_STRING(FLATBUFFERS_VERSION_MINOR) "."
FLATBUFFERS_STRING(FLATBUFFERS_VERSION_REVISION); FLATBUFFERS_STRING(FLATBUFFERS_VERSION_REVISION);
} }
// clang-format off
#define FLATBUFFERS_DEFINE_BITMASK_OPERATORS(E, T)\ #define FLATBUFFERS_DEFINE_BITMASK_OPERATORS(E, T)\
inline E operator | (E lhs, E rhs){\ inline E operator | (E lhs, E rhs){\
return E(T(lhs) | T(rhs));\ return E(T(lhs) | T(rhs));\

View File

@@ -371,10 +371,7 @@ void AppendToString(std::string &s, T &&v, bool keys_quoted) {
class Reference { class Reference {
public: public:
Reference() Reference()
: data_(nullptr), : data_(nullptr), parent_width_(0), byte_width_(0), type_(FBT_NULL) {}
parent_width_(0),
byte_width_(0),
type_(FBT_NULL) {}
Reference(const uint8_t *data, uint8_t parent_width, uint8_t byte_width, Reference(const uint8_t *data, uint8_t parent_width, uint8_t byte_width,
Type type) Type type)
@@ -1645,8 +1642,7 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
// comes at the cost of using additional memory the same size of // comes at the cost of using additional memory the same size of
// the buffer being verified, so it is by default off. // the buffer being verified, so it is by default off.
std::vector<uint8_t> *reuse_tracker = nullptr, std::vector<uint8_t> *reuse_tracker = nullptr,
bool _check_alignment = true, bool _check_alignment = true, size_t max_depth = 64)
size_t max_depth = 64)
: buf_(buf), : buf_(buf),
size_(buf_len), size_(buf_len),
depth_(0), depth_(0),
@@ -1689,18 +1685,16 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
auto o = static_cast<size_t>(p - buf_); auto o = static_cast<size_t>(p - buf_);
return VerifyBefore(o, len); return VerifyBefore(o, len);
} }
bool VerifyByteWidth(size_t width) { bool VerifyByteWidth(size_t width) {
return Check(width == 1 || width == 2 || width == 4 || width == 8); return Check(width == 1 || width == 2 || width == 4 || width == 8);
} }
bool VerifyType(int type) { bool VerifyType(int type) { return Check(type >= 0 && type < FBT_MAX_TYPE); }
return Check(type >= 0 && type < FBT_MAX_TYPE);
}
bool VerifyOffset(uint64_t off, const uint8_t *p) { bool VerifyOffset(uint64_t off, const uint8_t *p) {
return Check(off <= static_cast<uint64_t>(size_)) && return Check(off <= static_cast<uint64_t>(size_)) &&
off <= static_cast<uint64_t>(p - buf_); off <= static_cast<uint64_t>(p - buf_);
} }
bool VerifyAlignment(const uint8_t *p, size_t size) const { bool VerifyAlignment(const uint8_t *p, size_t size) const {
@@ -1708,16 +1702,16 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
return Check((o & (size - 1)) == 0 || !check_alignment_); return Check((o & (size - 1)) == 0 || !check_alignment_);
} }
// Macro, since we want to escape from parent function & use lazy args. // Macro, since we want to escape from parent function & use lazy args.
#define FLEX_CHECK_VERIFIED(P, PACKED_TYPE) \ #define FLEX_CHECK_VERIFIED(P, PACKED_TYPE) \
if (reuse_tracker_) { \ if (reuse_tracker_) { \
auto packed_type = PACKED_TYPE; \ auto packed_type = PACKED_TYPE; \
auto existing = (*reuse_tracker_)[P - buf_]; \ auto existing = (*reuse_tracker_)[P - buf_]; \
if (existing == packed_type) return true; \ if (existing == packed_type) return true; \
/* Fail verification if already set with different type! */ \ /* Fail verification if already set with different type! */ \
if (!Check(existing == 0)) return false; \ if (!Check(existing == 0)) return false; \
(*reuse_tracker_)[P - buf_] = packed_type; \ (*reuse_tracker_)[P - buf_] = packed_type; \
} }
bool VerifyVector(Reference r, const uint8_t *p, Type elem_type) { bool VerifyVector(Reference r, const uint8_t *p, Type elem_type) {
// Any kind of nesting goes thru this function, so guard against that // Any kind of nesting goes thru this function, so guard against that
@@ -1727,19 +1721,19 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
if (!Check(depth_ <= max_depth_ && num_vectors_ <= max_vectors_)) if (!Check(depth_ <= max_depth_ && num_vectors_ <= max_vectors_))
return false; return false;
auto size_byte_width = r.byte_width_; auto size_byte_width = r.byte_width_;
FLEX_CHECK_VERIFIED(p, PackedType(Builder::WidthB(size_byte_width), r.type_)); FLEX_CHECK_VERIFIED(p,
if (!VerifyBeforePointer(p, size_byte_width)) PackedType(Builder::WidthB(size_byte_width), r.type_));
return false; if (!VerifyBeforePointer(p, size_byte_width)) return false;
auto sized = Sized(p, size_byte_width); auto sized = Sized(p, size_byte_width);
auto num_elems = sized.size(); auto num_elems = sized.size();
auto elem_byte_width = auto elem_byte_width = r.type_ == FBT_STRING || r.type_ == FBT_BLOB
r.type_ == FBT_STRING || r.type_ == FBT_BLOB ? uint8_t(1) : r.byte_width_; ? uint8_t(1)
: r.byte_width_;
auto max_elems = SIZE_MAX / elem_byte_width; auto max_elems = SIZE_MAX / elem_byte_width;
if (!Check(num_elems < max_elems)) if (!Check(num_elems < max_elems))
return false; // Protect against byte_size overflowing. return false; // Protect against byte_size overflowing.
auto byte_size = num_elems * elem_byte_width; auto byte_size = num_elems * elem_byte_width;
if (!VerifyFromPointer(p, byte_size)) if (!VerifyFromPointer(p, byte_size)) return false;
return false;
if (elem_type == FBT_NULL) { if (elem_type == FBT_NULL) {
// Verify type bytes after the vector. // Verify type bytes after the vector.
if (!VerifyFromPointer(p + byte_size, num_elems)) return false; if (!VerifyFromPointer(p + byte_size, num_elems)) return false;
@@ -1760,28 +1754,25 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
bool VerifyKeys(const uint8_t *p, uint8_t byte_width) { bool VerifyKeys(const uint8_t *p, uint8_t byte_width) {
// The vector part of the map has already been verified. // The vector part of the map has already been verified.
const size_t num_prefixed_fields = 3; const size_t num_prefixed_fields = 3;
if (!VerifyBeforePointer(p, byte_width * num_prefixed_fields)) if (!VerifyBeforePointer(p, byte_width * num_prefixed_fields)) return false;
return false;
p -= byte_width * num_prefixed_fields; p -= byte_width * num_prefixed_fields;
auto off = ReadUInt64(p, byte_width); auto off = ReadUInt64(p, byte_width);
if (!VerifyOffset(off, p)) if (!VerifyOffset(off, p)) return false;
return false;
auto key_byte_with = auto key_byte_with =
static_cast<uint8_t>(ReadUInt64(p + byte_width, byte_width)); static_cast<uint8_t>(ReadUInt64(p + byte_width, byte_width));
if (!VerifyByteWidth(key_byte_with)) if (!VerifyByteWidth(key_byte_with)) return false;
return false;
return VerifyVector(Reference(p, byte_width, key_byte_with, FBT_VECTOR_KEY), return VerifyVector(Reference(p, byte_width, key_byte_with, FBT_VECTOR_KEY),
p - off, FBT_KEY); p - off, FBT_KEY);
} }
bool VerifyKey(const uint8_t* p) { bool VerifyKey(const uint8_t *p) {
FLEX_CHECK_VERIFIED(p, PackedType(BIT_WIDTH_8, FBT_KEY)); FLEX_CHECK_VERIFIED(p, PackedType(BIT_WIDTH_8, FBT_KEY));
while (p < buf_ + size_) while (p < buf_ + size_)
if (*p++) return true; if (*p++) return true;
return false; return false;
} }
#undef FLEX_CHECK_VERIFIED #undef FLEX_CHECK_VERIFIED
bool VerifyTerminator(const String &s) { bool VerifyTerminator(const String &s) {
return VerifyFromPointer(reinterpret_cast<const uint8_t *>(s.c_str()), return VerifyFromPointer(reinterpret_cast<const uint8_t *>(s.c_str()),
@@ -1799,37 +1790,26 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
} }
// All remaining types are an offset. // All remaining types are an offset.
auto off = ReadUInt64(r.data_, r.parent_width_); auto off = ReadUInt64(r.data_, r.parent_width_);
if (!VerifyOffset(off, r.data_)) if (!VerifyOffset(off, r.data_)) return false;
return false;
auto p = r.Indirect(); auto p = r.Indirect();
if (!VerifyAlignment(p, r.byte_width_)) if (!VerifyAlignment(p, r.byte_width_)) return false;
return false;
switch (r.type_) { switch (r.type_) {
case FBT_INDIRECT_INT: case FBT_INDIRECT_INT:
case FBT_INDIRECT_UINT: case FBT_INDIRECT_UINT:
case FBT_INDIRECT_FLOAT: case FBT_INDIRECT_FLOAT: return VerifyFromPointer(p, r.byte_width_);
return VerifyFromPointer(p, r.byte_width_); case FBT_KEY: return VerifyKey(p);
case FBT_KEY:
return VerifyKey(p);
case FBT_MAP: case FBT_MAP:
return VerifyVector(r, p, FBT_NULL) && return VerifyVector(r, p, FBT_NULL) && VerifyKeys(p, r.byte_width_);
VerifyKeys(p, r.byte_width_); case FBT_VECTOR: return VerifyVector(r, p, FBT_NULL);
case FBT_VECTOR: case FBT_VECTOR_INT: return VerifyVector(r, p, FBT_INT);
return VerifyVector(r, p, FBT_NULL);
case FBT_VECTOR_INT:
return VerifyVector(r, p, FBT_INT);
case FBT_VECTOR_BOOL: case FBT_VECTOR_BOOL:
case FBT_VECTOR_UINT: case FBT_VECTOR_UINT: return VerifyVector(r, p, FBT_UINT);
return VerifyVector(r, p, FBT_UINT); case FBT_VECTOR_FLOAT: return VerifyVector(r, p, FBT_FLOAT);
case FBT_VECTOR_FLOAT: case FBT_VECTOR_KEY: return VerifyVector(r, p, FBT_KEY);
return VerifyVector(r, p, FBT_FLOAT);
case FBT_VECTOR_KEY:
return VerifyVector(r, p, FBT_KEY);
case FBT_VECTOR_STRING_DEPRECATED: case FBT_VECTOR_STRING_DEPRECATED:
// Use of FBT_KEY here intentional, see elsewhere. // Use of FBT_KEY here intentional, see elsewhere.
return VerifyVector(r, p, FBT_KEY); return VerifyVector(r, p, FBT_KEY);
case FBT_BLOB: case FBT_BLOB: return VerifyVector(r, p, FBT_UINT);
return VerifyVector(r, p, FBT_UINT);
case FBT_STRING: case FBT_STRING:
return VerifyVector(r, p, FBT_UINT) && return VerifyVector(r, p, FBT_UINT) &&
VerifyTerminator(String(p, r.byte_width_)); VerifyTerminator(String(p, r.byte_width_));
@@ -1844,12 +1824,10 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
case FBT_VECTOR_FLOAT4: { case FBT_VECTOR_FLOAT4: {
uint8_t len = 0; uint8_t len = 0;
auto vtype = ToFixedTypedVectorElementType(r.type_, &len); auto vtype = ToFixedTypedVectorElementType(r.type_, &len);
if (!VerifyType(vtype)) if (!VerifyType(vtype)) return false;
return false;
return VerifyFromPointer(p, r.byte_width_ * len); return VerifyFromPointer(p, r.byte_width_ * len);
} }
default: default: return false;
return false;
} }
} }
@@ -1859,8 +1837,7 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
auto end = buf_ + size_; auto end = buf_ + size_;
auto byte_width = *--end; auto byte_width = *--end;
auto packed_type = *--end; auto packed_type = *--end;
return VerifyByteWidth(byte_width) && return VerifyByteWidth(byte_width) && Check(end - buf_ >= byte_width) &&
Check(end - buf_ >= byte_width) &&
VerifyRef(Reference(end - byte_width, byte_width, packed_type)); VerifyRef(Reference(end - byte_width, byte_width, packed_type));
} }
@@ -1875,14 +1852,14 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
std::vector<uint8_t> *reuse_tracker_; std::vector<uint8_t> *reuse_tracker_;
}; };
// Utility function that contructs the Verifier for you, see above for parameters. // Utility function that contructs the Verifier for you, see above for
// parameters.
inline bool VerifyBuffer(const uint8_t *buf, size_t buf_len, inline bool VerifyBuffer(const uint8_t *buf, size_t buf_len,
std::vector<uint8_t> *reuse_tracker = nullptr) { std::vector<uint8_t> *reuse_tracker = nullptr) {
Verifier verifier(buf, buf_len, reuse_tracker); Verifier verifier(buf, buf_len, reuse_tracker);
return verifier.VerifyBuffer(); return verifier.VerifyBuffer();
} }
#ifdef FLATBUFFERS_H_ #ifdef FLATBUFFERS_H_
// This is a verifier utility function that works together with the // This is a verifier utility function that works together with the
// FlatBuffers verifier, which should only be present if flatbuffer.h // FlatBuffers verifier, which should only be present if flatbuffer.h
@@ -1890,9 +1867,8 @@ inline bool VerifyBuffer(const uint8_t *buf, size_t buf_len,
inline bool VerifyNestedFlexBuffer(const flatbuffers::Vector<uint8_t> *nv, inline bool VerifyNestedFlexBuffer(const flatbuffers::Vector<uint8_t> *nv,
flatbuffers::Verifier &verifier) { flatbuffers::Verifier &verifier) {
if (!nv) return true; if (!nv) return true;
return verifier.Check( return verifier.Check(flexbuffers::VerifyBuffer(
flexbuffers::VerifyBuffer(nv->data(), nv->size(), nv->data(), nv->size(), verifier.GetFlexReuseTracker()));
verifier.GetFlexReuseTracker()));
} }
#endif #endif

View File

@@ -112,18 +112,19 @@ class Table {
// Verify a particular field. // Verify a particular field.
template<typename T> template<typename T>
bool VerifyField(const Verifier &verifier, voffset_t field, size_t align) const { bool VerifyField(const Verifier &verifier, voffset_t field,
size_t align) const {
// Calling GetOptionalFieldOffset should be safe now thanks to // Calling GetOptionalFieldOffset should be safe now thanks to
// VerifyTable(). // VerifyTable().
auto field_offset = GetOptionalFieldOffset(field); auto field_offset = GetOptionalFieldOffset(field);
// Check the actual field. // Check the actual field.
return !field_offset || return !field_offset || verifier.VerifyField<T>(data_, field_offset, align);
verifier.VerifyField<T>(data_, field_offset, align);
} }
// VerifyField for required fields. // VerifyField for required fields.
template<typename T> template<typename T>
bool VerifyFieldRequired(const Verifier &verifier, voffset_t field, size_t align) const { bool VerifyFieldRequired(const Verifier &verifier, voffset_t field,
size_t align) const {
auto field_offset = GetOptionalFieldOffset(field); auto field_offset = GetOptionalFieldOffset(field);
return verifier.Check(field_offset != 0) && return verifier.Check(field_offset != 0) &&
verifier.VerifyField<T>(data_, field_offset, align); verifier.VerifyField<T>(data_, field_offset, align);

View File

@@ -17,15 +17,15 @@
#ifndef FLATBUFFERS_UTIL_H_ #ifndef FLATBUFFERS_UTIL_H_
#define FLATBUFFERS_UTIL_H_ #define FLATBUFFERS_UTIL_H_
#include <errno.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h>
#include "flatbuffers/base.h" #include "flatbuffers/base.h"
#include "flatbuffers/stl_emulation.h" #include "flatbuffers/stl_emulation.h"
#ifndef FLATBUFFERS_PREFER_PRINTF #ifndef FLATBUFFERS_PREFER_PRINTF
# include <sstream>
# include <iomanip> # include <iomanip>
# include <sstream>
#else // FLATBUFFERS_PREFER_PRINTF #else // FLATBUFFERS_PREFER_PRINTF
# include <float.h> # include <float.h>
# include <stdio.h> # include <stdio.h>

View File

@@ -81,14 +81,15 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
} }
// Verify relative to a known-good base pointer. // Verify relative to a known-good base pointer.
bool VerifyFieldStruct(const uint8_t *base, voffset_t elem_off, size_t elem_len, bool VerifyFieldStruct(const uint8_t *base, voffset_t elem_off,
size_t align) const { size_t elem_len, size_t align) const {
auto f = static_cast<size_t>(base - buf_) + elem_off; auto f = static_cast<size_t>(base - buf_) + elem_off;
return VerifyAlignment(f, align) && Verify(f, elem_len); return VerifyAlignment(f, align) && Verify(f, elem_len);
} }
template<typename T> template<typename T>
bool VerifyField(const uint8_t *base, voffset_t elem_off, size_t align) const { bool VerifyField(const uint8_t *base, voffset_t elem_off,
size_t align) const {
auto f = static_cast<size_t>(base - buf_) + elem_off; auto f = static_cast<size_t>(base - buf_) + elem_off;
return VerifyAlignment(f, align) && Verify(f, sizeof(T)); return VerifyAlignment(f, align) && Verify(f, sizeof(T));
} }
@@ -259,9 +260,7 @@ class Verifier FLATBUFFERS_FINAL_CLASS {
// clang-format on // clang-format on
} }
std::vector<uint8_t> *GetFlexReuseTracker() { std::vector<uint8_t> *GetFlexReuseTracker() { return flex_reuse_tracker_; }
return flex_reuse_tracker_;
}
void SetFlexReuseTracker(std::vector<uint8_t> *rt) { void SetFlexReuseTracker(std::vector<uint8_t> *rt) {
flex_reuse_tracker_ = rt; flex_reuse_tracker_ = rt;

View File

@@ -538,7 +538,9 @@ class CppGenerator : public BaseGenerator {
// Check if a size-prefixed buffer has the identifier. // Check if a size-prefixed buffer has the identifier.
code_ += "inline \\"; code_ += "inline \\";
code_ += "bool SizePrefixed{{STRUCT_NAME}}BufferHasIdentifier(const void *buf) {"; code_ +=
"bool SizePrefixed{{STRUCT_NAME}}BufferHasIdentifier(const void "
"*buf) {";
code_ += " return flatbuffers::BufferHasIdentifier("; code_ += " return flatbuffers::BufferHasIdentifier(";
code_ += " buf, {{STRUCT_NAME}}Identifier(), true);"; code_ += " buf, {{STRUCT_NAME}}Identifier(), true);";
code_ += "}"; code_ += "}";
@@ -1482,7 +1484,7 @@ class CppGenerator : public BaseGenerator {
if (ev.union_type.base_type == BASE_TYPE_STRUCT) { if (ev.union_type.base_type == BASE_TYPE_STRUCT) {
if (ev.union_type.struct_def->fixed) { if (ev.union_type.struct_def->fixed) {
code_.SetValue("ALIGN", code_.SetValue("ALIGN",
NumToString(ev.union_type.struct_def->minalign)); NumToString(ev.union_type.struct_def->minalign));
code_ += code_ +=
" return verifier.VerifyField<{{TYPE}}>(" " return verifier.VerifyField<{{TYPE}}>("
"static_cast<const uint8_t *>(obj), 0, {{ALIGN}});"; "static_cast<const uint8_t *>(obj), 0, {{ALIGN}});";
@@ -2122,8 +2124,9 @@ class CppGenerator : public BaseGenerator {
code_.SetValue("OFFSET", GenFieldOffsetName(field)); code_.SetValue("OFFSET", GenFieldOffsetName(field));
if (IsScalar(field.value.type.base_type) || IsStruct(field.value.type)) { if (IsScalar(field.value.type.base_type) || IsStruct(field.value.type)) {
code_.SetValue("ALIGN", NumToString(InlineAlignment(field.value.type))); code_.SetValue("ALIGN", NumToString(InlineAlignment(field.value.type)));
code_ += "{{PRE}}VerifyField{{REQUIRED}}<{{SIZE}}>(verifier, " code_ +=
"{{OFFSET}}, {{ALIGN}})\\"; "{{PRE}}VerifyField{{REQUIRED}}<{{SIZE}}>(verifier, "
"{{OFFSET}}, {{ALIGN}})\\";
} else { } else {
code_ += "{{PRE}}VerifyOffset{{REQUIRED}}(verifier, {{OFFSET}})\\"; code_ += "{{PRE}}VerifyOffset{{REQUIRED}}(verifier, {{OFFSET}})\\";
} }
@@ -2175,11 +2178,13 @@ class CppGenerator : public BaseGenerator {
if (!nfn.empty()) { if (!nfn.empty()) {
code_.SetValue("CPP_NAME", nfn); code_.SetValue("CPP_NAME", nfn);
// FIXME: file_identifier. // FIXME: file_identifier.
code_ += "{{PRE}}verifier.VerifyNestedFlatBuffer<{{CPP_NAME}}>" code_ +=
"({{NAME}}(), nullptr)\\"; "{{PRE}}verifier.VerifyNestedFlatBuffer<{{CPP_NAME}}>"
"({{NAME}}(), nullptr)\\";
} else if (field.flexbuffer) { } else if (field.flexbuffer) {
code_ += "{{PRE}}flexbuffers::VerifyNestedFlexBuffer" code_ +=
"({{NAME}}(), verifier)\\"; "{{PRE}}flexbuffers::VerifyNestedFlexBuffer"
"({{NAME}}(), verifier)\\";
} }
break; break;
} }
@@ -2216,7 +2221,8 @@ class CppGenerator : public BaseGenerator {
} }
// Returns {field<val: -1, field==val: 0, field>val: +1}. // Returns {field<val: -1, field==val: 0, field>val: +1}.
code_.SetValue("KEY_TYPE", type); code_.SetValue("KEY_TYPE", type);
code_ += " int KeyCompareWithValue({{KEY_TYPE}} _{{FIELD_NAME}}) const {"; code_ +=
" int KeyCompareWithValue({{KEY_TYPE}} _{{FIELD_NAME}}) const {";
code_ += code_ +=
" return static_cast<int>({{FIELD_NAME}}() > _{{FIELD_NAME}}) - " " return static_cast<int>({{FIELD_NAME}}() > _{{FIELD_NAME}}) - "
"static_cast<int>({{FIELD_NAME}}() < _{{FIELD_NAME}});"; "static_cast<int>({{FIELD_NAME}}() < _{{FIELD_NAME}});";

View File

@@ -738,11 +738,8 @@ class PythonGenerator : public BaseGenerator {
} }
break; break;
} }
case BASE_TYPE_UNION: case BASE_TYPE_UNION: GetUnionField(struct_def, field, code_ptr); break;
GetUnionField(struct_def, field, code_ptr); default: FLATBUFFERS_ASSERT(0);
break;
default:
FLATBUFFERS_ASSERT(0);
} }
} }
if (IsVector(field.value.type) || IsArray(field.value.type)) { if (IsVector(field.value.type) || IsArray(field.value.type)) {
@@ -913,14 +910,9 @@ class PythonGenerator : public BaseGenerator {
import_list->insert("import " + package_reference); import_list->insert("import " + package_reference);
} }
break; break;
case BASE_TYPE_STRING: case BASE_TYPE_STRING: field_type += "str"; break;
field_type += "str"; case BASE_TYPE_NONE: field_type += "None"; break;
break; default: break;
case BASE_TYPE_NONE:
field_type += "None";
break;
default:
break;
} }
field_types += field_type + separator_string; field_types += field_type + separator_string;
} }
@@ -1253,8 +1245,7 @@ class PythonGenerator : public BaseGenerator {
GenUnPackForScalarVector(struct_def, field, &code); GenUnPackForScalarVector(struct_def, field, &code);
break; break;
} }
default: default: GenUnPackForScalar(struct_def, field, &code);
GenUnPackForScalar(struct_def, field, &code);
} }
} }

View File

@@ -859,7 +859,8 @@ class RustGenerator : public BaseGenerator {
code_ += " serializer.serialize_u32(self.bits() as u32)"; code_ += " serializer.serialize_u32(self.bits() as u32)";
} else { } else {
code_ += code_ +=
" serializer.serialize_unit_variant(\"{{ENUM_NAME}}\", self.0 as " " serializer.serialize_unit_variant(\"{{ENUM_NAME}}\", self.0 "
"as "
"u32, self.variant_name().unwrap())"; "u32, self.variant_name().unwrap())";
} }
code_ += " }"; code_ += " }";
@@ -2752,7 +2753,8 @@ class RustGenerator : public BaseGenerator {
ForAllStructFields(struct_def, [&](const FieldDef &unused) { ForAllStructFields(struct_def, [&](const FieldDef &unused) {
(void)unused; (void)unused;
code_ += code_ +=
" s.serialize_field(\"{{FIELD_NAME}}\", &self.{{FIELD_NAME}}())?;"; " s.serialize_field(\"{{FIELD_NAME}}\", "
"&self.{{FIELD_NAME}}())?;";
}); });
code_ += " s.end()"; code_ += " s.end()";
code_ += " }"; code_ += " }";
@@ -2959,8 +2961,9 @@ class RustGenerator : public BaseGenerator {
code_ += ""; code_ += "";
if (parser_.opts.rust_serialize) { if (parser_.opts.rust_serialize) {
code_ += indent + "extern crate serde;"; code_ += indent + "extern crate serde;";
code_ += indent + code_ +=
"use self::serde::ser::{Serialize, Serializer, SerializeStruct};"; indent +
"use self::serde::ser::{Serialize, Serializer, SerializeStruct};";
code_ += ""; code_ += "";
} }
code_ += indent + "extern crate flatbuffers;"; code_ += indent + "extern crate flatbuffers;";

View File

@@ -711,8 +711,9 @@ class SwiftGenerator : public BaseGenerator {
} }
if (IsBool(field.value.type.base_type)) { if (IsBool(field.value.type.base_type)) {
std::string default_value = field.IsOptional() ? "nil" : std::string default_value =
("0" == field.value.constant ? "false" : "true"); field.IsOptional() ? "nil"
: ("0" == field.value.constant ? "false" : "true");
code_.SetValue("CONSTANT", default_value); code_.SetValue("CONSTANT", default_value);
code_.SetValue("VALUETYPE", "Bool"); code_.SetValue("VALUETYPE", "Bool");
code_ += GenReaderMainBody(optional) + "\\"; code_ += GenReaderMainBody(optional) + "\\";

View File

@@ -343,8 +343,7 @@ class TsGenerator : public BaseGenerator {
switch (type.base_type) { switch (type.base_type) {
case BASE_TYPE_BOOL: return allowNull ? "boolean|null" : "boolean"; case BASE_TYPE_BOOL: return allowNull ? "boolean|null" : "boolean";
case BASE_TYPE_LONG: case BASE_TYPE_LONG:
case BASE_TYPE_ULONG: case BASE_TYPE_ULONG: return allowNull ? "bigint|null" : "bigint";
return allowNull ? "bigint|null" : "bigint";
default: default:
if (IsScalar(type.base_type)) { if (IsScalar(type.base_type)) {
if (type.enum_def) { if (type.enum_def) {
@@ -907,8 +906,7 @@ class TsGenerator : public BaseGenerator {
// a string that contains values for things that can be created inline or // a string that contains values for things that can be created inline or
// the variable name from field_offset_decl // the variable name from field_offset_decl
std::string field_offset_val; std::string field_offset_val;
const auto field_default_val = const auto field_default_val = GenDefaultValue(field, imports);
GenDefaultValue(field, imports);
// Emit a scalar field // Emit a scalar field
const auto is_string = IsString(field.value.type); const auto is_string = IsString(field.value.type);
@@ -1643,7 +1641,7 @@ class TsGenerator : public BaseGenerator {
std::string GetArgName(const FieldDef &field) { std::string GetArgName(const FieldDef &field) {
auto argname = MakeCamel(field.name, false); auto argname = MakeCamel(field.name, false);
if (!IsScalar(field.value.type.base_type)) { if (!IsScalar(field.value.type.base_type)) {
argname += "Offset"; argname += "Offset";
} else { } else {
argname = EscapeKeyword(argname); argname = EscapeKeyword(argname);

View File

@@ -1677,8 +1677,9 @@ CheckedError Parser::ParseNestedFlatbuffer(Value &val, FieldDef *field,
if (opts.json_nested_legacy_flatbuffers) { if (opts.json_nested_legacy_flatbuffers) {
ECHECK(ParseAnyValue(val, field, fieldn, parent_struct_def, 0)); ECHECK(ParseAnyValue(val, field, fieldn, parent_struct_def, 0));
} else { } else {
return Error("cannot parse nested_flatbuffer as bytes unless" return Error(
" --json-nested-bytes is set"); "cannot parse nested_flatbuffer as bytes unless"
" --json-nested-bytes is set");
} }
} else { } else {
auto cursor_at_value_begin = cursor_; auto cursor_at_value_begin = cursor_;
@@ -2167,9 +2168,7 @@ void EnumDef::SortByValue() {
}); });
else else
std::sort(v.begin(), v.end(), [](const EnumVal *e1, const EnumVal *e2) { std::sort(v.begin(), v.end(), [](const EnumVal *e1, const EnumVal *e2) {
if (e1->GetAsInt64() == e2->GetAsInt64()) { if (e1->GetAsInt64() == e2->GetAsInt64()) { return e1->name < e2->name; }
return e1->name < e2->name;
}
return e1->GetAsInt64() < e2->GetAsInt64(); return e1->GetAsInt64() < e2->GetAsInt64();
}); });
} }

View File

@@ -645,8 +645,7 @@ bool VerifyObject(flatbuffers::Verifier &v, const reflection::Schema &schema,
case reflection::Bool: case reflection::Bool:
case reflection::Byte: case reflection::Byte:
case reflection::UByte: case reflection::UByte:
if (!table->VerifyField<int8_t>(v, field_def->offset(), if (!table->VerifyField<int8_t>(v, field_def->offset(), sizeof(int8_t)))
sizeof(int8_t)))
return false; return false;
break; break;
case reflection::Short: case reflection::Short:
@@ -668,13 +667,11 @@ bool VerifyObject(flatbuffers::Verifier &v, const reflection::Schema &schema,
return false; return false;
break; break;
case reflection::Float: case reflection::Float:
if (!table->VerifyField<float>(v, field_def->offset(), if (!table->VerifyField<float>(v, field_def->offset(), sizeof(float)))
sizeof(float)))
return false; return false;
break; break;
case reflection::Double: case reflection::Double:
if (!table->VerifyField<double>(v, field_def->offset(), if (!table->VerifyField<double>(v, field_def->offset(), sizeof(double)))
sizeof(double)))
return false; return false;
break; break;
case reflection::String: case reflection::String:

View File

@@ -496,7 +496,7 @@ void MutateFlatBuffersTest(uint8_t *flatbuf, std::size_t length) {
} }
// Utility function to check a Monster object. // Utility function to check a Monster object.
void CheckMonsterObject(MonsterT* monster2) { void CheckMonsterObject(MonsterT *monster2) {
TEST_EQ(monster2->hp, 80); TEST_EQ(monster2->hp, 80);
TEST_EQ(monster2->mana, 150); // default TEST_EQ(monster2->mana, 150); // default
TEST_EQ_STR(monster2->name.c_str(), "MyMonster"); TEST_EQ_STR(monster2->name.c_str(), "MyMonster");
@@ -589,8 +589,7 @@ void ObjectFlatBuffersTest(uint8_t *flatbuf) {
// Test object copy. // Test object copy.
auto monster3 = *monster2; auto monster3 = *monster2;
flatbuffers::FlatBufferBuilder fbb3; flatbuffers::FlatBufferBuilder fbb3;
fbb3.Finish(CreateMonster(fbb3, &monster3, &rehasher), fbb3.Finish(CreateMonster(fbb3, &monster3, &rehasher), MonsterIdentifier());
MonsterIdentifier());
const auto len3 = fbb3.GetSize(); const auto len3 = fbb3.GetSize();
TEST_EQ(len2, len3); TEST_EQ(len2, len3);
TEST_EQ(memcmp(fbb2.GetBufferPointer(), fbb3.GetBufferPointer(), len2), 0); TEST_EQ(memcmp(fbb2.GetBufferPointer(), fbb3.GetBufferPointer(), len2), 0);