diff options
author | Derek Bailey <dbaileychess@gmail.com> | 2019-11-14 15:47:58 -0800 |
---|---|---|
committer | Wouter van Oortmerssen <aardappel@gmail.com> | 2019-11-14 15:47:58 -0800 |
commit | 80988ea869f7bbd5aff9283d49a03e297139ffa2 (patch) | |
tree | f98ce78a1b9c6505dc926af2bd149a8dce5320ad | |
parent | 0f2ff7eaa96b58adbfcf58cb08e123c943f1ff01 (diff) | |
download | flatbuffers-80988ea869f7bbd5aff9283d49a03e297139ffa2.tar.gz flatbuffers-80988ea869f7bbd5aff9283d49a03e297139ffa2.tar.bz2 flatbuffers-80988ea869f7bbd5aff9283d49a03e297139ffa2.zip |
Removed idl_gen_general.cpp and move contents to code_generators.cpp (#5625)
-rw-r--r-- | BUILD | 3 | ||||
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | android/jni/Android.mk | 3 | ||||
-rw-r--r-- | include/flatbuffers/idl.h | 19 | ||||
-rw-r--r-- | src/code_generators.cpp | 74 | ||||
-rw-r--r-- | src/flatc.cpp | 15 | ||||
-rw-r--r-- | src/flatc_main.cpp | 22 | ||||
-rw-r--r-- | src/idl_gen_general.cpp | 102 | ||||
-rw-r--r-- | src/idl_gen_java.cpp | 9 |
9 files changed, 105 insertions, 144 deletions
@@ -22,7 +22,6 @@ cc_library( srcs = [ "src/code_generators.cpp", "src/idl_gen_fbs.cpp", - "src/idl_gen_general.cpp", "src/idl_gen_text.cpp", "src/idl_parser.cpp", "src/reflection.cpp", @@ -88,7 +87,6 @@ cc_binary( "src/idl_gen_cpp.cpp", "src/idl_gen_csharp.cpp", "src/idl_gen_dart.cpp", - "src/idl_gen_general.cpp", "src/idl_gen_go.cpp", "src/idl_gen_grpc.cpp", "src/idl_gen_java.cpp", @@ -134,7 +132,6 @@ cc_test( "include/flatbuffers/registry.h", "src/code_generators.cpp", "src/idl_gen_fbs.cpp", - "src/idl_gen_general.cpp", "src/idl_gen_text.cpp", "src/idl_parser.cpp", "src/reflection.cpp", diff --git a/CMakeLists.txt b/CMakeLists.txt index 60ebe260..a0f292ed 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -83,7 +83,6 @@ set(FlatBuffers_Compiler_SRCS src/idl_gen_cpp.cpp src/idl_gen_csharp.cpp src/idl_gen_dart.cpp - src/idl_gen_general.cpp src/idl_gen_kotlin.cpp src/idl_gen_go.cpp src/idl_gen_java.cpp @@ -148,7 +147,6 @@ set(FlatBuffers_Sample_Text_SRCS set(FlatBuffers_Sample_BFBS_SRCS ${FlatBuffers_Library_SRCS} - src/idl_gen_general.cpp samples/sample_bfbs.cpp # file generated by running compiler on samples/monster.fbs ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h diff --git a/android/jni/Android.mk b/android/jni/Android.mk index e29c8728..5795a70f 100644 --- a/android/jni/Android.mk +++ b/android/jni/Android.mk @@ -53,8 +53,7 @@ LOCAL_SRC_FILES := android/jni/main.cpp \ tests/test_builder.cpp \ tests/native_type_test_impl.h \ tests/native_type_test_impl.cpp \ - src/idl_gen_fbs.cpp \ - src/idl_gen_general.cpp + src/idl_gen_fbs.cpp LOCAL_LDLIBS := -llog -landroid -latomic LOCAL_STATIC_LIBRARIES := android_native_app_glue flatbuffers_extra LOCAL_ARM_MODE := arm diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index 005134cc..4d567dc9 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -967,7 +967,7 @@ extern bool GenerateTextFile(const Parser &parser, const std::string &path, // Generate binary files from a given FlatBuffer, and a given Parser // object that has been populated with the corresponding schema. -// See idl_gen_general.cpp. +// See code_generators.cpp. extern bool GenerateBinary(const Parser &parser, const std::string &path, const std::string &file_name); @@ -1032,11 +1032,6 @@ extern bool GenerateJsonSchema(const Parser &parser, const std::string &path, extern bool GenerateKotlin(const Parser &parser, const std::string &path, const std::string &file_name); -// Generate Java/C#/.. files from the definitions in the Parser object. -// See idl_gen_general.cpp. -extern bool GenerateGeneral(const Parser &parser, const std::string &path, - const std::string &file_name); - // Generate a schema file from the internal representation, useful after // parsing a .proto schema. extern std::string GenerateFBS(const Parser &parser, @@ -1064,11 +1059,11 @@ extern std::string DartMakeRule(const Parser &parser, const std::string &path, extern std::string RustMakeRule(const Parser &parser, const std::string &path, const std::string &file_name); -// Generate a make rule for the generated Java/C#/... files. -// See idl_gen_general.cpp. -extern std::string GeneralMakeRule(const Parser &parser, - const std::string &path, - const std::string &file_name); +// Generate a make rule for generated Java or C# files. +// See code_generators.cpp. +extern std::string JavaCSharpMakeRule(const Parser &parser, + const std::string &path, + const std::string &file_name); // Generate a make rule for the generated text (JSON) files. // See idl_gen_text.cpp. @@ -1076,7 +1071,7 @@ extern std::string TextMakeRule(const Parser &parser, const std::string &path, const std::string &file_names); // Generate a make rule for the generated binary files. -// See idl_gen_general.cpp. +// See code_generators.cpp. extern std::string BinaryMakeRule(const Parser &parser, const std::string &path, const std::string &file_name); diff --git a/src/code_generators.cpp b/src/code_generators.cpp index a46c4b73..ff4d6149 100644 --- a/src/code_generators.cpp +++ b/src/code_generators.cpp @@ -287,6 +287,80 @@ std::string SimpleFloatConstantGenerator::NaN(float v) const { return this->NaN(static_cast<double>(v)); } +std::string JavaCSharpMakeRule(const Parser &parser, const std::string &path, + const std::string &file_name) { + FLATBUFFERS_ASSERT(parser.opts.lang == IDLOptions::kJava || + parser.opts.lang == IDLOptions::kCSharp); + + std::string file_extension = + (parser.opts.lang == IDLOptions::kJava) ? ".java" : ".cs"; + + std::string make_rule; + + for (auto it = parser.enums_.vec.begin(); it != parser.enums_.vec.end(); + ++it) { + auto &enum_def = **it; + if (!make_rule.empty()) make_rule += " "; + std::string directory = + BaseGenerator::NamespaceDir(parser, path, *enum_def.defined_namespace); + make_rule += directory + enum_def.name + file_extension; + } + + for (auto it = parser.structs_.vec.begin(); it != parser.structs_.vec.end(); + ++it) { + auto &struct_def = **it; + if (!make_rule.empty()) make_rule += " "; + std::string directory = BaseGenerator::NamespaceDir( + parser, path, *struct_def.defined_namespace); + make_rule += directory + struct_def.name + file_extension; + } + + make_rule += ": "; + auto included_files = parser.GetIncludedFilesRecursive(file_name); + for (auto it = included_files.begin(); it != included_files.end(); ++it) { + make_rule += " " + *it; + } + return make_rule; +} + +std::string BinaryFileName(const Parser &parser, const std::string &path, + const std::string &file_name) { + auto ext = parser.file_extension_.length() ? parser.file_extension_ : "bin"; + return path + file_name + "." + ext; +} + +bool GenerateBinary(const Parser &parser, const std::string &path, + const std::string &file_name) { + if (parser.opts.use_flexbuffers) { + auto data_vec = parser.flex_builder_.GetBuffer(); + auto data_ptr = reinterpret_cast<char *>(data(data_vec)); + return !parser.flex_builder_.GetSize() || + flatbuffers::SaveFile( + BinaryFileName(parser, path, file_name).c_str(), data_ptr, + parser.flex_builder_.GetSize(), true); + } + return !parser.builder_.GetSize() || + flatbuffers::SaveFile( + BinaryFileName(parser, path, file_name).c_str(), + reinterpret_cast<char *>(parser.builder_.GetBufferPointer()), + parser.builder_.GetSize(), true); +} + +std::string BinaryMakeRule(const Parser &parser, const std::string &path, + const std::string &file_name) { + if (!parser.builder_.GetSize()) return ""; + std::string filebase = + flatbuffers::StripPath(flatbuffers::StripExtension(file_name)); + std::string make_rule = + BinaryFileName(parser, path, filebase) + ": " + file_name; + auto included_files = + parser.GetIncludedFilesRecursive(parser.root_struct_def_->file); + for (auto it = included_files.begin(); it != included_files.end(); ++it) { + make_rule += " " + *it; + } + return make_rule; +} + } // namespace flatbuffers #if defined(_MSC_VER) diff --git a/src/flatc.cpp b/src/flatc.cpp index 74f2e238..257afa85 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -470,11 +470,16 @@ int FlatCompiler::Compile(int argc, const char **argv) { params_.generators[i].lang_name + " for " + filebase); } } else { - std::string make_rule = params_.generators[i].make_rule( - *parser.get(), output_path, filename); - if (!make_rule.empty()) - printf("%s\n", - flatbuffers::WordWrap(make_rule, 80, " ", " \\").c_str()); + if (params_.generators[i].make_rule == nullptr) { + Error(std::string("Cannot generate make rule for ") + + params_.generators[i].lang_name); + } else { + std::string make_rule = params_.generators[i].make_rule( + *parser.get(), output_path, filename); + if (!make_rule.empty()) + printf("%s\n", + flatbuffers::WordWrap(make_rule, 80, " ", " \\").c_str()); + } } if (grpc_enabled) { if (params_.generators[i].generateGRPC != nullptr) { diff --git a/src/flatc_main.cpp b/src/flatc_main.cpp index 786a1b1e..1b02d294 100644 --- a/src/flatc_main.cpp +++ b/src/flatc_main.cpp @@ -54,11 +54,11 @@ int main(int argc, const char *argv[]) { "Generate C++ headers for tables/structs", flatbuffers::CPPMakeRule }, { flatbuffers::GenerateGo, "-g", "--go", "Go", true, flatbuffers::GenerateGoGRPC, flatbuffers::IDLOptions::kGo, - "Generate Go files for tables/structs", flatbuffers::GeneralMakeRule }, + "Generate Go files for tables/structs", nullptr }, { flatbuffers::GenerateJava, "-j", "--java", "Java", true, flatbuffers::GenerateJavaGRPC, flatbuffers::IDLOptions::kJava, "Generate Java classes for tables/structs", - flatbuffers::GeneralMakeRule }, + flatbuffers::JavaCSharpMakeRule }, { flatbuffers::GenerateJSTS, "-s", "--js", "JavaScript", true, nullptr, flatbuffers::IDLOptions::kJs, "Generate JavaScript code for tables/structs", @@ -72,31 +72,29 @@ int main(int argc, const char *argv[]) { flatbuffers::JSTSMakeRule }, { flatbuffers::GenerateCSharp, "-n", "--csharp", "C#", true, nullptr, flatbuffers::IDLOptions::kCSharp, - "Generate C# classes for tables/structs", flatbuffers::GeneralMakeRule }, + "Generate C# classes for tables/structs", + flatbuffers::JavaCSharpMakeRule }, { flatbuffers::GeneratePython, "-p", "--python", "Python", true, nullptr, flatbuffers::IDLOptions::kPython, - "Generate Python files for tables/structs", - flatbuffers::GeneralMakeRule }, + "Generate Python files for tables/structs", nullptr }, { flatbuffers::GenerateLobster, nullptr, "--lobster", "Lobster", true, nullptr, flatbuffers::IDLOptions::kLobster, - "Generate Lobster files for tables/structs", - flatbuffers::GeneralMakeRule }, + "Generate Lobster files for tables/structs", nullptr }, { flatbuffers::GenerateLua, "-l", "--lua", "Lua", true, nullptr, flatbuffers::IDLOptions::kLua, "Generate Lua files for tables/structs", - flatbuffers::GeneralMakeRule }, + nullptr }, { flatbuffers::GenerateRust, "-r", "--rust", "Rust", true, nullptr, flatbuffers::IDLOptions::kRust, "Generate Rust files for tables/structs", flatbuffers::RustMakeRule }, { flatbuffers::GeneratePhp, nullptr, "--php", "PHP", true, nullptr, flatbuffers::IDLOptions::kPhp, "Generate PHP files for tables/structs", - flatbuffers::GeneralMakeRule }, + nullptr }, { flatbuffers::GenerateKotlin, nullptr, "--kotlin", "Kotlin", true, nullptr, flatbuffers::IDLOptions::kKotlin, - "Generate Kotlin classes for tables/structs", - flatbuffers::GeneralMakeRule }, + "Generate Kotlin classes for tables/structs", nullptr }, { flatbuffers::GenerateJsonSchema, nullptr, "--jsonschema", "JsonSchema", true, nullptr, flatbuffers::IDLOptions::kJsonSchema, - "Generate Json schema", flatbuffers::GeneralMakeRule }, + "Generate Json schema", nullptr }, }; flatbuffers::FlatCompiler::InitParams params; diff --git a/src/idl_gen_general.cpp b/src/idl_gen_general.cpp deleted file mode 100644 index a12719bf..00000000 --- a/src/idl_gen_general.cpp +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright 2014 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// independent from idl_parser, since this code is not needed for most clients - -#include "flatbuffers/code_generators.h" -#include "flatbuffers/flatbuffers.h" -#include "flatbuffers/idl.h" -#include "flatbuffers/util.h" - -#if defined(FLATBUFFERS_CPP98_STL) -# include <cctype> -#endif // defined(FLATBUFFERS_CPP98_STL) - -namespace flatbuffers { -std::string GeneralMakeRule(const Parser &parser, const std::string &path, - const std::string &file_name) { - FLATBUFFERS_ASSERT(parser.opts.lang <= IDLOptions::kMAX); - - std::string file_extension = - (parser.opts.lang == IDLOptions::kJava) ? ".java" : ".cs"; - - std::string make_rule; - - for (auto it = parser.enums_.vec.begin(); it != parser.enums_.vec.end(); - ++it) { - auto &enum_def = **it; - if (!make_rule.empty()) make_rule += " "; - std::string directory = - BaseGenerator::NamespaceDir(parser, path, *enum_def.defined_namespace); - make_rule += directory + enum_def.name + file_extension; - } - - for (auto it = parser.structs_.vec.begin(); it != parser.structs_.vec.end(); - ++it) { - auto &struct_def = **it; - if (!make_rule.empty()) make_rule += " "; - std::string directory = BaseGenerator::NamespaceDir( - parser, path, *struct_def.defined_namespace); - make_rule += directory + struct_def.name + file_extension; - } - - make_rule += ": "; - auto included_files = parser.GetIncludedFilesRecursive(file_name); - for (auto it = included_files.begin(); it != included_files.end(); ++it) { - make_rule += " " + *it; - } - return make_rule; -} - -std::string BinaryFileName(const Parser &parser, const std::string &path, - const std::string &file_name) { - auto ext = parser.file_extension_.length() ? parser.file_extension_ : "bin"; - return path + file_name + "." + ext; -} - -bool GenerateBinary(const Parser &parser, const std::string &path, - const std::string &file_name) { - if (parser.opts.use_flexbuffers) { - auto data_vec = parser.flex_builder_.GetBuffer(); - auto data_ptr = reinterpret_cast<char *>(data(data_vec)); - return !parser.flex_builder_.GetSize() || - flatbuffers::SaveFile( - BinaryFileName(parser, path, file_name).c_str(), data_ptr, - parser.flex_builder_.GetSize(), true); - } - return !parser.builder_.GetSize() || - flatbuffers::SaveFile( - BinaryFileName(parser, path, file_name).c_str(), - reinterpret_cast<char *>(parser.builder_.GetBufferPointer()), - parser.builder_.GetSize(), true); -} - -std::string BinaryMakeRule(const Parser &parser, const std::string &path, - const std::string &file_name) { - if (!parser.builder_.GetSize()) return ""; - std::string filebase = - flatbuffers::StripPath(flatbuffers::StripExtension(file_name)); - std::string make_rule = - BinaryFileName(parser, path, filebase) + ": " + file_name; - auto included_files = - parser.GetIncludedFilesRecursive(parser.root_struct_def_->file); - for (auto it = included_files.begin(); it != included_files.end(); ++it) { - make_rule += " " + *it; - } - return make_rule; -} - -} // namespace flatbuffers diff --git a/src/idl_gen_java.cpp b/src/idl_gen_java.cpp index 123ebddc..b257111f 100644 --- a/src/idl_gen_java.cpp +++ b/src/idl_gen_java.cpp @@ -599,8 +599,7 @@ class JavaGenerator : public BaseGenerator { // Generate a special accessor for the table that when used as the root // of a FlatBuffer - std::string method_name = - "getRootAs" + struct_def.name; + std::string method_name = "getRootAs" + struct_def.name; std::string method_signature = " public static " + struct_def.name + " " + method_name; @@ -1112,8 +1111,7 @@ class JavaGenerator : public BaseGenerator { code += "finish" + size_prefix[i] + struct_def.name; code += "Buffer(FlatBufferBuilder builder, " + GenOffsetType(); code += " offset) {"; - code += " builder.finish" + size_prefix[i] + - "(offset"; + code += " builder.finish" + size_prefix[i] + "(offset"; if (parser_.file_identifier_.length()) code += ", \"" + parser_.file_identifier_ + "\""; @@ -1185,8 +1183,7 @@ class JavaGenerator : public BaseGenerator { code += "__reset(_vector, _element_size, _bb); return this; }\n\n"; auto type_name = struct_def.name; - auto method_start = - method_indent + "public " + type_name + " get"; + auto method_start = method_indent + "public " + type_name + " get"; // Generate the accessors that don't do object reuse. code += method_start + "(int j) { return get"; code += "(new " + type_name + "(), j); }\n"; |