From 786f69b248f731b7df009046e8b688b67dbd1da8 Mon Sep 17 00:00:00 2001 From: mustiikhalil Date: Fri, 22 Jan 2021 21:46:53 +0300 Subject: [PATCH] Formats cpp code (#6349) --- include/flatbuffers/flatbuffers.h | 25 +++++------ include/flatbuffers/reflection.h | 42 +++++++++--------- src/idl_gen_cpp.cpp | 9 ++-- src/idl_gen_csharp.cpp | 9 ++-- src/idl_gen_go.cpp | 4 +- src/idl_gen_json_schema.cpp | 3 +- src/idl_gen_php.cpp | 4 +- src/idl_gen_python.cpp | 18 +++++--- src/idl_gen_rust.cpp | 91 +++++++++++++++++++++++++++++++++------ src/idl_parser.cpp | 16 +++---- tests/test.cpp | 46 ++++++++++---------- 11 files changed, 167 insertions(+), 100 deletions(-) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index 300ab6f..4402c71 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -21,7 +21,7 @@ #include "flatbuffers/stl_emulation.h" #ifndef FLATBUFFERS_CPP98_STL - #include +# include #endif #if defined(FLATBUFFERS_NAN_DEFAULTS) @@ -505,8 +505,8 @@ template class Array { (void)p2; CopyFromSpanImpl( - flatbuffers::integral_constant(), + flatbuffers::integral_constant < bool, + !scalar_tag::value || sizeof(T) == 1 || FLATBUFFERS_LITTLEENDIAN > (), src); } @@ -584,12 +584,12 @@ template class Array, length> { // Cast a raw T[length] to a raw flatbuffers::Array // without endian conversion. Use with care. template -Array& CastToArray(T (&arr)[length]) { +Array &CastToArray(T (&arr)[length]) { return *reinterpret_cast *>(arr); } template -const Array& CastToArray(const T (&arr)[length]) { +const Array &CastToArray(const T (&arr)[length]) { return *reinterpret_cast *>(arr); } @@ -648,7 +648,7 @@ static inline const char *GetCstring(const String *str) { static inline flatbuffers::string_view GetStringView(const String *str) { return str ? str->string_view() : flatbuffers::string_view(); } -#endif // FLATBUFFERS_HAS_STRING_VIEW +#endif // FLATBUFFERS_HAS_STRING_VIEW // Allocator interface. This is flatbuffers-specific and meant only for // `vector_downward` usage. @@ -1692,9 +1692,7 @@ class FlatBufferBuilder { // causing the wrong overload to be selected, remove it. AssertScalarT(); StartVector(len, sizeof(T)); - if (len == 0) { - return Offset>(EndVector(len)); - } + if (len == 0) { return Offset>(EndVector(len)); } // clang-format off #if FLATBUFFERS_LITTLEENDIAN PushBytes(reinterpret_cast(v), len * sizeof(T)); @@ -2825,9 +2823,12 @@ inline const char * const *ElementaryTypeNames() { // bitfields is otherwise implementation-defined and causes warnings on older // GCC compilers. struct TypeCode { - unsigned short base_type : 4; // ElementaryType - unsigned short is_repeating : 1; // Either vector (in table) or array (in struct) - signed short sequence_ref : 11; // Index into type_refs below, or -1 for none. + // ElementaryType + unsigned short base_type : 4; + // Either vector (in table) or array (in struct) + unsigned short is_repeating : 1; + // Index into type_refs below, or -1 for none. + signed short sequence_ref : 11; }; static_assert(sizeof(TypeCode) == 2, "TypeCode"); diff --git a/include/flatbuffers/reflection.h b/include/flatbuffers/reflection.h index 70d9971..c6fa411 100644 --- a/include/flatbuffers/reflection.h +++ b/include/flatbuffers/reflection.h @@ -47,29 +47,31 @@ inline bool IsLong(reflection::BaseType t) { inline size_t GetTypeSize(reflection::BaseType base_type) { // This needs to correspond to the BaseType enum. static size_t sizes[] = { - 0, // None - 1, // UType - 1, // Bool - 1, // Byte - 1, // UByte - 2, // Short - 2, // UShort - 4, // Int - 4, // UInt - 8, // Long - 8, // ULong - 4, // Float - 8, // Double - 4, // String - 4, // Vector - 4, // Obj - 4, // Union - 0, // Array. Only used in structs. 0 was chosen to prevent out-of-bounds errors. + 0, // None + 1, // UType + 1, // Bool + 1, // Byte + 1, // UByte + 2, // Short + 2, // UShort + 4, // Int + 4, // UInt + 8, // Long + 8, // ULong + 4, // Float + 8, // Double + 4, // String + 4, // Vector + 4, // Obj + 4, // Union + 0, // Array. Only used in structs. 0 was chosen to prevent out-of-bounds + // errors. 0 // MaxBaseType. This must be kept the last entry in this array. - }; + }; static_assert(sizeof(sizes) / sizeof(size_t) == reflection::MaxBaseType + 1, - "Size of sizes[] array does not match the count of BaseType enum values."); + "Size of sizes[] array does not match the count of BaseType " + "enum values."); return sizes[base_type]; } diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 742587a..629b2dc 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -1,4 +1,4 @@ - /* +/* * Copyright 2014 Google Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -2462,8 +2462,7 @@ class CppGenerator : public BaseGenerator { if (!field.deprecated) { code_.SetValue("FIELD_NAME", Name(field)); code_ += ",\n {{FIELD_NAME}}\\"; - if (IsString(field.value.type) || - IsVector(field.value.type)) { + if (IsString(field.value.type) || IsVector(field.value.type)) { code_ += "__\\"; } } @@ -2551,9 +2550,7 @@ class CppGenerator : public BaseGenerator { WrapInNameSpace(*field.value.type.enum_def) + ">("; } indexing += "_e->Get(_i)"; - if (field.value.type.enum_def) { - indexing += ")"; - } + if (field.value.type.enum_def) { indexing += ")"; } if (field.value.type.element == BASE_TYPE_BOOL) { indexing += " != 0"; } diff --git a/src/idl_gen_csharp.cpp b/src/idl_gen_csharp.cpp index bc2f9bd..b4ad472 100644 --- a/src/idl_gen_csharp.cpp +++ b/src/idl_gen_csharp.cpp @@ -1515,10 +1515,11 @@ class CSharpGenerator : public BaseGenerator { case BASE_TYPE_ARRAY: { auto type_name = GenTypeGet_ObjectAPI(field.value.type, opts); auto length_str = NumToString(field.value.type.fixed_length); - auto unpack_method = field.value.type.struct_def == nullptr ? "" - : field.value.type.struct_def->fixed - ? ".UnPack()" - : "?.UnPack()"; + auto unpack_method = field.value.type.struct_def == nullptr + ? "" + : field.value.type.struct_def->fixed + ? ".UnPack()" + : "?.UnPack()"; code += start + "new " + type_name.substr(0, type_name.length() - 1) + length_str + "];\n"; code += " for (var _j = 0; _j < " + length_str + "; ++_j) { _o." + diff --git a/src/idl_gen_go.cpp b/src/idl_gen_go.cpp index 68cc01f..5dfd8c8 100644 --- a/src/idl_gen_go.cpp +++ b/src/idl_gen_go.cpp @@ -266,7 +266,9 @@ class GoGenerator : public BaseGenerator { if (i == 0) { code += "\tn := flatbuffers.GetUOffsetT(buf[offset:])\n"; } else { - code += "\tn := flatbuffers.GetUOffsetT(buf[offset+flatbuffers.SizeUint32:])\n"; + code += + "\tn := " + "flatbuffers.GetUOffsetT(buf[offset+flatbuffers.SizeUint32:])\n"; } code += "\tx := &" + struct_def.name + "{}\n"; if (i == 0) { diff --git a/src/idl_gen_json_schema.cpp b/src/idl_gen_json_schema.cpp index 76540d1..f8e70bf 100644 --- a/src/idl_gen_json_schema.cpp +++ b/src/idl_gen_json_schema.cpp @@ -268,8 +268,7 @@ class JsonSchemaGenerator : public BaseGenerator { } bool save() const { - const auto file_path = - GeneratedFileName(path_, file_name_, parser_.opts); + const auto file_path = GeneratedFileName(path_, file_name_, parser_.opts); return SaveFile(file_path.c_str(), code_, false); } diff --git a/src/idl_gen_php.cpp b/src/idl_gen_php.cpp index b986fe3..b763582 100644 --- a/src/idl_gen_php.cpp +++ b/src/idl_gen_php.cpp @@ -735,9 +735,7 @@ class PhpGenerator : public BaseGenerator { } else { BuildFieldOfTable(field, offset, code_ptr); } - if (IsVector(field.value.type)) { - BuildVectorOfTable(field, code_ptr); - } + if (IsVector(field.value.type)) { BuildVectorOfTable(field, code_ptr); } } GetEndOffsetOnTable(struct_def, code_ptr); diff --git a/src/idl_gen_python.cpp b/src/idl_gen_python.cpp index 0aa5557..3cd93d6 100644 --- a/src/idl_gen_python.cpp +++ b/src/idl_gen_python.cpp @@ -413,14 +413,14 @@ class PythonGenerator : public BaseGenerator { const FieldDef &field, std::string *code_ptr) { auto nested = field.attributes.Lookup("nested_flatbuffer"); - if (!nested) { return; } // There is no nested flatbuffer. + if (!nested) { return; } // There is no nested flatbuffer. std::string unqualified_name = nested->constant; std::string qualified_name = nested->constant; auto nested_root = parser_.LookupStruct(nested->constant); if (nested_root == nullptr) { - qualified_name = parser_.current_namespace_->GetFullyQualifiedName( - nested->constant); + qualified_name = + parser_.current_namespace_->GetFullyQualifiedName(nested->constant); nested_root = parser_.LookupStruct(qualified_name); } FLATBUFFERS_ASSERT(nested_root); // Guaranteed to exist by parser. @@ -1250,7 +1250,8 @@ class PythonGenerator : public BaseGenerator { field_instance_name + "))):"; code_prefix += GenIndents(4) + "self." + field_instance_name + "[i].Pack(builder)"; - code_prefix += GenIndents(3) + field_instance_name + " = builder.EndVector()"; + code_prefix += + GenIndents(3) + field_instance_name + " = builder.EndVector()"; } else { // If the vector is a struct vector, we need to first build accessor for // each struct element. @@ -1267,7 +1268,8 @@ class PythonGenerator : public BaseGenerator { field_instance_name + "))):"; code_prefix += GenIndents(4) + "builder.PrependUOffsetTRelative" + "(" + field_instance_name + "list[i])"; - code_prefix += GenIndents(3) + field_instance_name + " = builder.EndVector()"; + code_prefix += + GenIndents(3) + field_instance_name + " = builder.EndVector()"; } // Adds the field into the struct. @@ -1341,7 +1343,8 @@ class PythonGenerator : public BaseGenerator { field_instance_name + "[i]))"; GenPackForScalarVectorFieldHelper(struct_def, field, code_prefix_ptr, 3); code_prefix += "(" + MakeLowerCamel(field) + "list[i])"; - code_prefix += GenIndents(3) + field_instance_name + " = builder.EndVector()"; + code_prefix += + GenIndents(3) + field_instance_name + " = builder.EndVector()"; return; } @@ -1353,7 +1356,8 @@ class PythonGenerator : public BaseGenerator { code_prefix += GenIndents(3) + "else:"; GenPackForScalarVectorFieldHelper(struct_def, field, code_prefix_ptr, 4); code_prefix += "(self." + field_instance_name + "[i])"; - code_prefix += GenIndents(4) + field_instance_name + " = builder.EndVector()"; + code_prefix += + GenIndents(4) + field_instance_name + " = builder.EndVector()"; } void GenPackForStructField(const StructDef &struct_def, const FieldDef &field, diff --git a/src/idl_gen_rust.cpp b/src/idl_gen_rust.cpp index 5fc3090..f9316ee 100644 --- a/src/idl_gen_rust.cpp +++ b/src/idl_gen_rust.cpp @@ -200,19 +200,77 @@ class RustGenerator : public BaseGenerator { // changes to that webpage in the future. // currently-used keywords - "as", "break", "const", "continue", "crate", "else", "enum", "extern", - "false", "fn", "for", "if", "impl", "in", "let", "loop", "match", "mod", - "move", "mut", "pub", "ref", "return", "Self", "self", "static", "struct", - "super", "trait", "true", "type", "unsafe", "use", "where", "while", + "as", + "break", + "const", + "continue", + "crate", + "else", + "enum", + "extern", + "false", + "fn", + "for", + "if", + "impl", + "in", + "let", + "loop", + "match", + "mod", + "move", + "mut", + "pub", + "ref", + "return", + "Self", + "self", + "static", + "struct", + "super", + "trait", + "true", + "type", + "unsafe", + "use", + "where", + "while", // future possible keywords - "abstract", "alignof", "become", "box", "do", "final", "macro", - "offsetof", "override", "priv", "proc", "pure", "sizeof", "typeof", - "unsized", "virtual", "yield", + "abstract", + "alignof", + "become", + "box", + "do", + "final", + "macro", + "offsetof", + "override", + "priv", + "proc", + "pure", + "sizeof", + "typeof", + "unsized", + "virtual", + "yield", // other rust terms we should not use - "std", "usize", "isize", "u8", "i8", "u16", "i16", "u32", "i32", "u64", - "i64", "u128", "i128", "f32", "f64", + "std", + "usize", + "isize", + "u8", + "i8", + "u16", + "i16", + "u32", + "i32", + "u64", + "i64", + "u128", + "i128", + "f32", + "f64", // These are terms the code generator can implement on types. // @@ -223,12 +281,19 @@ class RustGenerator : public BaseGenerator { // implementation detail, and how we implement methods could change in // the future. as a result, we proactively block these out as reserved // words. - "follow", "push", "size", "alignment", "to_little_endian", - "from_little_endian", nullptr, + "follow", + "push", + "size", + "alignment", + "to_little_endian", + "from_little_endian", + nullptr, // used by Enum constants - "ENUM_MAX", "ENUM_MIN", "ENUM_VALUES", - }; // clang-format on + "ENUM_MAX", + "ENUM_MIN", + "ENUM_VALUES", + }; for (auto kw = keywords; *kw; kw++) keywords_.insert(*kw); } diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index 324ba93..aeb452b 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -143,8 +143,7 @@ void Parser::Message(const std::string &msg) { } void Parser::Warning(const std::string &msg) { - if (!opts.no_warnings) - Message("warning: " + msg); + if (!opts.no_warnings) Message("warning: " + msg); } CheckedError Parser::Error(const std::string &msg) { @@ -203,8 +202,7 @@ bool atot_scalar(const char *s, T *val, bool_constant) { return true; } -template -CheckedError atot(const char *s, Parser &parser, T *val) { +template CheckedError atot(const char *s, Parser &parser, T *val) { auto done = atot_scalar(s, val, bool_constant::value>()); if (done) return NoError(); if (0 == *val) @@ -3301,13 +3299,11 @@ CheckedError Parser::DoParse(const char *source, const char **include_paths, return NoError(); } -CheckedError Parser::DoParseJson() -{ +CheckedError Parser::DoParseJson() { if (token_ != '{') { EXPECT('{'); } else { - if (!root_struct_def_) - return Error("no root type set to parse json with"); + if (!root_struct_def_) return Error("no root type set to parse json with"); if (builder_.GetSize()) { return Error("cannot have more than one json object in a file"); } @@ -3319,8 +3315,8 @@ CheckedError Parser::DoParseJson() file_identifier_.length() ? file_identifier_.c_str() : nullptr); } else { builder_.Finish(Offset(toff), file_identifier_.length() - ? file_identifier_.c_str() - : nullptr); + ? file_identifier_.c_str() + : nullptr); } } // Check that JSON file doesn't contain more objects or IDL directives. diff --git a/tests/test.cpp b/tests/test.cpp index 3b0ee47..d57b298 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -697,7 +697,7 @@ template bool is_quiet_nan_impl(T v) { std::memcpy(&b, &v, sizeof(T)); return ((b & qnan_base) == qnan_base); } -#if defined(__mips__) || defined(__hppa__) +# if defined(__mips__) || defined(__hppa__) static bool is_quiet_nan(float v) { return is_quiet_nan_impl(v) || is_quiet_nan_impl(v); @@ -706,14 +706,14 @@ static bool is_quiet_nan(double v) { return is_quiet_nan_impl(v) || is_quiet_nan_impl(v); } -#else +# else static bool is_quiet_nan(float v) { return is_quiet_nan_impl(v); } static bool is_quiet_nan(double v) { return is_quiet_nan_impl(v); } -#endif +# endif void TestMonsterExtraFloats() { TEST_EQ(is_quiet_nan(1.0), false); @@ -3420,21 +3420,21 @@ void FixedLengthArrayTest() { // set memory chunk of size ArrayStruct to 1's std::memset(static_cast(non_zero_memory), 1, arr_size); // after placement-new it should be all 0's -#if defined (_MSC_VER) && defined (_DEBUG) - #undef new -#endif - MyGame::Example::ArrayStruct *ap = new (non_zero_memory) MyGame::Example::ArrayStruct; -#if defined (_MSC_VER) && defined (_DEBUG) - #define new DEBUG_NEW -#endif +# if defined(_MSC_VER) && defined(_DEBUG) +# undef new +# endif + MyGame::Example::ArrayStruct *ap = + new (non_zero_memory) MyGame::Example::ArrayStruct; +# if defined(_MSC_VER) && defined(_DEBUG) +# define new DEBUG_NEW +# endif (void)ap; - for (size_t i = 0; i < arr_size; ++i) { - TEST_EQ(non_zero_memory[i], 0); - } + for (size_t i = 0; i < arr_size; ++i) { TEST_EQ(non_zero_memory[i], 0); } #endif } -#if !defined(FLATBUFFERS_SPAN_MINIMAL) && (!defined(_MSC_VER) || _MSC_VER >= 1700) +#if !defined(FLATBUFFERS_SPAN_MINIMAL) && \ + (!defined(_MSC_VER) || _MSC_VER >= 1700) void FixedLengthArrayConstructorTest() { const int32_t nested_a[2] = { 1, 2 }; MyGame::Example::TestEnum nested_c[2] = { MyGame::Example::TestEnum::A, @@ -3481,8 +3481,7 @@ void FixedLengthArrayConstructorTest() { TEST_EQ(arr_struct.f()->Get(1), -1); } #else -void FixedLengthArrayConstructorTest() { -} +void FixedLengthArrayConstructorTest() {} #endif void NativeTypeTest() { @@ -3639,12 +3638,15 @@ void OptionalScalarsTest() { schemas.push_back("table Monster { mana : bool; }"); schemas.push_back("table Monster { mana : bool = 42; }"); schemas.push_back("table Monster { mana : bool = null; }"); - schemas.push_back("enum Enum: int {A=0, B=1} " - "table Monster { mana : Enum; }"); - schemas.push_back("enum Enum: int {A=0, B=1} " - "table Monster { mana : Enum = B; }"); - schemas.push_back("enum Enum: int {A=0, B=1} " - "table Monster { mana : Enum = null; }"); + schemas.push_back( + "enum Enum: int {A=0, B=1} " + "table Monster { mana : Enum; }"); + schemas.push_back( + "enum Enum: int {A=0, B=1} " + "table Monster { mana : Enum = B; }"); + schemas.push_back( + "enum Enum: int {A=0, B=1} " + "table Monster { mana : Enum = null; }"); // Check the FieldDef is correctly set. for (auto schema = schemas.begin(); schema < schemas.end(); schema++) { -- 2.7.4