FreeBSD下的奇怪转换警告

时间:2017-08-14 12:10:08

标签: gcc warnings freebsd endianness

我有这样的代码:

# gcc x.cc -Wall -Wconversion -Wpedantic -lstdc++ -std=c++11
x.cc: In function 'int main()':
x.cc:5:2: warning: conversion to '__uint16_t {aka short unsigned int}' from '__uint32_t {aka unsigned int}' may alter its value [-Wconversion]
  be64toh(a);
  ^
x.cc:5:2: warning: conversion to '__uint16_t {aka short unsigned int}' from '__uint32_t {aka unsigned int}' may alter its value [-Wconversion]
x.cc:5:2: warning: conversion to '__uint32_t {aka unsigned int}' from 'uint64_t {aka long unsigned int}' may alter its value [-Wconversion]

当我尝试在FreeBSD下移植它时,我得到:

 10 = TEXT
 10 {
     data = levelfield:-1, field_facebook, slide
     override.if.isFalse.field = field_facebook
     wrap = <li><a href="|" data-toggle="tooltip" title="Facebook" target="_blank"><span class="icon-facebook"></span></a></li>
     required = 1
 }

为什么呢?有什么方法可以避免吗?

我应该使用sys / endian.h以外的其他一些库

1 个答案:

答案 0 :(得分:1)

是的,“gcc -E”给出答案。 be64toh是多个转换64-> 32-> 16的内联函数结果的叠加。我想gcc想知道这种转换。

   (__builtin_constant_p((
   a
   )) ? (((__uint64_t)(__builtin_constant_p(((__uint64_t)((
   a
   ))) & 0xffffffff) ? (((__uint32_t)((__uint16_t)(__builtin_constant_p(((__uint32_t)(((__uint64_t)((
   a
   ))) & 0xffffffff)) & 0xffff) ? (__uint16_t)(((__uint16_t)(((__uint32_t)(((__uint64_t)((
   a
   ))) & 0xffffffff)) & 0xffff)) << 8 | ((__uint16_t)(((__uint32_t)(((__uint64_t)((
   a
   ))) & 0xffffffff)) & 0xffff)) >> 8) : __bswap16_var(((__uint32_t)(((__uint64_t)((
   a
   ))) & 0xffffffff)) & 0xffff))) << 16) | ((__uint16_t)(__builtin_constant_p(((__uint32_t)(((__uint64_t)((
   a
   ))) & 0xffffffff)) >> 16) ? (__uint16_t)(((__uint16_t)(((__uint32_t)(((__uint64_t)((
   a
   ))) & 0xffffffff)) >> 16)) << 8 | ((__uint16_t)(((__uint32_t)(((__uint64_t)((
   a
   ))) & 0xffffffff)) >> 16)) >> 8) : __bswap16_var(((__uint32_t)(((__uint64_t)((
   a
   ))) & 0xffffffff)) >> 16)))) : __bswap32_var(((__uint64_t)((
   a
   ))) & 0xffffffff)) << 32) | (__builtin_constant_p(((__uint64_t)((
   a
   ))) >> 32) ? (((__uint32_t)((__uint16_t)(__builtin_constant_p(((__uint32_t)(((__uint64_t)((
   a
   ))) >> 32)) & 0xffff) ? (__uint16_t)(((__uint16_t)(((__uint32_t)(((__uint64_t)((
   a
   ))) >> 32)) & 0xffff)) << 8 | ((__uint16_t)(((__uint32_t)(((__uint64_t)((
   a
   ))) >> 32)) & 0xffff)) >> 8) : __bswap16_var(((__uint32_t)(((__uint64_t)((
   a
   ))) >> 32)) & 0xffff))) << 16) | ((__uint16_t)(__builtin_constant_p(((__uint32_t)(((__uint64_t)((
   a
   ))) >> 32)) >> 16) ? (__uint16_t)(((__uint16_t)(((__uint32_t)(((__uint64_t)((
   a
   ))) >> 32)) >> 16)) << 8 | ((__uint16_t)(((__uint32_t)(((__uint64_t)((
   a
   ))) >> 32)) >> 16)) >> 8) : __bswap16_var(((__uint32_t)(((__uint64_t)((
   a
   ))) >> 32)) >> 16)))) : __bswap32_var(((__uint64_t)((
   a
   ))) >> 32))) : __bswap64_var((
   a
   )))

提示:使用clang;)