1# Process this -*- Autotest -*- file with autom4te.
2
3# Macros for the GNU Bison Test suite.
4
5# Copyright (C) 2003-2012 Free Software Foundation, Inc.
6
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.
11#
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.
16#
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/>.
19
20m4_version_prereq([2.58])
21
22
23# m4_null_if(VAL, IF-TRUE, IF-FALSE)
24# ----------------------------------
25# If VAL evaluates to empty or 0, run IF-TRUE, otherwise IF-FALSE.
26m4_define([m4_null_if],
27[m4_case(m4_quote($1),
28         [0], [$2],
29         [],  [$2],
30         [$3])])
31
32## ------------- ##
33## Basic tests.  ##
34## ------------- ##
35
36# AT_MATCHES_CHECK(FILE, PERL-REGEXP, COUNT)
37# ------------------------------------------
38# Expect COUNT matches of the PERL-REGEXP in FILE.  The file is
39# taken in "slurp" mode, i.e., one can match end-of-lines.
40m4_define([AT_MATCHES_CHECK],
41[AT_CHECK([$PERL -0777 -ne '
42my $count = 0;
43s{$2}{ ++$count; "" }gem;
44printf "$count\n";' $1], [0], [$3
45])])
46
47
48# AT_SAVE_SPECIAL_FILES / AT_RESTORE_SPECIAL_FILES
49# ------------------------------------------------
50# Don't interfere with caller's files.
51m4_divert_text([PREPARE_TESTS],
52[at_save_special_files ()
53{
54  for at_save_file in stderr experr expout
55  do
56    test ! -f at-bison-check-$at_save_file.bak ||
57      as_fn_error 1 "fatal error: back-up on top of a back-up"
58    test ! -f $at_save_file || mv $at_save_file at-bison-check-$at_save_file.bak
59  done
60}
61
62at_restore_special_files ()
63{
64  for at_save_file in stderr experr expout
65  do
66    test ! -f at-bison-check-$at_save_file.bak ||
67      mv at-bison-check-$at_save_file.bak $at_save_file
68  done
69}
70])
71
72m4_define([AT_SAVE_SPECIAL_FILES],    [at_save_special_files])
73m4_define([AT_RESTORE_SPECIAL_FILES], [at_restore_special_files])
74
75
76
77## ------------------------------- ##
78## Macros decoding Bison options.  ##
79## ------------------------------- ##
80
81# AT_LOC_PUSHDEF(FIRST-LINE, FIRST-COLUMN, LAST-LINE, LAST-COLUMN)
82# ----------------------------------------------------------------
83# Pushdef AT_LOC_(FIRST|LAST)_(LINE|COLUMN).
84m4_define([AT_LOC_PUSHDEF],
85[m4_pushdef([AT_LOC_FIRST_LINE],  [AT_LOC.$1])
86m4_pushdef([AT_LOC_FIRST_COLUMN], [AT_LOC.$2])
87m4_pushdef([AT_LOC_LAST_LINE],    [AT_LOC.$3])
88m4_pushdef([AT_LOC_LAST_COLUMN],  [AT_LOC.$4])])
89
90# AT_LOC_POPDEF
91# -------------
92# Popdef AT_LOC_(FIRST|LAST)_(LINE|COL).
93m4_define([AT_LOC_POPDEF],
94[m4_popdef([AT_LOC_FIRST_LINE])
95m4_popdef([AT_LOC_FIRST_COLUMN])
96m4_popdef([AT_LOC_LAST_LINE])
97m4_popdef([AT_LOC_LAST_COLUMN])])
98
99
100
101# AT_BISON_OPTION_PUSHDEFS([BISON-OPTIONS])
102# -----------------------------------------
103m4_define([AT_BISON_OPTION_PUSHDEFS],
104[m4_divert_text([KILL],
105                [_AT_BISON_OPTION_PUSHDEFS($[1], $[2], [$1])])])
106
107
108# _AT_BISON_OPTION_PUSHDEFS($1, $2, [BISON-OPTIONS])
109# --------------------------------------------------
110# This macro works around the impossibility to define macros
111# inside macros, because issuing `[$1]' is not possible in M4 :(.
112# This sucks hard, GNU M4 should really provide M5 like $$1.
113m4_define([_AT_BISON_OPTION_PUSHDEFS],
114[m4_if([$1$2], $[1]$[2], [],
115       [m4_fatal([$0: Invalid arguments: $@])])dnl
116m4_pushdef([AT_DEFINES_IF],
117[m4_bmatch([$3], [%defines], [$1], [$2])])
118m4_pushdef([AT_SKEL_CC_IF],
119[m4_bmatch([$3], [%language "[Cc]\+\+"\|%skeleton "[a-z0-9]+\.cc"], [$1], [$2])])
120m4_pushdef([AT_SKEL_JAVA_IF],
121[m4_bmatch([$3], [%language "[Jj][Aa][Vv][Aa]"\|%skeleton "[a-z0-9]+\.java"], [$1], [$2])])
122# The target language: "c", "c++", or "java".
123m4_pushdef([AT_LANG],
124[AT_SKEL_JAVA_IF([java],
125                 [AT_SKEL_CC_IF([c++],
126                                [c])])])
127m4_pushdef([AT_GLR_IF],
128[m4_bmatch([$3], [%glr-parser\|%skeleton "glr\..*"], [$1], [$2])])
129m4_pushdef([AT_LALR1_CC_IF],
130[AT_SKEL_CC_IF([AT_GLR_IF([$2], [$1])], [$2])])
131m4_pushdef([AT_GLR_CC_IF],
132[AT_SKEL_CC_IF([AT_GLR_IF([$1], [$2])], [$2])])
133# Using yacc.c?
134m4_pushdef([AT_YACC_IF],
135[m4_bmatch([$3], [%language\|%glr-parser\|%skeleton], [$2], [$1])])
136m4_pushdef([AT_LEXPARAM_IF],
137[m4_bmatch([$3], [%lex-param], [$1], [$2])])
138m4_pushdef([AT_LOCATION_IF],
139[m4_bmatch([$3], [%locations], [$1], [$2])])
140m4_pushdef([AT_LOCATION_TYPE_IF],
141[m4_bmatch([$3], [%define \(api\.location\.type\|location_type\)], [$1], [$2])])
142m4_pushdef([AT_PARAM_IF],
143[m4_bmatch([$3], [%parse-param], [$1], [$2])])
144# Comma-terminated list of formals parse-parameters.
145# E.g., %parse-param { int x } {int y} -> "int x, int y, ".
146m4_pushdef([AT_PARSE_PARAMS])
147m4_bpatsubst([$3], [%parse-param { *\([^{}]*[^{} ]\) *}],
148             [m4_append([AT_PARSE_PARAMS], [\1, ])])
149
150m4_pushdef([AT_PURE_IF],
151[m4_bmatch([$3], [%define  *api\.pure\|%pure-parser],
152           [m4_bmatch([$3], [%define  *api\.pure *"?false"?], [$2], [$1])],
153           [$2])])
154m4_pushdef([AT_NAME_PREFIX],
155[m4_bmatch([$3], [\(%define api\.prefix\|%name-prefix\) ".*"],
156           [m4_bregexp([$3], [\(%define api\.prefix\|%name-prefix\) "\([^""]*\)"], [\2])],
157           [yy])])
158m4_pushdef([AT_API_prefix],
159[m4_bmatch([$3], [%define api\.prefix ".*"],
160           [m4_bregexp([$3], [%define api\.prefix "\([^""]*\)"], [\1])],
161           [yy])])
162m4_pushdef([AT_API_PREFIX],
163[m4_toupper(AT_API_prefix)])
164# yyerror receives the location if %location, and if the parser is pure. For
165# historical reasons, with the "yacc.c" skeleton, the location is not passed
166# unless an additional "%parse-param" is present, or if the purity is defined
167# as "full".
168m4_pushdef([AT_YYERROR_ARG_LOC_IF],
169[AT_LOCATION_IF([AT_PURE_IF([m4_bmatch([$3],
170                                       m4_quote(m4_join([\|],
171                                                        [%define api.pure "?full"?],
172                                                        [%glr-parser],
173                                                        [%parse-param],
174                                                        [%skeleton "?glr.c"?])),
175                                       [$1], [$2])],
176                            [$2])],
177                [$2])])
178
179# yyerror always sees the locations (when activated) if the parser is impure.
180# When the parser is pure, yyerror sees the location if it is received as an
181# argument.
182m4_pushdef([AT_YYERROR_SEES_LOC_IF],
183[AT_LOCATION_IF([AT_YACC_IF([AT_PURE_IF([AT_YYERROR_ARG_LOC_IF([$1], [$2])],
184                                        [$1])],
185                            [$1])],
186                [$2])])
187
188# The interface is pure: either because %define api.pure, or because we
189# are using the C++ parsers.
190m4_pushdef([AT_PURE_LEX_IF],
191[AT_PURE_IF([$1],
192	    [AT_SKEL_CC_IF([$1], [$2])])])
193
194m4_pushdef([AT_YYSTYPE],
195[AT_SKEL_CC_IF([AT_NAME_PREFIX[::parser::semantic_type]],
196               [AT_API_PREFIX[STYPE]])])
197m4_pushdef([AT_YYLTYPE],
198[AT_SKEL_CC_IF([AT_NAME_PREFIX[::parser::location_type]],
199               [AT_API_PREFIX[LTYPE]])])
200
201
202AT_PURE_LEX_IF(
203[m4_pushdef([AT_LOC], [(*llocp)])
204 m4_pushdef([AT_VAL], [(*lvalp)])
205 m4_pushdef([AT_YYLEX_FORMALS],
206	    [AT_YYSTYPE *lvalp[]AT_LOCATION_IF([, AT_YYLTYPE *llocp])])
207 m4_pushdef([AT_YYLEX_ARGS],
208	    [lvalp[]AT_LOCATION_IF([, llocp])])
209 m4_pushdef([AT_USE_LEX_ARGS],
210	    [(void) lvalp;AT_LOCATION_IF([(void) llocp])])
211 m4_pushdef([AT_YYLEX_PRE_FORMALS],
212	    [AT_YYLEX_FORMALS, ])
213 m4_pushdef([AT_YYLEX_PRE_ARGS],
214	    [AT_YYLEX_ARGS, ])
215],
216[m4_pushdef([AT_LOC], [[(]AT_NAME_PREFIX[lloc)]])
217 m4_pushdef([AT_VAL], [[(]AT_NAME_PREFIX[lval)]])
218 m4_pushdef([AT_YYLEX_FORMALS],     [void])
219 m4_pushdef([AT_YYLEX_ARGS],        [])
220 m4_pushdef([AT_USE_LEX_ARGS],    [])
221 m4_pushdef([AT_YYLEX_PRE_FORMALS], [])
222 m4_pushdef([AT_YYLEX_PRE_ARGS],    [])
223])
224
225# Handle the different types of location components.
226
227AT_SKEL_CC_IF(
228  [AT_LOCATION_TYPE_IF(
229    [AT_LOC_PUSHDEF([first.l], [first.c], [last.l], [last.c])],
230    [AT_LOC_PUSHDEF([begin.line], [begin.column], [end.line], [end.column])])],
231  [AT_LOC_PUSHDEF([first_line], [first_column], [last_line], [last_column])])
232
233])# _AT_BISON_OPTION_PUSHDEFS
234
235
236# AT_BISON_OPTION_POPDEFS
237# -----------------------
238m4_define([AT_BISON_OPTION_POPDEFS],
239[m4_divert_text([KILL],
240[m4_popdef([AT_YYLEX_PRE_ARGS])
241m4_popdef([AT_YYLEX_PRE_FORMALS])
242m4_popdef([AT_USE_LEX_ARGS])
243m4_popdef([AT_YYLEX_ARGS])
244m4_popdef([AT_YYLEX_FORMALS])
245m4_popdef([AT_YYLTYPE])
246m4_popdef([AT_YYSTYPE])
247m4_popdef([AT_VAL])
248m4_popdef([AT_LOC])
249m4_popdef([AT_PURE_LEX_IF])
250m4_popdef([AT_YYERROR_SEES_LOC_IF])
251m4_popdef([AT_YYERROR_ARG_LOC_IF])
252m4_popdef([AT_API_PREFIX])
253m4_popdef([AT_API_prefix])
254m4_popdef([AT_NAME_PREFIX])
255m4_popdef([AT_LOCATION_TYPE_IF])
256m4_popdef([AT_LOCATION_IF])
257m4_popdef([AT_PARSE_PARAMS])
258m4_popdef([AT_PARAM_IF])
259m4_popdef([AT_LEXPARAM_IF])
260m4_popdef([AT_YACC_IF])
261m4_popdef([AT_GLR_IF])
262m4_popdef([AT_SKEL_CC_IF])
263m4_popdef([AT_LANG])
264m4_popdef([AT_SKEL_JAVA_IF])
265m4_popdef([AT_GLR_CC_IF])
266m4_popdef([AT_LALR1_CC_IF])
267m4_popdef([AT_DEFINES_IF])
268AT_LOC_POPDEF])dnl
269])# AT_BISON_OPTION_POPDEFS
270
271
272
273## -------------------------- ##
274## Generating Grammar Files.  ##
275## -------------------------- ##
276
277# AT_DATA_SOURCE_PROLOGUE
278# ------------------------
279# The prologue that should be included in any source code that is
280# meant to be compiled.
281m4_define([AT_DATA_SOURCE_PROLOGUE],
282[[#include <config.h>
283/* We don't need perfect functions for these tests. */
284#undef malloc
285#undef memcmp
286#undef realloc
287]])
288
289# AT_DATA_GRAMMAR_PROLOGUE
290# ------------------------
291# The prologue that should be included in any grammar which parser is
292# meant to be compiled.
293m4_define([AT_DATA_GRAMMAR_PROLOGUE],
294[[%code top {
295]AT_DATA_SOURCE_PROLOGUE[]dnl
296[}
297]])
298
299# AT_DATA_SOURCE(NAME, CONTENT)
300# -----------------------------
301# Generate the file NAME, which CONTENT is preceded by
302# AT_DATA_SOURCE_PROLOGUE.
303m4_define([AT_DATA_SOURCE],
304[AT_DATA([$1],
305[AT_DATA_SOURCE_PROLOGUE
306$2])
307])
308
309# AT_DATA_GRAMMAR(NAME, CONTENT)
310# ------------------------------
311# Generate the file NAME, which CONTENT is preceded by
312# AT_DATA_GRAMMAR_PROLOGUE.
313m4_define([AT_DATA_GRAMMAR],
314[AT_DATA([$1],
315[AT_DATA_GRAMMAR_PROLOGUE
316$2])
317])
318
319# AT_YYLEX_PROTOTYPE
320# AT_YYLEX_DECLARE_EXTERN
321# AT_YYLEX_DECLARE
322# AT_YYLEX_DEFINE([INPUT], [ACTION])
323# ----------------------------------
324# INPUT can be empty, or in double quotes, or a list (in braces).
325# ACTION may compute yylval for instance, using "res" as token type,
326# and "toknum" as the number of calls to yylex (starting at 0).
327m4_define([AT_YYLEX_PROTOTYPE],
328[int AT_NAME_PREFIX[]lex (]AT_YYLEX_FORMALS[)[]dnl
329])
330
331m4_define([AT_YYLEX_DECLARE_EXTERN],
332[AT_YYLEX_PROTOTYPE;dnl
333])
334
335m4_define([AT_YYLEX_DECLARE],
336[static AT_YYLEX_DECLARE_EXTERN[]dnl
337])
338
339m4_define([AT_YYLEX_DEFINE],
340[[#include <assert.h>
341static
342]AT_YYLEX_PROTOTYPE[
343{
344  ]m4_bmatch([$1], [^\(".*"\)?$],
345             [[static char const input[] = ]m4_default([$1], [""])],
346             [[static int const input[] = ]$1])[;
347  static size_t toknum = 0;
348  int res;
349  ]AT_USE_LEX_ARGS[;
350  assert (toknum < sizeof input / sizeof input[0]);
351  res = input[toknum++];
352  ]$2[;]AT_LOCATION_IF([[
353  ]AT_LOC_FIRST_LINE[ = ]AT_LOC_LAST_LINE[ = 1;
354  ]AT_LOC_FIRST_COLUMN[ = ]AT_LOC_LAST_COLUMN[ = toknum;]])[
355  return res;
356}]dnl
357])
358
359# AT_YYERROR_FORMALS
360# AT_YYERROR_PROTOTYPE
361# AT_YYERROR_DECLARE_EXTERN
362# AT_YYERROR_DECLARE
363# AT_YYERROR_DEFINE
364# -------------------------
365# Must be called inside a AT_BISON_OPTION_PUSHDEFS/POPDEFS pair.
366m4_define([AT_YYERROR_FORMALS],
367[m4_case(AT_LANG,
368[c], [AT_YYERROR_ARG_LOC_IF([AT_YYLTYPE const * const llocp, ])AT_PARSE_PARAMS [const char *msg]])[]dnl
369])
370
371m4_define([AT_YYERROR_PROTOTYPE],
372[m4_case(AT_LANG,
373[c], [[void ]AT_NAME_PREFIX[error (]AT_YYERROR_FORMALS[)]])[]dnl
374])
375
376m4_define([AT_YYERROR_DECLARE_EXTERN],
377[m4_case(AT_LANG,
378[c], [AT_YYERROR_PROTOTYPE;])[]dnl
379])
380
381m4_define([AT_YYERROR_DECLARE],
382[m4_case(AT_LANG,
383[c], [static AT_YYERROR_DECLARE_EXTERN])[]dnl
384])
385
386m4_define([AT_YYERROR_DEFINE],
387[m4_case(AT_LANG,
388[c], [[#include <stdio.h>
389/* A C error reporting function.  */
390static
391]AT_YYERROR_PROTOTYPE[
392{]m4_bpatsubst(m4_defn([AT_PARSE_PARAMS]),
393              [[^,]+[^A-Za-z_0-9]\([A-Za-z_][A-Za-z_0-9]*\), *], [
394  YYUSE(\1);])dnl
395AT_YYERROR_SEES_LOC_IF([[
396  YY_LOCATION_PRINT (stderr, ]AT_LOC[);
397  fprintf (stderr, ": ");]])[
398  fprintf (stderr, "%s\n", msg);
399}]],
400[c++], [[/* A C++ error reporting function.  */
401void
402]AT_NAME_PREFIX[::parser::error (const location_type& l, const std::string& m)
403{
404  (void) l;
405  std::cerr << ]AT_LOCATION_IF([l << ": " << ])[m << std::endl;
406}]],
407[java], [AT_LOCATION_IF([[public void yyerror (Calc.Location l, String s)
408  {
409    if (l == null)
410      System.err.println (s);
411    else
412      System.err.println (l + ": " + s);
413  }
414]], [[
415  public void yyerror (String s)
416  {
417    System.err.println (s);
418  }]])],
419[m4_fatal([$0: invalid language: ]AT_LANG)])dnl
420])
421
422
423## --------------- ##
424## Running Bison.  ##
425## --------------- ##
426
427# AT_BISON_CHECK(BISON_ARGS, [OTHER_AT_CHECK_ARGS])
428# -------------------------------------------------
429# High-level routine that may call bison several times, under different
430# conditions.
431#
432# Check Bison by invoking `bison BISON_ARGS'.  BISON_ARGS should not contain
433# shell constructs (such as redirection or pipes) that would prevent
434# appending additional command-line arguments for bison. OTHER_AT_CHECK_ARGS
435# are the usual remaining arguments to AT_CHECK: STATUS, STDOUT, etc.
436#
437# This macro or AT_BISON_CHECK_NO_XML should always be used whenever invoking
438# Bison in the test suite.  For now it ensures that:
439#
440#   1. Valgrind doesn't report reachable memory when Bison is expected to have
441#      a non-zero exit status since Bison doesn't always try to free all memory
442#      in that case.
443#
444#   2. In the case of maintainer-xml-check, XML/XSLT output is compared with
445#      --graph and --report=all output for every working grammar.
446#
447#   3. If stderr contains a warning, -Werror and --warnings=error
448#      convert the warning to an error.
449#
450#   4. If stderr contains a warning, -Wnone and --warnings=none suppress it.
451m4_define([AT_BISON_CHECK],
452[m4_null_if([$2], [AT_BISON_CHECK_XML($@)])
453AT_BISON_CHECK_NO_XML($@)])
454
455m4_define([AT_BISON_WERROR_MSG],
456          [[bison: warnings being treated as errors]])
457
458
459# AT_BISON_CHECK_(BISON_ARGS, [OTHER_AT_CHECK_ARGS])
460# --------------------------------------------------
461# Low-level macro to run bison once.
462m4_define([AT_BISON_CHECK_],
463[AT_CHECK(AT_QUELL_VALGRIND[[ bison ]]$@)])
464
465
466# AT_BISON_CHECK_WARNINGS(BISON_ARGS, [OTHER_AT_CHECK_ARGS])
467# ----------------------------------------------------------
468# Check that warnings (if some are expected) are correctly
469# turned into errors with -Werror, etc.
470m4_define([AT_BISON_CHECK_WARNINGS],
471[m4_if(m4_bregexp([$4], [: warning: ]), [-1], [],
472      [m4_null_if([$2], [AT_BISON_CHECK_WARNINGS_($@)])])])
473
474m4_define([AT_BISON_CHECK_WARNINGS_],
475[[# Defining POSIXLY_CORRECT causes bison to complain if options are
476# added after the grammar file name, so skip these checks in that
477# case.
478if test "$POSIXLY_CORRECT_IS_EXPORTED" = false; then
479  ]AT_SAVE_SPECIAL_FILES[
480
481  # To avoid expanding it repeatedly, store specified stdout.
482  ]AT_DATA([expout], [$3])[
483
484  # Run with -Werror.
485  ]AT_BISON_CHECK_([$1[ -Werror]], [[1]], [expout], [stderr])[
486
487  # Build expected stderr up to and including the "warnings being
488  # treated as errors" message.
489  ]AT_DATA([[at-bison-check-warnings]], [$4])[
490  at_bison_check_first=`sed -n \
491    '/: warning: /{=;q;}' at-bison-check-warnings`
492  : ${at_bison_check_first:=1}
493  at_bison_check_first_tmp=`sed -n \
494    '/conflicts: [0-9].*reduce$/{=;q;}' at-bison-check-warnings`
495  : ${at_bison_check_first_tmp:=1}
496  if test $at_bison_check_first_tmp -lt $at_bison_check_first; then
497    at_bison_check_first=$at_bison_check_first_tmp
498  fi
499  if test $at_bison_check_first -gt 1; then
500    sed -n "1,`expr $at_bison_check_first - 1`"p \
501      at-bison-check-warnings > experr
502  fi
503  echo ']AT_BISON_WERROR_MSG[' >> experr
504
505  # Finish building expected stderr and check.  Unlike warnings,
506  # complaints cause bison to exit early.  Thus, with -Werror, bison
507  # does not necessarily report all warnings that it does without
508  # -Werror, but it at least reports one.
509  at_bison_check_last=`sed -n '$=' stderr`
510  : ${at_bison_check_last:=1}
511  at_bison_check_last=`expr $at_bison_check_last - 1`
512  sed -n "$at_bison_check_first,$at_bison_check_last"p \
513    at-bison-check-warnings >> experr
514  ]AT_CHECK([[sed 's,.*/\(]AT_BISON_WERROR_MSG[\)$,\1,' \
515              stderr 1>&2]], [[0]], [[]], [experr])[
516
517  # Now check --warnings=error.
518  cp stderr experr
519  ]AT_BISON_CHECK_([$1[ --warnings=error]], [[1]], [expout], [experr])[
520
521  # Now check -Wnone and --warnings=none by making sure that
522  # -Werror doesn't change the exit status when -Wnone or
523  # --warnings=none is specified.
524  ]AT_BISON_CHECK_([$1[ -Wnone -Werror]], [[0]], [expout])[
525  ]AT_BISON_CHECK_([$1[ --warnings=none -Werror]], [[0]], [expout])[
526
527  ]AT_RESTORE_SPECIAL_FILES[
528fi]dnl
529])
530
531# AT_BISON_CHECK_NO_XML(BISON_ARGS, [OTHER_AT_CHECK_ARGS])
532# --------------------------------------------------------
533# Same as AT_BISON_CHECK except don't perform XML/XSLT checks.  This is useful
534# when a tortured grammar's XML is known to be too large for xsltproc to
535# handle.
536m4_define([AT_BISON_CHECK_NO_XML],
537[AT_CHECK(m4_null_if([$2], [], [AT_QUELL_VALGRIND ])[[bison ]]$@)
538AT_BISON_CHECK_WARNINGS($@)])
539
540# AT_BISON_CHECK_XML(BISON_ARGS, [OTHER_AT_CHECK_ARGS])
541# -----------------------------------------------------
542# Run AT_BISON_CHECK's XML/XSLT checks if $BISON_TEST_XML=1 and $XSLTPROC is
543# defined.  It doesn't make sense to invoke this macro if Bison is expected to
544# have a non-zero exit status.
545m4_define([AT_BISON_CHECK_XML],
546[[if test x"$BISON_TEST_XML" = x1 && test x"$XSLTPROC" != x""; then]
547  AT_SAVE_SPECIAL_FILES
548  [mkdir xml-tests]
549  m4_pushdef([AT_BISON_ARGS],
550             [m4_bpatsubsts([[$1]],
551                            [--report(-file)?=[^][ ]*], [],
552                            [--graph=[^][ ]*], [],
553                            [--xml=[^][ ]*], [])])dnl
554  # Don't combine these Bison invocations since we want to be sure that
555  # --report=all isn't required to get the full XML file.
556  AT_BISON_CHECK_([[--report=all --report-file=xml-tests/test.output \
557                  --graph=xml-tests/test.dot ]]AT_BISON_ARGS,
558                  [[0]], [ignore], [ignore])
559  AT_BISON_CHECK_([[--xml=xml-tests/test.xml ]]AT_BISON_ARGS,
560                 [[0]], [ignore], [ignore])
561  m4_popdef([AT_BISON_ARGS])dnl
562  [cp xml-tests/test.output expout]
563  AT_CHECK([[$XSLTPROC \
564             `]]AT_QUELL_VALGRIND[[ bison --print-datadir`/xslt/xml2text.xsl \
565             xml-tests/test.xml]], [[0]], [expout])
566  [sort xml-tests/test.dot > expout]
567  AT_CHECK([[$XSLTPROC \
568             `]]AT_QUELL_VALGRIND[[ bison --print-datadir`/xslt/xml2dot.xsl \
569             xml-tests/test.xml | sort]], [[0]], [expout])
570  [rm -rf xml-tests expout]
571  AT_RESTORE_SPECIAL_FILES
572[fi]])
573
574# AT_QUELL_VALGRIND
575# -----------------
576# Put this before a Bison invocation to keep Valgrind from complaining about
577# reachable memory.
578#
579# Do not quote invocations of this macro within the first argument of AT_CHECK.
580# The triple quoting below will cause test cases to fail if you do.  If you do
581# so anyway but also decrease the quoting below to avoid that problem, AT_CHECK
582# will then fail to shell-escape its contents when attempting to print them.
583# The testsuite verbose output, at least, will be incorrect, but nothing may
584# fail to make sure you notice.
585m4_define([AT_QUELL_VALGRIND],
586[[[VALGRIND_OPTS="$VALGRIND_OPTS --leak-check=summary --show-reachable=no"; export VALGRIND_OPTS;]]])
587
588
589
590## ------------------------ ##
591## Compiling C, C++ Files.  ##
592## ------------------------ ##
593
594
595# AT_COMPILE(OUTPUT, [SOURCES = OUTPUT.c])
596# ----------------------------------------
597# Compile SOURCES into OUTPUT.
598#
599# If OUTPUT does not contain '.', assume that we are linking too,
600# otherwise pass "-c"; this is a hack.  The default SOURCES is OUTPUT
601# with trailing .o removed, and ".c" appended.
602m4_define([AT_COMPILE],
603[AT_CHECK([$BISON_C_WORKS], 0, ignore, ignore)
604AT_CHECK(m4_join([ ],
605                  [$CC $CFLAGS $CPPFLAGS],
606                  [m4_bmatch([$1], [[.]], [-c], [$LDFLAGS])],
607                  [-o $1],
608                  [m4_default([$2], [m4_bpatsubst([$1], [\.o$]).c])],
609                  [m4_bmatch([$1], [[.]], [], [$LIBS])]),
610	   0, [ignore], [ignore])])
611
612# AT_COMPILE_CXX(OUTPUT, [SOURCES = OUTPUT.cc])
613# ---------------------------------------------
614# Compile SOURCES into OUTPUT.  If the C++ compiler does not work,
615# ignore the test.
616#
617# If OUTPUT does not contain '.', assume that we are linking too,
618# otherwise pass "-c"; this is a hack.  The default SOURCES is OUTPUT
619# with trailing .o removed, and ".cc" appended.
620m4_define([AT_COMPILE_CXX],
621[AT_KEYWORDS(c++)
622AT_CHECK([$BISON_CXX_WORKS], 0, ignore, ignore)
623AT_CHECK(m4_join([ ],
624                 [$CXX $CXXFLAGS $CPPFLAGS],
625                 [m4_bmatch([$1], [[.]], [-c], [$LDFLAGS])],
626                 [-o $1],
627                 [m4_default([$2], [m4_bpatsubst([$1], [\.o$]).cc])],
628                 [m4_bmatch([$1], [[.]], [], [$LIBS])]),
629	 0, [ignore], [ignore])])
630
631# AT_JAVA_COMPILE(SOURCES)
632# ------------------------
633# Compile SOURCES into Java class files.  Skip the test if java or javac
634# is not installed.
635m4_define([AT_JAVA_COMPILE],
636[AT_KEYWORDS(java)
637AT_SKIP_IF([[test -z "$CONF_JAVAC"]])
638AT_SKIP_IF([[test -z "$CONF_JAVA"]])
639AT_CHECK([[$SHELL ../../../javacomp.sh ]$1],
640         [[0]], [ignore], [ignore])])
641
642# AT_LANG_COMPILE(OUTPUT, [SOURCES = OUTPUT.c]
643# --------------------------------------------
644# Compile SOURCES into OUTPUT.  Skip if compiler does not work.
645#
646# If OUTPUT does not contain '.', assume that we are linking too,
647# otherwise pass "-c"; this is a hack.  The default SOURCES is OUTPUT
648# with trailing .o removed, and ".c"/".cc" appended.
649m4_define([AT_LANG_COMPILE],
650[m4_case(AT_LANG,
651[c],    [AT_COMPILE([$1], [$2])],
652[c++],  [AT_COMPILE_CXX([$1], [$2])],
653[java], [AT_JAVA_COMPILE([$1.java], [$2])],
654        [m4_fatal([unknown language: ]m4_defn([AT_LANG]))])[]dnl
655])
656
657# AT_FULL_COMPILE(OUTPUT, [OTHER1], [OTHER2])
658# -------------------------------------------
659# Compile OUTPUT.y to OUTPUT.c, OUTPUT.cc, or OUTPUT.java, and then
660# compile it to OUTPUT or OUTPUT.class.  If OTHER is specified, compile
661# OUTPUT-OTHER.c, OUTPUT-OTHER.cc, or OUTPUT-OTHER.java to OUTPUT or
662# OUTPUT.java along with it.  Relies on AT_SKEL_CC_IF and
663# AT_SKEL_JAVA_IF.
664m4_define([AT_FULL_COMPILE],
665[m4_case(AT_LANG,
666[java],
667  [AT_BISON_CHECK([-o $1.java $1.y])
668   AT_LANG_COMPILE([$1],
669                   m4_join([ ],
670                           [$1.java],
671                           m4_ifval($2, [[$1-$2.java]]),
672                           m4_ifval($3, [[$1-$3.java]])))],
673[c++],
674  [AT_BISON_CHECK([-o $1.cc $1.y])
675   AT_LANG_COMPILE([$1],
676                   m4_join([ ],
677                           [$1.cc],
678                           m4_ifval($2, [[$1-$2.cc]]),
679                           m4_ifval($3, [[$1-$3.cc]])))],
680[c],
681  [AT_BISON_CHECK([-o $1.c $1.y])
682   AT_LANG_COMPILE([$1],
683                   m4_join([ ],
684                           [$1.c],
685                           m4_ifval($2, [[$1-$2.c]]),
686                           m4_ifval($3, [[$1-$3.c]])))])
687])
688
689
690
691# AT_SKIP_IF_CANNOT_LINK_C_AND_CXX
692# --------------------------------
693# Check that we can link together C and C++ objects.
694m4_define([AT_SKIP_IF_CANNOT_LINK_C_AND_CXX],
695[AT_DATA([c-and-cxx.h],
696[[#ifdef __cplusplus
697extern "C"
698{
699#endif
700  int fortytwo (void);
701#ifdef __cplusplus
702}
703#endif
704]])
705AT_DATA([c-only.c],
706[[#include "c-and-cxx.h"
707int
708main (void)
709{
710  return fortytwo () == 42 ? 0 : 1;
711}
712]])
713AT_DATA([cxx-only.cc],
714[[#include "c-and-cxx.h"
715int fortytwo ()
716{
717  return 42;
718}
719]])
720AT_COMPILE([c-only.o], [c-only.c])
721AT_COMPILE_CXX([cxx-only.o], [cxx-only.cc])
722AT_CHECK([$CXX $CXXFLAGS $CPPFLAGS $LDFLAGS c-only.o cxx-only.o -o c-and-cxx ||
723          exit 77], [ignore], [ignore])
724AT_PARSER_CHECK([./c-and-cxx])
725])
726
727
728## ---------------------------- ##
729## Running a generated parser.  ##
730## ---------------------------- ##
731
732
733# AT_PARSER_CHECK(COMMAND, EXIT-STATUS, EXPOUT, EXPERR, [PRE])
734# ------------------------------------------------------------
735# So that we can run `./testsuite PREPARSER='valgrind -q' for instance.
736#
737# Get rid of spurious messages when compiled with --coverage:
738# +profiling:/[...]/lib/fprintf.gcda:Merge mismatch for summaries
739m4_define([AT_PARSER_CHECK],
740[AT_CHECK([$5 $PREPARSER $1], [$2], [$3], [stderr])
741AT_CHECK([sed >&2 -e '/^profiling:.*:Merge mismatch for summaries/d' stderr],
742         [0], [], [$4])
743])
744
745
746# AT_JAVA_PARSER_CHECK(COMMAND, EXIT-STATUS, EXPOUT, EXPERR, [PRE])
747# -----------------------------------------------------------------
748m4_define([AT_JAVA_PARSER_CHECK],
749[AT_CHECK([$5[ $SHELL ../../../javaexec.sh ]$1], [$2], [$3], [$4])])
750
751
752# AT_TEST_TABLES_AND_PARSE(TITLE, COND-VALUE, TEST-SPEC,
753#                          DECLS, GRAMMAR, INPUT,
754#                          BISON-STDERR, TABLES-OR-LAST-STATE,
755#                          [OTHER-CHECKS],
756#                          [PARSER-EXIT-VALUE],
757#                          [PARSER-STDOUT], [PARSER-STDERR])
758# -------------------------------------------------------------
759# Using TITLE as the test group title, check the generated parser tables
760# and parser for a specified grammar file under a condition labeled by
761# COND-VALUE.
762#
763# TEST-SPEC is a comma-delimited list of attributes of this test.  Each
764# recognized attribute is described below where it is relevant.
765#
766# Insert DECLS and GRAMMAR into the declarations and grammar section of
767# the grammar file.  Insert basic yyerror, yylex, and main function
768# definitions as well.  Hardcode yylex to return the (possibly empty)
769# comma-delimited series of tokens in INPUT followed by token 0.
770#
771# If TEST-SPEC contains the attribute no-xml, then invoke bison using
772# AT_BISON_CHECK_NO_XML.  Otherwise, invoke bison using AT_BISON_CHECK.
773# On the bison command-line, specify `--report=all --defines'.  Check
774# that Bison exits with value 0, has no stdout, and has stderr
775# BISON-STDERR.
776#
777# If TEST-SPEC contains the attribute `last-state', check that the value
778# of TABLES-OR-LAST-STATE is the index of the last state generated for
779# the grammar; in other words, check the number of states (minus one).
780# Otherwise, check that everything in the `.output' file starting with
781# the definition of state 0 is the same as the entire value of
782# TABLES-OR-LAST-STATE.
783#
784# Expand the M4 in OTHER-CHECKS to perform additional checks of the
785# `.output' file, which is named `input.output', and/or grammar file,
786# which is named `input.y'.
787#
788# Finally, compile the generated parser and then run it using
789# AT_PARSER_CHECK with PARSER-EXIT-VALUE, PARSER-STDOUT, and
790# PARSER-STDERR as the 2nd-4th arguments.
791#
792# As a precondition, you must properly double-quote all arguments that
793# are to be interpreted as strings.
794#
795# AT_COND_CASE (when appearing in single-quoted segments of arguments)
796# invokes m4_case with its own arguments but COND-VALUE inserted as the
797# first argument.  This is useful, for example, when wrapping multiple
798# AT_TEST_TABLES_AND_PARSE invocations, each representing a different
799# condition, in another macro.
800#
801# For example:
802#
803#   # AT_TEST_SYNTAX_ERROR(DESCRIPTION, DECLS, GRAMMAR, INPUT, LAST-STATE,
804#   #                      PARSER-EXIT-VALUE, PARSER-STDOUT, PARSER-STDERR)
805#   # ---------------------------------------------------------------------
806#   m4_define([AT_TEST_SYNTAX_ERROR],
807#   [
808#     AT_TEST_TABLES_AND_PARSE([$1[ with %error-verbose]], [[verbose]],
809#                              [[last-state]],
810#                              [[%error-verbose ]$2], [$3], [$4],
811#                              [[]], [$5], [], [$6], [$7], [$8])
812#     AT_TEST_TABLES_AND_PARSE([$1[ with no %error-verbose]], [[no verbose]],
813#                              [[last-state]],
814#                              [$2], [$3], [$4],
815#                              [[]], [$5], [], [$6], [$7], [$8])
816#   ])
817#
818#   AT_TEST_SYNTAX_ERROR([[Single Char Grammar]],
819#                        [[%token 'b']], [[start: 'a' ;]], [['a', 'b']],
820#                        [[3]],
821#                        [[1]], [[]],
822#                        [AT_COND_CASE([[no verbose]],
823#                                      [[syntax error
824#   ]],
825#                                      [[syntax error, unexpected 'b', expecting $end
826#   ]])])
827m4_define([AT_TEST_TABLES_AND_PARSE],
828[m4_pushdef([AT_COND_CASE], [m4_case([$2], $][@)])
829
830AT_SETUP([$1])
831AT_BISON_OPTION_PUSHDEFS([$4])
832AT_DATA_GRAMMAR([[input.y]],
833[[%code {
834  #include <stdio.h>
835  ]AT_YYERROR_DECLARE[
836  ]AT_YYLEX_DECLARE[
837}
838
839]$4[
840
841%%
842
843]$5[
844
845%%
846]AT_YYERROR_DEFINE[
847static int
848yylex (void)
849{
850  static int const input[] = {
851    ]m4_if([$6], [], [], [$6], [[]], [], [$6[, ]])[0
852  };
853  static int const *inputp = input;
854  return *inputp++;
855}
856
857int
858main (void)
859{
860  return yyparse ();
861}
862]])
863
864# In some versions of Autoconf, AT_CHECK invokes AS_ESCAPE before
865# expanding macros, so it corrupts some special characters in the
866# macros.  To avoid this, expand now and pass it the result with proper
867# string quotation.  Assume args 7 through 12 expand to properly quoted
868# strings.
869
870m4_if(m4_index(m4_quote($3), [no-xml]), -1,
871      [AT_BISON_CHECK],
872      [AT_BISON_CHECK_NO_XML])([[--report=all --defines -o input.c input.y]],
873                               [0], [], m4_dquote($7))
874
875m4_if(m4_index(m4_quote($3), [last-state]), -1,
876      [AT_CHECK([[sed -n '/^State 0$/,$p' input.output]], [[0]],
877                m4_dquote($8))],
878      [AT_CHECK([[sed -n 's/^State //p' input.output | tail -1]], [[0]],
879                m4_dquote($8)[[
880]])])
881
882$9
883
884# Canonical LR generates very large tables, resulting in very long
885# files with #line directives that may overflow what the standards
886# (C90 and C++98) guarantee: 32767.  In that case, GCC's -pedantic
887# will issue an error.
888#
889# There is no "" around `wc` since some indent the result.
890m4_bmatch([$4], [%define lr.type canonical-lr],
891[if test 32767 -lt `wc -l < input.c`; then
892  CFLAGS=`echo " $CFLAGS " | sed -e 's/ -pedantic //'`
893  CXXFLAGS=`echo " $CXXFLAGS " | sed -e 's/ -pedantic //'`
894fi])
895AT_COMPILE([[input]])
896
897AT_PARSER_CHECK([[./input]],
898                m4_ifval([$10], [m4_dquote($10)]),
899                m4_ifval([$11], [m4_dquote($11)]),
900                m4_ifval([$12], [m4_dquote($12)]))
901
902AT_BISON_OPTION_POPDEFS
903AT_CLEANUP
904
905m4_popdef([AT_COND_CASE])])
906
907
908
909## ----------------------- ##
910## Launch the test suite.  ##
911## ----------------------- ##
912
913AT_INIT
914
915# Cannot assign CC and CFLAGS here, since atlocal is loaded after
916# options are processed, so we don't know the value of CXX and
917# CXXFLAGS yet.
918#
919# Note that it also means that command line values for CXX and
920# CXXFLAGS will not be propagated to CC and CFLAGS.
921AT_ARG_OPTION([compile-c-with-cxx],
922              [compile C parsers with the C++ compiler])
923
924AT_COLOR_TESTS
925
926AT_TESTED([bison])
927