|
| 1 | +//===--- Assertions.h - Assertion macros ----===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// This file provides three alternatives to the C/C++ standard `assert()` macro |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +#ifndef SWIFT_BASIC_ASSERTIONS_H |
| 18 | +#define SWIFT_BASIC_ASSERTIONS_H |
| 19 | + |
| 20 | +#include <stdint.h> |
| 21 | + |
| 22 | +// Only for use in this header |
| 23 | +#if __has_builtin(__builtin_expect) |
| 24 | +#define ASSERT_UNLIKELY(expression) (__builtin_expect(!!(expression), 0)) |
| 25 | +#else |
| 26 | +#define ASSERT_UNLIKELY(expression) ((expression)) |
| 27 | +#endif |
| 28 | + |
| 29 | +// ================================ Mandatory Asserts ================================ |
| 30 | + |
| 31 | +// `ASSERT(expr)`: |
| 32 | +// * is always compiled into the executable and |
| 33 | +// * always checks the condition, regardless of build or runtime settings. |
| 34 | +// This should be used for most assertions, which is why it |
| 35 | +// deserves the short name. In particular, for simple checks |
| 36 | +// (e.g., validating that something is non-null), this is just as |
| 37 | +// fast as a disabled `CONDITIONAL_ASSERT`, so there's no point in |
| 38 | +// using the conditional form. |
| 39 | +// |
| 40 | +// You can globally replace `assert` with `ASSERT` in a piece of code |
| 41 | +// to have all your assertions enabled in all builds. If you do this, |
| 42 | +// please do a little profiling first, just in case you have some checks |
| 43 | +// that are more expensive than you think. You can switch those to |
| 44 | +// `CONDITIONAL_ASSERT` or `DEBUG_ASSERT` as needed. |
| 45 | + |
| 46 | +#define ASSERT(expr) \ |
| 47 | + do { \ |
| 48 | + if (ASSERT_UNLIKELY(!expr)) { \ |
| 49 | + ASSERT_failure(#expr, __FILE__, __LINE__, __func__); \ |
| 50 | + } \ |
| 51 | + } while (0) |
| 52 | + |
| 53 | +// Function that reports the actual failure when it occurs. |
| 54 | +void ASSERT_failure(const char *expr, const char *file, int line, const char *func); |
| 55 | + |
| 56 | +// ================================ Conditional Asserts ================================ |
| 57 | + |
| 58 | +// `CONDITIONAL_ASSERT(expr)`: |
| 59 | +// * is always compiled into the executable, but |
| 60 | +// * only checks the condition if a runtime flag is defined. |
| 61 | +// That runtime flag is disabled by default in release builds |
| 62 | +// but can be enabled with the command-line flag `-compiler-assertions` |
| 63 | +// |
| 64 | +// Use this for asserts that are comparatively expensive to check. |
| 65 | +// |
| 66 | +// You can globally change `assert` to `CONDITIONAL_ASSERT` to make all your |
| 67 | +// assertions _optionally_ available in release builds. Anyone can then add |
| 68 | +// `-compiler-assertions` to their build flags to get more information about a |
| 69 | +// compiler problem. Before you check it in, do a little checking for |
| 70 | +// assertions that might be checked huge numbers of times (e.g., invariants |
| 71 | +// for inner loops or core utilities); those may need to become `DEBUG_ASSERT` |
| 72 | +// or else refactored to be checked more selectively. |
| 73 | +// |
| 74 | +// Over time, plan to change most of the resulting `CONDITIONAL_ASSERT` into |
| 75 | +// plain `ASSERT` to enable them by default. |
| 76 | + |
| 77 | +#define CONDITIONAL_ASSERT(expr) \ |
| 78 | + do { \ |
| 79 | + if (ASSERT_UNLIKELY(CONDITIONAL_ASSERT_enabled())) { \ |
| 80 | + ASSERT(expr); \ |
| 81 | + } \ |
| 82 | + } while (0) |
| 83 | + |
| 84 | +// Use `CONDITIONAL_ASSERT_enabled()` to guard complex, expensive code that |
| 85 | +// should only run when assertions are enabled. This is exactly the |
| 86 | +// same check that's used to enable `CONDITIONAL_ASSERT()` at runtime. |
| 87 | +// This is not often used -- if you are just setting a flag or updating |
| 88 | +// a counter, it's likely cheaper just to do it than to test whether or not |
| 89 | +// to do it. Only use this for relatively complex operations. |
| 90 | +// |
| 91 | +// if (CONDITIONAL_ASSERT_enabled()) { |
| 92 | +// ... stuff ... |
| 93 | +// } |
| 94 | + |
| 95 | +// Declare a callable function version of this runtime test. |
| 96 | +int CONDITIONAL_ASSERT_enabled(); |
| 97 | + |
| 98 | +// Define a macro version of this test |
| 99 | +extern int CONDITIONAL_ASSERT_Global_enable_flag; |
| 100 | +#define CONDITIONAL_ASSERT_enabled() \ |
| 101 | + (CONDITIONAL_ASSERT_Global_enable_flag != 0) |
| 102 | + |
| 103 | +// Profiling note: If you uncomment the line below to #undef the macro, then |
| 104 | +// we'll always call the function, which lets you profile assertions by |
| 105 | +// counting calls to this function. |
| 106 | + |
| 107 | +// #undef CONDITIONAL_ASSERT_enabled |
| 108 | + |
| 109 | +// ================================ Debug Asserts ================================ |
| 110 | + |
| 111 | +// `DEBUG_ASSERT(expr)`: |
| 112 | +// * is only compiled into the executable in debug or "asserts enabled" builds, and |
| 113 | +// * always performs the check (whenever it is compiled in). |
| 114 | +// |
| 115 | +// This basically makes it a synonym for the Standard C `assert(expr)`. |
| 116 | +// |
| 117 | +// You should mostly avoid this except for occasional experiments in your |
| 118 | +// local tree. It can be useful in two situations: |
| 119 | +// |
| 120 | +// * Assertions that are known to mis-fire. |
| 121 | +// Such assertions should not be `ASSERT` (since that will cause unnecessary |
| 122 | +// broken compiles) and `CONDITIONAL_ASSERT` gets enabled a lot by people who |
| 123 | +// are not compiler experts. So `DEBUG_ASSERT` is appropriate there until the |
| 124 | +// check can be fixed so it doesn't mis-fire. |
| 125 | +// |
| 126 | +// * Inner loops that can run billions of times. |
| 127 | +// For these, even the cost of testing whether `CONDITIONAL_ASSERT` is enabled |
| 128 | +// can be prohibitive. Use `DEBUG_ASSERT`, but also look for ways to refactor |
| 129 | +// so you can verify correctness without having an assertion in an inner loop. |
| 130 | +// |
| 131 | +// P.S. Please do not bulk replace `assert` with `DEBUG_ASSERT`. The whole |
| 132 | +// point of this package is to move us toward having assertions always compiled |
| 133 | +// in and always enabled. |
| 134 | +#ifdef NDEBUG |
| 135 | + #define DEBUG_ASSERT(expr) do { } while (0) |
| 136 | + #undef DEBUG_ASSERT_enabled |
| 137 | +#else |
| 138 | + #define DEBUG_ASSERT(expr) ASSERT(expr) |
| 139 | + #define DEBUG_ASSERT_enabled 1 |
| 140 | +#endif |
| 141 | + |
| 142 | +// Code that's only needed within `DEBUG_ASSERT` can be guarded as follows: |
| 143 | +// |
| 144 | +// #ifndef NDEBUG |
| 145 | +// ... code that's only needed for DEBUG_ASSERT ... |
| 146 | +// #endif |
| 147 | +// |
| 148 | +// or with the equivalent |
| 149 | +// |
| 150 | +// #ifdef DEBUG_ASSERT_enabled |
| 151 | +// ... code that's only needed for DEBUG_ASSERT ... |
| 152 | +// #endif |
| 153 | +// |
| 154 | +// For example, you may need this for variables or functions that |
| 155 | +// are only used within DEBUG_ASSERT statements. |
| 156 | + |
| 157 | +// A common case is to declare a variable or perform a simple |
| 158 | +// expression. These can be used to avoid some boilerplate: |
| 159 | +// |
| 160 | +// void doThings() { |
| 161 | +// DEBUG_ASSERT_DECL(std::vector<Things> thingsToVerify;); |
| 162 | +// while (!done) { |
| 163 | +// // ... do each thing ... |
| 164 | +// DEBUG_ASSERT_EXPR(thingsToVerify.append(item)); |
| 165 | +// } |
| 166 | +// DEBUG_ASSERT(verifyAllThe(thingsToVerify)); |
| 167 | +// } |
| 168 | + |
| 169 | +#ifdef DEBUG_ASSERT_enabled |
| 170 | + #define DEBUG_ASSERT_DECL(...) __VA_ARGS__ |
| 171 | + #define DEBUG_ASSERT_EXPR(...) do { __VA_ARGS__; } while (false) |
| 172 | +#else |
| 173 | + #define DEBUG_ASSERT_DECL(...) |
| 174 | + #define DEBUG_ASSERT_EXPR(...) do { } while (false) |
| 175 | +#endif |
| 176 | + |
| 177 | +// Older version of the same idea: |
| 178 | +#define SWIFT_ASSERT_ONLY_DECL DEBUG_ASSERT_DECL |
| 179 | +#define SWIFT_ASSERT_ONLY DEBUG_ASSERT_EXPR |
| 180 | + |
| 181 | +// ================================ Utility and Helper Functions ================================ |
| 182 | + |
| 183 | +// Utility function to print out help information for |
| 184 | +// various command-line options that affect the assertion |
| 185 | +// behavior. |
| 186 | +void ASSERT_help(); |
| 187 | + |
| 188 | +#endif // SWIFT_BASIC_ASSERTIONS_H |
0 commit comments