Compare commits

...
Author SHA1 Message Date
Anshul Nigham 9698e11376 Fix unused variable warnings across code generators
Removing `_LIBCPP_DISABLE_UNUSED_STRUCT_WARNINGS` from libc++ enables
`[[gnu::__warn_unused__]]` on standard library types (such as
`std::string` and `std::set`), surfacing `-Wunused-variable` warnings.
See [upstream LLVM commit](https://github.com/llvm/llvm-project/commit/ab8b037ef7b398aa864959a69f328fa69b567f74) for reference.

- Remove unreferenced variable declarations whose initializers have no
  side effects (`generated_functions`, `offset_prefix`, `offset_size`,
  `filebase`, `field_name`, `offset`, `ts_file_path_rel`,
  `ts_file_path`, and `union_accessor`).
- Annotate variables initialized via helper or member calls with
  `[[maybe_unused]]` (`offset_str`, `get_field`, `qualified`,
  `struct_var`, `struct_type`, `type`, `def_mod_name`, and
  `ts_file_path`).

PiperOrigin-RevId: 999136370
2026-10-23 18:40:05 +00:00
Derek Bailey 6dda43bbaa Ensure embedded FlatBuffers binary schemas are 8-byte aligned (#9425)
In `src/idl_gen_cpp.cpp`, code generated by `flatc --bfbs-gen-embed` emits `static const uint8_t bfbsData[...] = { ... };`. Because `uint8_t` has 1-byte alignment, the array may be placed at an unaligned address in `.rodata`, triggering alignment traps under Clang UBSan on Arm when accessed via FlatBuffers root accessors.

Add `alignas(uint64_t)` to the generated `bfbsData` array declaration in `GenerateBinarySchema` so embedded binary schemas are 8-byte aligned across all architectures.

PiperOrigin-RevId: 1003962636
2026-10-21 13:37:51 -07:00
Derek Bailey 9a8a511b52 Enable locale-independent strtod_l/strtof_l on Linux glibc (#9424)
Restrict the `__linux__` check for `FLATBUFFERS_LOCALE_INDEPENDENT` in `include/flatbuffers/base.h` to `(!defined(__linux__) || defined(__GLIBC__))` so Linux with glibc enables `strtod_l`/`strtof_l`.

PiperOrigin-RevId: 1002266254
2026-10-21 13:25:17 -07:00
Wouter van Oortmerssen b8431fbcd7 Lobster: change builder size constants (enum -> let)
This is because newer versions of the Lobster compiler reject enums with the same values, and older compilers are fine with the let versions also.
2026-09-14 11:02:13 -07:00
12 changed files with 19 additions and 36 deletions
-1
View File
@@ -504,7 +504,6 @@ grpc::string GenerateInterface(grpc_generator::File* file,
const grpc::string& filename) { const grpc::string& filename) {
grpc::string output; grpc::string output;
std::set<grpc::string> generated_functions;
std::map<grpc::string, grpc::string> vars; std::map<grpc::string, grpc::string> vars;
vars["PATH"] = file->package(); vars["PATH"] = file->package();
+1 -1
View File
@@ -271,7 +271,7 @@ namespace flatbuffers {
#if (defined(_MSC_VER) && _MSC_VER >= 1800) || \ #if (defined(_MSC_VER) && _MSC_VER >= 1800) || \
(defined(__ANDROID__) && defined(__ANDROID_API__) && __ANDROID_API__>= 26) || \ (defined(__ANDROID__) && defined(__ANDROID_API__) && __ANDROID_API__>= 26) || \
(defined(_XOPEN_VERSION) && (_XOPEN_VERSION >= 700)) && \ (defined(_XOPEN_VERSION) && (_XOPEN_VERSION >= 700)) && \
!defined(__Fuchsia__) !defined(__Fuchsia__) && (!defined(__linux__) || defined(__GLIBC__))
#define FLATBUFFERS_LOCALE_INDEPENDENT 1 #define FLATBUFFERS_LOCALE_INDEPENDENT 1
#else #else
#define FLATBUFFERS_LOCALE_INDEPENDENT 0 #define FLATBUFFERS_LOCALE_INDEPENDENT 0
+8 -9
View File
@@ -24,15 +24,14 @@ class handle:
struct offset: struct offset:
o:int o:int
enum sizeof: let sz_8 = 1
sz_8 = 1 let sz_16 = 2
sz_16 = 2 let sz_32 = 4
sz_32 = 4 let sz_64 = 8
sz_64 = 8 let sz_voffset = 2
sz_voffset = 2 let sz_uoffset = 4
sz_uoffset = 4 let sz_soffset = 4
sz_soffset = 4 let sz_metadata_fields = 2
sz_metadata_fields = 2
class builder: class builder:
buf = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" buf = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
-2
View File
@@ -722,8 +722,6 @@ void BinaryAnnotator::BuildTable(const uint64_t table_offset,
BinaryRegionComment offset_field_comment; BinaryRegionComment offset_field_comment;
offset_field_comment.type = BinaryRegionCommentType::TableOffsetField; offset_field_comment.type = BinaryRegionCommentType::TableOffsetField;
offset_field_comment.name = field->name()->c_str(); offset_field_comment.name = field->name()->c_str();
const std::string offset_prefix =
"offset to field `" + std::string(field->name()->c_str()) + "`";
// Validate any field that isn't inline (i.e., non-structs). // Validate any field that isn't inline (i.e., non-structs).
if (!IsInlineField(field)) { if (!IsInlineField(field)) {
+2 -3
View File
@@ -404,7 +404,7 @@ class CppGenerator : public BaseGenerator {
code_ += "struct {{STRUCT_NAME}}BinarySchema {"; code_ += "struct {{STRUCT_NAME}}BinarySchema {";
code_ += " static const uint8_t *data() {"; code_ += " static const uint8_t *data() {";
code_ += " // Buffer containing the binary schema."; code_ += " // Buffer containing the binary schema.";
code_ += " static const uint8_t bfbsData[" + code_ += " alignas(uint64_t) static const uint8_t bfbsData[" +
NumToString(parser_.builder_.GetSize()) + "] = {"; NumToString(parser_.builder_.GetSize()) + "] = {";
code_ += binary_schema_hex_text; code_ += binary_schema_hex_text;
code_ += " };"; code_ += " };";
@@ -2737,7 +2737,6 @@ class CppGenerator : public BaseGenerator {
if (!field.IsScalarOptional()) { if (!field.IsScalarOptional()) {
const bool is_scalar = IsScalar(type.base_type); const bool is_scalar = IsScalar(type.base_type);
std::string accessor; std::string accessor;
std::string offset_size = "";
if (is_scalar) { if (is_scalar) {
accessor = "GetField<"; accessor = "GetField<";
} else if (IsStruct(type)) { } else if (IsStruct(type)) {
@@ -2815,7 +2814,7 @@ class CppGenerator : public BaseGenerator {
void GenTableFieldType(const FieldDef& field) { void GenTableFieldType(const FieldDef& field) {
const auto& type = field.value.type; const auto& type = field.value.type;
const auto offset_str = GenFieldOffsetName(field); [[maybe_unused]] const auto offset_str = GenFieldOffsetName(field);
if (!field.IsScalarOptional()) { if (!field.IsScalarOptional()) {
std::string afterptr = " *" + NullableExtension(); std::string afterptr = " *" + NullableExtension();
code_.SetValue("FIELD_TYPE", code_.SetValue("FIELD_TYPE",
-2
View File
@@ -1149,8 +1149,6 @@ static bool GenerateDart(const Parser& parser, const std::string& path,
static std::string DartMakeRule(const Parser& parser, const std::string& path, static std::string DartMakeRule(const Parser& parser, const std::string& path,
const std::string& file_name) { const std::string& file_name) {
auto filebase =
flatbuffers::StripPath(flatbuffers::StripExtension(file_name));
dart::DartGenerator generator(parser, path, file_name); dart::DartGenerator generator(parser, path, file_name);
auto make_rule = generator.Filename("") + ": "; auto make_rule = generator.Filename("") + ": ";
+1 -1
View File
@@ -2176,7 +2176,7 @@ class JavaGenerator : public BaseGenerator {
if (field.deprecated) continue; if (field.deprecated) continue;
if (field.value.type.base_type == BASE_TYPE_UTYPE) continue; if (field.value.type.base_type == BASE_TYPE_UTYPE) continue;
if (field.value.type.element == BASE_TYPE_UTYPE) continue; if (field.value.type.element == BASE_TYPE_UTYPE) continue;
const auto get_field = namer_.Method("get", field); [[maybe_unused]] const auto get_field = namer_.Method("get", field);
code += " this." + namer_.Field(field) + " = "; code += " this." + namer_.Field(field) + " = ";
const auto type_name = const auto type_name =
-2
View File
@@ -966,8 +966,6 @@ class KotlinGenerator : public BaseGenerator {
auto bbgetter = ByteBufferGetter(field.value.type, "bb"); auto bbgetter = ByteBufferGetter(field.value.type, "bb");
auto ucast = CastToUsigned(field); auto ucast = CastToUsigned(field);
auto offset_val = NumToString(field.value.offset); auto offset_val = NumToString(field.value.offset);
auto offset_prefix =
"val o = __offset(" + offset_val + "); return o != 0 ? ";
auto value_base_type = field.value.type.base_type; auto value_base_type = field.value.type.base_type;
// Most field accessors need to retrieve and test the field offset // Most field accessors need to retrieve and test the field offset
// first, this is the offset value for that: // first, this is the offset value for that:
+1 -5
View File
@@ -146,7 +146,7 @@ class KotlinKMPGenerator : public BaseGenerator {
std::string code = std::string code =
"// " + std::string(FlatBuffersGeneratedWarning()) + "\n\n"; "// " + std::string(FlatBuffersGeneratedWarning()) + "\n\n";
auto qualified = ns.GetFullyQualifiedName(""); [[maybe_unused]] auto qualified = ns.GetFullyQualifiedName("");
std::string namespace_name = namer_.Namespace(ns); std::string namespace_name = namer_.Namespace(ns);
if (!namespace_name.empty()) { if (!namespace_name.empty()) {
code += "package " + namespace_name; code += "package " + namespace_name;
@@ -341,7 +341,6 @@ class KotlinKMPGenerator : public BaseGenerator {
std::string GenDefaultBufferValue(const FieldDef& field) const { std::string GenDefaultBufferValue(const FieldDef& field) const {
auto& value = field.value; auto& value = field.value;
auto base_type = value.type.base_type; auto base_type = value.type.base_type;
auto field_name = field.name;
std::string suffix = IsScalar(base_type) ? LiteralSuffix(value.type) : ""; std::string suffix = IsScalar(base_type) ? LiteralSuffix(value.type) : "";
if (field.IsScalarOptional()) { if (field.IsScalarOptional()) {
return "null"; return "null";
@@ -373,7 +372,6 @@ class KotlinKMPGenerator : public BaseGenerator {
std::string GenDefaultValue(const FieldDef& field) const { std::string GenDefaultValue(const FieldDef& field) const {
auto& value = field.value; auto& value = field.value;
auto base_type = value.type.base_type; auto base_type = value.type.base_type;
auto field_name = field.name;
std::string suffix = LiteralSuffix(value.type); std::string suffix = LiteralSuffix(value.type);
if (field.IsScalarOptional()) { if (field.IsScalarOptional()) {
return "null"; return "null";
@@ -1082,8 +1080,6 @@ class KotlinKMPGenerator : public BaseGenerator {
auto return_type = GetterReturnType(field); auto return_type = GetterReturnType(field);
auto bbgetter = ByteBufferGetter(field.value.type, "bb"); auto bbgetter = ByteBufferGetter(field.value.type, "bb");
auto offset_val = NumToString(field.value.offset); auto offset_val = NumToString(field.value.offset);
auto offset_prefix =
"val o = offset(" + offset_val + "); return o != 0 ? ";
auto value_base_type = field.value.type.base_type; auto value_base_type = field.value.type.base_type;
// Most field accessors need to retrieve and test the field offset // Most field accessors need to retrieve and test the field offset
// first, this is the offset value for that: // first, this is the offset value for that:
+2 -2
View File
@@ -2010,8 +2010,8 @@ class PythonGenerator : public BaseGenerator {
void InitializeFromPackedBuf(const StructDef& struct_def, void InitializeFromPackedBuf(const StructDef& struct_def,
std::string* code_ptr) const { std::string* code_ptr) const {
auto& code = *code_ptr; auto& code = *code_ptr;
const auto struct_var = namer_.Variable(struct_def); [[maybe_unused]] const auto struct_var = namer_.Variable(struct_def);
const auto struct_type = namer_.Type(struct_def); [[maybe_unused]] const auto struct_type = namer_.Type(struct_def);
code += GenIndents(1) + "@classmethod"; code += GenIndents(1) + "@classmethod";
code += GenIndents(1) + "def InitFromPackedBuf(cls, buf, pos=0):"; code += GenIndents(1) + "def InitFromPackedBuf(cls, buf, pos=0):";
+1 -3
View File
@@ -787,7 +787,6 @@ class SwiftGenerator : public BaseGenerator {
} }
void GenTableReaderFields(const FieldDef& field) { void GenTableReaderFields(const FieldDef& field) {
const auto offset = NumToString(field.value.offset);
const auto field_field = namer_.Field(field); const auto field_field = namer_.Field(field);
const auto type = GenType(field.value.type); const auto type = GenType(field.value.type);
code_.SetValue("FIELDVAR", namer_.Variable(field)); code_.SetValue("FIELDVAR", namer_.Variable(field));
@@ -1142,7 +1141,6 @@ class SwiftGenerator : public BaseGenerator {
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;
const auto offset = NumToString(field.value.offset);
code_.SetValue("FIELDVAR", namer_.Variable(field)); code_.SetValue("FIELDVAR", namer_.Variable(field));
code_.SetValue("VALUETYPE", GenerateVerifierType(field)); code_.SetValue("VALUETYPE", GenerateVerifierType(field));
@@ -1382,7 +1380,7 @@ class SwiftGenerator : public BaseGenerator {
const auto& field = **it; const auto& field = **it;
if (field.deprecated) continue; if (field.deprecated) continue;
const auto type = GenType(field.value.type); [[maybe_unused]] const auto type = GenType(field.value.type);
code_.SetValue("FIELDVAR", namer_.Variable(field)); code_.SetValue("FIELDVAR", namer_.Variable(field));
if (IsArray(field.value.type)) { if (IsArray(field.value.type)) {
code_.SetValue( code_.SetValue(
+3 -5
View File
@@ -196,7 +196,8 @@ class TsGenerator : public BaseGenerator {
SkipDir::OutputPathAndTrailingPathSeparator); SkipDir::OutputPathAndTrailingPathSeparator);
symbolic_name = definition.defined_namespace->components.back(); symbolic_name = definition.defined_namespace->components.back();
} else { } else {
auto def_mod_name = namer_.File(definition, SkipFile::SuffixAndExtension); [[maybe_unused]] auto def_mod_name =
namer_.File(definition, SkipFile::SuffixAndExtension);
symbolic_name = file_name_; symbolic_name = file_name_;
filepath = path_ + symbolic_name + ".ts"; filepath = path_ + symbolic_name + ".ts";
} }
@@ -286,12 +287,11 @@ class TsGenerator : public BaseGenerator {
auto base_name = auto base_name =
namer_.Directories(it.second.ns->components, SkipDir::OutputPath) + namer_.Directories(it.second.ns->components, SkipDir::OutputPath) +
base_file_name; base_file_name;
auto ts_file_path = base_name + ".ts"; [[maybe_unused]] auto ts_file_path = base_name + ".ts";
auto base_name_rel = std::string("./"); auto base_name_rel = std::string("./");
base_name_rel += base_name_rel +=
namer_.Directories(rel_components, SkipDir::OutputPath); namer_.Directories(rel_components, SkipDir::OutputPath);
base_name_rel += base_file_name; base_name_rel += base_file_name;
auto ts_file_path_rel = base_name_rel + ".ts";
auto type_name = def.first; auto type_name = def.first;
auto fully_qualified_type_name = auto fully_qualified_type_name =
it.second.ns->GetFullyQualifiedName(type_name); it.second.ns->GetFullyQualifiedName(type_name);
@@ -311,7 +311,6 @@ class TsGenerator : public BaseGenerator {
const auto child_ns_level = it.second.ns->components.size() + 1; const auto child_ns_level = it.second.ns->components.size() + 1;
for (const auto& it2 : ns_defs_) { for (const auto& it2 : ns_defs_) {
if (it2.second.ns->components.size() != child_ns_level) continue; if (it2.second.ns->components.size() != child_ns_level) continue;
auto ts_file_path = it2.second.path + ".ts";
code += "export * as " + it2.second.symbolic_name + " from './"; code += "export * as " + it2.second.symbolic_name + " from './";
int count = it2.second.ns->components.size() > 1 ? 2 : 1; int count = it2.second.ns->components.size() > 1 ? 2 : 1;
std::vector<std::string> rel_path; std::vector<std::string> rel_path;
@@ -1176,7 +1175,6 @@ class TsGenerator : public BaseGenerator {
if (union_type.enum_def) { if (union_type.enum_def) {
const auto& enum_def = *union_type.enum_def; const auto& enum_def = *union_type.enum_def;
const auto enum_type = AddImport(imports, dependent, enum_def).name; const auto enum_type = AddImport(imports, dependent, enum_def).name;
const std::string union_accessor = "this." + field_name;
const auto union_has_string = UnionHasStringType(enum_def); const auto union_has_string = UnionHasStringType(enum_def);
const auto field_binded_method = "this." + field_name + ".bind(this)"; const auto field_binded_method = "this." + field_name + ".bind(this)";