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