From 80e7a3669131d61caa5c91c5a80060ac7e6100b2 Mon Sep 17 00:00:00 2001 From: jules Date: Tue, 14 Oct 2014 14:32:25 +0100 Subject: [PATCH] Modified the DBG and jassert macros to force users to follow them with a semi-colon. This can avoid some nasty and subtle mistakes when the macros are elided in a release build. --- modules/juce_core/system/juce_PlatformDefs.h | 28 +++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/modules/juce_core/system/juce_PlatformDefs.h b/modules/juce_core/system/juce_PlatformDefs.h index 29f866ba5e..6779b46bd6 100644 --- a/modules/juce_core/system/juce_PlatformDefs.h +++ b/modules/juce_core/system/juce_PlatformDefs.h @@ -94,20 +94,34 @@ #define JUCE_ANALYZER_NORETURN #endif +//============================================================================== +#if JUCE_MSVC && ! DOXYGEN + #define MACRO_WITH_FORCED_SEMICOLON(x) \ + __pragma(warning(push)) \ + __pragma(warning(disable:4127)) \ + do { x } while (false) \ + __pragma(warning(pop)) +#else + /** This is the good old C++ trick for creating a macro that forces the user to put + a semicolon after it when they use it. + */ + #define MACRO_WITH_FORCED_SEMICOLON(x) do { x } while (false) +#endif + //============================================================================== #if JUCE_DEBUG || DOXYGEN /** Writes a string to the standard error stream. This is only compiled in a debug build. @see Logger::outputDebugString */ - #define DBG(dbgtext) { juce::String tempDbgBuf; tempDbgBuf << dbgtext; juce::Logger::outputDebugString (tempDbgBuf); } + #define DBG(dbgtext) MACRO_WITH_FORCED_SEMICOLON (juce::String tempDbgBuf; tempDbgBuf << dbgtext; juce::Logger::outputDebugString (tempDbgBuf);) //============================================================================== /** This will always cause an assertion failure. It is only compiled in a debug build, (unless JUCE_LOG_ASSERTIONS is enabled for your build). @see jassert */ - #define jassertfalse { juce_LogCurrentAssertion; if (juce::juce_isRunningUnderDebugger()) juce_breakDebugger; JUCE_ANALYZER_NORETURN } + #define jassertfalse MACRO_WITH_FORCED_SEMICOLON (juce_LogCurrentAssertion; if (juce::juce_isRunningUnderDebugger()) juce_breakDebugger; JUCE_ANALYZER_NORETURN) //============================================================================== /** Platform-independent assertion macro. @@ -117,19 +131,19 @@ correct behaviour of your program! @see jassertfalse */ - #define jassert(expression) { if (! (expression)) jassertfalse; } + #define jassert(expression) MACRO_WITH_FORCED_SEMICOLON (if (! (expression)) jassertfalse;) #else //============================================================================== // If debugging is disabled, these dummy debug and assertion macros are used.. #define DBG(dbgtext) - #define jassertfalse { juce_LogCurrentAssertion } + #define jassertfalse MACRO_WITH_FORCED_SEMICOLON (juce_LogCurrentAssertion) #if JUCE_LOG_ASSERTIONS - #define jassert(expression) { if (! (expression)) jassertfalse; } + #define jassert(expression) MACRO_WITH_FORCED_SEMICOLON (if (! (expression)) jassertfalse;) #else - #define jassert(a) {} + #define jassert(a) MACRO_WITH_FORCED_SEMICOLON () #endif #endif @@ -139,7 +153,7 @@ namespace juce { template struct JuceStaticAssert; - template <> struct JuceStaticAssert { static void dummy() {} }; + template <> struct JuceStaticAssert { static void dummy() {} }; } #endif