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