Merge pull request #3915 from Lakedaemon/clangFormat

clang formating header and code generators for cpp, general, js, php
This commit is contained in:
Wouter van Oortmerssen 2016-07-18 14:12:03 -07:00 committed by GitHub
commit 410fb15a07
16 changed files with 62 additions and 75 deletions

View File

@ -86,7 +86,10 @@ class BaseGenerator {
const std::string LastNamespacePart(const Namespace &ns) {
auto &namespaces = ns.components;
if (namespaces.size()) return *(namespaces.end() - 1); else return std::string("");
if (namespaces.size())
return *(namespaces.end() - 1);
else
return std::string("");
}
// tracks the current namespace for early exit in WrapInNameSpace

View File

@ -24,9 +24,7 @@
namespace flatbuffers {
struct IsAlnum {
bool operator()(char c) {
return !isalnum(c);
}
bool operator()(char c) { return !isalnum(c); }
};
static std::string GeneratedFileName(const std::string &path,
@ -184,7 +182,6 @@ class CppGenerator : public BaseGenerator {
code += "nullptr";
code += "); }\n\n";
if (parser_.file_extension_.length()) {
// Return the extension
code += "inline const char *" + name;
@ -264,30 +261,28 @@ class CppGenerator : public BaseGenerator {
// Return a C++ type for any type (scalar/pointer) specifically for
// building a flatbuffer.
std::string GenTypeWire(const Type &type,
const char *postfix, bool user_facing_type) {
std::string GenTypeWire(const Type &type, const char *postfix,
bool user_facing_type) {
return IsScalar(type.base_type)
? GenTypeBasic(type, user_facing_type) + postfix
: IsStruct(type)
? "const " + GenTypePointer(type) + " *"
: "flatbuffers::Offset<" + GenTypePointer(type) +
">" + postfix;
: IsStruct(type) ? "const " + GenTypePointer(type) + " *"
: "flatbuffers::Offset<" +
GenTypePointer(type) + ">" + postfix;
}
// Return a C++ type for any type (scalar/pointer) that reflects its
// serialized size.
std::string GenTypeSize(const Type &type) {
return IsScalar(type.base_type)
? GenTypeBasic(type, false)
return IsScalar(type.base_type) ? GenTypeBasic(type, false)
: IsStruct(type) ? GenTypePointer(type)
: "flatbuffers::uoffset_t";
}
// Return a C++ type for any type (scalar/pointer) specifically for
// using a flatbuffer.
std::string GenTypeGet(const Type &type,
const char *afterbasic, const char *beforeptr,
const char *afterptr, bool user_facing_type) {
std::string GenTypeGet(const Type &type, const char *afterbasic,
const char *beforeptr, const char *afterptr,
bool user_facing_type) {
return IsScalar(type.base_type)
? GenTypeBasic(type, user_facing_type) + afterbasic
: beforeptr + GenTypePointer(type) + afterptr;
@ -388,8 +383,8 @@ class CppGenerator : public BaseGenerator {
code += "()[static_cast<int>(e)";
if (enum_def.vals.vec.front()->value) {
code += " - static_cast<int>(";
code +=
GetEnumVal(enum_def, *enum_def.vals.vec.front(), parser_.opts) + ")";
code += GetEnumVal(enum_def, *enum_def.vals.vec.front(), parser_.opts) +
")";
}
code += "]; }\n\n";
}
@ -446,8 +441,7 @@ class CppGenerator : public BaseGenerator {
return "VT_" + uname;
}
void GenFullyQualifiedNameGetter(const std::string &name,
std::string &code) {
void GenFullyQualifiedNameGetter(const std::string &name, std::string &code) {
if (parser_.opts.generate_name_strings) {
code +=
" static FLATBUFFERS_CONSTEXPR const char *GetFullyQualifiedName() "
@ -469,21 +463,17 @@ class CppGenerator : public BaseGenerator {
code += field.name + " = ";
if (field.value.type.enum_def && IsScalar(field.value.type.base_type)) {
auto ev = field.value.type.enum_def->ReverseLookup(
static_cast<int>(StringToInt(field.value.constant.c_str())),
false);
static_cast<int>(StringToInt(field.value.constant.c_str())), false);
if (ev) {
code += WrapInNameSpace(
field.value.type.enum_def->defined_namespace,
GetEnumVal(*field.value.type.enum_def, *ev, parser_.opts));
}
else {
} else {
code += GenUnderlyingCast(field, true, field.value.constant);
}
}
else if (field.value.type.base_type == BASE_TYPE_BOOL) {
} else if (field.value.type.base_type == BASE_TYPE_BOOL) {
code += field.value.constant == "0" ? "false" : "true";
}
else {
} else {
code += GenDefaultConstant(field);
}
}
@ -529,8 +519,7 @@ class CppGenerator : public BaseGenerator {
if (!field.deprecated) { // Deprecated fields won't be accessible.
auto is_scalar = IsScalar(field.value.type.base_type);
GenComment(field.doc_comment, code_ptr, nullptr, " ");
code += " " +
GenTypeGet(field.value.type, " ", "const ", " *", true);
code += " " + GenTypeGet(field.value.type, " ", "const ", " *", true);
code += field.name + "() const { return ";
// Call a different accessor for pointers, that indirects.
auto accessor =
@ -538,8 +527,8 @@ class CppGenerator : public BaseGenerator {
? "GetField<"
: (IsStruct(field.value.type) ? "GetStruct<" : "GetPointer<");
auto offsetstr = GenFieldOffsetName(field);
auto call = accessor + GenTypeGet(field.value.type, "",
"const ", " *", false) +
auto call = accessor +
GenTypeGet(field.value.type, "", "const ", " *", false) +
">(" + offsetstr;
// Default value as second arg for non-pointer types.
if (IsScalar(field.value.type.base_type))
@ -556,8 +545,7 @@ class CppGenerator : public BaseGenerator {
code += GenUnderlyingCast(field, false, "_" + field.name);
code += "); }\n";
} else {
auto type =
GenTypeGet(field.value.type, " ", "", " *", true);
auto type = GenTypeGet(field.value.type, " ", "", " *", true);
code += " " + type + "mutable_" + field.name + "() { return ";
code += GenUnderlyingCast(field, true,
accessor + type + ">(" + offsetstr + ")");
@ -739,7 +727,7 @@ class CppGenerator : public BaseGenerator {
}
code += " return builder_.Finish();\n}\n\n";
//Generate a CreateX function with vector types as parameters
// Generate a CreateX function with vector types as parameters
if (gen_vector_pars) {
code += "inline flatbuffers::Offset<" + struct_def.name + "> Create";
code += struct_def.name;
@ -751,8 +739,7 @@ class CppGenerator : public BaseGenerator {
if (field.value.type.base_type == BASE_TYPE_STRING) {
code += ",\n const char *";
code += field.name + " = nullptr";
}
else if (field.value.type.base_type == BASE_TYPE_VECTOR) {
} else if (field.value.type.base_type == BASE_TYPE_VECTOR) {
code += ",\n const std::vector<";
code += GenTypeWire(field.value.type.VectorType(), "", false);
code += "> *" + field.name + " = nullptr";
@ -777,7 +764,8 @@ class CppGenerator : public BaseGenerator {
code += "_fbb.CreateVector<";
code += GenTypeWire(field.value.type.VectorType(), "", false);
code += ">(*" + field.name + ")";
} else code += ", " + field.name;
} else
code += ", " + field.name;
}
}
code += ");\n}\n\n";
@ -880,8 +868,7 @@ class CppGenerator : public BaseGenerator {
auto &field = **it;
GenComment(field.doc_comment, code_ptr, nullptr, " ");
auto is_scalar = IsScalar(field.value.type.base_type);
code += " " +
GenTypeGet(field.value.type, " ", "const ", " &", true);
code += " " + GenTypeGet(field.value.type, " ", "const ", " &", true);
code += field.name + "() const { return ";
code += GenUnderlyingCast(
field, true, is_scalar
@ -949,19 +936,16 @@ bool GenerateCPP(const Parser &parser, const std::string &path,
return generator.generate();
}
std::string CPPMakeRule(const Parser &parser,
const std::string &path,
std::string CPPMakeRule(const Parser &parser, const std::string &path,
const std::string &file_name) {
std::string filebase = flatbuffers::StripPath(
flatbuffers::StripExtension(file_name));
std::string filebase =
flatbuffers::StripPath(flatbuffers::StripExtension(file_name));
std::string make_rule = GeneratedFileName(path, filebase) + ": ";
auto included_files = parser.GetIncludedFilesRecursive(file_name);
for (auto it = included_files.begin();
it != included_files.end(); ++it) {
for (auto it = included_files.begin(); it != included_files.end(); ++it) {
make_rule += " " + *it;
}
return make_rule;
}
} // namespace flatbuffers

View File

@ -9,8 +9,8 @@ public final class Any {
public static final byte TestSimpleTableWithEnum = 2;
public static final byte MyGame_Example2_Monster = 3;
private static final String[] names = { "NONE", "Monster", "TestSimpleTableWithEnum", "MyGame_Example2_Monster", };
public static final String[] names = { "NONE", "Monster", "TestSimpleTableWithEnum", "MyGame_Example2_Monster", };
public static String name(int e) { return names[e]; }
};
}

View File

@ -8,8 +8,8 @@ public final class Color {
public static final byte Green = 2;
public static final byte Blue = 8;
private static final String[] names = { "Red", "Green", "", "", "", "", "", "Blue", };
public static final String[] names = { "Red", "Green", "", "", "", "", "", "Blue", };
public static String name(int e) { return names[e - Red]; }
};
}

View File

@ -135,5 +135,5 @@ public final class Monster extends Table {
return o;
}
public static void finishMonsterBuffer(FlatBufferBuilder builder, int offset) { builder.finish(offset, "MONS"); }
};
}

View File

@ -39,5 +39,5 @@ public final class Stat extends Table {
int o = builder.endObject();
return o;
}
};
}

View File

@ -23,5 +23,5 @@ public final class Test extends Struct {
builder.putShort(a);
return builder.offset();
}
};
}

View File

@ -29,5 +29,5 @@ public final class TestSimpleTableWithEnum extends Table {
int o = builder.endObject();
return o;
}
};
}

View File

@ -40,5 +40,5 @@ public final class Vec3 extends Struct {
builder.putFloat(x);
return builder.offset();
}
};
}

View File

@ -19,5 +19,5 @@ public final class Monster extends Table {
int o = builder.endObject();
return o;
}
};
}

View File

@ -8,8 +8,8 @@ public final class EnumInNestedNS {
public static final byte B = 1;
public static final byte C = 2;
private static final String[] names = { "A", "B", "C", };
public static final String[] names = { "A", "B", "C", };
public static String name(int e) { return names[e]; }
};
}

View File

@ -22,5 +22,5 @@ public final class StructInNestedNS extends Struct {
builder.putInt(a);
return builder.offset();
}
};
}

View File

@ -29,5 +29,5 @@ public final class TableInNestedNS extends Table {
int o = builder.endObject();
return o;
}
};
}

View File

@ -29,5 +29,5 @@ public final class SecondTableInA extends Table {
int o = builder.endObject();
return o;
}
};
}

View File

@ -28,5 +28,5 @@ public final class TableInFirstNS extends Table {
int o = builder.endObject();
return o;
}
};
}

View File

@ -34,5 +34,5 @@ public final class TableInC extends Table {
int o = builder.endObject();
return o;
}
};
}