added check for presence of required fields. (flatc) c++ (#4316)

* added check for presence of required fields.

* updates to resolve Travis CI build error.

* fixes for resolving appveyor build errors.

* fixes for resolving appveyor build errors.

* fixes for resolving appveyor build errors.

* updates per aardappel's comments.

* updated a variable's name.

* updates per aardappel's comments.
This commit is contained in:
sfariv 2017-05-18 09:32:04 -07:00 committed by Wouter van Oortmerssen
parent 0f5f7faa9f
commit 55dec4d2f8
1 changed files with 23 additions and 0 deletions

View File

@ -897,6 +897,29 @@ CheckedError Parser::ParseTable(const StructDef &struct_def, std::string *value,
EXPECT(','); EXPECT(',');
} }
// Check if all required fields are parsed.
for (auto field_it = struct_def.fields.vec.begin();
field_it != struct_def.fields.vec.end();
++field_it) {
auto required_field = *field_it;
if (!required_field->required) {
continue;
}
bool found = false;
for (auto pf_it = field_stack_.end() - fieldn;
pf_it != field_stack_.end();
++pf_it) {
auto parsed_field = pf_it->second;
if (parsed_field == required_field) {
found = true;
break;
}
}
if (!found) {
return Error("required field is missing: " + required_field->name + " in " + struct_def.name);
}
}
if (struct_def.fixed && fieldn != struct_def.fields.vec.size()) if (struct_def.fixed && fieldn != struct_def.fields.vec.size())
return Error("struct: wrong number of initializers: " + struct_def.name); return Error("struct: wrong number of initializers: " + struct_def.name);