1 #ifndef CCAN_COMPILER_H
2 #define CCAN_COMPILER_H
5 #if HAVE_ATTRIBUTE_COLD
7 * COLD_ATTRIBUTE - 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_ATTRIBUTE moan(const char *reason)
15 * fprintf(stderr, "Error: %s (%s)\n", reason, strerror(errno));
18 #define COLD_ATTRIBUTE __attribute__((cold))
20 #define COLD_ATTRIBUTE
23 #if HAVE_ATTRIBUTE_PRINTF
25 * PRINTF_ATTRIBUTE - 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_ATTRIBUTE(2,3) my_printf(const char *prefix,
33 * const char *fmt, ...);
35 #define PRINTF_ATTRIBUTE(nfmt, narg) \
36 __attribute__((format(__printf__, nfmt, narg)))
38 #define PRINTF_ATTRIBUTE(nfmt, narg)
41 #if HAVE_ATTRIBUTE_CONST
43 * IDEMPOTENT_ATTRIBUTE - a function's return depends only on its argument
45 * This allows the compiler to assume that the function will return the exact
46 * same value for the exact same arguments. This implies that the function
47 * must not use global variables, or dereference pointer arguments.
49 #define IDEMPOTENT_ATTRIBUTE __attribute__((const))
51 #define IDEMPOTENT_ATTRIBUTE
54 #if HAVE_ATTRIBUTE_UNUSED
56 * UNNEEDED_ATTRIBUTE - a parameter/variable/function may not be needed
58 * This suppresses warnings about unused variables or parameters, but tells
59 * the compiler that if it is unused it need not emit it into the source code.
62 * // With some preprocessor options, this is unnecessary.
63 * static UNNEEDED_ATTRIBUTE int counter;
65 * // With some preprocessor options, this is unnecessary.
66 * static UNNEEDED_ATTRIBUTE void add_to_counter(int add)
71 #define UNNEEDED_ATTRIBUTE __attribute__((unused))
73 #if HAVE_ATTRIBUTE_USED
75 * NEEDED_ATTRIBUTE - a parameter/variable/function is needed
77 * This suppresses warnings about unused variables or parameters, but tells
78 * the compiler that it must exist even if it (seems) unused.
81 * // Even if this is unused, these are vital for debugging.
82 * static UNNEEDED_ATTRIBUTE int counter;
83 * static UNNEEDED_ATTRIBUTE void dump_counter(void)
85 * printf("Counter is %i\n", counter);
88 #define NEEDED_ATTRIBUTE __attribute__((used))
90 /* Before used, unused functions and vars were always emitted. */
91 #define NEEDED_ATTRIBUTE __attribute__((unused))
94 #define UNNEEDED_ATTRIBUTE
95 #define NEEDED_ATTRIBUTE
98 #if HAVE_BUILTIN_CONSTANT_P
100 * IS_COMPILE_CONSTANT - does the compiler know the value of this expression?
101 * @expr: the expression to evaluate
103 * When an expression manipulation is complicated, it is usually better to
104 * implement it in a function. However, if the expression being manipulated is
105 * known at compile time, it is better to have the compiler see the entire
106 * expression so it can simply substitute the result.
108 * This can be done using the IS_COMPILE_CONSTANT() macro.
111 * enum greek { ALPHA, BETA, GAMMA, DELTA, EPSILON };
113 * // Out-of-line version.
114 * const char *greek_name(enum greek greek);
117 * static inline char *_greek_name(enum greek greek)
120 * case ALPHA: return "alpha";
121 * case BETA: return "beta";
122 * case GAMMA: return "gamma";
123 * case DELTA: return "delta";
124 * case EPSILON: return "epsilon";
125 * default: return "**INVALID**";
129 * // Use inline if compiler knows answer. Otherwise call function
130 * // to avoid copies of the same code everywhere.
131 * #define greek_name(g) \
132 * (IS_COMPILE_CONSTANT(greek) ? _greek_name(g) : greek_name(g))
134 #define IS_COMPILE_CONSTANT(expr) __builtin_constant_p(expr)
136 /* If we don't know, assume it's not. */
137 #define IS_COMPILE_CONSTANT(expr) 0
140 #if HAVE_WARN_UNUSED_RESULT
142 * WARN_UNUSED_RESULT - warn if a function return value is unused.
144 * Used to mark a function where it is extremely unlikely that the caller
145 * can ignore the result, eg realloc().
148 * // buf param may be freed by this; need return value!
149 * static char *WARN_UNUSED_RESULT enlarge(char *buf, unsigned *size)
151 * return realloc(buf, (*size) *= 2);
154 #define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
156 #define WARN_UNUSED_RESULT
158 #endif /* CCAN_COMPILER_H */