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.
This commit is contained in:
William S Fulton 2025-06-23 19:30:04 +01:00
parent 4bf9547982
commit 22a8088a98
14 changed files with 57 additions and 29 deletions

View File

@ -12,7 +12,6 @@
#endif #endif
#if defined(__clang__) #if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations" #pragma clang diagnostic ignored "-Wdeprecated-declarations"
#pragma clang diagnostic ignored "-Wattributes" #pragma clang diagnostic ignored "-Wattributes"
#pragma clang diagnostic ignored "-Wunused-variable" #pragma clang diagnostic ignored "-Wunused-variable"

View File

@ -18,6 +18,19 @@
#endif #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 %{ %inline %{
#include <initializer_list> #include <initializer_list>
#include <string> #include <string>

View File

@ -951,7 +951,8 @@ public:
/* Emit overloading dispatch function */ /* Emit overloading dispatch function */
int maxargs; 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 */ /* Generate a dispatch wrapper for all overloaded functions */

View File

@ -839,8 +839,9 @@ public:
/* Last node in overloaded chain */ /* Last node in overloaded chain */
int maxargs; int maxargs;
bool check_emitted = false;
String *tmp = NewString(""); 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 */ /* Generate a dispatch wrapper for all overloaded functions */
@ -861,12 +862,14 @@ public:
Printv(f->def, "static int ", wname, "(lua_State* L) {", NIL); Printv(f->def, "static int ", wname, "(lua_State* L) {", NIL);
Wrapper_add_local(f, "argc", "int argc"); Wrapper_add_local(f, "argc", "int argc");
Printf(tmp, "int argv[%d]={1", maxargs + 1); if (maxargs > 0 && check_emitted) {
for (int i = 1; i <= maxargs; i++) { Printf(tmp, "int argv[%d]={1", maxargs + 1);
Printf(tmp, ",%d", i + 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"); Printf(f->code, "argc = lua_gettop(L);\n");
Replaceall(dispatch, "$args", "self,args"); Replaceall(dispatch, "$args", "self,args");

View File

@ -703,10 +703,9 @@ public:
if (isOverloaded) { if (isOverloaded) {
if (!Getattr(n, "sym:nextSibling")) { if (!Getattr(n, "sym:nextSibling")) {
int maxargs; int maxargs;
bool check_emitted = false;
Wrapper *df = NewWrapper(); Wrapper *df = NewWrapper();
String *dispatch = Swig_overload_dispatch(n, String *dispatch = Swig_overload_dispatch(n, "free(argv);\n" "CAMLreturn(%s(args));\n", &maxargs, &check_emitted);
"free(argv);\n" "CAMLreturn(%s(args));\n",
&maxargs);
Wrapper_add_local(df, "argv", "value *argv"); Wrapper_add_local(df, "argv", "value *argv");

View File

@ -796,12 +796,13 @@ public:
String *iname = Getattr(n, "sym:name"); String *iname = Getattr(n, "sym:name");
String *wname = Swig_name_wrapper(iname); String *wname = Swig_name_wrapper(iname);
int maxargs; 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(""); String *tmp = NewString("");
Octave_begin_function(n, f->def, iname, wname, true); Octave_begin_function(n, f->def, iname, wname, true);
Wrapper_add_local(f, "argc", "int argc = args.length()"); 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); Printf(tmp, "octave_value_ref argv[%d]={", maxargs);
for (int j = 0; j < maxargs; ++j) for (int j = 0; j < maxargs; ++j)
Printf(tmp, "%soctave_value_ref(args,%d)", j ? "," : " ", j); Printf(tmp, "%soctave_value_ref(args,%d)", j ? "," : " ", j);

View File

@ -360,9 +360,9 @@ List *Swig_overload_rank(Node *n, bool script_lang_wrapping) {
return result; return result;
} }
// /* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
// * print_typecheck() * print_typecheck()
// * ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
static bool print_typecheck(String *f, int j, Parm *pj, bool implicitconvtypecheckoff) { static bool print_typecheck(String *f, int j, Parm *pj, bool implicitconvtypecheckoff) {
char tmp[256]; char tmp[256];
@ -425,10 +425,11 @@ static String *ReplaceFormat(const_String_or_char_ptr fmt, int j) {
/* /*
Cast dispatch mechanism. 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; int i, j;
*maxargs = 0; *maxargs = 0;
*check_emitted = false;
String *f = NewString(""); String *f = NewString("");
String *sw = NewString(""); String *sw = NewString("");
@ -533,6 +534,7 @@ String *Swig_overload_dispatch_cast(Node *n, const_String_or_char_ptr fmt, int *
} }
if (emitcheck) { if (emitcheck) {
*check_emitted = true;
if (need_v) { if (need_v) {
Printf(f, "int _v = 0;\n"); Printf(f, "int _v = 0;\n");
need_v = 0; 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). 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; int i, j;
*maxargs = 0; *maxargs = 0;
*check_emitted = false;
String *f = NewString(""); String *f = NewString("");
@ -713,6 +716,7 @@ static String *overload_dispatch_fast(Node *n, const_String_or_char_ptr fmt, int
} }
if (emitcheck) { if (emitcheck) {
*check_emitted = true;
if (need_v) { if (need_v) {
Printf(f, "int _v = 0;\n"); Printf(f, "int _v = 0;\n");
need_v = 0; need_v = 0;
@ -775,15 +779,16 @@ static String *overload_dispatch_fast(Node *n, const_String_or_char_ptr fmt, int
return f; 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")) { 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; int i, j;
*maxargs = 0; *maxargs = 0;
*check_emitted = false;
String *f = NewString(""); 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"); pj = Getattr(pj, "tmap:in:next");
continue; continue;
} }
*check_emitted = true;
if (j >= num_required) { if (j >= num_required) {
String *lfmt = ReplaceFormat(fmt, num_arguments); String *lfmt = ReplaceFormat(fmt, num_arguments);
Printf(f, "if (%s <= %d) {\n", argc_template_string, j); Printf(f, "if (%s <= %d) {\n", argc_template_string, j);

View File

@ -908,7 +908,8 @@ public:
} else if (!Getattr(n, "sym:nextSibling")) { } else if (!Getattr(n, "sym:nextSibling")) {
/* Generate overloaded dispatch function */ /* Generate overloaded dispatch function */
int maxargs; 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 */ /* Generate a dispatch wrapper for all overloaded functions */

View File

@ -1004,8 +1004,9 @@ public:
/* Last node in overloaded chain */ /* Last node in overloaded chain */
int maxargs; int maxargs;
bool check_emitted = false;
String *tmp = NewStringEmpty(); 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 */ /* Generate a dispatch wrapper for all overloaded functions */

View File

@ -2580,6 +2580,7 @@ public:
bool add_self = builtin_self && (!builtin_ctor || director_class); bool add_self = builtin_self && (!builtin_ctor || director_class);
int maxargs; int maxargs;
bool check_emitted = false;
String *tmp = NewString(""); String *tmp = NewString("");
String *dispatch; String *dispatch;
@ -2588,7 +2589,7 @@ public:
String *dispatch_code = NewStringf("return %s", dispatch_call); String *dispatch_code = NewStringf("return %s", dispatch_call);
if (castmode) { if (castmode) {
dispatch = Swig_overload_dispatch_cast(n, dispatch_code, &maxargs); dispatch = Swig_overload_dispatch_cast(n, dispatch_code, &maxargs, &check_emitted);
} else { } else {
String *fastdispatch_code; String *fastdispatch_code;
if (builtin_ctor) if (builtin_ctor)
@ -2599,7 +2600,7 @@ public:
Insert(fastdispatch_code, 0, "{\n"); Insert(fastdispatch_code, 0, "{\n");
Append(fastdispatch_code, "\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); Delete(fastdispatch_code);
} }

View File

@ -2043,8 +2043,9 @@ public:
/* Last node in overloaded chain */ /* Last node in overloaded chain */
int maxargs; int maxargs;
bool check_emitted = false;
String *tmp = NewString(""); 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 */ /* Generate a dispatch wrapper for all overloaded functions */

View File

@ -571,9 +571,10 @@ public:
String *functionName = Getattr(node, "sym:name"); String *functionName = Getattr(node, "sym:name");
String *wrapperName = Swig_name_wrapper(functionName); String *wrapperName = Swig_name_wrapper(functionName);
int maxargs = 0; int maxargs = 0;
bool check_emitted = false;
/* Generate the dispatch function */ /* 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(""); String *tmp = NewString("");
Printv(wrapper->def, "SWIGEXPORT int ", wrapperName, "(SWIG_GatewayParameters) {\n", NIL); Printv(wrapper->def, "SWIGEXPORT int ", wrapperName, "(SWIG_GatewayParameters) {\n", NIL);

View File

@ -388,8 +388,8 @@ void emit_mark_varargs(ParmList *l);
String *emit_action(Node *n); String *emit_action(Node *n);
int emit_action_code(Node *n, String *wrappercode, String *action); int emit_action_code(Node *n, String *wrappercode, String *action);
void Swig_overload_check(Node *n); 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(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 *); 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); List *Swig_overload_rank(Node *n, bool script_lang_wrapping);
SwigType *cplus_value_type(SwigType *t); SwigType *cplus_value_type(SwigType *t);

View File

@ -500,7 +500,8 @@ public:
/* Emit overloading dispatch function */ /* Emit overloading dispatch function */
int maxargs; 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 */ /* Generate a dispatch wrapper for all overloaded functions */