diff --git a/include/flatbuffers/reflection_generated.h b/include/flatbuffers/reflection_generated.h index dcb0f7e05..1ec414d30 100644 --- a/include/flatbuffers/reflection_generated.h +++ b/include/flatbuffers/reflection_generated.h @@ -6,6 +6,13 @@ #include "flatbuffers/flatbuffers.h" +// Ensure the included flatbuffers.h is the same version as when this file was +// generated, otherwise it may not be compatible. +static_assert(FLATBUFFERS_VERSION_MAJOR == 2 && + FLATBUFFERS_VERSION_MINOR == 0 && + FLATBUFFERS_VERSION_REVISION == 6, + "Non-compatible flatbuffers version included"); + namespace reflection { struct Type; diff --git a/samples/monster_generated.h b/samples/monster_generated.h index 5eef0f25e..850a1e14e 100644 --- a/samples/monster_generated.h +++ b/samples/monster_generated.h @@ -6,6 +6,13 @@ #include "flatbuffers/flatbuffers.h" +// Ensure the included flatbuffers.h is the same version as when this file was +// generated, otherwise it may not be compatible. +static_assert(FLATBUFFERS_VERSION_MAJOR == 2 && + FLATBUFFERS_VERSION_MINOR == 0 && + FLATBUFFERS_VERSION_REVISION == 6, + "Non-compatible flatbuffers version included"); + namespace MyGame { namespace Sample { diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index dfd9525c2..9545737e3 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -16,8 +16,10 @@ // independent from idl_parser, since this code is not needed for most clients +#include #include +#include "flatbuffers/base.h" #include "flatbuffers/code_generators.h" #include "flatbuffers/flatbuffers.h" #include "flatbuffers/flatc.h" @@ -199,6 +201,25 @@ class CppGenerator : public BaseGenerator { for (auto kw = keywords; *kw; kw++) keywords_.insert(*kw); } + // Adds code to check that the included flatbuffers.h is of the same version + // as the generated code. This check currently looks for exact version match, + // as we would guarantee that they are compatible, but in theory a newer + // version of flatbuffers.h should work with a old code gen if we do proper + // backwards support. + void GenFlatbuffersVersionCheck() { + code_ += + "// Ensure the included flatbuffers.h is the same version as when this " + "file was"; + code_ += "// generated, otherwise it may not be compatible."; + code_ += "static_assert(FLATBUFFERS_VERSION_MAJOR == " + + std::to_string(FLATBUFFERS_VERSION_MAJOR) + " &&"; + code_ += " FLATBUFFERS_VERSION_MINOR == " + + std::to_string(FLATBUFFERS_VERSION_MINOR) + " &&"; + code_ += " FLATBUFFERS_VERSION_REVISION == " + + std::to_string(FLATBUFFERS_VERSION_REVISION) + ","; + code_ += " \"Non-compatible flatbuffers version included\");"; + } + void GenIncludeDependencies() { int num_includes = 0; if (opts_.generate_object_based_api) { @@ -285,6 +306,8 @@ class CppGenerator : public BaseGenerator { code_ += "#include \"flatbuffers/flatbuffers.h\""; code_ += ""; + GenFlatbuffersVersionCheck(); + code_ += ""; SetNameSpace(struct_def.defined_namespace); auto name = Name(struct_def); @@ -349,6 +372,8 @@ class CppGenerator : public BaseGenerator { code_ += "#include \"flatbuffers/flexbuffers.h\""; } code_ += ""; + GenFlatbuffersVersionCheck(); + code_ += ""; if (opts_.include_dependence_headers) { GenIncludeDependencies(); } GenExtraIncludes(); diff --git a/tests/arrays_test_generated.h b/tests/arrays_test_generated.h index 1300b9272..b0f6f488b 100644 --- a/tests/arrays_test_generated.h +++ b/tests/arrays_test_generated.h @@ -6,6 +6,13 @@ #include "flatbuffers/flatbuffers.h" +// Ensure the included flatbuffers.h is the same version as when this file was +// generated, otherwise it may not be compatible. +static_assert(FLATBUFFERS_VERSION_MAJOR == 2 && + FLATBUFFERS_VERSION_MINOR == 0 && + FLATBUFFERS_VERSION_REVISION == 6, + "Non-compatible flatbuffers version included"); + namespace MyGame { namespace Example { diff --git a/tests/cpp17/generated_cpp17/monster_test_generated.h b/tests/cpp17/generated_cpp17/monster_test_generated.h index 666e38211..e802585b0 100644 --- a/tests/cpp17/generated_cpp17/monster_test_generated.h +++ b/tests/cpp17/generated_cpp17/monster_test_generated.h @@ -7,6 +7,13 @@ #include "flatbuffers/flatbuffers.h" #include "flatbuffers/flexbuffers.h" +// Ensure the included flatbuffers.h is the same version as when this file was +// generated, otherwise it may not be compatible. +static_assert(FLATBUFFERS_VERSION_MAJOR == 2 && + FLATBUFFERS_VERSION_MINOR == 0 && + FLATBUFFERS_VERSION_REVISION == 6, + "Non-compatible flatbuffers version included"); + namespace MyGame { struct InParentNamespace; diff --git a/tests/cpp17/generated_cpp17/optional_scalars_generated.h b/tests/cpp17/generated_cpp17/optional_scalars_generated.h index 24b5ccd76..13cf5af87 100644 --- a/tests/cpp17/generated_cpp17/optional_scalars_generated.h +++ b/tests/cpp17/generated_cpp17/optional_scalars_generated.h @@ -6,6 +6,13 @@ #include "flatbuffers/flatbuffers.h" +// Ensure the included flatbuffers.h is the same version as when this file was +// generated, otherwise it may not be compatible. +static_assert(FLATBUFFERS_VERSION_MAJOR == 2 && + FLATBUFFERS_VERSION_MINOR == 0 && + FLATBUFFERS_VERSION_REVISION == 6, + "Non-compatible flatbuffers version included"); + namespace optional_scalars { struct ScalarStuff; diff --git a/tests/cpp17/generated_cpp17/union_vector_generated.h b/tests/cpp17/generated_cpp17/union_vector_generated.h index aa0daf838..b488c98d9 100644 --- a/tests/cpp17/generated_cpp17/union_vector_generated.h +++ b/tests/cpp17/generated_cpp17/union_vector_generated.h @@ -6,6 +6,13 @@ #include "flatbuffers/flatbuffers.h" +// Ensure the included flatbuffers.h is the same version as when this file was +// generated, otherwise it may not be compatible. +static_assert(FLATBUFFERS_VERSION_MAJOR == 2 && + FLATBUFFERS_VERSION_MINOR == 0 && + FLATBUFFERS_VERSION_REVISION == 6, + "Non-compatible flatbuffers version included"); + struct Attacker; struct AttackerBuilder; struct AttackerT; diff --git a/tests/evolution_test/evolution_v1_generated.h b/tests/evolution_test/evolution_v1_generated.h index d5ddf8f75..67ebaf60f 100644 --- a/tests/evolution_test/evolution_v1_generated.h +++ b/tests/evolution_test/evolution_v1_generated.h @@ -6,6 +6,13 @@ #include "flatbuffers/flatbuffers.h" +// Ensure the included flatbuffers.h is the same version as when this file was +// generated, otherwise it may not be compatible. +static_assert(FLATBUFFERS_VERSION_MAJOR == 2 && + FLATBUFFERS_VERSION_MINOR == 0 && + FLATBUFFERS_VERSION_REVISION == 6, + "Non-compatible flatbuffers version included"); + namespace Evolution { namespace V1 { diff --git a/tests/evolution_test/evolution_v2_generated.h b/tests/evolution_test/evolution_v2_generated.h index 2a0efcb2c..779047da6 100644 --- a/tests/evolution_test/evolution_v2_generated.h +++ b/tests/evolution_test/evolution_v2_generated.h @@ -6,6 +6,13 @@ #include "flatbuffers/flatbuffers.h" +// Ensure the included flatbuffers.h is the same version as when this file was +// generated, otherwise it may not be compatible. +static_assert(FLATBUFFERS_VERSION_MAJOR == 2 && + FLATBUFFERS_VERSION_MINOR == 0 && + FLATBUFFERS_VERSION_REVISION == 6, + "Non-compatible flatbuffers version included"); + namespace Evolution { namespace V2 { diff --git a/tests/monster_extra_generated.h b/tests/monster_extra_generated.h index b27e092ea..b8affa986 100644 --- a/tests/monster_extra_generated.h +++ b/tests/monster_extra_generated.h @@ -6,6 +6,13 @@ #include "flatbuffers/flatbuffers.h" +// Ensure the included flatbuffers.h is the same version as when this file was +// generated, otherwise it may not be compatible. +static_assert(FLATBUFFERS_VERSION_MAJOR == 2 && + FLATBUFFERS_VERSION_MINOR == 0 && + FLATBUFFERS_VERSION_REVISION == 6, + "Non-compatible flatbuffers version included"); + namespace MyGame { struct MonsterExtra; diff --git a/tests/monster_test_bfbs_generated.h b/tests/monster_test_bfbs_generated.h index dc356814d..f93ba8be6 100644 --- a/tests/monster_test_bfbs_generated.h +++ b/tests/monster_test_bfbs_generated.h @@ -6,6 +6,13 @@ #include "flatbuffers/flatbuffers.h" +// Ensure the included flatbuffers.h is the same version as when this file was +// generated, otherwise it may not be compatible. +static_assert(FLATBUFFERS_VERSION_MAJOR == 2 && + FLATBUFFERS_VERSION_MINOR == 0 && + FLATBUFFERS_VERSION_REVISION == 6, + "Non-compatible flatbuffers version included"); + namespace MyGame { namespace Example { diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index adc530b6e..3b36ccc1e 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -7,6 +7,13 @@ #include "flatbuffers/flatbuffers.h" #include "flatbuffers/flexbuffers.h" +// Ensure the included flatbuffers.h is the same version as when this file was +// generated, otherwise it may not be compatible. +static_assert(FLATBUFFERS_VERSION_MAJOR == 2 && + FLATBUFFERS_VERSION_MINOR == 0 && + FLATBUFFERS_VERSION_REVISION == 6, + "Non-compatible flatbuffers version included"); + namespace MyGame { struct InParentNamespace; diff --git a/tests/namespace_test/namespace_test1_generated.h b/tests/namespace_test/namespace_test1_generated.h index bb822369e..ad635149c 100644 --- a/tests/namespace_test/namespace_test1_generated.h +++ b/tests/namespace_test/namespace_test1_generated.h @@ -6,6 +6,13 @@ #include "flatbuffers/flatbuffers.h" +// Ensure the included flatbuffers.h is the same version as when this file was +// generated, otherwise it may not be compatible. +static_assert(FLATBUFFERS_VERSION_MAJOR == 2 && + FLATBUFFERS_VERSION_MINOR == 0 && + FLATBUFFERS_VERSION_REVISION == 6, + "Non-compatible flatbuffers version included"); + namespace NamespaceA { namespace NamespaceB { diff --git a/tests/namespace_test/namespace_test2_generated.h b/tests/namespace_test/namespace_test2_generated.h index ff164ccce..5d76cecfe 100644 --- a/tests/namespace_test/namespace_test2_generated.h +++ b/tests/namespace_test/namespace_test2_generated.h @@ -6,6 +6,13 @@ #include "flatbuffers/flatbuffers.h" +// Ensure the included flatbuffers.h is the same version as when this file was +// generated, otherwise it may not be compatible. +static_assert(FLATBUFFERS_VERSION_MAJOR == 2 && + FLATBUFFERS_VERSION_MINOR == 0 && + FLATBUFFERS_VERSION_REVISION == 6, + "Non-compatible flatbuffers version included"); + namespace NamespaceA { struct TableInFirstNS; diff --git a/tests/native_type_test_generated.h b/tests/native_type_test_generated.h index 8678718f7..add274f8d 100644 --- a/tests/native_type_test_generated.h +++ b/tests/native_type_test_generated.h @@ -6,6 +6,13 @@ #include "flatbuffers/flatbuffers.h" +// Ensure the included flatbuffers.h is the same version as when this file was +// generated, otherwise it may not be compatible. +static_assert(FLATBUFFERS_VERSION_MAJOR == 2 && + FLATBUFFERS_VERSION_MINOR == 0 && + FLATBUFFERS_VERSION_REVISION == 6, + "Non-compatible flatbuffers version included"); + #include "native_type_test_impl.h" namespace Geometry { diff --git a/tests/optional_scalars_generated.h b/tests/optional_scalars_generated.h index c295ebac1..5a1075aad 100644 --- a/tests/optional_scalars_generated.h +++ b/tests/optional_scalars_generated.h @@ -6,6 +6,13 @@ #include "flatbuffers/flatbuffers.h" +// Ensure the included flatbuffers.h is the same version as when this file was +// generated, otherwise it may not be compatible. +static_assert(FLATBUFFERS_VERSION_MAJOR == 2 && + FLATBUFFERS_VERSION_MINOR == 0 && + FLATBUFFERS_VERSION_REVISION == 6, + "Non-compatible flatbuffers version included"); + namespace optional_scalars { struct ScalarStuff; diff --git a/tests/union_vector/union_vector_generated.h b/tests/union_vector/union_vector_generated.h index e08df6a7c..06281f314 100644 --- a/tests/union_vector/union_vector_generated.h +++ b/tests/union_vector/union_vector_generated.h @@ -6,6 +6,13 @@ #include "flatbuffers/flatbuffers.h" +// Ensure the included flatbuffers.h is the same version as when this file was +// generated, otherwise it may not be compatible. +static_assert(FLATBUFFERS_VERSION_MAJOR == 2 && + FLATBUFFERS_VERSION_MINOR == 0 && + FLATBUFFERS_VERSION_REVISION == 6, + "Non-compatible flatbuffers version included"); + struct Attacker; struct AttackerBuilder; struct AttackerT;