From 6ede404cf13233bd75f6af4fc487649c7bfd27c7 Mon Sep 17 00:00:00 2001 From: Alexandre Abadie Date: Thu, 7 Apr 2022 11:14:49 +0200 Subject: [PATCH 1/1] Silence Wcast-align in public headers --- include/flatbuffers/flatbuffers.h | 42 +++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index ee34d54e..52cad4c6 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -107,7 +107,10 @@ template struct IndirectHelper { typedef T mutable_return_type; static const size_t element_stride = sizeof(T); static return_type Read(const uint8_t *p, uoffset_t i) { +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-align" return EndianScalar((reinterpret_cast(p))[i]); +#pragma GCC diagnostic pop } }; template struct IndirectHelper> { @@ -116,7 +119,10 @@ template struct IndirectHelper> { static const size_t element_stride = sizeof(uoffset_t); static return_type Read(const uint8_t *p, uoffset_t i) { p += i * sizeof(uoffset_t); +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-align" return reinterpret_cast(p + ReadScalar(p)); +#pragma GCC diagnostic pop } }; template struct IndirectHelper { @@ -1034,12 +1040,18 @@ class vector_downward { // Specialized version of push() that avoids memcpy call for small data. template void push_small(const T &little_endian_t) { make_space(sizeof(T)); +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-align" *reinterpret_cast(cur_) = little_endian_t; +#pragma GCC diagnostic pop } template void scratch_push_small(const T &t) { ensure_space(sizeof(T)); +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-align" *reinterpret_cast(scratch_) = t; +#pragma GCC diagnostic pop scratch_ += sizeof(T); } @@ -1121,13 +1133,19 @@ const T *data(const std::vector &v) { // Eventually the returned pointer gets passed down to memcpy, so // we need it to be non-null to avoid undefined behavior. static uint8_t t; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-align" return v.empty() ? reinterpret_cast(&t) : &v.front(); +#pragma GCC diagnostic pop } template T *data(std::vector &v) { // Eventually the returned pointer gets passed down to memcpy, so // we need it to be non-null to avoid undefined behavior. static uint8_t t; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-align" return v.empty() ? reinterpret_cast(&t) : &v.front(); +#pragma GCC diagnostic pop } /// @endcond @@ -1465,7 +1483,10 @@ class FlatBufferBuilder { // Write the offsets into the table for (auto it = buf_.scratch_end() - num_field_loc * sizeof(FieldLoc); it < buf_.scratch_end(); it += sizeof(FieldLoc)) { +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-align" auto field_location = reinterpret_cast(it); +#pragma GCC diagnostic pop auto pos = static_cast(vtableoffsetloc - field_location->off); // If this asserts, it means you've set a field twice. FLATBUFFERS_ASSERT( @@ -1473,7 +1494,10 @@ class FlatBufferBuilder { WriteScalar(buf_.data() + field_location->id, pos); } ClearOffsets(); +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-align" auto vt1 = reinterpret_cast(buf_.data()); +#pragma GCC diagnostic pop auto vt1_size = ReadScalar(vt1); auto vt_use = GetSize(); // See if we already have generated a vtable with this exact same @@ -1481,8 +1505,11 @@ class FlatBufferBuilder { if (dedup_vtables_) { for (auto it = buf_.scratch_data(); it < buf_.scratch_end(); it += sizeof(uoffset_t)) { +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-align" auto vt_offset_ptr = reinterpret_cast(it); auto vt2 = reinterpret_cast(buf_.data_at(*vt_offset_ptr)); +#pragma GCC diagnostic pop auto vt2_size = ReadScalar(vt2); if (vt1_size != vt2_size || 0 != memcmp(vt2, vt1, vt1_size)) continue; vt_use = *vt_offset_ptr; @@ -2184,8 +2211,11 @@ class FlatBufferBuilder { struct StringOffsetCompare { StringOffsetCompare(const vector_downward &buf) : buf_(&buf) {} bool operator()(const Offset &a, const Offset &b) const { +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-align" auto stra = reinterpret_cast(buf_->data_at(a.o)); auto strb = reinterpret_cast(buf_->data_at(b.o)); +#pragma GCC diagnostic pop return StringLessThan(stra->data(), stra->size(), strb->data(), strb->size()); } @@ -2578,8 +2608,11 @@ class Table { template P GetPointer(voffset_t field) { auto field_offset = GetOptionalFieldOffset(field); auto p = data_ + field_offset; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-align" return field_offset ? reinterpret_cast

(p + ReadScalar(p)) : nullptr; +#pragma GCC diagnostic pop } template P GetPointer(voffset_t field) const { return const_cast(this)->GetPointer

(field); @@ -2588,7 +2621,10 @@ class Table { template P GetStruct(voffset_t field) const { auto field_offset = GetOptionalFieldOffset(field); auto p = const_cast(data_ + field_offset); +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-align" return field_offset ? reinterpret_cast

(p) : nullptr; +#pragma GCC diagnostic pop } template @@ -2691,7 +2727,10 @@ inline flatbuffers::Optional Table::GetOptional( template void FlatBufferBuilder::Required(Offset table, voffset_t field) { +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-align" auto table_ptr = reinterpret_cast(buf_.data_at(table.o)); +#pragma GCC diagnostic pop bool ok = table_ptr->GetOptionalFieldOffset(field) != 0; // If this fails, the caller will show what field needs to be set. FLATBUFFERS_ASSERT(ok); @@ -2703,7 +2742,10 @@ void FlatBufferBuilder::Required(Offset table, voffset_t field) { /// This may be useful if you want to pass on a root and have the recipient /// delete the buffer afterwards. inline const uint8_t *GetBufferStartFromRootPointer(const void *root) { +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-align" auto table = reinterpret_cast(root); +#pragma GCC diagnostic pop auto vtable = table->GetVTable(); // Either the vtable is before the root or after the root. auto start = (std::min)(vtable, reinterpret_cast(root)); -- 2.32.0