doc: refer to cex from sections dealing with conflicts
[bison.git] / src / system.h
blobeac89c274b7e743195672f6453e6fc66fb6711fd
1 /* System-dependent definitions for Bison.
3 Copyright (C) 2000-2007, 2009-2015, 2018-2020 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 <http://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/archive/html/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>
135 /*-------------.
136 | Assertions. |
137 `-------------*/
139 /* In the past, Bison defined aver to simply invoke abort in the case of
140 a failed assertion. The rationale was that <assert.h>'s assertions
141 were too heavyweight and could be disabled too easily. See
142 discussions at
143 <http://lists.gnu.org/archive/html/bison-patches/2006-01/msg00080.html>
144 <http://lists.gnu.org/archive/html/bison-patches/2006-09/msg00111.html>.
146 However, normal assert output can be helpful during development and
147 in bug reports from users. Moreover, it's not clear now that
148 <assert.h>'s assertions are significantly heavyweight. Finally, if
149 users want to experiment with disabling assertions, it's debatable
150 whether it's our responsibility to stop them. See discussion
151 starting at
152 <http://lists.gnu.org/archive/html/bison-patches/2009-09/msg00013.html>.
154 For now, we use assert but we call it aver throughout Bison in case
155 we later wish to try another scheme.
157 # include <assert.h>
158 # define aver assert
161 /*-----------.
162 | Obstacks. |
163 `-----------*/
165 # define obstack_chunk_alloc xmalloc
166 # define obstack_chunk_free free
167 # include <obstack.h>
169 /* String-grow: append Str to Obs. */
171 # define obstack_sgrow(Obs, Str) \
172 obstack_grow (Obs, Str, strlen (Str))
174 /* Output Str escaped to be a string.
176 For instance "\"foo\"" -> "\\\"foo\\\"". */
178 # define obstack_backslash(Obs, Str) \
179 do { \
180 char const *p__; \
181 for (p__ = Str; *p__; p__++) \
182 switch (*p__) \
184 case '"': obstack_sgrow (Obs, "\\\""); break; \
185 case '\\': obstack_sgrow (Obs, "\\\\"); break; \
186 default: obstack_1grow (Obs, *p__); break; \
188 } while (0)
191 /* Output Str escaped for our postprocessing (i.e., escape M4 special
192 characters).
194 For instance "[foo]" -> "@{foo@}", "$$" -> "$][$][". */
196 # define obstack_escape(Obs, Str) \
197 do { \
198 char const *p__; \
199 for (p__ = Str; *p__; p__++) \
200 switch (*p__) \
202 case '$': obstack_sgrow (Obs, "$]["); break; \
203 case '@': obstack_sgrow (Obs, "@@" ); break; \
204 case '[': obstack_sgrow (Obs, "@{" ); break; \
205 case ']': obstack_sgrow (Obs, "@}" ); break; \
206 default: obstack_1grow (Obs, *p__ ); break; \
208 } while (0)
211 /* Output Str both quoted for M4 (i.e., embed in [[...]]), and escaped
212 for our postprocessing (i.e., escape M4 special characters). If
213 Str is empty (or NULL), output "[]" instead of "[[]]" as it makes
214 M4 programming easier (m4_ifval can be used).
216 For instance "[foo]" -> "[[@{foo@}]]", "$$" -> "[[$][$][]]". */
218 # define obstack_quote(Obs, Str) \
219 do { \
220 char const* obstack_quote_p = Str; \
221 if (obstack_quote_p && obstack_quote_p[0]) \
223 obstack_sgrow (Obs, "[["); \
224 obstack_escape (Obs, obstack_quote_p); \
225 obstack_sgrow (Obs, "]]"); \
227 else \
228 obstack_sgrow (Obs, "[]"); \
229 } while (0)
232 /* Append the ending 0, finish Obs, and return the string. */
234 # define obstack_finish0(Obs) \
235 (obstack_1grow (Obs, '\0'), (char *) obstack_finish (Obs))
238 /*-----------------------------------------.
239 | Extensions to use for the output files. |
240 `-----------------------------------------*/
242 # ifndef OUTPUT_EXT
243 # define OUTPUT_EXT ".output"
244 # endif
246 # ifndef TAB_EXT
247 # define TAB_EXT ".tab"
248 # endif
252 /*---------------------.
253 | Free a linked list. |
254 `---------------------*/
256 # define LIST_FREE(Type, List) \
257 do { \
258 Type *_node, *_next; \
259 for (_node = List; _node; _node = _next) \
261 _next = _node->next; \
262 free (_node); \
264 } while (0)
266 #endif /* ! BISON_SYSTEM_H */