c语言 - 解决警告:将'const void *'传递给类型为 'AV *' 的参数



编译一个包含libmba的XS模块,我无法用我在C语言中的初学者水平的经验来解决这个警告:

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.

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

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

#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: %sn",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.h 的部分

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

在mba/diff.c中:

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)
{

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

解决:

inline
static const void * 
_av_idx(const void *a, int idx, void *context)
{
    SV *line = *av_fetch((AV *)a, idx, 0);
    //                   ^^^^^^
    STRLEN klen;
    char *key = SvPVbyte(line, klen);
    return key;
}

嗯..._av_idx您承诺不会更改第一个参数的内容

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

但是你继续将该参数发送到一个不承诺不更改它的函数(av_fetch(a, idx, 0))。这使你的承诺有点奇怪。

只需删除您的承诺...

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

编辑

或者您可以将参数复制到局部变量并传递该参数

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: %sn",key);
  return key;
}

相关内容

  • 没有找到相关文章

最新更新