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:
Anna Gringauze
2018-08-12 21:44:17 -07:00
committed by Neil MacIntosh
parent 6a75903c79
commit cea0d0ac2b
25 changed files with 940 additions and 503 deletions
+68 -20
View File
@@ -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 26497) // from catch
#endif
#include <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK, TEST_...
#include <gsl/gsl_byte> // for byte
@@ -47,6 +54,7 @@ struct DerivedClass : BaseClass
};
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("default_constructor")
{
{
@@ -81,6 +89,7 @@ TEST_CASE("default_constructor")
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("size_optimization")
{
{
@@ -94,56 +103,60 @@ TEST_CASE("size_optimization")
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("from_nullptr_size_constructor")
{
{
span<int> s{nullptr, static_cast<span<int>::index_type>(0)};
span<int> s{nullptr, narrow_cast<span<int>::index_type>(0)};
CHECK((s.size() == 0 && s.data() == nullptr));
span<const int> cs{nullptr, static_cast<span<int>::index_type>(0)};
span<const int> cs{nullptr, narrow_cast<span<int>::index_type>(0)};
CHECK((cs.size() == 0 && cs.data() == nullptr));
}
{
span<int, 0> s{nullptr, static_cast<span<int>::index_type>(0)};
span<int, 0> s{nullptr, narrow_cast<span<int>::index_type>(0)};
CHECK((s.size() == 0 && s.data() == nullptr));
span<const int, 0> cs{nullptr, static_cast<span<int>::index_type>(0)};
span<const int, 0> cs{nullptr, narrow_cast<span<int>::index_type>(0)};
CHECK((cs.size() == 0 && cs.data() == nullptr));
}
{
auto workaround_macro = []() {
span<int, 1> s{nullptr, static_cast<span<int>::index_type>(0)};
const span<int, 1> s{nullptr, narrow_cast<span<int>::index_type>(0)};
};
CHECK_THROWS_AS(workaround_macro(), fail_fast);
}
{
auto workaround_macro = []() { span<int> s{nullptr, 1}; };
auto workaround_macro = []() { const span<int> s{nullptr, 1}; };
CHECK_THROWS_AS(workaround_macro(), fail_fast);
auto const_workaround_macro = []() { span<const int> cs{nullptr, 1}; };
auto const_workaround_macro = []() { const span<const int> cs{nullptr, 1}; };
CHECK_THROWS_AS(const_workaround_macro(), fail_fast);
}
{
auto workaround_macro = []() { span<int, 0> s{nullptr, 1}; };
auto workaround_macro = []() { const span<int, 0> s{nullptr, 1}; };
CHECK_THROWS_AS(workaround_macro(), fail_fast);
auto const_workaround_macro = []() { span<const int, 0> s{nullptr, 1}; };
auto const_workaround_macro = []() { const span<const int, 0> s{nullptr, 1}; };
CHECK_THROWS_AS(const_workaround_macro(), fail_fast);
}
{
span<int*> s{nullptr, static_cast<span<int>::index_type>(0)};
span<int*> s{nullptr, narrow_cast<span<int>::index_type>(0)};
CHECK((s.size() == 0 && s.data() == nullptr));
span<const int*> cs{nullptr, static_cast<span<int>::index_type>(0)};
span<const int*> cs{nullptr, narrow_cast<span<int>::index_type>(0)};
CHECK((cs.size() == 0 && cs.data() == nullptr));
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
TEST_CASE("from_pointer_length_constructor")
{
int arr[4] = {1, 2, 3, 4};
@@ -164,7 +177,7 @@ TEST_CASE("from_pointer_length_constructor")
}
}
{
span<int> s = { &arr[i], 4-i };
span<int> s = { &arr[i], 4-narrow_cast<ptrdiff_t>(i) };
CHECK(s.size() == 4-i);
CHECK(s.data() == &arr[i]);
CHECK(s.empty() == (4-i == 0));
@@ -186,13 +199,13 @@ TEST_CASE("from_pointer_length_constructor")
{
int* p = nullptr;
span<int> s{p, static_cast<span<int>::index_type>(0)};
span<int> s{p, narrow_cast<span<int>::index_type>(0)};
CHECK((s.size() == 0 && s.data() == nullptr));
}
{
int* p = nullptr;
auto workaround_macro = [=]() { span<int> s{p, 2}; };
auto workaround_macro = [=]() { const span<int> s{p, 2}; };
CHECK_THROWS_AS(workaround_macro(), fail_fast);
}
@@ -204,7 +217,7 @@ TEST_CASE("from_pointer_length_constructor")
{
int* p = nullptr;
auto s = make_span(p, static_cast<span<int>::index_type>(0));
auto s = make_span(p, narrow_cast<span<int>::index_type>(0));
CHECK((s.size() == 0 && s.data() == nullptr));
}
@@ -215,6 +228,8 @@ TEST_CASE("from_pointer_length_constructor")
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("from_pointer_pointer_constructor")
{
int arr[4] = {1, 2, 3, 4};
@@ -291,6 +306,8 @@ TEST_CASE("from_pointer_pointer_constructor")
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.3) // NO-FORMAT: attribute // TODO: false positive?
TEST_CASE("from_array_constructor")
{
int arr[5] = {1, 2, 3, 4, 5};
@@ -333,7 +350,7 @@ TEST_CASE("from_array_constructor")
}
#endif
{
span<int[3]> s{&(arr2d[0]), 1};
span<int[3]> s{&arr2d[0], 1};
CHECK((s.size() == 1 && s.data() == &arr2d[0]));
}
@@ -382,6 +399,10 @@ TEST_CASE("from_array_constructor")
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
GSL_SUPPRESS(r.11) // NO-FORMAT: attribute
GSL_SUPPRESS(i.11) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
TEST_CASE("from_dynamic_array_constructor")
{
double(*arr)[3][4] = new double[100][3][4];
@@ -399,6 +420,8 @@ TEST_CASE("from_dynamic_array_constructor")
delete[] arr;
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("from_std_array_constructor")
{
std::array<int, 4> arr = {1, 2, 3, 4};
@@ -478,6 +501,7 @@ TEST_CASE("from_std_array_constructor")
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("from_const_std_array_constructor")
{
const std::array<int, 4> arr = {1, 2, 3, 4};
@@ -521,6 +545,7 @@ TEST_CASE("from_const_std_array_constructor")
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("from_std_array_const_constructor")
{
std::array<const int, 4> arr = {1, 2, 3, 4};
@@ -561,6 +586,7 @@ TEST_CASE("from_std_array_const_constructor")
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("from_container_constructor")
{
std::vector<int> v = {1, 2, 3};
@@ -653,6 +679,7 @@ TEST_CASE("from_container_constructor")
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("from_convertible_span_constructor")
{
{
@@ -690,6 +717,7 @@ TEST_CASE("from_convertible_span_constructor")
#endif
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("copy_move_and_assignment")
{
span<int> s1;
@@ -711,6 +739,7 @@ TEST_CASE("copy_move_and_assignment")
CHECK((s1.size() == 2 && s1.data() == &arr[1]));
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("first")
{
int arr[5] = {1, 2, 3, 4, 5};
@@ -749,6 +778,7 @@ TEST_CASE("first")
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("last")
{
int arr[5] = {1, 2, 3, 4, 5};
@@ -786,6 +816,7 @@ TEST_CASE("last")
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("subspan")
{
int arr[5] = {1, 2, 3, 4, 5};
@@ -867,6 +898,7 @@ TEST_CASE("subspan")
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("at_call")
{
int arr[4] = {1, 2, 3, 4};
@@ -886,6 +918,7 @@ TEST_CASE("at_call")
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("operator_function_call")
{
int arr[4] = {1, 2, 3, 4};
@@ -905,6 +938,7 @@ TEST_CASE("operator_function_call")
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("iterator_default_init")
{
span<int>::iterator it1;
@@ -912,6 +946,7 @@ TEST_CASE("iterator_default_init")
CHECK(it1 == it2);
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("const_iterator_default_init")
{
span<int>::const_iterator it1;
@@ -919,6 +954,7 @@ TEST_CASE("const_iterator_default_init")
CHECK(it1 == it2);
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("iterator_conversions")
{
span<int>::iterator badIt;
@@ -941,6 +977,7 @@ TEST_CASE("iterator_conversions")
CHECK(cit3 == s.cend());
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("iterator_comparisons")
{
int a[] = {1, 2, 3, 4};
@@ -988,6 +1025,7 @@ TEST_CASE("iterator_comparisons")
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("begin_end")
{
{
@@ -1043,6 +1081,7 @@ TEST_CASE("begin_end")
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("cbegin_cend")
{
{
@@ -1095,6 +1134,7 @@ TEST_CASE("cbegin_cend")
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("rbegin_rend")
{
{
@@ -1137,6 +1177,7 @@ TEST_CASE("rbegin_rend")
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("crbegin_crend")
{
{
@@ -1176,6 +1217,7 @@ TEST_CASE("crbegin_crend")
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("comparison_operators")
{
{
@@ -1296,6 +1338,7 @@ TEST_CASE("comparison_operators")
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("as_bytes")
{
int a[] = {1, 2, 3, 4};
@@ -1326,6 +1369,7 @@ TEST_CASE("as_bytes")
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("as_writeable_bytes")
{
int a[] = {1, 2, 3, 4};
@@ -1359,6 +1403,7 @@ TEST_CASE("as_writeable_bytes")
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("fixed_size_conversions")
{
int arr[] = {1, 2, 3, 4};
@@ -1389,7 +1434,7 @@ TEST_CASE("fixed_size_conversions")
{
span<int> s = arr;
auto f = [&]() {
span<int, 2> s2 = s;
const span<int, 2> s2 = s;
static_cast<void>(s2);
};
CHECK_THROWS_AS(f(), fail_fast);
@@ -1399,7 +1444,7 @@ TEST_CASE("fixed_size_conversions")
// you can convert statically
{
const span<int, 2> s2 = {arr, 2};
const span<int, 2> s2 = {&arr[0], 2};
static_cast<void>(s2);
}
{
@@ -1428,7 +1473,7 @@ TEST_CASE("fixed_size_conversions")
#endif
{
auto f = [&]() {
span<int, 4> _s4 = {arr2, 2};
const span<int, 4> _s4 = {arr2, 2};
static_cast<void>(_s4);
};
CHECK_THROWS_AS(f(), fail_fast);
@@ -1437,12 +1482,13 @@ TEST_CASE("fixed_size_conversions")
// this should fail - we are trying to assign a small dynamic span to a fixed_size larger one
span<int> av = arr2;
auto f = [&]() {
span<int, 4> _s4 = av;
const span<int, 4> _s4 = av;
static_cast<void>(_s4);
};
CHECK_THROWS_AS(f(), fail_fast);
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("interop_with_std_regex")
{
char lat[] = {'1', '2', '3', '4', '5', '6', 'E', 'F', 'G'};
@@ -1466,6 +1512,7 @@ TEST_CASE("interop_with_std_regex")
CHECK(match[0].second == (f_it + 1));
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("interop_with_gsl_at")
{
int arr[5] = {1, 2, 3, 4, 5};
@@ -1479,3 +1526,4 @@ TEST_CASE("default_constructible")
CHECK((std::is_default_constructible<span<int, 0>>::value));
CHECK((!std::is_default_constructible<span<int, 42>>::value));
}