forked from microsoft/GSL
fix cppcorecheck warnings (#703)
* Added c++17 test configurations for clang5.0 and clang6.0
* 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 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
* Ignore unknown attributes
* ignore unknown attributes in noexception mode tests
* fixed C26472 in at()
* created GSL_SUPPRESS macro to allow all compilers to parse suppression attributes
* try to fix gcc compilation problems with attributes
* ignore gsl::suppress for gcc
* move suppression to function level on return statements
clang5.0 and up does not allow attributes on return statemets in constexpr functions
* move suppression to function level on return statements
* use GSL_SUPPRESS in algorithm_tests
* Addressed PR comments
This commit is contained in:
committed by
Neil MacIntosh
parent
6a75903c79
commit
cea0d0ac2b
+34
-5
@@ -14,6 +14,13 @@
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifdef _MSC_VER
|
||||
// blanket turn off warnings from CppCoreCheck from catch
|
||||
// so people aren't annoyed by them when running the tool.
|
||||
#pragma warning(disable : 26440 26426) // from catch
|
||||
|
||||
#endif
|
||||
|
||||
#include <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK, TEST_...
|
||||
|
||||
#include <gsl/pointers> // for not_null, operator<, operator<=, operator>
|
||||
@@ -63,6 +70,7 @@ struct CustomPtr
|
||||
template <typename T, typename U>
|
||||
std::string operator==(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
||||
{
|
||||
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
||||
return reinterpret_cast<const void*>(lhs.p_) == reinterpret_cast<const void*>(rhs.p_) ? "true"
|
||||
: "false";
|
||||
}
|
||||
@@ -70,6 +78,7 @@ std::string operator==(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
||||
template <typename T, typename U>
|
||||
std::string operator!=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
||||
{
|
||||
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
||||
return reinterpret_cast<const void*>(lhs.p_) != reinterpret_cast<const void*>(rhs.p_) ? "true"
|
||||
: "false";
|
||||
}
|
||||
@@ -77,6 +86,7 @@ std::string operator!=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
||||
template <typename T, typename U>
|
||||
std::string operator<(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
||||
{
|
||||
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
||||
return reinterpret_cast<const void*>(lhs.p_) < reinterpret_cast<const void*>(rhs.p_) ? "true"
|
||||
: "false";
|
||||
}
|
||||
@@ -84,6 +94,7 @@ std::string operator<(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
||||
template <typename T, typename U>
|
||||
std::string operator>(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
||||
{
|
||||
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
||||
return reinterpret_cast<const void*>(lhs.p_) > reinterpret_cast<const void*>(rhs.p_) ? "true"
|
||||
: "false";
|
||||
}
|
||||
@@ -91,6 +102,7 @@ std::string operator>(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
||||
template <typename T, typename U>
|
||||
std::string operator<=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
||||
{
|
||||
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
||||
return reinterpret_cast<const void*>(lhs.p_) <= reinterpret_cast<const void*>(rhs.p_) ? "true"
|
||||
: "false";
|
||||
}
|
||||
@@ -98,6 +110,7 @@ std::string operator<=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
||||
template <typename T, typename U>
|
||||
std::string operator>=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
|
||||
{
|
||||
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
||||
return reinterpret_cast<const void*>(lhs.p_) >= reinterpret_cast<const void*>(rhs.p_) ? "true"
|
||||
: "false";
|
||||
}
|
||||
@@ -111,9 +124,19 @@ struct NonCopyableNonMovable
|
||||
NonCopyableNonMovable& operator=(NonCopyableNonMovable&&) = delete;
|
||||
};
|
||||
|
||||
bool helper(not_null<int*> p) { return *p == 12; }
|
||||
bool helper_const(not_null<const int*> p) { return *p == 12; }
|
||||
GSL_SUPPRESS(f.4) // NO-FORMAT: attribute
|
||||
bool helper(not_null<int*> p)
|
||||
{
|
||||
return *p == 12;
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(f.4) // NO-FORMAT: attribute
|
||||
bool helper_const(not_null<const int*> p)
|
||||
{
|
||||
return *p == 12;
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
TEST_CASE("TestNotNullConstructors")
|
||||
{
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
@@ -143,6 +166,7 @@ TEST_CASE("TestNotNullConstructors")
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
void ostream_helper(T v)
|
||||
{
|
||||
not_null<T*> p(&v);
|
||||
@@ -173,7 +197,8 @@ TEST_CASE("TestNotNullostream")
|
||||
ostream_helper<std::string>("string");
|
||||
}
|
||||
|
||||
|
||||
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
TEST_CASE("TestNotNullCasting")
|
||||
{
|
||||
MyBase base;
|
||||
@@ -236,6 +261,7 @@ TEST_CASE("TestNotNullRawPointerComparison")
|
||||
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
TEST_CASE("TestNotNullDereferenceOperator")
|
||||
{
|
||||
{
|
||||
@@ -297,6 +323,7 @@ TEST_CASE("TestNotNullSharedPtrComparison")
|
||||
CHECK((NotNullSp2(sp2) >= NotNullSp1(sp1)) == (sp2 >= sp1));
|
||||
}
|
||||
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
TEST_CASE("TestNotNullCustomPtrComparison")
|
||||
{
|
||||
int ints[2] = {42, 43};
|
||||
@@ -331,6 +358,8 @@ TEST_CASE("TestNotNullCustomPtrComparison")
|
||||
|
||||
|
||||
#if defined(__cplusplus) && (__cplusplus >= 201703L)
|
||||
|
||||
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
|
||||
TEST_CASE("TestNotNullConstructorTypeDeduction")
|
||||
{
|
||||
{
|
||||
@@ -357,7 +386,7 @@ TEST_CASE("TestNotNullConstructorTypeDeduction")
|
||||
{
|
||||
auto workaround_macro = []() {
|
||||
int* p1 = nullptr;
|
||||
not_null x{p1};
|
||||
const not_null x{p1};
|
||||
};
|
||||
CHECK_THROWS_AS(workaround_macro(), fail_fast);
|
||||
}
|
||||
@@ -365,7 +394,7 @@ TEST_CASE("TestNotNullConstructorTypeDeduction")
|
||||
{
|
||||
auto workaround_macro = []() {
|
||||
const int* p1 = nullptr;
|
||||
not_null x{p1};
|
||||
const not_null x{p1};
|
||||
};
|
||||
CHECK_THROWS_AS(workaround_macro(), fail_fast);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user