Use the Google Style for clang-format without exceptions (#8706)

This reduces the friction when merging from github and google repos by
using the exact same clang style guide.

MARKDOWN=true
This commit is contained in:
Derek Bailey
2025-09-23 21:19:33 -07:00
committed by GitHub
parent 881eaab706
commit 0e047869da
84 changed files with 8178 additions and 6653 deletions

View File

@@ -15,11 +15,11 @@
namespace flatbuffers {
namespace {
static bool BinaryRegionSort(const BinaryRegion &a, const BinaryRegion &b) {
static bool BinaryRegionSort(const BinaryRegion& a, const BinaryRegion& b) {
return a.offset < b.offset;
}
static void SetError(BinaryRegionComment &comment, BinaryRegionStatus status,
static void SetError(BinaryRegionComment& comment, BinaryRegionStatus status,
std::string message = "") {
comment.status = status;
comment.status_message = message;
@@ -40,7 +40,7 @@ static BinaryRegion MakeBinaryRegion(
return region;
}
static BinarySection MakeBinarySection(const std::string &name,
static BinarySection MakeBinarySection(const std::string& name,
const BinarySectionType type,
std::vector<BinaryRegion> regions) {
BinarySection section;
@@ -50,33 +50,37 @@ static BinarySection MakeBinarySection(const std::string &name,
return section;
}
static BinarySection MakeSingleRegionBinarySection(const std::string &name,
static BinarySection MakeSingleRegionBinarySection(const std::string& name,
const BinarySectionType type,
const BinaryRegion &region) {
const BinaryRegion& region) {
std::vector<BinaryRegion> regions;
regions.push_back(region);
return MakeBinarySection(name, type, std::move(regions));
}
static bool IsNonZeroRegion(const uint64_t offset, const uint64_t length,
const uint8_t *const binary) {
const uint8_t* const binary) {
for (uint64_t i = offset; i < offset + length; ++i) {
if (binary[i] != 0) { return true; }
if (binary[i] != 0) {
return true;
}
}
return false;
}
static bool IsPrintableRegion(const uint64_t offset, const uint64_t length,
const uint8_t *const binary) {
const uint8_t* const binary) {
for (uint64_t i = offset; i < offset + length; ++i) {
if (!isprint(binary[i])) { return false; }
if (!isprint(binary[i])) {
return false;
}
}
return true;
}
static BinarySection GenerateMissingSection(const uint64_t offset,
const uint64_t length,
const uint8_t *const binary) {
const uint8_t* const binary) {
std::vector<BinaryRegion> regions;
// Check if the region is all zeros or not, as that can tell us if it is
@@ -130,7 +134,9 @@ std::map<uint64_t, BinarySection> BinaryAnnotator::Annotate() {
}
// The binary is too short to read as a flatbuffers.
if (binary_length_ < FLATBUFFERS_MIN_BUFFER_SIZE) { return {}; }
if (binary_length_ < FLATBUFFERS_MIN_BUFFER_SIZE) {
return {};
}
// Make sure we start with a clean slate.
vtables_.clear();
@@ -234,20 +240,24 @@ uint64_t BinaryAnnotator::BuildHeader(const uint64_t header_offset) {
return root_table_loc;
}
BinaryAnnotator::VTable *BinaryAnnotator::GetOrBuildVTable(
const uint64_t vtable_offset, const reflection::Object *const table,
BinaryAnnotator::VTable* BinaryAnnotator::GetOrBuildVTable(
const uint64_t vtable_offset, const reflection::Object* const table,
const uint64_t offset_of_referring_table) {
// Get a list of vtables (if any) already defined at this offset.
std::list<VTable> &vtables = vtables_[vtable_offset];
std::list<VTable>& vtables = vtables_[vtable_offset];
// See if this vtable for the table type has been generated before.
for (VTable &vtable : vtables) {
if (vtable.referring_table == table) { return &vtable; }
for (VTable& vtable : vtables) {
if (vtable.referring_table == table) {
return &vtable;
}
}
// If we are trying to make a new vtable and it is already encompassed by
// another binary section, something is corrupted.
if (vtables.empty() && ContainsSection(vtable_offset)) { return nullptr; }
if (vtables.empty() && ContainsSection(vtable_offset)) {
return nullptr;
}
const std::string referring_table_name = table->name()->str();
@@ -352,7 +362,7 @@ BinaryAnnotator::VTable *BinaryAnnotator::GetOrBuildVTable(
uint16_t fields_processed = 0;
// Loop over all the fields.
ForAllFields(table, /*reverse=*/false, [&](const reflection::Field *field) {
ForAllFields(table, /*reverse=*/false, [&](const reflection::Field* field) {
const uint64_t field_offset = offset_start + field->id() * sizeof(uint16_t);
if (field_offset >= vtable_offset + vtable_size) {
@@ -497,8 +507,10 @@ BinaryAnnotator::VTable *BinaryAnnotator::GetOrBuildVTable(
void BinaryAnnotator::BuildTable(const uint64_t table_offset,
const BinarySectionType type,
const reflection::Object *const table) {
if (ContainsSection(table_offset)) { return; }
const reflection::Object* const table) {
if (ContainsSection(table_offset)) {
return;
}
BinaryRegionComment vtable_offset_comment;
vtable_offset_comment.type = BinaryRegionCommentType::TableVTableOffset;
@@ -548,7 +560,7 @@ void BinaryAnnotator::BuildTable(const uint64_t table_offset,
// Parse the vtable first so we know what the rest of the fields in the table
// are.
const VTable *const vtable =
const VTable* const vtable =
GetOrBuildVTable(vtable_offset, table, table_offset);
if (vtable == nullptr) {
@@ -571,18 +583,18 @@ void BinaryAnnotator::BuildTable(const uint64_t table_offset,
// not by their IDs. So copy them over to another vector that we can sort on
// the offset_from_table property.
std::vector<VTable::Entry> fields;
for (const auto &vtable_field : vtable->fields) {
for (const auto& vtable_field : vtable->fields) {
fields.push_back(vtable_field.second);
}
std::stable_sort(fields.begin(), fields.end(),
[](const VTable::Entry &a, const VTable::Entry &b) {
[](const VTable::Entry& a, const VTable::Entry& b) {
return a.offset_from_table < b.offset_from_table;
});
// Iterate over all the fields by order of their offset.
for (size_t i = 0; i < fields.size(); ++i) {
const reflection::Field *field = fields[i].field;
const reflection::Field* field = fields[i].field;
const uint16_t offset_from_table = fields[i].offset_from_table;
if (offset_from_table == 0) {
@@ -610,7 +622,9 @@ void BinaryAnnotator::BuildTable(const uint64_t table_offset,
: table_end_offset) -
field_offset;
if (unknown_field_length == 0) { continue; }
if (unknown_field_length == 0) {
continue;
}
std::string hint;
@@ -741,7 +755,7 @@ void BinaryAnnotator::BuildTable(const uint64_t table_offset,
switch (field->type()->base_type()) {
case reflection::BaseType::Obj: {
const reflection::Object *next_object =
const reflection::Object* next_object =
schema_->objects()->Get(field->type()->index());
if (next_object->is_struct()) {
@@ -830,7 +844,8 @@ void BinaryAnnotator::BuildTable(const uint64_t table_offset,
} break;
default: break;
default:
break;
}
}
@@ -855,14 +870,16 @@ void BinaryAnnotator::BuildTable(const uint64_t table_offset,
}
uint64_t BinaryAnnotator::BuildStruct(const uint64_t struct_offset,
std::vector<BinaryRegion> &regions,
std::vector<BinaryRegion>& regions,
const std::string referring_field_name,
const reflection::Object *const object) {
if (!object->is_struct()) { return struct_offset; }
const reflection::Object* const object) {
if (!object->is_struct()) {
return struct_offset;
}
uint64_t offset = struct_offset;
// Loop over all the fields in increasing order
ForAllFields(object, /*reverse=*/false, [&](const reflection::Field *field) {
ForAllFields(object, /*reverse=*/false, [&](const reflection::Field* field) {
if (IsScalar(field->type()->base_type())) {
// Structure Field value
const uint64_t type_size = GetTypeSize(field->type()->base_type());
@@ -971,11 +988,13 @@ uint64_t BinaryAnnotator::BuildStruct(const uint64_t struct_offset,
}
void BinaryAnnotator::BuildString(const uint64_t string_offset,
const reflection::Object *const table,
const reflection::Field *const field) {
const reflection::Object* const table,
const reflection::Field* const field) {
// Check if we have already generated this string section, and this is a
// shared string instance.
if (ContainsSection(string_offset)) { return; }
if (ContainsSection(string_offset)) {
return;
}
std::vector<BinaryRegion> regions;
const auto string_length = ReadScalar<uint32_t>(string_offset);
@@ -1034,10 +1053,12 @@ void BinaryAnnotator::BuildString(const uint64_t string_offset,
}
void BinaryAnnotator::BuildVector(
const uint64_t vector_offset, const reflection::Object *const table,
const reflection::Field *const field, const uint64_t parent_table_offset,
const uint64_t vector_offset, const reflection::Object* const table,
const reflection::Field* const field, const uint64_t parent_table_offset,
const std::map<uint16_t, VTable::Entry> vtable_fields) {
if (ContainsSection(vector_offset)) { return; }
if (ContainsSection(vector_offset)) {
return;
}
BinaryRegionComment vector_length_comment;
vector_length_comment.type = BinaryRegionCommentType::VectorLength;
@@ -1052,13 +1073,17 @@ void BinaryAnnotator::BuildVector(
if (is_64_bit_vector) {
auto v = ReadScalar<uint64_t>(vector_offset);
if (v.has_value()) { vector_length = v.value(); }
if (v.has_value()) {
vector_length = v.value();
}
vector_length_size_type = sizeof(uint64_t);
region_type = BinaryRegionType::Uint64;
section_type = BinarySectionType::Vector64;
} else {
auto v = ReadScalar<uint32_t>(vector_offset);
if (v.has_value()) { vector_length = v.value(); }
if (v.has_value()) {
vector_length = v.value();
}
vector_length_size_type = sizeof(uint32_t);
region_type = BinaryRegionType::Uint32;
section_type = BinarySectionType::Vector;
@@ -1108,7 +1133,7 @@ void BinaryAnnotator::BuildVector(
switch (field->type()->element()) {
case reflection::BaseType::Obj: {
const reflection::Object *object =
const reflection::Object* object =
schema_->objects()->Get(field->type()->index());
if (object->is_struct()) {
@@ -1117,7 +1142,9 @@ void BinaryAnnotator::BuildVector(
// Structs are inline to the vector.
const uint64_t next_offset =
BuildStruct(offset, regions, "[" + NumToString(i) + "]", object);
if (next_offset == offset) { break; }
if (next_offset == offset) {
break;
}
offset = next_offset;
}
} else {
@@ -1382,18 +1409,20 @@ void BinaryAnnotator::BuildVector(
std::string BinaryAnnotator::BuildUnion(const uint64_t union_offset,
const uint8_t realized_type,
const reflection::Field *const field) {
const reflection::Enum *next_enum =
const reflection::Field* const field) {
const reflection::Enum* next_enum =
schema_->enums()->Get(field->type()->index());
const reflection::EnumVal *enum_val = next_enum->values()->Get(realized_type);
const reflection::EnumVal* enum_val = next_enum->values()->Get(realized_type);
if (ContainsSection(union_offset)) { return enum_val->name()->c_str(); }
if (ContainsSection(union_offset)) {
return enum_val->name()->c_str();
}
const reflection::Type *union_type = enum_val->union_type();
const reflection::Type* union_type = enum_val->union_type();
if (union_type->base_type() == reflection::BaseType::Obj) {
const reflection::Object *object =
const reflection::Object* object =
schema_->objects()->Get(union_type->index());
if (object->is_struct()) {
@@ -1418,8 +1447,8 @@ std::string BinaryAnnotator::BuildUnion(const uint64_t union_offset,
void BinaryAnnotator::FixMissingRegions() {
std::vector<BinaryRegion> regions_to_insert;
for (auto &current_section : sections_) {
BinarySection &section = current_section.second;
for (auto& current_section : sections_) {
BinarySection& section = current_section.second;
if (section.regions.empty()) {
// TODO(dbaileychess): is this possible?
continue;
@@ -1427,7 +1456,7 @@ void BinaryAnnotator::FixMissingRegions() {
uint64_t offset = section.regions[0].offset + section.regions[0].length;
for (size_t i = 1; i < section.regions.size(); ++i) {
BinaryRegion &region = section.regions[i];
BinaryRegion& region = section.regions[i];
const uint64_t next_offset = region.offset;
if (!IsValidOffset(next_offset)) {
@@ -1470,8 +1499,8 @@ void BinaryAnnotator::FixMissingSections() {
std::vector<BinarySection> sections_to_insert;
for (auto &current_section : sections_) {
BinarySection &section = current_section.second;
for (auto& current_section : sections_) {
BinarySection& section = current_section.second;
const uint64_t section_start_offset = current_section.first;
const uint64_t section_end_offset =
section.regions.back().offset + section.regions.back().length;
@@ -1494,7 +1523,7 @@ void BinaryAnnotator::FixMissingSections() {
GenerateMissingSection(offset - 1, pad_bytes, binary_));
}
for (const BinarySection &section_to_insert : sections_to_insert) {
for (const BinarySection& section_to_insert : sections_to_insert) {
AddSection(section_to_insert.regions[0].offset, section_to_insert);
}
}
@@ -1502,11 +1531,15 @@ void BinaryAnnotator::FixMissingSections() {
bool BinaryAnnotator::ContainsSection(const uint64_t offset) {
auto it = sections_.lower_bound(offset);
// If the section is found, check that it is exactly equal its offset.
if (it != sections_.end() && it->first == offset) { return true; }
if (it != sections_.end() && it->first == offset) {
return true;
}
// If this was the first section, there are no other previous sections to
// check.
if (it == sections_.begin()) { return false; }
if (it == sections_.begin()) {
return false;
}
// Go back one section.
--it;
@@ -1516,7 +1549,7 @@ bool BinaryAnnotator::ContainsSection(const uint64_t offset) {
it->second.regions.back().length;
}
const reflection::Object *BinaryAnnotator::RootTable() const {
const reflection::Object* BinaryAnnotator::RootTable() const {
if (!root_table_.empty()) {
return schema_->objects()->LookupByKey(root_table_);
}