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


##########
tools/flatbuffers/brpc_flatc.cpp:
##########
@@ -0,0 +1,495 @@
+// 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 <cstdint>
+#include <exception>
+#include <fstream>
+#include <iostream>
+#include <iterator>
+#include <limits>
+#include <set>
+#include <sstream>
+#include <string>
+#include <vector>
+#include <flatbuffers/idl.h>
+
+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", "char16_t",

Review Comment:
   `char8_t` is a C++20 keyword but is absent from this rejection list. A 
schema using it as a service, method, type, or namespace component passes 
validation and then produces declarations that fail to compile for consumers 
using C++20, even though the generated code is documented as supporting C++14 
or newer. Add `char8_t` to the keyword set (and cover generation under C++20).



##########
tools/flatbuffers/brpc_flatc.cpp:
##########
@@ -0,0 +1,495 @@
+// 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 <cstdint>
+#include <exception>
+#include <fstream>
+#include <iostream>
+#include <iterator>
+#include <limits>
+#include <set>
+#include <sstream>
+#include <string>
+#include <vector>
+#include <flatbuffers/idl.h>
+
+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", "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;
+}
+
+bool ValidateName(const flatbuffers::Definition& definition,
+                  std::string* error) {
+    if (!IsCppIdentifier(definition.name)) {
+        *error = "C++ keyword is not supported: " + definition.name;
+        return false;
+    }
+    if (definition.defined_namespace) {
+        for (const auto& component : definition.defined_namespace->components) 
{
+            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;
+}
+
+bool CollectServices(const flatbuffers::Parser& parser,
+                     std::vector<Service>* services, std::string* error) {
+    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)) {

Review Comment:
   This validates each service independently but does not detect collisions 
between generated class names. A valid schema containing services `Echo` and 
`Echo_Stub` emits `class Echo_Stub` for both (the first service's stub and the 
second service itself), so the advertised multiple-service output cannot 
compile. Track the fully qualified service and `<service>_Stub` names across 
the schema and reject collisions before emitting files; add an acceptance case 
for this pairing.



-- 
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