From 28861d1d7d5ec6ce34d4bbdc10bec4aace341167 Mon Sep 17 00:00:00 2001 From: Derek Bailey Date: Wed, 31 May 2023 11:52:05 -0700 Subject: [PATCH] various fixes (#7986) --- CMakeLists.txt | 5 +- include/flatbuffers/idl.h | 10 ++ src/idl_gen_text.cpp | 20 +++ src/idl_parser.cpp | 3 +- tests/alignment_test.cpp | 2 +- tests/reflection_test.cpp | 2 +- tests/test.cpp | 22 ++- tests/union_underlying_type_test_generated.h | 138 +++++++++---------- 8 files changed, 122 insertions(+), 80 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b8fcfe251..6a2e83499 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -532,16 +532,17 @@ if(FLATBUFFERS_BUILD_TESTS) # 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_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_COMP};--scoped-enums") + 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/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}") compile_schema_for_test(tests/64bit/evolution/v2.fbs "${FLATC_OPT_COMP}") - compile_schema_for_test(tests/union_underlying_type_test.fbs "${FLATC_OPT_COMP}") + compile_schema_for_test(tests/union_underlying_type_test.fbs "${FLATC_OPT_SCOPED_ENUMS}") if(FLATBUFFERS_CODE_SANITIZE) add_fsanitize_to_target(flattests ${FLATBUFFERS_CODE_SANITIZE}) diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index 542169449..bb3dd503e 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -1218,6 +1218,16 @@ class Parser : public ParserState { // These functions return nullptr on success, or an error string, // which may happen if the flatbuffer cannot be encoded in JSON (e.g., // it contains non-UTF-8 byte arrays in String values). +extern bool GenerateTextFromTable(const Parser &parser, + const void *table, + const std::string &tablename, + std::string *text); +extern const char *GenerateText(const Parser &parser, const void *flatbuffer, + std::string *text); +extern const char *GenerateTextFile(const Parser &parser, + const std::string &path, + const std::string &file_name); + extern const char *GenTextFromTable(const Parser &parser, const void *table, const std::string &tablename, std::string *text); diff --git a/src/idl_gen_text.cpp b/src/idl_gen_text.cpp index a34667c4b..895367e9a 100644 --- a/src/idl_gen_text.cpp +++ b/src/idl_gen_text.cpp @@ -383,6 +383,14 @@ static const char *GenerateTextImpl(const Parser &parser, const Table *table, return nullptr; } +// Generate a text representation of a flatbuffer in JSON format. +// Deprecated: please use `GenTextFromTable` +bool GenerateTextFromTable(const Parser &parser, const void *table, + const std::string &table_name, + std::string *_text) { + return GenTextFromTable(parser, table, table_name, _text) != nullptr; +} + // Generate a text representation of a flatbuffer in JSON format. const char *GenTextFromTable(const Parser &parser, const void *table, const std::string &table_name, std::string *_text) { @@ -392,6 +400,12 @@ const char *GenTextFromTable(const Parser &parser, const void *table, return GenerateTextImpl(parser, root, *struct_def, _text); } +// Deprecated: please use `GenText` +const char *GenerateText(const Parser &parser, const void *flatbuffer, + std::string *_text) { + return GenText(parser, flatbuffer, _text); +} + // Generate a text representation of a flatbuffer in JSON format. const char *GenText(const Parser &parser, const void *flatbuffer, std::string *_text) { @@ -406,6 +420,12 @@ static std::string TextFileName(const std::string &path, return path + file_name + ".json"; } +// Deprecated: please use `GenTextFile` +const char *GenerateTextFile(const Parser &parser, const std::string &path, + const std::string &file_name) { + return GenTextFile(parser, path, file_name); +} + const char *GenTextFile(const Parser &parser, const std::string &path, const std::string &file_name) { if (parser.opts.use_flexbuffers) { diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index 67d63857e..2eef2d7bb 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -2719,7 +2719,8 @@ bool Parser::Supports64BitOffsets() const { } bool Parser::SupportsUnionUnderlyingType() const { - return (opts.lang_to_generate & ~(IDLOptions::kCpp | IDLOptions::kTs)) == 0; + return (opts.lang_to_generate & ~(IDLOptions::kCpp | IDLOptions::kTs | + IDLOptions::kBinary)) == 0; } Namespace *Parser::UniqueNamespace(Namespace *ns) { diff --git a/tests/alignment_test.cpp b/tests/alignment_test.cpp index 9fbf6d8eb..731c32866 100644 --- a/tests/alignment_test.cpp +++ b/tests/alignment_test.cpp @@ -1,6 +1,6 @@ #include "alignment_test.h" -#include "alignment_test_generated.h" +#include "tests/alignment_test_generated.h" #include "flatbuffers/flatbuffer_builder.h" #include "test_assert.h" diff --git a/tests/reflection_test.cpp b/tests/reflection_test.cpp index 869c98db4..880f137e1 100644 --- a/tests/reflection_test.cpp +++ b/tests/reflection_test.cpp @@ -1,6 +1,6 @@ #include "reflection_test.h" -#include "arrays_test_generated.h" +#include "tests/arrays_test_generated.h" #include "flatbuffers/minireflect.h" #include "flatbuffers/reflection.h" #include "flatbuffers/reflection_generated.h" diff --git a/tests/test.cpp b/tests/test.cpp index 791c43912..ad0e9b1f0 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -20,6 +20,12 @@ #include #include +#if defined(__ANDRIOD__) +#define INCLUDE_64_BIT_TESTS 0 +#else +#define INCLUDE_64_BIT_TESTS 1 +#endif + #include "alignment_test.h" #include "evolution_test.h" #include "flatbuffers/flatbuffers.h" @@ -38,12 +44,14 @@ #include "parser_test.h" #include "proto_test.h" #include "reflection_test.h" -#include "union_vector/union_vector_generated.h" +#include "tests/union_vector/union_vector_generated.h" #include "union_underlying_type_test_generated.h" #if !defined(_MSC_VER) || _MSC_VER >= 1700 -# include "arrays_test_generated.h" +# include "tests/arrays_test_generated.h" +#endif +#if INCLUDE_64_BIT_TESTS +#include "tests/64bit/offset64_test.h" #endif -#include "64bit/offset64_test.h" #include "flexbuffers_test.h" #include "is_quiet_nan.h" #include "monster_test_bfbs_generated.h" // Generated using --bfbs-comments --bfbs-builtins --cpp --bfbs-gen-embed @@ -1544,9 +1552,9 @@ void DoNotRequireEofTest(const std::string &tests_data_path) { void UnionUnderlyingTypeTest() { using namespace UnionUnderlyingType; TEST_ASSERT(sizeof(ABC) == sizeof(uint32_t)); - TEST_ASSERT(ABC::ABC_A == 555); - TEST_ASSERT(ABC::ABC_B == 666); - TEST_ASSERT(ABC::ABC_C == 777); + TEST_ASSERT(static_cast(ABC::A) == 555); + TEST_ASSERT(static_cast(ABC::B) == 666); + TEST_ASSERT(static_cast(ABC::C) == 777); DT buffer; AT a; @@ -1577,6 +1585,7 @@ void UnionUnderlyingTypeTest() { } static void Offset64Tests() { +#if INCLUDE_64_BIT_TESTS Offset64Test(); Offset64SerializedFirst(); Offset64NestedFlatBuffer(); @@ -1586,6 +1595,7 @@ static void Offset64Tests() { Offset64SizePrefix(); Offset64ManyVectors(); Offset64ForceAlign(); +#endif } int FlatBufferTests(const std::string &tests_data_path) { diff --git a/tests/union_underlying_type_test_generated.h b/tests/union_underlying_type_test_generated.h index c2642f06a..cd822ec51 100644 --- a/tests/union_underlying_type_test_generated.h +++ b/tests/union_underlying_type_test_generated.h @@ -48,74 +48,74 @@ inline const ::flatbuffers::TypeTable *CTypeTable(); inline const ::flatbuffers::TypeTable *DTypeTable(); -enum ABC : int32_t { - ABC_NONE = 0, - ABC_A = 555, - ABC_B = 666, - ABC_C = 777, - ABC_MIN = ABC_NONE, - ABC_MAX = ABC_C +enum class ABC : int32_t { + NONE = 0, + A = 555, + B = 666, + C = 777, + MIN = NONE, + MAX = C }; inline const ABC (&EnumValuesABC())[4] { static const ABC values[] = { - ABC_NONE, - ABC_A, - ABC_B, - ABC_C + ABC::NONE, + ABC::A, + ABC::B, + ABC::C }; return values; } inline const char *EnumNameABC(ABC e) { switch (e) { - case ABC_NONE: return "NONE"; - case ABC_A: return "A"; - case ABC_B: return "B"; - case ABC_C: return "C"; + case ABC::NONE: return "NONE"; + case ABC::A: return "A"; + case ABC::B: return "B"; + case ABC::C: return "C"; default: return ""; } } template struct ABCTraits { - static const ABC enum_value = ABC_NONE; + static const ABC enum_value = ABC::NONE; }; template<> struct ABCTraits { - static const ABC enum_value = ABC_A; + static const ABC enum_value = ABC::A; }; template<> struct ABCTraits { - static const ABC enum_value = ABC_B; + static const ABC enum_value = ABC::B; }; template<> struct ABCTraits { - static const ABC enum_value = ABC_C; + static const ABC enum_value = ABC::C; }; template struct ABCUnionTraits { - static const ABC enum_value = ABC_NONE; + static const ABC enum_value = ABC::NONE; }; template<> struct ABCUnionTraits { - static const ABC enum_value = ABC_A; + static const ABC enum_value = ABC::A; }; template<> struct ABCUnionTraits { - static const ABC enum_value = ABC_B; + static const ABC enum_value = ABC::B; }; template<> struct ABCUnionTraits { - static const ABC enum_value = ABC_C; + static const ABC enum_value = ABC::C; }; struct ABCUnion { ABC type; void *value; - ABCUnion() : type(ABC_NONE), value(nullptr) {} + ABCUnion() : type(ABC::NONE), value(nullptr) {} ABCUnion(ABCUnion&& u) FLATBUFFERS_NOEXCEPT : - type(ABC_NONE), value(nullptr) + type(ABC::NONE), value(nullptr) { std::swap(type, u.type); std::swap(value, u.value); } ABCUnion(const ABCUnion &); ABCUnion &operator=(const ABCUnion &u) @@ -131,7 +131,7 @@ struct ABCUnion { typedef typename std::remove_reference::type RT; Reset(); type = ABCUnionTraits::enum_value; - if (type != ABC_NONE) { + if (type != ABC::NONE) { value = new RT(std::forward(val)); } } @@ -140,27 +140,27 @@ struct ABCUnion { ::flatbuffers::Offset Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ::flatbuffers::rehasher_function_t *_rehasher = nullptr) const; UnionUnderlyingType::AT *AsA() { - return type == ABC_A ? + return type == ABC::A ? reinterpret_cast(value) : nullptr; } const UnionUnderlyingType::AT *AsA() const { - return type == ABC_A ? + return type == ABC::A ? reinterpret_cast(value) : nullptr; } UnionUnderlyingType::BT *AsB() { - return type == ABC_B ? + return type == ABC::B ? reinterpret_cast(value) : nullptr; } const UnionUnderlyingType::BT *AsB() const { - return type == ABC_B ? + return type == ABC::B ? reinterpret_cast(value) : nullptr; } UnionUnderlyingType::CT *AsC() { - return type == ABC_C ? + return type == ABC::C ? reinterpret_cast(value) : nullptr; } const UnionUnderlyingType::CT *AsC() const { - return type == ABC_C ? + return type == ABC::C ? reinterpret_cast(value) : nullptr; } }; @@ -169,18 +169,18 @@ struct ABCUnion { inline bool operator==(const ABCUnion &lhs, const ABCUnion &rhs) { if (lhs.type != rhs.type) return false; switch (lhs.type) { - case ABC_NONE: { + case ABC::NONE: { return true; } - case ABC_A: { + case ABC::A: { return *(reinterpret_cast(lhs.value)) == *(reinterpret_cast(rhs.value)); } - case ABC_B: { + case ABC::B: { return *(reinterpret_cast(lhs.value)) == *(reinterpret_cast(rhs.value)); } - case ABC_C: { + case ABC::C: { return *(reinterpret_cast(lhs.value)) == *(reinterpret_cast(rhs.value)); } @@ -195,7 +195,7 @@ inline bool operator!=(const ABCUnion &lhs, const ABCUnion &rhs) { } bool VerifyABC(::flatbuffers::Verifier &verifier, const void *obj, ABC type); -bool VerifyABCVector(::flatbuffers::Verifier &verifier, const ::flatbuffers::Vector<::flatbuffers::Offset> *values, const ::flatbuffers::Vector *types); +bool VerifyABCVector(::flatbuffers::Verifier &verifier, const ::flatbuffers::Vector<::flatbuffers::Offset> *values, const ::flatbuffers::Vector *types); struct AT : public ::flatbuffers::NativeTable { typedef A TableType; @@ -407,22 +407,22 @@ struct D FLATBUFFERS_FINAL_CLASS : private ::flatbuffers::Table { } template const T *test_union_as() const; const UnionUnderlyingType::A *test_union_as_A() const { - return test_union_type() == UnionUnderlyingType::ABC_A ? static_cast(test_union()) : nullptr; + return test_union_type() == UnionUnderlyingType::ABC::A ? static_cast(test_union()) : nullptr; } const UnionUnderlyingType::B *test_union_as_B() const { - return test_union_type() == UnionUnderlyingType::ABC_B ? static_cast(test_union()) : nullptr; + return test_union_type() == UnionUnderlyingType::ABC::B ? static_cast(test_union()) : nullptr; } const UnionUnderlyingType::C *test_union_as_C() const { - return test_union_type() == UnionUnderlyingType::ABC_C ? static_cast(test_union()) : nullptr; + return test_union_type() == UnionUnderlyingType::ABC::C ? static_cast(test_union()) : nullptr; } void *mutable_test_union() { return GetPointer(VT_TEST_UNION); } - const ::flatbuffers::Vector *test_vector_of_union_type() const { - return GetPointer *>(VT_TEST_VECTOR_OF_UNION_TYPE); + const ::flatbuffers::Vector *test_vector_of_union_type() const { + return GetPointer *>(VT_TEST_VECTOR_OF_UNION_TYPE); } - ::flatbuffers::Vector *mutable_test_vector_of_union_type() { - return GetPointer<::flatbuffers::Vector *>(VT_TEST_VECTOR_OF_UNION_TYPE); + ::flatbuffers::Vector *mutable_test_vector_of_union_type() { + return GetPointer<::flatbuffers::Vector *>(VT_TEST_VECTOR_OF_UNION_TYPE); } const ::flatbuffers::Vector<::flatbuffers::Offset> *test_vector_of_union() const { return GetPointer> *>(VT_TEST_VECTOR_OF_UNION); @@ -469,7 +469,7 @@ struct DBuilder { void add_test_union(::flatbuffers::Offset test_union) { fbb_.AddOffset(D::VT_TEST_UNION, test_union); } - void add_test_vector_of_union_type(::flatbuffers::Offset<::flatbuffers::Vector> test_vector_of_union_type) { + void add_test_vector_of_union_type(::flatbuffers::Offset<::flatbuffers::Vector> test_vector_of_union_type) { fbb_.AddOffset(D::VT_TEST_VECTOR_OF_UNION_TYPE, test_vector_of_union_type); } void add_test_vector_of_union(::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> test_vector_of_union) { @@ -488,9 +488,9 @@ struct DBuilder { inline ::flatbuffers::Offset CreateD( ::flatbuffers::FlatBufferBuilder &_fbb, - UnionUnderlyingType::ABC test_union_type = UnionUnderlyingType::ABC_NONE, + UnionUnderlyingType::ABC test_union_type = UnionUnderlyingType::ABC::NONE, ::flatbuffers::Offset test_union = 0, - ::flatbuffers::Offset<::flatbuffers::Vector> test_vector_of_union_type = 0, + ::flatbuffers::Offset<::flatbuffers::Vector> test_vector_of_union_type = 0, ::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset>> test_vector_of_union = 0) { DBuilder builder_(_fbb); builder_.add_test_vector_of_union(test_vector_of_union); @@ -502,11 +502,11 @@ inline ::flatbuffers::Offset CreateD( inline ::flatbuffers::Offset CreateDDirect( ::flatbuffers::FlatBufferBuilder &_fbb, - UnionUnderlyingType::ABC test_union_type = UnionUnderlyingType::ABC_NONE, + UnionUnderlyingType::ABC test_union_type = UnionUnderlyingType::ABC::NONE, ::flatbuffers::Offset test_union = 0, - const std::vector *test_vector_of_union_type = nullptr, + const std::vector *test_vector_of_union_type = nullptr, const std::vector<::flatbuffers::Offset> *test_vector_of_union = nullptr) { - auto test_vector_of_union_type__ = test_vector_of_union_type ? _fbb.CreateVector(*test_vector_of_union_type) : 0; + auto test_vector_of_union_type__ = test_vector_of_union_type ? _fbb.CreateVector(*test_vector_of_union_type) : 0; auto test_vector_of_union__ = test_vector_of_union ? _fbb.CreateVector<::flatbuffers::Offset>(*test_vector_of_union) : 0; return UnionUnderlyingType::CreateD( _fbb, @@ -666,7 +666,7 @@ inline ::flatbuffers::Offset CreateD(::flatbuffers::FlatBufferBuilder &_fbb, struct _VectorArgs { ::flatbuffers::FlatBufferBuilder *__fbb; const DT* __o; const ::flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; auto _test_union_type = _o->test_union.type; auto _test_union = _o->test_union.Pack(_fbb); - auto _test_vector_of_union_type = _o->test_vector_of_union.size() ? _fbb.CreateVector(_o->test_vector_of_union.size(), [](size_t i, _VectorArgs *__va) { return static_cast(__va->__o->test_vector_of_union[i].type); }, &_va) : 0; + auto _test_vector_of_union_type = _o->test_vector_of_union.size() ? _fbb.CreateVector(_o->test_vector_of_union.size(), [](size_t i, _VectorArgs *__va) { return __va->__o->test_vector_of_union[i].type; }, &_va) : 0; auto _test_vector_of_union = _o->test_vector_of_union.size() ? _fbb.CreateVector<::flatbuffers::Offset>(_o->test_vector_of_union.size(), [](size_t i, _VectorArgs *__va) { return __va->__o->test_vector_of_union[i].Pack(*__va->__fbb, __va->__rehasher); }, &_va) : 0; return UnionUnderlyingType::CreateD( _fbb, @@ -678,18 +678,18 @@ inline ::flatbuffers::Offset CreateD(::flatbuffers::FlatBufferBuilder &_fbb, inline bool VerifyABC(::flatbuffers::Verifier &verifier, const void *obj, ABC type) { switch (type) { - case ABC_NONE: { + case ABC::NONE: { return true; } - case ABC_A: { + case ABC::A: { auto ptr = reinterpret_cast(obj); return verifier.VerifyTable(ptr); } - case ABC_B: { + case ABC::B: { auto ptr = reinterpret_cast(obj); return verifier.VerifyTable(ptr); } - case ABC_C: { + case ABC::C: { auto ptr = reinterpret_cast(obj); return verifier.VerifyTable(ptr); } @@ -697,7 +697,7 @@ inline bool VerifyABC(::flatbuffers::Verifier &verifier, const void *obj, ABC ty } } -inline bool VerifyABCVector(::flatbuffers::Verifier &verifier, const ::flatbuffers::Vector<::flatbuffers::Offset> *values, const ::flatbuffers::Vector *types) { +inline bool VerifyABCVector(::flatbuffers::Verifier &verifier, const ::flatbuffers::Vector<::flatbuffers::Offset> *values, const ::flatbuffers::Vector *types) { if (!values || !types) return !values && !types; if (values->size() != types->size()) return false; for (::flatbuffers::uoffset_t i = 0; i < values->size(); ++i) { @@ -712,15 +712,15 @@ inline bool VerifyABCVector(::flatbuffers::Verifier &verifier, const ::flatbuffe inline void *ABCUnion::UnPack(const void *obj, ABC type, const ::flatbuffers::resolver_function_t *resolver) { (void)resolver; switch (type) { - case ABC_A: { + case ABC::A: { auto ptr = reinterpret_cast(obj); return ptr->UnPack(resolver); } - case ABC_B: { + case ABC::B: { auto ptr = reinterpret_cast(obj); return ptr->UnPack(resolver); } - case ABC_C: { + case ABC::C: { auto ptr = reinterpret_cast(obj); return ptr->UnPack(resolver); } @@ -731,15 +731,15 @@ inline void *ABCUnion::UnPack(const void *obj, ABC type, const ::flatbuffers::re inline ::flatbuffers::Offset ABCUnion::Pack(::flatbuffers::FlatBufferBuilder &_fbb, const ::flatbuffers::rehasher_function_t *_rehasher) const { (void)_rehasher; switch (type) { - case ABC_A: { + case ABC::A: { auto ptr = reinterpret_cast(value); return CreateA(_fbb, ptr, _rehasher).Union(); } - case ABC_B: { + case ABC::B: { auto ptr = reinterpret_cast(value); return CreateB(_fbb, ptr, _rehasher).Union(); } - case ABC_C: { + case ABC::C: { auto ptr = reinterpret_cast(value); return CreateC(_fbb, ptr, _rehasher).Union(); } @@ -749,15 +749,15 @@ inline ::flatbuffers::Offset ABCUnion::Pack(::flatbuffers::FlatBufferBuild inline ABCUnion::ABCUnion(const ABCUnion &u) : type(u.type), value(nullptr) { switch (type) { - case ABC_A: { + case ABC::A: { value = new UnionUnderlyingType::AT(*reinterpret_cast(u.value)); break; } - case ABC_B: { + case ABC::B: { value = new UnionUnderlyingType::BT(*reinterpret_cast(u.value)); break; } - case ABC_C: { + case ABC::C: { value = new UnionUnderlyingType::CT(*reinterpret_cast(u.value)); break; } @@ -768,17 +768,17 @@ inline ABCUnion::ABCUnion(const ABCUnion &u) : type(u.type), value(nullptr) { inline void ABCUnion::Reset() { switch (type) { - case ABC_A: { + case ABC::A: { auto ptr = reinterpret_cast(value); delete ptr; break; } - case ABC_B: { + case ABC::B: { auto ptr = reinterpret_cast(value); delete ptr; break; } - case ABC_C: { + case ABC::C: { auto ptr = reinterpret_cast(value); delete ptr; break; @@ -786,7 +786,7 @@ inline void ABCUnion::Reset() { default: break; } value = nullptr; - type = ABC_NONE; + type = ABC::NONE; } inline const ::flatbuffers::TypeTable *ABCTypeTable() {