Formats cpp code (#6349)

This commit is contained in:
mustiikhalil
2021-01-22 21:46:53 +03:00
committed by GitHub
parent 1da0a2dfac
commit 786f69b248
11 changed files with 167 additions and 100 deletions

View File

@@ -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";
}

View File

@@ -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." +

View File

@@ -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) {

View File

@@ -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);
}

View File

@@ -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);

View File

@@ -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,

View File

@@ -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);
}

View File

@@ -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<true>) {
return true;
}
template<typename T>
CheckedError atot(const char *s, Parser &parser, T *val) {
template<typename T> CheckedError atot(const char *s, Parser &parser, T *val) {
auto done = atot_scalar(s, val, bool_constant<is_floating_point<T>::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<Table>(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.