Improve error handling on Object API name collision. (#8275)

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.

Co-authored-by: Michael Beardsworth <beardsworth@intrinsic.ai>
This commit is contained in:
Michael Beardsworth
2024-04-05 12:27:43 -07:00
committed by GitHub
parent f4a9c5325b
commit e040f4e975

View File

@@ -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_ += "";