GCC 已开始警告阵列访问超出范围(如果不应该)。 看来编译器作者很难就此问题发出警告,但却在搞砸。 我从9.2版升级到9.3版,但结果是一样的。 我的问题是此代码:
/* Compute a checksum on the TLV data area and compare it to the value stored there. If they match, return true. Checked before using the ADC calibration data and DCO settings stored there. */ int tlv_good(void) { int *p, chk, i; chk = 0; p = (int *)(&TLV_CHECKSUM + 1); for(i = 0; i < 31; i++) chk ^= *p++; chk += TLV_CHECKSUM; return ~chk; } /* Use factory calibration data to correct ADC readings. Starts with filtered Q4 fixed point numbers but just throws away the fractional bits. Remembers if the TLV structure checksum has checked out previously. */ uint16_t corrected(int val) { int32_t tmp; static int *p = 0; if(p || tlv_good()) { p = (int *)&TLV_ADC10_1_TAG; tmp = val >> 3; tmp *= *(p + CAL_ADC_25VREF_FACTOR/2); tmp >>= 16; tmp *= *(p+CAL_ADC_GAIN_FACTOR/2); tmp >>= 16; tmp += *(p+CAL_ADC_OFFSET/2); return tmp; } else return val >> 4; }
第一个函数(tlv_bine())正常,但第二个函数会导致多个警告:
beeper.c: In function 'corrected': beeper.c:86:14: warning: array subscript 6 is outside array bounds of 'volatile unsigned char[1]' [-Warray-bounds] 86 | tmp *= *(p + CAL_ADC_25VREF_FACTOR/2); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /usr/ti/gcc/include/msp430g2553.h:65, from /usr/ti/gcc/include/msp430.h:1423, from beeper.c:1: /usr/ti/gcc/include/msp430g2553.h:904:1: note: while referencing 'TLV_ADC10_1_TAG' 904 | sfr_b(TLV_ADC10_1_TAG); /* TLV ADC10_1 TAG */ | ^~~~~
请注意,即使我正在使用整数的指针,编译器也在抱怨我使用的地址的来源和字符数组。 它没有对 tlv_Good ()中的指针引用提出任何此类投诉。 唯一的区别是所引用项目的大小。 一个定义为单词(Sfr_W),另一个定义为字符。 (SFR_b)。
更糟糕的是,当我查看代码时,它已经非常糟糕了。 尽管我使用整数的指针,但编译器一次提取一个字节的数据。 在将字节合并为整数之前,对一个字节执行缓慢而痛苦的八位偏移。
问题可能始于存储在内存中的这些数据结构在设备报头文件中被标记为特殊函数寄存器,但传播这些属性的编译器却导致了最大的麻烦。 没有禁用阵列边界警告,我没有找到解决该警告的方法。 无法使其生成 sane 代码。