From 22a8088a98aba78409359f1a161401254957310e Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Mon, 23 Jun 2025 19:30:04 +0100 Subject: [PATCH] Fix -Wunused-variable warning in Lua and Octave wrappers Add bool output parameter to Swig_overload_dispatch to say when it has generated code that will use the typecheck typemap code to help Lua and Octave to not emit unused argv[] arrays. Alas, resorted to warning suppression for deficient cpp11_initializer_list typecheck typemap in cpp11_std_initializer_list testcase. --- .../test-suite/cpp11_attribute_specifiers.i | 1 - Examples/test-suite/cpp11_initializer_list.i | 13 ++++++++++++ Source/Modules/guile.cxx | 3 ++- Source/Modules/lua.cxx | 15 ++++++++------ Source/Modules/ocaml.cxx | 5 ++--- Source/Modules/octave.cxx | 5 +++-- Source/Modules/overload.cxx | 20 ++++++++++++------- Source/Modules/perl5.cxx | 3 ++- Source/Modules/php.cxx | 3 ++- Source/Modules/python.cxx | 5 +++-- Source/Modules/ruby.cxx | 3 ++- Source/Modules/scilab.cxx | 3 ++- Source/Modules/swigmod.h | 4 ++-- Source/Modules/tcl8.cxx | 3 ++- 14 files changed, 57 insertions(+), 29 deletions(-) diff --git a/Examples/test-suite/cpp11_attribute_specifiers.i b/Examples/test-suite/cpp11_attribute_specifiers.i index ee5f6ad08..9d0f21f53 100644 --- a/Examples/test-suite/cpp11_attribute_specifiers.i +++ b/Examples/test-suite/cpp11_attribute_specifiers.i @@ -12,7 +12,6 @@ #endif #if defined(__clang__) -#pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" #pragma clang diagnostic ignored "-Wattributes" #pragma clang diagnostic ignored "-Wunused-variable" diff --git a/Examples/test-suite/cpp11_initializer_list.i b/Examples/test-suite/cpp11_initializer_list.i index 7e86cc025..7091ff821 100644 --- a/Examples/test-suite/cpp11_initializer_list.i +++ b/Examples/test-suite/cpp11_initializer_list.i @@ -18,6 +18,19 @@ #endif %} +#if defined(SWIGLUA) || defined(SWIGOCTAVE) +%{ +// The empty typecheck typemap for std::initializer_list results in $input not being used and a resulting unused variable warning +#if defined(__GNUC__) +#pragma GCC diagnostic ignored "-Wunused-variable" +#elif defined(__clang__) +#pragma clang diagnostic ignored "-Wunused-variable" +#elif defined(_MSC_VER) +#pragma warning(disable : 4990) +#endif +%} +#endif + %inline %{ #include #include diff --git a/Source/Modules/guile.cxx b/Source/Modules/guile.cxx index cb3517cba..3d0a49ade 100644 --- a/Source/Modules/guile.cxx +++ b/Source/Modules/guile.cxx @@ -951,7 +951,8 @@ public: /* Emit overloading dispatch function */ int maxargs; - String *dispatch = Swig_overload_dispatch(n, "return %s(argc,argv);", &maxargs); + bool check_emitted = false; + String *dispatch = Swig_overload_dispatch(n, "return %s(argc,argv);", &maxargs, &check_emitted); /* Generate a dispatch wrapper for all overloaded functions */ diff --git a/Source/Modules/lua.cxx b/Source/Modules/lua.cxx index 10e24e01f..83740b263 100644 --- a/Source/Modules/lua.cxx +++ b/Source/Modules/lua.cxx @@ -839,8 +839,9 @@ public: /* Last node in overloaded chain */ int maxargs; + bool check_emitted = false; String *tmp = NewString(""); - String *dispatch = Swig_overload_dispatch(n, "return %s(L);", &maxargs); + String *dispatch = Swig_overload_dispatch(n, "return %s(L);", &maxargs, &check_emitted); /* Generate a dispatch wrapper for all overloaded functions */ @@ -861,12 +862,14 @@ public: Printv(f->def, "static int ", wname, "(lua_State* L) {", NIL); Wrapper_add_local(f, "argc", "int argc"); - Printf(tmp, "int argv[%d]={1", maxargs + 1); - for (int i = 1; i <= maxargs; i++) { - Printf(tmp, ",%d", i + 1); + if (maxargs > 0 && check_emitted) { + Printf(tmp, "int argv[%d]={1", maxargs + 1); + for (int i = 1; i <= maxargs; i++) { + Printf(tmp, ",%d", i + 1); + } + Printf(tmp, "}"); + Wrapper_add_local(f, "argv", tmp); } - Printf(tmp, "}"); - Wrapper_add_local(f, "argv", tmp); Printf(f->code, "argc = lua_gettop(L);\n"); Replaceall(dispatch, "$args", "self,args"); diff --git a/Source/Modules/ocaml.cxx b/Source/Modules/ocaml.cxx index ce80eb0bb..e9f64175a 100644 --- a/Source/Modules/ocaml.cxx +++ b/Source/Modules/ocaml.cxx @@ -703,10 +703,9 @@ public: if (isOverloaded) { if (!Getattr(n, "sym:nextSibling")) { int maxargs; + bool check_emitted = false; Wrapper *df = NewWrapper(); - String *dispatch = Swig_overload_dispatch(n, - "free(argv);\n" "CAMLreturn(%s(args));\n", - &maxargs); + String *dispatch = Swig_overload_dispatch(n, "free(argv);\n" "CAMLreturn(%s(args));\n", &maxargs, &check_emitted); Wrapper_add_local(df, "argv", "value *argv"); diff --git a/Source/Modules/octave.cxx b/Source/Modules/octave.cxx index 9d8c0f11c..026ec11bd 100644 --- a/Source/Modules/octave.cxx +++ b/Source/Modules/octave.cxx @@ -796,12 +796,13 @@ public: String *iname = Getattr(n, "sym:name"); String *wname = Swig_name_wrapper(iname); int maxargs; - String *dispatch = Swig_overload_dispatch(n, "return %s(args, nargout);", &maxargs); + bool check_emitted = false; + String *dispatch = Swig_overload_dispatch(n, "return %s(args, nargout);", &maxargs, &check_emitted); String *tmp = NewString(""); Octave_begin_function(n, f->def, iname, wname, true); Wrapper_add_local(f, "argc", "int argc = args.length()"); - if (maxargs > 0) { + if (maxargs > 0 && check_emitted) { Printf(tmp, "octave_value_ref argv[%d]={", maxargs); for (int j = 0; j < maxargs; ++j) Printf(tmp, "%soctave_value_ref(args,%d)", j ? "," : " ", j); diff --git a/Source/Modules/overload.cxx b/Source/Modules/overload.cxx index 0d02a76b3..0b37ea9be 100644 --- a/Source/Modules/overload.cxx +++ b/Source/Modules/overload.cxx @@ -360,9 +360,9 @@ List *Swig_overload_rank(Node *n, bool script_lang_wrapping) { return result; } -// /* ----------------------------------------------------------------------------- -// * print_typecheck() -// * ----------------------------------------------------------------------------- */ +/* ----------------------------------------------------------------------------- + * print_typecheck() + * ----------------------------------------------------------------------------- */ static bool print_typecheck(String *f, int j, Parm *pj, bool implicitconvtypecheckoff) { char tmp[256]; @@ -425,10 +425,11 @@ static String *ReplaceFormat(const_String_or_char_ptr fmt, int j) { /* Cast dispatch mechanism. */ -String *Swig_overload_dispatch_cast(Node *n, const_String_or_char_ptr fmt, int *maxargs) { +String *Swig_overload_dispatch_cast(Node *n, const_String_or_char_ptr fmt, int *maxargs, bool *check_emitted) { int i, j; *maxargs = 0; + *check_emitted = false; String *f = NewString(""); String *sw = NewString(""); @@ -533,6 +534,7 @@ String *Swig_overload_dispatch_cast(Node *n, const_String_or_char_ptr fmt, int * } if (emitcheck) { + *check_emitted = true; if (need_v) { Printf(f, "int _v = 0;\n"); need_v = 0; @@ -612,10 +614,11 @@ String *Swig_overload_dispatch_cast(Node *n, const_String_or_char_ptr fmt, int * /* Fast dispatch mechanism, provided by Salvador Fandi~no Garc'ia (#930586). */ -static String *overload_dispatch_fast(Node *n, const_String_or_char_ptr fmt, int *maxargs, const_String_or_char_ptr fmt_fastdispatch) { +static String *overload_dispatch_fast(Node *n, const_String_or_char_ptr fmt, int *maxargs, bool *check_emitted, const_String_or_char_ptr fmt_fastdispatch) { int i, j; *maxargs = 0; + *check_emitted = false; String *f = NewString(""); @@ -713,6 +716,7 @@ static String *overload_dispatch_fast(Node *n, const_String_or_char_ptr fmt, int } if (emitcheck) { + *check_emitted = true; if (need_v) { Printf(f, "int _v = 0;\n"); need_v = 0; @@ -775,15 +779,16 @@ static String *overload_dispatch_fast(Node *n, const_String_or_char_ptr fmt, int return f; } -String *Swig_overload_dispatch(Node *n, const_String_or_char_ptr fmt, int *maxargs, const_String_or_char_ptr fmt_fastdispatch) { +String *Swig_overload_dispatch(Node *n, const_String_or_char_ptr fmt, int *maxargs, bool *check_emitted, const_String_or_char_ptr fmt_fastdispatch) { if (fast_dispatch_mode || GetFlag(n, "feature:fastdispatch")) { - return overload_dispatch_fast(n, fmt, maxargs, fmt_fastdispatch); + return overload_dispatch_fast(n, fmt, maxargs, check_emitted, fmt_fastdispatch); } int i, j; *maxargs = 0; + *check_emitted = false; String *f = NewString(""); @@ -824,6 +829,7 @@ String *Swig_overload_dispatch(Node *n, const_String_or_char_ptr fmt, int *maxar pj = Getattr(pj, "tmap:in:next"); continue; } + *check_emitted = true; if (j >= num_required) { String *lfmt = ReplaceFormat(fmt, num_arguments); Printf(f, "if (%s <= %d) {\n", argc_template_string, j); diff --git a/Source/Modules/perl5.cxx b/Source/Modules/perl5.cxx index b1e813599..dfb12f37a 100644 --- a/Source/Modules/perl5.cxx +++ b/Source/Modules/perl5.cxx @@ -908,7 +908,8 @@ public: } else if (!Getattr(n, "sym:nextSibling")) { /* Generate overloaded dispatch function */ int maxargs; - String *dispatch = Swig_overload_dispatch_cast(n, "PUSHMARK(MARK); SWIG_CALLXS(%s); return;", &maxargs); + bool check_emitted = false; + String *dispatch = Swig_overload_dispatch_cast(n, "PUSHMARK(MARK); SWIG_CALLXS(%s); return;", &maxargs, &check_emitted); /* Generate a dispatch wrapper for all overloaded functions */ diff --git a/Source/Modules/php.cxx b/Source/Modules/php.cxx index d676df1c4..d0f3f2e79 100644 --- a/Source/Modules/php.cxx +++ b/Source/Modules/php.cxx @@ -1004,8 +1004,9 @@ public: /* Last node in overloaded chain */ int maxargs; + bool check_emitted = false; String *tmp = NewStringEmpty(); - String *dispatch = Swig_overload_dispatch(n, "%s(INTERNAL_FUNCTION_PARAM_PASSTHRU); return;", &maxargs); + String *dispatch = Swig_overload_dispatch(n, "%s(INTERNAL_FUNCTION_PARAM_PASSTHRU); return;", &maxargs, &check_emitted); /* Generate a dispatch wrapper for all overloaded functions */ diff --git a/Source/Modules/python.cxx b/Source/Modules/python.cxx index 21aefeea8..e453ce489 100644 --- a/Source/Modules/python.cxx +++ b/Source/Modules/python.cxx @@ -2580,6 +2580,7 @@ public: bool add_self = builtin_self && (!builtin_ctor || director_class); int maxargs; + bool check_emitted = false; String *tmp = NewString(""); String *dispatch; @@ -2588,7 +2589,7 @@ public: String *dispatch_code = NewStringf("return %s", dispatch_call); if (castmode) { - dispatch = Swig_overload_dispatch_cast(n, dispatch_code, &maxargs); + dispatch = Swig_overload_dispatch_cast(n, dispatch_code, &maxargs, &check_emitted); } else { String *fastdispatch_code; if (builtin_ctor) @@ -2599,7 +2600,7 @@ public: Insert(fastdispatch_code, 0, "{\n"); Append(fastdispatch_code, "\n}"); } - dispatch = Swig_overload_dispatch(n, dispatch_code, &maxargs, fastdispatch_code); + dispatch = Swig_overload_dispatch(n, dispatch_code, &maxargs, &check_emitted, fastdispatch_code); Delete(fastdispatch_code); } diff --git a/Source/Modules/ruby.cxx b/Source/Modules/ruby.cxx index ec9b73754..41d1a0ed5 100644 --- a/Source/Modules/ruby.cxx +++ b/Source/Modules/ruby.cxx @@ -2043,8 +2043,9 @@ public: /* Last node in overloaded chain */ int maxargs; + bool check_emitted = false; String *tmp = NewString(""); - String *dispatch = Swig_overload_dispatch(n, "return %s(nargs, args, self);", &maxargs); + String *dispatch = Swig_overload_dispatch(n, "return %s(nargs, args, self);", &maxargs, &check_emitted); /* Generate a dispatch wrapper for all overloaded functions */ diff --git a/Source/Modules/scilab.cxx b/Source/Modules/scilab.cxx index 8a0af8928..d227891ea 100644 --- a/Source/Modules/scilab.cxx +++ b/Source/Modules/scilab.cxx @@ -571,9 +571,10 @@ public: String *functionName = Getattr(node, "sym:name"); String *wrapperName = Swig_name_wrapper(functionName); int maxargs = 0; + bool check_emitted = false; /* Generate the dispatch function */ - String *dispatch = Swig_overload_dispatch(node, "return %s(SWIG_GatewayArguments);", &maxargs); + String *dispatch = Swig_overload_dispatch(node, "return %s(SWIG_GatewayArguments);", &maxargs, &check_emitted); String *tmp = NewString(""); Printv(wrapper->def, "SWIGEXPORT int ", wrapperName, "(SWIG_GatewayParameters) {\n", NIL); diff --git a/Source/Modules/swigmod.h b/Source/Modules/swigmod.h index ea3503e02..ee55acc3e 100644 --- a/Source/Modules/swigmod.h +++ b/Source/Modules/swigmod.h @@ -388,8 +388,8 @@ void emit_mark_varargs(ParmList *l); String *emit_action(Node *n); int emit_action_code(Node *n, String *wrappercode, String *action); void Swig_overload_check(Node *n); -String *Swig_overload_dispatch(Node *n, const_String_or_char_ptr fmt, int *, const_String_or_char_ptr fmt_fastdispatch = 0); -String *Swig_overload_dispatch_cast(Node *n, const_String_or_char_ptr fmt, int *); +String *Swig_overload_dispatch(Node *n, const_String_or_char_ptr fmt, int *maxargs, bool *check_emitted, const_String_or_char_ptr fmt_fastdispatch = 0); +String *Swig_overload_dispatch_cast(Node *n, const_String_or_char_ptr fmt, int *maxargs, bool *check_emitted); List *Swig_overload_rank(Node *n, bool script_lang_wrapping); SwigType *cplus_value_type(SwigType *t); diff --git a/Source/Modules/tcl8.cxx b/Source/Modules/tcl8.cxx index 63c9ae20e..fe0711135 100644 --- a/Source/Modules/tcl8.cxx +++ b/Source/Modules/tcl8.cxx @@ -500,7 +500,8 @@ public: /* Emit overloading dispatch function */ int maxargs; - String *dispatch = Swig_overload_dispatch(n, "return %s(clientData, interp, objc, argv - 1);", &maxargs); + bool check_emitted = false; + String *dispatch = Swig_overload_dispatch(n, "return %s(clientData, interp, objc, argv - 1);", &maxargs, &check_emitted); /* Generate a dispatch wrapper for all overloaded functions */