mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-03 22:14:12 +00:00
New error: declaring an enum field without a default in range of the enum.
Change-Id: I4db7f032440c00d31c7434975b8a3f17c29d74b9 Tested: on Linux.
This commit is contained in:
@@ -653,7 +653,15 @@ CheckedError Parser::ParseField(StructDef &struct_def) {
|
|||||||
"default values currently only supported for scalars in tables");
|
"default values currently only supported for scalars in tables");
|
||||||
ECHECK(ParseSingleValue(field->value));
|
ECHECK(ParseSingleValue(field->value));
|
||||||
}
|
}
|
||||||
if (IsFloat(field->value.type.base_type)) {
|
if (type.enum_def &&
|
||||||
|
!type.enum_def->is_union &&
|
||||||
|
!type.enum_def->attributes.Lookup("bit_flags") &&
|
||||||
|
!type.enum_def->ReverseLookup(static_cast<int>(
|
||||||
|
StringToInt(field->value.constant.c_str())))) {
|
||||||
|
return Error("default value of " + field->value.constant + " for field " +
|
||||||
|
name + " is not part of enum " + type.enum_def->name);
|
||||||
|
}
|
||||||
|
if (IsFloat(type.base_type)) {
|
||||||
if (!strpbrk(field->value.constant.c_str(), ".eE"))
|
if (!strpbrk(field->value.constant.c_str(), ".eE"))
|
||||||
field->value.constant += ".0";
|
field->value.constant += ".0";
|
||||||
}
|
}
|
||||||
@@ -700,15 +708,15 @@ CheckedError Parser::ParseField(StructDef &struct_def) {
|
|||||||
return Error("can't deprecate fields in a struct");
|
return Error("can't deprecate fields in a struct");
|
||||||
field->required = field->attributes.Lookup("required") != nullptr;
|
field->required = field->attributes.Lookup("required") != nullptr;
|
||||||
if (field->required &&
|
if (field->required &&
|
||||||
(struct_def.fixed || IsScalar(field->value.type.base_type)))
|
(struct_def.fixed || IsScalar(type.base_type)))
|
||||||
return Error("only non-scalar fields in tables may be 'required'");
|
return Error("only non-scalar fields in tables may be 'required'");
|
||||||
field->key = field->attributes.Lookup("key") != nullptr;
|
field->key = field->attributes.Lookup("key") != nullptr;
|
||||||
if (field->key) {
|
if (field->key) {
|
||||||
if (struct_def.has_key) return Error("only one field may be set as 'key'");
|
if (struct_def.has_key) return Error("only one field may be set as 'key'");
|
||||||
struct_def.has_key = true;
|
struct_def.has_key = true;
|
||||||
if (!IsScalar(field->value.type.base_type)) {
|
if (!IsScalar(type.base_type)) {
|
||||||
field->required = true;
|
field->required = true;
|
||||||
if (field->value.type.base_type != BASE_TYPE_STRING)
|
if (type.base_type != BASE_TYPE_STRING)
|
||||||
return Error("'key' field must be string or scalar type");
|
return Error("'key' field must be string or scalar type");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -729,8 +737,7 @@ CheckedError Parser::ParseField(StructDef &struct_def) {
|
|||||||
if (nested->type.base_type != BASE_TYPE_STRING)
|
if (nested->type.base_type != BASE_TYPE_STRING)
|
||||||
return Error(
|
return Error(
|
||||||
"nested_flatbuffer attribute must be a string (the root type)");
|
"nested_flatbuffer attribute must be a string (the root type)");
|
||||||
if (field->value.type.base_type != BASE_TYPE_VECTOR ||
|
if (type.base_type != BASE_TYPE_VECTOR || type.element != BASE_TYPE_UCHAR)
|
||||||
field->value.type.element != BASE_TYPE_UCHAR)
|
|
||||||
return Error(
|
return Error(
|
||||||
"nested_flatbuffer attribute may only apply to a vector of ubyte");
|
"nested_flatbuffer attribute may only apply to a vector of ubyte");
|
||||||
// This will cause an error if the root type of the nested flatbuffer
|
// This will cause an error if the root type of the nested flatbuffer
|
||||||
@@ -746,8 +753,8 @@ CheckedError Parser::ParseField(StructDef &struct_def) {
|
|||||||
if (field->attributes.Lookup("flexbuffer")) {
|
if (field->attributes.Lookup("flexbuffer")) {
|
||||||
field->flexbuffer = true;
|
field->flexbuffer = true;
|
||||||
uses_flexbuffers_ = true;
|
uses_flexbuffers_ = true;
|
||||||
if (field->value.type.base_type != BASE_TYPE_VECTOR ||
|
if (type.base_type != BASE_TYPE_VECTOR ||
|
||||||
field->value.type.element != BASE_TYPE_UCHAR)
|
type.element != BASE_TYPE_UCHAR)
|
||||||
return Error("flexbuffer attribute may only apply to a vector of ubyte");
|
return Error("flexbuffer attribute may only apply to a vector of ubyte");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1179,6 +1179,8 @@ void ErrorTest() {
|
|||||||
TestError("table X { Y:int; Y:int; }", "field already");
|
TestError("table X { Y:int; Y:int; }", "field already");
|
||||||
TestError("table Y {} table X { Y:int; }", "same as table");
|
TestError("table Y {} table X { Y:int; }", "same as table");
|
||||||
TestError("struct X { Y:string; }", "only scalar");
|
TestError("struct X { Y:string; }", "only scalar");
|
||||||
|
TestError("table X { Y:string = 1; }", "default values");
|
||||||
|
TestError("enum Y:byte { Z = 1 } table X { y:Y; }", "not part of enum");
|
||||||
TestError("struct X { Y:int (deprecated); }", "deprecate");
|
TestError("struct X { Y:int (deprecated); }", "deprecate");
|
||||||
TestError("union Z { X } table X { Y:Z; } root_type X; { Y: {}, A:1 }",
|
TestError("union Z { X } table X { Y:Z; } root_type X; { Y: {}, A:1 }",
|
||||||
"missing type field");
|
"missing type field");
|
||||||
@@ -1968,7 +1970,7 @@ int main(int /*argc*/, const char * /*argv*/ []) {
|
|||||||
EndianSwapTest();
|
EndianSwapTest();
|
||||||
|
|
||||||
JsonDefaultTest();
|
JsonDefaultTest();
|
||||||
|
|
||||||
FlexBuffersTest();
|
FlexBuffersTest();
|
||||||
|
|
||||||
if (!testing_fails) {
|
if (!testing_fails) {
|
||||||
|
|||||||
Reference in New Issue
Block a user