macro.c 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448
  1. /* Part of CPP library. (Macro and #define handling.)
  2. Copyright (C) 1986-2015 Free Software Foundation, Inc.
  3. Written by Per Bothner, 1994.
  4. Based on CCCP program by Paul Rubin, June 1986
  5. Adapted to ANSI C, Richard Stallman, Jan 1987
  6. This program is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 3, or (at your option) any
  9. later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; see the file COPYING3. If not see
  16. <http://www.gnu.org/licenses/>.
  17. In other words, you are welcome to use, share and improve this program.
  18. You are forbidden to forbid anyone else to use, share and improve
  19. what you give them. Help stamp out software-hoarding! */
  20. #include "config.h"
  21. #include "system.h"
  22. #include "cpplib.h"
  23. #include "internal.h"
  24. typedef struct macro_arg macro_arg;
  25. /* This structure represents the tokens of a macro argument. These
  26. tokens can be macro themselves, in which case they can be either
  27. expanded or unexpanded. When they are expanded, this data
  28. structure keeps both the expanded and unexpanded forms. */
  29. struct macro_arg
  30. {
  31. const cpp_token **first; /* First token in unexpanded argument. */
  32. const cpp_token **expanded; /* Macro-expanded argument. */
  33. const cpp_token *stringified; /* Stringified argument. */
  34. unsigned int count; /* # of tokens in argument. */
  35. unsigned int expanded_count; /* # of tokens in expanded argument. */
  36. source_location *virt_locs; /* Where virtual locations for
  37. unexpanded tokens are stored. */
  38. source_location *expanded_virt_locs; /* Where virtual locations for
  39. expanded tokens are
  40. stored. */
  41. };
  42. /* The kind of macro tokens which the instance of
  43. macro_arg_token_iter is supposed to iterate over. */
  44. enum macro_arg_token_kind {
  45. MACRO_ARG_TOKEN_NORMAL,
  46. /* This is a macro argument token that got transformed into a string
  47. litteral, e.g. #foo. */
  48. MACRO_ARG_TOKEN_STRINGIFIED,
  49. /* This is a token resulting from the expansion of a macro
  50. argument that was itself a macro. */
  51. MACRO_ARG_TOKEN_EXPANDED
  52. };
  53. /* An iterator over tokens coming from a function-like macro
  54. argument. */
  55. typedef struct macro_arg_token_iter macro_arg_token_iter;
  56. struct macro_arg_token_iter
  57. {
  58. /* Whether or not -ftrack-macro-expansion is used. */
  59. bool track_macro_exp_p;
  60. /* The kind of token over which we are supposed to iterate. */
  61. enum macro_arg_token_kind kind;
  62. /* A pointer to the current token pointed to by the iterator. */
  63. const cpp_token **token_ptr;
  64. /* A pointer to the "full" location of the current token. If
  65. -ftrack-macro-expansion is used this location tracks loci across
  66. macro expansion. */
  67. const source_location *location_ptr;
  68. #ifdef ENABLE_CHECKING
  69. /* The number of times the iterator went forward. This useful only
  70. when checking is enabled. */
  71. size_t num_forwards;
  72. #endif
  73. };
  74. /* Saved data about an identifier being used as a macro argument
  75. name. */
  76. struct macro_arg_saved_data {
  77. /* The canonical (UTF-8) spelling of this identifier. */
  78. cpp_hashnode *canonical_node;
  79. /* The previous value of this identifier. */
  80. union _cpp_hashnode_value value;
  81. };
  82. /* Macro expansion. */
  83. static int enter_macro_context (cpp_reader *, cpp_hashnode *,
  84. const cpp_token *, source_location);
  85. static int builtin_macro (cpp_reader *, cpp_hashnode *, source_location);
  86. static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *,
  87. const cpp_token **, unsigned int);
  88. static void push_extended_tokens_context (cpp_reader *, cpp_hashnode *,
  89. _cpp_buff *, source_location *,
  90. const cpp_token **, unsigned int);
  91. static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *,
  92. _cpp_buff **, unsigned *);
  93. static cpp_context *next_context (cpp_reader *);
  94. static const cpp_token *padding_token (cpp_reader *, const cpp_token *);
  95. static void expand_arg (cpp_reader *, macro_arg *);
  96. static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int);
  97. static const cpp_token *stringify_arg (cpp_reader *, macro_arg *);
  98. static void paste_all_tokens (cpp_reader *, const cpp_token *);
  99. static bool paste_tokens (cpp_reader *, source_location,
  100. const cpp_token **, const cpp_token *);
  101. static void alloc_expanded_arg_mem (cpp_reader *, macro_arg *, size_t);
  102. static void ensure_expanded_arg_room (cpp_reader *, macro_arg *, size_t, size_t *);
  103. static void delete_macro_args (_cpp_buff*, unsigned num_args);
  104. static void set_arg_token (macro_arg *, const cpp_token *,
  105. source_location, size_t,
  106. enum macro_arg_token_kind,
  107. bool);
  108. static const source_location *get_arg_token_location (const macro_arg *,
  109. enum macro_arg_token_kind);
  110. static const cpp_token **arg_token_ptr_at (const macro_arg *,
  111. size_t,
  112. enum macro_arg_token_kind,
  113. source_location **virt_location);
  114. static void macro_arg_token_iter_init (macro_arg_token_iter *, bool,
  115. enum macro_arg_token_kind,
  116. const macro_arg *,
  117. const cpp_token **);
  118. static const cpp_token *macro_arg_token_iter_get_token
  119. (const macro_arg_token_iter *it);
  120. static source_location macro_arg_token_iter_get_location
  121. (const macro_arg_token_iter *);
  122. static void macro_arg_token_iter_forward (macro_arg_token_iter *);
  123. static _cpp_buff *tokens_buff_new (cpp_reader *, size_t,
  124. source_location **);
  125. static size_t tokens_buff_count (_cpp_buff *);
  126. static const cpp_token **tokens_buff_last_token_ptr (_cpp_buff *);
  127. static inline const cpp_token **tokens_buff_put_token_to (const cpp_token **,
  128. source_location *,
  129. const cpp_token *,
  130. source_location,
  131. source_location,
  132. const struct line_map *,
  133. unsigned int);
  134. static const cpp_token **tokens_buff_add_token (_cpp_buff *,
  135. source_location *,
  136. const cpp_token *,
  137. source_location,
  138. source_location,
  139. const struct line_map *,
  140. unsigned int);
  141. static inline void tokens_buff_remove_last_token (_cpp_buff *);
  142. static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *,
  143. macro_arg *, source_location);
  144. static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *,
  145. _cpp_buff **, unsigned *);
  146. static bool create_iso_definition (cpp_reader *, cpp_macro *);
  147. /* #define directive parsing and handling. */
  148. static cpp_token *alloc_expansion_token (cpp_reader *, cpp_macro *);
  149. static cpp_token *lex_expansion_token (cpp_reader *, cpp_macro *);
  150. static bool warn_of_redefinition (cpp_reader *, cpp_hashnode *,
  151. const cpp_macro *);
  152. static bool parse_params (cpp_reader *, cpp_macro *);
  153. static void check_trad_stringification (cpp_reader *, const cpp_macro *,
  154. const cpp_string *);
  155. static bool reached_end_of_context (cpp_context *);
  156. static void consume_next_token_from_context (cpp_reader *pfile,
  157. const cpp_token **,
  158. source_location *);
  159. static const cpp_token* cpp_get_token_1 (cpp_reader *, source_location *);
  160. static cpp_hashnode* macro_of_context (cpp_context *context);
  161. static bool in_macro_expansion_p (cpp_reader *pfile);
  162. /* Statistical counter tracking the number of macros that got
  163. expanded. */
  164. unsigned num_expanded_macros_counter = 0;
  165. /* Statistical counter tracking the total number tokens resulting
  166. from macro expansion. */
  167. unsigned num_macro_tokens_counter = 0;
  168. /* Emits a warning if NODE is a macro defined in the main file that
  169. has not been used. */
  170. int
  171. _cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node,
  172. void *v ATTRIBUTE_UNUSED)
  173. {
  174. if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
  175. {
  176. cpp_macro *macro = node->value.macro;
  177. if (!macro->used
  178. && MAIN_FILE_P (linemap_lookup (pfile->line_table, macro->line)))
  179. cpp_warning_with_line (pfile, CPP_W_UNUSED_MACROS, macro->line, 0,
  180. "macro \"%s\" is not used", NODE_NAME (node));
  181. }
  182. return 1;
  183. }
  184. /* Allocates and returns a CPP_STRING token, containing TEXT of length
  185. LEN, after null-terminating it. TEXT must be in permanent storage. */
  186. static const cpp_token *
  187. new_string_token (cpp_reader *pfile, unsigned char *text, unsigned int len)
  188. {
  189. cpp_token *token = _cpp_temp_token (pfile);
  190. text[len] = '\0';
  191. token->type = CPP_STRING;
  192. token->val.str.len = len;
  193. token->val.str.text = text;
  194. token->flags = 0;
  195. return token;
  196. }
  197. static const char * const monthnames[] =
  198. {
  199. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  200. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  201. };
  202. /* Helper function for builtin_macro. Returns the text generated by
  203. a builtin macro. */
  204. const uchar *
  205. _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
  206. {
  207. const uchar *result = NULL;
  208. linenum_type number = 1;
  209. switch (node->value.builtin)
  210. {
  211. default:
  212. cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
  213. NODE_NAME (node));
  214. break;
  215. case BT_TIMESTAMP:
  216. {
  217. if (CPP_OPTION (pfile, warn_date_time))
  218. cpp_warning (pfile, CPP_W_DATE_TIME, "macro \"%s\" might prevent "
  219. "reproducible builds", NODE_NAME (node));
  220. cpp_buffer *pbuffer = cpp_get_buffer (pfile);
  221. if (pbuffer->timestamp == NULL)
  222. {
  223. /* Initialize timestamp value of the assotiated file. */
  224. struct _cpp_file *file = cpp_get_file (pbuffer);
  225. if (file)
  226. {
  227. /* Generate __TIMESTAMP__ string, that represents
  228. the date and time of the last modification
  229. of the current source file. The string constant
  230. looks like "Sun Sep 16 01:03:52 1973". */
  231. struct tm *tb = NULL;
  232. struct stat *st = _cpp_get_file_stat (file);
  233. if (st)
  234. tb = localtime (&st->st_mtime);
  235. if (tb)
  236. {
  237. char *str = asctime (tb);
  238. size_t len = strlen (str);
  239. unsigned char *buf = _cpp_unaligned_alloc (pfile, len + 2);
  240. buf[0] = '"';
  241. strcpy ((char *) buf + 1, str);
  242. buf[len] = '"';
  243. pbuffer->timestamp = buf;
  244. }
  245. else
  246. {
  247. cpp_errno (pfile, CPP_DL_WARNING,
  248. "could not determine file timestamp");
  249. pbuffer->timestamp = UC"\"??? ??? ?? ??:??:?? ????\"";
  250. }
  251. }
  252. }
  253. result = pbuffer->timestamp;
  254. }
  255. break;
  256. case BT_FILE:
  257. case BT_BASE_FILE:
  258. {
  259. unsigned int len;
  260. const char *name;
  261. uchar *buf;
  262. if (node->value.builtin == BT_FILE)
  263. name = linemap_get_expansion_filename (pfile->line_table,
  264. pfile->line_table->highest_line);
  265. else
  266. {
  267. name = _cpp_get_file_name (pfile->main_file);
  268. if (!name)
  269. abort ();
  270. }
  271. len = strlen (name);
  272. buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
  273. result = buf;
  274. *buf = '"';
  275. buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
  276. *buf++ = '"';
  277. *buf = '\0';
  278. }
  279. break;
  280. case BT_INCLUDE_LEVEL:
  281. /* The line map depth counts the primary source as level 1, but
  282. historically __INCLUDE_DEPTH__ has called the primary source
  283. level 0. */
  284. number = pfile->line_table->depth - 1;
  285. break;
  286. case BT_SPECLINE:
  287. /* If __LINE__ is embedded in a macro, it must expand to the
  288. line of the macro's invocation, not its definition.
  289. Otherwise things like assert() will not work properly. */
  290. number = linemap_get_expansion_line (pfile->line_table,
  291. CPP_OPTION (pfile, traditional)
  292. ? pfile->line_table->highest_line
  293. : pfile->cur_token[-1].src_loc);
  294. break;
  295. /* __STDC__ has the value 1 under normal circumstances.
  296. However, if (a) we are in a system header, (b) the option
  297. stdc_0_in_system_headers is true (set by target config), and
  298. (c) we are not in strictly conforming mode, then it has the
  299. value 0. (b) and (c) are already checked in cpp_init_builtins. */
  300. case BT_STDC:
  301. if (cpp_in_system_header (pfile))
  302. number = 0;
  303. else
  304. number = 1;
  305. break;
  306. case BT_DATE:
  307. case BT_TIME:
  308. if (CPP_OPTION (pfile, warn_date_time))
  309. cpp_warning (pfile, CPP_W_DATE_TIME, "macro \"%s\" might prevent "
  310. "reproducible builds", NODE_NAME (node));
  311. if (pfile->date == NULL)
  312. {
  313. /* Allocate __DATE__ and __TIME__ strings from permanent
  314. storage. We only do this once, and don't generate them
  315. at init time, because time() and localtime() are very
  316. slow on some systems. */
  317. time_t tt;
  318. struct tm *tb = NULL;
  319. /* (time_t) -1 is a legitimate value for "number of seconds
  320. since the Epoch", so we have to do a little dance to
  321. distinguish that from a genuine error. */
  322. errno = 0;
  323. tt = time(NULL);
  324. if (tt != (time_t)-1 || errno == 0)
  325. tb = localtime (&tt);
  326. if (tb)
  327. {
  328. pfile->date = _cpp_unaligned_alloc (pfile,
  329. sizeof ("\"Oct 11 1347\""));
  330. sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
  331. monthnames[tb->tm_mon], tb->tm_mday,
  332. tb->tm_year + 1900);
  333. pfile->time = _cpp_unaligned_alloc (pfile,
  334. sizeof ("\"12:34:56\""));
  335. sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
  336. tb->tm_hour, tb->tm_min, tb->tm_sec);
  337. }
  338. else
  339. {
  340. cpp_errno (pfile, CPP_DL_WARNING,
  341. "could not determine date and time");
  342. pfile->date = UC"\"??? ?? ????\"";
  343. pfile->time = UC"\"??:??:??\"";
  344. }
  345. }
  346. if (node->value.builtin == BT_DATE)
  347. result = pfile->date;
  348. else
  349. result = pfile->time;
  350. break;
  351. case BT_COUNTER:
  352. if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive)
  353. cpp_error (pfile, CPP_DL_ERROR,
  354. "__COUNTER__ expanded inside directive with -fdirectives-only");
  355. number = pfile->counter++;
  356. break;
  357. case BT_HAS_ATTRIBUTE:
  358. number = pfile->cb.has_attribute (pfile);
  359. break;
  360. }
  361. if (result == NULL)
  362. {
  363. /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
  364. result = _cpp_unaligned_alloc (pfile, 21);
  365. sprintf ((char *) result, "%u", number);
  366. }
  367. return result;
  368. }
  369. /* Convert builtin macros like __FILE__ to a token and push it on the
  370. context stack. Also handles _Pragma, for which a new token may not
  371. be created. Returns 1 if it generates a new token context, 0 to
  372. return the token to the caller. LOC is the location of the expansion
  373. point of the macro. */
  374. static int
  375. builtin_macro (cpp_reader *pfile, cpp_hashnode *node, source_location loc)
  376. {
  377. const uchar *buf;
  378. size_t len;
  379. char *nbuf;
  380. if (node->value.builtin == BT_PRAGMA)
  381. {
  382. /* Don't interpret _Pragma within directives. The standard is
  383. not clear on this, but to me this makes most sense. */
  384. if (pfile->state.in_directive)
  385. return 0;
  386. return _cpp_do__Pragma (pfile);
  387. }
  388. buf = _cpp_builtin_macro_text (pfile, node);
  389. len = ustrlen (buf);
  390. nbuf = (char *) alloca (len + 1);
  391. memcpy (nbuf, buf, len);
  392. nbuf[len]='\n';
  393. cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true);
  394. _cpp_clean_line (pfile);
  395. /* Set pfile->cur_token as required by _cpp_lex_direct. */
  396. pfile->cur_token = _cpp_temp_token (pfile);
  397. cpp_token *token = _cpp_lex_direct (pfile);
  398. /* We should point to the expansion point of the builtin macro. */
  399. token->src_loc = loc;
  400. if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
  401. {
  402. /* We are tracking tokens resulting from macro expansion.
  403. Create a macro line map and generate a virtual location for
  404. the token resulting from the expansion of the built-in
  405. macro. */
  406. source_location *virt_locs = NULL;
  407. _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs);
  408. const line_map * map =
  409. linemap_enter_macro (pfile->line_table, node,
  410. token->src_loc, 1);
  411. tokens_buff_add_token (token_buf, virt_locs, token,
  412. pfile->line_table->builtin_location,
  413. pfile->line_table->builtin_location,
  414. map, /*macro_token_index=*/0);
  415. push_extended_tokens_context (pfile, node, token_buf, virt_locs,
  416. (const cpp_token **)token_buf->base,
  417. 1);
  418. }
  419. else
  420. _cpp_push_token_context (pfile, NULL, token, 1);
  421. if (pfile->buffer->cur != pfile->buffer->rlimit)
  422. cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
  423. NODE_NAME (node));
  424. _cpp_pop_buffer (pfile);
  425. return 1;
  426. }
  427. /* Copies SRC, of length LEN, to DEST, adding backslashes before all
  428. backslashes and double quotes. DEST must be of sufficient size.
  429. Returns a pointer to the end of the string. */
  430. uchar *
  431. cpp_quote_string (uchar *dest, const uchar *src, unsigned int len)
  432. {
  433. while (len--)
  434. {
  435. uchar c = *src++;
  436. if (c == '\\' || c == '"')
  437. {
  438. *dest++ = '\\';
  439. *dest++ = c;
  440. }
  441. else
  442. *dest++ = c;
  443. }
  444. return dest;
  445. }
  446. /* Convert a token sequence ARG to a single string token according to
  447. the rules of the ISO C #-operator. */
  448. static const cpp_token *
  449. stringify_arg (cpp_reader *pfile, macro_arg *arg)
  450. {
  451. unsigned char *dest;
  452. unsigned int i, escape_it, backslash_count = 0;
  453. const cpp_token *source = NULL;
  454. size_t len;
  455. if (BUFF_ROOM (pfile->u_buff) < 3)
  456. _cpp_extend_buff (pfile, &pfile->u_buff, 3);
  457. dest = BUFF_FRONT (pfile->u_buff);
  458. *dest++ = '"';
  459. /* Loop, reading in the argument's tokens. */
  460. for (i = 0; i < arg->count; i++)
  461. {
  462. const cpp_token *token = arg->first[i];
  463. if (token->type == CPP_PADDING)
  464. {
  465. if (source == NULL
  466. || (!(source->flags & PREV_WHITE)
  467. && token->val.source == NULL))
  468. source = token->val.source;
  469. continue;
  470. }
  471. escape_it = (token->type == CPP_STRING || token->type == CPP_CHAR
  472. || token->type == CPP_WSTRING || token->type == CPP_WCHAR
  473. || token->type == CPP_STRING32 || token->type == CPP_CHAR32
  474. || token->type == CPP_STRING16 || token->type == CPP_CHAR16
  475. || token->type == CPP_UTF8STRING
  476. || cpp_userdef_string_p (token->type)
  477. || cpp_userdef_char_p (token->type));
  478. /* Room for each char being written in octal, initial space and
  479. final quote and NUL. */
  480. len = cpp_token_len (token);
  481. if (escape_it)
  482. len *= 4;
  483. len += 3;
  484. if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
  485. {
  486. size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
  487. _cpp_extend_buff (pfile, &pfile->u_buff, len);
  488. dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
  489. }
  490. /* Leading white space? */
  491. if (dest - 1 != BUFF_FRONT (pfile->u_buff))
  492. {
  493. if (source == NULL)
  494. source = token;
  495. if (source->flags & PREV_WHITE)
  496. *dest++ = ' ';
  497. }
  498. source = NULL;
  499. if (escape_it)
  500. {
  501. _cpp_buff *buff = _cpp_get_buff (pfile, len);
  502. unsigned char *buf = BUFF_FRONT (buff);
  503. len = cpp_spell_token (pfile, token, buf, true) - buf;
  504. dest = cpp_quote_string (dest, buf, len);
  505. _cpp_release_buff (pfile, buff);
  506. }
  507. else
  508. dest = cpp_spell_token (pfile, token, dest, true);
  509. if (token->type == CPP_OTHER && token->val.str.text[0] == '\\')
  510. backslash_count++;
  511. else
  512. backslash_count = 0;
  513. }
  514. /* Ignore the final \ of invalid string literals. */
  515. if (backslash_count & 1)
  516. {
  517. cpp_error (pfile, CPP_DL_WARNING,
  518. "invalid string literal, ignoring final '\\'");
  519. dest--;
  520. }
  521. /* Commit the memory, including NUL, and return the token. */
  522. *dest++ = '"';
  523. len = dest - BUFF_FRONT (pfile->u_buff);
  524. BUFF_FRONT (pfile->u_buff) = dest + 1;
  525. return new_string_token (pfile, dest - len, len);
  526. }
  527. /* Try to paste two tokens. On success, return nonzero. In any
  528. case, PLHS is updated to point to the pasted token, which is
  529. guaranteed to not have the PASTE_LEFT flag set. LOCATION is
  530. the virtual location used for error reporting. */
  531. static bool
  532. paste_tokens (cpp_reader *pfile, source_location location,
  533. const cpp_token **plhs, const cpp_token *rhs)
  534. {
  535. unsigned char *buf, *end, *lhsend;
  536. cpp_token *lhs;
  537. unsigned int len;
  538. len = cpp_token_len (*plhs) + cpp_token_len (rhs) + 1;
  539. buf = (unsigned char *) alloca (len);
  540. end = lhsend = cpp_spell_token (pfile, *plhs, buf, true);
  541. /* Avoid comment headers, since they are still processed in stage 3.
  542. It is simpler to insert a space here, rather than modifying the
  543. lexer to ignore comments in some circumstances. Simply returning
  544. false doesn't work, since we want to clear the PASTE_LEFT flag. */
  545. if ((*plhs)->type == CPP_DIV && rhs->type != CPP_EQ)
  546. *end++ = ' ';
  547. /* In one obscure case we might see padding here. */
  548. if (rhs->type != CPP_PADDING)
  549. end = cpp_spell_token (pfile, rhs, end, true);
  550. *end = '\n';
  551. cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true);
  552. _cpp_clean_line (pfile);
  553. /* Set pfile->cur_token as required by _cpp_lex_direct. */
  554. pfile->cur_token = _cpp_temp_token (pfile);
  555. lhs = _cpp_lex_direct (pfile);
  556. if (pfile->buffer->cur != pfile->buffer->rlimit)
  557. {
  558. source_location saved_loc = lhs->src_loc;
  559. _cpp_pop_buffer (pfile);
  560. _cpp_backup_tokens (pfile, 1);
  561. *lhsend = '\0';
  562. /* We have to remove the PASTE_LEFT flag from the old lhs, but
  563. we want to keep the new location. */
  564. *lhs = **plhs;
  565. *plhs = lhs;
  566. lhs->src_loc = saved_loc;
  567. lhs->flags &= ~PASTE_LEFT;
  568. /* Mandatory error for all apart from assembler. */
  569. if (CPP_OPTION (pfile, lang) != CLK_ASM)
  570. cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
  571. "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
  572. buf, cpp_token_as_text (pfile, rhs));
  573. return false;
  574. }
  575. *plhs = lhs;
  576. _cpp_pop_buffer (pfile);
  577. return true;
  578. }
  579. /* Handles an arbitrarily long sequence of ## operators, with initial
  580. operand LHS. This implementation is left-associative,
  581. non-recursive, and finishes a paste before handling succeeding
  582. ones. If a paste fails, we back up to the RHS of the failing ##
  583. operator before pushing the context containing the result of prior
  584. successful pastes, with the effect that the RHS appears in the
  585. output stream after the pasted LHS normally. */
  586. static void
  587. paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
  588. {
  589. const cpp_token *rhs = NULL;
  590. cpp_context *context = pfile->context;
  591. source_location virt_loc = 0;
  592. /* We are expanding a macro and we must have been called on a token
  593. that appears at the left hand side of a ## operator. */
  594. if (macro_of_context (pfile->context) == NULL
  595. || (!(lhs->flags & PASTE_LEFT)))
  596. abort ();
  597. if (context->tokens_kind == TOKENS_KIND_EXTENDED)
  598. /* The caller must have called consume_next_token_from_context
  599. right before calling us. That has incremented the pointer to
  600. the current virtual location. So it now points to the location
  601. of the token that comes right after *LHS. We want the
  602. resulting pasted token to have the location of the current
  603. *LHS, though. */
  604. virt_loc = context->c.mc->cur_virt_loc[-1];
  605. else
  606. /* We are not tracking macro expansion. So the best virtual
  607. location we can get here is the expansion point of the macro we
  608. are currently expanding. */
  609. virt_loc = pfile->invocation_location;
  610. do
  611. {
  612. /* Take the token directly from the current context. We can do
  613. this, because we are in the replacement list of either an
  614. object-like macro, or a function-like macro with arguments
  615. inserted. In either case, the constraints to #define
  616. guarantee we have at least one more token. */
  617. if (context->tokens_kind == TOKENS_KIND_DIRECT)
  618. rhs = FIRST (context).token++;
  619. else if (context->tokens_kind == TOKENS_KIND_INDIRECT)
  620. rhs = *FIRST (context).ptoken++;
  621. else if (context->tokens_kind == TOKENS_KIND_EXTENDED)
  622. {
  623. /* So we are in presence of an extended token context, which
  624. means that each token in this context has a virtual
  625. location attached to it. So let's not forget to update
  626. the pointer to the current virtual location of the
  627. current token when we update the pointer to the current
  628. token */
  629. rhs = *FIRST (context).ptoken++;
  630. /* context->c.mc must be non-null, as if we were not in a
  631. macro context, context->tokens_kind could not be equal to
  632. TOKENS_KIND_EXTENDED. */
  633. context->c.mc->cur_virt_loc++;
  634. }
  635. if (rhs->type == CPP_PADDING)
  636. {
  637. if (rhs->flags & PASTE_LEFT)
  638. abort ();
  639. }
  640. if (!paste_tokens (pfile, virt_loc, &lhs, rhs))
  641. break;
  642. }
  643. while (rhs->flags & PASTE_LEFT);
  644. /* Put the resulting token in its own context. */
  645. if (context->tokens_kind == TOKENS_KIND_EXTENDED)
  646. {
  647. source_location *virt_locs = NULL;
  648. _cpp_buff *token_buf = tokens_buff_new (pfile, 1, &virt_locs);
  649. tokens_buff_add_token (token_buf, virt_locs, lhs,
  650. virt_loc, 0, NULL, 0);
  651. push_extended_tokens_context (pfile, context->c.mc->macro_node,
  652. token_buf, virt_locs,
  653. (const cpp_token **)token_buf->base, 1);
  654. }
  655. else
  656. _cpp_push_token_context (pfile, NULL, lhs, 1);
  657. }
  658. /* Returns TRUE if the number of arguments ARGC supplied in an
  659. invocation of the MACRO referenced by NODE is valid. An empty
  660. invocation to a macro with no parameters should pass ARGC as zero.
  661. Note that MACRO cannot necessarily be deduced from NODE, in case
  662. NODE was redefined whilst collecting arguments. */
  663. bool
  664. _cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc)
  665. {
  666. if (argc == macro->paramc)
  667. return true;
  668. if (argc < macro->paramc)
  669. {
  670. /* As an extension, variadic arguments are allowed to not appear in
  671. the invocation at all.
  672. e.g. #define debug(format, args...) something
  673. debug("string");
  674. This is exactly the same as if an empty variadic list had been
  675. supplied - debug("string", ). */
  676. if (argc + 1 == macro->paramc && macro->variadic)
  677. {
  678. if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
  679. {
  680. if (CPP_OPTION (pfile, cplusplus))
  681. cpp_error (pfile, CPP_DL_PEDWARN,
  682. "ISO C++11 requires at least one argument "
  683. "for the \"...\" in a variadic macro");
  684. else
  685. cpp_error (pfile, CPP_DL_PEDWARN,
  686. "ISO C99 requires at least one argument "
  687. "for the \"...\" in a variadic macro");
  688. }
  689. return true;
  690. }
  691. cpp_error (pfile, CPP_DL_ERROR,
  692. "macro \"%s\" requires %u arguments, but only %u given",
  693. NODE_NAME (node), macro->paramc, argc);
  694. }
  695. else
  696. cpp_error (pfile, CPP_DL_ERROR,
  697. "macro \"%s\" passed %u arguments, but takes just %u",
  698. NODE_NAME (node), argc, macro->paramc);
  699. return false;
  700. }
  701. /* Reads and returns the arguments to a function-like macro
  702. invocation. Assumes the opening parenthesis has been processed.
  703. If there is an error, emits an appropriate diagnostic and returns
  704. NULL. Each argument is terminated by a CPP_EOF token, for the
  705. future benefit of expand_arg(). If there are any deferred
  706. #pragma directives among macro arguments, store pointers to the
  707. CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer.
  708. What is returned is the buffer that contains the memory allocated
  709. to hold the macro arguments. NODE is the name of the macro this
  710. function is dealing with. If NUM_ARGS is non-NULL, *NUM_ARGS is
  711. set to the actual number of macro arguments allocated in the
  712. returned buffer. */
  713. static _cpp_buff *
  714. collect_args (cpp_reader *pfile, const cpp_hashnode *node,
  715. _cpp_buff **pragma_buff, unsigned *num_args)
  716. {
  717. _cpp_buff *buff, *base_buff;
  718. cpp_macro *macro;
  719. macro_arg *args, *arg;
  720. const cpp_token *token;
  721. unsigned int argc;
  722. source_location virt_loc;
  723. bool track_macro_expansion_p = CPP_OPTION (pfile, track_macro_expansion);
  724. unsigned num_args_alloced = 0;
  725. macro = node->value.macro;
  726. if (macro->paramc)
  727. argc = macro->paramc;
  728. else
  729. argc = 1;
  730. #define DEFAULT_NUM_TOKENS_PER_MACRO_ARG 50
  731. #define ARG_TOKENS_EXTENT 1000
  732. buff = _cpp_get_buff (pfile, argc * (DEFAULT_NUM_TOKENS_PER_MACRO_ARG
  733. * sizeof (cpp_token *)
  734. + sizeof (macro_arg)));
  735. base_buff = buff;
  736. args = (macro_arg *) buff->base;
  737. memset (args, 0, argc * sizeof (macro_arg));
  738. buff->cur = (unsigned char *) &args[argc];
  739. arg = args, argc = 0;
  740. /* Collect the tokens making up each argument. We don't yet know
  741. how many arguments have been supplied, whether too many or too
  742. few. Hence the slightly bizarre usage of "argc" and "arg". */
  743. do
  744. {
  745. unsigned int paren_depth = 0;
  746. unsigned int ntokens = 0;
  747. unsigned virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
  748. num_args_alloced++;
  749. argc++;
  750. arg->first = (const cpp_token **) buff->cur;
  751. if (track_macro_expansion_p)
  752. {
  753. virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
  754. arg->virt_locs = XNEWVEC (source_location,
  755. virt_locs_capacity);
  756. }
  757. for (;;)
  758. {
  759. /* Require space for 2 new tokens (including a CPP_EOF). */
  760. if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
  761. {
  762. buff = _cpp_append_extend_buff (pfile, buff,
  763. ARG_TOKENS_EXTENT
  764. * sizeof (cpp_token *));
  765. arg->first = (const cpp_token **) buff->cur;
  766. }
  767. if (track_macro_expansion_p
  768. && (ntokens + 2 > virt_locs_capacity))
  769. {
  770. virt_locs_capacity += ARG_TOKENS_EXTENT;
  771. arg->virt_locs = XRESIZEVEC (source_location,
  772. arg->virt_locs,
  773. virt_locs_capacity);
  774. }
  775. token = cpp_get_token_1 (pfile, &virt_loc);
  776. if (token->type == CPP_PADDING)
  777. {
  778. /* Drop leading padding. */
  779. if (ntokens == 0)
  780. continue;
  781. }
  782. else if (token->type == CPP_OPEN_PAREN)
  783. paren_depth++;
  784. else if (token->type == CPP_CLOSE_PAREN)
  785. {
  786. if (paren_depth-- == 0)
  787. break;
  788. }
  789. else if (token->type == CPP_COMMA)
  790. {
  791. /* A comma does not terminate an argument within
  792. parentheses or as part of a variable argument. */
  793. if (paren_depth == 0
  794. && ! (macro->variadic && argc == macro->paramc))
  795. break;
  796. }
  797. else if (token->type == CPP_EOF
  798. || (token->type == CPP_HASH && token->flags & BOL))
  799. break;
  800. else if (token->type == CPP_PRAGMA)
  801. {
  802. cpp_token *newtok = _cpp_temp_token (pfile);
  803. /* CPP_PRAGMA token lives in directive_result, which will
  804. be overwritten on the next directive. */
  805. *newtok = *token;
  806. token = newtok;
  807. do
  808. {
  809. if (*pragma_buff == NULL
  810. || BUFF_ROOM (*pragma_buff) < sizeof (cpp_token *))
  811. {
  812. _cpp_buff *next;
  813. if (*pragma_buff == NULL)
  814. *pragma_buff
  815. = _cpp_get_buff (pfile, 32 * sizeof (cpp_token *));
  816. else
  817. {
  818. next = *pragma_buff;
  819. *pragma_buff
  820. = _cpp_get_buff (pfile,
  821. (BUFF_FRONT (*pragma_buff)
  822. - (*pragma_buff)->base) * 2);
  823. (*pragma_buff)->next = next;
  824. }
  825. }
  826. *(const cpp_token **) BUFF_FRONT (*pragma_buff) = token;
  827. BUFF_FRONT (*pragma_buff) += sizeof (cpp_token *);
  828. if (token->type == CPP_PRAGMA_EOL)
  829. break;
  830. token = cpp_get_token_1 (pfile, &virt_loc);
  831. }
  832. while (token->type != CPP_EOF);
  833. /* In deferred pragmas parsing_args and prevent_expansion
  834. had been changed, reset it. */
  835. pfile->state.parsing_args = 2;
  836. pfile->state.prevent_expansion = 1;
  837. if (token->type == CPP_EOF)
  838. break;
  839. else
  840. continue;
  841. }
  842. set_arg_token (arg, token, virt_loc,
  843. ntokens, MACRO_ARG_TOKEN_NORMAL,
  844. CPP_OPTION (pfile, track_macro_expansion));
  845. ntokens++;
  846. }
  847. /* Drop trailing padding. */
  848. while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
  849. ntokens--;
  850. arg->count = ntokens;
  851. set_arg_token (arg, &pfile->eof, pfile->eof.src_loc,
  852. ntokens, MACRO_ARG_TOKEN_NORMAL,
  853. CPP_OPTION (pfile, track_macro_expansion));
  854. /* Terminate the argument. Excess arguments loop back and
  855. overwrite the final legitimate argument, before failing. */
  856. if (argc <= macro->paramc)
  857. {
  858. buff->cur = (unsigned char *) &arg->first[ntokens + 1];
  859. if (argc != macro->paramc)
  860. arg++;
  861. }
  862. }
  863. while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
  864. if (token->type == CPP_EOF)
  865. {
  866. /* We still need the CPP_EOF to end directives, and to end
  867. pre-expansion of a macro argument. Step back is not
  868. unconditional, since we don't want to return a CPP_EOF to our
  869. callers at the end of an -include-d file. */
  870. if (pfile->context->prev || pfile->state.in_directive)
  871. _cpp_backup_tokens (pfile, 1);
  872. cpp_error (pfile, CPP_DL_ERROR,
  873. "unterminated argument list invoking macro \"%s\"",
  874. NODE_NAME (node));
  875. }
  876. else
  877. {
  878. /* A single empty argument is counted as no argument. */
  879. if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
  880. argc = 0;
  881. if (_cpp_arguments_ok (pfile, macro, node, argc))
  882. {
  883. /* GCC has special semantics for , ## b where b is a varargs
  884. parameter: we remove the comma if b was omitted entirely.
  885. If b was merely an empty argument, the comma is retained.
  886. If the macro takes just one (varargs) parameter, then we
  887. retain the comma only if we are standards conforming.
  888. If FIRST is NULL replace_args () swallows the comma. */
  889. if (macro->variadic && (argc < macro->paramc
  890. || (argc == 1 && args[0].count == 0
  891. && !CPP_OPTION (pfile, std))))
  892. args[macro->paramc - 1].first = NULL;
  893. if (num_args)
  894. *num_args = num_args_alloced;
  895. return base_buff;
  896. }
  897. }
  898. /* An error occurred. */
  899. _cpp_release_buff (pfile, base_buff);
  900. return NULL;
  901. }
  902. /* Search for an opening parenthesis to the macro of NODE, in such a
  903. way that, if none is found, we don't lose the information in any
  904. intervening padding tokens. If we find the parenthesis, collect
  905. the arguments and return the buffer containing them. PRAGMA_BUFF
  906. argument is the same as in collect_args. If NUM_ARGS is non-NULL,
  907. *NUM_ARGS is set to the number of arguments contained in the
  908. returned buffer. */
  909. static _cpp_buff *
  910. funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node,
  911. _cpp_buff **pragma_buff, unsigned *num_args)
  912. {
  913. const cpp_token *token, *padding = NULL;
  914. for (;;)
  915. {
  916. token = cpp_get_token (pfile);
  917. if (token->type != CPP_PADDING)
  918. break;
  919. if (padding == NULL
  920. || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
  921. padding = token;
  922. }
  923. if (token->type == CPP_OPEN_PAREN)
  924. {
  925. pfile->state.parsing_args = 2;
  926. return collect_args (pfile, node, pragma_buff, num_args);
  927. }
  928. /* CPP_EOF can be the end of macro arguments, or the end of the
  929. file. We mustn't back up over the latter. Ugh. */
  930. if (token->type != CPP_EOF || token == &pfile->eof)
  931. {
  932. /* Back up. We may have skipped padding, in which case backing
  933. up more than one token when expanding macros is in general
  934. too difficult. We re-insert it in its own context. */
  935. _cpp_backup_tokens (pfile, 1);
  936. if (padding)
  937. _cpp_push_token_context (pfile, NULL, padding, 1);
  938. }
  939. return NULL;
  940. }
  941. /* Return the real number of tokens in the expansion of MACRO. */
  942. static inline unsigned int
  943. macro_real_token_count (const cpp_macro *macro)
  944. {
  945. unsigned int i;
  946. if (__builtin_expect (!macro->extra_tokens, true))
  947. return macro->count;
  948. for (i = 0; i < macro->count; i++)
  949. if (macro->exp.tokens[i].type == CPP_PASTE)
  950. return i;
  951. abort ();
  952. }
  953. /* Push the context of a macro with hash entry NODE onto the context
  954. stack. If we can successfully expand the macro, we push a context
  955. containing its yet-to-be-rescanned replacement list and return one.
  956. If there were additionally any unexpanded deferred #pragma
  957. directives among macro arguments, push another context containing
  958. the pragma tokens before the yet-to-be-rescanned replacement list
  959. and return two. Otherwise, we don't push a context and return
  960. zero. LOCATION is the location of the expansion point of the
  961. macro. */
  962. static int
  963. enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
  964. const cpp_token *result, source_location location)
  965. {
  966. /* The presence of a macro invalidates a file's controlling macro. */
  967. pfile->mi_valid = false;
  968. pfile->state.angled_headers = false;
  969. /* From here to when we push the context for the macro later down
  970. this function, we need to flag the fact that we are about to
  971. expand a macro. This is useful when -ftrack-macro-expansion is
  972. turned off. In that case, we need to record the location of the
  973. expansion point of the top-most macro we are about to to expand,
  974. into pfile->invocation_location. But we must not record any such
  975. location once the process of expanding the macro starts; that is,
  976. we must not do that recording between now and later down this
  977. function where set this flag to FALSE. */
  978. pfile->about_to_expand_macro_p = true;
  979. if ((node->flags & NODE_BUILTIN) && !(node->flags & NODE_USED))
  980. {
  981. node->flags |= NODE_USED;
  982. if ((!pfile->cb.user_builtin_macro
  983. || !pfile->cb.user_builtin_macro (pfile, node))
  984. && pfile->cb.used_define)
  985. pfile->cb.used_define (pfile, pfile->directive_line, node);
  986. }
  987. /* Handle standard macros. */
  988. if (! (node->flags & NODE_BUILTIN))
  989. {
  990. cpp_macro *macro = node->value.macro;
  991. _cpp_buff *pragma_buff = NULL;
  992. if (macro->fun_like)
  993. {
  994. _cpp_buff *buff;
  995. unsigned num_args = 0;
  996. pfile->state.prevent_expansion++;
  997. pfile->keep_tokens++;
  998. pfile->state.parsing_args = 1;
  999. buff = funlike_invocation_p (pfile, node, &pragma_buff,
  1000. &num_args);
  1001. pfile->state.parsing_args = 0;
  1002. pfile->keep_tokens--;
  1003. pfile->state.prevent_expansion--;
  1004. if (buff == NULL)
  1005. {
  1006. if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
  1007. cpp_warning (pfile, CPP_W_TRADITIONAL,
  1008. "function-like macro \"%s\" must be used with arguments in traditional C",
  1009. NODE_NAME (node));
  1010. if (pragma_buff)
  1011. _cpp_release_buff (pfile, pragma_buff);
  1012. pfile->about_to_expand_macro_p = false;
  1013. return 0;
  1014. }
  1015. if (macro->paramc > 0)
  1016. replace_args (pfile, node, macro,
  1017. (macro_arg *) buff->base,
  1018. location);
  1019. /* Free the memory used by the arguments of this
  1020. function-like macro. This memory has been allocated by
  1021. funlike_invocation_p and by replace_args. */
  1022. delete_macro_args (buff, num_args);
  1023. }
  1024. /* Disable the macro within its expansion. */
  1025. node->flags |= NODE_DISABLED;
  1026. if (!(node->flags & NODE_USED))
  1027. {
  1028. node->flags |= NODE_USED;
  1029. if (pfile->cb.used_define)
  1030. pfile->cb.used_define (pfile, pfile->directive_line, node);
  1031. }
  1032. if (pfile->cb.used)
  1033. pfile->cb.used (pfile, location, node);
  1034. macro->used = 1;
  1035. if (macro->paramc == 0)
  1036. {
  1037. unsigned tokens_count = macro_real_token_count (macro);
  1038. if (CPP_OPTION (pfile, track_macro_expansion))
  1039. {
  1040. unsigned int i;
  1041. const cpp_token *src = macro->exp.tokens;
  1042. const struct line_map *map;
  1043. source_location *virt_locs = NULL;
  1044. _cpp_buff *macro_tokens
  1045. = tokens_buff_new (pfile, tokens_count, &virt_locs);
  1046. /* Create a macro map to record the locations of the
  1047. tokens that are involved in the expansion. LOCATION
  1048. is the location of the macro expansion point. */
  1049. map = linemap_enter_macro (pfile->line_table,
  1050. node, location, tokens_count);
  1051. for (i = 0; i < tokens_count; ++i)
  1052. {
  1053. tokens_buff_add_token (macro_tokens, virt_locs,
  1054. src, src->src_loc,
  1055. src->src_loc, map, i);
  1056. ++src;
  1057. }
  1058. push_extended_tokens_context (pfile, node,
  1059. macro_tokens,
  1060. virt_locs,
  1061. (const cpp_token **)
  1062. macro_tokens->base,
  1063. tokens_count);
  1064. }
  1065. else
  1066. _cpp_push_token_context (pfile, node, macro->exp.tokens,
  1067. tokens_count);
  1068. num_macro_tokens_counter += tokens_count;
  1069. }
  1070. if (pragma_buff)
  1071. {
  1072. if (!pfile->state.in_directive)
  1073. _cpp_push_token_context (pfile, NULL,
  1074. padding_token (pfile, result), 1);
  1075. do
  1076. {
  1077. unsigned tokens_count;
  1078. _cpp_buff *tail = pragma_buff->next;
  1079. pragma_buff->next = NULL;
  1080. tokens_count = ((const cpp_token **) BUFF_FRONT (pragma_buff)
  1081. - (const cpp_token **) pragma_buff->base);
  1082. push_ptoken_context (pfile, NULL, pragma_buff,
  1083. (const cpp_token **) pragma_buff->base,
  1084. tokens_count);
  1085. pragma_buff = tail;
  1086. if (!CPP_OPTION (pfile, track_macro_expansion))
  1087. num_macro_tokens_counter += tokens_count;
  1088. }
  1089. while (pragma_buff != NULL);
  1090. pfile->about_to_expand_macro_p = false;
  1091. return 2;
  1092. }
  1093. pfile->about_to_expand_macro_p = false;
  1094. return 1;
  1095. }
  1096. pfile->about_to_expand_macro_p = false;
  1097. /* Handle built-in macros and the _Pragma operator. */
  1098. {
  1099. source_location loc;
  1100. if (/* The top-level macro invocation that triggered the expansion
  1101. we are looking at is with a standard macro ...*/
  1102. !(pfile->top_most_macro_node->flags & NODE_BUILTIN)
  1103. /* ... and it's a function-like macro invocation. */
  1104. && pfile->top_most_macro_node->value.macro->fun_like)
  1105. /* Then the location of the end of the macro invocation is the
  1106. location of the closing parenthesis. */
  1107. loc = pfile->cur_token[-1].src_loc;
  1108. else
  1109. /* Otherwise, the location of the end of the macro invocation is
  1110. the location of the expansion point of that top-level macro
  1111. invocation. */
  1112. loc = location;
  1113. return builtin_macro (pfile, node, loc);
  1114. }
  1115. }
  1116. /* De-allocate the memory used by BUFF which is an array of instances
  1117. of macro_arg. NUM_ARGS is the number of instances of macro_arg
  1118. present in BUFF. */
  1119. static void
  1120. delete_macro_args (_cpp_buff *buff, unsigned num_args)
  1121. {
  1122. macro_arg *macro_args;
  1123. unsigned i;
  1124. if (buff == NULL)
  1125. return;
  1126. macro_args = (macro_arg *) buff->base;
  1127. /* Walk instances of macro_arg to free their expanded tokens as well
  1128. as their macro_arg::virt_locs members. */
  1129. for (i = 0; i < num_args; ++i)
  1130. {
  1131. if (macro_args[i].expanded)
  1132. {
  1133. free (macro_args[i].expanded);
  1134. macro_args[i].expanded = NULL;
  1135. }
  1136. if (macro_args[i].virt_locs)
  1137. {
  1138. free (macro_args[i].virt_locs);
  1139. macro_args[i].virt_locs = NULL;
  1140. }
  1141. if (macro_args[i].expanded_virt_locs)
  1142. {
  1143. free (macro_args[i].expanded_virt_locs);
  1144. macro_args[i].expanded_virt_locs = NULL;
  1145. }
  1146. }
  1147. _cpp_free_buff (buff);
  1148. }
  1149. /* Set the INDEXth token of the macro argument ARG. TOKEN is the token
  1150. to set, LOCATION is its virtual location. "Virtual" location means
  1151. the location that encodes loci across macro expansion. Otherwise
  1152. it has to be TOKEN->SRC_LOC. KIND is the kind of tokens the
  1153. argument ARG is supposed to contain. Note that ARG must be
  1154. tailored so that it has enough room to contain INDEX + 1 numbers of
  1155. tokens, at least. */
  1156. static void
  1157. set_arg_token (macro_arg *arg, const cpp_token *token,
  1158. source_location location, size_t index,
  1159. enum macro_arg_token_kind kind,
  1160. bool track_macro_exp_p)
  1161. {
  1162. const cpp_token **token_ptr;
  1163. source_location *loc = NULL;
  1164. token_ptr =
  1165. arg_token_ptr_at (arg, index, kind,
  1166. track_macro_exp_p ? &loc : NULL);
  1167. *token_ptr = token;
  1168. if (loc != NULL)
  1169. {
  1170. #ifdef ENABLE_CHECKING
  1171. if (kind == MACRO_ARG_TOKEN_STRINGIFIED
  1172. || !track_macro_exp_p)
  1173. /* We can't set the location of a stringified argument
  1174. token and we can't set any location if we aren't tracking
  1175. macro expansion locations. */
  1176. abort ();
  1177. #endif
  1178. *loc = location;
  1179. }
  1180. }
  1181. /* Get the pointer to the location of the argument token of the
  1182. function-like macro argument ARG. This function must be called
  1183. only when we -ftrack-macro-expansion is on. */
  1184. static const source_location *
  1185. get_arg_token_location (const macro_arg *arg,
  1186. enum macro_arg_token_kind kind)
  1187. {
  1188. const source_location *loc = NULL;
  1189. const cpp_token **token_ptr =
  1190. arg_token_ptr_at (arg, 0, kind, (source_location **) &loc);
  1191. if (token_ptr == NULL)
  1192. return NULL;
  1193. return loc;
  1194. }
  1195. /* Return the pointer to the INDEXth token of the macro argument ARG.
  1196. KIND specifies the kind of token the macro argument ARG contains.
  1197. If VIRT_LOCATION is non NULL, *VIRT_LOCATION is set to the address
  1198. of the virtual location of the returned token if the
  1199. -ftrack-macro-expansion flag is on; otherwise, it's set to the
  1200. spelling location of the returned token. */
  1201. static const cpp_token **
  1202. arg_token_ptr_at (const macro_arg *arg, size_t index,
  1203. enum macro_arg_token_kind kind,
  1204. source_location **virt_location)
  1205. {
  1206. const cpp_token **tokens_ptr = NULL;
  1207. switch (kind)
  1208. {
  1209. case MACRO_ARG_TOKEN_NORMAL:
  1210. tokens_ptr = arg->first;
  1211. break;
  1212. case MACRO_ARG_TOKEN_STRINGIFIED:
  1213. tokens_ptr = (const cpp_token **) &arg->stringified;
  1214. break;
  1215. case MACRO_ARG_TOKEN_EXPANDED:
  1216. tokens_ptr = arg->expanded;
  1217. break;
  1218. }
  1219. if (tokens_ptr == NULL)
  1220. /* This can happen for e.g, an empty token argument to a
  1221. funtion-like macro. */
  1222. return tokens_ptr;
  1223. if (virt_location)
  1224. {
  1225. if (kind == MACRO_ARG_TOKEN_NORMAL)
  1226. *virt_location = &arg->virt_locs[index];
  1227. else if (kind == MACRO_ARG_TOKEN_EXPANDED)
  1228. *virt_location = &arg->expanded_virt_locs[index];
  1229. else if (kind == MACRO_ARG_TOKEN_STRINGIFIED)
  1230. *virt_location =
  1231. (source_location *) &tokens_ptr[index]->src_loc;
  1232. }
  1233. return &tokens_ptr[index];
  1234. }
  1235. /* Initialize an iterator so that it iterates over the tokens of a
  1236. function-like macro argument. KIND is the kind of tokens we want
  1237. ITER to iterate over. TOKEN_PTR points the first token ITER will
  1238. iterate over. */
  1239. static void
  1240. macro_arg_token_iter_init (macro_arg_token_iter *iter,
  1241. bool track_macro_exp_p,
  1242. enum macro_arg_token_kind kind,
  1243. const macro_arg *arg,
  1244. const cpp_token **token_ptr)
  1245. {
  1246. iter->track_macro_exp_p = track_macro_exp_p;
  1247. iter->kind = kind;
  1248. iter->token_ptr = token_ptr;
  1249. /* Unconditionally initialize this so that the compiler doesn't warn
  1250. about iter->location_ptr being possibly uninitialized later after
  1251. this code has been inlined somewhere. */
  1252. iter->location_ptr = NULL;
  1253. if (track_macro_exp_p)
  1254. iter->location_ptr = get_arg_token_location (arg, kind);
  1255. #ifdef ENABLE_CHECKING
  1256. iter->num_forwards = 0;
  1257. if (track_macro_exp_p
  1258. && token_ptr != NULL
  1259. && iter->location_ptr == NULL)
  1260. abort ();
  1261. #endif
  1262. }
  1263. /* Move the iterator one token forward. Note that if IT was
  1264. initialized on an argument that has a stringified token, moving it
  1265. forward doesn't make sense as a stringified token is essentially one
  1266. string. */
  1267. static void
  1268. macro_arg_token_iter_forward (macro_arg_token_iter *it)
  1269. {
  1270. switch (it->kind)
  1271. {
  1272. case MACRO_ARG_TOKEN_NORMAL:
  1273. case MACRO_ARG_TOKEN_EXPANDED:
  1274. it->token_ptr++;
  1275. if (it->track_macro_exp_p)
  1276. it->location_ptr++;
  1277. break;
  1278. case MACRO_ARG_TOKEN_STRINGIFIED:
  1279. #ifdef ENABLE_CHECKING
  1280. if (it->num_forwards > 0)
  1281. abort ();
  1282. #endif
  1283. break;
  1284. }
  1285. #ifdef ENABLE_CHECKING
  1286. it->num_forwards++;
  1287. #endif
  1288. }
  1289. /* Return the token pointed to by the iterator. */
  1290. static const cpp_token *
  1291. macro_arg_token_iter_get_token (const macro_arg_token_iter *it)
  1292. {
  1293. #ifdef ENABLE_CHECKING
  1294. if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
  1295. && it->num_forwards > 0)
  1296. abort ();
  1297. #endif
  1298. if (it->token_ptr == NULL)
  1299. return NULL;
  1300. return *it->token_ptr;
  1301. }
  1302. /* Return the location of the token pointed to by the iterator.*/
  1303. static source_location
  1304. macro_arg_token_iter_get_location (const macro_arg_token_iter *it)
  1305. {
  1306. #ifdef ENABLE_CHECKING
  1307. if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
  1308. && it->num_forwards > 0)
  1309. abort ();
  1310. #endif
  1311. if (it->track_macro_exp_p)
  1312. return *it->location_ptr;
  1313. else
  1314. return (*it->token_ptr)->src_loc;
  1315. }
  1316. /* Return the index of a token [resulting from macro expansion] inside
  1317. the total list of tokens resulting from a given macro
  1318. expansion. The index can be different depending on whether if we
  1319. want each tokens resulting from function-like macro arguments
  1320. expansion to have a different location or not.
  1321. E.g, consider this function-like macro:
  1322. #define M(x) x - 3
  1323. Then consider us "calling" it (and thus expanding it) like:
  1324. M(1+4)
  1325. It will be expanded into:
  1326. 1+4-3
  1327. Let's consider the case of the token '4'.
  1328. Its index can be 2 (it's the third token of the set of tokens
  1329. resulting from the expansion) or it can be 0 if we consider that
  1330. all tokens resulting from the expansion of the argument "1+2" have
  1331. the same index, which is 0. In this later case, the index of token
  1332. '-' would then be 1 and the index of token '3' would be 2.
  1333. The later case is useful to use less memory e.g, for the case of
  1334. the user using the option -ftrack-macro-expansion=1.
  1335. ABSOLUTE_TOKEN_INDEX is the index of the macro argument token we
  1336. are interested in. CUR_REPLACEMENT_TOKEN is the token of the macro
  1337. parameter (inside the macro replacement list) that corresponds to
  1338. the macro argument for which ABSOLUTE_TOKEN_INDEX is a token index
  1339. of.
  1340. If we refer to the example above, for the '4' argument token,
  1341. ABSOLUTE_TOKEN_INDEX would be set to 2, and CUR_REPLACEMENT_TOKEN
  1342. would be set to the token 'x', in the replacement list "x - 3" of
  1343. macro M.
  1344. This is a subroutine of replace_args. */
  1345. inline static unsigned
  1346. expanded_token_index (cpp_reader *pfile, cpp_macro *macro,
  1347. const cpp_token *cur_replacement_token,
  1348. unsigned absolute_token_index)
  1349. {
  1350. if (CPP_OPTION (pfile, track_macro_expansion) > 1)
  1351. return absolute_token_index;
  1352. return cur_replacement_token - macro->exp.tokens;
  1353. }
  1354. /* Replace the parameters in a function-like macro of NODE with the
  1355. actual ARGS, and place the result in a newly pushed token context.
  1356. Expand each argument before replacing, unless it is operated upon
  1357. by the # or ## operators. EXPANSION_POINT_LOC is the location of
  1358. the expansion point of the macro. E.g, the location of the
  1359. function-like macro invocation. */
  1360. static void
  1361. replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
  1362. macro_arg *args, source_location expansion_point_loc)
  1363. {
  1364. unsigned int i, total;
  1365. const cpp_token *src, *limit;
  1366. const cpp_token **first = NULL;
  1367. macro_arg *arg;
  1368. _cpp_buff *buff = NULL;
  1369. source_location *virt_locs = NULL;
  1370. unsigned int exp_count;
  1371. const struct line_map *map = NULL;
  1372. int track_macro_exp;
  1373. /* First, fully macro-expand arguments, calculating the number of
  1374. tokens in the final expansion as we go. The ordering of the if
  1375. statements below is subtle; we must handle stringification before
  1376. pasting. */
  1377. /* EXP_COUNT is the number of tokens in the macro replacement
  1378. list. TOTAL is the number of tokens /after/ macro parameters
  1379. have been replaced by their arguments. */
  1380. exp_count = macro_real_token_count (macro);
  1381. total = exp_count;
  1382. limit = macro->exp.tokens + exp_count;
  1383. for (src = macro->exp.tokens; src < limit; src++)
  1384. if (src->type == CPP_MACRO_ARG)
  1385. {
  1386. /* Leading and trailing padding tokens. */
  1387. total += 2;
  1388. /* Account for leading and padding tokens in exp_count too.
  1389. This is going to be important later down this function,
  1390. when we want to handle the case of (track_macro_exp <
  1391. 2). */
  1392. exp_count += 2;
  1393. /* We have an argument. If it is not being stringified or
  1394. pasted it is macro-replaced before insertion. */
  1395. arg = &args[src->val.macro_arg.arg_no - 1];
  1396. if (src->flags & STRINGIFY_ARG)
  1397. {
  1398. if (!arg->stringified)
  1399. arg->stringified = stringify_arg (pfile, arg);
  1400. }
  1401. else if ((src->flags & PASTE_LEFT)
  1402. || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
  1403. total += arg->count - 1;
  1404. else
  1405. {
  1406. if (!arg->expanded)
  1407. expand_arg (pfile, arg);
  1408. total += arg->expanded_count - 1;
  1409. }
  1410. }
  1411. /* When the compiler is called with the -ftrack-macro-expansion
  1412. flag, we need to keep track of the location of each token that
  1413. results from macro expansion.
  1414. A token resulting from macro expansion is not a new token. It is
  1415. simply the same token as the token coming from the macro
  1416. definition. The new things that are allocated are the buffer
  1417. that holds the tokens resulting from macro expansion and a new
  1418. location that records many things like the locus of the expansion
  1419. point as well as the original locus inside the definition of the
  1420. macro. This location is called a virtual location.
  1421. So the buffer BUFF holds a set of cpp_token*, and the buffer
  1422. VIRT_LOCS holds the virtual locations of the tokens held by BUFF.
  1423. Both of these two buffers are going to be hung off of the macro
  1424. context, when the latter is pushed. The memory allocated to
  1425. store the tokens and their locations is going to be freed once
  1426. the context of macro expansion is popped.
  1427. As far as tokens are concerned, the memory overhead of
  1428. -ftrack-macro-expansion is proportional to the number of
  1429. macros that get expanded multiplied by sizeof (source_location).
  1430. The good news is that extra memory gets freed when the macro
  1431. context is freed, i.e shortly after the macro got expanded. */
  1432. /* Is the -ftrack-macro-expansion flag in effect? */
  1433. track_macro_exp = CPP_OPTION (pfile, track_macro_expansion);
  1434. /* Now allocate memory space for tokens and locations resulting from
  1435. the macro expansion, copy the tokens and replace the arguments.
  1436. This memory must be freed when the context of the macro MACRO is
  1437. popped. */
  1438. buff = tokens_buff_new (pfile, total, track_macro_exp ? &virt_locs : NULL);
  1439. first = (const cpp_token **) buff->base;
  1440. /* Create a macro map to record the locations of the tokens that are
  1441. involved in the expansion. Note that the expansion point is set
  1442. to the location of the closing parenthesis. Otherwise, the
  1443. subsequent map created for the first token that comes after the
  1444. macro map might have a wrong line number. That would lead to
  1445. tokens with wrong line numbers after the macro expansion. This
  1446. adds up to the memory overhead of the -ftrack-macro-expansion
  1447. flag; for every macro that is expanded, a "macro map" is
  1448. created. */
  1449. if (track_macro_exp)
  1450. {
  1451. int num_macro_tokens = total;
  1452. if (track_macro_exp < 2)
  1453. /* Then the number of macro tokens won't take in account the
  1454. fact that function-like macro arguments can expand to
  1455. multiple tokens. This is to save memory at the expense of
  1456. accuracy.
  1457. Suppose we have #define SQARE(A) A * A
  1458. And then we do SQARE(2+3)
  1459. Then the tokens 2, +, 3, will have the same location,
  1460. saying they come from the expansion of the argument A. */
  1461. num_macro_tokens = exp_count;
  1462. map = linemap_enter_macro (pfile->line_table, node,
  1463. expansion_point_loc,
  1464. num_macro_tokens);
  1465. }
  1466. i = 0;
  1467. for (src = macro->exp.tokens; src < limit; src++)
  1468. {
  1469. unsigned int arg_tokens_count;
  1470. macro_arg_token_iter from;
  1471. const cpp_token **paste_flag = NULL;
  1472. const cpp_token **tmp_token_ptr;
  1473. if (src->type != CPP_MACRO_ARG)
  1474. {
  1475. /* Allocate a virtual location for token SRC, and add that
  1476. token and its virtual location into the buffers BUFF and
  1477. VIRT_LOCS. */
  1478. unsigned index = expanded_token_index (pfile, macro, src, i);
  1479. tokens_buff_add_token (buff, virt_locs, src,
  1480. src->src_loc, src->src_loc,
  1481. map, index);
  1482. i += 1;
  1483. continue;
  1484. }
  1485. paste_flag = 0;
  1486. arg = &args[src->val.macro_arg.arg_no - 1];
  1487. /* SRC is a macro parameter that we need to replace with its
  1488. corresponding argument. So at some point we'll need to
  1489. iterate over the tokens of the macro argument and copy them
  1490. into the "place" now holding the correspondig macro
  1491. parameter. We are going to use the iterator type
  1492. macro_argo_token_iter to handle that iterating. The 'if'
  1493. below is to initialize the iterator depending on the type of
  1494. tokens the macro argument has. It also does some adjustment
  1495. related to padding tokens and some pasting corner cases. */
  1496. if (src->flags & STRINGIFY_ARG)
  1497. {
  1498. arg_tokens_count = 1;
  1499. macro_arg_token_iter_init (&from,
  1500. CPP_OPTION (pfile,
  1501. track_macro_expansion),
  1502. MACRO_ARG_TOKEN_STRINGIFIED,
  1503. arg, &arg->stringified);
  1504. }
  1505. else if (src->flags & PASTE_LEFT)
  1506. {
  1507. arg_tokens_count = arg->count;
  1508. macro_arg_token_iter_init (&from,
  1509. CPP_OPTION (pfile,
  1510. track_macro_expansion),
  1511. MACRO_ARG_TOKEN_NORMAL,
  1512. arg, arg->first);
  1513. }
  1514. else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
  1515. {
  1516. int num_toks;
  1517. arg_tokens_count = arg->count;
  1518. macro_arg_token_iter_init (&from,
  1519. CPP_OPTION (pfile,
  1520. track_macro_expansion),
  1521. MACRO_ARG_TOKEN_NORMAL,
  1522. arg, arg->first);
  1523. num_toks = tokens_buff_count (buff);
  1524. if (num_toks != 0)
  1525. {
  1526. /* So the current parameter token is pasted to the previous
  1527. token in the replacement list. Let's look at what
  1528. we have as previous and current arguments. */
  1529. /* This is the previous argument's token ... */
  1530. tmp_token_ptr = tokens_buff_last_token_ptr (buff);
  1531. if ((*tmp_token_ptr)->type == CPP_COMMA
  1532. && macro->variadic
  1533. && src->val.macro_arg.arg_no == macro->paramc)
  1534. {
  1535. /* ... which is a comma; and the current parameter
  1536. is the last parameter of a variadic function-like
  1537. macro. If the argument to the current last
  1538. parameter is NULL, then swallow the comma,
  1539. otherwise drop the paste flag. */
  1540. if (macro_arg_token_iter_get_token (&from) == NULL)
  1541. tokens_buff_remove_last_token (buff);
  1542. else
  1543. paste_flag = tmp_token_ptr;
  1544. }
  1545. /* Remove the paste flag if the RHS is a placemarker. */
  1546. else if (arg_tokens_count == 0)
  1547. paste_flag = tmp_token_ptr;
  1548. }
  1549. }
  1550. else
  1551. {
  1552. arg_tokens_count = arg->expanded_count;
  1553. macro_arg_token_iter_init (&from,
  1554. CPP_OPTION (pfile,
  1555. track_macro_expansion),
  1556. MACRO_ARG_TOKEN_EXPANDED,
  1557. arg, arg->expanded);
  1558. }
  1559. /* Padding on the left of an argument (unless RHS of ##). */
  1560. if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
  1561. && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
  1562. {
  1563. const cpp_token *t = padding_token (pfile, src);
  1564. unsigned index = expanded_token_index (pfile, macro, src, i);
  1565. /* Allocate a virtual location for the padding token and
  1566. append the token and its location to BUFF and
  1567. VIRT_LOCS. */
  1568. tokens_buff_add_token (buff, virt_locs, t,
  1569. t->src_loc, t->src_loc,
  1570. map, index);
  1571. }
  1572. if (arg_tokens_count)
  1573. {
  1574. /* So now we've got the number of tokens that make up the
  1575. argument that is going to replace the current parameter
  1576. in the macro's replacement list. */
  1577. unsigned int j;
  1578. for (j = 0; j < arg_tokens_count; ++j)
  1579. {
  1580. /* So if track_macro_exp is < 2, the user wants to
  1581. save extra memory while tracking macro expansion
  1582. locations. So in that case here is what we do:
  1583. Suppose we have #define SQARE(A) A * A
  1584. And then we do SQARE(2+3)
  1585. Then the tokens 2, +, 3, will have the same location,
  1586. saying they come from the expansion of the argument
  1587. A.
  1588. So that means we are going to ignore the COUNT tokens
  1589. resulting from the expansion of the current macro
  1590. arugment. In other words all the ARG_TOKENS_COUNT tokens
  1591. resulting from the expansion of the macro argument will
  1592. have the index I. Normally, each of those token should
  1593. have index I+J. */
  1594. unsigned token_index = i;
  1595. unsigned index;
  1596. if (track_macro_exp > 1)
  1597. token_index += j;
  1598. index = expanded_token_index (pfile, macro, src, token_index);
  1599. tokens_buff_add_token (buff, virt_locs,
  1600. macro_arg_token_iter_get_token (&from),
  1601. macro_arg_token_iter_get_location (&from),
  1602. src->src_loc, map, index);
  1603. macro_arg_token_iter_forward (&from);
  1604. }
  1605. /* With a non-empty argument on the LHS of ##, the last
  1606. token should be flagged PASTE_LEFT. */
  1607. if (src->flags & PASTE_LEFT)
  1608. paste_flag =
  1609. (const cpp_token **) tokens_buff_last_token_ptr (buff);
  1610. }
  1611. else if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99)
  1612. && ! macro->syshdr && ! cpp_in_system_header (pfile))
  1613. {
  1614. if (CPP_OPTION (pfile, cplusplus))
  1615. cpp_pedwarning (pfile, CPP_W_PEDANTIC,
  1616. "invoking macro %s argument %d: "
  1617. "empty macro arguments are undefined"
  1618. " in ISO C++98",
  1619. NODE_NAME (node), src->val.macro_arg.arg_no);
  1620. else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat))
  1621. cpp_pedwarning (pfile,
  1622. CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
  1623. ? CPP_W_C90_C99_COMPAT : CPP_W_PEDANTIC,
  1624. "invoking macro %s argument %d: "
  1625. "empty macro arguments are undefined"
  1626. " in ISO C90",
  1627. NODE_NAME (node), src->val.macro_arg.arg_no);
  1628. }
  1629. else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
  1630. && ! CPP_OPTION (pfile, cplusplus)
  1631. && ! macro->syshdr && ! cpp_in_system_header (pfile))
  1632. cpp_warning (pfile, CPP_W_C90_C99_COMPAT,
  1633. "invoking macro %s argument %d: "
  1634. "empty macro arguments are undefined"
  1635. " in ISO C90",
  1636. NODE_NAME (node), src->val.macro_arg.arg_no);
  1637. /* Avoid paste on RHS (even case count == 0). */
  1638. if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
  1639. {
  1640. const cpp_token *t = &pfile->avoid_paste;
  1641. tokens_buff_add_token (buff, virt_locs,
  1642. t, t->src_loc, t->src_loc,
  1643. NULL, 0);
  1644. }
  1645. /* Add a new paste flag, or remove an unwanted one. */
  1646. if (paste_flag)
  1647. {
  1648. cpp_token *token = _cpp_temp_token (pfile);
  1649. token->type = (*paste_flag)->type;
  1650. token->val = (*paste_flag)->val;
  1651. if (src->flags & PASTE_LEFT)
  1652. token->flags = (*paste_flag)->flags | PASTE_LEFT;
  1653. else
  1654. token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
  1655. *paste_flag = token;
  1656. }
  1657. i += arg_tokens_count;
  1658. }
  1659. if (track_macro_exp)
  1660. push_extended_tokens_context (pfile, node, buff, virt_locs, first,
  1661. tokens_buff_count (buff));
  1662. else
  1663. push_ptoken_context (pfile, node, buff, first,
  1664. tokens_buff_count (buff));
  1665. num_macro_tokens_counter += tokens_buff_count (buff);
  1666. }
  1667. /* Return a special padding token, with padding inherited from SOURCE. */
  1668. static const cpp_token *
  1669. padding_token (cpp_reader *pfile, const cpp_token *source)
  1670. {
  1671. cpp_token *result = _cpp_temp_token (pfile);
  1672. result->type = CPP_PADDING;
  1673. /* Data in GCed data structures cannot be made const so far, so we
  1674. need a cast here. */
  1675. result->val.source = (cpp_token *) source;
  1676. result->flags = 0;
  1677. return result;
  1678. }
  1679. /* Get a new uninitialized context. Create a new one if we cannot
  1680. re-use an old one. */
  1681. static cpp_context *
  1682. next_context (cpp_reader *pfile)
  1683. {
  1684. cpp_context *result = pfile->context->next;
  1685. if (result == 0)
  1686. {
  1687. result = XNEW (cpp_context);
  1688. memset (result, 0, sizeof (cpp_context));
  1689. result->prev = pfile->context;
  1690. result->next = 0;
  1691. pfile->context->next = result;
  1692. }
  1693. pfile->context = result;
  1694. return result;
  1695. }
  1696. /* Push a list of pointers to tokens. */
  1697. static void
  1698. push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
  1699. const cpp_token **first, unsigned int count)
  1700. {
  1701. cpp_context *context = next_context (pfile);
  1702. context->tokens_kind = TOKENS_KIND_INDIRECT;
  1703. context->c.macro = macro;
  1704. context->buff = buff;
  1705. FIRST (context).ptoken = first;
  1706. LAST (context).ptoken = first + count;
  1707. }
  1708. /* Push a list of tokens.
  1709. A NULL macro means that we should continue the current macro
  1710. expansion, in essence. That means that if we are currently in a
  1711. macro expansion context, we'll make the new pfile->context refer to
  1712. the current macro. */
  1713. void
  1714. _cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
  1715. const cpp_token *first, unsigned int count)
  1716. {
  1717. cpp_context *context;
  1718. if (macro == NULL)
  1719. macro = macro_of_context (pfile->context);
  1720. context = next_context (pfile);
  1721. context->tokens_kind = TOKENS_KIND_DIRECT;
  1722. context->c.macro = macro;
  1723. context->buff = NULL;
  1724. FIRST (context).token = first;
  1725. LAST (context).token = first + count;
  1726. }
  1727. /* Build a context containing a list of tokens as well as their
  1728. virtual locations and push it. TOKENS_BUFF is the buffer that
  1729. contains the tokens pointed to by FIRST. If TOKENS_BUFF is
  1730. non-NULL, it means that the context owns it, meaning that
  1731. _cpp_pop_context will free it as well as VIRT_LOCS_BUFF that
  1732. contains the virtual locations.
  1733. A NULL macro means that we should continue the current macro
  1734. expansion, in essence. That means that if we are currently in a
  1735. macro expansion context, we'll make the new pfile->context refer to
  1736. the current macro. */
  1737. static void
  1738. push_extended_tokens_context (cpp_reader *pfile,
  1739. cpp_hashnode *macro,
  1740. _cpp_buff *token_buff,
  1741. source_location *virt_locs,
  1742. const cpp_token **first,
  1743. unsigned int count)
  1744. {
  1745. cpp_context *context;
  1746. macro_context *m;
  1747. if (macro == NULL)
  1748. macro = macro_of_context (pfile->context);
  1749. context = next_context (pfile);
  1750. context->tokens_kind = TOKENS_KIND_EXTENDED;
  1751. context->buff = token_buff;
  1752. m = XNEW (macro_context);
  1753. m->macro_node = macro;
  1754. m->virt_locs = virt_locs;
  1755. m->cur_virt_loc = virt_locs;
  1756. context->c.mc = m;
  1757. FIRST (context).ptoken = first;
  1758. LAST (context).ptoken = first + count;
  1759. }
  1760. /* Push a traditional macro's replacement text. */
  1761. void
  1762. _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
  1763. const uchar *start, size_t len)
  1764. {
  1765. cpp_context *context = next_context (pfile);
  1766. context->tokens_kind = TOKENS_KIND_DIRECT;
  1767. context->c.macro = macro;
  1768. context->buff = NULL;
  1769. CUR (context) = start;
  1770. RLIMIT (context) = start + len;
  1771. macro->flags |= NODE_DISABLED;
  1772. }
  1773. /* Creates a buffer that holds tokens a.k.a "token buffer", usually
  1774. for the purpose of storing them on a cpp_context. If VIRT_LOCS is
  1775. non-null (which means that -ftrack-macro-expansion is on),
  1776. *VIRT_LOCS is set to a newly allocated buffer that is supposed to
  1777. hold the virtual locations of the tokens resulting from macro
  1778. expansion. */
  1779. static _cpp_buff*
  1780. tokens_buff_new (cpp_reader *pfile, size_t len,
  1781. source_location **virt_locs)
  1782. {
  1783. size_t tokens_size = len * sizeof (cpp_token *);
  1784. size_t locs_size = len * sizeof (source_location);
  1785. if (virt_locs != NULL)
  1786. *virt_locs = XNEWVEC (source_location, locs_size);
  1787. return _cpp_get_buff (pfile, tokens_size);
  1788. }
  1789. /* Returns the number of tokens contained in a token buffer. The
  1790. buffer holds a set of cpp_token*. */
  1791. static size_t
  1792. tokens_buff_count (_cpp_buff *buff)
  1793. {
  1794. return (BUFF_FRONT (buff) - buff->base) / sizeof (cpp_token *);
  1795. }
  1796. /* Return a pointer to the last token contained in the token buffer
  1797. BUFF. */
  1798. static const cpp_token **
  1799. tokens_buff_last_token_ptr (_cpp_buff *buff)
  1800. {
  1801. return &((const cpp_token **) BUFF_FRONT (buff))[-1];
  1802. }
  1803. /* Remove the last token contained in the token buffer TOKENS_BUFF.
  1804. If VIRT_LOCS_BUFF is non-NULL, it should point at the buffer
  1805. containing the virtual locations of the tokens in TOKENS_BUFF; in
  1806. which case the function updates that buffer as well. */
  1807. static inline void
  1808. tokens_buff_remove_last_token (_cpp_buff *tokens_buff)
  1809. {
  1810. if (BUFF_FRONT (tokens_buff) > tokens_buff->base)
  1811. BUFF_FRONT (tokens_buff) =
  1812. (unsigned char *) &((cpp_token **) BUFF_FRONT (tokens_buff))[-1];
  1813. }
  1814. /* Insert a token into the token buffer at the position pointed to by
  1815. DEST. Note that the buffer is not enlarged so the previous token
  1816. that was at *DEST is overwritten. VIRT_LOC_DEST, if non-null,
  1817. means -ftrack-macro-expansion is effect; it then points to where to
  1818. insert the virtual location of TOKEN. TOKEN is the token to
  1819. insert. VIRT_LOC is the virtual location of the token, i.e, the
  1820. location possibly encoding its locus across macro expansion. If
  1821. TOKEN is an argument of a function-like macro (inside a macro
  1822. replacement list), PARM_DEF_LOC is the spelling location of the
  1823. macro parameter that TOKEN is replacing, in the replacement list of
  1824. the macro. If TOKEN is not an argument of a function-like macro or
  1825. if it doesn't come from a macro expansion, then VIRT_LOC can just
  1826. be set to the same value as PARM_DEF_LOC. If MAP is non null, it
  1827. means TOKEN comes from a macro expansion and MAP is the macro map
  1828. associated to the macro. MACRO_TOKEN_INDEX points to the index of
  1829. the token in the macro map; it is not considered if MAP is NULL.
  1830. Upon successful completion this function returns the a pointer to
  1831. the position of the token coming right after the insertion
  1832. point. */
  1833. static inline const cpp_token **
  1834. tokens_buff_put_token_to (const cpp_token **dest,
  1835. source_location *virt_loc_dest,
  1836. const cpp_token *token,
  1837. source_location virt_loc,
  1838. source_location parm_def_loc,
  1839. const struct line_map *map,
  1840. unsigned int macro_token_index)
  1841. {
  1842. source_location macro_loc = virt_loc;
  1843. const cpp_token **result;
  1844. if (virt_loc_dest)
  1845. {
  1846. /* -ftrack-macro-expansion is on. */
  1847. if (map)
  1848. macro_loc = linemap_add_macro_token (map, macro_token_index,
  1849. virt_loc, parm_def_loc);
  1850. *virt_loc_dest = macro_loc;
  1851. }
  1852. *dest = token;
  1853. result = &dest[1];
  1854. return result;
  1855. }
  1856. /* Adds a token at the end of the tokens contained in BUFFER. Note
  1857. that this function doesn't enlarge BUFFER when the number of tokens
  1858. reaches BUFFER's size; it aborts in that situation.
  1859. TOKEN is the token to append. VIRT_LOC is the virtual location of
  1860. the token, i.e, the location possibly encoding its locus across
  1861. macro expansion. If TOKEN is an argument of a function-like macro
  1862. (inside a macro replacement list), PARM_DEF_LOC is the location of
  1863. the macro parameter that TOKEN is replacing. If TOKEN doesn't come
  1864. from a macro expansion, then VIRT_LOC can just be set to the same
  1865. value as PARM_DEF_LOC. If MAP is non null, it means TOKEN comes
  1866. from a macro expansion and MAP is the macro map associated to the
  1867. macro. MACRO_TOKEN_INDEX points to the index of the token in the
  1868. macro map; It is not considered if MAP is NULL. If VIRT_LOCS is
  1869. non-null, it means -ftrack-macro-expansion is on; in which case
  1870. this function adds the virtual location DEF_LOC to the VIRT_LOCS
  1871. array, at the same index as the one of TOKEN in BUFFER. Upon
  1872. successful completion this function returns the a pointer to the
  1873. position of the token coming right after the insertion point. */
  1874. static const cpp_token **
  1875. tokens_buff_add_token (_cpp_buff *buffer,
  1876. source_location *virt_locs,
  1877. const cpp_token *token,
  1878. source_location virt_loc,
  1879. source_location parm_def_loc,
  1880. const struct line_map *map,
  1881. unsigned int macro_token_index)
  1882. {
  1883. const cpp_token **result;
  1884. source_location *virt_loc_dest = NULL;
  1885. unsigned token_index =
  1886. (BUFF_FRONT (buffer) - buffer->base) / sizeof (cpp_token *);
  1887. /* Abort if we pass the end the buffer. */
  1888. if (BUFF_FRONT (buffer) > BUFF_LIMIT (buffer))
  1889. abort ();
  1890. if (virt_locs != NULL)
  1891. virt_loc_dest = &virt_locs[token_index];
  1892. result =
  1893. tokens_buff_put_token_to ((const cpp_token **) BUFF_FRONT (buffer),
  1894. virt_loc_dest, token, virt_loc, parm_def_loc,
  1895. map, macro_token_index);
  1896. BUFF_FRONT (buffer) = (unsigned char *) result;
  1897. return result;
  1898. }
  1899. /* Allocate space for the function-like macro argument ARG to store
  1900. the tokens resulting from the macro-expansion of the tokens that
  1901. make up ARG itself. That space is allocated in ARG->expanded and
  1902. needs to be freed using free. */
  1903. static void
  1904. alloc_expanded_arg_mem (cpp_reader *pfile, macro_arg *arg, size_t capacity)
  1905. {
  1906. #ifdef ENABLE_CHECKING
  1907. if (arg->expanded != NULL
  1908. || arg->expanded_virt_locs != NULL)
  1909. abort ();
  1910. #endif
  1911. arg->expanded = XNEWVEC (const cpp_token *, capacity);
  1912. if (CPP_OPTION (pfile, track_macro_expansion))
  1913. arg->expanded_virt_locs = XNEWVEC (source_location, capacity);
  1914. }
  1915. /* If necessary, enlarge ARG->expanded to so that it can contain SIZE
  1916. tokens. */
  1917. static void
  1918. ensure_expanded_arg_room (cpp_reader *pfile, macro_arg *arg,
  1919. size_t size, size_t *expanded_capacity)
  1920. {
  1921. if (size <= *expanded_capacity)
  1922. return;
  1923. size *= 2;
  1924. arg->expanded =
  1925. XRESIZEVEC (const cpp_token *, arg->expanded, size);
  1926. *expanded_capacity = size;
  1927. if (CPP_OPTION (pfile, track_macro_expansion))
  1928. {
  1929. if (arg->expanded_virt_locs == NULL)
  1930. arg->expanded_virt_locs = XNEWVEC (source_location, size);
  1931. else
  1932. arg->expanded_virt_locs = XRESIZEVEC (source_location,
  1933. arg->expanded_virt_locs,
  1934. size);
  1935. }
  1936. }
  1937. /* Expand an argument ARG before replacing parameters in a
  1938. function-like macro. This works by pushing a context with the
  1939. argument's tokens, and then expanding that into a temporary buffer
  1940. as if it were a normal part of the token stream. collect_args()
  1941. has terminated the argument's tokens with a CPP_EOF so that we know
  1942. when we have fully expanded the argument. */
  1943. static void
  1944. expand_arg (cpp_reader *pfile, macro_arg *arg)
  1945. {
  1946. size_t capacity;
  1947. bool saved_warn_trad;
  1948. bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
  1949. if (arg->count == 0
  1950. || arg->expanded != NULL)
  1951. return;
  1952. /* Don't warn about funlike macros when pre-expanding. */
  1953. saved_warn_trad = CPP_WTRADITIONAL (pfile);
  1954. CPP_WTRADITIONAL (pfile) = 0;
  1955. /* Loop, reading in the tokens of the argument. */
  1956. capacity = 256;
  1957. alloc_expanded_arg_mem (pfile, arg, capacity);
  1958. if (track_macro_exp_p)
  1959. push_extended_tokens_context (pfile, NULL, NULL,
  1960. arg->virt_locs,
  1961. arg->first,
  1962. arg->count + 1);
  1963. else
  1964. push_ptoken_context (pfile, NULL, NULL,
  1965. arg->first, arg->count + 1);
  1966. for (;;)
  1967. {
  1968. const cpp_token *token;
  1969. source_location location;
  1970. ensure_expanded_arg_room (pfile, arg, arg->expanded_count + 1,
  1971. &capacity);
  1972. token = cpp_get_token_1 (pfile, &location);
  1973. if (token->type == CPP_EOF)
  1974. break;
  1975. set_arg_token (arg, token, location,
  1976. arg->expanded_count, MACRO_ARG_TOKEN_EXPANDED,
  1977. CPP_OPTION (pfile, track_macro_expansion));
  1978. arg->expanded_count++;
  1979. }
  1980. _cpp_pop_context (pfile);
  1981. CPP_WTRADITIONAL (pfile) = saved_warn_trad;
  1982. }
  1983. /* Returns the macro associated to the current context if we are in
  1984. the context a macro expansion, NULL otherwise. */
  1985. static cpp_hashnode*
  1986. macro_of_context (cpp_context *context)
  1987. {
  1988. if (context == NULL)
  1989. return NULL;
  1990. return (context->tokens_kind == TOKENS_KIND_EXTENDED)
  1991. ? context->c.mc->macro_node
  1992. : context->c.macro;
  1993. }
  1994. /* Return TRUE iff we are expanding a macro or are about to start
  1995. expanding one. If we are effectively expanding a macro, the
  1996. function macro_of_context returns a pointer to the macro being
  1997. expanded. */
  1998. static bool
  1999. in_macro_expansion_p (cpp_reader *pfile)
  2000. {
  2001. if (pfile == NULL)
  2002. return false;
  2003. return (pfile->about_to_expand_macro_p
  2004. || macro_of_context (pfile->context));
  2005. }
  2006. /* Pop the current context off the stack, re-enabling the macro if the
  2007. context represented a macro's replacement list. Initially the
  2008. context structure was not freed so that we can re-use it later, but
  2009. now we do free it to reduce peak memory consumption. */
  2010. void
  2011. _cpp_pop_context (cpp_reader *pfile)
  2012. {
  2013. cpp_context *context = pfile->context;
  2014. /* We should not be popping the base context. */
  2015. if (context == &pfile->base_context)
  2016. abort ();
  2017. if (context->c.macro)
  2018. {
  2019. cpp_hashnode *macro;
  2020. if (context->tokens_kind == TOKENS_KIND_EXTENDED)
  2021. {
  2022. macro_context *mc = context->c.mc;
  2023. macro = mc->macro_node;
  2024. /* If context->buff is set, it means the life time of tokens
  2025. is bound to the life time of this context; so we must
  2026. free the tokens; that means we must free the virtual
  2027. locations of these tokens too. */
  2028. if (context->buff && mc->virt_locs)
  2029. {
  2030. free (mc->virt_locs);
  2031. mc->virt_locs = NULL;
  2032. }
  2033. free (mc);
  2034. context->c.mc = NULL;
  2035. }
  2036. else
  2037. macro = context->c.macro;
  2038. /* Beware that MACRO can be NULL in cases like when we are
  2039. called from expand_arg. In those cases, a dummy context with
  2040. tokens is pushed just for the purpose of walking them using
  2041. cpp_get_token_1. In that case, no 'macro' field is set into
  2042. the dummy context. */
  2043. if (macro != NULL
  2044. /* Several contiguous macro expansion contexts can be
  2045. associated to the same macro; that means it's the same
  2046. macro expansion that spans across all these (sub)
  2047. contexts. So we should re-enable an expansion-disabled
  2048. macro only when we are sure we are really out of that
  2049. macro expansion. */
  2050. && macro_of_context (context->prev) != macro)
  2051. macro->flags &= ~NODE_DISABLED;
  2052. if (macro == pfile->top_most_macro_node && context->prev == NULL)
  2053. /* We are popping the context of the top-most macro node. */
  2054. pfile->top_most_macro_node = NULL;
  2055. }
  2056. if (context->buff)
  2057. {
  2058. /* Decrease memory peak consumption by freeing the memory used
  2059. by the context. */
  2060. _cpp_free_buff (context->buff);
  2061. }
  2062. pfile->context = context->prev;
  2063. /* decrease peak memory consumption by feeing the context. */
  2064. pfile->context->next = NULL;
  2065. free (context);
  2066. }
  2067. /* Return TRUE if we reached the end of the set of tokens stored in
  2068. CONTEXT, FALSE otherwise. */
  2069. static inline bool
  2070. reached_end_of_context (cpp_context *context)
  2071. {
  2072. if (context->tokens_kind == TOKENS_KIND_DIRECT)
  2073. return FIRST (context).token == LAST (context).token;
  2074. else if (context->tokens_kind == TOKENS_KIND_INDIRECT
  2075. || context->tokens_kind == TOKENS_KIND_EXTENDED)
  2076. return FIRST (context).ptoken == LAST (context).ptoken;
  2077. else
  2078. abort ();
  2079. }
  2080. /* Consume the next token contained in the current context of PFILE,
  2081. and return it in *TOKEN. It's "full location" is returned in
  2082. *LOCATION. If -ftrack-macro-location is in effeect, fFull location"
  2083. means the location encoding the locus of the token across macro
  2084. expansion; otherwise it's just is the "normal" location of the
  2085. token which (*TOKEN)->src_loc. */
  2086. static inline void
  2087. consume_next_token_from_context (cpp_reader *pfile,
  2088. const cpp_token ** token,
  2089. source_location *location)
  2090. {
  2091. cpp_context *c = pfile->context;
  2092. if ((c)->tokens_kind == TOKENS_KIND_DIRECT)
  2093. {
  2094. *token = FIRST (c).token;
  2095. *location = (*token)->src_loc;
  2096. FIRST (c).token++;
  2097. }
  2098. else if ((c)->tokens_kind == TOKENS_KIND_INDIRECT)
  2099. {
  2100. *token = *FIRST (c).ptoken;
  2101. *location = (*token)->src_loc;
  2102. FIRST (c).ptoken++;
  2103. }
  2104. else if ((c)->tokens_kind == TOKENS_KIND_EXTENDED)
  2105. {
  2106. macro_context *m = c->c.mc;
  2107. *token = *FIRST (c).ptoken;
  2108. if (m->virt_locs)
  2109. {
  2110. *location = *m->cur_virt_loc;
  2111. m->cur_virt_loc++;
  2112. }
  2113. else
  2114. *location = (*token)->src_loc;
  2115. FIRST (c).ptoken++;
  2116. }
  2117. else
  2118. abort ();
  2119. }
  2120. /* In the traditional mode of the preprocessor, if we are currently in
  2121. a directive, the location of a token must be the location of the
  2122. start of the directive line. This function returns the proper
  2123. location if we are in the traditional mode, and just returns
  2124. LOCATION otherwise. */
  2125. static inline source_location
  2126. maybe_adjust_loc_for_trad_cpp (cpp_reader *pfile, source_location location)
  2127. {
  2128. if (CPP_OPTION (pfile, traditional))
  2129. {
  2130. if (pfile->state.in_directive)
  2131. return pfile->directive_line;
  2132. }
  2133. return location;
  2134. }
  2135. /* Routine to get a token as well as its location.
  2136. Macro expansions and directives are transparently handled,
  2137. including entering included files. Thus tokens are post-macro
  2138. expansion, and after any intervening directives. External callers
  2139. see CPP_EOF only at EOF. Internal callers also see it when meeting
  2140. a directive inside a macro call, when at the end of a directive and
  2141. state.in_directive is still 1, and at the end of argument
  2142. pre-expansion.
  2143. LOC is an out parameter; *LOC is set to the location "as expected
  2144. by the user". Please read the comment of
  2145. cpp_get_token_with_location to learn more about the meaning of this
  2146. location. */
  2147. static const cpp_token*
  2148. cpp_get_token_1 (cpp_reader *pfile, source_location *location)
  2149. {
  2150. const cpp_token *result;
  2151. /* This token is a virtual token that either encodes a location
  2152. related to macro expansion or a spelling location. */
  2153. source_location virt_loc = 0;
  2154. /* pfile->about_to_expand_macro_p can be overriden by indirect calls
  2155. to functions that push macro contexts. So let's save it so that
  2156. we can restore it when we are about to leave this routine. */
  2157. bool saved_about_to_expand_macro = pfile->about_to_expand_macro_p;
  2158. for (;;)
  2159. {
  2160. cpp_hashnode *node;
  2161. cpp_context *context = pfile->context;
  2162. /* Context->prev == 0 <=> base context. */
  2163. if (!context->prev)
  2164. {
  2165. result = _cpp_lex_token (pfile);
  2166. virt_loc = result->src_loc;
  2167. }
  2168. else if (!reached_end_of_context (context))
  2169. {
  2170. consume_next_token_from_context (pfile, &result,
  2171. &virt_loc);
  2172. if (result->flags & PASTE_LEFT)
  2173. {
  2174. paste_all_tokens (pfile, result);
  2175. if (pfile->state.in_directive)
  2176. continue;
  2177. result = padding_token (pfile, result);
  2178. goto out;
  2179. }
  2180. }
  2181. else
  2182. {
  2183. if (pfile->context->c.macro)
  2184. ++num_expanded_macros_counter;
  2185. _cpp_pop_context (pfile);
  2186. if (pfile->state.in_directive)
  2187. continue;
  2188. result = &pfile->avoid_paste;
  2189. goto out;
  2190. }
  2191. if (pfile->state.in_directive && result->type == CPP_COMMENT)
  2192. continue;
  2193. if (result->type != CPP_NAME)
  2194. break;
  2195. node = result->val.node.node;
  2196. if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
  2197. break;
  2198. if (!(node->flags & NODE_DISABLED))
  2199. {
  2200. int ret = 0;
  2201. /* If not in a macro context, and we're going to start an
  2202. expansion, record the location and the top level macro
  2203. about to be expanded. */
  2204. if (!in_macro_expansion_p (pfile))
  2205. {
  2206. pfile->invocation_location = result->src_loc;
  2207. pfile->top_most_macro_node = node;
  2208. }
  2209. if (pfile->state.prevent_expansion)
  2210. break;
  2211. /* Conditional macros require that a predicate be evaluated
  2212. first. */
  2213. if ((node->flags & NODE_CONDITIONAL) != 0)
  2214. {
  2215. if (pfile->cb.macro_to_expand)
  2216. {
  2217. bool whitespace_after;
  2218. const cpp_token *peek_tok = cpp_peek_token (pfile, 0);
  2219. whitespace_after = (peek_tok->type == CPP_PADDING
  2220. || (peek_tok->flags & PREV_WHITE));
  2221. node = pfile->cb.macro_to_expand (pfile, result);
  2222. if (node)
  2223. ret = enter_macro_context (pfile, node, result,
  2224. virt_loc);
  2225. else if (whitespace_after)
  2226. {
  2227. /* If macro_to_expand hook returned NULL and it
  2228. ate some tokens, see if we don't need to add
  2229. a padding token in between this and the
  2230. next token. */
  2231. peek_tok = cpp_peek_token (pfile, 0);
  2232. if (peek_tok->type != CPP_PADDING
  2233. && (peek_tok->flags & PREV_WHITE) == 0)
  2234. _cpp_push_token_context (pfile, NULL,
  2235. padding_token (pfile,
  2236. peek_tok), 1);
  2237. }
  2238. }
  2239. }
  2240. else
  2241. ret = enter_macro_context (pfile, node, result,
  2242. virt_loc);
  2243. if (ret)
  2244. {
  2245. if (pfile->state.in_directive || ret == 2)
  2246. continue;
  2247. result = padding_token (pfile, result);
  2248. goto out;
  2249. }
  2250. }
  2251. else
  2252. {
  2253. /* Flag this token as always unexpandable. FIXME: move this
  2254. to collect_args()?. */
  2255. cpp_token *t = _cpp_temp_token (pfile);
  2256. t->type = result->type;
  2257. t->flags = result->flags | NO_EXPAND;
  2258. t->val = result->val;
  2259. result = t;
  2260. }
  2261. break;
  2262. }
  2263. out:
  2264. if (location != NULL)
  2265. {
  2266. if (virt_loc == 0)
  2267. virt_loc = result->src_loc;
  2268. *location = virt_loc;
  2269. if (!CPP_OPTION (pfile, track_macro_expansion)
  2270. && macro_of_context (pfile->context) != NULL)
  2271. /* We are in a macro expansion context, are not tracking
  2272. virtual location, but were asked to report the location
  2273. of the expansion point of the macro being expanded. */
  2274. *location = pfile->invocation_location;
  2275. *location = maybe_adjust_loc_for_trad_cpp (pfile, *location);
  2276. }
  2277. pfile->about_to_expand_macro_p = saved_about_to_expand_macro;
  2278. return result;
  2279. }
  2280. /* External routine to get a token. Also used nearly everywhere
  2281. internally, except for places where we know we can safely call
  2282. _cpp_lex_token directly, such as lexing a directive name.
  2283. Macro expansions and directives are transparently handled,
  2284. including entering included files. Thus tokens are post-macro
  2285. expansion, and after any intervening directives. External callers
  2286. see CPP_EOF only at EOF. Internal callers also see it when meeting
  2287. a directive inside a macro call, when at the end of a directive and
  2288. state.in_directive is still 1, and at the end of argument
  2289. pre-expansion. */
  2290. const cpp_token *
  2291. cpp_get_token (cpp_reader *pfile)
  2292. {
  2293. return cpp_get_token_1 (pfile, NULL);
  2294. }
  2295. /* Like cpp_get_token, but also returns a virtual token location
  2296. separate from the spelling location carried by the returned token.
  2297. LOC is an out parameter; *LOC is set to the location "as expected
  2298. by the user". This matters when a token results from macro
  2299. expansion; in that case the token's spelling location indicates the
  2300. locus of the token in the definition of the macro but *LOC
  2301. virtually encodes all the other meaningful locuses associated to
  2302. the token.
  2303. What? virtual location? Yes, virtual location.
  2304. If the token results from macro expansion and if macro expansion
  2305. location tracking is enabled its virtual location encodes (at the
  2306. same time):
  2307. - the spelling location of the token
  2308. - the locus of the macro expansion point
  2309. - the locus of the point where the token got instantiated as part
  2310. of the macro expansion process.
  2311. You have to use the linemap API to get the locus you are interested
  2312. in from a given virtual location.
  2313. Note however that virtual locations are not necessarily ordered for
  2314. relations '<' and '>'. One must use the function
  2315. linemap_location_before_p instead of using the relational operator
  2316. '<'.
  2317. If macro expansion tracking is off and if the token results from
  2318. macro expansion the virtual location is the expansion point of the
  2319. macro that got expanded.
  2320. When the token doesn't result from macro expansion, the virtual
  2321. location is just the same thing as its spelling location. */
  2322. const cpp_token *
  2323. cpp_get_token_with_location (cpp_reader *pfile, source_location *loc)
  2324. {
  2325. return cpp_get_token_1 (pfile, loc);
  2326. }
  2327. /* Returns true if we're expanding an object-like macro that was
  2328. defined in a system header. Just checks the macro at the top of
  2329. the stack. Used for diagnostic suppression. */
  2330. int
  2331. cpp_sys_macro_p (cpp_reader *pfile)
  2332. {
  2333. cpp_hashnode *node = NULL;
  2334. if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
  2335. node = pfile->context->c.mc->macro_node;
  2336. else
  2337. node = pfile->context->c.macro;
  2338. return node && node->value.macro && node->value.macro->syshdr;
  2339. }
  2340. /* Read each token in, until end of the current file. Directives are
  2341. transparently processed. */
  2342. void
  2343. cpp_scan_nooutput (cpp_reader *pfile)
  2344. {
  2345. /* Request a CPP_EOF token at the end of this file, rather than
  2346. transparently continuing with the including file. */
  2347. pfile->buffer->return_at_eof = true;
  2348. pfile->state.discarding_output++;
  2349. pfile->state.prevent_expansion++;
  2350. if (CPP_OPTION (pfile, traditional))
  2351. while (_cpp_read_logical_line_trad (pfile))
  2352. ;
  2353. else
  2354. while (cpp_get_token (pfile)->type != CPP_EOF)
  2355. ;
  2356. pfile->state.discarding_output--;
  2357. pfile->state.prevent_expansion--;
  2358. }
  2359. /* Step back one or more tokens obtained from the lexer. */
  2360. void
  2361. _cpp_backup_tokens_direct (cpp_reader *pfile, unsigned int count)
  2362. {
  2363. pfile->lookaheads += count;
  2364. while (count--)
  2365. {
  2366. pfile->cur_token--;
  2367. if (pfile->cur_token == pfile->cur_run->base
  2368. /* Possible with -fpreprocessed and no leading #line. */
  2369. && pfile->cur_run->prev != NULL)
  2370. {
  2371. pfile->cur_run = pfile->cur_run->prev;
  2372. pfile->cur_token = pfile->cur_run->limit;
  2373. }
  2374. }
  2375. }
  2376. /* Step back one (or more) tokens. Can only step back more than 1 if
  2377. they are from the lexer, and not from macro expansion. */
  2378. void
  2379. _cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
  2380. {
  2381. if (pfile->context->prev == NULL)
  2382. _cpp_backup_tokens_direct (pfile, count);
  2383. else
  2384. {
  2385. if (count != 1)
  2386. abort ();
  2387. if (pfile->context->tokens_kind == TOKENS_KIND_DIRECT)
  2388. FIRST (pfile->context).token--;
  2389. else if (pfile->context->tokens_kind == TOKENS_KIND_INDIRECT)
  2390. FIRST (pfile->context).ptoken--;
  2391. else if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
  2392. {
  2393. FIRST (pfile->context).ptoken--;
  2394. if (pfile->context->c.macro)
  2395. {
  2396. macro_context *m = pfile->context->c.mc;
  2397. m->cur_virt_loc--;
  2398. #ifdef ENABLE_CHECKING
  2399. if (m->cur_virt_loc < m->virt_locs)
  2400. abort ();
  2401. #endif
  2402. }
  2403. else
  2404. abort ();
  2405. }
  2406. else
  2407. abort ();
  2408. }
  2409. }
  2410. /* #define directive parsing and handling. */
  2411. /* Returns nonzero if a macro redefinition warning is required. */
  2412. static bool
  2413. warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node,
  2414. const cpp_macro *macro2)
  2415. {
  2416. const cpp_macro *macro1;
  2417. unsigned int i;
  2418. /* Some redefinitions need to be warned about regardless. */
  2419. if (node->flags & NODE_WARN)
  2420. return true;
  2421. /* Suppress warnings for builtins that lack the NODE_WARN flag,
  2422. unless Wbuiltin-macro-redefined. */
  2423. if (node->flags & NODE_BUILTIN
  2424. && (!pfile->cb.user_builtin_macro
  2425. || !pfile->cb.user_builtin_macro (pfile, node)))
  2426. return CPP_OPTION (pfile, warn_builtin_macro_redefined);
  2427. /* Redefinitions of conditional (context-sensitive) macros, on
  2428. the other hand, must be allowed silently. */
  2429. if (node->flags & NODE_CONDITIONAL)
  2430. return false;
  2431. /* Redefinition of a macro is allowed if and only if the old and new
  2432. definitions are the same. (6.10.3 paragraph 2). */
  2433. macro1 = node->value.macro;
  2434. /* Don't check count here as it can be different in valid
  2435. traditional redefinitions with just whitespace differences. */
  2436. if (macro1->paramc != macro2->paramc
  2437. || macro1->fun_like != macro2->fun_like
  2438. || macro1->variadic != macro2->variadic)
  2439. return true;
  2440. /* Check parameter spellings. */
  2441. for (i = 0; i < macro1->paramc; i++)
  2442. if (macro1->params[i] != macro2->params[i])
  2443. return true;
  2444. /* Check the replacement text or tokens. */
  2445. if (CPP_OPTION (pfile, traditional))
  2446. return _cpp_expansions_different_trad (macro1, macro2);
  2447. if (macro1->count != macro2->count)
  2448. return true;
  2449. for (i = 0; i < macro1->count; i++)
  2450. if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
  2451. return true;
  2452. return false;
  2453. }
  2454. /* Free the definition of hashnode H. */
  2455. void
  2456. _cpp_free_definition (cpp_hashnode *h)
  2457. {
  2458. /* Macros and assertions no longer have anything to free. */
  2459. h->type = NT_VOID;
  2460. /* Clear builtin flag in case of redefinition. */
  2461. h->flags &= ~(NODE_BUILTIN | NODE_DISABLED | NODE_USED);
  2462. }
  2463. /* Save parameter NODE (spelling SPELLING) to the parameter list of
  2464. macro MACRO. Returns zero on success, nonzero if the parameter is
  2465. a duplicate. */
  2466. bool
  2467. _cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node,
  2468. cpp_hashnode *spelling)
  2469. {
  2470. unsigned int len;
  2471. /* Constraint 6.10.3.6 - duplicate parameter names. */
  2472. if (node->flags & NODE_MACRO_ARG)
  2473. {
  2474. cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"",
  2475. NODE_NAME (node));
  2476. return true;
  2477. }
  2478. if (BUFF_ROOM (pfile->a_buff)
  2479. < (macro->paramc + 1) * sizeof (cpp_hashnode *))
  2480. _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
  2481. ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = spelling;
  2482. node->flags |= NODE_MACRO_ARG;
  2483. len = macro->paramc * sizeof (struct macro_arg_saved_data);
  2484. if (len > pfile->macro_buffer_len)
  2485. {
  2486. pfile->macro_buffer = XRESIZEVEC (unsigned char, pfile->macro_buffer,
  2487. len);
  2488. pfile->macro_buffer_len = len;
  2489. }
  2490. struct macro_arg_saved_data save;
  2491. save.value = node->value;
  2492. save.canonical_node = node;
  2493. ((struct macro_arg_saved_data *) pfile->macro_buffer)[macro->paramc - 1]
  2494. = save;
  2495. node->value.arg_index = macro->paramc;
  2496. return false;
  2497. }
  2498. /* Check the syntax of the parameters in a MACRO definition. Returns
  2499. false if an error occurs. */
  2500. static bool
  2501. parse_params (cpp_reader *pfile, cpp_macro *macro)
  2502. {
  2503. unsigned int prev_ident = 0;
  2504. for (;;)
  2505. {
  2506. const cpp_token *token = _cpp_lex_token (pfile);
  2507. switch (token->type)
  2508. {
  2509. default:
  2510. /* Allow/ignore comments in parameter lists if we are
  2511. preserving comments in macro expansions. */
  2512. if (token->type == CPP_COMMENT
  2513. && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
  2514. continue;
  2515. cpp_error (pfile, CPP_DL_ERROR,
  2516. "\"%s\" may not appear in macro parameter list",
  2517. cpp_token_as_text (pfile, token));
  2518. return false;
  2519. case CPP_NAME:
  2520. if (prev_ident)
  2521. {
  2522. cpp_error (pfile, CPP_DL_ERROR,
  2523. "macro parameters must be comma-separated");
  2524. return false;
  2525. }
  2526. prev_ident = 1;
  2527. if (_cpp_save_parameter (pfile, macro, token->val.node.node,
  2528. token->val.node.spelling))
  2529. return false;
  2530. continue;
  2531. case CPP_CLOSE_PAREN:
  2532. if (prev_ident || macro->paramc == 0)
  2533. return true;
  2534. /* Fall through to pick up the error. */
  2535. case CPP_COMMA:
  2536. if (!prev_ident)
  2537. {
  2538. cpp_error (pfile, CPP_DL_ERROR, "parameter name missing");
  2539. return false;
  2540. }
  2541. prev_ident = 0;
  2542. continue;
  2543. case CPP_ELLIPSIS:
  2544. macro->variadic = 1;
  2545. if (!prev_ident)
  2546. {
  2547. _cpp_save_parameter (pfile, macro,
  2548. pfile->spec_nodes.n__VA_ARGS__,
  2549. pfile->spec_nodes.n__VA_ARGS__);
  2550. pfile->state.va_args_ok = 1;
  2551. if (! CPP_OPTION (pfile, c99)
  2552. && CPP_OPTION (pfile, cpp_pedantic)
  2553. && CPP_OPTION (pfile, warn_variadic_macros))
  2554. {
  2555. if (CPP_OPTION (pfile, cplusplus))
  2556. cpp_pedwarning
  2557. (pfile, CPP_W_VARIADIC_MACROS,
  2558. "anonymous variadic macros were introduced in C++11");
  2559. else
  2560. cpp_pedwarning
  2561. (pfile, CPP_W_VARIADIC_MACROS,
  2562. "anonymous variadic macros were introduced in C99");
  2563. }
  2564. else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
  2565. && ! CPP_OPTION (pfile, cplusplus))
  2566. cpp_error (pfile, CPP_DL_WARNING,
  2567. "anonymous variadic macros were introduced in C99");
  2568. }
  2569. else if (CPP_OPTION (pfile, cpp_pedantic)
  2570. && CPP_OPTION (pfile, warn_variadic_macros))
  2571. {
  2572. if (CPP_OPTION (pfile, cplusplus))
  2573. cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS,
  2574. "ISO C++ does not permit named variadic macros");
  2575. else
  2576. cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS,
  2577. "ISO C does not permit named variadic macros");
  2578. }
  2579. /* We're at the end, and just expect a closing parenthesis. */
  2580. token = _cpp_lex_token (pfile);
  2581. if (token->type == CPP_CLOSE_PAREN)
  2582. return true;
  2583. /* Fall through. */
  2584. case CPP_EOF:
  2585. cpp_error (pfile, CPP_DL_ERROR, "missing ')' in macro parameter list");
  2586. return false;
  2587. }
  2588. }
  2589. }
  2590. /* Allocate room for a token from a macro's replacement list. */
  2591. static cpp_token *
  2592. alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro)
  2593. {
  2594. if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
  2595. _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
  2596. return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
  2597. }
  2598. /* Lex a token from the expansion of MACRO, but mark parameters as we
  2599. find them and warn of traditional stringification. */
  2600. static cpp_token *
  2601. lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
  2602. {
  2603. cpp_token *token, *saved_cur_token;
  2604. saved_cur_token = pfile->cur_token;
  2605. pfile->cur_token = alloc_expansion_token (pfile, macro);
  2606. token = _cpp_lex_direct (pfile);
  2607. pfile->cur_token = saved_cur_token;
  2608. /* Is this a parameter? */
  2609. if (token->type == CPP_NAME
  2610. && (token->val.node.node->flags & NODE_MACRO_ARG) != 0)
  2611. {
  2612. cpp_hashnode *spelling = token->val.node.spelling;
  2613. token->type = CPP_MACRO_ARG;
  2614. token->val.macro_arg.arg_no = token->val.node.node->value.arg_index;
  2615. token->val.macro_arg.spelling = spelling;
  2616. }
  2617. else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
  2618. && (token->type == CPP_STRING || token->type == CPP_CHAR))
  2619. check_trad_stringification (pfile, macro, &token->val.str);
  2620. return token;
  2621. }
  2622. static bool
  2623. create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
  2624. {
  2625. cpp_token *token;
  2626. const cpp_token *ctoken;
  2627. bool following_paste_op = false;
  2628. const char *paste_op_error_msg =
  2629. N_("'##' cannot appear at either end of a macro expansion");
  2630. unsigned int num_extra_tokens = 0;
  2631. /* Get the first token of the expansion (or the '(' of a
  2632. function-like macro). */
  2633. ctoken = _cpp_lex_token (pfile);
  2634. if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
  2635. {
  2636. bool ok = parse_params (pfile, macro);
  2637. macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
  2638. if (!ok)
  2639. return false;
  2640. /* Success. Commit or allocate the parameter array. */
  2641. if (pfile->hash_table->alloc_subobject)
  2642. {
  2643. cpp_hashnode **params =
  2644. (cpp_hashnode **) pfile->hash_table->alloc_subobject
  2645. (sizeof (cpp_hashnode *) * macro->paramc);
  2646. memcpy (params, macro->params,
  2647. sizeof (cpp_hashnode *) * macro->paramc);
  2648. macro->params = params;
  2649. }
  2650. else
  2651. BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
  2652. macro->fun_like = 1;
  2653. }
  2654. else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
  2655. {
  2656. /* While ISO C99 requires whitespace before replacement text
  2657. in a macro definition, ISO C90 with TC1 allows characters
  2658. from the basic source character set there. */
  2659. if (CPP_OPTION (pfile, c99))
  2660. {
  2661. if (CPP_OPTION (pfile, cplusplus))
  2662. cpp_error (pfile, CPP_DL_PEDWARN,
  2663. "ISO C++11 requires whitespace after the macro name");
  2664. else
  2665. cpp_error (pfile, CPP_DL_PEDWARN,
  2666. "ISO C99 requires whitespace after the macro name");
  2667. }
  2668. else
  2669. {
  2670. int warntype = CPP_DL_WARNING;
  2671. switch (ctoken->type)
  2672. {
  2673. case CPP_ATSIGN:
  2674. case CPP_AT_NAME:
  2675. case CPP_OBJC_STRING:
  2676. /* '@' is not in basic character set. */
  2677. warntype = CPP_DL_PEDWARN;
  2678. break;
  2679. case CPP_OTHER:
  2680. /* Basic character set sans letters, digits and _. */
  2681. if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
  2682. ctoken->val.str.text[0]) == NULL)
  2683. warntype = CPP_DL_PEDWARN;
  2684. break;
  2685. default:
  2686. /* All other tokens start with a character from basic
  2687. character set. */
  2688. break;
  2689. }
  2690. cpp_error (pfile, warntype,
  2691. "missing whitespace after the macro name");
  2692. }
  2693. }
  2694. if (macro->fun_like)
  2695. token = lex_expansion_token (pfile, macro);
  2696. else
  2697. {
  2698. token = alloc_expansion_token (pfile, macro);
  2699. *token = *ctoken;
  2700. }
  2701. for (;;)
  2702. {
  2703. /* Check the stringifying # constraint 6.10.3.2.1 of
  2704. function-like macros when lexing the subsequent token. */
  2705. if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
  2706. {
  2707. if (token->type == CPP_MACRO_ARG)
  2708. {
  2709. if (token->flags & PREV_WHITE)
  2710. token->flags |= SP_PREV_WHITE;
  2711. if (token[-1].flags & DIGRAPH)
  2712. token->flags |= SP_DIGRAPH;
  2713. token->flags &= ~PREV_WHITE;
  2714. token->flags |= STRINGIFY_ARG;
  2715. token->flags |= token[-1].flags & PREV_WHITE;
  2716. token[-1] = token[0];
  2717. macro->count--;
  2718. }
  2719. /* Let assembler get away with murder. */
  2720. else if (CPP_OPTION (pfile, lang) != CLK_ASM)
  2721. {
  2722. cpp_error (pfile, CPP_DL_ERROR,
  2723. "'#' is not followed by a macro parameter");
  2724. return false;
  2725. }
  2726. }
  2727. if (token->type == CPP_EOF)
  2728. {
  2729. /* Paste operator constraint 6.10.3.3.1:
  2730. Token-paste ##, can appear in both object-like and
  2731. function-like macros, but not at the end. */
  2732. if (following_paste_op)
  2733. {
  2734. cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
  2735. return false;
  2736. }
  2737. break;
  2738. }
  2739. /* Paste operator constraint 6.10.3.3.1. */
  2740. if (token->type == CPP_PASTE)
  2741. {
  2742. /* Token-paste ##, can appear in both object-like and
  2743. function-like macros, but not at the beginning. */
  2744. if (macro->count == 1)
  2745. {
  2746. cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
  2747. return false;
  2748. }
  2749. if (token[-1].flags & PASTE_LEFT)
  2750. {
  2751. macro->extra_tokens = 1;
  2752. num_extra_tokens++;
  2753. token->val.token_no = macro->count - 1;
  2754. }
  2755. else
  2756. {
  2757. --macro->count;
  2758. token[-1].flags |= PASTE_LEFT;
  2759. if (token->flags & DIGRAPH)
  2760. token[-1].flags |= SP_DIGRAPH;
  2761. if (token->flags & PREV_WHITE)
  2762. token[-1].flags |= SP_PREV_WHITE;
  2763. }
  2764. }
  2765. following_paste_op = (token->type == CPP_PASTE);
  2766. token = lex_expansion_token (pfile, macro);
  2767. }
  2768. macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
  2769. macro->traditional = 0;
  2770. /* Don't count the CPP_EOF. */
  2771. macro->count--;
  2772. /* Clear whitespace on first token for warn_of_redefinition(). */
  2773. if (macro->count)
  2774. macro->exp.tokens[0].flags &= ~PREV_WHITE;
  2775. /* Commit or allocate the memory. */
  2776. if (pfile->hash_table->alloc_subobject)
  2777. {
  2778. cpp_token *tokns =
  2779. (cpp_token *) pfile->hash_table->alloc_subobject (sizeof (cpp_token)
  2780. * macro->count);
  2781. if (num_extra_tokens)
  2782. {
  2783. /* Place second and subsequent ## or %:%: tokens in
  2784. sequences of consecutive such tokens at the end of the
  2785. list to preserve information about where they appear, how
  2786. they are spelt and whether they are preceded by
  2787. whitespace without otherwise interfering with macro
  2788. expansion. */
  2789. cpp_token *normal_dest = tokns;
  2790. cpp_token *extra_dest = tokns + macro->count - num_extra_tokens;
  2791. unsigned int i;
  2792. for (i = 0; i < macro->count; i++)
  2793. {
  2794. if (macro->exp.tokens[i].type == CPP_PASTE)
  2795. *extra_dest++ = macro->exp.tokens[i];
  2796. else
  2797. *normal_dest++ = macro->exp.tokens[i];
  2798. }
  2799. }
  2800. else
  2801. memcpy (tokns, macro->exp.tokens, sizeof (cpp_token) * macro->count);
  2802. macro->exp.tokens = tokns;
  2803. }
  2804. else
  2805. BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->exp.tokens[macro->count];
  2806. return true;
  2807. }
  2808. /* Parse a macro and save its expansion. Returns nonzero on success. */
  2809. bool
  2810. _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
  2811. {
  2812. cpp_macro *macro;
  2813. unsigned int i;
  2814. bool ok;
  2815. if (pfile->hash_table->alloc_subobject)
  2816. macro = (cpp_macro *) pfile->hash_table->alloc_subobject
  2817. (sizeof (cpp_macro));
  2818. else
  2819. macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
  2820. macro->line = pfile->directive_line;
  2821. macro->params = 0;
  2822. macro->paramc = 0;
  2823. macro->variadic = 0;
  2824. macro->used = !CPP_OPTION (pfile, warn_unused_macros);
  2825. macro->count = 0;
  2826. macro->fun_like = 0;
  2827. macro->extra_tokens = 0;
  2828. /* To suppress some diagnostics. */
  2829. macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
  2830. if (CPP_OPTION (pfile, traditional))
  2831. ok = _cpp_create_trad_definition (pfile, macro);
  2832. else
  2833. {
  2834. ok = create_iso_definition (pfile, macro);
  2835. /* We set the type for SEEN_EOL() in directives.c.
  2836. Longer term we should lex the whole line before coming here,
  2837. and just copy the expansion. */
  2838. /* Stop the lexer accepting __VA_ARGS__. */
  2839. pfile->state.va_args_ok = 0;
  2840. }
  2841. /* Clear the fast argument lookup indices. */
  2842. for (i = macro->paramc; i-- > 0; )
  2843. {
  2844. struct macro_arg_saved_data *save =
  2845. &((struct macro_arg_saved_data *) pfile->macro_buffer)[i];
  2846. struct cpp_hashnode *node = save->canonical_node;
  2847. node->flags &= ~ NODE_MACRO_ARG;
  2848. node->value = save->value;
  2849. }
  2850. if (!ok)
  2851. return ok;
  2852. if (node->type == NT_MACRO)
  2853. {
  2854. if (CPP_OPTION (pfile, warn_unused_macros))
  2855. _cpp_warn_if_unused_macro (pfile, node, NULL);
  2856. if (warn_of_redefinition (pfile, node, macro))
  2857. {
  2858. const int reason = ((node->flags & NODE_BUILTIN)
  2859. && !(node->flags & NODE_WARN))
  2860. ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE;
  2861. bool warned =
  2862. cpp_pedwarning_with_line (pfile, reason,
  2863. pfile->directive_line, 0,
  2864. "\"%s\" redefined", NODE_NAME (node));
  2865. if (warned && node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
  2866. cpp_error_with_line (pfile, CPP_DL_NOTE,
  2867. node->value.macro->line, 0,
  2868. "this is the location of the previous definition");
  2869. }
  2870. }
  2871. if (node->type != NT_VOID)
  2872. _cpp_free_definition (node);
  2873. /* Enter definition in hash table. */
  2874. node->type = NT_MACRO;
  2875. node->value.macro = macro;
  2876. if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_"))
  2877. && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_FORMAT_MACROS")
  2878. /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned
  2879. in the C standard, as something that one must use in C++.
  2880. However DR#593 and C++11 indicate that they play no role in C++.
  2881. We special-case them anyway. */
  2882. && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_LIMIT_MACROS")
  2883. && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_CONSTANT_MACROS"))
  2884. node->flags |= NODE_WARN;
  2885. /* If user defines one of the conditional macros, remove the
  2886. conditional flag */
  2887. node->flags &= ~NODE_CONDITIONAL;
  2888. return ok;
  2889. }
  2890. /* Warn if a token in STRING matches one of a function-like MACRO's
  2891. parameters. */
  2892. static void
  2893. check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
  2894. const cpp_string *string)
  2895. {
  2896. unsigned int i, len;
  2897. const uchar *p, *q, *limit;
  2898. /* Loop over the string. */
  2899. limit = string->text + string->len - 1;
  2900. for (p = string->text + 1; p < limit; p = q)
  2901. {
  2902. /* Find the start of an identifier. */
  2903. while (p < limit && !is_idstart (*p))
  2904. p++;
  2905. /* Find the end of the identifier. */
  2906. q = p;
  2907. while (q < limit && is_idchar (*q))
  2908. q++;
  2909. len = q - p;
  2910. /* Loop over the function macro arguments to see if the
  2911. identifier inside the string matches one of them. */
  2912. for (i = 0; i < macro->paramc; i++)
  2913. {
  2914. const cpp_hashnode *node = macro->params[i];
  2915. if (NODE_LEN (node) == len
  2916. && !memcmp (p, NODE_NAME (node), len))
  2917. {
  2918. cpp_error (pfile, CPP_DL_WARNING,
  2919. "macro argument \"%s\" would be stringified in traditional C",
  2920. NODE_NAME (node));
  2921. break;
  2922. }
  2923. }
  2924. }
  2925. }
  2926. /* Returns the name, arguments and expansion of a macro, in a format
  2927. suitable to be read back in again, and therefore also for DWARF 2
  2928. debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
  2929. Caller is expected to generate the "#define" bit if needed. The
  2930. returned text is temporary, and automatically freed later. */
  2931. const unsigned char *
  2932. cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node)
  2933. {
  2934. unsigned int i, len;
  2935. const cpp_macro *macro;
  2936. unsigned char *buffer;
  2937. if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
  2938. {
  2939. if (node->type != NT_MACRO
  2940. || !pfile->cb.user_builtin_macro
  2941. || !pfile->cb.user_builtin_macro (pfile, node))
  2942. {
  2943. cpp_error (pfile, CPP_DL_ICE,
  2944. "invalid hash type %d in cpp_macro_definition",
  2945. node->type);
  2946. return 0;
  2947. }
  2948. }
  2949. macro = node->value.macro;
  2950. /* Calculate length. */
  2951. len = NODE_LEN (node) * 10 + 2; /* ' ' and NUL. */
  2952. if (macro->fun_like)
  2953. {
  2954. len += 4; /* "()" plus possible final ".." of named
  2955. varargs (we have + 1 below). */
  2956. for (i = 0; i < macro->paramc; i++)
  2957. len += NODE_LEN (macro->params[i]) + 1; /* "," */
  2958. }
  2959. /* This should match below where we fill in the buffer. */
  2960. if (CPP_OPTION (pfile, traditional))
  2961. len += _cpp_replacement_text_len (macro);
  2962. else
  2963. {
  2964. unsigned int count = macro_real_token_count (macro);
  2965. for (i = 0; i < count; i++)
  2966. {
  2967. cpp_token *token = &macro->exp.tokens[i];
  2968. if (token->type == CPP_MACRO_ARG)
  2969. len += NODE_LEN (token->val.macro_arg.spelling);
  2970. else
  2971. len += cpp_token_len (token);
  2972. if (token->flags & STRINGIFY_ARG)
  2973. len++; /* "#" */
  2974. if (token->flags & PASTE_LEFT)
  2975. len += 3; /* " ##" */
  2976. if (token->flags & PREV_WHITE)
  2977. len++; /* " " */
  2978. }
  2979. }
  2980. if (len > pfile->macro_buffer_len)
  2981. {
  2982. pfile->macro_buffer = XRESIZEVEC (unsigned char,
  2983. pfile->macro_buffer, len);
  2984. pfile->macro_buffer_len = len;
  2985. }
  2986. /* Fill in the buffer. Start with the macro name. */
  2987. buffer = pfile->macro_buffer;
  2988. buffer = _cpp_spell_ident_ucns (buffer, node);
  2989. /* Parameter names. */
  2990. if (macro->fun_like)
  2991. {
  2992. *buffer++ = '(';
  2993. for (i = 0; i < macro->paramc; i++)
  2994. {
  2995. cpp_hashnode *param = macro->params[i];
  2996. if (param != pfile->spec_nodes.n__VA_ARGS__)
  2997. {
  2998. memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
  2999. buffer += NODE_LEN (param);
  3000. }
  3001. if (i + 1 < macro->paramc)
  3002. /* Don't emit a space after the comma here; we're trying
  3003. to emit a Dwarf-friendly definition, and the Dwarf spec
  3004. forbids spaces in the argument list. */
  3005. *buffer++ = ',';
  3006. else if (macro->variadic)
  3007. *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
  3008. }
  3009. *buffer++ = ')';
  3010. }
  3011. /* The Dwarf spec requires a space after the macro name, even if the
  3012. definition is the empty string. */
  3013. *buffer++ = ' ';
  3014. if (CPP_OPTION (pfile, traditional))
  3015. buffer = _cpp_copy_replacement_text (macro, buffer);
  3016. else if (macro->count)
  3017. /* Expansion tokens. */
  3018. {
  3019. unsigned int count = macro_real_token_count (macro);
  3020. for (i = 0; i < count; i++)
  3021. {
  3022. cpp_token *token = &macro->exp.tokens[i];
  3023. if (token->flags & PREV_WHITE)
  3024. *buffer++ = ' ';
  3025. if (token->flags & STRINGIFY_ARG)
  3026. *buffer++ = '#';
  3027. if (token->type == CPP_MACRO_ARG)
  3028. {
  3029. memcpy (buffer,
  3030. NODE_NAME (token->val.macro_arg.spelling),
  3031. NODE_LEN (token->val.macro_arg.spelling));
  3032. buffer += NODE_LEN (token->val.macro_arg.spelling);
  3033. }
  3034. else
  3035. buffer = cpp_spell_token (pfile, token, buffer, true);
  3036. if (token->flags & PASTE_LEFT)
  3037. {
  3038. *buffer++ = ' ';
  3039. *buffer++ = '#';
  3040. *buffer++ = '#';
  3041. /* Next has PREV_WHITE; see _cpp_create_definition. */
  3042. }
  3043. }
  3044. }
  3045. *buffer = '\0';
  3046. return pfile->macro_buffer;
  3047. }