cpp_macromap.hpp 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944
  1. /*=============================================================================
  2. Boost.Wave: A Standard compliant C++ preprocessor library
  3. Macro expansion engine
  4. http://www.boost.org/
  5. Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost
  6. Software License, Version 1.0. (See accompanying file
  7. LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. =============================================================================*/
  9. #if !defined(CPP_MACROMAP_HPP_CB8F51B0_A3F0_411C_AEF4_6FF631B8B414_INCLUDED)
  10. #define CPP_MACROMAP_HPP_CB8F51B0_A3F0_411C_AEF4_6FF631B8B414_INCLUDED
  11. #include <cstdlib>
  12. #include <cstdio>
  13. #include <ctime>
  14. #include <list>
  15. #include <map>
  16. #include <set>
  17. #include <vector>
  18. #include <iterator>
  19. #include <algorithm>
  20. #include <boost/assert.hpp>
  21. #include <boost/wave/wave_config.hpp>
  22. #if BOOST_WAVE_SERIALIZATION != 0
  23. #include <boost/serialization/serialization.hpp>
  24. #include <boost/serialization/shared_ptr.hpp>
  25. #endif
  26. #include <boost/filesystem/path.hpp>
  27. #include <boost/lexical_cast.hpp>
  28. #include <boost/wave/util/time_conversion_helper.hpp>
  29. #include <boost/wave/util/unput_queue_iterator.hpp>
  30. #include <boost/wave/util/macro_helpers.hpp>
  31. #include <boost/wave/util/macro_definition.hpp>
  32. #include <boost/wave/util/symbol_table.hpp>
  33. #include <boost/wave/util/cpp_macromap_utils.hpp>
  34. #include <boost/wave/util/cpp_macromap_predef.hpp>
  35. #include <boost/wave/util/filesystem_compatibility.hpp>
  36. #include <boost/wave/grammars/cpp_defined_grammar_gen.hpp>
  37. #include <boost/wave/wave_version.hpp>
  38. #include <boost/wave/cpp_exceptions.hpp>
  39. #include <boost/wave/language_support.hpp>
  40. // this must occur after all of the includes and before any code appears
  41. #ifdef BOOST_HAS_ABI_HEADERS
  42. #include BOOST_ABI_PREFIX
  43. #endif
  44. ///////////////////////////////////////////////////////////////////////////////
  45. namespace boost { namespace wave { namespace util {
  46. ///////////////////////////////////////////////////////////////////////////////
  47. //
  48. // macromap
  49. //
  50. // This class holds all currently defined macros and on demand expands
  51. // those macro definitions
  52. //
  53. ///////////////////////////////////////////////////////////////////////////////
  54. template <typename ContextT>
  55. class macromap {
  56. typedef macromap<ContextT> self_type;
  57. typedef typename ContextT::token_type token_type;
  58. typedef typename token_type::string_type string_type;
  59. typedef typename token_type::position_type position_type;
  60. typedef typename ContextT::token_sequence_type definition_container_type;
  61. typedef std::vector<token_type> parameter_container_type;
  62. typedef macro_definition<token_type, definition_container_type>
  63. macro_definition_type;
  64. typedef symbol_table<string_type, macro_definition_type>
  65. defined_macros_type;
  66. typedef typename defined_macros_type::value_type::second_type
  67. macro_ref_type;
  68. public:
  69. macromap(ContextT &ctx_)
  70. : current_macros(0), defined_macros(new defined_macros_type(1)),
  71. main_pos("", 0), ctx(ctx_), macro_uid(1)
  72. {
  73. current_macros = defined_macros.get();
  74. }
  75. ~macromap() {}
  76. // Add a new macro to the given macro scope
  77. bool add_macro(token_type const &name, bool has_parameters,
  78. parameter_container_type &parameters,
  79. definition_container_type &definition, bool is_predefined = false,
  80. defined_macros_type *scope = 0);
  81. // Tests, whether the given macro name is defined in the given macro scope
  82. bool is_defined(string_type const &name,
  83. typename defined_macros_type::iterator &it,
  84. defined_macros_type *scope = 0) const;
  85. // expects a token sequence as its parameters
  86. template <typename IteratorT>
  87. bool is_defined(IteratorT const &begin, IteratorT const &end) const;
  88. // expects an arbitrary string as its parameter
  89. bool is_defined(string_type const &str) const;
  90. // Get the macro definition for the given macro scope
  91. bool get_macro(string_type const &name, bool &has_parameters,
  92. bool &is_predefined, position_type &pos,
  93. parameter_container_type &parameters,
  94. definition_container_type &definition,
  95. defined_macros_type *scope = 0) const;
  96. // Remove a macro name from the given macro scope
  97. bool remove_macro(string_type const &name, position_type const& pos,
  98. bool even_predefined = false);
  99. template <typename IteratorT, typename ContainerT>
  100. token_type const &expand_tokensequence(IteratorT &first,
  101. IteratorT const &last, ContainerT &pending, ContainerT &expanded,
  102. bool& seen_newline, bool expand_operator_defined);
  103. // Expand all macros inside the given token sequence
  104. template <typename IteratorT, typename ContainerT>
  105. void expand_whole_tokensequence(ContainerT &expanded,
  106. IteratorT &first, IteratorT const &last,
  107. bool expand_operator_defined);
  108. // Init the predefined macros (add them to the given scope)
  109. void init_predefined_macros(char const *fname = "<Unknown>",
  110. defined_macros_type *scope = 0, bool at_global_scope = true);
  111. void predefine_macro(defined_macros_type *scope, string_type const &name,
  112. token_type const &t);
  113. // Init the internal macro symbol namespace
  114. void reset_macromap();
  115. position_type &get_main_pos() { return main_pos; }
  116. position_type const& get_main_pos() const { return main_pos; }
  117. // interface for macro name introspection
  118. typedef typename defined_macros_type::name_iterator name_iterator;
  119. typedef typename defined_macros_type::const_name_iterator const_name_iterator;
  120. name_iterator begin()
  121. { return defined_macros_type::make_iterator(current_macros->begin()); }
  122. name_iterator end()
  123. { return defined_macros_type::make_iterator(current_macros->end()); }
  124. const_name_iterator begin() const
  125. { return defined_macros_type::make_iterator(current_macros->begin()); }
  126. const_name_iterator end() const
  127. { return defined_macros_type::make_iterator(current_macros->end()); }
  128. protected:
  129. // Helper functions for expanding all macros in token sequences
  130. template <typename IteratorT, typename ContainerT>
  131. token_type const &expand_tokensequence_worker(ContainerT &pending,
  132. unput_queue_iterator<IteratorT, token_type, ContainerT> &first,
  133. unput_queue_iterator<IteratorT, token_type, ContainerT> const &last,
  134. bool& seen_newline, bool expand_operator_defined);
  135. // Collect all arguments supplied to a macro invocation
  136. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
  137. template <typename IteratorT, typename ContainerT, typename SizeT>
  138. typename std::vector<ContainerT>::size_type collect_arguments (
  139. token_type const curr_token, std::vector<ContainerT> &arguments,
  140. IteratorT &next, IteratorT const &end, SizeT const &parameter_count,
  141. bool& seen_newline);
  142. #else
  143. template <typename IteratorT, typename ContainerT, typename SizeT>
  144. typename std::vector<ContainerT>::size_type collect_arguments (
  145. token_type const curr_token, std::vector<ContainerT> &arguments,
  146. IteratorT &next, IteratorT &endparen, IteratorT const &end,
  147. SizeT const &parameter_count, bool& seen_newline);
  148. #endif
  149. // Expand a single macro name
  150. template <typename IteratorT, typename ContainerT>
  151. bool expand_macro(ContainerT &pending, token_type const &name,
  152. typename defined_macros_type::iterator it,
  153. IteratorT &first, IteratorT const &last,
  154. bool& seen_newline, bool expand_operator_defined,
  155. defined_macros_type *scope = 0, ContainerT *queue_symbol = 0);
  156. // Expand a predefined macro (__LINE__, __FILE__ and __INCLUDE_LEVEL__)
  157. template <typename ContainerT>
  158. bool expand_predefined_macro(token_type const &curr_token,
  159. ContainerT &expanded);
  160. // Expand a single macro argument
  161. template <typename ContainerT>
  162. void expand_argument (typename std::vector<ContainerT>::size_type arg,
  163. std::vector<ContainerT> &arguments,
  164. std::vector<ContainerT> &expanded_args, bool expand_operator_defined,
  165. std::vector<bool> &has_expanded_args);
  166. // Expand the replacement list (replaces parameters with arguments)
  167. template <typename ContainerT>
  168. void expand_replacement_list(
  169. macro_definition_type const &macrodefinition,
  170. std::vector<ContainerT> &arguments,
  171. bool expand_operator_defined, ContainerT &expanded);
  172. // Rescans the replacement list for macro expansion
  173. template <typename IteratorT, typename ContainerT>
  174. void rescan_replacement_list(token_type const &curr_token,
  175. macro_definition_type &macrodef, ContainerT &replacement_list,
  176. ContainerT &expanded, bool expand_operator_defined,
  177. IteratorT &nfirst, IteratorT const &nlast);
  178. // Resolves the operator defined() and replces the token with "0" or "1"
  179. template <typename IteratorT, typename ContainerT>
  180. token_type const &resolve_defined(IteratorT &first, IteratorT const &last,
  181. ContainerT &expanded);
  182. // Resolve operator _Pragma or the #pragma directive
  183. template <typename IteratorT, typename ContainerT>
  184. bool resolve_operator_pragma(IteratorT &first,
  185. IteratorT const &last, ContainerT &expanded, bool& seen_newline);
  186. // Handle the concatenation operator '##'
  187. template <typename ContainerT>
  188. bool concat_tokensequence(ContainerT &expanded);
  189. template <typename ContainerT>
  190. bool is_valid_concat(string_type new_value,
  191. position_type const &pos, ContainerT &rescanned);
  192. #if BOOST_WAVE_SERIALIZATION != 0
  193. public:
  194. BOOST_STATIC_CONSTANT(unsigned int, version = 0x10);
  195. BOOST_STATIC_CONSTANT(unsigned int, version_mask = 0x0f);
  196. private:
  197. friend class boost::serialization::access;
  198. template<typename Archive>
  199. void save(Archive &ar, const unsigned int version) const
  200. {
  201. using namespace boost::serialization;
  202. ar & make_nvp("defined_macros", defined_macros);
  203. }
  204. template<typename Archive>
  205. void load(Archive &ar, const unsigned int loaded_version)
  206. {
  207. using namespace boost::serialization;
  208. if (version != (loaded_version & ~version_mask)) {
  209. BOOST_WAVE_THROW(preprocess_exception, incompatible_config,
  210. "cpp_context state version", get_main_pos());
  211. }
  212. ar & make_nvp("defined_macros", defined_macros);
  213. current_macros = defined_macros.get();
  214. }
  215. BOOST_SERIALIZATION_SPLIT_MEMBER()
  216. #endif
  217. private:
  218. defined_macros_type *current_macros; // current symbol table
  219. boost::shared_ptr<defined_macros_type> defined_macros; // global symbol table
  220. token_type act_token; // current token
  221. position_type main_pos; // last token position in the pp_iterator
  222. string_type base_name; // the name to be expanded by __BASE_FILE__
  223. ContextT &ctx; // context object associated with the macromap
  224. long macro_uid;
  225. predefined_macros predef; // predefined macro support
  226. };
  227. ///////////////////////////////////////////////////////////////////////////////
  228. ///////////////////////////////////////////////////////////////////////////////
  229. //
  230. // add_macro(): adds a new macro to the macromap
  231. //
  232. ///////////////////////////////////////////////////////////////////////////////
  233. template <typename ContextT>
  234. inline bool
  235. macromap<ContextT>::add_macro(token_type const &name, bool has_parameters,
  236. parameter_container_type &parameters, definition_container_type &definition,
  237. bool is_predefined, defined_macros_type *scope)
  238. {
  239. if (!is_predefined && impl::is_special_macroname (name.get_value())) {
  240. // exclude special macro names
  241. BOOST_WAVE_THROW_NAME_CTX(ctx, macro_handling_exception,
  242. illegal_redefinition, name.get_value().c_str(), main_pos,
  243. name.get_value().c_str());
  244. return false;
  245. }
  246. if (boost::wave::need_variadics(ctx.get_language()) &&
  247. "__VA_ARGS__" == name.get_value())
  248. {
  249. // can't use __VA_ARGS__ as a macro name
  250. BOOST_WAVE_THROW_NAME_CTX(ctx, macro_handling_exception,
  251. bad_define_statement_va_args, name.get_value().c_str(), main_pos,
  252. name.get_value().c_str());
  253. return false;
  254. }
  255. if (AltExtTokenType == (token_id(name) & ExtTokenOnlyMask)) {
  256. // exclude special operator names
  257. BOOST_WAVE_THROW_NAME_CTX(ctx, macro_handling_exception,
  258. illegal_operator_redefinition, name.get_value().c_str(), main_pos,
  259. name.get_value().c_str());
  260. return false;
  261. }
  262. // try to define the new macro
  263. defined_macros_type *current_scope = scope ? scope : current_macros;
  264. typename defined_macros_type::iterator it = current_scope->find(name.get_value());
  265. if (it != current_scope->end()) {
  266. // redefinition, should not be different
  267. macro_definition_type* macrodef = (*it).second.get();
  268. if (macrodef->is_functionlike != has_parameters ||
  269. !impl::parameters_equal(macrodef->macroparameters, parameters) ||
  270. !impl::definition_equals(macrodef->macrodefinition, definition))
  271. {
  272. BOOST_WAVE_THROW_NAME_CTX(ctx, macro_handling_exception,
  273. macro_redefinition, name.get_value().c_str(), main_pos,
  274. name.get_value().c_str());
  275. }
  276. return false;
  277. }
  278. // test the validity of the parameter names
  279. if (has_parameters) {
  280. std::set<typename token_type::string_type> names;
  281. typedef typename parameter_container_type::iterator
  282. parameter_iterator_type;
  283. typedef typename std::set<typename token_type::string_type>::iterator
  284. name_iterator_type;
  285. parameter_iterator_type end = parameters.end();
  286. for (parameter_iterator_type itp = parameters.begin(); itp != end; ++itp)
  287. {
  288. name_iterator_type pit = names.find((*itp).get_value());
  289. if (pit != names.end()) {
  290. // duplicate parameter name
  291. BOOST_WAVE_THROW_NAME_CTX(ctx, macro_handling_exception,
  292. duplicate_parameter_name, (*pit).c_str(), main_pos,
  293. name.get_value().c_str());
  294. return false;
  295. }
  296. names.insert((*itp).get_value());
  297. }
  298. }
  299. // insert a new macro node
  300. std::pair<typename defined_macros_type::iterator, bool> p =
  301. current_scope->insert(
  302. typename defined_macros_type::value_type(
  303. name.get_value(),
  304. macro_ref_type(new macro_definition_type(name,
  305. has_parameters, is_predefined, ++macro_uid)
  306. )
  307. )
  308. );
  309. if (!p.second) {
  310. BOOST_WAVE_THROW_NAME_CTX(ctx, macro_handling_exception,
  311. macro_insertion_error, name.get_value().c_str(), main_pos,
  312. name.get_value().c_str());
  313. return false;
  314. }
  315. // add the parameters and the definition
  316. std::swap((*p.first).second->macroparameters, parameters);
  317. std::swap((*p.first).second->macrodefinition, definition);
  318. // call the context supplied preprocessing hook
  319. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
  320. ctx.get_hooks().defined_macro(name, has_parameters,
  321. (*p.first).second->macroparameters,
  322. (*p.first).second->macrodefinition, is_predefined);
  323. #else
  324. ctx.get_hooks().defined_macro(ctx.derived(), name, has_parameters,
  325. (*p.first).second->macroparameters,
  326. (*p.first).second->macrodefinition, is_predefined);
  327. #endif
  328. return true;
  329. }
  330. ///////////////////////////////////////////////////////////////////////////////
  331. //
  332. // is_defined(): returns, whether a given macro is already defined
  333. //
  334. ///////////////////////////////////////////////////////////////////////////////
  335. template <typename ContextT>
  336. inline bool
  337. macromap<ContextT>::is_defined(typename token_type::string_type const &name,
  338. typename defined_macros_type::iterator &it,
  339. defined_macros_type *scope) const
  340. {
  341. if (0 == scope) scope = current_macros;
  342. if ((it = scope->find(name)) != scope->end())
  343. return true; // found in symbol table
  344. // quick pre-check
  345. if (name.size() < 8 || '_' != name[0] || '_' != name[1])
  346. return false; // quick check failed
  347. return name == "__LINE__" || name == "__FILE__" ||
  348. name == "__INCLUDE_LEVEL__";
  349. }
  350. template <typename ContextT>
  351. template <typename IteratorT>
  352. inline bool
  353. macromap<ContextT>::is_defined(IteratorT const &begin,
  354. IteratorT const &end) const
  355. {
  356. // in normal mode the name under inspection should consist of an identifier
  357. // only
  358. token_id id = token_id(*begin);
  359. if (T_IDENTIFIER != id &&
  360. !IS_CATEGORY(id, KeywordTokenType) &&
  361. !IS_EXTCATEGORY(id, OperatorTokenType|AltExtTokenType) &&
  362. !IS_CATEGORY(id, BoolLiteralTokenType))
  363. {
  364. std::string msg(impl::get_full_name(begin, end));
  365. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, invalid_macroname,
  366. msg.c_str(), main_pos);
  367. return false;
  368. }
  369. IteratorT it = begin;
  370. string_type name ((*it).get_value());
  371. typename defined_macros_type::iterator cit;
  372. if (++it != end) {
  373. // there should be only one token as the inspected name
  374. std::string msg(impl::get_full_name(begin, end));
  375. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, invalid_macroname,
  376. msg.c_str(), main_pos);
  377. return false;
  378. }
  379. return is_defined(name, cit, 0);
  380. }
  381. ///////////////////////////////////////////////////////////////////////////////
  382. // same as above, only takes an arbitrary string type as its parameter
  383. template <typename ContextT>
  384. inline bool
  385. macromap<ContextT>::is_defined(string_type const &str) const
  386. {
  387. typename defined_macros_type::iterator cit;
  388. return is_defined(str, cit, 0);
  389. }
  390. ///////////////////////////////////////////////////////////////////////////////
  391. //
  392. // Get the macro definition for the given macro scope
  393. //
  394. ///////////////////////////////////////////////////////////////////////////////
  395. template <typename ContextT>
  396. inline bool
  397. macromap<ContextT>::get_macro(string_type const &name, bool &has_parameters,
  398. bool &is_predefined, position_type &pos,
  399. parameter_container_type &parameters,
  400. definition_container_type &definition,
  401. defined_macros_type *scope) const
  402. {
  403. typename defined_macros_type::iterator it;
  404. if (!is_defined(name, it, scope))
  405. return false;
  406. macro_definition_type &macro_def = *(*it).second.get();
  407. has_parameters = macro_def.is_functionlike;
  408. is_predefined = macro_def.is_predefined;
  409. pos = macro_def.macroname.get_position();
  410. parameters = macro_def.macroparameters;
  411. definition = macro_def.macrodefinition;
  412. return true;
  413. }
  414. ///////////////////////////////////////////////////////////////////////////////
  415. //
  416. // remove_macro(): remove a macro from the macromap
  417. //
  418. ///////////////////////////////////////////////////////////////////////////////
  419. template <typename ContextT>
  420. inline bool
  421. macromap<ContextT>::remove_macro(string_type const &name,
  422. position_type const& pos, bool even_predefined)
  423. {
  424. typename defined_macros_type::iterator it = current_macros->find(name);
  425. if (it != current_macros->end()) {
  426. if ((*it).second->is_predefined) {
  427. if (!even_predefined || impl::is_special_macroname(name)) {
  428. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
  429. bad_undefine_statement, name.c_str(), main_pos);
  430. return false;
  431. }
  432. }
  433. current_macros->erase(it);
  434. // call the context supplied preprocessing hook function
  435. token_type tok(T_IDENTIFIER, name, pos);
  436. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
  437. ctx.get_hooks().undefined_macro(tok);
  438. #else
  439. ctx.get_hooks().undefined_macro(ctx.derived(), tok);
  440. #endif
  441. return true;
  442. }
  443. else if (impl::is_special_macroname(name)) {
  444. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, bad_undefine_statement,
  445. name.c_str(), pos);
  446. }
  447. return false; // macro was not defined
  448. }
  449. ///////////////////////////////////////////////////////////////////////////////
  450. //
  451. // expand_tokensequence
  452. //
  453. // This function is a helper function which wraps the given iterator
  454. // range into corresponding unput_iterator's and calls the main workhorse
  455. // of the macro expansion engine (the function expand_tokensequence_worker)
  456. //
  457. // This is the top level macro expansion function called from the
  458. // preprocessing iterator component only.
  459. //
  460. ///////////////////////////////////////////////////////////////////////////////
  461. template <typename ContextT>
  462. template <typename IteratorT, typename ContainerT>
  463. inline typename ContextT::token_type const &
  464. macromap<ContextT>::expand_tokensequence(IteratorT &first,
  465. IteratorT const &last, ContainerT &pending, ContainerT &expanded,
  466. bool& seen_newline, bool expand_operator_defined)
  467. {
  468. typedef impl::gen_unput_queue_iterator<IteratorT, token_type, ContainerT>
  469. gen_type;
  470. typedef typename gen_type::return_type iterator_type;
  471. iterator_type first_it = gen_type::generate(expanded, first);
  472. iterator_type last_it = gen_type::generate(last);
  473. on_exit::assign<IteratorT, iterator_type> on_exit(first, first_it);
  474. return expand_tokensequence_worker(pending, first_it, last_it,
  475. seen_newline, expand_operator_defined);
  476. }
  477. ///////////////////////////////////////////////////////////////////////////////
  478. //
  479. // expand_tokensequence_worker
  480. //
  481. // This function is the main workhorse of the macro expansion engine. It
  482. // expands as much tokens as needed to identify the next preprocessed
  483. // token to return to the caller.
  484. // It returns the next preprocessed token.
  485. //
  486. // The iterator 'first' is adjusted accordingly.
  487. //
  488. ///////////////////////////////////////////////////////////////////////////////
  489. template <typename ContextT>
  490. template <typename IteratorT, typename ContainerT>
  491. inline typename ContextT::token_type const &
  492. macromap<ContextT>::expand_tokensequence_worker(
  493. ContainerT &pending,
  494. unput_queue_iterator<IteratorT, token_type, ContainerT> &first,
  495. unput_queue_iterator<IteratorT, token_type, ContainerT> const &last,
  496. bool& seen_newline, bool expand_operator_defined)
  497. {
  498. // if there exist pending tokens (tokens, which are already preprocessed), then
  499. // return the next one from there
  500. if (!pending.empty()) {
  501. on_exit::pop_front<definition_container_type> pop_front_token(pending);
  502. return act_token = pending.front();
  503. }
  504. // analyze the next element of the given sequence, if it is an
  505. // T_IDENTIFIER token, try to replace this as a macro etc.
  506. using namespace boost::wave;
  507. if (first != last) {
  508. token_id id = token_id(*first);
  509. // ignore placeholder tokens
  510. if (T_PLACEHOLDER == id) {
  511. token_type placeholder = *first;
  512. ++first;
  513. if (first == last)
  514. return act_token = placeholder;
  515. id = token_id(*first);
  516. }
  517. if (T_IDENTIFIER == id || IS_CATEGORY(id, KeywordTokenType) ||
  518. IS_EXTCATEGORY(id, OperatorTokenType|AltExtTokenType) ||
  519. IS_CATEGORY(id, BoolLiteralTokenType))
  520. {
  521. // try to replace this identifier as a macro
  522. if (expand_operator_defined && (*first).get_value() == "defined") {
  523. // resolve operator defined()
  524. return resolve_defined(first, last, pending);
  525. }
  526. else if (boost::wave::need_variadics(ctx.get_language()) &&
  527. (*first).get_value() == "_Pragma")
  528. {
  529. // in C99 mode only: resolve the operator _Pragma
  530. token_type curr_token = *first;
  531. if (!resolve_operator_pragma(first, last, pending, seen_newline) ||
  532. pending.size() > 0)
  533. {
  534. // unknown to us pragma or supplied replacement, return the
  535. // next token
  536. on_exit::pop_front<definition_container_type> pop_token(pending);
  537. return act_token = pending.front();
  538. }
  539. // the operator _Pragma() was eaten completely, continue
  540. return act_token = token_type(T_PLACEHOLDER, "_",
  541. curr_token.get_position());
  542. }
  543. token_type name_token (*first);
  544. typename defined_macros_type::iterator it;
  545. if (is_defined(name_token.get_value(), it)) {
  546. // the current token contains an identifier, which is currently
  547. // defined as a macro
  548. if (expand_macro(pending, name_token, it, first, last,
  549. seen_newline, expand_operator_defined))
  550. {
  551. // the tokens returned by expand_macro should be rescanned
  552. // beginning at the last token of the returned replacement list
  553. if (first != last) {
  554. // splice the last token back into the input queue
  555. typename ContainerT::reverse_iterator rit = pending.rbegin();
  556. first.get_unput_queue().splice(
  557. first.get_unput_queue().begin(), pending,
  558. (++rit).base(), pending.end());
  559. }
  560. // fall through ...
  561. }
  562. else if (!pending.empty()) {
  563. // return the first token from the pending queue
  564. on_exit::pop_front<definition_container_type> pop_queue (pending);
  565. return act_token = pending.front();
  566. }
  567. else {
  568. // macro expansion reached the eoi
  569. return act_token = token_type();
  570. }
  571. // return the next preprocessed token
  572. return expand_tokensequence_worker(pending, first, last,
  573. seen_newline, expand_operator_defined);
  574. }
  575. // else if (expand_operator_defined) {
  576. // // in preprocessing conditionals undefined identifiers and keywords
  577. // // are to be replaced with '0' (see. C++ standard 16.1.4, [cpp.cond])
  578. // return act_token =
  579. // token_type(T_INTLIT, "0", (*first++).get_position());
  580. // }
  581. else {
  582. act_token = name_token;
  583. ++first;
  584. return act_token;
  585. }
  586. }
  587. else if (expand_operator_defined && IS_CATEGORY(*first, BoolLiteralTokenType)) {
  588. // expanding a constant expression inside #if/#elif, special handling
  589. // of 'true' and 'false'
  590. // all remaining identifiers and keywords, except for true and false,
  591. // are replaced with the pp-number 0 (C++ standard 16.1.4, [cpp.cond])
  592. return act_token = token_type(T_INTLIT, T_TRUE != id ? "0" : "1",
  593. (*first++).get_position());
  594. }
  595. else {
  596. act_token = *first;
  597. ++first;
  598. return act_token;
  599. }
  600. }
  601. return act_token = token_type(); // eoi
  602. }
  603. ///////////////////////////////////////////////////////////////////////////////
  604. //
  605. // collect_arguments(): collect the actual arguments of a macro invocation
  606. //
  607. // return the number of successfully detected non-empty arguments
  608. //
  609. ///////////////////////////////////////////////////////////////////////////////
  610. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
  611. template <typename ContextT>
  612. template <typename IteratorT, typename ContainerT, typename SizeT>
  613. inline typename std::vector<ContainerT>::size_type
  614. macromap<ContextT>::collect_arguments (token_type const curr_token,
  615. std::vector<ContainerT> &arguments, IteratorT &next,
  616. IteratorT const &end, SizeT const &parameter_count, bool& seen_newline)
  617. #else
  618. template <typename ContextT>
  619. template <typename IteratorT, typename ContainerT, typename SizeT>
  620. inline typename std::vector<ContainerT>::size_type
  621. macromap<ContextT>::collect_arguments (token_type const curr_token,
  622. std::vector<ContainerT> &arguments, IteratorT &next, IteratorT &endparen,
  623. IteratorT const &end, SizeT const &parameter_count, bool& seen_newline)
  624. #endif
  625. {
  626. using namespace boost::wave;
  627. arguments.push_back(ContainerT());
  628. // collect the actual arguments
  629. typename std::vector<ContainerT>::size_type count_arguments = 0;
  630. int nested_parenthesis_level = 1;
  631. ContainerT *argument = &arguments[0];
  632. bool was_whitespace = false;
  633. token_type startof_argument_list = *next;
  634. while (++next != end && nested_parenthesis_level) {
  635. token_id id = token_id(*next);
  636. if (0 == parameter_count &&
  637. !IS_CATEGORY((*next), WhiteSpaceTokenType) && id != T_NEWLINE &&
  638. id != T_RIGHTPAREN && id != T_LEFTPAREN)
  639. {
  640. // there shouldn't be any arguments
  641. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
  642. too_many_macroarguments, curr_token.get_value().c_str(),
  643. main_pos);
  644. return 0;
  645. }
  646. switch (id) {
  647. case T_LEFTPAREN:
  648. ++nested_parenthesis_level;
  649. argument->push_back(*next);
  650. was_whitespace = false;
  651. break;
  652. case T_RIGHTPAREN:
  653. {
  654. if (--nested_parenthesis_level >= 1)
  655. argument->push_back(*next);
  656. else {
  657. // found closing parenthesis
  658. // trim_sequence(argument);
  659. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS == 0
  660. endparen = next;
  661. #endif
  662. if (parameter_count > 0) {
  663. if (argument->empty() ||
  664. impl::is_whitespace_only(*argument))
  665. {
  666. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  667. if (boost::wave::need_variadics(ctx.get_language())) {
  668. // store a placemarker as the argument
  669. argument->push_back(token_type(T_PLACEMARKER, "\xA7",
  670. (*next).get_position()));
  671. ++count_arguments;
  672. }
  673. #endif
  674. }
  675. else {
  676. ++count_arguments;
  677. }
  678. }
  679. }
  680. was_whitespace = false;
  681. }
  682. break;
  683. case T_COMMA:
  684. if (1 == nested_parenthesis_level) {
  685. // next parameter
  686. // trim_sequence(argument);
  687. if (argument->empty() ||
  688. impl::is_whitespace_only(*argument))
  689. {
  690. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  691. if (boost::wave::need_variadics(ctx.get_language())) {
  692. // store a placemarker as the argument
  693. argument->push_back(token_type(T_PLACEMARKER, "\xA7",
  694. (*next).get_position()));
  695. ++count_arguments;
  696. }
  697. #endif
  698. }
  699. else {
  700. ++count_arguments;
  701. }
  702. arguments.push_back(ContainerT()); // add new arg
  703. argument = &arguments[arguments.size()-1];
  704. }
  705. else {
  706. // surrounded by parenthesises, so store to current argument
  707. argument->push_back(*next);
  708. }
  709. was_whitespace = false;
  710. break;
  711. case T_NEWLINE:
  712. seen_newline = true;
  713. /* fall through */
  714. case T_SPACE:
  715. case T_SPACE2:
  716. case T_CCOMMENT:
  717. if (!was_whitespace)
  718. argument->push_back(token_type(T_SPACE, " ", (*next).get_position()));
  719. was_whitespace = true;
  720. break; // skip whitespace
  721. case T_PLACEHOLDER:
  722. break; // ignore placeholder
  723. default:
  724. argument->push_back(*next);
  725. was_whitespace = false;
  726. break;
  727. }
  728. }
  729. if (nested_parenthesis_level >= 1) {
  730. // missing ')': improperly terminated macro invocation
  731. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
  732. improperly_terminated_macro, "missing ')'", main_pos);
  733. return 0;
  734. }
  735. // if no argument was expected and we didn't find any, than remove the empty
  736. // element
  737. if (0 == parameter_count && 0 == count_arguments) {
  738. BOOST_ASSERT(1 == arguments.size());
  739. arguments.clear();
  740. }
  741. return count_arguments;
  742. }
  743. ///////////////////////////////////////////////////////////////////////////////
  744. //
  745. // expand_whole_tokensequence
  746. //
  747. // fully expands a given token sequence
  748. //
  749. ///////////////////////////////////////////////////////////////////////////////
  750. template <typename ContextT>
  751. template <typename IteratorT, typename ContainerT>
  752. inline void
  753. macromap<ContextT>::expand_whole_tokensequence(ContainerT &expanded,
  754. IteratorT &first, IteratorT const &last,
  755. bool expand_operator_defined)
  756. {
  757. typedef impl::gen_unput_queue_iterator<IteratorT, token_type, ContainerT>
  758. gen_type;
  759. typedef typename gen_type::return_type iterator_type;
  760. ContainerT empty;
  761. iterator_type first_it = gen_type::generate(empty, first);
  762. iterator_type last_it = gen_type::generate(last);
  763. on_exit::assign<IteratorT, iterator_type> on_exit(first, first_it);
  764. ContainerT pending_queue;
  765. bool seen_newline;
  766. while (!pending_queue.empty() || first_it != last_it) {
  767. expanded.push_back(
  768. expand_tokensequence_worker(pending_queue, first_it,
  769. last_it, seen_newline, expand_operator_defined)
  770. );
  771. }
  772. // should have returned all expanded tokens
  773. BOOST_ASSERT(pending_queue.empty()/* && unput_queue.empty()*/);
  774. }
  775. ///////////////////////////////////////////////////////////////////////////////
  776. //
  777. // expand_argument
  778. //
  779. // fully expands the given argument of a macro call
  780. //
  781. ///////////////////////////////////////////////////////////////////////////////
  782. template <typename ContextT>
  783. template <typename ContainerT>
  784. inline void
  785. macromap<ContextT>::expand_argument (
  786. typename std::vector<ContainerT>::size_type arg,
  787. std::vector<ContainerT> &arguments, std::vector<ContainerT> &expanded_args,
  788. bool expand_operator_defined, std::vector<bool> &has_expanded_args)
  789. {
  790. if (!has_expanded_args[arg]) {
  791. // expand the argument only once
  792. typedef typename std::vector<ContainerT>::value_type::iterator
  793. argument_iterator_type;
  794. argument_iterator_type begin_it = arguments[arg].begin();
  795. argument_iterator_type end_it = arguments[arg].end();
  796. expand_whole_tokensequence(expanded_args[arg], begin_it, end_it,
  797. expand_operator_defined);
  798. impl::remove_placeholders(expanded_args[arg]);
  799. has_expanded_args[arg] = true;
  800. }
  801. }
  802. ///////////////////////////////////////////////////////////////////////////////
  803. //
  804. // expand_replacement_list
  805. //
  806. // fully expands the replacement list of a given macro with the
  807. // actual arguments/expanded arguments
  808. // handles the '#' [cpp.stringize] and the '##' [cpp.concat] operator
  809. //
  810. ///////////////////////////////////////////////////////////////////////////////
  811. template <typename ContextT>
  812. template <typename ContainerT>
  813. inline void
  814. macromap<ContextT>::expand_replacement_list(
  815. macro_definition_type const &macrodef,
  816. std::vector<ContainerT> &arguments, bool expand_operator_defined,
  817. ContainerT &expanded)
  818. {
  819. using namespace boost::wave;
  820. typedef typename macro_definition_type::const_definition_iterator_t
  821. macro_definition_iter_t;
  822. std::vector<ContainerT> expanded_args(arguments.size());
  823. std::vector<bool> has_expanded_args(arguments.size());
  824. bool seen_concat = false;
  825. bool adjacent_concat = false;
  826. bool adjacent_stringize = false;
  827. macro_definition_iter_t cend = macrodef.macrodefinition.end();
  828. for (macro_definition_iter_t cit = macrodef.macrodefinition.begin();
  829. cit != cend; ++cit)
  830. {
  831. bool use_replaced_arg = true;
  832. token_id base_id = BASE_TOKEN(token_id(*cit));
  833. if (T_POUND_POUND == base_id) {
  834. // concatenation operator
  835. adjacent_concat = true;
  836. seen_concat = true;
  837. }
  838. else if (T_POUND == base_id) {
  839. // stringize operator
  840. adjacent_stringize = true;
  841. }
  842. else {
  843. if (adjacent_stringize || adjacent_concat ||
  844. T_POUND_POUND == impl::next_token<macro_definition_iter_t>
  845. ::peek(cit, cend))
  846. {
  847. use_replaced_arg = false;
  848. }
  849. if (adjacent_concat) // spaces after '##' ?
  850. adjacent_concat = IS_CATEGORY(*cit, WhiteSpaceTokenType);
  851. }
  852. if (IS_CATEGORY((*cit), ParameterTokenType)) {
  853. // copy argument 'i' instead of the parameter token i
  854. typename ContainerT::size_type i;
  855. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  856. bool is_ellipsis = false;
  857. if (IS_EXTCATEGORY((*cit), ExtParameterTokenType)) {
  858. BOOST_ASSERT(boost::wave::need_variadics(ctx.get_language()));
  859. i = token_id(*cit) - T_EXTPARAMETERBASE;
  860. is_ellipsis = true;
  861. }
  862. else
  863. #endif
  864. {
  865. i = token_id(*cit) - T_PARAMETERBASE;
  866. }
  867. BOOST_ASSERT(i < arguments.size());
  868. if (use_replaced_arg) {
  869. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  870. if (is_ellipsis) {
  871. position_type const &pos = (*cit).get_position();
  872. BOOST_ASSERT(boost::wave::need_variadics(ctx.get_language()));
  873. // ensure all variadic arguments to be expanded
  874. for (typename vector<ContainerT>::size_type arg = i;
  875. arg < expanded_args.size(); ++arg)
  876. {
  877. expand_argument(arg, arguments, expanded_args,
  878. expand_operator_defined, has_expanded_args);
  879. }
  880. impl::replace_ellipsis(expanded_args, i, expanded, pos);
  881. }
  882. else
  883. #endif
  884. {
  885. // ensure argument i to be expanded
  886. expand_argument(i, arguments, expanded_args,
  887. expand_operator_defined, has_expanded_args);
  888. // replace argument
  889. ContainerT const &arg = expanded_args[i];
  890. std::copy(arg.begin(), arg.end(),
  891. std::inserter(expanded, expanded.end()));
  892. }
  893. }
  894. else if (adjacent_stringize &&
  895. !IS_CATEGORY(*cit, WhiteSpaceTokenType))
  896. {
  897. // stringize the current argument
  898. BOOST_ASSERT(!arguments[i].empty());
  899. // safe a copy of the first tokens position (not a reference!)
  900. position_type pos ((*arguments[i].begin()).get_position());
  901. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  902. if (is_ellipsis && boost::wave::need_variadics(ctx.get_language())) {
  903. impl::trim_sequence_left(arguments[i]);
  904. impl::trim_sequence_right(arguments.back());
  905. expanded.push_back(token_type(T_STRINGLIT,
  906. impl::as_stringlit(arguments, i, pos), pos));
  907. }
  908. else
  909. #endif
  910. {
  911. impl::trim_sequence(arguments[i]);
  912. expanded.push_back(token_type(T_STRINGLIT,
  913. impl::as_stringlit(arguments[i], pos), pos));
  914. }
  915. adjacent_stringize = false;
  916. }
  917. else {
  918. // simply copy the original argument (adjacent '##' or '#')
  919. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  920. if (is_ellipsis) {
  921. position_type const &pos = (*cit).get_position();
  922. impl::trim_sequence_left(arguments[i]);
  923. impl::trim_sequence_right(arguments.back());
  924. BOOST_ASSERT(boost::wave::need_variadics(ctx.get_language()));
  925. impl::replace_ellipsis(arguments, i, expanded, pos);
  926. }
  927. else
  928. #endif
  929. {
  930. ContainerT &arg = arguments[i];
  931. impl::trim_sequence(arg);
  932. std::copy(arg.begin(), arg.end(),
  933. std::inserter(expanded, expanded.end()));
  934. }
  935. }
  936. }
  937. else if (!adjacent_stringize || T_POUND != base_id) {
  938. // insert the actual replacement token (if it is not the '#' operator)
  939. expanded.push_back(*cit);
  940. }
  941. }
  942. if (adjacent_stringize) {
  943. // error, '#' should not be the last token
  944. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, ill_formed_operator,
  945. "stringize ('#')", main_pos);
  946. return;
  947. }
  948. // handle the cpp.concat operator
  949. if (seen_concat)
  950. concat_tokensequence(expanded);
  951. }
  952. ///////////////////////////////////////////////////////////////////////////////
  953. //
  954. // rescan_replacement_list
  955. //
  956. // As the name implies, this function is used to rescan the replacement list
  957. // after the first macro substitution phase.
  958. //
  959. ///////////////////////////////////////////////////////////////////////////////
  960. template <typename ContextT>
  961. template <typename IteratorT, typename ContainerT>
  962. inline void
  963. macromap<ContextT>::rescan_replacement_list(token_type const &curr_token,
  964. macro_definition_type &macro_def, ContainerT &replacement_list,
  965. ContainerT &expanded, bool expand_operator_defined,
  966. IteratorT &nfirst, IteratorT const &nlast)
  967. {
  968. if (!replacement_list.empty()) {
  969. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  970. // remove the placemarkers
  971. if (boost::wave::need_variadics(ctx.get_language())) {
  972. typename ContainerT::iterator end = replacement_list.end();
  973. typename ContainerT::iterator it = replacement_list.begin();
  974. while (it != end) {
  975. using namespace boost::wave;
  976. if (T_PLACEMARKER == token_id(*it)) {
  977. typename ContainerT::iterator placemarker = it;
  978. ++it;
  979. replacement_list.erase(placemarker);
  980. }
  981. else {
  982. ++it;
  983. }
  984. }
  985. }
  986. #endif
  987. // rescan the replacement list, during this rescan the current macro under
  988. // expansion isn't available as an expandable macro
  989. on_exit::reset<bool> on_exit(macro_def.is_available_for_replacement, false);
  990. typename ContainerT::iterator begin_it = replacement_list.begin();
  991. typename ContainerT::iterator end_it = replacement_list.end();
  992. expand_whole_tokensequence(expanded, begin_it, end_it,
  993. expand_operator_defined);
  994. // trim replacement list, leave placeholder tokens untouched
  995. impl::trim_replacement_list(expanded);
  996. }
  997. if (expanded.empty()) {
  998. // the resulting replacement list should contain at least a placeholder
  999. // token
  1000. expanded.push_back(token_type(T_PLACEHOLDER, "_", curr_token.get_position()));
  1001. }
  1002. }
  1003. ///////////////////////////////////////////////////////////////////////////////
  1004. //
  1005. // expand_macro(): expands a defined macro
  1006. //
  1007. // This functions tries to expand the macro, to which points the 'first'
  1008. // iterator. The functions eats up more tokens, if the macro to expand is
  1009. // a function-like macro.
  1010. //
  1011. ///////////////////////////////////////////////////////////////////////////////
  1012. template <typename ContextT>
  1013. template <typename IteratorT, typename ContainerT>
  1014. inline bool
  1015. macromap<ContextT>::expand_macro(ContainerT &expanded,
  1016. token_type const &curr_token, typename defined_macros_type::iterator it,
  1017. IteratorT &first, IteratorT const &last,
  1018. bool& seen_newline, bool expand_operator_defined,
  1019. defined_macros_type *scope, ContainerT *queue_symbol)
  1020. {
  1021. using namespace boost::wave;
  1022. if (0 == scope) scope = current_macros;
  1023. BOOST_ASSERT(T_IDENTIFIER == token_id(curr_token) ||
  1024. IS_CATEGORY(token_id(curr_token), KeywordTokenType) ||
  1025. IS_EXTCATEGORY(token_id(curr_token), OperatorTokenType|AltExtTokenType) ||
  1026. IS_CATEGORY(token_id(curr_token), BoolLiteralTokenType));
  1027. if (it == scope->end()) {
  1028. ++first; // advance
  1029. // try to expand a predefined macro (__FILE__, __LINE__ or __INCLUDE_LEVEL__)
  1030. if (expand_predefined_macro(curr_token, expanded))
  1031. return false;
  1032. // not defined as a macro
  1033. if (0 != queue_symbol) {
  1034. expanded.splice(expanded.end(), *queue_symbol);
  1035. }
  1036. else {
  1037. expanded.push_back(curr_token);
  1038. }
  1039. return false;
  1040. }
  1041. // ensure the parameters to be replaced with special parameter tokens
  1042. macro_definition_type &macro_def = *(*it).second.get();
  1043. macro_def.replace_parameters();
  1044. // test if this macro is currently available for replacement
  1045. if (!macro_def.is_available_for_replacement) {
  1046. // this macro is marked as non-replaceable
  1047. // copy the macro name itself
  1048. if (0 != queue_symbol) {
  1049. queue_symbol->push_back(token_type(T_NONREPLACABLE_IDENTIFIER,
  1050. curr_token.get_value(), curr_token.get_position()));
  1051. expanded.splice(expanded.end(), *queue_symbol);
  1052. }
  1053. else {
  1054. expanded.push_back(token_type(T_NONREPLACABLE_IDENTIFIER,
  1055. curr_token.get_value(), curr_token.get_position()));
  1056. }
  1057. ++first;
  1058. return false;
  1059. }
  1060. // try to replace the current identifier as a function-like macro
  1061. ContainerT replacement_list;
  1062. if (T_LEFTPAREN == impl::next_token<IteratorT>::peek(first, last)) {
  1063. // called as a function-like macro
  1064. impl::skip_to_token(ctx, first, last, T_LEFTPAREN, seen_newline);
  1065. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS == 0
  1066. IteratorT seqstart = first;
  1067. IteratorT seqend = first;
  1068. #endif
  1069. if (macro_def.is_functionlike) {
  1070. // defined as a function-like macro
  1071. // collect the arguments
  1072. std::vector<ContainerT> arguments;
  1073. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
  1074. typename std::vector<ContainerT>::size_type count_args =
  1075. collect_arguments (curr_token, arguments, first, last,
  1076. macro_def.macroparameters.size(), seen_newline);
  1077. #else
  1078. typename std::vector<ContainerT>::size_type count_args =
  1079. collect_arguments (curr_token, arguments, first, seqend, last,
  1080. macro_def.macroparameters.size(), seen_newline);
  1081. #endif
  1082. // verify the parameter count
  1083. if (count_args < macro_def.macroparameters.size() ||
  1084. arguments.size() < macro_def.macroparameters.size())
  1085. {
  1086. if (count_args != arguments.size()) {
  1087. // must been at least one empty argument in C++ mode
  1088. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
  1089. empty_macroarguments, curr_token.get_value().c_str(),
  1090. main_pos);
  1091. }
  1092. else {
  1093. // too few macro arguments
  1094. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
  1095. too_few_macroarguments, curr_token.get_value().c_str(),
  1096. main_pos);
  1097. }
  1098. return false;
  1099. }
  1100. if (count_args > macro_def.macroparameters.size() ||
  1101. arguments.size() > macro_def.macroparameters.size())
  1102. {
  1103. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  1104. if (!macro_def.has_ellipsis)
  1105. #endif
  1106. {
  1107. // too many macro arguments
  1108. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
  1109. too_many_macroarguments,
  1110. curr_token.get_value().c_str(), main_pos);
  1111. return false;
  1112. }
  1113. }
  1114. // inject tracing support
  1115. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
  1116. ctx.get_hooks().expanding_function_like_macro(
  1117. macro_def.macroname, macro_def.macroparameters,
  1118. macro_def.macrodefinition, curr_token, arguments);
  1119. #else
  1120. if (ctx.get_hooks().expanding_function_like_macro(ctx.derived(),
  1121. macro_def.macroname, macro_def.macroparameters,
  1122. macro_def.macrodefinition, curr_token, arguments,
  1123. seqstart, seqend))
  1124. {
  1125. // // do not expand this macro, just copy the whole sequence
  1126. // expanded.push_back(curr_token);
  1127. // std::copy(seqstart, first,
  1128. // std::inserter(expanded, expanded.end()));
  1129. // do not expand macro, just copy macro name and parenthesis
  1130. expanded.push_back(curr_token);
  1131. expanded.push_back(*seqstart);
  1132. first = ++seqstart;
  1133. return false; // no further preprocessing required
  1134. }
  1135. #endif
  1136. // expand the replacement list of this macro
  1137. expand_replacement_list(macro_def, arguments, expand_operator_defined,
  1138. replacement_list);
  1139. }
  1140. else {
  1141. // defined as an object-like macro
  1142. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
  1143. ctx.get_hooks().expanding_object_like_macro(
  1144. macro_def.macroname, macro_def.macrodefinition, curr_token);
  1145. #else
  1146. if (ctx.get_hooks().expanding_object_like_macro(ctx.derived(),
  1147. macro_def.macroname, macro_def.macrodefinition, curr_token))
  1148. {
  1149. // do not expand this macro, just copy the whole sequence
  1150. expanded.push_back(curr_token);
  1151. return false; // no further preprocessing required
  1152. }
  1153. #endif
  1154. bool found = false;
  1155. impl::find_concat_operator concat_tag(found);
  1156. std::remove_copy_if(macro_def.macrodefinition.begin(),
  1157. macro_def.macrodefinition.end(),
  1158. std::inserter(replacement_list, replacement_list.end()),
  1159. concat_tag);
  1160. // handle concatenation operators
  1161. if (found && !concat_tokensequence(replacement_list))
  1162. return false;
  1163. }
  1164. }
  1165. else {
  1166. // called as an object like macro
  1167. if ((*it).second->is_functionlike) {
  1168. // defined as a function-like macro
  1169. if (0 != queue_symbol) {
  1170. queue_symbol->push_back(curr_token);
  1171. expanded.splice(expanded.end(), *queue_symbol);
  1172. }
  1173. else {
  1174. expanded.push_back(curr_token);
  1175. }
  1176. ++first; // skip macro name
  1177. return false; // no further preprocessing required
  1178. }
  1179. else {
  1180. // defined as an object-like macro (expand it)
  1181. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
  1182. ctx.get_hooks().expanding_object_like_macro(
  1183. macro_def.macroname, macro_def.macrodefinition, curr_token);
  1184. #else
  1185. if (ctx.get_hooks().expanding_object_like_macro(ctx.derived(),
  1186. macro_def.macroname, macro_def.macrodefinition, curr_token))
  1187. {
  1188. // do not expand this macro, just copy the whole sequence
  1189. expanded.push_back(curr_token);
  1190. ++first; // skip macro name
  1191. return false; // no further preprocessing required
  1192. }
  1193. #endif
  1194. bool found = false;
  1195. impl::find_concat_operator concat_tag(found);
  1196. std::remove_copy_if(macro_def.macrodefinition.begin(),
  1197. macro_def.macrodefinition.end(),
  1198. std::inserter(replacement_list, replacement_list.end()),
  1199. concat_tag);
  1200. // handle concatenation operators
  1201. if (found && !concat_tokensequence(replacement_list))
  1202. return false;
  1203. ++first; // skip macro name
  1204. }
  1205. }
  1206. // rescan the replacement list
  1207. ContainerT expanded_list;
  1208. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
  1209. ctx.get_hooks().expanded_macro(replacement_list);
  1210. #else
  1211. ctx.get_hooks().expanded_macro(ctx.derived(), replacement_list);
  1212. #endif
  1213. rescan_replacement_list(curr_token, macro_def, replacement_list,
  1214. expanded_list, expand_operator_defined, first, last);
  1215. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
  1216. ctx.get_hooks().rescanned_macro(expanded_list);
  1217. #else
  1218. ctx.get_hooks().rescanned_macro(ctx.derived(), expanded_list);
  1219. #endif
  1220. expanded.splice(expanded.end(), expanded_list);
  1221. return true; // rescan is required
  1222. }
  1223. ///////////////////////////////////////////////////////////////////////////////
  1224. //
  1225. // If the token under inspection points to a certain predefined macro it will
  1226. // be expanded, otherwise false is returned.
  1227. // (only __FILE__, __LINE__ and __INCLUDE_LEVEL__ macros are expanded here)
  1228. //
  1229. ///////////////////////////////////////////////////////////////////////////////
  1230. template <typename ContextT>
  1231. template <typename ContainerT>
  1232. inline bool
  1233. macromap<ContextT>::expand_predefined_macro(token_type const &curr_token,
  1234. ContainerT &expanded)
  1235. {
  1236. using namespace boost::wave;
  1237. string_type const &value = curr_token.get_value();
  1238. if (value.size() < 8 || '_' != value[0] || '_' != value[1])
  1239. return false; // quick check failed
  1240. if (value == "__LINE__") {
  1241. // expand the __LINE__ macro
  1242. std::string buffer = lexical_cast<std::string>(main_pos.get_line());
  1243. expanded.push_back(token_type(T_INTLIT, buffer.c_str(), curr_token.get_position()));
  1244. return true;
  1245. }
  1246. else if (value == "__FILE__") {
  1247. // expand the __FILE__ macro
  1248. namespace fs = boost::filesystem;
  1249. std::string file("\"");
  1250. fs::path filename(wave::util::create_path(main_pos.get_file().c_str()));
  1251. using boost::wave::util::impl::escape_lit;
  1252. file += escape_lit(wave::util::native_file_string(filename)) + "\"";
  1253. expanded.push_back(token_type(T_STRINGLIT, file.c_str(),
  1254. curr_token.get_position()));
  1255. return true;
  1256. }
  1257. else if (value == "__INCLUDE_LEVEL__") {
  1258. // expand the __INCLUDE_LEVEL__ macro
  1259. char buffer[22]; // 21 bytes holds all NUL-terminated unsigned 64-bit numbers
  1260. using namespace std; // for some systems sprintf is in namespace std
  1261. sprintf(buffer, "%d", (int)ctx.get_iteration_depth());
  1262. expanded.push_back(token_type(T_INTLIT, buffer, curr_token.get_position()));
  1263. return true;
  1264. }
  1265. return false; // no predefined token
  1266. }
  1267. ///////////////////////////////////////////////////////////////////////////////
  1268. //
  1269. // resolve_defined(): resolve the operator defined() and replace it with the
  1270. // correct T_INTLIT token
  1271. //
  1272. ///////////////////////////////////////////////////////////////////////////////
  1273. template <typename ContextT>
  1274. template <typename IteratorT, typename ContainerT>
  1275. inline typename ContextT::token_type const &
  1276. macromap<ContextT>::resolve_defined(IteratorT &first,
  1277. IteratorT const &last, ContainerT &pending)
  1278. {
  1279. using namespace boost::wave;
  1280. using namespace boost::wave::grammars;
  1281. ContainerT result;
  1282. IteratorT start = first;
  1283. boost::spirit::classic::parse_info<IteratorT> hit =
  1284. defined_grammar_gen<typename ContextT::lexer_type>::
  1285. parse_operator_defined(start, last, result);
  1286. if (!hit.hit) {
  1287. string_type msg ("defined(): ");
  1288. msg = msg + util::impl::as_string<string_type>(first, last);
  1289. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, ill_formed_expression,
  1290. msg.c_str(), main_pos);
  1291. // insert a dummy token
  1292. pending.push_back(token_type(T_INTLIT, "0", main_pos));
  1293. }
  1294. else {
  1295. impl::assign_iterator<IteratorT>::do_(first, hit.stop);
  1296. // insert a token, which reflects the outcome
  1297. pending.push_back(token_type(T_INTLIT,
  1298. is_defined(result.begin(), result.end()) ? "1" : "0",
  1299. main_pos));
  1300. }
  1301. on_exit::pop_front<definition_container_type> pop_front_token(pending);
  1302. return act_token = pending.front();
  1303. }
  1304. ///////////////////////////////////////////////////////////////////////////////
  1305. //
  1306. // resolve_operator_pragma(): resolve the operator _Pragma() and dispatch to
  1307. // the associated action
  1308. //
  1309. // This function returns true, if the pragma was correctly interpreted.
  1310. // The iterator 'first' is positioned behind the closing ')'.
  1311. // This function returns false, if the _Pragma was not known, the
  1312. // preprocessed token sequence is pushed back to the 'pending' sequence.
  1313. //
  1314. ///////////////////////////////////////////////////////////////////////////////
  1315. template <typename ContextT>
  1316. template <typename IteratorT, typename ContainerT>
  1317. inline bool
  1318. macromap<ContextT>::resolve_operator_pragma(IteratorT &first,
  1319. IteratorT const &last, ContainerT &pending, bool& seen_newline)
  1320. {
  1321. // isolate the parameter of the operator _Pragma
  1322. token_type pragma_token = *first;
  1323. if (!impl::skip_to_token(ctx, first, last, T_LEFTPAREN, seen_newline)) {
  1324. // illformed operator _Pragma
  1325. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, ill_formed_expression,
  1326. "operator _Pragma()", pragma_token.get_position());
  1327. return false;
  1328. }
  1329. std::vector<ContainerT> arguments;
  1330. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
  1331. typename std::vector<ContainerT>::size_type count_args =
  1332. collect_arguments (pragma_token, arguments, first, last, 1, seen_newline);
  1333. #else
  1334. IteratorT endparen = first;
  1335. typename std::vector<ContainerT>::size_type count_args =
  1336. collect_arguments (pragma_token, arguments, first, endparen, last, 1,
  1337. seen_newline);
  1338. #endif
  1339. // verify the parameter count
  1340. if (pragma_token.get_position().get_file().empty())
  1341. pragma_token.set_position(act_token.get_position());
  1342. if (count_args < 1 || arguments.size() < 1) {
  1343. // too few macro arguments
  1344. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, too_few_macroarguments,
  1345. pragma_token.get_value().c_str(), pragma_token.get_position());
  1346. return false;
  1347. }
  1348. if (count_args > 1 || arguments.size() > 1) {
  1349. // too many macro arguments
  1350. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, too_many_macroarguments,
  1351. pragma_token.get_value().c_str(), pragma_token.get_position());
  1352. return false;
  1353. }
  1354. // preprocess the pragma token body
  1355. typedef typename std::vector<ContainerT>::value_type::iterator
  1356. argument_iterator_type;
  1357. ContainerT expanded;
  1358. argument_iterator_type begin_it = arguments[0].begin();
  1359. argument_iterator_type end_it = arguments[0].end();
  1360. expand_whole_tokensequence(expanded, begin_it, end_it, false);
  1361. // un-escape the parameter of the operator _Pragma
  1362. typedef typename token_type::string_type string_type;
  1363. string_type pragma_cmd;
  1364. typename ContainerT::const_iterator end_exp = expanded.end();
  1365. for (typename ContainerT::const_iterator it_exp = expanded.begin();
  1366. it_exp != end_exp; ++it_exp)
  1367. {
  1368. if (T_EOF == token_id(*it_exp))
  1369. break;
  1370. if (IS_CATEGORY(*it_exp, WhiteSpaceTokenType))
  1371. continue;
  1372. if (T_STRINGLIT != token_id(*it_exp)) {
  1373. // ill formed operator _Pragma
  1374. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
  1375. ill_formed_pragma_option, "_Pragma",
  1376. pragma_token.get_position());
  1377. return false;
  1378. }
  1379. if (pragma_cmd.size() > 0) {
  1380. // there should be exactly one string literal (string literals are to
  1381. // be concatenated at translation phase 6, but _Pragma operators are
  1382. // to be executed at translation phase 4)
  1383. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
  1384. ill_formed_pragma_option, "_Pragma",
  1385. pragma_token.get_position());
  1386. return false;
  1387. }
  1388. // remove the '\"' and concat all given string literal-values
  1389. string_type token_str = (*it_exp).get_value();
  1390. pragma_cmd += token_str.substr(1, token_str.size() - 2);
  1391. }
  1392. string_type pragma_cmd_unesc = impl::unescape_lit(pragma_cmd);
  1393. // tokenize the pragma body
  1394. typedef typename ContextT::lexer_type lexer_type;
  1395. ContainerT pragma;
  1396. std::string pragma_cmd_str(pragma_cmd_unesc.c_str());
  1397. lexer_type it = lexer_type(pragma_cmd_str.begin(), pragma_cmd_str.end(),
  1398. pragma_token.get_position(), ctx.get_language());
  1399. lexer_type end = lexer_type();
  1400. for (/**/; it != end; ++it)
  1401. pragma.push_back(*it);
  1402. // analyze the preprocessed token sequence and eventually dispatch to the
  1403. // associated action
  1404. if (interpret_pragma(ctx, pragma_token, pragma.begin(), pragma.end(),
  1405. pending))
  1406. {
  1407. return true; // successfully recognized a wave specific pragma
  1408. }
  1409. // unknown pragma token sequence, push it back and return to the caller
  1410. pending.push_front(token_type(T_SPACE, " ", pragma_token.get_position()));
  1411. pending.push_front(token_type(T_RIGHTPAREN, ")", pragma_token.get_position()));
  1412. pending.push_front(token_type(T_STRINGLIT, string_type("\"") + pragma_cmd + "\"",
  1413. pragma_token.get_position()));
  1414. pending.push_front(token_type(T_LEFTPAREN, "(", pragma_token.get_position()));
  1415. pending.push_front(pragma_token);
  1416. return false;
  1417. }
  1418. ///////////////////////////////////////////////////////////////////////////////
  1419. //
  1420. // Test, whether the result of a concat operator is well formed or not.
  1421. //
  1422. // This is done by re-scanning (re-tokenizing) the resulting token sequence,
  1423. // which should give back exactly one token.
  1424. //
  1425. ///////////////////////////////////////////////////////////////////////////////
  1426. template <typename ContextT>
  1427. template <typename ContainerT>
  1428. inline bool
  1429. macromap<ContextT>::is_valid_concat(string_type new_value,
  1430. position_type const &pos, ContainerT &rescanned)
  1431. {
  1432. // re-tokenize the newly generated string
  1433. typedef typename ContextT::lexer_type lexer_type;
  1434. std::string value_to_test(new_value.c_str());
  1435. boost::wave::language_support lang =
  1436. boost::wave::enable_prefer_pp_numbers(ctx.get_language());
  1437. lang = boost::wave::enable_single_line(lang);
  1438. lexer_type it = lexer_type(value_to_test.begin(), value_to_test.end(), pos,
  1439. lang);
  1440. lexer_type end = lexer_type();
  1441. for (/**/; it != end && T_EOF != token_id(*it); ++it)
  1442. {
  1443. // as of Wave V2.0.7 pasting of tokens is valid only if the resulting
  1444. // tokens are pp_tokens (as mandated by C++11)
  1445. if (!is_pp_token(*it))
  1446. return false;
  1447. rescanned.push_back(*it);
  1448. }
  1449. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  1450. if (boost::wave::need_variadics(ctx.get_language()))
  1451. return true; // in variadics mode token pasting is well defined
  1452. #endif
  1453. // test if the newly generated token sequence contains more than 1 token
  1454. return 1 == rescanned.size();
  1455. }
  1456. ///////////////////////////////////////////////////////////////////////////////
  1457. //
  1458. // Handle all occurrences of the concatenation operator '##' inside the given
  1459. // token sequence.
  1460. //
  1461. ///////////////////////////////////////////////////////////////////////////////
  1462. template <typename Context>
  1463. inline void report_invalid_concatenation(Context& ctx,
  1464. typename Context::token_type const& prev,
  1465. typename Context::token_type const& next,
  1466. typename Context::position_type const& main_pos)
  1467. {
  1468. typename Context::string_type error_string("\"");
  1469. error_string += prev.get_value();
  1470. error_string += "\" and \"";
  1471. error_string += next.get_value();
  1472. error_string += "\"";
  1473. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, invalid_concat,
  1474. error_string.c_str(), main_pos);
  1475. }
  1476. template <typename ContextT>
  1477. template <typename ContainerT>
  1478. inline bool
  1479. macromap<ContextT>::concat_tokensequence(ContainerT &expanded)
  1480. {
  1481. using namespace boost::wave;
  1482. typedef typename ContainerT::iterator iterator_type;
  1483. iterator_type end = expanded.end();
  1484. iterator_type prev = end;
  1485. for (iterator_type it = expanded.begin(); it != end; /**/)
  1486. {
  1487. if (T_POUND_POUND == BASE_TOKEN(token_id(*it))) {
  1488. iterator_type next = it;
  1489. ++next;
  1490. if (prev == end || next == end) {
  1491. // error, '##' should be in between two tokens
  1492. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
  1493. ill_formed_operator, "concat ('##')", main_pos);
  1494. return false;
  1495. }
  1496. // replace prev##next with the concatenated value, skip whitespace
  1497. // before and after the '##' operator
  1498. while (IS_CATEGORY(*next, WhiteSpaceTokenType)) {
  1499. ++next;
  1500. if (next == end) {
  1501. // error, '##' should be in between two tokens
  1502. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
  1503. ill_formed_operator, "concat ('##')", main_pos);
  1504. return false;
  1505. }
  1506. }
  1507. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  1508. if (boost::wave::need_variadics(ctx.get_language())) {
  1509. if (T_PLACEMARKER == token_id(*next)) {
  1510. // remove the '##' and the next tokens from the sequence
  1511. iterator_type first_to_delete = prev;
  1512. expanded.erase(++first_to_delete, ++next);
  1513. it = next;
  1514. continue;
  1515. }
  1516. else if (T_PLACEMARKER == token_id(*prev)) {
  1517. // remove the '##' and the next tokens from the sequence
  1518. iterator_type first_to_delete = prev;
  1519. *prev = *next;
  1520. expanded.erase(++first_to_delete, ++next);
  1521. it = next;
  1522. continue;
  1523. }
  1524. }
  1525. #endif // BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  1526. // test if the concat operator has to concatenate two unrelated
  1527. // tokens i.e. the result yields more then one token
  1528. string_type concat_result;
  1529. ContainerT rescanned;
  1530. concat_result = ((*prev).get_value() + (*next).get_value());
  1531. // analyze the validity of the concatenation result
  1532. if (!is_valid_concat(concat_result, (*prev).get_position(),
  1533. rescanned) &&
  1534. !IS_CATEGORY(*prev, WhiteSpaceTokenType) &&
  1535. !IS_CATEGORY(*next, WhiteSpaceTokenType))
  1536. {
  1537. report_invalid_concatenation(ctx, *prev, *next, main_pos);
  1538. return false;
  1539. }
  1540. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  1541. if (boost::wave::need_variadics(ctx.get_language())) {
  1542. // remove the prev, '##' and the next tokens from the sequence
  1543. expanded.erase(prev, ++next); // remove not needed tokens
  1544. // some stl implementations clear() the container if we erased all
  1545. // the elements, which orphans all iterators. we re-initialize these
  1546. // here
  1547. if (expanded.empty())
  1548. end = next = expanded.end();
  1549. // replace the old token (pointed to by *prev) with the re-tokenized
  1550. // sequence
  1551. expanded.splice(next, rescanned);
  1552. // the last token of the inserted sequence is the new previous
  1553. prev = next;
  1554. if (next != expanded.end())
  1555. --prev;
  1556. }
  1557. else
  1558. #endif // BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  1559. {
  1560. // we leave the token_id unchanged, but unmark the token as
  1561. // disabled, if appropriate
  1562. (*prev).set_value(concat_result);
  1563. if (T_NONREPLACABLE_IDENTIFIER == token_id(*prev))
  1564. (*prev).set_token_id(T_IDENTIFIER);
  1565. // remove the '##' and the next tokens from the sequence
  1566. iterator_type first_to_delete = prev;
  1567. expanded.erase(++first_to_delete, ++next);
  1568. }
  1569. it = next;
  1570. continue;
  1571. }
  1572. // save last non-whitespace token position
  1573. if (!IS_CATEGORY(*it, WhiteSpaceTokenType))
  1574. prev = it;
  1575. ++it; // next token, please
  1576. }
  1577. return true;
  1578. }
  1579. ///////////////////////////////////////////////////////////////////////////////
  1580. //
  1581. // predefine_macro(): predefine a single macro
  1582. //
  1583. ///////////////////////////////////////////////////////////////////////////////
  1584. template <typename ContextT>
  1585. inline void
  1586. macromap<ContextT>::predefine_macro(defined_macros_type *scope,
  1587. string_type const &name, token_type const &t)
  1588. {
  1589. definition_container_type macrodefinition;
  1590. std::vector<token_type> param;
  1591. macrodefinition.push_back(t);
  1592. add_macro(token_type(T_IDENTIFIER, name, t.get_position()),
  1593. false, param, macrodefinition, true, scope);
  1594. }
  1595. ///////////////////////////////////////////////////////////////////////////////
  1596. //
  1597. // init_predefined_macros(): init the predefined macros
  1598. //
  1599. ///////////////////////////////////////////////////////////////////////////////
  1600. template <typename ContextT>
  1601. inline void
  1602. macromap<ContextT>::init_predefined_macros(char const *fname,
  1603. defined_macros_type *scope, bool at_global_scope)
  1604. {
  1605. // if no scope is given, use the current one
  1606. defined_macros_type *current_scope = scope ? scope : current_macros;
  1607. // first, add the static macros
  1608. position_type pos("<built-in>");
  1609. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  1610. if (boost::wave::need_c99(ctx.get_language())) {
  1611. // define C99 specifics
  1612. for (int i = 0; 0 != predef.static_data_c99(i).name; ++i) {
  1613. predefined_macros::static_macros const& m = predef.static_data_c99(i);
  1614. predefine_macro(current_scope, m.name,
  1615. token_type(m.token_id, m.value, pos));
  1616. }
  1617. }
  1618. else
  1619. #endif
  1620. {
  1621. #if BOOST_WAVE_SUPPORT_CPP0X != 0
  1622. if (boost::wave::need_cpp0x(ctx.get_language())) {
  1623. // define C++11 specifics
  1624. for (int i = 0; 0 != predef.static_data_cpp0x(i).name; ++i) {
  1625. predefined_macros::static_macros const& m = predef.static_data_cpp0x(i);
  1626. predefine_macro(current_scope, m.name,
  1627. token_type(m.token_id, m.value, pos));
  1628. }
  1629. }
  1630. else
  1631. #endif
  1632. {
  1633. // define C++ specifics
  1634. for (int i = 0; 0 != predef.static_data_cpp(i).name; ++i) {
  1635. predefined_macros::static_macros const& m = predef.static_data_cpp(i);
  1636. predefine_macro(current_scope, m.name,
  1637. token_type(m.token_id, m.value, pos));
  1638. }
  1639. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  1640. // define __WAVE_HAS_VARIADICS__, if appropriate
  1641. if (boost::wave::need_variadics(ctx.get_language())) {
  1642. predefine_macro(current_scope, "__WAVE_HAS_VARIADICS__",
  1643. token_type(T_INTLIT, "1", pos));
  1644. }
  1645. #endif
  1646. }
  1647. }
  1648. // predefine the __BASE_FILE__ macro which contains the main file name
  1649. namespace fs = boost::filesystem;
  1650. if (string_type(fname) != "<Unknown>") {
  1651. fs::path filename(create_path(fname));
  1652. using boost::wave::util::impl::escape_lit;
  1653. predefine_macro(current_scope, "__BASE_FILE__",
  1654. token_type(T_STRINGLIT, string_type("\"") +
  1655. escape_lit(native_file_string(filename)).c_str() + "\"", pos));
  1656. base_name = fname;
  1657. }
  1658. else if (!base_name.empty()) {
  1659. fs::path filename(create_path(base_name.c_str()));
  1660. using boost::wave::util::impl::escape_lit;
  1661. predefine_macro(current_scope, "__BASE_FILE__",
  1662. token_type(T_STRINGLIT, string_type("\"") +
  1663. escape_lit(native_file_string(filename)).c_str() + "\"", pos));
  1664. }
  1665. // now add the dynamic macros
  1666. for (int j = 0; 0 != predef.dynamic_data(j).name; ++j) {
  1667. predefined_macros::dynamic_macros const& m = predef.dynamic_data(j);
  1668. predefine_macro(current_scope, m.name,
  1669. token_type(m.token_id, (predef.* m.generator)(), pos));
  1670. }
  1671. }
  1672. ///////////////////////////////////////////////////////////////////////////////
  1673. //
  1674. // reset_macromap(): initialize the internal macro symbol namespace
  1675. //
  1676. ///////////////////////////////////////////////////////////////////////////////
  1677. template <typename ContextT>
  1678. inline void
  1679. macromap<ContextT>::reset_macromap()
  1680. {
  1681. current_macros->clear();
  1682. predef.reset();
  1683. act_token = token_type();
  1684. }
  1685. ///////////////////////////////////////////////////////////////////////////////
  1686. }}} // namespace boost::wave::util
  1687. #if BOOST_WAVE_SERIALIZATION != 0
  1688. namespace boost { namespace serialization {
  1689. template<typename ContextT>
  1690. struct version<boost::wave::util::macromap<ContextT> >
  1691. {
  1692. typedef boost::wave::util::macromap<ContextT> target_type;
  1693. typedef mpl::int_<target_type::version> type;
  1694. typedef mpl::integral_c_tag tag;
  1695. BOOST_STATIC_CONSTANT(unsigned int, value = version::type::value);
  1696. };
  1697. }} // namespace boost::serialization
  1698. #endif
  1699. // the suffix header occurs after all of the code
  1700. #ifdef BOOST_HAS_ABI_HEADERS
  1701. #include BOOST_ABI_SUFFIX
  1702. #endif
  1703. #endif // !defined(CPP_MACROMAP_HPP_CB8F51B0_A3F0_411C_AEF4_6FF631B8B414_INCLUDED)