cpp_iterator.hpp 94 KB

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