refs #493 Added BLACK_VERIFY macros, a weaker kind of Q_ASSERT.

This commit is contained in:
Mathew Sutcliffe
2015-11-30 19:12:55 +00:00
parent 468015949f
commit c644b81d08
4 changed files with 106 additions and 0 deletions

View File

@@ -36,6 +36,13 @@ namespace BlackMisc
return cat;
}
//! Verification
static const CLogCategory &verification()
{
static const CLogCategory cat { "swift.verification" };
return cat;
}
//! Validation
static const CLogCategory &validation()
{

View File

@@ -18,6 +18,7 @@ namespace BlackMisc
{
{ "uncategorized (swift)", exactMatch(CLogCategory::uncategorized()) },
{ "validation", exactMatch(CLogCategory::validation()) },
{ "verification", exactMatch(CLogCategory::verification()) },
{ "swift contexts", exactMatch(CLogCategory::context()) },
{ "swift context slots", exactMatch(CLogCategory::contextSlot()) },
{ "swift GUI", exactMatch(CLogCategory::guiComponent()) },

59
src/blackmisc/verify.cpp Normal file
View File

@@ -0,0 +1,59 @@
/* Copyright (C) 2015
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "blackmisc/verify.h"
#include "blackmisc/logmessage.h"
#if defined(Q_CC_MSVC)
#include <intrin.h>
#elif defined(Q_OS_UNIX)
#include <signal.h>
#endif
#if defined(Q_CC_CLANG)
#if __has_builtin(__builtin_debugger)
#define BLACK_HAS_BUILTIN_DEBUGGER
#endif
#endif
namespace BlackMisc
{
namespace Private
{
void failedVerify(const char *condition, const char *filename, int line, const char *context, const char *message)
{
Q_UNUSED(condition);
Q_UNUSED(filename);
Q_UNUSED(line);
Q_UNUSED(context);
Q_UNUSED(message);
#if defined(QT_NO_DEBUG)
if (context && message)
{
CLogMessage(CLogCategory::verification()).warning("Failed to verify: %1 (%2 in %3) in %4 line %5") << condition << message << context << filename << line;
}
else
{
CLogMessage(CLogCategory::verification()).warning("Failed to verify: %1 in %2 line %3") << condition << filename << line;
}
#elif defined(Q_CC_MSVC)
__debugbreak();
#elif defined(BLACK_HAS_BUILTIN_DEBUGGER)
__builtin_debugger();
#elif defined(Q_PROCESSOR_X86)
__asm__ volatile("int $0x03");
#elif defined(Q_PROCESSOR_ARM)
__asm__ volatile(".inst 0xe7f001f0");
#elif defined(Q_OS_UNIX)
raise(SIGTRAP);
#else
Q_ASSERT(false);
#endif
}
}
}

39
src/blackmisc/verify.h Normal file
View File

@@ -0,0 +1,39 @@
/* Copyright (C) 2015
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKMISC_VERIFY_H
#define BLACKMISC_VERIFY_H
#include "blackmisc/blackmiscexport.h"
#include <QtGlobal>
namespace BlackMisc
{
namespace Private
{
//! \private Do nothing.
inline void noop() {}
//! \private Called by BLACK_VERIFY when the condition is false.
BLACKMISC_EXPORT void failedVerify(const char *condition, const char *filename, int line, const char *context = nullptr, const char *message = nullptr);
}
}
/*!
* A weaker kind of assert. Still indicates a programmer mistake, but one which is recoverable.
* In debug builds, triggers a debugger breakpoint. In release builds, generates a warning.
*/
//! @{
#define BLACK_VERIFY_X(COND, WHERE, WHAT) ((COND) ? BlackMisc::Private::noop() : BlackMisc::Private::failedVerify(#COND, __FILE__, __LINE__, WHERE, WHAT))
#define BLACK_VERIFY(COND) BLACK_VERIFY_X(COND, nullptr, nullptr)
//! @}
#endif