tuklib_common.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // SPDX-License-Identifier: 0BSD
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //
  4. /// \file tuklib_common.h
  5. /// \brief Common definitions for tuklib modules
  6. //
  7. // Author: Lasse Collin
  8. //
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef TUKLIB_COMMON_H
  11. #define TUKLIB_COMMON_H
  12. // The config file may be replaced by a package-specific file.
  13. // It should include at least stddef.h, stdbool.h, inttypes.h, and limits.h.
  14. #include "tuklib_config.h"
  15. // TUKLIB_SYMBOL_PREFIX is prefixed to all symbols exported by
  16. // the tuklib modules. If you use a tuklib module in a library,
  17. // you should use TUKLIB_SYMBOL_PREFIX to make sure that there
  18. // are no symbol conflicts in case someone links your library
  19. // into application that also uses the same tuklib module.
  20. #ifndef TUKLIB_SYMBOL_PREFIX
  21. # define TUKLIB_SYMBOL_PREFIX
  22. #endif
  23. #define TUKLIB_CAT_X(a, b) a ## b
  24. #define TUKLIB_CAT(a, b) TUKLIB_CAT_X(a, b)
  25. #ifndef TUKLIB_SYMBOL
  26. # define TUKLIB_SYMBOL(sym) TUKLIB_CAT(TUKLIB_SYMBOL_PREFIX, sym)
  27. #endif
  28. #ifndef TUKLIB_DECLS_BEGIN
  29. # ifdef __cplusplus
  30. # define TUKLIB_DECLS_BEGIN extern "C" {
  31. # else
  32. # define TUKLIB_DECLS_BEGIN
  33. # endif
  34. #endif
  35. #ifndef TUKLIB_DECLS_END
  36. # ifdef __cplusplus
  37. # define TUKLIB_DECLS_END }
  38. # else
  39. # define TUKLIB_DECLS_END
  40. # endif
  41. #endif
  42. #if defined(__GNUC__) && defined(__GNUC_MINOR__)
  43. # define TUKLIB_GNUC_REQ(major, minor) \
  44. ((__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)) \
  45. || __GNUC__ > (major))
  46. #else
  47. # define TUKLIB_GNUC_REQ(major, minor) 0
  48. #endif
  49. // tuklib_attr_noreturn attribute is used to mark functions as non-returning.
  50. // We cannot use "noreturn" as the macro name because then C23 code that
  51. // uses [[noreturn]] would break as it would expand to [[ [[noreturn]] ]].
  52. //
  53. // tuklib_attr_noreturn must be used at the beginning of function declaration
  54. // to work in all cases. The [[noreturn]] syntax is the most limiting, it
  55. // must be even before any GNU C's __attribute__ keywords:
  56. //
  57. // tuklib_attr_noreturn
  58. // __attribute__((nonnull(1)))
  59. // extern void foo(const char *s);
  60. //
  61. // FIXME: Update __STDC_VERSION__ for the final C23 version. 202000 is used
  62. // by GCC 13 and Clang 15 with -std=c2x.
  63. #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202000
  64. # define tuklib_attr_noreturn [[noreturn]]
  65. #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112
  66. # define tuklib_attr_noreturn _Noreturn
  67. #elif TUKLIB_GNUC_REQ(2, 5)
  68. # define tuklib_attr_noreturn __attribute__((__noreturn__))
  69. #elif defined(_MSC_VER)
  70. # define tuklib_attr_noreturn __declspec(noreturn)
  71. #else
  72. # define tuklib_attr_noreturn
  73. #endif
  74. #if (defined(_WIN32) && !defined(__CYGWIN__)) \
  75. || defined(__OS2__) || defined(__MSDOS__)
  76. # define TUKLIB_DOSLIKE 1
  77. #endif
  78. #endif