]> git.saurik.com Git - bison.git/blame_incremental - tests/c++.at
build: fix VPATH issue
[bison.git] / tests / c++.at
... / ...
CommitLineData
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
18AT_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.
28m4_pushdef([AT_TEST],
29[AT_SETUP([Variants $1])
30
31AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" $1])
32# Store strings and integers in a list of strings.
33AT_DATA_GRAMMAR([list.yy],
34[[%debug
35%skeleton "lalr1.cc"
36%defines
37%define api.value.type variant
38]m4_bpatsubst([$1], [\\n], [
39])[
40
41%code requires // code for the .hh file
42{
43#include <list>
44#include <string>
45typedef std::list<std::string> strings_type;
46}
47
48%code // code for the .cc file
49{
50#include <cstdlib> // abort, getenv
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]AT_LOCATION_IF([,
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
106result:
107 list { std::cout << $][1 << std::endl; }
108;
109
110list:
111 /* nothing */ { /* Generates an empty string list */ }
112| list item { std::swap ($][$,$][1); $$.push_back ($][2); }
113| list error { std::swap ($][$,$][1); }
114;
115
116item:
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
129namespace yy
130{
131 static]AT_TOKEN_CTOR_IF([[
132 parser::symbol_type yylex ()]], [[
133 parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([,
134 parser::location_type* yylloc])[)]])[
135 {]AT_LOCATION_IF([
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 (]AT_LOCATION_IF([location ()])[);]],
142[AT_LOCATION_IF([
143 *yylloc = location ();])[
144 return parser::token::END_OF_FILE;]])[
145 }
146 else if (stage % 2)
147 {]AT_TOKEN_CTOR_IF([[
148 return parser::make_NUMBER (stage]AT_LOCATION_IF([, location ()])[);]],
149[[
150 yylval->BUILD (int, stage);]AT_LOCATION_IF([
151 *yylloc = location ();])[
152 return parser::token::NUMBER;]])[
153 }
154 else
155 {]AT_TOKEN_CTOR_IF([[
156 return parser::make_TEXT (string_cast (stage)]AT_LOCATION_IF([, location ()])[);]], [[
157 yylval->BUILD (std::string, string_cast (stage));]AT_LOCATION_IF([
158 *yylloc = location ();])[
159 return parser::token::TEXT;]])[
160 }
161 abort ();
162 }
163}
164
165]AT_YYERROR_DEFINE[
166
167int
168main ()
169{
170 yy::parser p;
171 p.set_debug_level (!!getenv ("YYDEBUG"));
172 return p.parse ();
173}
174]])
175
176AT_BISON_CHECK([-o list.cc list.yy])
177AT_COMPILE_CXX([list], [$NO_STRICT_ALIAS_CXXFLAGS list.cc])
178AT_PARSER_CHECK([./list], 0,
179[(0, 1, 2, 4)
180])
181
182AT_BISON_OPTION_POPDEFS
183AT_CLEANUP
184])
185
186AT_TEST([])
187AT_TEST([%define parse.assert])
188AT_TEST([%locations %define parse.assert])
189AT_TEST([[%define parse.assert %code {\n#define TWO_STAGE_BUILD\n}]])
190AT_TEST([[%define parse.assert %define api.token.constructor]])
191AT_TEST([[%define parse.assert %define api.token.constructor %define api.token.prefix "TOK_"]])
192AT_TEST([[%locations %define parse.assert %define api.token.constructor %define api.token.prefix "TOK_"]])
193
194m4_popdef([AT_TEST])
195
196
197## ----------------------- ##
198## Doxygen Documentation. ##
199## ----------------------- ##
200
201m4_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])])
206AT_SETUP([Doxygen $1 Documentation])
207
208AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
209AT_DATA([input.yy],
210[[%skeleton "lalr1.cc"
211%locations
212%debug
213%defines
214%%
215exp:;
216%%
217]AT_YYERROR_DEFINE[
218]])
219
220AT_BISON_CHECK([-o input.cc input.yy], 0)
221
222AT_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.
225PROJECT_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.
230QUIET = 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.
235WARNINGS = 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.
239WARN_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.
244WARN_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.
249WARN_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
256EXTRACT_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.
260EXTRACT_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.
264EXTRACT_STATIC = AT_DOXYGEN_PRIVATE
265])
266
267AT_CHECK([doxygen --version || exit 77], 0, ignore)
268AT_CHECK([doxygen], 0, [], [ignore])
269
270AT_BISON_OPTION_POPDEFS
271AT_CLEANUP
272
273m4_popdef([AT_DOXYGEN_PRIVATE])
274])# AT_CHECK_DOXYGEN
275
276AT_CHECK_DOXYGEN([Public])
277AT_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.
289m4_define([AT_CHECK_NAMESPACE],
290[
291
292AT_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
310start: ;
311
312%%
313
314void
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
321int
322main ()
323{
324 ]$1[::parser p;
325 return p.parse ();
326}
327]])
328
329AT_BISON_CHECK([[-o input.cc input.y]])
330
331m4_if([$#], [1],
332[AT_COMPILE_CXX([[input]], [[input.cc]])
333AT_PARSER_CHECK([[./input]])])
334
335])
336
337AT_SETUP([[Relative namespace references]])
338AT_CHECK_NAMESPACE([[foo]])
339AT_CHECK_NAMESPACE([[foo::bar]])
340AT_CHECK_NAMESPACE([[foo::bar::baz]])
341AT_CLEANUP
342
343AT_SETUP([[Absolute namespace references]])
344AT_CHECK_NAMESPACE([[::foo]])
345AT_CHECK_NAMESPACE([[::foo::bar]])
346AT_CHECK_NAMESPACE([[::foo::bar::baz]])
347AT_CHECK_NAMESPACE([[ ::foo]])
348AT_CHECK_NAMESPACE([[ ::foo::bar]])
349AT_CHECK_NAMESPACE([[ ::foo::bar::baz]])
350AT_CLEANUP
351
352AT_SETUP([[Syntactically invalid namespace references]])
353AT_CHECK_NAMESPACE([[:foo:bar]], [[-]])
354AT_CHECK_NAMESPACE([[foo: :bar]], [[-]])
355# This one is interesting because `[3]' is encoded as `@<:@3@:>@', which
356# contains single occurrences of `:'.
357AT_CHECK_NAMESPACE([[foo[3]::bar::baz]], [[-]])
358AT_CHECK_NAMESPACE([[foo::bar,baz]], [[-]])
359AT_CHECK_NAMESPACE([[foo::bar::(baz /* Pacify Emacs ) */]], [[-]])
360AT_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
371AT_SETUP([[Syntax error discarding no lookahead]])
372
373AT_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
393start: error-reduce consistent-error 'a' { USE ($3); };
394
395error-reduce:
396 'a' 'a' consistent-error 'a' { USE (($1, $2, $4)); }
397| 'a' error { std::cerr << "Reducing 'a'." << std::endl; USE ($1); }
398;
399
400consistent-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.
407start: 'b' consistent-error ;
408
409%%
410
411int
412yylex (yy::parser::semantic_type *)
413{
414 static char const *input = "aa";
415 return *input++;
416}
417
418void
419yy::parser::error (const std::string &m)
420{
421 std::cerr << m << std::endl;
422}
423
424int
425main ()
426{
427 yy::parser parser;
428 return parser.parse ();
429}
430]])
431AT_BISON_CHECK([[-o input.cc input.yy]])
432AT_COMPILE_CXX([[input]])
433# This used to print "Discarding 'a'." again at the end.
434AT_PARSER_CHECK([[./input]], [[1]], [[]],
435[[syntax error
436Discarding 'a'.
437Reducing 'a'.
438]])
439
440AT_CLEANUP
441
442
443## --------------------------- ##
444## Syntax error as exception. ##
445## --------------------------- ##
446
447AT_SETUP([[Syntax error as exception]])
448
449AT_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 api.value.type variant
460%define parse.error verbose
461%define parse.trace
462%%
463
464start:
465 thing
466| start thing
467;
468
469thing:
470 error { std::cerr << "caught error" << std::endl; }
471| item
472;
473
474item:
475 'a'
476| 's'
477 {
478 throw yy::parser::syntax_error ("invalid expression");
479 }
480
481%%
482
483int
484yylex (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
497void
498yy::parser::error (const std::string &m)
499{
500 std::cerr << "error: " << m << std::endl;
501}
502
503int
504main ()
505{
506 yy::parser parser;
507 parser.set_debug_level (!!getenv ("YYDEBUG"));
508 return parser.parse ();
509}
510]])
511AT_BISON_CHECK([[-o input.cc input.yy]])
512AT_COMPILE_CXX([[input]])
513
514AT_PARSER_CHECK([[./input]], [[0]], [[]],
515[[error: invalid expression
516caught error
517error: invalid character
518caught error
519]])
520
521AT_CLEANUP
522
523
524## ------------------ ##
525## Exception safety. ##
526## ------------------ ##
527
528AT_SETUP([[Exception safety]])
529
530AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
531
532AT_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
630start: list { delete $1; };
631
632list:
633 item { $$ = $1; }
634| item list { $$ = $1; delete $2; } // Right recursion to load the stack.
635;
636
637item:
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
651int
652yylex (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. */
675void
676yy::parser::error (const std::string& m)
677{
678 throw std::runtime_error (m);
679}
680
681int
682main (int argc, const char *argv[])
683{
684 switch (argc)
685 {
686 case 2:
687 input = argv[1];
688 break;
689 case 3:
690 assert (std::string(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]])
719AT_BISON_CHECK([[-o input.cc --report=all input.yy]])
720AT_COMPILE_CXX([[input]])
721
722AT_PARSER_CHECK([[./input aaaas]], [[2]], [[]],
723[[exception caught: reduction
724]])
725
726AT_PARSER_CHECK([[./input aaaal]], [[2]], [[]],
727[[exception caught: yylex
728]])
729
730AT_PARSER_CHECK([[./input i]], [[2]], [[]],
731[[exception caught: initial-action
732]])
733
734AT_PARSER_CHECK([[./input aaaap]])
735
736AT_PARSER_CHECK([[./input --debug aaaap]], [[2]], [[]], [[stderr]])
737AT_CHECK([[grep '^exception caught: printer$' stderr]], [], [ignore])
738
739AT_PARSER_CHECK([[./input aaaae]], [[2]], [[]],
740[[exception caught: syntax error
741]])
742
743AT_PARSER_CHECK([[./input aaaaE]], [[2]], [[]],
744[[exception caught: syntax error, unexpected $end, expecting 'a'
745]])
746
747AT_PARSER_CHECK([[./input aaaaT]], [[1]])
748
749# There is error-recovery, so exit success.
750AT_PARSER_CHECK([[./input aaaaR]], [[0]])
751
752AT_BISON_OPTION_POPDEFS
753
754AT_CLEANUP
755
756## ------------------------------------ ##
757## C++ GLR parser identifier shadowing ##
758## ------------------------------------ ##
759
760AT_SETUP([[C++ GLR parser identifier shadowing]])
761
762AT_DATA_GRAMMAR([input.yy], [
763%skeleton "glr.cc"
764
765%union
766{
767 int ival;
768}
769
770%token <ival> ZERO;
771
772%code
773{
774 int yylex (yy::parser::semantic_type *yylval);
775}
776
777%%
778exp: ZERO
779
780%%
781
782int yylex (yy::parser::semantic_type *yylval)
783{
784 return yy::parser::token::ZERO;
785}
786
787void yy::parser::error (std::string const& msg)
788{}
789
790int main()
791{}
792])
793
794AT_BISON_CHECK([[-o input.cc input.yy]])
795AT_COMPILE_CXX([[input]])
796
797AT_CLEANUP