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 
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(), static_cast<FwAssertArgType>(value))
51 
52 // F' Assertion functions can technically return even though the intention is for the assertion to terminate the
53 // program. This breaks static analysis depending on assertions, since the analyzer has to assume the assertion will
54 // return. When supported, annotate assertion functions as noreturn when statically analyzing.
55 #ifndef CLANG_ANALYZER_NORETURN
56 #ifndef __has_feature
57 #define __has_feature(x) 0 // Compatibility with non-clang compilers.
58 #endif
59 #if __has_feature(attribute_analyzer_noreturn)
60 #define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
61 #else
62 #define CLANG_ANALYZER_NORETURN
63 #endif
64 #endif
65 
66 // Define NOINLINE as __attribute__((noinline)) if that attribute is available.
67 // Marking assertion functions as NOINLINE can reduce code size without sacrificing performance
68 // in the common case that the function is not called.
69 #ifndef NOINLINE
70 #ifndef __has_attribute
71 #define __has_attribute(x) 0
72 #endif
73 #if __has_attribute(noinline)
74 #define NOINLINE __attribute__((noinline))
75 #else
76 #define NOINLINE
77 #endif
78 #endif
79 
80 namespace Fw {
83 
86 
89  FwAssertArgType arg1,
90  FwAssertArgType arg2,
92 
95  FwAssertArgType arg1,
96  FwAssertArgType arg2,
97  FwAssertArgType arg3,
99 
102  FwAssertArgType arg1,
103  FwAssertArgType arg2,
104  FwAssertArgType arg3,
105  FwAssertArgType arg4,
107 
110  FwAssertArgType arg1,
111  FwAssertArgType arg2,
112  FwAssertArgType arg3,
113  FwAssertArgType arg4,
114  FwAssertArgType arg5,
116 
119  FwAssertArgType arg1,
120  FwAssertArgType arg2,
121  FwAssertArgType arg3,
122  FwAssertArgType arg4,
123  FwAssertArgType arg5,
124  FwAssertArgType arg6,
126 } // namespace Fw
127 
128 // Base class for declaring an assert hook
129 // Each of the base class functions can be overridden
130 // or used by derived classes.
131 
132 namespace Fw {
133 // Base class for declaring an assert hook
134 class AssertHook {
135  public:
136  AssertHook() : previousHook(nullptr) {};
137  virtual ~AssertHook() {};
138  // override this function to intercept asserts
139  virtual void reportAssert(FILE_NAME_ARG file,
140  FwSizeType lineNo,
141  FwSizeType numArgs,
142  FwAssertArgType arg1,
143  FwAssertArgType arg2,
144  FwAssertArgType arg3,
145  FwAssertArgType arg4,
146  FwAssertArgType arg5,
147  FwAssertArgType arg6);
148  // default reportAssert() will call this when the message is built
149  // override it to do another kind of print. printf by default
150  virtual void printAssert(const CHAR* msg);
151  // do assert action. By default, calls assert.
152  // Called after reportAssert()
153  virtual void doAssert();
154  // register the hook
155  void registerHook();
156  // deregister the hook
157  void deregisterHook();
158 
159  protected:
160  private:
161  // the previous assert hook
162  AssertHook* previousHook;
163 };
164 } // namespace Fw
165 
166 #endif // FW_ASSERT_HPP
PlatformSizeType FwSizeType
int8_t I8
8-bit signed integer
Definition: BasicTypes.h:50
char CHAR
Definition: BasicTypes.h:59
I8 SwAssert(FILE_NAME_ARG file, FwSizeType lineNo)
Assert with no arguments.
Definition: Assert.cpp:130
#define FILE_NAME_ARG
Definition: Assert.hpp:16
void deregisterHook()
Definition: Assert.cpp:103
virtual void doAssert()
Definition: Assert.cpp:92
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:76
virtual ~AssertHook()
constructor
Definition: Assert.hpp:137
#define CLANG_ANALYZER_NORETURN
Definition: Assert.hpp:62
void registerHook()
Definition: Assert.cpp:98
virtual void printAssert(const CHAR *msg)
Definition: Assert.cpp:72
#define NOINLINE
Definition: Assert.hpp:76
Implementation of malloc based allocator.
PlatformAssertArgType FwAssertArgType
The type of arguments to assert functions.