From 97d087a40c8b0f1496aba4072047fb7c35f5ef8d Mon Sep 17 00:00:00 2001 From: Connor Smith Date: Tue, 19 Aug 2025 12:15:17 -0700 Subject: [PATCH] Fix --gen-compare to not generate comparators for native types. Per the definition of --gen-compare: only generate comparators for object API generated structs. Types annoated with native_type must define their own comparators if `--gen-compare` is enabled. Also enables --gen-compare for native_type_test and fixes the test by adding a comparator for the Native::Vector3D type. --- CMakeLists.txt | 5 ++--- scripts/generate_code.py | 2 +- src/idl_gen_cpp.cpp | 9 ++++++++- tests/native_type_test_generated.h | 17 +++++++++++++++++ tests/native_type_test_impl.h | 4 ++++ 5 files changed, 32 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f7f388f83ec..93dac0aaf98 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -537,14 +537,13 @@ if(FLATBUFFERS_BUILD_TESTS) add_definitions(-DFLATBUFFERS_TEST_PATH_PREFIX=${CMAKE_CURRENT_SOURCE_DIR}/) # The flattest target needs some generated files - SET(FLATC_OPT --cpp --gen-mutable --gen-object-api --reflect-names) - SET(FLATC_OPT_COMP ${FLATC_OPT};--gen-compare) + SET(FLATC_OPT_COMP --cpp --gen-compare --gen-mutable --gen-object-api --reflect-names) SET(FLATC_OPT_SCOPED_ENUMS ${FLATC_OPT_COMP};--scoped-enums) compile_schema_for_test(tests/alignment_test.fbs "${FLATC_OPT_COMP}") compile_schema_for_test(tests/arrays_test.fbs "${FLATC_OPT_SCOPED_ENUMS}") compile_schema_for_test(tests/native_inline_table_test.fbs "${FLATC_OPT_COMP}") - compile_schema_for_test(tests/native_type_test.fbs "${FLATC_OPT}") + compile_schema_for_test(tests/native_type_test.fbs "${FLATC_OPT_COMP}") compile_schema_for_test(tests/key_field/key_field_sample.fbs "${FLATC_OPT_COMP}") compile_schema_for_test(tests/64bit/test_64bit.fbs "${FLATC_OPT_COMP};--bfbs-gen-embed") compile_schema_for_test(tests/64bit/evolution/v1.fbs "${FLATC_OPT_COMP}") diff --git a/scripts/generate_code.py b/scripts/generate_code.py index 94a49ec9104..e2a71884b17 100755 --- a/scripts/generate_code.py +++ b/scripts/generate_code.py @@ -355,7 +355,7 @@ def glob(path, pattern): ) flatc( - ["--cpp", "--gen-mutable", "--gen-object-api", "--reflect-names"], + ["--cpp", "--gen-compare", "--gen-mutable", "--gen-object-api", "--reflect-names"], schema="native_type_test.fbs", ) diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 38f68b7262d..719d2272a90 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -512,7 +512,8 @@ class CppGenerator : public BaseGenerator { // Generate forward declarations for all equal operators if (opts_.generate_object_based_api && opts_.gen_compare) { for (const auto& struct_def : parser_.structs_.vec) { - if (!struct_def->generated) { + const auto native_type = struct_def->attributes.Lookup("native_type"); + if (!struct_def->generated && !native_type) { SetNameSpace(struct_def->defined_namespace); auto nativeName = NativeName(Name(*struct_def), struct_def, opts_); code_ += "bool operator==(const " + nativeName + " &lhs, const " + @@ -2190,6 +2191,12 @@ class CppGenerator : public BaseGenerator { void GenCompareOperator(const StructDef& struct_def, const std::string& accessSuffix = "") { + // Do not generate compare operators for native types. + const auto native_type = struct_def.attributes.Lookup("native_type"); + if (native_type) { + return; + } + std::string compare_op; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { diff --git a/tests/native_type_test_generated.h b/tests/native_type_test_generated.h index 608c8d45b52..e4708e36aac 100644 --- a/tests/native_type_test_generated.h +++ b/tests/native_type_test_generated.h @@ -25,6 +25,9 @@ struct ApplicationData; struct ApplicationDataBuilder; struct ApplicationDataT; +bool operator==(const ApplicationDataT &lhs, const ApplicationDataT &rhs); +bool operator!=(const ApplicationDataT &lhs, const ApplicationDataT &rhs); + inline const ::flatbuffers::TypeTable *Vector3DTypeTable(); inline const ::flatbuffers::TypeTable *Vector3DAltTypeTable(); @@ -235,6 +238,20 @@ inline ::flatbuffers::Offset CreateApplicationDataDirect( ::flatbuffers::Offset CreateApplicationData(::flatbuffers::FlatBufferBuilder &_fbb, const ApplicationDataT *_o, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr); + +inline bool operator==(const ApplicationDataT &lhs, const ApplicationDataT &rhs) { + return + (lhs.vectors == rhs.vectors) && + (lhs.vectors_alt == rhs.vectors_alt) && + ((lhs.position == rhs.position) || (lhs.position && rhs.position && *lhs.position == *rhs.position)) && + (lhs.position_inline == rhs.position_inline); +} + +inline bool operator!=(const ApplicationDataT &lhs, const ApplicationDataT &rhs) { + return !(lhs == rhs); +} + + inline ApplicationDataT::ApplicationDataT(const ApplicationDataT &o) : vectors(o.vectors), vectors_alt(o.vectors_alt), diff --git a/tests/native_type_test_impl.h b/tests/native_type_test_impl.h index 578a58a8b5b..ce2e061b530 100644 --- a/tests/native_type_test_impl.h +++ b/tests/native_type_test_impl.h @@ -17,6 +17,10 @@ struct Vector3D { this->y = _y; this->z = _z; } + + bool operator==(const Vector3D &other) const { + return (x == other.x) && (y == other.y) && (z == other.z); + } }; } // namespace Native