[Insight-developers] Swig Wrapping Warnings.

Hans J. Johnson hans-johnson at uiowa.edu
Mon, 05 Apr 2004 10:33:45 -0500


--=-o6rZiMYiY/fcGhYL/+It
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hello Wrapping Experts,

I've been attempting to get rid of compiler warnings from CableSwig tcl
wrapped code.  The first set of changes were relativly easy to make, and
eliminated about 7000 warnings.  Unfortunatly there are still about 5000
warnings left to go.

I beleive that one fix in cable swig will get rid of them, but I've been
unable to successfully figure out how to do it.  The problem is that
Swig chops off all const qualifiers for any local variable that will be
an lvalue.  This is OK for non-pointer (non-reference) types, because
the behavior does not affect the safety of the code, or it's use.  This,
however, is not the case for pointers where the const is specified
before the '*'.  

e.g.
===================================================================================================================
static int
_wrap_itkLightObject_GetNameOfClass(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {
    (void)clientData; (void)interp; (void)objc; (void)objv;
    itk::LightObject *arg1 = (itk::LightObject *) 0 ;
    char *result;

    if (SWIG_GetArgs(interp, objc, objv,"o:itkLightObject_GetNameOfClass self ",0) == TCL_ERROR) SWIG_fail;
    if ((SWIG_ConvertPtr(objv[1], (void **) &arg1, SWIGTYPE_p_itk__LightObject,SWIG_POINTER_EXCEPTION | 0) != TCL_OK)) SWIG_fail;
    try {
        result = (char *)((itk::LightObject const *)arg1)->GetNameOfClass();
    }
    catch(std::exception &_e) {
        {
            SWIG_exception(SWIG_RuntimeError, const_cast<char*>(_e.what()));
        }
    }
    Tcl_SetObjResult(interp,Tcl_NewStringObj(result,-1));
    return TCL_OK;
    fail:
    return TCL_ERROR;
}
===================================================================================================================
Two changes could remove the compiler warnings in the previous code snip are: 
(a) change the type of result to "const char * reusult;" and change/remove the type casting in the try block.
(b) use a const_cast<char *>() in the try block instead of the c-style (char *)---(const char *) may also work.

I've spent a few days trying to learn the swig wrapping mechanism well enough to make these changes, but have not
found a complete working solution and have not been confident that the partial solutions would not break other 
wrappings.  A patch that does the first 1/2 of option (a) is attached to this e-mail.  I could not track down where
the second part was being constucted.

I will not be able to devote any more time to this.

Sorry,
Hans J. Johnson
hans-johnson at uiowa.edu




--=-o6rZiMYiY/fcGhYL/+It
Content-Disposition: attachment; filename=SWIG_Source_patches.patch
Content-Type: text/x-patch; name=SWIG_Source_patches.patch; charset=UTF-8
Content-Transfer-Encoding: 7bit

Index: Modules/emit.cxx
===================================================================
RCS file: /cvsroot/CableSwig/CableSwig/SWIG/Source/Modules/emit.cxx,v
retrieving revision 1.2
diff -r1.2 emit.cxx
40c40,41
<       Wrapper_add_local(f,"result", SwigType_lstr(rt,"result"));
---
> //      Wrapper_add_local(f,"result", SwigType_lstr(rt,"result"));
>       Wrapper_add_local(f,"result", SwigType_rstr(rt,"result"));
45c46,47
< 	Wrapper_add_local(f,"result", SwigType_lstr(rt,"result"));
---
> //	Wrapper_add_local(f,"result", SwigType_lstr(rt,"result"));
> 	Wrapper_add_local(f,"result", SwigType_rstr(rt,"result"));
47c49,50
< 	Wrapper_add_local(f,"result", SwigType_lstr(vt,"result"));
---
> //	Wrapper_add_local(f,"result", SwigType_lstr(vt,"result"));
> 	Wrapper_add_local(f,"result", SwigType_rstr(vt,"result"));
Index: Swig/stype.c
===================================================================
RCS file: /cvsroot/CableSwig/CableSwig/SWIG/Source/Swig/stype.c,v
retrieving revision 1.5
diff -r1.5 stype.c
590a591,736
>  * SwigType_rstr()
>  *
>  * Produces a casting string that maps the type returned by lstr() to the real 
>  * datatype printed by str().
>  * ----------------------------------------------------------------------------- */
> 
> String *SwigType_rstr(SwigType *s, const String_or_char *name) {
>   String *result, *cast;
>   String *element = 0, *nextelement;
>   SwigType *td, *rs, *tc = 0;
>   List *elements;
>   int      nelements, i;
>   int      clear = 1;
>   int      firstarray = 1;
>   int      isreference = 0;
>   int      isarray = 0;
> 
>   result = NewString("");
> 
>   /*
>   if (SwigType_isconst(s)) {
>     tc = Copy(s);
>     Delete(SwigType_pop(tc));
>     rs = tc;
>   } else {
>     rs = s;
>   }
>   */
> 
>   if (SwigType_issimple(rs)) {
>     td = SwigType_typedef_resolve(rs);
>     if ((td) && (SwigType_isconst(td) || SwigType_isarray(td) || SwigType_isreference(td))) {
>       elements = SwigType_split(td);
>     } else if (td && SwigType_isenum(td)) {
>       elements = SwigType_split(rs);
>       clear = 0;
>     } else {
>       elements = SwigType_split(rs);
>     } 
>     if (td) Delete(td);
>   } else {
>     elements = SwigType_split(rs);
>   }
>   nelements = Len(elements);
>   if (nelements > 0) {
>     element = Getitem(elements,0);
>   }
>   /* Now, walk the type list and start emitting */
>   for (i = 0; i < nelements; i++) {
>     if (i < (nelements - 1)) {
>       nextelement = Getitem(elements,i+1);
>     } else {
>       nextelement = 0;
>     }
>     if (SwigType_isqualifier(element)) {
>       DOH *q = 0;
>       q = SwigType_parm(element);
>       Insert(result,0," ");
>       Insert(result,0,q);
>       Delete(q);
>       clear = 0;
>     } else if (SwigType_ispointer(element)) {
>       Insert(result,0,"*");
>       if ((nextelement) && ((SwigType_isfunction(nextelement) || (SwigType_isarray(nextelement))))) {
> 	Insert(result,0,"(");
> 	Append(result,")");
>       }
>       firstarray = 0;
>     } else  if (SwigType_ismemberpointer(element)) {
>       String *q;
>       Insert(result,0,"::*");
>       q = SwigType_parm(element);
>       Insert(result,0,q);
>       Delete(q);
>       if ((nextelement) && ((SwigType_isfunction(nextelement) || (SwigType_isarray(nextelement))))) {
> //	Insert(result,0,"(");
> //	Append(result,")");
>       }
>       firstarray = 0;
>     } else if (SwigType_isreference(element)) {
> 	Insert(result,0,"&");
> 	if ((nextelement) && ((SwigType_isfunction(nextelement) || (SwigType_isarray(nextelement))))) {
> //	  Insert(result,0,"(");
> //	  Append(result,")");
> 	}
> 	isreference = 1;
>     } else if (SwigType_isarray(element)) {
>       DOH *size;
>       if (firstarray && !isreference) {
> 	Append(result,"(*)");
> 	firstarray = 0;
>       } else {
> 	Append(result,"[");
> 	size = SwigType_parm(element);
> 	Append(result,size);
> 	Append(result,"]");
> 	Delete(size);
> 	clear = 0;
>       }
>       isarray = 1;
>     } else if (SwigType_isfunction(element)) {
>       DOH *parms, *p;
>       int j, plen;
>       Append(result,"(");
>       parms = SwigType_parmlist(element);
>       plen = Len(parms);
>       for (j = 0; j < plen; j++) {
> 	p = SwigType_str(Getitem(parms,j),0);
> 	Append(result,p);
> 	if (j < (plen-1)) Append(result,",");
>       }
>       Append(result,")");
>       Delete(parms);
>     } else if (SwigType_isenum(element)) {
>       String *bs = SwigType_namestr(element);
>       Insert(result,0,bs);
>       Delete(bs);
>       clear = 0;
>     } else {
>       String *bs = SwigType_namestr(element);
>       Insert(result,0," ");
>       Insert(result,0,bs);
>       Delete(bs);
>     }
>     element = nextelement;
>   }
>   Delete(elements);
>   if (clear) {
>     cast = NewString("");
>   } else {
> //    cast = NewStringf("(%s)",result);
>     cast = NewStringf("%s",result);
>   }
>   if (name) {
>     if (isreference) {
>       if (isarray) Clear(cast);
>       Append(cast,"*");
>     }
>     Append(cast,name);
>   }
>   Delete(result);
>   Delete(tc);
>   return cast;
> }
> 
> /* -----------------------------------------------------------------------------
Index: Swig/swig.h
===================================================================
RCS file: /cvsroot/CableSwig/CableSwig/SWIG/Source/Swig/swig.h,v
retrieving revision 1.3
diff -r1.3 swig.h
231a232
> extern String     *SwigType_rstr(SwigType *s, const String_or_char *id);

--=-o6rZiMYiY/fcGhYL/+It--