【发布时间】:2018-03-09 13:59:21
【问题描述】:
我正在使用定义以下联合的 Microchip 微控制器:
__extension__ typedef struct tagT1CONBITS {
union {
struct {
uint16_t :1;
uint16_t TCS:1;
uint16_t TSYNC:1;
uint16_t :1;
uint16_t TCKPS:2;
uint16_t TGATE:1;
uint16_t :6;
uint16_t TSIDL:1;
uint16_t :1;
uint16_t TON:1;
};
struct {
uint16_t :4;
uint16_t TCKPS0:1;
uint16_t TCKPS1:1;
};
};
} T1CONBITS;
extern volatile T1CONBITS T1CONbits __attribute__((__sfr__));
在我的代码中的某处,我将一个变量定义为一个 8 位无符号整数,我想将其分配给上述联合的字段之一。大致如下:
uint8_t tckps;
// The value of tckps is calculated here by some magic formula
tckps = magicformula();
// We asign the value of tckps to the uC register
T1CONbits.TCKPS = tckps;
我在 gcc 中启用了 -Wconversion 选项,这会导致以下警告:
warning: conversion to 'volatile unsigned char:2' from 'uint8_t' may alter its value
我可以理解为什么 gcc 会警告我。我目前在分配 tckps 变量之前对其进行值检查,所以我知道数据丢失不会成为问题,但我不知道如何满足 gcc 转换检查,因此它不会发出警告我在这种特殊情况下。
如何修复警告?
提前致谢!
编辑:添加工具链信息。
Microchip Language Tool Shell Version 1.33 (Build date: Oct 9 2017).
Copyright (c) 2012-2016 Microchip Technology Inc. All rights reserved
*** Executing: "C:\Program Files (x86)\Microchip\xc16\v1.33\bin\bin/elf-gcc.exe"
"-v"
Using built-in specs.
COLLECT_GCC=C:\Program Files (x86)\Microchip\xc16\v1.33\bin\bin/elf-gcc.exe
Target: pic30-elf
Configured with: /home/xc16/release-builds/build_20171009/src/XC_GCC/gcc/configure --build=i386-linux --host=i386-mingw32 --target=pic30-elf --disable-lto --disable-threads --disable-libmudflap --disable-libssp --disable-libstdcxx-pch --disable-hosted-libstdcxx --with-gnu-as --with-gnu-ld --enable-languages=c --disable-nls --disable-libgomp --without-headers --disable-libffi --disable-bootstrap --prefix=/bin --libexecdir=/bin --program-prefix=pic30- --with-libelf=/home/xc16/release-builds/build_20171009/bin/XC_GCC-elf-mingw32-xclm/host-libs/ --with-dwarf2 --with-gmp=/home/xc16/release-builds/build_20171009/bin/XC_GCC-elf-mingw32-xclm/host-libs --with-ppl=/home/xc16/release-builds/build_20171009/bin/XC_GCC-elf-mingw32-xclm/host-libs --with-cloog=/home/xc16/release-builds/build_20171009/bin/XC_GCC-elf-mingw32-xclm/host-libs --with-zlib=/home/xc16/release-builds/build_20171009/bin/XC_GCC-elf-mingw32-xclm/host-libs --with-bugurl=http://www.microchip.com/support --with-host-libstdcxx=-Wl,-Bstatic,-lstdc++,-Bdynamic,-lm
Thread model: single
gcc version 4.5.1 (XC16, Microchip v1.33) Build date: Oct 9 2017 (Microchip Technology)
【问题讨论】:
-
无法用 gcc 6 重现
-
我可以用 gcc 4.9.x 重现。
-
个人意见:我认为
-Wconversion不应该一直保持启用状态。它往往会发出一些错误警报,这些错误警报需要丑陋的强制转换或编译指示才能静音。噪声与收益比不是最佳的。 -
在我的例子中,这个标志导致了非常有用的发现。我个人更喜欢让它保持启用状态,尽管我可能不得不花时间在那里留下一些丑陋的演员表。
标签: c gcc embedded bit-fields microchip