Mercurial > hg > vamp-build-and-test
comparison DEPENDENCIES/generic/include/boost/wave/util/cpp_iterator.hpp @ 16:2665513ce2d3
Add boost headers
author | Chris Cannam |
---|---|
date | Tue, 05 Aug 2014 11:11:38 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
15:663ca0da4350 | 16:2665513ce2d3 |
---|---|
1 /*============================================================================= | |
2 Boost.Wave: A Standard compliant C++ preprocessor library | |
3 | |
4 Definition of the preprocessor iterator | |
5 | |
6 http://www.boost.org/ | |
7 | |
8 Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost | |
9 Software License, Version 1.0. (See accompanying file | |
10 LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
11 =============================================================================*/ | |
12 | |
13 #if !defined(CPP_ITERATOR_HPP_175CA88F_7273_43FA_9039_BCF7459E1F29_INCLUDED) | |
14 #define CPP_ITERATOR_HPP_175CA88F_7273_43FA_9039_BCF7459E1F29_INCLUDED | |
15 | |
16 #include <string> | |
17 #include <vector> | |
18 #include <list> | |
19 #include <cstdlib> | |
20 #include <cctype> | |
21 | |
22 #include <boost/assert.hpp> | |
23 #include <boost/shared_ptr.hpp> | |
24 #include <boost/filesystem/path.hpp> | |
25 #include <boost/filesystem/operations.hpp> | |
26 #include <boost/spirit/include/classic_multi_pass.hpp> | |
27 #include <boost/spirit/include/classic_parse_tree_utils.hpp> | |
28 | |
29 #include <boost/wave/wave_config.hpp> | |
30 #include <boost/pool/pool_alloc.hpp> | |
31 | |
32 #include <boost/wave/util/insert_whitespace_detection.hpp> | |
33 #include <boost/wave/util/macro_helpers.hpp> | |
34 #include <boost/wave/util/cpp_macromap_utils.hpp> | |
35 #include <boost/wave/util/interpret_pragma.hpp> | |
36 #include <boost/wave/util/transform_iterator.hpp> | |
37 #include <boost/wave/util/functor_input.hpp> | |
38 #include <boost/wave/util/filesystem_compatibility.hpp> | |
39 | |
40 #include <boost/wave/grammars/cpp_grammar_gen.hpp> | |
41 #include <boost/wave/grammars/cpp_expression_grammar_gen.hpp> | |
42 #if BOOST_WAVE_ENABLE_COMMANDLINE_MACROS != 0 | |
43 #include <boost/wave/grammars/cpp_predef_macros_gen.hpp> | |
44 #endif | |
45 | |
46 #include <boost/wave/whitespace_handling.hpp> | |
47 #include <boost/wave/cpp_iteration_context.hpp> | |
48 #include <boost/wave/cpp_exceptions.hpp> | |
49 #include <boost/wave/language_support.hpp> | |
50 | |
51 // this must occur after all of the includes and before any code appears | |
52 #ifdef BOOST_HAS_ABI_HEADERS | |
53 #include BOOST_ABI_PREFIX | |
54 #endif | |
55 | |
56 /////////////////////////////////////////////////////////////////////////////// | |
57 namespace boost { | |
58 namespace wave { | |
59 namespace util { | |
60 | |
61 /////////////////////////////////////////////////////////////////////////////// | |
62 // retrieve the macro name from the parse tree | |
63 template < | |
64 typename ContextT, typename ParseNodeT, typename TokenT, | |
65 typename PositionT | |
66 > | |
67 inline bool | |
68 retrieve_macroname(ContextT& ctx, ParseNodeT const &node, | |
69 boost::spirit::classic::parser_id id, TokenT ¯oname, PositionT& act_pos, | |
70 bool update_position) | |
71 { | |
72 ParseNodeT const *name_node = 0; | |
73 | |
74 using boost::spirit::classic::find_node; | |
75 if (!find_node(node, id, &name_node)) | |
76 { | |
77 // ill formed define statement (unexpected, should not happen) | |
78 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, bad_define_statement, | |
79 "bad parse tree (unexpected)", act_pos); | |
80 return false; | |
81 } | |
82 | |
83 typename ParseNodeT::children_t const &children = name_node->children; | |
84 | |
85 if (0 == children.size() || | |
86 children.front().value.begin() == children.front().value.end()) | |
87 { | |
88 // ill formed define statement (unexpected, should not happen) | |
89 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, bad_define_statement, | |
90 "bad parse tree (unexpected)", act_pos); | |
91 return false; | |
92 } | |
93 | |
94 // retrieve the macro name | |
95 macroname = *children.front().value.begin(); | |
96 if (update_position) { | |
97 macroname.set_position(act_pos); | |
98 act_pos.set_column(act_pos.get_column() + macroname.get_value().size()); | |
99 } | |
100 return true; | |
101 } | |
102 | |
103 /////////////////////////////////////////////////////////////////////////////// | |
104 // retrieve the macro parameters or the macro definition from the parse tree | |
105 template <typename ParseNodeT, typename ContainerT, typename PositionT> | |
106 inline bool | |
107 retrieve_macrodefinition( | |
108 ParseNodeT const &node, boost::spirit::classic::parser_id id, | |
109 ContainerT ¯odefinition, PositionT& act_pos, bool update_position) | |
110 { | |
111 using namespace boost::wave; | |
112 typedef typename ParseNodeT::const_tree_iterator const_tree_iterator; | |
113 | |
114 // find macro parameters/macro definition inside the parse tree | |
115 std::pair<const_tree_iterator, const_tree_iterator> nodes; | |
116 | |
117 using boost::spirit::classic::get_node_range; | |
118 if (get_node_range(node, id, nodes)) { | |
119 // copy all parameters to the supplied container | |
120 typename ContainerT::iterator last_nonwhite = macrodefinition.end(); | |
121 const_tree_iterator end = nodes.second; | |
122 | |
123 for (const_tree_iterator cit = nodes.first; cit != end; ++cit) { | |
124 if ((*cit).value.begin() != (*cit).value.end()) { | |
125 typename ContainerT::iterator inserted = macrodefinition.insert( | |
126 macrodefinition.end(), *(*cit).value.begin()); | |
127 | |
128 if (!IS_CATEGORY(macrodefinition.back(), WhiteSpaceTokenType) && | |
129 T_NEWLINE != token_id(macrodefinition.back()) && | |
130 T_EOF != token_id(macrodefinition.back())) | |
131 { | |
132 last_nonwhite = inserted; | |
133 } | |
134 | |
135 if (update_position) { | |
136 (*inserted).set_position(act_pos); | |
137 act_pos.set_column( | |
138 act_pos.get_column() + (*inserted).get_value().size()); | |
139 } | |
140 } | |
141 } | |
142 | |
143 // trim trailing whitespace (leading whitespace is trimmed by the grammar) | |
144 if (last_nonwhite != macrodefinition.end()) { | |
145 if (update_position) { | |
146 act_pos.set_column((*last_nonwhite).get_position().get_column() + | |
147 (*last_nonwhite).get_value().size()); | |
148 } | |
149 macrodefinition.erase(++last_nonwhite, macrodefinition.end()); | |
150 } | |
151 return true; | |
152 } | |
153 return false; | |
154 } | |
155 | |
156 #if BOOST_WAVE_ENABLE_COMMANDLINE_MACROS != 0 | |
157 /////////////////////////////////////////////////////////////////////////////// | |
158 // add an additional predefined macro given by a string (MACRO(x)=definition) | |
159 template <typename ContextT> | |
160 bool add_macro_definition(ContextT &ctx, std::string macrostring, | |
161 bool is_predefined, boost::wave::language_support language) | |
162 { | |
163 typedef typename ContextT::token_type token_type; | |
164 typedef typename ContextT::lexer_type lexer_type; | |
165 typedef typename token_type::position_type position_type; | |
166 typedef boost::wave::grammars::predefined_macros_grammar_gen<lexer_type> | |
167 predef_macros_type; | |
168 | |
169 using namespace boost::wave; | |
170 using namespace std; // isspace is in std namespace for some systems | |
171 | |
172 // skip leading whitespace | |
173 std::string::iterator begin = macrostring.begin(); | |
174 std::string::iterator end = macrostring.end(); | |
175 | |
176 while(begin != end && isspace(*begin)) | |
177 ++begin; | |
178 | |
179 // parse the macro definition | |
180 position_type act_pos("<command line>"); | |
181 boost::spirit::classic::tree_parse_info<lexer_type> hit = | |
182 predef_macros_type::parse_predefined_macro( | |
183 lexer_type(begin, end, position_type(), language), lexer_type()); | |
184 | |
185 if (!hit.match || (!hit.full && T_EOF != token_id(*hit.stop))) { | |
186 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, bad_macro_definition, | |
187 macrostring.c_str(), act_pos); | |
188 return false; | |
189 } | |
190 | |
191 // retrieve the macro definition from the parse tree | |
192 token_type macroname; | |
193 std::vector<token_type> macroparameters; | |
194 typename ContextT::token_sequence_type macrodefinition; | |
195 bool has_parameters = false; | |
196 | |
197 if (!boost::wave::util::retrieve_macroname(ctx, *hit.trees.begin(), | |
198 BOOST_WAVE_PLAIN_DEFINE_ID, macroname, act_pos, true)) | |
199 return false; | |
200 has_parameters = boost::wave::util::retrieve_macrodefinition(*hit.trees.begin(), | |
201 BOOST_WAVE_MACRO_PARAMETERS_ID, macroparameters, act_pos, true); | |
202 boost::wave::util::retrieve_macrodefinition(*hit.trees.begin(), | |
203 BOOST_WAVE_MACRO_DEFINITION_ID, macrodefinition, act_pos, true); | |
204 | |
205 // get rid of trailing T_EOF | |
206 if (!macrodefinition.empty() && token_id(macrodefinition.back()) == T_EOF) | |
207 macrodefinition.pop_back(); | |
208 | |
209 // If no macrodefinition is given, and the macro string does not end with a | |
210 // '=', then the macro should be defined with the value '1' | |
211 if (macrodefinition.empty() && '=' != macrostring[macrostring.size()-1]) | |
212 macrodefinition.push_back(token_type(T_INTLIT, "1", act_pos)); | |
213 | |
214 // add the new macro to the macromap | |
215 return ctx.add_macro_definition(macroname, has_parameters, macroparameters, | |
216 macrodefinition, is_predefined); | |
217 } | |
218 #endif // BOOST_WAVE_ENABLE_COMMANDLINE_MACROS != 0 | |
219 | |
220 /////////////////////////////////////////////////////////////////////////////// | |
221 } // namespace util | |
222 | |
223 /////////////////////////////////////////////////////////////////////////////// | |
224 // forward declaration | |
225 template <typename ContextT> class pp_iterator; | |
226 | |
227 namespace impl { | |
228 | |
229 /////////////////////////////////////////////////////////////////////////////// | |
230 // | |
231 // pp_iterator_functor | |
232 // | |
233 /////////////////////////////////////////////////////////////////////////////// | |
234 template <typename ContextT> | |
235 class pp_iterator_functor { | |
236 | |
237 public: | |
238 // interface to the boost::spirit::classic::iterator_policies::functor_input policy | |
239 typedef typename ContextT::token_type result_type; | |
240 | |
241 // eof token | |
242 static result_type const eof; | |
243 | |
244 private: | |
245 // type of a token sequence | |
246 typedef typename ContextT::token_sequence_type token_sequence_type; | |
247 | |
248 typedef typename ContextT::lexer_type lexer_type; | |
249 typedef typename result_type::string_type string_type; | |
250 typedef typename result_type::position_type position_type; | |
251 typedef boost::wave::grammars::cpp_grammar_gen<lexer_type, token_sequence_type> | |
252 cpp_grammar_type; | |
253 | |
254 // iteration context related types (an iteration context represents a current | |
255 // position in an included file) | |
256 typedef base_iteration_context<ContextT, lexer_type> | |
257 base_iteration_context_type; | |
258 typedef iteration_context<ContextT, lexer_type> iteration_context_type; | |
259 | |
260 // parse tree related types | |
261 typedef typename cpp_grammar_type::node_factory_type node_factory_type; | |
262 typedef boost::spirit::classic::tree_parse_info<lexer_type, node_factory_type> | |
263 tree_parse_info_type; | |
264 typedef boost::spirit::classic::tree_match<lexer_type, node_factory_type> | |
265 parse_tree_match_type; | |
266 typedef typename parse_tree_match_type::node_t parse_node_type; // tree_node<node_val_data<> > | |
267 typedef typename parse_tree_match_type::parse_node_t parse_node_value_type; // node_val_data<> | |
268 typedef typename parse_tree_match_type::container_t parse_tree_type; // parse_node_type::children_t | |
269 | |
270 public: | |
271 template <typename IteratorT> | |
272 pp_iterator_functor(ContextT &ctx_, IteratorT const &first_, | |
273 IteratorT const &last_, typename ContextT::position_type const &pos_) | |
274 : ctx(ctx_), | |
275 iter_ctx(new base_iteration_context_type(ctx, | |
276 lexer_type(first_, last_, pos_, | |
277 boost::wave::enable_prefer_pp_numbers(ctx.get_language())), | |
278 lexer_type(), | |
279 pos_.get_file().c_str() | |
280 )), | |
281 seen_newline(true), skipped_newline(false), | |
282 must_emit_line_directive(false), act_pos(ctx_.get_main_pos()), | |
283 whitespace(boost::wave::need_insert_whitespace(ctx.get_language())) | |
284 { | |
285 act_pos.set_file(pos_.get_file()); | |
286 #if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0 | |
287 ctx_.set_current_filename(pos_.get_file().c_str()); | |
288 #endif | |
289 iter_ctx->emitted_lines = (unsigned int)(-1); // force #line directive | |
290 } | |
291 | |
292 // get the next preprocessed token | |
293 result_type const &operator()(); | |
294 | |
295 // get the last recognized token (for error processing etc.) | |
296 result_type const ¤t_token() const { return act_token; } | |
297 | |
298 protected: | |
299 friend class pp_iterator<ContextT>; | |
300 bool on_include_helper(char const *t, char const *s, bool is_system, | |
301 bool include_next); | |
302 | |
303 protected: | |
304 result_type const &get_next_token(); | |
305 result_type const &pp_token(); | |
306 | |
307 template <typename IteratorT> | |
308 bool extract_identifier(IteratorT &it); | |
309 template <typename IteratorT> | |
310 bool ensure_is_last_on_line(IteratorT& it, bool call_hook = true); | |
311 template <typename IteratorT> | |
312 bool skip_to_eol_with_check(IteratorT &it, bool call_hook = true); | |
313 | |
314 bool pp_directive(); | |
315 template <typename IteratorT> | |
316 bool handle_pp_directive(IteratorT &it); | |
317 bool dispatch_directive(tree_parse_info_type const &hit, | |
318 result_type const& found_directive, | |
319 token_sequence_type const& found_eoltokens); | |
320 void replace_undefined_identifiers(token_sequence_type &expanded); | |
321 | |
322 void on_include(string_type const &s, bool is_system, bool include_next); | |
323 void on_include(typename parse_tree_type::const_iterator const &begin, | |
324 typename parse_tree_type::const_iterator const &end, bool include_next); | |
325 | |
326 void on_define(parse_node_type const &node); | |
327 void on_undefine(lexer_type const &it); | |
328 | |
329 void on_ifdef(result_type const& found_directive, lexer_type const &it); | |
330 // typename parse_tree_type::const_iterator const &end); | |
331 void on_ifndef(result_type const& found_directive, lexer_type const& it); | |
332 // typename parse_tree_type::const_iterator const &end); | |
333 void on_else(); | |
334 void on_endif(); | |
335 void on_illformed(typename result_type::string_type s); | |
336 | |
337 void on_line(typename parse_tree_type::const_iterator const &begin, | |
338 typename parse_tree_type::const_iterator const &end); | |
339 void on_if(result_type const& found_directive, | |
340 typename parse_tree_type::const_iterator const &begin, | |
341 typename parse_tree_type::const_iterator const &end); | |
342 void on_elif(result_type const& found_directive, | |
343 typename parse_tree_type::const_iterator const &begin, | |
344 typename parse_tree_type::const_iterator const &end); | |
345 void on_error(typename parse_tree_type::const_iterator const &begin, | |
346 typename parse_tree_type::const_iterator const &end); | |
347 #if BOOST_WAVE_SUPPORT_WARNING_DIRECTIVE != 0 | |
348 void on_warning(typename parse_tree_type::const_iterator const &begin, | |
349 typename parse_tree_type::const_iterator const &end); | |
350 #endif | |
351 bool on_pragma(typename parse_tree_type::const_iterator const &begin, | |
352 typename parse_tree_type::const_iterator const &end); | |
353 | |
354 bool emit_line_directive(); | |
355 bool returned_from_include(); | |
356 | |
357 bool interpret_pragma(token_sequence_type const &pragma_body, | |
358 token_sequence_type &result); | |
359 | |
360 private: | |
361 ContextT &ctx; // context, this iterator is associated with | |
362 boost::shared_ptr<base_iteration_context_type> iter_ctx; | |
363 | |
364 bool seen_newline; // needed for recognizing begin of line | |
365 bool skipped_newline; // a newline has been skipped since last one | |
366 bool must_emit_line_directive; // must emit a line directive | |
367 result_type act_token; // current token | |
368 typename result_type::position_type &act_pos; // current fileposition (references the macromap) | |
369 | |
370 token_sequence_type unput_queue; // tokens to be preprocessed again | |
371 token_sequence_type pending_queue; // tokens already preprocessed | |
372 | |
373 // detect whether to insert additional whitespace in between two adjacent | |
374 // tokens, which otherwise would form a different token type, if | |
375 // re-tokenized | |
376 boost::wave::util::insert_whitespace_detection whitespace; | |
377 }; | |
378 | |
379 /////////////////////////////////////////////////////////////////////////////// | |
380 // eof token | |
381 template <typename ContextT> | |
382 typename pp_iterator_functor<ContextT>::result_type const | |
383 pp_iterator_functor<ContextT>::eof; | |
384 | |
385 /////////////////////////////////////////////////////////////////////////////// | |
386 // | |
387 // returned_from_include() | |
388 // | |
389 // Tests if it is necessary to pop the include file context (eof inside | |
390 // a file was reached). If yes, it pops this context. Preprocessing will | |
391 // continue with the next outer file scope. | |
392 // | |
393 /////////////////////////////////////////////////////////////////////////////// | |
394 template <typename ContextT> | |
395 inline bool | |
396 pp_iterator_functor<ContextT>::returned_from_include() | |
397 { | |
398 if (iter_ctx->first == iter_ctx->last && ctx.get_iteration_depth() > 0) { | |
399 // call the include policy trace function | |
400 #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0 | |
401 ctx.get_hooks().returning_from_include_file(); | |
402 #else | |
403 ctx.get_hooks().returning_from_include_file(ctx.derived()); | |
404 #endif | |
405 | |
406 // restore the previous iteration context after finishing the preprocessing | |
407 // of the included file | |
408 BOOST_WAVE_STRINGTYPE oldfile = iter_ctx->real_filename; | |
409 position_type old_pos (act_pos); | |
410 | |
411 // if this file has include guards handle it as if it had a #pragma once | |
412 #if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0 | |
413 if (need_include_guard_detection(ctx.get_language())) { | |
414 std::string guard_name; | |
415 if (iter_ctx->first.has_include_guards(guard_name)) | |
416 ctx.add_pragma_once_header(ctx.get_current_filename(), guard_name); | |
417 } | |
418 #endif | |
419 iter_ctx = ctx.pop_iteration_context(); | |
420 | |
421 must_emit_line_directive = true; | |
422 iter_ctx->emitted_lines = (unsigned int)(-1); // force #line directive | |
423 seen_newline = true; | |
424 | |
425 // restore current file position | |
426 act_pos.set_file(iter_ctx->filename); | |
427 act_pos.set_line(iter_ctx->line); | |
428 act_pos.set_column(0); | |
429 | |
430 // restore the actual current file and directory | |
431 #if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0 | |
432 namespace fs = boost::filesystem; | |
433 fs::path rfp(wave::util::create_path(iter_ctx->real_filename.c_str())); | |
434 std::string real_filename(rfp.string()); | |
435 ctx.set_current_filename(real_filename.c_str()); | |
436 #endif | |
437 ctx.set_current_directory(iter_ctx->real_filename.c_str()); | |
438 ctx.set_current_relative_filename(iter_ctx->real_relative_filename.c_str()); | |
439 | |
440 // ensure the integrity of the #if/#endif stack | |
441 // report unbalanced #if/#endif now to make it possible to recover properly | |
442 if (iter_ctx->if_block_depth != ctx.get_if_block_depth()) { | |
443 using boost::wave::util::impl::escape_lit; | |
444 BOOST_WAVE_STRINGTYPE msg(escape_lit(oldfile)); | |
445 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, unbalanced_if_endif, | |
446 msg.c_str(), old_pos); | |
447 } | |
448 return true; | |
449 } | |
450 return false; | |
451 } | |
452 | |
453 /////////////////////////////////////////////////////////////////////////////// | |
454 // | |
455 // operator()(): get the next preprocessed token | |
456 // | |
457 // throws a preprocess_exception, if appropriate | |
458 // | |
459 /////////////////////////////////////////////////////////////////////////////// | |
460 namespace impl { | |
461 | |
462 // It may be necessary to emit a #line directive either | |
463 // - when comments need to be preserved: if the current token is not a | |
464 // whitespace, except comments | |
465 // - when comments are to be skipped: if the current token is not a | |
466 // whitespace token. | |
467 template <typename ContextT> | |
468 bool consider_emitting_line_directive(ContextT const& ctx, token_id id) | |
469 { | |
470 if (need_preserve_comments(ctx.get_language())) | |
471 { | |
472 if (!IS_CATEGORY(id, EOLTokenType) && !IS_CATEGORY(id, EOFTokenType)) | |
473 { | |
474 return true; | |
475 } | |
476 } | |
477 if (!IS_CATEGORY(id, WhiteSpaceTokenType) && | |
478 !IS_CATEGORY(id, EOLTokenType) && !IS_CATEGORY(id, EOFTokenType)) | |
479 { | |
480 return true; | |
481 } | |
482 return false; | |
483 } | |
484 } | |
485 | |
486 template <typename ContextT> | |
487 inline typename pp_iterator_functor<ContextT>::result_type const & | |
488 pp_iterator_functor<ContextT>::operator()() | |
489 { | |
490 using namespace boost::wave; | |
491 | |
492 // make sure the cwd has been initialized | |
493 ctx.init_context(); | |
494 | |
495 // loop over skip able whitespace until something significant is found | |
496 bool was_seen_newline = seen_newline; | |
497 bool was_skipped_newline = skipped_newline; | |
498 token_id id = T_UNKNOWN; | |
499 | |
500 try { // catch lexer exceptions | |
501 do { | |
502 if (skipped_newline) { | |
503 was_skipped_newline = true; | |
504 skipped_newline = false; | |
505 } | |
506 | |
507 // get_next_token assigns result to act_token member | |
508 get_next_token(); | |
509 | |
510 // if comments shouldn't be preserved replace them with newlines | |
511 id = token_id(act_token); | |
512 if (!need_preserve_comments(ctx.get_language()) && | |
513 (T_CPPCOMMENT == id || context_policies::util::ccomment_has_newline(act_token))) | |
514 { | |
515 act_token.set_token_id(id = T_NEWLINE); | |
516 act_token.set_value("\n"); | |
517 } | |
518 | |
519 if (IS_CATEGORY(id, EOLTokenType)) | |
520 seen_newline = true; | |
521 | |
522 } while (ctx.get_hooks().may_skip_whitespace(ctx.derived(), act_token, skipped_newline)); | |
523 } | |
524 catch (boost::wave::cpplexer::lexing_exception const& e) { | |
525 // dispatch any lexer exceptions to the context hook function | |
526 ctx.get_hooks().throw_exception(ctx.derived(), e); | |
527 return act_token; | |
528 } | |
529 | |
530 // restore the accumulated skipped_newline state for next invocation | |
531 if (was_skipped_newline) | |
532 skipped_newline = true; | |
533 | |
534 // if there were skipped any newlines, we must emit a #line directive | |
535 if ((must_emit_line_directive || (was_seen_newline && skipped_newline)) && | |
536 impl::consider_emitting_line_directive(ctx, id)) | |
537 { | |
538 // must emit a #line directive | |
539 if (need_emit_line_directives(ctx.get_language()) && emit_line_directive()) | |
540 { | |
541 skipped_newline = false; | |
542 ctx.get_hooks().may_skip_whitespace(ctx.derived(), act_token, skipped_newline); // feed ws eater FSM | |
543 id = token_id(act_token); | |
544 } | |
545 } | |
546 | |
547 // cleanup of certain tokens required | |
548 seen_newline = false; | |
549 switch (static_cast<unsigned int>(id)) { | |
550 case T_NONREPLACABLE_IDENTIFIER: | |
551 act_token.set_token_id(id = T_IDENTIFIER); | |
552 break; | |
553 | |
554 case T_GENERATEDNEWLINE: // was generated by emit_line_directive() | |
555 act_token.set_token_id(id = T_NEWLINE); | |
556 ++iter_ctx->emitted_lines; | |
557 seen_newline = true; | |
558 break; | |
559 | |
560 case T_NEWLINE: | |
561 case T_CPPCOMMENT: | |
562 seen_newline = true; | |
563 ++iter_ctx->emitted_lines; | |
564 break; | |
565 | |
566 #if BOOST_WAVE_SUPPORT_CPP0X != 0 | |
567 case T_RAWSTRINGLIT: | |
568 iter_ctx->emitted_lines += | |
569 context_policies::util::rawstring_count_newlines(act_token); | |
570 break; | |
571 #endif | |
572 | |
573 case T_CCOMMENT: // will come here only if whitespace is preserved | |
574 iter_ctx->emitted_lines += | |
575 context_policies::util::ccomment_count_newlines(act_token); | |
576 break; | |
577 | |
578 case T_PP_NUMBER: // re-tokenize the pp-number | |
579 { | |
580 token_sequence_type rescanned; | |
581 | |
582 std::string pp_number( | |
583 util::to_string<std::string>(act_token.get_value())); | |
584 | |
585 lexer_type it = lexer_type(pp_number.begin(), | |
586 pp_number.end(), act_token.get_position(), | |
587 ctx.get_language()); | |
588 lexer_type end = lexer_type(); | |
589 | |
590 for (/**/; it != end && T_EOF != token_id(*it); ++it) | |
591 rescanned.push_back(*it); | |
592 | |
593 pending_queue.splice(pending_queue.begin(), rescanned); | |
594 act_token = pending_queue.front(); | |
595 id = token_id(act_token); | |
596 pending_queue.pop_front(); | |
597 } | |
598 break; | |
599 | |
600 case T_EOF: | |
601 seen_newline = true; | |
602 break; | |
603 | |
604 default: // make sure whitespace at line begin keeps seen_newline status | |
605 if (IS_CATEGORY(id, WhiteSpaceTokenType)) | |
606 seen_newline = was_seen_newline; | |
607 break; | |
608 } | |
609 | |
610 if (whitespace.must_insert(id, act_token.get_value())) { | |
611 // must insert some whitespace into the output stream to avoid adjacent | |
612 // tokens, which would form different (and wrong) tokens | |
613 whitespace.shift_tokens(T_SPACE); | |
614 pending_queue.push_front(act_token); // push this token back | |
615 return act_token = result_type(T_SPACE, | |
616 typename result_type::string_type(" "), | |
617 act_token.get_position()); | |
618 } | |
619 whitespace.shift_tokens(id); | |
620 return ctx.get_hooks().generated_token(ctx.derived(), act_token); | |
621 } | |
622 | |
623 /////////////////////////////////////////////////////////////////////////////// | |
624 template <typename ContextT> | |
625 inline typename pp_iterator_functor<ContextT>::result_type const & | |
626 pp_iterator_functor<ContextT>::get_next_token() | |
627 { | |
628 using namespace boost::wave; | |
629 | |
630 // if there is something in the unput_queue, then return the next token from | |
631 // there (all tokens in the queue are preprocessed already) | |
632 if (!pending_queue.empty() || !unput_queue.empty()) | |
633 return pp_token(); // return next token | |
634 | |
635 // test for EOF, if there is a pending input context, pop it back and continue | |
636 // parsing with it | |
637 bool returned_from_include_file = returned_from_include(); | |
638 | |
639 // try to generate the next token | |
640 if (iter_ctx->first != iter_ctx->last) { | |
641 do { | |
642 // If there are pending tokens in the queue, we'll have to return | |
643 // these. This may happen from a #pragma directive, which got replaced | |
644 // by some token sequence. | |
645 if (!pending_queue.empty()) { | |
646 util::on_exit::pop_front<token_sequence_type> | |
647 pop_front_token(pending_queue); | |
648 | |
649 return act_token = pending_queue.front(); | |
650 } | |
651 | |
652 // adjust the current position (line and column) | |
653 bool was_seen_newline = seen_newline || returned_from_include_file; | |
654 | |
655 // fetch the current token | |
656 act_token = *iter_ctx->first; | |
657 act_pos = act_token.get_position(); | |
658 | |
659 // act accordingly on the current token | |
660 token_id id = token_id(act_token); | |
661 | |
662 if (T_EOF == id) { | |
663 // returned from an include file, continue with the next token | |
664 whitespace.shift_tokens(T_EOF); | |
665 ++iter_ctx->first; | |
666 | |
667 // now make sure this line has a newline | |
668 if ((!seen_newline || act_pos.get_column() > 1) && | |
669 !need_single_line(ctx.get_language())) | |
670 { | |
671 // warn, if this file does not end with a newline | |
672 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, | |
673 last_line_not_terminated, "", act_pos); | |
674 } | |
675 continue; // if this is the main file, the while loop breaks | |
676 } | |
677 else if (T_NEWLINE == id || T_CPPCOMMENT == id) { | |
678 // a newline is to be returned ASAP, a C++ comment too | |
679 // (the C++ comment token includes the trailing newline) | |
680 seen_newline = true; | |
681 ++iter_ctx->first; | |
682 | |
683 if (!ctx.get_if_block_status()) { | |
684 // skip this token because of the disabled #if block | |
685 whitespace.shift_tokens(id); // whitespace controller | |
686 util::impl::call_skipped_token_hook(ctx, act_token); | |
687 continue; | |
688 } | |
689 return act_token; | |
690 } | |
691 seen_newline = false; | |
692 | |
693 if (was_seen_newline && pp_directive()) { | |
694 // a pp directive was found | |
695 // pending_queue.push_back(result_type(T_NEWLINE, "\n", act_pos)); | |
696 // seen_newline = true; | |
697 // must_emit_line_directive = true; | |
698 | |
699 // loop to the next token to analyze | |
700 // simply fall through, since the iterator was already adjusted | |
701 // correctly | |
702 } | |
703 else if (ctx.get_if_block_status()) { | |
704 // preprocess this token, eat up more, if appropriate, return | |
705 // the next preprocessed token | |
706 return pp_token(); | |
707 } | |
708 else { | |
709 // compilation condition is false: if the current token is a | |
710 // newline, account for it, otherwise discard the actual token and | |
711 // try the next one | |
712 if (T_NEWLINE == token_id(act_token)) { | |
713 seen_newline = true; | |
714 must_emit_line_directive = true; | |
715 } | |
716 | |
717 // next token | |
718 util::impl::call_skipped_token_hook(ctx, act_token); | |
719 ++iter_ctx->first; | |
720 } | |
721 | |
722 } while ((iter_ctx->first != iter_ctx->last) || | |
723 (returned_from_include_file = returned_from_include())); | |
724 | |
725 // overall eof reached | |
726 if (ctx.get_if_block_depth() > 0 && !need_single_line(ctx.get_language())) | |
727 { | |
728 // missing endif directive(s) | |
729 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, | |
730 missing_matching_endif, "", act_pos); | |
731 } | |
732 } | |
733 else { | |
734 act_token = eof; // this is the last token | |
735 } | |
736 | |
737 // whitespace.shift_tokens(T_EOF); // whitespace controller | |
738 return act_token; // return eof token | |
739 } | |
740 | |
741 /////////////////////////////////////////////////////////////////////////////// | |
742 // | |
743 // emit_line_directive(): emits a line directive from the act_token data | |
744 // | |
745 /////////////////////////////////////////////////////////////////////////////// | |
746 template <typename ContextT> | |
747 inline bool | |
748 pp_iterator_functor<ContextT>::emit_line_directive() | |
749 { | |
750 using namespace boost::wave; | |
751 | |
752 typename ContextT::position_type pos = act_token.get_position(); | |
753 | |
754 // if (must_emit_line_directive && | |
755 // iter_ctx->emitted_lines+1 == act_pos.get_line() && | |
756 // iter_ctx->filename == act_pos.get_file()) | |
757 // { | |
758 // must_emit_line_directive = false; | |
759 // return false; | |
760 // } | |
761 | |
762 if (must_emit_line_directive || | |
763 iter_ctx->emitted_lines+1 != act_pos.get_line()) | |
764 { | |
765 // unput the current token | |
766 pending_queue.push_front(act_token); | |
767 pos.set_line(act_pos.get_line()); | |
768 | |
769 if (iter_ctx->emitted_lines+2 == act_pos.get_line() && act_pos.get_line() != 1) { | |
770 // prefer to output a single newline instead of the #line directive | |
771 // whitespace.shift_tokens(T_NEWLINE); | |
772 act_token = result_type(T_NEWLINE, "\n", pos); | |
773 } | |
774 else { | |
775 // account for the newline emitted here | |
776 act_pos.set_line(act_pos.get_line()-1); | |
777 iter_ctx->emitted_lines = act_pos.get_line()-1; | |
778 | |
779 token_sequence_type pending; | |
780 | |
781 if (!ctx.get_hooks().emit_line_directive(ctx, pending, act_token)) | |
782 { | |
783 unsigned int column = 6; | |
784 | |
785 // the hook did not generate anything, emit default #line | |
786 pos.set_column(1); | |
787 pending.push_back(result_type(T_PP_LINE, "#line", pos)); | |
788 | |
789 pos.set_column(column); // account for '#line' | |
790 pending.push_back(result_type(T_SPACE, " ", pos)); | |
791 | |
792 // 21 is the max required size for a 64 bit integer represented as a | |
793 // string | |
794 char buffer[22]; | |
795 | |
796 using namespace std; // for some systems sprintf is in namespace std | |
797 sprintf (buffer, "%ld", pos.get_line()); | |
798 | |
799 pos.set_column(++column); // account for ' ' | |
800 pending.push_back(result_type(T_INTLIT, buffer, pos)); | |
801 pos.set_column(column += (unsigned int)strlen(buffer)); // account for <number> | |
802 pending.push_back(result_type(T_SPACE, " ", pos)); | |
803 pos.set_column(++column); // account for ' ' | |
804 | |
805 std::string file("\""); | |
806 boost::filesystem::path filename( | |
807 wave::util::create_path(act_pos.get_file().c_str())); | |
808 | |
809 using wave::util::impl::escape_lit; | |
810 file += escape_lit(wave::util::native_file_string(filename)) + "\""; | |
811 | |
812 pending.push_back(result_type(T_STRINGLIT, file.c_str(), pos)); | |
813 pos.set_column(column += (unsigned int)file.size()); // account for filename | |
814 pending.push_back(result_type(T_GENERATEDNEWLINE, "\n", pos)); | |
815 } | |
816 | |
817 // if there is some replacement text, insert it into the pending queue | |
818 if (!pending.empty()) { | |
819 pending_queue.splice(pending_queue.begin(), pending); | |
820 act_token = pending_queue.front(); | |
821 pending_queue.pop_front(); | |
822 } | |
823 } | |
824 | |
825 must_emit_line_directive = false; // we are now in sync | |
826 return true; | |
827 } | |
828 | |
829 must_emit_line_directive = false; // we are now in sync | |
830 return false; | |
831 } | |
832 | |
833 /////////////////////////////////////////////////////////////////////////////// | |
834 // | |
835 // pptoken(): return the next preprocessed token | |
836 // | |
837 /////////////////////////////////////////////////////////////////////////////// | |
838 template <typename ContextT> | |
839 inline typename pp_iterator_functor<ContextT>::result_type const & | |
840 pp_iterator_functor<ContextT>::pp_token() | |
841 { | |
842 using namespace boost::wave; | |
843 | |
844 token_id id = token_id(*iter_ctx->first); | |
845 | |
846 // eat all T_PLACEHOLDER tokens, eventually slipped through out of the | |
847 // macro engine | |
848 do { | |
849 if (!pending_queue.empty()) { | |
850 // if there are pending tokens in the queue, return the first one | |
851 act_token = pending_queue.front(); | |
852 pending_queue.pop_front(); | |
853 act_pos = act_token.get_position(); | |
854 } | |
855 else if (!unput_queue.empty() | |
856 || T_IDENTIFIER == id | |
857 || IS_CATEGORY(id, KeywordTokenType) | |
858 || IS_EXTCATEGORY(id, OperatorTokenType|AltExtTokenType) | |
859 || IS_CATEGORY(id, BoolLiteralTokenType)) | |
860 { | |
861 // call the lexer, preprocess the required number of tokens, put them | |
862 // into the unput queue | |
863 act_token = ctx.expand_tokensequence(iter_ctx->first, | |
864 iter_ctx->last, pending_queue, unput_queue, skipped_newline); | |
865 } | |
866 else { | |
867 // simply return the next token | |
868 act_token = *iter_ctx->first; | |
869 ++iter_ctx->first; | |
870 } | |
871 id = token_id(act_token); | |
872 | |
873 } while (T_PLACEHOLDER == id); | |
874 return act_token; | |
875 } | |
876 | |
877 /////////////////////////////////////////////////////////////////////////////// | |
878 // | |
879 // pp_directive(): recognize a preprocessor directive | |
880 // | |
881 /////////////////////////////////////////////////////////////////////////////// | |
882 namespace impl { | |
883 | |
884 // call 'found_directive' preprocessing hook | |
885 template <typename ContextT> | |
886 bool call_found_directive_hook(ContextT& ctx, | |
887 typename ContextT::token_type const& found_directive) | |
888 { | |
889 #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0 | |
890 ctx.get_hooks().found_directive(found_directive); | |
891 #else | |
892 if (ctx.get_hooks().found_directive(ctx.derived(), found_directive)) | |
893 return true; // skip this directive and return newline only | |
894 #endif | |
895 return false; | |
896 } | |
897 | |
898 // // call 'skipped_token' preprocessing hook | |
899 // template <typename ContextT> | |
900 // void call_skipped_token_hook(ContextT& ctx, | |
901 // typename ContextT::token_type const& skipped) | |
902 // { | |
903 // #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0 | |
904 // ctx.get_hooks().skipped_token(skipped); | |
905 // #else | |
906 // ctx.get_hooks().skipped_token(ctx.derived(), skipped); | |
907 // #endif | |
908 // } | |
909 | |
910 template <typename ContextT, typename IteratorT> | |
911 bool next_token_is_pp_directive(ContextT &ctx, IteratorT &it, IteratorT const &end) | |
912 { | |
913 using namespace boost::wave; | |
914 | |
915 token_id id = T_UNKNOWN; | |
916 for (/**/; it != end; ++it) { | |
917 id = token_id(*it); | |
918 if (!IS_CATEGORY(id, WhiteSpaceTokenType)) | |
919 break; // skip leading whitespace | |
920 if (IS_CATEGORY(id, EOLTokenType) || IS_CATEGORY(id, EOFTokenType)) | |
921 break; // do not enter a new line | |
922 if (T_CPPCOMMENT == id || | |
923 context_policies::util::ccomment_has_newline(*it)) | |
924 { | |
925 break; | |
926 } | |
927 | |
928 // this token gets skipped | |
929 util::impl::call_skipped_token_hook(ctx, *it); | |
930 } | |
931 BOOST_ASSERT(it == end || id != T_UNKNOWN); | |
932 return it != end && IS_CATEGORY(id, PPTokenType); | |
933 } | |
934 | |
935 // verify that there isn't anything significant left on the line | |
936 template <typename ContextT, typename IteratorT> | |
937 bool pp_is_last_on_line(ContextT &ctx, IteratorT &it, IteratorT const &end, | |
938 bool call_hook = true) | |
939 { | |
940 using namespace boost::wave; | |
941 | |
942 // this token gets skipped | |
943 if (call_hook) | |
944 util::impl::call_skipped_token_hook(ctx, *it); | |
945 | |
946 for (++it; it != end; ++it) { | |
947 token_id id = token_id(*it); | |
948 | |
949 if (T_CPPCOMMENT == id || T_NEWLINE == id || | |
950 context_policies::util::ccomment_has_newline(*it)) | |
951 { | |
952 if (call_hook) | |
953 util::impl::call_skipped_token_hook(ctx, *it); | |
954 ++it; // skip eol/C/C++ comment | |
955 return true; // no more significant tokens on this line | |
956 } | |
957 | |
958 if (!IS_CATEGORY(id, WhiteSpaceTokenType)) | |
959 break; | |
960 | |
961 // this token gets skipped | |
962 if (call_hook) | |
963 util::impl::call_skipped_token_hook(ctx, *it); | |
964 } | |
965 return false; | |
966 } | |
967 | |
968 /////////////////////////////////////////////////////////////////////////// | |
969 template <typename ContextT, typename IteratorT> | |
970 bool skip_to_eol(ContextT &ctx, IteratorT &it, IteratorT const &end, | |
971 bool call_hook = true) | |
972 { | |
973 using namespace boost::wave; | |
974 | |
975 for (/**/; it != end; ++it) { | |
976 token_id id = token_id(*it); | |
977 | |
978 if (T_CPPCOMMENT == id || T_NEWLINE == id || | |
979 context_policies::util::ccomment_has_newline(*it)) | |
980 { | |
981 // always call hook for eol | |
982 util::impl::call_skipped_token_hook(ctx, *it); | |
983 ++it; // skip eol/C/C++ comment | |
984 return true; // found eol | |
985 } | |
986 | |
987 if (call_hook) | |
988 util::impl::call_skipped_token_hook(ctx, *it); | |
989 } | |
990 return false; | |
991 } | |
992 | |
993 /////////////////////////////////////////////////////////////////////////// | |
994 template <typename ContextT, typename ContainerT> | |
995 inline void | |
996 remove_leading_whitespace(ContextT &ctx, ContainerT& c, bool call_hook = true) | |
997 { | |
998 typename ContainerT::iterator it = c.begin(); | |
999 while (IS_CATEGORY(*it, WhiteSpaceTokenType)) { | |
1000 typename ContainerT::iterator save = it++; | |
1001 if (call_hook) | |
1002 util::impl::call_skipped_token_hook(ctx, *save); | |
1003 c.erase(save); | |
1004 } | |
1005 } | |
1006 } | |
1007 | |
1008 /////////////////////////////////////////////////////////////////////////////// | |
1009 template <typename ContextT> | |
1010 template <typename IteratorT> | |
1011 inline bool | |
1012 pp_iterator_functor<ContextT>::extract_identifier(IteratorT &it) | |
1013 { | |
1014 token_id id = util::impl::skip_whitespace(it, iter_ctx->last); | |
1015 if (T_IDENTIFIER == id || IS_CATEGORY(id, KeywordTokenType) || | |
1016 IS_EXTCATEGORY(id, OperatorTokenType|AltExtTokenType) || | |
1017 IS_CATEGORY(id, BoolLiteralTokenType)) | |
1018 { | |
1019 IteratorT save = it; | |
1020 if (impl::pp_is_last_on_line(ctx, save, iter_ctx->last, false)) | |
1021 return true; | |
1022 } | |
1023 | |
1024 // report the ill formed directive | |
1025 impl::skip_to_eol(ctx, it, iter_ctx->last); | |
1026 | |
1027 string_type str(util::impl::as_string<string_type>(iter_ctx->first, it)); | |
1028 | |
1029 seen_newline = true; | |
1030 iter_ctx->first = it; | |
1031 on_illformed(str); | |
1032 return false; | |
1033 } | |
1034 | |
1035 /////////////////////////////////////////////////////////////////////////////// | |
1036 template <typename ContextT> | |
1037 template <typename IteratorT> | |
1038 inline bool | |
1039 pp_iterator_functor<ContextT>::ensure_is_last_on_line(IteratorT& it, bool call_hook) | |
1040 { | |
1041 if (!impl::pp_is_last_on_line(ctx, it, iter_ctx->last, call_hook)) | |
1042 { | |
1043 // enable error recovery (start over with the next line) | |
1044 impl::skip_to_eol(ctx, it, iter_ctx->last); | |
1045 | |
1046 string_type str(util::impl::as_string<string_type>( | |
1047 iter_ctx->first, it)); | |
1048 | |
1049 seen_newline = true; | |
1050 iter_ctx->first = it; | |
1051 | |
1052 // report an invalid directive | |
1053 on_illformed(str); | |
1054 return false; | |
1055 } | |
1056 | |
1057 if (it == iter_ctx->last && !need_single_line(ctx.get_language())) | |
1058 { | |
1059 // The line doesn't end with an eol but eof token. | |
1060 seen_newline = true; // allow to resume after warning | |
1061 iter_ctx->first = it; | |
1062 | |
1063 // Trigger a warning that the last line was not terminated with a | |
1064 // newline. | |
1065 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, | |
1066 last_line_not_terminated, "", act_pos); | |
1067 | |
1068 return false; | |
1069 } | |
1070 return true; | |
1071 } | |
1072 | |
1073 template <typename ContextT> | |
1074 template <typename IteratorT> | |
1075 inline bool | |
1076 pp_iterator_functor<ContextT>::skip_to_eol_with_check(IteratorT &it, bool call_hook) | |
1077 { | |
1078 typename ContextT::string_type value ((*it).get_value()); | |
1079 if (!impl::skip_to_eol(ctx, it, iter_ctx->last, call_hook) && | |
1080 !need_single_line(ctx.get_language())) | |
1081 { | |
1082 // The line doesn't end with an eol but eof token. | |
1083 seen_newline = true; // allow to resume after warning | |
1084 iter_ctx->first = it; | |
1085 | |
1086 // Trigger a warning, that the last line was not terminated with a | |
1087 // newline. | |
1088 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, | |
1089 last_line_not_terminated, "", act_pos); | |
1090 return false; | |
1091 } | |
1092 | |
1093 // normal line ending reached, adjust iterator and flag | |
1094 seen_newline = true; | |
1095 iter_ctx->first = it; | |
1096 return true; | |
1097 } | |
1098 | |
1099 /////////////////////////////////////////////////////////////////////////////// | |
1100 // handle_pp_directive: handle certain pp_directives | |
1101 template <typename ContextT> | |
1102 template <typename IteratorT> | |
1103 inline bool | |
1104 pp_iterator_functor<ContextT>::handle_pp_directive(IteratorT &it) | |
1105 { | |
1106 token_id id = token_id(*it); | |
1107 bool can_exit = true; | |
1108 bool call_hook_in_skip = true; | |
1109 if (!ctx.get_if_block_status()) { | |
1110 if (IS_EXTCATEGORY(*it, PPConditionalTokenType)) { | |
1111 // simulate the if block hierarchy | |
1112 switch (static_cast<unsigned int>(id)) { | |
1113 case T_PP_IFDEF: // #ifdef | |
1114 case T_PP_IFNDEF: // #ifndef | |
1115 case T_PP_IF: // #if | |
1116 ctx.enter_if_block(false); | |
1117 break; | |
1118 | |
1119 case T_PP_ELIF: // #elif | |
1120 if (!ctx.get_enclosing_if_block_status()) { | |
1121 if (!ctx.enter_elif_block(false)) { | |
1122 // #else without matching #if | |
1123 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, | |
1124 missing_matching_if, "#elif", act_pos); | |
1125 return true; // do not analyze this directive any further | |
1126 } | |
1127 } | |
1128 else { | |
1129 can_exit = false; // #elif is not always safe to skip | |
1130 } | |
1131 break; | |
1132 | |
1133 case T_PP_ELSE: // #else | |
1134 case T_PP_ENDIF: // #endif | |
1135 { | |
1136 // handle this directive | |
1137 if (T_PP_ELSE == token_id(*it)) | |
1138 on_else(); | |
1139 else | |
1140 on_endif(); | |
1141 | |
1142 // make sure, there are no (non-whitespace) tokens left on | |
1143 // this line | |
1144 ensure_is_last_on_line(it); | |
1145 | |
1146 // we skipped to the end of this line already | |
1147 seen_newline = true; | |
1148 iter_ctx->first = it; | |
1149 } | |
1150 return true; | |
1151 | |
1152 default: // #something else | |
1153 on_illformed((*it).get_value()); | |
1154 break; | |
1155 } | |
1156 } | |
1157 else { | |
1158 util::impl::call_skipped_token_hook(ctx, *it); | |
1159 ++it; | |
1160 } | |
1161 } | |
1162 else { | |
1163 // try to handle the simple pp directives without parsing | |
1164 result_type directive = *it; | |
1165 bool include_next = false; | |
1166 switch (static_cast<unsigned int>(id)) { | |
1167 case T_PP_QHEADER: // #include "..." | |
1168 #if BOOST_WAVE_SUPPORT_INCLUDE_NEXT != 0 | |
1169 case T_PP_QHEADER_NEXT: | |
1170 #endif | |
1171 include_next = (T_PP_QHEADER_NEXT == id) ? true : false; | |
1172 if (!impl::call_found_directive_hook(ctx, *it)) | |
1173 { | |
1174 string_type dir((*it).get_value()); | |
1175 | |
1176 // make sure, there are no (non-whitespace) tokens left on | |
1177 // this line | |
1178 if (ensure_is_last_on_line(it)) | |
1179 { | |
1180 seen_newline = true; | |
1181 iter_ctx->first = it; | |
1182 on_include (dir, false, include_next); | |
1183 } | |
1184 return true; | |
1185 } | |
1186 break; | |
1187 | |
1188 case T_PP_HHEADER: // #include <...> | |
1189 #if BOOST_WAVE_SUPPORT_INCLUDE_NEXT != 0 | |
1190 case T_PP_HHEADER_NEXT: | |
1191 #endif | |
1192 include_next = (T_PP_HHEADER_NEXT == id) ? true : false; | |
1193 if (!impl::call_found_directive_hook(ctx, *it)) | |
1194 { | |
1195 string_type dir((*it).get_value()); | |
1196 | |
1197 // make sure, there are no (non-whitespace) tokens left on | |
1198 // this line | |
1199 if (ensure_is_last_on_line(it)) | |
1200 { | |
1201 seen_newline = true; | |
1202 iter_ctx->first = it; | |
1203 on_include (dir, true, include_next); | |
1204 } | |
1205 return true; | |
1206 } | |
1207 break; | |
1208 | |
1209 case T_PP_ELSE: // #else | |
1210 case T_PP_ENDIF: // #endif | |
1211 if (!impl::call_found_directive_hook(ctx, *it)) | |
1212 { | |
1213 // handle this directive | |
1214 if (T_PP_ELSE == token_id(*it)) | |
1215 on_else(); | |
1216 else | |
1217 on_endif(); | |
1218 | |
1219 // make sure, there are no (non-whitespace) tokens left on | |
1220 // this line | |
1221 ensure_is_last_on_line(it); | |
1222 | |
1223 // we skipped to the end of this line already | |
1224 seen_newline = true; | |
1225 iter_ctx->first = it; | |
1226 return true; | |
1227 } | |
1228 break; | |
1229 | |
1230 // extract everything on this line as arguments | |
1231 // case T_PP_IF: // #if | |
1232 // case T_PP_ELIF: // #elif | |
1233 // case T_PP_ERROR: // #error | |
1234 // case T_PP_WARNING: // #warning | |
1235 // case T_PP_PRAGMA: // #pragma | |
1236 // case T_PP_LINE: // #line | |
1237 // break; | |
1238 | |
1239 // extract first non-whitespace token as argument | |
1240 case T_PP_UNDEF: // #undef | |
1241 if (!impl::call_found_directive_hook(ctx, *it) && | |
1242 extract_identifier(it)) | |
1243 { | |
1244 on_undefine(it); | |
1245 } | |
1246 call_hook_in_skip = false; | |
1247 break; | |
1248 | |
1249 case T_PP_IFDEF: // #ifdef | |
1250 if (!impl::call_found_directive_hook(ctx, *it) && | |
1251 extract_identifier(it)) | |
1252 { | |
1253 on_ifdef(directive, it); | |
1254 } | |
1255 call_hook_in_skip = false; | |
1256 break; | |
1257 | |
1258 case T_PP_IFNDEF: // #ifndef | |
1259 if (!impl::call_found_directive_hook(ctx, *it) && | |
1260 extract_identifier(it)) | |
1261 { | |
1262 on_ifndef(directive, it); | |
1263 } | |
1264 call_hook_in_skip = false; | |
1265 break; | |
1266 | |
1267 #if BOOST_WAVE_SUPPORT_MS_EXTENSIONS != 0 | |
1268 // case T_MSEXT_PP_REGION: // #region ... | |
1269 // break; | |
1270 // | |
1271 // case T_MSEXT_PP_ENDREGION: // #endregion | |
1272 // break; | |
1273 #endif | |
1274 | |
1275 default: | |
1276 can_exit = false; | |
1277 break; | |
1278 } | |
1279 } | |
1280 | |
1281 // start over with the next line, if only possible | |
1282 if (can_exit) { | |
1283 skip_to_eol_with_check(it, call_hook_in_skip); | |
1284 return true; // may be safely ignored | |
1285 } | |
1286 return false; // do not ignore this pp directive | |
1287 } | |
1288 | |
1289 /////////////////////////////////////////////////////////////////////////////// | |
1290 // pp_directive(): recognize a preprocessor directive | |
1291 template <typename ContextT> | |
1292 inline bool | |
1293 pp_iterator_functor<ContextT>::pp_directive() | |
1294 { | |
1295 using namespace cpplexer; | |
1296 | |
1297 // test, if the next non-whitespace token is a pp directive | |
1298 lexer_type it = iter_ctx->first; | |
1299 | |
1300 if (!impl::next_token_is_pp_directive(ctx, it, iter_ctx->last)) { | |
1301 // skip null pp directive (no need to do it via the parser) | |
1302 if (it != iter_ctx->last && T_POUND == BASE_TOKEN(token_id(*it))) { | |
1303 if (impl::pp_is_last_on_line(ctx, it, iter_ctx->last)) { | |
1304 // start over with the next line | |
1305 seen_newline = true; | |
1306 iter_ctx->first = it; | |
1307 return true; | |
1308 } | |
1309 else if (ctx.get_if_block_status()) { | |
1310 // report invalid pp directive | |
1311 impl::skip_to_eol(ctx, it, iter_ctx->last); | |
1312 seen_newline = true; | |
1313 | |
1314 string_type str(boost::wave::util::impl::as_string<string_type>( | |
1315 iter_ctx->first, it)); | |
1316 | |
1317 token_sequence_type faulty_line; | |
1318 | |
1319 for (/**/; iter_ctx->first != it; ++iter_ctx->first) | |
1320 faulty_line.push_back(*iter_ctx->first); | |
1321 | |
1322 token_sequence_type pending; | |
1323 if (ctx.get_hooks().found_unknown_directive(ctx, faulty_line, pending)) | |
1324 { | |
1325 // if there is some replacement text, insert it into the pending queue | |
1326 if (!pending.empty()) | |
1327 pending_queue.splice(pending_queue.begin(), pending); | |
1328 return true; | |
1329 } | |
1330 | |
1331 // default behavior is to throw an exception | |
1332 on_illformed(str); | |
1333 } | |
1334 } | |
1335 | |
1336 // this line does not contain a pp directive, so simply return | |
1337 return false; | |
1338 } | |
1339 | |
1340 // found eof | |
1341 if (it == iter_ctx->last) | |
1342 return false; | |
1343 | |
1344 // ignore/handle all pp directives not related to conditional compilation while | |
1345 // if block status is false | |
1346 if (handle_pp_directive(it)) { | |
1347 // we may skip pp directives only if the current if block status is | |
1348 // false or if it was a #include directive we could handle directly | |
1349 return true; // the pp directive has been handled/skipped | |
1350 } | |
1351 | |
1352 // found a pp directive, so try to identify it, start with the pp_token | |
1353 bool found_eof = false; | |
1354 result_type found_directive; | |
1355 token_sequence_type found_eoltokens; | |
1356 | |
1357 tree_parse_info_type hit = cpp_grammar_type::parse_cpp_grammar( | |
1358 it, iter_ctx->last, act_pos, found_eof, found_directive, found_eoltokens); | |
1359 | |
1360 if (hit.match) { | |
1361 // position the iterator past the matched sequence to allow | |
1362 // resynchronization, if an error occurs | |
1363 iter_ctx->first = hit.stop; | |
1364 seen_newline = true; | |
1365 must_emit_line_directive = true; | |
1366 | |
1367 // found a valid pp directive, dispatch to the correct function to handle | |
1368 // the found pp directive | |
1369 bool result = dispatch_directive (hit, found_directive, found_eoltokens); | |
1370 | |
1371 if (found_eof && !need_single_line(ctx.get_language())) { | |
1372 // The line was terminated with an end of file token. | |
1373 // So trigger a warning, that the last line was not terminated with a | |
1374 // newline. | |
1375 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, | |
1376 last_line_not_terminated, "", act_pos); | |
1377 } | |
1378 return result; | |
1379 } | |
1380 else if (token_id(found_directive) != T_EOF) { | |
1381 // recognized invalid directive | |
1382 impl::skip_to_eol(ctx, it, iter_ctx->last); | |
1383 seen_newline = true; | |
1384 | |
1385 string_type str(boost::wave::util::impl::as_string<string_type>( | |
1386 iter_ctx->first, it)); | |
1387 iter_ctx->first = it; | |
1388 | |
1389 // report the ill formed directive | |
1390 on_illformed(str); | |
1391 } | |
1392 return false; | |
1393 } | |
1394 | |
1395 /////////////////////////////////////////////////////////////////////////////// | |
1396 // | |
1397 // dispatch_directive(): dispatch a recognized preprocessor directive | |
1398 // | |
1399 /////////////////////////////////////////////////////////////////////////////// | |
1400 template <typename ContextT> | |
1401 inline bool | |
1402 pp_iterator_functor<ContextT>::dispatch_directive( | |
1403 tree_parse_info_type const &hit, result_type const& found_directive, | |
1404 token_sequence_type const& found_eoltokens) | |
1405 { | |
1406 using namespace cpplexer; | |
1407 | |
1408 typedef typename parse_tree_type::const_iterator const_child_iterator_t; | |
1409 | |
1410 // this iterator points to the root node of the parse tree | |
1411 const_child_iterator_t begin = hit.trees.begin(); | |
1412 | |
1413 // decide, which preprocessor directive was found | |
1414 parse_tree_type const &root = (*begin).children; | |
1415 parse_node_value_type const &nodeval = get_first_leaf(*root.begin()).value; | |
1416 //long node_id = nodeval.id().to_long(); | |
1417 | |
1418 const_child_iterator_t begin_child_it = (*root.begin()).children.begin(); | |
1419 const_child_iterator_t end_child_it = (*root.begin()).children.end(); | |
1420 | |
1421 token_id id = token_id(found_directive); | |
1422 | |
1423 // call preprocessing hook | |
1424 if (impl::call_found_directive_hook(ctx, found_directive)) | |
1425 return true; // skip this directive and return newline only | |
1426 | |
1427 switch (static_cast<unsigned int>(id)) { | |
1428 // case T_PP_QHEADER: // #include "..." | |
1429 // #if BOOST_WAVE_SUPPORT_INCLUDE_NEXT != 0 | |
1430 // case T_PP_QHEADER_NEXT: // #include_next "..." | |
1431 // #endif | |
1432 // on_include ((*nodeval.begin()).get_value(), false, | |
1433 // T_PP_QHEADER_NEXT == id); | |
1434 // break; | |
1435 | |
1436 // case T_PP_HHEADER: // #include <...> | |
1437 // #if BOOST_WAVE_SUPPORT_INCLUDE_NEXT != 0 | |
1438 // case T_PP_HHEADER_NEXT: // #include_next <...> | |
1439 // #endif | |
1440 // on_include ((*nodeval.begin()).get_value(), true, | |
1441 // T_PP_HHEADER_NEXT == id); | |
1442 // break; | |
1443 | |
1444 case T_PP_INCLUDE: // #include ... | |
1445 #if BOOST_WAVE_SUPPORT_INCLUDE_NEXT != 0 | |
1446 case T_PP_INCLUDE_NEXT: // #include_next ... | |
1447 #endif | |
1448 on_include (begin_child_it, end_child_it, T_PP_INCLUDE_NEXT == id); | |
1449 break; | |
1450 | |
1451 case T_PP_DEFINE: // #define | |
1452 on_define (*begin); | |
1453 break; | |
1454 | |
1455 // case T_PP_UNDEF: // #undef | |
1456 // on_undefine(*nodeval.begin()); | |
1457 // break; | |
1458 // | |
1459 // case T_PP_IFDEF: // #ifdef | |
1460 // on_ifdef(found_directive, begin_child_it, end_child_it); | |
1461 // break; | |
1462 // | |
1463 // case T_PP_IFNDEF: // #ifndef | |
1464 // on_ifndef(found_directive, begin_child_it, end_child_it); | |
1465 // break; | |
1466 | |
1467 case T_PP_IF: // #if | |
1468 on_if(found_directive, begin_child_it, end_child_it); | |
1469 break; | |
1470 | |
1471 case T_PP_ELIF: // #elif | |
1472 on_elif(found_directive, begin_child_it, end_child_it); | |
1473 break; | |
1474 | |
1475 // case T_PP_ELSE: // #else | |
1476 // on_else(); | |
1477 // break; | |
1478 | |
1479 // case T_PP_ENDIF: // #endif | |
1480 // on_endif(); | |
1481 // break; | |
1482 | |
1483 case T_PP_LINE: // #line | |
1484 on_line(begin_child_it, end_child_it); | |
1485 break; | |
1486 | |
1487 case T_PP_ERROR: // #error | |
1488 on_error(begin_child_it, end_child_it); | |
1489 break; | |
1490 | |
1491 #if BOOST_WAVE_SUPPORT_WARNING_DIRECTIVE != 0 | |
1492 case T_PP_WARNING: // #warning | |
1493 on_warning(begin_child_it, end_child_it); | |
1494 break; | |
1495 #endif | |
1496 | |
1497 case T_PP_PRAGMA: // #pragma | |
1498 return on_pragma(begin_child_it, end_child_it); | |
1499 | |
1500 #if BOOST_WAVE_SUPPORT_MS_EXTENSIONS != 0 | |
1501 case T_MSEXT_PP_REGION: | |
1502 case T_MSEXT_PP_ENDREGION: | |
1503 break; // ignore these | |
1504 #endif | |
1505 | |
1506 default: // #something else | |
1507 on_illformed((*nodeval.begin()).get_value()); | |
1508 | |
1509 // if we end up here, we have been instructed to ignore the error, so | |
1510 // we simply copy the whole construct to the output | |
1511 { | |
1512 token_sequence_type expanded; | |
1513 get_token_value<result_type, parse_node_type> get_value; | |
1514 | |
1515 std::copy(make_ref_transform_iterator(begin_child_it, get_value), | |
1516 make_ref_transform_iterator(end_child_it, get_value), | |
1517 std::inserter(expanded, expanded.end())); | |
1518 pending_queue.splice(pending_queue.begin(), expanded); | |
1519 } | |
1520 break; | |
1521 } | |
1522 | |
1523 // properly skip trailing newline for all directives | |
1524 typename token_sequence_type::const_iterator eol = found_eoltokens.begin(); | |
1525 impl::skip_to_eol(ctx, eol, found_eoltokens.end()); | |
1526 return true; // return newline only | |
1527 } | |
1528 | |
1529 /////////////////////////////////////////////////////////////////////////////// | |
1530 // | |
1531 // on_include: handle #include <...> or #include "..." directives | |
1532 // | |
1533 /////////////////////////////////////////////////////////////////////////////// | |
1534 template <typename ContextT> | |
1535 inline void | |
1536 pp_iterator_functor<ContextT>::on_include (string_type const &s, | |
1537 bool is_system, bool include_next) | |
1538 { | |
1539 BOOST_ASSERT(ctx.get_if_block_status()); | |
1540 | |
1541 // strip quotes first, extract filename | |
1542 typename string_type::size_type pos_end = s.find_last_of(is_system ? '>' : '\"'); | |
1543 | |
1544 if (string_type::npos == pos_end) { | |
1545 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, bad_include_statement, | |
1546 s.c_str(), act_pos); | |
1547 return; | |
1548 } | |
1549 | |
1550 typename string_type::size_type pos_begin = | |
1551 s.find_last_of(is_system ? '<' : '\"', pos_end-1); | |
1552 | |
1553 if (string_type::npos == pos_begin) { | |
1554 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, bad_include_statement, | |
1555 s.c_str(), act_pos); | |
1556 return; | |
1557 } | |
1558 | |
1559 std::string file_token(s.substr(pos_begin, pos_end-pos_begin+1).c_str()); | |
1560 std::string file_path(s.substr(pos_begin+1, pos_end-pos_begin-1).c_str()); | |
1561 | |
1562 // finally include the file | |
1563 on_include_helper(file_token.c_str(), file_path.c_str(), is_system, | |
1564 include_next); | |
1565 } | |
1566 | |
1567 template <typename ContextT> | |
1568 inline bool | |
1569 pp_iterator_functor<ContextT>::on_include_helper (char const *f, char const *s, | |
1570 bool is_system, bool include_next) | |
1571 { | |
1572 namespace fs = boost::filesystem; | |
1573 | |
1574 // try to locate the given file, searching through the include path lists | |
1575 std::string file_path(s); | |
1576 std::string dir_path; | |
1577 #if BOOST_WAVE_SUPPORT_INCLUDE_NEXT != 0 | |
1578 char const *current_name = include_next ? iter_ctx->real_filename.c_str() : 0; | |
1579 #else | |
1580 char const *current_name = 0; // never try to match current file name | |
1581 #endif | |
1582 | |
1583 // call the 'found_include_directive' hook function | |
1584 #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0 | |
1585 ctx.get_hooks().found_include_directive(f, include_next); | |
1586 #else | |
1587 if (ctx.get_hooks().found_include_directive(ctx.derived(), f, include_next)) | |
1588 return true; // client returned false: skip file to include | |
1589 #endif | |
1590 | |
1591 file_path = util::impl::unescape_lit(file_path); | |
1592 std::string native_path_str; | |
1593 | |
1594 if (!ctx.get_hooks().locate_include_file(ctx, file_path, is_system, | |
1595 current_name, dir_path, native_path_str)) | |
1596 { | |
1597 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, bad_include_file, | |
1598 file_path.c_str(), act_pos); | |
1599 return false; | |
1600 } | |
1601 | |
1602 // test, if this file is known through a #pragma once directive | |
1603 #if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0 | |
1604 if (!ctx.has_pragma_once(native_path_str)) | |
1605 #endif | |
1606 { | |
1607 // the new include file determines the actual current directory | |
1608 ctx.set_current_directory(native_path_str.c_str()); | |
1609 | |
1610 // preprocess the opened file | |
1611 boost::shared_ptr<base_iteration_context_type> new_iter_ctx ( | |
1612 new iteration_context_type(ctx, native_path_str.c_str(), act_pos, | |
1613 boost::wave::enable_prefer_pp_numbers(ctx.get_language()), | |
1614 is_system ? base_iteration_context_type::system_header : | |
1615 base_iteration_context_type::user_header)); | |
1616 | |
1617 // call the include policy trace function | |
1618 #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0 | |
1619 ctx.get_hooks().opened_include_file(dir_path, file_path, | |
1620 ctx.get_iteration_depth(), is_system); | |
1621 #else | |
1622 ctx.get_hooks().opened_include_file(ctx.derived(), dir_path, file_path, | |
1623 is_system); | |
1624 #endif | |
1625 | |
1626 // store current file position | |
1627 iter_ctx->real_relative_filename = ctx.get_current_relative_filename().c_str(); | |
1628 iter_ctx->filename = act_pos.get_file(); | |
1629 iter_ctx->line = act_pos.get_line(); | |
1630 iter_ctx->if_block_depth = ctx.get_if_block_depth(); | |
1631 iter_ctx->emitted_lines = (unsigned int)(-1); // force #line directive | |
1632 | |
1633 // push the old iteration context onto the stack and continue with the new | |
1634 ctx.push_iteration_context(act_pos, iter_ctx); | |
1635 iter_ctx = new_iter_ctx; | |
1636 seen_newline = true; // fake a newline to trigger pp_directive | |
1637 must_emit_line_directive = true; | |
1638 | |
1639 act_pos.set_file(iter_ctx->filename); // initialize file position | |
1640 #if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0 | |
1641 fs::path rfp(wave::util::create_path(iter_ctx->real_filename.c_str())); | |
1642 std::string real_filename(rfp.string()); | |
1643 ctx.set_current_filename(real_filename.c_str()); | |
1644 #endif | |
1645 | |
1646 ctx.set_current_relative_filename(dir_path.c_str()); | |
1647 iter_ctx->real_relative_filename = dir_path.c_str(); | |
1648 | |
1649 act_pos.set_line(iter_ctx->line); | |
1650 act_pos.set_column(0); | |
1651 } | |
1652 return true; | |
1653 } | |
1654 | |
1655 /////////////////////////////////////////////////////////////////////////////// | |
1656 // | |
1657 // on_include(): handle #include ... directives | |
1658 // | |
1659 /////////////////////////////////////////////////////////////////////////////// | |
1660 | |
1661 namespace impl { | |
1662 | |
1663 // trim all whitespace from the beginning and the end of the given string | |
1664 template <typename StringT> | |
1665 inline StringT | |
1666 trim_whitespace(StringT const &s) | |
1667 { | |
1668 typedef typename StringT::size_type size_type; | |
1669 | |
1670 size_type first = s.find_first_not_of(" \t\v\f"); | |
1671 if (StringT::npos == first) | |
1672 return StringT(); | |
1673 size_type last = s.find_last_not_of(" \t\v\f"); | |
1674 return s.substr(first, last-first+1); | |
1675 } | |
1676 } | |
1677 | |
1678 template <typename ContextT> | |
1679 inline void | |
1680 pp_iterator_functor<ContextT>::on_include( | |
1681 typename parse_tree_type::const_iterator const &begin, | |
1682 typename parse_tree_type::const_iterator const &end, bool include_next) | |
1683 { | |
1684 BOOST_ASSERT(ctx.get_if_block_status()); | |
1685 | |
1686 // preprocess the given token sequence (the body of the #include directive) | |
1687 get_token_value<result_type, parse_node_type> get_value; | |
1688 token_sequence_type expanded; | |
1689 token_sequence_type toexpand; | |
1690 | |
1691 std::copy(make_ref_transform_iterator(begin, get_value), | |
1692 make_ref_transform_iterator(end, get_value), | |
1693 std::inserter(toexpand, toexpand.end())); | |
1694 | |
1695 typename token_sequence_type::iterator begin2 = toexpand.begin(); | |
1696 ctx.expand_whole_tokensequence(begin2, toexpand.end(), expanded, | |
1697 false); | |
1698 | |
1699 // now, include the file | |
1700 string_type s (impl::trim_whitespace(boost::wave::util::impl::as_string(expanded))); | |
1701 bool is_system = '<' == s[0] && '>' == s[s.size()-1]; | |
1702 | |
1703 if (!is_system && !('\"' == s[0] && '\"' == s[s.size()-1])) { | |
1704 // should resolve into something like <...> or "..." | |
1705 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, bad_include_statement, | |
1706 s.c_str(), act_pos); | |
1707 return; | |
1708 } | |
1709 on_include(s, is_system, include_next); | |
1710 } | |
1711 | |
1712 /////////////////////////////////////////////////////////////////////////////// | |
1713 // | |
1714 // on_define(): handle #define directives | |
1715 // | |
1716 /////////////////////////////////////////////////////////////////////////////// | |
1717 | |
1718 template <typename ContextT> | |
1719 inline void | |
1720 pp_iterator_functor<ContextT>::on_define (parse_node_type const &node) | |
1721 { | |
1722 BOOST_ASSERT(ctx.get_if_block_status()); | |
1723 | |
1724 // retrieve the macro definition from the parse tree | |
1725 result_type macroname; | |
1726 std::vector<result_type> macroparameters; | |
1727 token_sequence_type macrodefinition; | |
1728 bool has_parameters = false; | |
1729 position_type pos(act_token.get_position()); | |
1730 | |
1731 if (!boost::wave::util::retrieve_macroname(ctx, node, | |
1732 BOOST_WAVE_PLAIN_DEFINE_ID, macroname, pos, false)) | |
1733 return; | |
1734 has_parameters = boost::wave::util::retrieve_macrodefinition(node, | |
1735 BOOST_WAVE_MACRO_PARAMETERS_ID, macroparameters, pos, false); | |
1736 boost::wave::util::retrieve_macrodefinition(node, | |
1737 BOOST_WAVE_MACRO_DEFINITION_ID, macrodefinition, pos, false); | |
1738 | |
1739 if (has_parameters) { | |
1740 #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0 | |
1741 if (boost::wave::need_variadics(ctx.get_language())) { | |
1742 // test whether ellipsis are given, and if yes, if these are placed as the | |
1743 // last argument, test if __VA_ARGS__ is used as a macro parameter name | |
1744 using namespace cpplexer; | |
1745 typedef typename std::vector<result_type>::iterator | |
1746 parameter_iterator_t; | |
1747 | |
1748 bool seen_ellipses = false; | |
1749 parameter_iterator_t end = macroparameters.end(); | |
1750 for (parameter_iterator_t pit = macroparameters.begin(); | |
1751 pit != end; ++pit) | |
1752 { | |
1753 if (seen_ellipses) { | |
1754 // ellipses are not the last given formal argument | |
1755 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, | |
1756 bad_define_statement, macroname.get_value().c_str(), | |
1757 (*pit).get_position()); | |
1758 return; | |
1759 } | |
1760 if (T_ELLIPSIS == token_id(*pit)) | |
1761 seen_ellipses = true; | |
1762 | |
1763 // can't use __VA_ARGS__ as a argument name | |
1764 if ("__VA_ARGS__" == (*pit).get_value()) { | |
1765 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, | |
1766 bad_define_statement_va_args, | |
1767 macroname.get_value().c_str(), (*pit).get_position()); | |
1768 return; | |
1769 } | |
1770 } | |
1771 | |
1772 // if there wasn't an ellipsis, then there shouldn't be a __VA_ARGS__ | |
1773 // placeholder in the definition too [C99 Standard 6.10.3.5] | |
1774 if (!seen_ellipses) { | |
1775 typedef typename token_sequence_type::iterator definition_iterator_t; | |
1776 | |
1777 bool seen_va_args = false; | |
1778 definition_iterator_t pend = macrodefinition.end(); | |
1779 for (definition_iterator_t dit = macrodefinition.begin(); | |
1780 dit != pend; ++dit) | |
1781 { | |
1782 if (T_IDENTIFIER == token_id(*dit) && | |
1783 "__VA_ARGS__" == (*dit).get_value()) | |
1784 { | |
1785 seen_va_args = true; | |
1786 } | |
1787 } | |
1788 if (seen_va_args) { | |
1789 // must not have seen __VA_ARGS__ placeholder | |
1790 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, | |
1791 bad_define_statement_va_args, | |
1792 macroname.get_value().c_str(), act_token.get_position()); | |
1793 return; | |
1794 } | |
1795 } | |
1796 } | |
1797 else | |
1798 #endif // BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0 | |
1799 { | |
1800 // test, that there is no T_ELLIPSES given | |
1801 using namespace cpplexer; | |
1802 typedef typename std::vector<result_type>::iterator | |
1803 parameter_iterator_t; | |
1804 | |
1805 parameter_iterator_t end = macroparameters.end(); | |
1806 for (parameter_iterator_t pit = macroparameters.begin(); | |
1807 pit != end; ++pit) | |
1808 { | |
1809 if (T_ELLIPSIS == token_id(*pit)) { | |
1810 // if variadics are disabled, no ellipses should be given | |
1811 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, | |
1812 bad_define_statement, macroname.get_value().c_str(), | |
1813 (*pit).get_position()); | |
1814 return; | |
1815 } | |
1816 } | |
1817 } | |
1818 } | |
1819 | |
1820 // add the new macro to the macromap | |
1821 ctx.add_macro_definition(macroname, has_parameters, macroparameters, | |
1822 macrodefinition); | |
1823 } | |
1824 | |
1825 /////////////////////////////////////////////////////////////////////////////// | |
1826 // | |
1827 // on_undefine(): handle #undef directives | |
1828 // | |
1829 /////////////////////////////////////////////////////////////////////////////// | |
1830 template <typename ContextT> | |
1831 inline void | |
1832 pp_iterator_functor<ContextT>::on_undefine (lexer_type const &it) | |
1833 { | |
1834 BOOST_ASSERT(ctx.get_if_block_status()); | |
1835 | |
1836 // retrieve the macro name to undefine from the parse tree | |
1837 ctx.remove_macro_definition((*it).get_value()); // throws for predefined macros | |
1838 } | |
1839 | |
1840 /////////////////////////////////////////////////////////////////////////////// | |
1841 // | |
1842 // on_ifdef(): handle #ifdef directives | |
1843 // | |
1844 /////////////////////////////////////////////////////////////////////////////// | |
1845 template <typename ContextT> | |
1846 inline void | |
1847 pp_iterator_functor<ContextT>::on_ifdef( | |
1848 result_type const& found_directive, lexer_type const &it) | |
1849 // typename parse_tree_type::const_iterator const &it) | |
1850 // typename parse_tree_type::const_iterator const &end) | |
1851 { | |
1852 // get_token_value<result_type, parse_node_type> get_value; | |
1853 // token_sequence_type toexpand; | |
1854 // | |
1855 // std::copy(make_ref_transform_iterator((*begin).children.begin(), get_value), | |
1856 // make_ref_transform_iterator((*begin).children.end(), get_value), | |
1857 // std::inserter(toexpand, toexpand.end())); | |
1858 | |
1859 bool is_defined = false; | |
1860 token_sequence_type directive; | |
1861 | |
1862 directive.insert(directive.end(), *it); | |
1863 | |
1864 #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0 | |
1865 is_defined = ctx.is_defined_macro((*it).get_value()); // toexpand.begin(), toexpand.end()); | |
1866 ctx.get_hooks().evaluated_conditional_expression(directive, is_defined); | |
1867 #else | |
1868 do { | |
1869 is_defined = ctx.is_defined_macro((*it).get_value()); // toexpand.begin(), toexpand.end()); | |
1870 } while (ctx.get_hooks().evaluated_conditional_expression(ctx.derived(), | |
1871 found_directive, directive, is_defined)); | |
1872 #endif | |
1873 ctx.enter_if_block(is_defined); | |
1874 } | |
1875 | |
1876 /////////////////////////////////////////////////////////////////////////////// | |
1877 // | |
1878 // on_ifndef(): handle #ifndef directives | |
1879 // | |
1880 /////////////////////////////////////////////////////////////////////////////// | |
1881 template <typename ContextT> | |
1882 inline void | |
1883 pp_iterator_functor<ContextT>::on_ifndef( | |
1884 result_type const& found_directive, lexer_type const &it) | |
1885 // typename parse_tree_type::const_iterator const &it) | |
1886 // typename parse_tree_type::const_iterator const &end) | |
1887 { | |
1888 // get_token_value<result_type, parse_node_type> get_value; | |
1889 // token_sequence_type toexpand; | |
1890 // | |
1891 // std::copy(make_ref_transform_iterator((*begin).children.begin(), get_value), | |
1892 // make_ref_transform_iterator((*begin).children.end(), get_value), | |
1893 // std::inserter(toexpand, toexpand.end())); | |
1894 | |
1895 bool is_defined = false; | |
1896 token_sequence_type directive; | |
1897 | |
1898 directive.insert(directive.end(), *it); | |
1899 | |
1900 #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0 | |
1901 is_defined = ctx.is_defined_macro((*it).get_value()); // toexpand.begin(), toexpand.end()); | |
1902 ctx.get_hooks().evaluated_conditional_expression(directive, is_defined); | |
1903 #else | |
1904 do { | |
1905 is_defined = ctx.is_defined_macro((*it).get_value()); // toexpand.begin(), toexpand.end()); | |
1906 } while (ctx.get_hooks().evaluated_conditional_expression(ctx.derived(), | |
1907 found_directive, directive, is_defined)); | |
1908 #endif | |
1909 ctx.enter_if_block(!is_defined); | |
1910 } | |
1911 | |
1912 /////////////////////////////////////////////////////////////////////////////// | |
1913 // | |
1914 // on_else(): handle #else directives | |
1915 // | |
1916 /////////////////////////////////////////////////////////////////////////////// | |
1917 template <typename ContextT> | |
1918 inline void | |
1919 pp_iterator_functor<ContextT>::on_else() | |
1920 { | |
1921 if (!ctx.enter_else_block()) { | |
1922 // #else without matching #if | |
1923 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, missing_matching_if, | |
1924 "#else", act_pos); | |
1925 } | |
1926 } | |
1927 | |
1928 /////////////////////////////////////////////////////////////////////////////// | |
1929 // | |
1930 // on_endif(): handle #endif directives | |
1931 // | |
1932 /////////////////////////////////////////////////////////////////////////////// | |
1933 template <typename ContextT> | |
1934 inline void | |
1935 pp_iterator_functor<ContextT>::on_endif() | |
1936 { | |
1937 if (!ctx.exit_if_block()) { | |
1938 // #endif without matching #if | |
1939 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, missing_matching_if, | |
1940 "#endif", act_pos); | |
1941 } | |
1942 } | |
1943 | |
1944 /////////////////////////////////////////////////////////////////////////////// | |
1945 // replace all remaining (== undefined) identifiers with an integer literal '0' | |
1946 template <typename ContextT> | |
1947 inline void | |
1948 pp_iterator_functor<ContextT>::replace_undefined_identifiers( | |
1949 token_sequence_type &expanded) | |
1950 { | |
1951 typename token_sequence_type::iterator exp_end = expanded.end(); | |
1952 for (typename token_sequence_type::iterator exp_it = expanded.begin(); | |
1953 exp_it != exp_end; ++exp_it) | |
1954 { | |
1955 using namespace boost::wave; | |
1956 | |
1957 token_id id = token_id(*exp_it); | |
1958 if (IS_CATEGORY(id, IdentifierTokenType) || | |
1959 IS_CATEGORY(id, KeywordTokenType)) | |
1960 { | |
1961 (*exp_it).set_token_id(T_INTLIT); | |
1962 (*exp_it).set_value("0"); | |
1963 } | |
1964 } | |
1965 } | |
1966 | |
1967 /////////////////////////////////////////////////////////////////////////////// | |
1968 // | |
1969 // on_if(): handle #if directives | |
1970 // | |
1971 /////////////////////////////////////////////////////////////////////////////// | |
1972 template <typename ContextT> | |
1973 inline void | |
1974 pp_iterator_functor<ContextT>::on_if( | |
1975 result_type const& found_directive, | |
1976 typename parse_tree_type::const_iterator const &begin, | |
1977 typename parse_tree_type::const_iterator const &end) | |
1978 { | |
1979 // preprocess the given sequence into the provided list | |
1980 get_token_value<result_type, parse_node_type> get_value; | |
1981 token_sequence_type toexpand; | |
1982 | |
1983 std::copy(make_ref_transform_iterator(begin, get_value), | |
1984 make_ref_transform_iterator(end, get_value), | |
1985 std::inserter(toexpand, toexpand.end())); | |
1986 | |
1987 impl::remove_leading_whitespace(ctx, toexpand); | |
1988 | |
1989 bool if_status = false; | |
1990 grammars::value_error status = grammars::error_noerror; | |
1991 token_sequence_type expanded; | |
1992 | |
1993 do { | |
1994 expanded.clear(); | |
1995 | |
1996 typename token_sequence_type::iterator begin2 = toexpand.begin(); | |
1997 ctx.expand_whole_tokensequence(begin2, toexpand.end(), expanded); | |
1998 | |
1999 // replace all remaining (== undefined) identifiers with an integer literal '0' | |
2000 replace_undefined_identifiers(expanded); | |
2001 | |
2002 #if BOOST_WAVE_DUMP_CONDITIONAL_EXPRESSIONS != 0 | |
2003 { | |
2004 string_type outstr(boost::wave::util::impl::as_string(toexpand)); | |
2005 outstr += "(" + boost::wave::util::impl::as_string(expanded) + ")"; | |
2006 BOOST_WAVE_DUMP_CONDITIONAL_EXPRESSIONS_OUT << "#if " << outstr | |
2007 << std::endl; | |
2008 } | |
2009 #endif | |
2010 try { | |
2011 // parse the expression and enter the #if block | |
2012 if_status = grammars::expression_grammar_gen<result_type>:: | |
2013 evaluate(expanded.begin(), expanded.end(), act_pos, | |
2014 ctx.get_if_block_status(), status); | |
2015 } | |
2016 catch (boost::wave::preprocess_exception const& e) { | |
2017 // any errors occurred have to be dispatched to the context hooks | |
2018 ctx.get_hooks().throw_exception(ctx.derived(), e); | |
2019 break; | |
2020 } | |
2021 | |
2022 #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0 | |
2023 ctx.get_hooks().evaluated_conditional_expression(toexpand, if_status); | |
2024 } while (false); | |
2025 #else | |
2026 } while (ctx.get_hooks().evaluated_conditional_expression(ctx.derived(), | |
2027 found_directive, toexpand, if_status) | |
2028 && status == grammars::error_noerror); | |
2029 #endif | |
2030 | |
2031 ctx.enter_if_block(if_status); | |
2032 if (grammars::error_noerror != status) { | |
2033 // division or other error by zero occurred | |
2034 string_type expression = util::impl::as_string(expanded); | |
2035 if (0 == expression.size()) | |
2036 expression = "<empty expression>"; | |
2037 | |
2038 if (grammars::error_division_by_zero & status) { | |
2039 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, division_by_zero, | |
2040 expression.c_str(), act_pos); | |
2041 } | |
2042 else if (grammars::error_integer_overflow & status) { | |
2043 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, integer_overflow, | |
2044 expression.c_str(), act_pos); | |
2045 } | |
2046 else if (grammars::error_character_overflow & status) { | |
2047 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, | |
2048 character_literal_out_of_range, expression.c_str(), act_pos); | |
2049 } | |
2050 } | |
2051 } | |
2052 | |
2053 /////////////////////////////////////////////////////////////////////////////// | |
2054 // | |
2055 // on_elif(): handle #elif directives | |
2056 // | |
2057 /////////////////////////////////////////////////////////////////////////////// | |
2058 template <typename ContextT> | |
2059 inline void | |
2060 pp_iterator_functor<ContextT>::on_elif( | |
2061 result_type const& found_directive, | |
2062 typename parse_tree_type::const_iterator const &begin, | |
2063 typename parse_tree_type::const_iterator const &end) | |
2064 { | |
2065 // preprocess the given sequence into the provided list | |
2066 get_token_value<result_type, parse_node_type> get_value; | |
2067 token_sequence_type toexpand; | |
2068 | |
2069 std::copy(make_ref_transform_iterator(begin, get_value), | |
2070 make_ref_transform_iterator(end, get_value), | |
2071 std::inserter(toexpand, toexpand.end())); | |
2072 | |
2073 impl::remove_leading_whitespace(ctx, toexpand); | |
2074 | |
2075 // check current if block status | |
2076 if (ctx.get_if_block_some_part_status()) { | |
2077 if (!ctx.enter_elif_block(false)) { | |
2078 // #else without matching #if | |
2079 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, | |
2080 missing_matching_if, "#elif", act_pos); | |
2081 // fall through... | |
2082 } | |
2083 | |
2084 // skip all the expression and the trailing whitespace | |
2085 typename token_sequence_type::iterator begin2 = toexpand.begin(); | |
2086 | |
2087 impl::skip_to_eol(ctx, begin2, toexpand.end()); | |
2088 return; // one of previous #if/#elif was true, so don't enter this #elif | |
2089 } | |
2090 | |
2091 // preprocess the given sequence into the provided list | |
2092 bool if_status = false; | |
2093 grammars::value_error status = grammars::error_noerror; | |
2094 token_sequence_type expanded; | |
2095 | |
2096 do { | |
2097 expanded.clear(); | |
2098 | |
2099 typename token_sequence_type::iterator begin2 = toexpand.begin(); | |
2100 ctx.expand_whole_tokensequence(begin2, toexpand.end(), expanded); | |
2101 | |
2102 // replace all remaining (== undefined) identifiers with an integer literal '0' | |
2103 replace_undefined_identifiers(expanded); | |
2104 | |
2105 #if BOOST_WAVE_DUMP_CONDITIONAL_EXPRESSIONS != 0 | |
2106 { | |
2107 string_type outstr(boost::wave::util::impl::as_string(toexpand)); | |
2108 outstr += "(" + boost::wave::util::impl::as_string(expanded) + ")"; | |
2109 BOOST_WAVE_DUMP_CONDITIONAL_EXPRESSIONS_OUT << "#elif " << outstr << std::endl; | |
2110 } | |
2111 #endif | |
2112 | |
2113 try { | |
2114 // parse the expression and enter the #elif block | |
2115 if_status = grammars::expression_grammar_gen<result_type>:: | |
2116 evaluate(expanded.begin(), expanded.end(), act_pos, | |
2117 ctx.get_if_block_status(), status); | |
2118 } | |
2119 catch (boost::wave::preprocess_exception const& e) { | |
2120 // any errors occurred have to be dispatched to the context hooks | |
2121 ctx.get_hooks().throw_exception(ctx.derived(), e); | |
2122 } | |
2123 | |
2124 #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0 | |
2125 ctx.get_hooks().evaluated_conditional_expression(toexpand, if_status); | |
2126 } while (false); | |
2127 #else | |
2128 } while (ctx.get_hooks().evaluated_conditional_expression(ctx.derived(), | |
2129 found_directive, toexpand, if_status) | |
2130 && status == grammars::error_noerror); | |
2131 #endif | |
2132 | |
2133 if (!ctx.enter_elif_block(if_status)) { | |
2134 // #elif without matching #if | |
2135 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, missing_matching_if, | |
2136 "#elif", act_pos); | |
2137 return; | |
2138 } | |
2139 | |
2140 if (grammars::error_noerror != status) { | |
2141 // division or other error by zero occurred | |
2142 string_type expression = util::impl::as_string(expanded); | |
2143 if (0 == expression.size()) | |
2144 expression = "<empty expression>"; | |
2145 | |
2146 if (grammars::error_division_by_zero & status) { | |
2147 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, division_by_zero, | |
2148 expression.c_str(), act_pos); | |
2149 } | |
2150 else if (grammars::error_integer_overflow & status) { | |
2151 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, | |
2152 integer_overflow, expression.c_str(), act_pos); | |
2153 } | |
2154 else if (grammars::error_character_overflow & status) { | |
2155 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, | |
2156 character_literal_out_of_range, expression.c_str(), act_pos); | |
2157 } | |
2158 } | |
2159 } | |
2160 | |
2161 /////////////////////////////////////////////////////////////////////////////// | |
2162 // | |
2163 // on_illformed(): handles the illegal directive | |
2164 // | |
2165 /////////////////////////////////////////////////////////////////////////////// | |
2166 template <typename ContextT> | |
2167 inline void | |
2168 pp_iterator_functor<ContextT>::on_illformed( | |
2169 typename result_type::string_type s) | |
2170 { | |
2171 BOOST_ASSERT(ctx.get_if_block_status()); | |
2172 | |
2173 // some messages have more than one newline at the end | |
2174 typename string_type::size_type p = s.find_last_not_of('\n'); | |
2175 if (string_type::npos != p) | |
2176 s = s.substr(0, p+1); | |
2177 | |
2178 // throw the exception | |
2179 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, ill_formed_directive, | |
2180 s.c_str(), act_pos); | |
2181 } | |
2182 | |
2183 /////////////////////////////////////////////////////////////////////////////// | |
2184 // | |
2185 // on_line(): handle #line directives | |
2186 // | |
2187 /////////////////////////////////////////////////////////////////////////////// | |
2188 | |
2189 namespace impl { | |
2190 | |
2191 template <typename IteratorT, typename StringT> | |
2192 bool retrieve_line_info (IteratorT first, IteratorT const &last, | |
2193 unsigned int &line, StringT &file, | |
2194 boost::wave::preprocess_exception::error_code& error) | |
2195 { | |
2196 using namespace boost::wave; | |
2197 token_id id = token_id(*first); | |
2198 if (T_PP_NUMBER == id || T_INTLIT == id) { | |
2199 // extract line number | |
2200 using namespace std; // some systems have atoi in namespace std | |
2201 line = (unsigned int)atoi((*first).get_value().c_str()); | |
2202 if (0 == line) | |
2203 error = preprocess_exception::bad_line_number; | |
2204 | |
2205 // re-extract line number with spirit to diagnose overflow | |
2206 using namespace boost::spirit::classic; | |
2207 if (!parse((*first).get_value().c_str(), int_p).full) | |
2208 error = preprocess_exception::bad_line_number; | |
2209 | |
2210 // extract file name (if it is given) | |
2211 while (++first != last && IS_CATEGORY(*first, WhiteSpaceTokenType)) | |
2212 /**/; // skip whitespace | |
2213 | |
2214 if (first != last) { | |
2215 if (T_STRINGLIT != token_id(*first)) { | |
2216 error = preprocess_exception::bad_line_filename; | |
2217 return false; | |
2218 } | |
2219 | |
2220 StringT const &file_lit = (*first).get_value(); | |
2221 | |
2222 if ('L' == file_lit[0]) { | |
2223 error = preprocess_exception::bad_line_filename; | |
2224 return false; // shouldn't be a wide character string | |
2225 } | |
2226 | |
2227 file = file_lit.substr(1, file_lit.size()-2); | |
2228 | |
2229 // test if there is other junk on this line | |
2230 while (++first != last && IS_CATEGORY(*first, WhiteSpaceTokenType)) | |
2231 /**/; // skip whitespace | |
2232 } | |
2233 return first == last; | |
2234 } | |
2235 error = preprocess_exception::bad_line_statement; | |
2236 return false; | |
2237 } | |
2238 } | |
2239 | |
2240 template <typename ContextT> | |
2241 inline void | |
2242 pp_iterator_functor<ContextT>::on_line( | |
2243 typename parse_tree_type::const_iterator const &begin, | |
2244 typename parse_tree_type::const_iterator const &end) | |
2245 { | |
2246 BOOST_ASSERT(ctx.get_if_block_status()); | |
2247 | |
2248 // Try to extract the line number and file name from the given token list | |
2249 // directly. If that fails, preprocess the whole token sequence and try again | |
2250 // to extract this information. | |
2251 token_sequence_type expanded; | |
2252 get_token_value<result_type, parse_node_type> get_value; | |
2253 | |
2254 typedef typename ref_transform_iterator_generator< | |
2255 get_token_value<result_type, parse_node_type>, | |
2256 typename parse_tree_type::const_iterator | |
2257 >::type const_tree_iterator_t; | |
2258 | |
2259 const_tree_iterator_t first = make_ref_transform_iterator(begin, get_value); | |
2260 const_tree_iterator_t last = make_ref_transform_iterator(end, get_value); | |
2261 | |
2262 // try to interpret the #line body as a number followed by an optional | |
2263 // string literal | |
2264 unsigned int line = 0; | |
2265 preprocess_exception::error_code error = preprocess_exception::no_error; | |
2266 string_type file_name; | |
2267 token_sequence_type toexpand; | |
2268 | |
2269 std::copy(first, last, std::inserter(toexpand, toexpand.end())); | |
2270 if (!impl::retrieve_line_info(first, last, line, file_name, error)) { | |
2271 // preprocess the body of this #line message | |
2272 typename token_sequence_type::iterator begin2 = toexpand.begin(); | |
2273 ctx.expand_whole_tokensequence(begin2, toexpand.end(), | |
2274 expanded, false); | |
2275 | |
2276 error = preprocess_exception::no_error; | |
2277 if (!impl::retrieve_line_info(expanded.begin(), expanded.end(), | |
2278 line, file_name, error)) | |
2279 { | |
2280 typename ContextT::string_type msg( | |
2281 boost::wave::util::impl::as_string(expanded)); | |
2282 BOOST_WAVE_THROW_VAR_CTX(ctx, preprocess_exception, error, | |
2283 msg.c_str(), act_pos); | |
2284 return; | |
2285 } | |
2286 | |
2287 // call the corresponding pp hook function | |
2288 ctx.get_hooks().found_line_directive(ctx.derived(), expanded, line, | |
2289 file_name.c_str()); | |
2290 } | |
2291 else { | |
2292 // call the corresponding pp hook function | |
2293 ctx.get_hooks().found_line_directive(ctx.derived(), toexpand, line, | |
2294 file_name.c_str()); | |
2295 } | |
2296 | |
2297 // the queues should be empty at this point | |
2298 BOOST_ASSERT(unput_queue.empty()); | |
2299 BOOST_ASSERT(pending_queue.empty()); | |
2300 | |
2301 // make sure error recovery starts on the next line | |
2302 must_emit_line_directive = true; | |
2303 | |
2304 // diagnose possible error in detected line directive | |
2305 if (error != preprocess_exception::no_error) { | |
2306 typename ContextT::string_type msg( | |
2307 boost::wave::util::impl::as_string(expanded)); | |
2308 BOOST_WAVE_THROW_VAR_CTX(ctx, preprocess_exception, error, | |
2309 msg.c_str(), act_pos); | |
2310 return; | |
2311 } | |
2312 | |
2313 // set new line number/filename only if ok | |
2314 if (!file_name.empty()) { // reuse current file name | |
2315 using boost::wave::util::impl::unescape_lit; | |
2316 act_pos.set_file(unescape_lit(file_name).c_str()); | |
2317 } | |
2318 act_pos.set_line(line); | |
2319 iter_ctx->first.set_position(act_pos); | |
2320 } | |
2321 | |
2322 /////////////////////////////////////////////////////////////////////////////// | |
2323 // | |
2324 // on_error(): handle #error directives | |
2325 // | |
2326 /////////////////////////////////////////////////////////////////////////////// | |
2327 template <typename ContextT> | |
2328 inline void | |
2329 pp_iterator_functor<ContextT>::on_error( | |
2330 typename parse_tree_type::const_iterator const &begin, | |
2331 typename parse_tree_type::const_iterator const &end) | |
2332 { | |
2333 BOOST_ASSERT(ctx.get_if_block_status()); | |
2334 | |
2335 // preprocess the given sequence into the provided list | |
2336 token_sequence_type expanded; | |
2337 get_token_value<result_type, parse_node_type> get_value; | |
2338 | |
2339 typename ref_transform_iterator_generator< | |
2340 get_token_value<result_type, parse_node_type>, | |
2341 typename parse_tree_type::const_iterator | |
2342 >::type first = make_ref_transform_iterator(begin, get_value); | |
2343 | |
2344 #if BOOST_WAVE_PREPROCESS_ERROR_MESSAGE_BODY != 0 | |
2345 // preprocess the body of this #error message | |
2346 token_sequence_type toexpand; | |
2347 | |
2348 std::copy(first, make_ref_transform_iterator(end, get_value), | |
2349 std::inserter(toexpand, toexpand.end())); | |
2350 | |
2351 typename token_sequence_type::iterator begin2 = toexpand.begin(); | |
2352 ctx.expand_whole_tokensequence(begin2, toexpand.end(), expanded, | |
2353 false); | |
2354 if (!ctx.get_hooks().found_error_directive(ctx.derived(), toexpand)) | |
2355 #else | |
2356 // simply copy the body of this #error message to the issued diagnostic | |
2357 // message | |
2358 std::copy(first, make_ref_transform_iterator(end, get_value), | |
2359 std::inserter(expanded, expanded.end())); | |
2360 if (!ctx.get_hooks().found_error_directive(ctx.derived(), expanded)) | |
2361 #endif | |
2362 { | |
2363 // report the corresponding error | |
2364 BOOST_WAVE_STRINGTYPE msg(boost::wave::util::impl::as_string(expanded)); | |
2365 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, error_directive, | |
2366 msg.c_str(), act_pos); | |
2367 } | |
2368 } | |
2369 | |
2370 #if BOOST_WAVE_SUPPORT_WARNING_DIRECTIVE != 0 | |
2371 /////////////////////////////////////////////////////////////////////////////// | |
2372 // | |
2373 // on_warning(): handle #warning directives | |
2374 // | |
2375 /////////////////////////////////////////////////////////////////////////////// | |
2376 template <typename ContextT> | |
2377 inline void | |
2378 pp_iterator_functor<ContextT>::on_warning( | |
2379 typename parse_tree_type::const_iterator const &begin, | |
2380 typename parse_tree_type::const_iterator const &end) | |
2381 { | |
2382 BOOST_ASSERT(ctx.get_if_block_status()); | |
2383 | |
2384 // preprocess the given sequence into the provided list | |
2385 token_sequence_type expanded; | |
2386 get_token_value<result_type, parse_node_type> get_value; | |
2387 | |
2388 typename ref_transform_iterator_generator< | |
2389 get_token_value<result_type, parse_node_type>, | |
2390 typename parse_tree_type::const_iterator | |
2391 >::type first = make_ref_transform_iterator(begin, get_value); | |
2392 | |
2393 #if BOOST_WAVE_PREPROCESS_ERROR_MESSAGE_BODY != 0 | |
2394 // preprocess the body of this #warning message | |
2395 token_sequence_type toexpand; | |
2396 | |
2397 std::copy(first, make_ref_transform_iterator(end, get_value), | |
2398 std::inserter(toexpand, toexpand.end())); | |
2399 | |
2400 typename token_sequence_type::iterator begin2 = toexpand.begin(); | |
2401 ctx.expand_whole_tokensequence(begin2, toexpand.end(), expanded, | |
2402 false); | |
2403 if (!ctx.get_hooks().found_warning_directive(ctx.derived(), toexpand)) | |
2404 #else | |
2405 // simply copy the body of this #warning message to the issued diagnostic | |
2406 // message | |
2407 std::copy(first, make_ref_transform_iterator(end, get_value), | |
2408 std::inserter(expanded, expanded.end())); | |
2409 if (!ctx.get_hooks().found_warning_directive(ctx.derived(), expanded)) | |
2410 #endif | |
2411 { | |
2412 // report the corresponding error | |
2413 BOOST_WAVE_STRINGTYPE msg(boost::wave::util::impl::as_string(expanded)); | |
2414 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, warning_directive, | |
2415 msg.c_str(), act_pos); | |
2416 } | |
2417 } | |
2418 #endif // BOOST_WAVE_SUPPORT_WARNING_DIRECTIVE != 0 | |
2419 | |
2420 /////////////////////////////////////////////////////////////////////////////// | |
2421 // | |
2422 // on_pragma(): handle #pragma directives | |
2423 // | |
2424 /////////////////////////////////////////////////////////////////////////////// | |
2425 template <typename ContextT> | |
2426 inline bool | |
2427 pp_iterator_functor<ContextT>::on_pragma( | |
2428 typename parse_tree_type::const_iterator const &begin, | |
2429 typename parse_tree_type::const_iterator const &end) | |
2430 { | |
2431 using namespace boost::wave; | |
2432 | |
2433 BOOST_ASSERT(ctx.get_if_block_status()); | |
2434 | |
2435 // Look at the pragma token sequence and decide, if the first token is STDC | |
2436 // (see C99 standard [6.10.6.2]), if it is, the sequence must _not_ be | |
2437 // preprocessed. | |
2438 token_sequence_type expanded; | |
2439 get_token_value<result_type, parse_node_type> get_value; | |
2440 | |
2441 typedef typename ref_transform_iterator_generator< | |
2442 get_token_value<result_type, parse_node_type>, | |
2443 typename parse_tree_type::const_iterator | |
2444 >::type const_tree_iterator_t; | |
2445 | |
2446 const_tree_iterator_t first = make_ref_transform_iterator(begin, get_value); | |
2447 const_tree_iterator_t last = make_ref_transform_iterator(end, get_value); | |
2448 | |
2449 expanded.push_back(result_type(T_PP_PRAGMA, "#pragma", act_token.get_position())); | |
2450 expanded.push_back(result_type(T_SPACE, " ", act_token.get_position())); | |
2451 | |
2452 while (++first != last && IS_CATEGORY(*first, WhiteSpaceTokenType)) | |
2453 expanded.push_back(*first); // skip whitespace | |
2454 | |
2455 if (first != last) { | |
2456 if (T_IDENTIFIER == token_id(*first) && | |
2457 boost::wave::need_c99(ctx.get_language()) && | |
2458 (*first).get_value() == "STDC") | |
2459 { | |
2460 // do _not_ preprocess the token sequence | |
2461 std::copy(first, last, std::inserter(expanded, expanded.end())); | |
2462 } | |
2463 else { | |
2464 #if BOOST_WAVE_PREPROCESS_PRAGMA_BODY != 0 | |
2465 // preprocess the given tokensequence | |
2466 token_sequence_type toexpand; | |
2467 | |
2468 std::copy(first, last, std::inserter(toexpand, toexpand.end())); | |
2469 | |
2470 typename token_sequence_type::iterator begin2 = toexpand.begin(); | |
2471 ctx.expand_whole_tokensequence(begin2, toexpand.end(), | |
2472 expanded, false); | |
2473 #else | |
2474 // do _not_ preprocess the token sequence | |
2475 std::copy(first, last, std::inserter(expanded, expanded.end())); | |
2476 #endif | |
2477 } | |
2478 } | |
2479 expanded.push_back(result_type(T_NEWLINE, "\n", act_token.get_position())); | |
2480 | |
2481 // the queues should be empty at this point | |
2482 BOOST_ASSERT(unput_queue.empty()); | |
2483 BOOST_ASSERT(pending_queue.empty()); | |
2484 | |
2485 // try to interpret the expanded #pragma body | |
2486 token_sequence_type pending; | |
2487 if (interpret_pragma(expanded, pending)) { | |
2488 // if there is some replacement text, insert it into the pending queue | |
2489 if (!pending.empty()) | |
2490 pending_queue.splice(pending_queue.begin(), pending); | |
2491 return true; // this #pragma was successfully recognized | |
2492 } | |
2493 | |
2494 #if BOOST_WAVE_EMIT_PRAGMA_DIRECTIVES != 0 | |
2495 // Move the resulting token sequence into the pending_queue, so it will be | |
2496 // returned to the caller. | |
2497 if (boost::wave::need_emit_pragma_directives(ctx.get_language())) { | |
2498 pending_queue.splice(pending_queue.begin(), expanded); | |
2499 return false; // return the whole #pragma directive | |
2500 } | |
2501 #endif | |
2502 return true; // skip the #pragma at all | |
2503 } | |
2504 | |
2505 template <typename ContextT> | |
2506 inline bool | |
2507 pp_iterator_functor<ContextT>::interpret_pragma( | |
2508 token_sequence_type const &pragma_body, token_sequence_type &result) | |
2509 { | |
2510 using namespace cpplexer; | |
2511 | |
2512 typename token_sequence_type::const_iterator end = pragma_body.end(); | |
2513 typename token_sequence_type::const_iterator it = pragma_body.begin(); | |
2514 for (++it; it != end && IS_CATEGORY(*it, WhiteSpaceTokenType); ++it) | |
2515 /**/; // skip whitespace | |
2516 | |
2517 if (it == end) // eof reached | |
2518 return false; | |
2519 | |
2520 return boost::wave::util::interpret_pragma( | |
2521 ctx.derived(), act_token, it, end, result); | |
2522 } | |
2523 | |
2524 /////////////////////////////////////////////////////////////////////////////// | |
2525 } // namespace impl | |
2526 | |
2527 /////////////////////////////////////////////////////////////////////////////// | |
2528 // | |
2529 // pp_iterator | |
2530 // | |
2531 // The boost::wave::pp_iterator template is the iterator, through which | |
2532 // the resulting preprocessed input stream is accessible. | |
2533 // | |
2534 /////////////////////////////////////////////////////////////////////////////// | |
2535 | |
2536 template <typename ContextT> | |
2537 class pp_iterator | |
2538 : public boost::spirit::classic::multi_pass< | |
2539 boost::wave::impl::pp_iterator_functor<ContextT>, | |
2540 boost::wave::util::functor_input | |
2541 > | |
2542 { | |
2543 public: | |
2544 typedef boost::wave::impl::pp_iterator_functor<ContextT> input_policy_type; | |
2545 | |
2546 private: | |
2547 typedef | |
2548 boost::spirit::classic::multi_pass<input_policy_type, boost::wave::util::functor_input> | |
2549 base_type; | |
2550 typedef pp_iterator<ContextT> self_type; | |
2551 typedef boost::wave::util::functor_input functor_input_type; | |
2552 | |
2553 public: | |
2554 pp_iterator() | |
2555 {} | |
2556 | |
2557 template <typename IteratorT> | |
2558 pp_iterator(ContextT &ctx, IteratorT const &first, IteratorT const &last, | |
2559 typename ContextT::position_type const &pos) | |
2560 : base_type(input_policy_type(ctx, first, last, pos)) | |
2561 {} | |
2562 | |
2563 bool force_include(char const *path_, bool is_last) | |
2564 { | |
2565 bool result = this->get_functor().on_include_helper(path_, path_, | |
2566 false, false); | |
2567 if (is_last) { | |
2568 this->functor_input_type:: | |
2569 template inner<input_policy_type>::advance_input(); | |
2570 } | |
2571 return result; | |
2572 } | |
2573 }; | |
2574 | |
2575 /////////////////////////////////////////////////////////////////////////////// | |
2576 } // namespace wave | |
2577 } // namespace boost | |
2578 | |
2579 // the suffix header occurs after all of the code | |
2580 #ifdef BOOST_HAS_ABI_HEADERS | |
2581 #include BOOST_ABI_SUFFIX | |
2582 #endif | |
2583 | |
2584 #endif // !defined(CPP_ITERATOR_HPP_175CA88F_7273_43FA_9039_BCF7459E1F29_INCLUDED) |