bison-system.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* System-dependent definitions for Bison.
  2. Copyright (C) 2000-2007, 2009-2013 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #ifndef BISON_SYSTEM_H
  14. # define BISON_SYSTEM_H
  15. /* flex 2.5.31 gratutiously defines macros like INT8_MIN. But this
  16. runs afoul of pre-C99 compilers that have <inttypes.h> or
  17. <stdint.h>, which are included below if available. It also runs
  18. afoul of pre-C99 compilers that define these macros in <limits.h>. */
  19. # if ! defined __STDC_VERSION__ || __STDC_VERSION__ < 199901
  20. # undef INT8_MIN
  21. # undef INT16_MIN
  22. # undef INT32_MIN
  23. # undef INT8_MAX
  24. # undef INT16_MAX
  25. # undef UINT8_MAX
  26. # undef INT32_MAX
  27. # undef UINT16_MAX
  28. # undef UINT32_MAX
  29. # endif
  30. # include <limits.h>
  31. # include <stddef.h>
  32. # include <stdlib.h>
  33. # include <string.h>
  34. # define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
  35. # define STREQ(L, R) (strcmp(L, R) == 0)
  36. # define STRNEQ(L, R) (!STREQ(L, R))
  37. /* Just like strncmp, but the second argument must be a literal string
  38. and you don't specify the length. */
  39. # define STRNCMP_LIT(S, Literal) \
  40. strncmp (S, "" Literal "", sizeof (Literal) - 1)
  41. /* Whether Literal is a prefix of S. */
  42. # define STRPREFIX_LIT(Literal, S) \
  43. (STRNCMP_LIT (S, Literal) == 0)
  44. # include <unistd.h>
  45. #if (defined _MSC_VER) && (_MSC_VER < 1800)
  46. #else
  47. # include <inttypes.h>
  48. #endif
  49. # ifndef UINTPTR_MAX
  50. /* This isn't perfect, but it's good enough for Bison, which needs
  51. only to hash pointers. */
  52. typedef size_t uintptr_t;
  53. # endif
  54. /* Version mismatch. */
  55. # define EX_MISMATCH 63
  56. /*---------.
  57. | Gnulib. |
  58. `---------*/
  59. # include <unlocked-io.h>
  60. # include <verify.h>
  61. # include <xalloc.h>
  62. /*-----------------.
  63. | GCC extensions. |
  64. `-----------------*/
  65. /* Use PACIFY_CC to indicate that Code is unimportant to the logic of Bison
  66. but that it is necessary for suppressing compiler warnings. For example,
  67. Code might be a variable initializer that's always overwritten before the
  68. variable is used.
  69. PACIFY_CC is intended to be useful only as a comment as it does not alter
  70. Code. It is tempting to redefine PACIFY_CC so that it will suppress Code
  71. when configuring without --enable-gcc-warnings. However, that would mean
  72. that, for maintainers, Bison would compile with potentially less warnings
  73. and safer logic than it would for users. Due to the overhead of M4,
  74. suppressing Code is unlikely to offer any significant improvement in
  75. Bison's performance anyway. */
  76. # define PACIFY_CC(Code) Code
  77. # ifndef __attribute__
  78. /* This feature is available in gcc versions 2.5 and later. */
  79. # if (! defined __GNUC__ || __GNUC__ < 2 \
  80. || (__GNUC__ == 2 && __GNUC_MINOR__ < 5))
  81. # define __attribute__(Spec) /* empty */
  82. # endif
  83. # endif
  84. /* The __-protected variants of 'format' and 'printf' attributes
  85. are accepted by gcc versions 2.6.4 (effectively 2.7) and later. */
  86. # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
  87. # define __format__ format
  88. # define __printf__ printf
  89. # endif
  90. # ifndef ATTRIBUTE_NORETURN
  91. # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
  92. # endif
  93. # ifndef ATTRIBUTE_UNUSED
  94. # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
  95. # endif
  96. /*------.
  97. | NLS. |
  98. `------*/
  99. # include <locale.h>
  100. # include <gettext.h>
  101. # define _(Msgid) gettext (Msgid)
  102. # define N_(Msgid) (Msgid)
  103. /*-----------.
  104. | Booleans. |
  105. `-----------*/
  106. # include <stdbool.h>
  107. /*-------------.
  108. | Assertions. |
  109. `-------------*/
  110. /* In the past, Bison defined aver to simply invoke abort in the case of
  111. a failed assertion. The rationale was that <assert.h>'s assertions
  112. were too heavyweight and could be disabled too easily. See
  113. discussions at
  114. <http://lists.gnu.org/archive/html/bison-patches/2006-01/msg00080.html>
  115. <http://lists.gnu.org/archive/html/bison-patches/2006-09/msg00111.html>.
  116. However, normal assert output can be helpful during development and
  117. in bug reports from users. Moreover, it's not clear now that
  118. <assert.h>'s assertions are significantly heavyweight. Finally, if
  119. users want to experiment with disabling assertions, it's debatable
  120. whether it's our responsibility to stop them. See discussion
  121. starting at
  122. <http://lists.gnu.org/archive/html/bison-patches/2009-09/msg00013.html>.
  123. For now, we use assert but we call it aver throughout Bison in case
  124. we later wish to try another scheme.
  125. */
  126. # include <assert.h>
  127. # define aver assert
  128. /*-----------.
  129. | Obstacks. |
  130. `-----------*/
  131. # define obstack_chunk_alloc xmalloc
  132. # define obstack_chunk_free free
  133. # include <obstack.h>
  134. /* String-grow: append Str to Obs. */
  135. # define obstack_sgrow(Obs, Str) \
  136. obstack_grow (Obs, Str, strlen (Str))
  137. /* Output Str escaped for our postprocessing (i.e., escape M4 special
  138. characters).
  139. For instance "[foo]" -> "@{foo@}", "$$" -> "$][$][". */
  140. # define obstack_escape(Obs, Str) \
  141. do { \
  142. char const *p__; \
  143. for (p__ = Str; *p__; p__++) \
  144. switch (*p__) \
  145. { \
  146. case '$': obstack_sgrow (Obs, "$]["); break; \
  147. case '@': obstack_sgrow (Obs, "@@" ); break; \
  148. case '[': obstack_sgrow (Obs, "@{" ); break; \
  149. case ']': obstack_sgrow (Obs, "@}" ); break; \
  150. default: obstack_1grow (Obs, *p__ ); break; \
  151. } \
  152. } while (0)
  153. /* Output Str both quoted for M4 (i.e., embed in [[...]]), and escaped
  154. for our postprocessing (i.e., escape M4 special characters). If
  155. Str is empty (or NULL), output "[]" instead of "[[]]" as it make M4
  156. programming easier (m4_ifval can be used).
  157. For instance "[foo]" -> "[[@{foo@}]]", "$$" -> "[[$][$][]]". */
  158. # define obstack_quote(Obs, Str) \
  159. do { \
  160. char const* obstack_quote_p = Str; \
  161. if (obstack_quote_p && obstack_quote_p[0]) \
  162. { \
  163. obstack_sgrow (Obs, "[["); \
  164. obstack_escape (Obs, obstack_quote_p); \
  165. obstack_sgrow (Obs, "]]"); \
  166. } \
  167. else \
  168. obstack_sgrow (Obs, "[]"); \
  169. } while (0)
  170. /* Append the ending 0, finish Obs, and return the string. */
  171. # define obstack_finish0(Obs) \
  172. (obstack_1grow (Obs, '\0'), (char *) obstack_finish (Obs))
  173. /*-----------------------------------------.
  174. | Extensions to use for the output files. |
  175. `-----------------------------------------*/
  176. # ifndef OUTPUT_EXT
  177. # define OUTPUT_EXT ".output"
  178. # endif
  179. # ifndef TAB_EXT
  180. # define TAB_EXT ".tab"
  181. # endif
  182. /*---------------------.
  183. | Free a linked list. |
  184. `---------------------*/
  185. # define LIST_FREE(Type, List) \
  186. do { \
  187. Type *_node, *_next; \
  188. for (_node = List; _node; _node = _next) \
  189. { \
  190. _next = _node->next; \
  191. free (_node); \
  192. } \
  193. } while (0)
  194. /*---------------------------------------------.
  195. | Debugging memory allocation (must be last). |
  196. `---------------------------------------------*/
  197. # if WITH_DMALLOC
  198. # define DMALLOC_FUNC_CHECK
  199. # include <dmalloc.h>
  200. # endif /* WITH_DMALLOC */
  201. #endif /* ! BISON_SYSTEM_H */