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.]])
21 ## --------------------------- ##
22 ## C++ Variant-based Symbols. ##
23 ## --------------------------- ##
25 AT_SETUP([C++ Variant-based Symbols])
27 AT_KEYWORDS([variant])
29 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" %debug $1])
30 # Store strings and integers in a list of strings.
31 AT_DATA_GRAMMAR([list.y],
32 [[%skeleton "lalr1.cc"
33 %define api.value.type variant
39 // Get access to stack_symbol_type for the tests.
40 # define private public
47 %token <int> INT "int"
48 %type < std::list<int> > exp
50 %printer { yyo << $$; } <int>
53 for (std::list<int>::const_iterator i = $$.begin (); i != $$.end (); ++i)
61 %code requires { #include <list> }
62 %code { int yylex (yy::parser::semantic_type* yylval); }
65 exp: "int" { $$.push_back ($1); }
73 yy::parser::symbol_type s = yy::parser::make_INT(12);
74 std::cerr << s.value.as<int>() << std::endl;
78 yy::parser::symbol_type s = yy::parser::make_INT(123);
79 yy::parser::stack_symbol_type ss(1, s);
80 std::cerr << ss.value.as<int>() << std::endl;
84 yy::parser::stack_type st;
85 for (int i = 0; i < 100; ++i)
87 yy::parser::symbol_type s(yy::parser::make_INT(i));
88 yy::parser::stack_symbol_type ss(1, s);
95 AT_FULL_COMPILE([list])
96 AT_PARSER_CHECK([./list], 0, [],
101 AT_BISON_OPTION_POPDEFS
109 # AT_TEST([DIRECTIVES])
110 # ---------------------
111 # Check the support of variants in C++, with the additional DIRECTIVES.
112 m4_pushdef([AT_TEST],
113 [AT_SETUP([Variants $1])
115 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" %debug $1])
116 # Store strings and integers in a list of strings.
117 AT_DATA_GRAMMAR([list.y],
120 %define api.value.type variant
121 ]m4_bpatsubst([$1], [\\n], [
124 %code requires // code for the .hh file
128 typedef std::list<std::string> strings_type;
131 %code // code for the .cc file
133 #include <cstdlib> // abort, getenv
139 static]AT_TOKEN_CTOR_IF([[
140 parser::symbol_type yylex ()]], [[
141 parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([,
142 parser::location_type* yylloc])[)]])[;
145 // Printing a list of strings (for %printer).
146 // Koening look up will look into std, since that's an std::list.
150 operator<<(std::ostream& o, const strings_type& s)
153 for (strings_type::const_iterator i = s.begin (); i != s.end (); ++i)
163 // Conversion to string.
164 template <typename T>
167 string_cast (const T& t)
169 std::ostringstream o;
175 %token <::std::string> TEXT;
177 %token END_OF_FILE 0;
179 %type <::std::string> item;
180 // Using the template type to exercize its parsing.
181 // Starting with :: to ensure we don't output "<::" which starts by the
182 // digraph for the left square bracket.
183 %type <::std::list<std::string>> list result;
185 %printer { yyo << $][$; }
186 <int> <::std::string> <::std::list<std::string>>;
190 list { std::cout << $][1 << std::endl; }
194 /* nothing */ { /* Generates an empty string list */ }
195 | list item { std::swap ($][$,$][1); $$.push_back ($][2); }
196 | list error { std::swap ($][$,$][1); }
200 TEXT { std::swap ($][$,$][1); }
201 | NUMBER { if ($][1 == 3) YYERROR; else $][$ = string_cast ($][1); }
204 ]AT_TOKEN_CTOR_IF([],
205 [[#ifdef TWO_STAGE_BUILD
206 # define BUILD(Type, Value) build<Type> () = Value
208 # define BUILD(Type, Value) build (Value)
214 static]AT_TOKEN_CTOR_IF([[
215 parser::symbol_type yylex ()]], [[
216 parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([,
217 parser::location_type* yylloc])[)]])[
219 typedef parser::location_type location;])[
220 static int stage = -1;
222 if (stage == STAGE_MAX)
223 {]AT_TOKEN_CTOR_IF([[
224 return parser::make_END_OF_FILE (]AT_LOCATION_IF([location ()])[);]],
226 *yylloc = location ();])[
227 return parser::token::END_OF_FILE;]])[
230 {]AT_TOKEN_CTOR_IF([[
231 return parser::make_NUMBER (stage]AT_LOCATION_IF([, location ()])[);]],
233 yylval->BUILD (int, stage);]AT_LOCATION_IF([
234 *yylloc = location ();])[
235 return parser::token::NUMBER;]])[
238 {]AT_TOKEN_CTOR_IF([[
239 return parser::make_TEXT (string_cast (stage)]AT_LOCATION_IF([, location ()])[);]], [[
240 yylval->BUILD (std::string, string_cast (stage));]AT_LOCATION_IF([
241 *yylloc = location ();])[
242 return parser::token::TEXT;]])[
252 AT_FULL_COMPILE([list])
253 AT_PARSER_CHECK([./list], 0,
257 AT_BISON_OPTION_POPDEFS
262 AT_TEST([%define parse.assert])
263 AT_TEST([%locations %define parse.assert])
264 AT_TEST([[%define parse.assert %code {\n#define TWO_STAGE_BUILD\n}]])
265 AT_TEST([[%define parse.assert %define api.token.constructor]])
266 AT_TEST([[%define parse.assert %define api.token.constructor %define api.token.prefix "TOK_"]])
267 AT_TEST([[%locations %define parse.assert %define api.token.constructor %define api.token.prefix "TOK_"]])
272 ## ----------------------- ##
273 ## Doxygen Documentation. ##
274 ## ----------------------- ##
276 m4_define([AT_CHECK_DOXYGEN],
278 [Public], [m4_pushdef([AT_DOXYGEN_PRIVATE], [NO])],
279 [Private], [m4_pushdef([AT_DOXYGEN_PRIVATE], [YES])],
280 [m4_fatal([invalid argument: $1])])
281 AT_SETUP([Doxygen $1 Documentation])
283 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
285 [[%skeleton "lalr1.cc"
294 AT_BISON_CHECK([-o input.cc input.yy], 0)
297 [# The PROJECT_NAME tag is a single word (or a sequence of words
298 # surrounded by quotes) that should identify the project.
299 PROJECT_NAME = "Bison C++ Parser"
301 # The QUIET tag can be used to turn on/off the messages that are
302 # generated by doxygen. Possible values are YES and NO. If left blank
306 # The WARNINGS tag can be used to turn on/off the warning messages
307 # that are generated by doxygen. Possible values are YES and NO. If
308 # left blank NO is used.
310 # If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate
311 # warnings for undocumented members. If EXTRACT_ALL is set to YES then
312 # this flag will automatically be disabled.
313 WARN_IF_UNDOCUMENTED = YES
314 # If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings
315 # for potential errors in the documentation, such as not documenting
316 # some parameters in a documented function, or documenting parameters
317 # that don't exist or using markup commands wrongly.
318 WARN_IF_DOC_ERROR = YES
319 # The WARN_FORMAT tag determines the format of the warning messages
320 # that doxygen can produce. The string should contain the $file,
321 # $line, and $text tags, which will be replaced by the file and line
322 # number from which the warning originated and the warning text.
323 WARN_FORMAT = "$file:$line: $text"
325 # If the EXTRACT_ALL tag is set to YES doxygen will assume all
326 # entities in documentation are documented, even if no documentation
327 # was available. Private class members and static file members will
328 # be hidden unless the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set
332 # If the EXTRACT_PRIVATE tag is set to YES all private members of a
333 # class will be included in the documentation.
334 EXTRACT_PRIVATE = AT_DOXYGEN_PRIVATE
336 # If the EXTRACT_STATIC tag is set to YES all static members of a file
337 # will be included in the documentation.
338 EXTRACT_STATIC = AT_DOXYGEN_PRIVATE
341 AT_CHECK([doxygen --version || exit 77], 0, ignore)
342 AT_CHECK([doxygen], 0, [], [ignore])
344 AT_BISON_OPTION_POPDEFS
347 m4_popdef([AT_DOXYGEN_PRIVATE])
350 AT_CHECK_DOXYGEN([Public])
351 AT_CHECK_DOXYGEN([Private])
358 # AT_TEST(NAMESPACE-DECL, [COMPILE-ERROR])
359 # ----------------------------------------
360 # See if Bison can handle %define namespace "NAMESPACE-DECL". If COMPILE-ERROR
361 # is specified, then Bison should accept the input, but compilation will fail,
362 # so don't check compilation.
363 m4_pushdef([AT_TEST],
364 [AT_BISON_OPTION_PUSHDEFS([%language "C++" %define api.namespace "$1"])
365 AT_DATA_GRAMMAR([[input.y]],
367 %define api.namespace "]$1["
369 %define global_tokens_and_yystype
373 // YYSTYPE contains a namespace reference.
374 int yylex (YYSTYPE *lval, const ]$1[::parser::location_type*) {
387 ]$1[::parser::error (const ]$1[::parser::location_type &loc,
388 const std::string &msg)
390 std::cerr << "At " << loc << ": " << msg << std::endl;
397 AT_BISON_CHECK([[-o input.cc input.y]])
400 [AT_COMPILE_CXX([[input]], [[input.cc]])
401 AT_PARSER_CHECK([[./input]])])
402 AT_BISON_OPTION_POPDEFS
405 AT_SETUP([[Relative namespace references]])
407 AT_TEST([[foo::bar]])
408 AT_TEST([[foo::bar::baz]])
411 AT_SETUP([[Absolute namespace references]])
413 AT_TEST([[::foo::bar]])
414 AT_TEST([[::foo::bar::baz]])
415 AT_TEST([[@tb@::foo]])
416 AT_TEST([[ @tb@ ::foo::bar]])
417 AT_TEST([[ ::foo::bar::baz]])
420 AT_SETUP([[Syntactically invalid namespace references]])
421 AT_TEST([[:foo:bar]], [[-]])
422 AT_TEST([[foo: :bar]], [[-]])
423 # This one is interesting because '[3]' is encoded as '@<:@3@:>@', which
424 # contains single occurrences of ':'.
425 AT_TEST([[foo[3]::bar::baz]], [[-]])
426 AT_TEST([[foo::bar,baz]], [[-]])
427 AT_TEST([[foo::bar::(baz /* Pacify Emacs ) */]], [[-]])
432 ## -------------------------------------- ##
433 ## Syntax error discarding no lookahead. ##
434 ## -------------------------------------- ##
436 # After a syntax error, lalr1.cc used to not check whether there
437 # actually is a lookahead before discarding the lookahead. As a result,
438 # it mistakenly invoked the destructor for the previous lookahead.
440 AT_SETUP([[Syntax error discarding no lookahead]])
442 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
444 AT_DATA_GRAMMAR([[input.yy]],
445 [[%skeleton "lalr1.cc"
449 int yylex (yy::parser::semantic_type *);
453 %define parse.error verbose
458 std::cerr << "Discarding 'a'." << std::endl;
463 start: error-reduce consistent-error 'a' { USE ($3); };
466 'a' 'a' consistent-error 'a' { USE (($1, $2, $4)); }
467 | 'a' error { std::cerr << "Reducing 'a'." << std::endl; USE ($1); }
472 | /*empty*/ %prec 'a'
475 // Provide another context in which all rules are useful so that this
476 // test case looks a little more realistic.
477 start: 'b' consistent-error ;
482 yylex (yy::parser::semantic_type *)
484 static char const *input = "aa";
489 yy::parser::error (const std::string &m)
491 std::cerr << m << std::endl;
497 AT_BISON_CHECK([[-o input.cc input.yy]])
498 AT_COMPILE_CXX([[input]])
499 # This used to print "Discarding 'a'." again at the end.
500 AT_PARSER_CHECK([[./input]], [[1]], [[]],
506 AT_BISON_OPTION_POPDEFS
510 ## --------------------------- ##
511 ## Syntax error as exception. ##
512 ## --------------------------- ##
514 AT_SETUP([[Syntax error as exception]])
516 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
518 AT_DATA_GRAMMAR([[input.yy]],
519 [[%skeleton "lalr1.cc"
524 int yylex (yy::parser::semantic_type *);
527 %define api.value.type variant
528 %define parse.error verbose
538 error { std::cerr << "caught error" << std::endl; }
546 throw yy::parser::syntax_error ("invalid expression");
552 yylex (yy::parser::semantic_type *)
554 // 's': syntax error, 'l': lexical error.
555 static char const *input = "asal";
556 switch (int res = *input++)
559 throw yy::parser::syntax_error ("invalid character");
566 yy::parser::error (const std::string &m)
568 std::cerr << "error: " << m << std::endl;
573 AT_BISON_CHECK([[-o input.cc input.yy]])
574 AT_COMPILE_CXX([[input]])
576 AT_PARSER_CHECK([[./input]], [[0]], [[]],
577 [[error: invalid expression
579 error: invalid character
583 AT_BISON_OPTION_POPDEFS
587 ## ------------------ ##
588 ## Exception safety. ##
589 ## ------------------ ##
591 # AT_TEST([BISON-DIRECTIVES])
592 # ---------------------------
593 # Check that no object is leaked when exceptions are thrown.
594 m4_pushdef([AT_TEST],
595 [AT_SETUP([[Exception safety $1]])
597 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" $1])
599 AT_DATA_GRAMMAR([[input.yy]],
600 [[%skeleton "lalr1.cc"
607 #include <cstdlib> // size_t and getenv.
613 /// A class that counts its number of instances.
621 Object::instances.push_back(this);
622 log (this, "Object::Object");
628 Object::instances.push_back(this);
629 log (this, "Object::Object");
632 Object& operator= (char v)
640 Object::instances.remove (this);
641 log (this, "Object::~Object");
645 typedef std::list<const Object*> objects;
646 static objects instances;
651 return instances.empty ();
655 log (Object const *o, const std::string& msg)
660 std::cerr << o << "->";
661 std::cerr << msg << " {";
662 const char* sep = " ";
663 for (objects::const_iterator i = instances.begin(),
664 i_end = instances.end();
668 std::cerr << sep << *i;
671 std::cerr << " }" << std::endl;
680 #include <cstring> // strchr
682 int yylex (yy::parser::semantic_type *);
683 Object::objects Object::instances;
684 static char const *input;
690 yyo << &$$ << " '" << $$.val << '\'';
692 throw std::runtime_error ("printer");
695 %token <Object> 'a' 'E' 'e' 'p' 'R' 's' 'T'
696 %type <Object> list item
702 %destructor { delete $$; } <obj>;
705 yyo << $$ << " '" << $$->val << '\'';
707 throw std::runtime_error ("printer");
710 %token <obj> 'a' 'E' 'e' 'p' 'R' 's' 'T'
711 %type <obj> list item
716 if (strchr (input, 'i'))
717 throw std::runtime_error ("initial-action");
722 start: list {]AT_VARIANT_IF([], [ delete $][1]; )[};
725 item { $][$ = $][1; }
726 // Right recursion to load the stack.
727 | item list { $][$ = $][1; ]AT_VARIANT_IF([], [delete $][2]; )[}
732 | 'e' { YYUSE ($][$); YYUSE($][1); error ("syntax error"); }
733 // Not just 'E', otherwise we reduce when 'E' is the lookahead, and
734 // then the stack is emptied, defeating the point of the test.
735 | 'E' 'a' { YYUSE($][1); $][$ = $][2; }
736 | 'R' { $][$ = 0; ]AT_VARIANT_IF([], [delete $][1]; )[YYERROR; }
737 | 'p' { $][$ = $][1; }
738 | 's' { $][$ = $][1; throw std::runtime_error ("reduction"); }
739 | 'T' { $][$ = 0; ]AT_VARIANT_IF([], [delete $][1]; )[YYABORT; }
740 | error { $][$ = 0; yyerrok; }
745 yylex (yy::parser::semantic_type *lvalp)
748 // 'e': user action calls error.
749 // 'E': syntax error, with yyerror that throws.
750 // 'i': initial action throws.
751 // 'l': yylex throws.
752 // 'R': call YYERROR in the action
753 // 's': reduction throws.
754 // 'T': call YYABORT in the action
755 switch (int res = *input++)
758 throw std::runtime_error ("yylex");
760 lvalp]AT_VARIANT_IF([->build (res)], [->obj = new Object (res)])[;
767 /* A C++ error reporting function. */
769 yy::parser::error (const std::string& m)
771 throw std::runtime_error (m);
775 main (int argc, const char *argv[])
783 assert (std::string(argv[1]) == "--debug");
792 debug |= !!getenv ("YYDEBUG");
793 parser.set_debug_level (debug);
797 res = parser.parse ();
799 catch (const std::exception& e)
801 std::cerr << "exception caught: " << e.what () << std::endl;
805 std::cerr << "unknown exception caught" << std::endl;
807 Object::log (YY_NULL, "end");
808 assert (Object::empty());
812 AT_BISON_CHECK([[-o input.cc --report=all input.yy]])
813 AT_COMPILE_CXX([[input]])
815 AT_PARSER_CHECK([[./input aaaas]], [[2]], [[]],
816 [[exception caught: reduction
819 AT_PARSER_CHECK([[./input aaaal]], [[2]], [[]],
820 [[exception caught: yylex
823 AT_PARSER_CHECK([[./input i]], [[2]], [[]],
824 [[exception caught: initial-action
827 AT_PARSER_CHECK([[./input aaaap]])
829 AT_PARSER_CHECK([[./input --debug aaaap]], [[2]], [[]], [[stderr]])
830 AT_CHECK([[grep '^exception caught: printer$' stderr]], [], [ignore])
832 AT_PARSER_CHECK([[./input aaaae]], [[2]], [[]],
833 [[exception caught: syntax error
836 AT_PARSER_CHECK([[./input aaaaE]], [[2]], [[]],
837 [[exception caught: syntax error, unexpected $end, expecting 'a'
840 AT_PARSER_CHECK([[./input aaaaT]], [[1]])
842 # There is error-recovery, so exit success.
843 AT_PARSER_CHECK([[./input aaaaR]], [[0]])
845 AT_BISON_OPTION_POPDEFS
851 AT_TEST([%define api.value.type variant])
855 ## ------------------------------------ ##
856 ## C++ GLR parser identifier shadowing ##
857 ## ------------------------------------ ##
859 AT_SETUP([[C++ GLR parser identifier shadowing]])
861 AT_DATA_GRAMMAR([input.yy], [
873 int yylex (yy::parser::semantic_type *yylval);
881 int yylex (yy::parser::semantic_type *yylval)
883 // Note: this argument is unused, but named on purpose. There used to be a
884 // bug with a macro that erroneously expanded this identifier to
887 return yy::parser::token::ZERO;
890 void yy::parser::error (std::string const&)
897 AT_BISON_CHECK([[-o input.cc input.yy]])
898 AT_COMPILE_CXX([[input]])