Skip to content

Commit

Permalink
[assert] Add descriptions to debug build as default option
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Sep 27, 2020
1 parent d0a6196 commit 6f0c170
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 25 deletions.
54 changes: 39 additions & 15 deletions src/modm/architecture/interface/assert.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,25 @@
#include <modm/architecture/utils.hpp>

/// @cond
#define MODM_ASSERTION_INFO_HAS_DESCRIPTION {{ 1 if options.with_description else 0 }}
#ifndef MODM_ASSERTION_INFO_HAS_DESCRIPTION
%% if options.with_description == "debug"
#ifdef MODM_DEBUG_BUILD
#define MODM_ASSERTION_INFO_HAS_DESCRIPTION 1
#else
#define MODM_ASSERTION_INFO_HAS_DESCRIPTION 0
#endif
%% else
#define MODM_ASSERTION_INFO_HAS_DESCRIPTION {{ 1 if options.with_description == "release" else 0 }}
%% endif
#endif

// C-type information struct
typedef struct modm_packed
{
const char *name;
%% if options.with_description
#if MODM_ASSERTION_INFO_HAS_DESCRIPTION
const char *description;
%% endif
#endif
uintptr_t context;
uint8_t behavior;
} _modm_assertion_info;
Expand All @@ -39,18 +49,32 @@ typedef struct modm_packed
#define _modm_assert_ifss(s) ((const char *)(s))
%% endif

#define _modm_assert5(behavior, condition, name, description, context) \
({ \
const bool evaluated_condition = (bool) (condition); \
if (!evaluated_condition) { \
_modm_assertion_info info = { \
_modm_assert_ifss(name),{% if options.with_description %} _modm_assert_ifss(description),{% endif %} \
(uintptr_t)(context), (uint8_t)(behavior)}; \
modm_assert_report(&info); \
if (behavior & 4) __builtin_unreachable(); \
} \
evaluated_condition; \
})
#if MODM_ASSERTION_INFO_HAS_DESCRIPTION
#define _modm_assert5(behavior, condition, name, description, context) \
({ \
const bool evaluated_condition = (bool) (condition); \
if (!evaluated_condition) { \
_modm_assertion_info info = { \
_modm_assert_ifss(name), _modm_assert_ifss(description), \
(uintptr_t)(context), (uint8_t)(behavior)}; \
modm_assert_report(&info); \
if (behavior & 4) __builtin_unreachable(); \
} \
evaluated_condition; \
})
#else
#define _modm_assert5(behavior, condition, name, description, context) \
({ \
const bool evaluated_condition = (bool) (condition); \
if (!evaluated_condition) { \
_modm_assertion_info info = { \
_modm_assert_ifss(name), (uintptr_t)(context), (uint8_t)(behavior)}; \
modm_assert_report(&info); \
if (behavior & 4) __builtin_unreachable(); \
} \
evaluated_condition; \
})
#endif
#define _modm_assert4(behavior, condition, name, description) \
_modm_assert5(behavior, condition, name, description, -1)

Expand Down
6 changes: 3 additions & 3 deletions src/modm/architecture/interface/assert.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ struct modm_packed
AssertionInfo
{
const char *name; ///< Can be used to recognise the assertion in code
%% if options.with_description
#if MODM_ASSERTION_INFO_HAS_DESCRIPTION
const char *description; ///< Detailed failure description
%% endif
#endif
uintptr_t context; ///< Optional context depends on assertion
AbandonmentBehavior behavior; ///< Can this assertion be ignored?
};
Expand All @@ -66,7 +66,7 @@ using AssertionHandler = Abandonment (*)(const AssertionInfo &info);
*
* @ingroup modm_architecture_assert
*/
#define MODM_ASSERTION_INFO_HAS_DESCRIPTION {{ 1 if options.with_description else 0 }}
#define MODM_ASSERTION_INFO_HAS_DESCRIPTION 0/1

/**
* This adds a function to the list of assertion handlers to execute on
Expand Down
10 changes: 6 additions & 4 deletions src/modm/architecture/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ class Assert(Module):
module.description = FileReader("interface/assert.md")

def prepare(self, module, options):
platform = options[":target"].identifier.platform
module.add_option(
BooleanOption(name="with_description",
default=options[":target"].identifier.platform in ["hosted"],
description=Assert.with_description_descr))
EnumerationOption(name="with_description",
description=Assert.with_description_descr,
default="release" if platform == "hosted" else "debug",
enumeration=["off", "debug", "release"]))

module.depends(":architecture:register")
if options[":target"].identifier.platform in ["avr"]:
if platform == "avr":
module.depends(":architecture:accessor")
return True

Expand Down
6 changes: 3 additions & 3 deletions src/modm/board/board.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ modm_abandon(const modm::AssertionInfo &info)
MODM_LOG_ERROR << " @ " << (void *) info.context <<
" (" << (uint32_t) info.context << ")";
}
%% if options.get(":architecture:assert:with_description", False)
#if MODM_ASSERTION_INFO_HAS_DESCRIPTION
MODM_LOG_ERROR << " failed!\n " << info.description << "\nAbandoning...\n";
%% else
#else
MODM_LOG_ERROR << " failed!\nAbandoning...\n";
%% endif
#endif
%% else
(void)info;
%% endif
Expand Down

0 comments on commit 6f0c170

Please sign in to comment.