]> git.saurik.com Git - bison.git/blame - tests/c++.at
bison: avoid warnings from static code analysis
[bison.git] / tests / c++.at
CommitLineData
535ee0cb 1# Checking the C++ Features. -*- Autotest -*-
7d424de1 2
3209eb1c 3# Copyright (C) 2004-2005, 2007-2015 Free Software Foundation, Inc.
e019c247 4
f16b0819 5# This program is free software: you can redistribute it and/or modify
e019c247 6# it under the terms of the GNU General Public License as published by
f16b0819
PE
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
e019c247
AD
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
f16b0819 14#
e019c247 15# You should have received a copy of the GNU General Public License
f16b0819 16# along with this program. If not, see <http://www.gnu.org/licenses/>.
e019c247
AD
17
18AT_BANNER([[C++ Features.]])
19
20
75ae8299
AD
21## --------------- ##
22## C++ Locations. ##
23## --------------- ##
24
25AT_SETUP([C++ Locations])
26
27AT_BISON_OPTION_PUSHDEFS([%locations %skeleton "lalr1.cc"])
28AT_DATA_GRAMMAR([[input.y]],
29[[%code {#include <sstream>}
30%locations
31%debug
32%skeleton "lalr1.cc"
33%code
34{
35]AT_YYERROR_DECLARE[
36]AT_YYLEX_DECLARE[
37}
38%%
39exp: %empty;
40%%
41]AT_YYERROR_DEFINE[
42]AT_YYLEX_DEFINE[
43
44template <typename T>
45bool
46check (const T& in, const std::string& s)
47{
48 std::stringstream os;
49 os << in;
50 if (os.str () != s)
51 {
52 std::cerr << "fail: " << os.str () << ", expected: " << s << std::endl;
53 return false;
54 }
55 return true;
56}
57
58int
59main (void)
60{
61 int fail = 0;
62 ]AT_YYLTYPE[ loc; fail += check (loc, "1.1");
56351d4c 63 fail += check (loc + 10, "1.1-10");
75ae8299
AD
64 loc += 10; fail += check (loc, "1.1-10");
65 loc += -5; fail += check (loc, "1.1-5");
56351d4c 66 fail += check (loc - 5, "1.1");
75ae8299
AD
67 loc -= 5; fail += check (loc, "1.1");
68 // Check that we don't go below.
69 // http://lists.gnu.org/archive/html/bug-bison/2013-02/msg00000.html
70 loc -= 10; fail += check (loc, "1.1");
71
72 loc.columns (10); loc.lines (10); fail += check (loc, "1.1-11.0");
73 loc.lines (-2); fail += check (loc, "1.1-9.0");
74 loc.lines (-10); fail += check (loc, "1.1");
56351d4c
AD
75
76 ]AT_YYLTYPE[ loc2 (YY_NULLPTR, 5, 10);
77 fail += check (loc2, "5.10");
78 fail += check (loc + loc2, "1.1-5.9");
79 loc += loc2; fail += check (loc, "1.1-5.9");
75ae8299
AD
80 return !fail;
81}
82]])
83
84AT_FULL_COMPILE([input])
85AT_PARSER_CHECK([./input], 0)
86AT_BISON_OPTION_POPDEFS
87AT_CLEANUP
88
89
97ae878e
AD
90## --------------------------- ##
91## C++ Variant-based Symbols. ##
92## --------------------------- ##
93
94AT_SETUP([C++ Variant-based Symbols])
95
96AT_KEYWORDS([variant])
97
98AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" %debug $1])
99# Store strings and integers in a list of strings.
f2e1d409 100AT_DATA_GRAMMAR([list.y],
97ae878e
AD
101[[%skeleton "lalr1.cc"
102%define api.value.type variant
103%define parse.assert
104%debug
105
106%code top
107{
108 // Get access to stack_symbol_type for the tests.
109# define private public
110}
111%code provides
112{
113 ]AT_YYLEX_DECLARE[
114}
115
116%token <int> INT "int"
117%type < std::list<int> > exp
118
119%printer { yyo << $$; } <int>
120%printer
121 {
122 for (std::list<int>::const_iterator i = $$.begin (); i != $$.end (); ++i)
123 {
124 if (i != $$.begin ())
125 yyo << ", ";
126 yyo << *i;
127 }
128 } < std::list<int> >
129
130%code requires { #include <list> }
131%code { int yylex (yy::parser::semantic_type* yylval); }
132
133%%
134exp: "int" { $$.push_back ($1); }
135%%
136]AT_YYERROR_DEFINE[
137]AT_YYLEX_DEFINE[
138
139int main()
140{
141 {
142 yy::parser::symbol_type s = yy::parser::make_INT(12);
143 std::cerr << s.value.as<int>() << std::endl;
144 }
145
146 {
147 yy::parser::symbol_type s = yy::parser::make_INT(123);
148 yy::parser::stack_symbol_type ss(1, s);
149 std::cerr << ss.value.as<int>() << std::endl;
150 }
151
152 {
153 yy::parser::stack_type st;
154 for (int i = 0; i < 100; ++i)
155 {
156 yy::parser::symbol_type s(yy::parser::make_INT(i));
157 yy::parser::stack_symbol_type ss(1, s);
158 st.push(ss);
159 }
160 }
161}
162]])
163
f2e1d409 164AT_FULL_COMPILE([list])
97ae878e
AD
165AT_PARSER_CHECK([./list], 0, [],
166[12
167123
168])
169
170AT_BISON_OPTION_POPDEFS
171AT_CLEANUP
172
173
76307410
AD
174## ---------- ##
175## Variants. ##
176## ---------- ##
177
00cebd11
AD
178# Check that the variants are properly supported, including in error
179# recovery.
180
e36ec1f4
AD
181# AT_TEST([DIRECTIVES])
182# ---------------------
76307410 183# Check the support of variants in C++, with the additional DIRECTIVES.
e36ec1f4 184m4_pushdef([AT_TEST],
76307410
AD
185[AT_SETUP([Variants $1])
186
db40c3f8 187AT_BISON_OPTION_PUSHDEFS([%debug $1])
76307410 188# Store strings and integers in a list of strings.
f2e1d409 189AT_DATA_GRAMMAR([list.y],
76307410 190[[%debug
bc603897 191%define api.value.type variant
e5eb92e7
AD
192]m4_bpatsubst([$1], [\\n], [
193])[
76307410
AD
194
195%code requires // code for the .hh file
196{
197#include <list>
198#include <string>
199typedef std::list<std::string> strings_type;
200}
201
202%code // code for the .cc file
203{
f5fceda5 204#include <cstdlib> // abort, getenv
76307410 205#include <iostream>
76307410
AD
206#include <sstream>
207
e36ec1f4
AD
208 namespace yy
209 {
210 static]AT_TOKEN_CTOR_IF([[
211 parser::symbol_type yylex ()]], [[
f5fceda5
AD
212 parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([,
213 parser::location_type* yylloc])[)]])[;
e36ec1f4 214 }
76307410 215
dddec537 216 // Printing a list of strings (for %printer).
76307410
AD
217 // Koening look up will look into std, since that's an std::list.
218 namespace std
219 {
220 std::ostream&
221 operator<<(std::ostream& o, const strings_type& s)
222 {
f5da8149 223 o << '(';
77a1a208 224 for (strings_type::const_iterator i = s.begin (); i != s.end (); ++i)
f5da8149
AD
225 {
226 if (i != s.begin ())
227 o << ", ";
228 o << *i;
229 }
230 return o << ')';
76307410
AD
231 }
232 }
233
234 // Conversion to string.
235 template <typename T>
236 inline
237 std::string
8a4ec359 238 to_string (const T& t)
76307410
AD
239 {
240 std::ostringstream o;
241 o << t;
77a1a208 242 return o.str ();
76307410
AD
243 }
244}
245
f5da8149 246%token <::std::string> TEXT;
76307410 247%token <int> NUMBER;
76307410 248%token END_OF_FILE 0;
00cebd11 249%token COMMA ","
76307410 250
f5da8149 251%type <::std::string> item;
cb823b6f
AD
252// Using the template type to exercize its parsing.
253// Starting with :: to ensure we don't output "<::" which starts by the
254// digraph for the left square bracket.
ee028dce 255%type <::std::list<std::string>> list;
76307410 256
16bb9f16 257%printer { yyo << $$; }
9641b918 258 <int> <::std::string> <::std::list<std::string>>;
ee028dce
AD
259%destructor { std::cerr << "Destroy: " << $$ << '\n'; } <*>;
260%destructor { std::cerr << "Destroy: \"" << $$ << "\"\n"; } <::std::string>;
76307410
AD
261%%
262
263result:
f5da8149 264 list { std::cout << $][1 << std::endl; }
76307410
AD
265;
266
267list:
00cebd11
AD
268 item { $$.push_back ($][1); }
269| list "," item { std::swap ($$, $][1); $$.push_back ($][3); }
270| list error { std::swap ($$, $][1); }
76307410
AD
271;
272
273item:
00cebd11 274 TEXT { std::swap ($$, $][1); }
8a4ec359 275| NUMBER { if ($][1 == 3) YYERROR; else $$ = to_string ($][1); }
76307410
AD
276;
277%%
26a4d3c8
AD
278]AT_TOKEN_CTOR_IF([],
279[[#ifdef TWO_STAGE_BUILD
e36ec1f4 280# define BUILD(Type, Value) build<Type> () = Value
e5eb92e7 281#else
e36ec1f4 282# define BUILD(Type, Value) build (Value)
e5eb92e7 283#endif
26a4d3c8 284]])[
e36ec1f4
AD
285#define STAGE_MAX 5
286namespace yy
76307410 287{
e36ec1f4
AD
288 static]AT_TOKEN_CTOR_IF([[
289 parser::symbol_type yylex ()]], [[
f5fceda5
AD
290 parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([,
291 parser::location_type* yylloc])[)]])[
292 {]AT_LOCATION_IF([
293 typedef parser::location_type location;])[
00cebd11
AD
294 // The 5 is a syntax error whose recovery requires that we discard
295 // the lookahead. This tests a regression, see
296 // <http://savannah.gnu.org/support/?108481>.
297 static char const *input = "0,1,2,3,45,6";
298 switch (int stage = *input++)
299 {
300 case 0:]AT_TOKEN_CTOR_IF([[
f5fceda5
AD
301 return parser::make_END_OF_FILE (]AT_LOCATION_IF([location ()])[);]],
302[AT_LOCATION_IF([
303 *yylloc = location ();])[
e36ec1f4 304 return parser::token::END_OF_FILE;]])[
00cebd11
AD
305
306 case ',':
307 ]AT_TOKEN_CTOR_IF([[
308 return parser::make_COMMA (]AT_LOCATION_IF([location ()])[);]], [[
309]AT_LOCATION_IF([
f5fceda5 310 *yylloc = location ();])[
00cebd11
AD
311 return parser::token::COMMA;]])[
312
313 default:
314 stage = stage - '0';
315 if (stage % 2)
316 {]AT_TOKEN_CTOR_IF([[
317 return parser::make_NUMBER (stage]AT_LOCATION_IF([, location ()])[);]], [[
318 yylval->BUILD (int, stage);]AT_LOCATION_IF([
319 *yylloc = location ();])[
320 return parser::token::NUMBER;]])[
321 }
322 else
323 {]AT_TOKEN_CTOR_IF([[
324 return parser::make_TEXT (to_string (stage)]AT_LOCATION_IF([, location ()])[);]], [[
325 yylval->BUILD (std::string, to_string (stage));]AT_LOCATION_IF([
326 *yylloc = location ();])[
327 return parser::token::TEXT;]])[
328 }
329 }
330
e36ec1f4
AD
331 abort ();
332 }
76307410
AD
333}
334
f5fceda5 335]AT_YYERROR_DEFINE[
3ef9fa8f 336]AT_MAIN_DEFINE[
76307410
AD
337]])
338
f2e1d409 339AT_FULL_COMPILE([list])
2c08dc50 340AT_PARSER_CHECK([./list], 0,
ee028dce
AD
341[[(0, 1, 2, 4, 6)
342]],
343[[Destroy: ""
344Destroy: "0"
345Destroy: (0)
346Destroy: 1
347Destroy: "1"
348Destroy: ()
349Destroy: ""
350Destroy: "2"
351Destroy: ()
352Destroy: ""
353Destroy: 3
354Destroy: ()
355Destroy: ""
356Destroy: "4"
357Destroy: ()
358Destroy: ()
359Destroy: 5
360Destroy: ()
361Destroy: ""
362Destroy: "6"
363Destroy: ()
364Destroy: (0, 1, 2, 4, 6)
365]])
76307410 366
e36ec1f4 367AT_BISON_OPTION_POPDEFS
76307410
AD
368AT_CLEANUP
369])
370
db40c3f8
AD
371AT_TEST([[%skeleton "lalr1.cc" ]])
372AT_TEST([[%skeleton "lalr1.cc" %define parse.assert]])
00cebd11 373AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %locations]])
db40c3f8
AD
374AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %code {\n#define TWO_STAGE_BUILD\n}]])
375AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %define api.token.constructor]])
376AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %define api.token.constructor %define api.token.prefix {TOK_}]])
00cebd11 377AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %define api.token.constructor %define api.token.prefix {TOK_} %locations]])
e36ec1f4
AD
378
379m4_popdef([AT_TEST])
76307410
AD
380
381
e019c247
AD
382## ----------------------- ##
383## Doxygen Documentation. ##
384## ----------------------- ##
385
386m4_define([AT_CHECK_DOXYGEN],
387[m4_case([$1],
388 [Public], [m4_pushdef([AT_DOXYGEN_PRIVATE], [NO])],
389 [Private], [m4_pushdef([AT_DOXYGEN_PRIVATE], [YES])],
e9690142 390 [m4_fatal([invalid argument: $1])])
e019c247
AD
391AT_SETUP([Doxygen $1 Documentation])
392
535ee0cb 393AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
e019c247
AD
394AT_DATA([input.yy],
395[[%skeleton "lalr1.cc"
396%locations
e1d06b52 397%defines
e019c247 398%debug
e019c247 399%%
c7442984 400exp: /* empty */;
e019c247 401%%
535ee0cb 402]AT_YYERROR_DEFINE[
e019c247
AD
403]])
404
e1d06b52 405AT_BISON_CHECK([-o input.cc input.yy])
e019c247
AD
406
407AT_DATA([Doxyfile],
408[# The PROJECT_NAME tag is a single word (or a sequence of words
409# surrounded by quotes) that should identify the project.
410PROJECT_NAME = "Bison C++ Parser"
411
412# The QUIET tag can be used to turn on/off the messages that are
413# generated by doxygen. Possible values are YES and NO. If left blank
414# NO is used.
415QUIET = YES
416
417# The WARNINGS tag can be used to turn on/off the warning messages
418# that are generated by doxygen. Possible values are YES and NO. If
419# left blank NO is used.
420WARNINGS = YES
421# If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate
422# warnings for undocumented members. If EXTRACT_ALL is set to YES then
423# this flag will automatically be disabled.
424WARN_IF_UNDOCUMENTED = YES
425# If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings
426# for potential errors in the documentation, such as not documenting
427# some parameters in a documented function, or documenting parameters
428# that don't exist or using markup commands wrongly.
429WARN_IF_DOC_ERROR = YES
430# The WARN_FORMAT tag determines the format of the warning messages
431# that doxygen can produce. The string should contain the $file,
432# $line, and $text tags, which will be replaced by the file and line
433# number from which the warning originated and the warning text.
434WARN_FORMAT = "$file:$line: $text"
435
436# If the EXTRACT_ALL tag is set to YES doxygen will assume all
437# entities in documentation are documented, even if no documentation
438# was available. Private class members and static file members will
439# be hidden unless the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set
440# to YES
441EXTRACT_ALL = YES
442
443# If the EXTRACT_PRIVATE tag is set to YES all private members of a
444# class will be included in the documentation.
445EXTRACT_PRIVATE = AT_DOXYGEN_PRIVATE
446
447# If the EXTRACT_STATIC tag is set to YES all static members of a file
448# will be included in the documentation.
449EXTRACT_STATIC = AT_DOXYGEN_PRIVATE
450])
451
452AT_CHECK([doxygen --version || exit 77], 0, ignore)
453AT_CHECK([doxygen], 0, [], [ignore])
454
535ee0cb 455AT_BISON_OPTION_POPDEFS
e019c247
AD
456AT_CLEANUP
457
458m4_popdef([AT_DOXYGEN_PRIVATE])
459])# AT_CHECK_DOXYGEN
460
461AT_CHECK_DOXYGEN([Public])
462AT_CHECK_DOXYGEN([Private])
793fbca5 463
76307410 464
793fbca5
JD
465## ------------ ##
466## Namespaces. ##
467## ------------ ##
468
56b91ae0
AD
469# AT_TEST(NAMESPACE-DECL, [COMPILE-ERROR])
470# ----------------------------------------
793fbca5
JD
471# See if Bison can handle %define namespace "NAMESPACE-DECL". If COMPILE-ERROR
472# is specified, then Bison should accept the input, but compilation will fail,
473# so don't check compilation.
56b91ae0 474m4_pushdef([AT_TEST],
eb0e86ac 475[AT_BISON_OPTION_PUSHDEFS([%language "C++" %define api.namespace {$1}])
e1d06b52 476AT_DATA_GRAMMAR([[input.yy]],
793fbca5 477[[%language "C++"
eb0e86ac 478%define api.namespace {]$1[}
793fbca5
JD
479%union { int i; }
480%define global_tokens_and_yystype
2ea7730c 481%locations
793fbca5
JD
482
483%code {
484 // YYSTYPE contains a namespace reference.
d73e55e0 485 int yylex (YYSTYPE *lval, const ]$1[::parser::location_type*) {
793fbca5
JD
486 lval->i = 3;
487 return 0;
488 }
489}
490
491%%
492
493start: ;
494
495%%
496
497void
498]$1[::parser::error (const ]$1[::parser::location_type &loc,
499 const std::string &msg)
500{
501 std::cerr << "At " << loc << ": " << msg << std::endl;
502}
503
56b91ae0 504]AT_MAIN_DEFINE[
793fbca5
JD
505]])
506
bb1c50d8 507
e1d06b52 508AT_BISON_CHECK([[-o input.cc input.yy]])
793fbca5
JD
509
510m4_if([$#], [1],
e1d06b52 511[AT_COMPILE_CXX([[input]])
793fbca5 512AT_PARSER_CHECK([[./input]])])
56b91ae0 513AT_BISON_OPTION_POPDEFS
793fbca5
JD
514])
515
516AT_SETUP([[Relative namespace references]])
56b91ae0
AD
517AT_TEST([[foo]])
518AT_TEST([[foo::bar]])
519AT_TEST([[foo::bar::baz]])
793fbca5
JD
520AT_CLEANUP
521
522AT_SETUP([[Absolute namespace references]])
56b91ae0
AD
523AT_TEST([[::foo]])
524AT_TEST([[::foo::bar]])
525AT_TEST([[::foo::bar::baz]])
bb1c50d8
AD
526AT_TEST([[@tb@::foo]])
527AT_TEST([[ @tb@ ::foo::bar]])
56b91ae0 528AT_TEST([[ ::foo::bar::baz]])
793fbca5
JD
529AT_CLEANUP
530
531AT_SETUP([[Syntactically invalid namespace references]])
56b91ae0
AD
532AT_TEST([[:foo:bar]], [[-]])
533AT_TEST([[foo: :bar]], [[-]])
4c9b8f13
AD
534# This one is interesting because '[3]' is encoded as '@<:@3@:>@', which
535# contains single occurrences of ':'.
56b91ae0
AD
536AT_TEST([[foo[3]::bar::baz]], [[-]])
537AT_TEST([[foo::bar,baz]], [[-]])
538AT_TEST([[foo::bar::(baz /* Pacify Emacs ) */]], [[-]])
793fbca5 539AT_CLEANUP
d59beda0 540
56b91ae0 541m4_popdef([AT_TEST])
d59beda0
JD
542
543## -------------------------------------- ##
544## Syntax error discarding no lookahead. ##
545## -------------------------------------- ##
546
547# After a syntax error, lalr1.cc used to not check whether there
548# actually is a lookahead before discarding the lookahead. As a result,
549# it mistakenly invoked the destructor for the previous lookahead.
550
551AT_SETUP([[Syntax error discarding no lookahead]])
552
3ef9fa8f
AD
553AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
554
e1d06b52 555AT_DATA_GRAMMAR([[input.y]],
d59beda0
JD
556[[%skeleton "lalr1.cc"
557
558%code {
559 #include <string>
a8c5aaa5 560 int yylex (yy::parser::semantic_type *);
d59beda0
JD
561 #define USE(Args)
562}
563
d59beda0
JD
564%define parse.error verbose
565
566%nonassoc 'a' ;
567
568%destructor {
569 std::cerr << "Discarding 'a'." << std::endl;
570} 'a'
571
572%%
573
574start: error-reduce consistent-error 'a' { USE ($3); };
575
576error-reduce:
577 'a' 'a' consistent-error 'a' { USE (($1, $2, $4)); }
578| 'a' error { std::cerr << "Reducing 'a'." << std::endl; USE ($1); }
579;
580
581consistent-error:
582 'a'
583| /*empty*/ %prec 'a'
584;
585
5335b65a
JD
586// Provide another context in which all rules are useful so that this
587// test case looks a little more realistic.
588start: 'b' consistent-error ;
589
d59beda0
JD
590%%
591
592int
a8c5aaa5 593yylex (yy::parser::semantic_type *)
d59beda0
JD
594{
595 static char const *input = "aa";
596 return *input++;
597}
598
599void
a8c5aaa5 600yy::parser::error (const std::string &m)
d59beda0
JD
601{
602 std::cerr << m << std::endl;
603}
604
3ef9fa8f 605]AT_MAIN_DEFINE[
d59beda0 606]])
3ef9fa8f 607
e1d06b52 608AT_FULL_COMPILE([[input]])
d59beda0
JD
609# This used to print "Discarding 'a'." again at the end.
610AT_PARSER_CHECK([[./input]], [[1]], [[]],
611[[syntax error
612Discarding 'a'.
613Reducing 'a'.
614]])
615
3ef9fa8f 616AT_BISON_OPTION_POPDEFS
d59beda0 617AT_CLEANUP
199a2d6d
AD
618
619
620## --------------------------- ##
621## Syntax error as exception. ##
622## --------------------------- ##
623
624AT_SETUP([[Syntax error as exception]])
625
3ef9fa8f
AD
626AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
627
e1d06b52 628AT_DATA_GRAMMAR([[input.y]],
199a2d6d
AD
629[[%skeleton "lalr1.cc"
630
631%code
632{
0bb5783b 633 #include <cstdlib>
199a2d6d
AD
634 int yylex (yy::parser::semantic_type *);
635}
636
bc603897 637%define api.value.type variant
199a2d6d
AD
638%define parse.error verbose
639%define parse.trace
640%%
641
642start:
643 thing
644| start thing
645;
646
647thing:
648 error { std::cerr << "caught error" << std::endl; }
649| item
650;
651
652item:
653 'a'
654| 's'
655 {
77a1a208 656 throw yy::parser::syntax_error ("invalid expression");
199a2d6d
AD
657 }
658
659%%
660
661int
662yylex (yy::parser::semantic_type *)
663{
a6552c5d
AD
664 // 's': syntax error, 'l': lexical error.
665 static char const *input = "asal";
199a2d6d
AD
666 switch (int res = *input++)
667 {
a6552c5d 668 case 'l':
77a1a208 669 throw yy::parser::syntax_error ("invalid character");
199a2d6d
AD
670 default:
671 return res;
672 }
673}
674
675void
676yy::parser::error (const std::string &m)
677{
678 std::cerr << "error: " << m << std::endl;
679}
3ef9fa8f 680]AT_MAIN_DEFINE[
199a2d6d 681]])
3ef9fa8f 682
e1d06b52 683AT_FULL_COMPILE([[input]])
199a2d6d
AD
684
685AT_PARSER_CHECK([[./input]], [[0]], [[]],
686[[error: invalid expression
687caught error
a6552c5d
AD
688error: invalid character
689caught error
199a2d6d
AD
690]])
691
3ef9fa8f 692AT_BISON_OPTION_POPDEFS
199a2d6d 693AT_CLEANUP
cff92661
AD
694
695
696## ------------------ ##
697## Exception safety. ##
698## ------------------ ##
699
5cf6e669
AD
700# AT_TEST([BISON-DIRECTIVES = ''], [WITH-RECOVERY = "with"])
701# ----------------------------------------------------------
dc8e535c 702# Check that no object is leaked when exceptions are thrown.
5cf6e669 703# WITH-RECOVERY = "with" or "without".
dc8e535c 704m4_pushdef([AT_TEST],
5cf6e669 705[AT_SETUP([[Exception safety $2 error recovery $1]])
cff92661 706
02681666
AD
707AT_SKIP_IF_EXCEPTION_SUPPORT_IS_POOR
708
dc8e535c 709AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" $1])
cff92661
AD
710
711AT_DATA_GRAMMAR([[input.yy]],
712[[%skeleton "lalr1.cc"
cff92661 713%debug
e8b86af8 714%error-verbose
dc8e535c 715$1
cff92661
AD
716%code requires
717{
e8b86af8 718 #include <cassert>
cff92661
AD
719 #include <cstdlib> // size_t and getenv.
720 #include <iostream>
5cf6e669 721 #include <set>
cff92661 722
e8b86af8 723 bool debug = false;
cff92661 724
5cf6e669 725 /// A class that tracks its instances.
cff92661
AD
726 struct Object
727 {
e39fcdda
AD
728 char val;
729
5cf6e669
AD
730 Object ()
731 : val ('?')
e39fcdda 732 {
e39fcdda 733 log (this, "Object::Object");
5cf6e669 734 Object::instances.insert (this);
e39fcdda
AD
735 }
736
5cf6e669
AD
737 Object (const Object& that)
738 : val (that.val)
e39fcdda 739 {
e39fcdda 740 log (this, "Object::Object");
5cf6e669 741 Object::instances.insert (this);
e39fcdda
AD
742 }
743
5cf6e669
AD
744 Object (char v)
745 : val (v)
e39fcdda 746 {
5cf6e669
AD
747 log (this, "Object::Object");
748 Object::instances.insert (this);
e39fcdda
AD
749 }
750
751 ~Object ()
752 {
e39fcdda 753 log (this, "Object::~Object");
5cf6e669
AD
754 objects::const_iterator i = instances.find (this);
755 // Make sure this object is alive.
756 assert (i != instances.end ());
757 Object::instances.erase (i);
758 }
759
760 Object& operator= (char v)
761 {
762 val = v;
763 return *this;
e39fcdda
AD
764 }
765
766 // Static part.
5cf6e669 767 typedef std::set<const Object*> objects;
e8b86af8 768 static objects instances;
cff92661 769
e8b86af8
AD
770 static bool
771 empty ()
772 {
e39fcdda 773 return instances.empty ();
e8b86af8
AD
774 }
775
776 static void
777 log (Object const *o, const std::string& msg)
cff92661 778 {
cff92661 779 if (debug)
e8b86af8
AD
780 {
781 if (o)
782 std::cerr << o << "->";
783 std::cerr << msg << " {";
784 const char* sep = " ";
785 for (objects::const_iterator i = instances.begin(),
786 i_end = instances.end();
787 i != i_end;
788 ++i)
789 {
790 std::cerr << sep << *i;
791 sep = ", ";
792 }
793 std::cerr << " }" << std::endl;
794 }
795 }
cff92661
AD
796 };
797}
798
799%code
800{
801 #include <cassert>
a2642464 802 #include <cstring> // strchr
cff92661
AD
803 #include <stdexcept>
804 int yylex (yy::parser::semantic_type *);
e8b86af8 805 Object::objects Object::instances;
cff92661
AD
806 static char const *input;
807}
808
dc8e535c
AD
809]AT_VARIANT_IF([[
810%printer
cff92661 811{
dc8e535c
AD
812 yyo << &$$ << " '" << $$.val << '\'';
813 if ($$.val == 'p')
814 throw std::runtime_error ("printer");
815} <Object>;
cff92661 816
dc8e535c
AD
817%token <Object> 'a' 'E' 'e' 'p' 'R' 's' 'T'
818%type <Object> list item
819]], [[
820%union
a2642464 821{
dc8e535c 822 Object *obj;
a2642464 823}
cff92661 824%destructor { delete $$; } <obj>;
25a6ad2f
AD
825%printer
826{
e8b86af8 827 yyo << $$ << " '" << $$->val << '\'';
25a6ad2f
AD
828 if ($$->val == 'p')
829 throw std::runtime_error ("printer");
830} <obj>;
cff92661 831
e8b86af8 832%token <obj> 'a' 'E' 'e' 'p' 'R' 's' 'T'
cff92661 833%type <obj> list item
dc8e535c
AD
834]])[
835
836%initial-action
837{
838 if (strchr (input, 'i'))
839 throw std::runtime_error ("initial-action");
840}
cff92661
AD
841
842%%
843
dc8e535c 844start: list {]AT_VARIANT_IF([], [ delete $][1]; )[};
cff92661
AD
845
846list:
16bb9f16 847 item { $$ = $][1; }
dc8e535c 848 // Right recursion to load the stack.
16bb9f16 849| item list { $$ = $][1; ]AT_VARIANT_IF([], [delete $][2]; )[}
cff92661
AD
850;
851
852item:
16bb9f16 853 'a' { $$ = $][1; }
ee028dce 854| 'e' { YYUSE ($$); YYUSE ($][1); error ("syntax error"); }
e8b86af8
AD
855// Not just 'E', otherwise we reduce when 'E' is the lookahead, and
856// then the stack is emptied, defeating the point of the test.
ee028dce 857| 'E' 'a' { YYUSE ($][1); $$ = $][2; }
16bb9f16
AD
858| 'R' { ]AT_VARIANT_IF([], [$$ = YY_NULLPTR; delete $][1]; )[YYERROR; }
859| 'p' { $$ = $][1; }
860| 's' { $$ = $][1; throw std::runtime_error ("reduction"); }
861| 'T' { ]AT_VARIANT_IF([], [$$ = YY_NULLPTR; delete $][1]; )[YYABORT; }
5cf6e669
AD
862]m4_if([$2], [with],
863[[| error { $$ = ]AT_VARIANT_IF([], [new ])[Object ('R'); yyerrok; }]])[
e8b86af8 864;
cff92661
AD
865%%
866
867int
868yylex (yy::parser::semantic_type *lvalp)
869{
a2642464 870 // 'a': no error.
e8b86af8
AD
871 // 'e': user action calls error.
872 // 'E': syntax error, with yyerror that throws.
a2642464
AD
873 // 'i': initial action throws.
874 // 'l': yylex throws.
e8b86af8 875 // 'R': call YYERROR in the action
a2642464 876 // 's': reduction throws.
e8b86af8 877 // 'T': call YYABORT in the action
cff92661
AD
878 switch (int res = *input++)
879 {
dc8e535c
AD
880 case 'l':
881 throw std::runtime_error ("yylex");
882 default:
39bace5d
AD
883 lvalp->]AT_VARIANT_IF([build (Object (res))],
884 [obj = new Object (res)])[;
dc8e535c
AD
885 // Fall through.
886 case 0:
887 return res;
cff92661
AD
888 }
889}
890
e8b86af8
AD
891/* A C++ error reporting function. */
892void
23d13411 893yy::parser::error (const std::string& m)
e8b86af8 894{
e8b86af8
AD
895 throw std::runtime_error (m);
896}
cff92661
AD
897
898int
899main (int argc, const char *argv[])
900{
25a6ad2f
AD
901 switch (argc)
902 {
903 case 2:
904 input = argv[1];
905 break;
906 case 3:
2c08dc50 907 assert (std::string(argv[1]) == "--debug");
25a6ad2f
AD
908 debug = 1;
909 input = argv[2];
910 break;
911 default:
912 abort ();
913 }
914
cff92661 915 yy::parser parser;
25a6ad2f 916 debug |= !!getenv ("YYDEBUG");
cff92661
AD
917 parser.set_debug_level (debug);
918 int res = 2;
919 try
920 {
921 res = parser.parse ();
922 }
923 catch (const std::exception& e)
924 {
925 std::cerr << "exception caught: " << e.what () << std::endl;
926 }
927 catch (...)
928 {
929 std::cerr << "unknown exception caught" << std::endl;
930 }
8d0b7cef 931 Object::log (YY_NULLPTR, "end");
e8b86af8 932 assert (Object::empty());
cff92661
AD
933 return res;
934}
935]])
e8b86af8 936AT_BISON_CHECK([[-o input.cc --report=all input.yy]])
be6fa942 937AT_COMPILE_CXX([[input]])
cff92661
AD
938
939AT_PARSER_CHECK([[./input aaaas]], [[2]], [[]],
a2642464 940[[exception caught: reduction
cff92661
AD
941]])
942
943AT_PARSER_CHECK([[./input aaaal]], [[2]], [[]],
a2642464
AD
944[[exception caught: yylex
945]])
946
947AT_PARSER_CHECK([[./input i]], [[2]], [[]],
948[[exception caught: initial-action
cff92661
AD
949]])
950
25a6ad2f
AD
951AT_PARSER_CHECK([[./input aaaap]])
952
953AT_PARSER_CHECK([[./input --debug aaaap]], [[2]], [[]], [[stderr]])
ebbc76d0 954AT_CHECK([[grep '^exception caught: printer$' stderr]], [], [ignore])
25a6ad2f 955
e8b86af8
AD
956AT_PARSER_CHECK([[./input aaaae]], [[2]], [[]],
957[[exception caught: syntax error
958]])
959
960AT_PARSER_CHECK([[./input aaaaE]], [[2]], [[]],
961[[exception caught: syntax error, unexpected $end, expecting 'a'
962]])
963
964AT_PARSER_CHECK([[./input aaaaT]], [[1]])
965
5cf6e669 966AT_PARSER_CHECK([[./input aaaaR]], [m4_if([$2], [with], [0], [1])])
e8b86af8 967
cff92661
AD
968AT_BISON_OPTION_POPDEFS
969
970AT_CLEANUP
dc8e535c
AD
971])
972
5cf6e669
AD
973AT_TEST([], [with])
974AT_TEST([], [without])
975AT_TEST([%define api.value.type variant], [with])
976AT_TEST([%define api.value.type variant], [without])
dc8e535c
AD
977
978m4_popdef([AT_TEST])
99d795e8 979
e1d06b52
AD
980## ------------------------------------- ##
981## C++ GLR parser identifier shadowing. ##
982## ------------------------------------- ##
99d795e8
TR
983
984AT_SETUP([[C++ GLR parser identifier shadowing]])
985
986AT_DATA_GRAMMAR([input.yy], [
987%skeleton "glr.cc"
988
989%union
990{
991 int ival;
992}
993
994%token <ival> ZERO;
995
996%code
997{
998 int yylex (yy::parser::semantic_type *yylval);
999}
1000
1001%%
1002exp: ZERO
1003
1004%%
1005
1006int yylex (yy::parser::semantic_type *yylval)
1007{
32f4c0a1
TR
1008 // Note: this argument is unused, but named on purpose. There used to be a
1009 // bug with a macro that erroneously expanded this identifier to
1010 // yystackp->yyval.
1011 YYUSE (yylval);
99d795e8
TR
1012 return yy::parser::token::ZERO;
1013}
1014
32f4c0a1
TR
1015void yy::parser::error (std::string const&)
1016{}
99d795e8 1017
e1d06b52 1018int main ()
99d795e8
TR
1019{}
1020])
1021
1022AT_BISON_CHECK([[-o input.cc input.yy]])
1023AT_COMPILE_CXX([[input]])
1024
1025AT_CLEANUP