compiler.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright 2011 Rusty Russell
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option) any
  7. * later version. See LICENSE for more details.
  8. */
  9. #ifndef CCAN_COMPILER_H
  10. #define CCAN_COMPILER_H
  11. #include "config.h"
  12. #ifndef COLD
  13. #if HAVE_ATTRIBUTE_COLD
  14. /**
  15. * COLD - a function is unlikely to be called.
  16. *
  17. * Used to mark an unlikely code path and optimize appropriately.
  18. * It is usually used on logging or error routines.
  19. *
  20. * Example:
  21. * static void COLD moan(const char *reason)
  22. * {
  23. * fprintf(stderr, "Error: %s (%s)\n", reason, strerror(errno));
  24. * }
  25. */
  26. #define COLD __attribute__((cold))
  27. #else
  28. #define COLD
  29. #endif
  30. #endif
  31. #ifndef NORETURN
  32. #if HAVE_ATTRIBUTE_NORETURN
  33. /**
  34. * NORETURN - a function does not return
  35. *
  36. * Used to mark a function which exits; useful for suppressing warnings.
  37. *
  38. * Example:
  39. * static void NORETURN fail(const char *reason)
  40. * {
  41. * fprintf(stderr, "Error: %s (%s)\n", reason, strerror(errno));
  42. * exit(1);
  43. * }
  44. */
  45. #define NORETURN __attribute__((noreturn))
  46. #else
  47. #define NORETURN
  48. #endif
  49. #endif
  50. #ifndef PRINTF_FMT
  51. #if HAVE_ATTRIBUTE_PRINTF
  52. /**
  53. * PRINTF_FMT - a function takes printf-style arguments
  54. * @nfmt: the 1-based number of the function's format argument.
  55. * @narg: the 1-based number of the function's first variable argument.
  56. *
  57. * This allows the compiler to check your parameters as it does for printf().
  58. *
  59. * Example:
  60. * void PRINTF_FMT(2,3) my_printf(const char *prefix, const char *fmt, ...);
  61. */
  62. #define PRINTF_FMT(nfmt, narg) \
  63. __attribute__((format(__printf__, nfmt, narg)))
  64. #else
  65. #define PRINTF_FMT(nfmt, narg)
  66. #endif
  67. #endif
  68. #ifndef IDEMPOTENT
  69. #if HAVE_ATTRIBUTE_CONST
  70. /**
  71. * IDEMPOTENT - a function's return depends only on its argument
  72. *
  73. * This allows the compiler to assume that the function will return the exact
  74. * same value for the exact same arguments. This implies that the function
  75. * must not use global variables, or dereference pointer arguments.
  76. */
  77. #define IDEMPOTENT __attribute__((const))
  78. #else
  79. #define IDEMPOTENT
  80. #endif
  81. #endif
  82. #if HAVE_ATTRIBUTE_UNUSED
  83. #ifndef UNNEEDED
  84. /**
  85. * UNNEEDED - a variable/function may not be needed
  86. *
  87. * This suppresses warnings about unused variables or functions, but tells
  88. * the compiler that if it is unused it need not emit it into the source code.
  89. *
  90. * Example:
  91. * // With some preprocessor options, this is unnecessary.
  92. * static UNNEEDED int counter;
  93. *
  94. * // With some preprocessor options, this is unnecessary.
  95. * static UNNEEDED void add_to_counter(int add)
  96. * {
  97. * counter += add;
  98. * }
  99. */
  100. #define UNNEEDED __attribute__((unused))
  101. #endif
  102. #ifndef NEEDED
  103. #if HAVE_ATTRIBUTE_USED
  104. /**
  105. * NEEDED - a variable/function is needed
  106. *
  107. * This suppresses warnings about unused variables or functions, but tells
  108. * the compiler that it must exist even if it (seems) unused.
  109. *
  110. * Example:
  111. * // Even if this is unused, these are vital for debugging.
  112. * static NEEDED int counter;
  113. * static NEEDED void dump_counter(void)
  114. * {
  115. * printf("Counter is %i\n", counter);
  116. * }
  117. */
  118. #define NEEDED __attribute__((used))
  119. #else
  120. /* Before used, unused functions and vars were always emitted. */
  121. #define NEEDED __attribute__((unused))
  122. #endif
  123. #endif
  124. #ifndef UNUSED
  125. /**
  126. * UNUSED - a parameter is unused
  127. *
  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.
  131. *
  132. * Example:
  133. * // This is used as a callback, so needs to have this prototype.
  134. * static int some_callback(void *unused UNUSED)
  135. * {
  136. * return 0;
  137. * }
  138. */
  139. #define UNUSED __attribute__((unused))
  140. #endif
  141. #else
  142. #ifndef UNNEEDED
  143. #define UNNEEDED
  144. #endif
  145. #ifndef NEEDED
  146. #define NEEDED
  147. #endif
  148. #ifndef UNUSED
  149. #define UNUSED
  150. #endif
  151. #endif
  152. #ifndef IS_COMPILE_CONSTANT
  153. #if HAVE_BUILTIN_CONSTANT_P
  154. /**
  155. * IS_COMPILE_CONSTANT - does the compiler know the value of this expression?
  156. * @expr: the expression to evaluate
  157. *
  158. * When an expression manipulation is complicated, it is usually better to
  159. * implement it in a function. However, if the expression being manipulated is
  160. * known at compile time, it is better to have the compiler see the entire
  161. * expression so it can simply substitute the result.
  162. *
  163. * This can be done using the IS_COMPILE_CONSTANT() macro.
  164. *
  165. * Example:
  166. * enum greek { ALPHA, BETA, GAMMA, DELTA, EPSILON };
  167. *
  168. * // Out-of-line version.
  169. * const char *greek_name(enum greek greek);
  170. *
  171. * // Inline version.
  172. * static inline const char *_greek_name(enum greek greek)
  173. * {
  174. * switch (greek) {
  175. * case ALPHA: return "alpha";
  176. * case BETA: return "beta";
  177. * case GAMMA: return "gamma";
  178. * case DELTA: return "delta";
  179. * case EPSILON: return "epsilon";
  180. * default: return "**INVALID**";
  181. * }
  182. * }
  183. *
  184. * // Use inline if compiler knows answer. Otherwise call function
  185. * // to avoid copies of the same code everywhere.
  186. * #define greek_name(g) \
  187. * (IS_COMPILE_CONSTANT(greek) ? _greek_name(g) : greek_name(g))
  188. */
  189. #define IS_COMPILE_CONSTANT(expr) __builtin_constant_p(expr)
  190. #else
  191. /* If we don't know, assume it's not. */
  192. #define IS_COMPILE_CONSTANT(expr) 0
  193. #endif
  194. #endif
  195. #ifndef WARN_UNUSED_RESULT
  196. #if HAVE_WARN_UNUSED_RESULT
  197. /**
  198. * WARN_UNUSED_RESULT - warn if a function return value is unused.
  199. *
  200. * Used to mark a function where it is extremely unlikely that the caller
  201. * can ignore the result, eg realloc().
  202. *
  203. * Example:
  204. * // buf param may be freed by this; need return value!
  205. * static char *WARN_UNUSED_RESULT enlarge(char *buf, unsigned *size)
  206. * {
  207. * return realloc(buf, (*size) *= 2);
  208. * }
  209. */
  210. #define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
  211. #else
  212. #define WARN_UNUSED_RESULT
  213. #endif
  214. #endif
  215. #endif /* CCAN_COMPILER_H */