1 # Checking the C++ Features. -*- Autotest -*-
3 # Copyright (C) 2004-2005, 2007-2013 Free Software Foundation, Inc.
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.
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.
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/>.
18 AT_BANNER([[C++ Features.]])
21 ## --------------------------- ##
22 ## C++ Variant-based Symbols. ##
23 ## --------------------------- ##
25 AT_SETUP([C++ Variant-based Symbols])
27 AT_KEYWORDS([variant])
29 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" %debug $1])
30 # Store strings and integers in a list of strings.
31 AT_DATA_GRAMMAR([list.yy],
32 [[%skeleton "lalr1.cc"
33 %define api.value.type variant
39 // Get access to stack_symbol_type for the tests.
40 # define private public
47 %token <int> INT "int"
48 %type < std::list<int> > exp
50 %printer { yyo << $$; } <int>
53 for (std::list<int>::const_iterator i = $$.begin (); i != $$.end (); ++i)
61 %code requires { #include <list> }
62 %code { int yylex (yy::parser::semantic_type* yylval); }
65 exp: "int" { $$.push_back ($1); }
73 yy::parser::symbol_type s = yy::parser::make_INT(12);
74 std::cerr << s.value.as<int>() << std::endl;
78 yy::parser::symbol_type s = yy::parser::make_INT(123);
79 yy::parser::stack_symbol_type ss(1, s);
80 std::cerr << ss.value.as<int>() << std::endl;
84 yy::parser::stack_type st;
85 for (int i = 0; i < 100; ++i)
87 yy::parser::symbol_type s(yy::parser::make_INT(i));
88 yy::parser::stack_symbol_type ss(1, s);
95 AT_BISON_CHECK([-o list.cc list.yy])
96 AT_COMPILE_CXX([list], [$NO_STRICT_ALIAS_CXXFLAGS list.cc])
97 AT_PARSER_CHECK([./list], 0, [],
102 AT_BISON_OPTION_POPDEFS
110 # AT_TEST([DIRECTIVES])
111 # ---------------------
112 # Check the support of variants in C++, with the additional DIRECTIVES.
113 m4_pushdef([AT_TEST],
114 [AT_SETUP([Variants $1])
116 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" $1])
117 # Store strings and integers in a list of strings.
118 AT_DATA_GRAMMAR([list.yy],
122 %define api.value.type variant
123 ]m4_bpatsubst([$1], [\\n], [
126 %code requires // code for the .hh file
130 typedef std::list<std::string> strings_type;
133 %code // code for the .cc file
135 #include <cstdlib> // abort, getenv
141 static]AT_TOKEN_CTOR_IF([[
142 parser::symbol_type yylex ()]], [[
143 parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([,
144 parser::location_type* yylloc])[)]])[;
147 // Printing a list of strings (for %printer).
148 // Koening look up will look into std, since that's an std::list.
152 operator<<(std::ostream& o, const strings_type& s)
155 for (strings_type::const_iterator i = s.begin (); i != s.end (); ++i)
165 // Conversion to string.
166 template <typename T>
169 string_cast (const T& t)
171 std::ostringstream o;
177 %token <::std::string> TEXT;
179 %token END_OF_FILE 0;
181 %type <::std::string> item;
182 // Using the template type to exercize its parsing.
183 // Starting with :: to ensure we don't output "<::" which starts by the
184 // digraph for the left square bracket.
185 %type <::std::list<std::string>> list result;
187 %printer { yyo << $][$; }
188 <int> <::std::string> <::std::list<std::string>>;
192 list { std::cout << $][1 << std::endl; }
196 /* nothing */ { /* Generates an empty string list */ }
197 | list item { std::swap ($][$,$][1); $$.push_back ($][2); }
198 | list error { std::swap ($][$,$][1); }
202 TEXT { std::swap ($][$,$][1); }
203 | NUMBER { if ($][1 == 3) YYERROR; else $][$ = string_cast ($][1); }
207 #ifdef TWO_STAGE_BUILD
208 # define BUILD(Type, Value) build<Type> () = Value
210 # define BUILD(Type, Value) build (Value)
216 static]AT_TOKEN_CTOR_IF([[
217 parser::symbol_type yylex ()]], [[
218 parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([,
219 parser::location_type* yylloc])[)]])[
221 typedef parser::location_type location;])[
222 static int stage = -1;
224 if (stage == STAGE_MAX)
225 {]AT_TOKEN_CTOR_IF([[
226 return parser::make_END_OF_FILE (]AT_LOCATION_IF([location ()])[);]],
228 *yylloc = location ();])[
229 return parser::token::END_OF_FILE;]])[
232 {]AT_TOKEN_CTOR_IF([[
233 return parser::make_NUMBER (stage]AT_LOCATION_IF([, location ()])[);]],
235 yylval->BUILD (int, stage);]AT_LOCATION_IF([
236 *yylloc = location ();])[
237 return parser::token::NUMBER;]])[
240 {]AT_TOKEN_CTOR_IF([[
241 return parser::make_TEXT (string_cast (stage)]AT_LOCATION_IF([, location ()])[);]], [[
242 yylval->BUILD (std::string, string_cast (stage));]AT_LOCATION_IF([
243 *yylloc = location ();])[
244 return parser::token::TEXT;]])[
254 AT_BISON_CHECK([-o list.cc list.yy])
255 AT_COMPILE_CXX([list], [$NO_STRICT_ALIAS_CXXFLAGS list.cc])
256 AT_PARSER_CHECK([./list], 0,
260 AT_BISON_OPTION_POPDEFS
265 AT_TEST([%define parse.assert])
266 AT_TEST([%locations %define parse.assert])
267 AT_TEST([[%define parse.assert %code {\n#define TWO_STAGE_BUILD\n}]])
268 AT_TEST([[%define parse.assert %define api.token.constructor]])
269 AT_TEST([[%define parse.assert %define api.token.constructor %define api.token.prefix "TOK_"]])
270 AT_TEST([[%locations %define parse.assert %define api.token.constructor %define api.token.prefix "TOK_"]])
275 ## ----------------------- ##
276 ## Doxygen Documentation. ##
277 ## ----------------------- ##
279 m4_define([AT_CHECK_DOXYGEN],
281 [Public], [m4_pushdef([AT_DOXYGEN_PRIVATE], [NO])],
282 [Private], [m4_pushdef([AT_DOXYGEN_PRIVATE], [YES])],
283 [m4_fatal([invalid argument: $1])])
284 AT_SETUP([Doxygen $1 Documentation])
286 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
288 [[%skeleton "lalr1.cc"
298 AT_BISON_CHECK([-o input.cc input.yy], 0)
301 [# The PROJECT_NAME tag is a single word (or a sequence of words
302 # surrounded by quotes) that should identify the project.
303 PROJECT_NAME = "Bison C++ Parser"
305 # The QUIET tag can be used to turn on/off the messages that are
306 # generated by doxygen. Possible values are YES and NO. If left blank
310 # The WARNINGS tag can be used to turn on/off the warning messages
311 # that are generated by doxygen. Possible values are YES and NO. If
312 # left blank NO is used.
314 # If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate
315 # warnings for undocumented members. If EXTRACT_ALL is set to YES then
316 # this flag will automatically be disabled.
317 WARN_IF_UNDOCUMENTED = YES
318 # If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings
319 # for potential errors in the documentation, such as not documenting
320 # some parameters in a documented function, or documenting parameters
321 # that don't exist or using markup commands wrongly.
322 WARN_IF_DOC_ERROR = YES
323 # The WARN_FORMAT tag determines the format of the warning messages
324 # that doxygen can produce. The string should contain the $file,
325 # $line, and $text tags, which will be replaced by the file and line
326 # number from which the warning originated and the warning text.
327 WARN_FORMAT = "$file:$line: $text"
329 # If the EXTRACT_ALL tag is set to YES doxygen will assume all
330 # entities in documentation are documented, even if no documentation
331 # was available. Private class members and static file members will
332 # be hidden unless the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set
336 # If the EXTRACT_PRIVATE tag is set to YES all private members of a
337 # class will be included in the documentation.
338 EXTRACT_PRIVATE = AT_DOXYGEN_PRIVATE
340 # If the EXTRACT_STATIC tag is set to YES all static members of a file
341 # will be included in the documentation.
342 EXTRACT_STATIC = AT_DOXYGEN_PRIVATE
345 AT_CHECK([doxygen --version || exit 77], 0, ignore)
346 AT_CHECK([doxygen], 0, [], [ignore])
348 AT_BISON_OPTION_POPDEFS
351 m4_popdef([AT_DOXYGEN_PRIVATE])
354 AT_CHECK_DOXYGEN([Public])
355 AT_CHECK_DOXYGEN([Private])
362 # AT_TEST(NAMESPACE-DECL, [COMPILE-ERROR])
363 # ----------------------------------------
364 # See if Bison can handle %define namespace "NAMESPACE-DECL". If COMPILE-ERROR
365 # is specified, then Bison should accept the input, but compilation will fail,
366 # so don't check compilation.
367 m4_pushdef([AT_TEST],
368 [AT_BISON_OPTION_PUSHDEFS([%language "C++" %define api.namespace "$1"])
369 AT_DATA_GRAMMAR([[input.y]],
372 %define api.namespace "]$1["
374 %define global_tokens_and_yystype
378 // YYSTYPE contains a namespace reference.
379 int yylex (YYSTYPE *lval, const ]$1[::parser::location_type*) {
392 ]$1[::parser::error (const ]$1[::parser::location_type &loc,
393 const std::string &msg)
395 std::cerr << "At " << loc << ": " << msg << std::endl;
401 AT_BISON_CHECK([[-o input.cc input.y]])
404 [AT_COMPILE_CXX([[input]], [[input.cc]])
405 AT_PARSER_CHECK([[./input]])])
406 AT_BISON_OPTION_POPDEFS
409 AT_SETUP([[Relative namespace references]])
411 AT_TEST([[foo::bar]])
412 AT_TEST([[foo::bar::baz]])
415 AT_SETUP([[Absolute namespace references]])
417 AT_TEST([[::foo::bar]])
418 AT_TEST([[::foo::bar::baz]])
420 AT_TEST([[ ::foo::bar]])
421 AT_TEST([[ ::foo::bar::baz]])
424 AT_SETUP([[Syntactically invalid namespace references]])
425 AT_TEST([[:foo:bar]], [[-]])
426 AT_TEST([[foo: :bar]], [[-]])
427 # This one is interesting because `[3]' is encoded as `@<:@3@:>@', which
428 # contains single occurrences of `:'.
429 AT_TEST([[foo[3]::bar::baz]], [[-]])
430 AT_TEST([[foo::bar,baz]], [[-]])
431 AT_TEST([[foo::bar::(baz /* Pacify Emacs ) */]], [[-]])
436 ## -------------------------------------- ##
437 ## Syntax error discarding no lookahead. ##
438 ## -------------------------------------- ##
440 # After a syntax error, lalr1.cc used to not check whether there
441 # actually is a lookahead before discarding the lookahead. As a result,
442 # it mistakenly invoked the destructor for the previous lookahead.
444 AT_SETUP([[Syntax error discarding no lookahead]])
446 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
448 AT_DATA_GRAMMAR([[input.yy]],
449 [[%skeleton "lalr1.cc"
453 int yylex (yy::parser::semantic_type *);
458 %define parse.error verbose
463 std::cerr << "Discarding 'a'." << std::endl;
468 start: error-reduce consistent-error 'a' { USE ($3); };
471 'a' 'a' consistent-error 'a' { USE (($1, $2, $4)); }
472 | 'a' error { std::cerr << "Reducing 'a'." << std::endl; USE ($1); }
477 | /*empty*/ %prec 'a'
480 // Provide another context in which all rules are useful so that this
481 // test case looks a little more realistic.
482 start: 'b' consistent-error ;
487 yylex (yy::parser::semantic_type *)
489 static char const *input = "aa";
494 yy::parser::error (const std::string &m)
496 std::cerr << m << std::endl;
502 AT_BISON_CHECK([[-o input.cc input.yy]])
503 AT_COMPILE_CXX([[input]])
504 # This used to print "Discarding 'a'." again at the end.
505 AT_PARSER_CHECK([[./input]], [[1]], [[]],
511 AT_BISON_OPTION_POPDEFS
515 ## --------------------------- ##
516 ## Syntax error as exception. ##
517 ## --------------------------- ##
519 AT_SETUP([[Syntax error as exception]])
521 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
523 AT_DATA_GRAMMAR([[input.yy]],
524 [[%skeleton "lalr1.cc"
529 int yylex (yy::parser::semantic_type *);
533 %define api.value.type variant
534 %define parse.error verbose
544 error { std::cerr << "caught error" << std::endl; }
552 throw yy::parser::syntax_error ("invalid expression");
558 yylex (yy::parser::semantic_type *)
560 // 's': syntax error, 'l': lexical error.
561 static char const *input = "asal";
562 switch (int res = *input++)
565 throw yy::parser::syntax_error ("invalid character");
572 yy::parser::error (const std::string &m)
574 std::cerr << "error: " << m << std::endl;
579 AT_BISON_CHECK([[-o input.cc input.yy]])
580 AT_COMPILE_CXX([[input]])
582 AT_PARSER_CHECK([[./input]], [[0]], [[]],
583 [[error: invalid expression
585 error: invalid character
589 AT_BISON_OPTION_POPDEFS
593 ## ------------------ ##
594 ## Exception safety. ##
595 ## ------------------ ##
597 AT_SETUP([[Exception safety]])
599 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
601 AT_DATA_GRAMMAR([[input.yy]],
602 [[%skeleton "lalr1.cc"
603 %defines // FIXME: Mandated in 2.6.
610 #include <cstdlib> // size_t and getenv.
616 /// A class that counts its number of instances.
619 typedef std::list<const Object*> objects;
620 static objects instances;
626 return instances.empty();
630 log (Object const *o, const std::string& msg)
635 std::cerr << o << "->";
636 std::cerr << msg << " {";
637 const char* sep = " ";
638 for (objects::const_iterator i = instances.begin(),
639 i_end = instances.end();
643 std::cerr << sep << *i;
646 std::cerr << " }" << std::endl;
653 instances.push_back(this);
654 log (this, "Object::Object");
659 instances.remove(this);
660 log (this, "Object::~Object");
668 #include <cstring> // strchr
670 int yylex (yy::parser::semantic_type *);
671 Object::objects Object::instances;
672 static char const *input;
682 if (strchr (input, 'i'))
683 throw std::runtime_error ("initial-action");
686 %destructor { delete $$; } <obj>;
689 yyo << $$ << " '" << $$->val << '\'';
691 throw std::runtime_error ("printer");
694 %token <obj> 'a' 'E' 'e' 'p' 'R' 's' 'T'
695 %type <obj> list item
699 start: list { delete $1; };
703 | item list { $$ = $1; delete $2; } // Right recursion to load the stack.
708 | 'e' { YYUSE ($$); YYUSE($1); error ("syntax error"); }
709 // Not just 'E', otherwise we reduce when 'E' is the lookahead, and
710 // then the stack is emptied, defeating the point of the test.
711 | 'E' 'a' { YYUSE($1); $$ = $2; }
712 | 'R' { $$ = YY_NULL; delete $1; YYERROR; }
714 | 's' { $$ = $1; throw std::runtime_error ("reduction"); }
715 | 'T' { $$ = YY_NULL; delete $1; YYABORT; }
716 | error { $$ = YY_NULL; yyerrok; }
721 yylex (yy::parser::semantic_type *lvalp)
724 // 'e': user action calls error.
725 // 'E': syntax error, with yyerror that throws.
726 // 'i': initial action throws.
727 // 'l': yylex throws.
728 // 'R': call YYERROR in the action
729 // 's': reduction throws.
730 // 'T': call YYABORT in the action
731 switch (int res = *input++)
734 throw std::runtime_error ("yylex");
736 lvalp->obj = new Object (res);
743 /* A C++ error reporting function. */
745 yy::parser::error (const std::string& m)
747 throw std::runtime_error (m);
751 main (int argc, const char *argv[])
759 assert (std::string(argv[1]) == "--debug");
768 debug |= !!getenv ("YYDEBUG");
769 parser.set_debug_level (debug);
773 res = parser.parse ();
775 catch (const std::exception& e)
777 std::cerr << "exception caught: " << e.what () << std::endl;
781 std::cerr << "unknown exception caught" << std::endl;
783 Object::log (YY_NULL, "end");
784 assert (Object::empty());
788 AT_BISON_CHECK([[-o input.cc --report=all input.yy]])
789 AT_COMPILE_CXX([[input]])
791 AT_PARSER_CHECK([[./input aaaas]], [[2]], [[]],
792 [[exception caught: reduction
795 AT_PARSER_CHECK([[./input aaaal]], [[2]], [[]],
796 [[exception caught: yylex
799 AT_PARSER_CHECK([[./input i]], [[2]], [[]],
800 [[exception caught: initial-action
803 AT_PARSER_CHECK([[./input aaaap]])
805 AT_PARSER_CHECK([[./input --debug aaaap]], [[2]], [[]], [[stderr]])
806 AT_CHECK([[grep '^exception caught: printer$' stderr]], [], [ignore])
808 AT_PARSER_CHECK([[./input aaaae]], [[2]], [[]],
809 [[exception caught: syntax error
812 AT_PARSER_CHECK([[./input aaaaE]], [[2]], [[]],
813 [[exception caught: syntax error, unexpected $end, expecting 'a'
816 AT_PARSER_CHECK([[./input aaaaT]], [[1]])
818 # There is error-recovery, so exit success.
819 AT_PARSER_CHECK([[./input aaaaR]], [[0]])
821 AT_BISON_OPTION_POPDEFS
825 ## ------------------------------------ ##
826 ## C++ GLR parser identifier shadowing ##
827 ## ------------------------------------ ##
829 AT_SETUP([[C++ GLR parser identifier shadowing]])
831 AT_DATA_GRAMMAR([input.yy], [
843 int yylex (yy::parser::semantic_type *yylval);
851 int yylex (yy::parser::semantic_type *yylval)
853 // Note: this argument is unused, but named on purpose. There used to be a
854 // bug with a macro that erroneously expanded this identifier to
857 return yy::parser::token::ZERO;
860 void yy::parser::error (std::string const&)
867 AT_BISON_CHECK([[-o input.cc input.yy]])
868 AT_COMPILE_CXX([[input]])