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