1 #ifndef CCAN_COMPILER_H
2 #define CCAN_COMPILER_H
5 #if HAVE_ATTRIBUTE_COLD
7 * COLD - a function is unlikely to be called.
9 * Used to mark an unlikely code path and optimize appropriately.
10 * It is usually used on logging or error routines.
13 * static void COLD moan(const char *reason)
15 * fprintf(stderr, "Error: %s (%s)\n", reason, strerror(errno));
18 #define COLD __attribute__((cold))
23 #if HAVE_ATTRIBUTE_PRINTF
25 * PRINTF_FMT - a function takes printf-style arguments
26 * @nfmt: the 1-based number of the function's format argument.
27 * @narg: the 1-based number of the function's first variable argument.
29 * This allows the compiler to check your parameters as it does for printf().
32 * void PRINTF_FMT(2,3) my_printf(const char *prefix, const char *fmt, ...);
34 #define PRINTF_FMT(nfmt, narg) \
35 __attribute__((format(__printf__, nfmt, narg)))
37 #define PRINTF_FMT(nfmt, narg)
40 #if HAVE_ATTRIBUTE_CONST
42 * IDEMPOTENT - a function's return depends only on its argument
44 * This allows the compiler to assume that the function will return the exact
45 * same value for the exact same arguments. This implies that the function
46 * must not use global variables, or dereference pointer arguments.
48 #define IDEMPOTENT __attribute__((const))
53 #if HAVE_ATTRIBUTE_UNUSED
55 * UNNEEDED - a variable/function may not be needed
57 * This suppresses warnings about unused variables or functions, but tells
58 * the compiler that if it is unused it need not emit it into the source code.
61 * // With some preprocessor options, this is unnecessary.
62 * static UNNEEDED int counter;
64 * // With some preprocessor options, this is unnecessary.
65 * static UNNEEDED void add_to_counter(int add)
70 #define UNNEEDED __attribute__((unused))
72 #if HAVE_ATTRIBUTE_USED
74 * NEEDED - a variable/function is needed
76 * This suppresses warnings about unused variables or functions, but tells
77 * the compiler that it must exist even if it (seems) unused.
80 * // Even if this is unused, these are vital for debugging.
81 * static NEEDED int counter;
82 * static NEEDED void dump_counter(void)
84 * printf("Counter is %i\n", counter);
87 #define NEEDED __attribute__((used))
89 /* Before used, unused functions and vars were always emitted. */
90 #define NEEDED __attribute__((unused))
94 * UNUSED - a parameter is unused
96 * Some compilers (eg. gcc with -W or -Wunused) warn about unused
97 * function parameters. This suppresses such warnings and indicates
98 * to the reader that it's deliberate.
101 * // This is used as a callback, so needs to have this prototype.
102 * static int some_callback(void *unused UNUSED)
107 #define UNUSED __attribute__((unused))
114 #if HAVE_BUILTIN_CONSTANT_P
116 * IS_COMPILE_CONSTANT - does the compiler know the value of this expression?
117 * @expr: the expression to evaluate
119 * When an expression manipulation is complicated, it is usually better to
120 * implement it in a function. However, if the expression being manipulated is
121 * known at compile time, it is better to have the compiler see the entire
122 * expression so it can simply substitute the result.
124 * This can be done using the IS_COMPILE_CONSTANT() macro.
127 * enum greek { ALPHA, BETA, GAMMA, DELTA, EPSILON };
129 * // Out-of-line version.
130 * const char *greek_name(enum greek greek);
133 * static inline char *_greek_name(enum greek greek)
136 * case ALPHA: return "alpha";
137 * case BETA: return "beta";
138 * case GAMMA: return "gamma";
139 * case DELTA: return "delta";
140 * case EPSILON: return "epsilon";
141 * default: return "**INVALID**";
145 * // Use inline if compiler knows answer. Otherwise call function
146 * // to avoid copies of the same code everywhere.
147 * #define greek_name(g) \
148 * (IS_COMPILE_CONSTANT(greek) ? _greek_name(g) : greek_name(g))
150 #define IS_COMPILE_CONSTANT(expr) __builtin_constant_p(expr)
152 /* If we don't know, assume it's not. */
153 #define IS_COMPILE_CONSTANT(expr) 0
156 #if HAVE_WARN_UNUSED_RESULT
158 * WARN_UNUSED_RESULT - warn if a function return value is unused.
160 * Used to mark a function where it is extremely unlikely that the caller
161 * can ignore the result, eg realloc().
164 * // buf param may be freed by this; need return value!
165 * static char *WARN_UNUSED_RESULT enlarge(char *buf, unsigned *size)
167 * return realloc(buf, (*size) *= 2);
170 #define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
172 #define WARN_UNUSED_RESULT
174 #endif /* CCAN_COMPILER_H */