ROSE  0.11.51.0
Assert.h
1 // WARNING: Changes to this file must be contributed back to Sawyer or else they will
2 // be clobbered by the next update from Sawyer. The Sawyer repository is at
3 // https://github.com/matzke1/sawyer.
4 
5 
6 
7 
8 #ifndef Sawyer_Assert_H
9 #define Sawyer_Assert_H
10 
11 #include <Sawyer/Sawyer.h>
12 #include <string>
13 
14 // If SAWYER_NDEBUG is defined then some of the macros defined in this header become no-ops. For interoperability with the
15 // more standard NDEBUG symbol, we define SAWYER_NDEBUG if NDEBUG is defined.
16 #ifdef NDEBUG
17 #undef SAWYER_NDEBUG
18 #define SAWYER_NDEBUG
19 #endif
20 
21 namespace Sawyer { // documented elsewhere
22 
90 namespace Assert {
91 
94 SAWYER_EXPORT_NORETURN void fail(const char *mesg, const char *expr, const std::string &note,
95  const char *filename, unsigned linenum, const char *funcname) SAWYER_ATTR_NORETURN;
96 
98 typedef void (*AssertFailureHandler)(const char *mesg, const char *expr, const std::string &note,
99  const char *filename, unsigned linenum, const char *funcname);
100 
104 SAWYER_EXPORT extern AssertFailureHandler assertFailureHandler;
105 
106 } // namespace
107 } // namespace
108 
110 // These "always" macros are enabled regardless of whether SAWYER_NDEBUG is defined. Don't use them for
111 // expensive assertions.
113 
114 #define ASSERT_always_require(expr) ASSERT_always_require2(expr, "")
115 #define ASSERT_always_require2(expr, note) \
116  ((expr) ? \
117  static_cast<void>(0) : \
118  Sawyer::Assert::fail("assertion failed", "required: " #expr, (note), \
119  __FILE__, __LINE__, SAWYER_PRETTY_FUNCTION))
120 
121 #define ASSERT_always_forbid(expr) ASSERT_always_forbid2(expr, "")
122 #define ASSERT_always_forbid2(expr, note) \
123  (!(expr) ? \
124  static_cast<void>(0) : \
125  Sawyer::Assert::fail("assertion failed", \
126  "forbidden: " #expr, (note), __FILE__, __LINE__, SAWYER_PRETTY_FUNCTION))
127 
128 #define ASSERT_always_not_null(expr) ASSERT_always_not_null2(expr, "")
129 #define ASSERT_always_not_null2(expr, note) \
130  ((expr)!=NULL ? \
131  static_cast<void>(0) : \
132  Sawyer::Assert::fail("null pointer", \
133  #expr, (note), __FILE__, __LINE__, SAWYER_PRETTY_FUNCTION))
134 
135 #define ASSERT_always_not_reachable(note) \
136  Sawyer::Assert::fail("reached impossible state", NULL, (note), \
137  __FILE__, __LINE__, SAWYER_PRETTY_FUNCTION);
138 
139 #define ASSERT_always_not_implemented(note) \
140  Sawyer::Assert::fail("not implemented yet", NULL, (note), \
141  __FILE__, __LINE__, SAWYER_PRETTY_FUNCTION)
142 
143 #define ASSERT_always_this() /*void*/
144 
146 // The non-"always" macros might change behavior based on whether SAWYER_NDEBUG is defined.
148 
149 #ifdef SAWYER_NDEBUG
150 
151 #define ASSERT_require(expr) /*void*/
152 #define ASSERT_require2(expr, note) /*void*/
153 #define ASSERT_forbid(expr) /*void*/
154 #define ASSERT_forbid2(expr, note) /*void*/
155 #define ASSERT_not_null(expr) /*void*/
156 #define ASSERT_not_null2(expr, note) /*void*/
157 #define ASSERT_not_reachable(note) ASSERT_always_not_reachable(note)
158 #define ASSERT_not_implemented(note) ASSERT_always_not_implemented(note)
159 #define ASSERT_this() /*void*/
160 
161 #else
162 
163 #define ASSERT_require(expr) ASSERT_always_require(expr)
164 #define ASSERT_require2(expr, note) ASSERT_always_require2(expr, note)
165 #define ASSERT_forbid(expr) ASSERT_always_forbid(expr)
166 #define ASSERT_forbid2(expr, note) ASSERT_always_forbid2(expr, note)
167 #define ASSERT_not_null(expr) ASSERT_always_not_null(expr)
168 #define ASSERT_not_null2(expr, note) ASSERT_always_not_null2(expr, note)
169 #define ASSERT_not_reachable(note) ASSERT_always_not_reachable(note)
170 #define ASSERT_not_implemented(note) ASSERT_always_not_implemented(note)
171 #define ASSERT_this() /*void*/
172 
173 #endif
174 
176 // Macros recognized by some IDEs
178 
179 #define TODO(note) \
180  Sawyer::Assert::fail("not implemented yet", NULL, (note), \
181  __FILE__, __LINE__, SAWYER_PRETTY_FUNCTION)
182 
183 #define FIXME(note) \
184  Sawyer::Assert::fail("needs to be fixed", NULL, (note), \
185  __FILE__, __LINE__, SAWYER_PRETTY_FUNCTION)
186 
187 #endif
void fail(const char *mesg, const char *expr, const std::string &note, const char *filename, unsigned linenum, const char *funcname) __attribute__((noreturn))
Cause immediate failure.
void(* AssertFailureHandler)(const char *mesg, const char *expr, const std::string &note, const char *filename, unsigned linenum, const char *funcname)
Type for user-defined assertion failure handler.
Definition: Assert.h:98
Name space for the entire library.
Definition: FeasiblePath.h:787
AssertFailureHandler assertFailureHandler
Optional user callback to handle assertion failures.