cpp_iterator.hpp 93 KB

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