F´ Flight Software - C/C++ Documentation  devel
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 <FpConfig.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
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_RELATIVE_PATH_ASSERT
28 #define FILE_NAME_ARG const CHAR*
29 #define FW_ASSERT(...) \
30  ((void)((FW_ASSERT_FIRST_ARG(__VA_ARGS__, 0)) \
31  ? (0) \
32  : (Fw::SwAssert(ASSERT_RELATIVE_PATH, FW_ASSERT_NO_FIRST_ARG(__VA_ARGS__, __LINE__)))))
33 #else
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(__FILE__, FW_ASSERT_NO_FIRST_ARG(__VA_ARGS__, __LINE__)))))
39 #endif
40 #endif // if ASSERT is defined
41 
42 // F' Assertion functions can technically return even though the intention is for the assertion to terminate the
43 // program. This breaks static analysis depending on assertions, since the analyzer has to assume the assertion will
44 // return. When supported, annotate assertion functions as noreturn when statically analyzing.
45 #ifndef CLANG_ANALYZER_NORETURN
46 #ifndef __has_feature
47 #define __has_feature(x) 0 // Compatibility with non-clang compilers.
48 #endif
49 #if __has_feature(attribute_analyzer_noreturn)
50 #define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
51 #else
52 #define CLANG_ANALYZER_NORETURN
53 #endif
54 #endif
55 
56 namespace Fw {
59 
62 
66 
69  FwAssertArgType arg1,
70  FwAssertArgType arg2,
71  FwAssertArgType arg3,
73 
76  FwAssertArgType arg1,
77  FwAssertArgType arg2,
78  FwAssertArgType arg3,
79  FwAssertArgType arg4,
81 
84  FwAssertArgType arg1,
85  FwAssertArgType arg2,
86  FwAssertArgType arg3,
87  FwAssertArgType arg4,
88  FwAssertArgType arg5,
90 
93  FwAssertArgType arg1,
94  FwAssertArgType arg2,
95  FwAssertArgType arg3,
96  FwAssertArgType arg4,
97  FwAssertArgType arg5,
98  FwAssertArgType arg6,
100 } // namespace Fw
101 
102 // Base class for declaring an assert hook
103 // Each of the base class functions can be overridden
104 // or used by derived classes.
105 
106 namespace Fw {
107 // Base class for declaring an assert hook
108 class AssertHook {
109  public:
110  AssertHook() : previousHook(nullptr){};
111  virtual ~AssertHook(){};
112  // override this function to intercept asserts
113  virtual void reportAssert(FILE_NAME_ARG file,
114  NATIVE_UINT_TYPE lineNo,
115  NATIVE_UINT_TYPE numArgs,
116  FwAssertArgType arg1,
117  FwAssertArgType arg2,
118  FwAssertArgType arg3,
119  FwAssertArgType arg4,
120  FwAssertArgType arg5,
121  FwAssertArgType arg6);
122  // default reportAssert() will call this when the message is built
123  // override it to do another kind of print. printf by default
124  virtual void printAssert(const CHAR* msg);
125  // do assert action. By default, calls assert.
126  // Called after reportAssert()
127  virtual void doAssert();
128  // register the hook
129  void registerHook();
130  // deregister the hook
131  void deregisterHook();
132 
133  protected:
134  private:
135  // the previous assert hook
136  AssertHook* previousHook;
137 };
138 } // namespace Fw
139 
140 #endif // FW_ASSERT_HPP
#define CLANG_ANALYZER_NORETURN
Definition: Assert.hpp:52
#define FILE_NAME_ARG
Definition: Assert.hpp:16
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:51
char CHAR
Definition: BasicTypes.h:28
PlatformUIntType NATIVE_UINT_TYPE
Definition: BasicTypes.h:52
PlatformAssertArgType FwAssertArgType
Definition: FpConfig.h:34
C++-compatible configuration header for fprime configuration.
virtual void reportAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo, NATIVE_UINT_TYPE numArgs, FwAssertArgType arg1, FwAssertArgType arg2, FwAssertArgType arg3, FwAssertArgType arg4, FwAssertArgType arg5, FwAssertArgType arg6)
destructor
Definition: Assert.cpp:77
void deregisterHook()
Definition: Assert.cpp:103
virtual ~AssertHook()
constructor
Definition: Assert.hpp:111
virtual void doAssert()
Definition: Assert.cpp:92
void registerHook()
Definition: Assert.cpp:98
virtual void printAssert(const CHAR *msg)
Definition: Assert.cpp:73
NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo)
Assert with no arguments.
Definition: Assert.cpp:129