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(), \
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 {
70 
73 
77 
80  FwAssertArgType arg1,
81  FwAssertArgType arg2,
82  FwAssertArgType arg3,
84 
87  FwAssertArgType arg1,
88  FwAssertArgType arg2,
89  FwAssertArgType arg3,
90  FwAssertArgType arg4,
92 
95  FwAssertArgType arg1,
96  FwAssertArgType arg2,
97  FwAssertArgType arg3,
98  FwAssertArgType arg4,
99  FwAssertArgType arg5,
101 
104  FwAssertArgType arg1,
105  FwAssertArgType arg2,
106  FwAssertArgType arg3,
107  FwAssertArgType arg4,
108  FwAssertArgType arg5,
109  FwAssertArgType arg6,
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
PlatformSizeType FwSizeType
I8 SwAssert(FILE_NAME_ARG file, FwSizeType lineNo)
Assert with no arguments.
Definition: Assert.cpp:129
int8_t I8
8-bit signed integer
Definition: BasicTypes.h:53
char CHAR
Definition: BasicTypes.h:62
#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:77
virtual ~AssertHook()
constructor
Definition: Assert.hpp:122
#define CLANG_ANALYZER_NORETURN
Definition: Assert.hpp:63
void registerHook()
Definition: Assert.cpp:98
virtual void printAssert(const CHAR *msg)
Definition: Assert.cpp:73
PlatformAssertArgType FwAssertArgType
The type of arguments to assert functions.