]> git.saurik.com Git - bison.git/blob - tests/c++.at
Merge remote-tracking branch 'origin/maint'
[bison.git] / tests / c++.at
1 # Checking the C++ Features. -*- Autotest -*-
2
3 # Copyright (C) 2004-2005, 2007-2012 Free Software Foundation, Inc.
4
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
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.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 AT_BANNER([[C++ Features.]])
19
20
21 ## ---------- ##
22 ## Variants. ##
23 ## ---------- ##
24
25 # AT_TEST([DIRECTIVES])
26 # ---------------------
27 # Check the support of variants in C++, with the additional DIRECTIVES.
28 m4_pushdef([AT_TEST],
29 [AT_SETUP([Variants $1])
30
31 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" $1])
32 # Store strings and integers in a list of strings.
33 AT_DATA_GRAMMAR([list.yy],
34 [[%debug
35 %skeleton "lalr1.cc"
36 %defines
37 %define variant
38 %locations
39 ]m4_bpatsubst([$1], [\\n], [
40 ])[
41
42 %code requires // code for the .hh file
43 {
44 #include <list>
45 #include <string>
46 typedef std::list<std::string> strings_type;
47 }
48
49 %code // code for the .cc file
50 {
51 #include <iostream>
52 #include <sstream>
53
54 namespace yy
55 {
56 static]AT_TOKEN_CTOR_IF([[
57 parser::symbol_type yylex ()]], [[
58 parser::token_type yylex (parser::semantic_type* yylval,
59 parser::location_type* yylloc)]])[;
60 }
61
62 // Printing a list of strings (for %printer).
63 // Koening look up will look into std, since that's an std::list.
64 namespace std
65 {
66 std::ostream&
67 operator<<(std::ostream& o, const strings_type& s)
68 {
69 o << '(';
70 for (strings_type::const_iterator i = s.begin (); i != s.end (); ++i)
71 {
72 if (i != s.begin ())
73 o << ", ";
74 o << *i;
75 }
76 return o << ')';
77 }
78 }
79
80 // Conversion to string.
81 template <typename T>
82 inline
83 std::string
84 string_cast (const T& t)
85 {
86 std::ostringstream o;
87 o << t;
88 return o.str ();
89 }
90 }
91
92 %token <::std::string> TEXT;
93 %token <int> NUMBER;
94 %token END_OF_FILE 0;
95
96 %type <::std::string> item;
97 // Using the template type to exercize its parsing.
98 // Starting with :: to ensure we don't output "<::" which starts by the
99 // digraph for the left square bracket.
100 %type <::std::list<std::string>> list result;
101
102 %printer { yyo << $][$; }
103 <int> <::std::string> <::std::list<std::string>>;
104 %%
105
106 result:
107 list { std::cout << $][1 << std::endl; }
108 ;
109
110 list:
111 /* nothing */ { /* Generates an empty string list */ }
112 | list item { std::swap ($][$,$][1); $$.push_back ($][2); }
113 | list error { std::swap ($][$,$][1); }
114 ;
115
116 item:
117 TEXT { std::swap ($][$,$][1); }
118 | NUMBER { if ($][1 == 3) YYERROR; else $][$ = string_cast ($][1); }
119 ;
120 %%
121
122 #ifdef TWO_STAGE_BUILD
123 # define BUILD(Type, Value) build<Type> () = Value
124 #else
125 # define BUILD(Type, Value) build (Value)
126 #endif
127
128 #define STAGE_MAX 5
129 namespace yy
130 {
131 static]AT_TOKEN_CTOR_IF([[
132 parser::symbol_type yylex ()]], [[
133 parser::token_type yylex (parser::semantic_type* yylval,
134 parser::location_type* yylloc)]])[
135 {
136 typedef parser::location_type location;
137 static int stage = -1;
138 ++stage;
139 if (stage == STAGE_MAX)
140 {]AT_TOKEN_CTOR_IF([[
141 return parser::make_END_OF_FILE (location ());]], [[
142 *yylloc = location ();
143 return parser::token::END_OF_FILE;]])[
144 }
145 else if (stage % 2)
146 {]AT_TOKEN_CTOR_IF([[
147 return parser::make_NUMBER (stage, location ());]], [[
148 yylval->BUILD (int, stage);
149 *yylloc = location ();
150 return parser::token::NUMBER;]])[
151 }
152 else
153 {]AT_TOKEN_CTOR_IF([[
154 return parser::make_TEXT (string_cast (stage), location ());]], [[
155 yylval->BUILD (std::string, string_cast (stage));
156 *yylloc = location ();
157 return parser::token::TEXT;]])[
158 }
159 abort ();
160 }
161
162 void
163 parser::error (const parser::location_type&, const std::string& message)
164 {
165 std::cerr << message << std::endl;
166 }
167 }
168
169 int
170 main ()
171 {
172 yy::parser p;
173 p.set_debug_level (!!getenv ("YYDEBUG"));
174 return p.parse ();
175 }
176 ]])
177
178 AT_BISON_CHECK([-o list.cc list.yy])
179 AT_COMPILE_CXX([list])
180 AT_CHECK([./list], 0,
181 [(0, 1, 2, 4)
182 ])
183
184 AT_BISON_OPTION_POPDEFS
185 AT_CLEANUP
186 ])
187
188 AT_TEST([])
189 AT_TEST([%define parse.assert])
190 AT_TEST([[%define parse.assert %code {\n#define TWO_STAGE_BUILD\n}]])
191 AT_TEST([[%define parse.assert %define api.token.constructor]])
192 AT_TEST([[%define parse.assert %define api.token.constructor %define api.token.prefix "TOK_"]])
193
194 m4_popdef([AT_TEST])
195
196
197 ## ----------------------- ##
198 ## Doxygen Documentation. ##
199 ## ----------------------- ##
200
201 m4_define([AT_CHECK_DOXYGEN],
202 [m4_case([$1],
203 [Public], [m4_pushdef([AT_DOXYGEN_PRIVATE], [NO])],
204 [Private], [m4_pushdef([AT_DOXYGEN_PRIVATE], [YES])],
205 [m4_fatal([invalid argument: $1])])
206 AT_SETUP([Doxygen $1 Documentation])
207
208 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
209 AT_DATA([input.yy],
210 [[%skeleton "lalr1.cc"
211 %locations
212 %debug
213 %defines
214 %%
215 exp:;
216 %%
217 ]AT_YYERROR_DEFINE[
218 ]])
219
220 AT_BISON_CHECK([-o input.cc input.yy], 0)
221
222 AT_DATA([Doxyfile],
223 [# The PROJECT_NAME tag is a single word (or a sequence of words
224 # surrounded by quotes) that should identify the project.
225 PROJECT_NAME = "Bison C++ Parser"
226
227 # The QUIET tag can be used to turn on/off the messages that are
228 # generated by doxygen. Possible values are YES and NO. If left blank
229 # NO is used.
230 QUIET = YES
231
232 # The WARNINGS tag can be used to turn on/off the warning messages
233 # that are generated by doxygen. Possible values are YES and NO. If
234 # left blank NO is used.
235 WARNINGS = YES
236 # If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate
237 # warnings for undocumented members. If EXTRACT_ALL is set to YES then
238 # this flag will automatically be disabled.
239 WARN_IF_UNDOCUMENTED = YES
240 # If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings
241 # for potential errors in the documentation, such as not documenting
242 # some parameters in a documented function, or documenting parameters
243 # that don't exist or using markup commands wrongly.
244 WARN_IF_DOC_ERROR = YES
245 # The WARN_FORMAT tag determines the format of the warning messages
246 # that doxygen can produce. The string should contain the $file,
247 # $line, and $text tags, which will be replaced by the file and line
248 # number from which the warning originated and the warning text.
249 WARN_FORMAT = "$file:$line: $text"
250
251 # If the EXTRACT_ALL tag is set to YES doxygen will assume all
252 # entities in documentation are documented, even if no documentation
253 # was available. Private class members and static file members will
254 # be hidden unless the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set
255 # to YES
256 EXTRACT_ALL = YES
257
258 # If the EXTRACT_PRIVATE tag is set to YES all private members of a
259 # class will be included in the documentation.
260 EXTRACT_PRIVATE = AT_DOXYGEN_PRIVATE
261
262 # If the EXTRACT_STATIC tag is set to YES all static members of a file
263 # will be included in the documentation.
264 EXTRACT_STATIC = AT_DOXYGEN_PRIVATE
265 ])
266
267 AT_CHECK([doxygen --version || exit 77], 0, ignore)
268 AT_CHECK([doxygen], 0, [], [ignore])
269
270 AT_BISON_OPTION_POPDEFS
271 AT_CLEANUP
272
273 m4_popdef([AT_DOXYGEN_PRIVATE])
274 ])# AT_CHECK_DOXYGEN
275
276 AT_CHECK_DOXYGEN([Public])
277 AT_CHECK_DOXYGEN([Private])
278
279
280 ## ------------ ##
281 ## Namespaces. ##
282 ## ------------ ##
283
284 # AT_CHECK_NAMESPACE(NAMESPACE-DECL, [COMPILE-ERROR])
285 # ---------------------------------------------------
286 # See if Bison can handle %define namespace "NAMESPACE-DECL". If COMPILE-ERROR
287 # is specified, then Bison should accept the input, but compilation will fail,
288 # so don't check compilation.
289 m4_define([AT_CHECK_NAMESPACE],
290 [
291
292 AT_DATA_GRAMMAR([[input.y]],
293 [[%language "C++"
294 %defines
295 %define api.namespace "]$1["
296 %union { int i; }
297 %define global_tokens_and_yystype
298 %locations
299
300 %code {
301 // YYSTYPE contains a namespace reference.
302 int yylex (YYSTYPE *lval, const ]$1[::parser::location_type*) {
303 lval->i = 3;
304 return 0;
305 }
306 }
307
308 %%
309
310 start: ;
311
312 %%
313
314 void
315 ]$1[::parser::error (const ]$1[::parser::location_type &loc,
316 const std::string &msg)
317 {
318 std::cerr << "At " << loc << ": " << msg << std::endl;
319 }
320
321 int
322 main ()
323 {
324 ]$1[::parser p;
325 return p.parse ();
326 }
327 ]])
328
329 AT_BISON_CHECK([[-o input.cc input.y]])
330
331 m4_if([$#], [1],
332 [AT_COMPILE_CXX([[input]], [[input.cc]])
333 AT_PARSER_CHECK([[./input]])])
334
335 ])
336
337 AT_SETUP([[Relative namespace references]])
338 AT_CHECK_NAMESPACE([[foo]])
339 AT_CHECK_NAMESPACE([[foo::bar]])
340 AT_CHECK_NAMESPACE([[foo::bar::baz]])
341 AT_CLEANUP
342
343 AT_SETUP([[Absolute namespace references]])
344 AT_CHECK_NAMESPACE([[::foo]])
345 AT_CHECK_NAMESPACE([[::foo::bar]])
346 AT_CHECK_NAMESPACE([[::foo::bar::baz]])
347 AT_CHECK_NAMESPACE([[ ::foo]])
348 AT_CHECK_NAMESPACE([[ ::foo::bar]])
349 AT_CHECK_NAMESPACE([[ ::foo::bar::baz]])
350 AT_CLEANUP
351
352 AT_SETUP([[Syntactically invalid namespace references]])
353 AT_CHECK_NAMESPACE([[:foo:bar]], [[-]])
354 AT_CHECK_NAMESPACE([[foo: :bar]], [[-]])
355 # This one is interesting because `[3]' is encoded as `@<:@3@:>@', which
356 # contains single occurrences of `:'.
357 AT_CHECK_NAMESPACE([[foo[3]::bar::baz]], [[-]])
358 AT_CHECK_NAMESPACE([[foo::bar,baz]], [[-]])
359 AT_CHECK_NAMESPACE([[foo::bar::(baz /* Pacify Emacs ) */]], [[-]])
360 AT_CLEANUP
361
362
363 ## -------------------------------------- ##
364 ## Syntax error discarding no lookahead. ##
365 ## -------------------------------------- ##
366
367 # After a syntax error, lalr1.cc used to not check whether there
368 # actually is a lookahead before discarding the lookahead. As a result,
369 # it mistakenly invoked the destructor for the previous lookahead.
370
371 AT_SETUP([[Syntax error discarding no lookahead]])
372
373 AT_DATA_GRAMMAR([[input.yy]],
374 [[%skeleton "lalr1.cc"
375
376 %code {
377 #include <string>
378 int yylex (yy::parser::semantic_type *);
379 #define USE(Args)
380 }
381
382 %defines
383 %define parse.error verbose
384
385 %nonassoc 'a' ;
386
387 %destructor {
388 std::cerr << "Discarding 'a'." << std::endl;
389 } 'a'
390
391 %%
392
393 start: error-reduce consistent-error 'a' { USE ($3); };
394
395 error-reduce:
396 'a' 'a' consistent-error 'a' { USE (($1, $2, $4)); }
397 | 'a' error { std::cerr << "Reducing 'a'." << std::endl; USE ($1); }
398 ;
399
400 consistent-error:
401 'a'
402 | /*empty*/ %prec 'a'
403 ;
404
405 // Provide another context in which all rules are useful so that this
406 // test case looks a little more realistic.
407 start: 'b' consistent-error ;
408
409 %%
410
411 int
412 yylex (yy::parser::semantic_type *)
413 {
414 static char const *input = "aa";
415 return *input++;
416 }
417
418 void
419 yy::parser::error (const std::string &m)
420 {
421 std::cerr << m << std::endl;
422 }
423
424 int
425 main ()
426 {
427 yy::parser parser;
428 return parser.parse ();
429 }
430 ]])
431 AT_BISON_CHECK([[-o input.cc input.yy]])
432 AT_COMPILE_CXX([[input]])
433 # This used to print "Discarding 'a'." again at the end.
434 AT_PARSER_CHECK([[./input]], [[1]], [[]],
435 [[syntax error
436 Discarding 'a'.
437 Reducing 'a'.
438 ]])
439
440 AT_CLEANUP
441
442
443 ## --------------------------- ##
444 ## Syntax error as exception. ##
445 ## --------------------------- ##
446
447 AT_SETUP([[Syntax error as exception]])
448
449 AT_DATA_GRAMMAR([[input.yy]],
450 [[%skeleton "lalr1.cc"
451
452 %code
453 {
454 #include <cstdlib>
455 int yylex (yy::parser::semantic_type *);
456 }
457
458 %defines
459 %define variant
460 %define parse.error verbose
461 %define parse.trace
462 %%
463
464 start:
465 thing
466 | start thing
467 ;
468
469 thing:
470 error { std::cerr << "caught error" << std::endl; }
471 | item
472 ;
473
474 item:
475 'a'
476 | 's'
477 {
478 throw yy::parser::syntax_error ("invalid expression");
479 }
480
481 %%
482
483 int
484 yylex (yy::parser::semantic_type *)
485 {
486 // 's': syntax error, 'l': lexical error.
487 static char const *input = "asal";
488 switch (int res = *input++)
489 {
490 case 'l':
491 throw yy::parser::syntax_error ("invalid character");
492 default:
493 return res;
494 }
495 }
496
497 void
498 yy::parser::error (const std::string &m)
499 {
500 std::cerr << "error: " << m << std::endl;
501 }
502
503 int
504 main ()
505 {
506 yy::parser parser;
507 parser.set_debug_level (!!getenv ("YYDEBUG"));
508 return parser.parse ();
509 }
510 ]])
511 AT_BISON_CHECK([[-o input.cc input.yy]])
512 AT_COMPILE_CXX([[input]])
513
514 AT_PARSER_CHECK([[./input]], [[0]], [[]],
515 [[error: invalid expression
516 caught error
517 error: invalid character
518 caught error
519 ]])
520
521 AT_CLEANUP
522
523
524 ## ------------------ ##
525 ## Exception safety. ##
526 ## ------------------ ##
527
528 AT_SETUP([[Exception safety]])
529
530 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
531
532 AT_DATA_GRAMMAR([[input.yy]],
533 [[%skeleton "lalr1.cc"
534 %defines // FIXME: Mandated in 2.6.
535 %debug
536 %error-verbose
537
538 %code requires
539 {
540 #include <cassert>
541 #include <cstdlib> // size_t and getenv.
542 #include <iostream>
543 #include <list>
544
545 bool debug = false;
546
547 /// A class that counts its number of instances.
548 struct Object
549 {
550 typedef std::list<const Object*> objects;
551 static objects instances;
552 char val;
553
554 static bool
555 empty ()
556 {
557 return instances.empty();
558 }
559
560 static void
561 log (Object const *o, const std::string& msg)
562 {
563 if (debug)
564 {
565 if (o)
566 std::cerr << o << "->";
567 std::cerr << msg << " {";
568 const char* sep = " ";
569 for (objects::const_iterator i = instances.begin(),
570 i_end = instances.end();
571 i != i_end;
572 ++i)
573 {
574 std::cerr << sep << *i;
575 sep = ", ";
576 }
577 std::cerr << " }" << std::endl;
578 }
579 }
580
581 Object (char v)
582 : val (v)
583 {
584 instances.push_back(this);
585 log (this, "Object::Object");
586 }
587
588 ~Object ()
589 {
590 instances.remove(this);
591 log (this, "Object::~Object");
592 }
593 };
594 }
595
596 %code
597 {
598 #include <cassert>
599 #include <cstring> // strchr
600 #include <stdexcept>
601 int yylex (yy::parser::semantic_type *);
602 Object::objects Object::instances;
603 static char const *input;
604 }
605
606 %union
607 {
608 Object *obj;
609 }
610
611 %initial-action
612 {
613 if (strchr (input, 'i'))
614 throw std::runtime_error ("initial-action");
615 }
616
617 %destructor { delete $$; } <obj>;
618 %printer
619 {
620 yyo << $$ << " '" << $$->val << '\'';
621 if ($$->val == 'p')
622 throw std::runtime_error ("printer");
623 } <obj>;
624
625 %token <obj> 'a' 'E' 'e' 'p' 'R' 's' 'T'
626 %type <obj> list item
627
628 %%
629
630 start: list { delete $1; };
631
632 list:
633 item { $$ = $1; }
634 | item list { $$ = $1; delete $2; } // Right recursion to load the stack.
635 ;
636
637 item:
638 'a' { $$ = $1; }
639 | 'e' { YYUSE ($$); YYUSE($1); error ("syntax error"); }
640 // Not just 'E', otherwise we reduce when 'E' is the lookahead, and
641 // then the stack is emptied, defeating the point of the test.
642 | 'E' 'a' { YYUSE($1); $$ = $2; }
643 | 'R' { $$ = YY_NULL; delete $1; YYERROR; }
644 | 'p' { $$ = $1; }
645 | 's' { $$ = $1; throw std::runtime_error ("reduction"); }
646 | 'T' { $$ = YY_NULL; delete $1; YYABORT; }
647 | error { $$ = YY_NULL; yyerrok; }
648 ;
649 %%
650
651 int
652 yylex (yy::parser::semantic_type *lvalp)
653 {
654 // 'a': no error.
655 // 'e': user action calls error.
656 // 'E': syntax error, with yyerror that throws.
657 // 'i': initial action throws.
658 // 'l': yylex throws.
659 // 'R': call YYERROR in the action
660 // 's': reduction throws.
661 // 'T': call YYABORT in the action
662 switch (int res = *input++)
663 {
664 case 'l':
665 throw std::runtime_error ("yylex");
666 default:
667 lvalp->obj = new Object (res);
668 // Fall through.
669 case 0:
670 return res;
671 }
672 }
673
674 /* A C++ error reporting function. */
675 void
676 yy::parser::error (const std::string& m)
677 {
678 throw std::runtime_error (m);
679 }
680
681 int
682 main (int argc, const char *argv[])
683 {
684 switch (argc)
685 {
686 case 2:
687 input = argv[1];
688 break;
689 case 3:
690 assert (!strcmp (argv[1], "--debug"));
691 debug = 1;
692 input = argv[2];
693 break;
694 default:
695 abort ();
696 }
697
698 yy::parser parser;
699 debug |= !!getenv ("YYDEBUG");
700 parser.set_debug_level (debug);
701 int res = 2;
702 try
703 {
704 res = parser.parse ();
705 }
706 catch (const std::exception& e)
707 {
708 std::cerr << "exception caught: " << e.what () << std::endl;
709 }
710 catch (...)
711 {
712 std::cerr << "unknown exception caught" << std::endl;
713 }
714 Object::log (YY_NULL, "end");
715 assert (Object::empty());
716 return res;
717 }
718 ]])
719 AT_BISON_CHECK([[-o input.cc --report=all input.yy]])
720 AT_COMPILE_CXX([[input]])
721
722 AT_PARSER_CHECK([[./input aaaas]], [[2]], [[]],
723 [[exception caught: reduction
724 ]])
725
726 AT_PARSER_CHECK([[./input aaaal]], [[2]], [[]],
727 [[exception caught: yylex
728 ]])
729
730 AT_PARSER_CHECK([[./input i]], [[2]], [[]],
731 [[exception caught: initial-action
732 ]])
733
734 AT_PARSER_CHECK([[./input aaaap]])
735
736 AT_PARSER_CHECK([[./input --debug aaaap]], [[2]], [[]], [[stderr]])
737 AT_PARSER_CHECK([[grep '^exception caught: printer$' stderr]], [], [ignore])
738
739 AT_PARSER_CHECK([[./input aaaae]], [[2]], [[]],
740 [[exception caught: syntax error
741 ]])
742
743 AT_PARSER_CHECK([[./input aaaaE]], [[2]], [[]],
744 [[exception caught: syntax error, unexpected $end, expecting 'a'
745 ]])
746
747 AT_PARSER_CHECK([[./input aaaaT]], [[1]])
748
749 # There is error-recovery, so exit success.
750 AT_PARSER_CHECK([[./input aaaaR]], [[0]])
751
752 AT_BISON_OPTION_POPDEFS
753
754 AT_CLEANUP