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_CHECK_NAMESPACE(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_define([AT_CHECK_NAMESPACE],
285 AT_DATA_GRAMMAR([[input.y]],
288 %define api.namespace "]$1["
290 %define global_tokens_and_yystype
294 // YYSTYPE contains a namespace reference.
295 int yylex (YYSTYPE *lval, const ]$1[::parser::location_type*) {
308 ]$1[::parser::error (const ]$1[::parser::location_type &loc,
309 const std::string &msg)
311 std::cerr << "At " << loc << ": " << msg << std::endl;
322 AT_BISON_CHECK([[-o input.cc input.y]])
325 [AT_COMPILE_CXX([[input]], [[input.cc]])
326 AT_PARSER_CHECK([[./input]])])
330 AT_SETUP([[Relative namespace references]])
331 AT_CHECK_NAMESPACE([[foo]])
332 AT_CHECK_NAMESPACE([[foo::bar]])
333 AT_CHECK_NAMESPACE([[foo::bar::baz]])
336 AT_SETUP([[Absolute namespace references]])
337 AT_CHECK_NAMESPACE([[::foo]])
338 AT_CHECK_NAMESPACE([[::foo::bar]])
339 AT_CHECK_NAMESPACE([[::foo::bar::baz]])
340 AT_CHECK_NAMESPACE([[ ::foo]])
341 AT_CHECK_NAMESPACE([[ ::foo::bar]])
342 AT_CHECK_NAMESPACE([[ ::foo::bar::baz]])
345 AT_SETUP([[Syntactically invalid namespace references]])
346 AT_CHECK_NAMESPACE([[:foo:bar]], [[-]])
347 AT_CHECK_NAMESPACE([[foo: :bar]], [[-]])
348 # This one is interesting because `[3]' is encoded as `@<:@3@:>@', which
349 # contains single occurrences of `:'.
350 AT_CHECK_NAMESPACE([[foo[3]::bar::baz]], [[-]])
351 AT_CHECK_NAMESPACE([[foo::bar,baz]], [[-]])
352 AT_CHECK_NAMESPACE([[foo::bar::(baz /* Pacify Emacs ) */]], [[-]])
356 ## -------------------------------------- ##
357 ## Syntax error discarding no lookahead. ##
358 ## -------------------------------------- ##
360 # After a syntax error, lalr1.cc used to not check whether there
361 # actually is a lookahead before discarding the lookahead. As a result,
362 # it mistakenly invoked the destructor for the previous lookahead.
364 AT_SETUP([[Syntax error discarding no lookahead]])
366 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
368 AT_DATA_GRAMMAR([[input.yy]],
369 [[%skeleton "lalr1.cc"
373 int yylex (yy::parser::semantic_type *);
378 %define parse.error verbose
383 std::cerr << "Discarding 'a'." << std::endl;
388 start: error-reduce consistent-error 'a' { USE ($3); };
391 'a' 'a' consistent-error 'a' { USE (($1, $2, $4)); }
392 | 'a' error { std::cerr << "Reducing 'a'." << std::endl; USE ($1); }
397 | /*empty*/ %prec 'a'
400 // Provide another context in which all rules are useful so that this
401 // test case looks a little more realistic.
402 start: 'b' consistent-error ;
407 yylex (yy::parser::semantic_type *)
409 static char const *input = "aa";
414 yy::parser::error (const std::string &m)
416 std::cerr << m << std::endl;
422 AT_BISON_CHECK([[-o input.cc input.yy]])
423 AT_COMPILE_CXX([[input]])
424 # This used to print "Discarding 'a'." again at the end.
425 AT_PARSER_CHECK([[./input]], [[1]], [[]],
431 AT_BISON_OPTION_POPDEFS
435 ## --------------------------- ##
436 ## Syntax error as exception. ##
437 ## --------------------------- ##
439 AT_SETUP([[Syntax error as exception]])
441 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
443 AT_DATA_GRAMMAR([[input.yy]],
444 [[%skeleton "lalr1.cc"
449 int yylex (yy::parser::semantic_type *);
453 %define api.value.type variant
454 %define parse.error verbose
464 error { std::cerr << "caught error" << std::endl; }
472 throw yy::parser::syntax_error ("invalid expression");
478 yylex (yy::parser::semantic_type *)
480 // 's': syntax error, 'l': lexical error.
481 static char const *input = "asal";
482 switch (int res = *input++)
485 throw yy::parser::syntax_error ("invalid character");
492 yy::parser::error (const std::string &m)
494 std::cerr << "error: " << m << std::endl;
499 AT_BISON_CHECK([[-o input.cc input.yy]])
500 AT_COMPILE_CXX([[input]])
502 AT_PARSER_CHECK([[./input]], [[0]], [[]],
503 [[error: invalid expression
505 error: invalid character
509 AT_BISON_OPTION_POPDEFS
513 ## ------------------ ##
514 ## Exception safety. ##
515 ## ------------------ ##
517 AT_SETUP([[Exception safety]])
519 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
521 AT_DATA_GRAMMAR([[input.yy]],
522 [[%skeleton "lalr1.cc"
523 %defines // FIXME: Mandated in 2.6.
530 #include <cstdlib> // size_t and getenv.
536 /// A class that counts its number of instances.
539 typedef std::list<const Object*> objects;
540 static objects instances;
546 return instances.empty();
550 log (Object const *o, const std::string& msg)
555 std::cerr << o << "->";
556 std::cerr << msg << " {";
557 const char* sep = " ";
558 for (objects::const_iterator i = instances.begin(),
559 i_end = instances.end();
563 std::cerr << sep << *i;
566 std::cerr << " }" << std::endl;
573 instances.push_back(this);
574 log (this, "Object::Object");
579 instances.remove(this);
580 log (this, "Object::~Object");
588 #include <cstring> // strchr
590 int yylex (yy::parser::semantic_type *);
591 Object::objects Object::instances;
592 static char const *input;
602 if (strchr (input, 'i'))
603 throw std::runtime_error ("initial-action");
606 %destructor { delete $$; } <obj>;
609 yyo << $$ << " '" << $$->val << '\'';
611 throw std::runtime_error ("printer");
614 %token <obj> 'a' 'E' 'e' 'p' 'R' 's' 'T'
615 %type <obj> list item
619 start: list { delete $1; };
623 | item list { $$ = $1; delete $2; } // Right recursion to load the stack.
628 | 'e' { YYUSE ($$); YYUSE($1); error ("syntax error"); }
629 // Not just 'E', otherwise we reduce when 'E' is the lookahead, and
630 // then the stack is emptied, defeating the point of the test.
631 | 'E' 'a' { YYUSE($1); $$ = $2; }
632 | 'R' { $$ = YY_NULL; delete $1; YYERROR; }
634 | 's' { $$ = $1; throw std::runtime_error ("reduction"); }
635 | 'T' { $$ = YY_NULL; delete $1; YYABORT; }
636 | error { $$ = YY_NULL; yyerrok; }
641 yylex (yy::parser::semantic_type *lvalp)
644 // 'e': user action calls error.
645 // 'E': syntax error, with yyerror that throws.
646 // 'i': initial action throws.
647 // 'l': yylex throws.
648 // 'R': call YYERROR in the action
649 // 's': reduction throws.
650 // 'T': call YYABORT in the action
651 switch (int res = *input++)
654 throw std::runtime_error ("yylex");
656 lvalp->obj = new Object (res);
663 /* A C++ error reporting function. */
665 yy::parser::error (const std::string& m)
667 throw std::runtime_error (m);
671 main (int argc, const char *argv[])
679 assert (std::string(argv[1]) == "--debug");
688 debug |= !!getenv ("YYDEBUG");
689 parser.set_debug_level (debug);
693 res = parser.parse ();
695 catch (const std::exception& e)
697 std::cerr << "exception caught: " << e.what () << std::endl;
701 std::cerr << "unknown exception caught" << std::endl;
703 Object::log (YY_NULL, "end");
704 assert (Object::empty());
708 AT_BISON_CHECK([[-o input.cc --report=all input.yy]])
709 AT_COMPILE_CXX([[input]])
711 AT_PARSER_CHECK([[./input aaaas]], [[2]], [[]],
712 [[exception caught: reduction
715 AT_PARSER_CHECK([[./input aaaal]], [[2]], [[]],
716 [[exception caught: yylex
719 AT_PARSER_CHECK([[./input i]], [[2]], [[]],
720 [[exception caught: initial-action
723 AT_PARSER_CHECK([[./input aaaap]])
725 AT_PARSER_CHECK([[./input --debug aaaap]], [[2]], [[]], [[stderr]])
726 AT_CHECK([[grep '^exception caught: printer$' stderr]], [], [ignore])
728 AT_PARSER_CHECK([[./input aaaae]], [[2]], [[]],
729 [[exception caught: syntax error
732 AT_PARSER_CHECK([[./input aaaaE]], [[2]], [[]],
733 [[exception caught: syntax error, unexpected $end, expecting 'a'
736 AT_PARSER_CHECK([[./input aaaaT]], [[1]])
738 # There is error-recovery, so exit success.
739 AT_PARSER_CHECK([[./input aaaaR]], [[0]])
741 AT_BISON_OPTION_POPDEFS
745 ## ------------------------------------ ##
746 ## C++ GLR parser identifier shadowing ##
747 ## ------------------------------------ ##
749 AT_SETUP([[C++ GLR parser identifier shadowing]])
751 AT_DATA_GRAMMAR([input.yy], [
763 int yylex (yy::parser::semantic_type *yylval);
771 int yylex (yy::parser::semantic_type *yylval)
773 // Note: this argument is unused, but named on purpose. There used to be a
774 // bug with a macro that erroneously expanded this identifier to
777 return yy::parser::token::ZERO;
780 void yy::parser::error (std::string const&)
787 AT_BISON_CHECK([[-o input.cc input.yy]])
788 AT_COMPILE_CXX([[input]])