1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-24 23:34:37 +01:00

Allow control of assertion behaviour

This allows tests to change the behaviour at runtime so that OpenRCT2 aborts instead of showing a message box.
This commit is contained in:
Ted John
2017-02-01 23:11:47 +00:00
parent c4e89278c1
commit 3a4bc87198
4 changed files with 100 additions and 49 deletions

View File

@@ -2,8 +2,6 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<SolutionDir Condition="'$(SolutionDir)'==''">..\..\</SolutionDir>
<Config Condition="'$(Configuration)'=='Debug' OR '$(Configuration)'=='DebugTests'">Debug</Config>
<Config Condition="'$(Configuration)'=='Release' OR '$(Configuration)'=='ReleaseTests'">Release</Config>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
@@ -16,13 +14,13 @@
<CharacterSet>MultiByte</CharacterSet>
<OutDir>$(SolutionDir)bin\</OutDir>
<IntDir>$(SolutionDir)obj\$(ProjectName)\$(Config)_$(Platform)\</IntDir>
<IntDir>$(SolutionDir)obj\$(ProjectName)\$(Configuration)_$(Platform)\</IntDir>
<TargetName>$(ProjectName)</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Config)'=='Debug'">
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<UseDebugLibraries>true</UseDebugLibraries>
</PropertyGroup>
<PropertyGroup Condition="'$(Config)'=='Release'">
<PropertyGroup Condition="'$(Configuration)'=='Release'">
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
</PropertyGroup>
@@ -56,7 +54,7 @@
<AdditionalOptions>/OPT:NOLBR /ignore:4099 %(AdditionalOptions)</AdditionalOptions>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Config)'=='Debug'">
<ItemDefinitionGroup Condition="'$(Configuration)'=='Debug'">
<ClCompile>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
@@ -68,7 +66,7 @@
<LinkTimeCodeGeneration>UseFastLinkTimeCodeGeneration</LinkTimeCodeGeneration>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Config)'=='Release'">
<ItemDefinitionGroup Condition="'$(Configuration)'=='Release'">
<ClCompile>
<Optimization>Full</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
@@ -85,16 +83,6 @@
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)'=='DebugTests'">
<ClCompile>
<PreprocessorDefinitions>__TEST__;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)'=='ReleaseTests'">
<ClCompile>
<PreprocessorDefinitions>NDEBUG;__TEST__;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<!-- Debug|Win32 is reserved for RCT2 interop builds, this means it has to be a DLL -->
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'!='Debug|Win32'">

View File

@@ -24,14 +24,14 @@
#undef GetMessage
#endif
#include "../OpenRCT2.h"
#include "Console.hpp"
#include "Diagnostics.hpp"
#include "Guard.hpp"
#include "String.hpp"
extern "C"
{
#include "../OpenRCT2.h"
void openrct2_assert(bool expression, const char * message, ...)
{
va_list args;
@@ -44,6 +44,30 @@ extern "C"
namespace Guard
{
// The default behaviour when an assertion is raised.
static ASSERT_BEHAVIOUR _assertBehaviour =
#ifdef __WINDOWS__
ASSERT_BEHAVIOUR::MESSAGE_BOX
#else
ASSERT_BEHAVIOUR::CASSERT
#endif
;
#ifdef __WINDOWS__
static void GetAssertMessage(char * buffer, size_t bufferSize, const char * formattedMessage);
static void ForceCrash();
#endif
ASSERT_BEHAVIOUR GetAssertBehaviour()
{
return _assertBehaviour;
}
void SetAssertBehaviour(ASSERT_BEHAVIOUR behaviour)
{
_assertBehaviour = behaviour;
}
void Assert(bool expression, const char * message, ...)
{
va_list args;
@@ -56,47 +80,46 @@ namespace Guard
{
if (expression) return;
char version[128];
openrct2_write_full_version_info(version, sizeof(version));
Console::Error::WriteLine("An assertion failed, please report this to the OpenRCT2 developers.");
Console::Error::WriteLine("Version: %s", version);
// This is never freed, but acceptable considering we are about to crash out
utf8 * formattedMessage = nullptr;
if (message != nullptr)
{
Console::Error::WriteLine("Assertion failed:");
Console::Error::WriteLine_VA(message, args);
formattedMessage = String::Format_VA(message, args);
Console::Error::WriteLine(formattedMessage);
}
#ifdef DEBUG
Debug::Break();
#endif
#ifdef __WINDOWS__
char version[128];
openrct2_write_full_version_info(version, sizeof(version));
char buffer[512];
strcpy(buffer, "An assertion failed, please report this to the OpenRCT2 developers.\r\n\r\nVersion: ");
strcat(buffer, version);
if (message != nullptr)
{
strcat(buffer, "\r\n");
char *bufend = (char *)strchr(buffer, 0);
vsnprintf(bufend, sizeof(buffer) - (bufend - buffer), message, args);
}
#ifdef __TEST__
// Abort if we are building for testing
abort();
#else
// Show message box if we are not building for testing
sint32 result = MessageBoxA(nullptr, buffer, OPENRCT2_NAME, MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION);
if (result == IDABORT)
{
#ifdef USE_BREAKPAD
// Force a crash that breakpad will handle allowing us to get a dump
*((void**)0) = 0;
#else
switch (_assertBehaviour) {
case ASSERT_BEHAVIOUR::ABORT:
abort();
break;
default:
case ASSERT_BEHAVIOUR::CASSERT:
assert(false);
#endif
break;
#ifdef __WINDOWS__
case ASSERT_BEHAVIOUR::MESSAGE_BOX:
{
// Show message box if we are not building for testing
char buffer[512];
GetAssertMessage(buffer, sizeof(buffer), formattedMessage);
sint32 result = MessageBoxA(nullptr, buffer, OPENRCT2_NAME, MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION);
if (result == IDABORT)
{
ForceCrash();
}
break;
}
#endif
#else
assert(false);
#endif
}
}
void Fail(const char * message, ...)
@@ -111,4 +134,30 @@ namespace Guard
{
Assert_VA(false, message, args);
}
#ifdef __WINDOWS__
static void GetAssertMessage(char * buffer, size_t bufferSize, const char * formattedMessage)
{
char version[128];
openrct2_write_full_version_info(version, sizeof(version));
String::Set(buffer, bufferSize, "An assertion failed, please report this to the OpenRCT2 developers.\r\n\r\nVersion: ");
String::Append(buffer, bufferSize, version);
if (formattedMessage != nullptr)
{
String::Append(buffer, bufferSize, "\r\n");
String::Append(buffer, bufferSize, formattedMessage);
}
}
static void ForceCrash()
{
#ifdef USE_BREAKPAD
// Force a crash that breakpad will handle allowing us to get a dump
*((void**)0) = 0;
#else
assert(false);
#endif
}
#endif
}

View File

@@ -27,11 +27,21 @@ void openrct2_assert(bool expression, const char * message, ...);
#include <stdarg.h>
enum class ASSERT_BEHAVIOUR
{
ABORT,
CASSERT,
MESSAGE_BOX,
};
/**
* Utility methods for asserting function parameters.
*/
namespace Guard
{
ASSERT_BEHAVIOUR GetAssertBehaviour();
void SetAssertBehaviour(ASSERT_BEHAVIOUR behaviour);
void Assert(bool expression, const char * message = nullptr, ...);
void Assert_VA(bool expression, const char * message, va_list args);
void Fail(const char * message = nullptr, ...);

View File

@@ -3,9 +3,13 @@
#ifdef _MSC_VER
#include <gtest/gtest.h>
#include <openrct2/core/Guard.hpp>
int main(int argc, char * * argv)
{
// Abort on an assertions so the tests do not hang
Guard::SetAssertBehaviour(ASSERT_BEHAVIOUR::ABORT);
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}