解决警告:将'const void *'传递给'AV *'类型的参数

时间:2015-06-20 09:33:05

标签: c perl perl-xs

编译包含libmba的XS模块我无法用我在C中的初学者级别经验解决此警告:

inline-block

编译后的模块运行正常。但有没有办法在没有警告的情况下对其进行编码?

LCS / XS.xs中的相关部分:

helmut@Helmuts-MacBook-Air:~/github/LCS-XS$ make
"/Users/helmut/perl5/perlbrew/perls/perl-5.18.2/bin/perl"    "/Users/helmut/perl5/perlbrew/perls/perl-5.18.2/lib/5.18.2/ExtUtils/xsubpp"  -typemap "/Users/helmut/perl5/perlbrew/perls/perl-  5.18.2/lib/5.18.2/ExtUtils/typemap"  XS.xs > XS.xsc && mv XS.xsc XS.c
cc -c  -I. -fno-common -DPERL_DARWIN -fno-strict-aliasing -pipe - fstack-protector -I/usr/local/include -I/opt/local/include -O3   - DVERSION=\"0.01\" -DXS_VERSION=\"0.01\"  "- I/Users/helmut/perl5/perlbrew/perls/perl-5.18.2/lib/5.18.2/darwin- 2level/CORE"   XS.c
  XS.xs:55:26: warning: passing 'const void *' to parameter of type 'AV  *' (aka 'struct av *') discards qualifiers
    [-Wincompatible-pointer-types-discards-qualifiers]
       SV *line = *av_fetch(a, idx, 0);
                            ^
/Users/helmut/perl5/perlbrew/perls/perl-5.18.2/lib/5.18.2/darwin- 2level/CORE/embed.h:51:46: note: expanded from macro
'av_fetch'
#define av_fetch(a,b,c)         Perl_av_fetch(aTHX_ a,b,c)
                                                    ^
/Users/helmut/perl5/perlbrew/perls/perl-5.18.2/lib/5.18.2/darwin- 2level/CORE/proto.h:178:44: note: passing argument to
  parameter 'av' here
    PERL_CALLCONV SV**      Perl_av_fetch(pTHX_ AV *av, I32 key, I32 lval)
                                                   ^
1 warning generated.

mba / diff.h的部分

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include "ppport.h"

#include <string.h>

#include <mba/diff.h>
#include <mba/diff.c>

/* snipped */

inline
static const void * 
_av_idx(const void *a, int idx, void *context)
{
   //AV *av = a;
   SV *line = *av_fetch(a, idx, 0);
   STRLEN klen;
   char *key = SvPVbyte(line, klen);
   //printf("key: %s\n",key);

  return key;
}

/* snipped */

void lcs_LCS(obj, s1, s2)
  SV *obj
  AV * s1
  AV * s2

  PREINIT:
    struct CTX *ctx = (struct CTX *)SvIVX(SvRV(obj));

  PPCODE:
    int d, sn, i;
    struct varray *ses = varray_new(sizeof(struct diff_edit), NULL);

    IV n;
    IV m;
    n = av_top_index(s1);
    m = av_top_index(s2);

    // call libmba::diff()
    d = diff(s1, 0, n, s2, 0, m, &_av_idx, &_cmp_str,  NULL, 0, ses, &sn, NULL);

在mba / diff.c:

typedef const void *(*idx_fn)(const void *s, int idx, void *context);

在不改变libmba源的情况下解决此警告是否有良好的做法?

解决:

int
diff(const void *a, int aoff, int n,
    const void *b, int boff, int m,
    idx_fn idx, cmp_fn cmp, void *context, int dmax,
    struct varray *ses, int *sn,
    struct varray *buf)
{

1 个答案:

答案 0 :(得分:0)

Well ... in _av_idx you are promising you will not change the contents of the first parameter

inline static const void *_av_idx(const void *a, int idx, void *context)
//                                ^^^^^

But then you proceed to send that parameter to a function (av_fetch(a, idx, 0)) that does not promise to not change it. That makes your promise a little strange.

Just remove your promise ...

inline static const void *_av_idx(void *a, int idx, void *context)
//                       no const ^^^^^^^

Edit

Or you could copy the argument to a local variable and pass that

inline
static const void * 
_av_idx(const void *a, int idx, void *context)
{
   AV *a_copy;
   deep_copy(a_copy, a);
   if (a_copy != NULL) {
       SV *line = *av_fetch(a_copy, idx, 0);
       free(a_copy);
   } else {
       /* error */
   }
   STRLEN klen;
   char *key = SvPVbyte(line, klen);
   //printf("key: %s\n",key);

  return key;
}