From 05694bee248a5a18d187c03d9274ce609f399565 Mon Sep 17 00:00:00 2001 From: Michael Beardsworth Date: Fri, 5 Apr 2024 12:03:33 -0700 Subject: [PATCH] Improve error handling on Object API name collision. If a schema contains a message named e.g. FooT and a message named Foo while the Object API suffix is T, then two classes with colliding names will be generated. This scenario will produce a C++ compiler error, but it's confusing. This patch moves the error to the compiler, allowing the user to more readily act to correct the issue. --- src/idl_gen_cpp.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 301c08d2c56..ce03a5eeca5 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -467,6 +467,20 @@ class CppGenerator : public BaseGenerator { } if (opts_.generate_object_based_api) { auto nativeName = NativeName(Name(*struct_def), struct_def, opts_); + + // Check that nativeName doesn't collide the name of another struct. + for (const auto &other_struct_def : parser_.structs_.vec) { + if (other_struct_def == struct_def) { continue; } + + auto other_name = Name(*other_struct_def); + if (nativeName == other_name) { + LogCompilerError("Generated Object API type for " + + Name(*struct_def) + " collides with " + + other_name); + FLATBUFFERS_ASSERT(true); + } + } + if (!struct_def->fixed) { code_ += "struct " + nativeName + ";"; } } code_ += "";