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