XMegaForArduino / arduino

required (and optional) source files for the Arduino development environment, specifically the hardware/arduino sub-directory, to support xmega processors
19 stars 17 forks source link

Bogus warnings in compilers < 4.6 for PROGMEM initialized variables #29

Closed bombasticbob closed 7 years ago

bombasticbob commented 7 years ago

For quite a long time, the PROGMEM 'bogus warning' issue has been around. As an example see here:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=34734

This supposed to have been corrected in gcc 4.6 and later. I have not verified this.

The bug report offers a possible workaround which appears to do what it is supposed to do. This workaround is being integrated into several 'core/xmega' files that make use of PROGMEM data.

The workaround should ONLY apply for compiler versions before 4.6 . This way, there is no unintended effect for later compilers, in case the workaround causes trouble in gcc versions after 4.6 .

The fix needs proper testing on all platforms, and for compilers prior to (and after) gcc version 4.6

bombasticbob commented 7 years ago

in general the 'fix' involves placing this code near the top, following the include directives:

#if __GNUC__ > 4 || (__GNUC__ > 4 && __GNUC_MINOR__ >= 6)
#define PROGMEM_ORIG PROGMEM
#else // PROGMEM workaround

// to avoid the bogus "initialized variables" warning
#ifdef PROGMEM
#undef PROGMEM
#endif // PROGMEM re-define

#define PROGMEM __attribute__((section(".progmem.usbcore")))
#define PROGMEM_ORIG __attribute__((__progmem__))

#endif // check for GNUC >= or < 4.6

For variables and function parameters that complain about 'PROGMEM', use 'PROGMEM_ORIG' instead. Otherwise, it's pretty much seamless and shouldn't require editing for compilers >= 4.6

bombasticbob commented 7 years ago

(still needs full testing, though)

bombasticbob commented 7 years ago

this appears to be working properly and has been slightly tweeked from the above example (the 2nd half of the '#if' is actually wrong, should be 'GNUC == 4' inside the parentheses).

tested using the 1.8.x build environment in versions 0.9.1 and 0.9.2