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 
20 // Passing the __LINE__ argument at the end of the function ensures that
21 // the FW_ASSERT_NO_FIRST_ARG macro will never have an empty variadic variable
22 #if FW_ASSERT_LEVEL == FW_FILEID_ASSERT
23  #define FILE_NAME_ARG U32
24  #define FW_ASSERT(...) \
25  ((void) ((FW_ASSERT_FIRST_ARG(__VA_ARGS__, 0)) ? (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)) ? (0) : \
31  (Fw::SwAssert(ASSERT_RELATIVE_PATH, FW_ASSERT_NO_FIRST_ARG(__VA_ARGS__, __LINE__)))))
32 #else
33  #define FILE_NAME_ARG const CHAR*
34  #define FW_ASSERT(...) \
35  ((void) ((FW_ASSERT_FIRST_ARG(__VA_ARGS__, 0)) ? (0) : \
36  (Fw::SwAssert(__FILE__, FW_ASSERT_NO_FIRST_ARG(__VA_ARGS__, __LINE__)))))
37 #endif
38 #endif // if ASSERT is defined
39 
40 // F' Assertion functions can technically return even though the intention is for the assertion to terminate the program.
41 // This breaks static analysis depending on assertions, since the analyzer has to assume the assertion will return.
42 // When supported, annotate assertion functions as noreturn when statically analyzing.
43 #ifndef CLANG_ANALYZER_NORETURN
44 #ifndef __has_feature
45  #define __has_feature(x) 0 // Compatibility with non-clang compilers.
46 #endif
47 #if __has_feature(attribute_analyzer_noreturn)
48 #define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
49 #else
50 #define CLANG_ANALYZER_NORETURN
51 #endif
52 #endif
53 
54 namespace Fw {
57  FILE_NAME_ARG file,
59 
62  FILE_NAME_ARG file,
63  FwAssertArgType arg1,
65 
68  FILE_NAME_ARG file,
69  FwAssertArgType arg1,
70  FwAssertArgType arg2,
72 
75  FILE_NAME_ARG file,
76  FwAssertArgType arg1,
77  FwAssertArgType arg2,
78  FwAssertArgType arg3,
80 
83  FILE_NAME_ARG file,
84  FwAssertArgType arg1,
85  FwAssertArgType arg2,
86  FwAssertArgType arg3,
87  FwAssertArgType arg4,
89 
92  FILE_NAME_ARG file,
93  FwAssertArgType arg1,
94  FwAssertArgType arg2,
95  FwAssertArgType arg3,
96  FwAssertArgType arg4,
97  FwAssertArgType arg5,
99 
102  FILE_NAME_ARG file,
103  FwAssertArgType arg1,
104  FwAssertArgType arg2,
105  FwAssertArgType arg3,
106  FwAssertArgType arg4,
107  FwAssertArgType arg5,
108  FwAssertArgType arg6,
110 }
111 
112 // Base class for declaring an assert hook
113 // Each of the base class functions can be overridden
114 // or used by derived classes.
115 
116 namespace Fw {
117  // Base class for declaring an assert hook
118  class AssertHook {
119  public:
120  AssertHook() : previousHook(nullptr) {};
121  virtual ~AssertHook() {};
122  // override this function to intercept asserts
123  virtual void reportAssert(
124  FILE_NAME_ARG file,
125  NATIVE_UINT_TYPE lineNo,
126  NATIVE_UINT_TYPE numArgs,
127  FwAssertArgType arg1,
128  FwAssertArgType arg2,
129  FwAssertArgType arg3,
130  FwAssertArgType arg4,
131  FwAssertArgType arg5,
132  FwAssertArgType arg6
133  );
134  // default reportAssert() will call this when the message is built
135  // override it to do another kind of print. printf by default
136  virtual void printAssert(const CHAR* msg);
137  // do assert action. By default, calls assert.
138  // Called after reportAssert()
139  virtual void doAssert();
140  // register the hook
141  void registerHook();
142  // deregister the hook
143  void deregisterHook();
144 
145  protected:
146  private:
147  // the previous assert hook
148  AssertHook *previousHook;
149  };
150 }
151 
152 #endif // FW_ASSERT_HPP
#define CLANG_ANALYZER_NORETURN
Definition: Assert.hpp:50
#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:21
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:118
void deregisterHook()
Definition: Assert.cpp:160
virtual ~AssertHook()
constructor
Definition: Assert.hpp:121
virtual void doAssert()
Definition: Assert.cpp:149
void registerHook()
Definition: Assert.cpp:155
virtual void printAssert(const CHAR *msg)
Definition: Assert.cpp:113
NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo)
Assert with no arguments.
Definition: Assert.cpp:197