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