From c644b81d0849ee23c22a6706f4ed64e9c6972815 Mon Sep 17 00:00:00 2001 From: Mathew Sutcliffe Date: Mon, 30 Nov 2015 19:12:55 +0000 Subject: [PATCH] refs #493 Added BLACK_VERIFY macros, a weaker kind of Q_ASSERT. --- src/blackmisc/logcategory.h | 7 +++++ src/blackmisc/logpattern.cpp | 1 + src/blackmisc/verify.cpp | 59 ++++++++++++++++++++++++++++++++++++ src/blackmisc/verify.h | 39 ++++++++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 src/blackmisc/verify.cpp create mode 100644 src/blackmisc/verify.h diff --git a/src/blackmisc/logcategory.h b/src/blackmisc/logcategory.h index bd3a612be..603acec67 100644 --- a/src/blackmisc/logcategory.h +++ b/src/blackmisc/logcategory.h @@ -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() { diff --git a/src/blackmisc/logpattern.cpp b/src/blackmisc/logpattern.cpp index 48c1fc345..7633fafc7 100644 --- a/src/blackmisc/logpattern.cpp +++ b/src/blackmisc/logpattern.cpp @@ -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()) }, diff --git a/src/blackmisc/verify.cpp b/src/blackmisc/verify.cpp new file mode 100644 index 000000000..cbce879fa --- /dev/null +++ b/src/blackmisc/verify.cpp @@ -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 +#elif defined(Q_OS_UNIX) +#include +#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 + } + } +} diff --git a/src/blackmisc/verify.h b/src/blackmisc/verify.h new file mode 100644 index 000000000..5191ae1fa --- /dev/null +++ b/src/blackmisc/verify.h @@ -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 + +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