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_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;]])[
169 AT_BISON_CHECK([-o list.cc list.yy])
170 AT_COMPILE_CXX([list], [$NO_STRICT_ALIAS_CXXFLAGS list.cc])
171 AT_PARSER_CHECK([./list], 0,
175 AT_BISON_OPTION_POPDEFS
180 AT_TEST([%define parse.assert])
181 AT_TEST([%locations %define parse.assert])
182 AT_TEST([[%define parse.assert %code {\n#define TWO_STAGE_BUILD\n}]])
183 AT_TEST([[%define parse.assert %define api.token.constructor]])
184 AT_TEST([[%define parse.assert %define api.token.constructor %define api.token.prefix "TOK_"]])
185 AT_TEST([[%locations %define parse.assert %define api.token.constructor %define api.token.prefix "TOK_"]])
190 ## ----------------------- ##
191 ## Doxygen Documentation. ##
192 ## ----------------------- ##
194 m4_define([AT_CHECK_DOXYGEN],
196 [Public], [m4_pushdef([AT_DOXYGEN_PRIVATE], [NO])],
197 [Private], [m4_pushdef([AT_DOXYGEN_PRIVATE], [YES])],
198 [m4_fatal([invalid argument: $1])])
199 AT_SETUP([Doxygen $1 Documentation])
201 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
203 [[%skeleton "lalr1.cc"
213 AT_BISON_CHECK([-o input.cc input.yy], 0)
216 [# The PROJECT_NAME tag is a single word (or a sequence of words
217 # surrounded by quotes) that should identify the project.
218 PROJECT_NAME = "Bison C++ Parser"
220 # The QUIET tag can be used to turn on/off the messages that are
221 # generated by doxygen. Possible values are YES and NO. If left blank
225 # The WARNINGS tag can be used to turn on/off the warning messages
226 # that are generated by doxygen. Possible values are YES and NO. If
227 # left blank NO is used.
229 # If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate
230 # warnings for undocumented members. If EXTRACT_ALL is set to YES then
231 # this flag will automatically be disabled.
232 WARN_IF_UNDOCUMENTED = YES
233 # If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings
234 # for potential errors in the documentation, such as not documenting
235 # some parameters in a documented function, or documenting parameters
236 # that don't exist or using markup commands wrongly.
237 WARN_IF_DOC_ERROR = YES
238 # The WARN_FORMAT tag determines the format of the warning messages
239 # that doxygen can produce. The string should contain the $file,
240 # $line, and $text tags, which will be replaced by the file and line
241 # number from which the warning originated and the warning text.
242 WARN_FORMAT = "$file:$line: $text"
244 # If the EXTRACT_ALL tag is set to YES doxygen will assume all
245 # entities in documentation are documented, even if no documentation
246 # was available. Private class members and static file members will
247 # be hidden unless the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set
251 # If the EXTRACT_PRIVATE tag is set to YES all private members of a
252 # class will be included in the documentation.
253 EXTRACT_PRIVATE = AT_DOXYGEN_PRIVATE
255 # If the EXTRACT_STATIC tag is set to YES all static members of a file
256 # will be included in the documentation.
257 EXTRACT_STATIC = AT_DOXYGEN_PRIVATE
260 AT_CHECK([doxygen --version || exit 77], 0, ignore)
261 AT_CHECK([doxygen], 0, [], [ignore])
263 AT_BISON_OPTION_POPDEFS
266 m4_popdef([AT_DOXYGEN_PRIVATE])
269 AT_CHECK_DOXYGEN([Public])
270 AT_CHECK_DOXYGEN([Private])
277 # AT_TEST(NAMESPACE-DECL, [COMPILE-ERROR])
278 # ----------------------------------------
279 # See if Bison can handle %define namespace "NAMESPACE-DECL". If COMPILE-ERROR
280 # is specified, then Bison should accept the input, but compilation will fail,
281 # so don't check compilation.
282 m4_pushdef([AT_TEST],
283 [AT_BISON_OPTION_PUSHDEFS([%language "C++" %define api.namespace "$1"])
284 AT_DATA_GRAMMAR([[input.y]],
287 %define api.namespace "]$1["
289 %define global_tokens_and_yystype
293 // YYSTYPE contains a namespace reference.
294 int yylex (YYSTYPE *lval, const ]$1[::parser::location_type*) {
307 ]$1[::parser::error (const ]$1[::parser::location_type &loc,
308 const std::string &msg)
310 std::cerr << "At " << loc << ": " << msg << std::endl;
316 AT_BISON_CHECK([[-o input.cc input.y]])
319 [AT_COMPILE_CXX([[input]], [[input.cc]])
320 AT_PARSER_CHECK([[./input]])])
321 AT_BISON_OPTION_POPDEFS
324 AT_SETUP([[Relative namespace references]])
326 AT_TEST([[foo::bar]])
327 AT_TEST([[foo::bar::baz]])
330 AT_SETUP([[Absolute namespace references]])
332 AT_TEST([[::foo::bar]])
333 AT_TEST([[::foo::bar::baz]])
335 AT_TEST([[ ::foo::bar]])
336 AT_TEST([[ ::foo::bar::baz]])
339 AT_SETUP([[Syntactically invalid namespace references]])
340 AT_TEST([[:foo:bar]], [[-]])
341 AT_TEST([[foo: :bar]], [[-]])
342 # This one is interesting because `[3]' is encoded as `@<:@3@:>@', which
343 # contains single occurrences of `:'.
344 AT_TEST([[foo[3]::bar::baz]], [[-]])
345 AT_TEST([[foo::bar,baz]], [[-]])
346 AT_TEST([[foo::bar::(baz /* Pacify Emacs ) */]], [[-]])
351 ## -------------------------------------- ##
352 ## Syntax error discarding no lookahead. ##
353 ## -------------------------------------- ##
355 # After a syntax error, lalr1.cc used to not check whether there
356 # actually is a lookahead before discarding the lookahead. As a result,
357 # it mistakenly invoked the destructor for the previous lookahead.
359 AT_SETUP([[Syntax error discarding no lookahead]])
361 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
363 AT_DATA_GRAMMAR([[input.yy]],
364 [[%skeleton "lalr1.cc"
368 int yylex (yy::parser::semantic_type *);
373 %define parse.error verbose
378 std::cerr << "Discarding 'a'." << std::endl;
383 start: error-reduce consistent-error 'a' { USE ($3); };
386 'a' 'a' consistent-error 'a' { USE (($1, $2, $4)); }
387 | 'a' error { std::cerr << "Reducing 'a'." << std::endl; USE ($1); }
392 | /*empty*/ %prec 'a'
395 // Provide another context in which all rules are useful so that this
396 // test case looks a little more realistic.
397 start: 'b' consistent-error ;
402 yylex (yy::parser::semantic_type *)
404 static char const *input = "aa";
409 yy::parser::error (const std::string &m)
411 std::cerr << m << std::endl;
417 AT_BISON_CHECK([[-o input.cc input.yy]])
418 AT_COMPILE_CXX([[input]])
419 # This used to print "Discarding 'a'." again at the end.
420 AT_PARSER_CHECK([[./input]], [[1]], [[]],
426 AT_BISON_OPTION_POPDEFS
430 ## --------------------------- ##
431 ## Syntax error as exception. ##
432 ## --------------------------- ##
434 AT_SETUP([[Syntax error as exception]])
436 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
438 AT_DATA_GRAMMAR([[input.yy]],
439 [[%skeleton "lalr1.cc"
444 int yylex (yy::parser::semantic_type *);
448 %define api.value.type variant
449 %define parse.error verbose
459 error { std::cerr << "caught error" << std::endl; }
467 throw yy::parser::syntax_error ("invalid expression");
473 yylex (yy::parser::semantic_type *)
475 // 's': syntax error, 'l': lexical error.
476 static char const *input = "asal";
477 switch (int res = *input++)
480 throw yy::parser::syntax_error ("invalid character");
487 yy::parser::error (const std::string &m)
489 std::cerr << "error: " << m << std::endl;
494 AT_BISON_CHECK([[-o input.cc input.yy]])
495 AT_COMPILE_CXX([[input]])
497 AT_PARSER_CHECK([[./input]], [[0]], [[]],
498 [[error: invalid expression
500 error: invalid character
504 AT_BISON_OPTION_POPDEFS
508 ## ------------------ ##
509 ## Exception safety. ##
510 ## ------------------ ##
512 AT_SETUP([[Exception safety]])
514 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
516 AT_DATA_GRAMMAR([[input.yy]],
517 [[%skeleton "lalr1.cc"
518 %defines // FIXME: Mandated in 2.6.
525 #include <cstdlib> // size_t and getenv.
531 /// A class that counts its number of instances.
534 typedef std::list<const Object*> objects;
535 static objects instances;
541 return instances.empty();
545 log (Object const *o, const std::string& msg)
550 std::cerr << o << "->";
551 std::cerr << msg << " {";
552 const char* sep = " ";
553 for (objects::const_iterator i = instances.begin(),
554 i_end = instances.end();
558 std::cerr << sep << *i;
561 std::cerr << " }" << std::endl;
568 instances.push_back(this);
569 log (this, "Object::Object");
574 instances.remove(this);
575 log (this, "Object::~Object");
583 #include <cstring> // strchr
585 int yylex (yy::parser::semantic_type *);
586 Object::objects Object::instances;
587 static char const *input;
597 if (strchr (input, 'i'))
598 throw std::runtime_error ("initial-action");
601 %destructor { delete $$; } <obj>;
604 yyo << $$ << " '" << $$->val << '\'';
606 throw std::runtime_error ("printer");
609 %token <obj> 'a' 'E' 'e' 'p' 'R' 's' 'T'
610 %type <obj> list item
614 start: list { delete $1; };
618 | item list { $$ = $1; delete $2; } // Right recursion to load the stack.
623 | 'e' { YYUSE ($$); YYUSE($1); error ("syntax error"); }
624 // Not just 'E', otherwise we reduce when 'E' is the lookahead, and
625 // then the stack is emptied, defeating the point of the test.
626 | 'E' 'a' { YYUSE($1); $$ = $2; }
627 | 'R' { $$ = YY_NULL; delete $1; YYERROR; }
629 | 's' { $$ = $1; throw std::runtime_error ("reduction"); }
630 | 'T' { $$ = YY_NULL; delete $1; YYABORT; }
631 | error { $$ = YY_NULL; yyerrok; }
636 yylex (yy::parser::semantic_type *lvalp)
639 // 'e': user action calls error.
640 // 'E': syntax error, with yyerror that throws.
641 // 'i': initial action throws.
642 // 'l': yylex throws.
643 // 'R': call YYERROR in the action
644 // 's': reduction throws.
645 // 'T': call YYABORT in the action
646 switch (int res = *input++)
649 throw std::runtime_error ("yylex");
651 lvalp->obj = new Object (res);
658 /* A C++ error reporting function. */
660 yy::parser::error (const std::string& m)
662 throw std::runtime_error (m);
666 main (int argc, const char *argv[])
674 assert (std::string(argv[1]) == "--debug");
683 debug |= !!getenv ("YYDEBUG");
684 parser.set_debug_level (debug);
688 res = parser.parse ();
690 catch (const std::exception& e)
692 std::cerr << "exception caught: " << e.what () << std::endl;
696 std::cerr << "unknown exception caught" << std::endl;
698 Object::log (YY_NULL, "end");
699 assert (Object::empty());
703 AT_BISON_CHECK([[-o input.cc --report=all input.yy]])
704 AT_COMPILE_CXX([[input]])
706 AT_PARSER_CHECK([[./input aaaas]], [[2]], [[]],
707 [[exception caught: reduction
710 AT_PARSER_CHECK([[./input aaaal]], [[2]], [[]],
711 [[exception caught: yylex
714 AT_PARSER_CHECK([[./input i]], [[2]], [[]],
715 [[exception caught: initial-action
718 AT_PARSER_CHECK([[./input aaaap]])
720 AT_PARSER_CHECK([[./input --debug aaaap]], [[2]], [[]], [[stderr]])
721 AT_CHECK([[grep '^exception caught: printer$' stderr]], [], [ignore])
723 AT_PARSER_CHECK([[./input aaaae]], [[2]], [[]],
724 [[exception caught: syntax error
727 AT_PARSER_CHECK([[./input aaaaE]], [[2]], [[]],
728 [[exception caught: syntax error, unexpected $end, expecting 'a'
731 AT_PARSER_CHECK([[./input aaaaT]], [[1]])
733 # There is error-recovery, so exit success.
734 AT_PARSER_CHECK([[./input aaaaR]], [[0]])
736 AT_BISON_OPTION_POPDEFS
740 ## ------------------------------------ ##
741 ## C++ GLR parser identifier shadowing ##
742 ## ------------------------------------ ##
744 AT_SETUP([[C++ GLR parser identifier shadowing]])
746 AT_DATA_GRAMMAR([input.yy], [
758 int yylex (yy::parser::semantic_type *yylval);
766 int yylex (yy::parser::semantic_type *yylval)
768 // Note: this argument is unused, but named on purpose. There used to be a
769 // bug with a macro that erroneously expanded this identifier to
772 return yy::parser::token::ZERO;
775 void yy::parser::error (std::string const&)
782 AT_BISON_CHECK([[-o input.cc input.yy]])
783 AT_COMPILE_CXX([[input]])