From 7e1ee6be814e7968f627d6b3c5a190d27577d562 Mon Sep 17 00:00:00 2001 From: Carson Radtke Date: Tue, 9 Sep 2025 10:18:41 -0600 Subject: [PATCH] export proper syntax for GSL_SUPPRESS for new VS A new Visual Studio version will soon be available that deprecates the old syntax for gsl::suppress. Customers will now get a C4875 diagnostic on suppressions that look like `gsl::suppress(x)` urging them to use `gsl::suppress("x")` instead. This change updates the `GSL_SUPPRESS` macro to preprocess GSL_SUPPRESS(x) to gsl::suppress("x") on clang and new versions of MSVC. --- README.md | 2 +- docs/headers.md | 4 ++-- include/gsl/assert | 11 ++++++----- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d6b251c..784aebc 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ dyn_array | & move_owner | ☐ | A helper function that moves one `owner` to the other [final_action](docs/headers.md#user-content-H-util-final_action) | ☑ | A RAII style class that invokes a functor on its destruction [finally](docs/headers.md#user-content-H-util-finally) | ☑ | A helper function instantiating [final_action](docs/headers.md#user-content-H-util-final_action) -[GSL_SUPPRESS](docs/headers.md#user-content-H-assert-gsl_suppress) | ☑ | A macro that takes an argument and turns it into `[[gsl::suppress(x)]]` or `[[gsl::suppress("x")]]` +[GSL_SUPPRESS](docs/headers.md#user-content-H-assert-gsl_suppress) | ☑ | A macro that takes an argument and turns it into `[[gsl::suppress(x)]]` or `[[gsl::suppress("x")]]` depending on the compiler. [[implicit]] | ☐ | A "marker" to put on single-argument constructors to explicitly make them non-explicit [index](docs/headers.md#user-content-H-util-index) | ☑ | A type to use for all container and array indexing (currently an alias for `std::ptrdiff_t`) [narrow](docs/headers.md#user-content-H-narrow-narrow) | ☑ | A checked version of `narrow_cast`; it can throw [narrowing_error](docs/headers.md#user-content-H-narrow-narrowing_error) diff --git a/docs/headers.md b/docs/headers.md index 6cd2f44..e8372f7 100644 --- a/docs/headers.md +++ b/docs/headers.md @@ -49,9 +49,9 @@ See [GSL.assert: Assertions](https://isocpp.github.io/CppCoreGuidelines/CppCoreG This macro can be used to suppress a code analysis warning. The core guidelines request tools that check for the rules to respect suppressing a rule by writing -`[[gsl::suppress(tag)]]` or `[[gsl::suppress(tag, justification: "message")]]`. +`[[gsl::suppress("tag")]]` or `[[gsl::suppress("tag", justification: "message")]]`. -Clang does not use exactly that syntax, but requires `tag` to be put in double quotes `[[gsl::suppress("tag")]]`. +Older versions of MSVC (VS 2022 and earlier) only understand `[[gsl::suppress(tag)]]` without the double quotes around `tag`. For portable code you can use `GSL_SUPPRESS(tag)`. diff --git a/include/gsl/assert b/include/gsl/assert index d3e6ccd..3420ecb 100644 --- a/include/gsl/assert +++ b/include/gsl/assert @@ -47,13 +47,14 @@ // #if defined(__clang__) #define GSL_SUPPRESS(x) [[gsl::suppress(#x)]] -#else -#if defined(_MSC_VER) && !defined(__INTEL_COMPILER) && !defined(__NVCC__) -#define GSL_SUPPRESS(x) [[gsl::suppress(x)]] +#elif defined(_MSC_VER) && _MSC_VER >= 1950 +// Visual Studio versions after 2022 (_MSC_VER > 1944) support the justification message. +#define GSL_SUPPRESS(x) [[gsl::suppress(#x)]] +#elif defined(_MSC_VER) && !defined(__INTEL_COMPILER) && !defined(__NVCC__) +#define GSL_SUPPRESS(x) [[gsl::suppress(#x)]] #else #define GSL_SUPPRESS(x) -#endif // _MSC_VER -#endif // __clang__ +#endif // defined(__clang__) #if defined(__clang__) || defined(__GNUC__) #define GSL_LIKELY(x) __builtin_expect(!!(x), 1)