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