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 * 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(char *prefix, char *format, ...);
34 #define PRINTF_ATTRIBUTE(nfmt, narg) \
35 __attribute__((format(__printf__, nfmt, narg)))
37 #define PRINTF_ATTRIBUTE(nfmt, narg)
40 #if HAVE_ATTRIBUTE_CONST
42 * IDEMPOTENT_ATTRIBUTE - 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 __attribute__((const))
50 #define IDEMPOTENT_ATTRIBUTE
53 #if HAVE_ATTRIBUTE_UNUSED
55 * UNNEEDED_ATTRIBUTE - a parameter/variable/function may not be needed
57 * This suppresses warnings about unused variables or parameters, but tells
58 * the compiler that if it is unused it need not emit it into the source code.
61 * // With some config options, this is unnecessary.
62 * static UNNEEDED_ATTRIBUTE int counter;
68 * // With some config options, this is unnecessary.
69 * static UNNEEDED_ATTRIBUTE int add_to_counter(int add)
74 #define UNNEEDED_ATTRIBUTE __attribute__((unused))
76 #if HAVE_ATTRIBUTE_USED
78 * NEEDED_ATTRIBUTE - a parameter/variable/function is needed
80 * This suppresses warnings about unused variables or parameters, but tells
81 * the compiler that it must exist even if it (seems) unused.
84 * // Even if this is unused, these are vital for debugging.
85 * static UNNEEDED_ATTRIBUTE int counter;
86 * static UNNEEDED_ATTRIBUTE void dump_counter(void)
88 * printf("Counter is %i\n", counter);
91 #define NEEDED_ATTRIBUTE __attribute__((used))
93 /* Before used, unused functions and vars were always emitted. */
94 #define NEEDED_ATTRIBUTE __attribute__((unused))
97 #define UNNEEDED_ATTRIBUTE
98 #define NEEDED_ATTRIBUTE
101 #if HAVE_BUILTIN_CONSTANT_P
103 * IS_COMPILE_CONSTANT - does the compiler know the value of this expression?
104 * @expr: the expression to evaluate
106 * When an expression manipulation is complicated, it is usually better to
107 * implement it in a function. However, if the expression being manipulated is
108 * known at compile time, it is better to have the compiler see the entire
109 * expression so it can simply substitute the result.
111 * This can be done using the IS_COMPILE_CONSTANT() macro.
114 * enum greek { ALPHA, BETA, GAMMA, DELTA, EPSILON };
116 * // Out-of-line version.
117 * const char *greek_name(enum greek greek);
120 * static inline _greek_name(enum greek greek)
123 * case ALPHA: return "alpha";
124 * case BETA: return "beta";
125 * case GAMMA: return "gamma";
126 * case DELTA: return "delta";
127 * case EPSILON: return "epsilon";
128 * default: return "**INVALID**";
132 * // Use inline if compiler knows answer. Otherwise call function
133 * // to avoid copies of the same code everywhere.
134 * #define greek_name(g) \
135 * (IS_COMPILE_CONSTANT(greek) ? _greek_name(g) : greek_name(g))
137 #define IS_COMPILE_CONSTANT(expr) __builtin_constant_p(expr)
139 /* If we don't know, assume it's not. */
140 #define IS_COMPILE_CONSTANT(expr) 0
142 #endif /* CCAN_COMPILER_H */