Skip to content

Suppress warnings on VS - #642

Merged
Anna Gringauze (annagrin) merged 1 commit into
microsoft:masterfrom
beinhaerter:C26472_C26482
Aug 1, 2018
Merged

Anna Gringauze (annagrin) merged 1 commit into
microsoft:masterfrom
beinhaerter:C26472_C26482

Conversation

@beinhaerter

@beinhaerter Werner Henze (beinhaerter) commented Mar 16, 2018 •

Copy link
Copy Markdown
Contributor

For this short code snippet

#include <gsl/gsl_util>
#include <vector>

void pr642()
{
	short buf[3];
	std::vector<short> vec(3);
	gsl::at(buf, 2) = gsl::narrow<short>(42);
	gsl::at(vec, 2) = gsl::narrow<short>(42);
}

VS 15.6.2 code analysis generates annoying warnings.

...\gsl\include\gsl\gsl_util(93) : warning C26472 : Don't use a static_cast for arithmetic conversions. Use brace initialization, gsl::narrow_cast or gsl::narow (type.1).
...\gsl\include\gsl\gsl_util(127) : warning C26472 : Don't use a static_cast for arithmetic conversions. Use brace initialization, gsl::narrow_cast or gsl::narow (type.1).
...\gsl\include\gsl\gsl_util(127) : warning C26482 : Only index into arrays using constant expressions(bounds.2).
...\gsl\include\gsl\gsl_util(127) : warning C26446 : Prefer to use gsl::at() instead of unchecked subscript operator (bounds.4).
...\gsl\include\gsl\gsl_util(114) : warning C26472 : Don't use a static_cast for arithmetic conversions. Use brace initialization, gsl::narrow_cast or gsl::narow (type.1).
...\gsl\include\gsl\gsl_util(135) : warning C26472 : Don't use a static_cast for arithmetic conversions. Use brace initialization, gsl::narrow_cast or gsl::narow (type.1).
...\gsl\include\gsl\gsl_util(135) : warning C26446 : Prefer to use gsl::at() instead of unchecked subscript operator (bounds.4).
...\gsl\include\gsl\gsl_assert(106) : warning C26440 : Function 'gsl::details::throw_exceptiongsl::narrowing_error' can be declared 'noexcept' (f.6).

gsl::narrow, gsl::narrow_cast and gsl::at are the safe variants suggested by CppCoreGuideline. It does not make sense to let VS warn inside the implementation of these functions that unsafe static_cast is used and that the safe variants shall be used.

Comment thread include/gsl/gsl_util Outdated

#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable : 26472) // Don't use a static_cast for arithmetic conversions. Use brace initialization, gsl::narrow_cast or gsl::narow (type.1: http://go.microsoft.com/fwlink/p/?LinkID=620417).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: gsl::narow => gsl::narrow

@beinhaerter

Copy link
Copy Markdown
Contributor Author

The message as written is exactly what the compiler outputs and what is documented at https://github.com/MicrosoftDocs/visualstudio-docs/blob/master/docs/code-quality/C26472.md. But if you want me to lie about the message text in the code comment, no problem.

@scribam

scribam commented May 3, 2018

Copy link
Copy Markdown

Thank you for your feedback, the documentation has been updated.

@Farwaykorse

Farwaykorse commented May 30, 2018 •

Copy link
Copy Markdown
Contributor

(As mentioned in #688.)
An alternative would be the use of [[gsl::suppress(type.1)]] and [[gsl::suppress(bounds.2)]].

I do prefer the use of [[gsl::suppress(token)]].
It is more expressive and offers a general syntax for compilers and analysers implementing the Core Guideline checkers.
But there are some issues that would need to be solved, and require testing with the supported compilers.

I found a discussion on the use of these attributes in the GSL: isocpp/CppCoreGuidelines#1173

You still need an preprocessor #if block, with:

  • minimum compiler versions
  • a different syntax for the (current) Clang implementation, using quotes around the tokens.
    E.g. [[gsl::suppress("type.1")]].

@beinhaerter

Copy link
Copy Markdown
Contributor Author

Neil MacIntosh (@neilmacintosh) Anna Gringauze (@annagrin) What do you think? How should it be handled? I'd be happy to get rid of the MSVC warnings and change my PR to a version that you prefer.

@annagrin

Copy link
Copy Markdown

Werner Henze (@beinhaerter)
C++ Core Guidelines recommendation is to use
[[gsl::suppress(type.1)]] and [[gsl::suppress(bounds.2)]].

There are inconsistencies between compilers about suppression mechanisms, but I think it is fine to introduce conditional compilation around those (if only to show the need for standardization).

@annagrin

Copy link
Copy Markdown

Werner Henze (@beinhaerter) , Farwaykorse, I would welcome a PR!

@beinhaerter
Werner Henze (beinhaerter) force-pushed the C26472_C26482 branch 5 times, most recently from ae53651 to f744f5a Compare July 12, 2018 20:18
@beinhaerter

Copy link
Copy Markdown
Contributor Author

I updated the PR to use [[gsl::suppress]]. In my opinion default behavior should be to suppress the warnings, so I am checking for the compilers that can't correctly suppress.

#if compiler_is_known_to_not_do_it_right
#else
[[gsl::suppress(…)]]
#endif

I could not get the CI builds working with clang or g++ (tried [[gsl::suppress("type.1")]], [[clang::suppress("type.1")]], [[clang::suppress("Res-casts")]]), so for these compilers I just do the same as before - nothing.

- gsl::narrow, gsl::narrow_cast and gsl::at are the safe variants suggested by CppCoreGuideline. It does not make sense to let VS warn inside the implementation of these functions that unsafe static_cast is used and that the safe variants shall be used.
- Suppress warning that throw_exception can be declared noexcept (for the GSL_TERMINATE_ON_CONTRACT_VIOLATION case)
@beinhaerter

Copy link
Copy Markdown
Contributor Author

I am joining multiple suppresses into one gsl::suppress. If you want each gsl::suppress on a single line, please say so.
I added the gsl::suppress at function level. If you want it on line level (like Anna Gringauze (@annagrin) did in her PR #703), please say so.

@annagrin

Copy link
Copy Markdown

Werner Henze (@beinhaerter) rerunning one of the failed tasks (machine problems it seems). I am planning to merge yours and then mine, if I figure out how to make my PR work for all compilers.

@annagrin
Anna Gringauze (annagrin) merged commit f4a7158 into microsoft:master Aug 1, 2018
@beinhaerter
Werner Henze (beinhaerter) deleted the C26472_C26482 branch August 5, 2018 18:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants