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" %debug $1])
117 # Store strings and integers in a list of strings.
118 AT_DATA_GRAMMAR([list.yy],
121 %define api.value.type variant
122 ]m4_bpatsubst([$1], [\\n], [
125 %code requires // code for the .hh file
129 typedef std::list<std::string> strings_type;
132 %code // code for the .cc file
134 #include <cstdlib> // abort, getenv
140 static]AT_TOKEN_CTOR_IF([[
141 parser::symbol_type yylex ()]], [[
142 parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([,
143 parser::location_type* yylloc])[)]])[;
146 // Printing a list of strings (for %printer).
147 // Koening look up will look into std, since that's an std::list.
151 operator<<(std::ostream& o, const strings_type& s)
154 for (strings_type::const_iterator i = s.begin (); i != s.end (); ++i)
164 // Conversion to string.
165 template <typename T>
168 string_cast (const T& t)
170 std::ostringstream o;
176 %token <::std::string> TEXT;
178 %token END_OF_FILE 0;
180 %type <::std::string> item;
181 // Using the template type to exercize its parsing.
182 // Starting with :: to ensure we don't output "<::" which starts by the
183 // digraph for the left square bracket.
184 %type <::std::list<std::string>> list result;
186 %printer { yyo << $][$; }
187 <int> <::std::string> <::std::list<std::string>>;
191 list { std::cout << $][1 << std::endl; }
195 /* nothing */ { /* Generates an empty string list */ }
196 | list item { std::swap ($][$,$][1); $$.push_back ($][2); }
197 | list error { std::swap ($][$,$][1); }
201 TEXT { std::swap ($][$,$][1); }
202 | NUMBER { if ($][1 == 3) YYERROR; else $][$ = string_cast ($][1); }
205 ]AT_TOKEN_CTOR_IF([],
206 [[#ifdef TWO_STAGE_BUILD
207 # define BUILD(Type, Value) build<Type> () = Value
209 # define BUILD(Type, Value) build (Value)
215 static]AT_TOKEN_CTOR_IF([[
216 parser::symbol_type yylex ()]], [[
217 parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([,
218 parser::location_type* yylloc])[)]])[
220 typedef parser::location_type location;])[
221 static int stage = -1;
223 if (stage == STAGE_MAX)
224 {]AT_TOKEN_CTOR_IF([[
225 return parser::make_END_OF_FILE (]AT_LOCATION_IF([location ()])[);]],
227 *yylloc = location ();])[
228 return parser::token::END_OF_FILE;]])[
231 {]AT_TOKEN_CTOR_IF([[
232 return parser::make_NUMBER (stage]AT_LOCATION_IF([, location ()])[);]],
234 yylval->BUILD (int, stage);]AT_LOCATION_IF([
235 *yylloc = location ();])[
236 return parser::token::NUMBER;]])[
239 {]AT_TOKEN_CTOR_IF([[
240 return parser::make_TEXT (string_cast (stage)]AT_LOCATION_IF([, location ()])[);]], [[
241 yylval->BUILD (std::string, string_cast (stage));]AT_LOCATION_IF([
242 *yylloc = location ();])[
243 return parser::token::TEXT;]])[
253 AT_BISON_CHECK([-o list.cc list.yy])
254 AT_COMPILE_CXX([list], [$NO_STRICT_ALIAS_CXXFLAGS list.cc])
255 AT_PARSER_CHECK([./list], 0,
259 AT_BISON_OPTION_POPDEFS
264 AT_TEST([%define parse.assert])
265 AT_TEST([%locations %define parse.assert])
266 AT_TEST([[%define parse.assert %code {\n#define TWO_STAGE_BUILD\n}]])
267 AT_TEST([[%define parse.assert %define api.token.constructor]])
268 AT_TEST([[%define parse.assert %define api.token.constructor %define api.token.prefix "TOK_"]])
269 AT_TEST([[%locations %define parse.assert %define api.token.constructor %define api.token.prefix "TOK_"]])
274 ## ----------------------- ##
275 ## Doxygen Documentation. ##
276 ## ----------------------- ##
278 m4_define([AT_CHECK_DOXYGEN],
280 [Public], [m4_pushdef([AT_DOXYGEN_PRIVATE], [NO])],
281 [Private], [m4_pushdef([AT_DOXYGEN_PRIVATE], [YES])],
282 [m4_fatal([invalid argument: $1])])
283 AT_SETUP([Doxygen $1 Documentation])
285 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
287 [[%skeleton "lalr1.cc"
296 AT_BISON_CHECK([-o input.cc input.yy], 0)
299 [# The PROJECT_NAME tag is a single word (or a sequence of words
300 # surrounded by quotes) that should identify the project.
301 PROJECT_NAME = "Bison C++ Parser"
303 # The QUIET tag can be used to turn on/off the messages that are
304 # generated by doxygen. Possible values are YES and NO. If left blank
308 # The WARNINGS tag can be used to turn on/off the warning messages
309 # that are generated by doxygen. Possible values are YES and NO. If
310 # left blank NO is used.
312 # If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate
313 # warnings for undocumented members. If EXTRACT_ALL is set to YES then
314 # this flag will automatically be disabled.
315 WARN_IF_UNDOCUMENTED = YES
316 # If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings
317 # for potential errors in the documentation, such as not documenting
318 # some parameters in a documented function, or documenting parameters
319 # that don't exist or using markup commands wrongly.
320 WARN_IF_DOC_ERROR = YES
321 # The WARN_FORMAT tag determines the format of the warning messages
322 # that doxygen can produce. The string should contain the $file,
323 # $line, and $text tags, which will be replaced by the file and line
324 # number from which the warning originated and the warning text.
325 WARN_FORMAT = "$file:$line: $text"
327 # If the EXTRACT_ALL tag is set to YES doxygen will assume all
328 # entities in documentation are documented, even if no documentation
329 # was available. Private class members and static file members will
330 # be hidden unless the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set
334 # If the EXTRACT_PRIVATE tag is set to YES all private members of a
335 # class will be included in the documentation.
336 EXTRACT_PRIVATE = AT_DOXYGEN_PRIVATE
338 # If the EXTRACT_STATIC tag is set to YES all static members of a file
339 # will be included in the documentation.
340 EXTRACT_STATIC = AT_DOXYGEN_PRIVATE
343 AT_CHECK([doxygen --version || exit 77], 0, ignore)
344 AT_CHECK([doxygen], 0, [], [ignore])
346 AT_BISON_OPTION_POPDEFS
349 m4_popdef([AT_DOXYGEN_PRIVATE])
352 AT_CHECK_DOXYGEN([Public])
353 AT_CHECK_DOXYGEN([Private])
360 # AT_TEST(NAMESPACE-DECL, [COMPILE-ERROR])
361 # ----------------------------------------
362 # See if Bison can handle %define namespace "NAMESPACE-DECL". If COMPILE-ERROR
363 # is specified, then Bison should accept the input, but compilation will fail,
364 # so don't check compilation.
365 m4_pushdef([AT_TEST],
366 [AT_BISON_OPTION_PUSHDEFS([%language "C++" %define api.namespace "$1"])
367 AT_DATA_GRAMMAR([[input.y]],
369 %define api.namespace "]$1["
371 %define global_tokens_and_yystype
375 // YYSTYPE contains a namespace reference.
376 int yylex (YYSTYPE *lval, const ]$1[::parser::location_type*) {
389 ]$1[::parser::error (const ]$1[::parser::location_type &loc,
390 const std::string &msg)
392 std::cerr << "At " << loc << ": " << msg << std::endl;
399 AT_BISON_CHECK([[-o input.cc input.y]])
402 [AT_COMPILE_CXX([[input]], [[input.cc]])
403 AT_PARSER_CHECK([[./input]])])
404 AT_BISON_OPTION_POPDEFS
407 AT_SETUP([[Relative namespace references]])
409 AT_TEST([[foo::bar]])
410 AT_TEST([[foo::bar::baz]])
413 AT_SETUP([[Absolute namespace references]])
415 AT_TEST([[::foo::bar]])
416 AT_TEST([[::foo::bar::baz]])
417 AT_TEST([[@tb@::foo]])
418 AT_TEST([[ @tb@ ::foo::bar]])
419 AT_TEST([[ ::foo::bar::baz]])
422 AT_SETUP([[Syntactically invalid namespace references]])
423 AT_TEST([[:foo:bar]], [[-]])
424 AT_TEST([[foo: :bar]], [[-]])
425 # This one is interesting because `[3]' is encoded as `@<:@3@:>@', which
426 # contains single occurrences of `:'.
427 AT_TEST([[foo[3]::bar::baz]], [[-]])
428 AT_TEST([[foo::bar,baz]], [[-]])
429 AT_TEST([[foo::bar::(baz /* Pacify Emacs ) */]], [[-]])
434 ## -------------------------------------- ##
435 ## Syntax error discarding no lookahead. ##
436 ## -------------------------------------- ##
438 # After a syntax error, lalr1.cc used to not check whether there
439 # actually is a lookahead before discarding the lookahead. As a result,
440 # it mistakenly invoked the destructor for the previous lookahead.
442 AT_SETUP([[Syntax error discarding no lookahead]])
444 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
446 AT_DATA_GRAMMAR([[input.yy]],
447 [[%skeleton "lalr1.cc"
451 int yylex (yy::parser::semantic_type *);
455 %define parse.error verbose
460 std::cerr << "Discarding 'a'." << std::endl;
465 start: error-reduce consistent-error 'a' { USE ($3); };
468 'a' 'a' consistent-error 'a' { USE (($1, $2, $4)); }
469 | 'a' error { std::cerr << "Reducing 'a'." << std::endl; USE ($1); }
474 | /*empty*/ %prec 'a'
477 // Provide another context in which all rules are useful so that this
478 // test case looks a little more realistic.
479 start: 'b' consistent-error ;
484 yylex (yy::parser::semantic_type *)
486 static char const *input = "aa";
491 yy::parser::error (const std::string &m)
493 std::cerr << m << std::endl;
499 AT_BISON_CHECK([[-o input.cc input.yy]])
500 AT_COMPILE_CXX([[input]])
501 # This used to print "Discarding 'a'." again at the end.
502 AT_PARSER_CHECK([[./input]], [[1]], [[]],
508 AT_BISON_OPTION_POPDEFS
512 ## --------------------------- ##
513 ## Syntax error as exception. ##
514 ## --------------------------- ##
516 AT_SETUP([[Syntax error as exception]])
518 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
520 AT_DATA_GRAMMAR([[input.yy]],
521 [[%skeleton "lalr1.cc"
526 int yylex (yy::parser::semantic_type *);
529 %define api.value.type variant
530 %define parse.error verbose
540 error { std::cerr << "caught error" << std::endl; }
548 throw yy::parser::syntax_error ("invalid expression");
554 yylex (yy::parser::semantic_type *)
556 // 's': syntax error, 'l': lexical error.
557 static char const *input = "asal";
558 switch (int res = *input++)
561 throw yy::parser::syntax_error ("invalid character");
568 yy::parser::error (const std::string &m)
570 std::cerr << "error: " << m << std::endl;
575 AT_BISON_CHECK([[-o input.cc input.yy]])
576 AT_COMPILE_CXX([[input]])
578 AT_PARSER_CHECK([[./input]], [[0]], [[]],
579 [[error: invalid expression
581 error: invalid character
585 AT_BISON_OPTION_POPDEFS
589 ## ------------------ ##
590 ## Exception safety. ##
591 ## ------------------ ##
593 # AT_TEST([BISON-DIRECTIVES])
594 # ---------------------------
595 # Check that no object is leaked when exceptions are thrown.
596 m4_pushdef([AT_TEST],
597 [AT_SETUP([[Exception safety $1]])
599 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" $1])
601 AT_DATA_GRAMMAR([[input.yy]],
602 [[%skeleton "lalr1.cc"
609 #include <cstdlib> // size_t and getenv.
615 /// A class that counts its number of instances.
618 typedef std::list<const Object*> objects;
619 static objects instances;
625 return instances.empty();
629 log (Object const *o, const std::string& msg)
634 std::cerr << o << "->";
635 std::cerr << msg << " {";
636 const char* sep = " ";
637 for (objects::const_iterator i = instances.begin(),
638 i_end = instances.end();
642 std::cerr << sep << *i;
645 std::cerr << " }" << std::endl;
652 instances.push_back(this);
653 log (this, "Object::Object");
659 instances.push_back(this);
660 log (this, "Object::Object");
665 instances.remove(this);
666 log (this, "Object::~Object");
674 #include <cstring> // strchr
676 int yylex (yy::parser::semantic_type *);
677 Object::objects Object::instances;
678 static char const *input;
684 yyo << &$$ << " '" << $$.val << '\'';
686 throw std::runtime_error ("printer");
689 %token <Object> 'a' 'E' 'e' 'p' 'R' 's' 'T'
690 %type <Object> list item
696 %destructor { delete $$; } <obj>;
699 yyo << $$ << " '" << $$->val << '\'';
701 throw std::runtime_error ("printer");
704 %token <obj> 'a' 'E' 'e' 'p' 'R' 's' 'T'
705 %type <obj> list item
710 if (strchr (input, 'i'))
711 throw std::runtime_error ("initial-action");
716 start: list {]AT_VARIANT_IF([], [ delete $][1]; )[};
719 item { $][$ = $][1; }
720 // Right recursion to load the stack.
721 | item list { $][$ = $][1; ]AT_VARIANT_IF([], [delete $][2]; )[}
726 | 'e' { YYUSE ($][$); YYUSE($][1); error ("syntax error"); }
727 // Not just 'E', otherwise we reduce when 'E' is the lookahead, and
728 // then the stack is emptied, defeating the point of the test.
729 | 'E' 'a' { YYUSE($][1); $][$ = $][2; }
730 | 'R' { $][$ = YY_NULL; ]AT_VARIANT_IF([], [delete $][1]; )[YYERROR; }
731 | 'p' { $][$ = $][1; }
732 | 's' { $][$ = $][1; throw std::runtime_error ("reduction"); }
733 | 'T' { $][$ = YY_NULL; ]AT_VARIANT_IF([], [delete $][1]; )[YYABORT; }
734 | error { $][$ = YY_NULL; yyerrok; }
739 yylex (yy::parser::semantic_type *lvalp)
742 // 'e': user action calls error.
743 // 'E': syntax error, with yyerror that throws.
744 // 'i': initial action throws.
745 // 'l': yylex throws.
746 // 'R': call YYERROR in the action
747 // 's': reduction throws.
748 // 'T': call YYABORT in the action
749 switch (int res = *input++)
752 throw std::runtime_error ("yylex");
754 lvalp]AT_VARIANT_IF([->build (res)], [->obj = new Object (res)])[;
761 /* A C++ error reporting function. */
763 yy::parser::error (const std::string& m)
765 throw std::runtime_error (m);
769 main (int argc, const char *argv[])
777 assert (std::string(argv[1]) == "--debug");
786 debug |= !!getenv ("YYDEBUG");
787 parser.set_debug_level (debug);
791 res = parser.parse ();
793 catch (const std::exception& e)
795 std::cerr << "exception caught: " << e.what () << std::endl;
799 std::cerr << "unknown exception caught" << std::endl;
801 Object::log (YY_NULL, "end");
802 assert (Object::empty());
806 AT_BISON_CHECK([[-o input.cc --report=all input.yy]])
807 AT_COMPILE_CXX([[input]])
809 AT_PARSER_CHECK([[./input aaaas]], [[2]], [[]],
810 [[exception caught: reduction
813 AT_PARSER_CHECK([[./input aaaal]], [[2]], [[]],
814 [[exception caught: yylex
817 AT_PARSER_CHECK([[./input i]], [[2]], [[]],
818 [[exception caught: initial-action
821 AT_PARSER_CHECK([[./input aaaap]])
823 AT_PARSER_CHECK([[./input --debug aaaap]], [[2]], [[]], [[stderr]])
824 AT_CHECK([[grep '^exception caught: printer$' stderr]], [], [ignore])
826 AT_PARSER_CHECK([[./input aaaae]], [[2]], [[]],
827 [[exception caught: syntax error
830 AT_PARSER_CHECK([[./input aaaaE]], [[2]], [[]],
831 [[exception caught: syntax error, unexpected $end, expecting 'a'
834 AT_PARSER_CHECK([[./input aaaaT]], [[1]])
836 # There is error-recovery, so exit success.
837 AT_PARSER_CHECK([[./input aaaaR]], [[0]])
839 AT_BISON_OPTION_POPDEFS
845 AT_TEST([%define api.value.type variant])
849 ## ------------------------------------ ##
850 ## C++ GLR parser identifier shadowing ##
851 ## ------------------------------------ ##
853 AT_SETUP([[C++ GLR parser identifier shadowing]])
855 AT_DATA_GRAMMAR([input.yy], [
867 int yylex (yy::parser::semantic_type *yylval);
875 int yylex (yy::parser::semantic_type *yylval)
877 // Note: this argument is unused, but named on purpose. There used to be a
878 // bug with a macro that erroneously expanded this identifier to
881 return yy::parser::token::ZERO;
884 void yy::parser::error (std::string const&)
891 AT_BISON_CHECK([[-o input.cc input.yy]])
892 AT_COMPILE_CXX([[input]])