1
0
mirror of https://github.com/catchorg/Catch2.git synced 2025-01-16 15:18:00 +00:00

Compare commits

...

3 Commits

Author SHA1 Message Date
Martin Hořeňovský
24559493bf
Anchor some Clara vtables into the cpp file
Anchoring the vtables does 2 things
1) Fixes some instances of `-Wweak-vtables`
2) Decreases code size and linker pressure

However, there are still some unanchored ones, and thus we have
to keep suppressing `-Wweak-vtables` warning for Clang.
2020-08-20 23:16:11 +02:00
Martin Hořeňovský
7500ad1ffd
Turn Clara::Detail::ResultBase::Type into its own enum class 2020-08-20 22:59:06 +02:00
Martin Hořeňovský
1a97af45f1
Cleanup some stuff found by MSVC's /analyze
* Added some missing `noexcept`s on custom destructors.
* Fixed `std::move` being called on a const-reference.
* Initialized `ScopedMessage::m_moved` in class definition, instead
of doing so in constructors explicitly.
* Turned some `enum`s into `enum class`es.
* Initialized `StreamingReporterBase::currentTestCaseInfo` in class
definition.
* Some cleanups in SelfTest code.
2020-08-20 20:42:21 +02:00
10 changed files with 78 additions and 58 deletions

View File

@ -81,14 +81,15 @@ namespace Catch {
BenchmarkFunction(Fun&& fun)
: f(new model<typename std::decay<Fun>::type>(std::forward<Fun>(fun))) {}
BenchmarkFunction(BenchmarkFunction&& that)
: f(std::move(that.f)) {}
BenchmarkFunction( BenchmarkFunction&& that ) noexcept:
f( std::move( that.f ) ) {}
BenchmarkFunction(BenchmarkFunction const& that)
: f(that.f->clone()) {}
BenchmarkFunction& operator=(BenchmarkFunction&& that) {
f = std::move(that.f);
BenchmarkFunction&
operator=( BenchmarkFunction&& that ) noexcept {
f = std::move( that.f );
return *this;
}

View File

@ -26,16 +26,14 @@ namespace Catch {
////////////////////////////////////////////////////////////////////////////
ScopedMessage::ScopedMessage( MessageBuilder const& builder )
: m_info( builder.m_info ), m_moved()
{
ScopedMessage::ScopedMessage( MessageBuilder const& builder ):
m_info( builder.m_info ) {
m_info.message = builder.m_stream.str();
getResultCapture().pushScopedMessage( m_info );
}
ScopedMessage::ScopedMessage( ScopedMessage&& old )
: m_info( old.m_info ), m_moved()
{
ScopedMessage::ScopedMessage( ScopedMessage&& old ) noexcept:
m_info( std::move( old.m_info ) ) {
old.m_moved = true;
}

View File

@ -49,11 +49,11 @@ namespace Catch {
public:
explicit ScopedMessage( MessageBuilder const& builder );
ScopedMessage( ScopedMessage& duplicate ) = delete;
ScopedMessage( ScopedMessage&& old );
ScopedMessage( ScopedMessage&& old ) noexcept;
~ScopedMessage();
MessageInfo m_info;
bool m_moved;
bool m_moved = false;
};
class Capturer {

View File

@ -111,6 +111,8 @@ namespace Catch {
return ParserResult::ok( ParseResultType::Matched );
}
size_t ParserBase::cardinality() const { return 1; }
InternalParseResult ParserBase::parse( Args const& args ) const {
return parse( args.exeName(), TokenStream( args ) );
}
@ -124,7 +126,15 @@ namespace Catch {
return ParserResult::ok( ParseResultType::Matched );
}
} // namespace Detail
ResultBase::~ResultBase() = default;
bool BoundRef::isContainer() const { return false; }
bool BoundRef::isFlag() const { return false; }
bool BoundFlagRefBase::isFlag() const { return true; }
} // namespace Detail
Detail::InternalParseResult Arg::parse(std::string const&,
Detail::TokenStream const& tokens) const {

View File

@ -121,17 +121,28 @@ namespace Catch {
TokenStream& operator++();
};
class ResultBase {
public:
enum Type { Ok, LogicError, RuntimeError };
//! Denotes type of a parsing result
enum class ResultType {
Ok, ///< No errors
LogicError, ///< Error in user-specified arguments for
///< construction
RuntimeError ///< Error in parsing inputs
};
class ResultBase {
protected:
ResultBase( Type type ): m_type( type ) {}
virtual ~ResultBase() = default;
ResultBase( ResultType type ): m_type( type ) {}
virtual ~ResultBase(); // = default;
ResultBase(ResultBase const&) = default;
ResultBase& operator=(ResultBase const&) = default;
ResultBase(ResultBase&&) = default;
ResultBase& operator=(ResultBase&&) = default;
virtual void enforceOk() const = 0;
Type m_type;
ResultType m_type;
};
template <typename T> class ResultValueBase : public ResultBase {
@ -142,30 +153,30 @@ namespace Catch {
}
protected:
ResultValueBase( Type type ): ResultBase( type ) {}
ResultValueBase( ResultType type ): ResultBase( type ) {}
ResultValueBase( ResultValueBase const& other ):
ResultBase( other ) {
if ( m_type == ResultBase::Ok )
if ( m_type == ResultType::Ok )
new ( &m_value ) T( other.m_value );
}
ResultValueBase( Type, T const& value ): ResultBase( Ok ) {
ResultValueBase( ResultType, T const& value ): ResultBase( ResultType::Ok ) {
new ( &m_value ) T( value );
}
auto operator=( ResultValueBase const& other )
-> ResultValueBase& {
if ( m_type == ResultBase::Ok )
if ( m_type == ResultType::Ok )
m_value.~T();
ResultBase::operator=( other );
if ( m_type == ResultBase::Ok )
if ( m_type == ResultType::Ok )
new ( &m_value ) T( other.m_value );
return *this;
}
~ResultValueBase() override {
if ( m_type == Ok )
if ( m_type == ResultType::Ok )
m_value.~T();
}
@ -186,27 +197,27 @@ namespace Catch {
explicit BasicResult( BasicResult<U> const& other ):
ResultValueBase<T>( other.type() ),
m_errorMessage( other.errorMessage() ) {
assert( type() != ResultBase::Ok );
assert( type() != ResultType::Ok );
}
template <typename U>
static auto ok( U const& value ) -> BasicResult {
return { ResultBase::Ok, value };
return { ResultType::Ok, value };
}
static auto ok() -> BasicResult { return { ResultBase::Ok }; }
static auto ok() -> BasicResult { return { ResultType::Ok }; }
static auto logicError( std::string const& message )
-> BasicResult {
return { ResultBase::LogicError, message };
return { ResultType::LogicError, message };
}
static auto runtimeError( std::string const& message )
-> BasicResult {
return { ResultBase::RuntimeError, message };
return { ResultType::RuntimeError, message };
}
explicit operator bool() const {
return m_type == ResultBase::Ok;
return m_type == ResultType::Ok;
}
auto type() const -> ResultBase::Type { return m_type; }
auto type() const -> ResultType { return m_type; }
auto errorMessage() const -> std::string {
return m_errorMessage;
}
@ -216,19 +227,19 @@ namespace Catch {
// Errors shouldn't reach this point, but if they do
// the actual error message will be in m_errorMessage
assert( m_type != ResultBase::LogicError );
assert( m_type != ResultBase::RuntimeError );
if ( m_type != ResultBase::Ok )
assert( m_type != ResultType::LogicError );
assert( m_type != ResultType::RuntimeError );
if ( m_type != ResultType::Ok )
std::abort();
}
std::string
m_errorMessage; // Only populated if resultType is an error
BasicResult( ResultBase::Type type,
BasicResult( ResultType type,
std::string const& message ):
ResultValueBase<T>( type ), m_errorMessage( message ) {
assert( m_type != ResultBase::Ok );
assert( m_type != ResultType::Ok );
}
using ResultValueBase<T>::ResultValueBase;
@ -290,8 +301,8 @@ namespace Catch {
struct BoundRef : Catch::Detail::NonCopyable {
virtual ~BoundRef() = default;
virtual auto isContainer() const -> bool { return false; }
virtual auto isFlag() const -> bool { return false; }
virtual bool isContainer() const;
virtual bool isFlag() const;
};
struct BoundValueRefBase : BoundRef {
virtual auto setValue( std::string const& arg )
@ -299,7 +310,7 @@ namespace Catch {
};
struct BoundFlagRefBase : BoundRef {
virtual auto setFlag( bool flag ) -> ParserResult = 0;
bool isFlag() const override { return true; }
bool isFlag() const override;
};
template <typename T> struct BoundValueRef : BoundValueRefBase {
@ -307,8 +318,7 @@ namespace Catch {
explicit BoundValueRef( T& ref ): m_ref( ref ) {}
auto setValue( std::string const& arg )
-> ParserResult override {
ParserResult setValue( std::string const& arg ) override {
return convertInto( arg, m_ref );
}
};
@ -414,7 +424,7 @@ namespace Catch {
virtual auto parse( std::string const& exeName,
TokenStream const& tokens ) const
-> InternalParseResult = 0;
virtual auto cardinality() const -> size_t { return 1; }
virtual size_t cardinality() const;
InternalParseResult parse( Args const& args ) const;
};

View File

@ -41,7 +41,7 @@ namespace Catch {
}
std::vector<TagInfo> infos; infos.reserve(tagCounts.size());
for (auto const& tagc : tagCounts) {
for (auto& tagc : tagCounts) {
infos.push_back(std::move(tagc.second));
}

View File

@ -71,7 +71,7 @@ namespace Catch {
LazyStat<TestRunInfo> currentTestRunInfo;
LazyStat<GroupInfo> currentGroupInfo;
TestCaseInfo const* currentTestCaseInfo;
TestCaseInfo const* currentTestCaseInfo = nullptr;
std::vector<SectionInfo> m_sectionStack;
};

View File

@ -192,8 +192,9 @@ std::size_t& findMax(std::size_t& i, std::size_t& j, std::size_t& k) {
return k;
}
enum class Justification { Left, Right };
struct ColumnInfo {
enum Justification { Left, Right };
std::string name;
int width;
Justification justification;
@ -335,7 +336,7 @@ public:
auto padding = (strSize + 1 < static_cast<std::size_t>(colInfo.width))
? std::string(colInfo.width - (strSize + 1), ' ')
: std::string();
if (colInfo.justification == ColumnInfo::Left)
if (colInfo.justification == Justification::Left)
tp.m_os << colStr << padding << ' ';
else
tp.m_os << padding << colStr << ' ';
@ -358,19 +359,19 @@ ConsoleReporter::ConsoleReporter(ReporterConfig const& config)
if (config.fullConfig()->benchmarkNoAnalysis())
{
return{
{ "benchmark name", CATCH_CONFIG_CONSOLE_WIDTH - 43, ColumnInfo::Left },
{ " samples", 14, ColumnInfo::Right },
{ " iterations", 14, ColumnInfo::Right },
{ " mean", 14, ColumnInfo::Right }
{ "benchmark name", CATCH_CONFIG_CONSOLE_WIDTH - 43, Justification::Left },
{ " samples", 14, Justification::Right },
{ " iterations", 14, Justification::Right },
{ " mean", 14, Justification::Right }
};
}
else
{
return{
{ "benchmark name", CATCH_CONFIG_CONSOLE_WIDTH - 43, ColumnInfo::Left },
{ "samples mean std dev", 14, ColumnInfo::Right },
{ "iterations low mean low std dev", 14, ColumnInfo::Right },
{ "estimated high mean high std dev", 14, ColumnInfo::Right }
{ "benchmark name", CATCH_CONFIG_CONSOLE_WIDTH - 43, Justification::Left },
{ "samples mean std dev", 14, Justification::Right },
{ "iterations low mean low std dev", 14, Justification::Right },
{ "estimated high mean high std dev", 14, Justification::Right }
};
}
}())) {}

View File

@ -104,10 +104,10 @@ namespace {
move_detector(move_detector const& rhs) = default;
move_detector& operator=(move_detector const& rhs) = default;
move_detector(move_detector&& rhs) {
move_detector(move_detector&& rhs) noexcept {
rhs.has_moved = true;
}
move_detector& operator=(move_detector&& rhs) {
move_detector& operator=(move_detector&& rhs) noexcept {
rhs.has_moved = true;
return *this;
}

View File

@ -185,7 +185,7 @@ struct Obj
{
Obj():prop(&p){}
int p;
int p = 0;
int* prop;
};