microsoft / GSL

Guidelines Support Library
Other
6.11k stars 736 forks source link

Turning off clang unsafe buffer warnings fails when compiling with a gcc based compiler #1148

Open pirgia opened 6 months ago

pirgia commented 6 months ago

The code sections (in span and util)

// Turn off clang unsafe buffer warnings as all accessed are guarded by runtime checks
#if defined(__clang__) && __has_warning("-Wunsafe-buffer-usage")
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
#endif // defined(__clang__) && __has_warning("-Wunsafe-buffer-usage")

and (similarly)

#if defined(__clang__) && __has_warning("-Wunsafe-buffer-usage")
#pragma clang diagnostic pop
#endif

fails when compiling with a GCC-based compiler (the STM compiler for an STM32 microcontroller in this case). The generated error is:

missing binary operator before token "("

I solved this by separating the checks:

// Turn off clang unsafe buffer warnings as all accessed are guarded by runtime checks
#if defined(__clang__)
#  if __has_warning("-Wunsafe-buffer-usage")
#    pragma clang diagnostic push
#    pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
#  endif // __has_warning("-Wunsafe-buffer-usage")
#endif // defined(__clang__)

There are 4 points that need to be modified:

Thanks for your attention

christianbrugger commented 4 months ago

I am having the same issue, it even shows up now in godbolt:

https://godbolt.org/z/cEb1481Yd

The last commit that works for me with gcc is caae4dd.

I also can confirm that the pull request from @beinhaerter fixes the issue for me

jackluo923 commented 4 months ago

Confirmed to be working for me as well.

SunnyPaprika commented 2 months ago

It also shows errors from IntelliSense under VS2022 for this:

image