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