Started implementation for private flags in rust (#7269)

Adds flag to the cpp to fix bad annotations in all the languages

Addresses comments to the PR, and fixes logic for leaking annotations
This commit is contained in:
mustiikhalil
2022-06-05 23:04:05 +02:00
committed by GitHub
parent 967df08b1d
commit 11a1988705
13 changed files with 924 additions and 11 deletions

View File

@@ -3266,19 +3266,78 @@ CheckedError Parser::ParseRoot(const char *source, const char **include_paths,
for (auto val_it = enum_def.Vals().begin();
val_it != enum_def.Vals().end(); ++val_it) {
auto &val = **val_it;
if (!(opts.lang_to_generate != 0 && SupportsAdvancedUnionFeatures()) &&
(IsStruct(val.union_type) || IsString(val.union_type)))
return Error(
"only tables can be union elements in the generated language: " +
val.name);
}
}
}
auto err = CheckPrivateLeak();
if (err.Check()) return err;
// Parse JSON object only if the scheme has been parsed.
if (token_ == '{') { ECHECK(DoParseJson()); }
return NoError();
}
CheckedError Parser::CheckPrivateLeak() {
if (!opts.no_leak_private_annotations) return NoError();
// Iterate over all structs/tables to validate we arent leaking
// any private (structs/tables/enums)
for (auto it = structs_.vec.begin(); it != structs_.vec.end(); it++) {
auto &struct_def = **it;
for (auto fld_it = struct_def.fields.vec.begin();
fld_it != struct_def.fields.vec.end(); ++fld_it) {
auto &field = **fld_it;
if (field.value.type.enum_def) {
auto err =
CheckPrivatelyLeakedFields(struct_def, *field.value.type.enum_def);
if (err.Check()) { return err; }
} else if (field.value.type.struct_def) {
auto err = CheckPrivatelyLeakedFields(struct_def,
*field.value.type.struct_def);
if (err.Check()) { return err; }
}
}
}
// Iterate over all enums to validate we arent leaking
// any private (structs/tables)
for (auto it = enums_.vec.begin(); it != enums_.vec.end(); ++it) {
auto &enum_def = **it;
if (enum_def.is_union) {
for (auto val_it = enum_def.Vals().begin();
val_it != enum_def.Vals().end(); ++val_it) {
auto &val = **val_it;
if (val.union_type.struct_def) {
auto err =
CheckPrivatelyLeakedFields(enum_def, *val.union_type.struct_def);
if (err.Check()) { return err; }
}
}
}
}
return NoError();
}
CheckedError Parser::CheckPrivatelyLeakedFields(const Definition &def,
const Definition &value_type) {
if (!opts.no_leak_private_annotations) return NoError();
const auto is_private = def.attributes.Lookup("private");
const auto is_field_private = value_type.attributes.Lookup("private");
if (!is_private && is_field_private) {
return Error(
"Leaking private implementation, verify all objects have similar "
"annotations");
}
return NoError();
}
// Generate a unique hash for a file based on its name and contents (if any).
static uint64_t HashFile(const char *source_filename, const char *source) {
uint64_t hash = 0;