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