Skip to content

Commit e27ccfa

Browse files
authored
Merge pull request #134 from Flamefire/silence-override-warning
Supress 'suggest-override' warnings in mocked methods
2 parents 53aa393 + 90f2b84 commit e27ccfa

11 files changed

Lines changed: 72 additions & 40 deletions

File tree

doc/example/limitations_const_parameter_warning.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace limitations_const_parameter_warning_explanation {
2424
class derived : public base
2525
{
2626
public:
27-
virtual void method(const int);
27+
void method(const int) override;
2828
};
2929

3030
void derived::method(int) {}
@@ -35,7 +35,7 @@ namespace {
3535
//[ limitations_const_parameter_warning_solution
3636
MOCK_BASE_CLASS(mock_base, base)
3737
{
38-
void method(const int i) { method_stub(i); }
38+
void method(const int i) override { method_stub(i); }
3939
MOCK_METHOD(method_stub, 1, void(int), method)
4040
};
4141
//]

doc/example/motivation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class my_view : public view
4040
{
4141
public:
4242
my_view() : called(false) {}
43-
virtual void display(int result)
43+
void display(int result) override
4444
{
4545
called = true;
4646
value = result;

include/turtle/config.hpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// http://turtle.sourceforge.net
22
//
33
// Copyright Mathieu Champlon 2009
4-
// Copyright 2020-2025 Alexander Grund
4+
// Copyright 2020-2026 Alexander Grund
55
//
66
// Distributed under the Boost Software License, Version 1.0.
77
// (See accompanying file LICENSE_1_0.txt or copy at
@@ -32,6 +32,27 @@
3232
# endif
3333
#endif
3434

35+
#if defined(__clang__) && defined(__has_warning)
36+
# define MOCK_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push")
37+
# define MOCK_DIAGNOSTIC_POP _Pragma("clang diagnostic pop")
38+
# if __has_warning("-Wsuggest-override")
39+
# define MOCK_DIAGNOSTIC_IGNORE_SUGGEST_OVERRIDE _Pragma("clang diagnostic ignored \"-Wsuggest-override\"")
40+
# endif
41+
#elif defined(__GNUC__)
42+
# define MOCK_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
43+
# define MOCK_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
44+
# if(__GNUC__ >= 9)
45+
# define MOCK_DIAGNOSTIC_IGNORE_SUGGEST_OVERRIDE _Pragma("GCC diagnostic ignored \"-Wsuggest-override\"")
46+
# endif
47+
#endif
48+
#ifndef MOCK_DIAGNOSTIC_PUSH
49+
# define MOCK_DIAGNOSTIC_PUSH
50+
# define MOCK_DIAGNOSTIC_POP
51+
#endif
52+
#ifndef MOCK_DIAGNOSTIC_IGNORE_SUGGEST_OVERRIDE
53+
# define MOCK_DIAGNOSTIC_IGNORE_SUGGEST_OVERRIDE
54+
#endif
55+
3556
#if BOOST_VERSION >= 107700
3657
# define MOCK_CXX_VERSION BOOST_CXX_VERSION
3758
#elif defined(_MSC_VER)

include/turtle/detail/function_impl.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ namespace mock { namespace detail {
102102
context_->remove(*this);
103103
}
104104

105-
virtual bool verify() const
105+
bool verify() const override
106106
{
107107
lock _(mutex_);
108108
for(const auto& expectation : expectations_)
@@ -120,7 +120,7 @@ namespace mock { namespace detail {
120120
return valid_;
121121
}
122122

123-
virtual void reset()
123+
void reset() override
124124
{
125125
lock _(mutex_);
126126
valid_ = true;

include/turtle/detail/invocation.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,24 @@ namespace mock { namespace detail {
4444
throw std::invalid_argument("'min' > 'max'");
4545
}
4646

47-
virtual bool invoke()
47+
bool invoke() override
4848
{
4949
if(count_ == max_)
5050
return false;
5151
++count_;
5252
return true;
5353
}
5454

55-
virtual bool exhausted() const { return count_ >= max_; }
55+
bool exhausted() const override { return count_ >= max_; }
5656

57-
virtual bool verify() const { return min_ <= count_ && count_ <= max_; }
57+
bool verify() const override { return min_ <= count_ && count_ <= max_; }
5858

5959
protected:
6060
const std::size_t min_, max_;
6161
std::size_t count_;
6262

6363
private:
64-
virtual std::ostream& serialize(std::ostream& s) const
64+
std::ostream& serialize(std::ostream& s) const override
6565
{
6666
return s << "between( " << count_ << "/[" << min_ << ',' << max_ << "] )";
6767
}
@@ -73,7 +73,7 @@ namespace mock { namespace detail {
7373
explicit exactly(std::size_t count) : between(count, count) {}
7474

7575
private:
76-
virtual std::ostream& serialize(std::ostream& s) const
76+
std::ostream& serialize(std::ostream& s) const override
7777
{
7878
return s << "exactly( " << count_ << '/' << max_ << " )";
7979
}
@@ -85,7 +85,7 @@ namespace mock { namespace detail {
8585
never() : exactly(0) {}
8686

8787
private:
88-
virtual std::ostream& serialize(std::ostream& s) const { return s << "never()"; }
88+
std::ostream& serialize(std::ostream& s) const override { return s << "never()"; }
8989
};
9090

9191
class once : public exactly
@@ -94,7 +94,7 @@ namespace mock { namespace detail {
9494
once() : exactly(1) {}
9595

9696
private:
97-
virtual std::ostream& serialize(std::ostream& s) const { return s << "once()"; }
97+
std::ostream& serialize(std::ostream& s) const override { return s << "once()"; }
9898
};
9999

100100
class at_least : public between
@@ -103,7 +103,7 @@ namespace mock { namespace detail {
103103
explicit at_least(std::size_t min) : between(min, (std::numeric_limits<std::size_t>::max)()) {}
104104

105105
private:
106-
virtual std::ostream& serialize(std::ostream& s) const
106+
std::ostream& serialize(std::ostream& s) const override
107107
{
108108
return s << "at_least( " << count_ << '/' << min_ << " )";
109109
}
@@ -115,7 +115,7 @@ namespace mock { namespace detail {
115115
explicit at_most(std::size_t max) : between(0, max) {}
116116

117117
private:
118-
virtual std::ostream& serialize(std::ostream& s) const
118+
std::ostream& serialize(std::ostream& s) const override
119119
{
120120
return s << "at_most( " << count_ << '/' << max_ << " )";
121121
}
@@ -127,7 +127,7 @@ namespace mock { namespace detail {
127127
unlimited() : at_least(0) {}
128128

129129
private:
130-
virtual std::ostream& serialize(std::ostream& s) const { return s << "unlimited()"; }
130+
std::ostream& serialize(std::ostream& s) const override { return s << "unlimited()"; }
131131
};
132132
}} // namespace mock::detail
133133

include/turtle/detail/mock_impl.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// http://turtle.sourceforge.net
22
//
33
// Copyright Mathieu Champlon 2008
4-
// Copyright 2022-2025 Alexander Grund
4+
// Copyright 2022-2026 Alexander Grund
55
//
66
// Distributed under the Boost Software License, Version 1.0.
77
// (See accompanying file LICENSE_1_0.txt or copy at
@@ -60,10 +60,13 @@ namespace mock { namespace detail {
6060
#define MOCK_FORWARD_PARAM(z, n, S) std::forward<MOCK_PARAM(S, n)>(p##n)
6161
#define MOCK_FORWARD_PARAMS(n, S) BOOST_PP_ENUM(n, MOCK_FORWARD_PARAM, S)
6262
#define MOCK_METHOD_AUX(name, arity, signature, identifier, qualifier) \
63+
MOCK_DIAGNOSTIC_PUSH \
64+
MOCK_DIAGNOSTIC_IGNORE_SUGGEST_OVERRIDE \
6365
MOCK_DECL(name, arity, signature, qualifier) \
6466
{ \
6567
return MOCK_ANONYMOUS_HELPER(identifier)(MOCK_FORWARD_PARAMS(arity, signature)); \
66-
}
68+
} \
69+
MOCK_DIAGNOSTIC_POP
6770

6871
#define MOCK_METHOD_EXT(name, arity, signature, identifier) \
6972
MOCK_METHOD_AUX(name, arity, signature, identifier, ) \

include/turtle/detail/object_impl.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,23 @@ namespace mock { namespace detail {
2525
public:
2626
object_impl() : mutex_(std::make_shared<mutex>()) {}
2727

28-
virtual void add(const void* /*p*/,
29-
verifiable& v,
30-
boost::unit_test::const_string instance,
31-
boost::optional<type_name> type,
32-
boost::unit_test::const_string name)
28+
void add(const void* /*p*/,
29+
verifiable& v,
30+
boost::unit_test::const_string instance,
31+
boost::optional<type_name> type,
32+
boost::unit_test::const_string name) override
3333
{
3434
lock _(mutex_);
3535
if(children_.empty())
3636
detail::root.add(*this);
3737
children_[&v].update(parent_, instance, type, name);
3838
}
39-
virtual void add(verifiable& v)
39+
void add(verifiable& v) override
4040
{
4141
lock _(mutex_);
4242
group_.add(v);
4343
}
44-
virtual void remove(verifiable& v)
44+
void remove(verifiable& v) override
4545
{
4646
lock _(mutex_);
4747
group_.remove(v);
@@ -50,7 +50,7 @@ namespace mock { namespace detail {
5050
detail::root.remove(*this);
5151
}
5252

53-
virtual void serialize(std::ostream& s, const verifiable& v) const
53+
void serialize(std::ostream& s, const verifiable& v) const override
5454
{
5555
lock _(mutex_);
5656
const auto it = children_.find(&v);
@@ -60,12 +60,12 @@ namespace mock { namespace detail {
6060
s << "?";
6161
}
6262

63-
virtual bool verify() const
63+
bool verify() const override
6464
{
6565
lock _(mutex_);
6666
return group_.verify();
6767
}
68-
virtual void reset()
68+
void reset() override
6969
{
7070
lock _(mutex_);
7171
std::shared_ptr<object_impl> guard = shared_from_this();

include/turtle/detail/root.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,25 @@ namespace mock { namespace detail {
2424
class root_t : public singleton<root_t>, public context
2525
{
2626
public:
27-
virtual void add(const void* p,
28-
verifiable& v,
29-
boost::unit_test::const_string instance,
30-
boost::optional<type_name> type,
31-
boost::unit_test::const_string name)
27+
void add(const void* p,
28+
verifiable& v,
29+
boost::unit_test::const_string instance,
30+
boost::optional<type_name> type,
31+
boost::unit_test::const_string name) override
3232
{
3333
scoped_lock _(mutex_);
3434
auto it = children_.lower_bound(&v);
3535
if(it == children_.end() || children_.key_comp()(&v, it->first))
3636
it = children_.insert(it, std::make_pair(&v, counter_child(parents_, p)));
3737
it->second.update(instance, type, name);
3838
}
39-
virtual void add(verifiable& v)
39+
void add(verifiable& v) override
4040
{
4141
scoped_lock _(mutex_);
4242
group_.add(v);
4343
}
4444

45-
virtual void remove(verifiable& v)
45+
void remove(verifiable& v) override
4646
{
4747
scoped_lock _(mutex_);
4848
group_.remove(v);
@@ -60,7 +60,7 @@ namespace mock { namespace detail {
6060
group_.reset();
6161
}
6262

63-
virtual void serialize(std::ostream& s, const verifiable& v) const
63+
void serialize(std::ostream& s, const verifiable& v) const override
6464
{
6565
scoped_lock _(mutex_);
6666
const auto it = children_.find(&v);

include/turtle/stream.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace detail { namespace conversion {
4646
struct holder_imp : holder
4747
{
4848
explicit holder_imp(const T& t) : t_(t) {}
49-
virtual void serialize(std::ostream& s) const
49+
void serialize(std::ostream& s) const override
5050
{
5151
// if an error about an ambiguous conversion is generated by the
5252
// line below the solution is to add a serialization operator to a

test/CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2019 Alexander Grund
1+
# Copyright 2019-2026 Alexander Grund
22
# Distributed under the Boost Software License, Version 1.0.
33
# See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt
44

@@ -16,6 +16,14 @@ option(TURTLE_WERROR "Treat warnings as errors" ON)
1616
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
1717
target_compile_options(TurtleTestMain INTERFACE -Wall -Wextra -pedantic)
1818
include(CheckCXXCompilerFlag)
19+
# Common warning to check for consistent use of override/final
20+
check_cxx_compiler_flag(-Wsuggest-override TURTLE_CXX_SUGGEST_OVERRIDE)
21+
if(TURTLE_CXX_SUGGEST_OVERRIDE)
22+
# Prior to GCC 9.2 "final" was not considered "override"
23+
if(NOT (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.2))
24+
target_compile_options(TurtleTestMain INTERFACE -Wsuggest-override)
25+
endif()
26+
endif()
1927
check_cxx_compiler_flag(-Wunused-function TURTLE_CXX_UNUSED_FUNCTION)
2028
if(TURTLE_CXX_UNUSED_FUNCTION)
2129
target_compile_options(TurtleTestMain INTERFACE -Wno-unused-function)

0 commit comments

Comments
 (0)