Fix C Generator 62/263462/1
authorHwankyu Jhun <h.jhun@samsung.com>
Fri, 3 Sep 2021 02:42:12 +0000 (11:42 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Fri, 3 Sep 2021 02:48:08 +0000 (11:48 +0900)
The implemenation that the callback port check is separated.
If the interface doesn't have the delegate, the callback port check
is not needed.

Change-Id: I04aaf8c0d5a00347901afbbebda75f85744bf442
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
idlc/gen/c_stub_body_gen.cc
idlc/gen/c_stub_body_gen.h
idlc/gen/c_stub_body_gen_cb.h

index 9175243..c0d6826 100644 (file)
@@ -172,13 +172,18 @@ void CStubBodyGen::GenInterfaceDefs(std::ofstream& stream) {
 
 void CStubBodyGen::GenInterfaceDef(std::ofstream& stream,
     const Interface& iface) {
+  bool has_delegate = false;
   for (auto& d : iface.GetDeclarations().GetDecls()) {
     if (d->GetMethodType() != Declaration::MethodType::DELEGATE)
       continue;
 
     GenInterfaceDelegateDef(stream, iface, *d);
+    has_delegate = true;
   }
 
+  if (has_delegate)
+    GenInterfaceCallbackPortCheckDef(stream, iface);
+
   GenInterfaceContextDef(stream, iface);
   GenInterfaceBaseDef(stream, iface);
 }
@@ -194,6 +199,16 @@ void CStubBodyGen::GenInterfaceDelegateDef(std::ofstream& stream,
   stream << SmartIndent(code);
 }
 
+// @see #CB_INTERFACE_CALLBACK_PORT_CHECK_DEF
+void CStubBodyGen::GenInterfaceCallbackPortCheckDef(std::ofstream& stream,
+    const Interface& iface) {
+  std::string code = ReplaceAll(CB_INTERFACE_CALLBACK_PORT_CHECK_DEF,
+      "<PREFIX>", GetHandlePrefix());
+  code = ReplaceAll(code, "<NAME>", iface.GetID());
+
+  stream << SmartIndent(code);
+}
+
 // @see #CB_INTERFACE_CONTEXT_DEF
 void CStubBodyGen::GenInterfaceContextDef(std::ofstream& stream,
     const Interface& iface) {
@@ -225,11 +240,13 @@ void CStubBodyGen::GenInterfaces(std::ofstream& stream) {
 }
 
 void CStubBodyGen::GenInterface(std::ofstream& stream, const Interface& iface) {
+  bool has_delegate = false;
   for (auto& d : iface.GetDeclarations().GetDecls()) {
     if (d->GetMethodType() != Declaration::MethodType::DELEGATE)
       continue;
 
     GenInterfaceDelegateBase(stream, iface, *d);
+    has_delegate = true;
   }
 
   for (auto& d : iface.GetDeclarations().GetDecls()) {
@@ -241,6 +258,10 @@ void CStubBodyGen::GenInterface(std::ofstream& stream, const Interface& iface) {
 
   GenInterfaceMethodTable(stream, iface);
   GenInterfaceContextBase(stream, iface);
+
+  if (has_delegate)
+    GenInterfaceCallbackPortCheck(stream, iface);
+
   GenInterfaceBase(stream, iface);
 }
 
@@ -638,6 +659,16 @@ void CStubBodyGen::GenInterfaceMethodTable(std::ofstream& stream,
   stream << SmartIndent(code);
 }
 
+// @see #CB_INTERFACE_CALLBACK_PORT_CHECK
+void CStubBodyGen::GenInterfaceCallbackPortCheck(std::ofstream& stream,
+    const Interface& iface) {
+  std::string code = ReplaceAll(CB_INTERFACE_CALLBACK_PORT_CHECK, "<PREFIX>",
+      GetHandlePrefix());
+  code = ReplaceAll(code, "<NAME>", iface.GetID());
+
+  stream << SmartIndent(code);
+}
+
 // @see #CB_INTERFACE_PRIVILEGE_ADD
 // @see #CB_INTERFACE_TRUSTED_SET
 std::string CStubBodyGen::GenAccessControlSet(const Interface& iface) {
index 14169f2..b8358e8 100644 (file)
@@ -49,6 +49,8 @@ class CStubBodyGen : public CBodyGeneratorBase {
   void GenInterfaceContextDef(std::ofstream& stream, const Interface& iface);
   void GenInterfaceDelegateDef(std::ofstream& stream, const Interface& iface,
       const Declaration& decl);
+  void GenInterfaceCallbackPortCheckDef(std::ofstream& stream,
+      const Interface& iface);
   void GenInterfaceBaseDef(std::ofstream& stream, const Interface& iface);
 
   void GenInterfaces(std::ofstream& stream);
@@ -59,6 +61,8 @@ class CStubBodyGen : public CBodyGeneratorBase {
   void GenInterfaceMethodHandlerBase(std::ofstream& stream,
       const Interface& iface, const Declaration& decl);
   void GenInterfaceMethodTable(std::ofstream& stream, const Interface& iface);
+  void GenInterfaceCallbackPortCheck(std::ofstream& stream,
+      const Interface& iface);
   void GenInterfaceBase(std::ofstream& stream, const Interface& iface);
 
   std::string GenMethodEnums(const Interface& iface);
index d073959..b732a66 100644 (file)
@@ -115,7 +115,13 @@ typedef struct <PREFIX>_<NAME>_context_s {
   bool done;
 #endif /* TIDL_THREAD_ENABLE */
 } <PREFIX>_<NAME>_context_t;
+)__c_cb";
 
+/**
+ *
+ */
+constexpr const char CB_INTERFACE_CALLBACK_PORT_CHECK_DEF[] =
+R"__c_cb(
 static bool __<PREFIX>_<NAME>_exist_callback_port(rpc_port_h callback_port);
 )__c_cb";
 
@@ -399,41 +405,6 @@ static void __<PREFIX>_<NAME>_remove_context(<PREFIX>_<NAME>_context_h context)
   __<NAME>.contexts = g_list_remove(__<NAME>.contexts, context);
   g_rec_mutex_unlock(&__<NAME>.mutex);
 }
-
-static bool __<PREFIX>_<NAME>_exist_callback_port(rpc_port_h callback_port)
-{
-  rpc_port_h port;
-  GList *iter;
-
-  g_rec_mutex_lock(&__<NAME>.mutex);
-  iter = __<NAME>.callback_ports;
-  while (iter) {
-    port = iter->data;
-    if (port == callback_port) {
-      g_rec_mutex_unlock(&__<NAME>.mutex);
-      return true;
-    }
-
-    iter = g_list_next(iter);
-  }
-  g_rec_mutex_unlock(&__<NAME>.mutex);
-
-  return false;
-}
-
-static void __<PREFIX>_<NAME>_add_callback_port(rpc_port_h callback_port)
-{
-  g_rec_mutex_lock(&__<NAME>.mutex);
-  __<NAME>.callback_ports = g_list_append(__<NAME>.callback_ports, callback_port);
-  g_rec_mutex_unlock(&__<NAME>.mutex);
-}
-
-static void __<PREFIX>_<NAME>_remove_callback_port(rpc_port_h callback_port)
-{
-  g_rec_mutex_lock(&__<NAME>.mutex);
-  __<NAME>.callback_ports = g_list_remove(__<NAME>.callback_ports, callback_port);
-  g_rec_mutex_unlock(&__<NAME>.mutex);
-}
 )__c_cb";
 
 /**
@@ -932,6 +903,20 @@ R"__c_cb(
  */
 constexpr const char CB_INTERFACE_BASE[] =
 R"__c_cb(
+static void __<PREFIX>_<NAME>_add_callback_port(rpc_port_h callback_port)
+{
+  g_rec_mutex_lock(&__<NAME>.mutex);
+  __<NAME>.callback_ports = g_list_append(__<NAME>.callback_ports, callback_port);
+  g_rec_mutex_unlock(&__<NAME>.mutex);
+}
+
+static void __<PREFIX>_<NAME>_remove_callback_port(rpc_port_h callback_port)
+{
+  g_rec_mutex_lock(&__<NAME>.mutex);
+  __<NAME>.callback_ports = g_list_remove(__<NAME>.callback_ports, callback_port);
+  g_rec_mutex_unlock(&__<NAME>.mutex);
+}
+
 static void __<PREFIX>_<NAME>_connected_event_cb(const char *sender, const char *instance, void *user_data)
 {
   <PREFIX>_<NAME>_context_h context;
@@ -1136,6 +1121,34 @@ int <PREFIX>_<NAME>_get_client_number(unsigned int *client_number)
 )__c_cb";
 
 /**
+ * <PREFIX> The prefix of the interface.
+ * <NAME> The name of the interface.
+ */
+constexpr const char CB_INTERFACE_CALLBACK_PORT_CHECK[] =
+R"__c_cb(
+static bool __<PREFIX>_<NAME>_exist_callback_port(rpc_port_h callback_port)
+{
+  rpc_port_h port;
+  GList *iter;
+
+  g_rec_mutex_lock(&__<NAME>.mutex);
+  iter = __<NAME>.callback_ports;
+  while (iter) {
+    port = iter->data;
+    if (port == callback_port) {
+      g_rec_mutex_unlock(&__<NAME>.mutex);
+      return true;
+    }
+
+    iter = g_list_next(iter);
+  }
+  g_rec_mutex_unlock(&__<NAME>.mutex);
+
+  return false;
+}
+)__c_cb";
+
+/**
  * <NAME> The name of the interface.
  * <PRIVILEGE> The privilege name.
  */