From 061d4a2cb460cc7afebea3c5f1d20382b8454090 Mon Sep 17 00:00:00 2001 From: Roger Meier Date: Sun, 7 Oct 2012 11:51:00 +0000 Subject: [PATCH] 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 --- .../cpp/src/generate/t_c_glib_generator.cc | 2 - compiler/cpp/src/generate/t_cpp_generator.cc | 83 ++++++++++--------- compiler/cpp/src/generate/t_js_generator.cc | 4 - compiler/cpp/src/main.cc | 35 ++++++-- compiler/cpp/src/platform.h | 1 + 5 files changed, 71 insertions(+), 54 deletions(-) diff --git a/compiler/cpp/src/generate/t_c_glib_generator.cc b/compiler/cpp/src/generate/t_c_glib_generator.cc index b088299c..bdee1bf9 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 @@ void t_c_glib_generator::generate_object(t_struct *tstruct) { " /* public */" << endl; // for each field, add a member variable - bool has_nonrequired_fields = false; vector::const_iterator m_iter; const vector &members = tstruct->get_members(); for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { @@ -1670,7 +1669,6 @@ void t_c_glib_generator::generate_object(t_struct *tstruct) { 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 693be9c9..9b960bf4 100644 --- a/compiler/cpp/src/generate/t_cpp_generator.cc +++ b/compiler/cpp/src/generate/t_cpp_generator.cc @@ -1263,51 +1263,56 @@ void t_cpp_generator::generate_struct_reader(ofstream& out, 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 @@ void t_cpp_generator::generate_struct_reader(ofstream& out, 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 a6036476..2776cd42 100644 --- a/compiler/cpp/src/generate/t_js_generator.cc +++ b/compiler/cpp/src/generate/t_js_generator.cc @@ -434,10 +434,6 @@ string t_js_generator::render_const_value(t_type* type, t_const_value* value) { 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 406c094d..3f085aea 100644 --- a/compiler/cpp/src/main.cc +++ b/compiler/cpp/src/main.cc @@ -217,6 +217,31 @@ char *saferealpath(const char *path, char *resolved_path) { #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 @@ int main(int argc, char** argv) { 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 bd97f68e..04f04c70 100644 --- a/compiler/cpp/src/platform.h +++ b/compiler/cpp/src/platform.h @@ -23,6 +23,7 @@ */ #ifdef MINGW +#include #include #else #include -- 2.17.1