traditional.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  1. /* CPP Library - traditional lexical analysis and macro expansion.
  2. Copyright (C) 2002-2015 Free Software Foundation, Inc.
  3. Contributed by Neil Booth, May 2002
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 3, or (at your option) any
  7. later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "config.h"
  16. #include "system.h"
  17. #include "cpplib.h"
  18. #include "internal.h"
  19. /* The replacement text of a function-like macro is stored as a
  20. contiguous sequence of aligned blocks, each representing the text
  21. between subsequent parameters.
  22. Each block comprises the text between its surrounding parameters,
  23. the length of that text, and the one-based index of the following
  24. parameter. The final block in the replacement text is easily
  25. recognizable as it has an argument index of zero. */
  26. struct block
  27. {
  28. unsigned int text_len;
  29. unsigned short arg_index;
  30. uchar text[1];
  31. };
  32. #define BLOCK_HEADER_LEN offsetof (struct block, text)
  33. #define BLOCK_LEN(TEXT_LEN) CPP_ALIGN (BLOCK_HEADER_LEN + (TEXT_LEN))
  34. /* Structure holding information about a function-like macro
  35. invocation. */
  36. struct fun_macro
  37. {
  38. /* Memory buffer holding the trad_arg array. */
  39. _cpp_buff *buff;
  40. /* An array of size the number of macro parameters + 1, containing
  41. the offsets of the start of each macro argument in the output
  42. buffer. The argument continues until the character before the
  43. start of the next one. */
  44. size_t *args;
  45. /* The hashnode of the macro. */
  46. cpp_hashnode *node;
  47. /* The offset of the macro name in the output buffer. */
  48. size_t offset;
  49. /* The line the macro name appeared on. */
  50. source_location line;
  51. /* Number of parameters. */
  52. unsigned int paramc;
  53. /* Zero-based index of argument being currently lexed. */
  54. unsigned int argc;
  55. };
  56. /* Lexing state. It is mostly used to prevent macro expansion. */
  57. enum ls {ls_none = 0, /* Normal state. */
  58. ls_fun_open, /* When looking for '('. */
  59. ls_fun_close, /* When looking for ')'. */
  60. ls_defined, /* After defined. */
  61. ls_defined_close, /* Looking for ')' of defined(). */
  62. ls_hash, /* After # in preprocessor conditional. */
  63. ls_predicate, /* After the predicate, maybe paren? */
  64. ls_answer, /* In answer to predicate. */
  65. ls_has_include, /* After __has_include__. */
  66. ls_has_include_close}; /* Looking for ')' of __has_include__. */
  67. /* Lexing TODO: Maybe handle space in escaped newlines. Stop lex.c
  68. from recognizing comments and directives during its lexing pass. */
  69. static const uchar *skip_whitespace (cpp_reader *, const uchar *, int);
  70. static cpp_hashnode *lex_identifier (cpp_reader *, const uchar *);
  71. static const uchar *copy_comment (cpp_reader *, const uchar *, int);
  72. static void check_output_buffer (cpp_reader *, size_t);
  73. static void push_replacement_text (cpp_reader *, cpp_hashnode *);
  74. static bool scan_parameters (cpp_reader *, cpp_macro *);
  75. static bool recursive_macro (cpp_reader *, cpp_hashnode *);
  76. static void save_replacement_text (cpp_reader *, cpp_macro *, unsigned int);
  77. static void maybe_start_funlike (cpp_reader *, cpp_hashnode *, const uchar *,
  78. struct fun_macro *);
  79. static void save_argument (struct fun_macro *, size_t);
  80. static void replace_args_and_push (cpp_reader *, struct fun_macro *);
  81. static size_t canonicalize_text (uchar *, const uchar *, size_t, uchar *);
  82. /* Ensures we have N bytes' space in the output buffer, and
  83. reallocates it if not. */
  84. static void
  85. check_output_buffer (cpp_reader *pfile, size_t n)
  86. {
  87. /* We might need two bytes to terminate an unterminated comment, and
  88. one more to terminate the line with a NUL. */
  89. n += 2 + 1;
  90. if (n > (size_t) (pfile->out.limit - pfile->out.cur))
  91. {
  92. size_t size = pfile->out.cur - pfile->out.base;
  93. size_t new_size = (size + n) * 3 / 2;
  94. pfile->out.base = XRESIZEVEC (unsigned char, pfile->out.base, new_size);
  95. pfile->out.limit = pfile->out.base + new_size;
  96. pfile->out.cur = pfile->out.base + size;
  97. }
  98. }
  99. /* Skip a C-style block comment in a macro as a result of -CC.
  100. Buffer->cur points to the initial asterisk of the comment. */
  101. static void
  102. skip_macro_block_comment (cpp_reader *pfile)
  103. {
  104. const uchar *cur = pfile->buffer->cur;
  105. cur++;
  106. if (*cur == '/')
  107. cur++;
  108. /* People like decorating comments with '*', so check for '/'
  109. instead for efficiency. */
  110. while(! (*cur++ == '/' && cur[-2] == '*') )
  111. ;
  112. pfile->buffer->cur = cur;
  113. }
  114. /* CUR points to the asterisk introducing a comment in the current
  115. context. IN_DEFINE is true if we are in the replacement text of a
  116. macro.
  117. The asterisk and following comment is copied to the buffer pointed
  118. to by pfile->out.cur, which must be of sufficient size.
  119. Unterminated comments are diagnosed, and correctly terminated in
  120. the output. pfile->out.cur is updated depending upon IN_DEFINE,
  121. -C, -CC and pfile->state.in_directive.
  122. Returns a pointer to the first character after the comment in the
  123. input buffer. */
  124. static const uchar *
  125. copy_comment (cpp_reader *pfile, const uchar *cur, int in_define)
  126. {
  127. bool unterminated, copy = false;
  128. source_location src_loc = pfile->line_table->highest_line;
  129. cpp_buffer *buffer = pfile->buffer;
  130. buffer->cur = cur;
  131. if (pfile->context->prev)
  132. unterminated = false, skip_macro_block_comment (pfile);
  133. else
  134. unterminated = _cpp_skip_block_comment (pfile);
  135. if (unterminated)
  136. cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0,
  137. "unterminated comment");
  138. /* Comments in directives become spaces so that tokens are properly
  139. separated when the ISO preprocessor re-lexes the line. The
  140. exception is #define. */
  141. if (pfile->state.in_directive)
  142. {
  143. if (in_define)
  144. {
  145. if (CPP_OPTION (pfile, discard_comments_in_macro_exp))
  146. pfile->out.cur--;
  147. else
  148. copy = true;
  149. }
  150. else
  151. pfile->out.cur[-1] = ' ';
  152. }
  153. else if (CPP_OPTION (pfile, discard_comments))
  154. pfile->out.cur--;
  155. else
  156. copy = true;
  157. if (copy)
  158. {
  159. size_t len = (size_t) (buffer->cur - cur);
  160. memcpy (pfile->out.cur, cur, len);
  161. pfile->out.cur += len;
  162. if (unterminated)
  163. {
  164. *pfile->out.cur++ = '*';
  165. *pfile->out.cur++ = '/';
  166. }
  167. }
  168. return buffer->cur;
  169. }
  170. /* CUR points to any character in the input buffer. Skips over all
  171. contiguous horizontal white space and NULs, including comments if
  172. SKIP_COMMENTS, until reaching the first non-horizontal-whitespace
  173. character or the end of the current context. Escaped newlines are
  174. removed.
  175. The whitespace is copied verbatim to the output buffer, except that
  176. comments are handled as described in copy_comment().
  177. pfile->out.cur is updated.
  178. Returns a pointer to the first character after the whitespace in
  179. the input buffer. */
  180. static const uchar *
  181. skip_whitespace (cpp_reader *pfile, const uchar *cur, int skip_comments)
  182. {
  183. uchar *out = pfile->out.cur;
  184. for (;;)
  185. {
  186. unsigned int c = *cur++;
  187. *out++ = c;
  188. if (is_nvspace (c))
  189. continue;
  190. if (c == '/' && *cur == '*' && skip_comments)
  191. {
  192. pfile->out.cur = out;
  193. cur = copy_comment (pfile, cur, false /* in_define */);
  194. out = pfile->out.cur;
  195. continue;
  196. }
  197. out--;
  198. break;
  199. }
  200. pfile->out.cur = out;
  201. return cur - 1;
  202. }
  203. /* Lexes and outputs an identifier starting at CUR, which is assumed
  204. to point to a valid first character of an identifier. Returns
  205. the hashnode, and updates out.cur. */
  206. static cpp_hashnode *
  207. lex_identifier (cpp_reader *pfile, const uchar *cur)
  208. {
  209. size_t len;
  210. uchar *out = pfile->out.cur;
  211. cpp_hashnode *result;
  212. do
  213. *out++ = *cur++;
  214. while (is_numchar (*cur));
  215. CUR (pfile->context) = cur;
  216. len = out - pfile->out.cur;
  217. result = CPP_HASHNODE (ht_lookup (pfile->hash_table, pfile->out.cur,
  218. len, HT_ALLOC));
  219. pfile->out.cur = out;
  220. return result;
  221. }
  222. /* Overlays the true file buffer temporarily with text of length LEN
  223. starting at START. The true buffer is restored upon calling
  224. restore_buff(). */
  225. void
  226. _cpp_overlay_buffer (cpp_reader *pfile, const uchar *start, size_t len)
  227. {
  228. cpp_buffer *buffer = pfile->buffer;
  229. pfile->overlaid_buffer = buffer;
  230. pfile->saved_cur = buffer->cur;
  231. pfile->saved_rlimit = buffer->rlimit;
  232. pfile->saved_line_base = buffer->next_line;
  233. buffer->need_line = false;
  234. buffer->cur = start;
  235. buffer->line_base = start;
  236. buffer->rlimit = start + len;
  237. }
  238. /* Restores a buffer overlaid by _cpp_overlay_buffer(). */
  239. void
  240. _cpp_remove_overlay (cpp_reader *pfile)
  241. {
  242. cpp_buffer *buffer = pfile->overlaid_buffer;
  243. buffer->cur = pfile->saved_cur;
  244. buffer->rlimit = pfile->saved_rlimit;
  245. buffer->line_base = pfile->saved_line_base;
  246. buffer->need_line = true;
  247. pfile->overlaid_buffer = NULL;
  248. }
  249. /* Reads a logical line into the output buffer. Returns TRUE if there
  250. is more text left in the buffer. */
  251. bool
  252. _cpp_read_logical_line_trad (cpp_reader *pfile)
  253. {
  254. do
  255. {
  256. if (pfile->buffer->need_line && !_cpp_get_fresh_line (pfile))
  257. return false;
  258. }
  259. while (!_cpp_scan_out_logical_line (pfile, NULL, false)
  260. || pfile->state.skipping);
  261. return pfile->buffer != NULL;
  262. }
  263. /* Return true if NODE is a fun_like macro. */
  264. static inline bool
  265. fun_like_macro (cpp_hashnode *node)
  266. {
  267. if (node->flags & NODE_BUILTIN)
  268. return node->value.builtin == BT_HAS_ATTRIBUTE;
  269. else
  270. return node->value.macro->fun_like;
  271. }
  272. /* Set up state for finding the opening '(' of a function-like
  273. macro. */
  274. static void
  275. maybe_start_funlike (cpp_reader *pfile, cpp_hashnode *node, const uchar *start,
  276. struct fun_macro *macro)
  277. {
  278. unsigned int n;
  279. if (node->flags & NODE_BUILTIN)
  280. n = 1;
  281. else
  282. n = node->value.macro->paramc;
  283. if (macro->buff)
  284. _cpp_release_buff (pfile, macro->buff);
  285. macro->buff = _cpp_get_buff (pfile, (n + 1) * sizeof (size_t));
  286. macro->args = (size_t *) BUFF_FRONT (macro->buff);
  287. macro->node = node;
  288. macro->offset = start - pfile->out.base;
  289. macro->paramc = n;
  290. macro->argc = 0;
  291. }
  292. /* Save the OFFSET of the start of the next argument to MACRO. */
  293. static void
  294. save_argument (struct fun_macro *macro, size_t offset)
  295. {
  296. macro->argc++;
  297. if (macro->argc <= macro->paramc)
  298. macro->args[macro->argc] = offset;
  299. }
  300. /* Copies the next logical line in the current buffer (starting at
  301. buffer->cur) to the output buffer. The output is guaranteed to
  302. terminate with a NUL character. buffer->cur is updated.
  303. If MACRO is non-NULL, then we are scanning the replacement list of
  304. MACRO, and we call save_replacement_text() every time we meet an
  305. argument.
  306. If BUILTIN_MACRO_ARG is true, this is called to macro expand
  307. arguments of builtin function-like macros. */
  308. bool
  309. _cpp_scan_out_logical_line (cpp_reader *pfile, cpp_macro *macro,
  310. bool builtin_macro_arg)
  311. {
  312. bool result = true;
  313. cpp_context *context;
  314. const uchar *cur;
  315. uchar *out;
  316. struct fun_macro fmacro;
  317. unsigned int c, paren_depth = 0, quote;
  318. enum ls lex_state = ls_none;
  319. bool header_ok;
  320. const uchar *start_of_input_line;
  321. fmacro.buff = NULL;
  322. fmacro.args = NULL;
  323. fmacro.node = NULL;
  324. fmacro.offset = 0;
  325. fmacro.line = 0;
  326. fmacro.paramc = 0;
  327. fmacro.argc = 0;
  328. quote = 0;
  329. header_ok = pfile->state.angled_headers;
  330. CUR (pfile->context) = pfile->buffer->cur;
  331. RLIMIT (pfile->context) = pfile->buffer->rlimit;
  332. if (!builtin_macro_arg)
  333. {
  334. pfile->out.cur = pfile->out.base;
  335. pfile->out.first_line = pfile->line_table->highest_line;
  336. }
  337. /* start_of_input_line is needed to make sure that directives really,
  338. really start at the first character of the line. */
  339. start_of_input_line = pfile->buffer->cur;
  340. new_context:
  341. context = pfile->context;
  342. cur = CUR (context);
  343. check_output_buffer (pfile, RLIMIT (context) - cur);
  344. out = pfile->out.cur;
  345. for (;;)
  346. {
  347. if (!context->prev
  348. && !builtin_macro_arg
  349. && cur >= pfile->buffer->notes[pfile->buffer->cur_note].pos)
  350. {
  351. pfile->buffer->cur = cur;
  352. _cpp_process_line_notes (pfile, false);
  353. }
  354. c = *cur++;
  355. *out++ = c;
  356. /* Whitespace should "continue" out of the switch,
  357. non-whitespace should "break" out of it. */
  358. switch (c)
  359. {
  360. case ' ':
  361. case '\t':
  362. case '\f':
  363. case '\v':
  364. case '\0':
  365. continue;
  366. case '\n':
  367. /* If this is a macro's expansion, pop it. */
  368. if (context->prev)
  369. {
  370. pfile->out.cur = out - 1;
  371. _cpp_pop_context (pfile);
  372. goto new_context;
  373. }
  374. /* Omit the newline from the output buffer. */
  375. pfile->out.cur = out - 1;
  376. pfile->buffer->cur = cur;
  377. if (builtin_macro_arg)
  378. goto done;
  379. pfile->buffer->need_line = true;
  380. CPP_INCREMENT_LINE (pfile, 0);
  381. if ((lex_state == ls_fun_open || lex_state == ls_fun_close)
  382. && !pfile->state.in_directive
  383. && _cpp_get_fresh_line (pfile))
  384. {
  385. /* Newlines in arguments become a space, but we don't
  386. clear any in-progress quote. */
  387. if (lex_state == ls_fun_close)
  388. out[-1] = ' ';
  389. cur = pfile->buffer->cur;
  390. continue;
  391. }
  392. goto done;
  393. case '<':
  394. if (header_ok)
  395. quote = '>';
  396. break;
  397. case '>':
  398. if (c == quote)
  399. quote = 0;
  400. break;
  401. case '"':
  402. case '\'':
  403. if (c == quote)
  404. quote = 0;
  405. else if (!quote)
  406. quote = c;
  407. break;
  408. case '\\':
  409. /* Skip escaped quotes here, it's easier than above. */
  410. if (*cur == '\\' || *cur == '"' || *cur == '\'')
  411. *out++ = *cur++;
  412. break;
  413. case '/':
  414. /* Traditional CPP does not recognize comments within
  415. literals. */
  416. if (!quote && *cur == '*')
  417. {
  418. pfile->out.cur = out;
  419. cur = copy_comment (pfile, cur, macro != 0);
  420. out = pfile->out.cur;
  421. continue;
  422. }
  423. break;
  424. case '_':
  425. case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  426. case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  427. case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  428. case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  429. case 'y': case 'z':
  430. case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  431. case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  432. case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  433. case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  434. case 'Y': case 'Z':
  435. if (!pfile->state.skipping && (quote == 0 || macro))
  436. {
  437. cpp_hashnode *node;
  438. uchar *out_start = out - 1;
  439. pfile->out.cur = out_start;
  440. node = lex_identifier (pfile, cur - 1);
  441. out = pfile->out.cur;
  442. cur = CUR (context);
  443. if (node->type == NT_MACRO
  444. /* Should we expand for ls_answer? */
  445. && (lex_state == ls_none || lex_state == ls_fun_open)
  446. && !pfile->state.prevent_expansion)
  447. {
  448. /* Macros invalidate MI optimization. */
  449. pfile->mi_valid = false;
  450. if (fun_like_macro (node))
  451. {
  452. maybe_start_funlike (pfile, node, out_start, &fmacro);
  453. lex_state = ls_fun_open;
  454. fmacro.line = pfile->line_table->highest_line;
  455. continue;
  456. }
  457. else if (!recursive_macro (pfile, node))
  458. {
  459. /* Remove the object-like macro's name from the
  460. output, and push its replacement text. */
  461. pfile->out.cur = out_start;
  462. push_replacement_text (pfile, node);
  463. lex_state = ls_none;
  464. goto new_context;
  465. }
  466. }
  467. else if (macro && (node->flags & NODE_MACRO_ARG) != 0)
  468. {
  469. /* Found a parameter in the replacement text of a
  470. #define. Remove its name from the output. */
  471. pfile->out.cur = out_start;
  472. save_replacement_text (pfile, macro, node->value.arg_index);
  473. out = pfile->out.base;
  474. }
  475. else if (lex_state == ls_hash)
  476. {
  477. lex_state = ls_predicate;
  478. continue;
  479. }
  480. else if (pfile->state.in_expression
  481. && node == pfile->spec_nodes.n_defined)
  482. {
  483. lex_state = ls_defined;
  484. continue;
  485. }
  486. else if (pfile->state.in_expression
  487. && (node == pfile->spec_nodes.n__has_include__
  488. || node == pfile->spec_nodes.n__has_include_next__))
  489. {
  490. lex_state = ls_has_include;
  491. continue;
  492. }
  493. }
  494. break;
  495. case '(':
  496. if (quote == 0)
  497. {
  498. paren_depth++;
  499. if (lex_state == ls_fun_open)
  500. {
  501. if (recursive_macro (pfile, fmacro.node))
  502. lex_state = ls_none;
  503. else
  504. {
  505. lex_state = ls_fun_close;
  506. paren_depth = 1;
  507. out = pfile->out.base + fmacro.offset;
  508. fmacro.args[0] = fmacro.offset;
  509. }
  510. }
  511. else if (lex_state == ls_predicate)
  512. lex_state = ls_answer;
  513. else if (lex_state == ls_defined)
  514. lex_state = ls_defined_close;
  515. else if (lex_state == ls_has_include)
  516. lex_state = ls_has_include_close;
  517. }
  518. break;
  519. case ',':
  520. if (quote == 0 && lex_state == ls_fun_close && paren_depth == 1)
  521. save_argument (&fmacro, out - pfile->out.base);
  522. break;
  523. case ')':
  524. if (quote == 0)
  525. {
  526. paren_depth--;
  527. if (lex_state == ls_fun_close && paren_depth == 0)
  528. {
  529. if (fmacro.node->flags & NODE_BUILTIN)
  530. {
  531. /* Handle builtin function-like macros like
  532. __has_attribute. The already parsed arguments
  533. are put into a buffer, which is then preprocessed
  534. and the result is fed to _cpp_push_text_context
  535. with disabled expansion, where the ISO preprocessor
  536. parses it. While in traditional preprocessing
  537. macro arguments aren't immediately expanded, they in
  538. the end are because the macro with replaced arguments
  539. is preprocessed again. For the builtin function-like
  540. macros we need the argument immediately though,
  541. if we don't preprocess them, they would behave
  542. very differently from ISO preprocessor handling
  543. of those builtin macros. So, this handling is
  544. more similar to traditional preprocessing of
  545. #if directives, where we also keep preprocessing
  546. until everything is expanded, and then feed the
  547. result with disabled expansion to ISO preprocessor
  548. for handling the directives. */
  549. lex_state = ls_none;
  550. save_argument (&fmacro, out - pfile->out.base);
  551. cpp_macro m;
  552. memset (&m, '\0', sizeof (m));
  553. m.paramc = fmacro.paramc;
  554. if (_cpp_arguments_ok (pfile, &m, fmacro.node,
  555. fmacro.argc))
  556. {
  557. size_t len = fmacro.args[1] - fmacro.args[0];
  558. uchar *buf;
  559. /* Remove the macro's invocation from the
  560. output, and push its replacement text. */
  561. pfile->out.cur = pfile->out.base + fmacro.offset;
  562. CUR (context) = cur;
  563. buf = _cpp_unaligned_alloc (pfile, len + 2);
  564. buf[0] = '(';
  565. memcpy (buf + 1, pfile->out.base + fmacro.args[0],
  566. len);
  567. buf[len + 1] = '\n';
  568. const unsigned char *ctx_rlimit = RLIMIT (context);
  569. const unsigned char *saved_cur = pfile->buffer->cur;
  570. const unsigned char *saved_rlimit
  571. = pfile->buffer->rlimit;
  572. const unsigned char *saved_line_base
  573. = pfile->buffer->line_base;
  574. bool saved_need_line = pfile->buffer->need_line;
  575. cpp_buffer *saved_overlaid_buffer
  576. = pfile->overlaid_buffer;
  577. pfile->buffer->cur = buf;
  578. pfile->buffer->line_base = buf;
  579. pfile->buffer->rlimit = buf + len + 1;
  580. pfile->buffer->need_line = false;
  581. pfile->overlaid_buffer = pfile->buffer;
  582. bool saved_in_directive = pfile->state.in_directive;
  583. pfile->state.in_directive = true;
  584. cpp_context *saved_prev_context = context->prev;
  585. context->prev = NULL;
  586. _cpp_scan_out_logical_line (pfile, NULL, true);
  587. pfile->state.in_directive = saved_in_directive;
  588. check_output_buffer (pfile, 1);
  589. *pfile->out.cur = '\n';
  590. pfile->buffer->cur = pfile->out.base + fmacro.offset;
  591. pfile->buffer->line_base = pfile->buffer->cur;
  592. pfile->buffer->rlimit = pfile->out.cur;
  593. CUR (context) = pfile->buffer->cur;
  594. RLIMIT (context) = pfile->buffer->rlimit;
  595. pfile->state.prevent_expansion++;
  596. const uchar *text
  597. = _cpp_builtin_macro_text (pfile, fmacro.node);
  598. pfile->state.prevent_expansion--;
  599. context->prev = saved_prev_context;
  600. pfile->buffer->cur = saved_cur;
  601. pfile->buffer->rlimit = saved_rlimit;
  602. pfile->buffer->line_base = saved_line_base;
  603. pfile->buffer->need_line = saved_need_line;
  604. pfile->overlaid_buffer = saved_overlaid_buffer;
  605. pfile->out.cur = pfile->out.base + fmacro.offset;
  606. CUR (context) = cur;
  607. RLIMIT (context) = ctx_rlimit;
  608. len = ustrlen (text);
  609. buf = _cpp_unaligned_alloc (pfile, len + 1);
  610. memcpy (buf, text, len);
  611. buf[len] = '\n';
  612. text = buf;
  613. _cpp_push_text_context (pfile, fmacro.node,
  614. text, len);
  615. goto new_context;
  616. }
  617. break;
  618. }
  619. cpp_macro *m = fmacro.node->value.macro;
  620. m->used = 1;
  621. lex_state = ls_none;
  622. save_argument (&fmacro, out - pfile->out.base);
  623. /* A single zero-length argument is no argument. */
  624. if (fmacro.argc == 1
  625. && m->paramc == 0
  626. && out == pfile->out.base + fmacro.offset + 1)
  627. fmacro.argc = 0;
  628. if (_cpp_arguments_ok (pfile, m, fmacro.node, fmacro.argc))
  629. {
  630. /* Remove the macro's invocation from the
  631. output, and push its replacement text. */
  632. pfile->out.cur = pfile->out.base + fmacro.offset;
  633. CUR (context) = cur;
  634. replace_args_and_push (pfile, &fmacro);
  635. goto new_context;
  636. }
  637. }
  638. else if (lex_state == ls_answer || lex_state == ls_defined_close
  639. || lex_state == ls_has_include_close)
  640. lex_state = ls_none;
  641. }
  642. break;
  643. case '#':
  644. if (cur - 1 == start_of_input_line
  645. /* A '#' from a macro doesn't start a directive. */
  646. && !pfile->context->prev
  647. && !pfile->state.in_directive)
  648. {
  649. /* A directive. With the way _cpp_handle_directive
  650. currently works, we only want to call it if either we
  651. know the directive is OK, or we want it to fail and
  652. be removed from the output. If we want it to be
  653. passed through (the assembler case) then we must not
  654. call _cpp_handle_directive. */
  655. pfile->out.cur = out;
  656. cur = skip_whitespace (pfile, cur, true /* skip_comments */);
  657. out = pfile->out.cur;
  658. if (*cur == '\n')
  659. {
  660. /* Null directive. Ignore it and don't invalidate
  661. the MI optimization. */
  662. pfile->buffer->need_line = true;
  663. CPP_INCREMENT_LINE (pfile, 0);
  664. result = false;
  665. goto done;
  666. }
  667. else
  668. {
  669. bool do_it = false;
  670. if (is_numstart (*cur)
  671. && CPP_OPTION (pfile, lang) != CLK_ASM)
  672. do_it = true;
  673. else if (is_idstart (*cur))
  674. /* Check whether we know this directive, but don't
  675. advance. */
  676. do_it = lex_identifier (pfile, cur)->is_directive;
  677. if (do_it || CPP_OPTION (pfile, lang) != CLK_ASM)
  678. {
  679. /* This is a kludge. We want to have the ISO
  680. preprocessor lex the next token. */
  681. pfile->buffer->cur = cur;
  682. _cpp_handle_directive (pfile, false /* indented */);
  683. result = false;
  684. goto done;
  685. }
  686. }
  687. }
  688. if (pfile->state.in_expression)
  689. {
  690. lex_state = ls_hash;
  691. continue;
  692. }
  693. break;
  694. default:
  695. break;
  696. }
  697. /* Non-whitespace disables MI optimization and stops treating
  698. '<' as a quote in #include. */
  699. header_ok = false;
  700. if (!pfile->state.in_directive)
  701. pfile->mi_valid = false;
  702. if (lex_state == ls_none)
  703. continue;
  704. /* Some of these transitions of state are syntax errors. The
  705. ISO preprocessor will issue errors later. */
  706. if (lex_state == ls_fun_open)
  707. /* Missing '('. */
  708. lex_state = ls_none;
  709. else if (lex_state == ls_hash
  710. || lex_state == ls_predicate
  711. || lex_state == ls_defined
  712. || lex_state == ls_has_include)
  713. lex_state = ls_none;
  714. /* ls_answer and ls_defined_close keep going until ')'. */
  715. }
  716. done:
  717. if (fmacro.buff)
  718. _cpp_release_buff (pfile, fmacro.buff);
  719. if (lex_state == ls_fun_close)
  720. cpp_error_with_line (pfile, CPP_DL_ERROR, fmacro.line, 0,
  721. "unterminated argument list invoking macro \"%s\"",
  722. NODE_NAME (fmacro.node));
  723. return result;
  724. }
  725. /* Push a context holding the replacement text of the macro NODE on
  726. the context stack. NODE is either object-like, or a function-like
  727. macro with no arguments. */
  728. static void
  729. push_replacement_text (cpp_reader *pfile, cpp_hashnode *node)
  730. {
  731. size_t len;
  732. const uchar *text;
  733. uchar *buf;
  734. if (node->flags & NODE_BUILTIN)
  735. {
  736. text = _cpp_builtin_macro_text (pfile, node);
  737. len = ustrlen (text);
  738. buf = _cpp_unaligned_alloc (pfile, len + 1);
  739. memcpy (buf, text, len);
  740. buf[len] = '\n';
  741. text = buf;
  742. }
  743. else
  744. {
  745. cpp_macro *macro = node->value.macro;
  746. macro->used = 1;
  747. text = macro->exp.text;
  748. macro->traditional = 1;
  749. len = macro->count;
  750. }
  751. _cpp_push_text_context (pfile, node, text, len);
  752. }
  753. /* Returns TRUE if traditional macro recursion is detected. */
  754. static bool
  755. recursive_macro (cpp_reader *pfile, cpp_hashnode *node)
  756. {
  757. bool recursing = !!(node->flags & NODE_DISABLED);
  758. /* Object-like macros that are already expanding are necessarily
  759. recursive.
  760. However, it is possible to have traditional function-like macros
  761. that are not infinitely recursive but recurse to any given depth.
  762. Further, it is easy to construct examples that get ever longer
  763. until the point they stop recursing. So there is no easy way to
  764. detect true recursion; instead we assume any expansion more than
  765. 20 deep since the first invocation of this macro must be
  766. recursing. */
  767. if (recursing && fun_like_macro (node))
  768. {
  769. size_t depth = 0;
  770. cpp_context *context = pfile->context;
  771. do
  772. {
  773. depth++;
  774. if (context->c.macro == node && depth > 20)
  775. break;
  776. context = context->prev;
  777. }
  778. while (context);
  779. recursing = context != NULL;
  780. }
  781. if (recursing)
  782. cpp_error (pfile, CPP_DL_ERROR,
  783. "detected recursion whilst expanding macro \"%s\"",
  784. NODE_NAME (node));
  785. return recursing;
  786. }
  787. /* Return the length of the replacement text of a function-like or
  788. object-like non-builtin macro. */
  789. size_t
  790. _cpp_replacement_text_len (const cpp_macro *macro)
  791. {
  792. size_t len;
  793. if (macro->fun_like && (macro->paramc != 0))
  794. {
  795. const uchar *exp;
  796. len = 0;
  797. for (exp = macro->exp.text;;)
  798. {
  799. struct block *b = (struct block *) exp;
  800. len += b->text_len;
  801. if (b->arg_index == 0)
  802. break;
  803. len += NODE_LEN (macro->params[b->arg_index - 1]);
  804. exp += BLOCK_LEN (b->text_len);
  805. }
  806. }
  807. else
  808. len = macro->count;
  809. return len;
  810. }
  811. /* Copy the replacement text of MACRO to DEST, which must be of
  812. sufficient size. It is not NUL-terminated. The next character is
  813. returned. */
  814. uchar *
  815. _cpp_copy_replacement_text (const cpp_macro *macro, uchar *dest)
  816. {
  817. if (macro->fun_like && (macro->paramc != 0))
  818. {
  819. const uchar *exp;
  820. for (exp = macro->exp.text;;)
  821. {
  822. struct block *b = (struct block *) exp;
  823. cpp_hashnode *param;
  824. memcpy (dest, b->text, b->text_len);
  825. dest += b->text_len;
  826. if (b->arg_index == 0)
  827. break;
  828. param = macro->params[b->arg_index - 1];
  829. memcpy (dest, NODE_NAME (param), NODE_LEN (param));
  830. dest += NODE_LEN (param);
  831. exp += BLOCK_LEN (b->text_len);
  832. }
  833. }
  834. else
  835. {
  836. memcpy (dest, macro->exp.text, macro->count);
  837. dest += macro->count;
  838. }
  839. return dest;
  840. }
  841. /* Push a context holding the replacement text of the macro NODE on
  842. the context stack. NODE is either object-like, or a function-like
  843. macro with no arguments. */
  844. static void
  845. replace_args_and_push (cpp_reader *pfile, struct fun_macro *fmacro)
  846. {
  847. cpp_macro *macro = fmacro->node->value.macro;
  848. if (macro->paramc == 0)
  849. push_replacement_text (pfile, fmacro->node);
  850. else
  851. {
  852. const uchar *exp;
  853. uchar *p;
  854. _cpp_buff *buff;
  855. size_t len = 0;
  856. int cxtquote = 0;
  857. /* Get an estimate of the length of the argument-replaced text.
  858. This is a worst case estimate, assuming that every replacement
  859. text character needs quoting. */
  860. for (exp = macro->exp.text;;)
  861. {
  862. struct block *b = (struct block *) exp;
  863. len += b->text_len;
  864. if (b->arg_index == 0)
  865. break;
  866. len += 2 * (fmacro->args[b->arg_index]
  867. - fmacro->args[b->arg_index - 1] - 1);
  868. exp += BLOCK_LEN (b->text_len);
  869. }
  870. /* Allocate room for the expansion plus \n. */
  871. buff = _cpp_get_buff (pfile, len + 1);
  872. /* Copy the expansion and replace arguments. */
  873. /* Accumulate actual length, including quoting as necessary */
  874. p = BUFF_FRONT (buff);
  875. len = 0;
  876. for (exp = macro->exp.text;;)
  877. {
  878. struct block *b = (struct block *) exp;
  879. size_t arglen;
  880. int argquote;
  881. uchar *base;
  882. uchar *in;
  883. len += b->text_len;
  884. /* Copy the non-argument text literally, keeping
  885. track of whether matching quotes have been seen. */
  886. for (arglen = b->text_len, in = b->text; arglen > 0; arglen--)
  887. {
  888. if (*in == '"')
  889. cxtquote = ! cxtquote;
  890. *p++ = *in++;
  891. }
  892. /* Done if no more arguments */
  893. if (b->arg_index == 0)
  894. break;
  895. arglen = (fmacro->args[b->arg_index]
  896. - fmacro->args[b->arg_index - 1] - 1);
  897. base = pfile->out.base + fmacro->args[b->arg_index - 1];
  898. in = base;
  899. #if 0
  900. /* Skip leading whitespace in the text for the argument to
  901. be substituted. To be compatible with gcc 2.95, we would
  902. also need to trim trailing whitespace. Gcc 2.95 trims
  903. leading and trailing whitespace, which may be a bug. The
  904. current gcc testsuite explicitly checks that this leading
  905. and trailing whitespace in actual arguments is
  906. preserved. */
  907. while (arglen > 0 && is_space (*in))
  908. {
  909. in++;
  910. arglen--;
  911. }
  912. #endif
  913. for (argquote = 0; arglen > 0; arglen--)
  914. {
  915. if (cxtquote && *in == '"')
  916. {
  917. if (in > base && *(in-1) != '\\')
  918. argquote = ! argquote;
  919. /* Always add backslash before double quote if argument
  920. is expanded in a quoted context */
  921. *p++ = '\\';
  922. len++;
  923. }
  924. else if (cxtquote && argquote && *in == '\\')
  925. {
  926. /* Always add backslash before a backslash in an argument
  927. that is expanded in a quoted context and also in the
  928. range of a quoted context in the argument itself. */
  929. *p++ = '\\';
  930. len++;
  931. }
  932. *p++ = *in++;
  933. len++;
  934. }
  935. exp += BLOCK_LEN (b->text_len);
  936. }
  937. /* \n-terminate. */
  938. *p = '\n';
  939. _cpp_push_text_context (pfile, fmacro->node, BUFF_FRONT (buff), len);
  940. /* So we free buffer allocation when macro is left. */
  941. pfile->context->buff = buff;
  942. }
  943. }
  944. /* Read and record the parameters, if any, of a function-like macro
  945. definition. Destroys pfile->out.cur.
  946. Returns true on success, false on failure (syntax error or a
  947. duplicate parameter). On success, CUR (pfile->context) is just
  948. past the closing parenthesis. */
  949. static bool
  950. scan_parameters (cpp_reader *pfile, cpp_macro *macro)
  951. {
  952. const uchar *cur = CUR (pfile->context) + 1;
  953. bool ok;
  954. for (;;)
  955. {
  956. cur = skip_whitespace (pfile, cur, true /* skip_comments */);
  957. if (is_idstart (*cur))
  958. {
  959. struct cpp_hashnode *id = lex_identifier (pfile, cur);
  960. ok = false;
  961. if (_cpp_save_parameter (pfile, macro, id, id))
  962. break;
  963. cur = skip_whitespace (pfile, CUR (pfile->context),
  964. true /* skip_comments */);
  965. if (*cur == ',')
  966. {
  967. cur++;
  968. continue;
  969. }
  970. ok = (*cur == ')');
  971. break;
  972. }
  973. ok = (*cur == ')' && macro->paramc == 0);
  974. break;
  975. }
  976. if (!ok)
  977. cpp_error (pfile, CPP_DL_ERROR, "syntax error in macro parameter list");
  978. CUR (pfile->context) = cur + (*cur == ')');
  979. return ok;
  980. }
  981. /* Save the text from pfile->out.base to pfile->out.cur as
  982. the replacement text for the current macro, followed by argument
  983. ARG_INDEX, with zero indicating the end of the replacement
  984. text. */
  985. static void
  986. save_replacement_text (cpp_reader *pfile, cpp_macro *macro,
  987. unsigned int arg_index)
  988. {
  989. size_t len = pfile->out.cur - pfile->out.base;
  990. uchar *exp;
  991. if (macro->paramc == 0)
  992. {
  993. /* Object-like and function-like macros without parameters
  994. simply store their \n-terminated replacement text. */
  995. exp = _cpp_unaligned_alloc (pfile, len + 1);
  996. memcpy (exp, pfile->out.base, len);
  997. exp[len] = '\n';
  998. macro->exp.text = exp;
  999. macro->traditional = 1;
  1000. macro->count = len;
  1001. }
  1002. else
  1003. {
  1004. /* Store the text's length (unsigned int), the argument index
  1005. (unsigned short, base 1) and then the text. */
  1006. size_t blen = BLOCK_LEN (len);
  1007. struct block *block;
  1008. if (macro->count + blen > BUFF_ROOM (pfile->a_buff))
  1009. _cpp_extend_buff (pfile, &pfile->a_buff, macro->count + blen);
  1010. exp = BUFF_FRONT (pfile->a_buff);
  1011. block = (struct block *) (exp + macro->count);
  1012. macro->exp.text = exp;
  1013. macro->traditional = 1;
  1014. /* Write out the block information. */
  1015. block->text_len = len;
  1016. block->arg_index = arg_index;
  1017. memcpy (block->text, pfile->out.base, len);
  1018. /* Lex the rest into the start of the output buffer. */
  1019. pfile->out.cur = pfile->out.base;
  1020. macro->count += blen;
  1021. /* If we've finished, commit the memory. */
  1022. if (arg_index == 0)
  1023. BUFF_FRONT (pfile->a_buff) += macro->count;
  1024. }
  1025. }
  1026. /* Analyze and save the replacement text of a macro. Returns true on
  1027. success. */
  1028. bool
  1029. _cpp_create_trad_definition (cpp_reader *pfile, cpp_macro *macro)
  1030. {
  1031. const uchar *cur;
  1032. uchar *limit;
  1033. cpp_context *context = pfile->context;
  1034. /* The context has not been set up for command line defines, and CUR
  1035. has not been updated for the macro name for in-file defines. */
  1036. pfile->out.cur = pfile->out.base;
  1037. CUR (context) = pfile->buffer->cur;
  1038. RLIMIT (context) = pfile->buffer->rlimit;
  1039. check_output_buffer (pfile, RLIMIT (context) - CUR (context));
  1040. /* Is this a function-like macro? */
  1041. if (* CUR (context) == '(')
  1042. {
  1043. bool ok = scan_parameters (pfile, macro);
  1044. /* Remember the params so we can clear NODE_MACRO_ARG flags. */
  1045. macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
  1046. /* Setting macro to NULL indicates an error occurred, and
  1047. prevents unnecessary work in _cpp_scan_out_logical_line. */
  1048. if (!ok)
  1049. macro = NULL;
  1050. else
  1051. {
  1052. BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
  1053. macro->fun_like = 1;
  1054. }
  1055. }
  1056. /* Skip leading whitespace in the replacement text. */
  1057. pfile->buffer->cur
  1058. = skip_whitespace (pfile, CUR (context),
  1059. CPP_OPTION (pfile, discard_comments_in_macro_exp));
  1060. pfile->state.prevent_expansion++;
  1061. _cpp_scan_out_logical_line (pfile, macro, false);
  1062. pfile->state.prevent_expansion--;
  1063. if (!macro)
  1064. return false;
  1065. /* Skip trailing white space. */
  1066. cur = pfile->out.base;
  1067. limit = pfile->out.cur;
  1068. while (limit > cur && is_space (limit[-1]))
  1069. limit--;
  1070. pfile->out.cur = limit;
  1071. save_replacement_text (pfile, macro, 0);
  1072. return true;
  1073. }
  1074. /* Copy SRC of length LEN to DEST, but convert all contiguous
  1075. whitespace to a single space, provided it is not in quotes. The
  1076. quote currently in effect is pointed to by PQUOTE, and is updated
  1077. by the function. Returns the number of bytes copied. */
  1078. static size_t
  1079. canonicalize_text (uchar *dest, const uchar *src, size_t len, uchar *pquote)
  1080. {
  1081. uchar *orig_dest = dest;
  1082. uchar quote = *pquote;
  1083. while (len)
  1084. {
  1085. if (is_space (*src) && !quote)
  1086. {
  1087. do
  1088. src++, len--;
  1089. while (len && is_space (*src));
  1090. *dest++ = ' ';
  1091. }
  1092. else
  1093. {
  1094. if (*src == '\'' || *src == '"')
  1095. {
  1096. if (!quote)
  1097. quote = *src;
  1098. else if (quote == *src)
  1099. quote = 0;
  1100. }
  1101. *dest++ = *src++, len--;
  1102. }
  1103. }
  1104. *pquote = quote;
  1105. return dest - orig_dest;
  1106. }
  1107. /* Returns true if MACRO1 and MACRO2 have expansions different other
  1108. than in the form of their whitespace. */
  1109. bool
  1110. _cpp_expansions_different_trad (const cpp_macro *macro1,
  1111. const cpp_macro *macro2)
  1112. {
  1113. uchar *p1 = XNEWVEC (uchar, macro1->count + macro2->count);
  1114. uchar *p2 = p1 + macro1->count;
  1115. uchar quote1 = 0, quote2 = 0;
  1116. bool mismatch;
  1117. size_t len1, len2;
  1118. if (macro1->paramc > 0)
  1119. {
  1120. const uchar *exp1 = macro1->exp.text, *exp2 = macro2->exp.text;
  1121. mismatch = true;
  1122. for (;;)
  1123. {
  1124. struct block *b1 = (struct block *) exp1;
  1125. struct block *b2 = (struct block *) exp2;
  1126. if (b1->arg_index != b2->arg_index)
  1127. break;
  1128. len1 = canonicalize_text (p1, b1->text, b1->text_len, &quote1);
  1129. len2 = canonicalize_text (p2, b2->text, b2->text_len, &quote2);
  1130. if (len1 != len2 || memcmp (p1, p2, len1))
  1131. break;
  1132. if (b1->arg_index == 0)
  1133. {
  1134. mismatch = false;
  1135. break;
  1136. }
  1137. exp1 += BLOCK_LEN (b1->text_len);
  1138. exp2 += BLOCK_LEN (b2->text_len);
  1139. }
  1140. }
  1141. else
  1142. {
  1143. len1 = canonicalize_text (p1, macro1->exp.text, macro1->count, &quote1);
  1144. len2 = canonicalize_text (p2, macro2->exp.text, macro2->count, &quote2);
  1145. mismatch = (len1 != len2 || memcmp (p1, p2, len1));
  1146. }
  1147. free (p1);
  1148. return mismatch;
  1149. }