gnulib:update
[bison.git] / tests / local.at
blob14cd448cea8b8b72a06962b91ebee572fba627a2
1 # Process this -*- Autotest -*- file with autom4te.
3 # Macros for the GNU Bison Test suite.
5 # Copyright (C) 2003-2015, 2018-2019 Free Software Foundation, Inc.
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 m4_version_prereq([2.58])
23 # m4_null_if(VAL, IF-TRUE, IF-FALSE)
24 # ----------------------------------
25 # If VAL evaluates to empty or 0, run IF-TRUE, otherwise IF-FALSE.
26 m4_define([m4_null_if],
27 [m4_case(m4_quote($1),
28          [0], [$2],
29          [],  [$2],
30          [$3])])
33 # AT_SETUP_STRIP(TITLE)
34 # ---------------------
35 # Abbreviate the TITLE to be passed to AT_SETUP.  Remove new-lines
36 # that completely break AT_SETUP.
37 m4_define([AT_SETUP_STRIP],
38 [m4_bpatsubsts([$1],
39       [%\(language\|skeleton\) "?\([^\" ]*\)"?],
40         [\2],
41       [%define "?\([-A-Za-z0-9_.]+\)"? \({[^\}]+}\|"[^\"]+"\|[-A-Za-z0-9_.]+\)],
42         [\1=\2],
43       [%define "?\([-A-Za-z0-9_.]+\)"?],
44         [\1],
45       [ *
46 + *],    [ ])dnl
50 # AT_DATA_NO_FINAL_EOL(FILE, CONTENT)
51 # -----------------------------------
52 # Same as AT_DATA, except that CONTENT has no the final end of line.
54 # FIXME: AT_DATA or some variant of AT_DATA may eventually permit
55 # the final newline to be omitted.  See the threads starting at
56 # <http://lists.gnu.org/archive/html/bison-patches/2009-07/msg00019.html>.
57 m4_define([AT_DATA_NO_FINAL_EOL],
58 [AT_DATA([$1], [$2
60 AT_REQUIRE([
61 set x `LC_ALL=C ls -l '$1'` &&
62   size=$][6 &&
63   { test $size -eq 0 || dd obs=1 seek=`expr $size - 1` if=/dev/null of='$1'; }],
64   [], [ignore], [ignore])
69 ## ------------- ##
70 ## Basic tests.  ##
71 ## ------------- ##
74 # AT_PERL_CHECK(PERL-ARGS, ...)
75 # -----------------------------
76 # If Perl is available, run this test.
77 m4_define([AT_PERL_CHECK],
78 [if test x"$PERL" != x; then
79   AT_CHECK(["$PERL" $1], [$2], [$3], [$4])
84 # AT_REQUIRE(CMD, ...)
85 # --------------------
86 # Same as AT_CHECK(...) but skip this test if we failed.
87 m4_define([AT_REQUIRE],
88 [AT_CHECK([$1 || exit 77], [$2], [$3], [$4])])
91 # AT_PERL_REQUIRE(PERL-ARGS, ...)
92 # -------------------------------
93 # Run this Perl program, or skip the test if Perl is not available.
94 m4_define([AT_PERL_REQUIRE],
95 [AT_REQUIRE(["$PERL" $1], [$2], [$3], [$4])])
98 # AT_MATCHES_CHECK(FILE, PERL-REGEXP, COUNT)
99 # ------------------------------------------
100 # Expect COUNT matches of the PERL-REGEXP in FILE.  The file is
101 # taken in "slurp" mode, i.e., one can match end-of-lines.
102 m4_define([AT_MATCHES_CHECK],
103 [AT_PERL_CHECK([-0777 -ne '
104     my $count = 0;
105     s{$2}{ ++$count; "" }gem;
106     printf "$count\n";' $1], [0], [$3
107 ])])
110 # AT_SAVE_SPECIAL_FILES / AT_RESTORE_SPECIAL_FILES
111 # ------------------------------------------------
112 # Don't interfere with caller's files.
113 m4_divert_text([PREPARE_TESTS],
114 [at_save_special_files ()
116   for at_save_file in stderr experr expout
117   do
118     test ! -f at-bison-check-$at_save_file.bak ||
119       as_fn_error 1 "fatal error: back-up on top of a back-up"
120     test ! -f $at_save_file || mv $at_save_file at-bison-check-$at_save_file.bak
121   done
124 at_restore_special_files ()
126   for at_save_file in stderr experr expout
127   do
128     test ! -f at-bison-check-$at_save_file.bak ||
129       mv at-bison-check-$at_save_file.bak $at_save_file
130   done
134 m4_define([AT_SAVE_SPECIAL_FILES],    [at_save_special_files])
135 m4_define([AT_RESTORE_SPECIAL_FILES], [at_restore_special_files])
139 ## ------------------------------- ##
140 ## Macros decoding Bison options.  ##
141 ## ------------------------------- ##
143 # AT_LOC_PUSHDEF(FIRST-LINE, FIRST-COLUMN, LAST-LINE, LAST-COLUMN)
144 # ----------------------------------------------------------------
145 # Pushdef AT(_LOC)?_(FIRST|LAST)_(LINE|COLUMN).
146 m4_define([AT_LOC_PUSHDEF],
147 [m4_pushdef([AT_FIRST_LINE],  [$1])
148 m4_pushdef([AT_FIRST_COLUMN], [$2])
149 m4_pushdef([AT_LAST_LINE],    [$3])
150 m4_pushdef([AT_LAST_COLUMN],  [$4])
151 m4_pushdef([AT_LOC_FIRST_LINE],   [AT_LOC.AT_FIRST_LINE])
152 m4_pushdef([AT_LOC_FIRST_COLUMN], [AT_LOC.AT_FIRST_COLUMN])
153 m4_pushdef([AT_LOC_LAST_LINE],    [AT_LOC.AT_LAST_LINE])
154 m4_pushdef([AT_LOC_LAST_COLUMN],  [AT_LOC.AT_LAST_COLUMN])])
156 # AT_LOC_POPDEF
157 # -------------
158 # Popdef AT(_LOC)?_(FIRST|LAST)_(LINE|COLUMN).
159 m4_define([AT_LOC_POPDEF],
160 [m4_popdef([AT_LOC_FIRST_LINE])
161 m4_popdef([AT_LOC_FIRST_COLUMN])
162 m4_popdef([AT_LOC_LAST_LINE])
163 m4_popdef([AT_LOC_LAST_COLUMN])
164 m4_popdef([AT_FIRST_LINE])
165 m4_popdef([AT_FIRST_COLUMN])
166 m4_popdef([AT_LAST_LINE])
167 m4_popdef([AT_LAST_COLUMN])
172 # AT_BISON_OPTION_PUSHDEFS([BISON-OPTIONS])
173 # -----------------------------------------
174 m4_define([AT_BISON_OPTION_PUSHDEFS],
175 [m4_divert_text([KILL],
176                 [_AT_BISON_OPTION_PUSHDEFS($[1], $[2], [$1])])])
179 # _AT_BISON_OPTION_PUSHDEFS($1, $2, [BISON-OPTIONS])
180 # --------------------------------------------------
181 # This macro works around the impossibility to define macros
182 # inside macros, because issuing '[$1]' is not possible in M4 :(.
183 # This sucks hard, GNU M4 should really provide M5-like $$1.
184 m4_define([_AT_BISON_OPTION_PUSHDEFS],
185 [m4_pushdef([AT_BISON_OPTIONS], [$3])
187 m4_if([$1$2], $[1]$[2], [],
188        [m4_fatal([$0: invalid arguments: $@])])dnl
189 m4_pushdef([AT_AUTOMOVE_IF],
190 [m4_bmatch([$3], [%define api\.value\.automove], [$1], [$2])])
191 m4_pushdef([AT_DEFINES_IF],
192 [m4_bmatch([$3], [%defines], [$1], [$2])])
193 m4_pushdef([AT_DEBUG_IF],
194 [m4_bmatch([$3], [%debug\|%define parse.trace], [$1], [$2])])
195 m4_pushdef([AT_CXX_IF],
196 [m4_bmatch([$3], [%language "[Cc]\+\+"\|%skeleton "[a-z0-9]+\.cc"], [$1], [$2])])
197 m4_pushdef([AT_D_IF],
198 [m4_bmatch([$3], [%language "[Dd]"\|%skeleton "[a-z0-9]+\.d"], [$1], [$2])])
199 m4_pushdef([AT_JAVA_IF],
200 [m4_bmatch([$3], [%language "[Jj][Aa][Vv][Aa]"\|%skeleton "[a-z0-9]+\.java"], [$1], [$2])])
201 # The target language: "c", "c++", or "java".
202 m4_pushdef([AT_LANG],
203 [AT_JAVA_IF([java],
204             [AT_CXX_IF([c++],
205                        [AT_D_IF([d],
206                                 [c])])])])
207 m4_pushdef([AT_C_IF],
208 [m4_if(AT_LANG, [c], [$1], [$2])])
209 m4_pushdef([AT_GLR_IF],
210 [m4_bmatch([$3], [%glr-parser\|%skeleton "glr\..*"], [$1], [$2])])
211 m4_pushdef([AT_LALR1_CC_IF],
212 [AT_CXX_IF([AT_GLR_IF([$2], [$1])], [$2])])
213 m4_pushdef([AT_GLR_CC_IF],
214 [AT_CXX_IF([AT_GLR_IF([$1], [$2])], [$2])])
215 # Using yacc.c?
216 m4_pushdef([AT_YACC_IF],
217 [m4_bmatch([$3], [%language\|%glr-parser\|%skeleton], [$2], [$1])])
218 m4_pushdef([AT_LEXPARAM_IF],
219 [m4_bmatch([$3], [%lex-param], [$1], [$2])])
220 m4_pushdef([AT_LOCATION_IF],
221 [m4_bmatch([$3], [%locations], [$1], [$2])])
222 # Whether we use the Span location type (see calc.at), whose members are
223 # not named like those of the default location type (on purpose, to make
224 # sure we can use a user type).
225 m4_pushdef([AT_LOCATION_TYPE_SPAN_IF],
226 [m4_bmatch([$3], [%define \(api\.location\.type\|location_type\) \{Span\}], [$1], [$2])])
227 m4_pushdef([AT_PARAM_IF],
228 [m4_bmatch([$3], [%parse-param], [$1], [$2])])
229 # Comma-terminated list of formals parse-parameters.
230 # E.g., %parse-param { int x } %parse-param {int y} -> "int x, int y, ".
231 # FIXME: Support grouped parse-param.
232 m4_pushdef([AT_PARSE_PARAMS])
233 m4_bpatsubst([$3], [%parse-param { *\([^{}]*[^{} ]\) *}],
234              [m4_append([AT_PARSE_PARAMS], [\1, ])])
236 m4_pushdef([AT_PURE_IF],
237 [m4_bmatch([$3], [%define  *api\.pure\|%pure-parser],
238            [m4_bmatch([$3], [%define  *api\.pure *false], [$2], [$1])],
239            [$2])])
240 m4_pushdef([AT_PUSH_IF],
241 [m4_bmatch([$3], [%define api.push-pull \(both\|pull\)], [$1], [$2])])
242 # AT_NAME_PREFIX.
243 m4_pushdef([AT_NAME_PREFIX],
244 [m4_bmatch([$3], [\(%define api\.prefix\|%name-prefix\) .*],
245    [m4_bregexp([$3],
246      [\(%define api\.prefix\|%name-prefix\) [\{\"]\([^\"\}]*\)[\"\}]],
247      [\2])],
248    [AT_JAVA_IF([YY], [yy])])])
249 # AT_NAMESPACE: also consider api.prefix.  FIXME: Stop that confusion.
250 m4_pushdef([AT_NAMESPACE],
251 [m4_bmatch([$3], [\(%define api\.\(namespace\|prefix\)\|%name-prefix\) .*],
252    [m4_bregexp([$3],
253      [\(%define api\.\(namespace\|prefix\)\|%name-prefix\) [\{\"]\([^\"\}]*\)[\"\}]],
254      [\3])],
255    [yy])])
256 m4_pushdef([AT_TOKEN_CTOR_IF],
257 [m4_bmatch([$3], [%define api\.token\.constructor], [$1], [$2])])
258 m4_pushdef([AT_TOKEN_PREFIX],
259 [m4_bmatch([$3], [%define api\.token\.prefix {.*}],
260            [m4_bregexp([$3], [%define api\.token\.prefix {\(.*\)}], [\1])])])
261 m4_pushdef([AT_TOKEN_RAW_IF],
262 [m4_bmatch([$3], [%define api\.token\.raw], [$1], [$2])])
263 m4_pushdef([AT_VARIANT_IF],
264 [m4_bmatch([$3], [%define api\.value\.type variant], [$1], [$2])])
265 m4_pushdef([AT_API_prefix],
266 [m4_bmatch([$3], [%define api\.prefix {.*}],
267            [m4_bregexp([$3], [%define api\.prefix {\([^\}]*\)}], [\1])],
268            [AT_JAVA_IF([YY], [yy])])])
269 m4_pushdef([AT_API_PREFIX],
270 [m4_toupper(AT_API_prefix)])
271 # yyerror receives the location if %location, and if the parser is pure. For
272 # historical reasons, with the "yacc.c" skeleton, the location is not passed
273 # unless an additional "%parse-param" is present, or if the purity is defined
274 # as "full".
275 m4_pushdef([AT_YYERROR_ARG_LOC_IF],
276 [AT_LOCATION_IF([AT_PURE_IF([m4_bmatch([$3],
277                                        m4_quote(m4_join([\|],
278                                                         [%define api\.pure full],
279                                                         [%glr-parser],
280                                                         [%parse-param],
281                                                         [%skeleton "?glr.c"?])),
282                                        [$1], [$2])],
283                             [$2])],
284                     [$2])])
286 # yyerror always sees the locations (when activated) if the parser is impure.
287 # When the parser is pure, yyerror sees the location if it is received as an
288 # argument.
289 m4_pushdef([AT_YYERROR_SEES_LOC_IF],
290 [AT_LOCATION_IF([AT_YACC_IF([AT_PURE_IF([AT_YYERROR_ARG_LOC_IF([$1], [$2])],
291                                         [$1])],
292                             [$1])],
293                 [$2])])
295 # The interface is pure: either because %define api.pure, or because we
296 # are using the C++ parsers.
297 m4_pushdef([AT_PURE_LEX_IF],
298 [AT_PURE_IF([$1],
299             [AT_CXX_IF([$1], [$2])])])
301 m4_pushdef([AT_YYSTYPE],
302 [AT_CXX_IF([AT_NAMESPACE[::parser::semantic_type]],
303                [AT_API_PREFIX[STYPE]])])
304 m4_pushdef([AT_YYLTYPE],
305 [AT_CXX_IF([AT_NAMESPACE[::parser::location_type]],
306                [AT_API_PREFIX[LTYPE]])])
308 AT_TOKEN_CTOR_IF(
309 [m4_pushdef([AT_LOC], [[(]AT_NAME_PREFIX[lloc)]])
310  m4_pushdef([AT_VAL], [[(]AT_NAME_PREFIX[lval)]])
311  m4_pushdef([AT_YYLEX_FORMALS],     [])
312  m4_pushdef([AT_YYLEX_RETURN],      [yy::parser::symbol_type])
313  m4_pushdef([AT_YYLEX_ARGS],        [])
314  m4_pushdef([AT_USE_LEX_ARGS],      [])
315  m4_pushdef([AT_YYLEX_PRE_FORMALS], [])
316  m4_pushdef([AT_YYLEX_PRE_ARGS],    [])],
317 [AT_PURE_LEX_IF(
318 [m4_pushdef([AT_LOC], [(*llocp)])
319  m4_pushdef([AT_VAL], [(*lvalp)])
320  m4_pushdef([AT_YYLEX_FORMALS],
321             [AT_YYSTYPE *lvalp[]AT_LOCATION_IF([, AT_YYLTYPE *llocp])])
322  m4_pushdef([AT_YYLEX_RETURN], [int])
323  m4_pushdef([AT_YYLEX_ARGS],
324             [lvalp[]AT_LOCATION_IF([, llocp])])
325  m4_pushdef([AT_USE_LEX_ARGS],
326             [(void) lvalp;AT_LOCATION_IF([(void) llocp;])])
327  m4_pushdef([AT_YYLEX_PRE_FORMALS],
328             [AT_YYLEX_FORMALS, ])
329  m4_pushdef([AT_YYLEX_PRE_ARGS],
330             [AT_YYLEX_ARGS, ])
332 [m4_pushdef([AT_LOC], [[(]AT_NAME_PREFIX[lloc)]])
333  m4_pushdef([AT_VAL], [[(]AT_NAME_PREFIX[lval)]])
334  m4_pushdef([AT_YYLEX_FORMALS],     [void])
335  m4_pushdef([AT_YYLEX_RETURN],      [int])
336  m4_pushdef([AT_YYLEX_ARGS],        [])
337  m4_pushdef([AT_USE_LEX_ARGS],      [])
338  m4_pushdef([AT_YYLEX_PRE_FORMALS], [])
339  m4_pushdef([AT_YYLEX_PRE_ARGS],    [])
340 ])])
343 # Handle the different types of location components.
344 AT_LOCATION_TYPE_SPAN_IF(
345     [AT_LOC_PUSHDEF([first.l], [first.c], [last.l], [last.c])],
346     [AT_CXX_IF([AT_LOC_PUSHDEF([begin.line], [begin.column], [end.line], [end.column])],
347                [AT_LOC_PUSHDEF([first_line], [first_column], [last_line], [last_column])])])
350 AT_GLR_IF([AT_KEYWORDS([glr])])
351 ])# _AT_BISON_OPTION_PUSHDEFS
354 # AT_BISON_OPTION_POPDEFS
355 # -----------------------
356 m4_define([AT_BISON_OPTION_POPDEFS],
357 [m4_divert_text([KILL],
358 [m4_popdef([AT_BISON_OPTIONS])
360 m4_popdef([AT_YYLEX_PRE_ARGS])
361 m4_popdef([AT_YYLEX_PRE_FORMALS])
362 m4_popdef([AT_USE_LEX_ARGS])
363 m4_popdef([AT_YYLEX_ARGS])
364 m4_popdef([AT_YYLEX_FORMALS])
365 m4_popdef([AT_YYLTYPE])
366 m4_popdef([AT_YYSTYPE])
367 m4_popdef([AT_VAL])
368 m4_popdef([AT_LOC])
369 m4_popdef([AT_PURE_LEX_IF])
370 m4_popdef([AT_YYERROR_SEES_LOC_IF])
371 m4_popdef([AT_YYERROR_ARG_LOC_IF])
372 m4_popdef([AT_API_PREFIX])
373 m4_popdef([AT_API_prefix])
374 m4_popdef([AT_VARIANT_IF])
375 m4_popdef([AT_TOKEN_RAW_IF])
376 m4_popdef([AT_TOKEN_PREFIX])
377 m4_popdef([AT_TOKEN_CTOR_IF])
378 m4_popdef([AT_NAMESPACE])
379 m4_popdef([AT_NAME_PREFIX])
380 m4_popdef([AT_LOCATION_TYPE_SPAN_IF])
381 m4_popdef([AT_LOCATION_IF])
382 m4_popdef([AT_PARSE_PARAMS])
383 m4_popdef([AT_PUSH_IF])
384 m4_popdef([AT_PURE_IF])
385 m4_popdef([AT_PARAM_IF])
386 m4_popdef([AT_LEXPARAM_IF])
387 m4_popdef([AT_YACC_IF])
388 m4_popdef([AT_GLR_IF])
389 m4_popdef([AT_CXX_IF])
390 m4_popdef([AT_C_IF])
391 m4_popdef([AT_LANG])
392 m4_popdef([AT_JAVA_IF])
393 m4_popdef([AT_GLR_CC_IF])
394 m4_popdef([AT_LALR1_CC_IF])
395 m4_popdef([AT_DEFINES_IF])
396 m4_popdef([AT_DEBUG_IF])
397 AT_LOC_POPDEF])dnl
398 ])# AT_BISON_OPTION_POPDEFS
402 ## -------------------------- ##
403 ## Generating Grammar Files.  ##
404 ## -------------------------- ##
406 # AT_LANG_CASE(LANG1, IF-LANG1, LANG2, IF-LANG2, ..., DEFAULT)
407 # ------------------------------------------------------------
408 m4_define([AT_LANG_CASE],
409 [m4_case(AT_LANG, $@)])
412 # _AT_LANG_DISPATCH(LANG, MACRO, ARGS)
413 # ------------------------------------
414 # Call the specialization of MACRO for LANG with ARGS.  Complain if
415 # unavailable.
416 m4_define([_AT_LANG_DISPATCH],
417 [m4_ifdef([$2($1)],
418        [m4_indir([$2($1)], m4_shift2($@))],
419        [m4_fatal([$2: unknown language: $1])])])
422 # AT_LANG_DISPATCH(MACRO, ARGS)
423 # -----------------------------
424 # Call the specialization of MACRO for AT_LANG with ARGS.  Complain if
425 # unavailable.
426 m4_define([AT_LANG_DISPATCH],
427 [_AT_LANG_DISPATCH(AT_LANG, $@)])
431 # AT_DATA_SOURCE_PROLOGUE
432 # -----------------------
433 # The prologue that should be included in any source code that is
434 # meant to be compiled.  Keep atlocal.in sync (BISON_CXX_WORKS).
435 m4_define([AT_DATA_SOURCE_PROLOGUE],
436 [[/* Adjust to the compiler.
437   We used to do it here, but each time we add a new line,
438   we have to adjust all the line numbers in error messages.
439   It's simpler to use a constant include to a varying file.  */
440 #include <testsuite.h>
443 # AT_DATA_GRAMMAR_PROLOGUE
444 # ------------------------
445 # The prologue that should be included in any grammar whose parser is
446 # meant to be compiled.
447 m4_define([AT_DATA_GRAMMAR_PROLOGUE],
448 [[%code top {
449 ]AT_DATA_SOURCE_PROLOGUE[]dnl
453 # AT_DATA_SOURCE(NAME, CONTENT)
454 # -----------------------------
455 # Generate the file NAME, whose CONTENT is preceded by
456 # AT_DATA_SOURCE_PROLOGUE.
457 m4_define([AT_DATA_SOURCE],
458 [AT_DATA([$1],
459 [AT_DATA_SOURCE_PROLOGUE
460 $2])
464 # AT_DATA_GRAMMAR(NAME, CONTENT)
465 # ------------------------------
466 # Generate the file NAME, whose CONTENT is preceded by
467 # AT_DATA_GRAMMAR_PROLOGUE.
468 m4_define([AT_DATA_GRAMMAR], [AT_LANG_DISPATCH([$0], $@)])
471 # AT_YYLEX_PROTOTYPE
472 # AT_YYLEX_DECLARE_EXTERN
473 # AT_YYLEX_DECLARE
474 # AT_YYLEX_DEFINE([INPUT], [ACTION])
475 # ----------------------------------
476 # INPUT can be empty, or in double quotes, or a list (in braces).
477 # ACTION may compute yylval for instance, using "res" as token type,
478 # and "toknum" as the number of calls to yylex (starting at 0).
479 m4_define([AT_YYLEX_PROTOTYPE],
480 [AT_YYLEX_RETURN AT_NAME_PREFIX[]lex (]AT_YYLEX_FORMALS[)[]dnl
483 m4_define([AT_YYLEX_DECLARE_EXTERN],
484 [AT_YYLEX_PROTOTYPE;dnl
487 m4_define([AT_YYLEX_DECLARE],
488 [static AT_YYLEX_DECLARE_EXTERN[]dnl
492 # AT_YYLEX_DEFINE([INPUT], [ACTION])
493 m4_define([AT_YYLEX_DEFINE],  [AT_LANG_DISPATCH([$0], $@)])
496 # AT_YYERROR_FORMALS
497 # AT_YYERROR_PROTOTYPE
498 # AT_YYERROR_DECLARE_EXTERN
499 # AT_YYERROR_DECLARE
500 # AT_YYERROR_DEFINE
501 # -------------------------
502 # Must be called inside a AT_BISON_OPTION_PUSHDEFS/POPDEFS pair.
503 m4_define([AT_YYERROR_FORMALS],  [AT_LANG_DISPATCH([$0], $@)])
504 m4_define([AT_YYERROR_PROTOTYPE],[AT_LANG_DISPATCH([$0], $@)])
505 m4_define([AT_YYERROR_DECLARE_EXTERN], [AT_LANG_DISPATCH([$0], $@)])
506 m4_define([AT_YYERROR_DECLARE],  [AT_LANG_DISPATCH([$0], $@)])
507 m4_define([AT_YYERROR_DEFINE],   [AT_LANG_DISPATCH([$0], $@)])
509 # AT_MAIN_DEFINE
510 # --------------
511 m4_define([AT_MAIN_DEFINE],   [AT_LANG_DISPATCH([$0], $@)])
515 ## --- ##
516 ## C.  ##
517 ## --- ##
519 # AT_DATA_GRAMMAR(c)(NAME, CONTENT)
520 # ---------------------------------
521 # Generate the file NAME, with CONTENT.
522 m4_define([AT_DATA_GRAMMAR(c)],
523 [AT_DATA([$1],
524 [AT_DATA_GRAMMAR_PROLOGUE
525 $2])
528 m4_define([AT_YYERROR_FORMALS(c)],
529 [AT_YYERROR_ARG_LOC_IF([AT_YYLTYPE const * const llocp, ])AT_PARSE_PARAMS[const char *msg]])
531 m4_define([AT_YYERROR_PROTOTYPE(c)],
532 [[void ]AT_NAME_PREFIX[error (]AT_YYERROR_FORMALS[)]])
534 m4_define([AT_YYERROR_DECLARE_EXTERN(c)],
535 [AT_YYERROR_PROTOTYPE;])
537 m4_define([AT_YYERROR_DECLARE(c)],
538 [#include <stdio.h>
539 ]AT_LOCATION_IF([[
540 #if defined ]AT_YYLTYPE[_IS_TRIVIAL && ]AT_YYLTYPE[_IS_TRIVIAL
541 static int location_print (FILE *yyo, ]AT_YYLTYPE[ const * const yylocp);
542 # ifndef LOCATION_PRINT
543 #  define LOCATION_PRINT(File, Loc) location_print (File, &(Loc))
544 # endif
545 #endif
546 ]])[
547 static AT_YYERROR_DECLARE_EXTERN])
550 m4_define([AT_YYERROR_DEFINE(c)],
552 ]AT_LOCATION_IF([[
553 # if defined ]AT_YYLTYPE[_IS_TRIVIAL && ]AT_YYLTYPE[_IS_TRIVIAL
554 /* Print *YYLOCP on YYO. */
555 __attribute__((__unused__))
556 static int
557 location_print (FILE *yyo, ]AT_YYLTYPE[ const * const yylocp)
559   int res = 0;
560   int end_col = 0 != yylocp->last_column ? yylocp->last_column - 1 : 0;
561   if (0 <= yylocp->first_line)
562     {
563       res += fprintf (yyo, "%d", yylocp->first_line);
564       if (0 <= yylocp->first_column)
565         res += fprintf (yyo, ".%d", yylocp->first_column);
566     }
567   if (0 <= yylocp->last_line)
568     {
569       if (yylocp->first_line < yylocp->last_line)
570         {
571           res += fprintf (yyo, "-%d", yylocp->last_line);
572           if (0 <= end_col)
573             res += fprintf (yyo, ".%d", end_col);
574         }
575       else if (0 <= end_col && yylocp->first_column < end_col)
576         res += fprintf (yyo, "-%d", end_col);
577     }
578   return res;
580 #endif
581 ]])[
582 /* A C error reporting function.  */
583 static
584 ]AT_YYERROR_PROTOTYPE[
585 {]m4_bpatsubst(m4_defn([AT_PARSE_PARAMS]),
586               [[^,]+[^A-Za-z_0-9]\([A-Za-z_][A-Za-z_0-9]*\), *], [
587   YYUSE(\1);])dnl
588 AT_YYERROR_SEES_LOC_IF([[
589   LOCATION_PRINT (stderr, ]AT_LOC[);
590   fprintf (stderr, ": ");]])[
591   fprintf (stderr, "%s\n", msg);
592 }]])
595 m4_define([AT_YYLEX_DEFINE(c)],
596 [[#include <assert.h>
597 static
598 ]AT_YYLEX_PROTOTYPE[
600   ]m4_bmatch([$1], [^\(".*"\)?$],
601              [[static char const input[] = ]m4_default([$1], [""])],
602              [[static int const input[] = ]$1])[;
603   static int toknum = 0;
604   int res;
605   ]AT_USE_LEX_ARGS[
606   enum { input_elts = sizeof input / sizeof input[0] };
607   (void) input_elts;
608   assert (0 <= toknum && toknum < input_elts);
609   res = input[toknum++];
610   ]$2[;]AT_TOKEN_CTOR_IF([], [[
611   ]AT_LOCATION_IF([[
612   ]AT_LOC_FIRST_LINE[ = ]AT_LOC_LAST_LINE[ = 1;
613   ]AT_LOC_FIRST_COLUMN[ = ]AT_LOC_LAST_COLUMN[ = toknum;]])[
614   return res;]])[
615 }]])
618 m4_define([AT_MAIN_DEFINE(c)],
619 [[#include <stdlib.h> /* getenv. */
620 #include <string.h> /* strcmp. */
622 main (int argc, char const* argv[])
623 {]AT_DEBUG_IF([[
624   yydebug = !!getenv ("YYDEBUG");
625   for (int i = 1; i < argc; ++i)
626     if (!strcmp (argv[i], "-p")
627         || !strcmp (argv[i], "-d") || !strcmp (argv[i], "--debug"))
628       yydebug |= 1;
629     else if (!strcmp (argv[i], "-s") || !strcmp (argv[i], "--stat"))
630       yydebug |= 2;]], [[
631   (void) argc;
632   (void) argv;]])[
633   return ]AT_NAME_PREFIX[parse ();
634 }]])
636 m4_define([AT_LANG_FOR_EACH_STD(c)],
637 [$1])
640 ## ----- ##
641 ## C++.  ##
642 ## ----- ##
645 # AT_DATA_GRAMMAR(NAME, CONTENT)
646 # ------------------------------
647 m4_copy([AT_DATA_GRAMMAR(c)], [AT_DATA_GRAMMAR(c++)])
649 # No need to declare, it's part of the class interface.
650 m4_define([AT_YYERROR_DECLARE(c++)],        [])
651 m4_define([AT_YYERROR_DECLARE_EXTERN(c++)], [])
653 m4_define([AT_YYERROR_DEFINE(c++)],
654 [[/* A C++ error reporting function.  */
655 void
656 ]AT_NAMESPACE[::parser::error (]AT_LOCATION_IF([[const location_type& l, ]])[const std::string& m)
658   std::cerr << ]AT_LOCATION_IF([l << ": " << ])[m << '\n';
659 }]])
662 # AT_YYERROR_DEFINE(c++)([INPUT], [ACTION])
663 # -----------------------------------------
664 # Same as in C.
665 m4_copy([AT_YYLEX_DEFINE(c)], [AT_YYLEX_DEFINE(c++)])
668 m4_define([AT_MAIN_DEFINE(c++)],
669 [[#include <cstdlib> // getenv.
670 #include <cstring> // strcmp.
672 main (int argc, char const* argv[])
674   ]AT_NAMESPACE[::parser p;]AT_DEBUG_IF([[
675   int debug = !!getenv ("YYDEBUG");
676   for (int i = 1; i < argc; ++i)
677     if (!strcmp (argv[i], "-p")
678         || !strcmp (argv[i], "-d") || !strcmp (argv[i], "--debug"))
679       debug |= 1;
680     else if (!strcmp (argv[i], "-s") || !strcmp (argv[i], "--stat"))
681       debug |= 2;
682   p.set_debug_level (debug);]], [[
683   (void) argc;
684   (void) argv;]])[
685   return p.parse ();
686 }]])
688 m4_define([AT_LANG_FOR_EACH_STD(c++)],
689 [AT_FOR_EACH_CXX([$1])])
691 m4_define([AT_FOR_EACH_CXX],
692 [[at_for_each_std_CXXFLAGS_save=$CXXFLAGS
693 for at_std in '' \
694               ${CXX98_CXXFLAGS:+"$CXX98_CXXFLAGS"} \
695               ${CXX03_CXXFLAGS:+"$CXX03_CXXFLAGS"} \
696               ${CXX11_CXXFLAGS:+"$CXX11_CXXFLAGS"} \
697               ${CXX14_CXXFLAGS:+"$CXX14_CXXFLAGS"} \
698               ${CXX17_CXXFLAGS:+"$CXX17_CXXFLAGS"} \
699               ${CXX2A_CXXFLAGS:+"$CXX2A_CXXFLAGS"}
701   ]AS_ECHO(["======== Testing with C++ standard flags: '$at_cxx_std'"])[
702   CXXFLAGS="$at_for_each_std_CXXFLAGS_save $at_std"
703   ]$1[
704 done
705 CXXFLAGS=$at_for_each_std_CXXFLAGS_save
710 ## --- ##
711 ## D.  ##
712 ## --- ##
714 # AT_DATA_GRAMMAR(NAME, CONTENT)
715 # ------------------------------
716 m4_copy([AT_DATA], [AT_DATA_GRAMMAR(d)])
719 # No need to declare, it's part of the class interface.
720 m4_define([AT_YYERROR_DECLARE(d)],        [])
721 m4_define([AT_YYERROR_DECLARE_EXTERN(d)], [])
723 m4_define([AT_YYERROR_DEFINE(d)],
724 [[/* An error reporting function.  */
725 public void yyerror (]AT_LOCATION_IF([[YYLocation l, ]])[string m)
727   stderr.writeln (]AT_LOCATION_IF([[l, ": ", ]])[m);
728 }]])
731 m4_define([AT_MAIN_DEFINE(d)],
732 [[int main ()
734   auto l = new ]AT_API_prefix[Lexer ();
735   auto p = new ]AT_API_PREFIX[Parser (l);
736   return p.parse ();
737 }]])
741 ## ------ ##
742 ## Java.  ##
743 ## ------ ##
746 # AT_DATA_GRAMMAR(NAME, CONTENT)
747 # ------------------------------
748 m4_copy([AT_DATA], [AT_DATA_GRAMMAR(java)])
751 # AT_JAVA_POSITION_DEFINE
752 # -----------------------
753 m4_define([AT_JAVA_POSITION_DEFINE],
754 [[class Position {
755   public int line;
756   public int token;
758   public Position ()
759   {
760     line = 0;
761     token = 0;
762   }
764   public Position (int l, int t)
765   {
766     line = l;
767     token = t;
768   }
770   public boolean equals (Position l)
771   {
772     return l.line == line && l.token == token;
773   }
775   public String toString ()
776   {
777     return Integer.toString (line) + "." + Integer.toString(token);
778   }
780   public int lineno ()
781   {
782     return line;
783   }
785   public int token ()
786   {
787     return token;
788   }
789 }]])
792 m4_define([AT_YYERROR_DEFINE(java)],
793 [AT_LOCATION_IF([[public void yyerror (Calc.Location l, String m)
795   if (l == null)
796     System.err.println (m);
797   else
798     System.err.println (l + ": " + m);
799   }
800 ]], [[
801   public void yyerror (String m)
802   {
803     System.err.println (m);
804   }
808 m4_define([AT_YYLEX_DEFINE(java)],
810   /*--------.
811   | yylex.  |
812   `--------*/
814   public String input = "]$1[";
815   public int index = 0;
816   public int yylex ()
817   {
818     if (index < input.length ())
819       return input.charAt (index++);
820     else
821       return 0;
822   }
823   public Object getLVal ()
824   {
825     ]$2[;
826   }
829 m4_define([AT_MAIN_DEFINE(java)],
830 [[class input
832   public static void main (String args[]) throws IOException
833   {
834     ]AT_API_prefix[Parser p = new ]AT_API_prefix[Parser ();
835     System.exit (p.parse () ? 0 : 1);
836   }
837 }]])
839 m4_define([AT_LANG_FOR_EACH_STD(java)],
840 [$1])
842 ## --------------- ##
843 ## Running Bison.  ##
844 ## --------------- ##
846 # AT_BISON_CHECK(BISON_ARGS, [OTHER_AT_CHECK_ARGS])
847 # -------------------------------------------------
848 # High-level routine that may call bison several times, under different
849 # conditions.
851 # Check Bison by invoking 'bison BISON_ARGS'.  BISON_ARGS should not contain
852 # shell constructs (such as redirection or pipes) that would prevent
853 # appending additional command-line arguments for bison. OTHER_AT_CHECK_ARGS
854 # are the usual remaining arguments to AT_CHECK: STATUS, STDOUT, etc.
856 # This macro or AT_BISON_CHECK_NO_XML should always be used whenever invoking
857 # Bison in the test suite.  For now it ensures that:
859 #   1. Valgrind doesn't report reachable memory when Bison is expected to have
860 #      a non-zero exit status since Bison doesn't always try to free all memory
861 #      in that case.
863 #   2. In the case of maintainer-xml-check, XML/XSLT output is compared with
864 #      --graph and --report=all output for every working grammar.
866 #   3. If stderr contains a warning, -Werror and --warnings=error
867 #      convert the warning to an error.
869 #   4. If stderr contains a warning, -Wnone and --warnings=none suppress it.
870 m4_define([AT_BISON_CHECK],
871 [m4_null_if([$2], [AT_BISON_CHECK_XML($@)])
872 AT_BISON_CHECK_NO_XML($@)])
874 # AT_BISON_CHECK_(BISON_ARGS, [OTHER_AT_CHECK_ARGS])
875 # --------------------------------------------------
876 # Low-level macro to run bison once.
877 m4_define([AT_BISON_CHECK_],
878 [AT_CHECK(AT_SET_ENV[[ bison --color=no -fno-caret ]]$@)])
881 # AT_BISON_CHECK_WARNINGS(BISON_ARGS, [OTHER_AT_CHECK_ARGS])
882 # ----------------------------------------------------------
883 # Check that warnings (if some are expected) are correctly
884 # turned into errors with -Werror, etc.
886 # When -Wno-error is used, the rules are really different, don't try.
887 m4_define([AT_BISON_CHECK_WARNINGS],
888 [m4_if(m4_bregexp([$4], [: warning: ]), [-1], [],
889        m4_bregexp([$1], [-Wno-error=]), [-1],
890                   [m4_null_if([$2], [AT_BISON_CHECK_WARNINGS_($@)])])])
892 m4_define([AT_BISON_CHECK_WARNINGS_],
893 [[# Defining POSIXLY_CORRECT causes bison to complain if options are
894 # added after the grammar file name, so skip these checks in that
895 # case.
896 if test "$POSIXLY_CORRECT_IS_EXPORTED" = false; then
897           ]AT_SAVE_SPECIAL_FILES[
899   # To avoid expanding it repeatedly, store specified stdout.
900   ]AT_DATA([expout], [$3])[
902   # Run with -Werror.
903   ]AT_BISON_CHECK_([$1[ -Werror]], [[1]], [expout], [stderr])[
905   if test x"$PERL" != x; then
906     # Build expected stderr up to and including the "warnings being
907     # treated as errors" message.
908     ]AT_DATA([[experr]], [$4])[
909     "$PERL" -pi -e 's{(.*): warning:}{$][1: error:};' \
910                 -e 's{\[-W(.*)\]$}{@<:@-Werror=$][1@:>@}' \
911                     experr
912     ]AT_CHECK([[sed 's,.*/$,,' stderr 1>&2]], [[0]], [[]], [experr])[
914     # Now check --warnings=error.
915     cp stderr experr
916     ]AT_BISON_CHECK_([$1[ --warnings=error]], [[1]], [expout], [experr])[
917   fi
919   # Now check -Wnone and --warnings=none by making sure that
920   # -Werror doesn't change the exit status when -Wnone or
921   # --warnings=none is specified.  With traces disabled, there should
922   # be no output on stderr.
923   ]AT_BISON_CHECK_([$1[ -Wnone,none -Werror --trace=none]], [[0]], [expout])[
924   ]AT_BISON_CHECK_([$1[ --warnings=none -Werror --trace=none]], [[0]], [expout])[
926   ]AT_RESTORE_SPECIAL_FILES[
927 fi]dnl
930 # AT_BISON_CHECK_NO_XML(BISON_ARGS, [OTHER_AT_CHECK_ARGS])
931 # --------------------------------------------------------
932 # Same as AT_BISON_CHECK except don't perform XML/XSLT checks.  This is useful
933 # when a tortured grammar's XML is known to be too large for xsltproc to
934 # handle.
935 m4_define([AT_BISON_CHECK_NO_XML],
936 [AT_CHECK(AT_SET_ENV_IF([$2]) [[bison --color=no -fno-caret ]]$@)
937 AT_BISON_CHECK_WARNINGS($@)])
939 # AT_BISON_CHECK_XML(BISON_ARGS, [OTHER_AT_CHECK_ARGS])
940 # -----------------------------------------------------
941 # Run AT_BISON_CHECK's XML/XSLT checks if $BISON_TEST_XML=1 and $XSLTPROC is
942 # defined.  It doesn't make sense to invoke this macro if Bison is expected to
943 # have a non-zero exit status.
944 m4_define([AT_BISON_CHECK_XML],
945 [[if test x"$BISON_TEST_XML" = x1 && test x"$XSLTPROC" != x""; then]
946   AT_SAVE_SPECIAL_FILES
947   [mkdir xml-tests]
948   m4_pushdef([AT_BISON_ARGS],
949              [m4_bpatsubsts([[$1]],
950                             [--report(-file)?=[^][ ]*], [],
951                             [--graph=[^][ ]*], [],
952                             [--xml=[^][ ]*], [])])dnl
953   # Don't combine these Bison invocations since we want to be sure that
954   # --report=all isn't required to get the full XML file.
955   AT_BISON_CHECK_([[--report=all --report-file=xml-tests/test.output \
956              --graph=xml-tests/test.gv ]]AT_BISON_ARGS,
957            [[0]], [ignore], [ignore])
958   AT_BISON_CHECK_([[--xml=xml-tests/test.xml ]]AT_BISON_ARGS,
959            [[0]], [ignore], [ignore])
960   m4_popdef([AT_BISON_ARGS])dnl
961   [cp xml-tests/test.output expout]
962   AT_CHECK([[$XSLTPROC \
963              `]]AT_SET_ENV[[ bison --print-datadir`/xslt/xml2text.xsl \
964              xml-tests/test.xml]], [[0]], [expout])
965   [sort xml-tests/test.gv > expout]
966   AT_CHECK([[$XSLTPROC \
967              `]]AT_SET_ENV[[ bison --print-datadir`/xslt/xml2dot.xsl \
968              xml-tests/test.xml | sort]], [[0]], [expout])
969   [rm -rf xml-tests expout]
970   AT_RESTORE_SPECIAL_FILES
971 [fi]])
974 # AT_SET_ENV_IF(EXIT-STATUS)
975 # --------------------------
976 # Put this before a Bison invocation to set the environment to:
977 # - define COLUMNS to make the test suite independant of the user's
978 #   environment;
979 # - keep Valgrind from complaining about reachable memory (when
980 #   EXIT-STATUS is not 0).
982 # Do not quote invocations of this macro within the first argument of AT_CHECK.
983 # The triple quoting below will cause test cases to fail if you do.  If you do
984 # so anyway but also decrease the quoting below to avoid that problem, AT_CHECK
985 # will then fail to shell-escape its contents when attempting to print them.
986 # The testsuite verbose output, at least, will be incorrect, but nothing may
987 # fail to make sure you notice.
988 m4_define([AT_SET_ENV_IF],
989 [[[COLUMNS=1000; export COLUMNS;]] m4_null_if($1, [], [[[VALGRIND_OPTS="$VALGRIND_OPTS --leak-check=summary --show-reachable=no"; export VALGRIND_OPTS; ]]])])
992 # AT_SET_ENV
993 # ----------
994 # See above.
995 m4_define([AT_SET_ENV],
996 [AT_SET_ENV_IF([1])])
998 ## ------------------------ ##
999 ## Compiling C, C++ Files.  ##
1000 ## ------------------------ ##
1003 # AT_COMPILE(OUTPUT, [SOURCES = OUTPUT.c], [EXTRA-COMPILER-FLAGS])
1004 # ----------------------------------------------------------------
1005 # Compile SOURCES into OUTPUT.
1007 # If OUTPUT does not contain '.', assume that we are linking too,
1008 # otherwise pass "-c"; this is a hack.  The default SOURCES is OUTPUT
1009 # with trailing .o removed, and ".c" appended.
1010 m4_define([AT_COMPILE],
1011 [AT_SKIP_IF([[! $BISON_C_WORKS]])
1012 AT_CHECK(m4_join([ ],
1013                  [$CC $CFLAGS $CPPFLAGS $3],
1014                  [m4_bmatch([$1], [[.]], [-c], [$LDFLAGS])],
1015                  [-o $1],
1016                  [m4_default([$2], [m4_bpatsubst([$1], [\.o$]).c])],
1017                  [m4_bmatch([$1], [[.]], [], [$LIBS])]),
1018          0, [ignore], [ignore])])
1021 # AT_COMPILE_CXX(OUTPUT, [SOURCES = OUTPUT.cc], [EXTRA-COMPILER-FLAGS])
1022 # ---------------------------------------------------------------------
1023 # Compile SOURCES into OUTPUT.  If the C++ compiler does not work,
1024 # ignore the test.
1026 # If OUTPUT does not contain '.', assume that we are linking too,
1027 # otherwise pass "-c"; this is a hack.  The default SOURCES is OUTPUT
1028 # with trailing ".o" removed, and ".cc" appended.
1029 m4_define([AT_COMPILE_CXX],
1030 [AT_KEYWORDS(c++)
1031 AT_SKIP_IF([[! $BISON_CXX_WORKS]])
1032 AT_CHECK(m4_join([ ],
1033                  [$CXX $CXXFLAGS $CPPFLAGS $3],
1034                  [m4_bmatch([$1], [[.]], [-c], [$LDFLAGS])],
1035                  [-o $1],
1036                  [m4_default([$2], [m4_bpatsubst([$1], [\.o$]).cc])],
1037                  [m4_bmatch([$1], [[.]], [], [$LIBS])]),
1038          0, [ignore], [ignore])])
1041 # AT_COMPILE_D(OUTPUT, [SOURCES = OUTPUT.d], [EXTRA-COMPILER-FLAGS])
1042 # -------------------------------------------------------------------
1043 # Compile SOURCES into OUTPUT.  If the C++ compiler does not work,
1044 # ignore the test.
1046 # If OUTPUT does not contain '.', assume that we are linking too,
1047 # otherwise pass "-c"; this is a hack.  The default SOURCES is OUTPUT
1048 # with trailing ".o" removed, and ".cc" appended.
1049 m4_define([AT_COMPILE_D],
1050 [AT_KEYWORDS(d)
1051 AT_SKIP_IF([[! $BISON_DC_WORKS]])
1052 AT_CHECK(m4_join([ ],
1053                  [$DC $DCFLAGS $3],
1054                  [m4_bmatch([$1], [[.]], [-c])],
1055                  [-of$1],
1056                  [m4_default([$2], [m4_bpatsubst([$1], [\.o$]).d])]),
1057          0, [ignore], [ignore])])
1060 # AT_JAVA_COMPILE(SOURCES)
1061 # ------------------------
1062 # Compile SOURCES into Java class files.  Skip the test if java or javac
1063 # is not installed.
1064 m4_define([AT_JAVA_COMPILE],
1065 [AT_KEYWORDS(java)
1066 AT_SKIP_IF([[test -z "$CONF_JAVAC"]])
1067 AT_SKIP_IF([[test -z "$CONF_JAVA"]])
1068 AT_CHECK([[$SHELL ../../../javacomp.sh ]$1],
1069          [[0]], [ignore], [ignore])])
1072 # AT_LANG_FOR_EACH_STD(BODY)
1073 # --------------------------
1074 m4_define([AT_LANG_FOR_EACH_STD],  [AT_LANG_DISPATCH([$0], $@)])
1077 # AT_LANG_COMPILE(OUTPUT, [SOURCES = OUTPUT.c], [EXTRA-COMPILER-FLAGS])
1078 # ---------------------------------------------------------------------
1079 # Compile SOURCES into OUTPUT.  Skip if compiler does not work.
1081 # If OUTPUT does not contain '.', assume that we are linking too,
1082 # otherwise pass "-c"; this is a hack.  The default SOURCES is OUTPUT
1083 # with trailing .o removed, and ".c"/".cc" appended.
1084 m4_define([AT_LANG_COMPILE],  [AT_LANG_DISPATCH([$0], $@)])
1085 m4_define([AT_LANG_COMPILE(c)],    [AT_COMPILE([$1], [$2], [$3])])
1086 m4_define([AT_LANG_COMPILE(c++)],  [AT_COMPILE_CXX([$1], [$2], [$3])])
1087 m4_define([AT_LANG_COMPILE(d)],    [AT_COMPILE_D([$1], [$2], [$3])])
1088 m4_define([AT_LANG_COMPILE(java)], [AT_JAVA_COMPILE([$1.java], [$2], [$3])])
1091 # AT_LANG_EXT
1092 # -----------
1093 # The file extension corresponding to the language: c, cc, or java.
1094 m4_define([AT_LANG_EXT],  [AT_LANG_DISPATCH([$0], $@)])
1095 m4_define([AT_LANG_EXT(c)],    [c])
1096 m4_define([AT_LANG_EXT(c++)],  [cc])
1097 m4_define([AT_LANG_EXT(d)],    [d])
1098 m4_define([AT_LANG_EXT(java)], [java])
1101 # AT_LANG_HDR
1102 # -----------
1103 # The header file extension corresponding to the language: c, cc.
1104 m4_define([AT_LANG_HDR],  [AT_LANG_DISPATCH([$0], $@)])
1105 m4_define([AT_LANG_HDR(c)],    [h])
1106 m4_define([AT_LANG_HDR(c++)],  [hh])
1109 # AT_FULL_COMPILE(OUTPUT, [OTHER1], [OTHER2],
1110 #                 [$4: EXTRA-COMPILER-FLAGS, [$5: EXTRA-BISON-FLAGS])
1111 # -------------------------------------------------------------------
1112 # Compile OUTPUT.y to OUTPUT.c, OUTPUT.cc, or OUTPUT.java, and then
1113 # compile it to OUTPUT or OUTPUT.class.  If OTHER is specified, compile
1114 # OUTPUT-OTHER.c, OUTPUT-OTHER.cc, or OUTPUT-OTHER.java to OUTPUT or
1115 # OUTPUT.java along with it.  Relies on AT_CXX_IF and
1116 # AT_JAVA_IF.
1117 m4_define([AT_FULL_COMPILE],
1118 [AT_BISON_CHECK([$5 -o $1.AT_LANG_EXT $1.y])
1119  AT_LANG_COMPILE([$1],
1120                  m4_join([ ],
1121                          [$1.AT_LANG_EXT],
1122                          m4_ifval($2, [[$1-$2.]AT_LANG_EXT]),
1123                          m4_ifval($3, [[$1-$3.]AT_LANG_EXT])),
1124                  [$4])])
1128 # AT_SKIP_IF_CANNOT_LINK_C_AND_CXX
1129 # --------------------------------
1130 # Check that we can link together C and C++ objects.
1131 m4_define([AT_SKIP_IF_CANNOT_LINK_C_AND_CXX],
1132 [AT_DATA([c-and-cxx.h],
1133 [[#ifdef __cplusplus
1134 extern "C"
1136 #endif
1137   int fortytwo (void);
1138 #ifdef __cplusplus
1140 #endif
1142 AT_DATA([c-only.c],
1143 [[#include "c-and-cxx.h"
1145 main (void)
1147   return fortytwo () == 42 ? 0 : 1;
1150 AT_DATA([cxx-only.cc],
1151 [[#include "c-and-cxx.h"
1152 int fortytwo ()
1154   return 42;
1157 AT_COMPILE([c-only.o], [c-only.c])
1158 AT_COMPILE_CXX([cxx-only.o], [cxx-only.cc])
1159 AT_CHECK([$CXX $CXXFLAGS $CPPFLAGS $LDFLAGS c-only.o cxx-only.o -o c-and-cxx ||
1160           exit 77], [0], [ignore], [ignore])
1161 AT_C_PARSER_CHECK([c-and-cxx])
1165 # AT_REQUIRE_CXX_STD(STD, [IF-FAIL = SKIP])
1166 # -----------------------------------------
1167 # Skip unless this compiler supports at least C++ STD (e.g., "11",
1168 # "14", etc.).
1169 m4_define([AT_REQUIRE_CXX_STD],
1170 [AT_DATA([check.cc],
1171 [[int main ()
1173 #if !defined __cplusplus || __cplusplus < ]m4_case([$1],
1174     [98], [199711],
1175     [03], [199711],
1176     [11], [201103],
1177     [14], [201402],
1178     [17], [201703],
1179     [2a], [201709],
1180     [m4_fatal([$0: invalid arguments: $@])])[
1181   return 1;
1182 #else
1183   return 0;
1184 #endif
1187 AT_COMPILE_CXX([check])
1188 AT_CHECK([@&t@./check], [ignore])
1189 m4_ifval([$2],
1190          [if test $at_status != 0; then
1191             $2
1192           fi],
1193          [AT_SKIP_IF([test $at_status != 0])])
1197 # AT_SKIP_IF_EXCEPTION_SUPPORT_IS_POOR
1198 # ------------------------------------
1199 # Check that we can expect exceptions to be handled properly.
1200 # GCC 4.3 and 4.4 fail https://trac.macports.org/ticket/40853.
1201 m4_define([AT_SKIP_IF_EXCEPTION_SUPPORT_IS_POOR],
1202 [AT_DATA_SOURCE([exception.cc],
1203 [[#include <iostream>
1204 #include <stdexcept>
1206 void foo ()
1208   try
1209     {
1210       throw std::runtime_error ("foo");
1211     }
1212   catch (...)
1213     {
1214       std::cerr << "Inner caught\n";
1215       throw;
1216     }
1219 int main ()
1221   try
1222     {
1223       foo ();
1224     }
1225   catch (...)
1226     {
1227       std::cerr << "Outer caught\n";
1228       return 0;
1229     }
1230   return 1;
1233 AT_COMPILE_CXX([exception])
1234 # The "empty" quadrigraph is to protect from cfg.mk's
1235 # sc_at_parser_check.
1236 AT_CHECK([@&t@./exception || exit 77], [0], [], [ignore])
1240 ## ---------------------------- ##
1241 ## Running a generated parser.  ##
1242 ## ---------------------------- ##
1245 # AT_C_PARSER_CHECK(COMMAND, EXIT-STATUS, EXPOUT, EXPERR, [PRE])
1246 # --------------------------------------------------------------
1247 # So that we can run './testsuite PREPARSER='valgrind -q' for instance.
1249 # Get rid of spurious messages when compiled with --coverage:
1250 # +profiling:/[...]/lib/fprintf.gcda:Merge mismatch for summaries
1251 m4_define([AT_C_PARSER_CHECK],
1252 [AT_CHECK([$5 $PREPARSER ./$1], [$2], [$3], [stderr])
1253 AT_CHECK([sed >&2 -e '/^profiling:.*:Merge mismatch for summaries/d' stderr],
1254          [0], [], [$4])
1258 # AT_JAVA_PARSER_CHECK(COMMAND, EXIT-STATUS, EXPOUT, EXPERR, [PRE])
1259 # -----------------------------------------------------------------
1260 m4_define([AT_JAVA_PARSER_CHECK],
1261 [AT_CHECK([$5[ $SHELL ../../../javaexec.sh ]$1], [$2], [$3], [$4])])
1264 # AT_PARSER_CHECK(COMMAND, EXIT-STATUS, EXPOUT, EXPERR, [PRE])
1265 # ------------------------------------------------------------
1266 # If we know we are in Java (via AT_BISON_OPTION_PUSHDEFS/POPDEFS),
1267 # run AT_JAVA_PARSER_CHECK, otherwise AT_C_PARSER_CHECK.
1268 m4_define([AT_PARSER_CHECK],
1269 [m4_ifdef([AT_JAVA_IF],
1270           [AT_JAVA_IF([AT_JAVA_PARSER_CHECK($@)],
1271                       [AT_C_PARSER_CHECK($@)])],
1272           [AT_C_PARSER_CHECK($@)])])
1275 # AT_TEST_TABLES_AND_PARSE(TITLE, COND-VALUE, TEST-SPEC,
1276 #                          DECLS, GRAMMAR, INPUT,
1277 #                          BISON-STDERR, TABLES-OR-LAST-STATE,
1278 #                          [OTHER-CHECKS],
1279 #                          [PARSER-EXIT-VALUE],
1280 #                          [PARSER-STDOUT], [PARSER-STDERR])
1281 # -------------------------------------------------------------
1282 # Using TITLE as the test group title, check the generated parser tables
1283 # and parser for a specified grammar file under a condition labeled by
1284 # COND-VALUE.
1286 # TEST-SPEC is a comma-delimited list of attributes of this test.  Each
1287 # recognized attribute is described below where it is relevant.
1289 # Insert DECLS and GRAMMAR into the declarations and grammar section of
1290 # the grammar file.  Insert basic yyerror, yylex, and main function
1291 # definitions as well.  Hardcode yylex to return the (possibly empty)
1292 # comma-delimited series of tokens in INPUT followed by token 0.
1294 # If TEST-SPEC contains the attribute no-xml, then invoke bison using
1295 # AT_BISON_CHECK_NO_XML.  Otherwise, invoke bison using AT_BISON_CHECK.
1296 # On the bison command-line, specify `--report=all --defines'.  Check
1297 # that Bison exits with value 0, has no stdout, and has stderr
1298 # BISON-STDERR.
1300 # If TEST-SPEC contains the attribute 'last-state', check that the value
1301 # of TABLES-OR-LAST-STATE is the index of the last state generated for
1302 # the grammar; in other words, check the number of states (minus one).
1303 # Otherwise, check that everything in the '.output' file starting with
1304 # the definition of state 0 is the same as the entire value of
1305 # TABLES-OR-LAST-STATE.
1307 # Expand the M4 in OTHER-CHECKS to perform additional checks of the
1308 # '.output' file, which is named 'input.output', and/or grammar file,
1309 # which is named 'input.y'.
1311 # Finally, compile the generated parser and then run it using
1312 # AT_PARSER_CHECK with PARSER-EXIT-VALUE, PARSER-STDOUT, and
1313 # PARSER-STDERR as the 2nd-4th arguments.
1315 # As a precondition, you must properly double-quote all arguments that
1316 # are to be interpreted as strings.
1318 # AT_COND_CASE (when appearing in single-quoted segments of arguments)
1319 # invokes m4_case with its own arguments but COND-VALUE inserted as the
1320 # first argument.  This is useful, for example, when wrapping multiple
1321 # AT_TEST_TABLES_AND_PARSE invocations, each representing a different
1322 # condition, in another macro.
1324 # For example:
1326 #   # AT_TEST_SYNTAX_ERROR(DESCRIPTION, DECLS, GRAMMAR, INPUT, LAST-STATE,
1327 #   #                      PARSER-EXIT-VALUE, PARSER-STDOUT, PARSER-STDERR)
1328 #   # ---------------------------------------------------------------------
1329 #   m4_define([AT_TEST_SYNTAX_ERROR],
1330 #   [
1331 #     AT_TEST_TABLES_AND_PARSE([$1[ with %define parse.error verbose]], [[verbose]],
1332 #                              [[last-state]],
1333 #                              [[%define parse.error verbose ]$2], [$3], [$4],
1334 #                              [[]], [$5], [], [$6], [$7], [$8])
1335 #     AT_TEST_TABLES_AND_PARSE([$1[ with no %define parse.error verbose]], [[no verbose]],
1336 #                              [[last-state]],
1337 #                              [$2], [$3], [$4],
1338 #                              [[]], [$5], [], [$6], [$7], [$8])
1339 #   ])
1341 #   AT_TEST_SYNTAX_ERROR([[Single Char Grammar]],
1342 #                        [[%token 'b']], [[start: 'a' ;]], [['a', 'b']],
1343 #                        [[3]],
1344 #                        [[1]], [[]],
1345 #                        [AT_COND_CASE([[no verbose]],
1346 #                                      [[syntax error
1347 #   ]],
1348 #                                      [[syntax error, unexpected 'b', expecting $end
1349 #   ]])])
1350 m4_define([AT_TEST_TABLES_AND_PARSE],
1351 [m4_pushdef([AT_COND_CASE], [m4_case([$2], $][@)])
1353 AT_SETUP([$1])
1354 AT_BISON_OPTION_PUSHDEFS([$4])
1355 AT_DATA_GRAMMAR([[input.y]],
1356 [[%code {
1357   ]AT_YYERROR_DECLARE[
1358   ]AT_YYLEX_DECLARE[
1361 ]$4[
1365 ]$5[
1368 ]AT_YYERROR_DEFINE[
1369 static int
1370 yylex (void)
1372   static int const input[] = {
1373     ]m4_if([$6], [], [], [$6], [[]], [], [$6[, ]])[0
1374   };
1375   static int const *inputp = input;
1376   return *inputp++;
1379 ]AT_MAIN_DEFINE[
1382 # In some versions of Autoconf, AT_CHECK invokes AS_ESCAPE before
1383 # expanding macros, so it corrupts some special characters in the
1384 # macros.  To avoid this, expand now and pass it the result with proper
1385 # string quotation.  Assume args 7 through 12 expand to properly quoted
1386 # strings.
1388 m4_if(m4_index(m4_quote($3), [no-xml]), -1,
1389       [AT_BISON_CHECK],
1390       [AT_BISON_CHECK_NO_XML])([[-Wall --report=all --defines -o input.c input.y]],
1391                                [0], [], m4_dquote($7))
1393 m4_if(m4_index(m4_quote($3), [last-state]), -1,
1394       [AT_CHECK([[sed -n '/^State 0$/,$p' input.output]], [[0]],
1395                 m4_dquote($8))],
1396       [AT_CHECK([[sed -n 's/^State //p' input.output | tail -1]], [[0]],
1397                 m4_dquote($8)[[
1398 ]])])
1402 # Canonical LR generates very large tables, resulting in very long
1403 # files with #line directives that may overflow what the standards
1404 # (C90 and C++98) guarantee: 32767.  In that case, GCC's -pedantic
1405 # will issue an error.
1407 # There is no "" around `wc` since some indent the result.
1408 m4_bmatch([$4], [%define lr.type canonical-lr],
1409 [if test 32767 -lt `wc -l < input.c`; then
1410   CFLAGS=`echo " $CFLAGS " | sed -e 's/ -pedantic / /'`
1411   CXXFLAGS=`echo " $CXXFLAGS " | sed -e 's/ -pedantic / /'`
1412 fi])
1413 AT_COMPILE([[input]])
1415 AT_PARSER_CHECK([[input]],
1416                 m4_ifval([$10], [m4_dquote($10)]),
1417                 m4_ifval([$11], [m4_dquote($11)]),
1418                 m4_ifval([$12], [m4_dquote($12)]))
1420 AT_BISON_OPTION_POPDEFS
1421 AT_CLEANUP
1423 m4_popdef([AT_COND_CASE])])
1427 # AT_SETS_CHECK(INPUT, SETS, EXPECTED)
1428 # ------------------------------------
1429 # Extract the information about the grammar sets from a bison
1430 # trace output (INPUT), and compare to EXPECTED.
1432 # And remember, there is no alternation in portable sed.
1433 m4_define([AT_SETS_CHECK],
1434 [AT_DATA([extract.sed],
1435 [[#n
1436 ]m4_foreach([m4_Set], [$2], [[
1437 /^]m4_Set[/ {
1438    :]m4_Set[
1439    p
1440    n
1441    /^ *$/ !b ]m4_Set[
1443 ]])])
1445 AT_CHECK([sed -f extract.sed $1], 0, [$3])
1451 ## ----------------------- ##
1452 ## Launch the test suite.  ##
1453 ## ----------------------- ##
1455 AT_INIT
1457 # AT_SETUP([TITLE])
1458 # -----------------
1459 # Redefine AT_SETUP to be more concise.  Must be done after AT_INIT.
1460 m4_copy_force([AT_SETUP], [B4_SETUP])
1461 m4_define([AT_SETUP],
1462 [B4_SETUP(m4_expand([AT_SETUP_STRIP([[$1]])]))])
1464 # Cannot assign CC and CFLAGS here, since atlocal is loaded after
1465 # options are processed, so we don't know the value of CXX and
1466 # CXXFLAGS yet.
1468 # Note that it also means that command line values for CXX and
1469 # CXXFLAGS will not be propagated to CC and CFLAGS.
1470 AT_ARG_OPTION([compile-c-with-cxx],
1471               [compile C parsers with the C++ compiler])
1473 AT_COLOR_TESTS
1475 AT_TESTED([bison])