F´ Flight Software - C/C++ Documentation  NASA-v2.1.0
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>
6 
7 #if FW_ASSERT_LEVEL == FW_NO_ASSERT
8 
9 #define FW_ASSERT(...)
10 #define FW_STATIC_ASSERT(...)
11 
12 #else // ASSERT is defined
13 
14 #define FW_STATIC_CAT_(a, b) a ## b
15 #define FW_STATIC_CAT(a, b) FW_STATIC_CAT_(a, b)
16 #define FW_STATIC_ASSERT(cond) typedef int FW_STATIC_CAT(FW_STATIC_ASSERT,__LINE__)[(cond) ? 1 : -1]
17 
18 #if FW_ASSERT_LEVEL == FW_FILEID_ASSERT
19 #define FILE_NAME_ARG NATIVE_UINT_TYPE
20 #define FW_ASSERT(cond, ...) \
21  ((void) ((cond) ? (0) : \
22  (Fw::SwAssert(ASSERT_FILE_ID, __LINE__, ##__VA_ARGS__))))
23 #else
24 #define FILE_NAME_ARG const U8*
25 #define FW_ASSERT(cond, ...) \
26  ((void) ((cond) ? (0) : \
27  (Fw::SwAssert((U8*)__FILE__, __LINE__, ##__VA_ARGS__))))
28 #endif
29 
30 // F' Assertion functions can technically return even though the intention is for the assertion to terminate the program.
31 // This breaks static analysis depending on assertions, since the analyzer has to assume the assertion will return.
32 // When supported, annotate assertion functions as noreturn when statically analyzing.
33 #ifndef CLANG_ANALYZER_NORETURN
34 #ifndef __has_feature
35  #define __has_feature(x) 0 // Compatibility with non-clang compilers.
36 #endif
37 #if __has_feature(attribute_analyzer_noreturn)
38 #define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
39 #else
40 #define CLANG_ANALYZER_NORETURN
41 #endif
42 #endif
43 
44 namespace Fw {
45  NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo) CLANG_ANALYZER_NORETURN;
46  NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo, AssertArg arg1) CLANG_ANALYZER_NORETURN;
47  NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo, AssertArg arg1, AssertArg arg2) CLANG_ANALYZER_NORETURN;
48  NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo, AssertArg arg1, AssertArg arg2, AssertArg arg3) CLANG_ANALYZER_NORETURN;
49  NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo, AssertArg arg1, AssertArg arg2, AssertArg arg3, AssertArg arg4) CLANG_ANALYZER_NORETURN;
50  NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo, AssertArg arg1, AssertArg arg2, AssertArg arg3, AssertArg arg4, AssertArg arg5) CLANG_ANALYZER_NORETURN;
51  NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo, AssertArg arg1, AssertArg arg2, AssertArg arg3, AssertArg arg4, AssertArg arg5, AssertArg arg6) CLANG_ANALYZER_NORETURN;
52 }
53 
54 // Base class for declaring an assert hook
55 // Each of the base class functions can be overridden
56 // or used by derived classes.
57 
58 namespace Fw {
59  // Base class for declaring an assert hook
60  class AssertHook {
61  public:
62  AssertHook() : previousHook(NULL) {};
63  virtual ~AssertHook() {};
64  // override this function to intercept asserts
65  virtual void reportAssert(
66  FILE_NAME_ARG file,
67  NATIVE_UINT_TYPE lineNo,
68  NATIVE_UINT_TYPE numArgs,
69  AssertArg arg1,
70  AssertArg arg2,
71  AssertArg arg3,
72  AssertArg arg4,
73  AssertArg arg5,
74  AssertArg arg6
75  );
76  // default reportAssert() will call this when the message is built
77  // override it to do another kind of print. printf by default
78  virtual void printAssert(const I8* msg);
79  // do assert action. By default, calls assert.
80  // Called after reportAssert()
81  virtual void doAssert(void);
82  // register the hook
83  void registerHook(void);
84  // deregister the hook
85  void deregisterHook(void);
86 
87  protected:
88  private:
89  // the previous assert hook
90  AssertHook *previousHook;
91  };
92 
93 
94 }
95 #endif // if ASSERT is defined
96 
97 
98 #endif
I8
int8_t I8
8-bit signed integer
Definition: BasicTypes.hpp:75
NATIVE_UINT_TYPE
unsigned int NATIVE_UINT_TYPE
native unsigned integer type declaration
Definition: BasicTypes.hpp:30
FpConfig.hpp
ISF configuration file.
BasicTypes.hpp
Declares ISF basic types.
NATIVE_INT_TYPE
int NATIVE_INT_TYPE
native integer type declaration
Definition: BasicTypes.hpp:29
AssertArg
#define AssertArg
Definition: FpConfig.hpp:51
NULL
#define NULL
NULL.
Definition: BasicTypes.hpp:100
Fw
Definition: Buffer.cpp:21