1 # Checking the C++ Features. -*- Autotest -*-
3 # Copyright (C) 2004-2005, 2007-2015 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 fail += check (loc + 10, "1.1-10");
64 loc += 10; fail += check (loc, "1.1-10");
65 loc += -5; fail += check (loc, "1.1-5");
66 fail += check (loc - 5, "1.1");
67 loc -= 5; fail += check (loc, "1.1");
68 // Check that we don't go below.
69 // http://lists.gnu.org/archive/html/bug-bison/2013-02/msg00000.html
70 loc -= 10; fail += check (loc, "1.1");
72 loc.columns (10); loc.lines (10); fail += check (loc, "1.1-11.0");
73 loc.lines (-2); fail += check (loc, "1.1-9.0");
74 loc.lines (-10); fail += check (loc, "1.1");
76 ]AT_YYLTYPE[ loc2 (YY_NULLPTR, 5, 10);
77 fail += check (loc2, "5.10");
78 fail += check (loc + loc2, "1.1-5.9");
79 loc += loc2; fail += check (loc, "1.1-5.9");
84 AT_FULL_COMPILE([input])
85 AT_PARSER_CHECK([./input], 0)
86 AT_BISON_OPTION_POPDEFS
90 ## --------------------------- ##
91 ## C++ Variant-based Symbols. ##
92 ## --------------------------- ##
94 AT_SETUP([C++ Variant-based Symbols])
96 AT_KEYWORDS([variant])
98 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" %debug $1])
99 # Store strings and integers in a list of strings.
100 AT_DATA_GRAMMAR([list.y],
101 [[%skeleton "lalr1.cc"
102 %define api.value.type variant
108 // Get access to stack_symbol_type for the tests.
109 # define private public
116 %token <int> INT "int"
117 %type < std::list<int> > exp
119 %printer { yyo << $$; } <int>
122 for (std::list<int>::const_iterator i = $$.begin (); i != $$.end (); ++i)
124 if (i != $$.begin ())
130 %code requires { #include <list> }
131 %code { int yylex (yy::parser::semantic_type* yylval); }
134 exp: "int" { $$.push_back ($1); }
142 yy::parser::symbol_type s = yy::parser::make_INT(12);
143 std::cerr << s.value.as<int>() << std::endl;
147 yy::parser::symbol_type s = yy::parser::make_INT(123);
148 yy::parser::stack_symbol_type ss(1, s);
149 std::cerr << ss.value.as<int>() << std::endl;
153 yy::parser::stack_type st;
154 for (int i = 0; i < 100; ++i)
156 yy::parser::symbol_type s(yy::parser::make_INT(i));
157 yy::parser::stack_symbol_type ss(1, s);
164 AT_FULL_COMPILE([list])
165 AT_PARSER_CHECK([./list], 0, [],
170 AT_BISON_OPTION_POPDEFS
178 # Check that the variants are properly supported, including in error
181 # AT_TEST([DIRECTIVES])
182 # ---------------------
183 # Check the support of variants in C++, with the additional DIRECTIVES.
184 m4_pushdef([AT_TEST],
185 [AT_SETUP([Variants $1])
187 AT_BISON_OPTION_PUSHDEFS([%debug $1])
188 # Store strings and integers in a list of strings.
189 AT_DATA_GRAMMAR([list.y],
191 %define api.value.type variant
192 ]m4_bpatsubst([$1], [\\n], [
195 %code requires // code for the .hh file
199 typedef std::list<std::string> strings_type;
202 %code // code for the .cc file
204 #include <cstdlib> // abort, getenv
210 static]AT_TOKEN_CTOR_IF([[
211 parser::symbol_type yylex ()]], [[
212 parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([,
213 parser::location_type* yylloc])[)]])[;
216 // Printing a list of strings (for %printer).
217 // Koening look up will look into std, since that's an std::list.
221 operator<<(std::ostream& o, const strings_type& s)
224 for (strings_type::const_iterator i = s.begin (); i != s.end (); ++i)
234 // Conversion to string.
235 template <typename T>
238 to_string (const T& t)
240 std::ostringstream o;
246 %token <::std::string> TEXT;
248 %token END_OF_FILE 0;
251 %type <::std::string> item;
252 // Using the template type to exercize its parsing.
253 // Starting with :: to ensure we don't output "<::" which starts by the
254 // digraph for the left square bracket.
255 %type <::std::list<std::string>> list result;
257 %printer { yyo << $$; }
258 <int> <::std::string> <::std::list<std::string>>;
262 list { std::cout << $][1 << std::endl; }
266 item { $$.push_back ($][1); }
267 | list "," item { std::swap ($$, $][1); $$.push_back ($][3); }
268 | list error { std::swap ($$, $][1); }
272 TEXT { std::swap ($$, $][1); }
273 | NUMBER { if ($][1 == 3) YYERROR; else $$ = to_string ($][1); }
276 ]AT_TOKEN_CTOR_IF([],
277 [[#ifdef TWO_STAGE_BUILD
278 # define BUILD(Type, Value) build<Type> () = Value
280 # define BUILD(Type, Value) build (Value)
286 static]AT_TOKEN_CTOR_IF([[
287 parser::symbol_type yylex ()]], [[
288 parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([,
289 parser::location_type* yylloc])[)]])[
291 typedef parser::location_type location;])[
292 // The 5 is a syntax error whose recovery requires that we discard
293 // the lookahead. This tests a regression, see
294 // <http://savannah.gnu.org/support/?108481>.
295 static char const *input = "0,1,2,3,45,6";
296 switch (int stage = *input++)
298 case 0:]AT_TOKEN_CTOR_IF([[
299 return parser::make_END_OF_FILE (]AT_LOCATION_IF([location ()])[);]],
301 *yylloc = location ();])[
302 return parser::token::END_OF_FILE;]])[
306 return parser::make_COMMA (]AT_LOCATION_IF([location ()])[);]], [[
308 *yylloc = location ();])[
309 return parser::token::COMMA;]])[
314 {]AT_TOKEN_CTOR_IF([[
315 return parser::make_NUMBER (stage]AT_LOCATION_IF([, location ()])[);]], [[
316 yylval->BUILD (int, stage);]AT_LOCATION_IF([
317 *yylloc = location ();])[
318 return parser::token::NUMBER;]])[
321 {]AT_TOKEN_CTOR_IF([[
322 return parser::make_TEXT (to_string (stage)]AT_LOCATION_IF([, location ()])[);]], [[
323 yylval->BUILD (std::string, to_string (stage));]AT_LOCATION_IF([
324 *yylloc = location ();])[
325 return parser::token::TEXT;]])[
337 AT_FULL_COMPILE([list])
338 AT_PARSER_CHECK([./list], 0,
342 AT_BISON_OPTION_POPDEFS
346 AT_TEST([[%skeleton "lalr1.cc" ]])
347 AT_TEST([[%skeleton "lalr1.cc" %define parse.assert]])
348 AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %locations]])
349 AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %code {\n#define TWO_STAGE_BUILD\n}]])
350 AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %define api.token.constructor]])
351 AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %define api.token.constructor %define api.token.prefix {TOK_}]])
352 AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %define api.token.constructor %define api.token.prefix {TOK_} %locations]])
357 ## ----------------------- ##
358 ## Doxygen Documentation. ##
359 ## ----------------------- ##
361 m4_define([AT_CHECK_DOXYGEN],
363 [Public], [m4_pushdef([AT_DOXYGEN_PRIVATE], [NO])],
364 [Private], [m4_pushdef([AT_DOXYGEN_PRIVATE], [YES])],
365 [m4_fatal([invalid argument: $1])])
366 AT_SETUP([Doxygen $1 Documentation])
368 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
370 [[%skeleton "lalr1.cc"
379 AT_BISON_CHECK([-o input.cc input.yy], 0)
382 [# The PROJECT_NAME tag is a single word (or a sequence of words
383 # surrounded by quotes) that should identify the project.
384 PROJECT_NAME = "Bison C++ Parser"
386 # The QUIET tag can be used to turn on/off the messages that are
387 # generated by doxygen. Possible values are YES and NO. If left blank
391 # The WARNINGS tag can be used to turn on/off the warning messages
392 # that are generated by doxygen. Possible values are YES and NO. If
393 # left blank NO is used.
395 # If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate
396 # warnings for undocumented members. If EXTRACT_ALL is set to YES then
397 # this flag will automatically be disabled.
398 WARN_IF_UNDOCUMENTED = YES
399 # If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings
400 # for potential errors in the documentation, such as not documenting
401 # some parameters in a documented function, or documenting parameters
402 # that don't exist or using markup commands wrongly.
403 WARN_IF_DOC_ERROR = YES
404 # The WARN_FORMAT tag determines the format of the warning messages
405 # that doxygen can produce. The string should contain the $file,
406 # $line, and $text tags, which will be replaced by the file and line
407 # number from which the warning originated and the warning text.
408 WARN_FORMAT = "$file:$line: $text"
410 # If the EXTRACT_ALL tag is set to YES doxygen will assume all
411 # entities in documentation are documented, even if no documentation
412 # was available. Private class members and static file members will
413 # be hidden unless the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set
417 # If the EXTRACT_PRIVATE tag is set to YES all private members of a
418 # class will be included in the documentation.
419 EXTRACT_PRIVATE = AT_DOXYGEN_PRIVATE
421 # If the EXTRACT_STATIC tag is set to YES all static members of a file
422 # will be included in the documentation.
423 EXTRACT_STATIC = AT_DOXYGEN_PRIVATE
426 AT_CHECK([doxygen --version || exit 77], 0, ignore)
427 AT_CHECK([doxygen], 0, [], [ignore])
429 AT_BISON_OPTION_POPDEFS
432 m4_popdef([AT_DOXYGEN_PRIVATE])
435 AT_CHECK_DOXYGEN([Public])
436 AT_CHECK_DOXYGEN([Private])
443 # AT_TEST(NAMESPACE-DECL, [COMPILE-ERROR])
444 # ----------------------------------------
445 # See if Bison can handle %define namespace "NAMESPACE-DECL". If COMPILE-ERROR
446 # is specified, then Bison should accept the input, but compilation will fail,
447 # so don't check compilation.
448 m4_pushdef([AT_TEST],
449 [AT_BISON_OPTION_PUSHDEFS([%language "C++" %define api.namespace {$1}])
450 AT_DATA_GRAMMAR([[input.y]],
452 %define api.namespace {]$1[}
454 %define global_tokens_and_yystype
458 // YYSTYPE contains a namespace reference.
459 int yylex (YYSTYPE *lval, const ]$1[::parser::location_type*) {
472 ]$1[::parser::error (const ]$1[::parser::location_type &loc,
473 const std::string &msg)
475 std::cerr << "At " << loc << ": " << msg << std::endl;
482 AT_BISON_CHECK([[-o input.cc input.y]])
485 [AT_COMPILE_CXX([[input]], [[input.cc]])
486 AT_PARSER_CHECK([[./input]])])
487 AT_BISON_OPTION_POPDEFS
490 AT_SETUP([[Relative namespace references]])
492 AT_TEST([[foo::bar]])
493 AT_TEST([[foo::bar::baz]])
496 AT_SETUP([[Absolute namespace references]])
498 AT_TEST([[::foo::bar]])
499 AT_TEST([[::foo::bar::baz]])
500 AT_TEST([[@tb@::foo]])
501 AT_TEST([[ @tb@ ::foo::bar]])
502 AT_TEST([[ ::foo::bar::baz]])
505 AT_SETUP([[Syntactically invalid namespace references]])
506 AT_TEST([[:foo:bar]], [[-]])
507 AT_TEST([[foo: :bar]], [[-]])
508 # This one is interesting because '[3]' is encoded as '@<:@3@:>@', which
509 # contains single occurrences of ':'.
510 AT_TEST([[foo[3]::bar::baz]], [[-]])
511 AT_TEST([[foo::bar,baz]], [[-]])
512 AT_TEST([[foo::bar::(baz /* Pacify Emacs ) */]], [[-]])
517 ## -------------------------------------- ##
518 ## Syntax error discarding no lookahead. ##
519 ## -------------------------------------- ##
521 # After a syntax error, lalr1.cc used to not check whether there
522 # actually is a lookahead before discarding the lookahead. As a result,
523 # it mistakenly invoked the destructor for the previous lookahead.
525 AT_SETUP([[Syntax error discarding no lookahead]])
527 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
529 AT_DATA_GRAMMAR([[input.yy]],
530 [[%skeleton "lalr1.cc"
534 int yylex (yy::parser::semantic_type *);
538 %define parse.error verbose
543 std::cerr << "Discarding 'a'." << std::endl;
548 start: error-reduce consistent-error 'a' { USE ($3); };
551 'a' 'a' consistent-error 'a' { USE (($1, $2, $4)); }
552 | 'a' error { std::cerr << "Reducing 'a'." << std::endl; USE ($1); }
557 | /*empty*/ %prec 'a'
560 // Provide another context in which all rules are useful so that this
561 // test case looks a little more realistic.
562 start: 'b' consistent-error ;
567 yylex (yy::parser::semantic_type *)
569 static char const *input = "aa";
574 yy::parser::error (const std::string &m)
576 std::cerr << m << std::endl;
582 AT_BISON_CHECK([[-o input.cc input.yy]])
583 AT_COMPILE_CXX([[input]])
584 # This used to print "Discarding 'a'." again at the end.
585 AT_PARSER_CHECK([[./input]], [[1]], [[]],
591 AT_BISON_OPTION_POPDEFS
595 ## --------------------------- ##
596 ## Syntax error as exception. ##
597 ## --------------------------- ##
599 AT_SETUP([[Syntax error as exception]])
601 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
603 AT_DATA_GRAMMAR([[input.yy]],
604 [[%skeleton "lalr1.cc"
609 int yylex (yy::parser::semantic_type *);
612 %define api.value.type variant
613 %define parse.error verbose
623 error { std::cerr << "caught error" << std::endl; }
631 throw yy::parser::syntax_error ("invalid expression");
637 yylex (yy::parser::semantic_type *)
639 // 's': syntax error, 'l': lexical error.
640 static char const *input = "asal";
641 switch (int res = *input++)
644 throw yy::parser::syntax_error ("invalid character");
651 yy::parser::error (const std::string &m)
653 std::cerr << "error: " << m << std::endl;
658 AT_BISON_CHECK([[-o input.cc input.yy]])
659 AT_COMPILE_CXX([[input]])
661 AT_PARSER_CHECK([[./input]], [[0]], [[]],
662 [[error: invalid expression
664 error: invalid character
668 AT_BISON_OPTION_POPDEFS
672 ## ------------------ ##
673 ## Exception safety. ##
674 ## ------------------ ##
676 # AT_TEST([BISON-DIRECTIVES = ''], [WITH-RECOVERY = "with"])
677 # ----------------------------------------------------------
678 # Check that no object is leaked when exceptions are thrown.
679 # WITH-RECOVERY = "with" or "without".
680 m4_pushdef([AT_TEST],
681 [AT_SETUP([[Exception safety $2 error recovery $1]])
683 AT_SKIP_IF_EXCEPTION_SUPPORT_IS_POOR
685 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" $1])
687 AT_DATA_GRAMMAR([[input.yy]],
688 [[%skeleton "lalr1.cc"
695 #include <cstdlib> // size_t and getenv.
701 /// A class that tracks its instances.
709 log (this, "Object::Object");
710 Object::instances.insert (this);
713 Object (const Object& that)
716 log (this, "Object::Object");
717 Object::instances.insert (this);
723 log (this, "Object::Object");
724 Object::instances.insert (this);
729 log (this, "Object::~Object");
730 objects::const_iterator i = instances.find (this);
731 // Make sure this object is alive.
732 assert (i != instances.end ());
733 Object::instances.erase (i);
736 Object& operator= (char v)
743 typedef std::set<const Object*> objects;
744 static objects instances;
749 return instances.empty ();
753 log (Object const *o, const std::string& msg)
758 std::cerr << o << "->";
759 std::cerr << msg << " {";
760 const char* sep = " ";
761 for (objects::const_iterator i = instances.begin(),
762 i_end = instances.end();
766 std::cerr << sep << *i;
769 std::cerr << " }" << std::endl;
778 #include <cstring> // strchr
780 int yylex (yy::parser::semantic_type *);
781 Object::objects Object::instances;
782 static char const *input;
788 yyo << &$$ << " '" << $$.val << '\'';
790 throw std::runtime_error ("printer");
793 %token <Object> 'a' 'E' 'e' 'p' 'R' 's' 'T'
794 %type <Object> list item
800 %destructor { delete $$; } <obj>;
803 yyo << $$ << " '" << $$->val << '\'';
805 throw std::runtime_error ("printer");
808 %token <obj> 'a' 'E' 'e' 'p' 'R' 's' 'T'
809 %type <obj> list item
814 if (strchr (input, 'i'))
815 throw std::runtime_error ("initial-action");
820 start: list {]AT_VARIANT_IF([], [ delete $][1]; )[};
824 // Right recursion to load the stack.
825 | item list { $$ = $][1; ]AT_VARIANT_IF([], [delete $][2]; )[}
830 | 'e' { YYUSE ($$); YYUSE($][1); error ("syntax error"); }
831 // Not just 'E', otherwise we reduce when 'E' is the lookahead, and
832 // then the stack is emptied, defeating the point of the test.
833 | 'E' 'a' { YYUSE($][1); $$ = $][2; }
834 | 'R' { ]AT_VARIANT_IF([], [$$ = YY_NULLPTR; delete $][1]; )[YYERROR; }
836 | 's' { $$ = $][1; throw std::runtime_error ("reduction"); }
837 | 'T' { ]AT_VARIANT_IF([], [$$ = YY_NULLPTR; delete $][1]; )[YYABORT; }
839 [[| error { $$ = ]AT_VARIANT_IF([], [new ])[Object ('R'); yyerrok; }]])[
844 yylex (yy::parser::semantic_type *lvalp)
847 // 'e': user action calls error.
848 // 'E': syntax error, with yyerror that throws.
849 // 'i': initial action throws.
850 // 'l': yylex throws.
851 // 'R': call YYERROR in the action
852 // 's': reduction throws.
853 // 'T': call YYABORT in the action
854 switch (int res = *input++)
857 throw std::runtime_error ("yylex");
859 lvalp->]AT_VARIANT_IF([build (Object (res))],
860 [obj = new Object (res)])[;
867 /* A C++ error reporting function. */
869 yy::parser::error (const std::string& m)
871 throw std::runtime_error (m);
875 main (int argc, const char *argv[])
883 assert (std::string(argv[1]) == "--debug");
892 debug |= !!getenv ("YYDEBUG");
893 parser.set_debug_level (debug);
897 res = parser.parse ();
899 catch (const std::exception& e)
901 std::cerr << "exception caught: " << e.what () << std::endl;
905 std::cerr << "unknown exception caught" << std::endl;
907 Object::log (YY_NULLPTR, "end");
908 assert (Object::empty());
912 AT_BISON_CHECK([[-o input.cc --report=all input.yy]])
913 AT_COMPILE_CXX([[input]])
915 AT_PARSER_CHECK([[./input aaaas]], [[2]], [[]],
916 [[exception caught: reduction
919 AT_PARSER_CHECK([[./input aaaal]], [[2]], [[]],
920 [[exception caught: yylex
923 AT_PARSER_CHECK([[./input i]], [[2]], [[]],
924 [[exception caught: initial-action
927 AT_PARSER_CHECK([[./input aaaap]])
929 AT_PARSER_CHECK([[./input --debug aaaap]], [[2]], [[]], [[stderr]])
930 AT_CHECK([[grep '^exception caught: printer$' stderr]], [], [ignore])
932 AT_PARSER_CHECK([[./input aaaae]], [[2]], [[]],
933 [[exception caught: syntax error
936 AT_PARSER_CHECK([[./input aaaaE]], [[2]], [[]],
937 [[exception caught: syntax error, unexpected $end, expecting 'a'
940 AT_PARSER_CHECK([[./input aaaaT]], [[1]])
942 AT_PARSER_CHECK([[./input aaaaR]], [m4_if([$2], [with], [0], [1])])
944 AT_BISON_OPTION_POPDEFS
950 AT_TEST([], [without])
951 AT_TEST([%define api.value.type variant], [with])
952 AT_TEST([%define api.value.type variant], [without])
956 ## ------------------------------------ ##
957 ## C++ GLR parser identifier shadowing ##
958 ## ------------------------------------ ##
960 AT_SETUP([[C++ GLR parser identifier shadowing]])
962 AT_DATA_GRAMMAR([input.yy], [
974 int yylex (yy::parser::semantic_type *yylval);
982 int yylex (yy::parser::semantic_type *yylval)
984 // Note: this argument is unused, but named on purpose. There used to be a
985 // bug with a macro that erroneously expanded this identifier to
988 return yy::parser::token::ZERO;
991 void yy::parser::error (std::string const&)
998 AT_BISON_CHECK([[-o input.cc input.yy]])
999 AT_COMPILE_CXX([[input]])