Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1 | /* Part of CPP library. (Macro and #define handling.) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2 | Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998, |
| 3 | 1999, 2000 Free Software Foundation, Inc. |
| 4 | Written by Per Bothner, 1994. |
| 5 | Based on CCCP program by Paul Rubin, June 1986 |
| 6 | Adapted to ANSI C, Richard Stallman, Jan 1987 |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify it |
| 9 | under the terms of the GNU General Public License as published by the |
| 10 | Free Software Foundation; either version 2, or (at your option) any |
| 11 | later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 21 | |
| 22 | In other words, you are welcome to use, share and improve this program. |
| 23 | You are forbidden to forbid anyone else to use, share and improve |
| 24 | what you give them. Help stamp out software-hoarding! */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "system.h" |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 28 | #include "intl.h" /* for _("<command line>") below. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 29 | #include "cpplib.h" |
| 30 | #include "cpphash.h" |
| 31 | |
Neil Booth | 510fbf8 | 2000-10-31 19:59:44 +0000 | [diff] [blame] | 32 | #ifndef STDC_0_IN_SYSTEM_HEADERS |
| 33 | #define STDC_0_IN_SYSTEM_HEADERS 0 /* Boolean macro. */ |
| 34 | #endif |
| 35 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 36 | struct cpp_macro |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 37 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 38 | cpp_hashnode **params; /* Parameters, if any. */ |
| 39 | cpp_token *expansion; /* First token of replacement list. */ |
| 40 | const char *file; /* Defined in file name. */ |
| 41 | unsigned int line; /* Starting line number. */ |
| 42 | unsigned int count; /* Number of tokens in expansion. */ |
| 43 | unsigned short paramc; /* Number of parameters. */ |
| 44 | unsigned int fun_like : 1; /* If a function-like macro. */ |
| 45 | unsigned int var_args : 1; /* If a variable-args macro. */ |
| 46 | unsigned int disabled : 1; /* If macro is disabled. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 49 | typedef struct macro_arg macro_arg; |
| 50 | struct macro_arg |
| 51 | { |
| 52 | cpp_token *first; /* First token in unexpanded argument. */ |
| 53 | cpp_token *expanded; /* Macro-expanded argument. */ |
| 54 | cpp_token *stringified; /* Stringified argument. */ |
| 55 | unsigned int count; /* # of tokens in argument. */ |
| 56 | unsigned int expanded_count; /* # of tokens in expanded argument. */ |
| 57 | }; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 58 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 59 | /* Macro expansion. */ |
| 60 | |
| 61 | static void lock_pools PARAMS ((cpp_reader *)); |
| 62 | static void unlock_pools PARAMS ((cpp_reader *)); |
| 63 | static int enter_macro_context PARAMS ((cpp_reader *, cpp_token *)); |
| 64 | static void builtin_macro PARAMS ((cpp_reader *, cpp_token *)); |
| 65 | static cpp_context *push_arg_context PARAMS ((cpp_reader *, macro_arg *)); |
| 66 | static enum cpp_ttype parse_arg PARAMS ((cpp_reader *, macro_arg *, int)); |
| 67 | static macro_arg *parse_args PARAMS ((cpp_reader *, const cpp_hashnode *)); |
| 68 | static cpp_context *next_context PARAMS ((cpp_reader *)); |
| 69 | static void expand_arg PARAMS ((cpp_reader *, macro_arg *)); |
| 70 | static unsigned char *quote_string PARAMS ((unsigned char *, |
| 71 | const unsigned char *, |
| 72 | unsigned int)); |
| 73 | static void make_string_token PARAMS ((cpp_pool *, cpp_token *, |
| 74 | const U_CHAR *, unsigned int)); |
| 75 | static void make_number_token PARAMS ((cpp_reader *, cpp_token *, int)); |
| 76 | static void stringify_arg PARAMS ((cpp_reader *, macro_arg *)); |
| 77 | static void paste_all_tokens PARAMS ((cpp_reader *, cpp_token *)); |
| 78 | static void paste_payloads PARAMS ((cpp_reader *, cpp_token *, |
| 79 | const cpp_token *)); |
| 80 | static int funlike_invocation_p PARAMS ((cpp_reader *, const cpp_hashnode *, |
| 81 | struct toklist *)); |
| 82 | static void replace_args PARAMS ((cpp_reader *, cpp_macro *, macro_arg *, |
| 83 | struct toklist *)); |
| 84 | |
| 85 | /* Lookaheads. */ |
| 86 | |
| 87 | static void save_lookahead_token PARAMS ((cpp_reader *, const cpp_token *)); |
| 88 | static void take_lookahead_token PARAMS ((cpp_reader *, cpp_token *)); |
| 89 | static void release_lookahead PARAMS ((cpp_reader *)); |
| 90 | static cpp_lookahead *alloc_lookahead PARAMS ((cpp_reader *)); |
| 91 | static void free_lookahead PARAMS ((cpp_lookahead *)); |
| 92 | |
| 93 | /* #define directive parsing and handling. */ |
| 94 | |
| 95 | static cpp_token *lex_expansion_token PARAMS ((cpp_reader *, cpp_macro *)); |
| 96 | static int check_macro_redefinition PARAMS ((cpp_reader *, |
| 97 | const cpp_hashnode *, |
| 98 | const cpp_macro *)); |
| 99 | static int save_parameter PARAMS ((cpp_reader *, cpp_macro *, cpp_hashnode *)); |
| 100 | static int parse_params PARAMS ((cpp_reader *, cpp_macro *)); |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 101 | static void check_trad_stringification PARAMS ((cpp_reader *, |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 102 | const cpp_macro *, |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 103 | const cpp_string *)); |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 104 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 105 | /* Allocates a buffer to hold a token's TEXT, and converts TOKEN to a |
| 106 | CPP_STRING token containing TEXT in quoted form. */ |
| 107 | static void |
| 108 | make_string_token (pool, token, text, len) |
| 109 | cpp_pool *pool; |
| 110 | cpp_token *token; |
| 111 | const U_CHAR *text; |
| 112 | unsigned int len; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 113 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 114 | U_CHAR *buf = _cpp_pool_alloc (pool, len * 4); |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 115 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 116 | token->type = CPP_STRING; |
| 117 | token->val.str.text = buf; |
| 118 | token->val.str.len = quote_string (buf, text, len) - buf; |
| 119 | token->flags = 0; |
| 120 | } |
| 121 | |
| 122 | /* Allocates and converts a temporary token to a CPP_NUMBER token, |
| 123 | evaluating to NUMBER. */ |
| 124 | static void |
| 125 | make_number_token (pfile, token, number) |
| 126 | cpp_reader *pfile; |
| 127 | cpp_token *token; |
| 128 | int number; |
| 129 | { |
| 130 | unsigned char *buf = _cpp_pool_alloc (pfile->string_pool, 20); |
| 131 | |
| 132 | sprintf ((char *) buf, "%d", number); |
| 133 | token->type = CPP_NUMBER; |
| 134 | token->val.str.text = buf; |
| 135 | token->val.str.len = ustrlen (buf); |
| 136 | token->flags = 0; |
| 137 | } |
| 138 | |
| 139 | static const char * const monthnames[] = |
| 140 | { |
| 141 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 142 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 143 | }; |
| 144 | |
| 145 | /* Handle builtin macros like __FILE__. */ |
| 146 | static void |
| 147 | builtin_macro (pfile, token) |
| 148 | cpp_reader *pfile; |
| 149 | cpp_token *token; |
| 150 | { |
| 151 | unsigned char flags = token->flags & PREV_WHITE; |
| 152 | cpp_hashnode *node = token->val.node; |
| 153 | cpp_buffer *ip; |
| 154 | |
| 155 | switch (node->value.builtin) |
| 156 | { |
| 157 | case BT_FILE: |
| 158 | case BT_BASE_FILE: |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 159 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 160 | const char *file; |
| 161 | |
| 162 | ip = CPP_BUFFER (pfile); |
| 163 | if (ip == 0) |
| 164 | file = ""; |
| 165 | else |
| 166 | { |
| 167 | if (node->value.builtin == BT_BASE_FILE) |
| 168 | while (CPP_PREV_BUFFER (ip) != NULL) |
| 169 | ip = CPP_PREV_BUFFER (ip); |
| 170 | |
| 171 | file = ip->nominal_fname; |
| 172 | } |
| 173 | make_string_token (pfile->string_pool, token, |
| 174 | (U_CHAR *) file, strlen (file)); |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 175 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 176 | break; |
| 177 | |
| 178 | case BT_INCLUDE_LEVEL: |
| 179 | /* pfile->include_depth counts the primary source as level 1, |
| 180 | but historically __INCLUDE_DEPTH__ has called the primary |
| 181 | source level 0. */ |
| 182 | make_number_token (pfile, token, pfile->include_depth - 1); |
| 183 | break; |
| 184 | |
| 185 | case BT_SPECLINE: |
| 186 | /* If __LINE__ is embedded in a macro, it must expand to the |
| 187 | line of the macro's invocation, not its definition. |
| 188 | Otherwise things like assert() will not work properly. */ |
| 189 | make_number_token (pfile, token, cpp_get_line (pfile)->line); |
| 190 | break; |
| 191 | |
| 192 | case BT_STDC: |
| 193 | { |
| 194 | int stdc = 1; |
| 195 | |
Neil Booth | 510fbf8 | 2000-10-31 19:59:44 +0000 | [diff] [blame] | 196 | if (STDC_0_IN_SYSTEM_HEADERS && CPP_IN_SYSTEM_HEADER (pfile) |
Mark Mitchell | 8a47978 | 2000-10-30 08:41:23 +0000 | [diff] [blame] | 197 | && pfile->spec_nodes.n__STRICT_ANSI__->type == NT_VOID) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 198 | stdc = 0; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 199 | make_number_token (pfile, token, stdc); |
| 200 | } |
| 201 | break; |
| 202 | |
| 203 | case BT_DATE: |
| 204 | case BT_TIME: |
| 205 | if (pfile->date.type == CPP_EOF) |
| 206 | { |
| 207 | /* Allocate __DATE__ and __TIME__ from permanent storage, |
| 208 | and save them in pfile so we don't have to do this again. |
| 209 | We don't generate these strings at init time because |
| 210 | time() and localtime() are very slow on some systems. */ |
| 211 | time_t tt = time (NULL); |
| 212 | struct tm *tb = localtime (&tt); |
| 213 | |
| 214 | make_string_token (&pfile->ident_pool, &pfile->date, |
| 215 | DSC("Oct 11 1347")); |
| 216 | make_string_token (&pfile->ident_pool, &pfile->time, |
| 217 | DSC("12:34:56")); |
| 218 | |
| 219 | sprintf ((char *) pfile->date.val.str.text, "%s %2d %4d", |
| 220 | monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900); |
| 221 | sprintf ((char *) pfile->time.val.str.text, "%02d:%02d:%02d", |
| 222 | tb->tm_hour, tb->tm_min, tb->tm_sec); |
| 223 | } |
| 224 | *token = node->value.builtin == BT_DATE ? pfile->date: pfile->time; |
| 225 | break; |
| 226 | |
| 227 | default: |
| 228 | cpp_ice (pfile, "invalid builtin macro \"%s\"", node->name); |
| 229 | break; |
| 230 | } |
| 231 | |
| 232 | token->flags = flags; |
| 233 | } |
| 234 | |
| 235 | /* Used by cpperror.c to obtain the correct line and column to report |
| 236 | in a diagnostic. */ |
| 237 | const cpp_lexer_pos * |
| 238 | cpp_get_line (pfile) |
| 239 | cpp_reader *pfile; |
| 240 | { |
| 241 | /* Within a macro expansion, return the position of the outermost |
| 242 | invocation. */ |
| 243 | if (pfile->context->prev) |
| 244 | return &pfile->macro_pos; |
| 245 | return &pfile->lexer_pos; |
| 246 | } |
| 247 | |
| 248 | static void |
| 249 | lock_pools (pfile) |
| 250 | cpp_reader *pfile; |
| 251 | { |
| 252 | _cpp_lock_pool (&pfile->temp_string_pool); |
| 253 | _cpp_lock_pool (&pfile->argument_pool); |
| 254 | } |
| 255 | |
| 256 | static void |
| 257 | unlock_pools (pfile) |
| 258 | cpp_reader *pfile; |
| 259 | { |
| 260 | _cpp_unlock_pool (&pfile->temp_string_pool); |
| 261 | _cpp_unlock_pool (&pfile->argument_pool); |
| 262 | } |
| 263 | |
| 264 | static void |
| 265 | paste_payloads (pfile, lhs, rhs) |
| 266 | cpp_reader *pfile; |
| 267 | cpp_token *lhs; |
| 268 | const cpp_token *rhs; |
| 269 | { |
| 270 | unsigned int total_len = cpp_token_len (lhs) + cpp_token_len (rhs); |
| 271 | unsigned char *result, *end; |
| 272 | cpp_pool *pool; |
| 273 | |
| 274 | pool = lhs->type == CPP_NAME ? &pfile->ident_pool: pfile->string_pool; |
| 275 | result = _cpp_pool_alloc (pool, total_len + 1); |
| 276 | |
| 277 | /* Paste the spellings and null terminate. */ |
| 278 | end = cpp_spell_token (pfile, rhs, cpp_spell_token (pfile, lhs, result)); |
| 279 | *end = '\0'; |
| 280 | total_len = end - result; |
| 281 | |
| 282 | if (lhs->type == CPP_NAME) |
| 283 | { |
| 284 | lhs->val.node = cpp_lookup (pfile, result, total_len); |
| 285 | if (lhs->val.node->flags & NODE_OPERATOR) |
| 286 | { |
| 287 | lhs->flags |= NAMED_OP; |
| 288 | lhs->type = lhs->val.node->value.operator; |
| 289 | } |
| 290 | } |
| 291 | else |
| 292 | { |
| 293 | lhs->val.str.text = result; |
| 294 | lhs->val.str.len = total_len; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /* Adds backslashes before all backslashes and double quotes appearing |
| 299 | in strings. Non-printable characters are converted to octal. */ |
| 300 | static U_CHAR * |
| 301 | quote_string (dest, src, len) |
| 302 | U_CHAR *dest; |
| 303 | const U_CHAR *src; |
| 304 | unsigned int len; |
| 305 | { |
| 306 | while (len--) |
| 307 | { |
| 308 | U_CHAR c = *src++; |
| 309 | |
| 310 | if (c == '\\' || c == '"') |
| 311 | { |
| 312 | *dest++ = '\\'; |
| 313 | *dest++ = c; |
| 314 | } |
| 315 | else |
| 316 | { |
| 317 | if (ISPRINT (c)) |
| 318 | *dest++ = c; |
| 319 | else |
| 320 | { |
| 321 | sprintf ((char *) dest, "\\%03o", c); |
| 322 | dest += 4; |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | return dest; |
| 328 | } |
| 329 | |
| 330 | /* Convert a token sequence to a single string token according to the |
| 331 | rules of the ISO C #-operator. */ |
| 332 | static void |
| 333 | stringify_arg (pfile, arg) |
| 334 | cpp_reader *pfile; |
| 335 | macro_arg *arg; |
| 336 | { |
| 337 | cpp_pool *pool = pfile->string_pool; |
| 338 | unsigned char *start = POOL_FRONT (pool); |
| 339 | unsigned int i, escape_it, total_len = 0, backslash_count = 0; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 340 | |
| 341 | /* Loop, reading in the argument's tokens. */ |
| 342 | for (i = 0; i < arg->count; i++) |
| 343 | { |
| 344 | unsigned char *dest; |
| 345 | const cpp_token *token = &arg->first[i]; |
| 346 | unsigned int len = cpp_token_len (token); |
| 347 | |
| 348 | escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING |
| 349 | || token->type == CPP_CHAR || token->type == CPP_WCHAR |
| 350 | || token->type == CPP_OSTRING); |
| 351 | |
| 352 | if (escape_it) |
| 353 | /* Worst case is each char is octal. */ |
| 354 | len *= 4; |
| 355 | len++; /* Room for initial space. */ |
| 356 | |
| 357 | dest = &start[total_len]; |
| 358 | if (dest + len > POOL_LIMIT (pool)) |
| 359 | { |
| 360 | _cpp_next_chunk (pool, len, (unsigned char **) &start); |
| 361 | dest = &start[total_len]; |
| 362 | } |
| 363 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 364 | /* No leading white space. */ |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 365 | if (token->flags & PREV_WHITE && total_len > 0) |
| 366 | *dest++ = ' '; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 367 | |
| 368 | if (escape_it) |
| 369 | { |
| 370 | unsigned char *buf = (unsigned char *) xmalloc (len); |
| 371 | |
| 372 | len = cpp_spell_token (pfile, token, buf) - buf; |
| 373 | dest = quote_string (dest, buf, len); |
| 374 | free (buf); |
| 375 | } |
| 376 | else |
| 377 | dest = cpp_spell_token (pfile, token, dest); |
| 378 | total_len = dest - start; |
| 379 | |
Neil Booth | 6c53ebf | 2000-11-06 18:43:32 +0000 | [diff] [blame] | 380 | if (token->type == CPP_OTHER && token->val.c == '\\') |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 381 | backslash_count++; |
| 382 | else |
| 383 | backslash_count = 0; |
| 384 | } |
| 385 | |
| 386 | /* Ignore the final \ of invalid string literals. */ |
| 387 | if (backslash_count & 1) |
| 388 | { |
| 389 | cpp_warning (pfile, "invalid string literal, ignoring final '\\'"); |
| 390 | total_len--; |
| 391 | } |
| 392 | |
| 393 | POOL_COMMIT (pool, total_len); |
| 394 | |
| 395 | arg->stringified = xnew (cpp_token); |
| 396 | arg->stringified->flags = 0; |
| 397 | arg->stringified->type = CPP_STRING; |
| 398 | arg->stringified->val.str.text = start; |
| 399 | arg->stringified->val.str.len = total_len; |
| 400 | } |
| 401 | |
| 402 | /* Handles an arbitrarily long sequence of ## operators. This |
| 403 | implementation is left-associative, non-recursive, and finishes a |
| 404 | paste before handling succeeding ones. If the paste fails, the |
| 405 | right hand side of the ## operator is placed in the then-current |
| 406 | context's lookahead buffer, with the effect that it appears in the |
| 407 | output stream normally. */ |
| 408 | static void |
| 409 | paste_all_tokens (pfile, lhs) |
| 410 | cpp_reader *pfile; |
| 411 | cpp_token *lhs; |
| 412 | { |
| 413 | unsigned char orig_flags = lhs->flags; |
| 414 | cpp_token *rhs; |
| 415 | |
| 416 | do |
| 417 | { |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 418 | int digraph = 0; |
| 419 | enum cpp_ttype type; |
| 420 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 421 | /* Take the token directly from the current context. We can do |
| 422 | this, because we are in the replacement list of either an |
| 423 | object-like macro, or a function-like macro with arguments |
| 424 | inserted. In either case, the constraints to #define |
| 425 | guarantee we have at least one more token (empty arguments |
| 426 | become placemarkers). */ |
| 427 | rhs = pfile->context->list.first++; |
| 428 | |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 429 | type = cpp_can_paste (pfile, lhs, rhs, &digraph); |
| 430 | if (type == CPP_EOF) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 431 | { |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 432 | if (CPP_OPTION (pfile, warn_paste)) |
| 433 | cpp_warning (pfile, |
| 434 | "pasting \"%s\" and \"%s\" does not give a valid preprocessing token", |
| 435 | cpp_token_as_text (pfile, lhs), |
| 436 | cpp_token_as_text (pfile, rhs)); |
| 437 | |
| 438 | /* The standard states that behaviour is undefined. By the |
| 439 | principle of least surpise, we step back before the RHS, |
| 440 | and mark it to prevent macro expansion. Tests in the |
| 441 | testsuite rely on clearing PREV_WHITE here, though you |
| 442 | could argue we should actually set it. */ |
| 443 | rhs->flags &= ~PREV_WHITE; |
| 444 | rhs->flags |= NO_EXPAND; |
| 445 | |
| 446 | /* Step back so we read the RHS in next. */ |
| 447 | pfile->context->list.first--; |
| 448 | break; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 449 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 450 | |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 451 | lhs->type = type; |
| 452 | lhs->flags &= ~DIGRAPH; |
| 453 | if (digraph) |
| 454 | lhs->flags |= DIGRAPH; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 455 | |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 456 | if (type == CPP_NAME || type == CPP_NUMBER) |
| 457 | paste_payloads (pfile, lhs, rhs); |
| 458 | else if (type == CPP_WCHAR || type == CPP_WSTRING) |
| 459 | lhs->val.str = rhs->val.str; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 460 | } |
| 461 | while (rhs->flags & PASTE_LEFT); |
| 462 | |
| 463 | /* The pasted token has the PREV_WHITE flag of the LHS, is no longer |
| 464 | PASTE_LEFT, and is subject to macro expansion. */ |
| 465 | lhs->flags &= ~(PREV_WHITE | PASTE_LEFT | NO_EXPAND); |
| 466 | lhs->flags |= orig_flags & PREV_WHITE; |
| 467 | } |
| 468 | |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 469 | /* Reads the unexpanded tokens of a macro argument into ARG. VAR_ARGS |
| 470 | is non-zero if this is a variable argument. Returns the type of |
| 471 | the token that caused reading to finish. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 472 | static enum cpp_ttype |
| 473 | parse_arg (pfile, arg, var_args) |
| 474 | cpp_reader *pfile; |
| 475 | struct macro_arg *arg; |
| 476 | int var_args; |
| 477 | { |
| 478 | enum cpp_ttype result; |
| 479 | unsigned int paren = 0; |
| 480 | |
| 481 | arg->first = (cpp_token *) POOL_FRONT (&pfile->argument_pool); |
| 482 | for (;; arg->count++) |
| 483 | { |
| 484 | cpp_token *token = &arg->first[arg->count]; |
| 485 | if ((unsigned char *) (token + 1) >= POOL_LIMIT (&pfile->argument_pool)) |
| 486 | { |
| 487 | _cpp_next_chunk (&pfile->argument_pool, sizeof (cpp_token), |
| 488 | (unsigned char **) &arg->first); |
| 489 | token = &arg->first[arg->count]; |
| 490 | } |
| 491 | |
| 492 | _cpp_get_token (pfile, token); |
| 493 | result = token->type; |
| 494 | |
| 495 | if (result == CPP_OPEN_PAREN) |
| 496 | paren++; |
| 497 | else if (result == CPP_CLOSE_PAREN && paren-- == 0) |
| 498 | break; |
| 499 | /* Commas are not terminators within parantheses or var_args. */ |
| 500 | else if (result == CPP_COMMA && paren == 0 && !var_args) |
| 501 | break; |
| 502 | else if (result == CPP_EOF) |
| 503 | break; /* Error reported by caller. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 506 | /* Commit the memory used to store the arguments. */ |
| 507 | POOL_COMMIT (&pfile->argument_pool, arg->count * sizeof (cpp_token)); |
| 508 | |
| 509 | return result; |
| 510 | } |
| 511 | |
| 512 | /* Parse the arguments making up a macro invocation. */ |
| 513 | static macro_arg * |
| 514 | parse_args (pfile, node) |
| 515 | cpp_reader *pfile; |
| 516 | const cpp_hashnode *node; |
| 517 | { |
| 518 | cpp_macro *macro = node->value.macro; |
| 519 | macro_arg *args, *cur; |
| 520 | enum cpp_ttype type; |
| 521 | int argc, error = 0; |
| 522 | |
| 523 | /* Allocate room for at least one argument, and zero it out. */ |
| 524 | argc = macro->paramc ? macro->paramc: 1; |
| 525 | args = xcnewvec (macro_arg, argc); |
| 526 | |
| 527 | for (cur = args, argc = 0; ;) |
| 528 | { |
| 529 | argc++; |
| 530 | |
| 531 | type = parse_arg (pfile, cur, argc == macro->paramc && macro->var_args); |
| 532 | if (type == CPP_CLOSE_PAREN || type == CPP_EOF) |
| 533 | break; |
| 534 | |
| 535 | /* Re-use the last argument for excess arguments. */ |
| 536 | if (argc < macro->paramc) |
| 537 | cur++; |
| 538 | } |
| 539 | |
| 540 | if (type == CPP_EOF) |
| 541 | { |
| 542 | cpp_error (pfile, "unterminated argument list invoking macro \"%s\"", |
| 543 | node->name); |
| 544 | error = 1; |
| 545 | } |
| 546 | else if (argc < macro->paramc) |
| 547 | { |
| 548 | /* As an extension, a rest argument is allowed to not appear in |
| 549 | the invocation at all. |
| 550 | e.g. #define debug(format, args...) something |
| 551 | debug("string"); |
| 552 | |
| 553 | This is exactly the same as if there had been an empty rest |
| 554 | argument - debug("string", ). */ |
| 555 | |
| 556 | if (argc + 1 == macro->paramc && macro->var_args) |
| 557 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 558 | if (CPP_OPTION (pfile, c99) && CPP_PEDANTIC (pfile)) |
| 559 | cpp_pedwarn (pfile, "ISO C99 requires rest arguments to be used"); |
| 560 | } |
| 561 | else |
| 562 | { |
| 563 | cpp_error (pfile, |
| 564 | "macro \"%s\" requires %u arguments, but only %u given", |
| 565 | node->name, macro->paramc, argc); |
| 566 | error = 1; |
| 567 | } |
| 568 | } |
| 569 | else if (argc > macro->paramc) |
| 570 | { |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 571 | /* Empty argument to a macro taking no arguments is OK. */ |
| 572 | if (argc != 1 || cur->count) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 573 | { |
| 574 | cpp_error (pfile, |
| 575 | "macro \"%s\" passed %u arguments, but takes just %u", |
| 576 | node->name, argc, macro->paramc); |
| 577 | error = 1; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | if (error) |
| 582 | { |
| 583 | free (args); |
| 584 | args = 0; |
| 585 | } |
| 586 | |
| 587 | return args; |
| 588 | } |
| 589 | |
| 590 | static int |
| 591 | funlike_invocation_p (pfile, node, list) |
| 592 | cpp_reader *pfile; |
| 593 | const cpp_hashnode *node; |
| 594 | struct toklist *list; |
| 595 | { |
| 596 | cpp_context *orig_context; |
| 597 | cpp_token maybe_paren; |
| 598 | macro_arg *args = 0; |
| 599 | |
| 600 | pfile->state.parsing_args = 1; |
| 601 | pfile->state.prevent_expansion++; |
| 602 | orig_context = pfile->context; |
| 603 | |
| 604 | cpp_start_lookahead (pfile); |
| 605 | cpp_get_token (pfile, &maybe_paren); |
| 606 | cpp_stop_lookahead (pfile, maybe_paren.type == CPP_OPEN_PAREN); |
| 607 | |
| 608 | if (maybe_paren.type == CPP_OPEN_PAREN) |
| 609 | args = parse_args (pfile, node); |
| 610 | else if (CPP_WTRADITIONAL (pfile)) |
| 611 | cpp_warning (pfile, |
| 612 | "function-like macro \"%s\" must be used with arguments in traditional C", |
| 613 | node->name); |
| 614 | |
| 615 | /* Restore original context. */ |
| 616 | pfile->context = orig_context; |
| 617 | pfile->state.prevent_expansion--; |
| 618 | pfile->state.parsing_args = 0; |
| 619 | |
| 620 | if (args) |
| 621 | { |
| 622 | if (node->value.macro->paramc > 0) |
| 623 | replace_args (pfile, node->value.macro, args, list); |
| 624 | free (args); |
| 625 | } |
| 626 | |
| 627 | return args != 0; |
| 628 | } |
| 629 | |
| 630 | /* Push the context of a macro onto the context stack. TOKEN is the |
| 631 | macro name. If we can successfully start expanding the macro, |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 632 | TOKEN is replaced with the first token of the expansion, and we |
| 633 | return non-zero. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 634 | static int |
| 635 | enter_macro_context (pfile, token) |
| 636 | cpp_reader *pfile; |
| 637 | cpp_token *token; |
| 638 | { |
| 639 | cpp_context *context; |
| 640 | cpp_macro *macro; |
| 641 | unsigned char flags; |
| 642 | struct toklist list; |
| 643 | |
| 644 | macro = token->val.node->value.macro; |
| 645 | if (macro->disabled) |
| 646 | { |
| 647 | token->flags |= NO_EXPAND; |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 648 | return 0; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | /* Save the position of the outermost macro invocation. */ |
| 652 | if (!pfile->context->prev) |
| 653 | { |
| 654 | pfile->macro_pos = pfile->lexer_pos; |
| 655 | lock_pools (pfile); |
| 656 | } |
| 657 | |
| 658 | if (macro->fun_like && !funlike_invocation_p (pfile, token->val.node, &list)) |
| 659 | { |
| 660 | if (!pfile->context->prev) |
| 661 | unlock_pools (pfile); |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 662 | return 0; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 665 | if (macro->paramc == 0) |
| 666 | { |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 667 | list.first = macro->expansion; |
| 668 | list.limit = macro->expansion + macro->count; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 669 | } |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 670 | |
| 671 | /* Temporary kludge. */ |
| 672 | if (list.first == list.limit) |
| 673 | return 2; |
| 674 | |
| 675 | /* Now push its context. */ |
| 676 | context = next_context (pfile); |
| 677 | context->list = list; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 678 | context->macro = macro; |
| 679 | |
| 680 | /* The first expansion token inherits the PREV_WHITE of TOKEN. */ |
| 681 | flags = token->flags & PREV_WHITE; |
| 682 | *token = *context->list.first++; |
| 683 | token->flags |= flags; |
| 684 | |
| 685 | /* Disable the macro within its expansion. */ |
| 686 | macro->disabled = 1; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 687 | |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 688 | return 1; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 691 | /* Move to the next context. Create one if there is none. */ |
| 692 | static cpp_context * |
| 693 | next_context (pfile) |
| 694 | cpp_reader *pfile; |
| 695 | { |
| 696 | cpp_context *prev = pfile->context; |
| 697 | cpp_context *result = prev->next; |
| 698 | |
| 699 | if (result == 0) |
| 700 | { |
| 701 | result = xnew (cpp_context); |
| 702 | prev->next = result; |
| 703 | result->prev = prev; |
| 704 | result->next = 0; |
| 705 | } |
| 706 | |
| 707 | pfile->context = result; |
| 708 | return result; |
| 709 | } |
| 710 | |
| 711 | static void |
| 712 | replace_args (pfile, macro, args, list) |
| 713 | cpp_reader *pfile; |
| 714 | cpp_macro *macro; |
| 715 | macro_arg *args; |
| 716 | struct toklist *list; |
| 717 | { |
| 718 | unsigned int i, total; |
| 719 | const cpp_token *src, *limit; |
| 720 | cpp_token *dest; |
| 721 | macro_arg *arg; |
| 722 | |
| 723 | src = macro->expansion; |
| 724 | limit = src + macro->count; |
| 725 | |
| 726 | /* First, fully macro-expand arguments, calculating the number of |
| 727 | tokens in the final expansion as we go. This ensures that the |
| 728 | possible recursive use of argument_pool is fine. */ |
| 729 | total = limit - src; |
| 730 | for (; src < limit; src++) |
| 731 | if (src->type == CPP_MACRO_ARG) |
| 732 | { |
| 733 | /* We have an argument. If it is not being stringified or |
| 734 | pasted it is macro-replaced before insertion. */ |
Neil Booth | 6c53ebf | 2000-11-06 18:43:32 +0000 | [diff] [blame] | 735 | arg = &args[src->val.arg_no - 1]; |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 736 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 737 | if (src->flags & STRINGIFY_ARG) |
| 738 | { |
| 739 | if (!arg->stringified) |
| 740 | stringify_arg (pfile, arg); |
| 741 | } |
| 742 | else if ((src->flags & PASTE_LEFT) |
| 743 | || (src > macro->expansion && (src[-1].flags & PASTE_LEFT))) |
| 744 | total += arg->count - 1; |
| 745 | else |
| 746 | { |
| 747 | if (!arg->expanded) |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 748 | { |
| 749 | arg->expanded_count = 0; |
| 750 | if (arg->count) |
| 751 | expand_arg (pfile, arg); |
| 752 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 753 | total += arg->expanded_count - 1; |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | dest = (cpp_token *) _cpp_pool_alloc (&pfile->argument_pool, |
| 758 | total * sizeof (cpp_token)); |
| 759 | list->first = dest; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 760 | |
| 761 | for (src = macro->expansion; src < limit; src++) |
| 762 | if (src->type == CPP_MACRO_ARG) |
| 763 | { |
| 764 | unsigned int count; |
| 765 | const cpp_token *from; |
| 766 | |
Neil Booth | 6c53ebf | 2000-11-06 18:43:32 +0000 | [diff] [blame] | 767 | arg = &args[src->val.arg_no - 1]; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 768 | if (src->flags & STRINGIFY_ARG) |
| 769 | from = arg->stringified, count = 1; |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 770 | else if (src->flags & PASTE_LEFT) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 771 | count = arg->count, from = arg->first; |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 772 | else if (src > macro->expansion && (src[-1].flags & PASTE_LEFT)) |
| 773 | { |
| 774 | count = arg->count, from = arg->first; |
| 775 | if (dest != list->first) |
| 776 | { |
| 777 | /* GCC has special semantics for , ## b where b is a |
| 778 | varargs parameter: the comma disappears if b was |
| 779 | given no actual arguments (not merely if b is an |
| 780 | empty argument); otherwise pasting is turned off. */ |
| 781 | if (dest[-1].type == CPP_COMMA |
| 782 | && macro->var_args |
| 783 | && src->val.arg_no == macro->paramc) |
| 784 | { |
| 785 | if (count == 0) |
| 786 | dest--; |
| 787 | else |
| 788 | dest[-1].flags &= ~PASTE_LEFT; |
| 789 | } |
| 790 | /* Count == 0 is the RHS a placemarker case. */ |
| 791 | else if (count == 0) |
| 792 | dest[-1].flags &= ~PASTE_LEFT; |
| 793 | } |
| 794 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 795 | else |
| 796 | count = arg->expanded_count, from = arg->expanded; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 797 | |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 798 | /* Count == 0 is the LHS a placemarker case. */ |
| 799 | if (count) |
| 800 | { |
| 801 | memcpy (dest, from, count * sizeof (cpp_token)); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 802 | |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 803 | /* The first token gets PREV_WHITE of the CPP_MACRO_ARG. */ |
| 804 | dest->flags &= ~PREV_WHITE; |
| 805 | dest->flags |= src->flags & PREV_WHITE; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 806 | |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 807 | /* The last token gets the PASTE_LEFT of the CPP_MACRO_ARG. */ |
| 808 | dest[count - 1].flags |= src->flags & PASTE_LEFT; |
| 809 | |
| 810 | dest += count; |
| 811 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 812 | } |
| 813 | else |
| 814 | *dest++ = *src; |
| 815 | |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 816 | list->limit = dest; |
| 817 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 818 | /* Free the expanded arguments. */ |
| 819 | for (i = 0; i < macro->paramc; i++) |
| 820 | { |
| 821 | if (args[i].expanded) |
| 822 | free (args[i].expanded); |
| 823 | if (args[i].stringified) |
| 824 | free (args[i].stringified); |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | /* Subroutine of expand_arg to put the unexpanded tokens on the |
| 829 | context stack. */ |
| 830 | static cpp_context * |
| 831 | push_arg_context (pfile, arg) |
| 832 | cpp_reader *pfile; |
| 833 | macro_arg *arg; |
| 834 | { |
| 835 | cpp_context *context = next_context (pfile); |
| 836 | context->macro = 0; |
| 837 | context->list.first = arg->first; |
| 838 | context->list.limit = arg->first + arg->count; |
| 839 | |
| 840 | return context; |
| 841 | } |
| 842 | |
| 843 | static void |
| 844 | expand_arg (pfile, arg) |
| 845 | cpp_reader *pfile; |
| 846 | macro_arg *arg; |
| 847 | { |
| 848 | cpp_token *token; |
| 849 | unsigned int capacity = 256; |
| 850 | |
| 851 | /* Loop, reading in the arguments. */ |
| 852 | arg->expanded = (cpp_token *) xmalloc (capacity * sizeof (cpp_token)); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 853 | |
| 854 | push_arg_context (pfile, arg); |
| 855 | do |
| 856 | { |
| 857 | if (arg->expanded_count >= capacity) |
| 858 | { |
| 859 | capacity *= 2; |
| 860 | arg->expanded = (cpp_token *) |
| 861 | xrealloc (arg->expanded, capacity * sizeof (cpp_token)); |
| 862 | } |
| 863 | token = &arg->expanded[arg->expanded_count++]; |
| 864 | _cpp_get_token (pfile, token); |
| 865 | } |
| 866 | while (token->type != CPP_EOF); |
| 867 | |
| 868 | arg->expanded_count--; |
| 869 | |
| 870 | /* Pop the context we pushed. */ |
| 871 | pfile->context = pfile->context->prev; |
| 872 | } |
| 873 | |
| 874 | void |
| 875 | _cpp_pop_context (pfile) |
| 876 | cpp_reader *pfile; |
| 877 | { |
| 878 | cpp_context *context = pfile->context; |
| 879 | |
| 880 | pfile->context = context->prev; |
| 881 | /* Re-enable a macro and free resources when leaving its expansion. */ |
| 882 | if (!pfile->state.parsing_args) |
| 883 | { |
| 884 | if (!pfile->context->prev) |
| 885 | unlock_pools (pfile); |
| 886 | context->macro->disabled = 0; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | /* Internal routine to return a token, either from an in-progress |
Neil Booth | a949941 | 2000-11-09 21:18:15 +0000 | [diff] [blame] | 891 | macro expansion, or from the source file as appropriate. |
| 892 | Transparently enters included files. Handles macros, so tokens |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 893 | returned are post-expansion. Returns CPP_EOF at EOL and EOF. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 894 | void |
| 895 | _cpp_get_token (pfile, token) |
| 896 | cpp_reader *pfile; |
| 897 | cpp_token *token; |
| 898 | { |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 899 | next_token: |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 900 | for (;;) |
| 901 | { |
| 902 | cpp_context *context = pfile->context; |
| 903 | |
| 904 | if (pfile->la_read) |
| 905 | take_lookahead_token (pfile, token); |
| 906 | /* Context->prev == 0 <=> base context. */ |
| 907 | else if (!context->prev) |
| 908 | _cpp_lex_token (pfile, token); |
| 909 | else if (context->list.first != context->list.limit) |
| 910 | *token = *context->list.first++; |
| 911 | else |
| 912 | { |
| 913 | if (context->macro) |
| 914 | { |
| 915 | _cpp_pop_context (pfile); |
| 916 | continue; |
| 917 | } |
Neil Booth | a949941 | 2000-11-09 21:18:15 +0000 | [diff] [blame] | 918 | /* End of argument pre-expansion. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 919 | token->type = CPP_EOF; |
| 920 | token->flags = 0; |
| 921 | } |
| 922 | break; |
| 923 | } |
| 924 | |
| 925 | /* Only perform macro expansion (and therefore pasting) when not |
| 926 | skipping, or when skipping but in a directive. The only |
| 927 | directive where this could be true is #elif. A possible later |
| 928 | optimisation: get do_elif to clear skipping so we don't need the |
| 929 | directive test here. */ |
| 930 | if (pfile->skipping && !pfile->state.in_directive) |
| 931 | return; |
| 932 | |
| 933 | for (;;) |
| 934 | { |
| 935 | if (token->flags & PASTE_LEFT) |
| 936 | paste_all_tokens (pfile, token); |
| 937 | |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 938 | if (token->type != CPP_NAME) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 939 | break; |
| 940 | |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 941 | /* Handle macros and the _Pragma operator. */ |
| 942 | if (token->val.node->type == NT_MACRO |
| 943 | && !pfile->state.prevent_expansion |
| 944 | && !(token->flags & NO_EXPAND)) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 945 | { |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 946 | int m; |
| 947 | |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 948 | /* Macros invalidate controlling macros. */ |
| 949 | pfile->mi_state = MI_FAILED; |
| 950 | |
| 951 | if (token->val.node->flags & NODE_BUILTIN) |
| 952 | { |
| 953 | builtin_macro (pfile, token); |
| 954 | break; |
| 955 | } |
| 956 | |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 957 | m = enter_macro_context (pfile, token); |
| 958 | if (m == 1) |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 959 | continue; |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 960 | if (m == 2) |
| 961 | goto next_token; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 962 | } |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 963 | |
| 964 | if (token->val.node != pfile->spec_nodes.n__Pragma) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 965 | break; |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 966 | |
| 967 | /* Invalidate controlling macros. */ |
| 968 | pfile->mi_state = MI_FAILED; |
| 969 | _cpp_do__Pragma (pfile); |
| 970 | goto next_token; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | |
| 974 | /* External interface to get a token. Tokens are returned after macro |
| 975 | expansion and directives have been handled, as a continuous stream. |
Neil Booth | a949941 | 2000-11-09 21:18:15 +0000 | [diff] [blame] | 976 | Compared to the function above, CPP_EOF means EOF, and placemarker |
| 977 | tokens are filtered out. Also, it skips tokens if we're skipping, |
| 978 | and saves tokens to lookahead. |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 979 | |
Neil Booth | a949941 | 2000-11-09 21:18:15 +0000 | [diff] [blame] | 980 | CPP_EOF indicates end of original source file. For the benefit of |
| 981 | #pragma callbacks which may want to get the pragma's tokens, |
| 982 | returns CPP_EOF to indicate end-of-directive in this case. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 983 | void |
| 984 | cpp_get_token (pfile, token) |
| 985 | cpp_reader *pfile; |
| 986 | cpp_token *token; |
| 987 | { |
| 988 | for (;;) |
| 989 | { |
| 990 | _cpp_get_token (pfile, token); |
| 991 | |
| 992 | if (token->type == CPP_EOF) |
Neil Booth | a949941 | 2000-11-09 21:18:15 +0000 | [diff] [blame] | 993 | break; |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 994 | else if (pfile->skipping) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 995 | continue; |
| 996 | |
| 997 | /* Non-comment tokens invalidate any controlling macros. */ |
| 998 | if (token->type != CPP_COMMENT) |
| 999 | pfile->mi_state = MI_FAILED; |
| 1000 | |
| 1001 | break; |
| 1002 | } |
| 1003 | |
| 1004 | if (pfile->la_write) |
| 1005 | save_lookahead_token (pfile, token); |
| 1006 | } |
| 1007 | |
| 1008 | /* Read each token in, until EOF. Directives are transparently |
| 1009 | processed. */ |
| 1010 | void |
| 1011 | cpp_scan_buffer_nooutput (pfile) |
| 1012 | cpp_reader *pfile; |
| 1013 | { |
| 1014 | cpp_token token; |
| 1015 | |
| 1016 | do |
| 1017 | do |
| 1018 | cpp_get_token (pfile, &token); |
| 1019 | while (token.type != CPP_EOF); |
| 1020 | while (cpp_pop_buffer (pfile) != 0); |
| 1021 | } |
| 1022 | |
| 1023 | /* Lookahead handling. */ |
| 1024 | |
| 1025 | static void |
| 1026 | save_lookahead_token (pfile, token) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1027 | cpp_reader *pfile; |
| 1028 | const cpp_token *token; |
| 1029 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1030 | if (token->type != CPP_EOF) |
| 1031 | { |
| 1032 | cpp_lookahead *la = pfile->la_write; |
| 1033 | cpp_token_with_pos *twp; |
| 1034 | |
| 1035 | if (la->count == la->cap) |
| 1036 | { |
| 1037 | la->cap += la->cap + 8; |
| 1038 | la->tokens = (cpp_token_with_pos *) |
| 1039 | xrealloc (la->tokens, la->cap * sizeof (cpp_token_with_pos)); |
| 1040 | } |
| 1041 | |
| 1042 | twp = &la->tokens[la->count++]; |
| 1043 | twp->token = *token; |
| 1044 | twp->pos = *cpp_get_line (pfile); |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | static void |
| 1049 | take_lookahead_token (pfile, token) |
| 1050 | cpp_reader *pfile; |
| 1051 | cpp_token *token; |
| 1052 | { |
| 1053 | cpp_lookahead *la = pfile->la_read; |
| 1054 | cpp_token_with_pos *twp = &la->tokens[la->cur]; |
| 1055 | |
| 1056 | *token = twp->token; |
| 1057 | pfile->lexer_pos = twp->pos; |
| 1058 | |
| 1059 | if (++la->cur == la->count) |
| 1060 | release_lookahead (pfile); |
| 1061 | } |
| 1062 | |
| 1063 | /* Moves the lookahead at the front of the read list to the free store. */ |
| 1064 | static void |
| 1065 | release_lookahead (pfile) |
| 1066 | cpp_reader *pfile; |
| 1067 | { |
| 1068 | cpp_lookahead *la = pfile->la_read; |
| 1069 | |
| 1070 | pfile->la_read = la->next; |
| 1071 | la->next = pfile->la_unused; |
| 1072 | pfile->la_unused = la; |
| 1073 | unlock_pools (pfile); |
| 1074 | } |
| 1075 | |
| 1076 | /* Take a new lookahead from the free store, or allocate one if none. */ |
| 1077 | static cpp_lookahead * |
| 1078 | alloc_lookahead (pfile) |
| 1079 | cpp_reader *pfile; |
| 1080 | { |
| 1081 | cpp_lookahead *la = pfile->la_unused; |
| 1082 | |
| 1083 | if (la) |
| 1084 | pfile->la_unused = la->next; |
| 1085 | else |
| 1086 | { |
| 1087 | la = xnew (cpp_lookahead); |
| 1088 | la->tokens = 0; |
| 1089 | la->cap = 0; |
| 1090 | } |
| 1091 | |
| 1092 | la->cur = la->count = 0; |
| 1093 | return la; |
| 1094 | } |
| 1095 | |
| 1096 | /* Free memory associated with a lookahead list. */ |
| 1097 | static void |
| 1098 | free_lookahead (la) |
| 1099 | cpp_lookahead *la; |
| 1100 | { |
| 1101 | if (la->tokens) |
| 1102 | free ((PTR) la->tokens); |
| 1103 | free ((PTR) la); |
| 1104 | } |
| 1105 | |
| 1106 | /* Free all the lookaheads of a cpp_reader. */ |
| 1107 | void |
| 1108 | _cpp_free_lookaheads (pfile) |
| 1109 | cpp_reader *pfile; |
| 1110 | { |
| 1111 | cpp_lookahead *la, *lan; |
| 1112 | |
| 1113 | if (pfile->la_read) |
| 1114 | free_lookahead (pfile->la_read); |
| 1115 | if (pfile->la_write) |
| 1116 | free_lookahead (pfile->la_write); |
| 1117 | |
| 1118 | for (la = pfile->la_unused; la; la = lan) |
| 1119 | { |
| 1120 | lan = la->next; |
| 1121 | free_lookahead (la); |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | /* Allocate a lookahead and move it to the front of the write list. */ |
| 1126 | void |
| 1127 | cpp_start_lookahead (pfile) |
| 1128 | cpp_reader *pfile; |
| 1129 | { |
| 1130 | cpp_lookahead *la = alloc_lookahead (pfile); |
| 1131 | |
| 1132 | la->next = pfile->la_write; |
| 1133 | pfile->la_write = la; |
| 1134 | |
| 1135 | la->pos = *cpp_get_line (pfile); |
| 1136 | |
| 1137 | /* Don't allow memory pools to be re-used whilst we're reading ahead. */ |
| 1138 | lock_pools (pfile); |
| 1139 | } |
| 1140 | |
| 1141 | /* Stop reading ahead - either step back, or drop the read ahead. */ |
| 1142 | void |
| 1143 | cpp_stop_lookahead (pfile, drop) |
| 1144 | cpp_reader *pfile; |
| 1145 | int drop; |
| 1146 | { |
| 1147 | cpp_lookahead *la = pfile->la_write; |
| 1148 | |
| 1149 | pfile->la_write = la->next; |
| 1150 | la->next = pfile->la_read; |
| 1151 | pfile->la_read = la; |
| 1152 | |
| 1153 | if (drop || la->count == 0) |
| 1154 | release_lookahead (pfile); |
| 1155 | else |
| 1156 | pfile->lexer_pos = la->pos; |
| 1157 | } |
| 1158 | |
| 1159 | /* Push a single token back to the front of the queue. Only to be |
| 1160 | used by cpplib, and only then when necessary. POS is the position |
| 1161 | to report for the preceding token. */ |
| 1162 | void |
| 1163 | _cpp_push_token (pfile, token, pos) |
| 1164 | cpp_reader *pfile; |
| 1165 | const cpp_token *token; |
| 1166 | const cpp_lexer_pos *pos; |
| 1167 | { |
| 1168 | cpp_start_lookahead (pfile); |
| 1169 | save_lookahead_token (pfile, token); |
| 1170 | cpp_stop_lookahead (pfile, 0); |
| 1171 | pfile->lexer_pos = *pos; |
| 1172 | } |
| 1173 | |
| 1174 | /* #define directive parsing and handling. */ |
| 1175 | |
| 1176 | /* Returns non-zero if a macro redefinition is trivial. */ |
| 1177 | static int |
| 1178 | check_macro_redefinition (pfile, node, macro2) |
| 1179 | cpp_reader *pfile; |
| 1180 | const cpp_hashnode *node; |
| 1181 | const cpp_macro *macro2; |
| 1182 | { |
| 1183 | const cpp_macro *macro1; |
| 1184 | unsigned int i; |
| 1185 | |
| 1186 | if (node->type != NT_MACRO || node->flags & NODE_BUILTIN) |
| 1187 | return ! pfile->done_initializing; |
| 1188 | |
| 1189 | macro1 = node->value.macro; |
| 1190 | |
| 1191 | /* The quick failures. */ |
| 1192 | if (macro1->count != macro2->count |
| 1193 | || macro1->paramc != macro2->paramc |
| 1194 | || macro1->fun_like != macro2->fun_like |
| 1195 | || macro1->var_args != macro2->var_args) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1196 | return 0; |
| 1197 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1198 | /* Check each token. */ |
| 1199 | for (i = 0; i < macro1->count; i++) |
| 1200 | if (! _cpp_equiv_tokens (¯o1->expansion[i], ¯o2->expansion[i])) |
| 1201 | return 0; |
| 1202 | |
| 1203 | /* Check parameter spellings. */ |
| 1204 | for (i = 0; i < macro1->paramc; i++) |
| 1205 | if (macro1->params[i] != macro2->params[i]) |
| 1206 | return 0; |
| 1207 | |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1208 | return 1; |
| 1209 | } |
| 1210 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1211 | /* Free the definition of hashnode H. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1212 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1213 | void |
| 1214 | _cpp_free_definition (h) |
| 1215 | cpp_hashnode *h; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1216 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1217 | /* Macros and assertions no longer have anything to free. */ |
| 1218 | h->type = NT_VOID; |
| 1219 | /* Clear builtin flag in case of redefinition. */ |
| 1220 | h->flags &= ~NODE_BUILTIN; |
| 1221 | } |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1222 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1223 | static int |
| 1224 | save_parameter (pfile, macro, node) |
| 1225 | cpp_reader *pfile; |
| 1226 | cpp_macro *macro; |
| 1227 | cpp_hashnode *node; |
| 1228 | { |
| 1229 | cpp_hashnode **dest; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1230 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1231 | /* Constraint 6.10.3.6 - duplicate parameter names. */ |
| 1232 | if (node->arg_index) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1233 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1234 | cpp_error (pfile, "duplicate macro parameter \"%s\"", node->name); |
| 1235 | return 1; |
| 1236 | } |
| 1237 | |
| 1238 | dest = ¯o->params[macro->paramc]; |
| 1239 | |
| 1240 | /* Check we have room for the parameters. */ |
| 1241 | if ((unsigned char *) (dest + 1) >= POOL_LIMIT (&pfile->macro_pool)) |
| 1242 | { |
| 1243 | _cpp_next_chunk (&pfile->macro_pool, sizeof (cpp_hashnode *), |
| 1244 | (unsigned char **) ¯o->params); |
| 1245 | dest = ¯o->params[macro->paramc]; |
| 1246 | } |
| 1247 | |
| 1248 | *dest = node; |
| 1249 | node->arg_index = ++macro->paramc; |
| 1250 | return 0; |
| 1251 | } |
| 1252 | |
| 1253 | static int |
| 1254 | parse_params (pfile, macro) |
| 1255 | cpp_reader *pfile; |
| 1256 | cpp_macro *macro; |
| 1257 | { |
| 1258 | cpp_token token; |
| 1259 | unsigned int prev_ident = 0; |
| 1260 | |
| 1261 | macro->params = (cpp_hashnode **) POOL_FRONT (&pfile->macro_pool); |
| 1262 | for (;;) |
| 1263 | { |
| 1264 | _cpp_lex_token (pfile, &token); |
| 1265 | |
| 1266 | switch (token.type) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1267 | { |
| 1268 | default: |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1269 | cpp_error (pfile, "\"%s\" may not appear in macro parameter list", |
| 1270 | cpp_token_as_text (pfile, &token)); |
| 1271 | return 0; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1272 | |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1273 | case CPP_NAME: |
| 1274 | if (prev_ident) |
| 1275 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1276 | cpp_error (pfile, "macro parameters must be comma-separated"); |
| 1277 | return 0; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1278 | } |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1279 | prev_ident = 1; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1280 | |
| 1281 | if (save_parameter (pfile, macro, token.val.node)) |
| 1282 | return 0; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1283 | continue; |
| 1284 | |
| 1285 | case CPP_CLOSE_PAREN: |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1286 | if (prev_ident || macro->paramc == 0) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1287 | break; |
| 1288 | |
| 1289 | /* Fall through to pick up the error. */ |
| 1290 | case CPP_COMMA: |
| 1291 | if (!prev_ident) |
| 1292 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1293 | cpp_error (pfile, "parameter name missing"); |
| 1294 | return 0; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1295 | } |
| 1296 | prev_ident = 0; |
| 1297 | continue; |
| 1298 | |
| 1299 | case CPP_ELLIPSIS: |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1300 | macro->var_args = 1; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1301 | if (!prev_ident) |
| 1302 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1303 | save_parameter (pfile, macro, pfile->spec_nodes.n__VA_ARGS__); |
| 1304 | pfile->state.va_args_ok = 1; |
| 1305 | if (! CPP_OPTION (pfile, c99) && CPP_OPTION (pfile, pedantic)) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1306 | cpp_pedwarn (pfile, |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1307 | "C89 does not permit anonymous variable arguments"); |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1308 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1309 | else if (CPP_OPTION (pfile, pedantic)) |
| 1310 | cpp_pedwarn (pfile, |
| 1311 | "ISO C does not permit named variable arguments"); |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1312 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1313 | /* We're at the end, and just expect a closing parenthesis. */ |
| 1314 | _cpp_lex_token (pfile, &token); |
| 1315 | if (token.type == CPP_CLOSE_PAREN) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1316 | break; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1317 | /* Fall through. */ |
| 1318 | |
| 1319 | case CPP_EOF: |
| 1320 | cpp_error (pfile, "missing ')' in macro parameter list"); |
| 1321 | return 0; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1324 | /* Success. Commit the parameter array. */ |
| 1325 | POOL_COMMIT (&pfile->macro_pool, |
| 1326 | macro->paramc * sizeof (cpp_hashnode *)); |
| 1327 | return 1; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1328 | } |
| 1329 | } |
| 1330 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1331 | /* Lex a token from a macro's replacement list. Translate it to a |
| 1332 | CPP_MACRO_ARG if appropriate. */ |
| 1333 | static cpp_token * |
| 1334 | lex_expansion_token (pfile, macro) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1335 | cpp_reader *pfile; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1336 | cpp_macro *macro; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1337 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1338 | cpp_token *token = ¯o->expansion[macro->count]; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1339 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1340 | /* Check we have room for the token. */ |
| 1341 | if ((unsigned char *) (token + 1) >= POOL_LIMIT (&pfile->macro_pool)) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1342 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1343 | _cpp_next_chunk (&pfile->macro_pool, sizeof (cpp_token), |
| 1344 | (unsigned char **) ¯o->expansion); |
| 1345 | token = ¯o->expansion[macro->count]; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1348 | macro->count++; |
| 1349 | _cpp_lex_token (pfile, token); |
| 1350 | |
| 1351 | /* Is this an argument? */ |
| 1352 | if (token->type == CPP_NAME && token->val.node->arg_index) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1353 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1354 | token->type = CPP_MACRO_ARG; |
Neil Booth | 6c53ebf | 2000-11-06 18:43:32 +0000 | [diff] [blame] | 1355 | token->val.arg_no = token->val.node->arg_index; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1356 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1357 | else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0 |
| 1358 | && (token->type == CPP_STRING || token->type == CPP_CHAR)) |
| 1359 | check_trad_stringification (pfile, macro, &token->val.str); |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1360 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1361 | return token; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | /* Parse a macro and save its expansion. Returns non-zero on success. */ |
| 1365 | int |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1366 | _cpp_create_definition (pfile, node) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1367 | cpp_reader *pfile; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1368 | cpp_hashnode *node; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1369 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1370 | cpp_macro *macro; |
| 1371 | cpp_token *token; |
| 1372 | unsigned int i, ok = 1; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1373 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1374 | macro = (cpp_macro *) _cpp_pool_alloc (&pfile->macro_pool, |
| 1375 | sizeof (cpp_macro)); |
| 1376 | macro->file = pfile->buffer->nominal_fname; |
| 1377 | macro->line = pfile->directive_pos.line; |
| 1378 | macro->params = 0; |
| 1379 | macro->paramc = 0; |
| 1380 | macro->fun_like = 0; |
| 1381 | macro->var_args = 0; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1382 | macro->count = 0; |
| 1383 | macro->expansion = (cpp_token *) POOL_FRONT (&pfile->macro_pool); |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1384 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1385 | /* Get the first token of the expansion (or the '(' of a |
| 1386 | function-like macro). */ |
| 1387 | token = lex_expansion_token (pfile, macro); |
| 1388 | if (token->type == CPP_OPEN_PAREN && !(token->flags & PREV_WHITE)) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1389 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1390 | if (!(ok = parse_params (pfile, macro))) |
| 1391 | goto cleanup; |
| 1392 | macro->count = 0; |
| 1393 | macro->fun_like = 1; |
| 1394 | /* Some of the pool may have been used for the parameter store. */ |
| 1395 | macro->expansion = (cpp_token *) POOL_FRONT (&pfile->macro_pool); |
| 1396 | token = lex_expansion_token (pfile, macro); |
| 1397 | } |
| 1398 | else if (token->type != CPP_EOF && !(token->flags & PREV_WHITE)) |
| 1399 | cpp_pedwarn (pfile, "ISO C requires whitespace after the macro name"); |
| 1400 | |
| 1401 | /* Setting it here means we don't catch leading comments. */ |
| 1402 | pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments); |
| 1403 | |
| 1404 | for (;;) |
| 1405 | { |
| 1406 | /* Check the stringifying # constraint 6.10.3.2.1 of |
| 1407 | function-like macros when lexing the subsequent token. */ |
| 1408 | if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1409 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1410 | if (token->type == CPP_MACRO_ARG) |
| 1411 | { |
| 1412 | token->flags &= ~PREV_WHITE; |
| 1413 | token->flags |= STRINGIFY_ARG; |
| 1414 | token->flags |= token[-1].flags & PREV_WHITE; |
| 1415 | token[-1] = token[0]; |
| 1416 | macro->count--; |
| 1417 | } |
| 1418 | /* Let assembler get away with murder. */ |
| 1419 | else if (!CPP_OPTION (pfile, lang_asm)) |
| 1420 | { |
| 1421 | ok = 0; |
| 1422 | cpp_error (pfile, "'#' is not followed by a macro parameter"); |
| 1423 | goto cleanup; |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | if (token->type == CPP_EOF) |
| 1428 | break; |
| 1429 | |
| 1430 | /* Paste operator constraint 6.10.3.3.1. */ |
| 1431 | if (token->type == CPP_PASTE) |
| 1432 | { |
| 1433 | /* Token-paste ##, can appear in both object-like and |
| 1434 | function-like macros, but not at the ends. */ |
| 1435 | if (--macro->count > 0) |
| 1436 | token = lex_expansion_token (pfile, macro); |
| 1437 | |
| 1438 | if (macro->count == 0 || token->type == CPP_EOF) |
| 1439 | { |
| 1440 | ok = 0; |
| 1441 | cpp_error (pfile, |
| 1442 | "'##' cannot appear at either end of a macro expansion"); |
| 1443 | goto cleanup; |
| 1444 | } |
| 1445 | |
| 1446 | token[-1].flags |= PASTE_LEFT; |
| 1447 | /* Give it a PREV_WHITE for -dM etc. */ |
| 1448 | token->flags |= PREV_WHITE; |
| 1449 | } |
| 1450 | |
| 1451 | token = lex_expansion_token (pfile, macro); |
| 1452 | } |
| 1453 | |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 1454 | /* Don't count the CPP_EOF. */ |
| 1455 | macro->count--; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1456 | |
| 1457 | /* Clear the whitespace flag from the leading token. */ |
| 1458 | macro->expansion[0].flags &= ~PREV_WHITE; |
| 1459 | |
Neil Booth | 44ed91a | 2000-10-29 11:37:18 +0000 | [diff] [blame] | 1460 | /* Implement the macro-defined-to-itself optimisation. */ |
| 1461 | macro->disabled = (macro->count == 1 && !macro->fun_like |
| 1462 | && macro->expansion[0].type == CPP_NAME |
| 1463 | && macro->expansion[0].val.node == node); |
| 1464 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1465 | /* Commit the memory. */ |
| 1466 | POOL_COMMIT (&pfile->macro_pool, macro->count * sizeof (cpp_token)); |
| 1467 | |
| 1468 | /* Redefinition of a macro is allowed if and only if the old and new |
| 1469 | definitions are the same. (6.10.3 paragraph 2). */ |
| 1470 | if (node->type != NT_VOID) |
| 1471 | { |
| 1472 | if (CPP_PEDANTIC (pfile) |
| 1473 | && !check_macro_redefinition (pfile, node, macro)) |
| 1474 | { |
| 1475 | cpp_pedwarn_with_line (pfile, pfile->directive_pos.line, |
| 1476 | pfile->directive_pos.col, |
| 1477 | "\"%s\" redefined", node->name); |
| 1478 | |
| 1479 | if (pfile->done_initializing && node->type == NT_MACRO |
| 1480 | && !(node->flags & NODE_BUILTIN)) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1481 | cpp_pedwarn_with_file_and_line (pfile, |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1482 | node->value.macro->file, |
| 1483 | node->value.macro->line, 1, |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1484 | "this is the location of the previous definition"); |
| 1485 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1486 | _cpp_free_definition (node); |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | /* Enter definition in hash table. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1490 | node->type = NT_MACRO; |
| 1491 | node->value.macro = macro; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1492 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1493 | cleanup: |
| 1494 | |
| 1495 | /* Stop the lexer accepting __VA_ARGS__. */ |
| 1496 | pfile->state.va_args_ok = 0; |
| 1497 | |
| 1498 | /* Clear the fast argument lookup indices. */ |
| 1499 | for (i = macro->paramc; i-- > 0; ) |
| 1500 | macro->params[i]->arg_index = 0; |
| 1501 | |
| 1502 | return ok; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 1505 | /* Warn if a token in `string' matches one of the function macro |
| 1506 | arguments in `info'. This function assumes that the macro is a |
| 1507 | function macro and not an object macro. */ |
| 1508 | static void |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1509 | check_trad_stringification (pfile, macro, string) |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 1510 | cpp_reader *pfile; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1511 | const cpp_macro *macro; |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 1512 | const cpp_string *string; |
| 1513 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1514 | unsigned int i, len; |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 1515 | const U_CHAR *p, *q, *limit = string->text + string->len; |
| 1516 | |
| 1517 | /* Loop over the string. */ |
| 1518 | for (p = string->text; p < limit; p = q) |
| 1519 | { |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 1520 | /* Find the start of an identifier. */ |
Greg McGary | 61c16b1 | 2000-09-15 21:25:02 +0000 | [diff] [blame] | 1521 | while (p < limit && !is_idstart (*p)) |
| 1522 | p++; |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 1523 | |
| 1524 | /* Find the end of the identifier. */ |
| 1525 | q = p; |
Greg McGary | 61c16b1 | 2000-09-15 21:25:02 +0000 | [diff] [blame] | 1526 | while (q < limit && is_idchar (*q)) |
| 1527 | q++; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1528 | |
| 1529 | len = q - p; |
| 1530 | |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 1531 | /* Loop over the function macro arguments to see if the |
| 1532 | identifier inside the string matches one of them. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1533 | for (i = 0; i < macro->paramc; i++) |
| 1534 | { |
| 1535 | const cpp_hashnode *node = macro->params[i]; |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 1536 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1537 | if (node->length == len && !memcmp (p, node->name, len)) |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 1538 | { |
| 1539 | cpp_warning (pfile, |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1540 | "macro argument \"%s\" would be stringified with -traditional.", |
| 1541 | node->name); |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 1542 | break; |
| 1543 | } |
| 1544 | } |
| 1545 | } |
| 1546 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1547 | |
| 1548 | /* Returns the expansion of a macro, in a format suitable to be read |
| 1549 | back in again, and therefore also for DWARF 2 debugging info. |
| 1550 | Caller is expected to generate the "#define NAME" bit. The |
| 1551 | returned text is temporary, and automatically freed later. */ |
| 1552 | |
| 1553 | const unsigned char * |
| 1554 | cpp_macro_definition (pfile, node) |
| 1555 | cpp_reader *pfile; |
| 1556 | const cpp_hashnode *node; |
| 1557 | { |
| 1558 | unsigned int i, len; |
| 1559 | const cpp_macro *macro = node->value.macro; |
| 1560 | unsigned char *buffer; |
| 1561 | |
| 1562 | if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN)) |
| 1563 | { |
| 1564 | cpp_ice (pfile, "invalid hash type %d in dump_definition", node->type); |
| 1565 | return 0; |
| 1566 | } |
| 1567 | |
| 1568 | /* Calculate length. */ |
| 1569 | len = 1; /* ' ' */ |
| 1570 | if (macro->fun_like) |
| 1571 | { |
| 1572 | len += 3; /* "()" plus possible final "." of ellipsis. */ |
| 1573 | for (i = 0; i < macro->paramc; i++) |
| 1574 | len += macro->params[i]->length + 2; /* ", " */ |
| 1575 | } |
| 1576 | |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 1577 | for (i = 0; i < macro->count; i++) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1578 | { |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 1579 | cpp_token *token = ¯o->expansion[i]; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1580 | |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 1581 | if (token->type == CPP_MACRO_ARG) |
| 1582 | len += macro->params[token->val.arg_no - 1]->length; |
| 1583 | else |
| 1584 | len += cpp_token_len (token); /* Includes room for ' '. */ |
| 1585 | if (token->flags & STRINGIFY_ARG) |
| 1586 | len++; /* "#" */ |
| 1587 | if (token->flags & PASTE_LEFT) |
| 1588 | len += 3; /* " ##" */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | if (len > pfile->macro_buffer_len) |
| 1592 | pfile->macro_buffer = (U_CHAR *) xrealloc (pfile->macro_buffer, len); |
| 1593 | buffer = pfile->macro_buffer; |
| 1594 | |
| 1595 | /* Parameter names. */ |
| 1596 | if (macro->fun_like) |
| 1597 | { |
| 1598 | *buffer++ = '('; |
| 1599 | for (i = 0; i < macro->paramc; i++) |
| 1600 | { |
| 1601 | cpp_hashnode *param = macro->params[i]; |
| 1602 | |
| 1603 | if (param != pfile->spec_nodes.n__VA_ARGS__) |
| 1604 | { |
| 1605 | memcpy (buffer, param->name, param->length); |
| 1606 | buffer += param->length; |
| 1607 | } |
| 1608 | |
| 1609 | if (i + 1 < macro->paramc) |
| 1610 | *buffer++ = ',', *buffer++ = ' '; |
| 1611 | else if (macro->var_args) |
| 1612 | *buffer++ = '.', *buffer++ = '.', *buffer++ = '.'; |
| 1613 | } |
| 1614 | *buffer++ = ')'; |
| 1615 | } |
| 1616 | |
| 1617 | /* Expansion tokens. */ |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame^] | 1618 | if (macro->count) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1619 | { |
| 1620 | *buffer++ = ' '; |
| 1621 | for (i = 0; i < macro->count; i++) |
| 1622 | { |
| 1623 | cpp_token *token = ¯o->expansion[i]; |
| 1624 | |
| 1625 | if (token->flags & PREV_WHITE) |
| 1626 | *buffer++ = ' '; |
| 1627 | if (token->flags & STRINGIFY_ARG) |
| 1628 | *buffer++ = '#'; |
| 1629 | |
| 1630 | if (token->type == CPP_MACRO_ARG) |
| 1631 | { |
Neil Booth | 6c53ebf | 2000-11-06 18:43:32 +0000 | [diff] [blame] | 1632 | len = macro->params[token->val.arg_no - 1]->length; |
| 1633 | memcpy (buffer, macro->params[token->val.arg_no - 1]->name, len); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1634 | buffer += len; |
| 1635 | } |
| 1636 | else |
| 1637 | buffer = cpp_spell_token (pfile, token, buffer); |
| 1638 | |
| 1639 | if (token->flags & PASTE_LEFT) |
| 1640 | { |
| 1641 | *buffer++ = ' '; |
| 1642 | *buffer++ = '#'; |
| 1643 | *buffer++ = '#'; |
| 1644 | /* Next has PREV_WHITE; see _cpp_create_definition. */ |
| 1645 | } |
| 1646 | } |
| 1647 | } |
| 1648 | |
| 1649 | *buffer = '\0'; |
| 1650 | return pfile->macro_buffer; |
| 1651 | } |