cpp_iterator.hpp 94 KB

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