Gcc.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef Gcc_H
  16. #define Gcc_H
  17. #include "util/Js.h"
  18. // GCC > 6
  19. #if defined(__GNUC__) && (__GNUC__ > 6)
  20. #define Gcc_FALLTHRU \
  21. __attribute__((fallthrough));
  22. #endif
  23. // clang OR GCC >= 4.4
  24. #if defined(__clang__) || (defined(__GNUC__) && \
  25. (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)))
  26. #define Gcc_PRINTF( format_idx, arg_idx ) \
  27. __attribute__((__format__ (__printf__, format_idx, arg_idx)))
  28. #define Gcc_ALLOC_SIZE(...) \
  29. __attribute__ ((alloc_size(__VA_ARGS__)))
  30. #define Gcc_USE_RET \
  31. __attribute__ ((warn_unused_result))
  32. #define Gcc_PACKED \
  33. __attribute__ ((packed))
  34. #define Gcc_NORETURN \
  35. __attribute__((__noreturn__))
  36. #endif
  37. // GCC >= 4.4
  38. #if !defined(__clang__) && \
  39. defined(__GNUC__) && \
  40. (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
  41. #define Gcc_NONNULL(...) \
  42. __attribute__((__nonnull__(__VA_ARGS__)))
  43. #define Gcc_PURE \
  44. __attribute__ ((__pure__))
  45. #endif
  46. // clang only
  47. #if defined(__clang__)
  48. // C11 only
  49. //#define Gcc_NORETURN _Noreturn
  50. #endif
  51. #ifndef Gcc_PRINTF
  52. #define Gcc_PRINTF( format_idx, arg_idx )
  53. #endif
  54. #ifndef Gcc_NORETURN
  55. #define Gcc_NORETURN
  56. #endif
  57. #ifndef Gcc_NONNULL
  58. #define Gcc_NONNULL(...)
  59. #endif
  60. #ifndef Gcc_PURE
  61. #define Gcc_PURE
  62. #endif
  63. // Missing this will lead to very wrong code
  64. // #ifndef Gcc_PACKED
  65. // #define Gcc_PACKED
  66. // #endif
  67. #ifndef Gcc_FALLTHRU
  68. #define Gcc_FALLTHRU
  69. #endif
  70. #ifndef Gcc_ALLOC_SIZE
  71. #define Gcc_ALLOC_SIZE(...)
  72. #endif
  73. #ifndef Gcc_USE_RET
  74. #define Gcc_USE_RET
  75. #endif
  76. Js({ this.Gcc_shortFile = (x) => '"' + x.substring(x.lastIndexOf('/')+1) + '"'; })
  77. #define Gcc_SHORT_FILE \
  78. Js_or({ return this.Gcc_shortFile(__FILE__); }, __FILE__)
  79. #define Gcc_FILE Gcc_SHORT_FILE
  80. #define Gcc_LINE __LINE__
  81. Gcc_PRINTF(1,2)
  82. static inline void Gcc_checkPrintf(const char* format, ...)
  83. {
  84. // This does nothing except to trigger warnings if the format is wrong.
  85. }
  86. #endif