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