Skip to content

Commit b70982a

Browse files
committed
Fix warnings in Wall and Wextra for clang and gcc
1 parent 91db159 commit b70982a

11 files changed

+94
-66
lines changed

.github/workflows/cmake.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ jobs:
1313
fail-fast: false
1414
matrix:
1515
platform:
16-
- { name: Ubuntu GCC, os: ubuntu-latest, compiler: g++, arch: "64", cmakepp: "", flags: "-DCMAKE_CXX_COMPILER=g++ -DCMAKE_CXX_FLAGS=\"--coverage -Wall\""}
17-
- { name: Ubuntu Clang, os: ubuntu-latest, compiler: clang++, arch: "64", cmakepp: "", flags: "-DCMAKE_CXX_COMPILER=clang++ -DCMAKE_CXX_FLAGS=\"-stdlib=libc++ -Weverything\""}
18-
- { name: Windows 32, os: windows-latest, compiler: vs2019, arch: "32", cmakepp: "", flags: "-A Win32 -DCMAKE_CXX_FLAGS=\"/W4\""}
19-
- { name: Windows 64, os: windows-latest, compiler: vs2019, arch: "64", cmakepp: "", flags: "-A x64 -DCMAKE_CXX_FLAGS=\"/W4\""}
20-
- { name: MacOS, os: macos-latest, compiler: clang++, arch: "64", cmakepp: "", flags: "-DCMAKE_CXX_FLAGS=\"-Weverything\""}
21-
- { name: WebAssembly, os: ubuntu-latest, compiler: em++, arch: "32", cmakepp: "emcmake", flags: "-DCMAKE_CXX_FLAGS=\"-Weverything -s DISABLE_EXCEPTION_CATCHING=0\" -DCMAKE_CROSSCOMPILING_EMULATOR=node"}
16+
- { name: Ubuntu GCC, os: ubuntu-latest, compiler: g++, arch: "64", cmakepp: "", flags: "-DCMAKE_CXX_COMPILER=g++ -DCMAKE_CXX_FLAGS=\"--coverage -Wall -Wextra -Werror\""}
17+
- { name: Ubuntu Clang, os: ubuntu-latest, compiler: clang++, arch: "64", cmakepp: "", flags: "-DCMAKE_CXX_COMPILER=clang++ -DCMAKE_CXX_FLAGS=\"-stdlib=libc++ -Wall -Wextra -Werror\""}
18+
- { name: Windows 32, os: windows-latest, compiler: vs2019, arch: "32", cmakepp: "", flags: "-A Win32 -DCMAKE_CXX_FLAGS=\"/W4 /Wx\""}
19+
- { name: Windows 64, os: windows-latest, compiler: vs2019, arch: "64", cmakepp: "", flags: "-A x64 -DCMAKE_CXX_FLAGS=\"/W4 /Wx\""}
20+
- { name: MacOS, os: macos-latest, compiler: clang++, arch: "64", cmakepp: "", flags: "-DCMAKE_CXX_FLAGS=\"-Wall -Wextra -Werror\""}
21+
- { name: WebAssembly, os: ubuntu-latest, compiler: em++, arch: "32", cmakepp: "emcmake", flags: "-DCMAKE_CXX_FLAGS=\"-Wall -Wextra -Werror -s DISABLE_EXCEPTION_CATCHING=0\" -DCMAKE_CROSSCOMPILING_EMULATOR=node"}
2222
build-type:
2323
- Release
2424
- Debug

tests/runtime_tests_observer_assignment_copy.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,10 @@ TEMPLATE_LIST_TEST_CASE(
219219
{
220220
TestType ptr = make_pointer_deleter_1<TestType>();
221221
observer_ptr<TestType> optr{ptr};
222+
WARNING_PUSH;
223+
WARNING_DISABLE_SELF_ASSIGN;
222224
optr = optr;
225+
WARNING_POP;
223226

224227
CHECK(optr.get() == ptr.get());
225228
CHECK(optr.expired() == false);
@@ -237,7 +240,10 @@ TEMPLATE_LIST_TEST_CASE(
237240

238241
{
239242
observer_ptr<TestType> optr;
243+
WARNING_PUSH;
244+
WARNING_DISABLE_SELF_ASSIGN;
240245
optr = optr;
246+
WARNING_POP;
241247

242248
CHECK(optr.get() == nullptr);
243249
CHECK(optr.expired() == true);

tests/runtime_tests_observer_cast_copy.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,9 @@ TEMPLATE_LIST_TEST_CASE("observer dynamic_cast copy from empty", "[cast],[observ
147147
{
148148
auto run_test =
149149
[&]<typename start_type, typename cast_type, typename expected_return_type>() {
150-
TestType ptr0 = make_empty_pointer_deleter_1<TestType>();
151-
get_object<TestType>* raw_ptr = ptr0.get();
152-
observer_ptr<start_type> optr1 = ptr0;
153-
auto optr2 = oup::dynamic_pointer_cast<cast_type>(optr1);
150+
TestType ptr0 = make_empty_pointer_deleter_1<TestType>();
151+
observer_ptr<start_type> optr1 = ptr0;
152+
auto optr2 = oup::dynamic_pointer_cast<cast_type>(optr1);
154153

155154
using return_type = std::remove_cv_t<decltype(optr2)>;
156155

tests/runtime_tests_observer_cast_move.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,8 @@ TEMPLATE_LIST_TEST_CASE("observer dynamic_cast move from empty", "[cast],[observ
147147
{
148148
auto run_test =
149149
[&]<typename start_type, typename cast_type, typename expected_return_type>() {
150-
TestType ptr0 = make_empty_pointer_deleter_1<TestType>();
151-
get_object<TestType>* raw_ptr = ptr0.get();
152-
observer_ptr<start_type> optr1 = ptr0;
150+
TestType ptr0 = make_empty_pointer_deleter_1<TestType>();
151+
observer_ptr<start_type> optr1 = ptr0;
153152
auto optr2 = oup::dynamic_pointer_cast<cast_type>(std::move(optr1));
154153

155154
using return_type = std::remove_cv_t<decltype(optr2)>;
@@ -180,9 +179,8 @@ TEMPLATE_LIST_TEST_CASE(
180179
memory_tracker mem_track;
181180

182181
{
183-
TestType ptr0 = make_pointer_deleter_1<TestType>();
184-
get_object<TestType>* raw_ptr = ptr0.get();
185-
base_observer_ptr<TestType> optr1 = ptr0;
182+
TestType ptr0 = make_pointer_deleter_1<TestType>();
183+
base_observer_ptr<TestType> optr1 = ptr0;
186184
auto optr2 = oup::dynamic_pointer_cast<test_object_dead_end>(std::move(optr1));
187185

188186
using return_type = std::remove_cv_t<decltype(optr2)>;

tests/runtime_tests_owner_cast_move.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,9 @@ TEMPLATE_LIST_TEST_CASE("owner dynamic_cast move from empty", "[cast],[owner]",
158158
{
159159
auto run_test =
160160
[&]<typename start_type, typename cast_type, typename expected_return_type>() {
161-
TestType ptr0 = make_empty_pointer_deleter_1<TestType>();
162-
get_object<TestType>* raw_ptr = ptr0.get();
163-
start_type ptr1 = std::move(ptr0);
164-
auto ptr2 = oup::dynamic_pointer_cast<cast_type>(std::move(ptr1));
161+
TestType ptr0 = make_empty_pointer_deleter_1<TestType>();
162+
start_type ptr1 = std::move(ptr0);
163+
auto ptr2 = oup::dynamic_pointer_cast<cast_type>(std::move(ptr1));
165164

166165
using return_type = std::remove_cv_t<decltype(ptr2)>;
167166

tests/snatch.cpp

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
# define SNATCH_WITH_CXXOPTS 0
1212
#endif
1313

14-
namespace snatch::impl::color {
15-
const char* error_start = "\x1b[1;31m";
16-
const char* warning_start = "\x1b[1;33m";
17-
const char* status_start = "\x1b[1;36m";
18-
const char* fail_start = "\x1b[1;31m";
19-
const char* skipped_start = "\x1b[1;33m";
20-
const char* pass_start = "\x1b[1;32m";
21-
const char* highlight1_start = "\x1b[1;35m";
22-
const char* highlight2_start = "\x1b[1;36m";
23-
const char* reset = "\x1b[0m";
24-
} // namespace snatch::impl::color
14+
namespace { namespace color {
15+
const char* error_start [[maybe_unused]] = "\x1b[1;31m";
16+
const char* warning_start [[maybe_unused]] = "\x1b[1;33m";
17+
const char* status_start [[maybe_unused]] = "\x1b[1;36m";
18+
const char* fail_start [[maybe_unused]] = "\x1b[1;31m";
19+
const char* skipped_start [[maybe_unused]] = "\x1b[1;33m";
20+
const char* pass_start [[maybe_unused]] = "\x1b[1;32m";
21+
const char* highlight1_start [[maybe_unused]] = "\x1b[1;35m";
22+
const char* highlight2_start [[maybe_unused]] = "\x1b[1;36m";
23+
const char* reset [[maybe_unused]] = "\x1b[0m";
24+
}} // namespace ::color
2525

2626
namespace {
2727
using namespace snatch;
@@ -133,9 +133,13 @@ constexpr const char* get_format_code() noexcept {
133133

134134
template<typename T>
135135
bool append_fmt(basic_small_string& ss, T value) noexcept {
136-
const std::size_t length = std::snprintf(ss.end(), 0, get_format_code<T>(), value);
136+
const int return_code = std::snprintf(ss.end(), 0, get_format_code<T>(), value);
137+
if (return_code < 0) {
138+
return false;
139+
}
137140

138-
const bool could_fit = length <= ss.available();
141+
const std::size_t length = static_cast<std::size_t>(return_code);
142+
const bool could_fit = length <= ss.available();
139143

140144
const std::size_t offset = ss.size();
141145
const std::size_t prev_space = ss.available();
@@ -205,19 +209,21 @@ void truncate_end(basic_small_string& ss) noexcept {
205209
} // namespace snatch::impl
206210

207211
namespace snatch::matchers {
208-
contains_substring::contains_substring(std::string_view pattern) noexcept : pattern(pattern) {}
212+
contains_substring::contains_substring(std::string_view pattern) noexcept :
213+
substring_pattern(pattern) {}
209214

210215
bool contains_substring::match(std::string_view message) const noexcept {
211-
return message.find(pattern) != message.npos;
216+
return message.find(substring_pattern) != message.npos;
212217
}
213218

214219
std::string_view contains_substring::describe_fail(std::string_view message) const noexcept {
215-
description.clear();
216-
if (!append(description, "could not find '", pattern, "' in '", message, "'")) {
217-
truncate_end(description);
220+
description_buffer.clear();
221+
if (!append(
222+
description_buffer, "could not find '", substring_pattern, "' in '", message, "'")) {
223+
truncate_end(description_buffer);
218224
}
219225

220-
return description.str();
226+
return description_buffer.str();
221227
}
222228

223229
with_what_contains::with_what_contains(std::string_view pattern) noexcept :

tests/snatch/snatch.hpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,8 @@ const char* proxy<std::tuple<Args...>>::operator=(const F& func) noexcept {
531531

532532
namespace snatch::matchers {
533533
struct contains_substring {
534-
mutable impl::small_string<max_matcher_msg_length> description;
535-
std::string_view pattern;
534+
mutable impl::small_string<max_matcher_msg_length> description_buffer;
535+
std::string_view substring_pattern;
536536

537537
explicit contains_substring(std::string_view pattern) noexcept;
538538

@@ -563,6 +563,26 @@ struct with_what_contains : private contains_substring {
563563
#define TESTING_MACRO_CONCAT(x, y) TESTING_CONCAT_IMPL(x, y)
564564
#define TESTING_EXPR(x) snatch::impl::expression{} <= x
565565

566+
// clang-format off
567+
#if defined(__clang__)
568+
# define WARNING_PUSH _Pragma("clang diagnostic push")
569+
# define WARNING_POP _Pragma("clang diagnostic pop")
570+
# define WARNING_DISABLE_PARENTHESES do {} while (0)
571+
#elif defined(__GNUC__)
572+
# define WARNING_PUSH _Pragma("GCC diagnostic push")
573+
# define WARNING_POP _Pragma("GCC diagnostic pop")
574+
# define WARNING_DISABLE_PARENTHESES _Pragma("GCC diagnostic ignored \"-Wparentheses\"")
575+
#elif defined(_MSC_VER)
576+
# define WARNING_PUSH do {} while (0)
577+
# define WARNING_POP do {} while (0)
578+
# define WARNING_DISABLE_PARENTHESES do {} while (0)
579+
#else
580+
# define WARNING_PUSH do {} while (0)
581+
# define WARNING_POP do {} while (0)
582+
# define WARNING_DISABLE_PARENTHESES do {} while (0)
583+
#endif
584+
// clang-format on
585+
566586
#define TEST_CASE(NAME, TAGS) \
567587
static const char* TESTING_MACRO_CONCAT(test_id_, __COUNTER__) = \
568588
snatch::tests.add(NAME, TAGS) = \
@@ -577,7 +597,10 @@ struct with_what_contains : private contains_substring {
577597
do { \
578598
++CURRENT_CASE.tests; \
579599
if (!(EXP)) { \
600+
WARNING_PUSH; \
601+
WARNING_DISABLE_PARENTHESES; \
580602
const auto EXP2 = TESTING_EXPR(EXP); \
603+
WARNING_POP; \
581604
snatch::tests.print_failure(); \
582605
snatch::tests.print_location(CURRENT_CASE, __FILE__, __LINE__); \
583606
snatch::tests.print_details_expr("REQUIRE", #EXP, EXP2); \
@@ -589,7 +612,10 @@ struct with_what_contains : private contains_substring {
589612
do { \
590613
++CURRENT_CASE.tests; \
591614
if (!(EXP)) { \
615+
WARNING_PUSH; \
616+
WARNING_DISABLE_PARENTHESES; \
592617
const auto EXP2 = TESTING_EXPR(EXP); \
618+
WARNING_POP; \
593619
snatch::tests.print_failure(); \
594620
snatch::tests.print_location(CURRENT_CASE, __FILE__, __LINE__); \
595621
snatch::tests.print_details_expr("CHECK", #EXP, EXP2); \

tests/speed_benchmark.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void do_report(const char* name, const R& which) {
8686
}
8787

8888
double median(std::vector<double> v) {
89-
const auto n = v.size() / 2;
89+
const auto n = static_cast<std::ptrdiff_t>(v.size() / 2);
9090
std::nth_element(v.begin(), v.begin() + n, v.end());
9191
return *(v.begin() + n);
9292
}
@@ -172,7 +172,7 @@ int main() {
172172
std::cout << std::endl;
173173

174174
std::cout << "|---|---|";
175-
for (const auto& t : cols) {
175+
for (const auto& t [[maybe_unused]] : cols) {
176176
std::cout << "---|";
177177
}
178178
std::cout << std::endl;

tests/testing.hpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1 @@
1-
// #include <catch2/catch_template_test_macros.hpp>
2-
3-
// // Replace Catch2's REQUIRE_THROWS_AS, which allocates on Windows;
4-
// // this confuses our memory leak checks.
5-
// #undef REQUIRE_THROWS_AS
6-
// #define REQUIRE_THROWS_AS(EXPRESSION, EXCEPTION) \
7-
// do { \
8-
// try { \
9-
// EXPRESSION; \
10-
// FAIL("no exception thrown"); \
11-
// } catch (const EXCEPTION&) { \
12-
// } catch (const std::exception& e) { \
13-
// FAIL(e.what()); \
14-
// } catch (...) { \
15-
// FAIL("unexpected exception thrown"); \
16-
// } \
17-
// } while (0)
18-
191
#include "snatch/snatch.hpp"

tests/tests_common.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ test_object::test_object() {
1616
next_test_object_constructor_throws = false;
1717

1818
if constexpr (debug_instances) {
19-
std::printf("%p creation throws\n", this);
19+
std::printf("%p creation throws\n", static_cast<void*>(this));
2020
}
2121

2222
throw throw_constructor{};
2323
}
2424

2525
if constexpr (debug_instances) {
26-
std::printf("%p created\n", this);
26+
std::printf("%p created\n", static_cast<void*>(this));
2727
}
2828

2929
++instances;
@@ -34,22 +34,22 @@ test_object::test_object(state s) : state_(s) {
3434
next_test_object_constructor_throws = false;
3535

3636
if constexpr (debug_instances) {
37-
std::printf("%p creation throws\n", this);
37+
std::printf("%p creation throws\n", static_cast<void*>(this));
3838
}
3939

4040
throw throw_constructor{};
4141
}
4242

4343
if constexpr (debug_instances) {
44-
std::printf("%p created\n", this);
44+
std::printf("%p created\n", static_cast<void*>(this));
4545
}
4646

4747
++instances;
4848
}
4949

5050
test_object::~test_object() noexcept {
5151
if constexpr (debug_instances) {
52-
std::printf("%p deleted\n", this);
52+
std::printf("%p deleted\n", static_cast<void*>(this));
5353
}
5454

5555
--instances;

0 commit comments

Comments
 (0)