chenBright commented on code in PR #3558:
URL: https://github.com/apache/brpc/pull/3558#discussion_r4242209292


##########
tools/flatbuffers/brpc_flatc.cpp:
##########
@@ -0,0 +1,890 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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.
+
+#include <cerrno>
+#include <cstdio>
+#include <cstdint>
+#include <cstdlib>
+#include <exception>
+#include <fstream>
+#include <iostream>
+#include <iterator>
+#include <limits>
+#include <set>
+#include <sstream>
+#include <string>
+#include <vector>
+#include <fcntl.h>
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <flatbuffers/idl.h>
+
+#if FLATBUFFERS_VERSION_MAJOR != 25 || FLATBUFFERS_VERSION_MINOR != 2 || \
+    FLATBUFFERS_VERSION_REVISION != 10
+#error "brpc_flatc requires FlatBuffers 25.2.10 headers and library"
+#endif
+
+namespace {
+
+const char kLicense[] = R"(// Licensed to the Apache Software Foundation (ASF) 
under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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.
+
+// Generated by brpc_flatc. Do not edit.
+
+)";
+
+const char kArguments[] =
+    "::google::protobuf::RpcController* controller,\n"
+    "        const ::brpc::flatbuffers::Message* request,\n"
+    "        ::brpc::flatbuffers::Message* response,\n"
+    "        ::google::protobuf::Closure* done";
+
+struct Service {
+    const flatbuffers::ServiceDef* definition;
+    std::vector<int32_t> ids;
+};
+
+std::string JoinNamespace(const flatbuffers::Definition& definition,
+                          const std::string& separator) {
+    std::string result;
+    if (definition.defined_namespace) {
+        for (const auto& component : definition.defined_namespace->components) 
{
+            if (!result.empty()) {
+                result += separator;
+            }
+            result += component;
+        }
+    }
+    return result;
+}
+
+std::string Qualified(const flatbuffers::Definition& definition) {
+    const std::string ns = JoinNamespace(definition, "::");
+    return "::" + (ns.empty() ? "" : ns + "::") + definition.name;
+}
+
+std::string GuardComponent(const std::string& value) {
+    static const char digits[] = "0123456789ABCDEF";
+    std::string result;
+    for (unsigned char c : value) {
+        result += digits[c >> 4];
+        result += digits[c & 15];
+    }
+    return result;
+}
+
+bool IsCppIdentifier(const std::string& name) {
+    static const std::set<std::string> keywords = {
+        "alignas", "alignof", "and", "and_eq", "asm", "auto", "bitand",
+        "bitor", "bool", "break", "case", "catch", "char", "char8_t",
+        "char16_t", "char32_t", "class", "compl", "concept", "const",
+        "const_cast", "consteval", "constexpr", "constinit", "continue",
+        "co_await", "co_return", "co_yield", "decltype", "default",
+        "delete", "do",
+        "double", "dynamic_cast", "else", "enum", "explicit", "export",
+        "extern", "false", "float", "for", "friend", "goto", "if",
+        "inline", "int", "long", "mutable", "namespace", "new",
+        "noexcept", "not", "not_eq", "nullptr", "operator", "or", "or_eq",
+        "private", "protected", "public", "register", "reinterpret_cast",
+        "requires", "return", "short", "signed", "sizeof", "static",
+        "static_assert", "static_cast", "struct", "switch", "template",
+        "this", "thread_local", "throw", "true", "try", "typedef",
+        "typeid", "typename", "union", "unsigned", "using", "virtual",
+        "void", "volatile", "wchar_t", "while", "xor", "xor_eq"
+    };
+    return !name.empty() && keywords.count(name) == 0;
+}
+
+std::string FlatcName(const std::string& name) {
+    // flatc 25.2.10 escapes a different set than the C++20 rejection list.
+    static const std::set<std::string> extra = {
+        "atomic_cancel", "atomic_commit", "atomic_noexcept", "import",
+        "module", "synchronized"
+    };
+    const bool newer_keyword = name == "char8_t" || name == "consteval" ||
+                               name == "constinit";
+    return ((!IsCppIdentifier(name) && !newer_keyword) || extra.count(name))
+        ? name + "_" : name;
+}
+
+std::string FlatcNamespace(const flatbuffers::Definition& definition) {
+    const std::string ns = JoinNamespace(definition, "::");
+    return ns.empty() ? "::" : "::" + ns + "::";
+}
+
+bool IsReservedIdentifier(const std::string& name, bool global) {
+    return name.find("__") != std::string::npos ||
+           (!name.empty() && name[0] == '_' &&
+            (global || (name.size() > 1 && name[1] >= 'A' && name[1] <= 'Z')));
+}
+
+bool ValidateName(const flatbuffers::Definition& definition,
+                  std::string* error, bool member = false) {
+    if (!IsCppIdentifier(definition.name)) {
+        *error = "C++ keyword is not supported: " + definition.name;
+        return false;
+    }
+    const bool global = !member && (!definition.defined_namespace ||
+        definition.defined_namespace->components.empty());
+    if (IsReservedIdentifier(definition.name, global)) {
+        *error = "reserved C++ identifier: " + definition.name;
+        return false;
+    }
+    if (definition.defined_namespace) {
+        bool first = true;
+        for (const auto& component : definition.defined_namespace->components) 
{
+            if (IsReservedIdentifier(component, first)) {
+                *error = "reserved C++ identifier in namespace: " + component;
+                return false;
+            }
+            first = false;
+            if (!IsCppIdentifier(component)) {
+                *error = "C++ keyword namespace is not supported: " + 
component;
+                return false;
+            }
+        }
+    }
+    return true;
+}
+
+bool ParseId(const flatbuffers::Value& value, int32_t* id) {
+    if (value.constant.empty() ||
+        !flatbuffers::IsInteger(value.type.base_type)) {
+        return false;
+    }
+    int64_t number = 0;
+    for (char c : value.constant) {
+        if (c < '0' || c > '9') {
+            return false;
+        }
+        number = number * 10 + (c - '0');
+        if (number > std::numeric_limits<int32_t>::max()) {
+            return false;
+        }
+    }
+    *id = static_cast<int32_t>(number);
+    return true;
+}
+
+void CollectRootNames(const flatbuffers::Parser& parser,
+                      std::set<std::string>* names) {
+    if (!parser.root_struct_def_) {
+        return;
+    }
+    const auto& root = *parser.root_struct_def_;
+    const std::string ns = FlatcNamespace(root);
+    const std::string name = FlatcName(root.name);
+    names->insert(ns + "Get" + name);
+    names->insert(ns + "GetSizePrefixed" + name);
+    names->insert(ns + "Verify" + name + "Buffer");
+    names->insert(ns + "VerifySizePrefixed" + name + "Buffer");
+    names->insert(ns + "Finish" + name + "Buffer");
+    names->insert(ns + "FinishSizePrefixed" + name + "Buffer");
+    if (!parser.file_identifier_.empty()) {
+        names->insert(ns + name + "Identifier");
+        names->insert(ns + name + "BufferHasIdentifier");
+        names->insert(ns + "SizePrefixed" + name + "BufferHasIdentifier");
+    }
+    if (!parser.file_extension_.empty()) {
+        names->insert(ns + name + "Extension");
+    }
+}
+
+bool CollectServices(const flatbuffers::Parser& parser,
+                     const std::string& input, const char** include_paths,
+                     std::vector<Service>* services, std::string* error) {
+    std::set<std::string> generated_class_names = {
+        "::brpc", "::butil", "::flatbuffers", "::google", "::std"
+    };
+    CollectRootNames(parser, &generated_class_names);
+    // The parser resets root_type after each include; flatc emits those 
helpers
+    // when each imported schema is generated separately.
+    for (const auto& file : parser.GetIncludedFilesRecursive(input)) {
+        if (file == input) {
+            continue;
+        }
+        std::string schema;
+        flatbuffers::Parser included;
+        if (!flatbuffers::LoadFile(file.c_str(), false, &schema) ||
+            !included.Parse(schema.c_str(), include_paths, file.c_str())) {
+            *error = "cannot inspect imported root_type: " + file;
+            return false;
+        }
+        CollectRootNames(included, &generated_class_names);
+    }
+    // Reserve every namespace prefix, including those from imported schemas.
+    for (const auto* ns : parser.namespaces_) {
+        std::string prefix;
+        for (const auto& component : ns->components) {
+            prefix += "::" + component;
+            generated_class_names.insert(prefix);
+        }
+    }
+    for (const auto* definition : parser.services_.vec) {
+        if (definition->generated) {
+            const std::string service_name = Qualified(*definition);
+            generated_class_names.insert(service_name);
+            generated_class_names.insert(service_name + "_Stub");
+        }
+    }
+    for (const auto* definition : parser.structs_.vec) {
+        const std::string ns = FlatcNamespace(*definition);
+        const std::string name = FlatcName(definition->name);
+        const std::string type_name = ns + name;
+        generated_class_names.insert(type_name);
+        if (!definition->fixed) {
+            generated_class_names.insert(type_name + "Builder");
+            generated_class_names.insert(ns + "Create" + name);
+            for (const auto* field : definition->fields.vec) {
+                if (!field->deprecated &&
+                    (flatbuffers::IsString(field->value.type) ||
+                     flatbuffers::IsVector(field->value.type))) {
+                    generated_class_names.insert(
+                        ns + "Create" + name + "Direct");
+                    break;
+                }
+            }
+        }
+    }
+    // Match the default flatc --cpp API, including non-type declarations.
+    for (const auto* definition : parser.enums_.vec) {
+        const std::string ns = FlatcNamespace(*definition);
+        const std::string name = FlatcName(definition->name);
+        const std::string enum_name = ns + name;
+        generated_class_names.insert(enum_name);
+        for (const auto* value : definition->Vals()) {
+            generated_class_names.insert(enum_name + "_" + 
FlatcName(value->name));
+        }
+        if (definition->attributes.Lookup("bit_flags")) {
+            generated_class_names.insert(enum_name + "_NONE");
+            generated_class_names.insert(enum_name + "_ANY");
+        } else {
+            generated_class_names.insert(enum_name + "_MIN");
+            generated_class_names.insert(enum_name + "_MAX");
+        }
+        generated_class_names.insert(ns + "EnumValues" + name);
+        generated_class_names.insert(ns + "EnumName" + name);
+        if (definition->Distance() / definition->size() < 5) {
+            generated_class_names.insert(ns + "EnumNames" + name);
+        }
+        if (definition->is_union) {
+            if (!definition->uses_multiple_type_instances) {
+                generated_class_names.insert(enum_name + "Traits");
+            }
+            generated_class_names.insert(ns + "Verify" + name);
+            generated_class_names.insert(
+                ns + "Verify" + name + "Vector");
+        }
+    }
+    for (const auto* definition : parser.services_.vec) {
+        // Included schemas are generated separately, just as with flatc --cpp.
+        if (definition->generated) {
+            continue;
+        }
+        if (!ValidateName(*definition, error)) {
+            return false;
+        }
+        if (definition->name == "Stub" || definition->name == "descriptor" ||
+            definition->name == "GetDescriptor" ||
+            definition->name == "FBCallMethod") {
+            *error = "service name collides with generated API: " +
+                     definition->name;
+            return false;
+        }
+        const std::string service_name = Qualified(*definition);
+        const std::string stub_name = service_name + "_Stub";
+        if (IsReservedIdentifier(definition->name + "_Stub", false)) {
+            *error = "generated stub uses reserved C++ identifier: " + 
stub_name;
+            return false;
+        }
+        if (!generated_class_names.insert(service_name).second) {
+            *error = "generated service class name collides: " + service_name;
+            return false;
+        }
+        if (!generated_class_names.insert(stub_name).second) {
+            *error = "generated service class name collides: " + stub_name;
+            return false;
+        }
+        Service service = {definition, {}};
+        std::set<int32_t> ids;
+        if (definition->calls.vec.empty()) {
+            *error = "rpc_service must contain at least one method: " +
+                     definition->name;
+            return false;
+        }
+        for (const auto* call : definition->calls.vec) {
+            if (!ValidateName(*call, error, true) ||
+                !ValidateName(*call->request, error) ||
+                !ValidateName(*call->response, error)) {
+                return false;
+            }
+            if (call->name == definition->name ||
+                call->name == definition->name + "_Stub" ||
+                call->name == "descriptor" || call->name == "GetDescriptor" ||
+                call->name == "FBCallMethod" || call->name == "Stub" ||
+                call->name == "channel" || call->name == "channel_" ||
+                call->name == "owned_channel_") {
+                *error = "method name collides with generated API: " +
+                         call->name;
+                return false;
+            }
+            const auto* attribute = call->attributes.Lookup("id");
+            int32_t id = 0;
+            if (!attribute || !ParseId(*attribute, &id)) {
+                *error = definition->name + "." + call->name +
+                         " requires an explicit nonnegative int32 (id: N)";
+                return false;
+            }
+            if (!ids.insert(id).second) {
+                *error = "duplicate method id " + std::to_string(id) +
+                         " in " + definition->name;
+                return false;
+            }
+            if (call->attributes.Lookup("streaming")) {
+                *error = "streaming RPC is not supported: " + call->name;
+                return false;
+            }
+            service.ids.push_back(id);
+        }
+        services->push_back(service);
+    }
+    if (services->empty()) {
+        *error = "input schema contains no rpc_service to generate";
+        return false;
+    }
+    return true;
+}
+
+void OpenNamespace(const flatbuffers::Definition& definition,
+                   std::ostream& out) {
+    if (definition.defined_namespace) {
+        for (const auto& component : definition.defined_namespace->components) 
{
+            out << "namespace " << component << " {\n";
+        }
+    }
+    out << '\n';
+}
+
+void CloseNamespace(const flatbuffers::Definition& definition,
+                    std::ostream& out) {
+    if (definition.defined_namespace) {
+        const auto& components = definition.defined_namespace->components;
+        for (auto it = components.rbegin(); it != components.rend(); ++it) {
+            out << "}  // namespace " << *it << '\n';
+        }
+    }
+    out << '\n';
+}
+
+void GenerateHeader(const Service& service, std::ostream& out) {
+    const auto& definition = *service.definition;
+    const std::string& name = definition.name;
+    OpenNamespace(definition, out);
+    out << "class " << name << "_Stub;\n\n"
+        << "class " << name << " : public ::brpc::flatbuffers::Service {\n"
+        << "public:\n"
+        << "    typedef " << name << "_Stub Stub;\n"
+        << "    static const ::brpc::flatbuffers::ServiceDescriptor* "
+           "descriptor();\n"
+        << "    const ::brpc::flatbuffers::ServiceDescriptor* "
+           "GetDescriptor() override;\n"
+        << "    void FBCallMethod(\n"
+        << "        const ::brpc::flatbuffers::MethodDescriptor* method,\n"
+        << "        " << kArguments << ") override;\n";
+    for (const auto* call : definition.calls.vec) {
+        out << "    virtual void " << call->name << "(\n"
+            << "        " << kArguments << ");\n";
+    }
+    out << "};\n\n"
+        << "class " << name << "_Stub : public " << name << " {\n"
+        << "public:\n"
+        << "    explicit " << name << "_Stub(\n"
+        << "        ::brpc::flatbuffers::RpcChannel* channel,\n"
+        << "        ::brpc::flatbuffers::Service::ChannelOwnership "
+           "ownership =\n"
+        << "            ::brpc::flatbuffers::Service::"
+           "STUB_DOESNT_OWN_CHANNEL);\n"
+        << "    ::brpc::flatbuffers::RpcChannel* channel() const {\n"
+        << "        return channel_;\n"
+        << "    }\n";
+    for (const auto* call : definition.calls.vec) {
+        out << "    void " << call->name << "(\n"
+            << "        " << kArguments << ") override;\n";
+    }
+    out << "\nprivate:\n"
+        << "    ::brpc::flatbuffers::RpcChannel* channel_;\n"
+        << "    ::std::unique_ptr<::brpc::flatbuffers::RpcChannel> "
+           "owned_channel_;\n"
+        << "};\n\n";
+    CloseNamespace(definition, out);
+}
+
+void GenerateFailureHandler(std::ostream& out) {
+    // A local callable cannot be hidden by schema declarations or RPC names.
+    out << "    const auto fail = [controller, done](const char* reason) {\n"
+        << "        if (controller) {\n"
+        << "            controller->SetFailed(reason);\n"
+        << "        }\n"
+        << "        if (done) {\n"
+        << "            done->Run();\n"
+        << "        }\n"
+        << "    };\n";
+}
+
+void GenerateSource(const Service& service, std::ostream& out) {
+    const auto& definition = *service.definition;
+    const std::string& name = definition.name;
+    OpenNamespace(definition, out);
+    out << "const ::brpc::flatbuffers::ServiceDescriptor* " << name
+        << "::descriptor() {\n"
+        << "    struct Holder {\n"
+        << "        ::brpc::flatbuffers::ServiceDescriptor value;\n"
+        << "        Holder() {\n"
+        << "            const ::brpc::flatbuffers::BrpcDescriptorTable "
+           "table = {\n"
+        << "                \"" << JoinNamespace(definition, ".") << "\", \""
+        << name << "\",\n                \"";
+    for (size_t i = 0; i < definition.calls.vec.size(); ++i) {
+        out << (i == 0 ? "" : " ") << definition.calls.vec[i]->name;
+    }
+    out << "\", {";
+    for (size_t i = 0; i < service.ids.size(); ++i) {
+        out << (i == 0 ? "" : ", ") << service.ids[i];
+    }
+    out << "}\n            };\n"
+        << "            if (value.init(table) != 0) {\n"
+        << "                throw ::std::runtime_error(\n"
+        << "                    \"invalid generated service descriptor\");\n"
+        << "            }\n"
+        << "        }\n"
+        << "    };\n"
+        << "    static const Holder holder;\n"
+        << "    return &holder.value;\n"
+        << "}\n\n"
+        << "const ::brpc::flatbuffers::ServiceDescriptor* " << name
+        << "::GetDescriptor() {\n"
+        << "    return descriptor();\n"
+        << "}\n\n"
+        << "void " << name << "::FBCallMethod(\n"
+        << "        const ::brpc::flatbuffers::MethodDescriptor* method,\n"
+        << "        " << kArguments << ") {\n";
+    GenerateFailureHandler(out);
+    out << "    if (!method || method->service() != descriptor() ||\n"
+        << "        descriptor()->FindMethodByIndex(method->index()) != "
+           "method) {\n"
+        << "        fail(\"invalid service method\");\n"
+        << "        return;\n"
+        << "    }\n"
+        << "    if (!request || !response) {\n"
+        << "        fail(\"null request or response\");\n"
+        << "        return;\n"
+        << "    }\n"
+        << "    switch (method->index()) {\n";
+    for (size_t i = 0; i < definition.calls.vec.size(); ++i) {
+        const auto& call = *definition.calls.vec[i];
+        out << "    case " << service.ids[i] << ":\n"
+            << "        if (!request->Verify<" << Qualified(*call.request)
+            << ">()) {\n"

Review Comment:
   This valid schema is accepted by both generators:
   
      ```fbs
      namespace api;
      table module { value:int; }
      table Response {}
      rpc_service Echo {
          Call(module):Response (id: 1);
      }
      ```
   
   Default `flatc --cpp` generates `api::module_`, but the service generator 
emits `request->Verify<::api::module>()`, so compilation fails.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to