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.
616 typedef std::list<const Object*> objects;
617 static objects instances;
623 return instances.empty();
627 log (Object const *o, const std::string& msg)
632 std::cerr << o << "->";
633 std::cerr << msg << " {";
634 const char* sep = " ";
635 for (objects::const_iterator i = instances.begin(),
636 i_end = instances.end();
640 std::cerr << sep << *i;
643 std::cerr << " }" << std::endl;
650 instances.push_back(this);
651 log (this, "Object::Object");
657 instances.push_back(this);
658 log (this, "Object::Object");
663 instances.remove(this);
664 log (this, "Object::~Object");
672 #include <cstring> // strchr
674 int yylex (yy::parser::semantic_type *);
675 Object::objects Object::instances;
676 static char const *input;
682 yyo << &$$ << " '" << $$.val << '\'';
684 throw std::runtime_error ("printer");
687 %token <Object> 'a' 'E' 'e' 'p' 'R' 's' 'T'
688 %type <Object> list item
694 %destructor { delete $$; } <obj>;
697 yyo << $$ << " '" << $$->val << '\'';
699 throw std::runtime_error ("printer");
702 %token <obj> 'a' 'E' 'e' 'p' 'R' 's' 'T'
703 %type <obj> list item
708 if (strchr (input, 'i'))
709 throw std::runtime_error ("initial-action");
714 start: list {]AT_VARIANT_IF([], [ delete $][1]; )[};
717 item { $][$ = $][1; }
718 // Right recursion to load the stack.
719 | item list { $][$ = $][1; ]AT_VARIANT_IF([], [delete $][2]; )[}
724 | 'e' { YYUSE ($][$); YYUSE($][1); error ("syntax error"); }
725 // Not just 'E', otherwise we reduce when 'E' is the lookahead, and
726 // then the stack is emptied, defeating the point of the test.
727 | 'E' 'a' { YYUSE($][1); $][$ = $][2; }
728 | 'R' { $][$ = YY_NULL; ]AT_VARIANT_IF([], [delete $][1]; )[YYERROR; }
729 | 'p' { $][$ = $][1; }
730 | 's' { $][$ = $][1; throw std::runtime_error ("reduction"); }
731 | 'T' { $][$ = YY_NULL; ]AT_VARIANT_IF([], [delete $][1]; )[YYABORT; }
732 | error { $][$ = YY_NULL; yyerrok; }
737 yylex (yy::parser::semantic_type *lvalp)
740 // 'e': user action calls error.
741 // 'E': syntax error, with yyerror that throws.
742 // 'i': initial action throws.
743 // 'l': yylex throws.
744 // 'R': call YYERROR in the action
745 // 's': reduction throws.
746 // 'T': call YYABORT in the action
747 switch (int res = *input++)
750 throw std::runtime_error ("yylex");
752 lvalp]AT_VARIANT_IF([->build (res)], [->obj = new Object (res)])[;
759 /* A C++ error reporting function. */
761 yy::parser::error (const std::string& m)
763 throw std::runtime_error (m);
767 main (int argc, const char *argv[])
775 assert (std::string(argv[1]) == "--debug");
784 debug |= !!getenv ("YYDEBUG");
785 parser.set_debug_level (debug);
789 res = parser.parse ();
791 catch (const std::exception& e)
793 std::cerr << "exception caught: " << e.what () << std::endl;
797 std::cerr << "unknown exception caught" << std::endl;
799 Object::log (YY_NULL, "end");
800 assert (Object::empty());
804 AT_BISON_CHECK([[-o input.cc --report=all input.yy]])
805 AT_COMPILE_CXX([[input]])
807 AT_PARSER_CHECK([[./input aaaas]], [[2]], [[]],
808 [[exception caught: reduction
811 AT_PARSER_CHECK([[./input aaaal]], [[2]], [[]],
812 [[exception caught: yylex
815 AT_PARSER_CHECK([[./input i]], [[2]], [[]],
816 [[exception caught: initial-action
819 AT_PARSER_CHECK([[./input aaaap]])
821 AT_PARSER_CHECK([[./input --debug aaaap]], [[2]], [[]], [[stderr]])
822 AT_CHECK([[grep '^exception caught: printer$' stderr]], [], [ignore])
824 AT_PARSER_CHECK([[./input aaaae]], [[2]], [[]],
825 [[exception caught: syntax error
828 AT_PARSER_CHECK([[./input aaaaE]], [[2]], [[]],
829 [[exception caught: syntax error, unexpected $end, expecting 'a'
832 AT_PARSER_CHECK([[./input aaaaT]], [[1]])
834 # There is error-recovery, so exit success.
835 AT_PARSER_CHECK([[./input aaaaR]], [[0]])
837 AT_BISON_OPTION_POPDEFS
843 AT_TEST([%define api.value.type variant])
847 ## ------------------------------------ ##
848 ## C++ GLR parser identifier shadowing ##
849 ## ------------------------------------ ##
851 AT_SETUP([[C++ GLR parser identifier shadowing]])
853 AT_DATA_GRAMMAR([input.yy], [
865 int yylex (yy::parser::semantic_type *yylval);
873 int yylex (yy::parser::semantic_type *yylval)
875 // Note: this argument is unused, but named on purpose. There used to be a
876 // bug with a macro that erroneously expanded this identifier to
879 return yy::parser::token::ZERO;
882 void yy::parser::error (std::string const&)
889 AT_BISON_CHECK([[-o input.cc input.yy]])
890 AT_COMPILE_CXX([[input]])