mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-06 05:27:24 +00:00
Use the Google Style for clang-format without exceptions (#8706)
This reduces the friction when merging from github and google repos by using the exact same clang style guide. MARKDOWN=true
This commit is contained in:
@@ -39,38 +39,38 @@ namespace {
|
||||
namespace r = ::reflection;
|
||||
|
||||
std::set<std::string> LuaKeywords() {
|
||||
return { "and", "break", "do", "else", "elseif", "end",
|
||||
"false", "for", "function", "goto", "if", "in",
|
||||
"local", "nil", "not", "or", "repeat", "return",
|
||||
"then", "true", "until", "while" };
|
||||
return {"and", "break", "do", "else", "elseif", "end",
|
||||
"false", "for", "function", "goto", "if", "in",
|
||||
"local", "nil", "not", "or", "repeat", "return",
|
||||
"then", "true", "until", "while"};
|
||||
}
|
||||
|
||||
Namer::Config LuaDefaultConfig() {
|
||||
return { /*types=*/Case::kUpperCamel,
|
||||
/*constants=*/Case::kUnknown,
|
||||
/*methods=*/Case::kUpperCamel,
|
||||
/*functions=*/Case::kUpperCamel,
|
||||
/*fields=*/Case::kUpperCamel,
|
||||
/*variables=*/Case::kLowerCamel,
|
||||
/*variants=*/Case::kKeep,
|
||||
/*enum_variant_seperator=*/"",
|
||||
/*escape_keywords=*/Namer::Config::Escape::AfterConvertingCase,
|
||||
/*namespaces=*/Case::kKeep,
|
||||
/*namespace_seperator=*/"__",
|
||||
/*object_prefix=*/"",
|
||||
/*object_suffix=*/"",
|
||||
/*keyword_prefix=*/"",
|
||||
/*keyword_suffix=*/"_",
|
||||
/*filenames=*/Case::kKeep,
|
||||
/*directories=*/Case::kKeep,
|
||||
/*output_path=*/"",
|
||||
/*filename_suffix=*/"",
|
||||
/*filename_extension=*/".lua" };
|
||||
return {/*types=*/Case::kUpperCamel,
|
||||
/*constants=*/Case::kUnknown,
|
||||
/*methods=*/Case::kUpperCamel,
|
||||
/*functions=*/Case::kUpperCamel,
|
||||
/*fields=*/Case::kUpperCamel,
|
||||
/*variables=*/Case::kLowerCamel,
|
||||
/*variants=*/Case::kKeep,
|
||||
/*enum_variant_seperator=*/"",
|
||||
/*escape_keywords=*/Namer::Config::Escape::AfterConvertingCase,
|
||||
/*namespaces=*/Case::kKeep,
|
||||
/*namespace_seperator=*/"__",
|
||||
/*object_prefix=*/"",
|
||||
/*object_suffix=*/"",
|
||||
/*keyword_prefix=*/"",
|
||||
/*keyword_suffix=*/"_",
|
||||
/*filenames=*/Case::kKeep,
|
||||
/*directories=*/Case::kKeep,
|
||||
/*output_path=*/"",
|
||||
/*filename_suffix=*/"",
|
||||
/*filename_extension=*/".lua"};
|
||||
}
|
||||
|
||||
class LuaBfbsGenerator : public BaseBfbsGenerator {
|
||||
public:
|
||||
explicit LuaBfbsGenerator(const std::string &flatc_version)
|
||||
explicit LuaBfbsGenerator(const std::string& flatc_version)
|
||||
: BaseBfbsGenerator(),
|
||||
keywords_(),
|
||||
requires_(),
|
||||
@@ -79,11 +79,13 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
|
||||
flatc_version_(flatc_version),
|
||||
namer_(LuaDefaultConfig(), LuaKeywords()) {}
|
||||
|
||||
Status GenerateFromSchema(const r::Schema *schema,
|
||||
const CodeGenOptions &options)
|
||||
Status GenerateFromSchema(const r::Schema* schema,
|
||||
const CodeGenOptions& options)
|
||||
FLATBUFFERS_OVERRIDE {
|
||||
options_ = options;
|
||||
if (!GenerateEnums(schema->enums())) { return ERROR; }
|
||||
if (!GenerateEnums(schema->enums())) {
|
||||
return ERROR;
|
||||
}
|
||||
if (!GenerateObjects(schema->objects(), schema->root_table())) {
|
||||
return ERROR;
|
||||
}
|
||||
@@ -92,14 +94,14 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
|
||||
|
||||
using BaseBfbsGenerator::GenerateCode;
|
||||
|
||||
Status GenerateCode(const Parser &, const std::string &,
|
||||
const std::string &) override {
|
||||
Status GenerateCode(const Parser&, const std::string&,
|
||||
const std::string&) override {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
Status GenerateMakeRule(const Parser &parser, const std::string &path,
|
||||
const std::string &filename,
|
||||
std::string &output) override {
|
||||
Status GenerateMakeRule(const Parser& parser, const std::string& path,
|
||||
const std::string& filename,
|
||||
std::string& output) override {
|
||||
(void)parser;
|
||||
(void)path;
|
||||
(void)filename;
|
||||
@@ -107,16 +109,16 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
Status GenerateGrpcCode(const Parser &parser, const std::string &path,
|
||||
const std::string &filename) override {
|
||||
Status GenerateGrpcCode(const Parser& parser, const std::string& path,
|
||||
const std::string& filename) override {
|
||||
(void)parser;
|
||||
(void)path;
|
||||
(void)filename;
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
Status GenerateRootFile(const Parser &parser,
|
||||
const std::string &path) override {
|
||||
Status GenerateRootFile(const Parser& parser,
|
||||
const std::string& path) override {
|
||||
(void)parser;
|
||||
(void)path;
|
||||
return Status::NOT_IMPLEMENTED;
|
||||
@@ -138,8 +140,8 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
|
||||
|
||||
protected:
|
||||
bool GenerateEnums(
|
||||
const flatbuffers::Vector<flatbuffers::Offset<r::Enum>> *enums) {
|
||||
ForAllEnums(enums, [&](const r::Enum *enum_def) {
|
||||
const flatbuffers::Vector<flatbuffers::Offset<r::Enum>>* enums) {
|
||||
ForAllEnums(enums, [&](const r::Enum* enum_def) {
|
||||
std::string code;
|
||||
|
||||
StartCodeBlock(enum_def);
|
||||
@@ -151,7 +153,7 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
|
||||
GenerateDocumentation(enum_def->documentation(), "", code);
|
||||
code += "local " + enum_name + " = {\n";
|
||||
|
||||
ForAllEnumValues(enum_def, [&](const reflection::EnumVal *enum_val) {
|
||||
ForAllEnumValues(enum_def, [&](const reflection::EnumVal* enum_val) {
|
||||
GenerateDocumentation(enum_val->documentation(), " ", code);
|
||||
code += " " + namer_.Variant(enum_val->name()->str()) + " = " +
|
||||
NumToString(enum_val->value()) + ",\n";
|
||||
@@ -165,9 +167,9 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
|
||||
}
|
||||
|
||||
bool GenerateObjects(
|
||||
const flatbuffers::Vector<flatbuffers::Offset<r::Object>> *objects,
|
||||
const r::Object *root_object) {
|
||||
ForAllObjects(objects, [&](const r::Object *object) {
|
||||
const flatbuffers::Vector<flatbuffers::Offset<r::Object>>* objects,
|
||||
const r::Object* root_object) {
|
||||
ForAllObjects(objects, [&](const r::Object* object) {
|
||||
std::string code;
|
||||
|
||||
StartCodeBlock(object);
|
||||
@@ -215,9 +217,11 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
|
||||
code += "\n";
|
||||
|
||||
// Create all the field accessors.
|
||||
ForAllFields(object, /*reverse=*/false, [&](const r::Field *field) {
|
||||
ForAllFields(object, /*reverse=*/false, [&](const r::Field* field) {
|
||||
// Skip writing deprecated fields altogether.
|
||||
if (field->deprecated()) { return; }
|
||||
if (field->deprecated()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const std::string field_name = namer_.Field(*field);
|
||||
const r::BaseType base_type = field->type()->base_type();
|
||||
@@ -247,7 +251,9 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
|
||||
|
||||
std::string getter =
|
||||
GenerateGetter(field->type()) + "self.view.pos + o)";
|
||||
if (IsBool(base_type)) { getter = "(" + getter + " ~=0)"; }
|
||||
if (IsBool(base_type)) {
|
||||
getter = "(" + getter + " ~=0)";
|
||||
}
|
||||
code += " return " + getter + "\n";
|
||||
code += " end\n";
|
||||
code += " return " + DefaultValue(field) + "\n";
|
||||
@@ -280,7 +286,7 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
|
||||
code += " " + offset_prefix;
|
||||
code += " " + offset_prefix_2;
|
||||
|
||||
const r::Object *field_object = GetObject(field->type());
|
||||
const r::Object* field_object = GetObject(field->type());
|
||||
if (!field_object) {
|
||||
// TODO(derekbailey): this is an error condition. we
|
||||
// should report it better.
|
||||
@@ -333,7 +339,7 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
|
||||
} else {
|
||||
// Vector of structs are inline, so we need to query the
|
||||
// size of the struct.
|
||||
const reflection::Object *obj =
|
||||
const reflection::Object* obj =
|
||||
GetObjectByIndex(field->type()->index());
|
||||
element_size = obj->bytesize();
|
||||
}
|
||||
@@ -408,8 +414,10 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
|
||||
code += "end\n";
|
||||
code += "\n";
|
||||
|
||||
ForAllFields(object, /*reverse=*/false, [&](const r::Field *field) {
|
||||
if (field->deprecated()) { return; }
|
||||
ForAllFields(object, /*reverse=*/false, [&](const r::Field* field) {
|
||||
if (field->deprecated()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const std::string field_name = namer_.Field(*field);
|
||||
const std::string variable_name = namer_.Variable(*field);
|
||||
@@ -455,21 +463,21 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
|
||||
|
||||
private:
|
||||
void GenerateDocumentation(
|
||||
const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>
|
||||
*documentation,
|
||||
std::string indent, std::string &code) const {
|
||||
const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>*
|
||||
documentation,
|
||||
std::string indent, std::string& code) const {
|
||||
flatbuffers::ForAllDocumentation(
|
||||
documentation, [&](const flatbuffers::String *str) {
|
||||
documentation, [&](const flatbuffers::String* str) {
|
||||
code += indent + "--" + str->str() + "\n";
|
||||
});
|
||||
}
|
||||
|
||||
std::string GenerateStructBuilderArgs(const r::Object *object,
|
||||
std::string GenerateStructBuilderArgs(const r::Object* object,
|
||||
std::string prefix = "") const {
|
||||
std::string signature;
|
||||
ForAllFields(object, /*reverse=*/false, [&](const r::Field *field) {
|
||||
ForAllFields(object, /*reverse=*/false, [&](const r::Field* field) {
|
||||
if (IsStructOrTable(field->type()->base_type())) {
|
||||
const r::Object *field_object = GetObject(field->type());
|
||||
const r::Object* field_object = GetObject(field->type());
|
||||
signature += GenerateStructBuilderArgs(
|
||||
field_object, prefix + namer_.Variable(*field) + "_");
|
||||
} else {
|
||||
@@ -479,7 +487,7 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
|
||||
return signature;
|
||||
}
|
||||
|
||||
std::string AppendStructBuilderBody(const r::Object *object,
|
||||
std::string AppendStructBuilderBody(const r::Object* object,
|
||||
std::string prefix = "") const {
|
||||
std::string code;
|
||||
code += " builder:Prep(" + NumToString(object->minalign()) + ", " +
|
||||
@@ -487,13 +495,13 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
|
||||
|
||||
// We need to reverse the order we iterate over, since we build the
|
||||
// buffer backwards.
|
||||
ForAllFields(object, /*reverse=*/true, [&](const r::Field *field) {
|
||||
ForAllFields(object, /*reverse=*/true, [&](const r::Field* field) {
|
||||
const int32_t num_padding_bytes = field->padding();
|
||||
if (num_padding_bytes) {
|
||||
code += " builder:Pad(" + NumToString(num_padding_bytes) + ")\n";
|
||||
}
|
||||
if (IsStructOrTable(field->type()->base_type())) {
|
||||
const r::Object *field_object = GetObject(field->type());
|
||||
const r::Object* field_object = GetObject(field->type());
|
||||
code += AppendStructBuilderBody(field_object,
|
||||
prefix + namer_.Variable(*field) + "_");
|
||||
} else {
|
||||
@@ -505,36 +513,49 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
|
||||
return code;
|
||||
}
|
||||
|
||||
std::string GenerateMethod(const r::Field *field) const {
|
||||
std::string GenerateMethod(const r::Field* field) const {
|
||||
const r::BaseType base_type = field->type()->base_type();
|
||||
if (IsScalar(base_type)) { return namer_.Type(GenerateType(base_type)); }
|
||||
if (IsStructOrTable(base_type)) { return "Struct"; }
|
||||
if (IsScalar(base_type)) {
|
||||
return namer_.Type(GenerateType(base_type));
|
||||
}
|
||||
if (IsStructOrTable(base_type)) {
|
||||
return "Struct";
|
||||
}
|
||||
return "UOffsetTRelative";
|
||||
}
|
||||
|
||||
std::string GenerateGetter(const r::Type *type,
|
||||
std::string GenerateGetter(const r::Type* type,
|
||||
bool element_type = false) const {
|
||||
switch (element_type ? type->element() : type->base_type()) {
|
||||
case r::String: return "self.view:String(";
|
||||
case r::Union: return "self.view:Union(";
|
||||
case r::Vector: return GenerateGetter(type, true);
|
||||
case r::String:
|
||||
return "self.view:String(";
|
||||
case r::Union:
|
||||
return "self.view:Union(";
|
||||
case r::Vector:
|
||||
return GenerateGetter(type, true);
|
||||
default:
|
||||
return "self.view:Get(flatbuffers.N." +
|
||||
namer_.Type(GenerateType(type, element_type)) + ", ";
|
||||
}
|
||||
}
|
||||
|
||||
std::string GenerateType(const r::Type *type,
|
||||
std::string GenerateType(const r::Type* type,
|
||||
bool element_type = false) const {
|
||||
const r::BaseType base_type =
|
||||
element_type ? type->element() : type->base_type();
|
||||
if (IsScalar(base_type)) { return GenerateType(base_type); }
|
||||
if (IsScalar(base_type)) {
|
||||
return GenerateType(base_type);
|
||||
}
|
||||
switch (base_type) {
|
||||
case r::String: return "string";
|
||||
case r::Vector: return GenerateGetter(type, true);
|
||||
case r::Obj: return namer_.Type(namer_.Denamespace(GetObject(type)));
|
||||
case r::String:
|
||||
return "string";
|
||||
case r::Vector:
|
||||
return GenerateGetter(type, true);
|
||||
case r::Obj:
|
||||
return namer_.Type(namer_.Denamespace(GetObject(type)));
|
||||
|
||||
default: return "*flatbuffers.Table";
|
||||
default:
|
||||
return "*flatbuffers.Table";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -542,23 +563,36 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
|
||||
// Need to override the default naming to match the Lua runtime libraries.
|
||||
// TODO(derekbailey): make overloads in the runtime libraries to avoid this.
|
||||
switch (base_type) {
|
||||
case r::None: return "uint8";
|
||||
case r::UType: return "uint8";
|
||||
case r::Byte: return "int8";
|
||||
case r::UByte: return "uint8";
|
||||
case r::Short: return "int16";
|
||||
case r::UShort: return "uint16";
|
||||
case r::Int: return "int32";
|
||||
case r::UInt: return "uint32";
|
||||
case r::Long: return "int64";
|
||||
case r::ULong: return "uint64";
|
||||
case r::Float: return "Float32";
|
||||
case r::Double: return "Float64";
|
||||
default: return r::EnumNameBaseType(base_type);
|
||||
case r::None:
|
||||
return "uint8";
|
||||
case r::UType:
|
||||
return "uint8";
|
||||
case r::Byte:
|
||||
return "int8";
|
||||
case r::UByte:
|
||||
return "uint8";
|
||||
case r::Short:
|
||||
return "int16";
|
||||
case r::UShort:
|
||||
return "uint16";
|
||||
case r::Int:
|
||||
return "int32";
|
||||
case r::UInt:
|
||||
return "uint32";
|
||||
case r::Long:
|
||||
return "int64";
|
||||
case r::ULong:
|
||||
return "uint64";
|
||||
case r::Float:
|
||||
return "Float32";
|
||||
case r::Double:
|
||||
return "Float64";
|
||||
default:
|
||||
return r::EnumNameBaseType(base_type);
|
||||
}
|
||||
}
|
||||
|
||||
std::string DefaultValue(const r::Field *field) const {
|
||||
std::string DefaultValue(const r::Field* field) const {
|
||||
const r::BaseType base_type = field->type()->base_type();
|
||||
if (IsFloatingPoint(base_type)) {
|
||||
return NumToString(field->default_real());
|
||||
@@ -566,24 +600,26 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
|
||||
if (IsBool(base_type)) {
|
||||
return field->default_integer() ? "true" : "false";
|
||||
}
|
||||
if (IsScalar(base_type)) { return NumToString((field->default_integer())); }
|
||||
if (IsScalar(base_type)) {
|
||||
return NumToString((field->default_integer()));
|
||||
}
|
||||
// represents offsets
|
||||
return "0";
|
||||
}
|
||||
|
||||
void StartCodeBlock(const reflection::Enum *enum_def) {
|
||||
void StartCodeBlock(const reflection::Enum* enum_def) {
|
||||
current_enum_ = enum_def;
|
||||
current_obj_ = nullptr;
|
||||
requires_.clear();
|
||||
}
|
||||
|
||||
void StartCodeBlock(const reflection::Object *object) {
|
||||
void StartCodeBlock(const reflection::Object* object) {
|
||||
current_obj_ = object;
|
||||
current_enum_ = nullptr;
|
||||
requires_.clear();
|
||||
}
|
||||
|
||||
std::string RegisterRequires(const r::Field *field,
|
||||
std::string RegisterRequires(const r::Field* field,
|
||||
bool use_element = false) {
|
||||
std::string type_name;
|
||||
|
||||
@@ -591,12 +627,16 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
|
||||
use_element ? field->type()->element() : field->type()->base_type();
|
||||
|
||||
if (IsStructOrTable(type)) {
|
||||
const r::Object *object = GetObjectByIndex(field->type()->index());
|
||||
if (object == current_obj_) { return namer_.Denamespace(object); }
|
||||
const r::Object* object = GetObjectByIndex(field->type()->index());
|
||||
if (object == current_obj_) {
|
||||
return namer_.Denamespace(object);
|
||||
}
|
||||
type_name = object->name()->str();
|
||||
} else {
|
||||
const r::Enum *enum_def = GetEnumByIndex(field->type()->index());
|
||||
if (enum_def == current_enum_) { return namer_.Denamespace(enum_def); }
|
||||
const r::Enum* enum_def = GetEnumByIndex(field->type()->index());
|
||||
if (enum_def == current_enum_) {
|
||||
return namer_.Denamespace(enum_def);
|
||||
}
|
||||
type_name = enum_def->name()->str();
|
||||
}
|
||||
|
||||
@@ -609,15 +649,15 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
|
||||
return RegisterRequires(name, type_name);
|
||||
}
|
||||
|
||||
std::string RegisterRequires(const std::string &local_name,
|
||||
const std::string &requires_name) {
|
||||
std::string RegisterRequires(const std::string& local_name,
|
||||
const std::string& requires_name) {
|
||||
requires_[local_name] = requires_name;
|
||||
return local_name;
|
||||
}
|
||||
|
||||
void EmitCodeBlock(const std::string &code_block, const std::string &name,
|
||||
const std::string &ns,
|
||||
const std::string &declaring_file) const {
|
||||
void EmitCodeBlock(const std::string& code_block, const std::string& name,
|
||||
const std::string& ns,
|
||||
const std::string& declaring_file) const {
|
||||
const std::string root_type = schema_->root_table()->name()->str();
|
||||
const std::string root_file =
|
||||
schema_->root_table()->declaration_file()->str();
|
||||
@@ -665,15 +705,15 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
|
||||
std::map<std::string, std::string> requires_;
|
||||
CodeGenOptions options_;
|
||||
|
||||
const r::Object *current_obj_;
|
||||
const r::Enum *current_enum_;
|
||||
const r::Object* current_obj_;
|
||||
const r::Enum* current_enum_;
|
||||
const std::string flatc_version_;
|
||||
const BfbsNamer namer_;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
std::unique_ptr<CodeGenerator> NewLuaBfbsGenerator(
|
||||
const std::string &flatc_version) {
|
||||
const std::string& flatc_version) {
|
||||
return std::unique_ptr<LuaBfbsGenerator>(new LuaBfbsGenerator(flatc_version));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user