]> git.saurik.com Git - bison.git/blob - tests/c++.at
304a72d92700484e966737134651ada1e09bb9e3
[bison.git] / tests / c++.at
1 # Checking the C++ Features. -*- Autotest -*-
2
3 # Copyright (C) 2004-2005, 2007, 2009-2012 Free Software Foundation,
4 # Inc.
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 AT_BANNER([[C++ Features.]])
20
21
22 ## ----------------------- ##
23 ## Doxygen Documentation. ##
24 ## ----------------------- ##
25
26 m4_define([AT_CHECK_DOXYGEN],
27 [m4_case([$1],
28 [Public], [m4_pushdef([AT_DOXYGEN_PRIVATE], [NO])],
29 [Private], [m4_pushdef([AT_DOXYGEN_PRIVATE], [YES])],
30 [m4_fatal([invalid argument: $1])])
31 AT_SETUP([Doxygen $1 Documentation])
32
33 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
34 AT_DATA([input.yy],
35 [[%skeleton "lalr1.cc"
36 %locations
37 %debug
38 %defines
39 %%
40 exp:;
41 %%
42 ]AT_YYERROR_DEFINE[
43 ]])
44
45 AT_BISON_CHECK([-o input.cc input.yy], 0)
46
47 AT_DATA([Doxyfile],
48 [# The PROJECT_NAME tag is a single word (or a sequence of words
49 # surrounded by quotes) that should identify the project.
50 PROJECT_NAME = "Bison C++ Parser"
51
52 # The QUIET tag can be used to turn on/off the messages that are
53 # generated by doxygen. Possible values are YES and NO. If left blank
54 # NO is used.
55 QUIET = YES
56
57 # The WARNINGS tag can be used to turn on/off the warning messages
58 # that are generated by doxygen. Possible values are YES and NO. If
59 # left blank NO is used.
60 WARNINGS = YES
61 # If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate
62 # warnings for undocumented members. If EXTRACT_ALL is set to YES then
63 # this flag will automatically be disabled.
64 WARN_IF_UNDOCUMENTED = YES
65 # If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings
66 # for potential errors in the documentation, such as not documenting
67 # some parameters in a documented function, or documenting parameters
68 # that don't exist or using markup commands wrongly.
69 WARN_IF_DOC_ERROR = YES
70 # The WARN_FORMAT tag determines the format of the warning messages
71 # that doxygen can produce. The string should contain the $file,
72 # $line, and $text tags, which will be replaced by the file and line
73 # number from which the warning originated and the warning text.
74 WARN_FORMAT = "$file:$line: $text"
75
76 # If the EXTRACT_ALL tag is set to YES doxygen will assume all
77 # entities in documentation are documented, even if no documentation
78 # was available. Private class members and static file members will
79 # be hidden unless the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set
80 # to YES
81 EXTRACT_ALL = YES
82
83 # If the EXTRACT_PRIVATE tag is set to YES all private members of a
84 # class will be included in the documentation.
85 EXTRACT_PRIVATE = AT_DOXYGEN_PRIVATE
86
87 # If the EXTRACT_STATIC tag is set to YES all static members of a file
88 # will be included in the documentation.
89 EXTRACT_STATIC = AT_DOXYGEN_PRIVATE
90 ])
91
92 AT_CHECK([doxygen --version || exit 77], 0, ignore)
93 AT_CHECK([doxygen], 0, [], [ignore])
94
95 AT_BISON_OPTION_POPDEFS
96 AT_CLEANUP
97
98 m4_popdef([AT_DOXYGEN_PRIVATE])
99 ])# AT_CHECK_DOXYGEN
100
101 AT_CHECK_DOXYGEN([Public])
102 AT_CHECK_DOXYGEN([Private])
103
104 ## ------------ ##
105 ## Namespaces. ##
106 ## ------------ ##
107
108 # AT_CHECK_NAMESPACE(NAMESPACE-DECL, [COMPILE-ERROR])
109 # ---------------------------------------------------
110 # See if Bison can handle %define namespace "NAMESPACE-DECL". If COMPILE-ERROR
111 # is specified, then Bison should accept the input, but compilation will fail,
112 # so don't check compilation.
113 m4_define([AT_CHECK_NAMESPACE],
114 [
115
116 AT_DATA_GRAMMAR([[input.y]],
117 [[%language "C++"
118 %defines
119 %define namespace "]$1["
120 %union { int i; }
121 %define global_tokens_and_yystype
122
123 %code {
124 // YYSTYPE contains a namespace reference.
125 int yylex (YYSTYPE *lval) {
126 lval->i = 3;
127 return 0;
128 }
129 }
130
131 %%
132
133 start: ;
134
135 %%
136
137 void
138 ]$1[::parser::error (const ]$1[::parser::location_type &loc,
139 const std::string &msg)
140 {
141 std::cerr << "At " << loc << ": " << msg << std::endl;
142 }
143
144 int
145 main (void)
146 {
147 ]$1[::parser p;
148 return p.parse ();
149 }
150 ]])
151
152 AT_BISON_CHECK([[-o input.cc input.y]])
153
154 m4_if([$#], [1],
155 [AT_COMPILE_CXX([[input]], [[input.cc]])
156 AT_PARSER_CHECK([[./input]])])
157
158 ])
159
160 AT_SETUP([[Relative namespace references]])
161 AT_CHECK_NAMESPACE([[foo]])
162 AT_CHECK_NAMESPACE([[foo::bar]])
163 AT_CHECK_NAMESPACE([[foo::bar::baz]])
164 AT_CLEANUP
165
166 AT_SETUP([[Absolute namespace references]])
167 AT_CHECK_NAMESPACE([[::foo]])
168 AT_CHECK_NAMESPACE([[::foo::bar]])
169 AT_CHECK_NAMESPACE([[::foo::bar::baz]])
170 AT_CHECK_NAMESPACE([[ ::foo]])
171 AT_CHECK_NAMESPACE([[ ::foo::bar]])
172 AT_CHECK_NAMESPACE([[ ::foo::bar::baz]])
173 AT_CLEANUP
174
175 AT_SETUP([[Syntactically invalid namespace references]])
176 AT_CHECK_NAMESPACE([[:foo:bar]], [[-]])
177 AT_CHECK_NAMESPACE([[foo: :bar]], [[-]])
178 # This one is interesting because `[3]' is encoded as `@<:@3@:>@', which
179 # contains single occurrences of `:'.
180 AT_CHECK_NAMESPACE([[foo[3]::bar::baz]], [[-]])
181 AT_CHECK_NAMESPACE([[foo::bar,baz]], [[-]])
182 AT_CHECK_NAMESPACE([[foo::bar::(baz]], [[-]])
183 AT_CLEANUP
184
185
186 ## ------------------ ##
187 ## Exception safety. ##
188 ## ------------------ ##
189
190 AT_SETUP([[Exception safety]])
191
192 AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
193
194 AT_DATA_GRAMMAR([[input.yy]],
195 [[%skeleton "lalr1.cc"
196 %defines // FIXME: Mandated in 2.6.
197 %debug
198
199 %code requires
200 {
201 #include <cstdlib> // size_t and getenv.
202 #include <iostream>
203
204 int debug = 0;
205
206 struct Object
207 {
208 static size_t counter;
209
210 Object ()
211 {
212 ++counter;
213 if (debug)
214 std::cerr << "Object::Object() => counter == " << counter << std::endl;
215 }
216
217 ~Object ()
218 {
219 --counter;
220 if (debug)
221 std::cerr << "Object::~Object() => counter == " << counter << std::endl;
222 }
223 };
224 }
225
226 %code
227 {
228 #include <cassert>
229 #include <stdexcept>
230 int yylex (yy::parser::semantic_type *);
231 size_t Object::counter = 0;
232 static char const *input;
233 }
234
235 %union
236 {
237 Object* obj;
238 }
239
240 %destructor { delete $$; } <obj>;
241 %printer { yyo << "counter == " << $$->counter; } <obj>;
242
243 %token <obj> 'a' 's'
244 %type <obj> list item
245
246 %%
247
248 start: list { delete $1; };
249
250 list:
251 item { $$ = $1; }
252 | item list { $$ = $1; delete $2; } /* Right recursion to load the stack. */
253 ;
254
255 item:
256 'a'
257 {
258 std::swap ($$, $1);
259 }
260 | 's'
261 {
262 std::swap ($$, $1);
263 throw std::runtime_error ("invalid expression");
264 }
265
266 %%
267
268 int
269 yylex (yy::parser::semantic_type *lvalp)
270 {
271 // 'l': lexical exception, 's': syntactic exception.
272 switch (int res = *input++)
273 {
274 case 'l':
275 throw std::runtime_error ("invalid character");
276 default:
277 lvalp->obj = new Object;
278 // Fall through.
279 case 0:
280 return res;
281 }
282 }
283
284 ]AT_YYERROR_DEFINE[
285
286 int
287 main (int argc, const char *argv[])
288 {
289 assert (argc == 2);
290 input = argv[1];
291 yy::parser parser;
292 debug = !!getenv ("YYDEBUG");
293 parser.set_debug_level (debug);
294 int res = 2;
295 try
296 {
297 res = parser.parse ();
298 }
299 catch (const std::exception& e)
300 {
301 std::cerr << "exception caught: " << e.what () << std::endl;
302 }
303 catch (...)
304 {
305 std::cerr << "unknown exception caught" << std::endl;
306 }
307 assert (Object::counter == 0);
308 return res;
309 }
310 ]])
311 AT_BISON_CHECK([[-o input.cc input.yy]])
312 AT_COMPILE_CXX([[input]])
313
314 AT_PARSER_CHECK([[./input aaaas]], [[2]], [[]],
315 [[exception caught: invalid expression
316 ]])
317
318 AT_PARSER_CHECK([[./input aaaal]], [[2]], [[]],
319 [[exception caught: invalid character
320 ]])
321
322 AT_BISON_OPTION_POPDEFS
323
324 AT_CLEANUP