mirror of
https://github.com/revng/revng
synced 2026-06-21 14:07:57 +00:00
8c148607d3
Sometimes, dealing with move-semantics in C++ can be challenging. To mitigate this problem, this commit introduces `ClassSentinel`, a simple class that can be added as a member of a class and that will allow the user to easily monitor if an instance of a class is used after being moved in an unwanted way. To do so, simply call the `ClassSentinel::check` method on the class member. `ClassSentinel` performs similar (but less reliable) checks on usage of destroyed objects. `ClassSentinel` can also (optionally, though the `SENTINEL_STACKTRACES` macro) collect stack traces of the points where the object was moved or destroyed. This commit also includes some basic testing for the class.
39 lines
813 B
C++
39 lines
813 B
C++
/// \file sentinel.cpp
|
|
/// \brief Tests for ClassSentinel
|
|
|
|
//
|
|
// This file is distributed under the MIT License. See LICENSE.md for details.
|
|
//
|
|
|
|
// Standard includes
|
|
|
|
// Boost includes
|
|
#define BOOST_TEST_MODULE StackAnalysis
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
// Local includes
|
|
#include "classsentinel.h"
|
|
|
|
struct TestClass {
|
|
ClassSentinel Sentinel;
|
|
};
|
|
|
|
BOOST_AUTO_TEST_CASE(Sentinel) {
|
|
TestClass *DanglingPointer = nullptr;
|
|
|
|
{
|
|
TestClass Instance;
|
|
BOOST_TEST(!Instance.Sentinel.isMoved());
|
|
BOOST_TEST(!Instance.Sentinel.isDestroyed());
|
|
|
|
TestClass OtherInstance = std::move(Instance);
|
|
|
|
BOOST_TEST(Instance.Sentinel.isMoved());
|
|
BOOST_TEST(!Instance.Sentinel.isDestroyed());
|
|
|
|
DanglingPointer = &Instance;
|
|
}
|
|
|
|
BOOST_TEST(DanglingPointer->Sentinel.isDestroyed());
|
|
}
|