cpp_iterator.hpp 94 KB

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