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, |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 3 | 1999, 2000, 2001, 2002 Free Software Foundation, Inc. |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 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" |
| 28 | #include "cpplib.h" |
| 29 | #include "cpphash.h" |
| 30 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 31 | struct cpp_macro |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 32 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 33 | cpp_hashnode **params; /* Parameters, if any. */ |
Kazu Hirata | 6d2f888 | 2001-10-10 11:33:39 +0000 | [diff] [blame] | 34 | cpp_token *expansion; /* First token of replacement list. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 35 | unsigned int line; /* Starting line number. */ |
| 36 | unsigned int count; /* Number of tokens in expansion. */ |
| 37 | unsigned short paramc; /* Number of parameters. */ |
| 38 | unsigned int fun_like : 1; /* If a function-like macro. */ |
Neil Booth | 28e0f04 | 2000-12-09 12:06:37 +0000 | [diff] [blame] | 39 | unsigned int variadic : 1; /* If a variadic macro. */ |
Neil Booth | 7065e13 | 2001-02-14 07:38:20 +0000 | [diff] [blame] | 40 | unsigned int syshdr : 1; /* If macro defined in system header. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 43 | typedef struct macro_arg macro_arg; |
| 44 | struct macro_arg |
| 45 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 46 | const cpp_token **first; /* First token in unexpanded argument. */ |
Kazu Hirata | 6d2f888 | 2001-10-10 11:33:39 +0000 | [diff] [blame] | 47 | const cpp_token **expanded; /* Macro-expanded argument. */ |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 48 | const cpp_token *stringified; /* Stringified argument. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 49 | unsigned int count; /* # of tokens in argument. */ |
| 50 | unsigned int expanded_count; /* # of tokens in expanded argument. */ |
| 51 | }; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 52 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 53 | /* Macro expansion. */ |
| 54 | |
Neil Booth | 29b1074 | 2000-11-13 18:40:37 +0000 | [diff] [blame] | 55 | static int enter_macro_context PARAMS ((cpp_reader *, cpp_hashnode *)); |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 56 | static int builtin_macro PARAMS ((cpp_reader *, cpp_hashnode *)); |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 57 | static void push_token_context |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 58 | PARAMS ((cpp_reader *, cpp_hashnode *, const cpp_token *, unsigned int)); |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 59 | static void push_ptoken_context |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 60 | PARAMS ((cpp_reader *, cpp_hashnode *, _cpp_buff *, |
Neil Booth | 1e013d2 | 2001-09-26 21:44:35 +0000 | [diff] [blame] | 61 | const cpp_token **, unsigned int)); |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 62 | static _cpp_buff *collect_args PARAMS ((cpp_reader *, const cpp_hashnode *)); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 63 | static cpp_context *next_context PARAMS ((cpp_reader *)); |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 64 | static const cpp_token *padding_token |
| 65 | PARAMS ((cpp_reader *, const cpp_token *)); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 66 | static void expand_arg PARAMS ((cpp_reader *, macro_arg *)); |
Neil Booth | 562a5c2 | 2002-04-21 18:46:42 +0000 | [diff] [blame] | 67 | static const cpp_token *new_string_token PARAMS ((cpp_reader *, uchar *, |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 68 | unsigned int)); |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 69 | static const cpp_token *new_number_token PARAMS ((cpp_reader *, unsigned int)); |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 70 | static const cpp_token *stringify_arg PARAMS ((cpp_reader *, macro_arg *)); |
| 71 | static void paste_all_tokens PARAMS ((cpp_reader *, const cpp_token *)); |
Neil Booth | c9e7a60 | 2001-09-27 12:59:38 +0000 | [diff] [blame] | 72 | static bool paste_tokens PARAMS ((cpp_reader *, const cpp_token **, |
| 73 | const cpp_token *)); |
Neil Booth | e808ec9 | 2002-02-27 07:24:53 +0000 | [diff] [blame] | 74 | static void replace_args PARAMS ((cpp_reader *, cpp_hashnode *, cpp_macro *, |
| 75 | macro_arg *)); |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 76 | static _cpp_buff *funlike_invocation_p PARAMS ((cpp_reader *, cpp_hashnode *)); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 77 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 78 | /* #define directive parsing and handling. */ |
| 79 | |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 80 | static cpp_token *alloc_expansion_token PARAMS ((cpp_reader *, cpp_macro *)); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 81 | static cpp_token *lex_expansion_token PARAMS ((cpp_reader *, cpp_macro *)); |
Neil Booth | 4d6baaf | 2001-11-26 23:44:54 +0000 | [diff] [blame] | 82 | static int warn_of_redefinition PARAMS ((const cpp_hashnode *, |
Neil Booth | 618cdda | 2001-02-25 09:43:03 +0000 | [diff] [blame] | 83 | const cpp_macro *)); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 84 | static int save_parameter PARAMS ((cpp_reader *, cpp_macro *, cpp_hashnode *)); |
| 85 | static int parse_params PARAMS ((cpp_reader *, cpp_macro *)); |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 86 | static void check_trad_stringification PARAMS ((cpp_reader *, |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 87 | const cpp_macro *, |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 88 | const cpp_string *)); |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 89 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 90 | /* Allocates and returns a CPP_STRING token, containing TEXT of length |
| 91 | LEN, after null-terminating it. TEXT must be in permanent storage. */ |
| 92 | static const cpp_token * |
| 93 | new_string_token (pfile, text, len) |
| 94 | cpp_reader *pfile; |
| 95 | unsigned char *text; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 96 | unsigned int len; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 97 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 98 | cpp_token *token = _cpp_temp_token (pfile); |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 99 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 100 | text[len] = '\0'; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 101 | token->type = CPP_STRING; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 102 | token->val.str.len = len; |
| 103 | token->val.str.text = text; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 104 | token->flags = 0; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 105 | return token; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 108 | /* Allocates and returns a CPP_NUMBER token evaluating to NUMBER. */ |
| 109 | static const cpp_token * |
| 110 | new_number_token (pfile, number) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 111 | cpp_reader *pfile; |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 112 | unsigned int number; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 113 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 114 | cpp_token *token = _cpp_temp_token (pfile); |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 115 | /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */ |
| 116 | unsigned char *buf = _cpp_unaligned_alloc (pfile, 21); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 117 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 118 | sprintf ((char *) buf, "%u", number); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 119 | token->type = CPP_NUMBER; |
| 120 | token->val.str.text = buf; |
| 121 | token->val.str.len = ustrlen (buf); |
| 122 | token->flags = 0; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 123 | return token; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static const char * const monthnames[] = |
| 127 | { |
| 128 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 129 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 130 | }; |
| 131 | |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 132 | /* Handle builtin macros like __FILE__, and push the resulting token |
| 133 | on the context stack. Also handles _Pragma, for which no new token |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 134 | is created. Returns 1 if it generates a new token context, 0 to |
| 135 | return the token to the caller. */ |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 136 | static int |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 137 | builtin_macro (pfile, node) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 138 | cpp_reader *pfile; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 139 | cpp_hashnode *node; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 140 | { |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 141 | const cpp_token *result; |
| 142 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 143 | switch (node->value.builtin) |
| 144 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 145 | default: |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 146 | cpp_error (pfile, DL_ICE, "invalid built-in macro \"%s\"", |
| 147 | NODE_NAME (node)); |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 148 | return 0; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 149 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 150 | case BT_FILE: |
| 151 | case BT_BASE_FILE: |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 152 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 153 | unsigned int len; |
Neil Booth | 0bda476 | 2000-12-11 07:45:16 +0000 | [diff] [blame] | 154 | const char *name; |
Neil Booth | 562a5c2 | 2002-04-21 18:46:42 +0000 | [diff] [blame] | 155 | uchar *buf; |
Neil Booth | bb74c96 | 2001-08-17 22:23:49 +0000 | [diff] [blame] | 156 | const struct line_map *map = pfile->map; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 157 | |
Neil Booth | 0bda476 | 2000-12-11 07:45:16 +0000 | [diff] [blame] | 158 | if (node->value.builtin == BT_BASE_FILE) |
Neil Booth | bb74c96 | 2001-08-17 22:23:49 +0000 | [diff] [blame] | 159 | while (! MAIN_FILE_P (map)) |
| 160 | map = INCLUDED_FROM (&pfile->line_maps, map); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 161 | |
Neil Booth | bb74c96 | 2001-08-17 22:23:49 +0000 | [diff] [blame] | 162 | name = map->to_file; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 163 | len = strlen (name); |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 164 | buf = _cpp_unaligned_alloc (pfile, len * 4 + 1); |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 165 | len = cpp_quote_string (buf, (const unsigned char *) name, len) - buf; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 166 | |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 167 | result = new_string_token (pfile, buf, len); |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 168 | } |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 169 | break; |
| 170 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 171 | case BT_INCLUDE_LEVEL: |
Neil Booth | d8693c6 | 2001-08-21 23:05:12 +0000 | [diff] [blame] | 172 | /* The line map depth counts the primary source as level 1, but |
| 173 | historically __INCLUDE_DEPTH__ has called the primary source |
| 174 | level 0. */ |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 175 | result = new_number_token (pfile, pfile->line_maps.depth - 1); |
| 176 | break; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 177 | |
| 178 | case BT_SPECLINE: |
| 179 | /* If __LINE__ is embedded in a macro, it must expand to the |
| 180 | line of the macro's invocation, not its definition. |
| 181 | Otherwise things like assert() will not work properly. */ |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 182 | result = new_number_token (pfile, |
| 183 | SOURCE_LINE (pfile->map, |
| 184 | pfile->cur_token[-1].line)); |
| 185 | break; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 186 | |
| 187 | case BT_STDC: |
| 188 | { |
Neil Booth | 618cdda | 2001-02-25 09:43:03 +0000 | [diff] [blame] | 189 | int stdc = (!CPP_IN_SYSTEM_HEADER (pfile) |
| 190 | || pfile->spec_nodes.n__STRICT_ANSI__->type != NT_VOID); |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 191 | result = new_number_token (pfile, stdc); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 192 | } |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 193 | break; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 194 | |
| 195 | case BT_DATE: |
| 196 | case BT_TIME: |
| 197 | if (pfile->date.type == CPP_EOF) |
| 198 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 199 | /* Allocate __DATE__ and __TIME__ strings from permanent |
| 200 | storage. We only do this once, and don't generate them |
| 201 | at init time, because time() and localtime() are very |
| 202 | slow on some systems. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 203 | time_t tt = time (NULL); |
| 204 | struct tm *tb = localtime (&tt); |
| 205 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 206 | pfile->date.val.str.text = |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 207 | _cpp_unaligned_alloc (pfile, sizeof ("Oct 11 1347")); |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 208 | pfile->date.val.str.len = sizeof ("Oct 11 1347") - 1; |
| 209 | pfile->date.type = CPP_STRING; |
| 210 | pfile->date.flags = 0; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 211 | sprintf ((char *) pfile->date.val.str.text, "%s %2d %4d", |
| 212 | monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900); |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 213 | |
| 214 | pfile->time.val.str.text = |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 215 | _cpp_unaligned_alloc (pfile, sizeof ("12:34:56")); |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 216 | pfile->time.val.str.len = sizeof ("12:34:56") - 1; |
| 217 | pfile->time.type = CPP_STRING; |
| 218 | pfile->time.flags = 0; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 219 | sprintf ((char *) pfile->time.val.str.text, "%02d:%02d:%02d", |
| 220 | tb->tm_hour, tb->tm_min, tb->tm_sec); |
| 221 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 222 | |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 223 | if (node->value.builtin == BT_DATE) |
| 224 | result = &pfile->date; |
| 225 | else |
| 226 | result = &pfile->time; |
| 227 | break; |
| 228 | |
| 229 | case BT_PRAGMA: |
| 230 | /* Don't interpret _Pragma within directives. The standard is |
| 231 | not clear on this, but to me this makes most sense. */ |
| 232 | if (pfile->state.in_directive) |
| 233 | return 0; |
| 234 | |
| 235 | _cpp_do__Pragma (pfile); |
| 236 | return 1; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 237 | } |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 238 | |
| 239 | push_token_context (pfile, NULL, result, 1); |
| 240 | return 1; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 243 | /* Copies SRC, of length LEN, to DEST, adding backslashes before all |
| 244 | backslashes and double quotes. Non-printable characters are |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 245 | converted to octal. DEST must be of sufficient size. Returns |
| 246 | a pointer to the end of the string. */ |
Neil Booth | 562a5c2 | 2002-04-21 18:46:42 +0000 | [diff] [blame] | 247 | uchar * |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 248 | cpp_quote_string (dest, src, len) |
Neil Booth | 562a5c2 | 2002-04-21 18:46:42 +0000 | [diff] [blame] | 249 | uchar *dest; |
| 250 | const uchar *src; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 251 | unsigned int len; |
| 252 | { |
| 253 | while (len--) |
| 254 | { |
Neil Booth | 562a5c2 | 2002-04-21 18:46:42 +0000 | [diff] [blame] | 255 | uchar c = *src++; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 256 | |
| 257 | if (c == '\\' || c == '"') |
| 258 | { |
| 259 | *dest++ = '\\'; |
| 260 | *dest++ = c; |
| 261 | } |
| 262 | else |
| 263 | { |
| 264 | if (ISPRINT (c)) |
| 265 | *dest++ = c; |
| 266 | else |
| 267 | { |
| 268 | sprintf ((char *) dest, "\\%03o", c); |
| 269 | dest += 4; |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | return dest; |
| 275 | } |
| 276 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 277 | /* Convert a token sequence ARG to a single string token according to |
| 278 | the rules of the ISO C #-operator. */ |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 279 | static const cpp_token * |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 280 | stringify_arg (pfile, arg) |
| 281 | cpp_reader *pfile; |
| 282 | macro_arg *arg; |
| 283 | { |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 284 | unsigned char *dest = BUFF_FRONT (pfile->u_buff); |
| 285 | unsigned int i, escape_it, backslash_count = 0; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 286 | const cpp_token *source = NULL; |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 287 | size_t len; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 288 | |
| 289 | /* Loop, reading in the argument's tokens. */ |
| 290 | for (i = 0; i < arg->count; i++) |
| 291 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 292 | const cpp_token *token = arg->first[i]; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 293 | |
| 294 | if (token->type == CPP_PADDING) |
| 295 | { |
| 296 | if (source == NULL) |
| 297 | source = token->val.source; |
| 298 | continue; |
| 299 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 300 | |
| 301 | escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING |
Zack Weinberg | cc93758 | 2001-03-07 01:32:01 +0000 | [diff] [blame] | 302 | || token->type == CPP_CHAR || token->type == CPP_WCHAR); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 303 | |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 304 | /* Room for each char being written in octal, initial space and |
| 305 | final NUL. */ |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 306 | len = cpp_token_len (token); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 307 | if (escape_it) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 308 | len *= 4; |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 309 | len += 2; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 310 | |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 311 | if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 312 | { |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 313 | size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff); |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 314 | _cpp_extend_buff (pfile, &pfile->u_buff, len); |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 315 | dest = BUFF_FRONT (pfile->u_buff) + len_so_far; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 318 | /* Leading white space? */ |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 319 | if (dest != BUFF_FRONT (pfile->u_buff)) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 320 | { |
| 321 | if (source == NULL) |
| 322 | source = token; |
| 323 | if (source->flags & PREV_WHITE) |
| 324 | *dest++ = ' '; |
| 325 | } |
| 326 | source = NULL; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 327 | |
| 328 | if (escape_it) |
| 329 | { |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 330 | _cpp_buff *buff = _cpp_get_buff (pfile, len); |
| 331 | unsigned char *buf = BUFF_FRONT (buff); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 332 | len = cpp_spell_token (pfile, token, buf) - buf; |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 333 | dest = cpp_quote_string (dest, buf, len); |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 334 | _cpp_release_buff (pfile, buff); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 335 | } |
| 336 | else |
| 337 | dest = cpp_spell_token (pfile, token, dest); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 338 | |
Neil Booth | 6c53ebf | 2000-11-06 18:43:32 +0000 | [diff] [blame] | 339 | if (token->type == CPP_OTHER && token->val.c == '\\') |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 340 | backslash_count++; |
| 341 | else |
| 342 | backslash_count = 0; |
| 343 | } |
| 344 | |
| 345 | /* Ignore the final \ of invalid string literals. */ |
| 346 | if (backslash_count & 1) |
| 347 | { |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 348 | cpp_error (pfile, DL_WARNING, |
| 349 | "invalid string literal, ignoring final '\\'"); |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 350 | dest--; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 353 | /* Commit the memory, including NUL, and return the token. */ |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 354 | len = dest - BUFF_FRONT (pfile->u_buff); |
| 355 | BUFF_FRONT (pfile->u_buff) = dest + 1; |
| 356 | return new_string_token (pfile, dest - len, len); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Neil Booth | c9e7a60 | 2001-09-27 12:59:38 +0000 | [diff] [blame] | 359 | /* Try to paste two tokens. On success, return non-zero. In any |
| 360 | case, PLHS is updated to point to the pasted token, which is |
| 361 | guaranteed to not have the PASTE_LEFT flag set. */ |
| 362 | static bool |
| 363 | paste_tokens (pfile, plhs, rhs) |
Neil Booth | d63eefb | 2000-11-20 23:59:26 +0000 | [diff] [blame] | 364 | cpp_reader *pfile; |
Neil Booth | c9e7a60 | 2001-09-27 12:59:38 +0000 | [diff] [blame] | 365 | const cpp_token **plhs, *rhs; |
Neil Booth | d63eefb | 2000-11-20 23:59:26 +0000 | [diff] [blame] | 366 | { |
Neil Booth | c9e7a60 | 2001-09-27 12:59:38 +0000 | [diff] [blame] | 367 | unsigned char *buf, *end; |
| 368 | const cpp_token *lhs; |
| 369 | unsigned int len; |
| 370 | bool valid; |
Neil Booth | d63eefb | 2000-11-20 23:59:26 +0000 | [diff] [blame] | 371 | |
Neil Booth | c9e7a60 | 2001-09-27 12:59:38 +0000 | [diff] [blame] | 372 | lhs = *plhs; |
| 373 | len = cpp_token_len (lhs) + cpp_token_len (rhs) + 1; |
| 374 | buf = (unsigned char *) alloca (len); |
| 375 | end = cpp_spell_token (pfile, lhs, buf); |
Neil Booth | d63eefb | 2000-11-20 23:59:26 +0000 | [diff] [blame] | 376 | |
Neil Booth | c9e7a60 | 2001-09-27 12:59:38 +0000 | [diff] [blame] | 377 | /* Avoid comment headers, since they are still processed in stage 3. |
| 378 | It is simpler to insert a space here, rather than modifying the |
| 379 | lexer to ignore comments in some circumstances. Simply returning |
| 380 | false doesn't work, since we want to clear the PASTE_LEFT flag. */ |
| 381 | if (lhs->type == CPP_DIV |
| 382 | && (rhs->type == CPP_MULT || rhs->type == CPP_DIV)) |
| 383 | *end++ = ' '; |
| 384 | end = cpp_spell_token (pfile, rhs, end); |
Neil Booth | 4d6baaf | 2001-11-26 23:44:54 +0000 | [diff] [blame] | 385 | *end = '\0'; |
Neil Booth | d63eefb | 2000-11-20 23:59:26 +0000 | [diff] [blame] | 386 | |
Neil Booth | c9e7a60 | 2001-09-27 12:59:38 +0000 | [diff] [blame] | 387 | cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true, 1); |
Neil Booth | d63eefb | 2000-11-20 23:59:26 +0000 | [diff] [blame] | 388 | |
Neil Booth | c9e7a60 | 2001-09-27 12:59:38 +0000 | [diff] [blame] | 389 | /* Tweak the column number the lexer will report. */ |
| 390 | pfile->buffer->col_adjust = pfile->cur_token[-1].col - 1; |
Neil Booth | d63eefb | 2000-11-20 23:59:26 +0000 | [diff] [blame] | 391 | |
Neil Booth | c9e7a60 | 2001-09-27 12:59:38 +0000 | [diff] [blame] | 392 | /* We don't want a leading # to be interpreted as a directive. */ |
| 393 | pfile->buffer->saved_flags = 0; |
Neil Booth | d63eefb | 2000-11-20 23:59:26 +0000 | [diff] [blame] | 394 | |
Neil Booth | c9e7a60 | 2001-09-27 12:59:38 +0000 | [diff] [blame] | 395 | /* Set pfile->cur_token as required by _cpp_lex_direct. */ |
| 396 | pfile->cur_token = _cpp_temp_token (pfile); |
| 397 | *plhs = _cpp_lex_direct (pfile); |
Neil Booth | 480709c | 2001-10-21 14:04:42 +0000 | [diff] [blame] | 398 | valid = pfile->buffer->cur == pfile->buffer->rlimit; |
Neil Booth | c9e7a60 | 2001-09-27 12:59:38 +0000 | [diff] [blame] | 399 | _cpp_pop_buffer (pfile); |
Neil Booth | d63eefb | 2000-11-20 23:59:26 +0000 | [diff] [blame] | 400 | |
Neil Booth | c9e7a60 | 2001-09-27 12:59:38 +0000 | [diff] [blame] | 401 | return valid; |
Neil Booth | d63eefb | 2000-11-20 23:59:26 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 404 | /* Handles an arbitrarily long sequence of ## operators, with initial |
| 405 | operand LHS. This implementation is left-associative, |
| 406 | non-recursive, and finishes a paste before handling succeeding |
| 407 | ones. If a paste fails, we back up to the RHS of the failing ## |
| 408 | operator before pushing the context containing the result of prior |
| 409 | successful pastes, with the effect that the RHS appears in the |
| 410 | output stream after the pasted LHS normally. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 411 | static void |
| 412 | paste_all_tokens (pfile, lhs) |
| 413 | cpp_reader *pfile; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 414 | const cpp_token *lhs; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 415 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 416 | const cpp_token *rhs; |
| 417 | cpp_context *context = pfile->context; |
| 418 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 419 | do |
| 420 | { |
| 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 |
Neil Booth | d63eefb | 2000-11-20 23:59:26 +0000 | [diff] [blame] | 425 | guarantee we have at least one more token. */ |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 426 | if (context->direct_p) |
| 427 | rhs = context->first.token++; |
| 428 | else |
| 429 | rhs = *context->first.ptoken++; |
| 430 | |
| 431 | if (rhs->type == CPP_PADDING) |
| 432 | abort (); |
| 433 | |
Neil Booth | c9e7a60 | 2001-09-27 12:59:38 +0000 | [diff] [blame] | 434 | if (!paste_tokens (pfile, &lhs, rhs)) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 435 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 436 | _cpp_backup_tokens (pfile, 1); |
Neil Booth | c9e7a60 | 2001-09-27 12:59:38 +0000 | [diff] [blame] | 437 | |
| 438 | /* Mandatory warning for all apart from assembler. */ |
| 439 | if (CPP_OPTION (pfile, lang) != CLK_ASM) |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 440 | cpp_error (pfile, DL_WARNING, |
Neil Booth | c9e7a60 | 2001-09-27 12:59:38 +0000 | [diff] [blame] | 441 | "pasting \"%s\" and \"%s\" does not give a valid preprocessing token", |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 442 | cpp_token_as_text (pfile, lhs), |
| 443 | cpp_token_as_text (pfile, rhs)); |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame] | 444 | break; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 445 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 446 | } |
| 447 | while (rhs->flags & PASTE_LEFT); |
| 448 | |
Neil Booth | c9e7a60 | 2001-09-27 12:59:38 +0000 | [diff] [blame] | 449 | /* Put the resulting token in its own context. */ |
| 450 | push_token_context (pfile, NULL, lhs, 1); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 453 | /* Reads and returns the arguments to a function-like macro |
| 454 | invocation. Assumes the opening parenthesis has been processed. |
| 455 | If there is an error, emits an appropriate diagnostic and returns |
| 456 | NULL. Each argument is terminated by a CPP_EOF token, for the |
| 457 | future benefit of expand_arg(). */ |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 458 | static _cpp_buff * |
| 459 | collect_args (pfile, node) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 460 | cpp_reader *pfile; |
| 461 | const cpp_hashnode *node; |
| 462 | { |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 463 | _cpp_buff *buff, *base_buff; |
| 464 | cpp_macro *macro; |
| 465 | macro_arg *args, *arg; |
| 466 | const cpp_token *token; |
| 467 | unsigned int argc; |
| 468 | bool error = false; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 469 | |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 470 | macro = node->value.macro; |
| 471 | if (macro->paramc) |
| 472 | argc = macro->paramc; |
| 473 | else |
| 474 | argc = 1; |
| 475 | buff = _cpp_get_buff (pfile, argc * (50 * sizeof (cpp_token *) |
| 476 | + sizeof (macro_arg))); |
| 477 | base_buff = buff; |
| 478 | args = (macro_arg *) buff->base; |
| 479 | memset (args, 0, argc * sizeof (macro_arg)); |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 480 | buff->cur = (unsigned char *) &args[argc]; |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 481 | arg = args, argc = 0; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 482 | |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 483 | /* Collect the tokens making up each argument. We don't yet know |
| 484 | how many arguments have been supplied, whether too many or too |
| 485 | few. Hence the slightly bizarre usage of "argc" and "arg". */ |
| 486 | do |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 487 | { |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 488 | unsigned int paren_depth = 0; |
| 489 | unsigned int ntokens = 0; |
| 490 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 491 | argc++; |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 492 | arg->first = (const cpp_token **) buff->cur; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 493 | |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 494 | for (;;) |
| 495 | { |
| 496 | /* Require space for 2 new tokens (including a CPP_EOF). */ |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 497 | if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit) |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 498 | { |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 499 | buff = _cpp_append_extend_buff (pfile, buff, |
| 500 | 1000 * sizeof (cpp_token *)); |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 501 | arg->first = (const cpp_token **) buff->cur; |
| 502 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 503 | |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 504 | token = cpp_get_token (pfile); |
| 505 | |
| 506 | if (token->type == CPP_PADDING) |
| 507 | { |
| 508 | /* Drop leading padding. */ |
| 509 | if (ntokens == 0) |
| 510 | continue; |
| 511 | } |
| 512 | else if (token->type == CPP_OPEN_PAREN) |
| 513 | paren_depth++; |
| 514 | else if (token->type == CPP_CLOSE_PAREN) |
| 515 | { |
| 516 | if (paren_depth-- == 0) |
| 517 | break; |
| 518 | } |
| 519 | else if (token->type == CPP_COMMA) |
| 520 | { |
| 521 | /* A comma does not terminate an argument within |
| 522 | parentheses or as part of a variable argument. */ |
| 523 | if (paren_depth == 0 |
| 524 | && ! (macro->variadic && argc == macro->paramc)) |
| 525 | break; |
| 526 | } |
| 527 | else if (token->type == CPP_EOF |
| 528 | || (token->type == CPP_HASH && token->flags & BOL)) |
| 529 | break; |
| 530 | |
| 531 | arg->first[ntokens++] = token; |
| 532 | } |
| 533 | |
| 534 | /* Drop trailing padding. */ |
| 535 | while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING) |
| 536 | ntokens--; |
| 537 | |
| 538 | arg->count = ntokens; |
| 539 | arg->first[ntokens] = &pfile->eof; |
| 540 | |
| 541 | /* Terminate the argument. Excess arguments loop back and |
| 542 | overwrite the final legitimate argument, before failing. */ |
| 543 | if (argc <= macro->paramc) |
| 544 | { |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 545 | buff->cur = (unsigned char *) &arg->first[ntokens + 1]; |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 546 | if (argc != macro->paramc) |
| 547 | arg++; |
| 548 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 549 | } |
Neil Booth | e808ec9 | 2002-02-27 07:24:53 +0000 | [diff] [blame] | 550 | while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 551 | |
Neil Booth | e808ec9 | 2002-02-27 07:24:53 +0000 | [diff] [blame] | 552 | if (token->type == CPP_EOF) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 553 | { |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 554 | /* We still need the CPP_EOF to end directives, and to end |
| 555 | pre-expansion of a macro argument. Step back is not |
| 556 | unconditional, since we don't want to return a CPP_EOF to our |
| 557 | callers at the end of an -include-d file. */ |
Neil Booth | e808ec9 | 2002-02-27 07:24:53 +0000 | [diff] [blame] | 558 | if (pfile->context->prev || pfile->state.in_directive) |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 559 | _cpp_backup_tokens (pfile, 1); |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 560 | cpp_error (pfile, DL_ERROR, |
| 561 | "unterminated argument list invoking macro \"%s\"", |
Neil Booth | a28c5035 | 2001-05-16 22:02:09 +0000 | [diff] [blame] | 562 | NODE_NAME (node)); |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 563 | error = true; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 564 | } |
| 565 | else if (argc < macro->paramc) |
| 566 | { |
| 567 | /* As an extension, a rest argument is allowed to not appear in |
| 568 | the invocation at all. |
| 569 | e.g. #define debug(format, args...) something |
| 570 | debug("string"); |
| 571 | |
| 572 | This is exactly the same as if there had been an empty rest |
| 573 | argument - debug("string", ). */ |
| 574 | |
Neil Booth | 28e0f04 | 2000-12-09 12:06:37 +0000 | [diff] [blame] | 575 | if (argc + 1 == macro->paramc && macro->variadic) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 576 | { |
Neil Booth | 7065e13 | 2001-02-14 07:38:20 +0000 | [diff] [blame] | 577 | if (CPP_PEDANTIC (pfile) && ! macro->syshdr) |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 578 | cpp_error (pfile, DL_PEDWARN, |
| 579 | "ISO C99 requires rest arguments to be used"); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 580 | } |
| 581 | else |
| 582 | { |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 583 | cpp_error (pfile, DL_ERROR, |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 584 | "macro \"%s\" requires %u arguments, but only %u given", |
Neil Booth | a28c5035 | 2001-05-16 22:02:09 +0000 | [diff] [blame] | 585 | NODE_NAME (node), macro->paramc, argc); |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 586 | error = true; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | else if (argc > macro->paramc) |
| 590 | { |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame] | 591 | /* Empty argument to a macro taking no arguments is OK. */ |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 592 | if (argc != 1 || arg->count) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 593 | { |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 594 | cpp_error (pfile, DL_ERROR, |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 595 | "macro \"%s\" passed %u arguments, but takes just %u", |
Neil Booth | a28c5035 | 2001-05-16 22:02:09 +0000 | [diff] [blame] | 596 | NODE_NAME (node), argc, macro->paramc); |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 597 | error = true; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 601 | if (!error) |
| 602 | return base_buff; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 603 | |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 604 | _cpp_release_buff (pfile, base_buff); |
| 605 | return NULL; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 608 | /* Search for an opening parenthesis to the macro of NODE, in such a |
| 609 | way that, if none is found, we don't lose the information in any |
| 610 | intervening padding tokens. If we find the parenthesis, collect |
| 611 | the arguments and return the buffer containing them. */ |
| 612 | static _cpp_buff * |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 613 | funlike_invocation_p (pfile, node) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 614 | cpp_reader *pfile; |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 615 | cpp_hashnode *node; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 616 | { |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 617 | const cpp_token *token, *padding = NULL; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 618 | |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 619 | for (;;) |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 620 | { |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 621 | token = cpp_get_token (pfile); |
| 622 | if (token->type != CPP_PADDING) |
| 623 | break; |
| 624 | if (padding == NULL |
| 625 | || (!(padding->flags & PREV_WHITE) && token->val.source == NULL)) |
| 626 | padding = token; |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 627 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 628 | |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 629 | if (token->type == CPP_OPEN_PAREN) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 630 | { |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 631 | pfile->state.parsing_args = 2; |
| 632 | return collect_args (pfile, node); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Neil Booth | 9ac3b1b | 2002-04-21 16:17:55 +0000 | [diff] [blame] | 635 | /* CPP_EOF can be the end of macro arguments, or the end of the |
| 636 | file. We mustn't back up over the latter. Ugh. */ |
| 637 | if (token->type != CPP_EOF || token == &pfile->eof) |
| 638 | { |
| 639 | /* Back up. We may have skipped padding, in which case backing |
| 640 | up more than one token when expanding macros is in general |
| 641 | too difficult. We re-insert it in its own context. */ |
| 642 | _cpp_backup_tokens (pfile, 1); |
| 643 | if (padding) |
| 644 | push_token_context (pfile, NULL, padding, 1); |
| 645 | } |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 646 | |
| 647 | return NULL; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 650 | /* Push the context of a macro with hash entry NODE onto the context |
| 651 | stack. If we can successfully expand the macro, we push a context |
| 652 | containing its yet-to-be-rescanned replacement list and return one. |
| 653 | Otherwise, we don't push a context and return zero. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 654 | static int |
Neil Booth | 29b1074 | 2000-11-13 18:40:37 +0000 | [diff] [blame] | 655 | enter_macro_context (pfile, node) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 656 | cpp_reader *pfile; |
Neil Booth | 29b1074 | 2000-11-13 18:40:37 +0000 | [diff] [blame] | 657 | cpp_hashnode *node; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 658 | { |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 659 | /* The presence of a macro invalidates a file's controlling macro. */ |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 660 | pfile->mi_valid = false; |
| 661 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 662 | /* Handle standard macros. */ |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 663 | if (! (node->flags & NODE_BUILTIN)) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 664 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 665 | cpp_macro *macro = node->value.macro; |
| 666 | |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 667 | if (macro->fun_like) |
| 668 | { |
| 669 | _cpp_buff *buff; |
| 670 | |
| 671 | pfile->state.prevent_expansion++; |
| 672 | pfile->keep_tokens++; |
| 673 | pfile->state.parsing_args = 1; |
| 674 | buff = funlike_invocation_p (pfile, node); |
| 675 | pfile->state.parsing_args = 0; |
| 676 | pfile->keep_tokens--; |
| 677 | pfile->state.prevent_expansion--; |
| 678 | |
| 679 | if (buff == NULL) |
| 680 | { |
| 681 | if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr) |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 682 | cpp_error (pfile, DL_WARNING, |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 683 | "function-like macro \"%s\" must be used with arguments in traditional C", |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 684 | NODE_NAME (node)); |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 685 | |
| 686 | return 0; |
| 687 | } |
| 688 | |
Neil Booth | e808ec9 | 2002-02-27 07:24:53 +0000 | [diff] [blame] | 689 | if (macro->paramc > 0) |
| 690 | replace_args (pfile, node, macro, (macro_arg *) buff->base); |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 691 | _cpp_release_buff (pfile, buff); |
| 692 | } |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 693 | |
| 694 | /* Disable the macro within its expansion. */ |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 695 | node->flags |= NODE_DISABLED; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 696 | |
| 697 | if (macro->paramc == 0) |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 698 | push_token_context (pfile, node, macro->expansion, macro->count); |
| 699 | |
| 700 | return 1; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 701 | } |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 702 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 703 | /* Handle built-in macros and the _Pragma operator. */ |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 704 | return builtin_macro (pfile, node); |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 707 | /* Replace the parameters in a function-like macro of NODE with the |
| 708 | actual ARGS, and place the result in a newly pushed token context. |
| 709 | Expand each argument before replacing, unless it is operated upon |
| 710 | by the # or ## operators. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 711 | static void |
Neil Booth | e808ec9 | 2002-02-27 07:24:53 +0000 | [diff] [blame] | 712 | replace_args (pfile, node, macro, args) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 713 | cpp_reader *pfile; |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 714 | cpp_hashnode *node; |
Neil Booth | e808ec9 | 2002-02-27 07:24:53 +0000 | [diff] [blame] | 715 | cpp_macro *macro; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 716 | macro_arg *args; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 717 | { |
| 718 | unsigned int i, total; |
| 719 | const cpp_token *src, *limit; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 720 | const cpp_token **dest, **first; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 721 | macro_arg *arg; |
Neil Booth | 1e013d2 | 2001-09-26 21:44:35 +0000 | [diff] [blame] | 722 | _cpp_buff *buff; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 723 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 724 | /* First, fully macro-expand arguments, calculating the number of |
Neil Booth | 1e013d2 | 2001-09-26 21:44:35 +0000 | [diff] [blame] | 725 | tokens in the final expansion as we go. The ordering of the if |
| 726 | statements below is subtle; we must handle stringification before |
| 727 | pasting. */ |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 728 | total = macro->count; |
| 729 | limit = macro->expansion + macro->count; |
| 730 | |
| 731 | for (src = macro->expansion; src < limit; src++) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 732 | if (src->type == CPP_MACRO_ARG) |
| 733 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 734 | /* Leading and trailing padding tokens. */ |
| 735 | total += 2; |
| 736 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 737 | /* We have an argument. If it is not being stringified or |
| 738 | pasted it is macro-replaced before insertion. */ |
Neil Booth | 6c53ebf | 2000-11-06 18:43:32 +0000 | [diff] [blame] | 739 | arg = &args[src->val.arg_no - 1]; |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame] | 740 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 741 | if (src->flags & STRINGIFY_ARG) |
| 742 | { |
| 743 | if (!arg->stringified) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 744 | arg->stringified = stringify_arg (pfile, arg); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 745 | } |
| 746 | else if ((src->flags & PASTE_LEFT) |
| 747 | || (src > macro->expansion && (src[-1].flags & PASTE_LEFT))) |
| 748 | total += arg->count - 1; |
| 749 | else |
| 750 | { |
| 751 | if (!arg->expanded) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 752 | expand_arg (pfile, arg); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 753 | total += arg->expanded_count - 1; |
| 754 | } |
| 755 | } |
| 756 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 757 | /* Now allocate space for the expansion, copy the tokens and replace |
| 758 | the arguments. */ |
Neil Booth | 1e013d2 | 2001-09-26 21:44:35 +0000 | [diff] [blame] | 759 | buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *)); |
| 760 | first = (const cpp_token **) buff->base; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 761 | dest = first; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 762 | |
| 763 | for (src = macro->expansion; src < limit; src++) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 764 | { |
| 765 | unsigned int count; |
| 766 | const cpp_token **from, **paste_flag; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 767 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 768 | if (src->type != CPP_MACRO_ARG) |
| 769 | { |
| 770 | *dest++ = src; |
| 771 | continue; |
| 772 | } |
| 773 | |
| 774 | paste_flag = 0; |
| 775 | arg = &args[src->val.arg_no - 1]; |
| 776 | if (src->flags & STRINGIFY_ARG) |
| 777 | count = 1, from = &arg->stringified; |
| 778 | else if (src->flags & PASTE_LEFT) |
| 779 | count = arg->count, from = arg->first; |
| 780 | else if (src != macro->expansion && (src[-1].flags & PASTE_LEFT)) |
| 781 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 782 | count = arg->count, from = arg->first; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 783 | if (dest != first) |
| 784 | { |
| 785 | /* GCC has special semantics for , ## b where b is a |
| 786 | varargs parameter: the comma disappears if b was |
| 787 | given no actual arguments (not merely if b is an |
| 788 | empty argument); otherwise the paste flag is removed. */ |
| 789 | if (dest[-1]->type == CPP_COMMA |
| 790 | && macro->variadic |
| 791 | && src->val.arg_no == macro->paramc) |
| 792 | { |
| 793 | if (count == 0) |
| 794 | dest--; |
| 795 | else |
| 796 | paste_flag = dest - 1; |
| 797 | } |
| 798 | /* Remove the paste flag if the RHS is a placemarker. */ |
| 799 | else if (count == 0) |
| 800 | paste_flag = dest - 1; |
| 801 | } |
| 802 | } |
| 803 | else |
| 804 | count = arg->expanded_count, from = arg->expanded; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 805 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 806 | /* Padding on the left of an argument (unless RHS of ##). */ |
| 807 | if (!pfile->state.in_directive |
| 808 | && src != macro->expansion && !(src[-1].flags & PASTE_LEFT)) |
| 809 | *dest++ = padding_token (pfile, src); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 810 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 811 | if (count) |
| 812 | { |
| 813 | memcpy (dest, from, count * sizeof (cpp_token *)); |
| 814 | dest += count; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 815 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 816 | /* With a non-empty argument on the LHS of ##, the last |
| 817 | token should be flagged PASTE_LEFT. */ |
| 818 | if (src->flags & PASTE_LEFT) |
| 819 | paste_flag = dest - 1; |
| 820 | } |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame] | 821 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 822 | /* Avoid paste on RHS (even case count == 0). */ |
| 823 | if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT)) |
| 824 | *dest++ = &pfile->avoid_paste; |
Neil Booth | 26ec42e | 2001-01-28 11:22:23 +0000 | [diff] [blame] | 825 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 826 | /* Add a new paste flag, or remove an unwanted one. */ |
| 827 | if (paste_flag) |
| 828 | { |
| 829 | cpp_token *token = _cpp_temp_token (pfile); |
| 830 | token->type = (*paste_flag)->type; |
| 831 | token->val.str = (*paste_flag)->val.str; |
| 832 | if (src->flags & PASTE_LEFT) |
| 833 | token->flags = (*paste_flag)->flags | PASTE_LEFT; |
| 834 | else |
| 835 | token->flags = (*paste_flag)->flags & ~PASTE_LEFT; |
| 836 | *paste_flag = token; |
| 837 | } |
| 838 | } |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame] | 839 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 840 | /* Free the expanded arguments. */ |
| 841 | for (i = 0; i < macro->paramc; i++) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 842 | if (args[i].expanded) |
| 843 | free (args[i].expanded); |
| 844 | |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 845 | push_ptoken_context (pfile, node, buff, first, dest - first); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 848 | /* Return a special padding token, with padding inherited from SOURCE. */ |
| 849 | static const cpp_token * |
| 850 | padding_token (pfile, source) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 851 | cpp_reader *pfile; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 852 | const cpp_token *source; |
| 853 | { |
| 854 | cpp_token *result = _cpp_temp_token (pfile); |
| 855 | |
| 856 | result->type = CPP_PADDING; |
| 857 | result->val.source = source; |
| 858 | result->flags = 0; |
| 859 | return result; |
| 860 | } |
| 861 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 862 | /* Get a new uninitialized context. Create a new one if we cannot |
| 863 | re-use an old one. */ |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 864 | static cpp_context * |
| 865 | next_context (pfile) |
| 866 | cpp_reader *pfile; |
| 867 | { |
| 868 | cpp_context *result = pfile->context->next; |
| 869 | |
| 870 | if (result == 0) |
| 871 | { |
| 872 | result = xnew (cpp_context); |
| 873 | result->prev = pfile->context; |
| 874 | result->next = 0; |
| 875 | pfile->context->next = result; |
| 876 | } |
| 877 | |
| 878 | pfile->context = result; |
| 879 | return result; |
| 880 | } |
| 881 | |
| 882 | /* Push a list of pointers to tokens. */ |
| 883 | static void |
Neil Booth | 1e013d2 | 2001-09-26 21:44:35 +0000 | [diff] [blame] | 884 | push_ptoken_context (pfile, macro, buff, first, count) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 885 | cpp_reader *pfile; |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 886 | cpp_hashnode *macro; |
Neil Booth | 1e013d2 | 2001-09-26 21:44:35 +0000 | [diff] [blame] | 887 | _cpp_buff *buff; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 888 | const cpp_token **first; |
| 889 | unsigned int count; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 890 | { |
| 891 | cpp_context *context = next_context (pfile); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 892 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 893 | context->direct_p = false; |
| 894 | context->macro = macro; |
Neil Booth | 1e013d2 | 2001-09-26 21:44:35 +0000 | [diff] [blame] | 895 | context->buff = buff; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 896 | context->first.ptoken = first; |
| 897 | context->last.ptoken = first + count; |
| 898 | } |
| 899 | |
| 900 | /* Push a list of tokens. */ |
| 901 | static void |
| 902 | push_token_context (pfile, macro, first, count) |
| 903 | cpp_reader *pfile; |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 904 | cpp_hashnode *macro; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 905 | const cpp_token *first; |
| 906 | unsigned int count; |
| 907 | { |
| 908 | cpp_context *context = next_context (pfile); |
| 909 | |
| 910 | context->direct_p = true; |
| 911 | context->macro = macro; |
Neil Booth | 1e013d2 | 2001-09-26 21:44:35 +0000 | [diff] [blame] | 912 | context->buff = NULL; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 913 | context->first.token = first; |
| 914 | context->last.token = first + count; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 917 | /* Expand an argument ARG before replacing parameters in a |
| 918 | function-like macro. This works by pushing a context with the |
| 919 | argument's tokens, and then expanding that into a temporary buffer |
| 920 | as if it were a normal part of the token stream. collect_args() |
| 921 | has terminated the argument's tokens with a CPP_EOF so that we know |
| 922 | when we have fully expanded the argument. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 923 | static void |
| 924 | expand_arg (pfile, arg) |
| 925 | cpp_reader *pfile; |
| 926 | macro_arg *arg; |
| 927 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 928 | unsigned int capacity; |
| 929 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 930 | if (arg->count == 0) |
| 931 | return; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 932 | |
| 933 | /* Loop, reading in the arguments. */ |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 934 | capacity = 256; |
| 935 | arg->expanded = (const cpp_token **) |
| 936 | xmalloc (capacity * sizeof (cpp_token *)); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 937 | |
Neil Booth | 1e013d2 | 2001-09-26 21:44:35 +0000 | [diff] [blame] | 938 | push_ptoken_context (pfile, NULL, NULL, arg->first, arg->count + 1); |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 939 | for (;;) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 940 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 941 | const cpp_token *token; |
| 942 | |
| 943 | if (arg->expanded_count + 1 >= capacity) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 944 | { |
| 945 | capacity *= 2; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 946 | arg->expanded = (const cpp_token **) |
| 947 | xrealloc (arg->expanded, capacity * sizeof (cpp_token *)); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 948 | } |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 949 | |
| 950 | token = cpp_get_token (pfile); |
| 951 | |
| 952 | if (token->type == CPP_EOF) |
| 953 | break; |
| 954 | |
| 955 | arg->expanded[arg->expanded_count++] = token; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 956 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 957 | |
Neil Booth | 1e013d2 | 2001-09-26 21:44:35 +0000 | [diff] [blame] | 958 | _cpp_pop_context (pfile); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 959 | } |
| 960 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 961 | /* Pop the current context off the stack, re-enabling the macro if the |
| 962 | context represented a macro's replacement list. The context |
| 963 | structure is not freed so that we can re-use it later. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 964 | void |
| 965 | _cpp_pop_context (pfile) |
| 966 | cpp_reader *pfile; |
| 967 | { |
Neil Booth | 1e013d2 | 2001-09-26 21:44:35 +0000 | [diff] [blame] | 968 | cpp_context *context = pfile->context; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 969 | |
Neil Booth | 1e013d2 | 2001-09-26 21:44:35 +0000 | [diff] [blame] | 970 | if (context->macro) |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 971 | context->macro->flags &= ~NODE_DISABLED; |
Neil Booth | 1e013d2 | 2001-09-26 21:44:35 +0000 | [diff] [blame] | 972 | |
| 973 | if (context->buff) |
| 974 | _cpp_release_buff (pfile, context->buff); |
| 975 | |
| 976 | pfile->context = context->prev; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Neil Booth | 7f2f1a6 | 2000-11-14 18:32:06 +0000 | [diff] [blame] | 979 | /* Eternal routine to get a token. Also used nearly everywhere |
| 980 | internally, except for places where we know we can safely call |
| 981 | the lexer directly, such as lexing a directive name. |
| 982 | |
| 983 | Macro expansions and directives are transparently handled, |
| 984 | including entering included files. Thus tokens are post-macro |
| 985 | expansion, and after any intervening directives. External callers |
| 986 | see CPP_EOF only at EOF. Internal callers also see it when meeting |
| 987 | a directive inside a macro call, when at the end of a directive and |
| 988 | state.in_directive is still 1, and at the end of argument |
| 989 | pre-expansion. */ |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 990 | const cpp_token * |
| 991 | cpp_get_token (pfile) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 992 | cpp_reader *pfile; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 993 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 994 | const cpp_token *result; |
| 995 | |
Neil Booth | 29b1074 | 2000-11-13 18:40:37 +0000 | [diff] [blame] | 996 | for (;;) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 997 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 998 | cpp_hashnode *node; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 999 | cpp_context *context = pfile->context; |
| 1000 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1001 | /* Context->prev == 0 <=> base context. */ |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 1002 | if (!context->prev) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1003 | result = _cpp_lex_token (pfile); |
| 1004 | else if (context->first.token != context->last.token) |
Neil Booth | 29b1074 | 2000-11-13 18:40:37 +0000 | [diff] [blame] | 1005 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1006 | if (context->direct_p) |
| 1007 | result = context->first.token++; |
| 1008 | else |
| 1009 | result = *context->first.ptoken++; |
| 1010 | |
| 1011 | if (result->flags & PASTE_LEFT) |
Neil Booth | ec1a23e | 2001-01-31 07:48:54 +0000 | [diff] [blame] | 1012 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1013 | paste_all_tokens (pfile, result); |
| 1014 | if (pfile->state.in_directive) |
| 1015 | continue; |
| 1016 | return padding_token (pfile, result); |
Neil Booth | ec1a23e | 2001-01-31 07:48:54 +0000 | [diff] [blame] | 1017 | } |
Neil Booth | 29b1074 | 2000-11-13 18:40:37 +0000 | [diff] [blame] | 1018 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1019 | else |
| 1020 | { |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 1021 | _cpp_pop_context (pfile); |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1022 | if (pfile->state.in_directive) |
| 1023 | continue; |
| 1024 | return &pfile->avoid_paste; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1025 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1026 | |
Jason Thorpe | 477cdac | 2002-04-07 03:12:23 +0000 | [diff] [blame] | 1027 | if (pfile->state.in_directive && result->type == CPP_COMMENT) |
| 1028 | continue; |
| 1029 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1030 | if (result->type != CPP_NAME) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1031 | break; |
| 1032 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1033 | node = result->val.node; |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame] | 1034 | |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 1035 | if (node->type != NT_MACRO || (result->flags & NO_EXPAND)) |
| 1036 | break; |
| 1037 | |
| 1038 | if (!(node->flags & NODE_DISABLED)) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1039 | { |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 1040 | if (!pfile->state.prevent_expansion |
| 1041 | && enter_macro_context (pfile, node)) |
Neil Booth | bd96977 | 2001-02-01 19:13:53 +0000 | [diff] [blame] | 1042 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1043 | if (pfile->state.in_directive) |
| 1044 | continue; |
| 1045 | return padding_token (pfile, result); |
Neil Booth | bd96977 | 2001-02-01 19:13:53 +0000 | [diff] [blame] | 1046 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1047 | } |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 1048 | else |
| 1049 | { |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 1050 | /* Flag this token as always unexpandable. FIXME: move this |
| 1051 | to collect_args()?. */ |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 1052 | cpp_token *t = _cpp_temp_token (pfile); |
| 1053 | t->type = result->type; |
| 1054 | t->flags = result->flags | NO_EXPAND; |
| 1055 | t->val.str = result->val.str; |
| 1056 | result = t; |
| 1057 | } |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1058 | |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 1059 | break; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1060 | } |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1061 | |
| 1062 | return result; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Neil Booth | 7065e13 | 2001-02-14 07:38:20 +0000 | [diff] [blame] | 1065 | /* Returns true if we're expanding an object-like macro that was |
| 1066 | defined in a system header. Just checks the macro at the top of |
| 1067 | the stack. Used for diagnostic suppression. */ |
| 1068 | int |
Neil Booth | c691145 | 2001-03-03 11:32:32 +0000 | [diff] [blame] | 1069 | cpp_sys_macro_p (pfile) |
Neil Booth | 7065e13 | 2001-02-14 07:38:20 +0000 | [diff] [blame] | 1070 | cpp_reader *pfile; |
| 1071 | { |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 1072 | cpp_hashnode *node = pfile->context->macro; |
Neil Booth | 7065e13 | 2001-02-14 07:38:20 +0000 | [diff] [blame] | 1073 | |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 1074 | return node && node->value.macro && node->value.macro->syshdr; |
Neil Booth | 7065e13 | 2001-02-14 07:38:20 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Neil Booth | af0d16c | 2002-04-22 17:48:02 +0000 | [diff] [blame^] | 1077 | /* Read each token in, until end of the current file. Directives are |
| 1078 | transparently processed. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1079 | void |
Neil Booth | ef6e958 | 2001-08-04 12:01:59 +0000 | [diff] [blame] | 1080 | cpp_scan_nooutput (pfile) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1081 | cpp_reader *pfile; |
| 1082 | { |
Neil Booth | af0d16c | 2002-04-22 17:48:02 +0000 | [diff] [blame^] | 1083 | /* Request a CPP_EOF token at the end of this file, rather than |
| 1084 | transparently continuing with the including file. */ |
| 1085 | pfile->buffer->return_at_eof = true; |
| 1086 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1087 | while (cpp_get_token (pfile)->type != CPP_EOF) |
| 1088 | ; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 1091 | /* Step back one (or more) tokens. Can only step mack more than 1 if |
| 1092 | they are from the lexer, and not from macro expansion. */ |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1093 | void |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 1094 | _cpp_backup_tokens (pfile, count) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1095 | cpp_reader *pfile; |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 1096 | unsigned int count; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1097 | { |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 1098 | if (pfile->context->prev == NULL) |
| 1099 | { |
| 1100 | pfile->lookaheads += count; |
| 1101 | while (count--) |
| 1102 | { |
Neil Booth | 3293c3e | 2001-11-19 21:04:49 +0000 | [diff] [blame] | 1103 | pfile->cur_token--; |
| 1104 | if (pfile->cur_token == pfile->cur_run->base |
| 1105 | /* Possible with -fpreprocessed and no leading #line. */ |
| 1106 | && pfile->cur_run->prev != NULL) |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 1107 | { |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 1108 | pfile->cur_run = pfile->cur_run->prev; |
| 1109 | pfile->cur_token = pfile->cur_run->limit; |
| 1110 | } |
| 1111 | } |
| 1112 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1113 | else |
| 1114 | { |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 1115 | if (count != 1) |
| 1116 | abort (); |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1117 | if (pfile->context->direct_p) |
| 1118 | pfile->context->first.token--; |
| 1119 | else |
| 1120 | pfile->context->first.ptoken--; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1121 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | /* #define directive parsing and handling. */ |
| 1125 | |
Neil Booth | 618cdda | 2001-02-25 09:43:03 +0000 | [diff] [blame] | 1126 | /* Returns non-zero if a macro redefinition warning is required. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1127 | static int |
Neil Booth | 4d6baaf | 2001-11-26 23:44:54 +0000 | [diff] [blame] | 1128 | warn_of_redefinition (node, macro2) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1129 | const cpp_hashnode *node; |
| 1130 | const cpp_macro *macro2; |
| 1131 | { |
| 1132 | const cpp_macro *macro1; |
| 1133 | unsigned int i; |
| 1134 | |
Neil Booth | 618cdda | 2001-02-25 09:43:03 +0000 | [diff] [blame] | 1135 | /* Some redefinitions need to be warned about regardless. */ |
| 1136 | if (node->flags & NODE_WARN) |
| 1137 | return 1; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1138 | |
Neil Booth | 618cdda | 2001-02-25 09:43:03 +0000 | [diff] [blame] | 1139 | /* Redefinition of a macro is allowed if and only if the old and new |
Kazu Hirata | ec5c56d | 2001-08-01 17:57:27 +0000 | [diff] [blame] | 1140 | definitions are the same. (6.10.3 paragraph 2). */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1141 | macro1 = node->value.macro; |
| 1142 | |
| 1143 | /* The quick failures. */ |
| 1144 | if (macro1->count != macro2->count |
| 1145 | || macro1->paramc != macro2->paramc |
| 1146 | || macro1->fun_like != macro2->fun_like |
Neil Booth | 28e0f04 | 2000-12-09 12:06:37 +0000 | [diff] [blame] | 1147 | || macro1->variadic != macro2->variadic) |
Neil Booth | 618cdda | 2001-02-25 09:43:03 +0000 | [diff] [blame] | 1148 | return 1; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1149 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1150 | /* Check each token. */ |
| 1151 | for (i = 0; i < macro1->count; i++) |
| 1152 | if (! _cpp_equiv_tokens (¯o1->expansion[i], ¯o2->expansion[i])) |
Neil Booth | 618cdda | 2001-02-25 09:43:03 +0000 | [diff] [blame] | 1153 | return 1; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1154 | |
| 1155 | /* Check parameter spellings. */ |
| 1156 | for (i = 0; i < macro1->paramc; i++) |
| 1157 | if (macro1->params[i] != macro2->params[i]) |
Neil Booth | 618cdda | 2001-02-25 09:43:03 +0000 | [diff] [blame] | 1158 | return 1; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1159 | |
Neil Booth | 618cdda | 2001-02-25 09:43:03 +0000 | [diff] [blame] | 1160 | return 0; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1163 | /* Free the definition of hashnode H. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1164 | void |
| 1165 | _cpp_free_definition (h) |
| 1166 | cpp_hashnode *h; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1167 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1168 | /* Macros and assertions no longer have anything to free. */ |
| 1169 | h->type = NT_VOID; |
| 1170 | /* Clear builtin flag in case of redefinition. */ |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 1171 | h->flags &= ~(NODE_BUILTIN | NODE_DISABLED); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1172 | } |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1173 | |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 1174 | /* Save parameter NODE to the parameter list of macro MACRO. Returns |
Joseph Myers | f5143c4 | 2001-11-04 02:51:28 +0000 | [diff] [blame] | 1175 | zero on success, non-zero if the parameter is a duplicate. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1176 | static int |
| 1177 | save_parameter (pfile, macro, node) |
| 1178 | cpp_reader *pfile; |
| 1179 | cpp_macro *macro; |
| 1180 | cpp_hashnode *node; |
| 1181 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1182 | /* Constraint 6.10.3.6 - duplicate parameter names. */ |
| 1183 | if (node->arg_index) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1184 | { |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 1185 | cpp_error (pfile, DL_ERROR, "duplicate macro parameter \"%s\"", |
| 1186 | NODE_NAME (node)); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1187 | return 1; |
| 1188 | } |
| 1189 | |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1190 | if (BUFF_ROOM (pfile->a_buff) |
| 1191 | < (macro->paramc + 1) * sizeof (cpp_hashnode *)) |
| 1192 | _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *)); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1193 | |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1194 | ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node; |
| 1195 | node->arg_index = macro->paramc; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1196 | return 0; |
| 1197 | } |
| 1198 | |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1199 | /* Check the syntax of the parameters in a MACRO definition. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1200 | static int |
| 1201 | parse_params (pfile, macro) |
| 1202 | cpp_reader *pfile; |
| 1203 | cpp_macro *macro; |
| 1204 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1205 | unsigned int prev_ident = 0; |
| 1206 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1207 | for (;;) |
| 1208 | { |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 1209 | const cpp_token *token = _cpp_lex_token (pfile); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1210 | |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 1211 | switch (token->type) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1212 | { |
| 1213 | default: |
Jason Thorpe | 477cdac | 2002-04-07 03:12:23 +0000 | [diff] [blame] | 1214 | /* Allow/ignore comments in parameter lists if we are |
| 1215 | preserving comments in macro expansions. */ |
| 1216 | if (token->type == CPP_COMMENT |
| 1217 | && ! CPP_OPTION (pfile, discard_comments_in_macro_exp)) |
| 1218 | continue; |
| 1219 | |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 1220 | cpp_error (pfile, DL_ERROR, |
| 1221 | "\"%s\" may not appear in macro parameter list", |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 1222 | cpp_token_as_text (pfile, token)); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1223 | return 0; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1224 | |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1225 | case CPP_NAME: |
| 1226 | if (prev_ident) |
| 1227 | { |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 1228 | cpp_error (pfile, DL_ERROR, |
| 1229 | "macro parameters must be comma-separated"); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1230 | return 0; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1231 | } |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1232 | prev_ident = 1; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1233 | |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 1234 | if (save_parameter (pfile, macro, token->val.node)) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1235 | return 0; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1236 | continue; |
| 1237 | |
| 1238 | case CPP_CLOSE_PAREN: |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1239 | if (prev_ident || macro->paramc == 0) |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1240 | return 1; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1241 | |
| 1242 | /* Fall through to pick up the error. */ |
| 1243 | case CPP_COMMA: |
| 1244 | if (!prev_ident) |
| 1245 | { |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 1246 | cpp_error (pfile, DL_ERROR, "parameter name missing"); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1247 | return 0; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1248 | } |
| 1249 | prev_ident = 0; |
| 1250 | continue; |
| 1251 | |
| 1252 | case CPP_ELLIPSIS: |
Neil Booth | 28e0f04 | 2000-12-09 12:06:37 +0000 | [diff] [blame] | 1253 | macro->variadic = 1; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1254 | if (!prev_ident) |
| 1255 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1256 | save_parameter (pfile, macro, pfile->spec_nodes.n__VA_ARGS__); |
| 1257 | pfile->state.va_args_ok = 1; |
| 1258 | if (! CPP_OPTION (pfile, c99) && CPP_OPTION (pfile, pedantic)) |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 1259 | cpp_error (pfile, DL_PEDWARN, |
| 1260 | "anonymous variadic macros were introduced in C99"); |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1261 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1262 | else if (CPP_OPTION (pfile, pedantic)) |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 1263 | cpp_error (pfile, DL_PEDWARN, |
| 1264 | "ISO C does not permit named variadic macros"); |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1265 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1266 | /* We're at the end, and just expect a closing parenthesis. */ |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 1267 | token = _cpp_lex_token (pfile); |
| 1268 | if (token->type == CPP_CLOSE_PAREN) |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1269 | return 1; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1270 | /* Fall through. */ |
| 1271 | |
| 1272 | case CPP_EOF: |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 1273 | cpp_error (pfile, DL_ERROR, "missing ')' in macro parameter list"); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1274 | return 0; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1275 | } |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1276 | } |
| 1277 | } |
| 1278 | |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 1279 | /* Allocate room for a token from a macro's replacement list. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1280 | static cpp_token * |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 1281 | alloc_expansion_token (pfile, macro) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1282 | cpp_reader *pfile; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1283 | cpp_macro *macro; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1284 | { |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1285 | if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token)) |
| 1286 | _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token)); |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1287 | |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1288 | return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++]; |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 1291 | /* Lex a token from the expansion of MACRO, but mark parameters as we |
| 1292 | find them and warn of traditional stringification. */ |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 1293 | static cpp_token * |
| 1294 | lex_expansion_token (pfile, macro) |
| 1295 | cpp_reader *pfile; |
| 1296 | cpp_macro *macro; |
| 1297 | { |
| 1298 | cpp_token *token; |
| 1299 | |
| 1300 | pfile->cur_token = alloc_expansion_token (pfile, macro); |
| 1301 | token = _cpp_lex_direct (pfile); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1302 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 1303 | /* Is this a parameter? */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1304 | if (token->type == CPP_NAME && token->val.node->arg_index) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1305 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1306 | token->type = CPP_MACRO_ARG; |
Neil Booth | 6c53ebf | 2000-11-06 18:43:32 +0000 | [diff] [blame] | 1307 | token->val.arg_no = token->val.node->arg_index; |
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_WTRADITIONAL (pfile) && macro->paramc > 0 |
| 1310 | && (token->type == CPP_STRING || token->type == CPP_CHAR)) |
| 1311 | check_trad_stringification (pfile, macro, &token->val.str); |
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 | return token; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | /* Parse a macro and save its expansion. Returns non-zero on success. */ |
| 1317 | int |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1318 | _cpp_create_definition (pfile, node) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1319 | cpp_reader *pfile; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1320 | cpp_hashnode *node; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1321 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1322 | cpp_macro *macro; |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 1323 | cpp_token *token, *saved_cur_token; |
| 1324 | const cpp_token *ctoken; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1325 | unsigned int i, ok = 1; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1326 | |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1327 | macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro)); |
Neil Booth | 5041042 | 2001-09-15 10:18:03 +0000 | [diff] [blame] | 1328 | macro->line = pfile->directive_line; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1329 | macro->params = 0; |
| 1330 | macro->paramc = 0; |
Neil Booth | 28e0f04 | 2000-12-09 12:06:37 +0000 | [diff] [blame] | 1331 | macro->variadic = 0; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1332 | macro->count = 0; |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 1333 | macro->fun_like = 0; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1334 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1335 | /* Get the first token of the expansion (or the '(' of a |
| 1336 | function-like macro). */ |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 1337 | ctoken = _cpp_lex_token (pfile); |
| 1338 | |
| 1339 | if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE)) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1340 | { |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1341 | ok = parse_params (pfile, macro); |
| 1342 | macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff); |
| 1343 | if (!ok) |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 1344 | goto cleanup2; |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1345 | |
| 1346 | /* Success. Commit the parameter array. */ |
Neil Booth | 562a5c2 | 2002-04-21 18:46:42 +0000 | [diff] [blame] | 1347 | BUFF_FRONT (pfile->a_buff) = (uchar *) ¯o->params[macro->paramc]; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1348 | macro->fun_like = 1; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1349 | } |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 1350 | else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE)) |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 1351 | cpp_error (pfile, DL_PEDWARN, |
| 1352 | "ISO C requires whitespace after the macro name"); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1353 | |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 1354 | saved_cur_token = pfile->cur_token; |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 1355 | |
| 1356 | if (macro->fun_like) |
| 1357 | token = lex_expansion_token (pfile, macro); |
| 1358 | else |
| 1359 | { |
| 1360 | token = alloc_expansion_token (pfile, macro); |
| 1361 | *token = *ctoken; |
| 1362 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1363 | |
| 1364 | for (;;) |
| 1365 | { |
| 1366 | /* Check the stringifying # constraint 6.10.3.2.1 of |
| 1367 | function-like macros when lexing the subsequent token. */ |
| 1368 | if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like) |
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 | if (token->type == CPP_MACRO_ARG) |
| 1371 | { |
| 1372 | token->flags &= ~PREV_WHITE; |
| 1373 | token->flags |= STRINGIFY_ARG; |
| 1374 | token->flags |= token[-1].flags & PREV_WHITE; |
| 1375 | token[-1] = token[0]; |
| 1376 | macro->count--; |
| 1377 | } |
| 1378 | /* Let assembler get away with murder. */ |
Neil Booth | bdb05a7 | 2000-11-26 17:31:13 +0000 | [diff] [blame] | 1379 | else if (CPP_OPTION (pfile, lang) != CLK_ASM) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1380 | { |
| 1381 | ok = 0; |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 1382 | cpp_error (pfile, DL_ERROR, |
| 1383 | "'#' is not followed by a macro parameter"); |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 1384 | goto cleanup1; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | if (token->type == CPP_EOF) |
| 1389 | break; |
| 1390 | |
| 1391 | /* Paste operator constraint 6.10.3.3.1. */ |
| 1392 | if (token->type == CPP_PASTE) |
| 1393 | { |
| 1394 | /* Token-paste ##, can appear in both object-like and |
| 1395 | function-like macros, but not at the ends. */ |
| 1396 | if (--macro->count > 0) |
| 1397 | token = lex_expansion_token (pfile, macro); |
| 1398 | |
| 1399 | if (macro->count == 0 || token->type == CPP_EOF) |
| 1400 | { |
| 1401 | ok = 0; |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 1402 | cpp_error (pfile, DL_ERROR, |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1403 | "'##' cannot appear at either end of a macro expansion"); |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 1404 | goto cleanup1; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | token[-1].flags |= PASTE_LEFT; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | token = lex_expansion_token (pfile, macro); |
| 1411 | } |
| 1412 | |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1413 | macro->expansion = (cpp_token *) BUFF_FRONT (pfile->a_buff); |
| 1414 | |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame] | 1415 | /* Don't count the CPP_EOF. */ |
| 1416 | macro->count--; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1417 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 1418 | /* Clear whitespace on first token for warn_of_redefinition(). */ |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1419 | if (macro->count) |
| 1420 | macro->expansion[0].flags &= ~PREV_WHITE; |
| 1421 | |
| 1422 | /* Commit the memory. */ |
Neil Booth | 562a5c2 | 2002-04-21 18:46:42 +0000 | [diff] [blame] | 1423 | BUFF_FRONT (pfile->a_buff) = (uchar *) ¯o->expansion[macro->count]; |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1424 | |
Neil Booth | 44ed91a | 2000-10-29 11:37:18 +0000 | [diff] [blame] | 1425 | /* Implement the macro-defined-to-itself optimisation. */ |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 1426 | if (macro->count == 1 && !macro->fun_like |
| 1427 | && macro->expansion[0].type == CPP_NAME |
| 1428 | && macro->expansion[0].val.node == node) |
| 1429 | node->flags |= NODE_DISABLED; |
Neil Booth | 44ed91a | 2000-10-29 11:37:18 +0000 | [diff] [blame] | 1430 | |
Neil Booth | 7065e13 | 2001-02-14 07:38:20 +0000 | [diff] [blame] | 1431 | /* To suppress some diagnostics. */ |
Neil Booth | 47d89cf | 2001-08-11 07:33:39 +0000 | [diff] [blame] | 1432 | macro->syshdr = pfile->map->sysp != 0; |
Neil Booth | 7065e13 | 2001-02-14 07:38:20 +0000 | [diff] [blame] | 1433 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1434 | if (node->type != NT_VOID) |
| 1435 | { |
Neil Booth | 4d6baaf | 2001-11-26 23:44:54 +0000 | [diff] [blame] | 1436 | if (warn_of_redefinition (node, macro)) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1437 | { |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 1438 | cpp_error_with_line (pfile, DL_PEDWARN, pfile->directive_line, 0, |
| 1439 | "\"%s\" redefined", NODE_NAME (node)); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1440 | |
Neil Booth | 618cdda | 2001-02-25 09:43:03 +0000 | [diff] [blame] | 1441 | if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN)) |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 1442 | cpp_error_with_line (pfile, DL_PEDWARN, node->value.macro->line, 0, |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1443 | "this is the location of the previous definition"); |
| 1444 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1445 | _cpp_free_definition (node); |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | /* Enter definition in hash table. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1449 | node->type = NT_MACRO; |
| 1450 | node->value.macro = macro; |
Neil Booth | a28c5035 | 2001-05-16 22:02:09 +0000 | [diff] [blame] | 1451 | if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_"))) |
Neil Booth | 618cdda | 2001-02-25 09:43:03 +0000 | [diff] [blame] | 1452 | node->flags |= NODE_WARN; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1453 | |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 1454 | cleanup1: |
| 1455 | |
| 1456 | /* Set type for SEEN_EOL() in cpplib.c, restore the lexer position. */ |
| 1457 | saved_cur_token[-1].type = pfile->cur_token[-1].type; |
| 1458 | pfile->cur_token = saved_cur_token; |
| 1459 | |
| 1460 | cleanup2: |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1461 | |
| 1462 | /* Stop the lexer accepting __VA_ARGS__. */ |
| 1463 | pfile->state.va_args_ok = 0; |
| 1464 | |
| 1465 | /* Clear the fast argument lookup indices. */ |
| 1466 | for (i = macro->paramc; i-- > 0; ) |
| 1467 | macro->params[i]->arg_index = 0; |
| 1468 | |
| 1469 | return ok; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 1472 | /* Warn if a token in STRING matches one of a function-like MACRO's |
| 1473 | parameters. */ |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 1474 | static void |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1475 | check_trad_stringification (pfile, macro, string) |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 1476 | cpp_reader *pfile; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1477 | const cpp_macro *macro; |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 1478 | const cpp_string *string; |
| 1479 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1480 | unsigned int i, len; |
Neil Booth | 562a5c2 | 2002-04-21 18:46:42 +0000 | [diff] [blame] | 1481 | const uchar *p, *q, *limit = string->text + string->len; |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 1482 | |
| 1483 | /* Loop over the string. */ |
| 1484 | for (p = string->text; p < limit; p = q) |
| 1485 | { |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 1486 | /* Find the start of an identifier. */ |
Greg McGary | 61c16b1 | 2000-09-15 21:25:02 +0000 | [diff] [blame] | 1487 | while (p < limit && !is_idstart (*p)) |
| 1488 | p++; |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 1489 | |
| 1490 | /* Find the end of the identifier. */ |
| 1491 | q = p; |
Greg McGary | 61c16b1 | 2000-09-15 21:25:02 +0000 | [diff] [blame] | 1492 | while (q < limit && is_idchar (*q)) |
| 1493 | q++; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1494 | |
| 1495 | len = q - p; |
| 1496 | |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 1497 | /* Loop over the function macro arguments to see if the |
| 1498 | identifier inside the string matches one of them. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1499 | for (i = 0; i < macro->paramc; i++) |
| 1500 | { |
| 1501 | const cpp_hashnode *node = macro->params[i]; |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 1502 | |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 1503 | if (NODE_LEN (node) == len |
| 1504 | && !memcmp (p, NODE_NAME (node), len)) |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 1505 | { |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 1506 | cpp_error (pfile, DL_WARNING, |
Zack Weinberg | f458d1d | 2002-02-27 18:48:07 +0000 | [diff] [blame] | 1507 | "macro argument \"%s\" would be stringified in traditional C", |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 1508 | NODE_NAME (node)); |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 1509 | break; |
| 1510 | } |
| 1511 | } |
| 1512 | } |
| 1513 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1514 | |
Neil Booth | 7096171 | 2001-06-23 11:34:41 +0000 | [diff] [blame] | 1515 | /* Returns the name, arguments and expansion of a macro, in a format |
| 1516 | suitable to be read back in again, and therefore also for DWARF 2 |
| 1517 | debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION". |
| 1518 | Caller is expected to generate the "#define" bit if needed. The |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1519 | returned text is temporary, and automatically freed later. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1520 | const unsigned char * |
| 1521 | cpp_macro_definition (pfile, node) |
| 1522 | cpp_reader *pfile; |
| 1523 | const cpp_hashnode *node; |
| 1524 | { |
| 1525 | unsigned int i, len; |
| 1526 | const cpp_macro *macro = node->value.macro; |
| 1527 | unsigned char *buffer; |
| 1528 | |
| 1529 | if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN)) |
| 1530 | { |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 1531 | cpp_error (pfile, DL_ICE, |
| 1532 | "invalid hash type %d in cpp_macro_definition", node->type); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1533 | return 0; |
| 1534 | } |
| 1535 | |
| 1536 | /* Calculate length. */ |
Neil Booth | 7096171 | 2001-06-23 11:34:41 +0000 | [diff] [blame] | 1537 | len = NODE_LEN (node) + 1; /* ' ' */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1538 | if (macro->fun_like) |
| 1539 | { |
Jim Blandy | 64d0826 | 2002-04-05 00:12:40 +0000 | [diff] [blame] | 1540 | len += 4; /* "()" plus possible final ".." of named |
| 1541 | varargs (we have + 1 below). */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1542 | for (i = 0; i < macro->paramc; i++) |
Jim Blandy | 64d0826 | 2002-04-05 00:12:40 +0000 | [diff] [blame] | 1543 | len += NODE_LEN (macro->params[i]) + 1; /* "," */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame] | 1546 | for (i = 0; i < macro->count; i++) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1547 | { |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame] | 1548 | cpp_token *token = ¯o->expansion[i]; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1549 | |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame] | 1550 | if (token->type == CPP_MACRO_ARG) |
Neil Booth | a28c5035 | 2001-05-16 22:02:09 +0000 | [diff] [blame] | 1551 | len += NODE_LEN (macro->params[token->val.arg_no - 1]); |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame] | 1552 | else |
| 1553 | len += cpp_token_len (token); /* Includes room for ' '. */ |
| 1554 | if (token->flags & STRINGIFY_ARG) |
| 1555 | len++; /* "#" */ |
| 1556 | if (token->flags & PASTE_LEFT) |
| 1557 | len += 3; /* " ##" */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
| 1560 | if (len > pfile->macro_buffer_len) |
Alexandre Oliva | 4b49c36 | 2001-01-09 09:30:43 +0000 | [diff] [blame] | 1561 | { |
Neil Booth | 562a5c2 | 2002-04-21 18:46:42 +0000 | [diff] [blame] | 1562 | pfile->macro_buffer = (uchar *) xrealloc (pfile->macro_buffer, len); |
Alexandre Oliva | 4b49c36 | 2001-01-09 09:30:43 +0000 | [diff] [blame] | 1563 | pfile->macro_buffer_len = len; |
| 1564 | } |
Neil Booth | 7096171 | 2001-06-23 11:34:41 +0000 | [diff] [blame] | 1565 | |
| 1566 | /* Fill in the buffer. Start with the macro name. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1567 | buffer = pfile->macro_buffer; |
Neil Booth | 7096171 | 2001-06-23 11:34:41 +0000 | [diff] [blame] | 1568 | memcpy (buffer, NODE_NAME (node), NODE_LEN (node)); |
| 1569 | buffer += NODE_LEN (node); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1570 | |
| 1571 | /* Parameter names. */ |
| 1572 | if (macro->fun_like) |
| 1573 | { |
| 1574 | *buffer++ = '('; |
| 1575 | for (i = 0; i < macro->paramc; i++) |
| 1576 | { |
| 1577 | cpp_hashnode *param = macro->params[i]; |
| 1578 | |
| 1579 | if (param != pfile->spec_nodes.n__VA_ARGS__) |
| 1580 | { |
Neil Booth | a28c5035 | 2001-05-16 22:02:09 +0000 | [diff] [blame] | 1581 | memcpy (buffer, NODE_NAME (param), NODE_LEN (param)); |
| 1582 | buffer += NODE_LEN (param); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | if (i + 1 < macro->paramc) |
Jim Blandy | 64d0826 | 2002-04-05 00:12:40 +0000 | [diff] [blame] | 1586 | /* Don't emit a space after the comma here; we're trying |
| 1587 | to emit a Dwarf-friendly definition, and the Dwarf spec |
| 1588 | forbids spaces in the argument list. */ |
| 1589 | *buffer++ = ','; |
Neil Booth | 28e0f04 | 2000-12-09 12:06:37 +0000 | [diff] [blame] | 1590 | else if (macro->variadic) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1591 | *buffer++ = '.', *buffer++ = '.', *buffer++ = '.'; |
| 1592 | } |
| 1593 | *buffer++ = ')'; |
| 1594 | } |
| 1595 | |
Jim Blandy | e37b38d | 2002-03-19 21:43:39 +0000 | [diff] [blame] | 1596 | /* The Dwarf spec requires a space after the macro name, even if the |
| 1597 | definition is the empty string. */ |
| 1598 | *buffer++ = ' '; |
| 1599 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1600 | /* Expansion tokens. */ |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame] | 1601 | if (macro->count) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1602 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1603 | for (i = 0; i < macro->count; i++) |
| 1604 | { |
| 1605 | cpp_token *token = ¯o->expansion[i]; |
| 1606 | |
| 1607 | if (token->flags & PREV_WHITE) |
| 1608 | *buffer++ = ' '; |
| 1609 | if (token->flags & STRINGIFY_ARG) |
| 1610 | *buffer++ = '#'; |
| 1611 | |
| 1612 | if (token->type == CPP_MACRO_ARG) |
| 1613 | { |
Neil Booth | a28c5035 | 2001-05-16 22:02:09 +0000 | [diff] [blame] | 1614 | len = NODE_LEN (macro->params[token->val.arg_no - 1]); |
| 1615 | memcpy (buffer, |
| 1616 | NODE_NAME (macro->params[token->val.arg_no - 1]), len); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1617 | buffer += len; |
| 1618 | } |
| 1619 | else |
| 1620 | buffer = cpp_spell_token (pfile, token, buffer); |
| 1621 | |
| 1622 | if (token->flags & PASTE_LEFT) |
| 1623 | { |
| 1624 | *buffer++ = ' '; |
| 1625 | *buffer++ = '#'; |
| 1626 | *buffer++ = '#'; |
| 1627 | /* Next has PREV_WHITE; see _cpp_create_definition. */ |
| 1628 | } |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | *buffer = '\0'; |
| 1633 | return pfile->macro_buffer; |
| 1634 | } |