From 5317dd77d4506de24b0d5918fa3258fe275e8642 Mon Sep 17 00:00:00 2001 From: Oleksandr Masliuchenko Date: Sat, 22 Apr 2017 16:00:23 +0300 Subject: [PATCH] fixed strict-alliasing warning --- Adafruit_GFX.cpp | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/Adafruit_GFX.cpp b/Adafruit_GFX.cpp index 967eb77..4d78dba 100755 --- a/Adafruit_GFX.cpp +++ b/Adafruit_GFX.cpp @@ -70,6 +70,30 @@ POSSIBILITY OF SUCH DAMAGE. #define _swap_int16_t(a, b) { int16_t t = a; a = b; b = t; } #endif +inline GFXglyph * pgm_read_glyph_ptr(const GFXfont *gfxFont, uint8_t c) +{ +#ifdef __AVR__ + return &(((GFXglyph *)pgm_read_pointer(&gfxFont->glyph))[c]); +#else + // expression in __AVR__ section may generate "dereferencing type-punned pointer will break strict-aliasing rules" warning + // In fact, on other platforms (such as STM32) there is no need to do this pointer magic as program memory may be read in a usual way + // So expression may be simplified + return gfxFont->glyph + c; +#endif //__AVR__ +} + +inline uint8_t * pgm_read_bitmap_ptr(const GFXfont *gfxFont) +{ +#ifdef __AVR__ + return (uint8_t *)pgm_read_pointer(&gfxFont->bitmap); +#else + // expression in __AVR__ section generates "dereferencing type-punned pointer will break strict-aliasing rules" warning + // In fact, on other platforms (such as STM32) there is no need to do this pointer magic as program memory may be read in a usual way + // So expression may be simplified + return gfxFont->bitmap; +#endif //__AVR__ +} + Adafruit_GFX::Adafruit_GFX(int16_t w, int16_t h): WIDTH(w), HEIGHT(h) { @@ -594,8 +618,8 @@ void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c, // directly with 'bad' characters of font may cause mayhem! c -= pgm_read_byte(&gfxFont->first); - GFXglyph *glyph = &(((GFXglyph *)pgm_read_pointer(&gfxFont->glyph))[c]); - uint8_t *bitmap = (uint8_t *)pgm_read_pointer(&gfxFont->bitmap); + GFXglyph *glyph = pgm_read_glyph_ptr(gfxFont, c); + uint8_t *bitmap = pgm_read_bitmap_ptr(gfxFont); uint16_t bo = pgm_read_word(&glyph->bitmapOffset); uint8_t w = pgm_read_byte(&glyph->width), @@ -680,7 +704,7 @@ void Adafruit_GFX::write(uint8_t c) { uint8_t first = pgm_read_byte(&gfxFont->first); if((c >= first) && (c <= (uint8_t)pgm_read_byte(&gfxFont->last))) { uint8_t c2 = c - pgm_read_byte(&gfxFont->first); - GFXglyph *glyph = &(((GFXglyph *)pgm_read_pointer(&gfxFont->glyph))[c2]); + GFXglyph *glyph = pgm_read_glyph_ptr(gfxFont, c2); uint8_t w = pgm_read_byte(&glyph->width), h = pgm_read_byte(&glyph->height); if((w > 0) && (h > 0)) { // Is there an associated bitmap? @@ -806,7 +830,7 @@ void Adafruit_GFX::getTextBounds(char *str, int16_t x, int16_t y, if(c != '\r') { // Not a carriage return, is normal char if((c >= first) && (c <= last)) { // Char present in current font c -= first; - glyph = &(((GFXglyph *)pgm_read_pointer(&gfxFont->glyph))[c]); + glyph = pgm_read_glyph_ptr(gfxFont, c); gw = pgm_read_byte(&glyph->width); gh = pgm_read_byte(&glyph->height); xa = pgm_read_byte(&glyph->xAdvance); @@ -896,7 +920,7 @@ void Adafruit_GFX::getTextBounds(const __FlashStringHelper *str, if(c != '\r') { // Not a carriage return, is normal char if((c >= first) && (c <= last)) { // Char present in current font c -= first; - glyph = &(((GFXglyph *)pgm_read_pointer(&gfxFont->glyph))[c]); + glyph = pgm_read_glyph_ptr(gfxFont, c); gw = pgm_read_byte(&glyph->width); gh = pgm_read_byte(&glyph->height); xa = pgm_read_byte(&glyph->xAdvance);