Skip to content

Dev/annagrin/fix cppcorecheck warnings - #703

Merged
Neil MacIntosh (neilmacintosh) merged 19 commits into
microsoft:masterfrom
annagrin:dev/annagrin/fix_cppcorecheck_warnings
Aug 13, 2018
Merged

Neil MacIntosh (neilmacintosh) merged 19 commits into
microsoft:masterfrom
annagrin:dev/annagrin/fix_cppcorecheck_warnings

Conversation

@annagrin

@annagrin Anna Gringauze (annagrin) commented Jun 30, 2018 •

Copy link
Copy Markdown

Fixed CppCoreCheck warnings in GSL and tests

  • Added CMakeSettings.json for VS Open Folder configuration

    • So we can easily run CppCoreCheck in VS
  • Fixed CppCorecheck warnings where it made sense

  • Suppressed the rest

    • Some suppression does not work, so replaced by #pragma disable)
    • CppCoreCheck has noise, suppressed those with comments
    • Catch produces many warnings, blanket-supressed them all
  • Had to fix clang formatting to keep attributes in place

    • clang-format does not support attributes, so I am using
      "CommentPragmas: '^ NO-FORMAT:'" to skip formatiting on them

@annagrin
Anna Gringauze (annagrin) force-pushed the dev/annagrin/fix_cppcorecheck_warnings branch from b444c68 to 0b94bc3 Compare July 3, 2018 18:37
Anna Gringauze added 2 commits July 13, 2018 17:24
- Added CMakeSettings.json for VS Open Folder configuration
	- So we can easily run CppCoreCheck in VS
- Fixed CppCorecheck warnings where it made sense
- Suppressed the rest
	- Some suppression does not work due to compiler/tool bugs,
          so replaced by #pragma disable
	- CppCoreCheck has noise, suppressed those with comments
	- Catch produces many warnings, blanket-supressed them all
- Had to fix clang formatting to keep attributes in place
	- clang-format does not support attributes, so I am using
        - "CommentPragmas: '^ NO-FORMAT:'" to skip formatiting on them
- Removed GSL_NOEXCEPT macro, removed incorred noexcepts
@annagrin
Anna Gringauze (annagrin) force-pushed the dev/annagrin/fix_cppcorecheck_warnings branch from 34491bd to 439068b Compare July 16, 2018 05:40
@beinhaerter

Copy link
Copy Markdown
Contributor

It looks like you addressed much more warnings than I do in PR #642 and in a slightly different way (suppress before the line with the warning, not at function level). But I am surprised you did not #ifdef to check for different compiler support for gsl::support (MSVC vs. clang vs. gcc).

How do we want to proceed? Do you extend your PR and I close mine? Or shall we keep both open?

With my code in PR #642 and your changes I still get this warning:

...\gsl\include\gsl\gsl_util(141): warning C26472: Don't use a static_cast for arithmetic conversions. Use brace initialization, gsl::narrow_cast or gsl::narow (type.1).

If you add a [[gsl::suppress(type.1)]] // NO-FORMAT: attribute" before line 141 in gsl_util we get rid of that warning.

@annagrin

Copy link
Copy Markdown
Author

Werner Henze (@beinhaerter) I was experimenting:) I am happy to merge your changes into mine, but I would like to avoid ifdefs - currently I am trying my luck with forcing the compilers to ignore unknown attributes.

@annagrin

Copy link
Copy Markdown
Author

Werner Henze (@beinhaerter)

If you add a [[gsl::suppress(type.1)]] // NO-FORMAT: attribute" before line 141 in gsl_util we get rid of that warning.

Thanks, I missed that. I will use narrow_cast to fix the warning instead of suppressing in this case.

@annagrin

Anna Gringauze (annagrin) commented Aug 1, 2018 •

Copy link
Copy Markdown
Author

Werner Henze (@beinhaerter) Would you be able to review the PR? I merged in your changes, but replaced them with my macro.

Previously we were talking about ifdefing out attributes for gcc and clang. This time I wanted an experiment - how close can I get to actually keeping them all for all compilers, given that

  • clang already has CppCoreCheck implementation, and gcc might be able to do the same
  • compilers are supposed to be able to ignore uknown attributes

I achieved partial success:

  • there are some problems in msvc parser or our tools (sometimes suppression does not parse, sometimes parses but does not suppress). We are going to address those issues on our side
  • clang expects quotes around the string inside attribute
  • clang can't compile atribute on return statements in constexpr functions. Need to file a bug.
  • could not make gcc parse any of the attributes. Will investigate further...

Thanks in advance!

This change addresses #688, #432, #451, #578, #220 and probably more.

Comment thread include/gsl/gsl_assert
#define GSL_SUPPRESS(x) [[gsl::suppress(x)]]
#else
#if defined(__clang__)
#define GSL_SUPPRESS(x) [[gsl::suppress("x")]]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Has someone opened a bug report against Clang to get this syntax variation fixed?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I will (sent request to join their bug database)

Comment thread include/gsl/gsl_assert Outdated
#else
[[gsl::suppress(f.6)]]
#endif
GSL_SUPPRESS(f.6) // NO-FORMAT: attribute

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Couldn't we use a conditional noexcept for these cases, based on the throw/terminate build-mode? Yes, it might involve a macro, but probably less directly and would be no worse than suppressions. But I may be missing something else here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

It does work, adding

Comment thread include/gsl/gsl_byte Outdated

// don't warn about function style casts in byte related operators
#pragma warning(disable : 26493)
// Turn off rules that generate too much noise. To be fixed in the static analysis tool.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If that is MSVC /analyze, perhaps we should just call it that?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

sure

Comment thread include/gsl/gsl_byte
return static_cast<byte>(t);
}
template <>
// GSL_SUPPRESS(type.4) // NO-FORMAT: attribute // TODO: suppression does not work

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I can't remember, but is there a reason this can't be just return {t};? If there is, perhaps we should leave behind a comment to explain to others reading the code.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The only reason is that it does not work with c++14... will add comment.

Comment thread include/gsl/gsl_assert

//
// make suppress attributes parse for some compilers
// Hopefully temporary until suppresion stardization occurs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Typos: "suppression standardization"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

thanks!

Comment thread include/gsl/gsl_byte Outdated
// don't warn about function style casts in byte related operators
#pragma warning(disable : 26493)
// Turn off rules that generate too much noise. To be fixed in the static analysis tool.
#pragma warning(disable : 26493) // don't use c-style casts

@beinhaerter Werner Henze (beinhaerter) Aug 5, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could this be suppressed with GSL_SUPPRESS(type.4) or as we know this is MSVC maybe with [[gsl::suppress(x)]]?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Had to use #pragma suppress because MSVC has a bug where suppression does not always work in templates. To be fixed...

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Added comment to clarify

Comment thread include/gsl/multi_span Outdated
#pragma warning(disable : 4127) // conditional expression is constant
#pragma warning(disable : 4702) // unreachable code

// Turn off rules that generate too much noise. To be fixed in the static analysis tool.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If you have some MS internal insights and expect these fixes to be in version x of MSVC then maybe we could only use them #if _MSC_VER < x.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Not yet, will do when we have the fixes planned.

Comment thread include/gsl/multi_span Outdated
}

constexpr bool operator==(const multi_span_index& rhs) const GSL_NOEXCEPT
GSL_SUPPRESS(bounds .1) // NO-FORMAT: attribute

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Here and in the next line we could remove the space in "bounds ." and align it with the format in the other suppresses.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

There seems to be an issue with clang-format where it inserts the space even when it is supposed to ignore the commented line. Will fix.

Comment thread include/gsl/multi_span
}

size_type elementNum(std::size_t dim) const GSL_NOEXCEPT
GSL_SUPPRESS(c.128) // NO-FORMAT: attribute // no pointers to BoundsRanges should be ever used

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If this is required would it make sense to add a private operator& to prevent someone taking a pointer to the object?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Makes sense, will add as a separate PR later, thanks!

Comment thread include/gsl/multi_span
for (std::size_t i = 0; i < rank; ++i) {
ret += (m_extents[i] - 1) * m_strides[i];
}
for (std::size_t i = 0; i < rank; ++i) { ret += (m_extents[i] - 1) * m_strides[i]; }

@beinhaerter Werner Henze (beinhaerter) Aug 5, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In my opinion the old loop code block was better readable than this one liner. This also applies to changes below (where I don't mention it again).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Those are clang-format changes... Normally I would not introduce formatting changes at the same time as code changes, but had to do so in this case (clang-format does not support attributes)

Comment thread include/gsl/multi_span Outdated
typename = std::enable_if_t<std::is_same<std::remove_cv_t<value_type>,
std::remove_cv_t<OtherValueType>>::value>>
constexpr bool operator==(const multi_span<OtherValueType, OtherDimensions...>& other) const
noexcept

@beinhaerter Werner Henze (beinhaerter) Aug 5, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Previously the GSL_NOEXCEPT was on the same line. If there is no reason to put it on a new line, I would leave it there. Or is this something the editor automatically does? The same applies below.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

yeah, this is clang-format

Comment thread include/gsl/multi_span
{

GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
void validateThis() const {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

"{" in the next line would be more consistent with the rest of the code.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

will try to keep revert it, will see if clang-format will change it again

Comment thread include/gsl/multi_span

public:
reference operator*() const GSL_NOEXCEPT
reference operator*() const

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why is noexcept removed here? I know that validateThis is not noexcept, but noexcept is not removed for operator -> which looks quite similar.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

mistake in operator-> ... will fix

Comment thread include/gsl/span Outdated
constexpr span(std::array<ArrayElementType, N>& arr) noexcept
: storage_(&arr[0], details::extent_type<N>())
{

@beinhaerter Werner Henze (beinhaerter) Aug 5, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In other places you are replacing "{\n}" by "{}", here and below you are adding a new line?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

thanks for catching!

Comment thread include/gsl/string_span
typename = std::enable_if_t<std::is_convertible<
T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
bool operator<(gsl::basic_string_span<CharT, Extent> one, const T& other) GSL_NOEXCEPT
bool operator<(gsl::basic_string_span<CharT, Extent> one, const T& other)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I do not understand why the both operator< (and other operators below) differ in noexcept, but I assume you checked that and there is a reason behind it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

thy shouldn't, thanks!

Comment thread tests/CMakeLists.txt
-Werror
-Wextra
-Wno-missing-braces
-Wno-unknown-attributes

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I bet you have tested it, but I cannot find any information online describing what -Wunknown-attributes or -Wno-unknown-attributes do. And confusingly enough I find code that uses -Wunknown-attribute (no 's'). Do you have a link to a page describing it (gcc?, clang?)?

@annagrin Anna Gringauze (annagrin) Aug 13, 2018 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I couldn't find any documentation but clang gives the following warning on unknown attributes:

warning: unknown attribute ... ignored [-Wunknown-attributes]

So I took a guess at removing that:)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Can't reply to your comment below - sounds reasonable, created PR #719

@beinhaerter

Werner Henze (beinhaerter) commented Aug 5, 2018 •

Copy link
Copy Markdown
Contributor

I like getting rid of GSL_NOEXCEPT and seeing more warnings being suppressed or addressed. Also thanks for doing other cleanup like adding const.

I wonder if we can keep a single, central definition for GSL_SUPPRESS so that we do not need to duplicate the same macro definitions into multiple header files. Maybe it would be good to introduce a new internal header file, but with a name (or path like internal/) that makes clear that it is only for internal use?

I would have split this into one PR for the GSL_NOEXCEPT -> noexcept change (no-brainer) and another (one or two) for the warnings.

@neilmacintosh

Copy link
Copy Markdown
Contributor

Thanks Anna Gringauze (@annagrin), that's a huge diff! Looks good to me.

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.

3 participants