THRIFT-5168 Useless generated code when .thrift file only has service type
Client: cpp
Patch: zeshuai007 <51382517@qq.com>
This closes #2095
diff --git a/compiler/cpp/src/thrift/generate/t_cpp_generator.cc b/compiler/cpp/src/thrift/generate/t_cpp_generator.cc
index 372014d..c2d88cd 100644
--- a/compiler/cpp/src/thrift/generate/t_cpp_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_cpp_generator.cc
@@ -68,6 +68,7 @@
gen_moveable_ = false;
gen_no_ostream_operators_ = false;
gen_no_skeleton_ = false;
+ has_members_ = false;
for( iter = parsed_options.begin(); iter != parsed_options.end(); ++iter) {
if( iter->first.compare("pure_enums") == 0) {
@@ -358,6 +359,11 @@
bool gen_no_skeleton_;
/**
+ * True if thrift has member(s)
+ */
+ bool has_members_;
+
+ /**
* Strings for namespace, computed once up front then used directly
*/
@@ -505,6 +511,12 @@
f_types_.close();
f_types_impl_.close();
f_types_tcc_.close();
+
+ string f_types_impl_name = get_out_dir() + program_name_ + "_types.cpp";
+
+ if (!has_members_) {
+ remove(f_types_impl_name.c_str());
+ }
}
/**
@@ -693,55 +705,57 @@
void t_cpp_generator::generate_consts(std::vector<t_const*> consts) {
string f_consts_name = get_out_dir() + program_name_ + "_constants.h";
ofstream_with_content_based_conditional_update f_consts;
- f_consts.open(f_consts_name);
+ if (consts.size() > 0) {
+ f_consts.open(f_consts_name);
- string f_consts_impl_name = get_out_dir() + program_name_ + "_constants.cpp";
- ofstream_with_content_based_conditional_update f_consts_impl;
- f_consts_impl.open(f_consts_impl_name);
+ string f_consts_impl_name = get_out_dir() + program_name_ + "_constants.cpp";
+ ofstream_with_content_based_conditional_update f_consts_impl;
+ f_consts_impl.open(f_consts_impl_name);
- // Print header
- f_consts << autogen_comment();
- f_consts_impl << autogen_comment();
+ // Print header
+ f_consts << autogen_comment();
+ f_consts_impl << autogen_comment();
- // Start ifndef
- f_consts << "#ifndef " << program_name_ << "_CONSTANTS_H" << endl << "#define " << program_name_
- << "_CONSTANTS_H" << endl << endl << "#include \"" << get_include_prefix(*get_program())
- << program_name_ << "_types.h\"" << endl << endl << ns_open_ << endl << endl;
+ // Start ifndef
+ f_consts << "#ifndef " << program_name_ << "_CONSTANTS_H" << endl << "#define " << program_name_
+ << "_CONSTANTS_H" << endl << endl << "#include \"" << get_include_prefix(*get_program())
+ << program_name_ << "_types.h\"" << endl << endl << ns_open_ << endl << endl;
- f_consts_impl << "#include \"" << get_include_prefix(*get_program()) << program_name_
- << "_constants.h\"" << endl << endl << ns_open_ << endl << endl;
+ f_consts_impl << "#include \"" << get_include_prefix(*get_program()) << program_name_
+ << "_constants.h\"" << endl << endl << ns_open_ << endl << endl;
- f_consts << "class " << program_name_ << "Constants {" << endl << " public:" << endl << " "
- << program_name_ << "Constants();" << endl << endl;
- indent_up();
- vector<t_const*>::iterator c_iter;
- for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
- string name = (*c_iter)->get_name();
- t_type* type = (*c_iter)->get_type();
- f_consts << indent() << type_name(type) << " " << name << ";" << endl;
+ f_consts << "class " << program_name_ << "Constants {" << endl << " public:" << endl << " "
+ << program_name_ << "Constants();" << endl << endl;
+ indent_up();
+ vector<t_const*>::iterator c_iter;
+ for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
+ string name = (*c_iter)->get_name();
+ t_type* type = (*c_iter)->get_type();
+ f_consts << indent() << type_name(type) << " " << name << ";" << endl;
+ }
+ indent_down();
+ f_consts << "};" << endl;
+
+ f_consts_impl << "const " << program_name_ << "Constants g_" << program_name_ << "_constants;"
+ << endl << endl << program_name_ << "Constants::" << program_name_
+ << "Constants() {" << endl;
+ indent_up();
+ for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
+ print_const_value(f_consts_impl,
+ (*c_iter)->get_name(),
+ (*c_iter)->get_type(),
+ (*c_iter)->get_value());
+ }
+ indent_down();
+ indent(f_consts_impl) << "}" << endl;
+
+ f_consts << endl << "extern const " << program_name_ << "Constants g_" << program_name_
+ << "_constants;" << endl << endl << ns_close_ << endl << endl << "#endif" << endl;
+ f_consts.close();
+
+ f_consts_impl << endl << ns_close_ << endl << endl;
+ f_consts_impl.close();
}
- indent_down();
- f_consts << "};" << endl;
-
- f_consts_impl << "const " << program_name_ << "Constants g_" << program_name_ << "_constants;"
- << endl << endl << program_name_ << "Constants::" << program_name_
- << "Constants() {" << endl;
- indent_up();
- for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
- print_const_value(f_consts_impl,
- (*c_iter)->get_name(),
- (*c_iter)->get_type(),
- (*c_iter)->get_value());
- }
- indent_down();
- indent(f_consts_impl) << "}" << endl;
-
- f_consts << endl << "extern const " << program_name_ << "Constants g_" << program_name_
- << "_constants;" << endl << endl << ns_close_ << endl << endl << "#endif" << endl;
- f_consts.close();
-
- f_consts_impl << endl << ns_close_ << endl << endl;
- f_consts_impl.close();
}
/**
@@ -904,6 +918,8 @@
if (is_exception) {
generate_exception_what_method(f_types_impl_, tstruct);
}
+
+ has_members_ = true;
}
void t_cpp_generator::generate_copy_constructor(ostream& out,
diff --git a/lib/cpp/test/CMakeLists.txt b/lib/cpp/test/CMakeLists.txt
index fba15f6..48e2fd3 100644
--- a/lib/cpp/test/CMakeLists.txt
+++ b/lib/cpp/test/CMakeLists.txt
@@ -42,7 +42,6 @@
gen-cpp/Recursive_types.h
gen-cpp/ThriftTest_types.cpp
gen-cpp/ThriftTest_types.h
- gen-cpp/OneWayTest_types.cpp
gen-cpp/OneWayTest_types.h
gen-cpp/OneWayService.cpp
gen-cpp/OneWayService.h
@@ -392,7 +391,7 @@
COMMAND ${THRIFT_COMPILER} --gen cpp ${PROJECT_SOURCE_DIR}/test/ThriftTest.thrift
)
-add_custom_command(OUTPUT gen-cpp/OneWayService.cpp gen-cpp/OneWayTest_constants.cpp gen-cpp/OneWayTest_types.h gen-cpp/OneWayService.h gen-cpp/OneWayTest_constants.h gen-cpp/OneWayTest_types.cpp
+add_custom_command(OUTPUT gen-cpp/OneWayService.cpp gen-cpp/OneWayTest_types.h gen-cpp/OneWayService.h
COMMAND ${THRIFT_COMPILER} --gen cpp ${CMAKE_CURRENT_SOURCE_DIR}/OneWayTest.thrift
)
diff --git a/lib/cpp/test/Makefile.am b/lib/cpp/test/Makefile.am
index 8399d9e..8982683 100755
--- a/lib/cpp/test/Makefile.am
+++ b/lib/cpp/test/Makefile.am
@@ -30,7 +30,6 @@
gen-cpp/ParentService.h \
gen-cpp/OneWayTest_types.h \
gen-cpp/OneWayService.h \
- gen-cpp/OneWayTest_constants.h \
gen-cpp/proc_types.h
noinst_LTLIBRARIES = libtestgencpp.la libprocessortest.la
@@ -54,11 +53,8 @@
gen-cpp/TypedefTest_types.cpp \
gen-cpp/TypedefTest_types.h \
gen-cpp/OneWayService.cpp \
- gen-cpp/OneWayTest_constants.cpp \
gen-cpp/OneWayTest_types.h \
gen-cpp/OneWayService.h \
- gen-cpp/OneWayTest_constants.h \
- gen-cpp/OneWayTest_types.cpp \
ThriftTest_extras.cpp \
DebugProtoTest_extras.cpp
@@ -414,7 +410,7 @@
gen-cpp/SecondService.cpp gen-cpp/ThriftTest_constants.cpp gen-cpp/ThriftTest.cpp gen-cpp/ThriftTest_types.cpp gen-cpp/ThriftTest_types.h: $(top_srcdir)/test/ThriftTest.thrift
$(THRIFT) --gen cpp $<
-gen-cpp/OneWayService.cpp gen-cpp/OneWayTest_constants.cpp gen-cpp/OneWayTest_types.h gen-cpp/OneWayService.h gen-cpp/OneWayTest_constants.h gen-cpp/OneWayTest_types.cpp: OneWayTest.thrift
+gen-cpp/OneWayService.cpp gen-cpp/OneWayTest_types.h gen-cpp/OneWayService.h: OneWayTest.thrift
$(THRIFT) --gen cpp $<
gen-cpp/ChildService.cpp gen-cpp/ChildService.h gen-cpp/ParentService.cpp gen-cpp/ParentService.h gen-cpp/proc_types.cpp gen-cpp/proc_types.h: processor/proc.thrift
diff --git a/test/cpp/CMakeLists.txt b/test/cpp/CMakeLists.txt
index 90af782..a136a48 100755
--- a/test/cpp/CMakeLists.txt
+++ b/test/cpp/CMakeLists.txt
@@ -52,8 +52,6 @@
set(crossstressgencpp_SOURCES
gen-cpp/Service.cpp
- gen-cpp/StressTest_types.cpp
- gen-cpp/StressTest_constants.cpp
)
add_library(crossstressgencpp STATIC ${crossstressgencpp_SOURCES})
LINK_AGAINST_THRIFT_LIBRARY(crossstressgencpp thrift)
@@ -92,6 +90,6 @@
COMMAND ${THRIFT_COMPILER} --gen cpp:templates,cob_style -r ${PROJECT_SOURCE_DIR}/test/ThriftTest.thrift
)
-add_custom_command(OUTPUT gen-cpp/StressTest_types.cpp gen-cpp/StressTest_constants.cpp gen-cpp/Service.cpp
+add_custom_command(OUTPUT gen-cpp/Service.cpp
COMMAND ${THRIFT_COMPILER} --gen cpp ${PROJECT_SOURCE_DIR}/test/StressTest.thrift
)
diff --git a/test/cpp/Makefile.am b/test/cpp/Makefile.am
index 76ae82b..4f77c15 100755
--- a/test/cpp/Makefile.am
+++ b/test/cpp/Makefile.am
@@ -22,8 +22,6 @@
gen-cpp/ThriftTest_types.cpp \
gen-cpp/ThriftTest_constants.cpp \
gen-cpp/SecondService.cpp \
- gen-cpp/StressTest_types.cpp \
- gen-cpp/StressTest_constants.cpp \
gen-cpp/Service.cpp
noinst_LTLIBRARIES = libtestgencpp.la libstresstestgencpp.la
@@ -44,9 +42,6 @@
libtestgencpp_la_LIBADD = $(top_builddir)/lib/cpp/libthrift.la
nodist_libstresstestgencpp_la_SOURCES = \
- gen-cpp/StressTest_constants.cpp \
- gen-cpp/StressTest_types.cpp \
- gen-cpp/StressTest_constants.h \
gen-cpp/StressTest_types.h \
gen-cpp/Service.cpp \
gen-cpp/Service.h
@@ -105,7 +100,7 @@
gen-cpp/ThriftTest.cpp gen-cpp/ThriftTest_types.cpp gen-cpp/ThriftTest_constants.cpp gen-cpp/SecondService.cpp gen-cpp/SecondService.h gen-cpp/SecondService.tcc: $(top_srcdir)/test/ThriftTest.thrift $(THRIFT)
$(THRIFT) --gen cpp:templates,cob_style -r $<
-gen-cpp/StressTest_types.cpp gen-cpp/StressTest_constants.cpp gen-cpp/Service.cpp: $(top_srcdir)/test/StressTest.thrift $(THRIFT)
+gen-cpp/Service.cpp: $(top_srcdir)/test/StressTest.thrift $(THRIFT)
$(THRIFT) --gen cpp $<
AM_CPPFLAGS = $(BOOST_CPPFLAGS) $(LIBEVENT_CPPFLAGS) -I$(top_srcdir)/lib/cpp/src -Igen-cpp -I.
diff --git a/tutorial/cpp/CMakeLists.txt b/tutorial/cpp/CMakeLists.txt
index c6d8fc3..b250a2c 100644
--- a/tutorial/cpp/CMakeLists.txt
+++ b/tutorial/cpp/CMakeLists.txt
@@ -30,7 +30,6 @@
set(tutorialgencpp_SOURCES
gen-cpp/Calculator.cpp
gen-cpp/SharedService.cpp
- gen-cpp/shared_constants.cpp
gen-cpp/shared_types.cpp
gen-cpp/tutorial_constants.cpp
gen-cpp/tutorial_types.cpp
@@ -38,7 +37,7 @@
add_library(tutorialgencpp STATIC ${tutorialgencpp_SOURCES})
LINK_AGAINST_THRIFT_LIBRARY(tutorialgencpp thrift)
-add_custom_command(OUTPUT gen-cpp/Calculator.cpp gen-cpp/SharedService.cpp gen-cpp/shared_constants.cpp gen-cpp/shared_types.cpp gen-cpp/tutorial_constants.cpp gen-cpp/tutorial_types.cpp
+add_custom_command(OUTPUT gen-cpp/Calculator.cpp gen-cpp/SharedService.cpp gen-cpp/shared_types.cpp gen-cpp/tutorial_constants.cpp gen-cpp/tutorial_types.cpp
COMMAND ${THRIFT_COMPILER} --gen cpp -r ${PROJECT_SOURCE_DIR}/tutorial/tutorial.thrift
)
diff --git a/tutorial/cpp/Makefile.am b/tutorial/cpp/Makefile.am
index 95497d5..77fd6d5 100755
--- a/tutorial/cpp/Makefile.am
+++ b/tutorial/cpp/Makefile.am
@@ -27,8 +27,6 @@
gen-cpp/Calculator.h \
gen-cpp/SharedService.cpp \
gen-cpp/SharedService.h \
- gen-cpp/shared_constants.cpp \
- gen-cpp/shared_constants.h \
gen-cpp/shared_types.cpp \
gen-cpp/shared_types.h \
gen-cpp/tutorial_constants.cpp \
@@ -61,7 +59,7 @@
#
# Common thrift code generation rules
#
-gen-cpp/Calculator.cpp gen-cpp/SharedService.cpp gen-cpp/shared_constants.cpp gen-cpp/shared_types.cpp gen-cpp/tutorial_constants.cpp gen-cpp/tutorial_types.cpp: $(top_srcdir)/tutorial/tutorial.thrift
+gen-cpp/Calculator.cpp gen-cpp/SharedService.cpp gen-cpp/shared_types.cpp gen-cpp/tutorial_constants.cpp gen-cpp/tutorial_types.cpp: $(top_srcdir)/tutorial/tutorial.thrift
$(THRIFT) --gen cpp -r $<
AM_CPPFLAGS = $(BOOST_CPPFLAGS) $(LIBEVENT_CPPFLAGS) -I$(top_srcdir)/lib/cpp/src -Igen-cpp