微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 什么是“误报”?

我在 Understanding Strict Aliasing上注意到以下代码

uint32_t
swap_words( uint32_t arg )
{
    U32*     in = (U32*)&arg;
    uint16_t lo = in->u16[0];
    uint16_t hi = in->u16[1];

    in->u16[0] = hi;
    in->u16[1] = lo;

    return (in->u32);
}

据作者说,

The above source when compiled with GCC 4.0 with the
-Wstrict-aliasing=2 flag enabled will generate a warning. This warning is an example of a false positive. This type of cast is
allowed and will generate the appropriate code (see below). It is
documented clearly that -Wstrict-aliasing=2 may return false
positives.

我想知道什么是误报,我应该在别名变量时注意它吗?

以下是上面提到的“适当的代码(见下文)”,如果它是相关的:

在GNU C 4.0.0版(Apple Computer,Inc.build 5026)上使用-fstrict-aliasing -O3 -Wstrict-aliasing -std = c99编译(powerpc-apple-darwin8),

swap_words:
  stw r3,24(r1)  ; Store arg
  lhz r0,24(r1)  ; Load hi
  lhz r2,26(r1)  ; Load lo
  sth r0,26(r1)  ; Store result[1] = hi
  sth r2,24(r1)  ; Store result[0] = lo
  lwz r3,24(r1)  ; Load result
  blr            ; Return

解决方法

这里的“误报”意味着警告不正确,并且实际上并不存在问题.假阳性是科学和医学中的标准术语,表示评估过程中的错误导致 mistaken detection of a condition tested.(假阴性将是在适当的情况下未发出警告.)

如果您确定警告是误报,则无需修复代码即可将其删除.您可能仍希望重新构建代码以避免编译警告(或者如果可能,请关闭该部分代码的警告).通常无警告的编译确保当出现真正的警告时,你应该注意,你不要错过它.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐