compiler-tests.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
  2. /*
  3. * Copyright (c) 1993, 1994, 1995, 1996, 1997
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. All advertising materials mentioning features or use of this software
  15. * must display the following acknowledgement:
  16. * This product includes software developed by the Computer Systems
  17. * Engineering Group at Lawrence Berkeley Laboratory.
  18. * 4. Neither the name of the University nor of the Laboratory may be used
  19. * to endorse or promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. */
  34. #ifndef lib_pcap_compiler_tests_h
  35. #define lib_pcap_compiler_tests_h
  36. /*
  37. * This was introduced by Clang:
  38. *
  39. * https://clang.llvm.org/docs/LanguageExtensions.html#has-attribute
  40. *
  41. * in some version (which version?); it has been picked up by GCC 5.0.
  42. */
  43. #ifndef __has_attribute
  44. /*
  45. * It's a macro, so you can check whether it's defined to check
  46. * whether it's supported.
  47. *
  48. * If it's not, define it to always return 0, so that we move on to
  49. * the fallback checks.
  50. */
  51. #define __has_attribute(x) 0
  52. #endif
  53. /*
  54. * Note that the C90 spec's "6.8.1 Conditional inclusion" and the
  55. * C99 spec's and C11 spec's "6.10.1 Conditional inclusion" say:
  56. *
  57. * Prior to evaluation, macro invocations in the list of preprocessing
  58. * tokens that will become the controlling constant expression are
  59. * replaced (except for those macro names modified by the defined unary
  60. * operator), just as in normal text. If the token "defined" is
  61. * generated as a result of this replacement process or use of the
  62. * "defined" unary operator does not match one of the two specified
  63. * forms prior to macro replacement, the behavior is undefined.
  64. *
  65. * so you shouldn't use defined() in a #define that's used in #if or
  66. * #elif. Some versions of Clang, for example, will warn about this.
  67. *
  68. * Instead, we check whether the pre-defined macros for particular
  69. * compilers are defined and, if not, define the "is this version XXX
  70. * or a later version of this compiler" macros as 0.
  71. */
  72. /*
  73. * Check whether this is GCC major.minor or a later release, or some
  74. * compiler that claims to be "just like GCC" of that version or a
  75. * later release.
  76. */
  77. #if ! defined(__GNUC__)
  78. /* Not GCC and not "just like GCC" */
  79. #define PCAP_IS_AT_LEAST_GNUC_VERSION(major, minor) 0
  80. #else
  81. /* GCC or "just like GCC" */
  82. #define PCAP_IS_AT_LEAST_GNUC_VERSION(major, minor) \
  83. (__GNUC__ > (major) || \
  84. (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
  85. #endif
  86. /*
  87. * Check whether this is Clang major.minor or a later release.
  88. */
  89. #if !defined(__clang__)
  90. /* Not Clang */
  91. #define PCAP_IS_AT_LEAST_CLANG_VERSION(major, minor) 0
  92. #else
  93. /* Clang */
  94. #define PCAP_IS_AT_LEAST_CLANG_VERSION(major, minor) \
  95. (__clang_major__ > (major) || \
  96. (__clang_major__ == (major) && __clang_minor__ >= (minor)))
  97. #endif
  98. /*
  99. * Check whether this is Sun C/SunPro C/Oracle Studio major.minor
  100. * or a later release.
  101. *
  102. * The version number in __SUNPRO_C is encoded in hex BCD, with the
  103. * uppermost hex digit being the major version number, the next
  104. * one or two hex digits being the minor version number, and
  105. * the last digit being the patch version.
  106. *
  107. * It represents the *compiler* version, not the product version;
  108. * see
  109. *
  110. * https://sourceforge.net/p/predef/wiki/Compilers/
  111. *
  112. * for a partial mapping, which we assume continues for later
  113. * 12.x product releases.
  114. */
  115. #if ! defined(__SUNPRO_C)
  116. /* Not Sun/Oracle C */
  117. #define PCAP_IS_AT_LEAST_SUNC_VERSION(major,minor) 0
  118. #else
  119. /* Sun/Oracle C */
  120. #define PCAP_SUNPRO_VERSION_TO_BCD(major, minor) \
  121. (((minor) >= 10) ? \
  122. (((major) << 12) | (((minor)/10) << 8) | (((minor)%10) << 4)) : \
  123. (((major) << 8) | ((minor) << 4)))
  124. #define PCAP_IS_AT_LEAST_SUNC_VERSION(major,minor) \
  125. (__SUNPRO_C >= PCAP_SUNPRO_VERSION_TO_BCD((major), (minor)))
  126. #endif
  127. /*
  128. * Check whether this is IBM XL C major.minor or a later release.
  129. *
  130. * The version number in __xlC__ has the major version in the
  131. * upper 8 bits and the minor version in the lower 8 bits.
  132. * On AIX __xlC__ is always defined, __ibmxl__ becomes defined in XL C 16.1.
  133. * On Linux since XL C 13.1.6 __xlC__ is not defined by default anymore, but
  134. * __ibmxl__ is defined since at least XL C 13.1.1.
  135. */
  136. #if ! defined(__xlC__) && ! defined(__ibmxl__)
  137. /* Not XL C */
  138. #define PCAP_IS_AT_LEAST_XL_C_VERSION(major,minor) 0
  139. #else
  140. /* XL C */
  141. #if defined(__ibmxl__)
  142. /*
  143. * Later Linux version of XL C; use __ibmxl_version__ to test
  144. * the version.
  145. */
  146. #define PCAP_IS_AT_LEAST_XL_C_VERSION(major, minor) \
  147. (__ibmxl_version__ > (major) || \
  148. (__ibmxl_version__ == (major) && __ibmxl_release__ >= (minor)))
  149. #else /* __ibmxl__ */
  150. /*
  151. * __ibmxl__ not defined; use __xlC__ to test the version.
  152. */
  153. #define PCAP_IS_AT_LEAST_XL_C_VERSION(major, minor) \
  154. (__xlC__ >= (((major) << 8) | (minor)))
  155. #endif /* __ibmxl__ */
  156. #endif
  157. /*
  158. * Check whether this is HP aC++/HP C major.minor or a later release.
  159. *
  160. * The version number in __HP_aCC is encoded in zero-padded decimal BCD,
  161. * with the "A." stripped off, the uppermost two decimal digits being
  162. * the major version number, the next two decimal digits being the minor
  163. * version number, and the last two decimal digits being the patch version.
  164. * (Strip off the A., remove the . between the major and minor version
  165. * number, and add two digits of patch.)
  166. */
  167. #if ! defined(__HP_aCC)
  168. /* Not HP C */
  169. #define PCAP_IS_AT_LEAST_HP_C_VERSION(major,minor) 0
  170. #else
  171. /* HP C */
  172. #define PCAP_IS_AT_LEAST_HP_C_VERSION(major,minor) \
  173. (__HP_aCC >= ((major)*10000 + (minor)*100))
  174. #endif
  175. #endif /* lib_pcap_compiler_tests_h */