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