Adjust coding style for C Generator 18/176118/2
authorHwankyu Jhun <h.jhun@samsung.com>
Tue, 17 Apr 2018 00:46:05 +0000 (09:46 +0900)
committerHwanKyu Jhun <h.jhun@samsung.com>
Tue, 17 Apr 2018 00:51:55 +0000 (00:51 +0000)
- Removes unnecessary blank lines
+------------------------------+------------------------------+
| Before                       | After                        |
+------------------------------+------------------------------+
| static void func(void)       | static void func(void)       |
| {                            | {                            |
|     int k;                   |         int k;               |
|                              |                              |
|                              |         printf("Test");      |
|     printf("Test");          | }                            |
|                              |                              |
|                              |                              |
| }                            |                              |
+------------------------------+------------------------------+

Change-Id: I642c904d83eb7c426b7276fe2c122b0a9ec7fe14
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
idlc/c_gen/c_body_gen_base_cb.h
idlc/c_gen/c_gen_base.cc
idlc/c_gen/c_proxy_body_gen.cc
idlc/c_gen/c_proxy_body_gen_cb.h
idlc/c_gen/c_proxy_header_gen.cc

index 9dd223b..88f66bf 100644 (file)
@@ -157,7 +157,9 @@ $$
 const char CB_WRITE_LIST_BLOCK[] =
 R"__c_cb(
 do {
-    GList *iter = $$;
+    GList *iter;
+
+    iter = $$;
     while (iter) {
         $$value = iter->data;
 
@@ -168,7 +170,8 @@ do {
         }
         $$
     }
-} while (0);)__c_cb";
+} while (0);
+)__c_cb";
 
 const char CB_WRITE_ARRAY_BLOCK[] =
 R"__c_cb(
@@ -176,7 +179,8 @@ do {
     for (int i = 0; i < $$; i++) {
 $$
     }
-} while (0);)__c_cb";
+} while (0);
+)__c_cb";
 
 const char CB_READ_LIST_BLOCK[] =
 R"__c_cb(do {
@@ -189,10 +193,12 @@ R"__c_cb(do {
         $$
         $$ = g_list_append($$, value);
     }
-} while (0);)__c_cb";
+} while (0);
+)__c_cb";
 
 const char CB_READ_ARRAY_BLOCK[] =
-R"__c_cb(do {
+R"__c_cb(
+do {
     $$
 
     h->## = calloc(h->##_size, sizeof(*h->##));
@@ -206,11 +212,15 @@ R"__c_cb(do {
 
         $$
     }
-} while (0);)__c_cb";
+} while (0);
+)__c_cb";
 
 const char CB_FINALIZE_LIST_BLOCK[] =
-R"__c_cb(do {
-    GList *iter = $$;
+R"__c_cb(
+do {
+    GList *iter;
+
+    iter = $$;
     while (iter) {
         $$value = iter->data;
         $$
@@ -221,7 +231,8 @@ R"__c_cb(do {
 )__c_cb";
 
 const char CB_FINALIZE_ARRAY_BLOCK[] =
-R"__c_cb(do {
+R"__c_cb(
+do {
     for (int i = 0; i < $$; i++) {
         $$
     }
@@ -230,7 +241,8 @@ R"__c_cb(do {
 )__c_cb";
 
 const char CB_SETTER_LIST_BLOCK[] =
-R"__c_cb(do {
+R"__c_cb(
+do {
     $$value;
 
     value = calloc(1, sizeof(*value));
@@ -240,7 +252,8 @@ R"__c_cb(do {
 )__c_cb";
 
 const char CB_SETTER_ARRAY_BLOCK[] =
-R"__c_cb(do {
+R"__c_cb(
+do {
     h->## = calloc(##_size, sizeof(*##));
     if (!h->##) {
         dlog_print(DLOG_ERROR, LOG_TAG, "Out of memory");
@@ -255,7 +268,8 @@ R"__c_cb(do {
 )__c_cb";
 
 const char CB_GETTER_ARRAY_BLOCK[] =
-R"__c_cb(do {
+R"__c_cb(
+do {
     if (h->##_size == 0) {
         dlog_print(DLOG_WARN, LOG_TAG, "## is empty");
         break;
@@ -275,8 +289,11 @@ R"__c_cb(do {
 )__c_cb";
 
 const char CB_ITERATOR_BLOCK[] =
-R"__c_cb(do {
-    GList *iter = $$;
+R"__c_cb(
+do {
+    GList *iter;
+
+    iter = $$;
     while (iter) {
         $$value = iter->data;
 
@@ -294,8 +311,11 @@ R"__c_cb(do {
 )__c_cb";
 
 const char CB_CLONER_LIST_BLOCK[] =
-R"__c_cb(do {
-    GList *iter = $$;
+R"__c_cb(
+do {
+    GList *iter;
+
+    iter = $$;
     while (iter) {
         $$new_value;
         $$value = iter->data;
@@ -314,7 +334,8 @@ R"__c_cb(do {
 )__c_cb";
 
 const char CB_CLONER_ARRAY_BLOCK[] =
-R"__c_cb(do {
+R"__c_cb(
+do {
     if (h->##_size == 0) {
         dlog_print(DLOG_WARN, LOG_TAG, "## is empty");
         break;
@@ -336,11 +357,13 @@ R"__c_cb(do {
 const char CB_IF_STATEMENT_WITH_BRACES[] =
 R"__c_cb(if ($$) {
     $$
-})__c_cb";
+}
+)__c_cb";
 
 const char CB_IF_STATEMENT[] =
 R"__c_cb(if ($$)
-    $$)__c_cb";
+    $$
+)__c_cb";
 
 const char CB_DELEGATE_ENUM_FORMAT[] = "\n$$_DELEGATE_$$ = $$,";
 
index 1b8bd97..5468f04 100644 (file)
@@ -18,6 +18,7 @@
 #include <cassert>
 #include <vector>
 #include <sstream>
+#include <algorithm>
 
 #include "idlc/c_gen/c_gen_base.h"
 
@@ -158,58 +159,84 @@ void CGeneratorBase::AddStructureFromType(const BaseType& type) {
 std::string CGeneratorBase::SmartIndent(std::string lines) {
   std::stringstream ss(lines);
   std::string result;
-  std::string to;
+  std::string line;
+  std::string next_line;
+  std::string tab;
   std::string back;
   std::size_t found;
   bool if_statement = false;
   bool continuous = false;
-  int indent = 0;
+  int tab_size = 0;
+  int line_count = 0;
 
-  while (std::getline(ss, to, '\n')) {
-    to = Trim(to);
-    found = to.find('}');
+  while (std::getline(ss, next_line, '\n')) {
+    line_count++;
+    if (line_count == 1) {
+      line = Trim(next_line);
+      continue;
+    }
+
+    tab.clear();
+    found = line.find('}');
     if (found != std::string::npos) {
-      indent--;
+      tab_size--;
     }
 
-    if (to.length() > 0) {
-      for (int i = 0; i < indent; i++) {
-        result += "\t";
-      }
+    if (line.length() > 0) {
+      tab += Tab(tab_size);
     }
 
-    if (continuous && !indent &&
+    if (continuous && tab_size == 0 &&
         found == std::string::npos) {
-      for (int i = 0; i < 2; i++) {
-        result += "\t";
-      }
+      tab += Tab(2);
     }
 
     if (found == std::string::npos && if_statement) {
-      indent--;
+      tab_size--;
       if_statement = false;
     }
 
     continuous = false;
-    back = to.back();
+    back = line.back();
     if (back == ",")
       continuous = true;
 
-    result += to;
-    result += "\n";
+    if (line.empty() ||
+        line.length() == 0 ||
+        std::all_of(line.begin(), line.end(), isspace)) {
+      std::string n_line = Trim(next_line);
+      if (n_line.empty() ||
+          n_line.length() == 0 ||
+          n_line.find('}') != std::string::npos ||
+          std::all_of(n_line.begin(), n_line.end(), isspace)) {
+        line = n_line;
+        continue;
+      } else {
+        result += tab + line;
+        result += NLine(1);
+      }
+    } else {
+      result += tab + line;
+      result += NLine(1);
+    }
 
-    found = to.find('{');
+    found = line.find('{');
     if (found != std::string::npos) {
-      indent++;
+      tab_size++;
     } else {
-      found = to.find("if (");
+      found = line.find("if (");
       if (found != std::string::npos) {
-        indent++;
+        tab_size++;
         if_statement = true;
       }
     }
+
+    line = Trim(next_line);
   }
 
+  result += line;
+  result += NLine(1);
+
   return result;
 }
 
@@ -238,7 +265,6 @@ void CGeneratorBase::GenLicenseDescription(std::ofstream& stream) {
 
 void CGeneratorBase::GenIncludeDefaultHeaders(std::ofstream& stream,
                                               bool body) {
-  stream << NLine(1);
   if (body) {
     stream << CB_BODY_HEADER;
   } else {
index bd03282..b10469e 100644 (file)
@@ -504,7 +504,7 @@ std::string CProxyBodyGen::GetMethodReadString(const Interface& inf,
         );
   if (GetReturnTypeString(decl.GetType()) != "void ") {
     str += NLine(1);
-    str += "return ret;" + NLine(1);;
+    str += "return ret;";
   }
   return str;
 }
index 5d014c6..cbaba8b 100644 (file)
@@ -418,7 +418,8 @@ do {
 
     rpc_port_parcel_write(parcel, &handle->parcelable, handle);
     h->delegates = g_list_append(h->delegates, handle);
-} while (0);)__c_cb";
+} while (0);
+)__c_cb";
 
 const char CB_RECEIVE_BLOCK[] =
 R"__c_cb(
index 59e21de..c3edae1 100644 (file)
@@ -71,6 +71,7 @@ void CProxyHeaderGen::GenInterfaceDelegator(std::ofstream& stream,
                                             const Declaration& decl) {
   const char handle[] = "typedef struct $$_s *rpc_port_$$_h;";
 
+  stream << NLine(1);
   stream << GenTemplateString(handle,
       [&]()->std::string {
         return id + "_" + decl.GetID();
@@ -79,6 +80,7 @@ void CProxyHeaderGen::GenInterfaceDelegator(std::ofstream& stream,
         return id + "_" + decl.GetID();
       });
 
+  stream << NLine(1);
   stream << GenTemplateString(CB_INTERFACE_DELEGATOR,
       [&]()->std::string {
         return GetReturnTypeString(decl.GetType());