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;
257 %printer { yyo << $$; }
258 <int> <::std::string> <::std::list<std::string>>;
259 %destructor { std::cerr << "Destroy: " << $$ << '\n'; } <*>;
260 %destructor { std::cerr << "Destroy: \"" << $$ << "\"\n"; } <::std::string>;
264 list { std::cout << $][1 << std::endl; }
268 item { $$.push_back ($][1); }
269 | list "," item { std::swap ($$, $][1); $$.push_back ($][3); }
270 | list error { std::swap ($$, $][1); }
274 TEXT { std::swap ($$, $][1); }
275 | NUMBER { if ($][1 == 3) YYERROR; else $$ = to_string ($][1); }
278 ]AT_TOKEN_CTOR_IF([],
279 [[#ifdef TWO_STAGE_BUILD
280 # define BUILD(Type, Value) build<Type> () = Value
282 # define BUILD(Type, Value) build (Value)
288 static]AT_TOKEN_CTOR_IF([[
289 parser::symbol_type yylex ()]], [[
290 parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([,
291 parser::location_type* yylloc])[)]])[
293 typedef parser::location_type location;])[
294 // The 5 is a syntax error whose recovery requires that we discard
295 // the lookahead. This tests a regression, see
296 // <http://savannah.gnu.org/support/?108481>.
297 static char const *input = "0,1,2,3,45,6";
298 switch (int stage = *input++)
300 case 0:]AT_TOKEN_CTOR_IF([[
301 return parser::make_END_OF_FILE (]AT_LOCATION_IF([location ()])[);]],
303 *yylloc = location ();])[
304 return parser::token::END_OF_FILE;]])[
308 return parser::make_COMMA (]AT_LOCATION_IF([location ()])[);]], [[
310 *yylloc = location ();])[
311 return parser::token::COMMA;]])[
316 {]AT_TOKEN_CTOR_IF([[
317 return parser::make_NUMBER (stage]AT_LOCATION_IF([, location ()])[);]], [[
318 yylval->BUILD (int, stage);]AT_LOCATION_IF([
319 *yylloc = location ();])[
320 return parser::token::NUMBER;]])[
323 {]AT_TOKEN_CTOR_IF([[
324 return parser::make_TEXT (to_string (stage)]AT_LOCATION_IF([, location ()])[);]], [[
325 yylval->BUILD (std::string, to_string (stage));]AT_LOCATION_IF([
326 *yylloc = location ();])[
327 return parser::token::TEXT;]])[
339 AT_FULL_COMPILE([list])
340 AT_PARSER_CHECK([./list], 0,
364 Destroy: (0, 1, 2, 4, 6)
367 AT_BISON_OPTION_POPDEFS
371 AT_TEST([[%skeleton "lalr1.cc" ]])
372 AT_TEST([[%skeleton "lalr1.cc" %define parse.assert]])
373 AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %locations]])
374 AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %code {\n#define TWO_STAGE_BUILD\n}]])
375 AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %define api.token.constructor]])
376 AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %define api.token.constructor %define api.token.prefix {TOK_}]])
377 AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %define api.token.constructor %define api.token.prefix {TOK_} %locations]])
382 ## ----------------------- ##
383 ## Doxygen Documentation. ##
384 ## ----------------------- ##
386 m4_define([AT_CHECK_DOXYGEN],
388 [Public], [m4_pushdef([AT_DOXYGEN_PRIVATE], [NO])],
389 [Private], [m4_pushdef([AT_DOXYGEN_PRIVATE], [YES])],
390 [m4_fatal([invalid argument: $1])])
391 AT_SETUP([Doxygen $1 Documentation])
393 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
395 [[%skeleton "lalr1.cc"
405 AT_BISON_CHECK([-o input.cc input.yy])
408 [# The PROJECT_NAME tag is a single word (or a sequence of words
409 # surrounded by quotes) that should identify the project.
410 PROJECT_NAME = "Bison C++ Parser"
412 # The QUIET tag can be used to turn on/off the messages that are
413 # generated by doxygen. Possible values are YES and NO. If left blank
417 # The WARNINGS tag can be used to turn on/off the warning messages
418 # that are generated by doxygen. Possible values are YES and NO. If
419 # left blank NO is used.
421 # If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate
422 # warnings for undocumented members. If EXTRACT_ALL is set to YES then
423 # this flag will automatically be disabled.
424 WARN_IF_UNDOCUMENTED = YES
425 # If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings
426 # for potential errors in the documentation, such as not documenting
427 # some parameters in a documented function, or documenting parameters
428 # that don't exist or using markup commands wrongly.
429 WARN_IF_DOC_ERROR = YES
430 # The WARN_FORMAT tag determines the format of the warning messages
431 # that doxygen can produce. The string should contain the $file,
432 # $line, and $text tags, which will be replaced by the file and line
433 # number from which the warning originated and the warning text.
434 WARN_FORMAT = "$file:$line: $text"
436 # If the EXTRACT_ALL tag is set to YES doxygen will assume all
437 # entities in documentation are documented, even if no documentation
438 # was available. Private class members and static file members will
439 # be hidden unless the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set
443 # If the EXTRACT_PRIVATE tag is set to YES all private members of a
444 # class will be included in the documentation.
445 EXTRACT_PRIVATE = AT_DOXYGEN_PRIVATE
447 # If the EXTRACT_STATIC tag is set to YES all static members of a file
448 # will be included in the documentation.
449 EXTRACT_STATIC = AT_DOXYGEN_PRIVATE
452 AT_CHECK([doxygen --version || exit 77], 0, ignore)
453 AT_CHECK([doxygen], 0, [], [ignore])
455 AT_BISON_OPTION_POPDEFS
458 m4_popdef([AT_DOXYGEN_PRIVATE])
461 AT_CHECK_DOXYGEN([Public])
462 AT_CHECK_DOXYGEN([Private])
469 # AT_TEST(NAMESPACE-DECL, [COMPILE-ERROR])
470 # ----------------------------------------
471 # See if Bison can handle %define namespace "NAMESPACE-DECL". If COMPILE-ERROR
472 # is specified, then Bison should accept the input, but compilation will fail,
473 # so don't check compilation.
474 m4_pushdef([AT_TEST],
475 [AT_BISON_OPTION_PUSHDEFS([%language "C++" %define api.namespace {$1}])
476 AT_DATA_GRAMMAR([[input.yy]],
478 %define api.namespace {]$1[}
480 %define global_tokens_and_yystype
484 // YYSTYPE contains a namespace reference.
485 int yylex (YYSTYPE *lval, const ]$1[::parser::location_type*) {
498 ]$1[::parser::error (const ]$1[::parser::location_type &loc,
499 const std::string &msg)
501 std::cerr << "At " << loc << ": " << msg << std::endl;
508 AT_BISON_CHECK([[-o input.cc input.yy]])
511 [AT_COMPILE_CXX([[input]])
512 AT_PARSER_CHECK([[./input]])])
513 AT_BISON_OPTION_POPDEFS
516 AT_SETUP([[Relative namespace references]])
518 AT_TEST([[foo::bar]])
519 AT_TEST([[foo::bar::baz]])
522 AT_SETUP([[Absolute namespace references]])
524 AT_TEST([[::foo::bar]])
525 AT_TEST([[::foo::bar::baz]])
526 AT_TEST([[@tb@::foo]])
527 AT_TEST([[ @tb@ ::foo::bar]])
528 AT_TEST([[ ::foo::bar::baz]])
531 AT_SETUP([[Syntactically invalid namespace references]])
532 AT_TEST([[:foo:bar]], [[-]])
533 AT_TEST([[foo: :bar]], [[-]])
534 # This one is interesting because '[3]' is encoded as '@<:@3@:>@', which
535 # contains single occurrences of ':'.
536 AT_TEST([[foo[3]::bar::baz]], [[-]])
537 AT_TEST([[foo::bar,baz]], [[-]])
538 AT_TEST([[foo::bar::(baz /* Pacify Emacs ) */]], [[-]])
543 ## -------------------------------------- ##
544 ## Syntax error discarding no lookahead. ##
545 ## -------------------------------------- ##
547 # After a syntax error, lalr1.cc used to not check whether there
548 # actually is a lookahead before discarding the lookahead. As a result,
549 # it mistakenly invoked the destructor for the previous lookahead.
551 AT_SETUP([[Syntax error discarding no lookahead]])
553 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
555 AT_DATA_GRAMMAR([[input.y]],
556 [[%skeleton "lalr1.cc"
560 int yylex (yy::parser::semantic_type *);
564 %define parse.error verbose
569 std::cerr << "Discarding 'a'." << std::endl;
574 start: error-reduce consistent-error 'a' { USE ($3); };
577 'a' 'a' consistent-error 'a' { USE (($1, $2, $4)); }
578 | 'a' error { std::cerr << "Reducing 'a'." << std::endl; USE ($1); }
583 | /*empty*/ %prec 'a'
586 // Provide another context in which all rules are useful so that this
587 // test case looks a little more realistic.
588 start: 'b' consistent-error ;
593 yylex (yy::parser::semantic_type *)
595 static char const *input = "aa";
600 yy::parser::error (const std::string &m)
602 std::cerr << m << std::endl;
608 AT_FULL_COMPILE([[input]])
609 # This used to print "Discarding 'a'." again at the end.
610 AT_PARSER_CHECK([[./input]], [[1]], [[]],
616 AT_BISON_OPTION_POPDEFS
620 ## --------------------------- ##
621 ## Syntax error as exception. ##
622 ## --------------------------- ##
624 AT_SETUP([[Syntax error as exception]])
626 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
628 AT_DATA_GRAMMAR([[input.y]],
629 [[%skeleton "lalr1.cc"
634 int yylex (yy::parser::semantic_type *);
637 %define api.value.type variant
638 %define parse.error verbose
648 error { std::cerr << "caught error" << std::endl; }
656 throw yy::parser::syntax_error ("invalid expression");
662 yylex (yy::parser::semantic_type *)
664 // 's': syntax error, 'l': lexical error.
665 static char const *input = "asal";
666 switch (int res = *input++)
669 throw yy::parser::syntax_error ("invalid character");
676 yy::parser::error (const std::string &m)
678 std::cerr << "error: " << m << std::endl;
683 AT_FULL_COMPILE([[input]])
685 AT_PARSER_CHECK([[./input]], [[0]], [[]],
686 [[error: invalid expression
688 error: invalid character
692 AT_BISON_OPTION_POPDEFS
696 ## ------------------ ##
697 ## Exception safety. ##
698 ## ------------------ ##
700 # AT_TEST([BISON-DIRECTIVES = ''], [WITH-RECOVERY = "with"])
701 # ----------------------------------------------------------
702 # Check that no object is leaked when exceptions are thrown.
703 # WITH-RECOVERY = "with" or "without".
704 m4_pushdef([AT_TEST],
705 [AT_SETUP([[Exception safety $2 error recovery $1]])
707 AT_SKIP_IF_EXCEPTION_SUPPORT_IS_POOR
709 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" $1])
711 AT_DATA_GRAMMAR([[input.yy]],
712 [[%skeleton "lalr1.cc"
719 #include <cstdlib> // size_t and getenv.
726 /// A class that tracks its instances.
734 log (this, "Object::Object");
735 Object::instances.insert (this);
738 Object (const Object& that)
741 log (this, "Object::Object");
742 Object::instances.insert (this);
748 log (this, "Object::Object");
749 Object::instances.insert (this);
754 log (this, "Object::~Object");
755 objects::iterator i = instances.find (this);
756 // Make sure this object is alive.
757 assert (i != instances.end ());
758 Object::instances.erase (i);
761 Object& operator= (char v)
768 typedef std::set<const Object*> objects;
769 static objects instances;
774 return instances.empty ();
778 log (Object const *o, const std::string& msg)
783 std::cerr << o << "->";
784 std::cerr << msg << " {";
785 const char* sep = " ";
786 for (objects::const_iterator i = instances.begin(),
787 i_end = instances.end();
791 std::cerr << sep << *i;
794 std::cerr << " }" << std::endl;
803 #include <cstring> // strchr
805 int yylex (yy::parser::semantic_type *);
806 Object::objects Object::instances;
807 static char const *input;
813 yyo << &$$ << " '" << $$.val << '\'';
815 throw std::runtime_error ("printer");
818 %token <Object> 'a' 'E' 'e' 'p' 'R' 's' 'T'
819 %type <Object> list item
825 %destructor { delete $$; } <obj>;
828 yyo << $$ << " '" << $$->val << '\'';
830 throw std::runtime_error ("printer");
833 %token <obj> 'a' 'E' 'e' 'p' 'R' 's' 'T'
834 %type <obj> list item
839 if (strchr (input, 'i'))
840 throw std::runtime_error ("initial-action");
845 start: list {]AT_VARIANT_IF([], [ delete $][1]; )[};
849 // Right recursion to load the stack.
850 | item list { $$ = $][1; ]AT_VARIANT_IF([], [delete $][2]; )[}
855 | 'e' { YYUSE ($$); YYUSE ($][1); error ("syntax error"); }
856 // Not just 'E', otherwise we reduce when 'E' is the lookahead, and
857 // then the stack is emptied, defeating the point of the test.
858 | 'E' 'a' { YYUSE ($][1); $$ = $][2; }
859 | 'R' { ]AT_VARIANT_IF([], [$$ = YY_NULLPTR; delete $][1]; )[YYERROR; }
861 | 's' { $$ = $][1; throw std::runtime_error ("reduction"); }
862 | 'T' { ]AT_VARIANT_IF([], [$$ = YY_NULLPTR; delete $][1]; )[YYABORT; }
864 [[| error { $$ = ]AT_VARIANT_IF([], [new ])[Object ('R'); yyerrok; }]])[
869 yylex (yy::parser::semantic_type *lvalp)
872 // 'e': user action calls error.
873 // 'E': syntax error, with yyerror that throws.
874 // 'i': initial action throws.
875 // 'l': yylex throws.
876 // 'R': call YYERROR in the action
877 // 's': reduction throws.
878 // 'T': call YYABORT in the action
879 switch (int res = *input++)
882 throw std::runtime_error ("yylex");
884 lvalp->]AT_VARIANT_IF([build (Object (res))],
885 [obj = new Object (res)])[;
892 /* A C++ error reporting function. */
894 yy::parser::error (const std::string& m)
896 throw std::runtime_error (m);
900 main (int argc, const char *argv[])
908 assert (std::string(argv[1]) == "--debug");
917 debug |= !!getenv ("YYDEBUG");
918 parser.set_debug_level (debug);
922 res = parser.parse ();
924 catch (const std::exception& e)
926 std::cerr << "exception caught: " << e.what () << std::endl;
930 std::cerr << "unknown exception caught" << std::endl;
932 Object::log (YY_NULLPTR, "end");
933 assert (Object::empty());
937 AT_BISON_CHECK([[-o input.cc --report=all input.yy]])
938 AT_COMPILE_CXX([[input]])
940 AT_PARSER_CHECK([[./input aaaas]], [[2]], [[]],
941 [[exception caught: reduction
944 AT_PARSER_CHECK([[./input aaaal]], [[2]], [[]],
945 [[exception caught: yylex
948 AT_PARSER_CHECK([[./input i]], [[2]], [[]],
949 [[exception caught: initial-action
952 AT_PARSER_CHECK([[./input aaaap]])
954 AT_PARSER_CHECK([[./input --debug aaaap]], [[2]], [[]], [[stderr]])
955 AT_CHECK([[grep '^exception caught: printer$' stderr]], [], [ignore])
957 AT_PARSER_CHECK([[./input aaaae]], [[2]], [[]],
958 [[exception caught: syntax error
961 AT_PARSER_CHECK([[./input aaaaE]], [[2]], [[]],
962 [[exception caught: syntax error, unexpected $end, expecting 'a'
965 AT_PARSER_CHECK([[./input aaaaT]], [[1]])
967 AT_PARSER_CHECK([[./input aaaaR]], [m4_if([$2], [with], [0], [1])])
969 AT_BISON_OPTION_POPDEFS
975 AT_TEST([], [without])
976 AT_TEST([%define api.value.type variant], [with])
977 AT_TEST([%define api.value.type variant], [without])
981 ## ------------------------------------- ##
982 ## C++ GLR parser identifier shadowing. ##
983 ## ------------------------------------- ##
985 AT_SETUP([[C++ GLR parser identifier shadowing]])
987 AT_DATA_GRAMMAR([input.yy], [
999 int yylex (yy::parser::semantic_type *yylval);
1007 int yylex (yy::parser::semantic_type *yylval)
1009 // Note: this argument is unused, but named on purpose. There used to be a
1010 // bug with a macro that erroneously expanded this identifier to
1013 return yy::parser::token::ZERO;
1016 void yy::parser::error (std::string const&)
1023 AT_BISON_CHECK([[-o input.cc input.yy]])
1024 AT_COMPILE_CXX([[input]])