1 /* CC0 (Public domain) - see LICENSE file for details */
2 #ifndef CCAN_COMPILER_H
3 #define CCAN_COMPILER_H
7 #if HAVE_ATTRIBUTE_COLD
9 * COLD - a function is unlikely to be called.
11 * Used to mark an unlikely code path and optimize appropriately.
12 * It is usually used on logging or error routines.
15 * static void COLD moan(const char *reason)
17 * fprintf(stderr, "Error: %s (%s)\n", reason, strerror(errno));
20 #define COLD __attribute__((cold))
27 #if HAVE_ATTRIBUTE_NORETURN
29 * NORETURN - a function does not return
31 * Used to mark a function which exits; useful for suppressing warnings.
34 * static void NORETURN fail(const char *reason)
36 * fprintf(stderr, "Error: %s (%s)\n", reason, strerror(errno));
40 #define NORETURN __attribute__((noreturn))
47 #if HAVE_ATTRIBUTE_PRINTF
49 * PRINTF_FMT - a function takes printf-style arguments
50 * @nfmt: the 1-based number of the function's format argument.
51 * @narg: the 1-based number of the function's first variable argument.
53 * This allows the compiler to check your parameters as it does for printf().
56 * void PRINTF_FMT(2,3) my_printf(const char *prefix, const char *fmt, ...);
58 #define PRINTF_FMT(nfmt, narg) \
59 __attribute__((format(__printf__, nfmt, narg)))
61 #define PRINTF_FMT(nfmt, narg)
65 #ifndef CONST_FUNCTION
66 #if HAVE_ATTRIBUTE_CONST
68 * CONST_FUNCTION - a function's return depends only on its argument
70 * This allows the compiler to assume that the function will return the exact
71 * same value for the exact same arguments. This implies that the function
72 * must not use global variables, or dereference pointer arguments.
74 #define CONST_FUNCTION __attribute__((const))
76 #define CONST_FUNCTION
80 #if HAVE_ATTRIBUTE_UNUSED
83 * UNNEEDED - a variable/function may not be needed
85 * This suppresses warnings about unused variables or functions, but tells
86 * the compiler that if it is unused it need not emit it into the source code.
89 * // With some preprocessor options, this is unnecessary.
90 * static UNNEEDED int counter;
92 * // With some preprocessor options, this is unnecessary.
93 * static UNNEEDED void add_to_counter(int add)
98 #define UNNEEDED __attribute__((unused))
102 #if HAVE_ATTRIBUTE_USED
104 * NEEDED - a variable/function is needed
106 * This suppresses warnings about unused variables or functions, but tells
107 * the compiler that it must exist even if it (seems) unused.
110 * // Even if this is unused, these are vital for debugging.
111 * static NEEDED int counter;
112 * static NEEDED void dump_counter(void)
114 * printf("Counter is %i\n", counter);
117 #define NEEDED __attribute__((used))
119 /* Before used, unused functions and vars were always emitted. */
120 #define NEEDED __attribute__((unused))
126 * UNUSED - a parameter is unused
128 * Some compilers (eg. gcc with -W or -Wunused) warn about unused
129 * function parameters. This suppresses such warnings and indicates
130 * to the reader that it's deliberate.
133 * // This is used as a callback, so needs to have this prototype.
134 * static int some_callback(void *unused UNUSED)
139 #define UNUSED __attribute__((unused))
153 #ifndef IS_COMPILE_CONSTANT
154 #if HAVE_BUILTIN_CONSTANT_P
156 * IS_COMPILE_CONSTANT - does the compiler know the value of this expression?
157 * @expr: the expression to evaluate
159 * When an expression manipulation is complicated, it is usually better to
160 * implement it in a function. However, if the expression being manipulated is
161 * known at compile time, it is better to have the compiler see the entire
162 * expression so it can simply substitute the result.
164 * This can be done using the IS_COMPILE_CONSTANT() macro.
167 * enum greek { ALPHA, BETA, GAMMA, DELTA, EPSILON };
169 * // Out-of-line version.
170 * const char *greek_name(enum greek greek);
173 * static inline const char *_greek_name(enum greek greek)
176 * case ALPHA: return "alpha";
177 * case BETA: return "beta";
178 * case GAMMA: return "gamma";
179 * case DELTA: return "delta";
180 * case EPSILON: return "epsilon";
181 * default: return "**INVALID**";
185 * // Use inline if compiler knows answer. Otherwise call function
186 * // to avoid copies of the same code everywhere.
187 * #define greek_name(g) \
188 * (IS_COMPILE_CONSTANT(greek) ? _greek_name(g) : greek_name(g))
190 #define IS_COMPILE_CONSTANT(expr) __builtin_constant_p(expr)
192 /* If we don't know, assume it's not. */
193 #define IS_COMPILE_CONSTANT(expr) 0
197 #ifndef WARN_UNUSED_RESULT
198 #if HAVE_WARN_UNUSED_RESULT
200 * WARN_UNUSED_RESULT - warn if a function return value is unused.
202 * Used to mark a function where it is extremely unlikely that the caller
203 * can ignore the result, eg realloc().
206 * // buf param may be freed by this; need return value!
207 * static char *WARN_UNUSED_RESULT enlarge(char *buf, unsigned *size)
209 * return realloc(buf, (*size) *= 2);
212 #define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
214 #define WARN_UNUSED_RESULT
217 #endif /* CCAN_COMPILER_H */