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.]])
25 AT_SETUP([C++ Locations])
27 AT_BISON_OPTION_PUSHDEFS([%locations %skeleton "lalr1.cc"])
28 AT_DATA_GRAMMAR([[input.y]],
29 [[%code {#include <sstream>}
46 check (const T& in, const std::string& s)
52 std::cerr << "fail: " << os.str () << ", expected: " << s << std::endl;
62 ]AT_YYLTYPE[ loc; fail += check (loc, "1.1");
63 loc += 10; fail += check (loc, "1.1-10");
64 loc += -5; fail += check (loc, "1.1-5");
65 loc -= 5; fail += check (loc, "1.1");
66 // Check that we don't go below.
67 // http://lists.gnu.org/archive/html/bug-bison/2013-02/msg00000.html
68 loc -= 10; fail += check (loc, "1.1");
70 loc.columns (10); loc.lines (10); fail += check (loc, "1.1-11.0");
71 loc.lines (-2); fail += check (loc, "1.1-9.0");
72 loc.lines (-10); fail += check (loc, "1.1");
77 AT_FULL_COMPILE([input])
78 AT_PARSER_CHECK([./input], 0)
79 AT_BISON_OPTION_POPDEFS
83 ## --------------------------- ##
84 ## C++ Variant-based Symbols. ##
85 ## --------------------------- ##
87 AT_SETUP([C++ Variant-based Symbols])
89 AT_KEYWORDS([variant])
91 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" %debug $1])
92 # Store strings and integers in a list of strings.
93 AT_DATA_GRAMMAR([list.y],
94 [[%skeleton "lalr1.cc"
95 %define api.value.type variant
101 // Get access to stack_symbol_type for the tests.
102 # define private public
109 %token <int> INT "int"
110 %type < std::list<int> > exp
112 %printer { yyo << $$; } <int>
115 for (std::list<int>::const_iterator i = $$.begin (); i != $$.end (); ++i)
117 if (i != $$.begin ())
123 %code requires { #include <list> }
124 %code { int yylex (yy::parser::semantic_type* yylval); }
127 exp: "int" { $$.push_back ($1); }
135 yy::parser::symbol_type s = yy::parser::make_INT(12);
136 std::cerr << s.value.as<int>() << std::endl;
140 yy::parser::symbol_type s = yy::parser::make_INT(123);
141 yy::parser::stack_symbol_type ss(1, s);
142 std::cerr << ss.value.as<int>() << std::endl;
146 yy::parser::stack_type st;
147 for (int i = 0; i < 100; ++i)
149 yy::parser::symbol_type s(yy::parser::make_INT(i));
150 yy::parser::stack_symbol_type ss(1, s);
157 AT_FULL_COMPILE([list])
158 AT_PARSER_CHECK([./list], 0, [],
163 AT_BISON_OPTION_POPDEFS
171 # AT_TEST([DIRECTIVES])
172 # ---------------------
173 # Check the support of variants in C++, with the additional DIRECTIVES.
174 m4_pushdef([AT_TEST],
175 [AT_SETUP([Variants $1])
177 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" %debug $1])
178 # Store strings and integers in a list of strings.
179 AT_DATA_GRAMMAR([list.y],
182 %define api.value.type variant
183 ]m4_bpatsubst([$1], [\\n], [
186 %code requires // code for the .hh file
190 typedef std::list<std::string> strings_type;
193 %code // code for the .cc file
195 #include <cstdlib> // abort, getenv
201 static]AT_TOKEN_CTOR_IF([[
202 parser::symbol_type yylex ()]], [[
203 parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([,
204 parser::location_type* yylloc])[)]])[;
207 // Printing a list of strings (for %printer).
208 // Koening look up will look into std, since that's an std::list.
212 operator<<(std::ostream& o, const strings_type& s)
215 for (strings_type::const_iterator i = s.begin (); i != s.end (); ++i)
225 // Conversion to string.
226 template <typename T>
229 string_cast (const T& t)
231 std::ostringstream o;
237 %token <::std::string> TEXT;
239 %token END_OF_FILE 0;
241 %type <::std::string> item;
242 // Using the template type to exercize its parsing.
243 // Starting with :: to ensure we don't output "<::" which starts by the
244 // digraph for the left square bracket.
245 %type <::std::list<std::string>> list result;
247 %printer { yyo << $][$; }
248 <int> <::std::string> <::std::list<std::string>>;
252 list { std::cout << $][1 << std::endl; }
256 /* nothing */ { /* Generates an empty string list */ }
257 | list item { std::swap ($][$,$][1); $$.push_back ($][2); }
258 | list error { std::swap ($][$,$][1); }
262 TEXT { std::swap ($][$,$][1); }
263 | NUMBER { if ($][1 == 3) YYERROR; else $][$ = string_cast ($][1); }
266 ]AT_TOKEN_CTOR_IF([],
267 [[#ifdef TWO_STAGE_BUILD
268 # define BUILD(Type, Value) build<Type> () = Value
270 # define BUILD(Type, Value) build (Value)
276 static]AT_TOKEN_CTOR_IF([[
277 parser::symbol_type yylex ()]], [[
278 parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([,
279 parser::location_type* yylloc])[)]])[
281 typedef parser::location_type location;])[
282 static int stage = -1;
284 if (stage == STAGE_MAX)
285 {]AT_TOKEN_CTOR_IF([[
286 return parser::make_END_OF_FILE (]AT_LOCATION_IF([location ()])[);]],
288 *yylloc = location ();])[
289 return parser::token::END_OF_FILE;]])[
292 {]AT_TOKEN_CTOR_IF([[
293 return parser::make_NUMBER (stage]AT_LOCATION_IF([, location ()])[);]],
295 yylval->BUILD (int, stage);]AT_LOCATION_IF([
296 *yylloc = location ();])[
297 return parser::token::NUMBER;]])[
300 {]AT_TOKEN_CTOR_IF([[
301 return parser::make_TEXT (string_cast (stage)]AT_LOCATION_IF([, location ()])[);]], [[
302 yylval->BUILD (std::string, string_cast (stage));]AT_LOCATION_IF([
303 *yylloc = location ();])[
304 return parser::token::TEXT;]])[
314 AT_FULL_COMPILE([list])
315 AT_PARSER_CHECK([./list], 0,
319 AT_BISON_OPTION_POPDEFS
324 AT_TEST([%define parse.assert])
325 AT_TEST([%locations %define parse.assert])
326 AT_TEST([[%define parse.assert %code {\n#define TWO_STAGE_BUILD\n}]])
327 AT_TEST([[%define parse.assert %define api.token.constructor]])
328 AT_TEST([[%define parse.assert %define api.token.constructor %define api.token.prefix {TOK_}]])
329 AT_TEST([[%locations %define parse.assert %define api.token.constructor %define api.token.prefix {TOK_}]])
334 ## ----------------------- ##
335 ## Doxygen Documentation. ##
336 ## ----------------------- ##
338 m4_define([AT_CHECK_DOXYGEN],
340 [Public], [m4_pushdef([AT_DOXYGEN_PRIVATE], [NO])],
341 [Private], [m4_pushdef([AT_DOXYGEN_PRIVATE], [YES])],
342 [m4_fatal([invalid argument: $1])])
343 AT_SETUP([Doxygen $1 Documentation])
345 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
347 [[%skeleton "lalr1.cc"
356 AT_BISON_CHECK([-o input.cc input.yy], 0)
359 [# The PROJECT_NAME tag is a single word (or a sequence of words
360 # surrounded by quotes) that should identify the project.
361 PROJECT_NAME = "Bison C++ Parser"
363 # The QUIET tag can be used to turn on/off the messages that are
364 # generated by doxygen. Possible values are YES and NO. If left blank
368 # The WARNINGS tag can be used to turn on/off the warning messages
369 # that are generated by doxygen. Possible values are YES and NO. If
370 # left blank NO is used.
372 # If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate
373 # warnings for undocumented members. If EXTRACT_ALL is set to YES then
374 # this flag will automatically be disabled.
375 WARN_IF_UNDOCUMENTED = YES
376 # If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings
377 # for potential errors in the documentation, such as not documenting
378 # some parameters in a documented function, or documenting parameters
379 # that don't exist or using markup commands wrongly.
380 WARN_IF_DOC_ERROR = YES
381 # The WARN_FORMAT tag determines the format of the warning messages
382 # that doxygen can produce. The string should contain the $file,
383 # $line, and $text tags, which will be replaced by the file and line
384 # number from which the warning originated and the warning text.
385 WARN_FORMAT = "$file:$line: $text"
387 # If the EXTRACT_ALL tag is set to YES doxygen will assume all
388 # entities in documentation are documented, even if no documentation
389 # was available. Private class members and static file members will
390 # be hidden unless the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set
394 # If the EXTRACT_PRIVATE tag is set to YES all private members of a
395 # class will be included in the documentation.
396 EXTRACT_PRIVATE = AT_DOXYGEN_PRIVATE
398 # If the EXTRACT_STATIC tag is set to YES all static members of a file
399 # will be included in the documentation.
400 EXTRACT_STATIC = AT_DOXYGEN_PRIVATE
403 AT_CHECK([doxygen --version || exit 77], 0, ignore)
404 AT_CHECK([doxygen], 0, [], [ignore])
406 AT_BISON_OPTION_POPDEFS
409 m4_popdef([AT_DOXYGEN_PRIVATE])
412 AT_CHECK_DOXYGEN([Public])
413 AT_CHECK_DOXYGEN([Private])
420 # AT_TEST(NAMESPACE-DECL, [COMPILE-ERROR])
421 # ----------------------------------------
422 # See if Bison can handle %define namespace "NAMESPACE-DECL". If COMPILE-ERROR
423 # is specified, then Bison should accept the input, but compilation will fail,
424 # so don't check compilation.
425 m4_pushdef([AT_TEST],
426 [AT_BISON_OPTION_PUSHDEFS([%language "C++" %define api.namespace {$1}])
427 AT_DATA_GRAMMAR([[input.y]],
429 %define api.namespace {]$1[}
431 %define global_tokens_and_yystype
435 // YYSTYPE contains a namespace reference.
436 int yylex (YYSTYPE *lval, const ]$1[::parser::location_type*) {
449 ]$1[::parser::error (const ]$1[::parser::location_type &loc,
450 const std::string &msg)
452 std::cerr << "At " << loc << ": " << msg << std::endl;
459 AT_BISON_CHECK([[-o input.cc input.y]])
462 [AT_COMPILE_CXX([[input]], [[input.cc]])
463 AT_PARSER_CHECK([[./input]])])
464 AT_BISON_OPTION_POPDEFS
467 AT_SETUP([[Relative namespace references]])
469 AT_TEST([[foo::bar]])
470 AT_TEST([[foo::bar::baz]])
473 AT_SETUP([[Absolute namespace references]])
475 AT_TEST([[::foo::bar]])
476 AT_TEST([[::foo::bar::baz]])
477 AT_TEST([[@tb@::foo]])
478 AT_TEST([[ @tb@ ::foo::bar]])
479 AT_TEST([[ ::foo::bar::baz]])
482 AT_SETUP([[Syntactically invalid namespace references]])
483 AT_TEST([[:foo:bar]], [[-]])
484 AT_TEST([[foo: :bar]], [[-]])
485 # This one is interesting because '[3]' is encoded as '@<:@3@:>@', which
486 # contains single occurrences of ':'.
487 AT_TEST([[foo[3]::bar::baz]], [[-]])
488 AT_TEST([[foo::bar,baz]], [[-]])
489 AT_TEST([[foo::bar::(baz /* Pacify Emacs ) */]], [[-]])
494 ## -------------------------------------- ##
495 ## Syntax error discarding no lookahead. ##
496 ## -------------------------------------- ##
498 # After a syntax error, lalr1.cc used to not check whether there
499 # actually is a lookahead before discarding the lookahead. As a result,
500 # it mistakenly invoked the destructor for the previous lookahead.
502 AT_SETUP([[Syntax error discarding no lookahead]])
504 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
506 AT_DATA_GRAMMAR([[input.yy]],
507 [[%skeleton "lalr1.cc"
511 int yylex (yy::parser::semantic_type *);
515 %define parse.error verbose
520 std::cerr << "Discarding 'a'." << std::endl;
525 start: error-reduce consistent-error 'a' { USE ($3); };
528 'a' 'a' consistent-error 'a' { USE (($1, $2, $4)); }
529 | 'a' error { std::cerr << "Reducing 'a'." << std::endl; USE ($1); }
534 | /*empty*/ %prec 'a'
537 // Provide another context in which all rules are useful so that this
538 // test case looks a little more realistic.
539 start: 'b' consistent-error ;
544 yylex (yy::parser::semantic_type *)
546 static char const *input = "aa";
551 yy::parser::error (const std::string &m)
553 std::cerr << m << std::endl;
559 AT_BISON_CHECK([[-o input.cc input.yy]])
560 AT_COMPILE_CXX([[input]])
561 # This used to print "Discarding 'a'." again at the end.
562 AT_PARSER_CHECK([[./input]], [[1]], [[]],
568 AT_BISON_OPTION_POPDEFS
572 ## --------------------------- ##
573 ## Syntax error as exception. ##
574 ## --------------------------- ##
576 AT_SETUP([[Syntax error as exception]])
578 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
580 AT_DATA_GRAMMAR([[input.yy]],
581 [[%skeleton "lalr1.cc"
586 int yylex (yy::parser::semantic_type *);
589 %define api.value.type variant
590 %define parse.error verbose
600 error { std::cerr << "caught error" << std::endl; }
608 throw yy::parser::syntax_error ("invalid expression");
614 yylex (yy::parser::semantic_type *)
616 // 's': syntax error, 'l': lexical error.
617 static char const *input = "asal";
618 switch (int res = *input++)
621 throw yy::parser::syntax_error ("invalid character");
628 yy::parser::error (const std::string &m)
630 std::cerr << "error: " << m << std::endl;
635 AT_BISON_CHECK([[-o input.cc input.yy]])
636 AT_COMPILE_CXX([[input]])
638 AT_PARSER_CHECK([[./input]], [[0]], [[]],
639 [[error: invalid expression
641 error: invalid character
645 AT_BISON_OPTION_POPDEFS
649 ## ------------------ ##
650 ## Exception safety. ##
651 ## ------------------ ##
653 # AT_TEST([BISON-DIRECTIVES])
654 # ---------------------------
655 # Check that no object is leaked when exceptions are thrown.
656 m4_pushdef([AT_TEST],
657 [AT_SETUP([[Exception safety $1]])
659 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" $1])
661 AT_DATA_GRAMMAR([[input.yy]],
662 [[%skeleton "lalr1.cc"
669 #include <cstdlib> // size_t and getenv.
675 /// A class that counts its number of instances.
683 Object::instances.push_back(this);
684 log (this, "Object::Object");
690 Object::instances.push_back(this);
691 log (this, "Object::Object");
694 Object& operator= (char v)
702 Object::instances.remove (this);
703 log (this, "Object::~Object");
707 typedef std::list<const Object*> objects;
708 static objects instances;
713 return instances.empty ();
717 log (Object const *o, const std::string& msg)
722 std::cerr << o << "->";
723 std::cerr << msg << " {";
724 const char* sep = " ";
725 for (objects::const_iterator i = instances.begin(),
726 i_end = instances.end();
730 std::cerr << sep << *i;
733 std::cerr << " }" << std::endl;
742 #include <cstring> // strchr
744 int yylex (yy::parser::semantic_type *);
745 Object::objects Object::instances;
746 static char const *input;
752 yyo << &$$ << " '" << $$.val << '\'';
754 throw std::runtime_error ("printer");
757 %token <Object> 'a' 'E' 'e' 'p' 'R' 's' 'T'
758 %type <Object> list item
764 %destructor { delete $$; } <obj>;
767 yyo << $$ << " '" << $$->val << '\'';
769 throw std::runtime_error ("printer");
772 %token <obj> 'a' 'E' 'e' 'p' 'R' 's' 'T'
773 %type <obj> list item
778 if (strchr (input, 'i'))
779 throw std::runtime_error ("initial-action");
784 start: list {]AT_VARIANT_IF([], [ delete $][1]; )[};
787 item { $][$ = $][1; }
788 // Right recursion to load the stack.
789 | item list { $][$ = $][1; ]AT_VARIANT_IF([], [delete $][2]; )[}
794 | 'e' { YYUSE ($][$); YYUSE($][1); error ("syntax error"); }
795 // Not just 'E', otherwise we reduce when 'E' is the lookahead, and
796 // then the stack is emptied, defeating the point of the test.
797 | 'E' 'a' { YYUSE($][1); $][$ = $][2; }
798 | 'R' { $][$ = 0; ]AT_VARIANT_IF([], [delete $][1]; )[YYERROR; }
799 | 'p' { $][$ = $][1; }
800 | 's' { $][$ = $][1; throw std::runtime_error ("reduction"); }
801 | 'T' { $][$ = 0; ]AT_VARIANT_IF([], [delete $][1]; )[YYABORT; }
802 | error { $][$ = 0; yyerrok; }
807 yylex (yy::parser::semantic_type *lvalp)
810 // 'e': user action calls error.
811 // 'E': syntax error, with yyerror that throws.
812 // 'i': initial action throws.
813 // 'l': yylex throws.
814 // 'R': call YYERROR in the action
815 // 's': reduction throws.
816 // 'T': call YYABORT in the action
817 switch (int res = *input++)
820 throw std::runtime_error ("yylex");
822 lvalp]AT_VARIANT_IF([->build (res)], [->obj = new Object (res)])[;
829 /* A C++ error reporting function. */
831 yy::parser::error (const std::string& m)
833 throw std::runtime_error (m);
837 main (int argc, const char *argv[])
845 assert (std::string(argv[1]) == "--debug");
854 debug |= !!getenv ("YYDEBUG");
855 parser.set_debug_level (debug);
859 res = parser.parse ();
861 catch (const std::exception& e)
863 std::cerr << "exception caught: " << e.what () << std::endl;
867 std::cerr << "unknown exception caught" << std::endl;
869 Object::log (YY_NULL, "end");
870 assert (Object::empty());
874 AT_BISON_CHECK([[-o input.cc --report=all input.yy]])
875 AT_COMPILE_CXX([[input]])
877 AT_PARSER_CHECK([[./input aaaas]], [[2]], [[]],
878 [[exception caught: reduction
881 AT_PARSER_CHECK([[./input aaaal]], [[2]], [[]],
882 [[exception caught: yylex
885 AT_PARSER_CHECK([[./input i]], [[2]], [[]],
886 [[exception caught: initial-action
889 AT_PARSER_CHECK([[./input aaaap]])
891 AT_PARSER_CHECK([[./input --debug aaaap]], [[2]], [[]], [[stderr]])
892 AT_CHECK([[grep '^exception caught: printer$' stderr]], [], [ignore])
894 AT_PARSER_CHECK([[./input aaaae]], [[2]], [[]],
895 [[exception caught: syntax error
898 AT_PARSER_CHECK([[./input aaaaE]], [[2]], [[]],
899 [[exception caught: syntax error, unexpected $end, expecting 'a'
902 AT_PARSER_CHECK([[./input aaaaT]], [[1]])
904 # There is error-recovery, so exit success.
905 AT_PARSER_CHECK([[./input aaaaR]], [[0]])
907 AT_BISON_OPTION_POPDEFS
913 AT_TEST([%define api.value.type variant])
917 ## ------------------------------------ ##
918 ## C++ GLR parser identifier shadowing ##
919 ## ------------------------------------ ##
921 AT_SETUP([[C++ GLR parser identifier shadowing]])
923 AT_DATA_GRAMMAR([input.yy], [
935 int yylex (yy::parser::semantic_type *yylval);
943 int yylex (yy::parser::semantic_type *yylval)
945 // Note: this argument is unused, but named on purpose. There used to be a
946 // bug with a macro that erroneously expanded this identifier to
949 return yy::parser::token::ZERO;
952 void yy::parser::error (std::string const&)
959 AT_BISON_CHECK([[-o input.cc input.yy]])
960 AT_COMPILE_CXX([[input]])