[Github-comments] [geany/geany] Rewrite HL_N_ENTRIES macro to avoid a GCC8 false positive warning (#2398)

Colomban Wendling notifications at xxxxx
Sat Nov 16 17:39:26 UTC 2019


GCC 8 introduced `-Wsizeof-pointer-div` which is enabled by `-Wall` and
warns for sizeof divisions that look like they would compute the size
of a static array but are called on something on which this doesn't
work (e.g. a pointer as LHS).  This is quite reasonable and useful, but
it fails to detect the case where the computation is guarded against
being called on problematic values, like our HL_N_ENTRIES() macro that
accepts NULLs but won't use the sizeof computation result then.

Work around this by implementing HL_N_ENTRIES() macro in a way that
cannot trigger such a warning yet yield the same result.

Example warning:
```
../../src/highlighting.c: In function ‘highlighting_init_styles’:
/usr/include/glib-2.0/glib/gmacros.h:351:42: warning: division ‘sizeof (HLKeyword * {aka struct <anonymous> *}) / sizeof (HLKeyword {aka struct <anonymous>})’ does not compute the number of array elements [-Wsizeof-pointer-div]
 #define G_N_ELEMENTS(arr)  (sizeof (arr) / sizeof ((arr)[0]))
                                          ^
../../src/highlightingmappings.h:74:48: note: in expansion of macro ‘G_N_ELEMENTS’
 #define HL_N_ENTRIES(array) ((array != NULL) ? G_N_ELEMENTS(array) : 0)
                                                ^~~~~~~~~~~~
../../src/highlighting.c:966:5: note: in expansion of macro ‘HL_N_ENTRIES’
     HL_N_ENTRIES(highlighting_keywords_##LANG_NAME)); \
     ^~~~~~~~~~~~
../../src/highlighting.c:1011:3: note: in expansion of macro ‘init_styleset_case’
   init_styleset_case(CONF);
   ^~~~~~~~~~~~~~~~~~
```

---
Another solution could be reporting a bug to GCC for it to detect our use case and avoid the warning then, but that might or might not be much meaningful, and won't fix the issue on currently affected GCC versions anyway.
You can view, comment on, or merge this pull request online at:

  https://github.com/geany/geany/pull/2398

-- Commit Summary --

  * Rewrite HL_N_ENTRIES macro to avoid a GCC8 false positive warning

-- File Changes --

    M src/highlightingmappings.h (12)

-- Patch Links --

https://github.com/geany/geany/pull/2398.patch
https://github.com/geany/geany/pull/2398.diff

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/geany/geany/pull/2398
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.geany.org/pipermail/github-comments/attachments/20191116/488a0ddb/attachment.html>


More information about the Github-comments mailing list