]> git.ozlabs.org Git - ccan/blob - ccan/compiler/compiler.h
3601749003894974267a59e515db9a1cce6c8235
[ccan] / ccan / compiler / compiler.h
1 #ifndef CCAN_COMPILER_H
2 #define CCAN_COMPILER_H
3 #include "config.h"
4
5 #if HAVE_ATTRIBUTE_COLD
6 /**
7  * UNLIKELY_FUNCTION_ATTRIBUTE - a function is unlikely to be called.
8  *
9  * Used to mark an unlikely code path and optimize appropriately.
10  * It is usually used on logging or error routines.
11  *
12  * Example:
13  *      void UNLIKELY_FUNCTION_ATTRIBUTE moan(const char *reason)
14  *      {
15  *              fprintf(stderr, "Error: %s (%s)\n", reason, strerror(errno));
16  *      }
17  */
18 #define UNLIKELY_FUNCTION_ATTRIBUTE __attribute__((cold))
19 #else
20 #define UNLIKELY_FUNCTION_ATTRIBUTE
21 #endif
22
23 #if HAVE_ATTRIBUTE_PRINTF
24 /**
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.
28  *
29  * This allows the compiler to check your parameters as it does for printf().
30  *
31  * Example:
32  *      void PRINTF_ATTRIBUTE(2,3) my_printf(char *prefix, char *format, ...);
33  */
34 #define PRINTF_ATTRIBUTE(nfmt, narg) \
35         __attribute__((format(__printf__, nfmt, narg)))
36 #else
37 #define PRINTF_ATTRIBUTE(nfmt, narg)
38 #endif
39
40 #if HAVE_ATTRIBUTE_CONST
41 /**
42  * IDEMPOTENT_ATTRIBUTE - a function's return depends only on its argument
43  *
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.
47  */
48 #define IDEMPOTENT_ATTRIBUTE __attribute__((const))
49 #else
50 #define IDEMPOTENT_ATTRIBUTE
51 #endif
52
53 #if HAVE_ATTRIBUTE_UNUSED
54 /**
55  * UNNEEDED_ATTRIBUTE - a parameter/variable/function may not be needed
56  *
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.
59  *
60  * Example:
61  *      // With some config options, this is unnecessary.
62  *      static UNNEEDED_ATTRIBUTE int counter;
63  *      ...
64  *              #ifdef DEBUG
65  *              counter++;
66  *              #endif
67  *      ...
68  *      // With some config options, this is unnecessary.
69  *      static UNNEEDED_ATTRIBUTE int add_to_counter(int add)
70  *      {
71  *              counter += add;
72  *      }
73  */
74 #define UNNEEDED_ATTRIBUTE __attribute__((unused))
75
76 #if HAVE_ATTRIBUTE_USED
77 /**
78  * NEEDED_ATTRIBUTE - a parameter/variable/function is needed
79  *
80  * This suppresses warnings about unused variables or parameters, but tells
81  * the compiler that it must exist even if it (seems) unused.
82  *
83  * Example:
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)
87  *      {
88  *              printf("Counter is %i\n", counter);
89  *      }
90  */
91 #define NEEDED_ATTRIBUTE __attribute__((used))
92 #else
93 /* Before used, unused functions and vars were always emitted. */
94 #define NEEDED_ATTRIBUTE __attribute__((unused))
95 #endif
96 #else
97 #define UNNEEDED_ATTRIBUTE
98 #define NEEDED_ATTRIBUTE
99 #endif
100
101 #if HAVE_BUILTIN_CONSTANT_P
102 /**
103  * IS_COMPILE_CONSTANT - does the compiler know the value of this expression?
104  * @expr: the expression to evaluate
105  *
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.
110  *
111  * This can be done using the IS_COMPILE_CONSTANT() macro.
112  *
113  * Example:
114  *      enum greek { ALPHA, BETA, GAMMA, DELTA, EPSILON };
115  *
116  *      // Out-of-line version.
117  *      const char *greek_name(enum greek greek);
118  *
119  *      // Inline version.
120  *      static inline _greek_name(enum greek greek)
121  *      {
122  *              switch (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**";
129  *              }
130  *      }
131  *
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))
136  */
137 #define IS_COMPILE_CONSTANT(expr) __builtin_constant_p(expr)
138 #else
139 /* If we don't know, assume it's not. */
140 #define IS_COMPILE_CONSTANT(expr) 0
141 #endif
142 #endif /* CCAN_COMPILER_H */