ibireme / yyjson

The fastest JSON library in C
https://ibireme.github.io/yyjson/doc/doxygen/html/
MIT License
3.04k stars 262 forks source link

Improve compiler warnings handling #110

Closed pavelxdd closed 1 year ago

pavelxdd commented 1 year ago

When compiled via GCC 12 with -Wduplicated-branches, there's a warning: https://github.com/ibireme/yyjson/blob/8bc8bc38851aa95de7b29ae28d0afe63b58f157a/src/yyjson.c#L4122

yyjson.c:4122:50: error: this condition has identical branches [-Werror=duplicated-branches]
 4122 |     val->uni.u64 = (u64)(sign ? (u64)(~(_v) + 1) : (u64)(_v)); \
      |                                                  ^
yyjson.c:4170:32: note: in expansion of macro ‘return_i64’
 4170 |         if (!digi_is_fp(*cur)) return_i64(0);
      |                                ^~~~~~~~~~
compilation terminated due to -Wfatal-errors.

With -Wfloat-equal: https://github.com/ibireme/yyjson/blob/8bc8bc38851aa95de7b29ae28d0afe63b58f157a/src/yyjson.c#L4251

yyjson.c:4251:31: warning: comparing floating-point with ‘==’ or ‘!=’ is unsafe [-Wfloat-equal]
 4251 |     if (unlikely(val->uni.f64 == HUGE_VAL || val->uni.f64 == -HUGE_VAL)) {
      |                               ^~
yyjson.c:4251:59: warning: comparing floating-point with ‘==’ or ‘!=’ is unsafe [-Wfloat-equal]
 4251 |     if (unlikely(val->uni.f64 == HUGE_VAL || val->uni.f64 == -HUGE_VAL)) {
      |                                                           ^~                                                

Clang issues warnings only for -Wfloat-equal.

pavelxdd commented 1 year ago

I tried to copy yyjson.h and yyjson.c into my project sources, instead of compiling the library, so the compiler flags from my project issued these warnings.

ibireme commented 1 year ago

It seems that the warning options in CMakeLists are not enough, I will add some more warning options and fix the corresponding warnings soon.

ibireme commented 1 year ago

Fixed: https://github.com/ibireme/yyjson/commit/8b8df872be402eebc7ff42f75af3b0f1b39c46af