Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

fussy warning using pmg_read_word, pgmspace macros
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

fussy warning using pmg_read_word, pgmspace macros

by tomjennings on Mon Jul 13, 2020 1:49 pm

I'm getting this warning from the compiler when I reference static data tables stored in program-space. I realize the old pgmspace construct isn't needed for SAMDxxx, but this is legacy code, I still have older hardware with Mega 2560's installed, so I'm reluctant to modernize at this time.

I prefer to run with Compiler warnings=ALL, but there's so many of these I set it to MORE... This is an example of the code, looking up error codes in a table to reveal a text name of the error:

Code: Select all | TOGGLE FULL SIZE
void listError (errorV nnn) {
int i;
errorV e;

  for (i= 0; true; ++i) {
    e= (errorV)pgm_read_word (&errorNames [i].err);
    if (e == ERR_NONE) break;                       /* stop, end of table */
    if (e == (errorV)nnn) break;
  }
  Serial.print (flashText (errorNames [i].text));
  Serial.print (" ");
}


errorV an enum defined elsewhere. The data table looks like this:

Code: Select all | TOGGLE FULL SIZE
struct {
  errorV err;
  char text [PROGMEMTEXTWIDTH];

} PROGMEM const errorNames [] = {

/* Error names should be unique in 12 chars or so. */

  ERR_FUELLEVELSENSOR,      { "Sensor Fuel" },
  ERR_ENGINETEMPSENSOR,     { "Sensor Head" },
  ERR_RADTEMPSENSOR,        { "Sensor RadTemp" },
  ERR_OILPRESSURESENSOR,    { "Sensor OilPress" },
  ...


I get this warning:

Code: Select all | TOGGLE FULL SIZE
In file included from /Users/tomic/Library/Arduino15/packages/adafruit/hardware/samd/1.5.14/cores/arduino/Arduino.h:36:0,
                 from sketch/RoadsterChassisv7.ino.cpp:1:
sketch/monitor.h: In member function 'void _monitor::listError(errorV)':
/Users/tomic/Library/Arduino15/packages/adafruit/hardware/samd/1.5.14/cores/arduino/avr/pgmspace.h:103:60: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
 #define pgm_read_word(addr) (*(const unsigned short *)(addr))
                                                            ^
sketch/monitor.h:166:16: note: in expansion of macro 'pgm_read_word'
     e= (errorV)pgm_read_word (&errorNames [i].err);
                ^~~~~~~~~~~~~



"const unsigned short" machine addresses are hardware dependent, tbh I'm surprised, and happy, that the compiler makes it work at all.

Is this an oversight in the samd definition of the pgm_read_word macro that is a (probably low priority bug)? Or should I just shut up and suffer with the warnings for using such antiquated construct? (I suppose though, if the macros is defined, it ought to not throw warning?)




> Please be positive and constructive with your questions and comments.
The weather is really nice today! lol

tomjennings
 
Posts: 78
Joined: Thu Aug 17, 2006 1:21 am

Please be positive and constructive with your questions and comments.