1 # Checking the C++ Features. -*- Autotest -*-
3 # Copyright (C) 2004-2005, 2007-2012 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_TEST([DIRECTIVES])
26 # ---------------------
27 # Check the support of variants in C++, with the additional DIRECTIVES.
29 [AT_SETUP([Variants $1])
31 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" $1])
32 # Store strings and integers in a list of strings.
33 AT_DATA_GRAMMAR([list.yy],
37 %define api.value.type variant
38 ]m4_bpatsubst([$1], [\\n], [
41 %code requires // code for the .hh file
45 typedef std::list<std::string> strings_type;
48 %code // code for the .cc file
50 #include <cstdlib> // abort, getenv
56 static]AT_TOKEN_CTOR_IF([[
57 parser::symbol_type yylex ()]], [[
58 parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([,
59 parser::location_type* yylloc])[)]])[;
62 // Printing a list of strings (for %printer).
63 // Koening look up will look into std, since that's an std::list.
67 operator<<(std::ostream& o, const strings_type& s)
70 for (strings_type::const_iterator i = s.begin (); i != s.end (); ++i)
80 // Conversion to string.
84 string_cast (const T& t)
92 %token <::std::string> TEXT;
96 %type <::std::string> item;
97 // Using the template type to exercize its parsing.
98 // Starting with :: to ensure we don't output "<::" which starts by the
99 // digraph for the left square bracket.
100 %type <::std::list<std::string>> list result;
102 %printer { yyo << $][$; }
103 <int> <::std::string> <::std::list<std::string>>;
107 list { std::cout << $][1 << std::endl; }
111 /* nothing */ { /* Generates an empty string list */ }
112 | list item { std::swap ($][$,$][1); $$.push_back ($][2); }
113 | list error { std::swap ($][$,$][1); }
117 TEXT { std::swap ($][$,$][1); }
118 | NUMBER { if ($][1 == 3) YYERROR; else $][$ = string_cast ($][1); }
122 #ifdef TWO_STAGE_BUILD
123 # define BUILD(Type, Value) build<Type> () = Value
125 # define BUILD(Type, Value) build (Value)
131 static]AT_TOKEN_CTOR_IF([[
132 parser::symbol_type yylex ()]], [[
133 parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([,
134 parser::location_type* yylloc])[)]])[
136 typedef parser::location_type location;])[
137 static int stage = -1;
139 if (stage == STAGE_MAX)
140 {]AT_TOKEN_CTOR_IF([[
141 return parser::make_END_OF_FILE (]AT_LOCATION_IF([location ()])[);]],
143 *yylloc = location ();])[
144 return parser::token::END_OF_FILE;]])[
147 {]AT_TOKEN_CTOR_IF([[
148 return parser::make_NUMBER (stage]AT_LOCATION_IF([, location ()])[);]],
150 yylval->BUILD (int, stage);]AT_LOCATION_IF([
151 *yylloc = location ();])[
152 return parser::token::NUMBER;]])[
155 {]AT_TOKEN_CTOR_IF([[
156 return parser::make_TEXT (string_cast (stage)]AT_LOCATION_IF([, location ()])[);]], [[
157 yylval->BUILD (std::string, string_cast (stage));]AT_LOCATION_IF([
158 *yylloc = location ();])[
159 return parser::token::TEXT;]])[
171 p.set_debug_level (!!getenv ("YYDEBUG"));
176 AT_BISON_CHECK([-o list.cc list.yy])
177 AT_COMPILE_CXX([list], [$NO_STRICT_ALIAS_CXXFLAGS list.cc])
178 AT_PARSER_CHECK([./list], 0,
182 AT_BISON_OPTION_POPDEFS
187 AT_TEST([%define parse.assert])
188 AT_TEST([%locations %define parse.assert])
189 AT_TEST([[%define parse.assert %code {\n#define TWO_STAGE_BUILD\n}]])
190 AT_TEST([[%define parse.assert %define api.token.constructor]])
191 AT_TEST([[%define parse.assert %define api.token.constructor %define api.token.prefix "TOK_"]])
192 AT_TEST([[%locations %define parse.assert %define api.token.constructor %define api.token.prefix "TOK_"]])
197 ## ----------------------- ##
198 ## Doxygen Documentation. ##
199 ## ----------------------- ##
201 m4_define([AT_CHECK_DOXYGEN],
203 [Public], [m4_pushdef([AT_DOXYGEN_PRIVATE], [NO])],
204 [Private], [m4_pushdef([AT_DOXYGEN_PRIVATE], [YES])],
205 [m4_fatal([invalid argument: $1])])
206 AT_SETUP([Doxygen $1 Documentation])
208 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
210 [[%skeleton "lalr1.cc"
220 AT_BISON_CHECK([-o input.cc input.yy], 0)
223 [# The PROJECT_NAME tag is a single word (or a sequence of words
224 # surrounded by quotes) that should identify the project.
225 PROJECT_NAME = "Bison C++ Parser"
227 # The QUIET tag can be used to turn on/off the messages that are
228 # generated by doxygen. Possible values are YES and NO. If left blank
232 # The WARNINGS tag can be used to turn on/off the warning messages
233 # that are generated by doxygen. Possible values are YES and NO. If
234 # left blank NO is used.
236 # If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate
237 # warnings for undocumented members. If EXTRACT_ALL is set to YES then
238 # this flag will automatically be disabled.
239 WARN_IF_UNDOCUMENTED = YES
240 # If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings
241 # for potential errors in the documentation, such as not documenting
242 # some parameters in a documented function, or documenting parameters
243 # that don't exist or using markup commands wrongly.
244 WARN_IF_DOC_ERROR = YES
245 # The WARN_FORMAT tag determines the format of the warning messages
246 # that doxygen can produce. The string should contain the $file,
247 # $line, and $text tags, which will be replaced by the file and line
248 # number from which the warning originated and the warning text.
249 WARN_FORMAT = "$file:$line: $text"
251 # If the EXTRACT_ALL tag is set to YES doxygen will assume all
252 # entities in documentation are documented, even if no documentation
253 # was available. Private class members and static file members will
254 # be hidden unless the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set
258 # If the EXTRACT_PRIVATE tag is set to YES all private members of a
259 # class will be included in the documentation.
260 EXTRACT_PRIVATE = AT_DOXYGEN_PRIVATE
262 # If the EXTRACT_STATIC tag is set to YES all static members of a file
263 # will be included in the documentation.
264 EXTRACT_STATIC = AT_DOXYGEN_PRIVATE
267 AT_CHECK([doxygen --version || exit 77], 0, ignore)
268 AT_CHECK([doxygen], 0, [], [ignore])
270 AT_BISON_OPTION_POPDEFS
273 m4_popdef([AT_DOXYGEN_PRIVATE])
276 AT_CHECK_DOXYGEN([Public])
277 AT_CHECK_DOXYGEN([Private])
284 # AT_CHECK_NAMESPACE(NAMESPACE-DECL, [COMPILE-ERROR])
285 # ---------------------------------------------------
286 # See if Bison can handle %define namespace "NAMESPACE-DECL". If COMPILE-ERROR
287 # is specified, then Bison should accept the input, but compilation will fail,
288 # so don't check compilation.
289 m4_define([AT_CHECK_NAMESPACE],
292 AT_DATA_GRAMMAR([[input.y]],
295 %define api.namespace "]$1["
297 %define global_tokens_and_yystype
301 // YYSTYPE contains a namespace reference.
302 int yylex (YYSTYPE *lval, const ]$1[::parser::location_type*) {
315 ]$1[::parser::error (const ]$1[::parser::location_type &loc,
316 const std::string &msg)
318 std::cerr << "At " << loc << ": " << msg << std::endl;
329 AT_BISON_CHECK([[-o input.cc input.y]])
332 [AT_COMPILE_CXX([[input]], [[input.cc]])
333 AT_PARSER_CHECK([[./input]])])
337 AT_SETUP([[Relative namespace references]])
338 AT_CHECK_NAMESPACE([[foo]])
339 AT_CHECK_NAMESPACE([[foo::bar]])
340 AT_CHECK_NAMESPACE([[foo::bar::baz]])
343 AT_SETUP([[Absolute namespace references]])
344 AT_CHECK_NAMESPACE([[::foo]])
345 AT_CHECK_NAMESPACE([[::foo::bar]])
346 AT_CHECK_NAMESPACE([[::foo::bar::baz]])
347 AT_CHECK_NAMESPACE([[ ::foo]])
348 AT_CHECK_NAMESPACE([[ ::foo::bar]])
349 AT_CHECK_NAMESPACE([[ ::foo::bar::baz]])
352 AT_SETUP([[Syntactically invalid namespace references]])
353 AT_CHECK_NAMESPACE([[:foo:bar]], [[-]])
354 AT_CHECK_NAMESPACE([[foo: :bar]], [[-]])
355 # This one is interesting because `[3]' is encoded as `@<:@3@:>@', which
356 # contains single occurrences of `:'.
357 AT_CHECK_NAMESPACE([[foo[3]::bar::baz]], [[-]])
358 AT_CHECK_NAMESPACE([[foo::bar,baz]], [[-]])
359 AT_CHECK_NAMESPACE([[foo::bar::(baz /* Pacify Emacs ) */]], [[-]])
363 ## -------------------------------------- ##
364 ## Syntax error discarding no lookahead. ##
365 ## -------------------------------------- ##
367 # After a syntax error, lalr1.cc used to not check whether there
368 # actually is a lookahead before discarding the lookahead. As a result,
369 # it mistakenly invoked the destructor for the previous lookahead.
371 AT_SETUP([[Syntax error discarding no lookahead]])
373 AT_DATA_GRAMMAR([[input.yy]],
374 [[%skeleton "lalr1.cc"
378 int yylex (yy::parser::semantic_type *);
383 %define parse.error verbose
388 std::cerr << "Discarding 'a'." << std::endl;
393 start: error-reduce consistent-error 'a' { USE ($3); };
396 'a' 'a' consistent-error 'a' { USE (($1, $2, $4)); }
397 | 'a' error { std::cerr << "Reducing 'a'." << std::endl; USE ($1); }
402 | /*empty*/ %prec 'a'
405 // Provide another context in which all rules are useful so that this
406 // test case looks a little more realistic.
407 start: 'b' consistent-error ;
412 yylex (yy::parser::semantic_type *)
414 static char const *input = "aa";
419 yy::parser::error (const std::string &m)
421 std::cerr << m << std::endl;
428 return parser.parse ();
431 AT_BISON_CHECK([[-o input.cc input.yy]])
432 AT_COMPILE_CXX([[input]])
433 # This used to print "Discarding 'a'." again at the end.
434 AT_PARSER_CHECK([[./input]], [[1]], [[]],
443 ## --------------------------- ##
444 ## Syntax error as exception. ##
445 ## --------------------------- ##
447 AT_SETUP([[Syntax error as exception]])
449 AT_DATA_GRAMMAR([[input.yy]],
450 [[%skeleton "lalr1.cc"
455 int yylex (yy::parser::semantic_type *);
459 %define api.value.type variant
460 %define parse.error verbose
470 error { std::cerr << "caught error" << std::endl; }
478 throw yy::parser::syntax_error ("invalid expression");
484 yylex (yy::parser::semantic_type *)
486 // 's': syntax error, 'l': lexical error.
487 static char const *input = "asal";
488 switch (int res = *input++)
491 throw yy::parser::syntax_error ("invalid character");
498 yy::parser::error (const std::string &m)
500 std::cerr << "error: " << m << std::endl;
507 parser.set_debug_level (!!getenv ("YYDEBUG"));
508 return parser.parse ();
511 AT_BISON_CHECK([[-o input.cc input.yy]])
512 AT_COMPILE_CXX([[input]])
514 AT_PARSER_CHECK([[./input]], [[0]], [[]],
515 [[error: invalid expression
517 error: invalid character
524 ## ------------------ ##
525 ## Exception safety. ##
526 ## ------------------ ##
528 AT_SETUP([[Exception safety]])
530 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
532 AT_DATA_GRAMMAR([[input.yy]],
533 [[%skeleton "lalr1.cc"
534 %defines // FIXME: Mandated in 2.6.
541 #include <cstdlib> // size_t and getenv.
547 /// A class that counts its number of instances.
550 typedef std::list<const Object*> objects;
551 static objects instances;
557 return instances.empty();
561 log (Object const *o, const std::string& msg)
566 std::cerr << o << "->";
567 std::cerr << msg << " {";
568 const char* sep = " ";
569 for (objects::const_iterator i = instances.begin(),
570 i_end = instances.end();
574 std::cerr << sep << *i;
577 std::cerr << " }" << std::endl;
584 instances.push_back(this);
585 log (this, "Object::Object");
590 instances.remove(this);
591 log (this, "Object::~Object");
599 #include <cstring> // strchr
601 int yylex (yy::parser::semantic_type *);
602 Object::objects Object::instances;
603 static char const *input;
613 if (strchr (input, 'i'))
614 throw std::runtime_error ("initial-action");
617 %destructor { delete $$; } <obj>;
620 yyo << $$ << " '" << $$->val << '\'';
622 throw std::runtime_error ("printer");
625 %token <obj> 'a' 'E' 'e' 'p' 'R' 's' 'T'
626 %type <obj> list item
630 start: list { delete $1; };
634 | item list { $$ = $1; delete $2; } // Right recursion to load the stack.
639 | 'e' { YYUSE ($$); YYUSE($1); error ("syntax error"); }
640 // Not just 'E', otherwise we reduce when 'E' is the lookahead, and
641 // then the stack is emptied, defeating the point of the test.
642 | 'E' 'a' { YYUSE($1); $$ = $2; }
643 | 'R' { $$ = YY_NULL; delete $1; YYERROR; }
645 | 's' { $$ = $1; throw std::runtime_error ("reduction"); }
646 | 'T' { $$ = YY_NULL; delete $1; YYABORT; }
647 | error { $$ = YY_NULL; yyerrok; }
652 yylex (yy::parser::semantic_type *lvalp)
655 // 'e': user action calls error.
656 // 'E': syntax error, with yyerror that throws.
657 // 'i': initial action throws.
658 // 'l': yylex throws.
659 // 'R': call YYERROR in the action
660 // 's': reduction throws.
661 // 'T': call YYABORT in the action
662 switch (int res = *input++)
665 throw std::runtime_error ("yylex");
667 lvalp->obj = new Object (res);
674 /* A C++ error reporting function. */
676 yy::parser::error (const std::string& m)
678 throw std::runtime_error (m);
682 main (int argc, const char *argv[])
690 assert (std::string(argv[1]) == "--debug");
699 debug |= !!getenv ("YYDEBUG");
700 parser.set_debug_level (debug);
704 res = parser.parse ();
706 catch (const std::exception& e)
708 std::cerr << "exception caught: " << e.what () << std::endl;
712 std::cerr << "unknown exception caught" << std::endl;
714 Object::log (YY_NULL, "end");
715 assert (Object::empty());
719 AT_BISON_CHECK([[-o input.cc --report=all input.yy]])
720 AT_COMPILE_CXX([[input]])
722 AT_PARSER_CHECK([[./input aaaas]], [[2]], [[]],
723 [[exception caught: reduction
726 AT_PARSER_CHECK([[./input aaaal]], [[2]], [[]],
727 [[exception caught: yylex
730 AT_PARSER_CHECK([[./input i]], [[2]], [[]],
731 [[exception caught: initial-action
734 AT_PARSER_CHECK([[./input aaaap]])
736 AT_PARSER_CHECK([[./input --debug aaaap]], [[2]], [[]], [[stderr]])
737 AT_CHECK([[grep '^exception caught: printer$' stderr]], [], [ignore])
739 AT_PARSER_CHECK([[./input aaaae]], [[2]], [[]],
740 [[exception caught: syntax error
743 AT_PARSER_CHECK([[./input aaaaE]], [[2]], [[]],
744 [[exception caught: syntax error, unexpected $end, expecting 'a'
747 AT_PARSER_CHECK([[./input aaaaT]], [[1]])
749 # There is error-recovery, so exit success.
750 AT_PARSER_CHECK([[./input aaaaR]], [[0]])
752 AT_BISON_OPTION_POPDEFS