Skip to content

Commit 28861d1

Browse files
authored
various fixes (#7986)
1 parent 204473c commit 28861d1

File tree

8 files changed

+122
-80
lines changed

8 files changed

+122
-80
lines changed

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,16 +532,17 @@ if(FLATBUFFERS_BUILD_TESTS)
532532
# The flattest target needs some generated files
533533
SET(FLATC_OPT --cpp --gen-mutable --gen-object-api --reflect-names)
534534
SET(FLATC_OPT_COMP ${FLATC_OPT};--gen-compare)
535+
SET(FLATC_OPT_SCOPED_ENUMS ${FLATC_OPT_COMP};--scoped-enums)
535536

536537
compile_schema_for_test(tests/alignment_test.fbs "${FLATC_OPT_COMP}")
537-
compile_schema_for_test(tests/arrays_test.fbs "${FLATC_OPT_COMP};--scoped-enums")
538+
compile_schema_for_test(tests/arrays_test.fbs "${FLATC_OPT_SCOPED_ENUMS}")
538539
compile_schema_for_test(tests/native_inline_table_test.fbs "${FLATC_OPT_COMP}")
539540
compile_schema_for_test(tests/native_type_test.fbs "${FLATC_OPT}")
540541
compile_schema_for_test(tests/key_field/key_field_sample.fbs "${FLATC_OPT_COMP}")
541542
compile_schema_for_test(tests/64bit/test_64bit.fbs "${FLATC_OPT_COMP};--bfbs-gen-embed")
542543
compile_schema_for_test(tests/64bit/evolution/v1.fbs "${FLATC_OPT_COMP}")
543544
compile_schema_for_test(tests/64bit/evolution/v2.fbs "${FLATC_OPT_COMP}")
544-
compile_schema_for_test(tests/union_underlying_type_test.fbs "${FLATC_OPT_COMP}")
545+
compile_schema_for_test(tests/union_underlying_type_test.fbs "${FLATC_OPT_SCOPED_ENUMS}")
545546

546547
if(FLATBUFFERS_CODE_SANITIZE)
547548
add_fsanitize_to_target(flattests ${FLATBUFFERS_CODE_SANITIZE})

include/flatbuffers/idl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,16 @@ class Parser : public ParserState {
12181218
// These functions return nullptr on success, or an error string,
12191219
// which may happen if the flatbuffer cannot be encoded in JSON (e.g.,
12201220
// it contains non-UTF-8 byte arrays in String values).
1221+
extern bool GenerateTextFromTable(const Parser &parser,
1222+
const void *table,
1223+
const std::string &tablename,
1224+
std::string *text);
1225+
extern const char *GenerateText(const Parser &parser, const void *flatbuffer,
1226+
std::string *text);
1227+
extern const char *GenerateTextFile(const Parser &parser,
1228+
const std::string &path,
1229+
const std::string &file_name);
1230+
12211231
extern const char *GenTextFromTable(const Parser &parser, const void *table,
12221232
const std::string &tablename,
12231233
std::string *text);

src/idl_gen_text.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,14 @@ static const char *GenerateTextImpl(const Parser &parser, const Table *table,
383383
return nullptr;
384384
}
385385

386+
// Generate a text representation of a flatbuffer in JSON format.
387+
// Deprecated: please use `GenTextFromTable`
388+
bool GenerateTextFromTable(const Parser &parser, const void *table,
389+
const std::string &table_name,
390+
std::string *_text) {
391+
return GenTextFromTable(parser, table, table_name, _text) != nullptr;
392+
}
393+
386394
// Generate a text representation of a flatbuffer in JSON format.
387395
const char *GenTextFromTable(const Parser &parser, const void *table,
388396
const std::string &table_name, std::string *_text) {
@@ -392,6 +400,12 @@ const char *GenTextFromTable(const Parser &parser, const void *table,
392400
return GenerateTextImpl(parser, root, *struct_def, _text);
393401
}
394402

403+
// Deprecated: please use `GenText`
404+
const char *GenerateText(const Parser &parser, const void *flatbuffer,
405+
std::string *_text) {
406+
return GenText(parser, flatbuffer, _text);
407+
}
408+
395409
// Generate a text representation of a flatbuffer in JSON format.
396410
const char *GenText(const Parser &parser, const void *flatbuffer,
397411
std::string *_text) {
@@ -406,6 +420,12 @@ static std::string TextFileName(const std::string &path,
406420
return path + file_name + ".json";
407421
}
408422

423+
// Deprecated: please use `GenTextFile`
424+
const char *GenerateTextFile(const Parser &parser, const std::string &path,
425+
const std::string &file_name) {
426+
return GenTextFile(parser, path, file_name);
427+
}
428+
409429
const char *GenTextFile(const Parser &parser, const std::string &path,
410430
const std::string &file_name) {
411431
if (parser.opts.use_flexbuffers) {

src/idl_parser.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2719,7 +2719,8 @@ bool Parser::Supports64BitOffsets() const {
27192719
}
27202720

27212721
bool Parser::SupportsUnionUnderlyingType() const {
2722-
return (opts.lang_to_generate & ~(IDLOptions::kCpp | IDLOptions::kTs)) == 0;
2722+
return (opts.lang_to_generate & ~(IDLOptions::kCpp | IDLOptions::kTs |
2723+
IDLOptions::kBinary)) == 0;
27232724
}
27242725

27252726
Namespace *Parser::UniqueNamespace(Namespace *ns) {

tests/alignment_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "alignment_test.h"
22

3-
#include "alignment_test_generated.h"
3+
#include "tests/alignment_test_generated.h"
44
#include "flatbuffers/flatbuffer_builder.h"
55
#include "test_assert.h"
66

tests/reflection_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "reflection_test.h"
22

3-
#include "arrays_test_generated.h"
3+
#include "tests/arrays_test_generated.h"
44
#include "flatbuffers/minireflect.h"
55
#include "flatbuffers/reflection.h"
66
#include "flatbuffers/reflection_generated.h"

tests/test.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
#include <memory>
2121
#include <string>
2222

23+
#if defined(__ANDRIOD__)
24+
#define INCLUDE_64_BIT_TESTS 0
25+
#else
26+
#define INCLUDE_64_BIT_TESTS 1
27+
#endif
28+
2329
#include "alignment_test.h"
2430
#include "evolution_test.h"
2531
#include "flatbuffers/flatbuffers.h"
@@ -38,12 +44,14 @@
3844
#include "parser_test.h"
3945
#include "proto_test.h"
4046
#include "reflection_test.h"
41-
#include "union_vector/union_vector_generated.h"
47+
#include "tests/union_vector/union_vector_generated.h"
4248
#include "union_underlying_type_test_generated.h"
4349
#if !defined(_MSC_VER) || _MSC_VER >= 1700
44-
# include "arrays_test_generated.h"
50+
# include "tests/arrays_test_generated.h"
51+
#endif
52+
#if INCLUDE_64_BIT_TESTS
53+
#include "tests/64bit/offset64_test.h"
4554
#endif
46-
#include "64bit/offset64_test.h"
4755
#include "flexbuffers_test.h"
4856
#include "is_quiet_nan.h"
4957
#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) {
15441552
void UnionUnderlyingTypeTest() {
15451553
using namespace UnionUnderlyingType;
15461554
TEST_ASSERT(sizeof(ABC) == sizeof(uint32_t));
1547-
TEST_ASSERT(ABC::ABC_A == 555);
1548-
TEST_ASSERT(ABC::ABC_B == 666);
1549-
TEST_ASSERT(ABC::ABC_C == 777);
1555+
TEST_ASSERT(static_cast<int32_t>(ABC::A) == 555);
1556+
TEST_ASSERT(static_cast<int32_t>(ABC::B) == 666);
1557+
TEST_ASSERT(static_cast<int32_t>(ABC::C) == 777);
15501558

15511559
DT buffer;
15521560
AT a;
@@ -1577,6 +1585,7 @@ void UnionUnderlyingTypeTest() {
15771585
}
15781586

15791587
static void Offset64Tests() {
1588+
#if INCLUDE_64_BIT_TESTS
15801589
Offset64Test();
15811590
Offset64SerializedFirst();
15821591
Offset64NestedFlatBuffer();
@@ -1586,6 +1595,7 @@ static void Offset64Tests() {
15861595
Offset64SizePrefix();
15871596
Offset64ManyVectors();
15881597
Offset64ForceAlign();
1598+
#endif
15891599
}
15901600

15911601
int FlatBufferTests(const std::string &tests_data_path) {

0 commit comments

Comments
 (0)