idl_gen_cpp.cpp: clang-format run, to better separate changes in content from formatting.

This commit is contained in:
Luca Longinotti 2019-03-01 14:31:17 +01:00
parent a1f14005ab
commit 28cb2e92d5
1 changed files with 114 additions and 115 deletions

View File

@ -43,103 +43,104 @@ class CppGenerator : public BaseGenerator {
float_const_gen_("std::numeric_limits<double>::", float_const_gen_("std::numeric_limits<double>::",
"std::numeric_limits<float>::", "quiet_NaN()", "std::numeric_limits<float>::", "quiet_NaN()",
"infinity()") { "infinity()") {
static const char * const keywords[] = { static const char *const keywords[] = {
"alignas", "alignas",
"alignof", "alignof",
"and", "and",
"and_eq", "and_eq",
"asm", "asm",
"atomic_cancel", "atomic_cancel",
"atomic_commit", "atomic_commit",
"atomic_noexcept", "atomic_noexcept",
"auto", "auto",
"bitand", "bitand",
"bitor", "bitor",
"bool", "bool",
"break", "break",
"case", "case",
"catch", "catch",
"char", "char",
"char16_t", "char16_t",
"char32_t", "char32_t",
"class", "class",
"compl", "compl",
"concept", "concept",
"const", "const",
"constexpr", "constexpr",
"const_cast", "const_cast",
"continue", "continue",
"co_await", "co_await",
"co_return", "co_return",
"co_yield", "co_yield",
"decltype", "decltype",
"default", "default",
"delete", "delete",
"do", "do",
"double", "double",
"dynamic_cast", "dynamic_cast",
"else", "else",
"enum", "enum",
"explicit", "explicit",
"export", "export",
"extern", "extern",
"false", "false",
"float", "float",
"for", "for",
"friend", "friend",
"goto", "goto",
"if", "if",
"import", "import",
"inline", "inline",
"int", "int",
"long", "long",
"module", "module",
"mutable", "mutable",
"namespace", "namespace",
"new", "new",
"noexcept", "noexcept",
"not", "not",
"not_eq", "not_eq",
"nullptr", "nullptr",
"operator", "operator",
"or", "or",
"or_eq", "or_eq",
"private", "private",
"protected", "protected",
"public", "public",
"register", "register",
"reinterpret_cast", "reinterpret_cast",
"requires", "requires",
"return", "return",
"short", "short",
"signed", "signed",
"sizeof", "sizeof",
"static", "static",
"static_assert", "static_assert",
"static_cast", "static_cast",
"struct", "struct",
"switch", "switch",
"synchronized", "synchronized",
"template", "template",
"this", "this",
"thread_local", "thread_local",
"throw", "throw",
"true", "true",
"try", "try",
"typedef", "typedef",
"typeid", "typeid",
"typename", "typename",
"union", "union",
"unsigned", "unsigned",
"using", "using",
"virtual", "virtual",
"void", "void",
"volatile", "volatile",
"wchar_t", "wchar_t",
"while", "while",
"xor", "xor",
"xor_eq", "xor_eq",
nullptr }; nullptr,
};
for (auto kw = keywords; *kw; kw++) keywords_.insert(*kw); for (auto kw = keywords; *kw; kw++) keywords_.insert(*kw);
} }
@ -242,7 +243,7 @@ class CppGenerator : public BaseGenerator {
// Generate forward declarations for all equal operators // Generate forward declarations for all equal operators
if (parser_.opts.generate_object_based_api && parser_.opts.gen_compare) { if (parser_.opts.generate_object_based_api && parser_.opts.gen_compare) {
for (auto it = parser_.structs_.vec.begin(); for (auto it = parser_.structs_.vec.begin();
it != parser_.structs_.vec.end(); ++it) { it != parser_.structs_.vec.end(); ++it) {
const auto &struct_def = **it; const auto &struct_def = **it;
if (!struct_def.generated) { if (!struct_def.generated) {
SetNameSpace(struct_def.defined_namespace); SetNameSpace(struct_def.defined_namespace);
@ -494,8 +495,8 @@ class CppGenerator : public BaseGenerator {
// Return a C++ type from the table in idl.h // Return a C++ type from the table in idl.h
std::string GenTypeBasic(const Type &type, bool user_facing_type) const { std::string GenTypeBasic(const Type &type, bool user_facing_type) const {
static const char * const ctypename[] = { static const char *const ctypename[] = {
// clang-format off // clang-format off
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, \ #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, \
RTYPE) \ RTYPE) \
#CTYPE, #CTYPE,
@ -581,8 +582,7 @@ class CppGenerator : public BaseGenerator {
bool is_constructor) { bool is_constructor) {
auto &ptr_type = PtrType(field); auto &ptr_type = PtrType(field);
if (ptr_type != "naked") { if (ptr_type != "naked") {
return (ptr_type != "default_ptr_type" ? ptr_type : return (ptr_type != "default_ptr_type" ? ptr_type : parser_.opts.cpp_object_api_pointer_type) + "<" + type + ">";
parser_.opts.cpp_object_api_pointer_type) + "<" + type + ">";
} else if (is_constructor) { } else if (is_constructor) {
return ""; return "";
} else { } else {
@ -1128,13 +1128,13 @@ class CppGenerator : public BaseGenerator {
code_ += " switch (lhs.type) {"; code_ += " switch (lhs.type) {";
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
++it) { ++it) {
const auto &ev = **it; const auto &ev = **it;
code_.SetValue("NATIVE_ID", GetEnumValUse(enum_def, ev)); code_.SetValue("NATIVE_ID", GetEnumValUse(enum_def, ev));
if (ev.value) { if (ev.value) {
const auto native_type = const auto native_type =
NativeName(GetUnionElement(ev, true, true, true), NativeName(GetUnionElement(ev, true, true, true),
ev.union_type.struct_def, parser_.opts); ev.union_type.struct_def, parser_.opts);
code_.SetValue("NATIVE_TYPE", native_type); code_.SetValue("NATIVE_TYPE", native_type);
code_ += " case {{NATIVE_ID}}: {"; code_ += " case {{NATIVE_ID}}: {";
code_ += " return *(reinterpret_cast<const {{NATIVE_TYPE}} *>(lhs.value)) =="; code_ += " return *(reinterpret_cast<const {{NATIVE_TYPE}} *>(lhs.value)) ==";
@ -1395,7 +1395,7 @@ class CppGenerator : public BaseGenerator {
} }
std::string GenDefaultConstant(const FieldDef &field) { std::string GenDefaultConstant(const FieldDef &field) {
if(IsFloat(field.value.type.base_type)) if (IsFloat(field.value.type.base_type))
return float_const_gen_.GenFloatConstant(field); return float_const_gen_.GenFloatConstant(field);
else else
return field.value.constant; return field.value.constant;
@ -1461,8 +1461,8 @@ class CppGenerator : public BaseGenerator {
auto cpp_type = field.attributes.Lookup("cpp_type"); auto cpp_type = field.attributes.Lookup("cpp_type");
auto full_type = auto full_type =
(cpp_type ? (field.value.type.base_type == BASE_TYPE_VECTOR (cpp_type ? (field.value.type.base_type == BASE_TYPE_VECTOR
? "std::vector<" + GenTypeNativePtr(cpp_type->constant, &field, false) + "> " ? "std::vector<" + GenTypeNativePtr(cpp_type->constant, &field, false) + "> "
: GenTypeNativePtr(cpp_type->constant, &field, false)) : GenTypeNativePtr(cpp_type->constant, &field, false))
: type + " "); : type + " ");
code_.SetValue("FIELD_TYPE", full_type); code_.SetValue("FIELD_TYPE", full_type);
code_.SetValue("FIELD_NAME", Name(field)); code_.SetValue("FIELD_NAME", Name(field));
@ -1520,9 +1520,9 @@ class CppGenerator : public BaseGenerator {
it != struct_def.fields.vec.end(); ++it) { it != struct_def.fields.vec.end(); ++it) {
const auto &field = **it; const auto &field = **it;
if (!field.deprecated && // Deprecated fields won't be accessible. if (!field.deprecated && // Deprecated fields won't be accessible.
field.value.type.base_type != BASE_TYPE_UTYPE && field.value.type.base_type != BASE_TYPE_UTYPE &&
(field.value.type.base_type != BASE_TYPE_VECTOR || (field.value.type.base_type != BASE_TYPE_VECTOR ||
field.value.type.element != BASE_TYPE_UTYPE)) { field.value.type.element != BASE_TYPE_UTYPE)) {
if (!compare_op.empty()) { if (!compare_op.empty()) {
compare_op += " &&\n "; compare_op += " &&\n ";
} }
@ -1706,7 +1706,6 @@ class CppGenerator : public BaseGenerator {
code_ += " }"; code_ += " }";
} }
GenFullyQualifiedNameGetter(struct_def, Name(struct_def)); GenFullyQualifiedNameGetter(struct_def, Name(struct_def));
// Generate field id constants. // Generate field id constants.
@ -2064,8 +2063,9 @@ class CppGenerator : public BaseGenerator {
// Generate a CreateXDirect function with vector types as parameters // Generate a CreateXDirect function with vector types as parameters
if (has_string_or_vector_fields) { if (has_string_or_vector_fields) {
code_ += "inline flatbuffers::Offset<{{STRUCT_NAME}}> " code_ +=
"Create{{STRUCT_NAME}}Direct("; "inline flatbuffers::Offset<{{STRUCT_NAME}}> "
"Create{{STRUCT_NAME}}Direct(";
code_ += " flatbuffers::FlatBufferBuilder &_fbb\\"; code_ += " flatbuffers::FlatBufferBuilder &_fbb\\";
for (auto it = struct_def.fields.vec.begin(); for (auto it = struct_def.fields.vec.begin();
it != struct_def.fields.vec.end(); ++it) { it != struct_def.fields.vec.end(); ++it) {
@ -2223,7 +2223,7 @@ class CppGenerator : public BaseGenerator {
} else { } else {
code += "_o->" + name + "[_i]" + access + " = "; code += "_o->" + name + "[_i]" + access + " = ";
code += code +=
GenUnpackVal(field.value.type.VectorType(), indexing, true, field); GenUnpackVal(field.value.type.VectorType(), indexing, true, field);
} }
code += "; } }"; code += "; } }";
break; break;
@ -2294,7 +2294,6 @@ class CppGenerator : public BaseGenerator {
type + ">((*_rehasher)(" + value + GenPtrGet(field) + ")) : 0"; type + ">((*_rehasher)(" + value + GenPtrGet(field) + ")) : 0";
} }
std::string code; std::string code;
switch (field.value.type.base_type) { switch (field.value.type.base_type) {
// String fields are of the form: // String fields are of the form: