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