bulk code format fix (#8707)

This commit is contained in:
Derek Bailey
2025-09-23 21:50:27 -07:00
committed by GitHub
parent 0e047869da
commit caf3b494db
559 changed files with 38871 additions and 31276 deletions

View File

@@ -47,8 +47,8 @@ class AnnotatedBinaryTextGenerator {
};
explicit AnnotatedBinaryTextGenerator(
const Options &options, std::map<uint64_t, BinarySection> annotations,
const uint8_t *const binary, const int64_t binary_length)
const Options& options, std::map<uint64_t, BinarySection> annotations,
const uint8_t* const binary, const int64_t binary_length)
: annotations_(std::move(annotations)),
binary_(binary),
binary_length_(binary_length),
@@ -56,14 +56,14 @@ class AnnotatedBinaryTextGenerator {
// Generate the annotated binary for the given `filename`. Returns true if the
// annotated binary was successfully saved.
bool Generate(const std::string &filename, const std::string &schema_filename,
const std::string &output_filename = "");
bool Generate(const std::string& filename, const std::string& schema_filename,
const std::string& output_filename = "");
private:
const std::map<uint64_t, BinarySection> annotations_;
// The binary data itself.
const uint8_t *binary_;
const uint8_t* binary_;
const int64_t binary_length_;
// Output configuration

View File

@@ -27,20 +27,24 @@ namespace flatbuffers {
namespace {
static void ForAllEnums(
const flatbuffers::Vector<flatbuffers::Offset<reflection::Enum>> *enums,
std::function<void(const reflection::Enum *)> func) {
for (auto it = enums->cbegin(); it != enums->cend(); ++it) { func(*it); }
const flatbuffers::Vector<flatbuffers::Offset<reflection::Enum>>* enums,
std::function<void(const reflection::Enum*)> func) {
for (auto it = enums->cbegin(); it != enums->cend(); ++it) {
func(*it);
}
}
static void ForAllObjects(
const flatbuffers::Vector<flatbuffers::Offset<reflection::Object>> *objects,
std::function<void(const reflection::Object *)> func) {
for (auto it = objects->cbegin(); it != objects->cend(); ++it) { func(*it); }
const flatbuffers::Vector<flatbuffers::Offset<reflection::Object>>* objects,
std::function<void(const reflection::Object*)> func) {
for (auto it = objects->cbegin(); it != objects->cend(); ++it) {
func(*it);
}
}
static void ForAllEnumValues(
const reflection::Enum *enum_def,
std::function<void(const reflection::EnumVal *)> func) {
const reflection::Enum* enum_def,
std::function<void(const reflection::EnumVal*)> func) {
for (auto it = enum_def->values()->cbegin(); it != enum_def->values()->cend();
++it) {
func(*it);
@@ -48,10 +52,12 @@ static void ForAllEnumValues(
}
static void ForAllDocumentation(
const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>
*documentation,
std::function<void(const flatbuffers::String *)> func) {
if (!documentation) { return; }
const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>*
documentation,
std::function<void(const flatbuffers::String*)> func) {
if (!documentation) {
return;
}
for (auto it = documentation->cbegin(); it != documentation->cend(); ++it) {
func(*it);
}
@@ -59,7 +65,7 @@ static void ForAllDocumentation(
// Maps the field index into object->fields() to the field's ID (the ith element
// in the return vector).
static std::vector<uint32_t> FieldIdToIndex(const reflection::Object *object) {
static std::vector<uint32_t> FieldIdToIndex(const reflection::Object* object) {
std::vector<uint32_t> field_index_by_id;
field_index_by_id.resize(object->fields()->size());
@@ -101,15 +107,15 @@ class BaseBfbsGenerator : public CodeGenerator {
virtual ~BaseBfbsGenerator() {}
BaseBfbsGenerator() : schema_(nullptr) {}
virtual Status GenerateFromSchema(const reflection::Schema *schema,
const CodeGenOptions &options) = 0;
virtual Status GenerateFromSchema(const reflection::Schema* schema,
const CodeGenOptions& options) = 0;
virtual uint64_t SupportedAdvancedFeatures() const = 0;
// Override of the Generator::GenerateCode method that does the initial
// deserialization and verification steps.
Status GenerateCode(const uint8_t *buffer, int64_t length,
const CodeGenOptions &options) FLATBUFFERS_OVERRIDE {
Status GenerateCode(const uint8_t* buffer, int64_t length,
const CodeGenOptions& options) FLATBUFFERS_OVERRIDE {
flatbuffers::Verifier verifier(buffer, static_cast<size_t>(length));
if (!reflection::VerifySchemaBuffer(verifier)) {
return FAILED_VERIFICATION;
@@ -133,7 +139,7 @@ class BaseBfbsGenerator : public CodeGenerator {
// GetObject returns the underlying object struct of the given type
// if element_type is true and GetObject is a list of objects then
// GetObject will correctly return the object struct of the vector's elements
const reflection::Object *GetObject(const reflection::Type *type,
const reflection::Object* GetObject(const reflection::Type* type,
bool element_type = false) const {
const reflection::BaseType base_type =
element_type ? type->element() : type->base_type();
@@ -146,7 +152,7 @@ class BaseBfbsGenerator : public CodeGenerator {
// GetEnum returns the underlying enum struct of the given type
// if element_type is true and GetEnum is a list of enums then
// GetEnum will correctly return the enum struct of the vector's elements
const reflection::Enum *GetEnum(const reflection::Type *type,
const reflection::Enum* GetEnum(const reflection::Type* type,
bool element_type = false) const {
const reflection::BaseType base_type =
element_type ? type->element() : type->base_type();
@@ -160,7 +166,7 @@ class BaseBfbsGenerator : public CodeGenerator {
// Used to get a object that is reference by index. (e.g.
// reflection::Type::index). Returns nullptr if no object is available.
const reflection::Object *GetObjectByIndex(int32_t index) const {
const reflection::Object* GetObjectByIndex(int32_t index) const {
if (!schema_ || index < 0 ||
index >= static_cast<int32_t>(schema_->objects()->size())) {
return nullptr;
@@ -170,7 +176,7 @@ class BaseBfbsGenerator : public CodeGenerator {
// Used to get a enum that is reference by index. (e.g.
// reflection::Type::index). Returns nullptr if no enum is available.
const reflection::Enum *GetEnumByIndex(int32_t index) const {
const reflection::Enum* GetEnumByIndex(int32_t index) const {
if (!schema_ || index < 0 ||
index >= static_cast<int32_t>(schema_->enums()->size())) {
return nullptr;
@@ -178,8 +184,8 @@ class BaseBfbsGenerator : public CodeGenerator {
return schema_->enums()->Get(index);
}
void ForAllFields(const reflection::Object *object, bool reverse,
std::function<void(const reflection::Field *)> func) const {
void ForAllFields(const reflection::Object* object, bool reverse,
std::function<void(const reflection::Field*)> func) const {
const std::vector<uint32_t> field_to_id_map = FieldIdToIndex(object);
for (size_t i = 0; i < field_to_id_map.size(); ++i) {
func(object->fields()->Get(
@@ -187,18 +193,18 @@ class BaseBfbsGenerator : public CodeGenerator {
}
}
bool IsTable(const reflection::Type *type, bool use_element = false) const {
bool IsTable(const reflection::Type* type, bool use_element = false) const {
return !IsStruct(type, use_element);
}
bool IsStruct(const reflection::Type *type, bool use_element = false) const {
bool IsStruct(const reflection::Type* type, bool use_element = false) const {
const reflection::BaseType base_type =
use_element ? type->element() : type->base_type();
return IsStructOrTable(base_type) &&
GetObjectByIndex(type->index())->is_struct();
}
const reflection::Schema *schema_;
const reflection::Schema* schema_;
};
} // namespace flatbuffers

View File

@@ -26,7 +26,7 @@ namespace flatbuffers {
// Constructs a new Lua Code generator.
std::unique_ptr<CodeGenerator> NewLuaBfbsGenerator(
const std::string &flatc_version);
const std::string& flatc_version);
} // namespace flatbuffers

View File

@@ -26,7 +26,7 @@ namespace flatbuffers {
// Constructs a new Nim Code generator.
std::unique_ptr<CodeGenerator> NewNimBfbsGenerator(
const std::string &flatc_version);
const std::string& flatc_version);
} // namespace flatbuffers

View File

@@ -26,22 +26,22 @@ class BfbsNamer : public Namer {
using Namer::Variable;
using Namer::Variant;
template<typename T>
std::string Denamespace(T t, std::string &namespace_prefix,
template <typename T>
std::string Denamespace(T t, std::string& namespace_prefix,
const char delimiter = '.') const {
return Namer::Denamespace(t->name()->c_str(), namespace_prefix, delimiter);
}
template<typename T>
template <typename T>
std::string Denamespace(T t, const char delimiter = '.') const {
return Namer::Denamespace(t->name()->c_str(), delimiter);
}
virtual std::string Field(const ::reflection::Field &f) const {
virtual std::string Field(const ::reflection::Field& f) const {
return Field(f.name()->str());
}
virtual std::string Variable(const ::reflection::Field &f) const {
virtual std::string Variable(const ::reflection::Field& f) const {
return Variable(f.name()->str());
}
};

View File

@@ -57,7 +57,7 @@ enum class BinaryRegionType {
UOffset64 = 18,
};
template<typename T>
template <typename T>
static inline std::string ToHex(T i, size_t width = sizeof(T)) {
std::stringstream stream;
stream << std::hex << std::uppercase << std::setfill('0')
@@ -205,52 +205,85 @@ struct BinarySection {
inline static BinaryRegionType GetRegionType(reflection::BaseType base_type) {
switch (base_type) {
case reflection::UType: return BinaryRegionType::UType;
case reflection::Bool: return BinaryRegionType::Uint8;
case reflection::Byte: return BinaryRegionType::Uint8;
case reflection::UByte: return BinaryRegionType::Uint8;
case reflection::Short: return BinaryRegionType::Int16;
case reflection::UShort: return BinaryRegionType::Uint16;
case reflection::Int: return BinaryRegionType::Uint32;
case reflection::UInt: return BinaryRegionType::Uint32;
case reflection::Long: return BinaryRegionType::Int64;
case reflection::ULong: return BinaryRegionType::Uint64;
case reflection::Float: return BinaryRegionType::Float;
case reflection::Double: return BinaryRegionType::Double;
default: return BinaryRegionType::Unknown;
case reflection::UType:
return BinaryRegionType::UType;
case reflection::Bool:
return BinaryRegionType::Uint8;
case reflection::Byte:
return BinaryRegionType::Uint8;
case reflection::UByte:
return BinaryRegionType::Uint8;
case reflection::Short:
return BinaryRegionType::Int16;
case reflection::UShort:
return BinaryRegionType::Uint16;
case reflection::Int:
return BinaryRegionType::Uint32;
case reflection::UInt:
return BinaryRegionType::Uint32;
case reflection::Long:
return BinaryRegionType::Int64;
case reflection::ULong:
return BinaryRegionType::Uint64;
case reflection::Float:
return BinaryRegionType::Float;
case reflection::Double:
return BinaryRegionType::Double;
default:
return BinaryRegionType::Unknown;
}
}
inline static std::string ToString(const BinaryRegionType type) {
switch (type) {
case BinaryRegionType::UOffset: return "UOffset32";
case BinaryRegionType::UOffset64: return "UOffset64";
case BinaryRegionType::SOffset: return "SOffset32";
case BinaryRegionType::VOffset: return "VOffset16";
case BinaryRegionType::Bool: return "bool";
case BinaryRegionType::Char: return "char";
case BinaryRegionType::Byte: return "int8_t";
case BinaryRegionType::Uint8: return "uint8_t";
case BinaryRegionType::Uint16: return "uint16_t";
case BinaryRegionType::Uint32: return "uint32_t";
case BinaryRegionType::Uint64: return "uint64_t";
case BinaryRegionType::Int8: return "int8_t";
case BinaryRegionType::Int16: return "int16_t";
case BinaryRegionType::Int32: return "int32_t";
case BinaryRegionType::Int64: return "int64_t";
case BinaryRegionType::Double: return "double";
case BinaryRegionType::Float: return "float";
case BinaryRegionType::UType: return "UType8";
case BinaryRegionType::Unknown: return "?uint8_t";
default: return "todo";
case BinaryRegionType::UOffset:
return "UOffset32";
case BinaryRegionType::UOffset64:
return "UOffset64";
case BinaryRegionType::SOffset:
return "SOffset32";
case BinaryRegionType::VOffset:
return "VOffset16";
case BinaryRegionType::Bool:
return "bool";
case BinaryRegionType::Char:
return "char";
case BinaryRegionType::Byte:
return "int8_t";
case BinaryRegionType::Uint8:
return "uint8_t";
case BinaryRegionType::Uint16:
return "uint16_t";
case BinaryRegionType::Uint32:
return "uint32_t";
case BinaryRegionType::Uint64:
return "uint64_t";
case BinaryRegionType::Int8:
return "int8_t";
case BinaryRegionType::Int16:
return "int16_t";
case BinaryRegionType::Int32:
return "int32_t";
case BinaryRegionType::Int64:
return "int64_t";
case BinaryRegionType::Double:
return "double";
case BinaryRegionType::Float:
return "float";
case BinaryRegionType::UType:
return "UType8";
case BinaryRegionType::Unknown:
return "?uint8_t";
default:
return "todo";
}
}
class BinaryAnnotator {
public:
explicit BinaryAnnotator(const uint8_t *const bfbs,
explicit BinaryAnnotator(const uint8_t* const bfbs,
const uint64_t bfbs_length,
const uint8_t *const binary,
const uint8_t* const binary,
const uint64_t binary_length,
const bool is_size_prefixed)
: bfbs_(bfbs),
@@ -261,8 +294,8 @@ class BinaryAnnotator {
binary_length_(binary_length),
is_size_prefixed_(is_size_prefixed) {}
BinaryAnnotator(const reflection::Schema *schema,
const std::string &root_table, const uint8_t *binary,
BinaryAnnotator(const reflection::Schema* schema,
const std::string& root_table, const uint8_t* binary,
uint64_t binary_length, bool is_size_prefixed)
: bfbs_(nullptr),
bfbs_length_(0),
@@ -277,11 +310,11 @@ class BinaryAnnotator {
private:
struct VTable {
struct Entry {
const reflection::Field *field = nullptr;
const reflection::Field* field = nullptr;
uint16_t offset_from_table = 0;
};
const reflection::Object *referring_table = nullptr;
const reflection::Object* referring_table = nullptr;
// Field ID -> {field def, offset from table}
std::map<uint16_t, Entry> fields;
@@ -296,25 +329,25 @@ class BinaryAnnotator {
// attempts to get an existing vtable given the offset and table type,
// otherwise it will built the vtable, memorize it, and return the built
// VTable. Returns nullptr if building the VTable fails.
VTable *GetOrBuildVTable(uint64_t offset, const reflection::Object *table,
VTable* GetOrBuildVTable(uint64_t offset, const reflection::Object* table,
uint64_t offset_of_referring_table);
void BuildTable(uint64_t offset, const BinarySectionType type,
const reflection::Object *table);
const reflection::Object* table);
uint64_t BuildStruct(uint64_t offset, std::vector<BinaryRegion> &regions,
uint64_t BuildStruct(uint64_t offset, std::vector<BinaryRegion>& regions,
const std::string referring_field_name,
const reflection::Object *structure);
const reflection::Object* structure);
void BuildString(uint64_t offset, const reflection::Object *table,
const reflection::Field *field);
void BuildString(uint64_t offset, const reflection::Object* table,
const reflection::Field* field);
void BuildVector(uint64_t offset, const reflection::Object *table,
const reflection::Field *field, uint64_t parent_table_offset,
void BuildVector(uint64_t offset, const reflection::Object* table,
const reflection::Field* field, uint64_t parent_table_offset,
const std::map<uint16_t, VTable::Entry> vtable_fields);
std::string BuildUnion(uint64_t offset, uint8_t realized_type,
const reflection::Field *field);
const reflection::Field* field);
void FixMissingRegions();
void FixMissingSections();
@@ -325,7 +358,8 @@ class BinaryAnnotator {
// Determines if performing a GetScalar request for `T` at `offset` would read
// passed the end of the binary.
template<typename T> inline bool IsValidRead(const uint64_t offset) const {
template <typename T>
inline bool IsValidRead(const uint64_t offset) const {
return IsValidRead(offset, sizeof(T));
}
@@ -339,9 +373,11 @@ class BinaryAnnotator {
return IsValidOffset(offset) ? binary_length_ - offset : 0;
}
template<typename T>
template <typename T>
flatbuffers::Optional<T> ReadScalar(const uint64_t offset) const {
if (!IsValidRead<T>(offset)) { return flatbuffers::nullopt; }
if (!IsValidRead<T>(offset)) {
return flatbuffers::nullopt;
}
return flatbuffers::ReadScalar<T>(binary_ + offset);
}
@@ -349,11 +385,11 @@ class BinaryAnnotator {
// Adds the provided `section` keyed by the `offset` it occurs at. If a
// section is already added at that offset, it doesn't replace the existing
// one.
void AddSection(const uint64_t offset, const BinarySection &section) {
void AddSection(const uint64_t offset, const BinarySection& section) {
sections_.insert(std::make_pair(offset, section));
}
bool IsInlineField(const reflection::Field *const field) {
bool IsInlineField(const reflection::Field* const field) {
if (field->type()->base_type() == reflection::BaseType::Obj) {
return schema_->objects()->Get(field->type()->index())->is_struct();
}
@@ -365,28 +401,32 @@ class BinaryAnnotator {
type == reflection::BaseType::Union);
}
bool IsUnionType(const reflection::Field *const field) {
bool IsUnionType(const reflection::Field* const field) {
return IsUnionType(field->type()->base_type()) &&
field->type()->index() >= 0;
}
bool IsValidUnionValue(const reflection::Field *const field,
bool IsValidUnionValue(const reflection::Field* const field,
const uint8_t value) {
return IsUnionType(field) &&
IsValidUnionValue(field->type()->index(), value);
}
bool IsValidUnionValue(const uint32_t enum_id, const uint8_t value) {
if (enum_id >= schema_->enums()->size()) { return false; }
if (enum_id >= schema_->enums()->size()) {
return false;
}
const reflection::Enum *enum_def = schema_->enums()->Get(enum_id);
const reflection::Enum* enum_def = schema_->enums()->Get(enum_id);
if (enum_def == nullptr) { return false; }
if (enum_def == nullptr) {
return false;
}
return value < enum_def->values()->size();
}
uint64_t GetElementSize(const reflection::Field *const field) {
uint64_t GetElementSize(const reflection::Field* const field) {
if (IsScalar(field->type()->element())) {
return GetTypeSize(field->type()->element());
}
@@ -396,22 +436,23 @@ class BinaryAnnotator {
auto obj = schema_->objects()->Get(field->type()->index());
return obj->is_struct() ? obj->bytesize() : sizeof(uint32_t);
}
default: return sizeof(uint32_t);
default:
return sizeof(uint32_t);
}
}
bool ContainsSection(const uint64_t offset);
const reflection::Object *RootTable() const;
const reflection::Object* RootTable() const;
// The schema for the binary file
const uint8_t *bfbs_;
const uint8_t* bfbs_;
const uint64_t bfbs_length_;
const reflection::Schema *schema_;
const reflection::Schema* schema_;
const std::string root_table_;
// The binary data itself.
const uint8_t *binary_;
const uint8_t* binary_;
const uint64_t binary_length_;
const bool is_size_prefixed_;