Commit 7a637929 authored by Vladimir Glavnyy's avatar Vladimir Glavnyy Committed by Wouter van Oortmerssen

Remove unused variables (#5382)

- Fix GenerateTextFromTable (aliasing typo)
- Fix unused variable in idl_gen_dart.cpp
- Fix std::string passing (should be non-const value or const-reference)
- Remove unused variables
parent 7d7d796c
......@@ -82,8 +82,8 @@ class BaseGenerator {
protected:
BaseGenerator(const Parser &parser, const std::string &path,
const std::string &file_name,
const std::string qualifying_start,
const std::string qualifying_separator)
std::string qualifying_start,
std::string qualifying_separator)
: parser_(parser),
path_(path),
file_name_(file_name),
......
......@@ -790,7 +790,7 @@ class Parser : public ParserState {
StructDef *LookupStruct(const std::string &id) const;
std::string UnqualifiedName(std::string fullQualifiedName);
std::string UnqualifiedName(const std::string &fullQualifiedName);
FLATBUFFERS_CHECKED_ERROR Error(const std::string &msg);
......
......@@ -72,13 +72,13 @@ const char *BaseGenerator::FlatBuffersGeneratedWarning() {
std::string BaseGenerator::NamespaceDir(const Parser &parser,
const std::string &path,
const Namespace &ns) {
EnsureDirExists(path.c_str());
EnsureDirExists(path);
if (parser.opts.one_file) return path;
std::string namespace_dir = path; // Either empty or ends in separator.
auto &namespaces = ns.components;
for (auto it = namespaces.begin(); it != namespaces.end(); ++it) {
namespace_dir += *it + kPathSeparator;
EnsureDirExists(namespace_dir.c_str());
EnsureDirExists(namespace_dir);
}
return namespace_dir;
}
......
......@@ -520,7 +520,7 @@ class CppGenerator : public BaseGenerator {
static std::string TranslateNameSpace(const std::string &qualified_name) {
std::string cpp_qualified_name = qualified_name;
size_t start_pos = 0;
while ((start_pos = cpp_qualified_name.find(".", start_pos)) !=
while ((start_pos = cpp_qualified_name.find('.', start_pos)) !=
std::string::npos) {
cpp_qualified_name.replace(start_pos, 1, "::");
}
......
......@@ -105,10 +105,10 @@ class DartGenerator : public BaseGenerator {
static std::string ImportAliasName(const std::string &ns) {
std::string ret;
ret.assign(ns);
size_t pos = ret.find(".");
size_t pos = ret.find('.');
while (pos != std::string::npos) {
ret.replace(pos, 1, "_");
pos = ret.find(".", pos + 1);
pos = ret.find('.', pos + 1);
}
return ret;
......@@ -409,23 +409,23 @@ class DartGenerator : public BaseGenerator {
auto object_namespace = BuildNamespaceName(*struct_def.defined_namespace);
std::string code;
auto object_name = struct_def.name;
const auto &object_name = struct_def.name;
// Emit constructor
GenDocComment(struct_def.doc_comment, &code, "");
auto reader_name = "_" + struct_def.name + "Reader";
auto builder_name = struct_def.name + "Builder";
auto object_builder_name = struct_def.name + "ObjectBuilder";
auto reader_name = "_" + object_name + "Reader";
auto builder_name = object_name + "Builder";
auto object_builder_name = object_name + "ObjectBuilder";
std::string reader_code, builder_code;
code += "class " + struct_def.name + " {\n";
code += "class " + object_name + " {\n";
code += " " + struct_def.name + "._(this._bc, this._bcOffset);\n";
code += " " + object_name + "._(this._bc, this._bcOffset);\n";
if (!struct_def.fixed) {
code += " factory " + struct_def.name + "(List<int> bytes) {\n";
code += " factory " + object_name + "(List<int> bytes) {\n";
code += " " + _kFb + ".BufferContext rootRef = new " + _kFb +
".BufferContext.fromBytes(bytes);\n";
code += " return reader.read(rootRef, 0);\n";
......@@ -433,7 +433,7 @@ class DartGenerator : public BaseGenerator {
}
code += "\n";
code += " static const " + _kFb + ".Reader<" + struct_def.name +
code += " static const " + _kFb + ".Reader<" + object_name +
"> reader = const " + reader_name + "();\n\n";
code += " final " + _kFb + ".BufferContext _bc;\n";
......@@ -454,7 +454,7 @@ class DartGenerator : public BaseGenerator {
}
std::string NamespaceAliasFromUnionType(const std::string &in) {
if (in.find("_") == std::string::npos) { return in; }
if (in.find('_') == std::string::npos) { return in; }
std::stringstream ss(in);
std::string item;
......
......@@ -1611,7 +1611,7 @@ std::string GeneralMakeRule(const Parser &parser, const std::string &path,
for (auto it = parser.enums_.vec.begin(); it != parser.enums_.vec.end();
++it) {
auto &enum_def = **it;
if (make_rule != "") make_rule += " ";
if (!make_rule.empty()) make_rule += " ";
std::string directory =
BaseGenerator::NamespaceDir(parser, path, *enum_def.defined_namespace);
make_rule += directory + enum_def.name + lang.file_extension;
......@@ -1620,7 +1620,7 @@ std::string GeneralMakeRule(const Parser &parser, const std::string &path,
for (auto it = parser.structs_.vec.begin(); it != parser.structs_.vec.end();
++it) {
auto &struct_def = **it;
if (make_rule != "") make_rule += " ";
if (!make_rule.empty()) make_rule += " ";
std::string directory = BaseGenerator::NamespaceDir(
parser, path, *struct_def.defined_namespace);
make_rule += directory + struct_def.name + lang.file_extension;
......
......@@ -188,7 +188,7 @@ class GoGenerator : public BaseGenerator {
}
// A single enum name member.
void EnumNameMember(const EnumDef &enum_def, const EnumVal ev,
void EnumNameMember(const EnumDef &enum_def, const EnumVal &ev,
size_t max_name_length, std::string *code_ptr) {
std::string &code = *code_ptr;
code += "\t";
......@@ -228,7 +228,7 @@ class GoGenerator : public BaseGenerator {
}
// A single enum value member.
void EnumValueMember(const EnumDef &enum_def, const EnumVal ev,
void EnumValueMember(const EnumDef &enum_def, const EnumVal &ev,
size_t max_name_length, std::string *code_ptr) {
std::string &code = *code_ptr;
code += "\t\"";
......@@ -895,7 +895,7 @@ class GoGenerator : public BaseGenerator {
EndBuilderBody(code_ptr);
}
// Begin by declaring namespace and imports.
void BeginFile(const std::string name_space_name, const bool needs_imports,
void BeginFile(const std::string &name_space_name, const bool needs_imports,
const bool is_enum, std::string *code_ptr) {
std::string &code = *code_ptr;
code = code + "// Code generated by the FlatBuffers compiler. DO NOT EDIT.\n\n";
......
......@@ -149,7 +149,7 @@ class FlatBufPrinter : public grpc_generator::Printer {
}
void Print(const char *s) {
if (s == nullptr || std::strlen(s) == 0) { return; }
if (s == nullptr || *s == '\0') { return; }
// Add this string, but for each part separated by \n, add indentation.
for (;;) {
// Current indentation.
......
......@@ -214,7 +214,7 @@ class JsTsGenerator : public BaseGenerator {
std::string &code = *code_ptr;
std::string &exports = *exports_ptr;
for (auto it = sorted_namespaces.begin(); it != sorted_namespaces.end();
it++) {
++it) {
if (lang_.language == IDLOptions::kTs) {
if (it->find('.') == std::string::npos) {
code += "import { flatbuffers } from \"./flatbuffers\"\n";
......
......@@ -88,7 +88,7 @@ namespace lua {
}
// Begin enum code with a class declaration.
void BeginEnum(const std::string class_name, std::string *code_ptr) {
void BeginEnum(const std::string &class_name, std::string *code_ptr) {
std::string &code = *code_ptr;
code += "local " + class_name + " = {\n";
}
......@@ -683,7 +683,7 @@ namespace lua {
}
// Begin by declaring namespace and imports.
void BeginFile(const std::string name_space_name, const bool needs_imports,
void BeginFile(const std::string &name_space_name, const bool needs_imports,
std::string *code_ptr) {
std::string &code = *code_ptr;
code += std::string(Comment) + FlatBuffersGeneratedWarning() + "\n\n";
......
......@@ -232,7 +232,6 @@ class PhpGenerator : public BaseGenerator {
// Get the value of a table's scalar.
void GetScalarFieldOfTable(const FieldDef &field, std::string *code_ptr) {
std::string &code = *code_ptr;
std::string getter = GenGetter(field.value.type);
code += Indent + "/**\n";
code += Indent + " * @return " + GenTypeGet(field.value.type) + "\n";
......
......@@ -95,7 +95,7 @@ class PythonGenerator : public BaseGenerator {
}
// Begin enum code with a class declaration.
void BeginEnum(const std::string class_name, std::string *code_ptr) {
void BeginEnum(const std::string &class_name, std::string *code_ptr) {
std::string &code = *code_ptr;
code += "class " + class_name + "(object):\n";
}
......@@ -750,7 +750,7 @@ class PythonGenerator : public BaseGenerator {
}
// Begin by declaring namespace and imports.
void BeginFile(const std::string name_space_name, const bool needs_imports,
void BeginFile(const std::string &name_space_name, const bool needs_imports,
std::string *code_ptr) {
std::string &code = *code_ptr;
code = code + "# " + FlatBuffersGeneratedWarning() + "\n\n";
......
......@@ -449,7 +449,7 @@ class RustGenerator : public BaseGenerator {
std::stringstream stream;
stream << "::";
for (auto d = dst->components.begin(); d != dst->components.end(); d++) {
for (auto d = dst->components.begin(); d != dst->components.end(); ++d) {
stream << MakeSnakeCase(*d) + "::";
}
return stream.str();
......@@ -481,15 +481,15 @@ class RustGenerator : public BaseGenerator {
if (s == src->components.end()) { break; }
if (d == dst->components.end()) { break; }
if (*s != *d) { break; }
s++;
d++;
i++;
++s;
++d;
++i;
}
for (; s != src->components.end(); s++) {
for (; s != src->components.end(); ++s) {
stream << "super::";
}
for (; d != dst->components.end(); d++) {
for (; d != dst->components.end(); ++d) {
stream << MakeSnakeCase(*d) + "::";
}
return stream.str();
......@@ -758,7 +758,7 @@ class RustGenerator : public BaseGenerator {
// the vtable, or
// 3) return a hardcoded value because the vtable field value is set to zero.
std::string TableBuilderArgsDefnType(const FieldDef &field,
const std::string lifetime) {
const std::string &lifetime) {
const Type& type = field.value.type;
switch (GetFullType(type)) {
......@@ -786,7 +786,6 @@ class RustGenerator : public BaseGenerator {
return typname;
}
case ftUnionValue: {
const auto typname = WrapInNameSpace(*type.enum_def);
return "Option<flatbuffers::WIPOffset<flatbuffers::UnionWIPOffset>>";
}
......@@ -836,10 +835,11 @@ class RustGenerator : public BaseGenerator {
return GetDefaultScalarValue(field);
}
std::string TableBuilderAddFuncDefaultValue(const FieldDef &field) {
// All branches of switch do the same action!
switch (GetFullType(field.value.type)) {
case ftUnionKey:
case ftEnumKey: {
const std::string basetype = GetTypeBasic(field.value.type);
const std::string basetype = GetTypeBasic(field.value.type); //<- never used
return GetDefaultScalarValue(field);
}
......@@ -848,7 +848,7 @@ class RustGenerator : public BaseGenerator {
}
std::string TableBuilderArgsAddFuncType(const FieldDef &field,
const std::string lifetime) {
const std::string &lifetime) {
const Type& type = field.value.type;
switch (GetFullType(field.value.type)) {
......@@ -883,7 +883,6 @@ class RustGenerator : public BaseGenerator {
", " + typname + ">>";
}
case ftVectorOfUnionValue: {
const auto typname = WrapInNameSpace(*type.enum_def);
return "flatbuffers::WIPOffset<flatbuffers::Vector<" + lifetime + \
", flatbuffers::ForwardsUOffset<flatbuffers::Table<" + \
lifetime + ">>>";
......@@ -916,7 +915,6 @@ class RustGenerator : public BaseGenerator {
return typname;
}
case ftUnionValue: {
const auto typname = WrapInNameSpace(*type.enum_def);
return "flatbuffers::WIPOffset<flatbuffers::UnionWIPOffset>";
}
}
......@@ -970,7 +968,7 @@ class RustGenerator : public BaseGenerator {
}
std::string GenTableAccessorFuncReturnType(const FieldDef &field,
const std::string lifetime) {
const std::string &lifetime) {
const Type& type = field.value.type;
switch (GetFullType(field.value.type)) {
......@@ -1041,8 +1039,8 @@ class RustGenerator : public BaseGenerator {
}
std::string GenTableAccessorFuncBody(const FieldDef &field,
const std::string lifetime,
const std::string offset_prefix) {
const std::string &lifetime,
const std::string &offset_prefix) {
const std::string offset_name = offset_prefix + "::" + \
GetFieldOffsetName(field);
const Type& type = field.value.type;
......@@ -1072,7 +1070,7 @@ class RustGenerator : public BaseGenerator {
}
case ftUnionKey:
case ftEnumKey: {
const auto underlying_typname = GetTypeBasic(type);
const auto underlying_typname = GetTypeBasic(type); //<- never used
const auto typname = WrapInNameSpace(*type.enum_def);
const auto default_value = GetDefaultScalarValue(field);
return "self._tab.get::<" + typname + ">(" + offset_name + \
......@@ -1394,8 +1392,6 @@ class RustGenerator : public BaseGenerator {
const bool is_scalar = IsScalar(field.value.type.base_type);
std::string offset = GetFieldOffsetName(field);
std::string name = Name(field);
std::string value = GetDefaultScalarValue(field);
// Generate functions to add data, which take one of two forms.
//
......
......@@ -309,10 +309,10 @@ bool GenerateTextFromTable(const Parser &parser, const void *table,
if (struct_def == nullptr) {
return false;
}
auto text = *_text;
auto &text = *_text;
text.reserve(1024); // Reduce amount of inevitable reallocs.
auto root = static_cast<const Table *>(table);
if (!GenStruct(*struct_def, root, 0, parser.opts, _text)) {
if (!GenStruct(*struct_def, root, 0, parser.opts, &text)) {
return false;
}
text += NewLine(parser.opts);
......
......@@ -2099,7 +2099,7 @@ Namespace *Parser::UniqueNamespace(Namespace *ns) {
return ns;
}
std::string Parser::UnqualifiedName(std::string full_qualified_name) {
std::string Parser::UnqualifiedName(const std::string &full_qualified_name) {
Namespace *ns = new Namespace();
std::size_t current, previous = 0;
......@@ -3066,13 +3066,12 @@ bool StructDef::Deserialize(Parser &parser, const reflection::Object *object) {
name = parser.UnqualifiedName(object->name()->str());
predecl = false;
sortbysize = attributes.Lookup("original_order") == nullptr && !fixed;
std::vector<uoffset_t> indexes =
std::vector<uoffset_t>(object->fields()->size());
for (uoffset_t i = 0; i < object->fields()->size(); i++)
indexes[object->fields()->Get(i)->id()] = i;
const auto& of = *(object->fields());
auto indexes = std::vector<uoffset_t>(of.size());
for (uoffset_t i = 0; i < of.size(); i++) indexes[of.Get(i)->id()] = i;
size_t tmp_struct_size = 0;
for (size_t i = 0; i < indexes.size(); i++) {
auto field = object->fields()->Get(indexes[i]);
auto field = of.Get(indexes[i]);
auto field_def = new FieldDef();
if (!field_def->Deserialize(parser, field) ||
fields.Add(field_def->name, field_def)) {
......@@ -3084,7 +3083,7 @@ bool StructDef::Deserialize(Parser &parser, const reflection::Object *object) {
auto size = InlineSize(field_def->value.type);
auto next_field =
i + 1 < indexes.size()
? object->fields()->Get(indexes[i+1])
? of.Get(indexes[i+1])
: nullptr;
tmp_struct_size += size;
field_def->padding =
......
......@@ -127,12 +127,12 @@ static const char kPathSeparatorWindows = '\\';
static const char *PathSeparatorSet = "\\/"; // Intentionally no ':'
std::string StripExtension(const std::string &filepath) {
size_t i = filepath.find_last_of(".");
size_t i = filepath.find_last_of('.');
return i != std::string::npos ? filepath.substr(0, i) : filepath;
}
std::string GetExtension(const std::string &filepath) {
size_t i = filepath.find_last_of(".");
size_t i = filepath.find_last_of('.');
return i != std::string::npos ? filepath.substr(i + 1) : "";
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment