mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-27 21:48:06 +00:00
Checking for clashes between field names and generated field names.
This happens when the schema is parsed, to avoid compile time errors later, which would be harder to understand. Bug: 16325216 Change-Id: I24cabf1adaf1700796b91e3a9641bca43a68bfbd Tested: on OS X.
This commit is contained in:
@@ -800,6 +800,31 @@ void Parser::ParseDecl() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Check that no identifiers clash with auto generated fields.
|
||||||
|
// This is not an ideal situation, but should occur very infrequently,
|
||||||
|
// and allows us to keep using very readable names for type & length fields
|
||||||
|
// without inducing compile errors.
|
||||||
|
auto CheckClash = [&fields, &struct_def](const char *suffix,
|
||||||
|
BaseType basetype) {
|
||||||
|
auto len = strlen(suffix);
|
||||||
|
for (auto it = fields.begin(); it != fields.end(); ++it) {
|
||||||
|
auto &name = (*it)->name;
|
||||||
|
if (name.length() > len &&
|
||||||
|
name.compare(name.length() - len, len, suffix) == 0 &&
|
||||||
|
(*it)->value.type.base_type != BASE_TYPE_UTYPE) {
|
||||||
|
auto field = struct_def.fields.Lookup(
|
||||||
|
name.substr(0, name.length() - len));
|
||||||
|
if (field && field->value.type.base_type == basetype)
|
||||||
|
Error("Field " + name +
|
||||||
|
" would clash with generated functions for field " +
|
||||||
|
field->name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
CheckClash("_type", BASE_TYPE_UNION);
|
||||||
|
CheckClash("Type", BASE_TYPE_UNION);
|
||||||
|
CheckClash("_length", BASE_TYPE_VECTOR);
|
||||||
|
CheckClash("Length", BASE_TYPE_VECTOR);
|
||||||
Expect('}');
|
Expect('}');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -488,6 +488,7 @@ void ErrorTest() {
|
|||||||
TestError("struct X { Y:int; } root_type X;", "a table");
|
TestError("struct X { Y:int; } root_type X;", "a table");
|
||||||
TestError("union X { Y }", "referenced");
|
TestError("union X { Y }", "referenced");
|
||||||
TestError("union Z { X } struct X { Y:int; }", "only tables");
|
TestError("union Z { X } struct X { Y:int; }", "only tables");
|
||||||
|
TestError("table X { Y:[int]; YLength:int; }", "clash");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Additional parser testing not covered elsewhere.
|
// Additional parser testing not covered elsewhere.
|
||||||
|
|||||||
Reference in New Issue
Block a user