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 // F' Assertion functions can technically return even though the intention is for the assertion to terminate the
49 // program. This breaks static analysis depending on assertions, since the analyzer has to assume the assertion will
50 // return. When supported, annotate assertion functions as noreturn when statically analyzing.
51 #ifndef CLANG_ANALYZER_NORETURN
52 #ifndef __has_feature
53 #define __has_feature(x) 0 // Compatibility with non-clang compilers.
54 #endif
55 #if __has_feature(attribute_analyzer_noreturn)
56 #define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
57 #else
58 #define CLANG_ANALYZER_NORETURN
59 #endif
60 #endif
61 
62 namespace Fw {
65 
68 
72 
75  FwAssertArgType arg1,
76  FwAssertArgType arg2,
77  FwAssertArgType arg3,
79 
82  FwAssertArgType arg1,
83  FwAssertArgType arg2,
84  FwAssertArgType arg3,
85  FwAssertArgType arg4,
87 
90  FwAssertArgType arg1,
91  FwAssertArgType arg2,
92  FwAssertArgType arg3,
93  FwAssertArgType arg4,
94  FwAssertArgType arg5,
96 
99  FwAssertArgType arg1,
100  FwAssertArgType arg2,
101  FwAssertArgType arg3,
102  FwAssertArgType arg4,
103  FwAssertArgType arg5,
104  FwAssertArgType arg6,
106 } // namespace Fw
107 
108 // Base class for declaring an assert hook
109 // Each of the base class functions can be overridden
110 // or used by derived classes.
111 
112 namespace Fw {
113 // Base class for declaring an assert hook
114 class AssertHook {
115  public:
116  AssertHook() : previousHook(nullptr){};
117  virtual ~AssertHook(){};
118  // override this function to intercept asserts
119  virtual void reportAssert(FILE_NAME_ARG file,
120  FwSizeType lineNo,
121  FwSizeType numArgs,
122  FwAssertArgType arg1,
123  FwAssertArgType arg2,
124  FwAssertArgType arg3,
125  FwAssertArgType arg4,
126  FwAssertArgType arg5,
127  FwAssertArgType arg6);
128  // default reportAssert() will call this when the message is built
129  // override it to do another kind of print. printf by default
130  virtual void printAssert(const CHAR* msg);
131  // do assert action. By default, calls assert.
132  // Called after reportAssert()
133  virtual void doAssert();
134  // register the hook
135  void registerHook();
136  // deregister the hook
137  void deregisterHook();
138 
139  protected:
140  private:
141  // the previous assert hook
142  AssertHook* previousHook;
143 };
144 } // namespace Fw
145 
146 #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:117
#define CLANG_ANALYZER_NORETURN
Definition: Assert.hpp:58
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.