我在
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