cpp_macromap.hpp 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946
  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/wave/util/time_conversion_helper.hpp>
  28. #include <boost/wave/util/unput_queue_iterator.hpp>
  29. #include <boost/wave/util/macro_helpers.hpp>
  30. #include <boost/wave/util/macro_definition.hpp>
  31. #include <boost/wave/util/symbol_table.hpp>
  32. #include <boost/wave/util/cpp_macromap_utils.hpp>
  33. #include <boost/wave/util/cpp_macromap_predef.hpp>
  34. #include <boost/wave/util/filesystem_compatibility.hpp>
  35. #include <boost/wave/grammars/cpp_defined_grammar_gen.hpp>
  36. #include <boost/wave/wave_version.hpp>
  37. #include <boost/wave/cpp_exceptions.hpp>
  38. #include <boost/wave/language_support.hpp>
  39. // this must occur after all of the includes and before any code appears
  40. #ifdef BOOST_HAS_ABI_HEADERS
  41. #include BOOST_ABI_PREFIX
  42. #endif
  43. ///////////////////////////////////////////////////////////////////////////////
  44. namespace boost { namespace wave { namespace util {
  45. ///////////////////////////////////////////////////////////////////////////////
  46. //
  47. // macromap
  48. //
  49. // This class holds all currently defined macros and on demand expands
  50. // those macro definitions
  51. //
  52. ///////////////////////////////////////////////////////////////////////////////
  53. template <typename ContextT>
  54. class macromap {
  55. typedef macromap<ContextT> self_type;
  56. typedef typename ContextT::token_type token_type;
  57. typedef typename token_type::string_type string_type;
  58. typedef typename token_type::position_type position_type;
  59. typedef typename ContextT::token_sequence_type definition_container_type;
  60. typedef std::vector<token_type> parameter_container_type;
  61. typedef macro_definition<token_type, definition_container_type>
  62. macro_definition_type;
  63. typedef symbol_table<string_type, macro_definition_type>
  64. defined_macros_type;
  65. typedef typename defined_macros_type::value_type::second_type
  66. macro_ref_type;
  67. public:
  68. macromap(ContextT &ctx_)
  69. : current_macros(0), defined_macros(new defined_macros_type(1)),
  70. main_pos("", 0), ctx(ctx_), macro_uid(1)
  71. {
  72. current_macros = defined_macros.get();
  73. }
  74. ~macromap() {}
  75. // Add a new macro to the given macro scope
  76. bool add_macro(token_type const &name, bool has_parameters,
  77. parameter_container_type &parameters,
  78. definition_container_type &definition, bool is_predefined = false,
  79. defined_macros_type *scope = 0);
  80. // Tests, whether the given macro name is defined in the given macro scope
  81. bool is_defined(string_type const &name,
  82. typename defined_macros_type::iterator &it,
  83. defined_macros_type *scope = 0) const;
  84. // expects a token sequence as its parameters
  85. template <typename IteratorT>
  86. bool is_defined(IteratorT const &begin, IteratorT const &end) const;
  87. // expects an arbitrary string as its parameter
  88. bool is_defined(string_type const &str) const;
  89. // Get the macro definition for the given macro scope
  90. bool get_macro(string_type const &name, bool &has_parameters,
  91. bool &is_predefined, position_type &pos,
  92. parameter_container_type &parameters,
  93. definition_container_type &definition,
  94. defined_macros_type *scope = 0) const;
  95. // Remove a macro name from the given macro scope
  96. bool remove_macro(string_type const &name, position_type const& pos,
  97. bool even_predefined = false);
  98. template <typename IteratorT, typename ContainerT>
  99. token_type const &expand_tokensequence(IteratorT &first,
  100. IteratorT const &last, ContainerT &pending, ContainerT &expanded,
  101. bool& seen_newline, bool expand_operator_defined);
  102. // Expand all macros inside the given token sequence
  103. template <typename IteratorT, typename ContainerT>
  104. void expand_whole_tokensequence(ContainerT &expanded,
  105. IteratorT &first, IteratorT const &last,
  106. bool expand_operator_defined);
  107. // Init the predefined macros (add them to the given scope)
  108. void init_predefined_macros(char const *fname = "<Unknown>",
  109. defined_macros_type *scope = 0, bool at_global_scope = true);
  110. void predefine_macro(defined_macros_type *scope, string_type const &name,
  111. token_type const &t);
  112. // Init the internal macro symbol namespace
  113. void reset_macromap();
  114. position_type &get_main_pos() { return main_pos; }
  115. position_type const& get_main_pos() const { return main_pos; }
  116. // interface for macro name introspection
  117. typedef typename defined_macros_type::name_iterator name_iterator;
  118. typedef typename defined_macros_type::const_name_iterator const_name_iterator;
  119. name_iterator begin()
  120. { return defined_macros_type::make_iterator(current_macros->begin()); }
  121. name_iterator end()
  122. { return defined_macros_type::make_iterator(current_macros->end()); }
  123. const_name_iterator begin() const
  124. { return defined_macros_type::make_iterator(current_macros->begin()); }
  125. const_name_iterator end() const
  126. { return defined_macros_type::make_iterator(current_macros->end()); }
  127. protected:
  128. // Helper functions for expanding all macros in token sequences
  129. template <typename IteratorT, typename ContainerT>
  130. token_type const &expand_tokensequence_worker(ContainerT &pending,
  131. unput_queue_iterator<IteratorT, token_type, ContainerT> &first,
  132. unput_queue_iterator<IteratorT, token_type, ContainerT> const &last,
  133. bool& seen_newline, bool expand_operator_defined);
  134. // Collect all arguments supplied to a macro invocation
  135. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
  136. template <typename IteratorT, typename ContainerT, typename SizeT>
  137. typename std::vector<ContainerT>::size_type collect_arguments (
  138. token_type const curr_token, std::vector<ContainerT> &arguments,
  139. IteratorT &next, IteratorT const &end, SizeT const &parameter_count,
  140. bool& seen_newline);
  141. #else
  142. template <typename IteratorT, typename ContainerT, typename SizeT>
  143. typename std::vector<ContainerT>::size_type collect_arguments (
  144. token_type const curr_token, std::vector<ContainerT> &arguments,
  145. IteratorT &next, IteratorT &endparen, IteratorT const &end,
  146. SizeT const &parameter_count, bool& seen_newline);
  147. #endif
  148. // Expand a single macro name
  149. template <typename IteratorT, typename ContainerT>
  150. bool expand_macro(ContainerT &pending, token_type const &name,
  151. typename defined_macros_type::iterator it,
  152. IteratorT &first, IteratorT const &last,
  153. bool& seen_newline, bool expand_operator_defined,
  154. defined_macros_type *scope = 0, ContainerT *queue_symbol = 0);
  155. // Expand a predefined macro (__LINE__, __FILE__ and __INCLUDE_LEVEL__)
  156. template <typename ContainerT>
  157. bool expand_predefined_macro(token_type const &curr_token,
  158. ContainerT &expanded);
  159. // Expand a single macro argument
  160. template <typename ContainerT>
  161. void expand_argument (typename std::vector<ContainerT>::size_type arg,
  162. std::vector<ContainerT> &arguments,
  163. std::vector<ContainerT> &expanded_args, bool expand_operator_defined,
  164. std::vector<bool> &has_expanded_args);
  165. // Expand the replacement list (replaces parameters with arguments)
  166. template <typename ContainerT>
  167. void expand_replacement_list(
  168. macro_definition_type const &macrodefinition,
  169. std::vector<ContainerT> &arguments,
  170. bool expand_operator_defined, ContainerT &expanded);
  171. // Rescans the replacement list for macro expansion
  172. template <typename IteratorT, typename ContainerT>
  173. void rescan_replacement_list(token_type const &curr_token,
  174. macro_definition_type &macrodef, ContainerT &replacement_list,
  175. ContainerT &expanded, bool expand_operator_defined,
  176. IteratorT &nfirst, IteratorT const &nlast);
  177. // Resolves the operator defined() and replces the token with "0" or "1"
  178. template <typename IteratorT, typename ContainerT>
  179. token_type const &resolve_defined(IteratorT &first, IteratorT const &last,
  180. ContainerT &expanded);
  181. // Resolve operator _Pragma or the #pragma directive
  182. template <typename IteratorT, typename ContainerT>
  183. bool resolve_operator_pragma(IteratorT &first,
  184. IteratorT const &last, ContainerT &expanded, bool& seen_newline);
  185. // Handle the concatenation operator '##'
  186. template <typename ContainerT>
  187. bool concat_tokensequence(ContainerT &expanded);
  188. template <typename ContainerT>
  189. bool is_valid_concat(string_type new_value,
  190. position_type const &pos, ContainerT &rescanned);
  191. #if BOOST_WAVE_SERIALIZATION != 0
  192. public:
  193. BOOST_STATIC_CONSTANT(unsigned int, version = 0x10);
  194. BOOST_STATIC_CONSTANT(unsigned int, version_mask = 0x0f);
  195. private:
  196. friend class boost::serialization::access;
  197. template<typename Archive>
  198. void save(Archive &ar, const unsigned int version) const
  199. {
  200. using namespace boost::serialization;
  201. ar & make_nvp("defined_macros", defined_macros);
  202. }
  203. template<typename Archive>
  204. void load(Archive &ar, const unsigned int loaded_version)
  205. {
  206. using namespace boost::serialization;
  207. if (version != (loaded_version & ~version_mask)) {
  208. BOOST_WAVE_THROW(preprocess_exception, incompatible_config,
  209. "cpp_context state version", get_main_pos());
  210. }
  211. ar & make_nvp("defined_macros", defined_macros);
  212. current_macros = defined_macros.get();
  213. }
  214. BOOST_SERIALIZATION_SPLIT_MEMBER()
  215. #endif
  216. private:
  217. defined_macros_type *current_macros; // current symbol table
  218. boost::shared_ptr<defined_macros_type> defined_macros; // global symbol table
  219. token_type act_token; // current token
  220. position_type main_pos; // last token position in the pp_iterator
  221. string_type base_name; // the name to be expanded by __BASE_FILE__
  222. ContextT &ctx; // context object associated with the macromap
  223. long macro_uid;
  224. predefined_macros predef; // predefined macro support
  225. };
  226. ///////////////////////////////////////////////////////////////////////////////
  227. ///////////////////////////////////////////////////////////////////////////////
  228. //
  229. // add_macro(): adds a new macro to the macromap
  230. //
  231. ///////////////////////////////////////////////////////////////////////////////
  232. template <typename ContextT>
  233. inline bool
  234. macromap<ContextT>::add_macro(token_type const &name, bool has_parameters,
  235. parameter_container_type &parameters, definition_container_type &definition,
  236. bool is_predefined, defined_macros_type *scope)
  237. {
  238. if (!is_predefined && impl::is_special_macroname (name.get_value())) {
  239. // exclude special macro names
  240. BOOST_WAVE_THROW_NAME_CTX(ctx, macro_handling_exception,
  241. illegal_redefinition, name.get_value().c_str(), main_pos,
  242. name.get_value().c_str());
  243. return false;
  244. }
  245. if (boost::wave::need_variadics(ctx.get_language()) &&
  246. "__VA_ARGS__" == name.get_value())
  247. {
  248. // can't use __VA_ARGS__ as a macro name
  249. BOOST_WAVE_THROW_NAME_CTX(ctx, macro_handling_exception,
  250. bad_define_statement_va_args, name.get_value().c_str(), main_pos,
  251. name.get_value().c_str());
  252. return false;
  253. }
  254. if (AltExtTokenType == (token_id(name) & ExtTokenOnlyMask)) {
  255. // exclude special operator names
  256. BOOST_WAVE_THROW_NAME_CTX(ctx, macro_handling_exception,
  257. illegal_operator_redefinition, name.get_value().c_str(), main_pos,
  258. name.get_value().c_str());
  259. return false;
  260. }
  261. // try to define the new macro
  262. defined_macros_type *current_scope = scope ? scope : current_macros;
  263. typename defined_macros_type::iterator it = current_scope->find(name.get_value());
  264. if (it != current_scope->end()) {
  265. // redefinition, should not be different
  266. macro_definition_type* macrodef = (*it).second.get();
  267. if (macrodef->is_functionlike != has_parameters ||
  268. !impl::parameters_equal(macrodef->macroparameters, parameters) ||
  269. !impl::definition_equals(macrodef->macrodefinition, definition))
  270. {
  271. BOOST_WAVE_THROW_NAME_CTX(ctx, macro_handling_exception,
  272. macro_redefinition, name.get_value().c_str(), main_pos,
  273. name.get_value().c_str());
  274. }
  275. return false;
  276. }
  277. // test the validity of the parameter names
  278. if (has_parameters) {
  279. std::set<typename token_type::string_type> names;
  280. typedef typename parameter_container_type::iterator
  281. parameter_iterator_type;
  282. typedef typename std::set<typename token_type::string_type>::iterator
  283. name_iterator_type;
  284. parameter_iterator_type end = parameters.end();
  285. for (parameter_iterator_type itp = parameters.begin(); itp != end; ++itp)
  286. {
  287. name_iterator_type pit = names.find((*itp).get_value());
  288. if (pit != names.end()) {
  289. // duplicate parameter name
  290. BOOST_WAVE_THROW_NAME_CTX(ctx, macro_handling_exception,
  291. duplicate_parameter_name, (*pit).c_str(), main_pos,
  292. name.get_value().c_str());
  293. return false;
  294. }
  295. names.insert((*itp).get_value());
  296. }
  297. }
  298. // insert a new macro node
  299. std::pair<typename defined_macros_type::iterator, bool> p =
  300. current_scope->insert(
  301. typename defined_macros_type::value_type(
  302. name.get_value(),
  303. macro_ref_type(new macro_definition_type(name,
  304. has_parameters, is_predefined, ++macro_uid)
  305. )
  306. )
  307. );
  308. if (!p.second) {
  309. BOOST_WAVE_THROW_NAME_CTX(ctx, macro_handling_exception,
  310. macro_insertion_error, name.get_value().c_str(), main_pos,
  311. name.get_value().c_str());
  312. return false;
  313. }
  314. // add the parameters and the definition
  315. std::swap((*p.first).second->macroparameters, parameters);
  316. std::swap((*p.first).second->macrodefinition, definition);
  317. // call the context supplied preprocessing hook
  318. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
  319. ctx.get_hooks().defined_macro(name, has_parameters,
  320. (*p.first).second->macroparameters,
  321. (*p.first).second->macrodefinition, is_predefined);
  322. #else
  323. ctx.get_hooks().defined_macro(ctx.derived(), name, has_parameters,
  324. (*p.first).second->macroparameters,
  325. (*p.first).second->macrodefinition, is_predefined);
  326. #endif
  327. return true;
  328. }
  329. ///////////////////////////////////////////////////////////////////////////////
  330. //
  331. // is_defined(): returns, whether a given macro is already defined
  332. //
  333. ///////////////////////////////////////////////////////////////////////////////
  334. template <typename ContextT>
  335. inline bool
  336. macromap<ContextT>::is_defined(typename token_type::string_type const &name,
  337. typename defined_macros_type::iterator &it,
  338. defined_macros_type *scope) const
  339. {
  340. if (0 == scope) scope = current_macros;
  341. if ((it = scope->find(name)) != scope->end())
  342. return true; // found in symbol table
  343. // quick pre-check
  344. if (name.size() < 8 || '_' != name[0] || '_' != name[1])
  345. return false; // quick check failed
  346. return name == "__LINE__" || name == "__FILE__" ||
  347. name == "__INCLUDE_LEVEL__";
  348. }
  349. template <typename ContextT>
  350. template <typename IteratorT>
  351. inline bool
  352. macromap<ContextT>::is_defined(IteratorT const &begin,
  353. IteratorT const &end) const
  354. {
  355. // in normal mode the name under inspection should consist of an identifier
  356. // only
  357. token_id id = token_id(*begin);
  358. if (T_IDENTIFIER != id &&
  359. !IS_CATEGORY(id, KeywordTokenType) &&
  360. !IS_EXTCATEGORY(id, OperatorTokenType|AltExtTokenType) &&
  361. !IS_CATEGORY(id, BoolLiteralTokenType))
  362. {
  363. std::string msg(impl::get_full_name(begin, end));
  364. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, invalid_macroname,
  365. msg.c_str(), main_pos);
  366. return false;
  367. }
  368. IteratorT it = begin;
  369. string_type name ((*it).get_value());
  370. typename defined_macros_type::iterator cit;
  371. if (++it != end) {
  372. // there should be only one token as the inspected name
  373. std::string msg(impl::get_full_name(begin, end));
  374. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, invalid_macroname,
  375. msg.c_str(), main_pos);
  376. return false;
  377. }
  378. return is_defined(name, cit, 0);
  379. }
  380. ///////////////////////////////////////////////////////////////////////////////
  381. // same as above, only takes an arbitrary string type as its parameter
  382. template <typename ContextT>
  383. inline bool
  384. macromap<ContextT>::is_defined(string_type const &str) const
  385. {
  386. typename defined_macros_type::iterator cit;
  387. return is_defined(str, cit, 0);
  388. }
  389. ///////////////////////////////////////////////////////////////////////////////
  390. //
  391. // Get the macro definition for the given macro scope
  392. //
  393. ///////////////////////////////////////////////////////////////////////////////
  394. template <typename ContextT>
  395. inline bool
  396. macromap<ContextT>::get_macro(string_type const &name, bool &has_parameters,
  397. bool &is_predefined, position_type &pos,
  398. parameter_container_type &parameters,
  399. definition_container_type &definition,
  400. defined_macros_type *scope) const
  401. {
  402. typename defined_macros_type::iterator it;
  403. if (!is_defined(name, it, scope))
  404. return false;
  405. macro_definition_type &macro_def = *(*it).second.get();
  406. has_parameters = macro_def.is_functionlike;
  407. is_predefined = macro_def.is_predefined;
  408. pos = macro_def.macroname.get_position();
  409. parameters = macro_def.macroparameters;
  410. definition = macro_def.macrodefinition;
  411. return true;
  412. }
  413. ///////////////////////////////////////////////////////////////////////////////
  414. //
  415. // remove_macro(): remove a macro from the macromap
  416. //
  417. ///////////////////////////////////////////////////////////////////////////////
  418. template <typename ContextT>
  419. inline bool
  420. macromap<ContextT>::remove_macro(string_type const &name,
  421. position_type const& pos, bool even_predefined)
  422. {
  423. typename defined_macros_type::iterator it = current_macros->find(name);
  424. if (it != current_macros->end()) {
  425. if ((*it).second->is_predefined) {
  426. if (!even_predefined || impl::is_special_macroname(name)) {
  427. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
  428. bad_undefine_statement, name.c_str(), main_pos);
  429. return false;
  430. }
  431. }
  432. current_macros->erase(it);
  433. // call the context supplied preprocessing hook function
  434. token_type tok(T_IDENTIFIER, name, pos);
  435. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
  436. ctx.get_hooks().undefined_macro(tok);
  437. #else
  438. ctx.get_hooks().undefined_macro(ctx.derived(), tok);
  439. #endif
  440. return true;
  441. }
  442. else if (impl::is_special_macroname(name)) {
  443. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, bad_undefine_statement,
  444. name.c_str(), pos);
  445. }
  446. return false; // macro was not defined
  447. }
  448. ///////////////////////////////////////////////////////////////////////////////
  449. //
  450. // expand_tokensequence
  451. //
  452. // This function is a helper function which wraps the given iterator
  453. // range into corresponding unput_iterator's and calls the main workhorse
  454. // of the macro expansion engine (the function expand_tokensequence_worker)
  455. //
  456. // This is the top level macro expansion function called from the
  457. // preprocessing iterator component only.
  458. //
  459. ///////////////////////////////////////////////////////////////////////////////
  460. template <typename ContextT>
  461. template <typename IteratorT, typename ContainerT>
  462. inline typename ContextT::token_type const &
  463. macromap<ContextT>::expand_tokensequence(IteratorT &first,
  464. IteratorT const &last, ContainerT &pending, ContainerT &expanded,
  465. bool& seen_newline, bool expand_operator_defined)
  466. {
  467. typedef impl::gen_unput_queue_iterator<IteratorT, token_type, ContainerT>
  468. gen_type;
  469. typedef typename gen_type::return_type iterator_type;
  470. iterator_type first_it = gen_type::generate(expanded, first);
  471. iterator_type last_it = gen_type::generate(last);
  472. on_exit::assign<IteratorT, iterator_type> on_exit(first, first_it);
  473. return expand_tokensequence_worker(pending, first_it, last_it,
  474. seen_newline, expand_operator_defined);
  475. }
  476. ///////////////////////////////////////////////////////////////////////////////
  477. //
  478. // expand_tokensequence_worker
  479. //
  480. // This function is the main workhorse of the macro expansion engine. It
  481. // expands as much tokens as needed to identify the next preprocessed
  482. // token to return to the caller.
  483. // It returns the next preprocessed token.
  484. //
  485. // The iterator 'first' is adjusted accordingly.
  486. //
  487. ///////////////////////////////////////////////////////////////////////////////
  488. template <typename ContextT>
  489. template <typename IteratorT, typename ContainerT>
  490. inline typename ContextT::token_type const &
  491. macromap<ContextT>::expand_tokensequence_worker(
  492. ContainerT &pending,
  493. unput_queue_iterator<IteratorT, token_type, ContainerT> &first,
  494. unput_queue_iterator<IteratorT, token_type, ContainerT> const &last,
  495. bool& seen_newline, bool expand_operator_defined)
  496. {
  497. // if there exist pending tokens (tokens, which are already preprocessed), then
  498. // return the next one from there
  499. if (!pending.empty()) {
  500. on_exit::pop_front<definition_container_type> pop_front_token(pending);
  501. return act_token = pending.front();
  502. }
  503. // analyze the next element of the given sequence, if it is an
  504. // T_IDENTIFIER token, try to replace this as a macro etc.
  505. using namespace boost::wave;
  506. typedef unput_queue_iterator<IteratorT, token_type, ContainerT> iterator_type;
  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 (static_cast<unsigned int>(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. char buffer[22]; // 21 bytes holds all NUL-terminated unsigned 64-bit numbers
  1243. using namespace std; // for some systems sprintf is in namespace std
  1244. sprintf(buffer, "%ld", main_pos.get_line());
  1245. expanded.push_back(token_type(T_INTLIT, buffer, curr_token.get_position()));
  1246. return true;
  1247. }
  1248. else if (value == "__FILE__") {
  1249. // expand the __FILE__ macro
  1250. namespace fs = boost::filesystem;
  1251. std::string file("\"");
  1252. fs::path filename(wave::util::create_path(main_pos.get_file().c_str()));
  1253. using boost::wave::util::impl::escape_lit;
  1254. file += escape_lit(wave::util::native_file_string(filename)) + "\"";
  1255. expanded.push_back(token_type(T_STRINGLIT, file.c_str(),
  1256. curr_token.get_position()));
  1257. return true;
  1258. }
  1259. else if (value == "__INCLUDE_LEVEL__") {
  1260. // expand the __INCLUDE_LEVEL__ macro
  1261. char buffer[22]; // 21 bytes holds all NUL-terminated unsigned 64-bit numbers
  1262. using namespace std; // for some systems sprintf is in namespace std
  1263. sprintf(buffer, "%d", (int)ctx.get_iteration_depth());
  1264. expanded.push_back(token_type(T_INTLIT, buffer, curr_token.get_position()));
  1265. return true;
  1266. }
  1267. return false; // no predefined token
  1268. }
  1269. ///////////////////////////////////////////////////////////////////////////////
  1270. //
  1271. // resolve_defined(): resolve the operator defined() and replace it with the
  1272. // correct T_INTLIT token
  1273. //
  1274. ///////////////////////////////////////////////////////////////////////////////
  1275. template <typename ContextT>
  1276. template <typename IteratorT, typename ContainerT>
  1277. inline typename ContextT::token_type const &
  1278. macromap<ContextT>::resolve_defined(IteratorT &first,
  1279. IteratorT const &last, ContainerT &pending)
  1280. {
  1281. using namespace boost::wave;
  1282. using namespace boost::wave::grammars;
  1283. ContainerT result;
  1284. IteratorT start = first;
  1285. boost::spirit::classic::parse_info<IteratorT> hit =
  1286. defined_grammar_gen<typename ContextT::lexer_type>::
  1287. parse_operator_defined(start, last, result);
  1288. if (!hit.hit) {
  1289. string_type msg ("defined(): ");
  1290. msg = msg + util::impl::as_string<string_type>(first, last);
  1291. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, ill_formed_expression,
  1292. msg.c_str(), main_pos);
  1293. // insert a dummy token
  1294. pending.push_back(token_type(T_INTLIT, "0", main_pos));
  1295. }
  1296. else {
  1297. impl::assign_iterator<IteratorT>::do_(first, hit.stop);
  1298. // insert a token, which reflects the outcome
  1299. pending.push_back(token_type(T_INTLIT,
  1300. is_defined(result.begin(), result.end()) ? "1" : "0",
  1301. main_pos));
  1302. }
  1303. on_exit::pop_front<definition_container_type> pop_front_token(pending);
  1304. return act_token = pending.front();
  1305. }
  1306. ///////////////////////////////////////////////////////////////////////////////
  1307. //
  1308. // resolve_operator_pragma(): resolve the operator _Pragma() and dispatch to
  1309. // the associated action
  1310. //
  1311. // This function returns true, if the pragma was correctly interpreted.
  1312. // The iterator 'first' is positioned behind the closing ')'.
  1313. // This function returns false, if the _Pragma was not known, the
  1314. // preprocessed token sequence is pushed back to the 'pending' sequence.
  1315. //
  1316. ///////////////////////////////////////////////////////////////////////////////
  1317. template <typename ContextT>
  1318. template <typename IteratorT, typename ContainerT>
  1319. inline bool
  1320. macromap<ContextT>::resolve_operator_pragma(IteratorT &first,
  1321. IteratorT const &last, ContainerT &pending, bool& seen_newline)
  1322. {
  1323. // isolate the parameter of the operator _Pragma
  1324. token_type pragma_token = *first;
  1325. if (!impl::skip_to_token(ctx, first, last, T_LEFTPAREN, seen_newline)) {
  1326. // illformed operator _Pragma
  1327. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, ill_formed_expression,
  1328. "operator _Pragma()", pragma_token.get_position());
  1329. return false;
  1330. }
  1331. std::vector<ContainerT> arguments;
  1332. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
  1333. typename std::vector<ContainerT>::size_type count_args =
  1334. collect_arguments (pragma_token, arguments, first, last, 1, seen_newline);
  1335. #else
  1336. IteratorT endparen = first;
  1337. typename std::vector<ContainerT>::size_type count_args =
  1338. collect_arguments (pragma_token, arguments, first, endparen, last, 1,
  1339. seen_newline);
  1340. #endif
  1341. // verify the parameter count
  1342. if (pragma_token.get_position().get_file().empty())
  1343. pragma_token.set_position(act_token.get_position());
  1344. if (count_args < 1 || arguments.size() < 1) {
  1345. // too few macro arguments
  1346. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, too_few_macroarguments,
  1347. pragma_token.get_value().c_str(), pragma_token.get_position());
  1348. return false;
  1349. }
  1350. if (count_args > 1 || arguments.size() > 1) {
  1351. // too many macro arguments
  1352. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, too_many_macroarguments,
  1353. pragma_token.get_value().c_str(), pragma_token.get_position());
  1354. return false;
  1355. }
  1356. // preprocess the pragma token body
  1357. typedef typename std::vector<ContainerT>::value_type::iterator
  1358. argument_iterator_type;
  1359. ContainerT expanded;
  1360. argument_iterator_type begin_it = arguments[0].begin();
  1361. argument_iterator_type end_it = arguments[0].end();
  1362. expand_whole_tokensequence(expanded, begin_it, end_it, false);
  1363. // un-escape the parameter of the operator _Pragma
  1364. typedef typename token_type::string_type string_type;
  1365. string_type pragma_cmd;
  1366. typename ContainerT::const_iterator end_exp = expanded.end();
  1367. for (typename ContainerT::const_iterator it_exp = expanded.begin();
  1368. it_exp != end_exp; ++it_exp)
  1369. {
  1370. if (T_EOF == token_id(*it_exp))
  1371. break;
  1372. if (IS_CATEGORY(*it_exp, WhiteSpaceTokenType))
  1373. continue;
  1374. if (T_STRINGLIT != token_id(*it_exp)) {
  1375. // ill formed operator _Pragma
  1376. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
  1377. ill_formed_pragma_option, "_Pragma",
  1378. pragma_token.get_position());
  1379. return false;
  1380. }
  1381. if (pragma_cmd.size() > 0) {
  1382. // there should be exactly one string literal (string literals are to
  1383. // be concatenated at translation phase 6, but _Pragma operators are
  1384. // to be executed at translation phase 4)
  1385. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
  1386. ill_formed_pragma_option, "_Pragma",
  1387. pragma_token.get_position());
  1388. return false;
  1389. }
  1390. // remove the '\"' and concat all given string literal-values
  1391. string_type token_str = (*it_exp).get_value();
  1392. pragma_cmd += token_str.substr(1, token_str.size() - 2);
  1393. }
  1394. string_type pragma_cmd_unesc = impl::unescape_lit(pragma_cmd);
  1395. // tokenize the pragma body
  1396. typedef typename ContextT::lexer_type lexer_type;
  1397. ContainerT pragma;
  1398. std::string pragma_cmd_str(pragma_cmd_unesc.c_str());
  1399. lexer_type it = lexer_type(pragma_cmd_str.begin(), pragma_cmd_str.end(),
  1400. pragma_token.get_position(), ctx.get_language());
  1401. lexer_type end = lexer_type();
  1402. for (/**/; it != end; ++it)
  1403. pragma.push_back(*it);
  1404. // analyze the preprocessed token sequence and eventually dispatch to the
  1405. // associated action
  1406. if (interpret_pragma(ctx, pragma_token, pragma.begin(), pragma.end(),
  1407. pending))
  1408. {
  1409. return true; // successfully recognized a wave specific pragma
  1410. }
  1411. // unknown pragma token sequence, push it back and return to the caller
  1412. pending.push_front(token_type(T_SPACE, " ", pragma_token.get_position()));
  1413. pending.push_front(token_type(T_RIGHTPAREN, ")", pragma_token.get_position()));
  1414. pending.push_front(token_type(T_STRINGLIT, string_type("\"") + pragma_cmd + "\"",
  1415. pragma_token.get_position()));
  1416. pending.push_front(token_type(T_LEFTPAREN, "(", pragma_token.get_position()));
  1417. pending.push_front(pragma_token);
  1418. return false;
  1419. }
  1420. ///////////////////////////////////////////////////////////////////////////////
  1421. //
  1422. // Test, whether the result of a concat operator is well formed or not.
  1423. //
  1424. // This is done by re-scanning (re-tokenizing) the resulting token sequence,
  1425. // which should give back exactly one token.
  1426. //
  1427. ///////////////////////////////////////////////////////////////////////////////
  1428. template <typename ContextT>
  1429. template <typename ContainerT>
  1430. inline bool
  1431. macromap<ContextT>::is_valid_concat(string_type new_value,
  1432. position_type const &pos, ContainerT &rescanned)
  1433. {
  1434. // re-tokenize the newly generated string
  1435. typedef typename ContextT::lexer_type lexer_type;
  1436. std::string value_to_test(new_value.c_str());
  1437. boost::wave::language_support lang =
  1438. boost::wave::enable_prefer_pp_numbers(ctx.get_language());
  1439. lang = boost::wave::enable_single_line(lang);
  1440. lexer_type it = lexer_type(value_to_test.begin(), value_to_test.end(), pos,
  1441. lang);
  1442. lexer_type end = lexer_type();
  1443. for (/**/; it != end && T_EOF != token_id(*it); ++it)
  1444. {
  1445. // as of Wave V2.0.7 pasting of tokens is valid only if the resulting
  1446. // tokens are pp_tokens (as mandated by C++11)
  1447. if (!is_pp_token(*it))
  1448. return false;
  1449. rescanned.push_back(*it);
  1450. }
  1451. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  1452. if (boost::wave::need_variadics(ctx.get_language()))
  1453. return true; // in variadics mode token pasting is well defined
  1454. #endif
  1455. // test if the newly generated token sequence contains more than 1 token
  1456. return 1 == rescanned.size();
  1457. }
  1458. ///////////////////////////////////////////////////////////////////////////////
  1459. //
  1460. // Handle all occurrences of the concatenation operator '##' inside the given
  1461. // token sequence.
  1462. //
  1463. ///////////////////////////////////////////////////////////////////////////////
  1464. template <typename Context>
  1465. inline void report_invalid_concatenation(Context& ctx,
  1466. typename Context::token_type const& prev,
  1467. typename Context::token_type const& next,
  1468. typename Context::position_type const& main_pos)
  1469. {
  1470. typename Context::string_type error_string("\"");
  1471. error_string += prev.get_value();
  1472. error_string += "\" and \"";
  1473. error_string += next.get_value();
  1474. error_string += "\"";
  1475. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, invalid_concat,
  1476. error_string.c_str(), main_pos);
  1477. }
  1478. template <typename ContextT>
  1479. template <typename ContainerT>
  1480. inline bool
  1481. macromap<ContextT>::concat_tokensequence(ContainerT &expanded)
  1482. {
  1483. using namespace boost::wave;
  1484. typedef typename ContainerT::iterator iterator_type;
  1485. iterator_type end = expanded.end();
  1486. iterator_type prev = end;
  1487. for (iterator_type it = expanded.begin(); it != end; /**/)
  1488. {
  1489. if (T_POUND_POUND == BASE_TOKEN(token_id(*it))) {
  1490. iterator_type next = it;
  1491. ++next;
  1492. if (prev == end || next == end) {
  1493. // error, '##' should be in between two tokens
  1494. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
  1495. ill_formed_operator, "concat ('##')", main_pos);
  1496. return false;
  1497. }
  1498. // replace prev##next with the concatenated value, skip whitespace
  1499. // before and after the '##' operator
  1500. while (IS_CATEGORY(*next, WhiteSpaceTokenType)) {
  1501. ++next;
  1502. if (next == end) {
  1503. // error, '##' should be in between two tokens
  1504. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
  1505. ill_formed_operator, "concat ('##')", main_pos);
  1506. return false;
  1507. }
  1508. }
  1509. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  1510. if (boost::wave::need_variadics(ctx.get_language())) {
  1511. if (T_PLACEMARKER == token_id(*next)) {
  1512. // remove the '##' and the next tokens from the sequence
  1513. iterator_type first_to_delete = prev;
  1514. expanded.erase(++first_to_delete, ++next);
  1515. it = next;
  1516. continue;
  1517. }
  1518. else if (T_PLACEMARKER == token_id(*prev)) {
  1519. // remove the '##' and the next tokens from the sequence
  1520. iterator_type first_to_delete = prev;
  1521. *prev = *next;
  1522. expanded.erase(++first_to_delete, ++next);
  1523. it = next;
  1524. continue;
  1525. }
  1526. }
  1527. #endif // BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  1528. // test if the concat operator has to concatenate two unrelated
  1529. // tokens i.e. the result yields more then one token
  1530. string_type concat_result;
  1531. ContainerT rescanned;
  1532. concat_result = ((*prev).get_value() + (*next).get_value());
  1533. // analyze the validity of the concatenation result
  1534. if (!is_valid_concat(concat_result, (*prev).get_position(),
  1535. rescanned) &&
  1536. !IS_CATEGORY(*prev, WhiteSpaceTokenType) &&
  1537. !IS_CATEGORY(*next, WhiteSpaceTokenType))
  1538. {
  1539. report_invalid_concatenation(ctx, *prev, *next, main_pos);
  1540. return false;
  1541. }
  1542. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  1543. if (boost::wave::need_variadics(ctx.get_language())) {
  1544. // remove the prev, '##' and the next tokens from the sequence
  1545. expanded.erase(prev, ++next); // remove not needed tokens
  1546. // some stl implementations clear() the container if we erased all
  1547. // the elements, which orphans all iterators. we re-initialize these
  1548. // here
  1549. if (expanded.empty())
  1550. end = next = expanded.end();
  1551. // replace the old token (pointed to by *prev) with the re-tokenized
  1552. // sequence
  1553. expanded.splice(next, rescanned);
  1554. // the last token of the inserted sequence is the new previous
  1555. prev = next;
  1556. if (next != expanded.end())
  1557. --prev;
  1558. }
  1559. else
  1560. #endif // BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  1561. {
  1562. // we leave the token_id unchanged, but unmark the token as
  1563. // disabled, if appropriate
  1564. (*prev).set_value(concat_result);
  1565. if (T_NONREPLACABLE_IDENTIFIER == token_id(*prev))
  1566. (*prev).set_token_id(T_IDENTIFIER);
  1567. // remove the '##' and the next tokens from the sequence
  1568. iterator_type first_to_delete = prev;
  1569. expanded.erase(++first_to_delete, ++next);
  1570. }
  1571. it = next;
  1572. continue;
  1573. }
  1574. // save last non-whitespace token position
  1575. if (!IS_CATEGORY(*it, WhiteSpaceTokenType))
  1576. prev = it;
  1577. ++it; // next token, please
  1578. }
  1579. return true;
  1580. }
  1581. ///////////////////////////////////////////////////////////////////////////////
  1582. //
  1583. // predefine_macro(): predefine a single macro
  1584. //
  1585. ///////////////////////////////////////////////////////////////////////////////
  1586. template <typename ContextT>
  1587. inline void
  1588. macromap<ContextT>::predefine_macro(defined_macros_type *scope,
  1589. string_type const &name, token_type const &t)
  1590. {
  1591. definition_container_type macrodefinition;
  1592. std::vector<token_type> param;
  1593. macrodefinition.push_back(t);
  1594. add_macro(token_type(T_IDENTIFIER, name, t.get_position()),
  1595. false, param, macrodefinition, true, scope);
  1596. }
  1597. ///////////////////////////////////////////////////////////////////////////////
  1598. //
  1599. // init_predefined_macros(): init the predefined macros
  1600. //
  1601. ///////////////////////////////////////////////////////////////////////////////
  1602. template <typename ContextT>
  1603. inline void
  1604. macromap<ContextT>::init_predefined_macros(char const *fname,
  1605. defined_macros_type *scope, bool at_global_scope)
  1606. {
  1607. // if no scope is given, use the current one
  1608. defined_macros_type *current_scope = scope ? scope : current_macros;
  1609. // first, add the static macros
  1610. position_type pos("<built-in>");
  1611. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  1612. if (boost::wave::need_c99(ctx.get_language())) {
  1613. // define C99 specifics
  1614. for (int i = 0; 0 != predef.static_data_c99(i).name; ++i) {
  1615. predefined_macros::static_macros const& m = predef.static_data_c99(i);
  1616. predefine_macro(current_scope, m.name,
  1617. token_type(m.token_id, m.value, pos));
  1618. }
  1619. }
  1620. else
  1621. #endif
  1622. {
  1623. #if BOOST_WAVE_SUPPORT_CPP0X != 0
  1624. if (boost::wave::need_cpp0x(ctx.get_language())) {
  1625. // define C++11 specifics
  1626. for (int i = 0; 0 != predef.static_data_cpp0x(i).name; ++i) {
  1627. predefined_macros::static_macros const& m = predef.static_data_cpp0x(i);
  1628. predefine_macro(current_scope, m.name,
  1629. token_type(m.token_id, m.value, pos));
  1630. }
  1631. }
  1632. else
  1633. #endif
  1634. {
  1635. // define C++ specifics
  1636. for (int i = 0; 0 != predef.static_data_cpp(i).name; ++i) {
  1637. predefined_macros::static_macros const& m = predef.static_data_cpp(i);
  1638. predefine_macro(current_scope, m.name,
  1639. token_type(m.token_id, m.value, pos));
  1640. }
  1641. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  1642. // define __WAVE_HAS_VARIADICS__, if appropriate
  1643. if (boost::wave::need_variadics(ctx.get_language())) {
  1644. predefine_macro(current_scope, "__WAVE_HAS_VARIADICS__",
  1645. token_type(T_INTLIT, "1", pos));
  1646. }
  1647. #endif
  1648. }
  1649. }
  1650. // predefine the __BASE_FILE__ macro which contains the main file name
  1651. namespace fs = boost::filesystem;
  1652. if (string_type(fname) != "<Unknown>") {
  1653. fs::path filename(create_path(fname));
  1654. using boost::wave::util::impl::escape_lit;
  1655. predefine_macro(current_scope, "__BASE_FILE__",
  1656. token_type(T_STRINGLIT, string_type("\"") +
  1657. escape_lit(native_file_string(filename)).c_str() + "\"", pos));
  1658. base_name = fname;
  1659. }
  1660. else if (!base_name.empty()) {
  1661. fs::path filename(create_path(base_name.c_str()));
  1662. using boost::wave::util::impl::escape_lit;
  1663. predefine_macro(current_scope, "__BASE_FILE__",
  1664. token_type(T_STRINGLIT, string_type("\"") +
  1665. escape_lit(native_file_string(filename)).c_str() + "\"", pos));
  1666. }
  1667. // now add the dynamic macros
  1668. for (int j = 0; 0 != predef.dynamic_data(j).name; ++j) {
  1669. predefined_macros::dynamic_macros const& m = predef.dynamic_data(j);
  1670. predefine_macro(current_scope, m.name,
  1671. token_type(m.token_id, (predef.* m.generator)(), pos));
  1672. }
  1673. }
  1674. ///////////////////////////////////////////////////////////////////////////////
  1675. //
  1676. // reset_macromap(): initialize the internal macro symbol namespace
  1677. //
  1678. ///////////////////////////////////////////////////////////////////////////////
  1679. template <typename ContextT>
  1680. inline void
  1681. macromap<ContextT>::reset_macromap()
  1682. {
  1683. current_macros->clear();
  1684. predef.reset();
  1685. act_token = token_type();
  1686. }
  1687. ///////////////////////////////////////////////////////////////////////////////
  1688. }}} // namespace boost::wave::util
  1689. #if BOOST_WAVE_SERIALIZATION != 0
  1690. namespace boost { namespace serialization {
  1691. template<typename ContextT>
  1692. struct version<boost::wave::util::macromap<ContextT> >
  1693. {
  1694. typedef boost::wave::util::macromap<ContextT> target_type;
  1695. typedef mpl::int_<target_type::version> type;
  1696. typedef mpl::integral_c_tag tag;
  1697. BOOST_STATIC_CONSTANT(unsigned int, value = version::type::value);
  1698. };
  1699. }} // namespace boost::serialization
  1700. #endif
  1701. // the suffix header occurs after all of the code
  1702. #ifdef BOOST_HAS_ABI_HEADERS
  1703. #include BOOST_ABI_SUFFIX
  1704. #endif
  1705. #endif // !defined(CPP_MACROMAP_HPP_CB8F51B0_A3F0_411C_AEF4_6FF631B8B414_INCLUDED)