options: rename --defines as --header
[bison.git] / tests / headers.at
blob38eb8066e17453ac2f032531382bd9060962c39c
1 # Bison Parser Headers.                               -*- Autotest -*-
3 # Copyright (C) 2001-2002, 2006-2007, 2009-2015, 2018-2020 Free Software
4 # Foundation, Inc.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 AT_BANNER([[Parser Headers.]])
22 ## --------------------- ##
23 ## Invalid CPP headers.  ##
24 ## --------------------- ##
26 # AT_TEST_CPP_GUARD_H(BASE-NAME, [DIRECTIVES])
27 # --------------------------------------------
28 # FIXME: Much of this can be covered by calc.at.
29 m4_define([AT_TEST_CPP_GUARD_H],
30 [AT_SETUP([Invalid CPP guards: $2 --defines=$1.h])
31 AT_BISON_OPTION_PUSHDEFS([$2])
32 # Possibly create inner directories.
33 dirname=`AS_DIRNAME([$1])`
34 AS_MKDIR_P([$dirname])
36 AT_DATA_GRAMMAR([$1.y],
37 [$2
39 #include <$1.h>
40 ]AT_YYERROR_DECLARE_EXTERN[
41 ]AT_YYLEX_DECLARE_EXTERN[
44 dummy: %empty;
46 #include <$1.h>
49 AT_BISON_CHECK([--defines=$1.h --output=$1.c $1.y])
50 AT_COMPILE([$1.o], [-I. -c $1.c])
52 AT_BISON_OPTION_POPDEFS
53 AT_CLEANUP
56 AT_TEST_CPP_GUARD_H([input/input])
57 AT_TEST_CPP_GUARD_H([9foo])
58 AT_TEST_CPP_GUARD_H([input/input], [%glr-parser])
59 AT_TEST_CPP_GUARD_H([9foo],        [%glr-parser])
63 ## ---------------- ##
64 ## export YYLTYPE.  ##
65 ## ---------------- ##
67 AT_SETUP([export YYLTYPE])
69 AT_BISON_OPTION_PUSHDEFS([%name-prefix "my_"])
70 AT_DATA_GRAMMAR([input.y],
71 [[%locations
73 %name-prefix "my_"
75 #include <stdio.h>
76 #include <stdlib.h>
78 ]AT_YYERROR_DEFINE[
79 ]AT_YYLEX_DEFINE[
82 exp: %empty;
83 ]])
85 AT_BISON_CHECK([--header -o input.c input.y], [], [],
86 [[input.y:11.1-18: warning: deprecated directive: '%name-prefix "my_"', use '%define api.prefix {my_}' [-Wdeprecated]
87 input.y: warning: fix-its can be applied.  Rerun with option '--update'. [-Wother]
88 ]])
90 # YYLTYPE should be defined, and MY_LLOC declared.
91 AT_DATA([caller.c],
92 [[#include "input.h"
93 YYLTYPE *my_llocp = &my_lloc;
95 int my_parse (void);
97 ]AT_MAIN_DEFINE[
98 ]])
100 # Link and execute, just to make sure everything is fine (and in
101 # particular, that MY_LLOC is indeed defined somewhere).
102 AT_COMPILE([caller.o])
103 AT_COMPILE([input.o])
104 AT_COMPILE([caller], [caller.o input.o])
105 AT_PARSER_CHECK([caller])
106 AT_BISON_OPTION_POPDEFS
107 AT_CLEANUP
111 ## -------------- ##
112 ## Sane headers.  ##
113 ## -------------- ##
115 # AT_TEST([DIRECTIVES], [COMPILER-FLAGS])
116 # ---------------------------------------
117 # Check that headers are self-contained and protected against multiple
118 # inclusions.
120 m4_pushdef([AT_TEST],
121 [AT_SETUP([Sane headers: $1])
123 AT_BISON_OPTION_PUSHDEFS([$1])
124 AT_DATA_GRAMMAR([input.y],
125 [[$1
126 %define parse.error verbose
127 ]AT_VARIANT_IF([%token <int> X],
128 [%union {int integer;}
129 %token <integer> X])[
130 %code {
131 #include <stdio.h> /* printf. */
132 ]AT_PUSH_IF([[
133 #if defined __GNUC__ && (7 == __GNUC__ || 9 == __GNUC__)
134 # pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
135 #endif
136 ]])[
137   ]AT_YYERROR_DECLARE[
138   ]AT_YYLEX_DECLARE[
141 exp:
142   X { printf ("x\n"); }
146 ]AT_YYERROR_DEFINE[
147 ]AT_YYLEX_DEFINE([{AT_CXX_IF([yy::parser::token::])X, 0}])[
150 AT_BISON_CHECK([-d -o input.AT_LANG_EXT input.y])
152 AT_LANG_COMPILE([input.o])
154 AT_DATA([main.cc],
155 [AT_DATA_SOURCE_PROLOGUE
156 AT_MAIN_DEFINE
159 # Check that the headers are self-contained, and protected against
160 # multiple inclusions.  While at it, check they are sane for C++.
161 for h in *.AT_LANG_HDR
163   # No shell expansion with AT_DATA.
164   cat >$h.AT_LANG_EXT <<EOF
165 AT_DATA_SOURCE_PROLOGUE
166 #include "$h"
167 #include "$h"
169   AT_LANG_COMPILE([$h.o])
170 done
172 AT_BISON_OPTION_POPDEFS
174 AT_CLEANUP
175 ])# AT_TEST
177 AT_TEST([])
178 AT_TEST([%locations %debug])
180 AT_TEST([%glr-parser])
181 AT_TEST([%locations %debug %glr-parser])
183 AT_TEST([%define api.pure])
184 AT_TEST([%define api.push-pull both])
185 AT_TEST([%define api.pure %define api.push-pull both])
187 AT_TEST([%language "c++"])
188 AT_TEST([%locations %debug %language "c++"])
189 AT_TEST([%language "c++" %define api.value.type variant %define parse.assert])
191 AT_TEST([%locations %language "c++" %glr-parser])
195 ## ----------------- ##
196 ## Several parsers.  ##
197 ## ----------------- ##
199 AT_SETUP([Several parsers])
201 # AT_TEST([PREFIX], [DIRECTIVES])
202 # -------------------------------
203 # Generate and compile to *.o.  Make sure there is no (allowed) YY*
204 # nor yy* identifiers in the header after applying api.prefix.  Check
205 # that headers can be compiled by a C++ compiler.
206 m4_pushdef([AT_TEST],
207 [AT_BISON_OPTION_PUSHDEFS([%define api.prefix {$1_} $2])
208 AT_DATA_GRAMMAR([$1.y],
209 [[%define api.prefix {$1_}
211 %define parse.error verbose
212 %union
214   int integer;
217 #include <stdio.h> /* printf. */
218 ]AT_PUSH_IF([[
219 #if defined __GNUC__ && (7 == __GNUC__ || 9 == __GNUC__)
220 # pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
221 #endif
222 ]])[
223   ]AT_YYERROR_DECLARE[
224   ]AT_YYLEX_DECLARE[
227 exp:
228   'x' '1' { printf ("x1\n"); }
229 | 'x' '2' { printf ("x2\n"); }
230 | 'x' '3' { printf ("x3\n"); }
231 | 'x' '4' { printf ("x4\n"); }
232 | 'x' '5' { printf ("x5\n"); }
233 | 'x' '6' { printf ("x6\n"); }
234 | 'x' '7' { printf ("x7\n"); }
235 | 'x' '8' { printf ("x8\n"); }
236 | 'x' '9' { printf ("x9\n"); }
240 ]AT_YYERROR_DEFINE[
241 ]AT_YYLEX_DEFINE(["$1"])[
244 AT_BISON_CHECK([-d -o $1.AT_LANG_EXT $1.y])
246 AT_LANG_COMPILE([$1.o])
247 AT_CHECK([[echo "$1" >>expout]])
249 AT_BISON_OPTION_POPDEFS
250 ])# AT_TEST
252 AT_DATA([main.cc],
253 [AT_DATA_SOURCE_PROLOGUE
254 [// If we are compiling with CC=$CXX, then do not load the C headers
255 // inside extern "C", since they were _not_ compiled this way.
256 #if ! CC_IS_CXX
257 extern "C"
259 #endif
260   #include "x1.h"
261   #include "x2.h"
262   #include "x3.h"
263   #include "x4.h"
264   #include "x6.h"
265   #include "x7.h"
266   #include "x8.h"
267 #if ! CC_IS_CXX
269 #endif
270 #include "x5.hh"
271 #include "x9.hh"
273 #define RUN(S)                                  \
274   do {                                          \
275     int res = S;                                \
276     if (res)                                    \
277       std::cerr << #S": " << res << '\n';       \
278   } while (false)
281 main (void)
283   RUN(x1_parse());
284   RUN(x2_parse());
285   RUN(x3_parse());
286   RUN(x4_parse());
287   x5_::parser p5;
288   RUN(p5.parse());
289   RUN(x6_parse());
290   RUN(x7_parse());
291   RUN(x8_parse());
292   x9_::parser p9;
293   RUN(p9.parse());
294   return 0;
296 ]])# main.cc
298 AT_TEST([x1], [])
299 AT_TEST([x2], [%locations %debug])
300 AT_TEST([x3], [%glr-parser])
301 AT_TEST([x4], [%locations %debug %glr-parser])
302 AT_TEST([x5], [%locations %debug %language "c++"])
303 AT_TEST([x6], [%define api.pure])
304 AT_TEST([x7], [%define api.push-pull both])
305 AT_TEST([x8], [%define api.pure %define api.push-pull both])
306 AT_TEST([x9], [%locations %code requires {#include "location.hh"} %define api.location.type {x5_::location} %debug %language "c++"])
307 #AT_TEST([x5], [%locations %language "c++" %glr-parser])
309 # Check that api.prefix works properly:
311 # - no 'yy' left.
312 #   C++ output relies on namespaces and still uses yy a lot.
314 # - no 'YY' left.
315 #   Ignore comments, YYChar (template parameter), YYPUSH_MORE(_DEFINED)?
316 #   (constant definition), YY_\w+_INCLUDED (header guards).
317 #   YYDEBUG (not renamed) can be read, but not changed.
318 AT_PERL_CHECK([[-n -0777 -e '
319   s{/\*.*?\*/}{}gs;
320   s{//.*}{}g;
321   s{\b((defined|if)\ YYDEBUG
322       |YYChar
323       |YYNTOKENS  # This is actual scoped in a C++ class.
324       |YYPUSH_MORE(?:_DEFINED)?
325       |S_(YY(ACCEPT|EMPTY|EOF|error|UNDEF))  # These guys are scoped.
326       |YYUSE
327       |YY_ATTRIBUTE(?:_PURE|_UNUSED)
328       |YY(?:_REINTERPRET)?_CAST
329       |YY_CONSTEXPR
330       |YY_COPY
331       |YY_CPLUSPLUS
332       |YY_IGNORE_(?:MAYBE_UNINITIALIZED|USELESS_CAST)_(?:BEGIN|END)
333       |YY_INITIAL_VALUE
334       |YY_MOVE
335       |YY_MOVE_OR_COPY
336       |YY_MOVE_REF
337       |YY_NOEXCEPT
338       |YY_NOTHROW
339       |YY_NULLPTR
340       |YY_RVREF
341       |YY_\w+_INCLUDED
342       )\b}{}gx;
343   while (/^(.*YY.*)$/gm)
344   {
345     print "$ARGV: invalid exported YY: $1\n";
346   }
347   if ($ARGV =~ /\.h$/)
348   {
349     while (/^(.*yy.*)$/gm)
350     {
351       print "$ARGV: invalid exported yy: $1\n";
352     }
353   }
354 ' -- *.hh *.h]])
356 # Do this late, so that other checks have been performed.
357 AT_SKIP_IF_CANNOT_LINK_C_AND_CXX
359 AT_COMPILE_CXX([parser], [[x[1-9].o -DCC_IS_CXX=$CC_IS_CXX main.cc]])
360 AT_PARSER_CHECK([parser], [0], [[expout]])
362 m4_popdef([AT_TEST])
364 AT_CLEANUP