Made all C++ files clang-formatted.

Also added missing generated files.

Change-Id: Ifd22a643a08e3f2edfce92812ed57b87fc0e1875
This commit is contained in:
Wouter van Oortmerssen
2017-12-21 10:54:28 -08:00
parent 5d9930aa0d
commit 89711c9c47
43 changed files with 5850 additions and 5992 deletions

View File

@@ -16,21 +16,21 @@
// independent from idl_parser, since this code is not needed for most clients
#include <string>
#include <sstream>
#include <string>
#include "flatbuffers/code_generators.h"
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/util.h"
#include "flatbuffers/code_generators.h"
#ifdef _WIN32
#include <direct.h>
#define PATH_SEPARATOR "\\"
#define mkdir(n, m) _mkdir(n)
# include <direct.h>
# define PATH_SEPARATOR "\\"
# define mkdir(n, m) _mkdir(n)
#else
#include <sys/stat.h>
#define PATH_SEPARATOR "/"
# include <sys/stat.h>
# define PATH_SEPARATOR "/"
#endif
namespace flatbuffers {
@@ -44,9 +44,10 @@ namespace go {
// see https://golang.org/ref/spec#Keywords
static const char *g_golang_keywords[] = {
"break", "default", "func", "interface", "select", "case", "defer", "go",
"map", "struct", "chan", "else", "goto", "package", "switch", "const",
"fallthrough", "if", "range", "type", "continue", "for", "import", "return", "var",
"break", "default", "func", "interface", "select", "case", "defer",
"go", "map", "struct", "chan", "else", "goto", "package",
"switch", "const", "fallthrough", "if", "range", "type", "continue",
"for", "import", "return", "var",
};
static std::string GenGetter(const Type &type);
@@ -57,23 +58,20 @@ static void GenReceiver(const StructDef &struct_def, std::string *code_ptr);
static std::string GenTypeBasic(const Type &type);
static std::string GenTypeGet(const Type &type);
static std::string TypeName(const FieldDef &field);
static std::string GoIdentity(const std::string& name) {
for (size_t i=0; i<sizeof(g_golang_keywords)/sizeof(g_golang_keywords[0]); i++) {
if (name == g_golang_keywords[i]) {
return MakeCamel(name + "_", false);
}
static std::string GoIdentity(const std::string &name) {
for (size_t i = 0;
i < sizeof(g_golang_keywords) / sizeof(g_golang_keywords[0]); i++) {
if (name == g_golang_keywords[i]) { return MakeCamel(name + "_", false); }
}
return MakeCamel(name, false);
}
// Most field accessors need to retrieve and test the field offset first,
// this is the prefix code for that.
std::string OffsetPrefix(const FieldDef &field) {
return "{\n\to := flatbuffers.UOffsetT(rcv._tab.Offset(" +
NumToString(field.value.offset) +
"))\n\tif o != 0 {\n";
NumToString(field.value.offset) + "))\n\tif o != 0 {\n";
}
// Begin a class declaration.
@@ -169,7 +167,7 @@ static void InitializeExisting(const StructDef &struct_def,
// Implement the table accessor
static void GenTableAccessor(const StructDef &struct_def,
std::string *code_ptr) {
std::string *code_ptr) {
std::string &code = *code_ptr;
GenReceiver(struct_def, code_ptr);
@@ -177,16 +175,15 @@ static void GenTableAccessor(const StructDef &struct_def,
code += "{\n";
if (struct_def.fixed) {
code += "\treturn rcv._tab.Table\n";
code += "\treturn rcv._tab.Table\n";
} else {
code += "\treturn rcv._tab\n";
code += "\treturn rcv._tab\n";
}
code += "}\n\n";
}
// Get the length of a vector.
static void GetVectorLen(const StructDef &struct_def,
const FieldDef &field,
static void GetVectorLen(const StructDef &struct_def, const FieldDef &field,
std::string *code_ptr) {
std::string &code = *code_ptr;
@@ -198,8 +195,7 @@ static void GetVectorLen(const StructDef &struct_def,
}
// Get a [ubyte] vector as a byte slice.
static void GetUByteSlice(const StructDef &struct_def,
const FieldDef &field,
static void GetUByteSlice(const StructDef &struct_def, const FieldDef &field,
std::string *code_ptr) {
std::string &code = *code_ptr;
@@ -219,7 +215,7 @@ static void GetScalarFieldOfStruct(const StructDef &struct_def,
GenReceiver(struct_def, code_ptr);
code += " " + MakeCamel(field.name);
code += "() " + TypeName(field) + " {\n";
code +="\treturn " + getter;
code += "\treturn " + getter;
code += "(rcv._tab.Pos + flatbuffers.UOffsetT(";
code += NumToString(field.value.offset) + "))\n}\n";
}
@@ -284,12 +280,11 @@ static void GetStructFieldOfTable(const StructDef &struct_def,
}
// Get the value of a string.
static void GetStringField(const StructDef &struct_def,
const FieldDef &field,
static void GetStringField(const StructDef &struct_def, const FieldDef &field,
std::string *code_ptr) {
std::string &code = *code_ptr;
GenReceiver(struct_def, code_ptr);
code += " " + MakeCamel(field.name);
code += " " + MakeCamel(field.name);
code += "() " + TypeName(field) + " ";
code += OffsetPrefix(field) + "\t\treturn " + GenGetter(field.value.type);
code += "(o + rcv._tab.Pos)\n\t}\n\treturn nil\n";
@@ -297,8 +292,7 @@ static void GetStringField(const StructDef &struct_def,
}
// Get the value of a union from an object.
static void GetUnionField(const StructDef &struct_def,
const FieldDef &field,
static void GetUnionField(const StructDef &struct_def, const FieldDef &field,
std::string *code_ptr) {
std::string &code = *code_ptr;
GenReceiver(struct_def, code_ptr);
@@ -365,8 +359,8 @@ static void BeginBuilderArgs(const StructDef &struct_def,
std::string &code = *code_ptr;
if (code.substr(code.length() - 2) != "\n\n") {
// a previous mutate has not put an extra new line
code += "\n";
// a previous mutate has not put an extra new line
code += "\n";
}
code += "func Create" + struct_def.name;
code += "(builder *flatbuffers.Builder";
@@ -375,22 +369,19 @@ static void BeginBuilderArgs(const StructDef &struct_def,
// Recursively generate arguments for a constructor, to deal with nested
// structs.
static void StructBuilderArgs(const StructDef &struct_def,
const char *nameprefix,
std::string *code_ptr) {
const char *nameprefix, std::string *code_ptr) {
for (auto it = struct_def.fields.vec.begin();
it != struct_def.fields.vec.end();
++it) {
it != struct_def.fields.vec.end(); ++it) {
auto &field = **it;
if (IsStruct(field.value.type)) {
// Generate arguments for a struct inside a struct. To ensure names
// don't clash, and to make it obvious these arguments are constructing
// a nested struct, prefix the name with the field name.
StructBuilderArgs(*field.value.type.struct_def,
(nameprefix + (field.name + "_")).c_str(),
code_ptr);
(nameprefix + (field.name + "_")).c_str(), code_ptr);
} else {
std::string &code = *code_ptr;
code += (std::string)", " + nameprefix;
code += (std::string) ", " + nameprefix;
code += GoIdentity(field.name);
code += " " + GenTypeBasic(field.value.type);
}
@@ -406,21 +397,18 @@ static void EndBuilderArgs(std::string *code_ptr) {
// Recursively generate struct construction statements and instert manual
// padding.
static void StructBuilderBody(const StructDef &struct_def,
const char *nameprefix,
std::string *code_ptr) {
const char *nameprefix, std::string *code_ptr) {
std::string &code = *code_ptr;
code += "\tbuilder.Prep(" + NumToString(struct_def.minalign) + ", ";
code += NumToString(struct_def.bytesize) + ")\n";
for (auto it = struct_def.fields.vec.rbegin();
it != struct_def.fields.vec.rend();
++it) {
it != struct_def.fields.vec.rend(); ++it) {
auto &field = **it;
if (field.padding)
code += "\tbuilder.Pad(" + NumToString(field.padding) + ")\n";
if (IsStruct(field.value.type)) {
StructBuilderBody(*field.value.type.struct_def,
(nameprefix + (field.name + "_")).c_str(),
code_ptr);
(nameprefix + (field.name + "_")).c_str(), code_ptr);
} else {
code += "\tbuilder.Prepend" + GenMethod(field) + "(";
code += nameprefix + GoIdentity(field.name) + ")\n";
@@ -447,8 +435,7 @@ static void GetStartOfTable(const StructDef &struct_def,
// Set the value of a table's field.
static void BuildFieldOfTable(const StructDef &struct_def,
const FieldDef &field,
const size_t offset,
const FieldDef &field, const size_t offset,
std::string *code_ptr) {
std::string &code = *code_ptr;
code += "func " + struct_def.name + "Add" + MakeCamel(field.name);
@@ -476,8 +463,7 @@ static void BuildFieldOfTable(const StructDef &struct_def,
// Set the value of one of the members of a table's vector.
static void BuildVectorOfTable(const StructDef &struct_def,
const FieldDef &field,
std::string *code_ptr) {
const FieldDef &field, std::string *code_ptr) {
std::string &code = *code_ptr;
code += "func " + struct_def.name + "Start";
code += MakeCamel(field.name);
@@ -508,8 +494,7 @@ static void GenReceiver(const StructDef &struct_def, std::string *code_ptr) {
// Generate a struct field getter, conditioned on its child type(s).
static void GenStructAccessor(const StructDef &struct_def,
const FieldDef &field,
std::string *code_ptr) {
const FieldDef &field, std::string *code_ptr) {
GenComment(field.doc_comment, code_ptr, nullptr, "");
if (IsScalar(field.value.type.base_type)) {
if (struct_def.fixed) {
@@ -526,9 +511,7 @@ static void GenStructAccessor(const StructDef &struct_def,
GetStructFieldOfTable(struct_def, field, code_ptr);
}
break;
case BASE_TYPE_STRING:
GetStringField(struct_def, field, code_ptr);
break;
case BASE_TYPE_STRING: GetStringField(struct_def, field, code_ptr); break;
case BASE_TYPE_VECTOR: {
auto vectortype = field.value.type.VectorType();
if (vectortype.base_type == BASE_TYPE_STRUCT) {
@@ -538,11 +521,8 @@ static void GenStructAccessor(const StructDef &struct_def,
}
break;
}
case BASE_TYPE_UNION:
GetUnionField(struct_def, field, code_ptr);
break;
default:
assert(0);
case BASE_TYPE_UNION: GetUnionField(struct_def, field, code_ptr); break;
default: assert(0);
}
}
if (field.value.type.base_type == BASE_TYPE_VECTOR) {
@@ -555,8 +535,8 @@ static void GenStructAccessor(const StructDef &struct_def,
// Mutate the value of a struct's scalar.
static void MutateScalarFieldOfStruct(const StructDef &struct_def,
const FieldDef &field,
std::string *code_ptr) {
const FieldDef &field,
std::string *code_ptr) {
std::string &code = *code_ptr;
std::string type = MakeCamel(GenTypeBasic(field.value.type));
std::string setter = "rcv._tab.Mutate" + type;
@@ -569,8 +549,8 @@ static void MutateScalarFieldOfStruct(const StructDef &struct_def,
// Mutate the value of a table's scalar.
static void MutateScalarFieldOfTable(const StructDef &struct_def,
const FieldDef &field,
std::string *code_ptr) {
const FieldDef &field,
std::string *code_ptr) {
std::string &code = *code_ptr;
std::string type = MakeCamel(GenTypeBasic(field.value.type));
std::string setter = "rcv._tab.Mutate" + type + "Slot";
@@ -582,9 +562,8 @@ static void MutateScalarFieldOfTable(const StructDef &struct_def,
}
// Generate a struct field setter, conditioned on its child type(s).
static void GenStructMutator(const StructDef &struct_def,
const FieldDef &field,
std::string *code_ptr) {
static void GenStructMutator(const StructDef &struct_def, const FieldDef &field,
std::string *code_ptr) {
GenComment(field.doc_comment, code_ptr, nullptr, "");
if (IsScalar(field.value.type.base_type)) {
if (struct_def.fixed) {
@@ -601,8 +580,7 @@ static void GenTableBuilders(const StructDef &struct_def,
GetStartOfTable(struct_def, code_ptr);
for (auto it = struct_def.fields.vec.begin();
it != struct_def.fields.vec.end();
++it) {
it != struct_def.fields.vec.end(); ++it) {
auto &field = **it;
if (field.deprecated) continue;
@@ -617,8 +595,7 @@ static void GenTableBuilders(const StructDef &struct_def,
}
// Generate struct or table methods.
static void GenStruct(const StructDef &struct_def,
std::string *code_ptr) {
static void GenStruct(const StructDef &struct_def, std::string *code_ptr) {
if (struct_def.generated) return;
GenComment(struct_def.doc_comment, code_ptr, nullptr);
@@ -636,8 +613,7 @@ static void GenStruct(const StructDef &struct_def,
// Generate struct fields accessors
for (auto it = struct_def.fields.vec.begin();
it != struct_def.fields.vec.end();
++it) {
it != struct_def.fields.vec.end(); ++it) {
auto &field = **it;
if (field.deprecated) continue;
@@ -661,8 +637,7 @@ static void GenEnum(const EnumDef &enum_def, std::string *code_ptr) {
GenComment(enum_def.doc_comment, code_ptr, nullptr);
BeginEnum(code_ptr);
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) {
auto &ev = **it;
GenComment(ev.doc_comment, code_ptr, nullptr, "\t");
@@ -671,8 +646,7 @@ static void GenEnum(const EnumDef &enum_def, std::string *code_ptr) {
EndEnum(code_ptr);
BeginEnumNames(enum_def, code_ptr);
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) {
auto &ev = **it;
EnumNameMember(enum_def, ev, code_ptr);
@@ -686,48 +660,43 @@ static std::string GenGetter(const Type &type) {
case BASE_TYPE_STRING: return "rcv._tab.ByteVector";
case BASE_TYPE_UNION: return "rcv._tab.Union";
case BASE_TYPE_VECTOR: return GenGetter(type.VectorType());
default:
return "rcv._tab.Get" + MakeCamel(GenTypeGet(type));
default: return "rcv._tab.Get" + MakeCamel(GenTypeGet(type));
}
}
// Returns the method name for use with add/put calls.
static std::string GenMethod(const FieldDef &field) {
return IsScalar(field.value.type.base_type)
? MakeCamel(GenTypeBasic(field.value.type))
: (IsStruct(field.value.type) ? "Struct" : "UOffsetT");
? MakeCamel(GenTypeBasic(field.value.type))
: (IsStruct(field.value.type) ? "Struct" : "UOffsetT");
}
static std::string GenTypeBasic(const Type &type) {
static const char *ctypename[] = {
// clang-format off
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
#GTYPE,
FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
#undef FLATBUFFERS_TD
// clang-format on
};
return ctypename[type.base_type];
}
static std::string GenTypePointer(const Type &type) {
switch (type.base_type) {
case BASE_TYPE_STRING:
return "[]byte";
case BASE_TYPE_VECTOR:
return GenTypeGet(type.VectorType());
case BASE_TYPE_STRUCT:
return type.struct_def->name;
case BASE_TYPE_STRING: return "[]byte";
case BASE_TYPE_VECTOR: return GenTypeGet(type.VectorType());
case BASE_TYPE_STRUCT: return type.struct_def->name;
case BASE_TYPE_UNION:
// fall through
default:
return "*flatbuffers.Table";
default: return "*flatbuffers.Table";
}
}
static std::string GenTypeGet(const Type &type) {
return IsScalar(type.base_type)
? GenTypeBasic(type)
: GenTypePointer(type);
return IsScalar(type.base_type) ? GenTypeBasic(type) : GenTypePointer(type);
}
static std::string TypeName(const FieldDef &field) {
@@ -749,7 +718,8 @@ class GoGenerator : public BaseGenerator {
public:
GoGenerator(const Parser &parser, const std::string &path,
const std::string &file_name, const std::string &go_namespace)
: BaseGenerator(parser, path, file_name, "" /* not used*/, "" /* not used */) {
: BaseGenerator(parser, path, file_name, "" /* not used*/,
"" /* not used */) {
std::istringstream iss(go_namespace);
std::string component;
while (std::getline(iss, component, '.')) {
@@ -802,8 +772,8 @@ class GoGenerator : public BaseGenerator {
if (needs_imports) {
code += "import (\n";
if (!parser_.opts.go_import.empty()) {
code += "\tflatbuffers \"" + parser_.opts.go_import +"\"\n";
} else{
code += "\tflatbuffers \"" + parser_.opts.go_import + "\"\n";
} else {
code += "\tflatbuffers \"github.com/google/flatbuffers/go\"\n";
}
code += ")\n\n";
@@ -815,12 +785,12 @@ class GoGenerator : public BaseGenerator {
bool needs_imports) {
if (!classcode.length()) return true;
Namespace& ns = go_namespace_.components.empty() ? *def.defined_namespace : go_namespace_;
Namespace &ns = go_namespace_.components.empty() ? *def.defined_namespace
: go_namespace_;
std::string code = "";
BeginFile(LastNamespacePart(ns), needs_imports, &code);
code += classcode;
std::string filename =
NamespaceDir(ns) + def.name + ".go";
std::string filename = NamespaceDir(ns) + def.name + ".go";
return SaveFile(filename.c_str(), code, false);
}