THRIFT-1695 allow warning-free compilation in VS 2012 and GNU 4.6
Patch: Ben Craig & James K Lowden


git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1395277 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/cpp/src/generate/t_c_glib_generator.cc b/compiler/cpp/src/generate/t_c_glib_generator.cc
index b088299..bdee1bf 100644
--- a/compiler/cpp/src/generate/t_c_glib_generator.cc
+++ b/compiler/cpp/src/generate/t_c_glib_generator.cc
@@ -1662,7 +1662,6 @@
     "  /* public */" << endl;
 
   // for each field, add a member variable
-  bool has_nonrequired_fields = false;
   vector<t_field *>::const_iterator m_iter;
   const vector<t_field *> &members = tstruct->get_members();
   for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
@@ -1670,7 +1669,6 @@
     f_types_ <<
       "  " << type_name (t) << " " << (*m_iter)->get_name() << ";" << endl;
     if ((*m_iter)->get_req() != t_field::T_REQUIRED) {
-      has_nonrequired_fields = true;
       f_types_ <<
         "  gboolean __isset_" << (*m_iter)->get_name() << ";" << endl;
     }
diff --git a/compiler/cpp/src/generate/t_cpp_generator.cc b/compiler/cpp/src/generate/t_cpp_generator.cc
index 693be9c..9b960bf 100644
--- a/compiler/cpp/src/generate/t_cpp_generator.cc
+++ b/compiler/cpp/src/generate/t_cpp_generator.cc
@@ -1263,51 +1263,56 @@
       indent() << "  break;" << endl <<
       indent() << "}" << endl;
 
-    // Switch statement on the field we are reading
-    indent(out) <<
-      "switch (fid)" << endl;
+    if(fields.empty()) {
+      out <<
+        indent() << "xfer += iprot->skip(ftype);" << endl;
+    }
+    else {
+      // Switch statement on the field we are reading
+      indent(out) <<
+        "switch (fid)" << endl;
 
-      scope_up(out);
+        scope_up(out);
 
-      // Generate deserialization code for known cases
-      for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
-        indent(out) <<
-          "case " << (*f_iter)->get_key() << ":" << endl;
-        indent_up();
-        indent(out) <<
-          "if (ftype == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl;
-        indent_up();
+        // Generate deserialization code for known cases
+        for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+          indent(out) <<
+            "case " << (*f_iter)->get_key() << ":" << endl;
+          indent_up();
+          indent(out) <<
+            "if (ftype == " << type_to_enum((*f_iter)->get_type()) << ") {" << endl;
+          indent_up();
 
-        const char *isset_prefix =
-          ((*f_iter)->get_req() != t_field::T_REQUIRED) ? "this->__isset." : "isset_";
+          const char *isset_prefix =
+            ((*f_iter)->get_req() != t_field::T_REQUIRED) ? "this->__isset." : "isset_";
 
 #if 0
-        // This code throws an exception if the same field is encountered twice.
-        // We've decided to leave it out for performance reasons.
-        // TODO(dreiss): Generate this code and "if" it out to make it easier
-        // for people recompiling thrift to include it.
-        out <<
-          indent() << "if (" << isset_prefix << (*f_iter)->get_name() << ")" << endl <<
-          indent() << "  throw TProtocolException(TProtocolException::INVALID_DATA);" << endl;
+          // This code throws an exception if the same field is encountered twice.
+          // We've decided to leave it out for performance reasons.
+          // TODO(dreiss): Generate this code and "if" it out to make it easier
+          // for people recompiling thrift to include it.
+          out <<
+            indent() << "if (" << isset_prefix << (*f_iter)->get_name() << ")" << endl <<
+            indent() << "  throw TProtocolException(TProtocolException::INVALID_DATA);" << endl;
 #endif
 
-        if (pointers && !(*f_iter)->get_type()->is_xception()) {
-          generate_deserialize_field(out, *f_iter, "(*(this->", "))");
-        } else {
-          generate_deserialize_field(out, *f_iter, "this->");
-        }
-        out <<
-          indent() << isset_prefix << (*f_iter)->get_name() << " = true;" << endl;
-        indent_down();
-        out <<
-          indent() << "} else {" << endl <<
-          indent() << "  xfer += iprot->skip(ftype);" << endl <<
-          // TODO(dreiss): Make this an option when thrift structs
-          // have a common base class.
-          // indent() << "  throw TProtocolException(TProtocolException::INVALID_DATA);" << endl <<
-          indent() << "}" << endl <<
-          indent() << "break;" << endl;
-        indent_down();
+          if (pointers && !(*f_iter)->get_type()->is_xception()) {
+            generate_deserialize_field(out, *f_iter, "(*(this->", "))");
+          } else {
+            generate_deserialize_field(out, *f_iter, "this->");
+          }
+          out <<
+            indent() << isset_prefix << (*f_iter)->get_name() << " = true;" << endl;
+          indent_down();
+          out <<
+            indent() << "} else {" << endl <<
+            indent() << "  xfer += iprot->skip(ftype);" << endl <<
+            // TODO(dreiss): Make this an option when thrift structs
+            // have a common base class.
+            // indent() << "  throw TProtocolException(TProtocolException::INVALID_DATA);" << endl <<
+            indent() << "}" << endl <<
+            indent() << "break;" << endl;
+          indent_down();
       }
 
       // In the default case we skip the field
@@ -1317,7 +1322,7 @@
         indent() << "  break;" << endl;
 
       scope_down(out);
-
+    } //!fields.empty()
     // Read field end marker
     indent(out) <<
       "xfer += iprot->readFieldEnd();" << endl;
diff --git a/compiler/cpp/src/generate/t_js_generator.cc b/compiler/cpp/src/generate/t_js_generator.cc
index a603647..2776cd4 100644
--- a/compiler/cpp/src/generate/t_js_generator.cc
+++ b/compiler/cpp/src/generate/t_js_generator.cc
@@ -434,10 +434,6 @@
     out << "})";
   } else if (type->is_map()) {
     t_type* ktype = ((t_map*)type)->get_key_type();
-    bool    key_is_string = false;
-    if (ktype->is_base_type() && ((t_base_type*)ktype)->get_base() == t_base_type::TYPE_STRING){
-        key_is_string = true;
-    }
 
     t_type* vtype = ((t_map*)type)->get_val_type();
     out << "{";
diff --git a/compiler/cpp/src/main.cc b/compiler/cpp/src/main.cc
index 406c094..3f085ae 100644
--- a/compiler/cpp/src/main.cc
+++ b/compiler/cpp/src/main.cc
@@ -217,6 +217,31 @@
 #endif
 }
 
+bool check_is_directory(const char *dir_name) {
+#ifdef MINGW
+  DWORD attributes = ::GetFileAttributesA(dir_name);
+  if(attributes == INVALID_FILE_ATTRIBUTES) {
+    fprintf(stderr, "Output directory %s is unusable: GetLastError() = %ld\n", dir_name, GetLastError());
+    return false;
+  }
+  if((attributes & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY) {
+    fprintf(stderr, "Output directory %s exists but is not a directory\n", dir_name);
+    return false;
+  }
+  return true;
+#else
+  struct stat sb;
+  if (stat(dir_name, &sb) < 0) {
+    fprintf(stderr, "Output directory %s is unusable: %s\n", dir_name, strerror(errno));
+    return false;
+  }
+  if (! S_ISDIR(sb.st_mode)) {
+    fprintf(stderr, "Output directory %s exists but is not a directory\n", dir_name);
+    return false;
+  }
+  return true;
+#endif
+}
 
 /**
  * Report an error to the user. This is called yyerror for historical
@@ -1088,16 +1113,8 @@
           out_path.erase(last);
         }
 #endif
-
-        struct stat sb;
-        if (stat(out_path.c_str(), &sb) < 0) {
-          fprintf(stderr, "Output directory %s is unusable: %s\n", out_path.c_str(), strerror(errno));
+        if (!check_is_directory(out_path.c_str()))
           return -1;
-        }
-        if (! S_ISDIR(sb.st_mode)) {
-          fprintf(stderr, "Output directory %s exists but is not a directory\n", out_path.c_str());
-          return -1;
-        }
       } else {
         fprintf(stderr, "Unrecognized option: %s\n", arg);
         usage();
diff --git a/compiler/cpp/src/platform.h b/compiler/cpp/src/platform.h
index bd97f68..04f04c7 100644
--- a/compiler/cpp/src/platform.h
+++ b/compiler/cpp/src/platform.h
@@ -23,6 +23,7 @@
  */
 
 #ifdef MINGW
+#include <direct.h>
 #include <io.h>
 #else
 #include <sys/types.h>