gnulib: update
[bison.git] / src / system.h
blobbbdf9fd0f8f9837f4329d003374f5411d1b5c1c7
1 /* System-dependent definitions for Bison.
3 Copyright (C) 2000-2007, 2009-2015, 2018-2021 Free Software
4 Foundation, Inc.
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.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19 #ifndef BISON_SYSTEM_H
20 # define BISON_SYSTEM_H
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
38 # include <limits.h>
39 # include <stddef.h>
40 # include <stdlib.h>
41 # include <string.h>
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))
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)
52 /* Whether Literal is a prefix of S. */
53 # define STRPREFIX_LIT(Literal, S) \
54 (STRNCMP_LIT (S, Literal) == 0)
56 # include <unistd.h>
57 # include <inttypes.h>
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
65 /* Version mismatch. */
66 # define EX_MISMATCH 63
68 /*---------.
69 | Gnulib. |
70 `---------*/
72 # include <unlocked-io.h>
73 # include <verify.h>
74 # include <xalloc.h>
76 // Clang and ICC like to pretend they are GCC.
77 # if defined __GNUC__ && !defined __clang__ && !defined __ICC
78 # define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
79 # endif
81 // See https://lists.gnu.org/r/bug-bison/2019-10/msg00061.html
82 // and https://trac.macports.org/ticket/59927.
83 # if defined GCC_VERSION && 405 <= GCC_VERSION
84 # define IGNORE_TYPE_LIMITS_BEGIN \
85 _Pragma ("GCC diagnostic push") \
86 _Pragma ("GCC diagnostic ignored \"-Wtype-limits\"")
87 # define IGNORE_TYPE_LIMITS_END \
88 _Pragma ("GCC diagnostic pop")
89 # else
90 # define IGNORE_TYPE_LIMITS_BEGIN
91 # define IGNORE_TYPE_LIMITS_END
92 # endif
95 /*-----------------.
96 | GCC extensions. |
97 `-----------------*/
99 /* Use PACIFY_CC to indicate that Code is unimportant to the logic of Bison
100 but that it is necessary for suppressing compiler warnings. For example,
101 Code might be a variable initializer that's always overwritten before the
102 variable is used.
104 PACIFY_CC is intended to be useful only as a comment as it does not alter
105 Code. It is tempting to redefine PACIFY_CC so that it will suppress Code
106 when configuring without --enable-gcc-warnings. However, that would mean
107 that, for maintainers, Bison would compile with potentially less warnings
108 and safer logic than it would for users. Due to the overhead of M4,
109 suppressing Code is unlikely to offer any significant improvement in
110 Bison's performance anyway. */
111 # define PACIFY_CC(Code) Code
113 # include <attribute.h>
116 /*------.
117 | NLS. |
118 `------*/
120 # include <locale.h>
122 # include <gettext.h>
123 # define _(Msgid) gettext (Msgid)
124 # define N_(Msgid) (Msgid)
127 /*-----------.
128 | Booleans. |
129 `-----------*/
131 # include <stdbool.h>
134 /*-----------.
135 | Integers. |
136 `-----------*/
138 static inline int
139 min_int (int a, int b)
141 return a < b ? a : b;
144 static inline int
145 max_int (int a, int b)
147 return a >= b ? a : b;
151 /*-------------.
152 | Assertions. |
153 `-------------*/
155 /* In the past, Bison defined aver to simply invoke abort in the case of
156 a failed assertion. The rationale was that <assert.h>'s assertions
157 were too heavyweight and could be disabled too easily. See
158 discussions at
159 <https://lists.gnu.org/r/bison-patches/2006-01/msg00080.html>
160 <https://lists.gnu.org/r/bison-patches/2006-09/msg00111.html>.
162 However, normal assert output can be helpful during development and
163 in bug reports from users. Moreover, it's not clear now that
164 <assert.h>'s assertions are significantly heavyweight. Finally, if
165 users want to experiment with disabling assertions, it's debatable
166 whether it's our responsibility to stop them. See discussion
167 starting at
168 <https://lists.gnu.org/r/bison-patches/2009-09/msg00013.html>.
170 For now, we use assert but we call it aver throughout Bison in case
171 we later wish to try another scheme.
173 # include <assert.h>
174 # define aver assert
177 /*-----------.
178 | Obstacks. |
179 `-----------*/
181 # define obstack_chunk_alloc xmalloc
182 # define obstack_chunk_free free
183 # include <obstack.h>
185 /* String-grow: append Str to Obs. */
187 # define obstack_sgrow(Obs, Str) \
188 obstack_grow (Obs, Str, strlen (Str))
190 /* Output Str escaped to be a string.
192 For instance "\"foo\"" -> "\\\"foo\\\"". */
194 # define obstack_backslash(Obs, Str) \
195 do { \
196 char const *p__; \
197 for (p__ = Str; *p__; p__++) \
198 switch (*p__) \
200 case '"': obstack_sgrow (Obs, "\\\""); break; \
201 case '\\': obstack_sgrow (Obs, "\\\\"); break; \
202 default: obstack_1grow (Obs, *p__); break; \
204 } while (0)
207 /* Output Str escaped for our postprocessing (i.e., escape M4 special
208 characters).
210 For instance "[foo]" -> "@{foo@}", "$$" -> "$][$][". */
212 # define obstack_escape(Obs, Str) \
213 do { \
214 char const *p__; \
215 for (p__ = Str; *p__; p__++) \
216 switch (*p__) \
218 case '$': obstack_sgrow (Obs, "$]["); break; \
219 case '@': obstack_sgrow (Obs, "@@" ); break; \
220 case '[': obstack_sgrow (Obs, "@{" ); break; \
221 case ']': obstack_sgrow (Obs, "@}" ); break; \
222 default: obstack_1grow (Obs, *p__ ); break; \
224 } while (0)
227 /* Output Str both quoted for M4 (i.e., embed in [[...]]), and escaped
228 for our postprocessing (i.e., escape M4 special characters). If
229 Str is empty (or NULL), output "[]" instead of "[[]]" as it makes
230 M4 programming easier (m4_ifval can be used).
232 For instance "[foo]" -> "[[@{foo@}]]", "$$" -> "[[$][$][]]". */
234 # define obstack_quote(Obs, Str) \
235 do { \
236 char const* obstack_quote_p = Str; \
237 if (obstack_quote_p && obstack_quote_p[0]) \
239 obstack_sgrow (Obs, "[["); \
240 obstack_escape (Obs, obstack_quote_p); \
241 obstack_sgrow (Obs, "]]"); \
243 else \
244 obstack_sgrow (Obs, "[]"); \
245 } while (0)
248 /* Append the ending 0, finish Obs, and return the string. */
250 # define obstack_finish0(Obs) \
251 (obstack_1grow (Obs, '\0'), (char *) obstack_finish (Obs))
254 /*-----------------------------------------.
255 | Extensions to use for the output files. |
256 `-----------------------------------------*/
258 # ifndef OUTPUT_EXT
259 # define OUTPUT_EXT ".output"
260 # endif
262 # ifndef TAB_EXT
263 # define TAB_EXT ".tab"
264 # endif
268 /*---------------------.
269 | Free a linked list. |
270 `---------------------*/
272 # define LIST_FREE(Type, List) \
273 do { \
274 Type *_node, *_next; \
275 for (_node = List; _node; _node = _next) \
277 _next = _node->next; \
278 free (_node); \
280 } while (0)
282 #endif /* ! BISON_SYSTEM_H */