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