From b513db86c7d185975782315d2c67525466ae8ebc Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Thu, 8 Feb 2018 14:56:16 -0800 Subject: [PATCH] Misc fixes from internal integration / clang tidy. Change-Id: Ic5e8f6a423b426abb9f8b90d39db0f85f28b94be Tested: on Linux. --- grpc/src/compiler/java_generator.cc | 3 ++- include/flatbuffers/util.h | 4 ++++ src/idl_gen_grpc.cpp | 9 +-------- src/idl_gen_php.cpp | 14 +++++++------- src/idl_parser.cpp | 9 +++++---- src/reflection.cpp | 2 +- tests/test.cpp | 3 +-- 7 files changed, 21 insertions(+), 23 deletions(-) diff --git a/grpc/src/compiler/java_generator.cc b/grpc/src/compiler/java_generator.cc index b22d41937..93d3108f5 100644 --- a/grpc/src/compiler/java_generator.cc +++ b/grpc/src/compiler/java_generator.cc @@ -49,6 +49,7 @@ typedef grpc_generator::CommentHolder typedef grpc_generator::Method MethodDescriptor; namespace grpc_java_generator { +typedef std::string string; // Generates imports for the service void GenerateImports(grpc_generator::File* file, grpc_generator::Printer* printer, VARS& vars) { @@ -60,7 +61,7 @@ void GenerateImports(grpc_generator::File* file, printer->Print(vars, "//source: $filename$.fbs\n\n"); printer->Print(vars, "package $Package$;\n\n"); vars["Package"] = vars["Package"] + "."; - if (file->additional_headers() != "") { + if (!file->additional_headers().empty()) { printer->Print(file->additional_headers().c_str()); printer->Print("\n\n"); } diff --git a/include/flatbuffers/util.h b/include/flatbuffers/util.h index 27a965325..e3178e30a 100644 --- a/include/flatbuffers/util.h +++ b/include/flatbuffers/util.h @@ -231,6 +231,10 @@ inline std::string ConCatPathFileName(const std::string &path, } } filepath += filename; + // Ignore './' at the start of filepath. + if (filepath[0] == '.' && filepath[1] == kPathSeparator) { + filepath.erase(0, 2); + } return filepath; } diff --git a/src/idl_gen_grpc.cpp b/src/idl_gen_grpc.cpp index 5f9f35e44..89cf4300e 100644 --- a/src/idl_gen_grpc.cpp +++ b/src/idl_gen_grpc.cpp @@ -314,10 +314,7 @@ class JavaGRPCGenerator : public flatbuffers::BaseGenerator { public: JavaGRPCGenerator(const Parser &parser, const std::string &path, const std::string &file_name) - : BaseGenerator(parser, path, file_name, "", "." /*separator*/), - parser_(parser), - path_(path), - file_name_(file_name) {} + : BaseGenerator(parser, path, file_name, "", "." /*separator*/) {} bool generate() { FlatBufFile file(parser_, file_name_, FlatBufFile::kLanguageJava); @@ -335,10 +332,6 @@ class JavaGRPCGenerator : public flatbuffers::BaseGenerator { } return true; } - - protected: - const Parser &parser_; - const std::string &path_, &file_name_; }; bool GenerateJavaGRPC(const Parser &parser, const std::string &path, diff --git a/src/idl_gen_php.cpp b/src/idl_gen_php.cpp index 08250e98c..56429cec3 100644 --- a/src/idl_gen_php.cpp +++ b/src/idl_gen_php.cpp @@ -33,13 +33,13 @@ class PhpGenerator : public BaseGenerator { const std::string &file_name) : BaseGenerator(parser, path, file_name, "\\", "\\"){}; bool generate() { - if (!generateEnums()) return false; - if (!generateStructs()) return false; + if (!GenerateEnums()) return false; + if (!GenerateStructs()) return false; return true; } private: - bool generateEnums() { + bool GenerateEnums() { for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end(); ++it) { auto &enum_def = **it; @@ -50,7 +50,7 @@ class PhpGenerator : public BaseGenerator { return true; } - bool generateStructs() { + bool GenerateStructs() { for (auto it = parser_.structs_.vec.begin(); it != parser_.structs_.vec.end(); ++it) { auto &struct_def = **it; @@ -62,9 +62,9 @@ class PhpGenerator : 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; + auto &code = *code_ptr; code += ">(const char *s, Parser &parser, std::string Namespace::GetFullyQualifiedName(const std::string &name, size_t max_components) const { // Early exit if we don't have a defined namespace. - if (components.size() == 0 || !max_components) { return name; } + if (components.empty() || !max_components) { return name; } std::stringstream stream; for (size_t i = 0; i < std::min(components.size(), max_components); i++) { if (i) { stream << "."; } @@ -648,10 +648,11 @@ CheckedError Parser::ParseField(StructDef &struct_def) { if (token_ == '=') { NEXT(); - if (!IsScalar(type.base_type) || struct_def.fixed) + ECHECK(ParseSingleValue(field->value)); + if (!IsScalar(type.base_type) || + (struct_def.fixed && field->value.constant != "0")) return Error( "default values currently only supported for scalars in tables"); - ECHECK(ParseSingleValue(field->value)); } if (type.enum_def && !type.enum_def->is_union && @@ -1492,7 +1493,7 @@ CheckedError Parser::ParseEnum(bool is_union, EnumDef **dest) { } auto prevsize = enum_def.vals.vec.size(); auto value = - enum_def.vals.vec.size() ? enum_def.vals.vec.back()->value + 1 : 0; + !enum_def.vals.vec.empty() ? enum_def.vals.vec.back()->value + 1 : 0; auto &ev = *new EnumVal(value_name, value); if (enum_def.vals.Add(value_name, &ev)) return Error("enum value already exists: " + value_name); diff --git a/src/reflection.cpp b/src/reflection.cpp index d5bcef1b7..7ffefe7a5 100644 --- a/src/reflection.cpp +++ b/src/reflection.cpp @@ -513,7 +513,7 @@ bool VerifyVectorOfStructs(flatbuffers::Verifier &v, // forward declare to resolve cyclic deps between VerifyObject and VerifyVector bool VerifyObject(flatbuffers::Verifier &v, const reflection::Schema &schema, const reflection::Object &obj, - const flatbuffers::Table *table, bool isRequired); + const flatbuffers::Table *table, bool required); bool VerifyVector(flatbuffers::Verifier &v, const reflection::Schema &schema, const flatbuffers::Table &table, diff --git a/tests/test.cpp b/tests/test.cpp index 1102273fa..0dea810c9 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -1179,7 +1179,7 @@ void ErrorTest() { TestError("table X { Y:int; Y:int; }", "field already"); TestError("table Y {} table X { Y:int; }", "same as table"); TestError("struct X { Y:string; }", "only scalar"); - TestError("table X { Y:string = 1; }", "default values"); + TestError("table X { Y:string = \"\"; }", "default values"); TestError("enum Y:byte { Z = 1 } table X { y:Y; }", "not part of enum"); TestError("struct X { Y:int (deprecated); }", "deprecate"); TestError("union Z { X } table X { Y:Z; } root_type X; { Y: {}, A:1 }", @@ -1212,7 +1212,6 @@ void ErrorTest() { TestError("union X { Y }", "referenced"); TestError("union Z { X } struct X { Y:int; }", "only tables"); TestError("table X { Y:[int]; YLength:int; }", "clash"); - TestError("table X { Y:string = 1; }", "scalar"); TestError("table X { Y:byte; } root_type X; { Y:1, Y:2 }", "more than once"); }