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_CHECK_VARIANTS([DIRECTIVES])
26 # -------------------------------
27 # Check the support of variants in C++, with the additional DIRECTIVES.
28 m4_define([AT_CHECK_VARIANTS],
29 [AT_SETUP([Variants $1])
31 # Store strings and integers in a list of strings.
32 AT_DATA_GRAMMAR([list.yy],
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
54 #if defined USE_LEX_SYMBOL
55 yy::parser::symbol_type yylex ();
57 yy::parser::token_type yylex (yy::parser::semantic_type* yylval,
58 yy::parser::location_type* yylloc);
61 // Printing a list of strings (for %printer).
62 // Koening look up will look into std, since that's an std::list.
66 operator<<(std::ostream& o, const strings_type& s)
69 for (strings_type::const_iterator i = s.begin (); i != s.end (); ++i)
79 // Conversion to string.
83 string_cast (const T& t)
91 %token <::std::string> TEXT;
95 %type <::std::string> item;
96 // Using the template type to exercize its parsing.
97 // Starting with :: to ensure we don't output "<::" which starts by the
98 // digraph for the left square bracket.
99 %type <::std::list<std::string>> list result;
101 %printer { yyo << $][$; }
102 <int> <::std::string> <::std::list<std::string>>;
106 list { std::cout << $][1 << std::endl; }
110 /* nothing */ { /* Generates an empty string list */ }
111 | list item { std::swap ($][$,$][1); $$.push_back ($][2); }
112 | list error { std::swap ($][$,$][1); }
116 TEXT { std::swap ($][$,$][1); }
117 | NUMBER { if ($][1 == 3) YYERROR; else $][$ = string_cast ($][1); }
123 #if defined USE_LEX_SYMBOL
124 yy::parser::symbol_type yylex ()
126 yy::parser::token_type yylex (yy::parser::semantic_type* yylval,
127 yy::parser::location_type* yylloc)
130 #ifndef USE_LEX_SYMBOL
131 typedef yy::parser::token token;
133 typedef yy::parser::location_type location_type;
134 static int stage = -1;
136 if (stage == STAGE_MAX)
138 #if defined USE_LEX_SYMBOL
139 return yy::parser::make_END_OF_FILE (location_type ());
141 *yylloc = location_type ();
142 return token::END_OF_FILE;
147 #if defined USE_LEX_SYMBOL
148 return yy::parser::make_NUMBER (stage, location_type ());
150 # if defined ONE_STAGE_BUILD
151 yylval->build (stage);
153 yylval->build<int>() = stage;
155 *yylloc = location_type ();
156 return token::NUMBER;
161 #if defined USE_LEX_SYMBOL
162 return yy::parser::make_TEXT (string_cast (stage), location_type ());
164 # if defined ONE_STAGE_BUILD
165 yylval->build (string_cast (stage));
167 yylval->build<std::string>() = string_cast (stage);
169 *yylloc = location_type ();
177 yy::parser::error (const yy::parser::location_type&,
178 const std::string& message)
180 std::cerr << message << std::endl;
187 p.set_debug_level (!!getenv ("YYDEBUG"));
192 AT_BISON_CHECK([-o list.cc list.yy])
193 AT_COMPILE_CXX([list])
194 AT_CHECK([./list], 0,
201 AT_CHECK_VARIANTS([])
202 AT_CHECK_VARIANTS([%define parse.assert])
203 AT_CHECK_VARIANTS([[%define parse.assert %code {\n#define ONE_STAGE_BUILD\n}]])
204 AT_CHECK_VARIANTS([[%define parse.assert %define lex_symbol %code {\n#define USE_LEX_SYMBOL\n}]])
205 AT_CHECK_VARIANTS([[%define parse.assert %define lex_symbol %code {\n#define USE_LEX_SYMBOL\n} %define api.tokens.prefix "TOK_"]])
208 ## ----------------------- ##
209 ## Doxygen Documentation. ##
210 ## ----------------------- ##
212 m4_define([AT_CHECK_DOXYGEN],
214 [Public], [m4_pushdef([AT_DOXYGEN_PRIVATE], [NO])],
215 [Private], [m4_pushdef([AT_DOXYGEN_PRIVATE], [YES])],
216 [m4_fatal([invalid argument: $1])])
217 AT_SETUP([Doxygen $1 Documentation])
219 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
221 [[%skeleton "lalr1.cc"
231 AT_BISON_CHECK([-o input.cc input.yy], 0)
234 [# The PROJECT_NAME tag is a single word (or a sequence of words
235 # surrounded by quotes) that should identify the project.
236 PROJECT_NAME = "Bison C++ Parser"
238 # The QUIET tag can be used to turn on/off the messages that are
239 # generated by doxygen. Possible values are YES and NO. If left blank
243 # The WARNINGS tag can be used to turn on/off the warning messages
244 # that are generated by doxygen. Possible values are YES and NO. If
245 # left blank NO is used.
247 # If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate
248 # warnings for undocumented members. If EXTRACT_ALL is set to YES then
249 # this flag will automatically be disabled.
250 WARN_IF_UNDOCUMENTED = YES
251 # If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings
252 # for potential errors in the documentation, such as not documenting
253 # some parameters in a documented function, or documenting parameters
254 # that don't exist or using markup commands wrongly.
255 WARN_IF_DOC_ERROR = YES
256 # The WARN_FORMAT tag determines the format of the warning messages
257 # that doxygen can produce. The string should contain the $file,
258 # $line, and $text tags, which will be replaced by the file and line
259 # number from which the warning originated and the warning text.
260 WARN_FORMAT = "$file:$line: $text"
262 # If the EXTRACT_ALL tag is set to YES doxygen will assume all
263 # entities in documentation are documented, even if no documentation
264 # was available. Private class members and static file members will
265 # be hidden unless the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set
269 # If the EXTRACT_PRIVATE tag is set to YES all private members of a
270 # class will be included in the documentation.
271 EXTRACT_PRIVATE = AT_DOXYGEN_PRIVATE
273 # If the EXTRACT_STATIC tag is set to YES all static members of a file
274 # will be included in the documentation.
275 EXTRACT_STATIC = AT_DOXYGEN_PRIVATE
278 AT_CHECK([doxygen --version || exit 77], 0, ignore)
279 AT_CHECK([doxygen], 0, [], [ignore])
281 AT_BISON_OPTION_POPDEFS
284 m4_popdef([AT_DOXYGEN_PRIVATE])
287 AT_CHECK_DOXYGEN([Public])
288 AT_CHECK_DOXYGEN([Private])
295 # AT_CHECK_NAMESPACE(NAMESPACE-DECL, [COMPILE-ERROR])
296 # ---------------------------------------------------
297 # See if Bison can handle %define namespace "NAMESPACE-DECL". If COMPILE-ERROR
298 # is specified, then Bison should accept the input, but compilation will fail,
299 # so don't check compilation.
300 m4_define([AT_CHECK_NAMESPACE],
303 AT_DATA_GRAMMAR([[input.y]],
306 %define api.namespace "]$1["
308 %define global_tokens_and_yystype
312 // YYSTYPE contains a namespace reference.
313 int yylex (YYSTYPE *lval, const ]$1[::parser::location_type*) {
326 ]$1[::parser::error (const ]$1[::parser::location_type &loc,
327 const std::string &msg)
329 std::cerr << "At " << loc << ": " << msg << std::endl;
340 AT_BISON_CHECK([[-o input.cc input.y]])
343 [AT_COMPILE_CXX([[input]], [[input.cc]])
344 AT_PARSER_CHECK([[./input]])])
348 AT_SETUP([[Relative namespace references]])
349 AT_CHECK_NAMESPACE([[foo]])
350 AT_CHECK_NAMESPACE([[foo::bar]])
351 AT_CHECK_NAMESPACE([[foo::bar::baz]])
354 AT_SETUP([[Absolute namespace references]])
355 AT_CHECK_NAMESPACE([[::foo]])
356 AT_CHECK_NAMESPACE([[::foo::bar]])
357 AT_CHECK_NAMESPACE([[::foo::bar::baz]])
358 AT_CHECK_NAMESPACE([[ ::foo]])
359 AT_CHECK_NAMESPACE([[ ::foo::bar]])
360 AT_CHECK_NAMESPACE([[ ::foo::bar::baz]])
363 AT_SETUP([[Syntactically invalid namespace references]])
364 AT_CHECK_NAMESPACE([[:foo:bar]], [[-]])
365 AT_CHECK_NAMESPACE([[foo: :bar]], [[-]])
366 # This one is interesting because `[3]' is encoded as `@<:@3@:>@', which
367 # contains single occurrences of `:'.
368 AT_CHECK_NAMESPACE([[foo[3]::bar::baz]], [[-]])
369 AT_CHECK_NAMESPACE([[foo::bar,baz]], [[-]])
370 AT_CHECK_NAMESPACE([[foo::bar::(baz /* Pacify Emacs ) */]], [[-]])
374 ## -------------------------------------- ##
375 ## Syntax error discarding no lookahead. ##
376 ## -------------------------------------- ##
378 # After a syntax error, lalr1.cc used to not check whether there
379 # actually is a lookahead before discarding the lookahead. As a result,
380 # it mistakenly invoked the destructor for the previous lookahead.
382 AT_SETUP([[Syntax error discarding no lookahead]])
384 AT_DATA_GRAMMAR([[input.yy]],
385 [[%skeleton "lalr1.cc"
389 int yylex (yy::parser::semantic_type *);
394 %define parse.error verbose
399 std::cerr << "Discarding 'a'." << std::endl;
404 start: error-reduce consistent-error 'a' { USE ($3); };
407 'a' 'a' consistent-error 'a' { USE (($1, $2, $4)); }
408 | 'a' error { std::cerr << "Reducing 'a'." << std::endl; USE ($1); }
413 | /*empty*/ %prec 'a'
416 // Provide another context in which all rules are useful so that this
417 // test case looks a little more realistic.
418 start: 'b' consistent-error ;
423 yylex (yy::parser::semantic_type *)
425 static char const *input = "aa";
430 yy::parser::error (const std::string &m)
432 std::cerr << m << std::endl;
439 return parser.parse ();
442 AT_BISON_CHECK([[-o input.cc input.yy]])
443 AT_COMPILE_CXX([[input]])
444 # This used to print "Discarding 'a'." again at the end.
445 AT_PARSER_CHECK([[./input]], [[1]], [[]],
454 ## --------------------------- ##
455 ## Syntax error as exception. ##
456 ## --------------------------- ##
458 AT_SETUP([[Syntax error as exception]])
460 AT_DATA_GRAMMAR([[input.yy]],
461 [[%skeleton "lalr1.cc"
466 int yylex (yy::parser::semantic_type *);
471 %define parse.error verbose
481 error { std::cerr << "caught error" << std::endl; }
489 throw yy::parser::syntax_error ("invalid expression");
495 yylex (yy::parser::semantic_type *)
497 // 's': syntax error, 'l': lexical error.
498 static char const *input = "asal";
499 switch (int res = *input++)
502 throw yy::parser::syntax_error ("invalid character");
509 yy::parser::error (const std::string &m)
511 std::cerr << "error: " << m << std::endl;
518 parser.set_debug_level (!!getenv ("YYDEBUG"));
519 return parser.parse ();
522 AT_BISON_CHECK([[-o input.cc input.yy]])
523 AT_COMPILE_CXX([[input]])
525 AT_PARSER_CHECK([[./input]], [[0]], [[]],
526 [[error: invalid expression
528 error: invalid character
535 ## ------------------ ##
536 ## Exception safety. ##
537 ## ------------------ ##
539 AT_SETUP([[Exception safety]])
541 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
543 AT_DATA_GRAMMAR([[input.yy]],
544 [[%skeleton "lalr1.cc"
545 %defines // FIXME: Mandated in 2.6.
552 #include <cstdlib> // size_t and getenv.
558 /// A class that counts its number of instances.
561 typedef std::list<const Object*> objects;
562 static objects instances;
568 return instances.empty();
572 log (Object const *o, const std::string& msg)
577 std::cerr << o << "->";
578 std::cerr << msg << " {";
579 const char* sep = " ";
580 for (objects::const_iterator i = instances.begin(),
581 i_end = instances.end();
585 std::cerr << sep << *i;
588 std::cerr << " }" << std::endl;
595 instances.push_back(this);
596 log (this, "Object::Object");
601 instances.remove(this);
602 log (this, "Object::~Object");
610 #include <cstring> // strchr
612 int yylex (yy::parser::semantic_type *);
613 Object::objects Object::instances;
614 static char const *input;
624 if (strchr (input, 'i'))
625 throw std::runtime_error ("initial-action");
628 %destructor { delete $$; } <obj>;
631 yyo << $$ << " '" << $$->val << '\'';
633 throw std::runtime_error ("printer");
636 %token <obj> 'a' 'E' 'e' 'p' 'R' 's' 'T'
637 %type <obj> list item
641 start: list { delete $1; };
645 | item list { $$ = $1; delete $2; } // Right recursion to load the stack.
650 | 'e' { YYUSE ($$); YYUSE($1); error ("syntax error"); }
651 // Not just 'E', otherwise we reduce when 'E' is the lookahead, and
652 // then the stack is emptied, defeating the point of the test.
653 | 'E' 'a' { YYUSE($1); $$ = $2; }
654 | 'R' { $$ = YY_NULL; delete $1; YYERROR; }
656 | 's' { $$ = $1; throw std::runtime_error ("reduction"); }
657 | 'T' { $$ = YY_NULL; delete $1; YYABORT; }
658 | error { $$ = YY_NULL; yyerrok; }
663 yylex (yy::parser::semantic_type *lvalp)
666 // 'e': user action calls error.
667 // 'E': syntax error, with yyerror that throws.
668 // 'i': initial action throws.
669 // 'l': yylex throws.
670 // 'R': call YYERROR in the action
671 // 's': reduction throws.
672 // 'T': call YYABORT in the action
673 switch (int res = *input++)
676 throw std::runtime_error ("yylex");
678 lvalp->obj = new Object (res);
685 /* A C++ error reporting function. */
687 yy::parser::error (const std::string& m)
689 throw std::runtime_error (m);
693 main (int argc, const char *argv[])
701 assert (!strcmp (argv[1], "--debug"));
710 debug |= !!getenv ("YYDEBUG");
711 parser.set_debug_level (debug);
715 res = parser.parse ();
717 catch (const std::exception& e)
719 std::cerr << "exception caught: " << e.what () << std::endl;
723 std::cerr << "unknown exception caught" << std::endl;
725 Object::log (YY_NULL, "end");
726 assert (Object::empty());
730 AT_BISON_CHECK([[-o input.cc --report=all input.yy]])
731 AT_COMPILE_CXX([[input]])
733 AT_PARSER_CHECK([[./input aaaas]], [[2]], [[]],
734 [[exception caught: reduction
737 AT_PARSER_CHECK([[./input aaaal]], [[2]], [[]],
738 [[exception caught: yylex
741 AT_PARSER_CHECK([[./input i]], [[2]], [[]],
742 [[exception caught: initial-action
745 AT_PARSER_CHECK([[./input aaaap]])
747 AT_PARSER_CHECK([[./input --debug aaaap]], [[2]], [[]], [[stderr]])
748 AT_PARSER_CHECK([[grep '^exception caught: printer$' stderr]], [], [ignore])
750 AT_PARSER_CHECK([[./input aaaae]], [[2]], [[]],
751 [[exception caught: syntax error
754 AT_PARSER_CHECK([[./input aaaaE]], [[2]], [[]],
755 [[exception caught: syntax error, unexpected $end, expecting 'a'
758 AT_PARSER_CHECK([[./input aaaaT]], [[1]])
760 # There is error-recovery, so exit success.
761 AT_PARSER_CHECK([[./input aaaaR]], [[0]])
763 AT_BISON_OPTION_POPDEFS