XS tips and tricks, unused variables



for those of you developing wrappers this may be of interest:

for class static functions, that accept an SV * class as the first paramter,
like the following:

gboolean
some_class_static_function (class, param1, param2)
        SV  * class
        int   param1
        int   param2
    C_ARGS:
        param1, param2

can be re-written as this illistraites to prevent both the unused paramter
warning and the unnecessary stack operation.

gboolean
some_class_static_function (class, param1, param2)
        int   param1
        int   param2
    C_ARGS:
        param1, param2

the same goes for bindings that use the C style declairations:

gboolean
some_class_static_function (SV * class, int param1, int param2)
    C_ARGS:
        param1, param2

just leave out the type on any unused paramter

gboolean
some_class_static_function (class, int param1, int param2)
    C_ARGS:
        param1, param2

the same thing works for any paramter that needs to be passed to a wrapper
function, but will not be used, for example functions that accept any number
of paramters. so

gboolean
some_function_that_takes_n_params (int foo, double bar, char first_label, ...)
        int      foo
        double   bar
        char   * first_label
    CODE:
        /* do the stack magic, which wouldn't use first_label */

can be done as

gboolean
some_function_that_takes_n_params (int foo, double bar, char first_label, ...)
        int      foo
        double   bar
    CODE:
        /* do the stack magic, which wouldn't use first_label */

in this case "first_label, ..." would be in the usage print for this function,
but the parameter would never actually exist and the stack slurping code would
work just the same.

with the newest ParseXS, as you all already know, most of the unused variable
and paramter warnings are cleaned up, but there are cases when you do special
things that it can not predict and fix for you, something like the following:

void
some_functions (int param)
        int param
    ALIAS:
        Some::Pkg::alias1 = 1
        Some::Pkg::alias2 = 2
        Some::Pkg::alias3 = 3
    CODE:
        /* we always call the same function no matter what and we don't need
         * to know what name we were called by */
        some_c_function (param);

the above code would print a warning about an unused variable ix, which would
hold the id of the function name by which this was called. to get rid of that
warning use a macro defined in perl.h (or one of those standard perl XS
headers, PERL_UNUSED_VAR.

void
some_functions (int param)
        int param
    ALIAS:
        Some::Pkg::alias1 = 1
        Some::Pkg::alias2 = 2
        Some::Pkg::alias3 = 3
    CODE:
        PERL_UNUSED_VAR (ix);
        /* we always call the same function no matter what and we don't need
         * to know what name we were called by */
        some_c_function (param);

i might ought to make a weekly series out of this or something. i was joking
but it might actually be a good idea, not just for Gtk2-Perl. i don't know how
long i could actually keep it up with interesting things, but i could pick
topics like ALIAS, PPCODE, variable args to handle what would of been passed
as an array to C. if i do i won't bother posting them to the list but i'll let
the list know. i sounds like a good idea anyway, we'll see if i have the
gumption to follow through on it...

-rm



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]