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 vector 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::vector<int> > exp
119 %printer { yyo << $$; } <int>
122 for (std::vector<int>::const_iterator i = $$.begin (); i != $$.end (); ++i)
124 if (i != $$.begin ())
128 } < std::vector<int> >
130 %code requires { #include <vector> }
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 vector 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::vector<std::string> strings_type;
202 %code top // code for the .cc file.
204 #include <cstdlib> // abort, getenv
211 typedef std::vector<std::string> strings_type;
215 // Must be available early, as is used in %destructor.
217 operator<<(std::ostream& o, const strings_type& s)
220 for (strings_type::const_iterator i = s.begin (); i != s.end (); ++i)
231 %code // code for the .cc file.
235 static]AT_TOKEN_CTOR_IF([[
236 parser::symbol_type yylex ()]], [[
237 parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([,
238 parser::location_type* yylloc])[)]])[;
240 // Conversion to string.
241 template <typename T>
244 to_string (const T& t)
246 std::ostringstream o;
253 %token <::std::string> TEXT;
255 %token END_OF_FILE 0;
258 %type <::std::string> item;
259 // Using the template type to exercize its parsing.
260 // Starting with :: to ensure we don't output "<::" which starts by the
261 // digraph for the left square bracket.
262 %type <::std::vector<std::string>> list;
264 %printer { yyo << $$; }
265 <int> <::std::string> <::std::vector<std::string>>;
266 %destructor { std::cerr << "Destroy: " << $$ << '\n'; } <*>;
267 %destructor { std::cerr << "Destroy: \"" << $$ << "\"\n"; } <::std::string>;
271 list { std::cout << $][1 << std::endl; }
275 item { $$.push_back ($][1); }
276 | list "," item { std::swap ($$, $][1); $$.push_back ($][3); }
277 | list error { std::swap ($$, $][1); }
281 TEXT { std::swap ($$, $][1); }
282 | NUMBER { if ($][1 == 3) YYERROR; else $$ = to_string ($][1); }
285 ]AT_TOKEN_CTOR_IF([],
286 [[#ifdef TWO_STAGE_BUILD
287 # define BUILD(Type, Value) build<Type> () = Value
289 # define BUILD(Type, Value) build (Value)
295 static]AT_TOKEN_CTOR_IF([[
296 parser::symbol_type yylex ()]], [[
297 parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([,
298 parser::location_type* yylloc])[)]])[
300 typedef parser::location_type location;])[
301 // The 5 is a syntax error whose recovery requires that we discard
302 // the lookahead. This tests a regression, see
303 // <http://savannah.gnu.org/support/?108481>.
304 static char const *input = "0,1,2,3,45,6";
305 switch (int stage = *input++)
307 case 0:]AT_TOKEN_CTOR_IF([[
308 return parser::make_END_OF_FILE (]AT_LOCATION_IF([location ()])[);]],
310 *yylloc = location ();])[
311 return parser::token::END_OF_FILE;]])[
315 return parser::make_COMMA (]AT_LOCATION_IF([location ()])[);]], [[
317 *yylloc = location ();])[
318 return parser::token::COMMA;]])[
323 {]AT_TOKEN_CTOR_IF([[
324 return parser::make_NUMBER (stage]AT_LOCATION_IF([, location ()])[);]], [[
325 yylval->BUILD (int, stage);]AT_LOCATION_IF([
326 *yylloc = location ();])[
327 return parser::token::NUMBER;]])[
330 {]AT_TOKEN_CTOR_IF([[
331 return parser::make_TEXT (to_string (stage)]AT_LOCATION_IF([, location ()])[);]], [[
332 yylval->BUILD (std::string, to_string (stage));]AT_LOCATION_IF([
333 *yylloc = location ();])[
334 return parser::token::TEXT;]])[
346 AT_FULL_COMPILE([list])
347 AT_PARSER_CHECK([./list], 0,
370 Destroy: (0, 1, 2, 4, 6)
373 AT_BISON_OPTION_POPDEFS
377 AT_TEST([[%skeleton "lalr1.cc" ]])
378 AT_TEST([[%skeleton "lalr1.cc" %define parse.assert]])
379 AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %locations]])
380 AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %code {\n#define TWO_STAGE_BUILD\n}]])
381 AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %define api.token.constructor]])
382 AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %define api.token.constructor %define api.token.prefix {TOK_}]])
383 AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %define api.token.constructor %define api.token.prefix {TOK_} %locations]])
388 ## ----------------------- ##
389 ## Doxygen Documentation. ##
390 ## ----------------------- ##
392 m4_define([AT_CHECK_DOXYGEN],
394 [Public], [m4_pushdef([AT_DOXYGEN_PRIVATE], [NO])],
395 [Private], [m4_pushdef([AT_DOXYGEN_PRIVATE], [YES])],
396 [m4_fatal([invalid argument: $1])])
397 AT_SETUP([Doxygen $1 Documentation])
399 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
401 [[%skeleton "lalr1.cc"
411 AT_BISON_CHECK([-o input.cc input.yy])
414 [# The PROJECT_NAME tag is a single word (or a sequence of words
415 # surrounded by quotes) that should identify the project.
416 PROJECT_NAME = "Bison C++ Parser"
418 # The QUIET tag can be used to turn on/off the messages that are
419 # generated by doxygen. Possible values are YES and NO. If left blank
423 # The WARNINGS tag can be used to turn on/off the warning messages
424 # that are generated by doxygen. Possible values are YES and NO. If
425 # left blank NO is used.
427 # If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate
428 # warnings for undocumented members. If EXTRACT_ALL is set to YES then
429 # this flag will automatically be disabled.
430 WARN_IF_UNDOCUMENTED = YES
431 # If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings
432 # for potential errors in the documentation, such as not documenting
433 # some parameters in a documented function, or documenting parameters
434 # that don't exist or using markup commands wrongly.
435 WARN_IF_DOC_ERROR = YES
436 # The WARN_FORMAT tag determines the format of the warning messages
437 # that doxygen can produce. The string should contain the $file,
438 # $line, and $text tags, which will be replaced by the file and line
439 # number from which the warning originated and the warning text.
440 WARN_FORMAT = "$file:$line: $text"
442 # If the EXTRACT_ALL tag is set to YES doxygen will assume all
443 # entities in documentation are documented, even if no documentation
444 # was available. Private class members and static file members will
445 # be hidden unless the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set
449 # If the EXTRACT_PRIVATE tag is set to YES all private members of a
450 # class will be included in the documentation.
451 EXTRACT_PRIVATE = AT_DOXYGEN_PRIVATE
453 # If the EXTRACT_STATIC tag is set to YES all static members of a file
454 # will be included in the documentation.
455 EXTRACT_STATIC = AT_DOXYGEN_PRIVATE
458 AT_CHECK([doxygen --version || exit 77], 0, ignore)
459 AT_CHECK([doxygen], 0, [], [ignore])
461 AT_BISON_OPTION_POPDEFS
464 m4_popdef([AT_DOXYGEN_PRIVATE])
467 AT_CHECK_DOXYGEN([Public])
468 AT_CHECK_DOXYGEN([Private])
475 # AT_TEST(NAMESPACE-DECL, [COMPILE-ERROR])
476 # ----------------------------------------
477 # See if Bison can handle %define namespace "NAMESPACE-DECL". If COMPILE-ERROR
478 # is specified, then Bison should accept the input, but compilation will fail,
479 # so don't check compilation.
480 m4_pushdef([AT_TEST],
481 [AT_BISON_OPTION_PUSHDEFS([%language "C++" %define api.namespace {$1}])
482 AT_DATA_GRAMMAR([[input.yy]],
484 %define api.namespace {]$1[}
486 %define global_tokens_and_yystype
490 // YYSTYPE contains a namespace reference.
491 int yylex (YYSTYPE *lval, const ]$1[::parser::location_type*) {
504 ]$1[::parser::error (const ]$1[::parser::location_type &loc,
505 const std::string &msg)
507 std::cerr << "At " << loc << ": " << msg << std::endl;
514 AT_BISON_CHECK([[-o input.cc input.yy]])
517 [AT_COMPILE_CXX([[input]])
518 AT_PARSER_CHECK([[./input]])])
519 AT_BISON_OPTION_POPDEFS
522 AT_SETUP([[Relative namespace references]])
524 AT_TEST([[foo::bar]])
525 AT_TEST([[foo::bar::baz]])
528 AT_SETUP([[Absolute namespace references]])
530 AT_TEST([[::foo::bar]])
531 AT_TEST([[::foo::bar::baz]])
532 AT_TEST([[@tb@::foo]])
533 AT_TEST([[ @tb@ ::foo::bar]])
534 AT_TEST([[ ::foo::bar::baz]])
537 AT_SETUP([[Syntactically invalid namespace references]])
538 AT_TEST([[:foo:bar]], [[-]])
539 AT_TEST([[foo: :bar]], [[-]])
540 # This one is interesting because '[3]' is encoded as '@<:@3@:>@', which
541 # contains single occurrences of ':'.
542 AT_TEST([[foo[3]::bar::baz]], [[-]])
543 AT_TEST([[foo::bar,baz]], [[-]])
544 AT_TEST([[foo::bar::(baz /* Pacify Emacs ) */]], [[-]])
549 ## -------------------------------------- ##
550 ## Syntax error discarding no lookahead. ##
551 ## -------------------------------------- ##
553 # After a syntax error, lalr1.cc used to not check whether there
554 # actually is a lookahead before discarding the lookahead. As a result,
555 # it mistakenly invoked the destructor for the previous lookahead.
557 AT_SETUP([[Syntax error discarding no lookahead]])
559 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
561 AT_DATA_GRAMMAR([[input.y]],
562 [[%skeleton "lalr1.cc"
566 int yylex (yy::parser::semantic_type *);
570 %define parse.error verbose
575 std::cerr << "Discarding 'a'." << std::endl;
580 start: error-reduce consistent-error 'a' { USE ($3); };
583 'a' 'a' consistent-error 'a' { USE (($1, $2, $4)); }
584 | 'a' error { std::cerr << "Reducing 'a'." << std::endl; USE ($1); }
589 | /*empty*/ %prec 'a'
592 // Provide another context in which all rules are useful so that this
593 // test case looks a little more realistic.
594 start: 'b' consistent-error ;
599 yylex (yy::parser::semantic_type *)
601 static char const *input = "aa";
606 yy::parser::error (const std::string &m)
608 std::cerr << m << std::endl;
614 AT_FULL_COMPILE([[input]])
615 # This used to print "Discarding 'a'." again at the end.
616 AT_PARSER_CHECK([[./input]], [[1]], [[]],
622 AT_BISON_OPTION_POPDEFS
626 ## --------------------------- ##
627 ## Syntax error as exception. ##
628 ## --------------------------- ##
630 AT_SETUP([[Syntax error as exception]])
632 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
634 AT_DATA_GRAMMAR([[input.y]],
635 [[%skeleton "lalr1.cc"
640 int yylex (yy::parser::semantic_type *);
643 %define api.value.type variant
644 %define parse.error verbose
654 error { std::cerr << "caught error" << std::endl; }
662 throw yy::parser::syntax_error ("invalid expression");
668 yylex (yy::parser::semantic_type *)
670 // 's': syntax error, 'l': lexical error.
671 static char const *input = "asal";
672 switch (int res = *input++)
675 throw yy::parser::syntax_error ("invalid character");
682 yy::parser::error (const std::string &m)
684 std::cerr << "error: " << m << std::endl;
689 AT_FULL_COMPILE([[input]])
691 AT_PARSER_CHECK([[./input]], [[0]], [[]],
692 [[error: invalid expression
694 error: invalid character
698 AT_BISON_OPTION_POPDEFS
702 ## ------------------ ##
703 ## Exception safety. ##
704 ## ------------------ ##
706 # AT_TEST([BISON-DIRECTIVES = ''], [WITH-RECOVERY = "with"])
707 # ----------------------------------------------------------
708 # Check that no object is leaked when exceptions are thrown.
709 # WITH-RECOVERY = "with" or "without".
710 m4_pushdef([AT_TEST],
711 [AT_SETUP([[Exception safety $2 error recovery $1]])
713 AT_SKIP_IF_EXCEPTION_SUPPORT_IS_POOR
715 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" $1])
717 AT_DATA_GRAMMAR([[input.yy]],
718 [[%skeleton "lalr1.cc"
725 #include <cstdlib> // size_t and getenv.
732 /// A class that tracks its instances.
740 log (this, "Object::Object");
741 Object::instances.insert (this);
744 Object (const Object& that)
747 log (this, "Object::Object");
748 Object::instances.insert (this);
754 log (this, "Object::Object");
755 Object::instances.insert (this);
760 log (this, "Object::~Object");
761 objects::iterator i = instances.find (this);
762 // Make sure this object is alive.
763 assert (i != instances.end ());
764 Object::instances.erase (i);
767 Object& operator= (char v)
774 typedef std::set<const Object*> objects;
775 static objects instances;
780 return instances.empty ();
784 log (Object const *o, const std::string& msg)
789 std::cerr << o << "->";
790 std::cerr << msg << " {";
791 const char* sep = " ";
792 for (objects::const_iterator i = instances.begin(),
793 i_end = instances.end();
797 std::cerr << sep << *i;
800 std::cerr << " }" << std::endl;
809 #include <cstring> // strchr
811 int yylex (yy::parser::semantic_type *);
812 Object::objects Object::instances;
813 static char const *input;
819 yyo << &$$ << " '" << $$.val << '\'';
821 throw std::runtime_error ("printer");
824 %token <Object> 'a' 'E' 'e' 'p' 'R' 's' 'T'
825 %type <Object> list item
831 %destructor { delete $$; } <obj>;
834 yyo << $$ << " '" << $$->val << '\'';
836 throw std::runtime_error ("printer");
839 %token <obj> 'a' 'E' 'e' 'p' 'R' 's' 'T'
840 %type <obj> list item
845 if (strchr (input, 'i'))
846 throw std::runtime_error ("initial-action");
851 start: list {]AT_VARIANT_IF([], [ delete $][1]; )[};
855 // Right recursion to load the stack.
856 | item list { $$ = $][1; ]AT_VARIANT_IF([], [delete $][2]; )[}
861 | 'e' { YYUSE ($$); YYUSE ($][1); error ("syntax error"); }
862 // Not just 'E', otherwise we reduce when 'E' is the lookahead, and
863 // then the stack is emptied, defeating the point of the test.
864 | 'E' 'a' { YYUSE ($][1); $$ = $][2; }
865 | 'R' { ]AT_VARIANT_IF([], [$$ = YY_NULLPTR; delete $][1]; )[YYERROR; }
867 | 's' { $$ = $][1; throw std::runtime_error ("reduction"); }
868 | 'T' { ]AT_VARIANT_IF([], [$$ = YY_NULLPTR; delete $][1]; )[YYABORT; }
870 [[| error { $$ = ]AT_VARIANT_IF([], [new ])[Object ('R'); yyerrok; }]])[
875 yylex (yy::parser::semantic_type *lvalp)
878 // 'e': user action calls error.
879 // 'E': syntax error, with yyerror that throws.
880 // 'i': initial action throws.
881 // 'l': yylex throws.
882 // 'R': call YYERROR in the action
883 // 's': reduction throws.
884 // 'T': call YYABORT in the action
885 switch (int res = *input++)
888 throw std::runtime_error ("yylex");
890 lvalp->]AT_VARIANT_IF([build (Object (res))],
891 [obj = new Object (res)])[;
898 /* A C++ error reporting function. */
900 yy::parser::error (const std::string& m)
902 throw std::runtime_error (m);
906 main (int argc, const char *argv[])
914 assert (std::string(argv[1]) == "--debug");
923 debug |= !!getenv ("YYDEBUG");
924 parser.set_debug_level (debug);
928 res = parser.parse ();
930 catch (const std::exception& e)
932 std::cerr << "exception caught: " << e.what () << std::endl;
936 std::cerr << "unknown exception caught" << std::endl;
938 Object::log (YY_NULLPTR, "end");
939 assert (Object::empty());
943 AT_BISON_CHECK([[-o input.cc --report=all input.yy]])
944 AT_COMPILE_CXX([[input]])
946 AT_PARSER_CHECK([[./input aaaas]], [[2]], [[]],
947 [[exception caught: reduction
950 AT_PARSER_CHECK([[./input aaaal]], [[2]], [[]],
951 [[exception caught: yylex
954 AT_PARSER_CHECK([[./input i]], [[2]], [[]],
955 [[exception caught: initial-action
958 AT_PARSER_CHECK([[./input aaaap]])
960 AT_PARSER_CHECK([[./input --debug aaaap]], [[2]], [[]], [[stderr]])
961 AT_CHECK([[grep '^exception caught: printer$' stderr]], [], [ignore])
963 AT_PARSER_CHECK([[./input aaaae]], [[2]], [[]],
964 [[exception caught: syntax error
967 AT_PARSER_CHECK([[./input aaaaE]], [[2]], [[]],
968 [[exception caught: syntax error, unexpected $end, expecting 'a'
971 AT_PARSER_CHECK([[./input aaaaT]], [[1]])
973 AT_PARSER_CHECK([[./input aaaaR]], [m4_if([$2], [with], [0], [1])])
975 AT_BISON_OPTION_POPDEFS
981 AT_TEST([], [without])
982 AT_TEST([%define api.value.type variant], [with])
983 AT_TEST([%define api.value.type variant], [without])
987 ## ------------------------------------- ##
988 ## C++ GLR parser identifier shadowing. ##
989 ## ------------------------------------- ##
991 AT_SETUP([[C++ GLR parser identifier shadowing]])
993 AT_DATA_GRAMMAR([input.yy], [
1005 int yylex (yy::parser::semantic_type *yylval);
1013 int yylex (yy::parser::semantic_type *yylval)
1015 // Note: this argument is unused, but named on purpose. There used to be a
1016 // bug with a macro that erroneously expanded this identifier to
1019 return yy::parser::token::ZERO;
1022 void yy::parser::error (std::string const&)
1029 AT_BISON_CHECK([[-o input.cc input.yy]])
1030 AT_COMPILE_CXX([[input]])