compiler.h 5.4 KB

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