diff options
author | Casper <casperneo@uchicago.edu> | 2021-04-06 07:23:45 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-06 07:23:45 -0400 |
commit | 261cf3b20473abdf95fc34da0827e4986f065c39 (patch) | |
tree | c10c9fc913d356708fac31765e8323aa8de786c4 /src/idl_parser.cpp | |
parent | cd67261bbab01b60c2407dcbf4736037aae5d8e4 (diff) | |
download | flatbuffers-261cf3b20473abdf95fc34da0827e4986f065c39.tar.gz flatbuffers-261cf3b20473abdf95fc34da0827e4986f065c39.tar.bz2 flatbuffers-261cf3b20473abdf95fc34da0827e4986f065c39.zip |
Default-empty vectors of enums (#6505)
* disable clippy
* Vector of enum default
* swift and tests
* git clang format
* Rewrite enum parser checks
* Remove Voids from more_defaults
* vector enum swift
* remove vector accessor from swift
* clang format
Co-authored-by: Casper Neo <cneo@google.com>
Diffstat (limited to 'src/idl_parser.cpp')
-rw-r--r-- | src/idl_parser.cpp | 42 |
1 files changed, 25 insertions, 17 deletions
diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index f20b6f9a..69fa9d13 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -910,24 +910,32 @@ CheckedError Parser::ParseField(StructDef &struct_def) { } if (type.enum_def) { - // The type.base_type can only be scalar, union, array or vector. - // Table, struct or string can't have enum_def. - // Default value of union and vector in NONE, NULL translated to "0". - FLATBUFFERS_ASSERT(IsInteger(type.base_type) || - (type.base_type == BASE_TYPE_UNION) || IsVector(type) || - IsArray(type)); - if (IsVector(type)) { - // Vector can't use initialization list. - FLATBUFFERS_ASSERT(field->value.constant == "0"); + // Verify the enum's type and default value. + const std::string &constant = field->value.constant; + if (type.base_type == BASE_TYPE_UNION) { + if (constant != "0") { return Error("Union defaults must be NONE"); } + } else if (IsVector(type)) { + if (constant != "0" && constant != "[]") { + return Error("Vector defaults may only be `[]`."); + } + } else if (IsArray(type)) { + if (constant != "0") { + return Error("Array defaults are not supported yet."); + } } else { - // All unions should have the NONE ("0") enum value. - auto in_enum = field->IsOptional() || - type.enum_def->attributes.Lookup("bit_flags") || - type.enum_def->FindByValue(field->value.constant); - if (false == in_enum) - return Error("default value of " + field->value.constant + - " for field " + name + " is not part of enum " + - type.enum_def->name); + if (!IsInteger(type.base_type)) { + return Error("Enums must have integer base types"); + } + // Optional and bitflags enums may have default constants that are not + // their specified variants. + if (!field->IsOptional() && + type.enum_def->attributes.Lookup("bit_flags") == nullptr) { + if (type.enum_def->FindByValue(constant) == nullptr) { + return Error("default value of `" + constant + "` for " + "field `" + + name + "` is not part of enum `" + type.enum_def->name + + "`."); + } + } } } |