Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / nlassert / repo / include / nlassert.h
1 /*
2  *   Copyright 2010-2016 Nest Labs Inc. All Rights Reserved.
3  *
4  *   Licensed under the Apache License, Version 2.0 (the "License");
5  *   you may not use this file except in compliance with the License.
6  *   You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *   Unless required by applicable law or agreed to in writing, software
11  *   distributed under the License is distributed on an "AS IS" BASIS,
12  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *   See the License for the specific language governing permissions and
14  *   limitations under the License.
15  *
16  */
17
18 /**
19  *    @file
20  *      This file defines macros and interfaces for performing both
21  *      compile- and run-time assertion checking and run-time
22  *      exception handling.
23  *
24  *      Where exception-handing is concerned, the format of the macros
25  *      are inspired by those found in Mac OS Classic and, later, Mac
26  *      OS X. These, in turn, were inspired by "Living In an
27  *      Exceptional World" by Sean Parent (develop, The Apple
28  *      Technical Journal, Issue 11, August/September 1992)
29  *      <http://www.mactech.com/articles/develop/issue_11/Parent_final.html>
30  *      for the methodology behind these error handling and assertion
31  *      macros.
32  *
33  */
34
35 /**
36  *  @mainpage notitle
37  *
38  *  @section introduction Introduction
39  *
40  *  This package defines macros and interfaces for performing both
41  *  compile- and run-time assertion checking and run-time exception
42  *  handling.
43  *
44  *  Where exception-handing is concerned, the format of the macros are
45  *  inspired by those found in Mac OS Classic and, later, Mac OS
46  *  X. These, in turn, were inspired by "Living In an Exceptional
47  *  World" by Sean Parent (develop, The Apple Technical Journal, Issue
48  *  11, August/September 1992). See:
49  *
50  *    http://www.mactech.com/articles/develop/issue_11/Parent_final.html
51  *
52  *  for the methodology behind these error handling and assertion
53  *  macros.
54  *
55  *  @section overview Overview
56  *
57  *  The interfaces in this package come in two interface modalities:
58  *
59  *  <dl>
60  *     <dt>[Run-time](@ref run-time)</dt>
61  *         <dd>Interfaces that dynamically check a logical assertion and
62  *             alter run-time execution on assertion firing.</dd>
63  *     <dt>[Compile-time](@ref compile-time)</dt>
64  *         <dd>Interfaces that statically check a logical assertion and
65  *             terminate compile-time execution on assertion firing.</dd>
66  *  </dl>
67  *
68  *  @subsection run-time Run-time
69  *
70  *  The [run-time modality interfaces](@ref run-time-interfaces) in
71  *  this package come in three families:
72  *
73  *  <dl>
74  *      <dt>[Assertion](@ref run-time-assertions)</dt>
75  *          <dd>Similar to the traditional C Standard Library
76  *          [assert()](http://pubs.opengroup.org/onlinepubs/009695399/functions/assert.html).</dd>
77  *
78  *      <dt>[Precondition](@ref preconditions)</dt>
79  *          <dd>Designed to be placed at the head of an interface or
80  *          method to check incoming parameters and return on
81  *          assertion failure.</dd>
82  *
83  *      <dt>[Exception](@ref exceptions)</dt>
84  *          <dd>Designed to jump to a local label on assertion failure
85  *          to support the method of error and exception handling that
86  *          advocates a single function or method return site and, by
87  *          extension, consolidated points of exception and error
88  *          handling as well as resource clean-up.</dd>
89  *  </dl>
90  *
91  *  There are several styles of interfaces within each family and
92  *  several potential variants within each style, all of which are
93  *  summarized below and are described in detail in the following
94  *  sections.
95  *
96  *  @subsection compile-time Compile-time
97  *
98  *  The [compile-time modality interfaces](@ref compile-time-interfaces)
99  *  in this package are simpler and come in a single family with a
100  *  couple of variants.
101  *
102  *  @section run-time-interfaces Run-time Interfaces
103  *
104  *  @subsection triggers Behavior Triggers
105  *
106  *  Unlike the traditional C Standard Library
107  *  [assert()](http://pubs.opengroup.org/onlinepubs/009695399/functions/assert.html)
108  *  or other assertion checking packages, this package offers the
109  *  ability to enable and customize one or more of a few triggers that
110  *  run when an assertion fires, including:
111  *
112  *     * [Abort](@ref abort-behavior)
113  *     * [Backtrace](@ref backtrace-behavior)
114  *     * [Log](@ref log-behavior)
115  *     * [Trap](@ref trap-behavior)
116  *
117  *  @subsubsection abort-behavior Abort
118  *
119  *  The abort behavior trigger allows, on an assertion firing, to
120  *  execute a trigger that terminates overall program or system
121  *  execution.
122  *
123  *  Note, however, the abort behavior trigger is only available in
124  *  some styles of the [assertion-family](@ref run-time-assertions) of
125  *  interfaces.
126  *
127  *  Please see #NL_ASSERT_ABORT() and @ref customization for more
128  *  information about this behavior trigger.
129  *
130  *  @subsubsection backtrace-behavior Backtrace
131  *
132  *  The backtrace behavior trigger allows, on an assertion firing, to
133  *  execute a trigger that generates a stack backtrace.
134  *
135  *  This style of assertion is available, when configured, on all
136  *  interface families.
137  *
138  *  Please see #NL_ASSERT_BACKTRACE() and @ref customization for more
139  *  information about this behavior trigger.
140  *
141  *  @subsubsection log-behavior Log
142  *
143  *  The log behavior trigger allows, on an assertion firing, to
144  *  execute a trigger that logs a message summarizing the assertion
145  *  that fired.
146  *
147  *  This style of assertion is available, when configured, on all
148  *  interface families.
149  *
150  *  Please see #NL_ASSERT_LOG() and @ref customization for more
151  *  information about this behavior trigger.
152  *
153  *  @subsubsection trap-behavior Trap
154  *
155  *  The log behavior trigger allows, on an assertion firing, to
156  *  execute a trigger that generates a debugger trap or exception.
157  *
158  *  This style of assertion is available, when configured, on all
159  *  interface families.
160  *
161  *  Please see #NL_ASSERT_TRAP() and @ref customization for more
162  *  information about this behavior trigger.
163  *
164  *  @subsection run-time-assertions Assertion Interfaces
165  *
166  *  The assertion interfaces are similar to the traditional C
167  *  Standard Library
168  *  [assert()](http://pubs.opengroup.org/onlinepubs/009695399/functions/assert.html).
169  *
170  *  These interfaces include the following styles:
171  *
172  *    * [Assert](@ref run-time-assert)
173  *    * [Abort](@ref run-time-abort)
174  *    * [Check](@ref run-time-check)
175  *    * [Verify](@ref run-time-verify)
176  *
177  *  The following table summarizes the relationship and features among
178  *  the styles:
179
180  *  <table>
181  *      <tr>
182  *          <th rowspan="2">Style</th>
183  *          <th colspan="4">Behaviors</th>
184  *          <th colspan="2">Availability</th>
185  *      </tr>
186  *      <tr>
187  *          <th>Abort</th>
188  *          <th>Backtrace</th>
189  *          <th>Log</th>
190  *          <th>Trap</th>
191  *          <th>Non-production</th>
192  *          <th>Production</th>
193  *      </tr>
194  *      <tr>
195  *          <td>Assert</td>
196  *          <td align="center">Non-production</td>
197  *          <td align="center">Non-production</td>
198  *          <td align="center">Non-production</td>
199  *          <td align="center"></td>
200  *          <td align="center">X</td>
201  *          <td align="center"></td>
202  *      </tr>
203  *      <tr>
204  *          <td>Abort</td>
205  *          <td align="center">X</td>
206  *          <td align="center">X</td>
207  *          <td align="center">X</td>
208  *          <td align="center"></td>
209  *          <td align="center">X</td>
210  *          <td align="center">X</td>
211  *      </tr>
212  *      <tr>
213  *          <td>Check</td>
214  *          <td align="center"></td>
215  *          <td align="center">Non-production</td>
216  *          <td align="center">Non-production</td>
217  *          <td align="center">Non-production</td>
218  *          <td align="center">X</td>
219  *          <td align="center"></td>
220  *      </tr>
221  *      <tr>
222  *          <td>Verify</td>
223  *          <td align="center"></td>
224  *          <td align="center">X</td>
225  *          <td align="center">X</td>
226  *          <td align="center">Non-production</td>
227  *          <td align="center">X</td>
228  *          <td align="center">X</td>
229  *      </tr>
230  *  </table>
231  *
232  *  @note The above described behaviors are only in effect when
233  *        #NL_ASSERT_USE_FLAGS_DEFAULT to 1. See @ref customization
234  *        for more information on configuring and customizing the
235  *        trigger behaviors.
236  *
237  *  @subsubsection run-time-assert Assert
238  *
239  *  These assertions are identical to the traditional C Standard
240  *  Library-style assertion (see
241  *  [assert()](http://pubs.opengroup.org/onlinepubs/009695399/functions/assert.html))
242  *  except that side-effects, if any, in the asserted expression will be
243  *  produced even when the assertion is made inactive, as in production build
244  *  configurations, by setting #NL_ASSERT_PRODUCTION to true.
245  *
246  *  Like the C Standard Library assertion, a trigger of this style of
247  *  assertion interface will invoke #NL_ASSERT_ABORT().
248  *
249  *  @subsubsection run-time-abort Abort
250  *
251  *  These assertions are identical to the @ref run-time-assert style
252  *  assertions; however, they are active in __both__ non-production
253  *  __and__ production build configurations.
254  *
255  *  A trigger of this style of assertion interface will invoke #NL_ASSERT_ABORT().
256  *
257  *  @subsubsection run-time-check Check
258  *
259  *  These assertions are similar to the @ref run-time-assert style; however,
260  *  this style __does not__ abort. Normal program flow and execution
261  *  will continue past this assertion.
262  *
263  *  Side-effects, if any, in the asserted expression will be produced even when
264  *  the assertion is made inactive, as in production build configurations, by
265  *  setting #NL_ASSERT_PRODUCTION to true.
266  *
267  *  @subsubsection run-time-verify Verify
268  *
269  *  These assertions are similar to the @ref run-time-abort style; however,
270  *  this style __does not__ abort. Normal program flow and execution
271  *  will continue past this assertion.
272  *
273  *  These are active in __both__ non-production __and__ production
274  *  build configurations.
275  *
276  *  @subsection preconditions Precondition Interfaces
277  *
278  *  These assertions are designed to be placed at the head of an
279  *  interface or method to check incoming parameters.
280  *
281  *  These assertions implicitly return, either void or a specified
282  *  value for non-void interfaces or methods.
283  *
284  *  @note This family of interfaces may be in violation of your site's
285  *  or project's coding style and best practices by virtue of its
286  *  implicit return. If so, please consider using the
287  *  [exception-style](@ref exceptions) interfaces instead.
288  *
289  *  The following table summarizes the features for this family of interfaces:
290  *
291  *  <table>
292  *      <tr>
293  *          <th rowspan="2">Style</th>
294  *          <th colspan="4">Behaviors</th>
295  *          <th colspan="2">Availability</th>
296  *      </tr>
297  *      <tr>
298  *          <th>Abort</th>
299  *          <th>Backtrace</th>
300  *          <th>Log</th>
301  *          <th>Trap</th>
302  *          <th>Non-production</th>
303  *          <th>Production</th>
304  *      </tr>
305  *      <tr>
306  *          <td>Precondition</td>
307  *          <td align="center"></td>
308  *          <td align="center">X</td>
309  *          <td align="center">X</td>
310  *          <td align="center"></td>
311  *          <td align="center">X</td>
312  *          <td align="center">X</td>
313  *      </tr>
314  *  </table>
315  *
316  *  @note The above described behaviors are only in effect when
317  *        #NL_ASSERT_USE_FLAGS_DEFAULT to 1. See @ref customization
318  *        for more information on configuring and customizing the
319  *        trigger behaviors.
320  *
321  *  @subsection exceptions Exception Interfaces
322  *
323  *  This family of interfaces is designed to support the method of
324  *  error and exception handling that advocates a single function or
325  *  method return site and, by extension, consolidated points of
326  *  exception and error handling as well as resource clean-up.
327  *
328  *  A general example usage of this family of interfaces is:
329  *
330  *  @code
331  *  int Bar(uint8_t **aBuffer, const Foo *aParameter)
332  *  {
333  *      const size_t size   = 1024;
334  *      int          retval = 0;
335  *
336  *      nlREQUIRE(aBuffer != NULL, exit, retval = -EINVAL);
337  *
338  *      *aBuffer = (uint8_t *)malloc(size);
339  *      nlREQUIRE(*aBuffer != NULL, exit, retval = -ENOMEM);
340  *
341  *      memset(*aBuffer, 0, size);
342  *
343  *  exit:
344  *      return retval;
345  *  }
346  *  @endcode
347  *
348  *  As shown in the example, this family checks for the specified
349  *  condition, which is expected to commonly be true, and branches to
350  *  the specified label if the condition is false.
351  *
352  *    * [Expect](@ref expect)
353  *    * [Desire](@ref desire)
354  *    * [Require](@ref require)
355  *
356  *  This family of interfaces are all identical across the styles and
357  *  all styles support an identical set of variants. The only
358  *  difference among them is the default configured trigger action
359  *  behavior, as summarized in the table below:
360  *
361  *  <table>
362  *      <tr>
363  *          <th rowspan="2">Style</th>
364  *          <th colspan="3">Behaviors</th>
365  *          <th colspan="2">Availability</th>
366  *      </tr>
367  *      <tr>
368  *          <th>Backtrace</th>
369  *          <th>Log</th>
370  *          <th>Trap</th>
371  *          <th>Non-production</th>
372  *          <th>Production</th>
373  *      </tr>
374  *      <tr>
375  *          <td>Expect</td>
376  *          <td></td>
377  *          <td></td>
378  *          <td></td>
379  *          <td align="center">X</td>
380  *          <td align="center">X</td>
381  *      </tr>
382  *      <tr>
383  *          <td>Desire</td>
384  *          <td></td>
385  *          <td align="center">Non-production</td>
386  *          <td></td>
387  *          <td align="center">X</td>
388  *          <td align="center">X</td>
389  *      </tr>
390  *      <tr>
391  *          <td>Require</td>
392  *          <td align="center">X</td>
393  *          <td align="center">X</td>
394  *          <td align="center">Non-production</td>
395  *          <td align="center">X</td>
396  *          <td align="center">X</td>
397  *      </tr>
398  *  </table>
399  *
400  *  @note The above described behaviors are only in effect when
401  *        #NL_ASSERT_USE_FLAGS_DEFAULT to 1. See @ref customization
402  *        for more information on configuring and customizing the
403  *        trigger behaviors.
404  *
405  *  @subsubsection expect Expect
406  *
407  *  These assertions are designed to be placed anywhere an exceptional
408  *  condition might occur where handling needs to locally jump to
409  *  error-handling code.
410  *
411  *  These checks are always present and do nothing beyond branching to
412  *  the named exception label. Consequently, they are simply mnemonics
413  *  or syntactic sugar.
414  *
415  *  This style of check should be used where either true or false
416  *  evaluation of the assertion expression are equally likely since
417  *  there is no default configured trigger behavior.
418  *
419  *  __Anticipated Assertion Firing Frequency:__ Frequent
420  *
421  *  @subsubsection desire Desire
422  *
423  *  These are identical to the @ref expect style checks; except for
424  *  their non-production and production configured behavior.
425  *
426  *  This style of check should be used where false evaluation of the
427  *  assertion expression is expected to be occasional.
428  *
429  *  __Anticipated Assertion Firing Frequency:__ Occasional
430  *
431  *  @subsubsection require Require
432  *
433  *  These are identical to the @ref expect style checks; except for
434  *  their non-production and production configured behavior.
435  *
436  *  This style of check should be used where false evaluation of the
437  *  assertion expression is expected to be rare.
438  *
439  *  __Anticipated Assertion Firing Frequency:__ Rare
440  *
441  * @subsection variants Variants
442  *
443  * The three families of run-time interface are available in one of several
444  * variants, as summarized below. <em>&lt;STYLE&gt;</em> may be replaced with one of (see [Run-time Availability](@ref run-time-availability) for details):
445  *
446  *   * ASSERT
447  *   * ABORT
448  *   * CHECK
449  *   * VERIFY
450  *   * PRECONDITION
451  *   * EXPECT
452  *   * DESIRE
453  *   * REQUIRE
454  *
455  * to form an actual interface name.
456  *
457  * | Interface Variant                              | Description                                                                                                  |
458  * |:---------------------------------------------- |:------------------------------------------------------------------------------------------------------------ |
459  * | nl<em>&lt;STYLE&gt;</em>                       | Base variant; execute the check.                                                                             |
460  * | nl<em>&lt;STYLE&gt;</em>\_ACTION               | Execute the base check and execute the action if the check fails.                                            |
461  * | nl<em>&lt;STYLE&gt;</em>\_PRINT                | Execute the base check and print the descriptive string if the check fails.                                  |
462  * | nl<em>&lt;STYLE&gt;</em>\_ACTION_PRINT         | Execute the base check and both execute the action and print the descriptive string if the check fails.      |
463  * | nl<em>&lt;STYLE&gt;</em>\_SUCCESS              | Adds a check against zero (0) as the logical condition to assert.                                            |
464  * | nl<em>&lt;STYLE&gt;</em>\_SUCCESS_ACTION       | Execute the success check and execute the action if the check fails.                                         |
465  * | nl<em>&lt;STYLE&gt;</em>\_SUCCESS_PRINT        | Execute the success check and print the descriptive string if the check fails.                               |
466  * | nl<em>&lt;STYLE&gt;</em>\_SUCCESS_ACTION_PRINT | Execute the success check and both execute the action and print the descriptive string if the check fails.   |
467  * | nlN<em>&lt;STYLE&gt;</em>                      | Inverts the logical sense of the base check; execute the check.                                              |
468  * | nlN<em>&lt;STYLE&gt;</em>\_ACTION              | Execute the inversion check and execute the action if the check fails.                                       |
469  * | nlN<em>&lt;STYLE&gt;</em>\_PRINT               | Execute the inversion check and print the descriptive string if the check fails.                             |
470  * | nlN<em>&lt;STYLE&gt;</em>\_ACTION_PRINT        | Execute the inversion check and both execute the action and print the descriptive string if the check fails. |
471  *
472  * @section run-time-availability Run-time Availability
473  *
474  * The following table indicates the availability of the run-time interface
475  * variants for each style.
476  *
477  * <table>
478  *     <tr>
479  *         <th rowspan="2">Interface Variant</th>
480  *         <th colspan="8">Style</th>
481  *     </tr>
482  *     <tr>
483  *         <th>Assert</th>
484  *         <th>Abort</th>
485  *         <th>Check</th>
486  *         <th>Verify</th>
487  *         <th>Precondition</th>
488  *         <th>Expect</th>
489  *         <th>Desire</th>
490  *         <th>Require</th>
491  *     </tr>
492  *     <tr>
493  *         <td>nl<em>&lt;STYLE&gt;</em></td>
494  *         <td align="center">X</td>
495  *         <td align="center">X</td>
496  *         <td align="center">X</td>
497  *         <td align="center">X</td>
498  *         <td align="center">X</td>
499  *         <td align="center">X</td>
500  *         <td align="center">X</td>
501  *         <td align="center">X</td>
502  *     </tr>
503  *     <tr>
504  *         <td>nl<em>&lt;STYLE&gt;</em>\_ACTION</td>
505  *         <td align="center">X</td>
506  *         <td align="center">X</td>
507  *         <td align="center">X</td>
508  *         <td align="center">X</td>
509  *         <td align="center">X</td>
510  *         <td align="center">X</td>
511  *         <td align="center">X</td>
512  *         <td align="center">X</td>
513  *     </tr>
514  *     <tr>
515  *         <td>nl<em>&lt;STYLE&gt;</em>\_PRINT</td>
516  *         <td align="center"></td>
517  *         <td align="center"></td>
518  *         <td align="center">X</td>
519  *         <td align="center">X</td>
520  *         <td align="center">X</td>
521  *         <td align="center">X</td>
522  *         <td align="center">X</td>
523  *         <td align="center">X</td>
524  *     </tr>
525  *     <tr>
526  *         <td>nl<em>&lt;STYLE&gt;</em>\_ACTION_PRINT</td>
527  *         <td align="center"></td>
528  *         <td align="center"></td>
529  *         <td align="center"></td>
530  *         <td align="center"></td>
531  *         <td align="center"></td>
532  *         <td align="center">X</td>
533  *         <td align="center">X</td>
534  *         <td align="center">X</td>
535  *     </tr>
536  *     <tr>
537  *         <td>nl<em>&lt;STYLE&gt;</em>\_SUCCESS</td>
538  *         <td align="center"></td>
539  *         <td align="center"></td>
540  *         <td align="center">X</td>
541  *         <td align="center">X</td>
542  *         <td align="center">X</td>
543  *         <td align="center">X</td>
544  *         <td align="center">X</td>
545  *         <td align="center">X</td>
546  *     </tr>
547  *     <tr>
548  *         <td>nl<em>&lt;STYLE&gt;</em>\_SUCCESS_ACTION</td>
549  *         <td align="center"></td>
550  *         <td align="center"></td>
551  *         <td align="center">X</td>
552  *         <td align="center">X</td>
553  *         <td align="center">X</td>
554  *         <td align="center">X</td>
555  *         <td align="center">X</td>
556  *         <td align="center">X</td>
557  *     </tr>
558  *     <tr>
559  *         <td>nl<em>&lt;STYLE&gt;</em>\_SUCCESS_PRINT</td>
560  *         <td align="center"></td>
561  *         <td align="center"></td>
562  *         <td align="center">X</td>
563  *         <td align="center">X</td>
564  *         <td align="center">X</td>
565  *         <td align="center">X</td>
566  *         <td align="center">X</td>
567  *         <td align="center">X</td>
568  *     </tr>
569  *     <tr>
570  *         <td>nl<em>&lt;STYLE&gt;</em>\_SUCCESS_ACTION_PRINT</td>
571  *         <td align="center"></td>
572  *         <td align="center"></td>
573  *         <td align="center"></td>
574  *         <td align="center"></td>
575  *         <td align="center"></td>
576  *         <td align="center">X</td>
577  *         <td align="center">X</td>
578  *         <td align="center">X</td>
579  *     </tr>
580  *     <tr>
581  *         <td>nlN<em>&lt;STYLE&gt;</em></td>
582  *         <td align="center"></td>
583  *         <td align="center"></td>
584  *         <td align="center">X</td>
585  *         <td align="center">X</td>
586  *         <td align="center">X</td>
587  *         <td align="center">X</td>
588  *         <td align="center">X</td>
589  *         <td align="center">X</td>
590  *     </tr>
591  *     <tr>
592  *         <td>nlN<em>&lt;STYLE&gt;</em>\_ACTION</td>
593  *         <td align="center"></td>
594  *         <td align="center"></td>
595  *         <td align="center">X</td>
596  *         <td align="center">X</td>
597  *         <td align="center">X</td>
598  *         <td align="center">X</td>
599  *         <td align="center">X</td>
600  *         <td align="center">X</td>
601  *     </tr>
602  *     <tr>
603  *         <td>nlN<em>&lt;STYLE&gt;</em>\_PRINT</td>
604  *         <td align="center"></td>
605  *         <td align="center"></td>
606  *         <td align="center">X</td>
607  *         <td align="center">X</td>
608  *         <td align="center">X</td>
609  *         <td align="center">X</td>
610  *         <td align="center">X</td>
611  *         <td align="center">X</td>
612  *     </tr>
613  *     <tr>
614  *         <td>nlN<em>&lt;STYLE&gt;</em>\_ACTION_PRINT</td>
615  *         <td align="center"></td>
616  *         <td align="center"></td>
617  *         <td align="center"></td>
618  *         <td align="center"></td>
619  *         <td align="center"></td>
620  *         <td align="center">X</td>
621  *         <td align="center">X</td>
622  *         <td align="center">X</td>
623  *     </tr>
624  * </table>
625  *
626  * @section customization Customization
627  *
628  * The Nest Labs Assertion library is designed "out of the box" to
629  * provide a high degree of utility. However, there are a number of
630  * aspects that can be tuned on a per-module, -project, or -site basis
631  * to customize the appearance and behavior through user-definable
632  * attributes via the C preprocessor.
633  *
634  * @subsection userattrs User-definable Attributes
635  *
636  * The following attributes may be defined by the user before
637  * the nlassert.h header is included by the preprocessor, overriding
638  * the default behavior:
639  *
640  *   * #NL_ASSERT_PRODUCTION
641  *   * #NL_ASSERT_PREFIX_STRING
642  *   * #NL_ASSERT_COMPONENT_STRING
643  *   * #NL_ASSERT_FILE
644  *   * #NL_ASSERT_ABORT
645  *   * #NL_ASSERT_BACKTRACE
646  *   * #NL_ASSERT_LOG
647  *   * #NL_ASSERT_TRAP
648  *
649  * Without any customization, all of the interface styles are set to
650  * use __no__ trigger behaviors (i.e. #NL_ASSERT_FLAG_NONE). However, a
651  * set of default trigger behaviors (as documented in the tables
652  * above) may be enabled by setting #NL_ASSERT_USE_FLAGS_DEFAULT to 1
653  * before nlassert.h is included by the preprocessor.
654  *
655  * Otherwise, the following attributes may be overridden, customizing
656  * the trigger behavior of each assertion interface family for both
657  * non-production and production configurations:
658  *
659  *   * #NL_ASSERT_EXPECT_FLAGS
660  *
661  *   * #NL_ASSERT_CHECK_NONPRODUCTION_FLAGS
662  *   * #NL_ASSERT_VERIFY_NONPRODUCTION_FLAGS
663  *   * #NL_ASSERT_ASSERT_NONPRODUCTION_FLAGS
664  *   * #NL_ASSERT_ABORT_NONPRODUCTION_FLAGS
665  *   * #NL_ASSERT_DESIRE_NONPRODUCTION_FLAGS
666  *   * #NL_ASSERT_REQUIRE_NONPRODUCTION_FLAGS
667  *   * #NL_ASSERT_PRECONDITION_NONPRODUCTION_FLAGS
668  *
669  *   * #NL_ASSERT_VERIFY_PRODUCTION_FLAGS
670  *   * #NL_ASSERT_ABORT_PRODUCTION_FLAGS
671  *   * #NL_ASSERT_DESIRE_PRODUCTION_FLAGS
672  *   * #NL_ASSERT_REQUIRE_PRODUCTION_FLAGS
673  *   * #NL_ASSERT_PRECONDITION_PRODUCTION_FLAGS
674  *
675  *  @section compile-time-interfaces Compile-time Interfaces
676  *
677  *  The compile-time interfaces are a limited subset of their run-time
678  *  counterparts. Rather than altering run-time execution on assertion
679  *  firing against a dynamically-checked run-time condition, these
680  *  interfaces terminate compilation against a statically-checked
681  *  compile-time condition.
682  *
683  *  @subsection compile-time-assertions Assertion Interfaces
684  *
685  *  These interfaces have only one style:
686  *
687  *    * [Assert](@ref compile-time-assert)
688  *
689  *  <table>
690  *      <tr>
691  *          <th rowspan="2">Style</th>
692  *          <th colspan="2">Availability</th>
693  *      </tr>
694  *      <tr>
695  *          <th>Non-production</th>
696  *          <th>Production</th>
697  *      </tr>
698  *      <tr>
699  *          <td>Assert</td>
700  *          <td align="center">X</td>
701  *          <td align="center">X</td>
702  *      </tr>
703  *  </table>
704  *
705  *  @subsubsection compile-time-assert Assert
706  *
707  *  These assertions are active in __both__ non-production
708  *  __and__ production build configurations. This behavior
709  *  is unconventional with respect to the [run-time assert-style assertions](@ref run-time-assert)
710  *  but very conventional with respect to the similar
711  *  C11/C++11 static-assertion standards.
712  *
713  * @subsection compile-time-variants Variants
714  *
715  * The compile-time interfaces are available in two variants:
716  *
717  * | Interface Variant                              | Description                                                                                                  |
718  * |:---------------------------------------------- |:------------------------------------------------------------------------------------------------------------ |
719  * | nlSTATIC\_ASSERT                               | Base variant; execute the check.                                                                             |
720  * | nlSTATIC\_ASSERT\_PRINT                        | Execute the base check with a descriptive string. Note, this string is not actually emitted in any meaningful way. It serves to simply comment or annotate the assertion and to provide interface parallelism with the run-time assertion interfaces.                                  |
721  *
722  * @section compile-time-availability Compile-time Availability
723  *
724  * The following table indicates the availability of the compile-time interface
725  * variants.
726  *
727  * <table>
728  *     <tr>
729  *         <th rowspan="2">Interface Variant</th>
730  *         <th colspan="2">Availability</th>
731  *     </tr>
732  *     <tr>
733  *         <th>Non-Production</th>
734  *         <th>Production</th>
735  *     </tr>
736  *     <tr>
737  *         <td>nlSTATIC\_ASSERT</td>
738  *         <td align="center">X</td>
739  *         <td align="center">X</td>
740  *     </tr>
741  *     <tr>
742  *         <td>nlSTATIC\_ASSERT\_PRINT</td>
743  *         <td align="center">X</td>
744  *         <td align="center">X</td>
745  *     </tr>
746  * </table>
747  *
748  * @section compatibility Standard C Library Compatibility
749  *
750  * This package also provides an ISO/IEC 9899:1999-, C89-, and
751  * C99-compatible Standard C Library header (via assert.h) and
752  * assertion interface definition (assert()), implemented atop Nest
753  * Labs assertion checking and runtime exception interfaces such that
754  * consistent platform and system capabilities, behavior, and output
755  * may be implemented and enforced across the two interfaces.
756  *
757  * Systems wishing to use this compatibility header and interface in
758  * lieu of their Standard C Library header of the same name should
759  * ensure that their toolchain is configured to either ignore or
760  * deprioritize standard search paths while placing the directory this
761  * header is contained in among the preferred header search paths.
762  *
763  */
764
765 #ifndef NLCORE_NLASSERT_H
766 #define NLCORE_NLASSERT_H
767
768 #include <stdint.h>
769 #include <stdlib.h>
770 #include <stdio.h>
771
772 /**
773  *  @name Behavioral Control Flags
774  *
775  *  @brief
776  *    These flags are used to influence the behavior of the various
777  *    classes and styles of assertion macros.
778  *
779  *  @{
780  *
781  */
782
783 /**
784  *  @def NL_ASSERT_FLAG_NONE
785  *
786  *  @brief
787  *    Perform no actions when an assertion expression evaluates to
788  *    false.
789  *
790  *  @sa #NL_ASSERT_FLAG_BACKTRACE
791  *  @sa #NL_ASSERT_FLAG_LOG
792  *  @sa #NL_ASSERT_FLAG_TRAP
793  *
794  */
795 #define NL_ASSERT_FLAG_NONE             0x00000000
796
797 /**
798  *  @def NL_ASSERT_FLAG_BACKTRACE
799  *
800  *  @brief
801  *    Invoke #NL_ASSERT_BACKTRACE() when an assertion expression
802  *    evaluates to false.
803  *
804  *  @note For the *_ACTION*-style assertion variants,
805  *        #NL_ASSERT_BACKTRACE() is a pre-action trigger and will run
806  *        before the specified action.
807  *
808  *  @sa #NL_ASSERT_FLAG_NONE
809  *  @sa #NL_ASSERT_FLAG_LOG
810  *  @sa #NL_ASSERT_FLAG_TRAP
811  *
812  *  @sa #NL_ASSERT_BACKTRACE
813  *
814  */
815 #define NL_ASSERT_FLAG_BACKTRACE        0x00000001
816
817 /**
818  *  @def NL_ASSERT_FLAG_LOG
819  *
820  *  @brief
821  *    Invoke #NL_ASSERT_LOG() when an assertion expression evaluates
822  *    to false.
823  *
824  *  @note For the *_ACTION*-style assertion variants,
825  *        #NL_ASSERT_LOG() is a pre-action trigger and will run
826  *        before the specified action.
827  *
828  *  @sa #NL_ASSERT_FLAG_NONE
829  *  @sa #NL_ASSERT_FLAG_BACKTRACE
830  *  @sa #NL_ASSERT_FLAG_TRAP
831  *
832  *  @sa #NL_ASSERT_LOG
833  *
834  */
835 #define NL_ASSERT_FLAG_LOG              0x00000002
836
837 /**
838  *  @def NL_ASSERT_FLAG_TRAP
839  *
840  *  @brief
841  *    Invoke #NL_ASSERT_TRAP() when an assertion expression evaluates
842  *    to false.
843  *
844  *  @note For the *_ACTION*-style assertion variants,
845  *        #NL_ASSERT_TRAP() is a post-action trigger and will run
846  *        after the specified action.
847  *
848  *  @sa #NL_ASSERT_FLAG_NONE
849  *  @sa #NL_ASSERT_FLAG_BACKTRACE
850  *  @sa #NL_ASSERT_FLAG_LOG
851  *
852  *  @sa #NL_ASSERT_TRAP
853  *
854  */
855 #define NL_ASSERT_FLAG_TRAP             0x00000004
856
857 /**
858  *  @}
859  *
860  */
861
862 /**
863  *  @def NL_ASSERT_USE_FLAGS_DEFAULT
864  *
865  *  @brief
866  *    Enable (1) or disable (0) use of the default trigger behavior flags.
867  *
868  *  This enables or disables the use of the default trigger behavior
869  *  flags as specified by:
870  *
871  *   * #NL_ASSERT_EXPECT_FLAGS_DEFAULT
872  *
873  *   * #NL_ASSERT_CHECK_NONPRODUCTION_FLAGS_DEFAULT
874  *   * #NL_ASSERT_VERIFY_NONPRODUCTION_FLAGS_DEFAULT
875  *   * #NL_ASSERT_ASSERT_NONPRODUCTION_FLAGS_DEFAULT
876  *   * #NL_ASSERT_ABORT_NONPRODUCTION_FLAGS_DEFAULT
877  *   * #NL_ASSERT_DESIRE_NONPRODUCTION_FLAGS_DEFAULT
878  *   * #NL_ASSERT_REQUIRE_NONPRODUCTION_FLAGS_DEFAULT
879  *   * #NL_ASSERT_PRECONDITION_NONPRODUCTION_FLAGS_DEFAULT
880  *
881  *   * #NL_ASSERT_VERIFY_PRODUCTION_FLAGS_DEFAULT
882  *   * #NL_ASSERT_ABORT_PRODUCTION_FLAGS_DEFAULT
883  *   * #NL_ASSERT_DESIRE_PRODUCTION_FLAGS_DEFAULT
884  *   * #NL_ASSERT_REQUIRE_PRODUCTION_FLAGS_DEFAULT
885  *   * #NL_ASSERT_PRECONDITION_PRODUCTION_FLAGS_DEFAULT
886  *
887  *  Setting this to 1, effectively does the following:
888  *
889  *  @code
890  *  #define NL_ASSERT_<STYLE>_<CONFIGURATION>_FLAGS NL_ASSERT_<STYLE>_<CONFIGURATION>_FLAGS_DEFAULT
891  *  @endcode
892  *
893  *  for each assertion interface <em>&lt;STYLE&gt;</em> for both
894  *  non-production and production (see #NL_ASSERT_PRODUCTION) <em>&lt;CONFIGURATION&gt;</em> .
895  *
896  */
897 #if !defined(NL_ASSERT_USE_FLAGS_DEFAULT)
898 #define NL_ASSERT_USE_FLAGS_DEFAULT     0
899 #endif /* !defined(NL_ASSERT_USE_FLAGS_DEFAULT) */
900
901 /**
902  *  @def NL_ASSERT_PRODUCTION
903  *
904  *  @brief
905  *    Enable (1) or disable (0) when production (or non-production)
906  *    assertion behavior is desired.
907  *
908  *  When production behavior is asserted, a number of interface
909  *  families are elided entirely and for others, the default behavior
910  *  changes (default: ((defined(NDEBUG) && NDEBUG) || (defined(DEBUG)
911  *  && !DEBUG) || 1)).
912  *
913  */
914 #if !defined(NL_ASSERT_PRODUCTION)
915 #if defined(NDEBUG)
916 #define NL_ASSERT_PRODUCTION            NDEBUG
917 #elif defined(DEBUG)
918 #define NL_ASSERT_PRODUCTION            !DEBUG
919 #else
920 #define NL_ASSERT_PRODUCTION            1
921 #endif /* defined(NDEBUG) */
922 #endif /* !defined(NL_ASSERT_PRODUCTION) */
923
924 /**
925  *  @name Log Output Definitions
926  *
927  *  @brief
928  *    These definitions control how the output of assertion log
929  *    messages appear, when so configured, on assertion expression
930  *    failure evaluation.
931  *
932  *  @{
933  *
934  */
935
936 /**
937  *  @def NL_ASSERT_PREFIX_STRING
938  *
939  *  @brief
940  *    This is the string printed at the beginning of the assertion
941  *    printed (default: 'ASSERT: ').
942  *
943  *    Developers may, but are generally not encouraged to, override
944  *    this by defining #NL_ASSERT_PREFIX_STRING before nlassert.h is
945  *    included by the preprocessor, as shown in the following example:
946  *
947  *  @code
948  *    #define NL_ASSERT_PREFIX_STRING   "assertion: "
949  *
950  *    #include <nlassert.h>
951  *  @endcode
952  *
953  *  @sa #NL_ASSERT_PREFIX_STRING
954  */
955 #if !defined(NL_ASSERT_PREFIX_STRING)
956 #define NL_ASSERT_PREFIX_STRING         "ASSERT: "
957 #endif /* !defined(NL_ASSERT_PREFIX_STRING) */
958
959 /**
960  *  @def NL_ASSERT_COMPONENT_STRING
961  *
962  *  @brief
963  *    This is the string printed following the prefix string (see
964  *    #NL_ASSERT_PREFIX_STRING) that indicates what module, program,
965  *    application or subsystem the assertion occurred in (default:
966  *    '').
967  *
968  *    Developers may override this by defining
969  *    #NL_ASSERT_COMPONENT_STRING before nlassert.h is included by the
970  *    preprocessor, as shown in the following example:
971  *
972  *  @code
973  *    #define NL_ASSERT_COMPONENT_STRING   "nlbar"
974  *
975  *    #include <nlassert.h>
976  *  @endcode
977  *
978  *  @sa #NL_ASSERT_PREFIX_STRING
979  *
980  */
981 #if !defined(NL_ASSERT_COMPONENT_STRING)
982 #define NL_ASSERT_COMPONENT_STRING      ""
983 #endif  /* !defined(NL_ASSERT_COMPONENT_STRING) */
984
985 /**
986  *  @}
987  *
988  */
989
990 /**
991  *  @def NL_ASSERT_ABORT()
992  *
993  *  @brief
994  *    This macro is invoked when an nlASSERT- or nlABORT-style
995  *    assertion expression evaluates to false.
996  *
997  *    By default, this is defined to the C Standard Library
998  *    [abort()](http://pubs.opengroup.org/onlinepubs/009695399/functions/abort.html). When
999  *    overridden, this should generally provide similar functionality,
1000  *    terminating the overall process or system execution.
1001  *
1002  *    Developers may override and customize this by defining
1003  *    #NL_ASSERT_ABORT() before nlassert.h is included by the
1004  *    preprocessor.
1005  *
1006  */
1007 #if !defined(NL_ASSERT_ABORT)
1008 #define NL_ASSERT_ABORT()               abort()
1009 #endif /* !defined(NL_ASSERT_ABORT) */
1010
1011 /**
1012  *  @def NL_ASSERT_BACKTRACE()
1013  *
1014  *  @brief
1015  *    This macro is invoked when an assertion expression evaluates to
1016  *    false when the macro has been configured with / passed
1017  *    #NL_ASSERT_FLAG_BACKTRACE.
1018  *
1019  *  By default, this is defined to do nothing. However, when defined,
1020  *  this should generally generate a stack back trace when invoked.
1021  *
1022  *  Developers may override and customize this by defining
1023  *  #NL_ASSERT_BACKTRACE() before nlassert.h is included by the
1024  *  preprocessor.
1025  *
1026  *  @note For the *_ACTION*-style assertion variants,
1027  *        #NL_ASSERT_BACKTRACE() is a pre-action trigger and will run
1028  *        before the specified action.
1029  *
1030  *  @sa #NL_ASSERT_FLAG_BACKTRACE
1031  *
1032  */
1033 #if !defined(NL_ASSERT_BACKTRACE)
1034 #define NL_ASSERT_BACKTRACE()
1035 #endif /* !defined(NL_ASSERT_BACKTRACE) */
1036
1037 /**
1038  *  @def NL_ASSERT_TRAP()
1039  *
1040  *  @brief
1041  *    This macro is invoked when an assertion expression evaluates to
1042  *    false when the macro has been configured with / passed
1043  *    #NL_ASSERT_FLAG_TRAP.
1044  *
1045  *  By default, this is defined to do nothing. However, when defined,
1046  *  this should generally generate a debug trap or breakpoint such that
1047  *  when the assertion expression evaluates to false an attached
1048  *  debugger will stop at the assertion point.
1049  *
1050  *  Developers may override and customize this by defining
1051  *  #NL_ASSERT_TRAP() before nlassert.h is included by the
1052  *  preprocessor, as shown in the following example:
1053  *
1054  *  @code
1055  *    #if defined(__i386__) || defined(__x86_64__)
1056  *    #define DEBUGGER_TRAP    __asm__ __volatile__("int3")
1057  *    #elif defined(__arm__)
1058  *    #define DEBUGGER_TRAP    __asm__ __volatile__("bkpt")
1059  *    #endif
1060  *
1061  *    #define NL_ASSERT_TRAP()                               \
1062  *        do                                                 \
1063  *        {                                                  \
1064  *            if (IsDebuggerAttached())                      \
1065  *            {                                              \
1066  *                DEBUGGER_TRAP;                             \
1067  *            }                                              \
1068  *        } while (0)
1069  *
1070  *    #include <nlassert.h>
1071  *  @endcode
1072  *
1073  *  @note For the *_ACTION*-style assertion variants,
1074  *        #NL_ASSERT_TRAP() is a post-action trigger and will run
1075  *        after the specified action.
1076  *
1077  *  @sa #NL_ASSERT_FLAG_TRAP
1078  *
1079  */
1080 #if !defined(NL_ASSERT_TRAP)
1081 #define NL_ASSERT_TRAP()
1082 #endif /* !defined(NL_ASSERT_TRAP) */
1083
1084 /**
1085  *  @def NL_ASSERT_LOG_FORMAT_DEFAULT
1086  *
1087  *  @brief
1088  *    This is the NULL-terminated C string literal with C Standard
1089  *    Library-style format specifiers used by #NL_ASSERT_LOG_DEFAULT.
1090  *
1091  *  This may be used by overrides to #NL_ASSERT_LOG that want to use a
1092  *  consistent output formatting.
1093  *
1094  *  @sa #NL_ASSERT_LOG_DEFAULT
1095  *
1096  */
1097 #define NL_ASSERT_LOG_FORMAT_DEFAULT                           "%s%s%s%s, %s%sfile: %s, line: %d\n"
1098
1099 /**
1100  *  @def NL_ASSERT_LOG_DEFAULT(aPrefix, aName, aCondition, aLabel, aFile, aLine, aMessage)
1101  *
1102  *  @brief
1103  *    This macro conforms to #NL_ASSERT_LOG and may be assigned to
1104  *    #NL_ASSERT_LOG, as shown in the following example, to display an
1105  *    assertion message when the assertion triggers, via the C
1106  *    Standard I/O Library error stream.
1107  *
1108  *  @code
1109  *    #define NL_ASSERT_LOG(aPrefix, aName, aCondition, aLabel, aFile, aLine, aMessage) \
1110  *        NL_ASSERT_LOG_DEFAULT(aPrefix, aName, aCondition, aLabel, aFile, aLine, aMessage)
1111  *
1112  *    #include <nlassert.h>
1113  *  @endcode
1114  *
1115  *  @param[in]  aPrefix     A pointer to a NULL-terminated C string printed
1116  *                          at the beginning of the logged assertion
1117  *                          message. Typically this is and should be
1118  *                          #NL_ASSERT_PREFIX_STRING.
1119  *  @param[in]  aName       A pointer to a NULL-terminated C string printed
1120  *                          following @p aPrefix that indicates what
1121  *                          module, program, application or subsystem
1122  *                          the assertion occurred in Typically this
1123  *                          is and should be
1124  *                          #NL_ASSERT_COMPONENT_STRING.
1125  *  @param[in]  aCondition  A pointer to a NULL-terminated C string indicating
1126  *                          the expression that evaluated to false in
1127  *                          the assertion. Typically this is a
1128  *                          stringified version of the actual
1129  *                          assertion expression.
1130  *  @param[in]  aLabel      An optional pointer to a NULL-terminated C string
1131  *                          indicating, for exception-style
1132  *                          assertions, the label that will be
1133  *                          branched to when the assertion expression
1134  *                          evaluates to false.
1135  *  @param[in]  aFile       A pointer to a NULL-terminated C string indicating
1136  *                          the file in which the exception
1137  *                          occurred. Typically this is and should be
1138  *                          \_\_FILE\_\_ from the C preprocessor or
1139  *                          #NL_ASSERT_FILE.
1140  *  @param[in]  aLine       The line number in @p aFile on which the assertion
1141  *                          expression evaluated to false. Typically
1142  *                          this is and should be \_\_LINE\_\_ from the C
1143  *                          preprocessor.
1144  *  @param[in]  aMessage    An optional pointer to a NULL-terminated C string
1145  *                          containing a caller-specified message
1146  *                          further describing the assertion failure.
1147  *
1148  *  @sa #NL_ASSERT_LOG
1149  *
1150  *  @sa #NL_ASSERT_FLAG_LOG
1151  *
1152  *  @sa #NL_ASSERT_PREFIX_STRING
1153  *  @sa #NL_ASSERT_COMPONENT_STRING
1154  *  @sa #NL_ASSERT_LOG_FORMAT_DEFAULT
1155  *
1156  *  @sa #NL_ASSERT_FILE
1157  *
1158  */
1159 #define NL_ASSERT_LOG_DEFAULT(aPrefix, aName, aCondition, aLabel, aFile, aLine, aMessage) \
1160     do                                                                                    \
1161     {                                                                                     \
1162         fprintf(stderr,                                                                   \
1163                 NL_ASSERT_LOG_FORMAT_DEFAULT,                                             \
1164                 aPrefix,                                                                  \
1165                 (((aName) == 0) || (*(aName) == '\0')) ? "" : aName,                      \
1166                 (((aName) == 0) || (*(aName) == '\0')) ? "" : ": ",                       \
1167                 aCondition,                                                               \
1168                 ((aMessage == 0) ? "" : aMessage),                                        \
1169                 ((aMessage == 0) ? "" : ", "),                                            \
1170                 aFile,                                                                    \
1171                 aLine);                                                                   \
1172     } while (0)
1173
1174 /**
1175  *  @def NL_ASSERT_LOG(aPrefix, aName, aCondition, aLabel, aFile, aLine, aMessage)
1176  *
1177  *  @brief
1178  *    This macro is invoked when an assertion expression evaluates to
1179  *    false when the macro has been configured with / passed
1180  *    #NL_ASSERT_FLAG_LOG.
1181  *
1182  *  This is intended to display a message, to an appropriate log stream, informing the developer where the assertion was and what expression failed, similar to:
1183  *
1184  *      "ASSERT: MyComponent: aPointer != NULL, bad pointer, file: foo.c, line: 453"
1185  *
1186  *  Developers may override and customize this by defining
1187  *  #NL_ASSERT_LOG() before nlassert.h is included by the
1188  *  preprocessor.
1189  *
1190  *  @note For the *_ACTION*-style assertion variants, #NL_ASSERT_LOG()
1191  *        is a pre-action trigger and will run before the specified action.
1192  *
1193  *  @param[in]  aPrefix     A pointer to a NULL-terminated C string printed
1194  *                          at the beginning of the logged assertion
1195  *                          message. Typically this is and should be
1196  *                          #NL_ASSERT_PREFIX_STRING.
1197  *  @param[in]  aName       A pointer to a NULL-terminated C string printed
1198  *                          following @p aPrefix that indicates what
1199  *                          module, program, application or subsystem
1200  *                          the assertion occurred in Typically this
1201  *                          is and should be
1202  *                          #NL_ASSERT_COMPONENT_STRING.
1203  *  @param[in]  aCondition  A pointer to a NULL-terminated C string indicating
1204  *                          the expression that evaluated to false in
1205  *                          the assertion. Typically this is a
1206  *                          stringified version of the actual
1207  *                          assertion expression.
1208  *  @param[in]  aLabel      An optional pointer to a NULL-terminated C string
1209  *                          indicating, for exception-style
1210  *                          assertions, the label that will be
1211  *                          branched to when the assertion expression
1212  *                          evaluates to false.
1213  *  @param[in]  aFile       A pointer to a NULL-terminated C string indicating
1214  *                          the file in which the exception
1215  *                          occurred. Typically this is and should be
1216  *                          \_\_FILE\_\_ from the C preprocessor or
1217  *                          #NL_ASSERT_FILE.
1218  *  @param[in]  aLine       The line number in @p aFile on which the assertion
1219  *                          expression evaluated to false. Typically
1220  *                          this is and should be \_\_LINE\_\_ from the C
1221  *                          preprocessor.
1222  *  @param[in]  aMessage    An optional pointer to a NULL-terminated C string
1223  *                          containing a caller-specified message
1224  *                          further describing the assertion failure.
1225  *
1226  *  @sa #NL_ASSERT_LOG_DEFAULT
1227  *
1228  *  @sa #NL_ASSERT_FLAG_LOG
1229  *
1230  *  @sa #NL_ASSERT_PREFIX_STRING
1231  *  @sa #NL_ASSERT_COMPONENT_STRING
1232  *
1233  *  @sa #NL_ASSERT_FILE
1234  *
1235  */
1236 #if !defined(NL_ASSERT_LOG)
1237 #define NL_ASSERT_LOG(aPrefix, aName, aCondition, aLabel, aFile, aLine, aMessage)
1238 #endif /* !defined(NL_ASSERT_LOG) */
1239
1240 /**
1241  *  @def NL_ASSERT_FILE
1242  *
1243  *  @brief
1244  *    This is the NULL-terminated C string literal with the fully-,
1245  *    partially-, or non-qualified path of the file name in which an
1246  *    assertion occurred (default \_\_FILE\_\_).
1247  *
1248  *  This may be used by developers to override the C preprocessor default,
1249  *  potentially shortening the size of string literals used for
1250  *  assertion file names and, consequently, decreasing the size of a
1251  *  particular target image.
1252  *
1253  */
1254 #if !defined(NL_ASSERT_FILE)
1255 #define NL_ASSERT_FILE __FILE__
1256 #endif /* !defined(NL_ASSERT_FILE) */
1257
1258 /**
1259  *  @defgroup static-modality Static
1260  *  @defgroup assert-style Assert
1261  *  @defgroup abort-style Abort
1262  *  @defgroup check-style Check
1263  *  @defgroup verify-style Verify
1264  *  @defgroup precondition-style Precondition
1265  *  @defgroup expect-style Expect
1266  *  @defgroup desire-style Desire
1267  *  @defgroup require-style Require
1268  *
1269  */
1270
1271 /**
1272  *  @ingroup static-modality
1273  *
1274  *  @{
1275  *
1276  */
1277
1278 /**
1279  *  @def nlSTATIC_ASSERT(aCondition)
1280  *
1281  *  @brief
1282  *    This checks, at compile-time, for the specified condition, which
1283  *    is expected to commonly be true, and terminates compilation if
1284  *    the condition is false.
1285  *
1286  *  @note Unlike the runtime assert macros, this compile-time macro is active
1287  *        regardless of the state of #NL_ASSERT_PRODUCTION.
1288  *
1289  *  @param[in]  aCondition  A Boolean expression to be evaluated.
1290  *
1291  *  @sa #nlSTATIC_ASSERT_PRINT
1292  *
1293  */
1294 #define nlSTATIC_ASSERT(aCondition)                                             _nlSTATIC_ASSERT(aCondition, #aCondition)
1295
1296 /**
1297  *  @def nlSTATIC_ASSERT_PRINT(aCondition, aMessage)
1298  *
1299  *  @brief
1300  *    This checks, at compile-time, for the specified condition, which
1301  *    is expected to commonly be true, and terminates compilation if
1302  *    the condition is false.
1303  *
1304  *  @note Unlike the runtime assert macros, this compile-time macro is active
1305  *        regardless of the state of #NL_ASSERT_PRODUCTION.
1306  *
1307  *  @param[in]  aCondition  A Boolean expression to be evaluated.
1308  *  @param[in]  aMessage    A pointer to a NULL-terminated C string
1309  *                          containing a caller-specified message
1310  *                          further describing the assertion
1311  *                          failure. Note, this message is not
1312  *                          actually emitted in any meaningful way for
1313  *                          non-C11 or -C++11 code. It serves to
1314  *                          simply comment or annotate the assertion
1315  *                          and to provide interface parallelism with
1316  *                          the run-time assertion interfaces.
1317  *
1318  *  @sa #nlSTATIC_ASSERT
1319  *
1320  */
1321 #define nlSTATIC_ASSERT_PRINT(aCondition, aMessage)                             _nlSTATIC_ASSERT(aCondition, aMessage)
1322
1323 /**
1324  *  @}
1325  *
1326  */
1327
1328 /**
1329  *  @ingroup expect-style
1330  *
1331  *  @{
1332  *
1333  */
1334
1335 /**
1336  *  @def NL_ASSERT_EXPECT_FLAGS_DEFAULT
1337  *
1338  *  @brief
1339  *    This defines the default behavioral flags for expect-style
1340  *    exception family assertions.
1341  *
1342  *  This may be used to override #NL_ASSERT_EXPECT_FLAGS as follows:
1343  *
1344  *  @code
1345  *    #define NL_ASSERT_EXPECT_FLAGS NL_ASSERT_EXPECT_FLAGS_DEFAULT
1346  *
1347  *    #include <nlassert.h>
1348  *  @endcode
1349  *
1350  *  @sa #NL_ASSERT_EXPECT_FLAGS
1351  *
1352  *  @sa #NL_ASSERT_FLAG_BACKTRACE
1353  *  @sa #NL_ASSERT_FLAG_LOG
1354  *  @sa #NL_ASSERT_FLAG_TRAP
1355  *
1356  */
1357 #define NL_ASSERT_EXPECT_FLAGS_DEFAULT                                          (NL_ASSERT_FLAG_NONE)
1358
1359 /**
1360  *  @def NL_ASSERT_EXPECT_FLAGS
1361  *
1362  *  @brief
1363  *    This defines the behavioral flags that govern the behavior for
1364  *    expect-style exception family assertions when the assertion
1365  *    expression evaluates to false.
1366  *
1367  *  @sa #NL_ASSERT_EXPECT_FLAGS_DEFAULT
1368  *
1369  *  @sa #NL_ASSERT_FLAG_BACKTRACE
1370  *  @sa #NL_ASSERT_FLAG_LOG
1371  *  @sa #NL_ASSERT_FLAG_TRAP
1372  */
1373 #if NL_ASSERT_USE_FLAGS_DEFAULT
1374 #define NL_ASSERT_EXPECT_FLAGS                                                  NL_ASSERT_EXPECT_FLAGS_DEFAULT
1375 #elif !defined(NL_ASSERT_EXPECT_FLAGS)
1376 #define NL_ASSERT_EXPECT_FLAGS                                                  (NL_ASSERT_FLAG_NONE)
1377 #endif
1378
1379 /**
1380  *  @def nlEXPECT(aCondition, aLabel)
1381  *
1382  *  @brief
1383  *    This checks for the specified condition, which is expected to
1384  *    commonly be true, and branches to @p aLabel if the condition is
1385  *    false.
1386  *
1387  *  __Anticipated Assertion Firing Frequency:__ Frequent
1388  *
1389  *  @param[in]  aCondition  A Boolean expression to be evaluated.
1390  *  @param[in]  aLabel      The local label to which execution branches
1391  *                          when @p aCondition evaluates to false (i.e.
1392  *                          compares equal to zero).
1393  *
1394  *  @sa #NL_ASSERT_EXPECT_FLAGS
1395  *
1396  *  @sa #nlDESIRE
1397  *  @sa #nlREQUIRE
1398  *
1399  */
1400 #define nlEXPECT(aCondition, aLabel)                                            __nlEXPECT(NL_ASSERT_EXPECT_FLAGS, aCondition, aLabel)
1401
1402 /**
1403  *  @def nlEXPECT_PRINT(aCondition, aLabel, aMessage)
1404  *
1405  *  @brief
1406  *    This checks for the specified condition, which is expected to
1407  *    commonly be true, and both prints @p aMessage and then branches
1408  *    to @p aLabel if the condition is false.
1409  *
1410  *  __Anticipated Assertion Firing Frequency:__ Frequent
1411  *
1412  *  @param[in]  aCondition  A Boolean expression to be evaluated.
1413  *  @param[in]  aLabel      The local label to which execution branches
1414  *                          when @p aCondition evaluates to false (i.e.
1415  *                          compares equal to zero).
1416  *  @param[in]  aMessage    A pointer to a NULL-terminated C string
1417  *                          containing a caller-specified message
1418  *                          further describing the assertion failure.
1419  *
1420  *  @sa #NL_ASSERT_EXPECT_FLAGS
1421  *
1422  *  @sa #nlDESIRE_PRINT
1423  *  @sa #nlREQUIRE_PRINT
1424  *
1425  */
1426 #define nlEXPECT_PRINT(aCondition, aLabel, aMessage)                            __nlEXPECT_PRINT(NL_ASSERT_EXPECT_FLAGS, aCondition, aLabel, aMessage)
1427
1428 /**
1429  *  @def nlEXPECT_ACTION(aCondition, aLabel, anAction)
1430  *
1431  *  @brief
1432  *    This checks for the specified condition, which is expected to
1433  *    commonly be true, and both executes @p anAction and branches to
1434  *    @p aLabel if the condition is false.
1435  *
1436  *  __Anticipated Assertion Firing Frequency:__ Frequent
1437  *
1438  *  @param[in]  aCondition  A Boolean expression to be evaluated.
1439  *  @param[in]  aLabel      The local label to which execution branches
1440  *                          when @p aCondition evaluates to false (i.e.
1441  *                          compares equal to zero).
1442  *  @param[in]  anAction    An expression or block to execute when the
1443  *                          assertion fails. This will be executed
1444  *                          after #NL_ASSERT_LOG() and
1445  *                          #NL_ASSERT_BACKTRACE() but before
1446  *                          #NL_ASSERT_TRAP().
1447  *
1448  *  @sa #NL_ASSERT_EXPECT_FLAGS
1449  *
1450  *  @sa #nlDESIRE_ACTION
1451  *  @sa #nlREQUIRE_ACTION
1452  *
1453  */
1454 #define nlEXPECT_ACTION(aCondition, aLabel, anAction)                           __nlEXPECT_ACTION(NL_ASSERT_EXPECT_FLAGS, aCondition, aLabel, anAction)
1455
1456 /**
1457  *  @def nlEXPECT_ACTION_PRINT(aCondition, aLabel, anAction, aMessage)
1458  *
1459  *  @brief
1460  *    This checks for the specified condition, which is expected to
1461  *    commonly be true, prints @p aMessage, executes @p anAction, and
1462  *    branches to @p aLabel if the condition is false.
1463  *
1464  *  __Anticipated Assertion Firing Frequency:__ Frequent
1465  *
1466  *  @param[in]  aCondition  A Boolean expression to be evaluated.
1467  *  @param[in]  aLabel      The local label to which execution branches
1468  *                          when @p aCondition evaluates to false (i.e.
1469  *                          compares equal to zero).
1470  *  @param[in]  anAction    An expression or block to execute when the
1471  *                          assertion fails. This will be executed
1472  *                          after #NL_ASSERT_LOG() and
1473  *                          #NL_ASSERT_BACKTRACE() but before
1474  *                          #NL_ASSERT_TRAP().
1475  *  @param[in]  aMessage    A pointer to a NULL-terminated C string
1476  *                          containing a caller-specified message
1477  *                          further describing the assertion failure.
1478  *
1479  *  @sa #NL_ASSERT_EXPECT_FLAGS
1480  *
1481  *  @sa #nlDESIRE_ACTION_PRINT
1482  *  @sa #nlREQUIRE_ACTION_PRINT
1483  *
1484  */
1485 #define nlEXPECT_ACTION_PRINT(aCondition, aLabel, anAction, aMessage)           __nlEXPECT_ACTION_PRINT(NL_ASSERT_EXPECT_FLAGS, aCondition, aLabel, anAction, aMessage)
1486
1487 /**
1488  *  @def nlEXPECT_SUCCESS(aStatus, aLabel)
1489  *
1490  *  @brief
1491  *    This checks for the specified status, which is expected to
1492  *    commonly be successful (i.e. zero (0)), and branches to @p
1493  *    aLabel if the status is unsuccessful.
1494  *
1495  *  __Anticipated Assertion Firing Frequency:__ Frequent
1496  *
1497  *  @param[in]  aStatus     A scalar status to be evaluated against zero (0).
1498  *  @param[in]  aLabel      The local label to which execution branches
1499  *                          when @p aStatus does not evaluate to zero
1500  *                          (0).
1501  *
1502  *  @sa #NL_ASSERT_EXPECT_FLAGS
1503  *
1504  *  @sa #nlDESIRE_SUCCESS
1505  *  @sa #nlREQUIRE_SUCCESS
1506  *
1507  */
1508 #define nlEXPECT_SUCCESS(aStatus, aLabel)                                       __nlEXPECT_SUCCESS(NL_ASSERT_EXPECT_FLAGS, aStatus, aLabel)
1509
1510 /**
1511  *  @def nlEXPECT_SUCCESS_PRINT(aStatus, aLabel, aMessage)
1512  *
1513  *  @brief
1514  *    This checks for the specified status, which is expected to
1515  *    commonly be successful (i.e. zero (0)), and both prints @p
1516  *    aMessage and then branches to @p aLabel if the status is
1517  *    unsuccessful.
1518  *
1519  *  __Anticipated Assertion Firing Frequency:__ Frequent
1520  *
1521  *  @param[in]  aStatus     A scalar status to be evaluated against zero (0).
1522  *  @param[in]  aLabel      The local label to which execution branches
1523  *                          when @p aStatus does not evaluate to zero
1524  *                          (0).
1525  *  @param[in]  aMessage    A pointer to a NULL-terminated C string
1526  *                          containing a caller-specified message
1527  *                          further describing the assertion failure.
1528  *
1529  *  @sa #NL_ASSERT_EXPECT_FLAGS
1530  *
1531  *  @sa #nlDESIRE_SUCCESS_PRINT
1532  *  @sa #nlREQUIRE_SUCCESS_PRINT
1533  *
1534  */
1535 #define nlEXPECT_SUCCESS_PRINT(aStatus, aLabel, aMessage)                       __nlEXPECT_SUCCESS_PRINT(NL_ASSERT_EXPECT_FLAGS, aStatus, aLabel, aMessage)
1536
1537 /**
1538  *  @def nlEXPECT_SUCCESS_ACTION(aStatus, aLabel, anAction)
1539  *
1540  *  @brief
1541  *    This checks for the specified status, which is expected to
1542  *    commonly be successful (i.e. zero (0)), and both executes @p
1543  *    anAction and branches to @p aLabel if the status is unsuccessful.
1544  *
1545  *  __Anticipated Assertion Firing Frequency:__ Frequent
1546  *
1547  *  @param[in]  aStatus     A scalar status to be evaluated against zero (0).
1548  *  @param[in]  aLabel      The local label to which execution branches
1549  *                          when @p aStatus does not evaluate to zero
1550  *                          (0).
1551  *  @param[in]  anAction    An expression or block to execute when the
1552  *                          assertion fails. This will be executed
1553  *                          after #NL_ASSERT_LOG() and
1554  *                          #NL_ASSERT_BACKTRACE() but before
1555  *                          #NL_ASSERT_TRAP().
1556  *
1557  *  @sa #NL_ASSERT_EXPECT_FLAGS
1558  *
1559  *  @sa #nlDESIRE_SUCCESS_ACTION
1560  *  @sa #nlREQUIRE_SUCCESS_ACTION
1561  *
1562  */
1563 #define nlEXPECT_SUCCESS_ACTION(aStatus, aLabel, anAction)                      __nlEXPECT_SUCCESS_ACTION(NL_ASSERT_EXPECT_FLAGS, aStatus, aLabel, anAction)
1564
1565 /**
1566  *  @def nlEXPECT_SUCCESS_ACTION_PRINT(aStatus, aLabel, anAction, aMessage)
1567  *
1568  *  @brief
1569  *    This checks for the specified status, which is expected to
1570  *    commonly be successful (i.e. zero (0)), prints @p aMessage,
1571  *    executes @p anAction, and branches to @p aLabel if the status is
1572  *    unsuccessful.
1573  *
1574  *  __Anticipated Assertion Firing Frequency:__ Frequent
1575  *
1576  *  @param[in]  aStatus     A scalar status to be evaluated against zero (0).
1577  *  @param[in]  aLabel      The local label to which execution branches
1578  *                          when @p aStatus does not evaluate to zero
1579  *                          (0).
1580  *  @param[in]  anAction    An expression or block to execute when the
1581  *                          assertion fails. This will be executed
1582  *                          after #NL_ASSERT_LOG() and
1583  *                          #NL_ASSERT_BACKTRACE() but before
1584  *                          #NL_ASSERT_TRAP().
1585  *  @param[in]  aMessage    A pointer to a NULL-terminated C string
1586  *                          containing a caller-specified message
1587  *                          further describing the assertion failure.
1588  *
1589  *  @sa #NL_ASSERT_EXPECT_FLAGS
1590  *
1591  *  @sa #nlDESIRE_SUCCESS_ACTION_PRINT
1592  *  @sa #nlREQUIRE_SUCCESS_ACTION_PRINT
1593  *
1594  */
1595 #define nlEXPECT_SUCCESS_ACTION_PRINT(aStatus, aLabel, anAction, aMessage)      __nlEXPECT_SUCCESS_ACTION_PRINT(NL_ASSERT_EXPECT_FLAGS, aStatus, aLabel, anAction, aMessage)
1596
1597 /**
1598  *  @def nlNEXPECT(aCondition, aLabel)
1599  *
1600  *  @brief
1601  *    This checks for the specified condition, which is expected to
1602  *    commonly be false, and branches to @p aLabel if the condition is
1603  *    true.
1604  *
1605  *  @note This is the logical inverse of #nlEXPECT.
1606  *
1607  *  __Anticipated Assertion Firing Frequency:__ Frequent
1608  *
1609  *  @param[in]  aCondition  A Boolean expression to be evaluated.
1610  *  @param[in]  aLabel      The local label to which execution branches
1611  *                          when @p aCondition evaluates to false (i.e.
1612  *                          compares equal to zero).
1613  *
1614  *  @sa #NL_ASSERT_EXPECT_FLAGS
1615  *
1616  *  @sa #nlEXPECT
1617  *  @sa #nlNDESIRE
1618  *  @sa #nlNREQUIRE
1619  *
1620  */
1621 #define nlNEXPECT(aCondition, aLabel)                                           __nlNEXPECT(NL_ASSERT_EXPECT_FLAGS, aCondition, aLabel)
1622
1623 /**
1624  *  @def nlNEXPECT_PRINT(aCondition, aLabel, aMessage)
1625  *
1626  *  @brief
1627  *    This checks for the specified condition, which is expected to
1628  *    commonly be false, and both prints @p aMessage and then branches
1629  *    to @p aLabel if the condition is true.
1630  *
1631  *  @note This is the logical inverse of #nlEXPECT_PRINT.
1632  *
1633  *  __Anticipated Assertion Firing Frequency:__ Frequent
1634  *
1635  *  @param[in]  aCondition  A Boolean expression to be evaluated.
1636  *  @param[in]  aLabel      The local label to which execution branches
1637  *                          when @p aCondition evaluates to false (i.e.
1638  *                          compares equal to zero).
1639  *  @param[in]  aMessage    A pointer to a NULL-terminated C string
1640  *                          containing a caller-specified message
1641  *                          further describing the assertion failure.
1642  *
1643  *  @sa #NL_ASSERT_EXPECT_FLAGS
1644  *
1645  *  @sa #nlEXPECT_PRINT
1646  *  @sa #nlNDESIRE_PRINT
1647  *  @sa #nlNREQUIRE_PRINT
1648  *
1649  */
1650 #define nlNEXPECT_PRINT(aCondition, aLabel, aMessage)                           __nlNEXPECT_PRINT(NL_ASSERT_EXPECT_FLAGS, aCondition, aLabel, aMessage)
1651
1652 /**
1653  *  @def nlNEXPECT_ACTION(aCondition, aLabel, anAction)
1654  *
1655  *  @brief
1656  *    This checks for the specified condition, which is expected to
1657  *    commonly be false, and both executes @p anAction and branches to
1658  *    @p aLabel if the condition is true.
1659  *
1660  *  @note This is the logical inverse of #nlEXPECT_ACTION.
1661  *
1662  *  __Anticipated Assertion Firing Frequency:__ Frequent
1663  *
1664  *  @param[in]  aCondition  A Boolean expression to be evaluated.
1665  *  @param[in]  aLabel      The local label to which execution branches
1666  *                          when @p aCondition evaluates to false (i.e.
1667  *                          compares equal to zero).
1668  *  @param[in]  anAction    An expression or block to execute when the
1669  *                          assertion fails. This will be executed
1670  *                          after #NL_ASSERT_LOG() and
1671  *                          #NL_ASSERT_BACKTRACE() but before
1672  *                          #NL_ASSERT_TRAP().
1673  *
1674  *  @sa #NL_ASSERT_EXPECT_FLAGS
1675  *
1676  *  @sa #nlEXPECT_ACTION
1677  *  @sa #nlNDESIRE_ACTION
1678  *  @sa #nlNREQUIRE_ACTION
1679  *
1680  */
1681 #define nlNEXPECT_ACTION(aCondition, aLabel, anAction)                          __nlNEXPECT_ACTION(NL_ASSERT_EXPECT_FLAGS, aCondition, aLabel, anAction)
1682
1683 /**
1684  *  @def nlNEXPECT_ACTION_PRINT(aCondition, aLabel, anAction, aMessage)
1685  *
1686  *  @brief
1687  *    This checks for the specified condition, which is expected to
1688  *    commonly be false, prints @p aMessage, executes @p anAction, and
1689  *    branches to @p aLabel if the condition is true.
1690  *
1691  *  @note This is the logical inverse of #nlEXPECT_ACTION_PRINT.
1692  *
1693  *  __Anticipated Assertion Firing Frequency:__ Frequent
1694  *
1695  *  @param[in]  aCondition  A Boolean expression to be evaluated.
1696  *  @param[in]  aLabel      The local label to which execution branches
1697  *                          when @p aCondition evaluates to false (i.e.
1698  *                          compares equal to zero).
1699  *  @param[in]  anAction    An expression or block to execute when the
1700  *                          assertion fails. This will be executed
1701  *                          after #NL_ASSERT_LOG() and
1702  *                          #NL_ASSERT_BACKTRACE() but before
1703  *                          #NL_ASSERT_TRAP().
1704  *  @param[in]  aMessage    A pointer to a NULL-terminated C string
1705  *                          containing a caller-specified message
1706  *                          further describing the assertion failure.
1707  *
1708  *  @sa #NL_ASSERT_EXPECT_FLAGS
1709  *
1710  *  @sa #nlEXPECT_ACTION_PRINT
1711  *  @sa #nlNDESIRE_ACTION_PRINT
1712  *  @sa #nlNREQUIRE_ACTION_PRINT
1713  *
1714  */
1715 #define nlNEXPECT_ACTION_PRINT(aCondition, aLabel, anAction, aMessage)          __nlNEXPECT_ACTION_PRINT(NL_ASSERT_EXPECT_FLAGS, aCondition, aLabel, anAction, aMessage)
1716
1717 /**
1718  *  @}
1719  *
1720  */
1721
1722 #if !NL_ASSERT_PRODUCTION
1723 #include "nlassert-nonproduction.h"
1724 #else
1725 #include "nlassert-production.h"
1726 #endif /* !NL_ASSERT_PRODUCTION */
1727
1728 #endif /* NLCORE_NLASSERT_H */