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/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. typedef unput_queue_iterator<IteratorT, token_type, ContainerT> iterator_type;
  508. if (first != last) {
  509. token_id id = token_id(*first);
  510. // ignore placeholder tokens
  511. if (T_PLACEHOLDER == id) {
  512. token_type placeholder = *first;
  513. ++first;
  514. if (first == last)
  515. return act_token = placeholder;
  516. id = token_id(*first);
  517. }
  518. if (T_IDENTIFIER == id || IS_CATEGORY(id, KeywordTokenType) ||
  519. IS_EXTCATEGORY(id, OperatorTokenType|AltExtTokenType) ||
  520. IS_CATEGORY(id, BoolLiteralTokenType))
  521. {
  522. // try to replace this identifier as a macro
  523. if (expand_operator_defined && (*first).get_value() == "defined") {
  524. // resolve operator defined()
  525. return resolve_defined(first, last, pending);
  526. }
  527. else if (boost::wave::need_variadics(ctx.get_language()) &&
  528. (*first).get_value() == "_Pragma")
  529. {
  530. // in C99 mode only: resolve the operator _Pragma
  531. token_type curr_token = *first;
  532. if (!resolve_operator_pragma(first, last, pending, seen_newline) ||
  533. pending.size() > 0)
  534. {
  535. // unknown to us pragma or supplied replacement, return the
  536. // next token
  537. on_exit::pop_front<definition_container_type> pop_token(pending);
  538. return act_token = pending.front();
  539. }
  540. // the operator _Pragma() was eaten completely, continue
  541. return act_token = token_type(T_PLACEHOLDER, "_",
  542. curr_token.get_position());
  543. }
  544. token_type name_token (*first);
  545. typename defined_macros_type::iterator it;
  546. if (is_defined(name_token.get_value(), it)) {
  547. // the current token contains an identifier, which is currently
  548. // defined as a macro
  549. if (expand_macro(pending, name_token, it, first, last,
  550. seen_newline, expand_operator_defined))
  551. {
  552. // the tokens returned by expand_macro should be rescanned
  553. // beginning at the last token of the returned replacement list
  554. if (first != last) {
  555. // splice the last token back into the input queue
  556. typename ContainerT::reverse_iterator rit = pending.rbegin();
  557. first.get_unput_queue().splice(
  558. first.get_unput_queue().begin(), pending,
  559. (++rit).base(), pending.end());
  560. }
  561. // fall through ...
  562. }
  563. else if (!pending.empty()) {
  564. // return the first token from the pending queue
  565. on_exit::pop_front<definition_container_type> pop_queue (pending);
  566. return act_token = pending.front();
  567. }
  568. else {
  569. // macro expansion reached the eoi
  570. return act_token = token_type();
  571. }
  572. // return the next preprocessed token
  573. return expand_tokensequence_worker(pending, first, last,
  574. seen_newline, expand_operator_defined);
  575. }
  576. // else if (expand_operator_defined) {
  577. // // in preprocessing conditionals undefined identifiers and keywords
  578. // // are to be replaced with '0' (see. C++ standard 16.1.4, [cpp.cond])
  579. // return act_token =
  580. // token_type(T_INTLIT, "0", (*first++).get_position());
  581. // }
  582. else {
  583. act_token = name_token;
  584. ++first;
  585. return act_token;
  586. }
  587. }
  588. else if (expand_operator_defined && IS_CATEGORY(*first, BoolLiteralTokenType)) {
  589. // expanding a constant expression inside #if/#elif, special handling
  590. // of 'true' and 'false'
  591. // all remaining identifiers and keywords, except for true and false,
  592. // are replaced with the pp-number 0 (C++ standard 16.1.4, [cpp.cond])
  593. return act_token = token_type(T_INTLIT, T_TRUE != id ? "0" : "1",
  594. (*first++).get_position());
  595. }
  596. else {
  597. act_token = *first;
  598. ++first;
  599. return act_token;
  600. }
  601. }
  602. return act_token = token_type(); // eoi
  603. }
  604. ///////////////////////////////////////////////////////////////////////////////
  605. //
  606. // collect_arguments(): collect the actual arguments of a macro invocation
  607. //
  608. // return the number of successfully detected non-empty arguments
  609. //
  610. ///////////////////////////////////////////////////////////////////////////////
  611. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
  612. template <typename ContextT>
  613. template <typename IteratorT, typename ContainerT, typename SizeT>
  614. inline typename std::vector<ContainerT>::size_type
  615. macromap<ContextT>::collect_arguments (token_type const curr_token,
  616. std::vector<ContainerT> &arguments, IteratorT &next,
  617. IteratorT const &end, SizeT const &parameter_count, bool& seen_newline)
  618. #else
  619. template <typename ContextT>
  620. template <typename IteratorT, typename ContainerT, typename SizeT>
  621. inline typename std::vector<ContainerT>::size_type
  622. macromap<ContextT>::collect_arguments (token_type const curr_token,
  623. std::vector<ContainerT> &arguments, IteratorT &next, IteratorT &endparen,
  624. IteratorT const &end, SizeT const &parameter_count, bool& seen_newline)
  625. #endif
  626. {
  627. using namespace boost::wave;
  628. arguments.push_back(ContainerT());
  629. // collect the actual arguments
  630. typename std::vector<ContainerT>::size_type count_arguments = 0;
  631. int nested_parenthesis_level = 1;
  632. ContainerT *argument = &arguments[0];
  633. bool was_whitespace = false;
  634. token_type startof_argument_list = *next;
  635. while (++next != end && nested_parenthesis_level) {
  636. token_id id = token_id(*next);
  637. if (0 == parameter_count &&
  638. !IS_CATEGORY((*next), WhiteSpaceTokenType) && id != T_NEWLINE &&
  639. id != T_RIGHTPAREN && id != T_LEFTPAREN)
  640. {
  641. // there shouldn't be any arguments
  642. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
  643. too_many_macroarguments, curr_token.get_value().c_str(),
  644. main_pos);
  645. return 0;
  646. }
  647. switch (id) {
  648. case T_LEFTPAREN:
  649. ++nested_parenthesis_level;
  650. argument->push_back(*next);
  651. was_whitespace = false;
  652. break;
  653. case T_RIGHTPAREN:
  654. {
  655. if (--nested_parenthesis_level >= 1)
  656. argument->push_back(*next);
  657. else {
  658. // found closing parenthesis
  659. // trim_sequence(argument);
  660. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS == 0
  661. endparen = next;
  662. #endif
  663. if (parameter_count > 0) {
  664. if (argument->empty() ||
  665. impl::is_whitespace_only(*argument))
  666. {
  667. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  668. if (boost::wave::need_variadics(ctx.get_language())) {
  669. // store a placemarker as the argument
  670. argument->push_back(token_type(T_PLACEMARKER, "\xA7",
  671. (*next).get_position()));
  672. ++count_arguments;
  673. }
  674. #endif
  675. }
  676. else {
  677. ++count_arguments;
  678. }
  679. }
  680. }
  681. was_whitespace = false;
  682. }
  683. break;
  684. case T_COMMA:
  685. if (1 == nested_parenthesis_level) {
  686. // next parameter
  687. // trim_sequence(argument);
  688. if (argument->empty() ||
  689. impl::is_whitespace_only(*argument))
  690. {
  691. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  692. if (boost::wave::need_variadics(ctx.get_language())) {
  693. // store a placemarker as the argument
  694. argument->push_back(token_type(T_PLACEMARKER, "\xA7",
  695. (*next).get_position()));
  696. ++count_arguments;
  697. }
  698. #endif
  699. }
  700. else {
  701. ++count_arguments;
  702. }
  703. arguments.push_back(ContainerT()); // add new arg
  704. argument = &arguments[arguments.size()-1];
  705. }
  706. else {
  707. // surrounded by parenthesises, so store to current argument
  708. argument->push_back(*next);
  709. }
  710. was_whitespace = false;
  711. break;
  712. case T_NEWLINE:
  713. seen_newline = true;
  714. /* fall through */
  715. case T_SPACE:
  716. case T_SPACE2:
  717. case T_CCOMMENT:
  718. if (!was_whitespace)
  719. argument->push_back(token_type(T_SPACE, " ", (*next).get_position()));
  720. was_whitespace = true;
  721. break; // skip whitespace
  722. case T_PLACEHOLDER:
  723. break; // ignore placeholder
  724. default:
  725. argument->push_back(*next);
  726. was_whitespace = false;
  727. break;
  728. }
  729. }
  730. if (nested_parenthesis_level >= 1) {
  731. // missing ')': improperly terminated macro invocation
  732. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
  733. improperly_terminated_macro, "missing ')'", main_pos);
  734. return 0;
  735. }
  736. // if no argument was expected and we didn't find any, than remove the empty
  737. // element
  738. if (0 == parameter_count && 0 == count_arguments) {
  739. BOOST_ASSERT(1 == arguments.size());
  740. arguments.clear();
  741. }
  742. return count_arguments;
  743. }
  744. ///////////////////////////////////////////////////////////////////////////////
  745. //
  746. // expand_whole_tokensequence
  747. //
  748. // fully expands a given token sequence
  749. //
  750. ///////////////////////////////////////////////////////////////////////////////
  751. template <typename ContextT>
  752. template <typename IteratorT, typename ContainerT>
  753. inline void
  754. macromap<ContextT>::expand_whole_tokensequence(ContainerT &expanded,
  755. IteratorT &first, IteratorT const &last,
  756. bool expand_operator_defined)
  757. {
  758. typedef impl::gen_unput_queue_iterator<IteratorT, token_type, ContainerT>
  759. gen_type;
  760. typedef typename gen_type::return_type iterator_type;
  761. ContainerT empty;
  762. iterator_type first_it = gen_type::generate(empty, first);
  763. iterator_type last_it = gen_type::generate(last);
  764. on_exit::assign<IteratorT, iterator_type> on_exit(first, first_it);
  765. ContainerT pending_queue;
  766. bool seen_newline;
  767. while (!pending_queue.empty() || first_it != last_it) {
  768. expanded.push_back(
  769. expand_tokensequence_worker(pending_queue, first_it,
  770. last_it, seen_newline, expand_operator_defined)
  771. );
  772. }
  773. // should have returned all expanded tokens
  774. BOOST_ASSERT(pending_queue.empty()/* && unput_queue.empty()*/);
  775. }
  776. ///////////////////////////////////////////////////////////////////////////////
  777. //
  778. // expand_argument
  779. //
  780. // fully expands the given argument of a macro call
  781. //
  782. ///////////////////////////////////////////////////////////////////////////////
  783. template <typename ContextT>
  784. template <typename ContainerT>
  785. inline void
  786. macromap<ContextT>::expand_argument (
  787. typename std::vector<ContainerT>::size_type arg,
  788. std::vector<ContainerT> &arguments, std::vector<ContainerT> &expanded_args,
  789. bool expand_operator_defined, std::vector<bool> &has_expanded_args)
  790. {
  791. if (!has_expanded_args[arg]) {
  792. // expand the argument only once
  793. typedef typename std::vector<ContainerT>::value_type::iterator
  794. argument_iterator_type;
  795. argument_iterator_type begin_it = arguments[arg].begin();
  796. argument_iterator_type end_it = arguments[arg].end();
  797. expand_whole_tokensequence(expanded_args[arg], begin_it, end_it,
  798. expand_operator_defined);
  799. impl::remove_placeholders(expanded_args[arg]);
  800. has_expanded_args[arg] = true;
  801. }
  802. }
  803. ///////////////////////////////////////////////////////////////////////////////
  804. //
  805. // expand_replacement_list
  806. //
  807. // fully expands the replacement list of a given macro with the
  808. // actual arguments/expanded arguments
  809. // handles the '#' [cpp.stringize] and the '##' [cpp.concat] operator
  810. //
  811. ///////////////////////////////////////////////////////////////////////////////
  812. template <typename ContextT>
  813. template <typename ContainerT>
  814. inline void
  815. macromap<ContextT>::expand_replacement_list(
  816. macro_definition_type const &macrodef,
  817. std::vector<ContainerT> &arguments, bool expand_operator_defined,
  818. ContainerT &expanded)
  819. {
  820. using namespace boost::wave;
  821. typedef typename macro_definition_type::const_definition_iterator_t
  822. macro_definition_iter_t;
  823. std::vector<ContainerT> expanded_args(arguments.size());
  824. std::vector<bool> has_expanded_args(arguments.size());
  825. bool seen_concat = false;
  826. bool adjacent_concat = false;
  827. bool adjacent_stringize = false;
  828. macro_definition_iter_t cend = macrodef.macrodefinition.end();
  829. for (macro_definition_iter_t cit = macrodef.macrodefinition.begin();
  830. cit != cend; ++cit)
  831. {
  832. bool use_replaced_arg = true;
  833. token_id base_id = BASE_TOKEN(token_id(*cit));
  834. if (T_POUND_POUND == base_id) {
  835. // concatenation operator
  836. adjacent_concat = true;
  837. seen_concat = true;
  838. }
  839. else if (T_POUND == base_id) {
  840. // stringize operator
  841. adjacent_stringize = true;
  842. }
  843. else {
  844. if (adjacent_stringize || adjacent_concat ||
  845. T_POUND_POUND == impl::next_token<macro_definition_iter_t>
  846. ::peek(cit, cend))
  847. {
  848. use_replaced_arg = false;
  849. }
  850. if (adjacent_concat) // spaces after '##' ?
  851. adjacent_concat = IS_CATEGORY(*cit, WhiteSpaceTokenType);
  852. }
  853. if (IS_CATEGORY((*cit), ParameterTokenType)) {
  854. // copy argument 'i' instead of the parameter token i
  855. typename ContainerT::size_type i;
  856. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  857. bool is_ellipsis = false;
  858. if (IS_EXTCATEGORY((*cit), ExtParameterTokenType)) {
  859. BOOST_ASSERT(boost::wave::need_variadics(ctx.get_language()));
  860. i = token_id(*cit) - T_EXTPARAMETERBASE;
  861. is_ellipsis = true;
  862. }
  863. else
  864. #endif
  865. {
  866. i = token_id(*cit) - T_PARAMETERBASE;
  867. }
  868. BOOST_ASSERT(i < arguments.size());
  869. if (use_replaced_arg) {
  870. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  871. if (is_ellipsis) {
  872. position_type const &pos = (*cit).get_position();
  873. BOOST_ASSERT(boost::wave::need_variadics(ctx.get_language()));
  874. // ensure all variadic arguments to be expanded
  875. for (typename vector<ContainerT>::size_type arg = i;
  876. arg < expanded_args.size(); ++arg)
  877. {
  878. expand_argument(arg, arguments, expanded_args,
  879. expand_operator_defined, has_expanded_args);
  880. }
  881. impl::replace_ellipsis(expanded_args, i, expanded, pos);
  882. }
  883. else
  884. #endif
  885. {
  886. // ensure argument i to be expanded
  887. expand_argument(i, arguments, expanded_args,
  888. expand_operator_defined, has_expanded_args);
  889. // replace argument
  890. ContainerT const &arg = expanded_args[i];
  891. std::copy(arg.begin(), arg.end(),
  892. std::inserter(expanded, expanded.end()));
  893. }
  894. }
  895. else if (adjacent_stringize &&
  896. !IS_CATEGORY(*cit, WhiteSpaceTokenType))
  897. {
  898. // stringize the current argument
  899. BOOST_ASSERT(!arguments[i].empty());
  900. // safe a copy of the first tokens position (not a reference!)
  901. position_type pos ((*arguments[i].begin()).get_position());
  902. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  903. if (is_ellipsis && boost::wave::need_variadics(ctx.get_language())) {
  904. impl::trim_sequence_left(arguments[i]);
  905. impl::trim_sequence_right(arguments.back());
  906. expanded.push_back(token_type(T_STRINGLIT,
  907. impl::as_stringlit(arguments, i, pos), pos));
  908. }
  909. else
  910. #endif
  911. {
  912. impl::trim_sequence(arguments[i]);
  913. expanded.push_back(token_type(T_STRINGLIT,
  914. impl::as_stringlit(arguments[i], pos), pos));
  915. }
  916. adjacent_stringize = false;
  917. }
  918. else {
  919. // simply copy the original argument (adjacent '##' or '#')
  920. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  921. if (is_ellipsis) {
  922. position_type const &pos = (*cit).get_position();
  923. impl::trim_sequence_left(arguments[i]);
  924. impl::trim_sequence_right(arguments.back());
  925. BOOST_ASSERT(boost::wave::need_variadics(ctx.get_language()));
  926. impl::replace_ellipsis(arguments, i, expanded, pos);
  927. }
  928. else
  929. #endif
  930. {
  931. ContainerT &arg = arguments[i];
  932. impl::trim_sequence(arg);
  933. std::copy(arg.begin(), arg.end(),
  934. std::inserter(expanded, expanded.end()));
  935. }
  936. }
  937. }
  938. else if (!adjacent_stringize || T_POUND != base_id) {
  939. // insert the actual replacement token (if it is not the '#' operator)
  940. expanded.push_back(*cit);
  941. }
  942. }
  943. if (adjacent_stringize) {
  944. // error, '#' should not be the last token
  945. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, ill_formed_operator,
  946. "stringize ('#')", main_pos);
  947. return;
  948. }
  949. // handle the cpp.concat operator
  950. if (seen_concat)
  951. concat_tokensequence(expanded);
  952. }
  953. ///////////////////////////////////////////////////////////////////////////////
  954. //
  955. // rescan_replacement_list
  956. //
  957. // As the name implies, this function is used to rescan the replacement list
  958. // after the first macro substitution phase.
  959. //
  960. ///////////////////////////////////////////////////////////////////////////////
  961. template <typename ContextT>
  962. template <typename IteratorT, typename ContainerT>
  963. inline void
  964. macromap<ContextT>::rescan_replacement_list(token_type const &curr_token,
  965. macro_definition_type &macro_def, ContainerT &replacement_list,
  966. ContainerT &expanded, bool expand_operator_defined,
  967. IteratorT &nfirst, IteratorT const &nlast)
  968. {
  969. if (!replacement_list.empty()) {
  970. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  971. // remove the placemarkers
  972. if (boost::wave::need_variadics(ctx.get_language())) {
  973. typename ContainerT::iterator end = replacement_list.end();
  974. typename ContainerT::iterator it = replacement_list.begin();
  975. while (it != end) {
  976. using namespace boost::wave;
  977. if (T_PLACEMARKER == token_id(*it)) {
  978. typename ContainerT::iterator placemarker = it;
  979. ++it;
  980. replacement_list.erase(placemarker);
  981. }
  982. else {
  983. ++it;
  984. }
  985. }
  986. }
  987. #endif
  988. // rescan the replacement list, during this rescan the current macro under
  989. // expansion isn't available as an expandable macro
  990. on_exit::reset<bool> on_exit(macro_def.is_available_for_replacement, false);
  991. typename ContainerT::iterator begin_it = replacement_list.begin();
  992. typename ContainerT::iterator end_it = replacement_list.end();
  993. expand_whole_tokensequence(expanded, begin_it, end_it,
  994. expand_operator_defined);
  995. // trim replacement list, leave placeholder tokens untouched
  996. impl::trim_replacement_list(expanded);
  997. }
  998. if (expanded.empty()) {
  999. // the resulting replacement list should contain at least a placeholder
  1000. // token
  1001. expanded.push_back(token_type(T_PLACEHOLDER, "_", curr_token.get_position()));
  1002. }
  1003. }
  1004. ///////////////////////////////////////////////////////////////////////////////
  1005. //
  1006. // expand_macro(): expands a defined macro
  1007. //
  1008. // This functions tries to expand the macro, to which points the 'first'
  1009. // iterator. The functions eats up more tokens, if the macro to expand is
  1010. // a function-like macro.
  1011. //
  1012. ///////////////////////////////////////////////////////////////////////////////
  1013. template <typename ContextT>
  1014. template <typename IteratorT, typename ContainerT>
  1015. inline bool
  1016. macromap<ContextT>::expand_macro(ContainerT &expanded,
  1017. token_type const &curr_token, typename defined_macros_type::iterator it,
  1018. IteratorT &first, IteratorT const &last,
  1019. bool& seen_newline, bool expand_operator_defined,
  1020. defined_macros_type *scope, ContainerT *queue_symbol)
  1021. {
  1022. using namespace boost::wave;
  1023. if (0 == scope) scope = current_macros;
  1024. BOOST_ASSERT(T_IDENTIFIER == token_id(curr_token) ||
  1025. IS_CATEGORY(token_id(curr_token), KeywordTokenType) ||
  1026. IS_EXTCATEGORY(token_id(curr_token), OperatorTokenType|AltExtTokenType) ||
  1027. IS_CATEGORY(token_id(curr_token), BoolLiteralTokenType));
  1028. if (it == scope->end()) {
  1029. ++first; // advance
  1030. // try to expand a predefined macro (__FILE__, __LINE__ or __INCLUDE_LEVEL__)
  1031. if (expand_predefined_macro(curr_token, expanded))
  1032. return false;
  1033. // not defined as a macro
  1034. if (0 != queue_symbol) {
  1035. expanded.splice(expanded.end(), *queue_symbol);
  1036. }
  1037. else {
  1038. expanded.push_back(curr_token);
  1039. }
  1040. return false;
  1041. }
  1042. // ensure the parameters to be replaced with special parameter tokens
  1043. macro_definition_type &macro_def = *(*it).second.get();
  1044. macro_def.replace_parameters();
  1045. // test if this macro is currently available for replacement
  1046. if (!macro_def.is_available_for_replacement) {
  1047. // this macro is marked as non-replaceable
  1048. // copy the macro name itself
  1049. if (0 != queue_symbol) {
  1050. queue_symbol->push_back(token_type(T_NONREPLACABLE_IDENTIFIER,
  1051. curr_token.get_value(), curr_token.get_position()));
  1052. expanded.splice(expanded.end(), *queue_symbol);
  1053. }
  1054. else {
  1055. expanded.push_back(token_type(T_NONREPLACABLE_IDENTIFIER,
  1056. curr_token.get_value(), curr_token.get_position()));
  1057. }
  1058. ++first;
  1059. return false;
  1060. }
  1061. // try to replace the current identifier as a function-like macro
  1062. ContainerT replacement_list;
  1063. if (T_LEFTPAREN == impl::next_token<IteratorT>::peek(first, last)) {
  1064. // called as a function-like macro
  1065. impl::skip_to_token(ctx, first, last, T_LEFTPAREN, seen_newline);
  1066. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS == 0
  1067. IteratorT seqstart = first;
  1068. IteratorT seqend = first;
  1069. #endif
  1070. if (macro_def.is_functionlike) {
  1071. // defined as a function-like macro
  1072. // collect the arguments
  1073. std::vector<ContainerT> arguments;
  1074. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
  1075. typename std::vector<ContainerT>::size_type count_args =
  1076. collect_arguments (curr_token, arguments, first, last,
  1077. macro_def.macroparameters.size(), seen_newline);
  1078. #else
  1079. typename std::vector<ContainerT>::size_type count_args =
  1080. collect_arguments (curr_token, arguments, first, seqend, last,
  1081. macro_def.macroparameters.size(), seen_newline);
  1082. #endif
  1083. // verify the parameter count
  1084. if (count_args < macro_def.macroparameters.size() ||
  1085. arguments.size() < macro_def.macroparameters.size())
  1086. {
  1087. if (count_args != arguments.size()) {
  1088. // must been at least one empty argument in C++ mode
  1089. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
  1090. empty_macroarguments, curr_token.get_value().c_str(),
  1091. main_pos);
  1092. }
  1093. else {
  1094. // too few macro arguments
  1095. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
  1096. too_few_macroarguments, curr_token.get_value().c_str(),
  1097. main_pos);
  1098. }
  1099. return false;
  1100. }
  1101. if (count_args > macro_def.macroparameters.size() ||
  1102. arguments.size() > macro_def.macroparameters.size())
  1103. {
  1104. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  1105. if (!macro_def.has_ellipsis)
  1106. #endif
  1107. {
  1108. // too many macro arguments
  1109. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
  1110. too_many_macroarguments,
  1111. curr_token.get_value().c_str(), main_pos);
  1112. return false;
  1113. }
  1114. }
  1115. // inject tracing support
  1116. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
  1117. ctx.get_hooks().expanding_function_like_macro(
  1118. macro_def.macroname, macro_def.macroparameters,
  1119. macro_def.macrodefinition, curr_token, arguments);
  1120. #else
  1121. if (ctx.get_hooks().expanding_function_like_macro(ctx.derived(),
  1122. macro_def.macroname, macro_def.macroparameters,
  1123. macro_def.macrodefinition, curr_token, arguments,
  1124. seqstart, seqend))
  1125. {
  1126. // // do not expand this macro, just copy the whole sequence
  1127. // expanded.push_back(curr_token);
  1128. // std::copy(seqstart, first,
  1129. // std::inserter(expanded, expanded.end()));
  1130. // do not expand macro, just copy macro name and parenthesis
  1131. expanded.push_back(curr_token);
  1132. expanded.push_back(*seqstart);
  1133. first = ++seqstart;
  1134. return false; // no further preprocessing required
  1135. }
  1136. #endif
  1137. // expand the replacement list of this macro
  1138. expand_replacement_list(macro_def, arguments, expand_operator_defined,
  1139. replacement_list);
  1140. }
  1141. else {
  1142. // defined as an object-like macro
  1143. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
  1144. ctx.get_hooks().expanding_object_like_macro(
  1145. macro_def.macroname, macro_def.macrodefinition, curr_token);
  1146. #else
  1147. if (ctx.get_hooks().expanding_object_like_macro(ctx.derived(),
  1148. macro_def.macroname, macro_def.macrodefinition, curr_token))
  1149. {
  1150. // do not expand this macro, just copy the whole sequence
  1151. expanded.push_back(curr_token);
  1152. return false; // no further preprocessing required
  1153. }
  1154. #endif
  1155. bool found = false;
  1156. impl::find_concat_operator concat_tag(found);
  1157. std::remove_copy_if(macro_def.macrodefinition.begin(),
  1158. macro_def.macrodefinition.end(),
  1159. std::inserter(replacement_list, replacement_list.end()),
  1160. concat_tag);
  1161. // handle concatenation operators
  1162. if (found && !concat_tokensequence(replacement_list))
  1163. return false;
  1164. }
  1165. }
  1166. else {
  1167. // called as an object like macro
  1168. if ((*it).second->is_functionlike) {
  1169. // defined as a function-like macro
  1170. if (0 != queue_symbol) {
  1171. queue_symbol->push_back(curr_token);
  1172. expanded.splice(expanded.end(), *queue_symbol);
  1173. }
  1174. else {
  1175. expanded.push_back(curr_token);
  1176. }
  1177. ++first; // skip macro name
  1178. return false; // no further preprocessing required
  1179. }
  1180. else {
  1181. // defined as an object-like macro (expand it)
  1182. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
  1183. ctx.get_hooks().expanding_object_like_macro(
  1184. macro_def.macroname, macro_def.macrodefinition, curr_token);
  1185. #else
  1186. if (ctx.get_hooks().expanding_object_like_macro(ctx.derived(),
  1187. macro_def.macroname, macro_def.macrodefinition, curr_token))
  1188. {
  1189. // do not expand this macro, just copy the whole sequence
  1190. expanded.push_back(curr_token);
  1191. ++first; // skip macro name
  1192. return false; // no further preprocessing required
  1193. }
  1194. #endif
  1195. bool found = false;
  1196. impl::find_concat_operator concat_tag(found);
  1197. std::remove_copy_if(macro_def.macrodefinition.begin(),
  1198. macro_def.macrodefinition.end(),
  1199. std::inserter(replacement_list, replacement_list.end()),
  1200. concat_tag);
  1201. // handle concatenation operators
  1202. if (found && !concat_tokensequence(replacement_list))
  1203. return false;
  1204. ++first; // skip macro name
  1205. }
  1206. }
  1207. // rescan the replacement list
  1208. ContainerT expanded_list;
  1209. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
  1210. ctx.get_hooks().expanded_macro(replacement_list);
  1211. #else
  1212. ctx.get_hooks().expanded_macro(ctx.derived(), replacement_list);
  1213. #endif
  1214. rescan_replacement_list(curr_token, macro_def, replacement_list,
  1215. expanded_list, expand_operator_defined, first, last);
  1216. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
  1217. ctx.get_hooks().rescanned_macro(expanded_list);
  1218. #else
  1219. ctx.get_hooks().rescanned_macro(ctx.derived(), expanded_list);
  1220. #endif
  1221. expanded.splice(expanded.end(), expanded_list);
  1222. return true; // rescan is required
  1223. }
  1224. ///////////////////////////////////////////////////////////////////////////////
  1225. //
  1226. // If the token under inspection points to a certain predefined macro it will
  1227. // be expanded, otherwise false is returned.
  1228. // (only __FILE__, __LINE__ and __INCLUDE_LEVEL__ macros are expanded here)
  1229. //
  1230. ///////////////////////////////////////////////////////////////////////////////
  1231. template <typename ContextT>
  1232. template <typename ContainerT>
  1233. inline bool
  1234. macromap<ContextT>::expand_predefined_macro(token_type const &curr_token,
  1235. ContainerT &expanded)
  1236. {
  1237. using namespace boost::wave;
  1238. string_type const &value = curr_token.get_value();
  1239. if (value.size() < 8 || '_' != value[0] || '_' != value[1])
  1240. return false; // quick check failed
  1241. if (value == "__LINE__") {
  1242. // expand the __LINE__ macro
  1243. std::string buffer = lexical_cast<std::string>(main_pos.get_line());
  1244. expanded.push_back(token_type(T_INTLIT, buffer.c_str(), curr_token.get_position()));
  1245. return true;
  1246. }
  1247. else if (value == "__FILE__") {
  1248. // expand the __FILE__ macro
  1249. namespace fs = boost::filesystem;
  1250. std::string file("\"");
  1251. fs::path filename(wave::util::create_path(main_pos.get_file().c_str()));
  1252. using boost::wave::util::impl::escape_lit;
  1253. file += escape_lit(wave::util::native_file_string(filename)) + "\"";
  1254. expanded.push_back(token_type(T_STRINGLIT, file.c_str(),
  1255. curr_token.get_position()));
  1256. return true;
  1257. }
  1258. else if (value == "__INCLUDE_LEVEL__") {
  1259. // expand the __INCLUDE_LEVEL__ macro
  1260. char buffer[22]; // 21 bytes holds all NUL-terminated unsigned 64-bit numbers
  1261. using namespace std; // for some systems sprintf is in namespace std
  1262. sprintf(buffer, "%d", (int)ctx.get_iteration_depth());
  1263. expanded.push_back(token_type(T_INTLIT, buffer, curr_token.get_position()));
  1264. return true;
  1265. }
  1266. return false; // no predefined token
  1267. }
  1268. ///////////////////////////////////////////////////////////////////////////////
  1269. //
  1270. // resolve_defined(): resolve the operator defined() and replace it with the
  1271. // correct T_INTLIT token
  1272. //
  1273. ///////////////////////////////////////////////////////////////////////////////
  1274. template <typename ContextT>
  1275. template <typename IteratorT, typename ContainerT>
  1276. inline typename ContextT::token_type const &
  1277. macromap<ContextT>::resolve_defined(IteratorT &first,
  1278. IteratorT const &last, ContainerT &pending)
  1279. {
  1280. using namespace boost::wave;
  1281. using namespace boost::wave::grammars;
  1282. ContainerT result;
  1283. IteratorT start = first;
  1284. boost::spirit::classic::parse_info<IteratorT> hit =
  1285. defined_grammar_gen<typename ContextT::lexer_type>::
  1286. parse_operator_defined(start, last, result);
  1287. if (!hit.hit) {
  1288. string_type msg ("defined(): ");
  1289. msg = msg + util::impl::as_string<string_type>(first, last);
  1290. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, ill_formed_expression,
  1291. msg.c_str(), main_pos);
  1292. // insert a dummy token
  1293. pending.push_back(token_type(T_INTLIT, "0", main_pos));
  1294. }
  1295. else {
  1296. impl::assign_iterator<IteratorT>::do_(first, hit.stop);
  1297. // insert a token, which reflects the outcome
  1298. pending.push_back(token_type(T_INTLIT,
  1299. is_defined(result.begin(), result.end()) ? "1" : "0",
  1300. main_pos));
  1301. }
  1302. on_exit::pop_front<definition_container_type> pop_front_token(pending);
  1303. return act_token = pending.front();
  1304. }
  1305. ///////////////////////////////////////////////////////////////////////////////
  1306. //
  1307. // resolve_operator_pragma(): resolve the operator _Pragma() and dispatch to
  1308. // the associated action
  1309. //
  1310. // This function returns true, if the pragma was correctly interpreted.
  1311. // The iterator 'first' is positioned behind the closing ')'.
  1312. // This function returns false, if the _Pragma was not known, the
  1313. // preprocessed token sequence is pushed back to the 'pending' sequence.
  1314. //
  1315. ///////////////////////////////////////////////////////////////////////////////
  1316. template <typename ContextT>
  1317. template <typename IteratorT, typename ContainerT>
  1318. inline bool
  1319. macromap<ContextT>::resolve_operator_pragma(IteratorT &first,
  1320. IteratorT const &last, ContainerT &pending, bool& seen_newline)
  1321. {
  1322. // isolate the parameter of the operator _Pragma
  1323. token_type pragma_token = *first;
  1324. if (!impl::skip_to_token(ctx, first, last, T_LEFTPAREN, seen_newline)) {
  1325. // illformed operator _Pragma
  1326. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, ill_formed_expression,
  1327. "operator _Pragma()", pragma_token.get_position());
  1328. return false;
  1329. }
  1330. std::vector<ContainerT> arguments;
  1331. #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
  1332. typename std::vector<ContainerT>::size_type count_args =
  1333. collect_arguments (pragma_token, arguments, first, last, 1, seen_newline);
  1334. #else
  1335. IteratorT endparen = first;
  1336. typename std::vector<ContainerT>::size_type count_args =
  1337. collect_arguments (pragma_token, arguments, first, endparen, last, 1,
  1338. seen_newline);
  1339. #endif
  1340. // verify the parameter count
  1341. if (pragma_token.get_position().get_file().empty())
  1342. pragma_token.set_position(act_token.get_position());
  1343. if (count_args < 1 || arguments.size() < 1) {
  1344. // too few macro arguments
  1345. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, too_few_macroarguments,
  1346. pragma_token.get_value().c_str(), pragma_token.get_position());
  1347. return false;
  1348. }
  1349. if (count_args > 1 || arguments.size() > 1) {
  1350. // too many macro arguments
  1351. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, too_many_macroarguments,
  1352. pragma_token.get_value().c_str(), pragma_token.get_position());
  1353. return false;
  1354. }
  1355. // preprocess the pragma token body
  1356. typedef typename std::vector<ContainerT>::value_type::iterator
  1357. argument_iterator_type;
  1358. ContainerT expanded;
  1359. argument_iterator_type begin_it = arguments[0].begin();
  1360. argument_iterator_type end_it = arguments[0].end();
  1361. expand_whole_tokensequence(expanded, begin_it, end_it, false);
  1362. // un-escape the parameter of the operator _Pragma
  1363. typedef typename token_type::string_type string_type;
  1364. string_type pragma_cmd;
  1365. typename ContainerT::const_iterator end_exp = expanded.end();
  1366. for (typename ContainerT::const_iterator it_exp = expanded.begin();
  1367. it_exp != end_exp; ++it_exp)
  1368. {
  1369. if (T_EOF == token_id(*it_exp))
  1370. break;
  1371. if (IS_CATEGORY(*it_exp, WhiteSpaceTokenType))
  1372. continue;
  1373. if (T_STRINGLIT != token_id(*it_exp)) {
  1374. // ill formed operator _Pragma
  1375. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
  1376. ill_formed_pragma_option, "_Pragma",
  1377. pragma_token.get_position());
  1378. return false;
  1379. }
  1380. if (pragma_cmd.size() > 0) {
  1381. // there should be exactly one string literal (string literals are to
  1382. // be concatenated at translation phase 6, but _Pragma operators are
  1383. // to be executed at translation phase 4)
  1384. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
  1385. ill_formed_pragma_option, "_Pragma",
  1386. pragma_token.get_position());
  1387. return false;
  1388. }
  1389. // remove the '\"' and concat all given string literal-values
  1390. string_type token_str = (*it_exp).get_value();
  1391. pragma_cmd += token_str.substr(1, token_str.size() - 2);
  1392. }
  1393. string_type pragma_cmd_unesc = impl::unescape_lit(pragma_cmd);
  1394. // tokenize the pragma body
  1395. typedef typename ContextT::lexer_type lexer_type;
  1396. ContainerT pragma;
  1397. std::string pragma_cmd_str(pragma_cmd_unesc.c_str());
  1398. lexer_type it = lexer_type(pragma_cmd_str.begin(), pragma_cmd_str.end(),
  1399. pragma_token.get_position(), ctx.get_language());
  1400. lexer_type end = lexer_type();
  1401. for (/**/; it != end; ++it)
  1402. pragma.push_back(*it);
  1403. // analyze the preprocessed token sequence and eventually dispatch to the
  1404. // associated action
  1405. if (interpret_pragma(ctx, pragma_token, pragma.begin(), pragma.end(),
  1406. pending))
  1407. {
  1408. return true; // successfully recognized a wave specific pragma
  1409. }
  1410. // unknown pragma token sequence, push it back and return to the caller
  1411. pending.push_front(token_type(T_SPACE, " ", pragma_token.get_position()));
  1412. pending.push_front(token_type(T_RIGHTPAREN, ")", pragma_token.get_position()));
  1413. pending.push_front(token_type(T_STRINGLIT, string_type("\"") + pragma_cmd + "\"",
  1414. pragma_token.get_position()));
  1415. pending.push_front(token_type(T_LEFTPAREN, "(", pragma_token.get_position()));
  1416. pending.push_front(pragma_token);
  1417. return false;
  1418. }
  1419. ///////////////////////////////////////////////////////////////////////////////
  1420. //
  1421. // Test, whether the result of a concat operator is well formed or not.
  1422. //
  1423. // This is done by re-scanning (re-tokenizing) the resulting token sequence,
  1424. // which should give back exactly one token.
  1425. //
  1426. ///////////////////////////////////////////////////////////////////////////////
  1427. template <typename ContextT>
  1428. template <typename ContainerT>
  1429. inline bool
  1430. macromap<ContextT>::is_valid_concat(string_type new_value,
  1431. position_type const &pos, ContainerT &rescanned)
  1432. {
  1433. // re-tokenize the newly generated string
  1434. typedef typename ContextT::lexer_type lexer_type;
  1435. std::string value_to_test(new_value.c_str());
  1436. boost::wave::language_support lang =
  1437. boost::wave::enable_prefer_pp_numbers(ctx.get_language());
  1438. lang = boost::wave::enable_single_line(lang);
  1439. lexer_type it = lexer_type(value_to_test.begin(), value_to_test.end(), pos,
  1440. lang);
  1441. lexer_type end = lexer_type();
  1442. for (/**/; it != end && T_EOF != token_id(*it); ++it)
  1443. {
  1444. // as of Wave V2.0.7 pasting of tokens is valid only if the resulting
  1445. // tokens are pp_tokens (as mandated by C++11)
  1446. if (!is_pp_token(*it))
  1447. return false;
  1448. rescanned.push_back(*it);
  1449. }
  1450. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  1451. if (boost::wave::need_variadics(ctx.get_language()))
  1452. return true; // in variadics mode token pasting is well defined
  1453. #endif
  1454. // test if the newly generated token sequence contains more than 1 token
  1455. return 1 == rescanned.size();
  1456. }
  1457. ///////////////////////////////////////////////////////////////////////////////
  1458. //
  1459. // Handle all occurrences of the concatenation operator '##' inside the given
  1460. // token sequence.
  1461. //
  1462. ///////////////////////////////////////////////////////////////////////////////
  1463. template <typename Context>
  1464. inline void report_invalid_concatenation(Context& ctx,
  1465. typename Context::token_type const& prev,
  1466. typename Context::token_type const& next,
  1467. typename Context::position_type const& main_pos)
  1468. {
  1469. typename Context::string_type error_string("\"");
  1470. error_string += prev.get_value();
  1471. error_string += "\" and \"";
  1472. error_string += next.get_value();
  1473. error_string += "\"";
  1474. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, invalid_concat,
  1475. error_string.c_str(), main_pos);
  1476. }
  1477. template <typename ContextT>
  1478. template <typename ContainerT>
  1479. inline bool
  1480. macromap<ContextT>::concat_tokensequence(ContainerT &expanded)
  1481. {
  1482. using namespace boost::wave;
  1483. typedef typename ContainerT::iterator iterator_type;
  1484. iterator_type end = expanded.end();
  1485. iterator_type prev = end;
  1486. for (iterator_type it = expanded.begin(); it != end; /**/)
  1487. {
  1488. if (T_POUND_POUND == BASE_TOKEN(token_id(*it))) {
  1489. iterator_type next = it;
  1490. ++next;
  1491. if (prev == end || next == end) {
  1492. // error, '##' should be in between two tokens
  1493. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
  1494. ill_formed_operator, "concat ('##')", main_pos);
  1495. return false;
  1496. }
  1497. // replace prev##next with the concatenated value, skip whitespace
  1498. // before and after the '##' operator
  1499. while (IS_CATEGORY(*next, WhiteSpaceTokenType)) {
  1500. ++next;
  1501. if (next == end) {
  1502. // error, '##' should be in between two tokens
  1503. BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
  1504. ill_formed_operator, "concat ('##')", main_pos);
  1505. return false;
  1506. }
  1507. }
  1508. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  1509. if (boost::wave::need_variadics(ctx.get_language())) {
  1510. if (T_PLACEMARKER == token_id(*next)) {
  1511. // remove the '##' and the next tokens from the sequence
  1512. iterator_type first_to_delete = prev;
  1513. expanded.erase(++first_to_delete, ++next);
  1514. it = next;
  1515. continue;
  1516. }
  1517. else if (T_PLACEMARKER == token_id(*prev)) {
  1518. // remove the '##' and the next tokens from the sequence
  1519. iterator_type first_to_delete = prev;
  1520. *prev = *next;
  1521. expanded.erase(++first_to_delete, ++next);
  1522. it = next;
  1523. continue;
  1524. }
  1525. }
  1526. #endif // BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  1527. // test if the concat operator has to concatenate two unrelated
  1528. // tokens i.e. the result yields more then one token
  1529. string_type concat_result;
  1530. ContainerT rescanned;
  1531. concat_result = ((*prev).get_value() + (*next).get_value());
  1532. // analyze the validity of the concatenation result
  1533. if (!is_valid_concat(concat_result, (*prev).get_position(),
  1534. rescanned) &&
  1535. !IS_CATEGORY(*prev, WhiteSpaceTokenType) &&
  1536. !IS_CATEGORY(*next, WhiteSpaceTokenType))
  1537. {
  1538. report_invalid_concatenation(ctx, *prev, *next, main_pos);
  1539. return false;
  1540. }
  1541. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  1542. if (boost::wave::need_variadics(ctx.get_language())) {
  1543. // remove the prev, '##' and the next tokens from the sequence
  1544. expanded.erase(prev, ++next); // remove not needed tokens
  1545. // some stl implementations clear() the container if we erased all
  1546. // the elements, which orphans all iterators. we re-initialize these
  1547. // here
  1548. if (expanded.empty())
  1549. end = next = expanded.end();
  1550. // replace the old token (pointed to by *prev) with the re-tokenized
  1551. // sequence
  1552. expanded.splice(next, rescanned);
  1553. // the last token of the inserted sequence is the new previous
  1554. prev = next;
  1555. if (next != expanded.end())
  1556. --prev;
  1557. }
  1558. else
  1559. #endif // BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  1560. {
  1561. // we leave the token_id unchanged, but unmark the token as
  1562. // disabled, if appropriate
  1563. (*prev).set_value(concat_result);
  1564. if (T_NONREPLACABLE_IDENTIFIER == token_id(*prev))
  1565. (*prev).set_token_id(T_IDENTIFIER);
  1566. // remove the '##' and the next tokens from the sequence
  1567. iterator_type first_to_delete = prev;
  1568. expanded.erase(++first_to_delete, ++next);
  1569. }
  1570. it = next;
  1571. continue;
  1572. }
  1573. // save last non-whitespace token position
  1574. if (!IS_CATEGORY(*it, WhiteSpaceTokenType))
  1575. prev = it;
  1576. ++it; // next token, please
  1577. }
  1578. return true;
  1579. }
  1580. ///////////////////////////////////////////////////////////////////////////////
  1581. //
  1582. // predefine_macro(): predefine a single macro
  1583. //
  1584. ///////////////////////////////////////////////////////////////////////////////
  1585. template <typename ContextT>
  1586. inline void
  1587. macromap<ContextT>::predefine_macro(defined_macros_type *scope,
  1588. string_type const &name, token_type const &t)
  1589. {
  1590. definition_container_type macrodefinition;
  1591. std::vector<token_type> param;
  1592. macrodefinition.push_back(t);
  1593. add_macro(token_type(T_IDENTIFIER, name, t.get_position()),
  1594. false, param, macrodefinition, true, scope);
  1595. }
  1596. ///////////////////////////////////////////////////////////////////////////////
  1597. //
  1598. // init_predefined_macros(): init the predefined macros
  1599. //
  1600. ///////////////////////////////////////////////////////////////////////////////
  1601. template <typename ContextT>
  1602. inline void
  1603. macromap<ContextT>::init_predefined_macros(char const *fname,
  1604. defined_macros_type *scope, bool at_global_scope)
  1605. {
  1606. // if no scope is given, use the current one
  1607. defined_macros_type *current_scope = scope ? scope : current_macros;
  1608. // first, add the static macros
  1609. position_type pos("<built-in>");
  1610. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  1611. if (boost::wave::need_c99(ctx.get_language())) {
  1612. // define C99 specifics
  1613. for (int i = 0; 0 != predef.static_data_c99(i).name; ++i) {
  1614. predefined_macros::static_macros const& m = predef.static_data_c99(i);
  1615. predefine_macro(current_scope, m.name,
  1616. token_type(m.token_id, m.value, pos));
  1617. }
  1618. }
  1619. else
  1620. #endif
  1621. {
  1622. #if BOOST_WAVE_SUPPORT_CPP0X != 0
  1623. if (boost::wave::need_cpp0x(ctx.get_language())) {
  1624. // define C++11 specifics
  1625. for (int i = 0; 0 != predef.static_data_cpp0x(i).name; ++i) {
  1626. predefined_macros::static_macros const& m = predef.static_data_cpp0x(i);
  1627. predefine_macro(current_scope, m.name,
  1628. token_type(m.token_id, m.value, pos));
  1629. }
  1630. }
  1631. else
  1632. #endif
  1633. {
  1634. // define C++ specifics
  1635. for (int i = 0; 0 != predef.static_data_cpp(i).name; ++i) {
  1636. predefined_macros::static_macros const& m = predef.static_data_cpp(i);
  1637. predefine_macro(current_scope, m.name,
  1638. token_type(m.token_id, m.value, pos));
  1639. }
  1640. #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
  1641. // define __WAVE_HAS_VARIADICS__, if appropriate
  1642. if (boost::wave::need_variadics(ctx.get_language())) {
  1643. predefine_macro(current_scope, "__WAVE_HAS_VARIADICS__",
  1644. token_type(T_INTLIT, "1", pos));
  1645. }
  1646. #endif
  1647. }
  1648. }
  1649. // predefine the __BASE_FILE__ macro which contains the main file name
  1650. namespace fs = boost::filesystem;
  1651. if (string_type(fname) != "<Unknown>") {
  1652. fs::path filename(create_path(fname));
  1653. using boost::wave::util::impl::escape_lit;
  1654. predefine_macro(current_scope, "__BASE_FILE__",
  1655. token_type(T_STRINGLIT, string_type("\"") +
  1656. escape_lit(native_file_string(filename)).c_str() + "\"", pos));
  1657. base_name = fname;
  1658. }
  1659. else if (!base_name.empty()) {
  1660. fs::path filename(create_path(base_name.c_str()));
  1661. using boost::wave::util::impl::escape_lit;
  1662. predefine_macro(current_scope, "__BASE_FILE__",
  1663. token_type(T_STRINGLIT, string_type("\"") +
  1664. escape_lit(native_file_string(filename)).c_str() + "\"", pos));
  1665. }
  1666. // now add the dynamic macros
  1667. for (int j = 0; 0 != predef.dynamic_data(j).name; ++j) {
  1668. predefined_macros::dynamic_macros const& m = predef.dynamic_data(j);
  1669. predefine_macro(current_scope, m.name,
  1670. token_type(m.token_id, (predef.* m.generator)(), pos));
  1671. }
  1672. }
  1673. ///////////////////////////////////////////////////////////////////////////////
  1674. //
  1675. // reset_macromap(): initialize the internal macro symbol namespace
  1676. //
  1677. ///////////////////////////////////////////////////////////////////////////////
  1678. template <typename ContextT>
  1679. inline void
  1680. macromap<ContextT>::reset_macromap()
  1681. {
  1682. current_macros->clear();
  1683. predef.reset();
  1684. act_token = token_type();
  1685. }
  1686. ///////////////////////////////////////////////////////////////////////////////
  1687. }}} // namespace boost::wave::util
  1688. #if BOOST_WAVE_SERIALIZATION != 0
  1689. namespace boost { namespace serialization {
  1690. template<typename ContextT>
  1691. struct version<boost::wave::util::macromap<ContextT> >
  1692. {
  1693. typedef boost::wave::util::macromap<ContextT> target_type;
  1694. typedef mpl::int_<target_type::version> type;
  1695. typedef mpl::integral_c_tag tag;
  1696. BOOST_STATIC_CONSTANT(unsigned int, value = version::type::value);
  1697. };
  1698. }} // namespace boost::serialization
  1699. #endif
  1700. // the suffix header occurs after all of the code
  1701. #ifdef BOOST_HAS_ABI_HEADERS
  1702. #include BOOST_ABI_SUFFIX
  1703. #endif
  1704. #endif // !defined(CPP_MACROMAP_HPP_CB8F51B0_A3F0_411C_AEF4_6FF631B8B414_INCLUDED)