The asserts replaced by FLATBUFFERS_ASSERT. (#4701)

* The asserts replaced by FLATBUFFERS_ASSERT. Several asserts have converted to static_asserts.

* Regenerate header monster generate_code.sh
This commit is contained in:
Vladimir Glavnyy
2018-04-16 22:57:59 +07:00
committed by Wouter van Oortmerssen
parent 86153fd740
commit a66f9e769b
23 changed files with 157 additions and 149 deletions

View File

@@ -47,7 +47,7 @@ void CodeWriter::operator+=(std::string text) {
const std::string &value = iter->second;
stream_ << value;
} else {
assert(false && "could not find key");
FLATBUFFERS_ASSERT(false && "could not find key");
stream_ << key;
}

View File

@@ -216,7 +216,7 @@ class CppGenerator : public BaseGenerator {
if (parser_.opts.include_dependence_headers) { GenIncludeDependencies(); }
assert(!cur_name_space_);
FLATBUFFERS_ASSERT(!cur_name_space_);
// Generate forward declarations for all structs/tables, since they may
// have circular references.
@@ -667,7 +667,7 @@ class CppGenerator : public BaseGenerator {
return actual_type ? (native_type ? "std::string" : "flatbuffers::String")
: Name(ev);
} else {
assert(false);
FLATBUFFERS_ASSERT(false);
return Name(ev);
}
}
@@ -897,7 +897,7 @@ class CppGenerator : public BaseGenerator {
}
if (parser_.opts.scoped_enums || parser_.opts.prefixed_enums) {
assert(minv && maxv);
FLATBUFFERS_ASSERT(minv && maxv);
code_.SetValue("SEP", ",\n");
if (enum_def.attributes.Lookup("bit_flags")) {
@@ -1118,7 +1118,7 @@ class CppGenerator : public BaseGenerator {
code_ += getptr;
code_ += " return verifier.Verify(ptr);";
} else {
assert(false);
FLATBUFFERS_ASSERT(false);
}
code_ += " }";
} else {
@@ -1169,7 +1169,7 @@ class CppGenerator : public BaseGenerator {
} else if (ev.union_type.base_type == BASE_TYPE_STRING) {
code_ += " return new std::string(ptr->c_str(), ptr->size());";
} else {
assert(false);
FLATBUFFERS_ASSERT(false);
}
code_ += " }";
}
@@ -1202,7 +1202,7 @@ class CppGenerator : public BaseGenerator {
} else if (ev.union_type.base_type == BASE_TYPE_STRING) {
code_ += " return _fbb.CreateString(*ptr).Union();";
} else {
assert(false);
FLATBUFFERS_ASSERT(false);
}
code_ += " }";
}
@@ -1244,7 +1244,7 @@ class CppGenerator : public BaseGenerator {
" value = new {{TYPE}}(*reinterpret_cast<{{TYPE}} *>"
"(u.value));";
} else {
code_ += " assert(false); // {{TYPE}} not copyable.";
code_ += " FLATBUFFERS_ASSERT(false); // {{TYPE}} not copyable.";
}
code_ += " break;";
code_ += " }";
@@ -1697,7 +1697,7 @@ class CppGenerator : public BaseGenerator {
nested->constant);
nested_root = parser_.LookupStruct(qualified_name);
}
assert(nested_root); // Guaranteed to exist by parser.
FLATBUFFERS_ASSERT(nested_root); // Guaranteed to exist by parser.
(void)nested_root;
code_.SetValue("CPP_NAME", TranslateNameSpace(qualified_name));
@@ -2092,7 +2092,7 @@ class CppGenerator : public BaseGenerator {
break;
}
case BASE_TYPE_UTYPE: {
assert(union_field->value.type.base_type == BASE_TYPE_UNION);
FLATBUFFERS_ASSERT(union_field->value.type.base_type == BASE_TYPE_UNION);
// Generate code that sets the union type, of the form:
// _o->field.type = _e;
code += "_o->" + union_field->name + ".type = _e;";
@@ -2403,7 +2403,7 @@ class CppGenerator : public BaseGenerator {
f((1 << i) * 8, code_ptr, id);
}
}
assert(!(field.padding & ~0xF));
FLATBUFFERS_ASSERT(!(field.padding & ~0xF));
}
}

View File

@@ -131,7 +131,7 @@ const LanguageParameters &GetLangParams(IDLOptions::Language lang) {
if (lang == IDLOptions::kJava) {
return language_parameters[0];
} else {
assert(lang == IDLOptions::kCSharp);
FLATBUFFERS_ASSERT(lang == IDLOptions::kCSharp);
return language_parameters[1];
}
}
@@ -274,7 +274,7 @@ class GeneralGenerator : public BaseGenerator {
if (lang_.language == IDLOptions::kJava) {
return java_typename[type.base_type];
} else {
assert(lang_.language == IDLOptions::kCSharp);
FLATBUFFERS_ASSERT(lang_.language == IDLOptions::kCSharp);
return csharp_typename[type.base_type];
}
}
@@ -999,7 +999,7 @@ class GeneralGenerator : public BaseGenerator {
code += "(obj, o) : null";
}
break;
default: assert(0);
default: FLATBUFFERS_ASSERT(0);
}
}
code += member_suffix;
@@ -1427,7 +1427,7 @@ bool GenerateGeneral(const Parser &parser, const std::string &path,
std::string GeneralMakeRule(const Parser &parser, const std::string &path,
const std::string &file_name) {
assert(parser.opts.lang <= IDLOptions::kMAX);
FLATBUFFERS_ASSERT(parser.opts.lang <= IDLOptions::kMAX);
const auto &lang = GetLangParams(parser.opts.lang);
std::string make_rule;

View File

@@ -522,7 +522,7 @@ static void GenStructAccessor(const StructDef &struct_def,
break;
}
case BASE_TYPE_UNION: GetUnionField(struct_def, field, code_ptr); break;
default: assert(0);
default: FLATBUFFERS_ASSERT(0);
}
}
if (field.value.type.base_type == BASE_TYPE_VECTOR) {

View File

@@ -160,7 +160,7 @@ class FlatBufPrinter : public grpc_generator::Printer {
void Indent() { indent_++; }
void Outdent() {
indent_--;
assert(indent_ >= 0);
FLATBUFFERS_ASSERT(indent_ >= 0);
}
private:

View File

@@ -54,7 +54,7 @@ const JsLanguageParameters &GetJsLangParams(IDLOptions::Language lang) {
if (lang == IDLOptions::kJs) {
return js_language_parameters[0];
} else {
assert(lang == IDLOptions::kTs);
FLATBUFFERS_ASSERT(lang == IDLOptions::kTs);
return js_language_parameters[1];
}
}
@@ -870,7 +870,7 @@ class JsGenerator : public BaseGenerator {
" : null;\n";
break;
default: assert(0);
default: FLATBUFFERS_ASSERT(0);
}
}
code += "};\n\n";
@@ -1181,7 +1181,7 @@ bool GenerateJS(const Parser &parser, const std::string &path,
std::string JSMakeRule(const Parser &parser, const std::string &path,
const std::string &file_name) {
assert(parser.opts.lang <= IDLOptions::kMAX);
FLATBUFFERS_ASSERT(parser.opts.lang <= IDLOptions::kMAX);
const auto &lang = GetJsLangParams(parser.opts.lang);
std::string filebase =

View File

@@ -702,7 +702,7 @@ class PhpGenerator : public BaseGenerator {
break;
}
case BASE_TYPE_UNION: GetUnionField(field, code_ptr); break;
default: assert(0);
default: FLATBUFFERS_ASSERT(0);
}
}
if (field.value.type.base_type == BASE_TYPE_VECTOR) {

View File

@@ -459,7 +459,7 @@ static void GenStructAccessor(const StructDef &struct_def,
break;
}
case BASE_TYPE_UNION: GetUnionField(struct_def, field, code_ptr); break;
default: assert(0);
default: FLATBUFFERS_ASSERT(0);
}
}
if (field.value.type.base_type == BASE_TYPE_VECTOR) {

View File

@@ -108,7 +108,7 @@ bool Print<const void *>(const void *val, Type type, int indent,
case BASE_TYPE_UNION:
// If this assert hits, you have an corrupt buffer, a union type field
// was not present or was out of range.
assert(union_type);
FLATBUFFERS_ASSERT(union_type);
return Print<const void *>(val, *union_type, indent, nullptr, opts,
_text);
case BASE_TYPE_STRUCT:
@@ -143,7 +143,7 @@ bool Print<const void *>(const void *val, Type type, int indent,
// clang-format on
}
break;
default: assert(0);
default: FLATBUFFERS_ASSERT(0);
}
return true;
}
@@ -173,7 +173,7 @@ static bool GenFieldOffset(const FieldDef &fd, const Table *table, bool fixed,
const void *val = nullptr;
if (fixed) {
// The only non-scalar fields in structs are structs.
assert(IsStruct(fd.value.type));
FLATBUFFERS_ASSERT(IsStruct(fd.value.type));
val = reinterpret_cast<const Struct *>(table)->GetStruct<const void *>(
fd.value.offset);
} else if (fd.flexbuffer) {
@@ -261,7 +261,7 @@ static bool GenStruct(const StructDef &struct_def, const Table *table,
bool GenerateText(const Parser &parser, const void *flatbuffer,
std::string *_text) {
std::string &text = *_text;
assert(parser.root_struct_def_); // call SetRootType()
FLATBUFFERS_ASSERT(parser.root_struct_def_); // call SetRootType()
text.reserve(1024); // Reduce amount of inevitable reallocs.
auto root = parser.opts.size_prefixed ?
GetSizePrefixedRoot<Table>(flatbuffer) : GetRoot<Table>(flatbuffer);

View File

@@ -826,7 +826,7 @@ CheckedError Parser::ParseAnyValue(Value &val, FieldDef *field,
const StructDef *parent_struct_def) {
switch (val.type.base_type) {
case BASE_TYPE_UNION: {
assert(field);
FLATBUFFERS_ASSERT(field);
std::string constant;
// Find corresponding type field we may have already parsed.
for (auto elem = field_stack_.rbegin();
@@ -843,9 +843,9 @@ CheckedError Parser::ParseAnyValue(Value &val, FieldDef *field,
// output these in alphabetical order, meaning it comes after this
// value. So we scan past the value to find it, then come back here.
auto type_name = field->name + UnionTypeFieldSuffix();
assert(parent_struct_def);
FLATBUFFERS_ASSERT(parent_struct_def);
auto type_field = parent_struct_def->fields.Lookup(type_name);
assert(type_field); // Guaranteed by ParseField().
FLATBUFFERS_ASSERT(type_field); // Guaranteed by ParseField().
// Remember where we are in the source file, so we can come back here.
auto backup = *static_cast<ParserState *>(this);
ECHECK(SkipAnyJsonValue()); // The table.
@@ -882,7 +882,7 @@ CheckedError Parser::ParseAnyValue(Value &val, FieldDef *field,
} else if (enum_val->union_type.base_type == BASE_TYPE_STRING) {
ECHECK(ParseString(val));
} else {
assert(false);
FLATBUFFERS_ASSERT(false);
}
break;
}
@@ -917,7 +917,7 @@ CheckedError Parser::ParseAnyValue(Value &val, FieldDef *field,
}
void Parser::SerializeStruct(const StructDef &struct_def, const Value &val) {
assert(val.constant.length() == struct_def.bytesize);
FLATBUFFERS_ASSERT(val.constant.length() == struct_def.bytesize);
builder_.Align(struct_def.minalign);
builder_.PushBytes(reinterpret_cast<const uint8_t *>(val.constant.c_str()),
struct_def.bytesize);
@@ -1105,14 +1105,14 @@ CheckedError Parser::ParseTable(const StructDef &struct_def, std::string *value,
if (struct_def.fixed) {
builder_.ClearOffsets();
builder_.EndStruct();
assert(value);
FLATBUFFERS_ASSERT(value);
// Temporarily store this struct in the value string, since it is to
// be serialized in-place elsewhere.
value->assign(
reinterpret_cast<const char *>(builder_.GetCurrentBufferPointer()),
struct_def.bytesize);
builder_.PopBytes(struct_def.bytesize);
assert(!ovalue);
FLATBUFFERS_ASSERT(!ovalue);
} else {
auto val = builder_.EndTable(start);
if (ovalue) *ovalue = val;
@@ -1195,7 +1195,7 @@ CheckedError Parser::ParseNestedFlatbuffer(Value &val, FieldDef *field,
// Create and initialize new parser
Parser nested_parser;
assert(field->nested_flatbuffer);
FLATBUFFERS_ASSERT(field->nested_flatbuffer);
nested_parser.root_struct_def_ = field->nested_flatbuffer;
nested_parser.enums_ = enums_;
nested_parser.opts = opts;
@@ -1307,7 +1307,7 @@ CheckedError Parser::ParseEnumFromString(Type &type, int64_t *result) {
}
CheckedError Parser::ParseHash(Value &e, FieldDef *field) {
assert(field);
FLATBUFFERS_ASSERT(field);
Value *hash_name = field->attributes.Lookup("hash");
switch (e.type.base_type) {
case BASE_TYPE_SHORT: {
@@ -1346,7 +1346,7 @@ CheckedError Parser::ParseHash(Value &e, FieldDef *field) {
e.constant = NumToString(hashed_value);
break;
}
default: assert(0);
default: FLATBUFFERS_ASSERT(0);
}
NEXT();
return NoError();
@@ -1401,7 +1401,7 @@ CheckedError Parser::ParseSingleValue(const std::string *name, Value &e) {
e.constant = NumToString(strtod(attribute_.c_str(), &end));
if (*end) return Error("invalid float: " + attribute_);
} else {
assert(0); // Shouldn't happen, we covered all types.
FLATBUFFERS_ASSERT(0); // Shouldn't happen, we covered all types.
e.constant = "0";
}
NEXT();
@@ -2279,7 +2279,7 @@ CheckedError Parser::ParseRoot(const char *source, const char **include_paths,
auto &bt = field.value.type.base_type == BASE_TYPE_VECTOR
? field.value.type.element
: field.value.type.base_type;
assert(bt == BASE_TYPE_STRUCT);
FLATBUFFERS_ASSERT(bt == BASE_TYPE_STRUCT);
bt = enum_def->underlying_type.base_type;
struct_def.refcount--;
enum_def->refcount++;
@@ -2613,7 +2613,7 @@ Definition::SerializeAttributes(FlatBufferBuilder *builder,
std::vector<flatbuffers::Offset<reflection::KeyValue>> attrs;
for (auto kv = attributes.dict.begin(); kv != attributes.dict.end(); ++kv) {
auto it = parser.known_attributes_.find(kv->first);
assert(it != parser.known_attributes_.end());
FLATBUFFERS_ASSERT(it != parser.known_attributes_.end());
if (!it->second) { // Custom attribute.
attrs.push_back(reflection::CreateKeyValue(
*builder, builder->CreateString(kv->first),

View File

@@ -275,7 +275,7 @@ class ResizeContext {
break;
}
case reflection::String: break;
default: assert(false);
default: FLATBUFFERS_ASSERT(false);
}
}
// Check if the vtable offset points beyond the insertion point.
@@ -478,7 +478,7 @@ Offset<const Table *> CopyTable(FlatBufferBuilder &fbb,
}
}
}
assert(offset_idx == offsets.size());
FLATBUFFERS_ASSERT(offset_idx == offsets.size());
if (objectdef.is_struct()) {
fbb.ClearOffsets();
return fbb.EndStruct();
@@ -518,11 +518,11 @@ bool VerifyObject(flatbuffers::Verifier &v, const reflection::Schema &schema,
bool VerifyVector(flatbuffers::Verifier &v, const reflection::Schema &schema,
const flatbuffers::Table &table,
const reflection::Field &vec_field) {
assert(vec_field.type()->base_type() == reflection::Vector);
FLATBUFFERS_ASSERT(vec_field.type()->base_type() == reflection::Vector);
if (!table.VerifyField<uoffset_t>(v, vec_field.offset())) return false;
switch (vec_field.type()->element()) {
case reflection::None: assert(false); break;
case reflection::None: FLATBUFFERS_ASSERT(false); break;
case reflection::UType:
return v.Verify(flatbuffers::GetFieldV<uint8_t>(table, vec_field));
case reflection::Bool:
@@ -552,7 +552,7 @@ bool VerifyVector(flatbuffers::Verifier &v, const reflection::Schema &schema,
return false;
}
}
case reflection::Vector: assert(false); break;
case reflection::Vector: FLATBUFFERS_ASSERT(false); break;
case reflection::Obj: {
auto obj = schema.objects()->Get(vec_field.type()->index());
if (obj->is_struct()) {
@@ -575,8 +575,8 @@ bool VerifyVector(flatbuffers::Verifier &v, const reflection::Schema &schema,
}
return true;
}
case reflection::Union: assert(false); break;
default: assert(false); break;
case reflection::Union: FLATBUFFERS_ASSERT(false); break;
default: FLATBUFFERS_ASSERT(false); break;
}
return false;
@@ -597,7 +597,7 @@ bool VerifyObject(flatbuffers::Verifier &v, const reflection::Schema &schema,
for (uoffset_t i = 0; i < obj.fields()->size(); i++) {
auto field_def = obj.fields()->Get(i);
switch (field_def->type()->base_type()) {
case reflection::None: assert(false); break;
case reflection::None: FLATBUFFERS_ASSERT(false); break;
case reflection::UType:
if (!table->VerifyField<uint8_t>(v, field_def->offset())) return false;
break;
@@ -665,7 +665,7 @@ bool VerifyObject(flatbuffers::Verifier &v, const reflection::Schema &schema,
}
break;
}
default: assert(false); break;
default: FLATBUFFERS_ASSERT(false); break;
}
}

View File

@@ -47,12 +47,12 @@ static LoadFileFunction g_load_file_function = LoadFileRaw;
static FileExistsFunction g_file_exists_function = FileExistsRaw;
bool LoadFile(const char *name, bool binary, std::string *buf) {
assert(g_load_file_function);
FLATBUFFERS_ASSERT(g_load_file_function);
return g_load_file_function(name, binary, buf);
}
bool FileExists(const char *name) {
assert(g_file_exists_function);
FLATBUFFERS_ASSERT(g_file_exists_function);
return g_file_exists_function(name);
}