compiler.h 5.8 KB

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