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([%debug $1])
178 # Store strings and integers in a list of strings.
179 AT_DATA_GRAMMAR([list.y],
181 %define api.value.type variant
182 ]m4_bpatsubst([$1], [\\n], [
185 %code requires // code for the .hh file
189 typedef std::list<std::string> strings_type;
192 %code // code for the .cc file
194 #include <cstdlib> // abort, getenv
200 static]AT_TOKEN_CTOR_IF([[
201 parser::symbol_type yylex ()]], [[
202 parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([,
203 parser::location_type* yylloc])[)]])[;
206 // Printing a list of strings (for %printer).
207 // Koening look up will look into std, since that's an std::list.
211 operator<<(std::ostream& o, const strings_type& s)
214 for (strings_type::const_iterator i = s.begin (); i != s.end (); ++i)
224 // Conversion to string.
225 template <typename T>
228 string_cast (const T& t)
230 std::ostringstream o;
236 %token <::std::string> TEXT;
238 %token END_OF_FILE 0;
240 %type <::std::string> item;
241 // Using the template type to exercize its parsing.
242 // Starting with :: to ensure we don't output "<::" which starts by the
243 // digraph for the left square bracket.
244 %type <::std::list<std::string>> list result;
246 %printer { yyo << $$; }
247 <int> <::std::string> <::std::list<std::string>>;
251 list { std::cout << $][1 << std::endl; }
255 /* nothing */ { /* Generates an empty string list */ }
256 | list item { std::swap ($$,$][1); $$.push_back ($][2); }
257 | list error { std::swap ($$,$][1); }
261 TEXT { std::swap ($$,$][1); }
262 | NUMBER { if ($][1 == 3) YYERROR; else $$ = string_cast ($][1); }
265 ]AT_TOKEN_CTOR_IF([],
266 [[#ifdef TWO_STAGE_BUILD
267 # define BUILD(Type, Value) build<Type> () = Value
269 # define BUILD(Type, Value) build (Value)
275 static]AT_TOKEN_CTOR_IF([[
276 parser::symbol_type yylex ()]], [[
277 parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([,
278 parser::location_type* yylloc])[)]])[
280 typedef parser::location_type location;])[
281 static int stage = -1;
283 if (stage == STAGE_MAX)
284 {]AT_TOKEN_CTOR_IF([[
285 return parser::make_END_OF_FILE (]AT_LOCATION_IF([location ()])[);]],
287 *yylloc = location ();])[
288 return parser::token::END_OF_FILE;]])[
291 {]AT_TOKEN_CTOR_IF([[
292 return parser::make_NUMBER (stage]AT_LOCATION_IF([, location ()])[);]],
294 yylval->BUILD (int, stage);]AT_LOCATION_IF([
295 *yylloc = location ();])[
296 return parser::token::NUMBER;]])[
299 {]AT_TOKEN_CTOR_IF([[
300 return parser::make_TEXT (string_cast (stage)]AT_LOCATION_IF([, location ()])[);]], [[
301 yylval->BUILD (std::string, string_cast (stage));]AT_LOCATION_IF([
302 *yylloc = location ();])[
303 return parser::token::TEXT;]])[
313 AT_FULL_COMPILE([list])
314 AT_PARSER_CHECK([./list], 0,
318 AT_BISON_OPTION_POPDEFS
322 AT_TEST([[%skeleton "lalr1.cc" ]])
323 AT_TEST([[%skeleton "lalr1.cc" %define parse.assert]])
324 AT_TEST([[%skeleton "lalr1.cc" %locations %define parse.assert]])
325 AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %code {\n#define TWO_STAGE_BUILD\n}]])
326 AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %define api.token.constructor]])
327 AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %define api.token.constructor %define api.token.prefix {TOK_}]])
328 AT_TEST([[%skeleton "lalr1.cc" %locations %define parse.assert %define api.token.constructor %define api.token.prefix {TOK_}]])
333 ## ----------------------- ##
334 ## Doxygen Documentation. ##
335 ## ----------------------- ##
337 m4_define([AT_CHECK_DOXYGEN],
339 [Public], [m4_pushdef([AT_DOXYGEN_PRIVATE], [NO])],
340 [Private], [m4_pushdef([AT_DOXYGEN_PRIVATE], [YES])],
341 [m4_fatal([invalid argument: $1])])
342 AT_SETUP([Doxygen $1 Documentation])
344 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
346 [[%skeleton "lalr1.cc"
355 AT_BISON_CHECK([-o input.cc input.yy], 0)
358 [# The PROJECT_NAME tag is a single word (or a sequence of words
359 # surrounded by quotes) that should identify the project.
360 PROJECT_NAME = "Bison C++ Parser"
362 # The QUIET tag can be used to turn on/off the messages that are
363 # generated by doxygen. Possible values are YES and NO. If left blank
367 # The WARNINGS tag can be used to turn on/off the warning messages
368 # that are generated by doxygen. Possible values are YES and NO. If
369 # left blank NO is used.
371 # If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate
372 # warnings for undocumented members. If EXTRACT_ALL is set to YES then
373 # this flag will automatically be disabled.
374 WARN_IF_UNDOCUMENTED = YES
375 # If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings
376 # for potential errors in the documentation, such as not documenting
377 # some parameters in a documented function, or documenting parameters
378 # that don't exist or using markup commands wrongly.
379 WARN_IF_DOC_ERROR = YES
380 # The WARN_FORMAT tag determines the format of the warning messages
381 # that doxygen can produce. The string should contain the $file,
382 # $line, and $text tags, which will be replaced by the file and line
383 # number from which the warning originated and the warning text.
384 WARN_FORMAT = "$file:$line: $text"
386 # If the EXTRACT_ALL tag is set to YES doxygen will assume all
387 # entities in documentation are documented, even if no documentation
388 # was available. Private class members and static file members will
389 # be hidden unless the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set
393 # If the EXTRACT_PRIVATE tag is set to YES all private members of a
394 # class will be included in the documentation.
395 EXTRACT_PRIVATE = AT_DOXYGEN_PRIVATE
397 # If the EXTRACT_STATIC tag is set to YES all static members of a file
398 # will be included in the documentation.
399 EXTRACT_STATIC = AT_DOXYGEN_PRIVATE
402 AT_CHECK([doxygen --version || exit 77], 0, ignore)
403 AT_CHECK([doxygen], 0, [], [ignore])
405 AT_BISON_OPTION_POPDEFS
408 m4_popdef([AT_DOXYGEN_PRIVATE])
411 AT_CHECK_DOXYGEN([Public])
412 AT_CHECK_DOXYGEN([Private])
419 # AT_TEST(NAMESPACE-DECL, [COMPILE-ERROR])
420 # ----------------------------------------
421 # See if Bison can handle %define namespace "NAMESPACE-DECL". If COMPILE-ERROR
422 # is specified, then Bison should accept the input, but compilation will fail,
423 # so don't check compilation.
424 m4_pushdef([AT_TEST],
425 [AT_BISON_OPTION_PUSHDEFS([%language "C++" %define api.namespace {$1}])
426 AT_DATA_GRAMMAR([[input.y]],
428 %define api.namespace {]$1[}
430 %define global_tokens_and_yystype
434 // YYSTYPE contains a namespace reference.
435 int yylex (YYSTYPE *lval, const ]$1[::parser::location_type*) {
448 ]$1[::parser::error (const ]$1[::parser::location_type &loc,
449 const std::string &msg)
451 std::cerr << "At " << loc << ": " << msg << std::endl;
458 AT_BISON_CHECK([[-o input.cc input.y]])
461 [AT_COMPILE_CXX([[input]], [[input.cc]])
462 AT_PARSER_CHECK([[./input]])])
463 AT_BISON_OPTION_POPDEFS
466 AT_SETUP([[Relative namespace references]])
468 AT_TEST([[foo::bar]])
469 AT_TEST([[foo::bar::baz]])
472 AT_SETUP([[Absolute namespace references]])
474 AT_TEST([[::foo::bar]])
475 AT_TEST([[::foo::bar::baz]])
476 AT_TEST([[@tb@::foo]])
477 AT_TEST([[ @tb@ ::foo::bar]])
478 AT_TEST([[ ::foo::bar::baz]])
481 AT_SETUP([[Syntactically invalid namespace references]])
482 AT_TEST([[:foo:bar]], [[-]])
483 AT_TEST([[foo: :bar]], [[-]])
484 # This one is interesting because '[3]' is encoded as '@<:@3@:>@', which
485 # contains single occurrences of ':'.
486 AT_TEST([[foo[3]::bar::baz]], [[-]])
487 AT_TEST([[foo::bar,baz]], [[-]])
488 AT_TEST([[foo::bar::(baz /* Pacify Emacs ) */]], [[-]])
493 ## -------------------------------------- ##
494 ## Syntax error discarding no lookahead. ##
495 ## -------------------------------------- ##
497 # After a syntax error, lalr1.cc used to not check whether there
498 # actually is a lookahead before discarding the lookahead. As a result,
499 # it mistakenly invoked the destructor for the previous lookahead.
501 AT_SETUP([[Syntax error discarding no lookahead]])
503 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
505 AT_DATA_GRAMMAR([[input.yy]],
506 [[%skeleton "lalr1.cc"
510 int yylex (yy::parser::semantic_type *);
514 %define parse.error verbose
519 std::cerr << "Discarding 'a'." << std::endl;
524 start: error-reduce consistent-error 'a' { USE ($3); };
527 'a' 'a' consistent-error 'a' { USE (($1, $2, $4)); }
528 | 'a' error { std::cerr << "Reducing 'a'." << std::endl; USE ($1); }
533 | /*empty*/ %prec 'a'
536 // Provide another context in which all rules are useful so that this
537 // test case looks a little more realistic.
538 start: 'b' consistent-error ;
543 yylex (yy::parser::semantic_type *)
545 static char const *input = "aa";
550 yy::parser::error (const std::string &m)
552 std::cerr << m << std::endl;
558 AT_BISON_CHECK([[-o input.cc input.yy]])
559 AT_COMPILE_CXX([[input]])
560 # This used to print "Discarding 'a'." again at the end.
561 AT_PARSER_CHECK([[./input]], [[1]], [[]],
567 AT_BISON_OPTION_POPDEFS
571 ## --------------------------- ##
572 ## Syntax error as exception. ##
573 ## --------------------------- ##
575 AT_SETUP([[Syntax error as exception]])
577 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
579 AT_DATA_GRAMMAR([[input.yy]],
580 [[%skeleton "lalr1.cc"
585 int yylex (yy::parser::semantic_type *);
588 %define api.value.type variant
589 %define parse.error verbose
599 error { std::cerr << "caught error" << std::endl; }
607 throw yy::parser::syntax_error ("invalid expression");
613 yylex (yy::parser::semantic_type *)
615 // 's': syntax error, 'l': lexical error.
616 static char const *input = "asal";
617 switch (int res = *input++)
620 throw yy::parser::syntax_error ("invalid character");
627 yy::parser::error (const std::string &m)
629 std::cerr << "error: " << m << std::endl;
634 AT_BISON_CHECK([[-o input.cc input.yy]])
635 AT_COMPILE_CXX([[input]])
637 AT_PARSER_CHECK([[./input]], [[0]], [[]],
638 [[error: invalid expression
640 error: invalid character
644 AT_BISON_OPTION_POPDEFS
648 ## ------------------ ##
649 ## Exception safety. ##
650 ## ------------------ ##
652 # AT_TEST([BISON-DIRECTIVES])
653 # ---------------------------
654 # Check that no object is leaked when exceptions are thrown.
655 m4_pushdef([AT_TEST],
656 [AT_SETUP([[Exception safety $1]])
658 AT_SKIP_IF_EXCEPTION_SUPPORT_IS_POOR
660 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" $1])
662 AT_DATA_GRAMMAR([[input.yy]],
663 [[%skeleton "lalr1.cc"
670 #include <cstdlib> // size_t and getenv.
676 /// A class that counts its number of instances.
684 Object::instances.push_back(this);
685 log (this, "Object::Object");
691 Object::instances.push_back(this);
692 log (this, "Object::Object");
695 Object& operator= (char v)
703 Object::instances.remove (this);
704 log (this, "Object::~Object");
708 typedef std::list<const Object*> objects;
709 static objects instances;
714 return instances.empty ();
718 log (Object const *o, const std::string& msg)
723 std::cerr << o << "->";
724 std::cerr << msg << " {";
725 const char* sep = " ";
726 for (objects::const_iterator i = instances.begin(),
727 i_end = instances.end();
731 std::cerr << sep << *i;
734 std::cerr << " }" << std::endl;
743 #include <cstring> // strchr
745 int yylex (yy::parser::semantic_type *);
746 Object::objects Object::instances;
747 static char const *input;
753 yyo << &$$ << " '" << $$.val << '\'';
755 throw std::runtime_error ("printer");
758 %token <Object> 'a' 'E' 'e' 'p' 'R' 's' 'T'
759 %type <Object> list item
765 %destructor { delete $$; } <obj>;
768 yyo << $$ << " '" << $$->val << '\'';
770 throw std::runtime_error ("printer");
773 %token <obj> 'a' 'E' 'e' 'p' 'R' 's' 'T'
774 %type <obj> list item
779 if (strchr (input, 'i'))
780 throw std::runtime_error ("initial-action");
785 start: list {]AT_VARIANT_IF([], [ delete $][1]; )[};
789 // Right recursion to load the stack.
790 | item list { $$ = $][1; ]AT_VARIANT_IF([], [delete $][2]; )[}
795 | 'e' { YYUSE ($$); YYUSE($][1); error ("syntax error"); }
796 // Not just 'E', otherwise we reduce when 'E' is the lookahead, and
797 // then the stack is emptied, defeating the point of the test.
798 | 'E' 'a' { YYUSE($][1); $$ = $][2; }
799 | 'R' { ]AT_VARIANT_IF([], [$$ = YY_NULLPTR; delete $][1]; )[YYERROR; }
801 | 's' { $$ = $][1; throw std::runtime_error ("reduction"); }
802 | 'T' { ]AT_VARIANT_IF([], [$$ = YY_NULLPTR; delete $][1]; )[YYABORT; }
803 | error { ]AT_VARIANT_IF([], [$][$ = YY_NULLPTR; ])[yyerrok; }
808 yylex (yy::parser::semantic_type *lvalp)
811 // 'e': user action calls error.
812 // 'E': syntax error, with yyerror that throws.
813 // 'i': initial action throws.
814 // 'l': yylex throws.
815 // 'R': call YYERROR in the action
816 // 's': reduction throws.
817 // 'T': call YYABORT in the action
818 switch (int res = *input++)
821 throw std::runtime_error ("yylex");
823 lvalp->]AT_VARIANT_IF([build (Object (res))],
824 [obj = new Object (res)])[;
831 /* A C++ error reporting function. */
833 yy::parser::error (const std::string& m)
835 throw std::runtime_error (m);
839 main (int argc, const char *argv[])
847 assert (std::string(argv[1]) == "--debug");
856 debug |= !!getenv ("YYDEBUG");
857 parser.set_debug_level (debug);
861 res = parser.parse ();
863 catch (const std::exception& e)
865 std::cerr << "exception caught: " << e.what () << std::endl;
869 std::cerr << "unknown exception caught" << std::endl;
871 Object::log (YY_NULLPTR, "end");
872 assert (Object::empty());
876 AT_BISON_CHECK([[-o input.cc --report=all input.yy]])
877 AT_COMPILE_CXX([[input]])
879 AT_PARSER_CHECK([[./input aaaas]], [[2]], [[]],
880 [[exception caught: reduction
883 AT_PARSER_CHECK([[./input aaaal]], [[2]], [[]],
884 [[exception caught: yylex
887 AT_PARSER_CHECK([[./input i]], [[2]], [[]],
888 [[exception caught: initial-action
891 AT_PARSER_CHECK([[./input aaaap]])
893 AT_PARSER_CHECK([[./input --debug aaaap]], [[2]], [[]], [[stderr]])
894 AT_CHECK([[grep '^exception caught: printer$' stderr]], [], [ignore])
896 AT_PARSER_CHECK([[./input aaaae]], [[2]], [[]],
897 [[exception caught: syntax error
900 AT_PARSER_CHECK([[./input aaaaE]], [[2]], [[]],
901 [[exception caught: syntax error, unexpected $end, expecting 'a'
904 AT_PARSER_CHECK([[./input aaaaT]], [[1]])
906 # There is error-recovery, so exit success.
907 AT_PARSER_CHECK([[./input aaaaR]], [[0]])
909 AT_BISON_OPTION_POPDEFS
915 AT_TEST([%define api.value.type variant])
919 ## ------------------------------------ ##
920 ## C++ GLR parser identifier shadowing ##
921 ## ------------------------------------ ##
923 AT_SETUP([[C++ GLR parser identifier shadowing]])
925 AT_DATA_GRAMMAR([input.yy], [
937 int yylex (yy::parser::semantic_type *yylval);
945 int yylex (yy::parser::semantic_type *yylval)
947 // Note: this argument is unused, but named on purpose. There used to be a
948 // bug with a macro that erroneously expanded this identifier to
951 return yy::parser::token::ZERO;
954 void yy::parser::error (std::string const&)
961 AT_BISON_CHECK([[-o input.cc input.yy]])
962 AT_COMPILE_CXX([[input]])