1 /*=============================================================================
2 Boost.Wave: A Standard compliant C++ preprocessor library
4 Definition of the preprocessor iterator
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 =============================================================================*/
13 #if !defined(CPP_ITERATOR_HPP_175CA88F_7273_43FA_9039_BCF7459E1F29_INCLUDED)
14 #define CPP_ITERATOR_HPP_175CA88F_7273_43FA_9039_BCF7459E1F29_INCLUDED
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>
29 #include <boost/wave/wave_config.hpp>
30 #include <boost/pool/pool_alloc.hpp>
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>
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>
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>
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
56 ///////////////////////////////////////////////////////////////////////////////
61 ///////////////////////////////////////////////////////////////////////////////
62 // retrieve the macro name from the parse tree
64 typename ContextT, typename ParseNodeT, typename TokenT,
68 retrieve_macroname(ContextT& ctx, ParseNodeT const &node,
69 boost::spirit::classic::parser_id id, TokenT ¯oname, PositionT& act_pos,
72 ParseNodeT const *name_node = 0;
74 using boost::spirit::classic::find_node;
75 if (!find_node(node, id, &name_node))
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);
83 typename ParseNodeT::children_t const &children = name_node->children;
85 if (0 == children.size() ||
86 children.front().value.begin() == children.front().value.end())
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);
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());
103 ///////////////////////////////////////////////////////////////////////////////
104 // retrieve the macro parameters or the macro definition from the parse tree
105 template <typename ParseNodeT, typename ContainerT, typename PositionT>
107 retrieve_macrodefinition(
108 ParseNodeT const &node, boost::spirit::classic::parser_id id,
109 ContainerT ¯odefinition, PositionT& act_pos, bool update_position)
111 using namespace boost::wave;
112 typedef typename ParseNodeT::const_tree_iterator const_tree_iterator;
114 // find macro parameters/macro definition inside the parse tree
115 std::pair<const_tree_iterator, const_tree_iterator> nodes;
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;
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());
128 if (!IS_CATEGORY(macrodefinition.back(), WhiteSpaceTokenType) &&
129 T_NEWLINE != token_id(macrodefinition.back()) &&
130 T_EOF != token_id(macrodefinition.back()))
132 last_nonwhite = inserted;
135 if (update_position) {
136 (*inserted).set_position(act_pos);
138 act_pos.get_column() + (*inserted).get_value().size());
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());
149 macrodefinition.erase(++last_nonwhite, macrodefinition.end());
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)
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>
169 using namespace boost::wave;
170 using namespace std; // isspace is in std namespace for some systems
172 // skip leading whitespace
173 std::string::iterator begin = macrostring.begin();
174 std::string::iterator end = macrostring.end();
176 while(begin != end && isspace(*begin))
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());
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);
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;
197 if (!boost::wave::util::retrieve_macroname(ctx, *hit.trees.begin(),
198 BOOST_WAVE_PLAIN_DEFINE_ID, macroname, act_pos, true))
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);
205 // get rid of trailing T_EOF
206 if (!macrodefinition.empty() && token_id(macrodefinition.back()) == T_EOF)
207 macrodefinition.pop_back();
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));
214 // add the new macro to the macromap
215 return ctx.add_macro_definition(macroname, has_parameters, macroparameters,
216 macrodefinition, is_predefined);
218 #endif // BOOST_WAVE_ENABLE_COMMANDLINE_MACROS != 0
220 ///////////////////////////////////////////////////////////////////////////////
223 ///////////////////////////////////////////////////////////////////////////////
224 // forward declaration
225 template <typename ContextT> class pp_iterator;
229 ///////////////////////////////////////////////////////////////////////////////
231 // pp_iterator_functor
233 ///////////////////////////////////////////////////////////////////////////////
234 template <typename ContextT>
235 class pp_iterator_functor {
238 // interface to the boost::spirit::classic::iterator_policies::functor_input policy
239 typedef typename ContextT::token_type result_type;
242 static result_type const eof;
245 // type of a token sequence
246 typedef typename ContextT::token_sequence_type token_sequence_type;
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>
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;
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
271 template <typename IteratorT>
272 pp_iterator_functor(ContextT &ctx_, IteratorT const &first_,
273 IteratorT const &last_, typename ContextT::position_type const &pos_)
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())),
279 pos_.get_file().c_str()
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()))
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());
289 iter_ctx->emitted_lines = (unsigned int)(-1); // force #line directive
292 // get the next preprocessed token
293 result_type const &operator()();
295 // get the last recognized token (for error processing etc.)
296 result_type const ¤t_token() const { return act_token; }
299 friend class pp_iterator<ContextT>;
300 bool on_include_helper(char const *t, char const *s, bool is_system,
304 result_type const &get_next_token();
305 result_type const &pp_token();
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);
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);
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);
326 void on_define(parse_node_type const &node);
327 void on_undefine(lexer_type const &it);
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);
335 void on_illformed(typename result_type::string_type s);
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);
351 bool on_pragma(typename parse_tree_type::const_iterator const &begin,
352 typename parse_tree_type::const_iterator const &end);
354 bool emit_line_directive();
355 bool returned_from_include();
357 bool interpret_pragma(token_sequence_type const &pragma_body,
358 token_sequence_type &result);
361 ContextT &ctx; // context, this iterator is associated with
362 boost::shared_ptr<base_iteration_context_type> iter_ctx;
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)
370 token_sequence_type unput_queue; // tokens to be preprocessed again
371 token_sequence_type pending_queue; // tokens already preprocessed
373 // detect whether to insert additional whitespace in between two adjacent
374 // tokens, which otherwise would form a different token type, if
376 boost::wave::util::insert_whitespace_detection whitespace;
379 ///////////////////////////////////////////////////////////////////////////////
381 template <typename ContextT>
382 typename pp_iterator_functor<ContextT>::result_type const
383 pp_iterator_functor<ContextT>::eof;
385 ///////////////////////////////////////////////////////////////////////////////
387 // returned_from_include()
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.
393 ///////////////////////////////////////////////////////////////////////////////
394 template <typename ContextT>
396 pp_iterator_functor<ContextT>::returned_from_include()
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();
403 ctx.get_hooks().returning_from_include_file(ctx.derived());
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);
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);
419 iter_ctx = ctx.pop_iteration_context();
421 must_emit_line_directive = true;
422 iter_ctx->emitted_lines = (unsigned int)(-1); // force #line directive
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);
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());
437 ctx.set_current_directory(iter_ctx->real_filename.c_str());
438 ctx.set_current_relative_filename(iter_ctx->real_relative_filename.c_str());
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);
453 ///////////////////////////////////////////////////////////////////////////////
455 // operator()(): get the next preprocessed token
457 // throws a preprocess_exception, if appropriate
459 ///////////////////////////////////////////////////////////////////////////////
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
467 template <typename ContextT>
468 bool consider_emitting_line_directive(ContextT const& ctx, token_id id)
470 if (need_preserve_comments(ctx.get_language()))
472 if (!IS_CATEGORY(id, EOLTokenType) && !IS_CATEGORY(id, EOFTokenType))
477 if (!IS_CATEGORY(id, WhiteSpaceTokenType) &&
478 !IS_CATEGORY(id, EOLTokenType) && !IS_CATEGORY(id, EOFTokenType))
486 template <typename ContextT>
487 inline typename pp_iterator_functor<ContextT>::result_type const &
488 pp_iterator_functor<ContextT>::operator()()
490 using namespace boost::wave;
492 // make sure the cwd has been initialized
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;
500 try { // catch lexer exceptions
502 if (skipped_newline) {
503 was_skipped_newline = true;
504 skipped_newline = false;
507 // get_next_token assigns result to act_token member
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)))
515 act_token.set_token_id(id = T_NEWLINE);
516 act_token.set_value("\n");
519 if (IS_CATEGORY(id, EOLTokenType))
522 } while (ctx.get_hooks().may_skip_whitespace(ctx.derived(), act_token, skipped_newline));
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);
530 // restore the accumulated skipped_newline state for next invocation
531 if (was_skipped_newline)
532 skipped_newline = true;
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))
538 // must emit a #line directive
539 if (need_emit_line_directives(ctx.get_language()) && emit_line_directive())
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);
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);
554 case T_GENERATEDNEWLINE: // was generated by emit_line_directive()
555 act_token.set_token_id(id = T_NEWLINE);
556 ++iter_ctx->emitted_lines;
563 ++iter_ctx->emitted_lines;
566 #if BOOST_WAVE_SUPPORT_CPP0X != 0
568 iter_ctx->emitted_lines +=
569 context_policies::util::rawstring_count_newlines(act_token);
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);
578 case T_PP_NUMBER: // re-tokenize the pp-number
580 token_sequence_type rescanned;
582 std::string pp_number(
583 util::to_string<std::string>(act_token.get_value()));
585 lexer_type it = lexer_type(pp_number.begin(),
586 pp_number.end(), act_token.get_position(),
588 lexer_type end = lexer_type();
590 for (/**/; it != end && T_EOF != token_id(*it); ++it)
591 rescanned.push_back(*it);
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();
604 default: // make sure whitespace at line begin keeps seen_newline status
605 if (IS_CATEGORY(id, WhiteSpaceTokenType))
606 seen_newline = was_seen_newline;
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());
619 whitespace.shift_tokens(id);
620 return ctx.get_hooks().generated_token(ctx.derived(), act_token);
623 ///////////////////////////////////////////////////////////////////////////////
624 template <typename ContextT>
625 inline typename pp_iterator_functor<ContextT>::result_type const &
626 pp_iterator_functor<ContextT>::get_next_token()
628 using namespace boost::wave;
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
635 // test for EOF, if there is a pending input context, pop it back and continue
637 bool returned_from_include_file = returned_from_include();
639 // try to generate the next token
640 if (iter_ctx->first != iter_ctx->last) {
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);
649 return act_token = pending_queue.front();
652 // adjust the current position (line and column)
653 bool was_seen_newline = seen_newline || returned_from_include_file;
655 // fetch the current token
656 act_token = *iter_ctx->first;
657 act_pos = act_token.get_position();
659 // act accordingly on the current token
660 token_id id = token_id(act_token);
663 // returned from an include file, continue with the next token
664 whitespace.shift_tokens(T_EOF);
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()))
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);
675 continue; // if this is the main file, the while loop breaks
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)
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);
691 seen_newline = false;
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;
699 // loop to the next token to analyze
700 // simply fall through, since the iterator was already adjusted
703 else if (ctx.get_if_block_status()) {
704 // preprocess this token, eat up more, if appropriate, return
705 // the next preprocessed token
709 // compilation condition is false: if the current token is a
710 // newline, account for it, otherwise discard the actual token and
712 if (T_NEWLINE == token_id(act_token)) {
714 must_emit_line_directive = true;
718 util::impl::call_skipped_token_hook(ctx, act_token);
722 } while ((iter_ctx->first != iter_ctx->last) ||
723 (returned_from_include_file = returned_from_include()));
725 // overall eof reached
726 if (ctx.get_if_block_depth() > 0 && !need_single_line(ctx.get_language()))
728 // missing endif directive(s)
729 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
730 missing_matching_endif, "", act_pos);
734 act_token = eof; // this is the last token
737 // whitespace.shift_tokens(T_EOF); // whitespace controller
738 return act_token; // return eof token
741 ///////////////////////////////////////////////////////////////////////////////
743 // emit_line_directive(): emits a line directive from the act_token data
745 ///////////////////////////////////////////////////////////////////////////////
746 template <typename ContextT>
748 pp_iterator_functor<ContextT>::emit_line_directive()
750 using namespace boost::wave;
752 typename ContextT::position_type pos = act_token.get_position();
754 // if (must_emit_line_directive &&
755 // iter_ctx->emitted_lines+1 == act_pos.get_line() &&
756 // iter_ctx->filename == act_pos.get_file())
758 // must_emit_line_directive = false;
762 if (must_emit_line_directive ||
763 iter_ctx->emitted_lines+1 != act_pos.get_line())
765 // unput the current token
766 pending_queue.push_front(act_token);
767 pos.set_line(act_pos.get_line());
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);
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;
779 token_sequence_type pending;
781 if (!ctx.get_hooks().emit_line_directive(ctx, pending, act_token))
783 unsigned int column = 6;
785 // the hook did not generate anything, emit default #line
787 pending.push_back(result_type(T_PP_LINE, "#line", pos));
789 pos.set_column(column); // account for '#line'
790 pending.push_back(result_type(T_SPACE, " ", pos));
792 // 21 is the max required size for a 64 bit integer represented as a
796 using namespace std; // for some systems sprintf is in namespace std
797 sprintf (buffer, "%ld", pos.get_line());
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 ' '
805 std::string file("\"");
806 boost::filesystem::path filename(
807 wave::util::create_path(act_pos.get_file().c_str()));
809 using wave::util::impl::escape_lit;
810 file += escape_lit(wave::util::native_file_string(filename)) + "\"";
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));
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();
825 must_emit_line_directive = false; // we are now in sync
829 must_emit_line_directive = false; // we are now in sync
833 ///////////////////////////////////////////////////////////////////////////////
835 // pptoken(): return the next preprocessed token
837 ///////////////////////////////////////////////////////////////////////////////
838 template <typename ContextT>
839 inline typename pp_iterator_functor<ContextT>::result_type const &
840 pp_iterator_functor<ContextT>::pp_token()
842 using namespace boost::wave;
844 token_id id = token_id(*iter_ctx->first);
846 // eat all T_PLACEHOLDER tokens, eventually slipped through out of the
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();
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))
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);
867 // simply return the next token
868 act_token = *iter_ctx->first;
871 id = token_id(act_token);
873 } while (T_PLACEHOLDER == id);
877 ///////////////////////////////////////////////////////////////////////////////
879 // pp_directive(): recognize a preprocessor directive
881 ///////////////////////////////////////////////////////////////////////////////
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)
889 #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
890 ctx.get_hooks().found_directive(found_directive);
892 if (ctx.get_hooks().found_directive(ctx.derived(), found_directive))
893 return true; // skip this directive and return newline only
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)
903 // #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
904 // ctx.get_hooks().skipped_token(skipped);
906 // ctx.get_hooks().skipped_token(ctx.derived(), skipped);
910 template <typename ContextT, typename IteratorT>
911 bool next_token_is_pp_directive(ContextT &ctx, IteratorT &it, IteratorT const &end)
913 using namespace boost::wave;
915 token_id id = T_UNKNOWN;
916 for (/**/; it != end; ++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))
928 // this token gets skipped
929 util::impl::call_skipped_token_hook(ctx, *it);
931 BOOST_ASSERT(it == end || id != T_UNKNOWN);
932 return it != end && IS_CATEGORY(id, PPTokenType);
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)
940 using namespace boost::wave;
942 // this token gets skipped
944 util::impl::call_skipped_token_hook(ctx, *it);
946 for (++it; it != end; ++it) {
947 token_id id = token_id(*it);
949 if (T_CPPCOMMENT == id || T_NEWLINE == id ||
950 context_policies::util::ccomment_has_newline(*it))
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
958 if (!IS_CATEGORY(id, WhiteSpaceTokenType))
961 // this token gets skipped
963 util::impl::call_skipped_token_hook(ctx, *it);
968 ///////////////////////////////////////////////////////////////////////////
969 template <typename ContextT, typename IteratorT>
970 bool skip_to_eol(ContextT &ctx, IteratorT &it, IteratorT const &end,
971 bool call_hook = true)
973 using namespace boost::wave;
975 for (/**/; it != end; ++it) {
976 token_id id = token_id(*it);
978 if (T_CPPCOMMENT == id || T_NEWLINE == id ||
979 context_policies::util::ccomment_has_newline(*it))
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
988 util::impl::call_skipped_token_hook(ctx, *it);
993 ///////////////////////////////////////////////////////////////////////////
994 template <typename ContextT, typename ContainerT>
996 remove_leading_whitespace(ContextT &ctx, ContainerT& c, bool call_hook = true)
998 typename ContainerT::iterator it = c.begin();
999 while (IS_CATEGORY(*it, WhiteSpaceTokenType)) {
1000 typename ContainerT::iterator save = it++;
1002 util::impl::call_skipped_token_hook(ctx, *save);
1008 ///////////////////////////////////////////////////////////////////////////////
1009 template <typename ContextT>
1010 template <typename IteratorT>
1012 pp_iterator_functor<ContextT>::extract_identifier(IteratorT &it)
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))
1019 IteratorT save = it;
1020 if (impl::pp_is_last_on_line(ctx, save, iter_ctx->last, false))
1024 // report the ill formed directive
1025 impl::skip_to_eol(ctx, it, iter_ctx->last);
1027 string_type str(util::impl::as_string<string_type>(iter_ctx->first, it));
1029 seen_newline = true;
1030 iter_ctx->first = it;
1035 ///////////////////////////////////////////////////////////////////////////////
1036 template <typename ContextT>
1037 template <typename IteratorT>
1039 pp_iterator_functor<ContextT>::ensure_is_last_on_line(IteratorT& it, bool call_hook)
1041 if (!impl::pp_is_last_on_line(ctx, it, iter_ctx->last, call_hook))
1043 // enable error recovery (start over with the next line)
1044 impl::skip_to_eol(ctx, it, iter_ctx->last);
1046 string_type str(util::impl::as_string<string_type>(
1047 iter_ctx->first, it));
1049 seen_newline = true;
1050 iter_ctx->first = it;
1052 // report an invalid directive
1057 if (it == iter_ctx->last && !need_single_line(ctx.get_language()))
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;
1063 // Trigger a warning that the last line was not terminated with a
1065 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
1066 last_line_not_terminated, "", act_pos);
1073 template <typename ContextT>
1074 template <typename IteratorT>
1076 pp_iterator_functor<ContextT>::skip_to_eol_with_check(IteratorT &it, bool call_hook)
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()))
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;
1086 // Trigger a warning, that the last line was not terminated with a
1088 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
1089 last_line_not_terminated, "", act_pos);
1093 // normal line ending reached, adjust iterator and flag
1094 seen_newline = true;
1095 iter_ctx->first = it;
1099 ///////////////////////////////////////////////////////////////////////////////
1100 // handle_pp_directive: handle certain pp_directives
1101 template <typename ContextT>
1102 template <typename IteratorT>
1104 pp_iterator_functor<ContextT>::handle_pp_directive(IteratorT &it)
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);
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
1129 can_exit = false; // #elif is not always safe to skip
1133 case T_PP_ELSE: // #else
1134 case T_PP_ENDIF: // #endif
1136 // handle this directive
1137 if (T_PP_ELSE == token_id(*it))
1142 // make sure, there are no (non-whitespace) tokens left on
1144 ensure_is_last_on_line(it);
1146 // we skipped to the end of this line already
1147 seen_newline = true;
1148 iter_ctx->first = it;
1152 default: // #something else
1153 on_illformed((*it).get_value());
1158 util::impl::call_skipped_token_hook(ctx, *it);
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:
1171 include_next = (T_PP_QHEADER_NEXT == id) ? true : false;
1172 if (!impl::call_found_directive_hook(ctx, *it))
1174 string_type dir((*it).get_value());
1176 // make sure, there are no (non-whitespace) tokens left on
1178 if (ensure_is_last_on_line(it))
1180 seen_newline = true;
1181 iter_ctx->first = it;
1182 on_include (dir, false, include_next);
1188 case T_PP_HHEADER: // #include <...>
1189 #if BOOST_WAVE_SUPPORT_INCLUDE_NEXT != 0
1190 case T_PP_HHEADER_NEXT:
1192 include_next = (T_PP_HHEADER_NEXT == id) ? true : false;
1193 if (!impl::call_found_directive_hook(ctx, *it))
1195 string_type dir((*it).get_value());
1197 // make sure, there are no (non-whitespace) tokens left on
1199 if (ensure_is_last_on_line(it))
1201 seen_newline = true;
1202 iter_ctx->first = it;
1203 on_include (dir, true, include_next);
1209 case T_PP_ELSE: // #else
1210 case T_PP_ENDIF: // #endif
1211 if (!impl::call_found_directive_hook(ctx, *it))
1213 // handle this directive
1214 if (T_PP_ELSE == token_id(*it))
1219 // make sure, there are no (non-whitespace) tokens left on
1221 ensure_is_last_on_line(it);
1223 // we skipped to the end of this line already
1224 seen_newline = true;
1225 iter_ctx->first = it;
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
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))
1246 call_hook_in_skip = false;
1249 case T_PP_IFDEF: // #ifdef
1250 if (!impl::call_found_directive_hook(ctx, *it) &&
1251 extract_identifier(it))
1253 on_ifdef(directive, it);
1255 call_hook_in_skip = false;
1258 case T_PP_IFNDEF: // #ifndef
1259 if (!impl::call_found_directive_hook(ctx, *it) &&
1260 extract_identifier(it))
1262 on_ifndef(directive, it);
1264 call_hook_in_skip = false;
1267 #if BOOST_WAVE_SUPPORT_MS_EXTENSIONS != 0
1268 // case T_MSEXT_PP_REGION: // #region ...
1271 // case T_MSEXT_PP_ENDREGION: // #endregion
1281 // start over with the next line, if only possible
1283 skip_to_eol_with_check(it, call_hook_in_skip);
1284 return true; // may be safely ignored
1286 return false; // do not ignore this pp directive
1289 ///////////////////////////////////////////////////////////////////////////////
1290 // pp_directive(): recognize a preprocessor directive
1291 template <typename ContextT>
1293 pp_iterator_functor<ContextT>::pp_directive()
1295 using namespace cpplexer;
1297 // test, if the next non-whitespace token is a pp directive
1298 lexer_type it = iter_ctx->first;
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;
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;
1314 string_type str(boost::wave::util::impl::as_string<string_type>(
1315 iter_ctx->first, it));
1317 token_sequence_type faulty_line;
1319 for (/**/; iter_ctx->first != it; ++iter_ctx->first)
1320 faulty_line.push_back(*iter_ctx->first);
1322 token_sequence_type pending;
1323 if (ctx.get_hooks().found_unknown_directive(ctx, faulty_line, pending))
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);
1331 // default behavior is to throw an exception
1336 // this line does not contain a pp directive, so simply return
1341 if (it == iter_ctx->last)
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
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;
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);
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;
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);
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
1375 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
1376 last_line_not_terminated, "", act_pos);
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;
1385 string_type str(boost::wave::util::impl::as_string<string_type>(
1386 iter_ctx->first, it));
1387 iter_ctx->first = it;
1389 // report the ill formed directive
1395 ///////////////////////////////////////////////////////////////////////////////
1397 // dispatch_directive(): dispatch a recognized preprocessor directive
1399 ///////////////////////////////////////////////////////////////////////////////
1400 template <typename ContextT>
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)
1406 using namespace cpplexer;
1408 typedef typename parse_tree_type::const_iterator const_child_iterator_t;
1410 // this iterator points to the root node of the parse tree
1411 const_child_iterator_t begin = hit.trees.begin();
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();
1418 const_child_iterator_t begin_child_it = (*root.begin()).children.begin();
1419 const_child_iterator_t end_child_it = (*root.begin()).children.end();
1421 token_id id = token_id(found_directive);
1423 // call preprocessing hook
1424 if (impl::call_found_directive_hook(ctx, found_directive))
1425 return true; // skip this directive and return newline only
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 "..."
1432 // on_include ((*nodeval.begin()).get_value(), false,
1433 // T_PP_QHEADER_NEXT == id);
1436 // case T_PP_HHEADER: // #include <...>
1437 // #if BOOST_WAVE_SUPPORT_INCLUDE_NEXT != 0
1438 // case T_PP_HHEADER_NEXT: // #include_next <...>
1440 // on_include ((*nodeval.begin()).get_value(), true,
1441 // T_PP_HHEADER_NEXT == id);
1444 case T_PP_INCLUDE: // #include ...
1445 #if BOOST_WAVE_SUPPORT_INCLUDE_NEXT != 0
1446 case T_PP_INCLUDE_NEXT: // #include_next ...
1448 on_include (begin_child_it, end_child_it, T_PP_INCLUDE_NEXT == id);
1451 case T_PP_DEFINE: // #define
1455 // case T_PP_UNDEF: // #undef
1456 // on_undefine(*nodeval.begin());
1459 // case T_PP_IFDEF: // #ifdef
1460 // on_ifdef(found_directive, begin_child_it, end_child_it);
1463 // case T_PP_IFNDEF: // #ifndef
1464 // on_ifndef(found_directive, begin_child_it, end_child_it);
1467 case T_PP_IF: // #if
1468 on_if(found_directive, begin_child_it, end_child_it);
1471 case T_PP_ELIF: // #elif
1472 on_elif(found_directive, begin_child_it, end_child_it);
1475 // case T_PP_ELSE: // #else
1479 // case T_PP_ENDIF: // #endif
1483 case T_PP_LINE: // #line
1484 on_line(begin_child_it, end_child_it);
1487 case T_PP_ERROR: // #error
1488 on_error(begin_child_it, end_child_it);
1491 #if BOOST_WAVE_SUPPORT_WARNING_DIRECTIVE != 0
1492 case T_PP_WARNING: // #warning
1493 on_warning(begin_child_it, end_child_it);
1497 case T_PP_PRAGMA: // #pragma
1498 return on_pragma(begin_child_it, end_child_it);
1500 #if BOOST_WAVE_SUPPORT_MS_EXTENSIONS != 0
1501 case T_MSEXT_PP_REGION:
1502 case T_MSEXT_PP_ENDREGION:
1503 break; // ignore these
1506 default: // #something else
1507 on_illformed((*nodeval.begin()).get_value());
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
1512 token_sequence_type expanded;
1513 get_token_value<result_type, parse_node_type> get_value;
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);
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
1529 ///////////////////////////////////////////////////////////////////////////////
1531 // on_include: handle #include <...> or #include "..." directives
1533 ///////////////////////////////////////////////////////////////////////////////
1534 template <typename ContextT>
1536 pp_iterator_functor<ContextT>::on_include (string_type const &s,
1537 bool is_system, bool include_next)
1539 BOOST_ASSERT(ctx.get_if_block_status());
1541 // strip quotes first, extract filename
1542 typename string_type::size_type pos_end = s.find_last_of(is_system ? '>' : '\"');
1544 if (string_type::npos == pos_end) {
1545 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, bad_include_statement,
1546 s.c_str(), act_pos);
1550 typename string_type::size_type pos_begin =
1551 s.find_last_of(is_system ? '<' : '\"', pos_end-1);
1553 if (string_type::npos == pos_begin) {
1554 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, bad_include_statement,
1555 s.c_str(), act_pos);
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());
1562 // finally include the file
1563 on_include_helper(file_token.c_str(), file_path.c_str(), is_system,
1567 template <typename ContextT>
1569 pp_iterator_functor<ContextT>::on_include_helper (char const *f, char const *s,
1570 bool is_system, bool include_next)
1572 namespace fs = boost::filesystem;
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;
1580 char const *current_name = 0; // never try to match current file name
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);
1587 if (ctx.get_hooks().found_include_directive(ctx.derived(), f, include_next))
1588 return true; // client returned false: skip file to include
1591 file_path = util::impl::unescape_lit(file_path);
1592 std::string native_path_str;
1594 if (!ctx.get_hooks().locate_include_file(ctx, file_path, is_system,
1595 current_name, dir_path, native_path_str))
1597 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, bad_include_file,
1598 file_path.c_str(), act_pos);
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))
1607 // the new include file determines the actual current directory
1608 ctx.set_current_directory(native_path_str.c_str());
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));
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);
1622 ctx.get_hooks().opened_include_file(ctx.derived(), dir_path, file_path,
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
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;
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());
1646 ctx.set_current_relative_filename(dir_path.c_str());
1647 iter_ctx->real_relative_filename = dir_path.c_str();
1649 act_pos.set_line(iter_ctx->line);
1650 act_pos.set_column(0);
1655 ///////////////////////////////////////////////////////////////////////////////
1657 // on_include(): handle #include ... directives
1659 ///////////////////////////////////////////////////////////////////////////////
1663 // trim all whitespace from the beginning and the end of the given string
1664 template <typename StringT>
1666 trim_whitespace(StringT const &s)
1668 typedef typename StringT::size_type size_type;
1670 size_type first = s.find_first_not_of(" \t\v\f");
1671 if (StringT::npos == first)
1673 size_type last = s.find_last_not_of(" \t\v\f");
1674 return s.substr(first, last-first+1);
1678 template <typename ContextT>
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)
1684 BOOST_ASSERT(ctx.get_if_block_status());
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;
1691 std::copy(make_ref_transform_iterator(begin, get_value),
1692 make_ref_transform_iterator(end, get_value),
1693 std::inserter(toexpand, toexpand.end()));
1695 typename token_sequence_type::iterator begin2 = toexpand.begin();
1696 ctx.expand_whole_tokensequence(begin2, toexpand.end(), expanded,
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];
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);
1709 on_include(s, is_system, include_next);
1712 ///////////////////////////////////////////////////////////////////////////////
1714 // on_define(): handle #define directives
1716 ///////////////////////////////////////////////////////////////////////////////
1718 template <typename ContextT>
1720 pp_iterator_functor<ContextT>::on_define (parse_node_type const &node)
1722 BOOST_ASSERT(ctx.get_if_block_status());
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());
1731 if (!boost::wave::util::retrieve_macroname(ctx, node,
1732 BOOST_WAVE_PLAIN_DEFINE_ID, macroname, pos, false))
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);
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;
1748 bool seen_ellipses = false;
1749 parameter_iterator_t end = macroparameters.end();
1750 for (parameter_iterator_t pit = macroparameters.begin();
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());
1760 if (T_ELLIPSIS == token_id(*pit))
1761 seen_ellipses = true;
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());
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;
1777 bool seen_va_args = false;
1778 definition_iterator_t pend = macrodefinition.end();
1779 for (definition_iterator_t dit = macrodefinition.begin();
1782 if (T_IDENTIFIER == token_id(*dit) &&
1783 "__VA_ARGS__" == (*dit).get_value())
1785 seen_va_args = true;
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());
1798 #endif // BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
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;
1805 parameter_iterator_t end = macroparameters.end();
1806 for (parameter_iterator_t pit = macroparameters.begin();
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());
1820 // add the new macro to the macromap
1821 ctx.add_macro_definition(macroname, has_parameters, macroparameters,
1825 ///////////////////////////////////////////////////////////////////////////////
1827 // on_undefine(): handle #undef directives
1829 ///////////////////////////////////////////////////////////////////////////////
1830 template <typename ContextT>
1832 pp_iterator_functor<ContextT>::on_undefine (lexer_type const &it)
1834 BOOST_ASSERT(ctx.get_if_block_status());
1836 // retrieve the macro name to undefine from the parse tree
1837 ctx.remove_macro_definition((*it).get_value()); // throws for predefined macros
1840 ///////////////////////////////////////////////////////////////////////////////
1842 // on_ifdef(): handle #ifdef directives
1844 ///////////////////////////////////////////////////////////////////////////////
1845 template <typename ContextT>
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)
1852 // get_token_value<result_type, parse_node_type> get_value;
1853 // token_sequence_type toexpand;
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()));
1859 bool is_defined = false;
1860 token_sequence_type directive;
1862 directive.insert(directive.end(), *it);
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);
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));
1873 ctx.enter_if_block(is_defined);
1876 ///////////////////////////////////////////////////////////////////////////////
1878 // on_ifndef(): handle #ifndef directives
1880 ///////////////////////////////////////////////////////////////////////////////
1881 template <typename ContextT>
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)
1888 // get_token_value<result_type, parse_node_type> get_value;
1889 // token_sequence_type toexpand;
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()));
1895 bool is_defined = false;
1896 token_sequence_type directive;
1898 directive.insert(directive.end(), *it);
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);
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));
1909 ctx.enter_if_block(!is_defined);
1912 ///////////////////////////////////////////////////////////////////////////////
1914 // on_else(): handle #else directives
1916 ///////////////////////////////////////////////////////////////////////////////
1917 template <typename ContextT>
1919 pp_iterator_functor<ContextT>::on_else()
1921 if (!ctx.enter_else_block()) {
1922 // #else without matching #if
1923 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, missing_matching_if,
1928 ///////////////////////////////////////////////////////////////////////////////
1930 // on_endif(): handle #endif directives
1932 ///////////////////////////////////////////////////////////////////////////////
1933 template <typename ContextT>
1935 pp_iterator_functor<ContextT>::on_endif()
1937 if (!ctx.exit_if_block()) {
1938 // #endif without matching #if
1939 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, missing_matching_if,
1944 ///////////////////////////////////////////////////////////////////////////////
1945 // replace all remaining (== undefined) identifiers with an integer literal '0'
1946 template <typename ContextT>
1948 pp_iterator_functor<ContextT>::replace_undefined_identifiers(
1949 token_sequence_type &expanded)
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)
1955 using namespace boost::wave;
1957 token_id id = token_id(*exp_it);
1958 if (IS_CATEGORY(id, IdentifierTokenType) ||
1959 IS_CATEGORY(id, KeywordTokenType))
1961 (*exp_it).set_token_id(T_INTLIT);
1962 (*exp_it).set_value("0");
1967 ///////////////////////////////////////////////////////////////////////////////
1969 // on_if(): handle #if directives
1971 ///////////////////////////////////////////////////////////////////////////////
1972 template <typename ContextT>
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)
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;
1983 std::copy(make_ref_transform_iterator(begin, get_value),
1984 make_ref_transform_iterator(end, get_value),
1985 std::inserter(toexpand, toexpand.end()));
1987 impl::remove_leading_whitespace(ctx, toexpand);
1989 bool if_status = false;
1990 grammars::value_error status = grammars::error_noerror;
1991 token_sequence_type expanded;
1996 typename token_sequence_type::iterator begin2 = toexpand.begin();
1997 ctx.expand_whole_tokensequence(begin2, toexpand.end(), expanded);
1999 // replace all remaining (== undefined) identifiers with an integer literal '0'
2000 replace_undefined_identifiers(expanded);
2002 #if BOOST_WAVE_DUMP_CONDITIONAL_EXPRESSIONS != 0
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
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);
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);
2022 #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
2023 ctx.get_hooks().evaluated_conditional_expression(toexpand, if_status);
2026 } while (ctx.get_hooks().evaluated_conditional_expression(ctx.derived(),
2027 found_directive, toexpand, if_status)
2028 && status == grammars::error_noerror);
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>";
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);
2042 else if (grammars::error_integer_overflow & status) {
2043 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, integer_overflow,
2044 expression.c_str(), act_pos);
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);
2053 ///////////////////////////////////////////////////////////////////////////////
2055 // on_elif(): handle #elif directives
2057 ///////////////////////////////////////////////////////////////////////////////
2058 template <typename ContextT>
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)
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;
2069 std::copy(make_ref_transform_iterator(begin, get_value),
2070 make_ref_transform_iterator(end, get_value),
2071 std::inserter(toexpand, toexpand.end()));
2073 impl::remove_leading_whitespace(ctx, toexpand);
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);
2084 // skip all the expression and the trailing whitespace
2085 typename token_sequence_type::iterator begin2 = toexpand.begin();
2087 impl::skip_to_eol(ctx, begin2, toexpand.end());
2088 return; // one of previous #if/#elif was true, so don't enter this #elif
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;
2099 typename token_sequence_type::iterator begin2 = toexpand.begin();
2100 ctx.expand_whole_tokensequence(begin2, toexpand.end(), expanded);
2102 // replace all remaining (== undefined) identifiers with an integer literal '0'
2103 replace_undefined_identifiers(expanded);
2105 #if BOOST_WAVE_DUMP_CONDITIONAL_EXPRESSIONS != 0
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;
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);
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);
2124 #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
2125 ctx.get_hooks().evaluated_conditional_expression(toexpand, if_status);
2128 } while (ctx.get_hooks().evaluated_conditional_expression(ctx.derived(),
2129 found_directive, toexpand, if_status)
2130 && status == grammars::error_noerror);
2133 if (!ctx.enter_elif_block(if_status)) {
2134 // #elif without matching #if
2135 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, missing_matching_if,
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>";
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);
2150 else if (grammars::error_integer_overflow & status) {
2151 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
2152 integer_overflow, expression.c_str(), act_pos);
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);
2161 ///////////////////////////////////////////////////////////////////////////////
2163 // on_illformed(): handles the illegal directive
2165 ///////////////////////////////////////////////////////////////////////////////
2166 template <typename ContextT>
2168 pp_iterator_functor<ContextT>::on_illformed(
2169 typename result_type::string_type s)
2171 BOOST_ASSERT(ctx.get_if_block_status());
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);
2178 // throw the exception
2179 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, ill_formed_directive,
2180 s.c_str(), act_pos);
2183 ///////////////////////////////////////////////////////////////////////////////
2185 // on_line(): handle #line directives
2187 ///////////////////////////////////////////////////////////////////////////////
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)
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());
2203 error = preprocess_exception::bad_line_number;
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;
2210 // extract file name (if it is given)
2211 while (++first != last && IS_CATEGORY(*first, WhiteSpaceTokenType))
2212 /**/; // skip whitespace
2214 if (first != last) {
2215 if (T_STRINGLIT != token_id(*first)) {
2216 error = preprocess_exception::bad_line_filename;
2220 StringT const &file_lit = (*first).get_value();
2222 if ('L' == file_lit[0]) {
2223 error = preprocess_exception::bad_line_filename;
2224 return false; // shouldn't be a wide character string
2227 file = file_lit.substr(1, file_lit.size()-2);
2229 // test if there is other junk on this line
2230 while (++first != last && IS_CATEGORY(*first, WhiteSpaceTokenType))
2231 /**/; // skip whitespace
2233 return first == last;
2235 error = preprocess_exception::bad_line_statement;
2240 template <typename ContextT>
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)
2246 BOOST_ASSERT(ctx.get_if_block_status());
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;
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;
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);
2262 // try to interpret the #line body as a number followed by an optional
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;
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(),
2276 error = preprocess_exception::no_error;
2277 if (!impl::retrieve_line_info(expanded.begin(), expanded.end(),
2278 line, file_name, error))
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);
2287 // call the corresponding pp hook function
2288 ctx.get_hooks().found_line_directive(ctx.derived(), expanded, line,
2292 // call the corresponding pp hook function
2293 ctx.get_hooks().found_line_directive(ctx.derived(), toexpand, line,
2297 // the queues should be empty at this point
2298 BOOST_ASSERT(unput_queue.empty());
2299 BOOST_ASSERT(pending_queue.empty());
2301 // make sure error recovery starts on the next line
2302 must_emit_line_directive = true;
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);
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());
2318 act_pos.set_line(line);
2319 iter_ctx->first.set_position(act_pos);
2322 ///////////////////////////////////////////////////////////////////////////////
2324 // on_error(): handle #error directives
2326 ///////////////////////////////////////////////////////////////////////////////
2327 template <typename ContextT>
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)
2333 BOOST_ASSERT(ctx.get_if_block_status());
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;
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);
2344 #if BOOST_WAVE_PREPROCESS_ERROR_MESSAGE_BODY != 0
2345 // preprocess the body of this #error message
2346 token_sequence_type toexpand;
2348 std::copy(first, make_ref_transform_iterator(end, get_value),
2349 std::inserter(toexpand, toexpand.end()));
2351 typename token_sequence_type::iterator begin2 = toexpand.begin();
2352 ctx.expand_whole_tokensequence(begin2, toexpand.end(), expanded,
2354 if (!ctx.get_hooks().found_error_directive(ctx.derived(), toexpand))
2356 // simply copy the body of this #error message to the issued diagnostic
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))
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);
2370 #if BOOST_WAVE_SUPPORT_WARNING_DIRECTIVE != 0
2371 ///////////////////////////////////////////////////////////////////////////////
2373 // on_warning(): handle #warning directives
2375 ///////////////////////////////////////////////////////////////////////////////
2376 template <typename ContextT>
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)
2382 BOOST_ASSERT(ctx.get_if_block_status());
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;
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);
2393 #if BOOST_WAVE_PREPROCESS_ERROR_MESSAGE_BODY != 0
2394 // preprocess the body of this #warning message
2395 token_sequence_type toexpand;
2397 std::copy(first, make_ref_transform_iterator(end, get_value),
2398 std::inserter(toexpand, toexpand.end()));
2400 typename token_sequence_type::iterator begin2 = toexpand.begin();
2401 ctx.expand_whole_tokensequence(begin2, toexpand.end(), expanded,
2403 if (!ctx.get_hooks().found_warning_directive(ctx.derived(), toexpand))
2405 // simply copy the body of this #warning message to the issued diagnostic
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))
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);
2418 #endif // BOOST_WAVE_SUPPORT_WARNING_DIRECTIVE != 0
2420 ///////////////////////////////////////////////////////////////////////////////
2422 // on_pragma(): handle #pragma directives
2424 ///////////////////////////////////////////////////////////////////////////////
2425 template <typename ContextT>
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)
2431 using namespace boost::wave;
2433 BOOST_ASSERT(ctx.get_if_block_status());
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
2438 token_sequence_type expanded;
2439 get_token_value<result_type, parse_node_type> get_value;
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;
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);
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()));
2452 while (++first != last && IS_CATEGORY(*first, WhiteSpaceTokenType))
2453 expanded.push_back(*first); // skip whitespace
2455 if (first != last) {
2456 if (T_IDENTIFIER == token_id(*first) &&
2457 boost::wave::need_c99(ctx.get_language()) &&
2458 (*first).get_value() == "STDC")
2460 // do _not_ preprocess the token sequence
2461 std::copy(first, last, std::inserter(expanded, expanded.end()));
2464 #if BOOST_WAVE_PREPROCESS_PRAGMA_BODY != 0
2465 // preprocess the given tokensequence
2466 token_sequence_type toexpand;
2468 std::copy(first, last, std::inserter(toexpand, toexpand.end()));
2470 typename token_sequence_type::iterator begin2 = toexpand.begin();
2471 ctx.expand_whole_tokensequence(begin2, toexpand.end(),
2474 // do _not_ preprocess the token sequence
2475 std::copy(first, last, std::inserter(expanded, expanded.end()));
2479 expanded.push_back(result_type(T_NEWLINE, "\n", act_token.get_position()));
2481 // the queues should be empty at this point
2482 BOOST_ASSERT(unput_queue.empty());
2483 BOOST_ASSERT(pending_queue.empty());
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
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
2502 return true; // skip the #pragma at all
2505 template <typename ContextT>
2507 pp_iterator_functor<ContextT>::interpret_pragma(
2508 token_sequence_type const &pragma_body, token_sequence_type &result)
2510 using namespace cpplexer;
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
2517 if (it == end) // eof reached
2520 return boost::wave::util::interpret_pragma(
2521 ctx.derived(), act_token, it, end, result);
2524 ///////////////////////////////////////////////////////////////////////////////
2527 ///////////////////////////////////////////////////////////////////////////////
2531 // The boost::wave::pp_iterator template is the iterator, through which
2532 // the resulting preprocessed input stream is accessible.
2534 ///////////////////////////////////////////////////////////////////////////////
2536 template <typename ContextT>
2538 : public boost::spirit::classic::multi_pass<
2539 boost::wave::impl::pp_iterator_functor<ContextT>,
2540 boost::wave::util::functor_input
2544 typedef boost::wave::impl::pp_iterator_functor<ContextT> input_policy_type;
2548 boost::spirit::classic::multi_pass<input_policy_type, boost::wave::util::functor_input>
2550 typedef pp_iterator<ContextT> self_type;
2551 typedef boost::wave::util::functor_input functor_input_type;
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))
2563 bool force_include(char const *path_, bool is_last)
2565 bool result = this->get_functor().on_include_helper(path_, path_,
2568 this->functor_input_type::
2569 template inner<input_policy_type>::advance_input();
2575 ///////////////////////////////////////////////////////////////////////////////
2577 } // namespace boost
2579 // the suffix header occurs after all of the code
2580 #ifdef BOOST_HAS_ABI_HEADERS
2581 #include BOOST_ABI_SUFFIX
2584 #endif // !defined(CPP_ITERATOR_HPP_175CA88F_7273_43FA_9039_BCF7459E1F29_INCLUDED)