F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
Assert.hpp
Go to the documentation of this file.
1
#ifndef FW_ASSERT_HPP
2
#define FW_ASSERT_HPP
3
4
#include <
Fw/FPrimeBasicTypes.hpp
>
5
6
// Return only the first argument passed to the macro.
7
#define FW_ASSERT_FIRST_ARG(ARG_0, ...) ARG_0
8
// Return all the arguments of the macro, but the first one
9
#define FW_ASSERT_NO_FIRST_ARG(ARG_0, ...) __VA_ARGS__
10
11
#if FW_ASSERT_LEVEL == FW_NO_ASSERT
12
// Users may override the NO_ASSERT case should they choose
13
#ifndef FW_ASSERT
14
#define FW_ASSERT(...) ((void)(FW_ASSERT_FIRST_ARG(__VA_ARGS__)))
15
#endif
16
#define FILE_NAME_ARG const CHAR*
17
#else // ASSERT is defined
18
19
// Passing the __LINE__ argument at the end of the function ensures that
20
// the FW_ASSERT_NO_FIRST_ARG macro will never have an empty variadic variable
21
#if FW_ASSERT_LEVEL == FW_FILEID_ASSERT && defined ASSERT_FILE_ID
22
#define FILE_NAME_ARG U32
23
#define FW_ASSERT(...) \
24
((void)((FW_ASSERT_FIRST_ARG(__VA_ARGS__, 0)) \
25
? (0) \
26
: (Fw::SwAssert(ASSERT_FILE_ID, FW_ASSERT_NO_FIRST_ARG(__VA_ARGS__, __LINE__)))))
27
#elif FW_ASSERT_LEVEL == FW_FILEID_ASSERT && !defined ASSERT_FILE_ID
28
#define FILE_NAME_ARG U32
29
#define FW_ASSERT(...) \
30
((void)((FW_ASSERT_FIRST_ARG(__VA_ARGS__, 0)) \
31
? (0) \
32
: (Fw::SwAssert(static_cast<U32>(0), FW_ASSERT_NO_FIRST_ARG(__VA_ARGS__, __LINE__)))))
33
#elif FW_ASSERT_LEVEL == FW_RELATIVE_PATH_ASSERT && defined ASSERT_RELATIVE_PATH
34
#define FILE_NAME_ARG const CHAR*
35
#define FW_ASSERT(...) \
36
((void)((FW_ASSERT_FIRST_ARG(__VA_ARGS__, 0)) \
37
? (0) \
38
: (Fw::SwAssert(ASSERT_RELATIVE_PATH, FW_ASSERT_NO_FIRST_ARG(__VA_ARGS__, __LINE__)))))
39
#else
40
#define FILE_NAME_ARG const CHAR*
41
#define FW_ASSERT(...) \
42
((void)((FW_ASSERT_FIRST_ARG(__VA_ARGS__, 0)) \
43
? (0) \
44
: (Fw::SwAssert(__FILE__, FW_ASSERT_NO_FIRST_ARG(__VA_ARGS__, __LINE__)))))
45
#endif
46
#endif // if ASSERT is defined
47
48
// Helper macro asserting that a value fits into a type without overflow. Helpful for checking before static casts
49
#define FW_ASSERT_NO_OVERFLOW(value, T) \
50
FW_ASSERT((value) <= std::numeric_limits<T>::max(), \
51
static_cast<FwAssertArgType>(value))
52
53
// F' Assertion functions can technically return even though the intention is for the assertion to terminate the
54
// program. This breaks static analysis depending on assertions, since the analyzer has to assume the assertion will
55
// return. When supported, annotate assertion functions as noreturn when statically analyzing.
56
#ifndef CLANG_ANALYZER_NORETURN
57
#ifndef __has_feature
58
#define __has_feature(x) 0 // Compatibility with non-clang compilers.
59
#endif
60
#if __has_feature(attribute_analyzer_noreturn)
61
#define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
62
#else
63
#define CLANG_ANALYZER_NORETURN
64
#endif
65
#endif
66
67
namespace
Fw
{
69
I8
SwAssert
(
FILE_NAME_ARG
file
,
FwSizeType
lineNo)
CLANG_ANALYZER_NORETURN
;
70
72
I8
SwAssert
(
FILE_NAME_ARG
file
,
FwAssertArgType
arg1,
FwSizeType
lineNo)
CLANG_ANALYZER_NORETURN
;
73
75
I8
SwAssert
(
FILE_NAME_ARG
file
,
FwAssertArgType
arg1,
FwAssertArgType
arg2,
FwSizeType
lineNo)
76
CLANG_ANALYZER_NORETURN
;
77
79
I8
SwAssert
(
FILE_NAME_ARG
file
,
80
FwAssertArgType
arg1,
81
FwAssertArgType
arg2,
82
FwAssertArgType
arg3,
83
FwSizeType
lineNo)
CLANG_ANALYZER_NORETURN
;
84
86
I8
SwAssert
(
FILE_NAME_ARG
file
,
87
FwAssertArgType
arg1,
88
FwAssertArgType
arg2,
89
FwAssertArgType
arg3,
90
FwAssertArgType
arg4,
91
FwSizeType
lineNo)
CLANG_ANALYZER_NORETURN
;
92
94
I8
SwAssert
(
FILE_NAME_ARG
file
,
95
FwAssertArgType
arg1,
96
FwAssertArgType
arg2,
97
FwAssertArgType
arg3,
98
FwAssertArgType
arg4,
99
FwAssertArgType
arg5,
100
FwSizeType
lineNo)
CLANG_ANALYZER_NORETURN
;
101
103
I8
SwAssert
(
FILE_NAME_ARG
file
,
104
FwAssertArgType
arg1,
105
FwAssertArgType
arg2,
106
FwAssertArgType
arg3,
107
FwAssertArgType
arg4,
108
FwAssertArgType
arg5,
109
FwAssertArgType
arg6,
110
FwSizeType
lineNo)
CLANG_ANALYZER_NORETURN
;
111
}
// namespace Fw
112
113
// Base class for declaring an assert hook
114
// Each of the base class functions can be overridden
115
// or used by derived classes.
116
117
namespace
Fw
{
118
// Base class for declaring an assert hook
119
class
AssertHook
{
120
public
:
121
AssertHook
() : previousHook(nullptr){};
122
virtual
~AssertHook
(){};
123
// override this function to intercept asserts
124
virtual
void
reportAssert
(
FILE_NAME_ARG
file
,
125
FwSizeType
lineNo,
126
FwSizeType
numArgs,
127
FwAssertArgType
arg1,
128
FwAssertArgType
arg2,
129
FwAssertArgType
arg3,
130
FwAssertArgType
arg4,
131
FwAssertArgType
arg5,
132
FwAssertArgType
arg6);
133
// default reportAssert() will call this when the message is built
134
// override it to do another kind of print. printf by default
135
virtual
void
printAssert
(
const
CHAR
* msg);
136
// do assert action. By default, calls assert.
137
// Called after reportAssert()
138
virtual
void
doAssert
();
139
// register the hook
140
void
registerHook
();
141
// deregister the hook
142
void
deregisterHook
();
143
144
protected
:
145
private
:
146
// the previous assert hook
147
AssertHook
* previousHook;
148
};
149
}
// namespace Fw
150
151
#endif // FW_ASSERT_HPP
FwSizeType
PlatformSizeType FwSizeType
Definition:
FwSizeTypeAliasAc.h:15
Fw::SwAssert
I8 SwAssert(FILE_NAME_ARG file, FwSizeType lineNo)
Assert with no arguments.
Definition:
Assert.cpp:129
FPrimeBasicTypes.hpp
I8
int8_t I8
8-bit signed integer
Definition:
BasicTypes.h:53
CHAR
char CHAR
Definition:
BasicTypes.h:62
FILE_NAME_ARG
#define FILE_NAME_ARG
Definition:
Assert.hpp:16
Fw::AssertHook::deregisterHook
void deregisterHook()
Definition:
Assert.cpp:103
Fw::AssertHook::doAssert
virtual void doAssert()
Definition:
Assert.cpp:92
Fw::AssertHook
Definition:
Assert.hpp:119
Fw::AssertHook::reportAssert
virtual void reportAssert(FILE_NAME_ARG file, FwSizeType lineNo, FwSizeType numArgs, FwAssertArgType arg1, FwAssertArgType arg2, FwAssertArgType arg3, FwAssertArgType arg4, FwAssertArgType arg5, FwAssertArgType arg6)
destructor
Definition:
Assert.cpp:77
Fw::AssertHook::~AssertHook
virtual ~AssertHook()
constructor
Definition:
Assert.hpp:122
CLANG_ANALYZER_NORETURN
#define CLANG_ANALYZER_NORETURN
Definition:
Assert.hpp:63
Fw::AssertHook::registerHook
void registerHook()
Definition:
Assert.cpp:98
Fw::AssertHook::printAssert
virtual void printAssert(const CHAR *msg)
Definition:
Assert.cpp:73
ComCcsdsConfig::QueueDepths::file
Definition:
FppConstantsAc.hpp:73
Fw::AssertHook::AssertHook
AssertHook()
Definition:
Assert.hpp:121
Fw
Definition:
FppConstantsAc.hpp:121
FwAssertArgType
PlatformAssertArgType FwAssertArgType
The type of arguments to assert functions.
Definition:
FwAssertArgTypeAliasAc.h:14
Fw
Types
Assert.hpp
Generated by
1.8.14