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