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, |
Tom Tromey | ee38036 | 2007-01-30 15:46:01 +0000 | [diff] [blame] | 3 | 1999, 2000, 2001, 2002, 2003, 2004, 2005, |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 4 | 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 5 | Written by Per Bothner, 1994. |
| 6 | Based on CCCP program by Paul Rubin, June 1986 |
| 7 | Adapted to ANSI C, Richard Stallman, Jan 1987 |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify it |
| 10 | under the terms of the GNU General Public License as published by the |
Jakub Jelinek | 748086b | 2009-04-09 17:00:19 +0200 | [diff] [blame] | 11 | Free Software Foundation; either version 3, or (at your option) any |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 12 | later version. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
Jakub Jelinek | 748086b | 2009-04-09 17:00:19 +0200 | [diff] [blame] | 20 | along with this program; see the file COPYING3. If not see |
| 21 | <http://www.gnu.org/licenses/>. |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 22 | |
| 23 | In other words, you are welcome to use, share and improve this program. |
| 24 | You are forbidden to forbid anyone else to use, share and improve |
| 25 | what you give them. Help stamp out software-hoarding! */ |
| 26 | |
| 27 | #include "config.h" |
| 28 | #include "system.h" |
| 29 | #include "cpplib.h" |
Paolo Bonzini | 4f4e53dd | 2004-05-24 10:50:45 +0000 | [diff] [blame] | 30 | #include "internal.h" |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 31 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 32 | typedef struct macro_arg macro_arg; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 33 | /* This structure represents the tokens of a macro argument. These |
| 34 | tokens can be macro themselves, in which case they can be either |
| 35 | expanded or unexpanded. When they are expanded, this data |
| 36 | structure keeps both the expanded and unexpanded forms. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 37 | struct macro_arg |
| 38 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 39 | const cpp_token **first; /* First token in unexpanded argument. */ |
Kazu Hirata | 6d2f888 | 2001-10-10 11:33:39 +0000 | [diff] [blame] | 40 | const cpp_token **expanded; /* Macro-expanded argument. */ |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 41 | const cpp_token *stringified; /* Stringified argument. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 42 | unsigned int count; /* # of tokens in argument. */ |
| 43 | unsigned int expanded_count; /* # of tokens in expanded argument. */ |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 44 | source_location *virt_locs; /* Where virtual locations for |
| 45 | unexpanded tokens are stored. */ |
| 46 | source_location *expanded_virt_locs; /* Where virtual locations for |
| 47 | expanded tokens are |
| 48 | stored. */ |
| 49 | }; |
| 50 | |
| 51 | /* The kind of macro tokens which the instance of |
| 52 | macro_arg_token_iter is supposed to iterate over. */ |
| 53 | enum macro_arg_token_kind { |
| 54 | MACRO_ARG_TOKEN_NORMAL, |
| 55 | /* This is a macro argument token that got transformed into a string |
| 56 | litteral, e.g. #foo. */ |
| 57 | MACRO_ARG_TOKEN_STRINGIFIED, |
| 58 | /* This is a token resulting from the expansion of a macro |
| 59 | argument that was itself a macro. */ |
| 60 | MACRO_ARG_TOKEN_EXPANDED |
| 61 | }; |
| 62 | |
| 63 | /* An iterator over tokens coming from a function-like macro |
| 64 | argument. */ |
| 65 | typedef struct macro_arg_token_iter macro_arg_token_iter; |
| 66 | struct macro_arg_token_iter |
| 67 | { |
| 68 | /* Whether or not -ftrack-macro-expansion is used. */ |
| 69 | bool track_macro_exp_p; |
| 70 | /* The kind of token over which we are supposed to iterate. */ |
| 71 | enum macro_arg_token_kind kind; |
| 72 | /* A pointer to the current token pointed to by the iterator. */ |
| 73 | const cpp_token **token_ptr; |
| 74 | /* A pointer to the "full" location of the current token. If |
| 75 | -ftrack-macro-expansion is used this location tracks loci accross |
| 76 | macro expansion. */ |
| 77 | const source_location *location_ptr; |
| 78 | #ifdef ENABLE_CHECKING |
| 79 | /* The number of times the iterator went forward. This useful only |
| 80 | when checking is enabled. */ |
| 81 | size_t num_forwards; |
| 82 | #endif |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 83 | }; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 84 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 85 | /* Macro expansion. */ |
| 86 | |
Jakub Jelinek | 765d600 | 2008-01-25 10:01:27 +0100 | [diff] [blame] | 87 | static int enter_macro_context (cpp_reader *, cpp_hashnode *, |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 88 | const cpp_token *, source_location); |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 89 | static int builtin_macro (cpp_reader *, cpp_hashnode *); |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 90 | static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *, |
| 91 | const cpp_token **, unsigned int); |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 92 | static void push_extended_tokens_context (cpp_reader *, cpp_hashnode *, |
| 93 | _cpp_buff *, source_location *, |
| 94 | const cpp_token **, unsigned int); |
Jakub Jelinek | 765d600 | 2008-01-25 10:01:27 +0100 | [diff] [blame] | 95 | static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *, |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 96 | _cpp_buff **, unsigned *); |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 97 | static cpp_context *next_context (cpp_reader *); |
| 98 | static const cpp_token *padding_token (cpp_reader *, const cpp_token *); |
| 99 | static void expand_arg (cpp_reader *, macro_arg *); |
| 100 | static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int); |
| 101 | static const cpp_token *stringify_arg (cpp_reader *, macro_arg *); |
| 102 | static void paste_all_tokens (cpp_reader *, const cpp_token *); |
| 103 | static bool paste_tokens (cpp_reader *, const cpp_token **, const cpp_token *); |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 104 | static void alloc_expanded_arg_mem (cpp_reader *, macro_arg *, size_t); |
| 105 | static void ensure_expanded_arg_room (cpp_reader *, macro_arg *, size_t, size_t *); |
| 106 | static void delete_macro_args (_cpp_buff*, unsigned num_args); |
| 107 | static void set_arg_token (macro_arg *, const cpp_token *, |
| 108 | source_location, size_t, |
| 109 | enum macro_arg_token_kind, |
| 110 | bool); |
| 111 | static const source_location *get_arg_token_location (const macro_arg *, |
| 112 | enum macro_arg_token_kind); |
| 113 | static const cpp_token **arg_token_ptr_at (const macro_arg *, |
| 114 | size_t, |
| 115 | enum macro_arg_token_kind, |
| 116 | source_location **virt_location); |
| 117 | |
| 118 | static void macro_arg_token_iter_init (macro_arg_token_iter *, bool, |
| 119 | enum macro_arg_token_kind, |
| 120 | const macro_arg *, |
| 121 | const cpp_token **); |
| 122 | static const cpp_token *macro_arg_token_iter_get_token |
| 123 | (const macro_arg_token_iter *it); |
| 124 | static source_location macro_arg_token_iter_get_location |
| 125 | (const macro_arg_token_iter *); |
| 126 | static void macro_arg_token_iter_forward (macro_arg_token_iter *); |
| 127 | static _cpp_buff *tokens_buff_new (cpp_reader *, size_t, |
| 128 | source_location **); |
| 129 | static size_t tokens_buff_count (_cpp_buff *); |
| 130 | static const cpp_token **tokens_buff_last_token_ptr (_cpp_buff *); |
| 131 | static const cpp_token **tokens_buff_put_token_to (const cpp_token **, |
| 132 | source_location *, |
| 133 | const cpp_token *, |
| 134 | source_location, |
| 135 | source_location, |
| 136 | const struct line_map *, |
| 137 | unsigned int); |
| 138 | |
| 139 | static const cpp_token **tokens_buff_add_token (_cpp_buff *, |
| 140 | source_location *, |
| 141 | const cpp_token *, |
| 142 | source_location, |
| 143 | source_location, |
| 144 | const struct line_map *, |
| 145 | unsigned int); |
| 146 | static void tokens_buff_remove_last_token (_cpp_buff *); |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 147 | static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *, |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 148 | macro_arg *, source_location); |
Jakub Jelinek | 765d600 | 2008-01-25 10:01:27 +0100 | [diff] [blame] | 149 | static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *, |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 150 | _cpp_buff **, unsigned *); |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 151 | static bool create_iso_definition (cpp_reader *, cpp_macro *); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 152 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 153 | /* #define directive parsing and handling. */ |
| 154 | |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 155 | static cpp_token *alloc_expansion_token (cpp_reader *, cpp_macro *); |
| 156 | static cpp_token *lex_expansion_token (cpp_reader *, cpp_macro *); |
Jakub Jelinek | 8e680db | 2010-06-11 20:37:34 +0200 | [diff] [blame] | 157 | static bool warn_of_redefinition (cpp_reader *, cpp_hashnode *, |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 158 | const cpp_macro *); |
| 159 | static bool parse_params (cpp_reader *, cpp_macro *); |
| 160 | static void check_trad_stringification (cpp_reader *, const cpp_macro *, |
| 161 | const cpp_string *); |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 162 | static bool reached_end_of_context (cpp_context *); |
| 163 | static void consume_next_token_from_context (cpp_reader *pfile, |
| 164 | const cpp_token **, |
| 165 | source_location *); |
| 166 | static const cpp_token* cpp_get_token_1 (cpp_reader *, source_location *); |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 167 | |
Tom Tromey | 64a1a42 | 2011-10-17 09:59:52 +0000 | [diff] [blame^] | 168 | /* Statistical counter tracking the number of macros that got |
| 169 | expanded. */ |
| 170 | unsigned num_expanded_macros_counter = 0; |
| 171 | /* Statistical counter tracking the total number tokens resulting |
| 172 | from macro expansion. */ |
| 173 | unsigned num_macro_tokens_counter = 0; |
| 174 | |
Neil Booth | a69cbaa | 2002-07-23 22:57:49 +0000 | [diff] [blame] | 175 | /* Emits a warning if NODE is a macro defined in the main file that |
| 176 | has not been used. */ |
| 177 | int |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 178 | _cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node, |
| 179 | void *v ATTRIBUTE_UNUSED) |
Neil Booth | a69cbaa | 2002-07-23 22:57:49 +0000 | [diff] [blame] | 180 | { |
| 181 | if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN)) |
| 182 | { |
| 183 | cpp_macro *macro = node->value.macro; |
| 184 | |
| 185 | if (!macro->used |
Per Bothner | 50f59cd | 2004-01-19 21:30:18 -0800 | [diff] [blame] | 186 | && MAIN_FILE_P (linemap_lookup (pfile->line_table, macro->line))) |
Simon Baldwin | 87cf065 | 2010-04-07 17:18:10 +0000 | [diff] [blame] | 187 | cpp_warning_with_line (pfile, CPP_W_UNUSED_MACROS, macro->line, 0, |
| 188 | "macro \"%s\" is not used", NODE_NAME (node)); |
Neil Booth | a69cbaa | 2002-07-23 22:57:49 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | return 1; |
| 192 | } |
| 193 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 194 | /* Allocates and returns a CPP_STRING token, containing TEXT of length |
| 195 | LEN, after null-terminating it. TEXT must be in permanent storage. */ |
| 196 | static const cpp_token * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 197 | new_string_token (cpp_reader *pfile, unsigned char *text, unsigned int len) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 198 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 199 | cpp_token *token = _cpp_temp_token (pfile); |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 200 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 201 | text[len] = '\0'; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 202 | token->type = CPP_STRING; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 203 | token->val.str.len = len; |
| 204 | token->val.str.text = text; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 205 | token->flags = 0; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 206 | return token; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 209 | static const char * const monthnames[] = |
| 210 | { |
| 211 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 212 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 213 | }; |
| 214 | |
Zack Weinberg | 21b1149 | 2004-09-09 19:16:56 +0000 | [diff] [blame] | 215 | /* Helper function for builtin_macro. Returns the text generated by |
| 216 | a builtin macro. */ |
Neil Booth | 278c466 | 2002-06-19 05:40:08 +0000 | [diff] [blame] | 217 | const uchar * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 218 | _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 219 | { |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 220 | const struct line_map *map; |
Neil Booth | 278c466 | 2002-06-19 05:40:08 +0000 | [diff] [blame] | 221 | const uchar *result = NULL; |
Manuel López-Ibáñez | 1bb6466 | 2008-07-21 09:33:38 +0000 | [diff] [blame] | 222 | linenum_type number = 1; |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 223 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 224 | switch (node->value.builtin) |
| 225 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 226 | default: |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 227 | cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"", |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 228 | NODE_NAME (node)); |
Neil Booth | 278c466 | 2002-06-19 05:40:08 +0000 | [diff] [blame] | 229 | break; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 230 | |
Grigory Zagorodnev | be8ac3e | 2006-02-18 09:25:31 +0000 | [diff] [blame] | 231 | case BT_TIMESTAMP: |
| 232 | { |
| 233 | cpp_buffer *pbuffer = cpp_get_buffer (pfile); |
| 234 | if (pbuffer->timestamp == NULL) |
| 235 | { |
| 236 | /* Initialize timestamp value of the assotiated file. */ |
| 237 | struct _cpp_file *file = cpp_get_file (pbuffer); |
| 238 | if (file) |
| 239 | { |
| 240 | /* Generate __TIMESTAMP__ string, that represents |
| 241 | the date and time of the last modification |
| 242 | of the current source file. The string constant |
| 243 | looks like "Sun Sep 16 01:03:52 1973". */ |
| 244 | struct tm *tb = NULL; |
| 245 | struct stat *st = _cpp_get_file_stat (file); |
| 246 | if (st) |
| 247 | tb = localtime (&st->st_mtime); |
| 248 | if (tb) |
| 249 | { |
| 250 | char *str = asctime (tb); |
| 251 | size_t len = strlen (str); |
| 252 | unsigned char *buf = _cpp_unaligned_alloc (pfile, len + 2); |
| 253 | buf[0] = '"'; |
| 254 | strcpy ((char *) buf + 1, str); |
| 255 | buf[len] = '"'; |
| 256 | pbuffer->timestamp = buf; |
| 257 | } |
| 258 | else |
| 259 | { |
| 260 | cpp_errno (pfile, CPP_DL_WARNING, |
| 261 | "could not determine file timestamp"); |
Kris Van Hees | b6baa67 | 2008-04-18 13:58:08 +0000 | [diff] [blame] | 262 | pbuffer->timestamp = UC"\"??? ??? ?? ??:??:?? ????\""; |
Grigory Zagorodnev | be8ac3e | 2006-02-18 09:25:31 +0000 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | } |
| 266 | result = pbuffer->timestamp; |
| 267 | } |
| 268 | break; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 269 | case BT_FILE: |
| 270 | case BT_BASE_FILE: |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 271 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 272 | unsigned int len; |
Neil Booth | 0bda476 | 2000-12-11 07:45:16 +0000 | [diff] [blame] | 273 | const char *name; |
Neil Booth | 562a5c2 | 2002-04-21 18:46:42 +0000 | [diff] [blame] | 274 | uchar *buf; |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 275 | |
| 276 | if (node->value.builtin == BT_FILE) |
| 277 | name = linemap_get_expansion_filename (pfile->line_table, |
| 278 | pfile->line_table->highest_line); |
| 279 | else |
| 280 | { |
| 281 | map = linemap_lookup (pfile->line_table, pfile->line_table->highest_line); |
| 282 | while (! MAIN_FILE_P (map)) |
| 283 | map = INCLUDED_FROM (pfile->line_table, map); |
| 284 | name = ORDINARY_MAP_FILE_NAME (map); |
| 285 | } |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 286 | len = strlen (name); |
Andrew Pinski | 651ed94 | 2005-11-04 00:23:01 +0000 | [diff] [blame] | 287 | buf = _cpp_unaligned_alloc (pfile, len * 2 + 3); |
Neil Booth | 278c466 | 2002-06-19 05:40:08 +0000 | [diff] [blame] | 288 | result = buf; |
| 289 | *buf = '"'; |
| 290 | buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len); |
| 291 | *buf++ = '"'; |
| 292 | *buf = '\0'; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 293 | } |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 294 | break; |
| 295 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 296 | case BT_INCLUDE_LEVEL: |
Neil Booth | d8693c6 | 2001-08-21 23:05:12 +0000 | [diff] [blame] | 297 | /* The line map depth counts the primary source as level 1, but |
| 298 | historically __INCLUDE_DEPTH__ has called the primary source |
| 299 | level 0. */ |
Per Bothner | 50f59cd | 2004-01-19 21:30:18 -0800 | [diff] [blame] | 300 | number = pfile->line_table->depth - 1; |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 301 | break; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 302 | |
| 303 | case BT_SPECLINE: |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 304 | map = LINEMAPS_LAST_ORDINARY_MAP (pfile->line_table); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 305 | /* If __LINE__ is embedded in a macro, it must expand to the |
| 306 | line of the macro's invocation, not its definition. |
| 307 | Otherwise things like assert() will not work properly. */ |
Tom Tromey | 4642737 | 2011-10-17 11:58:56 +0200 | [diff] [blame] | 308 | number = linemap_get_expansion_line (pfile->line_table, |
| 309 | CPP_OPTION (pfile, traditional) |
| 310 | ? pfile->line_table->highest_line |
| 311 | : pfile->cur_token[-1].src_loc); |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 312 | break; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 313 | |
Zack Weinberg | 5279d73 | 2002-05-16 19:03:02 +0000 | [diff] [blame] | 314 | /* __STDC__ has the value 1 under normal circumstances. |
| 315 | However, if (a) we are in a system header, (b) the option |
Zack Weinberg | 2a1dc0d | 2002-05-21 21:55:37 +0000 | [diff] [blame] | 316 | stdc_0_in_system_headers is true (set by target config), and |
| 317 | (c) we are not in strictly conforming mode, then it has the |
Jakub Jelinek | 8390099 | 2006-01-23 22:50:15 +0100 | [diff] [blame] | 318 | value 0. (b) and (c) are already checked in cpp_init_builtins. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 319 | case BT_STDC: |
Jakub Jelinek | 8390099 | 2006-01-23 22:50:15 +0100 | [diff] [blame] | 320 | if (cpp_in_system_header (pfile)) |
| 321 | number = 0; |
| 322 | else |
| 323 | number = 1; |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 324 | break; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 325 | |
| 326 | case BT_DATE: |
| 327 | case BT_TIME: |
Neil Booth | 278c466 | 2002-06-19 05:40:08 +0000 | [diff] [blame] | 328 | if (pfile->date == NULL) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 329 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 330 | /* Allocate __DATE__ and __TIME__ strings from permanent |
| 331 | storage. We only do this once, and don't generate them |
| 332 | at init time, because time() and localtime() are very |
| 333 | slow on some systems. */ |
Zack Weinberg | 56da720 | 2002-08-02 04:18:16 +0000 | [diff] [blame] | 334 | time_t tt; |
| 335 | struct tm *tb = NULL; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 336 | |
Zack Weinberg | 56da720 | 2002-08-02 04:18:16 +0000 | [diff] [blame] | 337 | /* (time_t) -1 is a legitimate value for "number of seconds |
| 338 | since the Epoch", so we have to do a little dance to |
| 339 | distinguish that from a genuine error. */ |
| 340 | errno = 0; |
| 341 | tt = time(NULL); |
| 342 | if (tt != (time_t)-1 || errno == 0) |
| 343 | tb = localtime (&tt); |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 344 | |
Zack Weinberg | 56da720 | 2002-08-02 04:18:16 +0000 | [diff] [blame] | 345 | if (tb) |
| 346 | { |
| 347 | pfile->date = _cpp_unaligned_alloc (pfile, |
| 348 | sizeof ("\"Oct 11 1347\"")); |
| 349 | sprintf ((char *) pfile->date, "\"%s %2d %4d\"", |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 350 | monthnames[tb->tm_mon], tb->tm_mday, |
| 351 | tb->tm_year + 1900); |
Zack Weinberg | 56da720 | 2002-08-02 04:18:16 +0000 | [diff] [blame] | 352 | |
| 353 | pfile->time = _cpp_unaligned_alloc (pfile, |
| 354 | sizeof ("\"12:34:56\"")); |
| 355 | sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"", |
| 356 | tb->tm_hour, tb->tm_min, tb->tm_sec); |
| 357 | } |
| 358 | else |
| 359 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 360 | cpp_errno (pfile, CPP_DL_WARNING, |
Zack Weinberg | 56da720 | 2002-08-02 04:18:16 +0000 | [diff] [blame] | 361 | "could not determine date and time"); |
| 362 | |
Kris Van Hees | b6baa67 | 2008-04-18 13:58:08 +0000 | [diff] [blame] | 363 | pfile->date = UC"\"??? ?? ????\""; |
| 364 | pfile->time = UC"\"??:??:??\""; |
Zack Weinberg | 56da720 | 2002-08-02 04:18:16 +0000 | [diff] [blame] | 365 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 366 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 367 | |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 368 | if (node->value.builtin == BT_DATE) |
Neil Booth | 278c466 | 2002-06-19 05:40:08 +0000 | [diff] [blame] | 369 | result = pfile->date; |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 370 | else |
Neil Booth | 278c466 | 2002-06-19 05:40:08 +0000 | [diff] [blame] | 371 | result = pfile->time; |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 372 | break; |
Ollie Wild | a702045 | 2007-05-24 20:55:36 +0000 | [diff] [blame] | 373 | |
| 374 | case BT_COUNTER: |
Ollie Wild | ccfc4c9 | 2007-07-30 18:29:20 +0000 | [diff] [blame] | 375 | if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive) |
| 376 | cpp_error (pfile, CPP_DL_ERROR, |
| 377 | "__COUNTER__ expanded inside directive with -fdirectives-only"); |
Ollie Wild | a702045 | 2007-05-24 20:55:36 +0000 | [diff] [blame] | 378 | number = pfile->counter++; |
| 379 | break; |
Neil Booth | 278c466 | 2002-06-19 05:40:08 +0000 | [diff] [blame] | 380 | } |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 381 | |
Neil Booth | 278c466 | 2002-06-19 05:40:08 +0000 | [diff] [blame] | 382 | if (result == NULL) |
| 383 | { |
| 384 | /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */ |
| 385 | result = _cpp_unaligned_alloc (pfile, 21); |
| 386 | sprintf ((char *) result, "%u", number); |
| 387 | } |
| 388 | |
| 389 | return result; |
| 390 | } |
| 391 | |
| 392 | /* 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] | 393 | context stack. Also handles _Pragma, for which a new token may not |
| 394 | 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] | 395 | return the token to the caller. */ |
| 396 | static int |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 397 | builtin_macro (cpp_reader *pfile, cpp_hashnode *node) |
Neil Booth | 278c466 | 2002-06-19 05:40:08 +0000 | [diff] [blame] | 398 | { |
| 399 | const uchar *buf; |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 400 | size_t len; |
| 401 | char *nbuf; |
Neil Booth | 278c466 | 2002-06-19 05:40:08 +0000 | [diff] [blame] | 402 | |
| 403 | if (node->value.builtin == BT_PRAGMA) |
| 404 | { |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 405 | /* Don't interpret _Pragma within directives. The standard is |
| 406 | not clear on this, but to me this makes most sense. */ |
| 407 | if (pfile->state.in_directive) |
| 408 | return 0; |
| 409 | |
Tom Tromey | 5b9a40d | 2007-10-31 14:50:13 +0000 | [diff] [blame] | 410 | return _cpp_do__Pragma (pfile); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 411 | } |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 412 | |
Neil Booth | 278c466 | 2002-06-19 05:40:08 +0000 | [diff] [blame] | 413 | buf = _cpp_builtin_macro_text (pfile, node); |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 414 | len = ustrlen (buf); |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 415 | nbuf = (char *) alloca (len + 1); |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 416 | memcpy (nbuf, buf, len); |
| 417 | nbuf[len]='\n'; |
Neil Booth | 278c466 | 2002-06-19 05:40:08 +0000 | [diff] [blame] | 418 | |
Per Bothner | 40de9f7 | 2003-10-02 07:30:34 +0000 | [diff] [blame] | 419 | cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true); |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 420 | _cpp_clean_line (pfile); |
Neil Booth | 278c466 | 2002-06-19 05:40:08 +0000 | [diff] [blame] | 421 | |
| 422 | /* Set pfile->cur_token as required by _cpp_lex_direct. */ |
| 423 | pfile->cur_token = _cpp_temp_token (pfile); |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 424 | _cpp_push_token_context (pfile, NULL, _cpp_lex_direct (pfile), 1); |
Neil Booth | 278c466 | 2002-06-19 05:40:08 +0000 | [diff] [blame] | 425 | if (pfile->buffer->cur != pfile->buffer->rlimit) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 426 | cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"", |
Neil Booth | 278c466 | 2002-06-19 05:40:08 +0000 | [diff] [blame] | 427 | NODE_NAME (node)); |
| 428 | _cpp_pop_buffer (pfile); |
| 429 | |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 430 | return 1; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 433 | /* Copies SRC, of length LEN, to DEST, adding backslashes before all |
Andrew Pinski | 651ed94 | 2005-11-04 00:23:01 +0000 | [diff] [blame] | 434 | backslashes and double quotes. DEST must be of sufficient size. |
| 435 | Returns a pointer to the end of the string. */ |
Neil Booth | 562a5c2 | 2002-04-21 18:46:42 +0000 | [diff] [blame] | 436 | uchar * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 437 | cpp_quote_string (uchar *dest, const uchar *src, unsigned int len) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 438 | { |
| 439 | while (len--) |
| 440 | { |
Neil Booth | 562a5c2 | 2002-04-21 18:46:42 +0000 | [diff] [blame] | 441 | uchar c = *src++; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 442 | |
| 443 | if (c == '\\' || c == '"') |
| 444 | { |
| 445 | *dest++ = '\\'; |
| 446 | *dest++ = c; |
| 447 | } |
| 448 | else |
Andrew Pinski | 651ed94 | 2005-11-04 00:23:01 +0000 | [diff] [blame] | 449 | *dest++ = c; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | return dest; |
| 453 | } |
| 454 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 455 | /* Convert a token sequence ARG to a single string token according to |
| 456 | the rules of the ISO C #-operator. */ |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 457 | static const cpp_token * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 458 | stringify_arg (cpp_reader *pfile, macro_arg *arg) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 459 | { |
Neil Booth | 6338b35 | 2003-04-23 22:44:06 +0000 | [diff] [blame] | 460 | unsigned char *dest; |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 461 | unsigned int i, escape_it, backslash_count = 0; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 462 | const cpp_token *source = NULL; |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 463 | size_t len; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 464 | |
Neil Booth | 6338b35 | 2003-04-23 22:44:06 +0000 | [diff] [blame] | 465 | if (BUFF_ROOM (pfile->u_buff) < 3) |
| 466 | _cpp_extend_buff (pfile, &pfile->u_buff, 3); |
| 467 | dest = BUFF_FRONT (pfile->u_buff); |
| 468 | *dest++ = '"'; |
| 469 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 470 | /* Loop, reading in the argument's tokens. */ |
| 471 | for (i = 0; i < arg->count; i++) |
| 472 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 473 | const cpp_token *token = arg->first[i]; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 474 | |
| 475 | if (token->type == CPP_PADDING) |
| 476 | { |
Joseph Myers | 18f41a1 | 2009-04-12 23:20:02 +0100 | [diff] [blame] | 477 | if (source == NULL |
| 478 | || (!(source->flags & PREV_WHITE) |
| 479 | && token->val.source == NULL)) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 480 | source = token->val.source; |
| 481 | continue; |
| 482 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 483 | |
Kris Van Hees | b6baa67 | 2008-04-18 13:58:08 +0000 | [diff] [blame] | 484 | escape_it = (token->type == CPP_STRING || token->type == CPP_CHAR |
Ian Lance Taylor | fd2ab21 | 2009-09-02 17:35:30 +0000 | [diff] [blame] | 485 | || token->type == CPP_WSTRING || token->type == CPP_WCHAR |
Kris Van Hees | b6baa67 | 2008-04-18 13:58:08 +0000 | [diff] [blame] | 486 | || token->type == CPP_STRING32 || token->type == CPP_CHAR32 |
Jakub Jelinek | 2c6e3f5 | 2009-10-19 23:41:15 +0200 | [diff] [blame] | 487 | || token->type == CPP_STRING16 || token->type == CPP_CHAR16 |
| 488 | || token->type == CPP_UTF8STRING); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 489 | |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 490 | /* Room for each char being written in octal, initial space and |
Neil Booth | 6338b35 | 2003-04-23 22:44:06 +0000 | [diff] [blame] | 491 | final quote and NUL. */ |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 492 | len = cpp_token_len (token); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 493 | if (escape_it) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 494 | len *= 4; |
Neil Booth | 6338b35 | 2003-04-23 22:44:06 +0000 | [diff] [blame] | 495 | len += 3; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 496 | |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 497 | if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 498 | { |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 499 | size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff); |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 500 | _cpp_extend_buff (pfile, &pfile->u_buff, len); |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 501 | dest = BUFF_FRONT (pfile->u_buff) + len_so_far; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 504 | /* Leading white space? */ |
Neil Booth | 6338b35 | 2003-04-23 22:44:06 +0000 | [diff] [blame] | 505 | if (dest - 1 != BUFF_FRONT (pfile->u_buff)) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 506 | { |
| 507 | if (source == NULL) |
| 508 | source = token; |
| 509 | if (source->flags & PREV_WHITE) |
| 510 | *dest++ = ' '; |
| 511 | } |
| 512 | source = NULL; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 513 | |
| 514 | if (escape_it) |
| 515 | { |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 516 | _cpp_buff *buff = _cpp_get_buff (pfile, len); |
| 517 | unsigned char *buf = BUFF_FRONT (buff); |
Geoffrey Keating | 47e2049 | 2005-03-12 10:44:06 +0000 | [diff] [blame] | 518 | len = cpp_spell_token (pfile, token, buf, true) - buf; |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 519 | dest = cpp_quote_string (dest, buf, len); |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 520 | _cpp_release_buff (pfile, buff); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 521 | } |
| 522 | else |
Geoffrey Keating | 47e2049 | 2005-03-12 10:44:06 +0000 | [diff] [blame] | 523 | dest = cpp_spell_token (pfile, token, dest, true); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 524 | |
Neil Booth | 1067694 | 2003-04-22 19:28:00 +0000 | [diff] [blame] | 525 | if (token->type == CPP_OTHER && token->val.str.text[0] == '\\') |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 526 | backslash_count++; |
| 527 | else |
| 528 | backslash_count = 0; |
| 529 | } |
| 530 | |
| 531 | /* Ignore the final \ of invalid string literals. */ |
| 532 | if (backslash_count & 1) |
| 533 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 534 | cpp_error (pfile, CPP_DL_WARNING, |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 535 | "invalid string literal, ignoring final '\\'"); |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 536 | dest--; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 539 | /* Commit the memory, including NUL, and return the token. */ |
Neil Booth | 6338b35 | 2003-04-23 22:44:06 +0000 | [diff] [blame] | 540 | *dest++ = '"'; |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 541 | len = dest - BUFF_FRONT (pfile->u_buff); |
| 542 | BUFF_FRONT (pfile->u_buff) = dest + 1; |
| 543 | return new_string_token (pfile, dest - len, len); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Kazu Hirata | da7d830 | 2002-09-22 02:03:17 +0000 | [diff] [blame] | 546 | /* Try to paste two tokens. On success, return nonzero. In any |
Neil Booth | c9e7a60 | 2001-09-27 12:59:38 +0000 | [diff] [blame] | 547 | case, PLHS is updated to point to the pasted token, which is |
| 548 | guaranteed to not have the PASTE_LEFT flag set. */ |
| 549 | static bool |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 550 | 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] | 551 | { |
Jakub Jelinek | de000d2 | 2006-10-12 11:25:59 +0200 | [diff] [blame] | 552 | unsigned char *buf, *end, *lhsend; |
Tom Tromey | fca35e1 | 2007-05-02 19:33:44 +0000 | [diff] [blame] | 553 | cpp_token *lhs; |
Neil Booth | c9e7a60 | 2001-09-27 12:59:38 +0000 | [diff] [blame] | 554 | unsigned int len; |
Neil Booth | d63eefb | 2000-11-20 23:59:26 +0000 | [diff] [blame] | 555 | |
Tom Tromey | fca35e1 | 2007-05-02 19:33:44 +0000 | [diff] [blame] | 556 | len = cpp_token_len (*plhs) + cpp_token_len (rhs) + 1; |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 557 | buf = (unsigned char *) alloca (len); |
Tom Tromey | fca35e1 | 2007-05-02 19:33:44 +0000 | [diff] [blame] | 558 | end = lhsend = cpp_spell_token (pfile, *plhs, buf, false); |
Neil Booth | d63eefb | 2000-11-20 23:59:26 +0000 | [diff] [blame] | 559 | |
Neil Booth | c9e7a60 | 2001-09-27 12:59:38 +0000 | [diff] [blame] | 560 | /* Avoid comment headers, since they are still processed in stage 3. |
| 561 | It is simpler to insert a space here, rather than modifying the |
| 562 | lexer to ignore comments in some circumstances. Simply returning |
| 563 | false doesn't work, since we want to clear the PASTE_LEFT flag. */ |
Tom Tromey | fca35e1 | 2007-05-02 19:33:44 +0000 | [diff] [blame] | 564 | if ((*plhs)->type == CPP_DIV && rhs->type != CPP_EQ) |
Neil Booth | c9e7a60 | 2001-09-27 12:59:38 +0000 | [diff] [blame] | 565 | *end++ = ' '; |
Tom Tromey | f373b44 | 2007-11-01 18:20:48 +0000 | [diff] [blame] | 566 | /* In one obscure case we might see padding here. */ |
| 567 | if (rhs->type != CPP_PADDING) |
| 568 | end = cpp_spell_token (pfile, rhs, end, false); |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 569 | *end = '\n'; |
Neil Booth | d63eefb | 2000-11-20 23:59:26 +0000 | [diff] [blame] | 570 | |
Per Bothner | 40de9f7 | 2003-10-02 07:30:34 +0000 | [diff] [blame] | 571 | cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true); |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 572 | _cpp_clean_line (pfile); |
Neil Booth | d63eefb | 2000-11-20 23:59:26 +0000 | [diff] [blame] | 573 | |
Neil Booth | c9e7a60 | 2001-09-27 12:59:38 +0000 | [diff] [blame] | 574 | /* Set pfile->cur_token as required by _cpp_lex_direct. */ |
| 575 | pfile->cur_token = _cpp_temp_token (pfile); |
Tom Tromey | fca35e1 | 2007-05-02 19:33:44 +0000 | [diff] [blame] | 576 | lhs = _cpp_lex_direct (pfile); |
Jakub Jelinek | de000d2 | 2006-10-12 11:25:59 +0200 | [diff] [blame] | 577 | if (pfile->buffer->cur != pfile->buffer->rlimit) |
| 578 | { |
Tom Tromey | fca35e1 | 2007-05-02 19:33:44 +0000 | [diff] [blame] | 579 | source_location saved_loc = lhs->src_loc; |
| 580 | |
Jakub Jelinek | de000d2 | 2006-10-12 11:25:59 +0200 | [diff] [blame] | 581 | _cpp_pop_buffer (pfile); |
| 582 | _cpp_backup_tokens (pfile, 1); |
| 583 | *lhsend = '\0'; |
Neil Booth | d63eefb | 2000-11-20 23:59:26 +0000 | [diff] [blame] | 584 | |
Tom Tromey | fca35e1 | 2007-05-02 19:33:44 +0000 | [diff] [blame] | 585 | /* We have to remove the PASTE_LEFT flag from the old lhs, but |
| 586 | we want to keep the new location. */ |
| 587 | *lhs = **plhs; |
| 588 | *plhs = lhs; |
| 589 | lhs->src_loc = saved_loc; |
| 590 | lhs->flags &= ~PASTE_LEFT; |
| 591 | |
Jakub Jelinek | de000d2 | 2006-10-12 11:25:59 +0200 | [diff] [blame] | 592 | /* Mandatory error for all apart from assembler. */ |
| 593 | if (CPP_OPTION (pfile, lang) != CLK_ASM) |
| 594 | cpp_error (pfile, CPP_DL_ERROR, |
| 595 | "pasting \"%s\" and \"%s\" does not give a valid preprocessing token", |
| 596 | buf, cpp_token_as_text (pfile, rhs)); |
| 597 | return false; |
| 598 | } |
| 599 | |
Tom Tromey | fca35e1 | 2007-05-02 19:33:44 +0000 | [diff] [blame] | 600 | *plhs = lhs; |
Jakub Jelinek | de000d2 | 2006-10-12 11:25:59 +0200 | [diff] [blame] | 601 | _cpp_pop_buffer (pfile); |
| 602 | return true; |
Neil Booth | d63eefb | 2000-11-20 23:59:26 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 605 | /* Handles an arbitrarily long sequence of ## operators, with initial |
| 606 | operand LHS. This implementation is left-associative, |
| 607 | non-recursive, and finishes a paste before handling succeeding |
| 608 | ones. If a paste fails, we back up to the RHS of the failing ## |
| 609 | operator before pushing the context containing the result of prior |
| 610 | successful pastes, with the effect that the RHS appears in the |
| 611 | output stream after the pasted LHS normally. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 612 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 613 | paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 614 | { |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 615 | const cpp_token *rhs = NULL; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 616 | cpp_context *context = pfile->context; |
| 617 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 618 | do |
| 619 | { |
| 620 | /* Take the token directly from the current context. We can do |
| 621 | this, because we are in the replacement list of either an |
| 622 | object-like macro, or a function-like macro with arguments |
| 623 | inserted. In either case, the constraints to #define |
Neil Booth | d63eefb | 2000-11-20 23:59:26 +0000 | [diff] [blame] | 624 | guarantee we have at least one more token. */ |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 625 | if (context->tokens_kind == TOKENS_KIND_DIRECT) |
Neil Booth | 82eda77 | 2002-06-04 13:07:06 +0000 | [diff] [blame] | 626 | rhs = FIRST (context).token++; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 627 | else if (context->tokens_kind == TOKENS_KIND_INDIRECT) |
Neil Booth | 82eda77 | 2002-06-04 13:07:06 +0000 | [diff] [blame] | 628 | rhs = *FIRST (context).ptoken++; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 629 | else if (context->tokens_kind == TOKENS_KIND_EXTENDED) |
| 630 | { |
| 631 | /* So we are in presence of an extended token context, which |
| 632 | means that each token in this context has a virtual |
| 633 | location attached to it. So let's not forget to update |
| 634 | the pointer to the current virtual location of the |
| 635 | current token when we update the pointer to the current |
| 636 | token */ |
| 637 | |
| 638 | rhs = *FIRST (context).ptoken++; |
| 639 | /* context->c.mc must be non-null, as if we were not in a |
| 640 | macro context, context->tokens_kind could not be equal to |
| 641 | TOKENS_KIND_EXTENDED. */ |
| 642 | context->c.mc->cur_virt_loc++; |
| 643 | } |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 644 | |
| 645 | if (rhs->type == CPP_PADDING) |
Tom Tromey | f373b44 | 2007-11-01 18:20:48 +0000 | [diff] [blame] | 646 | { |
| 647 | if (rhs->flags & PASTE_LEFT) |
| 648 | abort (); |
| 649 | } |
Neil Booth | c9e7a60 | 2001-09-27 12:59:38 +0000 | [diff] [blame] | 650 | if (!paste_tokens (pfile, &lhs, rhs)) |
Jakub Jelinek | de000d2 | 2006-10-12 11:25:59 +0200 | [diff] [blame] | 651 | break; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 652 | } |
| 653 | while (rhs->flags & PASTE_LEFT); |
| 654 | |
Neil Booth | c9e7a60 | 2001-09-27 12:59:38 +0000 | [diff] [blame] | 655 | /* Put the resulting token in its own context. */ |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 656 | _cpp_push_token_context (pfile, NULL, lhs, 1); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Neil Booth | 1ce676a | 2002-06-09 20:04:17 +0000 | [diff] [blame] | 659 | /* Returns TRUE if the number of arguments ARGC supplied in an |
| 660 | invocation of the MACRO referenced by NODE is valid. An empty |
| 661 | invocation to a macro with no parameters should pass ARGC as zero. |
| 662 | |
| 663 | Note that MACRO cannot necessarily be deduced from NODE, in case |
| 664 | NODE was redefined whilst collecting arguments. */ |
| 665 | bool |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 666 | _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] | 667 | { |
| 668 | if (argc == macro->paramc) |
| 669 | return true; |
| 670 | |
| 671 | if (argc < macro->paramc) |
| 672 | { |
| 673 | /* As an extension, a rest argument is allowed to not appear in |
| 674 | the invocation at all. |
| 675 | e.g. #define debug(format, args...) something |
| 676 | debug("string"); |
| 677 | |
| 678 | This is exactly the same as if there had been an empty rest |
| 679 | argument - debug("string", ). */ |
| 680 | |
| 681 | if (argc + 1 == macro->paramc && macro->variadic) |
| 682 | { |
| 683 | if (CPP_PEDANTIC (pfile) && ! macro->syshdr) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 684 | cpp_error (pfile, CPP_DL_PEDWARN, |
Neil Booth | 1ce676a | 2002-06-09 20:04:17 +0000 | [diff] [blame] | 685 | "ISO C99 requires rest arguments to be used"); |
| 686 | return true; |
| 687 | } |
| 688 | |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 689 | cpp_error (pfile, CPP_DL_ERROR, |
Neil Booth | 1ce676a | 2002-06-09 20:04:17 +0000 | [diff] [blame] | 690 | "macro \"%s\" requires %u arguments, but only %u given", |
| 691 | NODE_NAME (node), macro->paramc, argc); |
| 692 | } |
| 693 | else |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 694 | cpp_error (pfile, CPP_DL_ERROR, |
Neil Booth | 1ce676a | 2002-06-09 20:04:17 +0000 | [diff] [blame] | 695 | "macro \"%s\" passed %u arguments, but takes just %u", |
| 696 | NODE_NAME (node), argc, macro->paramc); |
| 697 | |
| 698 | return false; |
| 699 | } |
| 700 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 701 | /* Reads and returns the arguments to a function-like macro |
| 702 | invocation. Assumes the opening parenthesis has been processed. |
| 703 | If there is an error, emits an appropriate diagnostic and returns |
| 704 | NULL. Each argument is terminated by a CPP_EOF token, for the |
Jakub Jelinek | 765d600 | 2008-01-25 10:01:27 +0100 | [diff] [blame] | 705 | future benefit of expand_arg(). If there are any deferred |
| 706 | #pragma directives among macro arguments, store pointers to the |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 707 | CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer. |
| 708 | |
| 709 | What is returned is the buffer that contains the memory allocated |
| 710 | to hold the macro arguments. NODE is the name of the macro this |
| 711 | function is dealing with. If NUM_ARGS is non-NULL, *NUM_ARGS is |
| 712 | set to the actual number of macro arguments allocated in the |
| 713 | returned buffer. */ |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 714 | static _cpp_buff * |
Jakub Jelinek | 765d600 | 2008-01-25 10:01:27 +0100 | [diff] [blame] | 715 | collect_args (cpp_reader *pfile, const cpp_hashnode *node, |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 716 | _cpp_buff **pragma_buff, unsigned *num_args) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 717 | { |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 718 | _cpp_buff *buff, *base_buff; |
| 719 | cpp_macro *macro; |
| 720 | macro_arg *args, *arg; |
| 721 | const cpp_token *token; |
| 722 | unsigned int argc; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 723 | source_location virt_loc; |
| 724 | bool track_macro_expansion_p = CPP_OPTION (pfile, track_macro_expansion); |
| 725 | unsigned num_args_alloced = 0; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 726 | |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 727 | macro = node->value.macro; |
| 728 | if (macro->paramc) |
| 729 | argc = macro->paramc; |
| 730 | else |
| 731 | argc = 1; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 732 | |
| 733 | #define DEFAULT_NUM_TOKENS_PER_MACRO_ARG 50 |
| 734 | #define ARG_TOKENS_EXTENT 1000 |
| 735 | |
| 736 | buff = _cpp_get_buff (pfile, argc * (DEFAULT_NUM_TOKENS_PER_MACRO_ARG |
| 737 | * sizeof (cpp_token *) |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 738 | + sizeof (macro_arg))); |
| 739 | base_buff = buff; |
| 740 | args = (macro_arg *) buff->base; |
| 741 | memset (args, 0, argc * sizeof (macro_arg)); |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 742 | buff->cur = (unsigned char *) &args[argc]; |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 743 | arg = args, argc = 0; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 744 | |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 745 | /* Collect the tokens making up each argument. We don't yet know |
| 746 | how many arguments have been supplied, whether too many or too |
| 747 | few. Hence the slightly bizarre usage of "argc" and "arg". */ |
| 748 | do |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 749 | { |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 750 | unsigned int paren_depth = 0; |
| 751 | unsigned int ntokens = 0; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 752 | unsigned virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG; |
| 753 | num_args_alloced++; |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 754 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 755 | argc++; |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 756 | arg->first = (const cpp_token **) buff->cur; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 757 | if (track_macro_expansion_p) |
| 758 | { |
| 759 | virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG; |
| 760 | arg->virt_locs = XNEWVEC (source_location, |
| 761 | virt_locs_capacity); |
| 762 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 763 | |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 764 | for (;;) |
| 765 | { |
| 766 | /* Require space for 2 new tokens (including a CPP_EOF). */ |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 767 | if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit) |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 768 | { |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 769 | buff = _cpp_append_extend_buff (pfile, buff, |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 770 | ARG_TOKENS_EXTENT |
| 771 | * sizeof (cpp_token *)); |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 772 | arg->first = (const cpp_token **) buff->cur; |
| 773 | } |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 774 | if (track_macro_expansion_p |
| 775 | && (ntokens + 2 > virt_locs_capacity)) |
| 776 | { |
| 777 | virt_locs_capacity += ARG_TOKENS_EXTENT; |
| 778 | arg->virt_locs = XRESIZEVEC (source_location, |
| 779 | arg->virt_locs, |
| 780 | virt_locs_capacity); |
| 781 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 782 | |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 783 | token = cpp_get_token_1 (pfile, &virt_loc); |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 784 | |
| 785 | if (token->type == CPP_PADDING) |
| 786 | { |
| 787 | /* Drop leading padding. */ |
| 788 | if (ntokens == 0) |
| 789 | continue; |
| 790 | } |
| 791 | else if (token->type == CPP_OPEN_PAREN) |
| 792 | paren_depth++; |
| 793 | else if (token->type == CPP_CLOSE_PAREN) |
| 794 | { |
| 795 | if (paren_depth-- == 0) |
| 796 | break; |
| 797 | } |
| 798 | else if (token->type == CPP_COMMA) |
| 799 | { |
| 800 | /* A comma does not terminate an argument within |
| 801 | parentheses or as part of a variable argument. */ |
| 802 | if (paren_depth == 0 |
| 803 | && ! (macro->variadic && argc == macro->paramc)) |
| 804 | break; |
| 805 | } |
| 806 | else if (token->type == CPP_EOF |
| 807 | || (token->type == CPP_HASH && token->flags & BOL)) |
| 808 | break; |
Jakub Jelinek | 765d600 | 2008-01-25 10:01:27 +0100 | [diff] [blame] | 809 | else if (token->type == CPP_PRAGMA) |
| 810 | { |
| 811 | cpp_token *newtok = _cpp_temp_token (pfile); |
| 812 | |
| 813 | /* CPP_PRAGMA token lives in directive_result, which will |
| 814 | be overwritten on the next directive. */ |
| 815 | *newtok = *token; |
| 816 | token = newtok; |
| 817 | do |
| 818 | { |
| 819 | if (*pragma_buff == NULL |
| 820 | || BUFF_ROOM (*pragma_buff) < sizeof (cpp_token *)) |
| 821 | { |
| 822 | _cpp_buff *next; |
| 823 | if (*pragma_buff == NULL) |
| 824 | *pragma_buff |
| 825 | = _cpp_get_buff (pfile, 32 * sizeof (cpp_token *)); |
| 826 | else |
| 827 | { |
| 828 | next = *pragma_buff; |
| 829 | *pragma_buff |
| 830 | = _cpp_get_buff (pfile, |
| 831 | (BUFF_FRONT (*pragma_buff) |
| 832 | - (*pragma_buff)->base) * 2); |
| 833 | (*pragma_buff)->next = next; |
| 834 | } |
| 835 | } |
| 836 | *(const cpp_token **) BUFF_FRONT (*pragma_buff) = token; |
| 837 | BUFF_FRONT (*pragma_buff) += sizeof (cpp_token *); |
| 838 | if (token->type == CPP_PRAGMA_EOL) |
| 839 | break; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 840 | token = cpp_get_token_1 (pfile, &virt_loc); |
Jakub Jelinek | 765d600 | 2008-01-25 10:01:27 +0100 | [diff] [blame] | 841 | } |
| 842 | while (token->type != CPP_EOF); |
| 843 | |
| 844 | /* In deferred pragmas parsing_args and prevent_expansion |
| 845 | had been changed, reset it. */ |
| 846 | pfile->state.parsing_args = 2; |
| 847 | pfile->state.prevent_expansion = 1; |
| 848 | |
| 849 | if (token->type == CPP_EOF) |
| 850 | break; |
| 851 | else |
| 852 | continue; |
| 853 | } |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 854 | set_arg_token (arg, token, virt_loc, |
| 855 | ntokens, MACRO_ARG_TOKEN_NORMAL, |
| 856 | CPP_OPTION (pfile, track_macro_expansion)); |
| 857 | ntokens++; |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | /* Drop trailing padding. */ |
| 861 | while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING) |
| 862 | ntokens--; |
| 863 | |
| 864 | arg->count = ntokens; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 865 | set_arg_token (arg, &pfile->eof, pfile->eof.src_loc, |
| 866 | ntokens, MACRO_ARG_TOKEN_NORMAL, |
| 867 | CPP_OPTION (pfile, track_macro_expansion)); |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 868 | |
| 869 | /* Terminate the argument. Excess arguments loop back and |
| 870 | overwrite the final legitimate argument, before failing. */ |
| 871 | if (argc <= macro->paramc) |
| 872 | { |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 873 | buff->cur = (unsigned char *) &arg->first[ntokens + 1]; |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 874 | if (argc != macro->paramc) |
| 875 | arg++; |
| 876 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 877 | } |
Neil Booth | e808ec9 | 2002-02-27 07:24:53 +0000 | [diff] [blame] | 878 | while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 879 | |
Neil Booth | e808ec9 | 2002-02-27 07:24:53 +0000 | [diff] [blame] | 880 | if (token->type == CPP_EOF) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 881 | { |
Neil Booth | ece54d5 | 2001-09-28 09:40:22 +0000 | [diff] [blame] | 882 | /* We still need the CPP_EOF to end directives, and to end |
| 883 | pre-expansion of a macro argument. Step back is not |
| 884 | unconditional, since we don't want to return a CPP_EOF to our |
| 885 | callers at the end of an -include-d file. */ |
Neil Booth | e808ec9 | 2002-02-27 07:24:53 +0000 | [diff] [blame] | 886 | if (pfile->context->prev || pfile->state.in_directive) |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 887 | _cpp_backup_tokens (pfile, 1); |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 888 | cpp_error (pfile, CPP_DL_ERROR, |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 889 | "unterminated argument list invoking macro \"%s\"", |
Neil Booth | a28c5035 | 2001-05-16 22:02:09 +0000 | [diff] [blame] | 890 | NODE_NAME (node)); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 891 | } |
Neil Booth | 1ce676a | 2002-06-09 20:04:17 +0000 | [diff] [blame] | 892 | else |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 893 | { |
Neil Booth | 1ce676a | 2002-06-09 20:04:17 +0000 | [diff] [blame] | 894 | /* A single empty argument is counted as no argument. */ |
| 895 | if (argc == 1 && macro->paramc == 0 && args[0].count == 0) |
| 896 | argc = 0; |
| 897 | if (_cpp_arguments_ok (pfile, macro, node, argc)) |
Neil Booth | 58551c2 | 2002-08-06 20:35:46 +0000 | [diff] [blame] | 898 | { |
| 899 | /* GCC has special semantics for , ## b where b is a varargs |
| 900 | parameter: we remove the comma if b was omitted entirely. |
| 901 | If b was merely an empty argument, the comma is retained. |
| 902 | If the macro takes just one (varargs) parameter, then we |
| 903 | retain the comma only if we are standards conforming. |
| 904 | |
| 905 | If FIRST is NULL replace_args () swallows the comma. */ |
| 906 | if (macro->variadic && (argc < macro->paramc |
| 907 | || (argc == 1 && args[0].count == 0 |
| 908 | && !CPP_OPTION (pfile, std)))) |
| 909 | args[macro->paramc - 1].first = NULL; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 910 | if (num_args) |
| 911 | *num_args = num_args_alloced; |
Neil Booth | 58551c2 | 2002-08-06 20:35:46 +0000 | [diff] [blame] | 912 | return base_buff; |
| 913 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 914 | } |
| 915 | |
Neil Booth | 1ce676a | 2002-06-09 20:04:17 +0000 | [diff] [blame] | 916 | /* An error occurred. */ |
Neil Booth | b8af0ca | 2001-09-26 17:52:50 +0000 | [diff] [blame] | 917 | _cpp_release_buff (pfile, base_buff); |
| 918 | return NULL; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 921 | /* Search for an opening parenthesis to the macro of NODE, in such a |
| 922 | way that, if none is found, we don't lose the information in any |
| 923 | intervening padding tokens. If we find the parenthesis, collect |
Jakub Jelinek | 765d600 | 2008-01-25 10:01:27 +0100 | [diff] [blame] | 924 | the arguments and return the buffer containing them. PRAGMA_BUFF |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 925 | argument is the same as in collect_args. If NUM_ARGS is non-NULL, |
| 926 | *NUM_ARGS is set to the number of arguments contained in the |
| 927 | returned buffer. */ |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 928 | static _cpp_buff * |
Jakub Jelinek | 765d600 | 2008-01-25 10:01:27 +0100 | [diff] [blame] | 929 | funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node, |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 930 | _cpp_buff **pragma_buff, unsigned *num_args) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 931 | { |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 932 | const cpp_token *token, *padding = NULL; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 933 | |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 934 | for (;;) |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 935 | { |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 936 | token = cpp_get_token (pfile); |
| 937 | if (token->type != CPP_PADDING) |
| 938 | break; |
| 939 | if (padding == NULL |
| 940 | || (!(padding->flags & PREV_WHITE) && token->val.source == NULL)) |
| 941 | padding = token; |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 942 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 943 | |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 944 | if (token->type == CPP_OPEN_PAREN) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 945 | { |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 946 | pfile->state.parsing_args = 2; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 947 | return collect_args (pfile, node, pragma_buff, num_args); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Neil Booth | 9ac3b1b | 2002-04-21 16:17:55 +0000 | [diff] [blame] | 950 | /* CPP_EOF can be the end of macro arguments, or the end of the |
| 951 | file. We mustn't back up over the latter. Ugh. */ |
| 952 | if (token->type != CPP_EOF || token == &pfile->eof) |
| 953 | { |
| 954 | /* Back up. We may have skipped padding, in which case backing |
| 955 | up more than one token when expanding macros is in general |
| 956 | too difficult. We re-insert it in its own context. */ |
| 957 | _cpp_backup_tokens (pfile, 1); |
| 958 | if (padding) |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 959 | _cpp_push_token_context (pfile, NULL, padding, 1); |
Neil Booth | 9ac3b1b | 2002-04-21 16:17:55 +0000 | [diff] [blame] | 960 | } |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 961 | |
| 962 | return NULL; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Joseph Myers | aa50850 | 2009-04-19 18:10:56 +0100 | [diff] [blame] | 965 | /* Return the real number of tokens in the expansion of MACRO. */ |
| 966 | static inline unsigned int |
| 967 | macro_real_token_count (const cpp_macro *macro) |
| 968 | { |
| 969 | unsigned int i; |
| 970 | if (__builtin_expect (!macro->extra_tokens, true)) |
| 971 | return macro->count; |
| 972 | for (i = 0; i < macro->count; i++) |
| 973 | if (macro->exp.tokens[i].type == CPP_PASTE) |
| 974 | return i; |
| 975 | abort (); |
| 976 | } |
| 977 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 978 | /* Push the context of a macro with hash entry NODE onto the context |
| 979 | stack. If we can successfully expand the macro, we push a context |
| 980 | containing its yet-to-be-rescanned replacement list and return one. |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 981 | If there were additionally any unexpanded deferred #pragma |
| 982 | directives among macro arguments, push another context containing |
| 983 | the pragma tokens before the yet-to-be-rescanned replacement list |
| 984 | and return two. Otherwise, we don't push a context and return |
| 985 | zero. LOCATION is the location of the expansion point of the |
| 986 | macro. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 987 | static int |
Jakub Jelinek | 765d600 | 2008-01-25 10:01:27 +0100 | [diff] [blame] | 988 | enter_macro_context (cpp_reader *pfile, cpp_hashnode *node, |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 989 | const cpp_token *result, source_location location) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 990 | { |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 991 | /* The presence of a macro invalidates a file's controlling macro. */ |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 992 | pfile->mi_valid = false; |
| 993 | |
Neil Booth | 3620711 | 2002-05-24 19:26:30 +0000 | [diff] [blame] | 994 | pfile->state.angled_headers = false; |
| 995 | |
Joseph Myers | 93d45d9 | 2008-04-02 20:42:53 +0100 | [diff] [blame] | 996 | if ((node->flags & NODE_BUILTIN) && !(node->flags & NODE_USED)) |
| 997 | { |
| 998 | node->flags |= NODE_USED; |
Jakub Jelinek | 8e680db | 2010-06-11 20:37:34 +0200 | [diff] [blame] | 999 | if ((!pfile->cb.user_builtin_macro |
| 1000 | || !pfile->cb.user_builtin_macro (pfile, node)) |
| 1001 | && pfile->cb.used_define) |
Joseph Myers | 93d45d9 | 2008-04-02 20:42:53 +0100 | [diff] [blame] | 1002 | pfile->cb.used_define (pfile, pfile->directive_line, node); |
| 1003 | } |
| 1004 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 1005 | /* Handle standard macros. */ |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 1006 | if (! (node->flags & NODE_BUILTIN)) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1007 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1008 | cpp_macro *macro = node->value.macro; |
Jakub Jelinek | 765d600 | 2008-01-25 10:01:27 +0100 | [diff] [blame] | 1009 | _cpp_buff *pragma_buff = NULL; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1010 | |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 1011 | if (macro->fun_like) |
| 1012 | { |
| 1013 | _cpp_buff *buff; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1014 | unsigned num_args = 0; |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 1015 | |
| 1016 | pfile->state.prevent_expansion++; |
| 1017 | pfile->keep_tokens++; |
| 1018 | pfile->state.parsing_args = 1; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1019 | buff = funlike_invocation_p (pfile, node, &pragma_buff, |
| 1020 | &num_args); |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 1021 | pfile->state.parsing_args = 0; |
| 1022 | pfile->keep_tokens--; |
| 1023 | pfile->state.prevent_expansion--; |
| 1024 | |
| 1025 | if (buff == NULL) |
| 1026 | { |
| 1027 | if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr) |
Simon Baldwin | 87cf065 | 2010-04-07 17:18:10 +0000 | [diff] [blame] | 1028 | cpp_warning (pfile, CPP_W_TRADITIONAL, |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 1029 | "function-like macro \"%s\" must be used with arguments in traditional C", |
Simon Baldwin | 87cf065 | 2010-04-07 17:18:10 +0000 | [diff] [blame] | 1030 | NODE_NAME (node)); |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 1031 | |
Jakub Jelinek | 765d600 | 2008-01-25 10:01:27 +0100 | [diff] [blame] | 1032 | if (pragma_buff) |
| 1033 | _cpp_release_buff (pfile, pragma_buff); |
| 1034 | |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 1035 | return 0; |
| 1036 | } |
| 1037 | |
Neil Booth | e808ec9 | 2002-02-27 07:24:53 +0000 | [diff] [blame] | 1038 | if (macro->paramc > 0) |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1039 | replace_args (pfile, node, macro, |
| 1040 | (macro_arg *) buff->base, |
| 1041 | location); |
| 1042 | /* Free the memory used by the arguments of this |
| 1043 | function-like macro. This memory has been allocated by |
| 1044 | funlike_invocation_p and by replace_args. */ |
| 1045 | delete_macro_args (buff, num_args); |
Neil Booth | d6da836 | 2001-10-08 06:15:14 +0000 | [diff] [blame] | 1046 | } |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1047 | |
| 1048 | /* Disable the macro within its expansion. */ |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 1049 | node->flags |= NODE_DISABLED; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1050 | |
Joseph Myers | 93d45d9 | 2008-04-02 20:42:53 +0100 | [diff] [blame] | 1051 | if (!(node->flags & NODE_USED)) |
| 1052 | { |
| 1053 | node->flags |= NODE_USED; |
| 1054 | if (pfile->cb.used_define) |
| 1055 | pfile->cb.used_define (pfile, pfile->directive_line, node); |
| 1056 | } |
| 1057 | |
Arnaud Charlet | 3de8a54 | 2009-11-20 08:18:16 +0000 | [diff] [blame] | 1058 | if (pfile->cb.used) |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1059 | pfile->cb.used (pfile, location, node); |
Arnaud Charlet | 3de8a54 | 2009-11-20 08:18:16 +0000 | [diff] [blame] | 1060 | |
Neil Booth | a69cbaa | 2002-07-23 22:57:49 +0000 | [diff] [blame] | 1061 | macro->used = 1; |
| 1062 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1063 | if (macro->paramc == 0) |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1064 | { |
| 1065 | if (CPP_OPTION (pfile, track_macro_expansion)) |
| 1066 | { |
| 1067 | unsigned int i, count = macro->count; |
| 1068 | const cpp_token *src = macro->exp.tokens; |
| 1069 | const struct line_map *map; |
| 1070 | source_location *virt_locs = NULL; |
| 1071 | _cpp_buff *macro_tokens = |
| 1072 | tokens_buff_new (pfile, count, &virt_locs); |
| 1073 | |
| 1074 | /* Create a macro map to record the locations of the |
| 1075 | tokens that are involved in the expansion. LOCATION |
| 1076 | is the location of the macro expansion point. */ |
| 1077 | map = linemap_enter_macro (pfile->line_table, |
| 1078 | node, location, count); |
| 1079 | for (i = 0; i < count; ++i) |
| 1080 | { |
| 1081 | tokens_buff_add_token (macro_tokens, virt_locs, |
| 1082 | src, src->src_loc, |
| 1083 | src->src_loc, map, i); |
| 1084 | ++src; |
| 1085 | } |
| 1086 | push_extended_tokens_context (pfile, node, |
| 1087 | macro_tokens, |
| 1088 | virt_locs, |
| 1089 | (const cpp_token **) |
| 1090 | macro_tokens->base, |
| 1091 | count); |
Tom Tromey | 64a1a42 | 2011-10-17 09:59:52 +0000 | [diff] [blame^] | 1092 | num_macro_tokens_counter += count; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1093 | } |
| 1094 | else |
Tom Tromey | 64a1a42 | 2011-10-17 09:59:52 +0000 | [diff] [blame^] | 1095 | { |
| 1096 | unsigned tokens_count = macro_real_token_count (macro); |
| 1097 | _cpp_push_token_context (pfile, node, macro->exp.tokens, |
| 1098 | tokens_count); |
| 1099 | num_macro_tokens_counter += tokens_count; |
| 1100 | } |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1101 | } |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 1102 | |
Jakub Jelinek | 765d600 | 2008-01-25 10:01:27 +0100 | [diff] [blame] | 1103 | if (pragma_buff) |
| 1104 | { |
| 1105 | if (!pfile->state.in_directive) |
| 1106 | _cpp_push_token_context (pfile, NULL, |
| 1107 | padding_token (pfile, result), 1); |
| 1108 | do |
| 1109 | { |
Tom Tromey | 64a1a42 | 2011-10-17 09:59:52 +0000 | [diff] [blame^] | 1110 | unsigned tokens_count; |
Jakub Jelinek | 765d600 | 2008-01-25 10:01:27 +0100 | [diff] [blame] | 1111 | _cpp_buff *tail = pragma_buff->next; |
| 1112 | pragma_buff->next = NULL; |
Tom Tromey | 64a1a42 | 2011-10-17 09:59:52 +0000 | [diff] [blame^] | 1113 | tokens_count = ((const cpp_token **) BUFF_FRONT (pragma_buff) |
| 1114 | - (const cpp_token **) pragma_buff->base); |
Jakub Jelinek | 765d600 | 2008-01-25 10:01:27 +0100 | [diff] [blame] | 1115 | push_ptoken_context (pfile, NULL, pragma_buff, |
| 1116 | (const cpp_token **) pragma_buff->base, |
Tom Tromey | 64a1a42 | 2011-10-17 09:59:52 +0000 | [diff] [blame^] | 1117 | tokens_count); |
Jakub Jelinek | 765d600 | 2008-01-25 10:01:27 +0100 | [diff] [blame] | 1118 | pragma_buff = tail; |
Tom Tromey | 64a1a42 | 2011-10-17 09:59:52 +0000 | [diff] [blame^] | 1119 | if (!CPP_OPTION (pfile, track_macro_expansion)) |
| 1120 | num_macro_tokens_counter += tokens_count; |
| 1121 | |
Jakub Jelinek | 765d600 | 2008-01-25 10:01:27 +0100 | [diff] [blame] | 1122 | } |
| 1123 | while (pragma_buff != NULL); |
| 1124 | return 2; |
| 1125 | } |
| 1126 | |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 1127 | return 1; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1128 | } |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 1129 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 1130 | /* Handle built-in macros and the _Pragma operator. */ |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 1131 | return builtin_macro (pfile, node); |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1134 | /* De-allocate the memory used by BUFF which is an array of instances |
| 1135 | of macro_arg. NUM_ARGS is the number of instances of macro_arg |
| 1136 | present in BUFF. */ |
| 1137 | static void |
| 1138 | delete_macro_args (_cpp_buff *buff, unsigned num_args) |
| 1139 | { |
| 1140 | macro_arg *macro_args; |
| 1141 | unsigned i; |
| 1142 | |
| 1143 | if (buff == NULL) |
| 1144 | return; |
| 1145 | |
| 1146 | macro_args = (macro_arg *) buff->base; |
| 1147 | |
| 1148 | /* Walk instances of macro_arg to free their expanded tokens as well |
| 1149 | as their macro_arg::virt_locs members. */ |
| 1150 | for (i = 0; i < num_args; ++i) |
| 1151 | { |
| 1152 | if (macro_args[i].expanded) |
| 1153 | { |
| 1154 | free (macro_args[i].expanded); |
| 1155 | macro_args[i].expanded = NULL; |
| 1156 | } |
| 1157 | if (macro_args[i].virt_locs) |
| 1158 | { |
| 1159 | free (macro_args[i].virt_locs); |
| 1160 | macro_args[i].virt_locs = NULL; |
| 1161 | } |
| 1162 | if (macro_args[i].expanded_virt_locs) |
| 1163 | { |
| 1164 | free (macro_args[i].expanded_virt_locs); |
| 1165 | macro_args[i].expanded_virt_locs = NULL; |
| 1166 | } |
| 1167 | } |
| 1168 | _cpp_free_buff (buff); |
| 1169 | } |
| 1170 | |
| 1171 | /* Set the INDEXth token of the macro argument ARG. TOKEN is the token |
| 1172 | to set, LOCATION is its virtual location. "Virtual" location means |
| 1173 | the location that encodes loci accross macro expansion. Otherwise |
| 1174 | it has to be TOKEN->SRC_LOC. KIND is the kind of tokens the |
| 1175 | argument ARG is supposed to contain. Note that ARG must be |
| 1176 | tailored so that it has enough room to contain INDEX + 1 numbers of |
| 1177 | tokens, at least. */ |
| 1178 | static void |
| 1179 | set_arg_token (macro_arg *arg, const cpp_token *token, |
| 1180 | source_location location, size_t index, |
| 1181 | enum macro_arg_token_kind kind, |
| 1182 | bool track_macro_exp_p) |
| 1183 | { |
| 1184 | const cpp_token **token_ptr; |
| 1185 | source_location *loc = NULL; |
| 1186 | |
| 1187 | token_ptr = |
| 1188 | arg_token_ptr_at (arg, index, kind, |
| 1189 | track_macro_exp_p ? &loc : NULL); |
| 1190 | *token_ptr = token; |
| 1191 | |
| 1192 | if (loc != NULL) |
| 1193 | { |
| 1194 | #ifdef ENABLE_CHECKING |
| 1195 | if (kind == MACRO_ARG_TOKEN_STRINGIFIED |
| 1196 | || !track_macro_exp_p) |
| 1197 | /* We can't set the location of a stringified argument |
| 1198 | token and we can't set any location if we aren't tracking |
| 1199 | macro expansion locations. */ |
| 1200 | abort (); |
| 1201 | #endif |
| 1202 | *loc = location; |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | /* Get the pointer to the location of the argument token of the |
| 1207 | function-like macro argument ARG. This function must be called |
| 1208 | only when we -ftrack-macro-expansion is on. */ |
| 1209 | static const source_location * |
| 1210 | get_arg_token_location (const macro_arg *arg, |
| 1211 | enum macro_arg_token_kind kind) |
| 1212 | { |
| 1213 | const source_location *loc = NULL; |
| 1214 | const cpp_token **token_ptr = |
| 1215 | arg_token_ptr_at (arg, 0, kind, (source_location **) &loc); |
| 1216 | |
| 1217 | if (token_ptr == NULL) |
| 1218 | return NULL; |
| 1219 | |
| 1220 | return loc; |
| 1221 | } |
| 1222 | |
| 1223 | /* Return the pointer to the INDEXth token of the macro argument ARG. |
| 1224 | KIND specifies the kind of token the macro argument ARG contains. |
| 1225 | If VIRT_LOCATION is non NULL, *VIRT_LOCATION is set to the address |
| 1226 | of the virtual location of the returned token if the |
| 1227 | -ftrack-macro-expansion flag is on; otherwise, it's set to the |
| 1228 | spelling location of the returned token. */ |
| 1229 | static const cpp_token ** |
| 1230 | arg_token_ptr_at (const macro_arg *arg, size_t index, |
| 1231 | enum macro_arg_token_kind kind, |
| 1232 | source_location **virt_location) |
| 1233 | { |
| 1234 | const cpp_token **tokens_ptr = NULL; |
| 1235 | |
| 1236 | switch (kind) |
| 1237 | { |
| 1238 | case MACRO_ARG_TOKEN_NORMAL: |
| 1239 | tokens_ptr = arg->first; |
| 1240 | break; |
| 1241 | case MACRO_ARG_TOKEN_STRINGIFIED: |
| 1242 | tokens_ptr = (const cpp_token **) &arg->stringified; |
| 1243 | break; |
| 1244 | case MACRO_ARG_TOKEN_EXPANDED: |
| 1245 | tokens_ptr = arg->expanded; |
| 1246 | break; |
| 1247 | } |
| 1248 | |
| 1249 | if (tokens_ptr == NULL) |
| 1250 | /* This can happen for e.g, an empty token argument to a |
| 1251 | funtion-like macro. */ |
| 1252 | return tokens_ptr; |
| 1253 | |
| 1254 | if (virt_location) |
| 1255 | { |
| 1256 | if (kind == MACRO_ARG_TOKEN_NORMAL) |
| 1257 | *virt_location = &arg->virt_locs[index]; |
| 1258 | else if (kind == MACRO_ARG_TOKEN_EXPANDED) |
| 1259 | *virt_location = &arg->expanded_virt_locs[index]; |
| 1260 | else if (kind == MACRO_ARG_TOKEN_STRINGIFIED) |
| 1261 | *virt_location = |
| 1262 | (source_location *) &tokens_ptr[index]->src_loc; |
| 1263 | } |
| 1264 | return &tokens_ptr[index]; |
| 1265 | } |
| 1266 | |
| 1267 | /* Initialize an iterator so that it iterates over the tokens of a |
| 1268 | function-like macro argument. KIND is the kind of tokens we want |
| 1269 | ITER to iterate over. TOKEN_PTR points the first token ITER will |
| 1270 | iterate over. */ |
| 1271 | static void |
| 1272 | macro_arg_token_iter_init (macro_arg_token_iter *iter, |
| 1273 | bool track_macro_exp_p, |
| 1274 | enum macro_arg_token_kind kind, |
| 1275 | const macro_arg *arg, |
| 1276 | const cpp_token **token_ptr) |
| 1277 | { |
| 1278 | iter->track_macro_exp_p = track_macro_exp_p; |
| 1279 | iter->kind = kind; |
| 1280 | iter->token_ptr = token_ptr; |
| 1281 | if (track_macro_exp_p) |
| 1282 | iter->location_ptr = get_arg_token_location (arg, kind); |
| 1283 | #ifdef ENABLE_CHECKING |
| 1284 | iter->num_forwards = 0; |
| 1285 | if (track_macro_exp_p |
| 1286 | && token_ptr != NULL |
| 1287 | && iter->location_ptr == NULL) |
| 1288 | abort (); |
| 1289 | #endif |
| 1290 | } |
| 1291 | |
| 1292 | /* Move the iterator one token forward. Note that if IT was |
| 1293 | initialized on an argument that has a stringified token, moving it |
| 1294 | foward doesn't make sense as a stringified token is essentially one |
| 1295 | string. */ |
| 1296 | static void |
| 1297 | macro_arg_token_iter_forward (macro_arg_token_iter *it) |
| 1298 | { |
| 1299 | switch (it->kind) |
| 1300 | { |
| 1301 | case MACRO_ARG_TOKEN_NORMAL: |
| 1302 | case MACRO_ARG_TOKEN_EXPANDED: |
| 1303 | it->token_ptr++; |
| 1304 | if (it->track_macro_exp_p) |
| 1305 | it->location_ptr++; |
| 1306 | break; |
| 1307 | case MACRO_ARG_TOKEN_STRINGIFIED: |
| 1308 | #ifdef ENABLE_CHECKING |
| 1309 | if (it->num_forwards > 0) |
| 1310 | abort (); |
| 1311 | #endif |
| 1312 | break; |
| 1313 | } |
| 1314 | |
| 1315 | #ifdef ENABLE_CHECKING |
| 1316 | it->num_forwards++; |
| 1317 | #endif |
| 1318 | } |
| 1319 | |
| 1320 | /* Return the token pointed to by the iterator. */ |
| 1321 | static const cpp_token * |
| 1322 | macro_arg_token_iter_get_token (const macro_arg_token_iter *it) |
| 1323 | { |
| 1324 | #ifdef ENABLE_CHECKING |
| 1325 | if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED |
| 1326 | && it->num_forwards > 0) |
| 1327 | abort (); |
| 1328 | #endif |
| 1329 | if (it->token_ptr == NULL) |
| 1330 | return NULL; |
| 1331 | return *it->token_ptr; |
| 1332 | } |
| 1333 | |
| 1334 | /* Return the location of the token pointed to by the iterator.*/ |
| 1335 | static source_location |
| 1336 | macro_arg_token_iter_get_location (const macro_arg_token_iter *it) |
| 1337 | { |
| 1338 | #ifdef ENABLE_CHECKING |
| 1339 | if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED |
| 1340 | && it->num_forwards > 0) |
| 1341 | abort (); |
| 1342 | #endif |
| 1343 | if (it->track_macro_exp_p) |
| 1344 | return *it->location_ptr; |
| 1345 | else |
| 1346 | return (*it->token_ptr)->src_loc; |
| 1347 | } |
| 1348 | |
| 1349 | /* Return the index of a token [resulting from macro expansion] inside |
| 1350 | the total list of tokens resulting from a given macro |
| 1351 | expansion. The index can be different depending on whether if we |
| 1352 | want each tokens resulting from function-like macro arguments |
| 1353 | expansion to have a different location or not. |
| 1354 | |
| 1355 | E.g, consider this function-like macro: |
| 1356 | |
| 1357 | #define M(x) x - 3 |
| 1358 | |
| 1359 | Then consider us "calling" it (and thus expanding it) like: |
| 1360 | |
| 1361 | M(1+4) |
| 1362 | |
| 1363 | It will be expanded into: |
| 1364 | |
| 1365 | 1+4-3 |
| 1366 | |
| 1367 | Let's consider the case of the token '4'. |
| 1368 | |
| 1369 | Its index can be 2 (it's the third token of the set of tokens |
| 1370 | resulting from the expansion) or it can be 0 if we consider that |
| 1371 | all tokens resulting from the expansion of the argument "1+2" have |
| 1372 | the same index, which is 0. In this later case, the index of token |
| 1373 | '-' would then be 1 and the index of token '3' would be 2. |
| 1374 | |
| 1375 | The later case is useful to use less memory e.g, for the case of |
| 1376 | the user using the option -ftrack-macro-expansion=1. |
| 1377 | |
| 1378 | ABSOLUTE_TOKEN_INDEX is the index of the macro argument token we |
| 1379 | are interested in. CUR_REPLACEMENT_TOKEN is the token of the macro |
| 1380 | parameter (inside the macro replacement list) that corresponds to |
| 1381 | the macro argument for which ABSOLUTE_TOKEN_INDEX is a token index |
| 1382 | of. |
| 1383 | |
| 1384 | If we refer to the example above, for the '4' argument token, |
| 1385 | ABSOLUTE_TOKEN_INDEX would be set to 2, and CUR_REPLACEMENT_TOKEN |
| 1386 | would be set to the token 'x', in the replacement list "x - 3" of |
| 1387 | macro M. |
| 1388 | |
| 1389 | This is a subroutine of replace_args. */ |
| 1390 | inline static unsigned |
| 1391 | expanded_token_index (cpp_reader *pfile, cpp_macro *macro, |
| 1392 | const cpp_token *cur_replacement_token, |
| 1393 | unsigned absolute_token_index) |
| 1394 | { |
| 1395 | if (CPP_OPTION (pfile, track_macro_expansion) > 1) |
| 1396 | return absolute_token_index; |
| 1397 | return cur_replacement_token - macro->exp.tokens; |
| 1398 | } |
| 1399 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 1400 | /* Replace the parameters in a function-like macro of NODE with the |
| 1401 | actual ARGS, and place the result in a newly pushed token context. |
| 1402 | Expand each argument before replacing, unless it is operated upon |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1403 | by the # or ## operators. EXPANSION_POINT_LOC is the location of |
| 1404 | the expansion point of the macro. E.g, the location of the |
| 1405 | function-like macro invocation. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1406 | static void |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1407 | replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro, |
| 1408 | macro_arg *args, source_location expansion_point_loc) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1409 | { |
| 1410 | unsigned int i, total; |
| 1411 | const cpp_token *src, *limit; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1412 | const cpp_token **first = NULL; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1413 | macro_arg *arg; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1414 | _cpp_buff *buff = NULL; |
| 1415 | source_location *virt_locs = NULL; |
| 1416 | unsigned int exp_count; |
| 1417 | const struct line_map *map = NULL; |
| 1418 | int track_macro_exp; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1419 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1420 | /* First, fully macro-expand arguments, calculating the number of |
Neil Booth | 1e013d2 | 2001-09-26 21:44:35 +0000 | [diff] [blame] | 1421 | tokens in the final expansion as we go. The ordering of the if |
| 1422 | statements below is subtle; we must handle stringification before |
| 1423 | pasting. */ |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1424 | |
| 1425 | /* EXP_COUNT is the number of tokens in the macro replacement |
| 1426 | list. TOTAL is the number of tokens /after/ macro parameters |
| 1427 | have been replaced by their arguments. */ |
| 1428 | exp_count = macro_real_token_count (macro); |
| 1429 | total = exp_count; |
| 1430 | limit = macro->exp.tokens + exp_count; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1431 | |
Neil Booth | 601328b | 2002-05-16 05:53:24 +0000 | [diff] [blame] | 1432 | for (src = macro->exp.tokens; src < limit; src++) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1433 | if (src->type == CPP_MACRO_ARG) |
| 1434 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1435 | /* Leading and trailing padding tokens. */ |
| 1436 | total += 2; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1437 | /* Account for leading and padding tokens in exp_count too. |
| 1438 | This is going to be important later down this function, |
| 1439 | when we want to handle the case of (track_macro_exp < |
| 1440 | 2). */ |
| 1441 | exp_count += 2; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1442 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1443 | /* We have an argument. If it is not being stringified or |
| 1444 | pasted it is macro-replaced before insertion. */ |
Joseph Myers | 9a0c618 | 2009-05-10 15:27:32 +0100 | [diff] [blame] | 1445 | arg = &args[src->val.macro_arg.arg_no - 1]; |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame] | 1446 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1447 | if (src->flags & STRINGIFY_ARG) |
| 1448 | { |
| 1449 | if (!arg->stringified) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1450 | arg->stringified = stringify_arg (pfile, arg); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1451 | } |
| 1452 | else if ((src->flags & PASTE_LEFT) |
Neil Booth | 601328b | 2002-05-16 05:53:24 +0000 | [diff] [blame] | 1453 | || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT))) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1454 | total += arg->count - 1; |
| 1455 | else |
| 1456 | { |
| 1457 | if (!arg->expanded) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1458 | expand_arg (pfile, arg); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1459 | total += arg->expanded_count - 1; |
| 1460 | } |
| 1461 | } |
| 1462 | |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1463 | /* When the compiler is called with the -ftrack-macro-expansion |
| 1464 | flag, we need to keep track of the location of each token that |
| 1465 | results from macro expansion. |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1466 | |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1467 | A token resulting from macro expansion is not a new token. It is |
| 1468 | simply the same token as the token coming from the macro |
| 1469 | definition. The new things that are allocated are the buffer |
| 1470 | that holds the tokens resulting from macro expansion and a new |
| 1471 | location that records many things like the locus of the expansion |
| 1472 | point as well as the original locus inside the definition of the |
| 1473 | macro. This location is called a virtual location. |
| 1474 | |
| 1475 | So the buffer BUFF holds a set of cpp_token*, and the buffer |
| 1476 | VIRT_LOCS holds the virtual locations of the tokens held by BUFF. |
| 1477 | |
| 1478 | Both of these two buffers are going to be hung off of the macro |
| 1479 | context, when the latter is pushed. The memory allocated to |
| 1480 | store the tokens and their locations is going to be freed once |
| 1481 | the context of macro expansion is popped. |
| 1482 | |
| 1483 | As far as tokens are concerned, the memory overhead of |
| 1484 | -ftrack-macro-expansion is proportional to the number of |
| 1485 | macros that get expanded multiplied by sizeof (source_location). |
| 1486 | The good news is that extra memory gets freed when the macro |
| 1487 | context is freed, i.e shortly after the macro got expanded. */ |
| 1488 | |
| 1489 | /* Is the -ftrack-macro-expansion flag in effect? */ |
| 1490 | track_macro_exp = CPP_OPTION (pfile, track_macro_expansion); |
| 1491 | |
| 1492 | /* Now allocate memory space for tokens and locations resulting from |
| 1493 | the macro expansion, copy the tokens and replace the arguments. |
| 1494 | This memory must be freed when the context of the macro MACRO is |
| 1495 | popped. */ |
| 1496 | buff = tokens_buff_new (pfile, total, track_macro_exp ? &virt_locs : NULL); |
| 1497 | |
| 1498 | first = (const cpp_token **) buff->base; |
| 1499 | |
| 1500 | /* Create a macro map to record the locations of the tokens that are |
| 1501 | involved in the expansion. Note that the expansion point is set |
| 1502 | to the location of the closing parenthesis. Otherwise, the |
| 1503 | subsequent map created for the first token that comes after the |
| 1504 | macro map might have a wrong line number. That would lead to |
| 1505 | tokens with wrong line numbers after the macro expansion. This |
| 1506 | adds up to the memory overhead of the -ftrack-macro-expansion |
| 1507 | flag; for every macro that is expanded, a "macro map" is |
| 1508 | created. */ |
| 1509 | if (track_macro_exp) |
| 1510 | { |
| 1511 | int num_macro_tokens = total; |
| 1512 | if (track_macro_exp < 2) |
| 1513 | /* Then the number of macro tokens won't take in account the |
| 1514 | fact that function-like macro arguments can expand to |
| 1515 | multiple tokens. This is to save memory at the expense of |
| 1516 | accuracy. |
| 1517 | |
| 1518 | Suppose we have #define SQARE(A) A * A |
| 1519 | |
| 1520 | And then we do SQARE(2+3) |
| 1521 | |
| 1522 | Then the tokens 2, +, 3, will have the same location, |
| 1523 | saying they come from the expansion of the argument A. */ |
| 1524 | num_macro_tokens = exp_count; |
| 1525 | map = linemap_enter_macro (pfile->line_table, node, |
| 1526 | expansion_point_loc, |
| 1527 | num_macro_tokens); |
| 1528 | } |
| 1529 | i = 0; |
Neil Booth | 601328b | 2002-05-16 05:53:24 +0000 | [diff] [blame] | 1530 | for (src = macro->exp.tokens; src < limit; src++) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1531 | { |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1532 | unsigned int arg_tokens_count; |
| 1533 | macro_arg_token_iter from; |
| 1534 | const cpp_token **paste_flag = NULL; |
| 1535 | const cpp_token **tmp_token_ptr; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1536 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1537 | if (src->type != CPP_MACRO_ARG) |
| 1538 | { |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1539 | /* Allocate a virtual location for token SRC, and add that |
| 1540 | token and its virtual location into the buffers BUFF and |
| 1541 | VIRT_LOCS. */ |
| 1542 | unsigned index = expanded_token_index (pfile, macro, src, i); |
| 1543 | tokens_buff_add_token (buff, virt_locs, src, |
| 1544 | src->src_loc, src->src_loc, |
| 1545 | map, index); |
| 1546 | i += 1; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1547 | continue; |
| 1548 | } |
| 1549 | |
| 1550 | paste_flag = 0; |
Joseph Myers | 9a0c618 | 2009-05-10 15:27:32 +0100 | [diff] [blame] | 1551 | arg = &args[src->val.macro_arg.arg_no - 1]; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1552 | /* SRC is a macro parameter that we need to replace with its |
| 1553 | corresponding argument. So at some point we'll need to |
| 1554 | iterate over the tokens of the macro argument and copy them |
| 1555 | into the "place" now holding the correspondig macro |
| 1556 | parameter. We are going to use the iterator type |
| 1557 | macro_argo_token_iter to handle that iterating. The 'if' |
| 1558 | below is to initialize the iterator depending on the type of |
| 1559 | tokens the macro argument has. It also does some adjustment |
| 1560 | related to padding tokens and some pasting corner cases. */ |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1561 | if (src->flags & STRINGIFY_ARG) |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1562 | { |
| 1563 | arg_tokens_count = 1; |
| 1564 | macro_arg_token_iter_init (&from, |
| 1565 | CPP_OPTION (pfile, |
| 1566 | track_macro_expansion), |
| 1567 | MACRO_ARG_TOKEN_STRINGIFIED, |
| 1568 | arg, &arg->stringified); |
| 1569 | } |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1570 | else if (src->flags & PASTE_LEFT) |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1571 | { |
| 1572 | arg_tokens_count = arg->count; |
| 1573 | macro_arg_token_iter_init (&from, |
| 1574 | CPP_OPTION (pfile, |
| 1575 | track_macro_expansion), |
| 1576 | MACRO_ARG_TOKEN_NORMAL, |
| 1577 | arg, arg->first); |
| 1578 | } |
Neil Booth | 601328b | 2002-05-16 05:53:24 +0000 | [diff] [blame] | 1579 | else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT)) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1580 | { |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1581 | int num_toks; |
| 1582 | arg_tokens_count = arg->count; |
| 1583 | macro_arg_token_iter_init (&from, |
| 1584 | CPP_OPTION (pfile, |
| 1585 | track_macro_expansion), |
| 1586 | MACRO_ARG_TOKEN_NORMAL, |
| 1587 | arg, arg->first); |
| 1588 | |
| 1589 | num_toks = tokens_buff_count (buff); |
| 1590 | |
| 1591 | if (num_toks != 0) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1592 | { |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1593 | /* So the current parameter token is pasted to the previous |
| 1594 | token in the replacement list. Let's look at what |
| 1595 | we have as previous and current arguments. */ |
| 1596 | |
| 1597 | /* This is the previous argument's token ... */ |
| 1598 | tmp_token_ptr = tokens_buff_last_token_ptr (buff); |
| 1599 | |
| 1600 | if ((*tmp_token_ptr)->type == CPP_COMMA |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1601 | && macro->variadic |
Joseph Myers | 9a0c618 | 2009-05-10 15:27:32 +0100 | [diff] [blame] | 1602 | && src->val.macro_arg.arg_no == macro->paramc) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1603 | { |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1604 | /* ... which is a comma; and the current parameter |
| 1605 | is the last parameter of a variadic function-like |
| 1606 | macro. If the argument to the current last |
| 1607 | parameter is NULL, then swallow the comma, |
| 1608 | otherwise drop the paste flag. */ |
| 1609 | if (macro_arg_token_iter_get_token (&from) == NULL) |
| 1610 | tokens_buff_remove_last_token (buff); |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1611 | else |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1612 | paste_flag = tmp_token_ptr; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1613 | } |
| 1614 | /* Remove the paste flag if the RHS is a placemarker. */ |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1615 | else if (arg_tokens_count == 0) |
| 1616 | paste_flag = tmp_token_ptr; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1617 | } |
| 1618 | } |
| 1619 | else |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1620 | { |
| 1621 | arg_tokens_count = arg->expanded_count; |
| 1622 | macro_arg_token_iter_init (&from, |
| 1623 | CPP_OPTION (pfile, |
| 1624 | track_macro_expansion), |
| 1625 | MACRO_ARG_TOKEN_EXPANDED, |
| 1626 | arg, arg->expanded); |
| 1627 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1628 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1629 | /* Padding on the left of an argument (unless RHS of ##). */ |
Zack Weinberg | a8d0dda | 2003-02-21 18:06:30 +0000 | [diff] [blame] | 1630 | if ((!pfile->state.in_directive || pfile->state.directive_wants_padding) |
Neil Booth | 601328b | 2002-05-16 05:53:24 +0000 | [diff] [blame] | 1631 | && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT)) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1632 | { |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1633 | const cpp_token *t = padding_token (pfile, src); |
| 1634 | unsigned index = expanded_token_index (pfile, macro, src, i); |
| 1635 | /* Allocate a virtual location for the padding token and |
| 1636 | append the token and its location to BUFF and |
| 1637 | VIRT_LOCS. */ |
| 1638 | tokens_buff_add_token (buff, virt_locs, t, |
| 1639 | t->src_loc, t->src_loc, |
| 1640 | map, index); |
| 1641 | } |
| 1642 | |
| 1643 | if (arg_tokens_count) |
| 1644 | { |
| 1645 | /* So now we've got the number of tokens that make up the |
| 1646 | argument that is going to replace the current parameter |
| 1647 | in the macro's replacement list. */ |
| 1648 | unsigned int j; |
| 1649 | for (j = 0; j < arg_tokens_count; ++j) |
| 1650 | { |
| 1651 | /* So if track_macro_exp is < 2, the user wants to |
| 1652 | save extra memory while tracking macro expansion |
| 1653 | locations. So in that case here is what we do: |
| 1654 | |
| 1655 | Suppose we have #define SQARE(A) A * A |
| 1656 | |
| 1657 | And then we do SQARE(2+3) |
| 1658 | |
| 1659 | Then the tokens 2, +, 3, will have the same location, |
| 1660 | saying they come from the expansion of the argument |
| 1661 | A. |
| 1662 | |
| 1663 | So that means we are going to ignore the COUNT tokens |
| 1664 | resulting from the expansion of the current macro |
| 1665 | arugment. In other words all the ARG_TOKENS_COUNT tokens |
| 1666 | resulting from the expansion of the macro argument will |
| 1667 | have the index I. Normally, each of those token should |
| 1668 | have index I+J. */ |
| 1669 | unsigned token_index = i; |
| 1670 | unsigned index; |
| 1671 | if (track_macro_exp > 1) |
| 1672 | token_index += j; |
| 1673 | |
| 1674 | index = expanded_token_index (pfile, macro, src, token_index); |
| 1675 | tokens_buff_add_token (buff, virt_locs, |
| 1676 | macro_arg_token_iter_get_token (&from), |
| 1677 | macro_arg_token_iter_get_location (&from), |
| 1678 | src->src_loc, map, index); |
| 1679 | macro_arg_token_iter_forward (&from); |
| 1680 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1681 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1682 | /* With a non-empty argument on the LHS of ##, the last |
| 1683 | token should be flagged PASTE_LEFT. */ |
| 1684 | if (src->flags & PASTE_LEFT) |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1685 | paste_flag = |
| 1686 | (const cpp_token **) tokens_buff_last_token_ptr (buff); |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1687 | } |
Andrew Haley | e85edc9 | 2008-07-03 10:31:50 +0000 | [diff] [blame] | 1688 | else if (CPP_PEDANTIC (pfile) && ! macro->syshdr |
| 1689 | && ! CPP_OPTION (pfile, c99) |
| 1690 | && ! cpp_in_system_header (pfile)) |
| 1691 | { |
| 1692 | cpp_error (pfile, CPP_DL_PEDWARN, |
| 1693 | "invoking macro %s argument %d: " |
| 1694 | "empty macro arguments are undefined" |
| 1695 | " in ISO C90 and ISO C++98", |
| 1696 | NODE_NAME (node), |
Joseph Myers | 9a0c618 | 2009-05-10 15:27:32 +0100 | [diff] [blame] | 1697 | src->val.macro_arg.arg_no); |
Andrew Haley | e85edc9 | 2008-07-03 10:31:50 +0000 | [diff] [blame] | 1698 | } |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame] | 1699 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1700 | /* Avoid paste on RHS (even case count == 0). */ |
| 1701 | if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT)) |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1702 | { |
| 1703 | const cpp_token *t = &pfile->avoid_paste; |
| 1704 | tokens_buff_add_token (buff, virt_locs, |
| 1705 | t, t->src_loc, t->src_loc, |
| 1706 | NULL, 0); |
| 1707 | } |
Neil Booth | 26ec42e | 2001-01-28 11:22:23 +0000 | [diff] [blame] | 1708 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1709 | /* Add a new paste flag, or remove an unwanted one. */ |
| 1710 | if (paste_flag) |
| 1711 | { |
| 1712 | cpp_token *token = _cpp_temp_token (pfile); |
| 1713 | token->type = (*paste_flag)->type; |
Jakub Jelinek | 7309671 | 2005-03-04 16:33:23 +0100 | [diff] [blame] | 1714 | token->val = (*paste_flag)->val; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1715 | if (src->flags & PASTE_LEFT) |
| 1716 | token->flags = (*paste_flag)->flags | PASTE_LEFT; |
| 1717 | else |
| 1718 | token->flags = (*paste_flag)->flags & ~PASTE_LEFT; |
| 1719 | *paste_flag = token; |
| 1720 | } |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1721 | |
| 1722 | i += arg_tokens_count; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1723 | } |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame] | 1724 | |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1725 | if (track_macro_exp) |
| 1726 | push_extended_tokens_context (pfile, node, buff, virt_locs, first, |
| 1727 | tokens_buff_count (buff)); |
| 1728 | else |
| 1729 | push_ptoken_context (pfile, node, buff, first, |
| 1730 | tokens_buff_count (buff)); |
Tom Tromey | 64a1a42 | 2011-10-17 09:59:52 +0000 | [diff] [blame^] | 1731 | |
| 1732 | num_macro_tokens_counter += tokens_buff_count (buff); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1733 | } |
| 1734 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1735 | /* Return a special padding token, with padding inherited from SOURCE. */ |
| 1736 | static const cpp_token * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1737 | padding_token (cpp_reader *pfile, const cpp_token *source) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1738 | { |
| 1739 | cpp_token *result = _cpp_temp_token (pfile); |
| 1740 | |
| 1741 | result->type = CPP_PADDING; |
Paolo Bonzini | be0f1e5 | 2005-02-14 08:52:24 +0000 | [diff] [blame] | 1742 | |
| 1743 | /* Data in GCed data structures cannot be made const so far, so we |
| 1744 | need a cast here. */ |
| 1745 | result->val.source = (cpp_token *) source; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1746 | result->flags = 0; |
| 1747 | return result; |
| 1748 | } |
| 1749 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 1750 | /* Get a new uninitialized context. Create a new one if we cannot |
| 1751 | re-use an old one. */ |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1752 | static cpp_context * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1753 | next_context (cpp_reader *pfile) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1754 | { |
| 1755 | cpp_context *result = pfile->context->next; |
| 1756 | |
| 1757 | if (result == 0) |
| 1758 | { |
Bernardo Innocenti | 72bb2c3 | 2004-07-24 20:04:42 +0200 | [diff] [blame] | 1759 | result = XNEW (cpp_context); |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1760 | memset (result, 0, sizeof (cpp_context)); |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1761 | result->prev = pfile->context; |
| 1762 | result->next = 0; |
| 1763 | pfile->context->next = result; |
| 1764 | } |
| 1765 | |
| 1766 | pfile->context = result; |
| 1767 | return result; |
| 1768 | } |
| 1769 | |
| 1770 | /* Push a list of pointers to tokens. */ |
| 1771 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1772 | push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff, |
| 1773 | const cpp_token **first, unsigned int count) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1774 | { |
| 1775 | cpp_context *context = next_context (pfile); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1776 | |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1777 | context->tokens_kind = TOKENS_KIND_INDIRECT; |
| 1778 | context->c.macro = macro; |
Neil Booth | 1e013d2 | 2001-09-26 21:44:35 +0000 | [diff] [blame] | 1779 | context->buff = buff; |
Neil Booth | 82eda77 | 2002-06-04 13:07:06 +0000 | [diff] [blame] | 1780 | FIRST (context).ptoken = first; |
| 1781 | LAST (context).ptoken = first + count; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | /* Push a list of tokens. */ |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1785 | void |
| 1786 | _cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro, |
| 1787 | const cpp_token *first, unsigned int count) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1788 | { |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1789 | cpp_context *context = next_context (pfile); |
| 1790 | |
| 1791 | context->tokens_kind = TOKENS_KIND_DIRECT; |
| 1792 | context->c.macro = macro; |
| 1793 | context->buff = NULL; |
Neil Booth | 82eda77 | 2002-06-04 13:07:06 +0000 | [diff] [blame] | 1794 | FIRST (context).token = first; |
| 1795 | LAST (context).token = first + count; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1798 | /* Build a context containing a list of tokens as well as their |
| 1799 | virtual locations and push it. TOKENS_BUFF is the buffer that |
| 1800 | contains the tokens pointed to by FIRST. If TOKENS_BUFF is |
| 1801 | non-NULL, it means that the context owns it, meaning that |
| 1802 | _cpp_pop_context will free it as well as VIRT_LOCS_BUFF that |
| 1803 | contains the virtual locations. */ |
| 1804 | static void |
| 1805 | push_extended_tokens_context (cpp_reader *pfile, |
| 1806 | cpp_hashnode *macro, |
| 1807 | _cpp_buff *token_buff, |
| 1808 | source_location *virt_locs, |
| 1809 | const cpp_token **first, |
| 1810 | unsigned int count) |
| 1811 | { |
| 1812 | cpp_context *context = next_context (pfile); |
| 1813 | macro_context *m; |
| 1814 | |
| 1815 | context->tokens_kind = TOKENS_KIND_EXTENDED; |
| 1816 | context->buff = token_buff; |
| 1817 | |
| 1818 | m = XNEW (macro_context); |
| 1819 | m->macro_node = macro; |
| 1820 | m->virt_locs = virt_locs; |
| 1821 | m->cur_virt_loc = virt_locs; |
| 1822 | context->c.mc = m; |
| 1823 | FIRST (context).ptoken = first; |
| 1824 | LAST (context).ptoken = first + count; |
| 1825 | } |
| 1826 | |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 1827 | /* Push a traditional macro's replacement text. */ |
| 1828 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1829 | _cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro, |
| 1830 | const uchar *start, size_t len) |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 1831 | { |
| 1832 | cpp_context *context = next_context (pfile); |
| 1833 | |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1834 | context->tokens_kind = TOKENS_KIND_DIRECT; |
| 1835 | context->c.macro = macro; |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 1836 | context->buff = NULL; |
| 1837 | CUR (context) = start; |
Neil Booth | 1ce676a | 2002-06-09 20:04:17 +0000 | [diff] [blame] | 1838 | RLIMIT (context) = start + len; |
Neil Booth | 974c43f | 2002-06-13 06:25:28 +0000 | [diff] [blame] | 1839 | macro->flags |= NODE_DISABLED; |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 1842 | /* Creates a buffer that holds tokens a.k.a "token buffer", usually |
| 1843 | for the purpose of storing them on a cpp_context. If VIRT_LOCS is |
| 1844 | non-null (which means that -ftrack-macro-expansion is on), |
| 1845 | *VIRT_LOCS is set to a newly allocated buffer that is supposed to |
| 1846 | hold the virtual locations of the tokens resulting from macro |
| 1847 | expansion. */ |
| 1848 | static _cpp_buff* |
| 1849 | tokens_buff_new (cpp_reader *pfile, size_t len, |
| 1850 | source_location **virt_locs) |
| 1851 | { |
| 1852 | size_t tokens_size = len * sizeof (cpp_token *); |
| 1853 | size_t locs_size = len * sizeof (source_location); |
| 1854 | |
| 1855 | if (virt_locs != NULL) |
| 1856 | *virt_locs = XNEWVEC (source_location, locs_size); |
| 1857 | return _cpp_get_buff (pfile, tokens_size); |
| 1858 | } |
| 1859 | |
| 1860 | /* Returns the number of tokens contained in a token buffer. The |
| 1861 | buffer holds a set of cpp_token*. */ |
| 1862 | static size_t |
| 1863 | tokens_buff_count (_cpp_buff *buff) |
| 1864 | { |
| 1865 | return (BUFF_FRONT (buff) - buff->base) / sizeof (cpp_token *); |
| 1866 | } |
| 1867 | |
| 1868 | /* Return a pointer to the last token contained in the token buffer |
| 1869 | BUFF. */ |
| 1870 | static const cpp_token ** |
| 1871 | tokens_buff_last_token_ptr (_cpp_buff *buff) |
| 1872 | { |
| 1873 | return &((const cpp_token **) BUFF_FRONT (buff))[-1]; |
| 1874 | } |
| 1875 | |
| 1876 | /* Remove the last token contained in the token buffer TOKENS_BUFF. |
| 1877 | If VIRT_LOCS_BUFF is non-NULL, it should point at the buffer |
| 1878 | containing the virtual locations of the tokens in TOKENS_BUFF; in |
| 1879 | which case the function updates that buffer as well. */ |
| 1880 | static inline void |
| 1881 | tokens_buff_remove_last_token (_cpp_buff *tokens_buff) |
| 1882 | |
| 1883 | { |
| 1884 | if (BUFF_FRONT (tokens_buff) > tokens_buff->base) |
| 1885 | BUFF_FRONT (tokens_buff) = |
| 1886 | (unsigned char *) &((cpp_token **) BUFF_FRONT (tokens_buff))[-1]; |
| 1887 | } |
| 1888 | |
| 1889 | /* Insert a token into the token buffer at the position pointed to by |
| 1890 | DEST. Note that the buffer is not enlarged so the previous token |
| 1891 | that was at *DEST is overwritten. VIRT_LOC_DEST, if non-null, |
| 1892 | means -ftrack-macro-expansion is effect; it then points to where to |
| 1893 | insert the virtual location of TOKEN. TOKEN is the token to |
| 1894 | insert. VIRT_LOC is the virtual location of the token, i.e, the |
| 1895 | location possibly encoding its locus accross macro expansion. If |
| 1896 | TOKEN is an argument of a function-like macro (inside a macro |
| 1897 | replacement list), PARM_DEF_LOC is the spelling location of the |
| 1898 | macro parameter that TOKEN is replacing, in the replacement list of |
| 1899 | the macro. If TOKEN is not an argument of a function-like macro or |
| 1900 | if it doesn't come from a macro expansion, then VIRT_LOC can just |
| 1901 | be set to the same value as PARM_DEF_LOC. If MAP is non null, it |
| 1902 | means TOKEN comes from a macro expansion and MAP is the macro map |
| 1903 | associated to the macro. MACRO_TOKEN_INDEX points to the index of |
| 1904 | the token in the macro map; it is not considered if MAP is NULL. |
| 1905 | |
| 1906 | Upon successful completion this function returns the a pointer to |
| 1907 | the position of the token coming right after the insertion |
| 1908 | point. */ |
| 1909 | static inline const cpp_token ** |
| 1910 | tokens_buff_put_token_to (const cpp_token **dest, |
| 1911 | source_location *virt_loc_dest, |
| 1912 | const cpp_token *token, |
| 1913 | source_location virt_loc, |
| 1914 | source_location parm_def_loc, |
| 1915 | const struct line_map *map, |
| 1916 | unsigned int macro_token_index) |
| 1917 | { |
| 1918 | source_location macro_loc = virt_loc; |
| 1919 | const cpp_token **result; |
| 1920 | |
| 1921 | if (virt_loc_dest) |
| 1922 | { |
| 1923 | /* -ftrack-macro-expansion is on. */ |
| 1924 | if (map) |
| 1925 | macro_loc = linemap_add_macro_token (map, macro_token_index, |
| 1926 | virt_loc, parm_def_loc); |
| 1927 | *virt_loc_dest = macro_loc; |
| 1928 | } |
| 1929 | *dest = token; |
| 1930 | result = &dest[1]; |
| 1931 | |
| 1932 | return result; |
| 1933 | } |
| 1934 | |
| 1935 | /* Adds a token at the end of the tokens contained in BUFFER. Note |
| 1936 | that this function doesn't enlarge BUFFER when the number of tokens |
| 1937 | reaches BUFFER's size; it aborts in that situation. |
| 1938 | |
| 1939 | TOKEN is the token to append. VIRT_LOC is the virtual location of |
| 1940 | the token, i.e, the location possibly encoding its locus accross |
| 1941 | macro expansion. If TOKEN is an argument of a function-like macro |
| 1942 | (inside a macro replacement list), PARM_DEF_LOC is the location of |
| 1943 | the macro parameter that TOKEN is replacing. If TOKEN doesn't come |
| 1944 | from a macro expansion, then VIRT_LOC can just be set to the same |
| 1945 | value as PARM_DEF_LOC. If MAP is non null, it means TOKEN comes |
| 1946 | from a macro expansion and MAP is the macro map associated to the |
| 1947 | macro. MACRO_TOKEN_INDEX points to the index of the token in the |
| 1948 | macro map; It is not considered if MAP is NULL. If VIRT_LOCS is |
| 1949 | non-null, it means -ftrack-macro-expansion is on; in which case |
| 1950 | this function adds the virtual location DEF_LOC to the VIRT_LOCS |
| 1951 | array, at the same index as the one of TOKEN in BUFFER. Upon |
| 1952 | successful completion this function returns the a pointer to the |
| 1953 | position of the token coming right after the insertion point. */ |
| 1954 | static const cpp_token ** |
| 1955 | tokens_buff_add_token (_cpp_buff *buffer, |
| 1956 | source_location *virt_locs, |
| 1957 | const cpp_token *token, |
| 1958 | source_location virt_loc, |
| 1959 | source_location parm_def_loc, |
| 1960 | const struct line_map *map, |
| 1961 | unsigned int macro_token_index) |
| 1962 | { |
| 1963 | const cpp_token **result; |
| 1964 | source_location *virt_loc_dest = NULL; |
| 1965 | unsigned token_index = |
| 1966 | (BUFF_FRONT (buffer) - buffer->base) / sizeof (cpp_token *); |
| 1967 | |
| 1968 | /* Abort if we pass the end the buffer. */ |
| 1969 | if (BUFF_FRONT (buffer) > BUFF_LIMIT (buffer)) |
| 1970 | abort (); |
| 1971 | |
| 1972 | if (virt_locs != NULL) |
| 1973 | virt_loc_dest = &virt_locs[token_index]; |
| 1974 | |
| 1975 | result = |
| 1976 | tokens_buff_put_token_to ((const cpp_token **) BUFF_FRONT (buffer), |
| 1977 | virt_loc_dest, token, virt_loc, parm_def_loc, |
| 1978 | map, macro_token_index); |
| 1979 | |
| 1980 | BUFF_FRONT (buffer) = (unsigned char *) result; |
| 1981 | return result; |
| 1982 | } |
| 1983 | |
| 1984 | /* Allocate space for the function-like macro argument ARG to store |
| 1985 | the tokens resulting from the macro-expansion of the tokens that |
| 1986 | make up ARG itself. That space is allocated in ARG->expanded and |
| 1987 | needs to be freed using free. */ |
| 1988 | static void |
| 1989 | alloc_expanded_arg_mem (cpp_reader *pfile, macro_arg *arg, size_t capacity) |
| 1990 | { |
| 1991 | #ifdef ENABLE_CHECKING |
| 1992 | if (arg->expanded != NULL |
| 1993 | || arg->expanded_virt_locs != NULL) |
| 1994 | abort (); |
| 1995 | #endif |
| 1996 | arg->expanded = XNEWVEC (const cpp_token *, capacity); |
| 1997 | if (CPP_OPTION (pfile, track_macro_expansion)) |
| 1998 | arg->expanded_virt_locs = XNEWVEC (source_location, capacity); |
| 1999 | |
| 2000 | } |
| 2001 | |
| 2002 | /* If necessary, enlarge ARG->expanded to so that it can contain SIZE |
| 2003 | tokens. */ |
| 2004 | static void |
| 2005 | ensure_expanded_arg_room (cpp_reader *pfile, macro_arg *arg, |
| 2006 | size_t size, size_t *expanded_capacity) |
| 2007 | { |
| 2008 | if (size <= *expanded_capacity) |
| 2009 | return; |
| 2010 | |
| 2011 | size *= 2; |
| 2012 | |
| 2013 | arg->expanded = |
| 2014 | XRESIZEVEC (const cpp_token *, arg->expanded, size); |
| 2015 | *expanded_capacity = size; |
| 2016 | |
| 2017 | if (CPP_OPTION (pfile, track_macro_expansion)) |
| 2018 | { |
| 2019 | if (arg->expanded_virt_locs == NULL) |
| 2020 | arg->expanded_virt_locs = XNEWVEC (source_location, size); |
| 2021 | else |
| 2022 | arg->expanded_virt_locs = XRESIZEVEC (source_location, |
| 2023 | arg->expanded_virt_locs, |
| 2024 | size); |
| 2025 | } |
| 2026 | } |
| 2027 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 2028 | /* Expand an argument ARG before replacing parameters in a |
| 2029 | function-like macro. This works by pushing a context with the |
| 2030 | argument's tokens, and then expanding that into a temporary buffer |
| 2031 | as if it were a normal part of the token stream. collect_args() |
| 2032 | has terminated the argument's tokens with a CPP_EOF so that we know |
| 2033 | when we have fully expanded the argument. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2034 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2035 | expand_arg (cpp_reader *pfile, macro_arg *arg) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2036 | { |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2037 | size_t capacity; |
Neil Booth | 56941bf | 2002-09-20 19:44:09 +0000 | [diff] [blame] | 2038 | bool saved_warn_trad; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2039 | bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion); |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 2040 | |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2041 | if (arg->count == 0 |
| 2042 | || arg->expanded != NULL) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 2043 | return; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2044 | |
Neil Booth | 56941bf | 2002-09-20 19:44:09 +0000 | [diff] [blame] | 2045 | /* Don't warn about funlike macros when pre-expanding. */ |
| 2046 | saved_warn_trad = CPP_WTRADITIONAL (pfile); |
| 2047 | CPP_WTRADITIONAL (pfile) = 0; |
| 2048 | |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2049 | /* Loop, reading in the tokens of the argument. */ |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 2050 | capacity = 256; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2051 | alloc_expanded_arg_mem (pfile, arg, capacity); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2052 | |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2053 | if (track_macro_exp_p) |
| 2054 | push_extended_tokens_context (pfile, NULL, NULL, |
| 2055 | arg->virt_locs, |
| 2056 | arg->first, |
| 2057 | arg->count + 1); |
| 2058 | else |
| 2059 | push_ptoken_context (pfile, NULL, NULL, |
| 2060 | arg->first, arg->count + 1); |
| 2061 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 2062 | for (;;) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2063 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 2064 | const cpp_token *token; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2065 | source_location location; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 2066 | |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2067 | ensure_expanded_arg_room (pfile, arg, arg->expanded_count + 1, |
| 2068 | &capacity); |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 2069 | |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2070 | token = cpp_get_token_1 (pfile, &location); |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 2071 | |
| 2072 | if (token->type == CPP_EOF) |
| 2073 | break; |
| 2074 | |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2075 | set_arg_token (arg, token, location, |
| 2076 | arg->expanded_count, MACRO_ARG_TOKEN_EXPANDED, |
| 2077 | CPP_OPTION (pfile, track_macro_expansion)); |
| 2078 | arg->expanded_count++; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2079 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2080 | |
Neil Booth | 1e013d2 | 2001-09-26 21:44:35 +0000 | [diff] [blame] | 2081 | _cpp_pop_context (pfile); |
Neil Booth | 56941bf | 2002-09-20 19:44:09 +0000 | [diff] [blame] | 2082 | |
| 2083 | CPP_WTRADITIONAL (pfile) = saved_warn_trad; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 2086 | /* Pop the current context off the stack, re-enabling the macro if the |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2087 | context represented a macro's replacement list. Initially the |
| 2088 | context structure was not freed so that we can re-use it later, but |
| 2089 | now we do free it to reduce peak memory consumption. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2090 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2091 | _cpp_pop_context (cpp_reader *pfile) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2092 | { |
Neil Booth | 1e013d2 | 2001-09-26 21:44:35 +0000 | [diff] [blame] | 2093 | cpp_context *context = pfile->context; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2094 | |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2095 | if (context->c.macro) |
| 2096 | { |
| 2097 | cpp_hashnode *macro; |
| 2098 | if (context->tokens_kind == TOKENS_KIND_EXTENDED) |
| 2099 | { |
| 2100 | macro_context *mc = context->c.mc; |
| 2101 | macro = mc->macro_node; |
| 2102 | /* If context->buff is set, it means the life time of tokens |
| 2103 | is bound to the life time of this context; so we must |
| 2104 | free the tokens; that means we must free the virtual |
| 2105 | locations of these tokens too. */ |
| 2106 | if (context->buff && mc->virt_locs) |
| 2107 | { |
| 2108 | free (mc->virt_locs); |
| 2109 | mc->virt_locs = NULL; |
| 2110 | } |
| 2111 | free (mc); |
| 2112 | context->c.mc = NULL; |
| 2113 | } |
| 2114 | else |
| 2115 | macro = context->c.macro; |
| 2116 | |
| 2117 | /* Beware that MACRO can be NULL in cases like when we are |
| 2118 | called from expand_arg. In those cases, a dummy context with |
| 2119 | tokens is pushed just for the purpose of walking them using |
| 2120 | cpp_get_token_1. In that case, no 'macro' field is set into |
| 2121 | the dummy context. */ |
| 2122 | if (macro != NULL) |
| 2123 | macro->flags &= ~NODE_DISABLED; |
| 2124 | } |
Neil Booth | 1e013d2 | 2001-09-26 21:44:35 +0000 | [diff] [blame] | 2125 | |
| 2126 | if (context->buff) |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2127 | { |
| 2128 | /* Decrease memory peak consumption by freeing the memory used |
| 2129 | by the context. */ |
| 2130 | _cpp_free_buff (context->buff); |
| 2131 | } |
Neil Booth | 1e013d2 | 2001-09-26 21:44:35 +0000 | [diff] [blame] | 2132 | |
| 2133 | pfile->context = context->prev; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2134 | /* decrease peak memory consumption by feeing the context. */ |
| 2135 | pfile->context->next = NULL; |
| 2136 | free (context); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2137 | } |
| 2138 | |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2139 | /* Return TRUE if we reached the end of the set of tokens stored in |
| 2140 | CONTEXT, FALSE otherwise. */ |
| 2141 | static inline bool |
| 2142 | reached_end_of_context (cpp_context *context) |
| 2143 | { |
| 2144 | if (context->tokens_kind == TOKENS_KIND_DIRECT) |
| 2145 | return FIRST (context).token == LAST (context).token; |
| 2146 | else if (context->tokens_kind == TOKENS_KIND_INDIRECT |
| 2147 | || context->tokens_kind == TOKENS_KIND_EXTENDED) |
| 2148 | return FIRST (context).ptoken == LAST (context).ptoken; |
| 2149 | else |
| 2150 | abort (); |
| 2151 | } |
| 2152 | |
| 2153 | /* Consume the next token contained in the current context of PFILE, |
| 2154 | and return it in *TOKEN. It's "full location" is returned in |
| 2155 | *LOCATION. If -ftrack-macro-location is in effeect, fFull location" |
| 2156 | means the location encoding the locus of the token accross macro |
| 2157 | expansion; otherwise it's just is the "normal" location of the |
| 2158 | token which (*TOKEN)->src_loc. */ |
| 2159 | static inline void |
| 2160 | consume_next_token_from_context (cpp_reader *pfile, |
| 2161 | const cpp_token ** token, |
| 2162 | source_location *location) |
| 2163 | { |
| 2164 | cpp_context *c = pfile->context; |
| 2165 | |
| 2166 | if ((c)->tokens_kind == TOKENS_KIND_DIRECT) |
| 2167 | { |
| 2168 | *token = FIRST (c).token; |
| 2169 | *location = (*token)->src_loc; |
| 2170 | FIRST (c).token++; |
| 2171 | } |
| 2172 | else if ((c)->tokens_kind == TOKENS_KIND_INDIRECT) |
| 2173 | { |
| 2174 | *token = *FIRST (c).ptoken; |
| 2175 | *location = (*token)->src_loc; |
| 2176 | FIRST (c).ptoken++; |
| 2177 | } |
| 2178 | else if ((c)->tokens_kind == TOKENS_KIND_EXTENDED) |
| 2179 | { |
| 2180 | macro_context *m = c->c.mc; |
| 2181 | *token = *FIRST (c).ptoken; |
| 2182 | if (m->virt_locs) |
| 2183 | { |
| 2184 | *location = *m->cur_virt_loc; |
| 2185 | m->cur_virt_loc++; |
| 2186 | } |
| 2187 | else |
| 2188 | *location = (*token)->src_loc; |
| 2189 | FIRST (c).ptoken++; |
| 2190 | } |
| 2191 | else |
| 2192 | abort (); |
| 2193 | } |
| 2194 | |
| 2195 | /* In the traditional mode of the preprocessor, if we are currently in |
| 2196 | a directive, the location of a token must be the location of the |
| 2197 | start of the directive line. This function returns the proper |
| 2198 | location if we are in the traditional mode, and just returns |
| 2199 | LOCATION otherwise. */ |
| 2200 | |
| 2201 | static inline source_location |
| 2202 | maybe_adjust_loc_for_trad_cpp (cpp_reader *pfile, source_location location) |
| 2203 | { |
| 2204 | if (CPP_OPTION (pfile, traditional)) |
| 2205 | { |
| 2206 | if (pfile->state.in_directive) |
| 2207 | return pfile->directive_line; |
| 2208 | } |
| 2209 | return location; |
| 2210 | } |
| 2211 | |
| 2212 | /* Routine to get a token as well as its location. |
Neil Booth | 7f2f1a6 | 2000-11-14 18:32:06 +0000 | [diff] [blame] | 2213 | |
| 2214 | Macro expansions and directives are transparently handled, |
| 2215 | including entering included files. Thus tokens are post-macro |
| 2216 | expansion, and after any intervening directives. External callers |
| 2217 | see CPP_EOF only at EOF. Internal callers also see it when meeting |
| 2218 | a directive inside a macro call, when at the end of a directive and |
| 2219 | state.in_directive is still 1, and at the end of argument |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2220 | pre-expansion. |
| 2221 | |
| 2222 | LOC is an out parameter; *LOC is set to the location "as expected |
| 2223 | by the user". Please read the comment of |
| 2224 | cpp_get_token_with_location to learn more about the meaning of this |
| 2225 | location. */ |
| 2226 | static const cpp_token* |
| 2227 | cpp_get_token_1 (cpp_reader *pfile, source_location *location) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2228 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 2229 | const cpp_token *result; |
Tom Tromey | 5ffeb913 | 2007-09-06 16:24:05 +0000 | [diff] [blame] | 2230 | bool can_set = pfile->set_invocation_location; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2231 | /* This token is a virtual token that either encodes a location |
| 2232 | related to macro expansion or a spelling location. */ |
| 2233 | source_location virt_loc = 0; |
Tom Tromey | 5ffeb913 | 2007-09-06 16:24:05 +0000 | [diff] [blame] | 2234 | pfile->set_invocation_location = false; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 2235 | |
Neil Booth | 29b1074 | 2000-11-13 18:40:37 +0000 | [diff] [blame] | 2236 | for (;;) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2237 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 2238 | cpp_hashnode *node; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2239 | cpp_context *context = pfile->context; |
| 2240 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2241 | /* Context->prev == 0 <=> base context. */ |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 2242 | if (!context->prev) |
Neil Booth | 29b1074 | 2000-11-13 18:40:37 +0000 | [diff] [blame] | 2243 | { |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2244 | result = _cpp_lex_token (pfile); |
| 2245 | virt_loc = result->src_loc; |
| 2246 | } |
| 2247 | else if (!reached_end_of_context (context)) |
| 2248 | { |
| 2249 | consume_next_token_from_context (pfile, &result, |
| 2250 | &virt_loc); |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 2251 | if (result->flags & PASTE_LEFT) |
Neil Booth | ec1a23e | 2001-01-31 07:48:54 +0000 | [diff] [blame] | 2252 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 2253 | paste_all_tokens (pfile, result); |
| 2254 | if (pfile->state.in_directive) |
| 2255 | continue; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2256 | result = padding_token (pfile, result); |
| 2257 | goto out; |
Neil Booth | ec1a23e | 2001-01-31 07:48:54 +0000 | [diff] [blame] | 2258 | } |
Neil Booth | 29b1074 | 2000-11-13 18:40:37 +0000 | [diff] [blame] | 2259 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2260 | else |
| 2261 | { |
Tom Tromey | 64a1a42 | 2011-10-17 09:59:52 +0000 | [diff] [blame^] | 2262 | if (pfile->context->c.macro) |
| 2263 | ++num_expanded_macros_counter; |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 2264 | _cpp_pop_context (pfile); |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 2265 | if (pfile->state.in_directive) |
| 2266 | continue; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2267 | result = &pfile->avoid_paste; |
| 2268 | goto out; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2269 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2270 | |
Jason Thorpe | 477cdac | 2002-04-07 03:12:23 +0000 | [diff] [blame] | 2271 | if (pfile->state.in_directive && result->type == CPP_COMMENT) |
| 2272 | continue; |
| 2273 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 2274 | if (result->type != CPP_NAME) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2275 | break; |
| 2276 | |
Joseph Myers | 9a0c618 | 2009-05-10 15:27:32 +0100 | [diff] [blame] | 2277 | node = result->val.node.node; |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame] | 2278 | |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 2279 | if (node->type != NT_MACRO || (result->flags & NO_EXPAND)) |
| 2280 | break; |
Kazu Hirata | df38348 | 2002-05-22 22:02:16 +0000 | [diff] [blame] | 2281 | |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 2282 | if (!(node->flags & NODE_DISABLED)) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 2283 | { |
Ben Elliston | 5950c3c | 2008-07-14 05:09:48 +0000 | [diff] [blame] | 2284 | int ret = 0; |
Tom Tromey | 5ffeb913 | 2007-09-06 16:24:05 +0000 | [diff] [blame] | 2285 | /* If not in a macro context, and we're going to start an |
| 2286 | expansion, record the location. */ |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2287 | if (can_set && !context->c.macro) |
Tom Tromey | 5ffeb913 | 2007-09-06 16:24:05 +0000 | [diff] [blame] | 2288 | pfile->invocation_location = result->src_loc; |
Jakub Jelinek | 765d600 | 2008-01-25 10:01:27 +0100 | [diff] [blame] | 2289 | if (pfile->state.prevent_expansion) |
| 2290 | break; |
Ben Elliston | 5950c3c | 2008-07-14 05:09:48 +0000 | [diff] [blame] | 2291 | |
| 2292 | /* Conditional macros require that a predicate be evaluated |
| 2293 | first. */ |
Jakub Jelinek | a37a7b8 | 2009-03-30 17:00:52 +0200 | [diff] [blame] | 2294 | if ((node->flags & NODE_CONDITIONAL) != 0) |
| 2295 | { |
| 2296 | if (pfile->cb.macro_to_expand) |
| 2297 | { |
| 2298 | bool whitespace_after; |
| 2299 | const cpp_token *peek_tok = cpp_peek_token (pfile, 0); |
| 2300 | |
| 2301 | whitespace_after = (peek_tok->type == CPP_PADDING |
| 2302 | || (peek_tok->flags & PREV_WHITE)); |
| 2303 | node = pfile->cb.macro_to_expand (pfile, result); |
| 2304 | if (node) |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2305 | ret = enter_macro_context (pfile, node, result, |
| 2306 | virt_loc); |
Jakub Jelinek | a37a7b8 | 2009-03-30 17:00:52 +0200 | [diff] [blame] | 2307 | else if (whitespace_after) |
| 2308 | { |
| 2309 | /* If macro_to_expand hook returned NULL and it |
| 2310 | ate some tokens, see if we don't need to add |
| 2311 | a padding token in between this and the |
| 2312 | next token. */ |
| 2313 | peek_tok = cpp_peek_token (pfile, 0); |
| 2314 | if (peek_tok->type != CPP_PADDING |
| 2315 | && (peek_tok->flags & PREV_WHITE) == 0) |
| 2316 | _cpp_push_token_context (pfile, NULL, |
| 2317 | padding_token (pfile, |
| 2318 | peek_tok), 1); |
| 2319 | } |
| 2320 | } |
| 2321 | } |
| 2322 | else |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2323 | ret = enter_macro_context (pfile, node, result, |
| 2324 | virt_loc); |
Jakub Jelinek | a37a7b8 | 2009-03-30 17:00:52 +0200 | [diff] [blame] | 2325 | if (ret) |
Ben Elliston | 5950c3c | 2008-07-14 05:09:48 +0000 | [diff] [blame] | 2326 | { |
Jakub Jelinek | 765d600 | 2008-01-25 10:01:27 +0100 | [diff] [blame] | 2327 | if (pfile->state.in_directive || ret == 2) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 2328 | continue; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2329 | result = padding_token (pfile, result); |
| 2330 | goto out; |
Neil Booth | bd96977 | 2001-02-01 19:13:53 +0000 | [diff] [blame] | 2331 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2332 | } |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 2333 | else |
| 2334 | { |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 2335 | /* Flag this token as always unexpandable. FIXME: move this |
| 2336 | to collect_args()?. */ |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 2337 | cpp_token *t = _cpp_temp_token (pfile); |
| 2338 | t->type = result->type; |
| 2339 | t->flags = result->flags | NO_EXPAND; |
Jakub Jelinek | 7309671 | 2005-03-04 16:33:23 +0100 | [diff] [blame] | 2340 | t->val = result->val; |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 2341 | result = t; |
| 2342 | } |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 2343 | |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 2344 | break; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2345 | } |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 2346 | |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2347 | out: |
| 2348 | if (location != NULL) |
| 2349 | { |
| 2350 | if (virt_loc == 0) |
| 2351 | virt_loc = result->src_loc; |
| 2352 | *location = virt_loc; |
| 2353 | |
| 2354 | if (!CPP_OPTION (pfile, track_macro_expansion) |
| 2355 | && can_set |
| 2356 | && pfile->context->c.macro != NULL) |
| 2357 | /* We are in a macro expansion context, are not tracking |
| 2358 | virtual location, but were asked to report the location |
| 2359 | of the expansion point of the macro being expanded. */ |
| 2360 | *location = pfile->invocation_location; |
| 2361 | |
| 2362 | *location = maybe_adjust_loc_for_trad_cpp (pfile, *location); |
| 2363 | } |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 2364 | return result; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2365 | } |
| 2366 | |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2367 | /* External routine to get a token. Also used nearly everywhere |
| 2368 | internally, except for places where we know we can safely call |
| 2369 | _cpp_lex_token directly, such as lexing a directive name. |
| 2370 | |
| 2371 | Macro expansions and directives are transparently handled, |
| 2372 | including entering included files. Thus tokens are post-macro |
| 2373 | expansion, and after any intervening directives. External callers |
| 2374 | see CPP_EOF only at EOF. Internal callers also see it when meeting |
| 2375 | a directive inside a macro call, when at the end of a directive and |
| 2376 | state.in_directive is still 1, and at the end of argument |
| 2377 | pre-expansion. */ |
| 2378 | const cpp_token * |
| 2379 | cpp_get_token (cpp_reader *pfile) |
| 2380 | { |
| 2381 | return cpp_get_token_1 (pfile, NULL); |
| 2382 | } |
| 2383 | |
| 2384 | /* Like cpp_get_token, but also returns a virtual token location |
| 2385 | separate from the spelling location carried by the returned token. |
| 2386 | |
| 2387 | LOC is an out parameter; *LOC is set to the location "as expected |
| 2388 | by the user". This matters when a token results from macro |
| 2389 | expansion; in that case the token's spelling location indicates the |
| 2390 | locus of the token in the definition of the macro but *LOC |
| 2391 | virtually encodes all the other meaningful locuses associated to |
| 2392 | the token. |
| 2393 | |
| 2394 | What? virtual location? Yes, virtual location. |
| 2395 | |
| 2396 | If the token results from macro expansion and if macro expansion |
| 2397 | location tracking is enabled its virtual location encodes (at the |
| 2398 | same time): |
| 2399 | |
| 2400 | - the spelling location of the token |
| 2401 | |
| 2402 | - the locus of the macro expansion point |
| 2403 | |
| 2404 | - the locus of the point where the token got instantiated as part |
| 2405 | of the macro expansion process. |
| 2406 | |
| 2407 | You have to use the linemap API to get the locus you are interested |
| 2408 | in from a given virtual location. |
| 2409 | |
| 2410 | Note however that virtual locations are not necessarily ordered for |
| 2411 | relations '<' and '>'. One must use the function |
| 2412 | linemap_location_before_p instead of using the relational operator |
| 2413 | '<'. |
| 2414 | |
| 2415 | If macro expansion tracking is off and if the token results from |
| 2416 | macro expansion the virtual location is the expansion point of the |
| 2417 | macro that got expanded. |
| 2418 | |
| 2419 | When the token doesn't result from macro expansion, the virtual |
| 2420 | location is just the same thing as its spelling location. */ |
| 2421 | |
Tom Tromey | 5ffeb913 | 2007-09-06 16:24:05 +0000 | [diff] [blame] | 2422 | const cpp_token * |
| 2423 | cpp_get_token_with_location (cpp_reader *pfile, source_location *loc) |
| 2424 | { |
| 2425 | const cpp_token *result; |
| 2426 | |
| 2427 | pfile->set_invocation_location = true; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2428 | result = cpp_get_token_1 (pfile, loc); |
Tom Tromey | 5ffeb913 | 2007-09-06 16:24:05 +0000 | [diff] [blame] | 2429 | return result; |
| 2430 | } |
| 2431 | |
Neil Booth | 7065e13 | 2001-02-14 07:38:20 +0000 | [diff] [blame] | 2432 | /* Returns true if we're expanding an object-like macro that was |
| 2433 | defined in a system header. Just checks the macro at the top of |
| 2434 | the stack. Used for diagnostic suppression. */ |
| 2435 | int |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2436 | cpp_sys_macro_p (cpp_reader *pfile) |
Neil Booth | 7065e13 | 2001-02-14 07:38:20 +0000 | [diff] [blame] | 2437 | { |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2438 | cpp_hashnode *node = pfile->context->c.macro; |
Neil Booth | 7065e13 | 2001-02-14 07:38:20 +0000 | [diff] [blame] | 2439 | |
Neil Booth | 644edda | 2001-10-02 12:57:24 +0000 | [diff] [blame] | 2440 | return node && node->value.macro && node->value.macro->syshdr; |
Neil Booth | 7065e13 | 2001-02-14 07:38:20 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
Neil Booth | af0d16c | 2002-04-22 17:48:02 +0000 | [diff] [blame] | 2443 | /* Read each token in, until end of the current file. Directives are |
| 2444 | transparently processed. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2445 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2446 | cpp_scan_nooutput (cpp_reader *pfile) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2447 | { |
Per Bothner | 22234f5 | 2004-02-18 14:02:39 -0800 | [diff] [blame] | 2448 | /* Request a CPP_EOF token at the end of this file, rather than |
| 2449 | transparently continuing with the including file. */ |
| 2450 | pfile->buffer->return_at_eof = true; |
| 2451 | |
Zack Weinberg | c6e8380 | 2004-06-05 20:58:06 +0000 | [diff] [blame] | 2452 | pfile->state.discarding_output++; |
| 2453 | pfile->state.prevent_expansion++; |
| 2454 | |
Neil Booth | 590e198 | 2002-07-01 12:47:54 +0000 | [diff] [blame] | 2455 | if (CPP_OPTION (pfile, traditional)) |
| 2456 | while (_cpp_read_logical_line_trad (pfile)) |
| 2457 | ; |
| 2458 | else |
| 2459 | while (cpp_get_token (pfile)->type != CPP_EOF) |
| 2460 | ; |
Zack Weinberg | c6e8380 | 2004-06-05 20:58:06 +0000 | [diff] [blame] | 2461 | |
| 2462 | pfile->state.discarding_output--; |
| 2463 | pfile->state.prevent_expansion--; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2464 | } |
| 2465 | |
Ben Elliston | 5950c3c | 2008-07-14 05:09:48 +0000 | [diff] [blame] | 2466 | /* Step back one or more tokens obtained from the lexer. */ |
| 2467 | void |
| 2468 | _cpp_backup_tokens_direct (cpp_reader *pfile, unsigned int count) |
| 2469 | { |
| 2470 | pfile->lookaheads += count; |
| 2471 | while (count--) |
| 2472 | { |
| 2473 | pfile->cur_token--; |
| 2474 | if (pfile->cur_token == pfile->cur_run->base |
| 2475 | /* Possible with -fpreprocessed and no leading #line. */ |
| 2476 | && pfile->cur_run->prev != NULL) |
| 2477 | { |
| 2478 | pfile->cur_run = pfile->cur_run->prev; |
| 2479 | pfile->cur_token = pfile->cur_run->limit; |
| 2480 | } |
| 2481 | } |
| 2482 | } |
| 2483 | |
Jakub Jelinek | 1c90c6f | 2006-06-09 23:13:25 +0200 | [diff] [blame] | 2484 | /* Step back one (or more) tokens. Can only step back more than 1 if |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 2485 | they are from the lexer, and not from macro expansion. */ |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 2486 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2487 | _cpp_backup_tokens (cpp_reader *pfile, unsigned int count) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2488 | { |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 2489 | if (pfile->context->prev == NULL) |
Ben Elliston | 5950c3c | 2008-07-14 05:09:48 +0000 | [diff] [blame] | 2490 | _cpp_backup_tokens_direct (pfile, count); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2491 | else |
| 2492 | { |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 2493 | if (count != 1) |
| 2494 | abort (); |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2495 | if (pfile->context->tokens_kind == TOKENS_KIND_DIRECT) |
Neil Booth | 82eda77 | 2002-06-04 13:07:06 +0000 | [diff] [blame] | 2496 | FIRST (pfile->context).token--; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2497 | else if (pfile->context->tokens_kind == TOKENS_KIND_INDIRECT) |
Neil Booth | 82eda77 | 2002-06-04 13:07:06 +0000 | [diff] [blame] | 2498 | FIRST (pfile->context).ptoken--; |
Tom Tromey | 92582b7 | 2011-10-17 09:59:12 +0000 | [diff] [blame] | 2499 | else if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED) |
| 2500 | { |
| 2501 | FIRST (pfile->context).ptoken--; |
| 2502 | if (pfile->context->c.macro) |
| 2503 | { |
| 2504 | macro_context *m = pfile->context->c.mc; |
| 2505 | m->cur_virt_loc--; |
| 2506 | #ifdef ENABLE_CHECKING |
| 2507 | if (m->cur_virt_loc < m->virt_locs) |
| 2508 | abort (); |
| 2509 | #endif |
| 2510 | } |
| 2511 | else |
| 2512 | abort (); |
| 2513 | } |
| 2514 | else |
| 2515 | abort (); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2516 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2517 | } |
| 2518 | |
| 2519 | /* #define directive parsing and handling. */ |
| 2520 | |
Kazu Hirata | da7d830 | 2002-09-22 02:03:17 +0000 | [diff] [blame] | 2521 | /* Returns nonzero if a macro redefinition warning is required. */ |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 2522 | static bool |
Jakub Jelinek | 8e680db | 2010-06-11 20:37:34 +0200 | [diff] [blame] | 2523 | warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node, |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2524 | const cpp_macro *macro2) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2525 | { |
| 2526 | const cpp_macro *macro1; |
| 2527 | unsigned int i; |
| 2528 | |
Neil Booth | 618cdda | 2001-02-25 09:43:03 +0000 | [diff] [blame] | 2529 | /* Some redefinitions need to be warned about regardless. */ |
| 2530 | if (node->flags & NODE_WARN) |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 2531 | return true; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2532 | |
Simon Baldwin | c047ce9 | 2008-09-18 15:39:08 +0000 | [diff] [blame] | 2533 | /* Suppress warnings for builtins that lack the NODE_WARN flag. */ |
| 2534 | if (node->flags & NODE_BUILTIN) |
Jakub Jelinek | 8e680db | 2010-06-11 20:37:34 +0200 | [diff] [blame] | 2535 | { |
| 2536 | if (!pfile->cb.user_builtin_macro |
| 2537 | || !pfile->cb.user_builtin_macro (pfile, node)) |
| 2538 | return false; |
| 2539 | } |
Simon Baldwin | c047ce9 | 2008-09-18 15:39:08 +0000 | [diff] [blame] | 2540 | |
Ben Elliston | 5950c3c | 2008-07-14 05:09:48 +0000 | [diff] [blame] | 2541 | /* Redefinitions of conditional (context-sensitive) macros, on |
| 2542 | the other hand, must be allowed silently. */ |
| 2543 | if (node->flags & NODE_CONDITIONAL) |
| 2544 | return false; |
| 2545 | |
Neil Booth | 618cdda | 2001-02-25 09:43:03 +0000 | [diff] [blame] | 2546 | /* 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] | 2547 | definitions are the same. (6.10.3 paragraph 2). */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2548 | macro1 = node->value.macro; |
| 2549 | |
Neil Booth | 6618c5d | 2002-06-10 06:03:13 +0000 | [diff] [blame] | 2550 | /* Don't check count here as it can be different in valid |
| 2551 | traditional redefinitions with just whitespace differences. */ |
| 2552 | if (macro1->paramc != macro2->paramc |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2553 | || macro1->fun_like != macro2->fun_like |
Neil Booth | 28e0f04 | 2000-12-09 12:06:37 +0000 | [diff] [blame] | 2554 | || macro1->variadic != macro2->variadic) |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 2555 | return true; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2556 | |
| 2557 | /* Check parameter spellings. */ |
| 2558 | for (i = 0; i < macro1->paramc; i++) |
| 2559 | if (macro1->params[i] != macro2->params[i]) |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 2560 | return true; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2561 | |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 2562 | /* Check the replacement text or tokens. */ |
| 2563 | if (CPP_OPTION (pfile, traditional)) |
Neil Booth | 6618c5d | 2002-06-10 06:03:13 +0000 | [diff] [blame] | 2564 | return _cpp_expansions_different_trad (macro1, macro2); |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 2565 | |
DJ Delorie | a7f36da | 2003-06-01 14:55:15 -0400 | [diff] [blame] | 2566 | if (macro1->count != macro2->count) |
| 2567 | return true; |
| 2568 | |
| 2569 | for (i = 0; i < macro1->count; i++) |
| 2570 | if (!_cpp_equiv_tokens (¯o1->exp.tokens[i], ¯o2->exp.tokens[i])) |
| 2571 | return true; |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 2572 | |
| 2573 | return false; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2574 | } |
| 2575 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2576 | /* Free the definition of hashnode H. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2577 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2578 | _cpp_free_definition (cpp_hashnode *h) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2579 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2580 | /* Macros and assertions no longer have anything to free. */ |
| 2581 | h->type = NT_VOID; |
| 2582 | /* Clear builtin flag in case of redefinition. */ |
Joseph Myers | 93d45d9 | 2008-04-02 20:42:53 +0100 | [diff] [blame] | 2583 | h->flags &= ~(NODE_BUILTIN | NODE_DISABLED | NODE_USED); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2584 | } |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2585 | |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 2586 | /* Save parameter NODE to the parameter list of macro MACRO. Returns |
Kazu Hirata | da7d830 | 2002-09-22 02:03:17 +0000 | [diff] [blame] | 2587 | zero on success, nonzero if the parameter is a duplicate. */ |
Neil Booth | c70f6ed | 2002-06-07 06:26:32 +0000 | [diff] [blame] | 2588 | bool |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2589 | _cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2590 | { |
Zack Weinberg | 4977bab | 2002-12-16 18:23:00 +0000 | [diff] [blame] | 2591 | unsigned int len; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2592 | /* Constraint 6.10.3.6 - duplicate parameter names. */ |
Zack Weinberg | 4977bab | 2002-12-16 18:23:00 +0000 | [diff] [blame] | 2593 | if (node->flags & NODE_MACRO_ARG) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2594 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 2595 | cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"", |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 2596 | NODE_NAME (node)); |
Neil Booth | 23ff022 | 2002-07-17 17:27:14 +0000 | [diff] [blame] | 2597 | return true; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 2600 | if (BUFF_ROOM (pfile->a_buff) |
| 2601 | < (macro->paramc + 1) * sizeof (cpp_hashnode *)) |
| 2602 | _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *)); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2603 | |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 2604 | ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node; |
Zack Weinberg | 4977bab | 2002-12-16 18:23:00 +0000 | [diff] [blame] | 2605 | node->flags |= NODE_MACRO_ARG; |
| 2606 | len = macro->paramc * sizeof (union _cpp_hashnode_value); |
| 2607 | if (len > pfile->macro_buffer_len) |
| 2608 | { |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 2609 | pfile->macro_buffer = XRESIZEVEC (unsigned char, pfile->macro_buffer, |
| 2610 | len); |
Zack Weinberg | 4977bab | 2002-12-16 18:23:00 +0000 | [diff] [blame] | 2611 | pfile->macro_buffer_len = len; |
| 2612 | } |
| 2613 | ((union _cpp_hashnode_value *) pfile->macro_buffer)[macro->paramc - 1] |
| 2614 | = node->value; |
| 2615 | |
| 2616 | node->value.arg_index = macro->paramc; |
Neil Booth | 23ff022 | 2002-07-17 17:27:14 +0000 | [diff] [blame] | 2617 | return false; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2618 | } |
| 2619 | |
Neil Booth | 23ff022 | 2002-07-17 17:27:14 +0000 | [diff] [blame] | 2620 | /* Check the syntax of the parameters in a MACRO definition. Returns |
| 2621 | false if an error occurs. */ |
| 2622 | static bool |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2623 | parse_params (cpp_reader *pfile, cpp_macro *macro) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2624 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2625 | unsigned int prev_ident = 0; |
| 2626 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2627 | for (;;) |
| 2628 | { |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 2629 | const cpp_token *token = _cpp_lex_token (pfile); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2630 | |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 2631 | switch (token->type) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2632 | { |
| 2633 | default: |
Jason Thorpe | 477cdac | 2002-04-07 03:12:23 +0000 | [diff] [blame] | 2634 | /* Allow/ignore comments in parameter lists if we are |
| 2635 | preserving comments in macro expansions. */ |
| 2636 | if (token->type == CPP_COMMENT |
| 2637 | && ! CPP_OPTION (pfile, discard_comments_in_macro_exp)) |
| 2638 | continue; |
| 2639 | |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 2640 | cpp_error (pfile, CPP_DL_ERROR, |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 2641 | "\"%s\" may not appear in macro parameter list", |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 2642 | cpp_token_as_text (pfile, token)); |
Neil Booth | 23ff022 | 2002-07-17 17:27:14 +0000 | [diff] [blame] | 2643 | return false; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2644 | |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2645 | case CPP_NAME: |
| 2646 | if (prev_ident) |
| 2647 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 2648 | cpp_error (pfile, CPP_DL_ERROR, |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 2649 | "macro parameters must be comma-separated"); |
Neil Booth | 23ff022 | 2002-07-17 17:27:14 +0000 | [diff] [blame] | 2650 | return false; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2651 | } |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2652 | prev_ident = 1; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2653 | |
Joseph Myers | 9a0c618 | 2009-05-10 15:27:32 +0100 | [diff] [blame] | 2654 | if (_cpp_save_parameter (pfile, macro, token->val.node.node)) |
Neil Booth | 23ff022 | 2002-07-17 17:27:14 +0000 | [diff] [blame] | 2655 | return false; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2656 | continue; |
| 2657 | |
| 2658 | case CPP_CLOSE_PAREN: |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2659 | if (prev_ident || macro->paramc == 0) |
Neil Booth | 23ff022 | 2002-07-17 17:27:14 +0000 | [diff] [blame] | 2660 | return true; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2661 | |
| 2662 | /* Fall through to pick up the error. */ |
| 2663 | case CPP_COMMA: |
| 2664 | if (!prev_ident) |
| 2665 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 2666 | cpp_error (pfile, CPP_DL_ERROR, "parameter name missing"); |
Neil Booth | 23ff022 | 2002-07-17 17:27:14 +0000 | [diff] [blame] | 2667 | return false; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2668 | } |
| 2669 | prev_ident = 0; |
| 2670 | continue; |
| 2671 | |
| 2672 | case CPP_ELLIPSIS: |
Neil Booth | 28e0f04 | 2000-12-09 12:06:37 +0000 | [diff] [blame] | 2673 | macro->variadic = 1; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2674 | if (!prev_ident) |
| 2675 | { |
Neil Booth | c70f6ed | 2002-06-07 06:26:32 +0000 | [diff] [blame] | 2676 | _cpp_save_parameter (pfile, macro, |
| 2677 | pfile->spec_nodes.n__VA_ARGS__); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2678 | pfile->state.va_args_ok = 1; |
Richard Henderson | e5b7921 | 2004-02-19 14:18:50 -0800 | [diff] [blame] | 2679 | if (! CPP_OPTION (pfile, c99) |
Joseph Myers | e3339d0 | 2010-09-29 15:49:14 +0100 | [diff] [blame] | 2680 | && CPP_OPTION (pfile, cpp_pedantic) |
Richard Henderson | e5b7921 | 2004-02-19 14:18:50 -0800 | [diff] [blame] | 2681 | && CPP_OPTION (pfile, warn_variadic_macros)) |
Simon Baldwin | 87cf065 | 2010-04-07 17:18:10 +0000 | [diff] [blame] | 2682 | cpp_pedwarning |
| 2683 | (pfile, CPP_W_VARIADIC_MACROS, |
| 2684 | "anonymous variadic macros were introduced in C99"); |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2685 | } |
Joseph Myers | e3339d0 | 2010-09-29 15:49:14 +0100 | [diff] [blame] | 2686 | else if (CPP_OPTION (pfile, cpp_pedantic) |
Richard Henderson | e5b7921 | 2004-02-19 14:18:50 -0800 | [diff] [blame] | 2687 | && CPP_OPTION (pfile, warn_variadic_macros)) |
Simon Baldwin | 87cf065 | 2010-04-07 17:18:10 +0000 | [diff] [blame] | 2688 | cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS, |
| 2689 | "ISO C does not permit named variadic macros"); |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2690 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2691 | /* We're at the end, and just expect a closing parenthesis. */ |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 2692 | token = _cpp_lex_token (pfile); |
| 2693 | if (token->type == CPP_CLOSE_PAREN) |
Neil Booth | 23ff022 | 2002-07-17 17:27:14 +0000 | [diff] [blame] | 2694 | return true; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2695 | /* Fall through. */ |
| 2696 | |
| 2697 | case CPP_EOF: |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 2698 | cpp_error (pfile, CPP_DL_ERROR, "missing ')' in macro parameter list"); |
Neil Booth | 23ff022 | 2002-07-17 17:27:14 +0000 | [diff] [blame] | 2699 | return false; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2700 | } |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2701 | } |
| 2702 | } |
| 2703 | |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 2704 | /* Allocate room for a token from a macro's replacement list. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2705 | static cpp_token * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2706 | alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2707 | { |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 2708 | if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token)) |
| 2709 | _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token)); |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2710 | |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 2711 | return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++]; |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 2712 | } |
| 2713 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 2714 | /* Lex a token from the expansion of MACRO, but mark parameters as we |
| 2715 | find them and warn of traditional stringification. */ |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 2716 | static cpp_token * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2717 | lex_expansion_token (cpp_reader *pfile, cpp_macro *macro) |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 2718 | { |
Tom Tromey | ee38036 | 2007-01-30 15:46:01 +0000 | [diff] [blame] | 2719 | cpp_token *token, *saved_cur_token; |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 2720 | |
Tom Tromey | ee38036 | 2007-01-30 15:46:01 +0000 | [diff] [blame] | 2721 | saved_cur_token = pfile->cur_token; |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 2722 | pfile->cur_token = alloc_expansion_token (pfile, macro); |
| 2723 | token = _cpp_lex_direct (pfile); |
Tom Tromey | ee38036 | 2007-01-30 15:46:01 +0000 | [diff] [blame] | 2724 | pfile->cur_token = saved_cur_token; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2725 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 2726 | /* Is this a parameter? */ |
Zack Weinberg | 4977bab | 2002-12-16 18:23:00 +0000 | [diff] [blame] | 2727 | if (token->type == CPP_NAME |
Joseph Myers | 9a0c618 | 2009-05-10 15:27:32 +0100 | [diff] [blame] | 2728 | && (token->val.node.node->flags & NODE_MACRO_ARG) != 0) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2729 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2730 | token->type = CPP_MACRO_ARG; |
Joseph Myers | 9a0c618 | 2009-05-10 15:27:32 +0100 | [diff] [blame] | 2731 | token->val.macro_arg.arg_no = token->val.node.node->value.arg_index; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2732 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2733 | else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0 |
| 2734 | && (token->type == CPP_STRING || token->type == CPP_CHAR)) |
| 2735 | check_trad_stringification (pfile, macro, &token->val.str); |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2736 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2737 | return token; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2738 | } |
| 2739 | |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 2740 | static bool |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2741 | create_iso_definition (cpp_reader *pfile, cpp_macro *macro) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2742 | { |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 2743 | cpp_token *token; |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 2744 | const cpp_token *ctoken; |
Simon Martin | 126e073 | 2007-05-23 20:58:34 +0000 | [diff] [blame] | 2745 | bool following_paste_op = false; |
| 2746 | const char *paste_op_error_msg = |
| 2747 | N_("'##' cannot appear at either end of a macro expansion"); |
Joseph Myers | aa50850 | 2009-04-19 18:10:56 +0100 | [diff] [blame] | 2748 | unsigned int num_extra_tokens = 0; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2749 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2750 | /* Get the first token of the expansion (or the '(' of a |
| 2751 | function-like macro). */ |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 2752 | ctoken = _cpp_lex_token (pfile); |
| 2753 | |
| 2754 | if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE)) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2755 | { |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 2756 | bool ok = parse_params (pfile, macro); |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 2757 | macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff); |
| 2758 | if (!ok) |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 2759 | return false; |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 2760 | |
Geoffrey Keating | d804416 | 2004-06-09 20:10:13 +0000 | [diff] [blame] | 2761 | /* Success. Commit or allocate the parameter array. */ |
| 2762 | if (pfile->hash_table->alloc_subobject) |
| 2763 | { |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 2764 | cpp_hashnode **params = |
| 2765 | (cpp_hashnode **) pfile->hash_table->alloc_subobject |
| 2766 | (sizeof (cpp_hashnode *) * macro->paramc); |
Paolo Bonzini | be0f1e5 | 2005-02-14 08:52:24 +0000 | [diff] [blame] | 2767 | memcpy (params, macro->params, |
| 2768 | sizeof (cpp_hashnode *) * macro->paramc); |
| 2769 | macro->params = params; |
Geoffrey Keating | d804416 | 2004-06-09 20:10:13 +0000 | [diff] [blame] | 2770 | } |
| 2771 | else |
| 2772 | BUFF_FRONT (pfile->a_buff) = (uchar *) ¯o->params[macro->paramc]; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2773 | macro->fun_like = 1; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2774 | } |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 2775 | else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE)) |
Jakub Jelinek | cae064e | 2005-04-05 22:07:06 +0200 | [diff] [blame] | 2776 | { |
| 2777 | /* While ISO C99 requires whitespace before replacement text |
| 2778 | in a macro definition, ISO C90 with TC1 allows there characters |
| 2779 | from the basic source character set. */ |
| 2780 | if (CPP_OPTION (pfile, c99)) |
| 2781 | cpp_error (pfile, CPP_DL_PEDWARN, |
| 2782 | "ISO C99 requires whitespace after the macro name"); |
| 2783 | else |
| 2784 | { |
| 2785 | int warntype = CPP_DL_WARNING; |
| 2786 | switch (ctoken->type) |
| 2787 | { |
| 2788 | case CPP_ATSIGN: |
| 2789 | case CPP_AT_NAME: |
| 2790 | case CPP_OBJC_STRING: |
| 2791 | /* '@' is not in basic character set. */ |
| 2792 | warntype = CPP_DL_PEDWARN; |
| 2793 | break; |
| 2794 | case CPP_OTHER: |
| 2795 | /* Basic character set sans letters, digits and _. */ |
| 2796 | if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~", |
| 2797 | ctoken->val.str.text[0]) == NULL) |
| 2798 | warntype = CPP_DL_PEDWARN; |
| 2799 | break; |
| 2800 | default: |
| 2801 | /* All other tokens start with a character from basic |
| 2802 | character set. */ |
| 2803 | break; |
| 2804 | } |
| 2805 | cpp_error (pfile, warntype, |
| 2806 | "missing whitespace after the macro name"); |
| 2807 | } |
| 2808 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2809 | |
Neil Booth | 14baae0 | 2001-09-17 18:26:12 +0000 | [diff] [blame] | 2810 | if (macro->fun_like) |
| 2811 | token = lex_expansion_token (pfile, macro); |
| 2812 | else |
| 2813 | { |
| 2814 | token = alloc_expansion_token (pfile, macro); |
| 2815 | *token = *ctoken; |
| 2816 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2817 | |
| 2818 | for (;;) |
| 2819 | { |
| 2820 | /* Check the stringifying # constraint 6.10.3.2.1 of |
| 2821 | function-like macros when lexing the subsequent token. */ |
| 2822 | if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like) |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 2823 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2824 | if (token->type == CPP_MACRO_ARG) |
| 2825 | { |
Joseph Myers | aa50850 | 2009-04-19 18:10:56 +0100 | [diff] [blame] | 2826 | if (token->flags & PREV_WHITE) |
| 2827 | token->flags |= SP_PREV_WHITE; |
| 2828 | if (token[-1].flags & DIGRAPH) |
| 2829 | token->flags |= SP_DIGRAPH; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2830 | token->flags &= ~PREV_WHITE; |
| 2831 | token->flags |= STRINGIFY_ARG; |
| 2832 | token->flags |= token[-1].flags & PREV_WHITE; |
| 2833 | token[-1] = token[0]; |
| 2834 | macro->count--; |
| 2835 | } |
| 2836 | /* Let assembler get away with murder. */ |
Neil Booth | bdb05a7 | 2000-11-26 17:31:13 +0000 | [diff] [blame] | 2837 | else if (CPP_OPTION (pfile, lang) != CLK_ASM) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2838 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 2839 | cpp_error (pfile, CPP_DL_ERROR, |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 2840 | "'#' is not followed by a macro parameter"); |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 2841 | return false; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2842 | } |
| 2843 | } |
| 2844 | |
| 2845 | if (token->type == CPP_EOF) |
Simon Martin | 126e073 | 2007-05-23 20:58:34 +0000 | [diff] [blame] | 2846 | { |
| 2847 | /* Paste operator constraint 6.10.3.3.1: |
| 2848 | Token-paste ##, can appear in both object-like and |
| 2849 | function-like macros, but not at the end. */ |
| 2850 | if (following_paste_op) |
| 2851 | { |
| 2852 | cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg); |
| 2853 | return false; |
| 2854 | } |
| 2855 | break; |
| 2856 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2857 | |
| 2858 | /* Paste operator constraint 6.10.3.3.1. */ |
| 2859 | if (token->type == CPP_PASTE) |
| 2860 | { |
| 2861 | /* Token-paste ##, can appear in both object-like and |
Simon Martin | 126e073 | 2007-05-23 20:58:34 +0000 | [diff] [blame] | 2862 | function-like macros, but not at the beginning. */ |
| 2863 | if (macro->count == 1) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2864 | { |
Simon Martin | 126e073 | 2007-05-23 20:58:34 +0000 | [diff] [blame] | 2865 | cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg); |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 2866 | return false; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2867 | } |
| 2868 | |
Joseph Myers | aa50850 | 2009-04-19 18:10:56 +0100 | [diff] [blame] | 2869 | if (token[-1].flags & PASTE_LEFT) |
| 2870 | { |
| 2871 | macro->extra_tokens = 1; |
| 2872 | num_extra_tokens++; |
Joseph Myers | 9a0c618 | 2009-05-10 15:27:32 +0100 | [diff] [blame] | 2873 | token->val.token_no = macro->count - 1; |
Joseph Myers | aa50850 | 2009-04-19 18:10:56 +0100 | [diff] [blame] | 2874 | } |
| 2875 | else |
| 2876 | { |
| 2877 | --macro->count; |
| 2878 | token[-1].flags |= PASTE_LEFT; |
| 2879 | if (token->flags & DIGRAPH) |
| 2880 | token[-1].flags |= SP_DIGRAPH; |
| 2881 | if (token->flags & PREV_WHITE) |
| 2882 | token[-1].flags |= SP_PREV_WHITE; |
| 2883 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2884 | } |
| 2885 | |
Simon Martin | 126e073 | 2007-05-23 20:58:34 +0000 | [diff] [blame] | 2886 | following_paste_op = (token->type == CPP_PASTE); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2887 | token = lex_expansion_token (pfile, macro); |
| 2888 | } |
| 2889 | |
Neil Booth | 601328b | 2002-05-16 05:53:24 +0000 | [diff] [blame] | 2890 | macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff); |
Geoffrey Keating | d804416 | 2004-06-09 20:10:13 +0000 | [diff] [blame] | 2891 | macro->traditional = 0; |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 2892 | |
Neil Booth | 4c2b647 | 2000-11-11 13:19:01 +0000 | [diff] [blame] | 2893 | /* Don't count the CPP_EOF. */ |
| 2894 | macro->count--; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2895 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 2896 | /* Clear whitespace on first token for warn_of_redefinition(). */ |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 2897 | if (macro->count) |
Neil Booth | 601328b | 2002-05-16 05:53:24 +0000 | [diff] [blame] | 2898 | macro->exp.tokens[0].flags &= ~PREV_WHITE; |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 2899 | |
Geoffrey Keating | d804416 | 2004-06-09 20:10:13 +0000 | [diff] [blame] | 2900 | /* Commit or allocate the memory. */ |
| 2901 | if (pfile->hash_table->alloc_subobject) |
| 2902 | { |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 2903 | cpp_token *tokns = |
| 2904 | (cpp_token *) pfile->hash_table->alloc_subobject (sizeof (cpp_token) |
| 2905 | * macro->count); |
Joseph Myers | aa50850 | 2009-04-19 18:10:56 +0100 | [diff] [blame] | 2906 | if (num_extra_tokens) |
| 2907 | { |
| 2908 | /* Place second and subsequent ## or %:%: tokens in |
| 2909 | sequences of consecutive such tokens at the end of the |
| 2910 | list to preserve information about where they appear, how |
| 2911 | they are spelt and whether they are preceded by |
| 2912 | whitespace without otherwise interfering with macro |
| 2913 | expansion. */ |
| 2914 | cpp_token *normal_dest = tokns; |
| 2915 | cpp_token *extra_dest = tokns + macro->count - num_extra_tokens; |
| 2916 | unsigned int i; |
| 2917 | for (i = 0; i < macro->count; i++) |
| 2918 | { |
| 2919 | if (macro->exp.tokens[i].type == CPP_PASTE) |
| 2920 | *extra_dest++ = macro->exp.tokens[i]; |
| 2921 | else |
| 2922 | *normal_dest++ = macro->exp.tokens[i]; |
| 2923 | } |
| 2924 | } |
| 2925 | else |
| 2926 | memcpy (tokns, macro->exp.tokens, sizeof (cpp_token) * macro->count); |
Geoffrey Keating | d804416 | 2004-06-09 20:10:13 +0000 | [diff] [blame] | 2927 | macro->exp.tokens = tokns; |
| 2928 | } |
| 2929 | else |
| 2930 | BUFF_FRONT (pfile->a_buff) = (uchar *) ¯o->exp.tokens[macro->count]; |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 2931 | |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 2932 | return true; |
| 2933 | } |
Neil Booth | 44ed91a | 2000-10-29 11:37:18 +0000 | [diff] [blame] | 2934 | |
Kazu Hirata | da7d830 | 2002-09-22 02:03:17 +0000 | [diff] [blame] | 2935 | /* Parse a macro and save its expansion. Returns nonzero on success. */ |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 2936 | bool |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2937 | _cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node) |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 2938 | { |
| 2939 | cpp_macro *macro; |
| 2940 | unsigned int i; |
| 2941 | bool ok; |
| 2942 | |
Geoffrey Keating | d804416 | 2004-06-09 20:10:13 +0000 | [diff] [blame] | 2943 | if (pfile->hash_table->alloc_subobject) |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 2944 | macro = (cpp_macro *) pfile->hash_table->alloc_subobject |
| 2945 | (sizeof (cpp_macro)); |
Geoffrey Keating | d804416 | 2004-06-09 20:10:13 +0000 | [diff] [blame] | 2946 | else |
| 2947 | macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro)); |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 2948 | macro->line = pfile->directive_line; |
| 2949 | macro->params = 0; |
| 2950 | macro->paramc = 0; |
| 2951 | macro->variadic = 0; |
Neil Booth | 23345bb | 2003-03-14 21:47:50 +0000 | [diff] [blame] | 2952 | macro->used = !CPP_OPTION (pfile, warn_unused_macros); |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 2953 | macro->count = 0; |
| 2954 | macro->fun_like = 0; |
Joseph Myers | aa50850 | 2009-04-19 18:10:56 +0100 | [diff] [blame] | 2955 | macro->extra_tokens = 0; |
Neil Booth | 7065e13 | 2001-02-14 07:38:20 +0000 | [diff] [blame] | 2956 | /* To suppress some diagnostics. */ |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 2957 | macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0; |
Neil Booth | 7065e13 | 2001-02-14 07:38:20 +0000 | [diff] [blame] | 2958 | |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 2959 | if (CPP_OPTION (pfile, traditional)) |
| 2960 | ok = _cpp_create_trad_definition (pfile, macro); |
| 2961 | else |
| 2962 | { |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 2963 | ok = create_iso_definition (pfile, macro); |
| 2964 | |
Tom Tromey | ee38036 | 2007-01-30 15:46:01 +0000 | [diff] [blame] | 2965 | /* We set the type for SEEN_EOL() in directives.c. |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 2966 | |
| 2967 | Longer term we should lex the whole line before coming here, |
| 2968 | and just copy the expansion. */ |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 2969 | |
| 2970 | /* Stop the lexer accepting __VA_ARGS__. */ |
| 2971 | pfile->state.va_args_ok = 0; |
| 2972 | } |
| 2973 | |
| 2974 | /* Clear the fast argument lookup indices. */ |
| 2975 | for (i = macro->paramc; i-- > 0; ) |
Zack Weinberg | 4977bab | 2002-12-16 18:23:00 +0000 | [diff] [blame] | 2976 | { |
| 2977 | struct cpp_hashnode *node = macro->params[i]; |
| 2978 | node->flags &= ~ NODE_MACRO_ARG; |
| 2979 | node->value = ((union _cpp_hashnode_value *) pfile->macro_buffer)[i]; |
| 2980 | } |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 2981 | |
| 2982 | if (!ok) |
| 2983 | return ok; |
| 2984 | |
Neil Booth | c2734e0 | 2002-07-26 16:29:31 +0000 | [diff] [blame] | 2985 | if (node->type == NT_MACRO) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2986 | { |
Neil Booth | a69cbaa | 2002-07-23 22:57:49 +0000 | [diff] [blame] | 2987 | if (CPP_OPTION (pfile, warn_unused_macros)) |
| 2988 | _cpp_warn_if_unused_macro (pfile, node, NULL); |
| 2989 | |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 2990 | if (warn_of_redefinition (pfile, node, macro)) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2991 | { |
Simon Baldwin | 87cf065 | 2010-04-07 17:18:10 +0000 | [diff] [blame] | 2992 | const int reason = (node->flags & NODE_BUILTIN) |
| 2993 | ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE; |
Joseph Myers | 148e421 | 2009-03-29 23:56:07 +0100 | [diff] [blame] | 2994 | bool warned; |
Simon Baldwin | 87cf065 | 2010-04-07 17:18:10 +0000 | [diff] [blame] | 2995 | |
| 2996 | warned = cpp_pedwarning_with_line (pfile, reason, |
| 2997 | pfile->directive_line, 0, |
| 2998 | "\"%s\" redefined", |
| 2999 | NODE_NAME (node)); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3000 | |
Joseph Myers | 148e421 | 2009-03-29 23:56:07 +0100 | [diff] [blame] | 3001 | if (warned && node->type == NT_MACRO && !(node->flags & NODE_BUILTIN)) |
| 3002 | cpp_error_with_line (pfile, CPP_DL_NOTE, |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 3003 | node->value.macro->line, 0, |
| 3004 | "this is the location of the previous definition"); |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 3005 | } |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 3006 | } |
| 3007 | |
Neil Booth | c2734e0 | 2002-07-26 16:29:31 +0000 | [diff] [blame] | 3008 | if (node->type != NT_VOID) |
| 3009 | _cpp_free_definition (node); |
| 3010 | |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 3011 | /* Enter definition in hash table. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3012 | node->type = NT_MACRO; |
| 3013 | node->value.macro = macro; |
Tom Tromey | 607f74e | 2007-11-30 18:24:01 +0000 | [diff] [blame] | 3014 | if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")) |
Tom Tromey | ec46053 | 2008-01-22 21:43:49 +0000 | [diff] [blame] | 3015 | && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_FORMAT_MACROS") |
| 3016 | /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned |
| 3017 | in the C standard, as something that one must use in C++. |
| 3018 | However DR#593 indicates that these aren't actually mentioned |
| 3019 | in the C++ standard. We special-case them anyway. */ |
| 3020 | && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_LIMIT_MACROS") |
| 3021 | && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_CONSTANT_MACROS")) |
Neil Booth | 618cdda | 2001-02-25 09:43:03 +0000 | [diff] [blame] | 3022 | node->flags |= NODE_WARN; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 3023 | |
Ben Elliston | 5950c3c | 2008-07-14 05:09:48 +0000 | [diff] [blame] | 3024 | /* If user defines one of the conditional macros, remove the |
| 3025 | conditional flag */ |
| 3026 | node->flags &= ~NODE_CONDITIONAL; |
| 3027 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3028 | return ok; |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 3029 | } |
| 3030 | |
Neil Booth | d15a58c | 2002-01-03 18:32:55 +0000 | [diff] [blame] | 3031 | /* Warn if a token in STRING matches one of a function-like MACRO's |
| 3032 | parameters. */ |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 3033 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 3034 | check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro, |
| 3035 | const cpp_string *string) |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 3036 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3037 | unsigned int i, len; |
Neil Booth | 6338b35 | 2003-04-23 22:44:06 +0000 | [diff] [blame] | 3038 | const uchar *p, *q, *limit; |
Kazu Hirata | df38348 | 2002-05-22 22:02:16 +0000 | [diff] [blame] | 3039 | |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 3040 | /* Loop over the string. */ |
Neil Booth | 6338b35 | 2003-04-23 22:44:06 +0000 | [diff] [blame] | 3041 | limit = string->text + string->len - 1; |
| 3042 | for (p = string->text + 1; p < limit; p = q) |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 3043 | { |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 3044 | /* Find the start of an identifier. */ |
Greg McGary | 61c16b1 | 2000-09-15 21:25:02 +0000 | [diff] [blame] | 3045 | while (p < limit && !is_idstart (*p)) |
| 3046 | p++; |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 3047 | |
| 3048 | /* Find the end of the identifier. */ |
| 3049 | q = p; |
Greg McGary | 61c16b1 | 2000-09-15 21:25:02 +0000 | [diff] [blame] | 3050 | while (q < limit && is_idchar (*q)) |
| 3051 | q++; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3052 | |
| 3053 | len = q - p; |
| 3054 | |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 3055 | /* Loop over the function macro arguments to see if the |
| 3056 | identifier inside the string matches one of them. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3057 | for (i = 0; i < macro->paramc; i++) |
| 3058 | { |
| 3059 | const cpp_hashnode *node = macro->params[i]; |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 3060 | |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 3061 | if (NODE_LEN (node) == len |
| 3062 | && !memcmp (p, NODE_NAME (node), len)) |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 3063 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 3064 | cpp_error (pfile, CPP_DL_WARNING, |
Zack Weinberg | f458d1d | 2002-02-27 18:48:07 +0000 | [diff] [blame] | 3065 | "macro argument \"%s\" would be stringified in traditional C", |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 3066 | NODE_NAME (node)); |
Kaveh R. Ghazi | e1aa514 | 2000-09-10 03:41:50 +0000 | [diff] [blame] | 3067 | break; |
| 3068 | } |
| 3069 | } |
| 3070 | } |
| 3071 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3072 | |
Neil Booth | 7096171 | 2001-06-23 11:34:41 +0000 | [diff] [blame] | 3073 | /* Returns the name, arguments and expansion of a macro, in a format |
| 3074 | suitable to be read back in again, and therefore also for DWARF 2 |
| 3075 | debugging info. e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION". |
| 3076 | Caller is expected to generate the "#define" bit if needed. The |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3077 | returned text is temporary, and automatically freed later. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3078 | const unsigned char * |
Jakub Jelinek | 8e680db | 2010-06-11 20:37:34 +0200 | [diff] [blame] | 3079 | cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3080 | { |
| 3081 | unsigned int i, len; |
Jakub Jelinek | 8e680db | 2010-06-11 20:37:34 +0200 | [diff] [blame] | 3082 | const cpp_macro *macro; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3083 | unsigned char *buffer; |
| 3084 | |
| 3085 | if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN)) |
| 3086 | { |
Jakub Jelinek | 8e680db | 2010-06-11 20:37:34 +0200 | [diff] [blame] | 3087 | if (node->type != NT_MACRO |
| 3088 | || !pfile->cb.user_builtin_macro |
| 3089 | || !pfile->cb.user_builtin_macro (pfile, node)) |
| 3090 | { |
| 3091 | cpp_error (pfile, CPP_DL_ICE, |
| 3092 | "invalid hash type %d in cpp_macro_definition", |
| 3093 | node->type); |
| 3094 | return 0; |
| 3095 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3096 | } |
| 3097 | |
Jakub Jelinek | 8e680db | 2010-06-11 20:37:34 +0200 | [diff] [blame] | 3098 | macro = node->value.macro; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3099 | /* Calculate length. */ |
Neil Booth | 8dc901d | 2002-05-29 19:30:07 +0000 | [diff] [blame] | 3100 | len = NODE_LEN (node) + 2; /* ' ' and NUL. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3101 | if (macro->fun_like) |
| 3102 | { |
Jim Blandy | 64d0826 | 2002-04-05 00:12:40 +0000 | [diff] [blame] | 3103 | len += 4; /* "()" plus possible final ".." of named |
| 3104 | varargs (we have + 1 below). */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3105 | for (i = 0; i < macro->paramc; i++) |
Jim Blandy | 64d0826 | 2002-04-05 00:12:40 +0000 | [diff] [blame] | 3106 | len += NODE_LEN (macro->params[i]) + 1; /* "," */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3107 | } |
| 3108 | |
Eric Christopher | 6da55c0 | 2005-02-15 23:18:04 +0000 | [diff] [blame] | 3109 | /* This should match below where we fill in the buffer. */ |
Neil Booth | 278c466 | 2002-06-19 05:40:08 +0000 | [diff] [blame] | 3110 | if (CPP_OPTION (pfile, traditional)) |
| 3111 | len += _cpp_replacement_text_len (macro); |
| 3112 | else |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3113 | { |
Joseph Myers | aa50850 | 2009-04-19 18:10:56 +0100 | [diff] [blame] | 3114 | unsigned int count = macro_real_token_count (macro); |
| 3115 | for (i = 0; i < count; i++) |
Neil Booth | 278c466 | 2002-06-19 05:40:08 +0000 | [diff] [blame] | 3116 | { |
| 3117 | cpp_token *token = ¯o->exp.tokens[i]; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3118 | |
Neil Booth | 278c466 | 2002-06-19 05:40:08 +0000 | [diff] [blame] | 3119 | if (token->type == CPP_MACRO_ARG) |
Joseph Myers | 9a0c618 | 2009-05-10 15:27:32 +0100 | [diff] [blame] | 3120 | len += NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1]); |
Neil Booth | 278c466 | 2002-06-19 05:40:08 +0000 | [diff] [blame] | 3121 | else |
Eric Christopher | 6da55c0 | 2005-02-15 23:18:04 +0000 | [diff] [blame] | 3122 | len += cpp_token_len (token); |
| 3123 | |
Neil Booth | 278c466 | 2002-06-19 05:40:08 +0000 | [diff] [blame] | 3124 | if (token->flags & STRINGIFY_ARG) |
| 3125 | len++; /* "#" */ |
| 3126 | if (token->flags & PASTE_LEFT) |
| 3127 | len += 3; /* " ##" */ |
Eric Christopher | 6da55c0 | 2005-02-15 23:18:04 +0000 | [diff] [blame] | 3128 | if (token->flags & PREV_WHITE) |
| 3129 | len++; /* " " */ |
Neil Booth | 278c466 | 2002-06-19 05:40:08 +0000 | [diff] [blame] | 3130 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3131 | } |
| 3132 | |
| 3133 | if (len > pfile->macro_buffer_len) |
Alexandre Oliva | 4b49c36 | 2001-01-09 09:30:43 +0000 | [diff] [blame] | 3134 | { |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 3135 | pfile->macro_buffer = XRESIZEVEC (unsigned char, |
| 3136 | pfile->macro_buffer, len); |
Alexandre Oliva | 4b49c36 | 2001-01-09 09:30:43 +0000 | [diff] [blame] | 3137 | pfile->macro_buffer_len = len; |
| 3138 | } |
Neil Booth | 7096171 | 2001-06-23 11:34:41 +0000 | [diff] [blame] | 3139 | |
| 3140 | /* Fill in the buffer. Start with the macro name. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3141 | buffer = pfile->macro_buffer; |
Neil Booth | 7096171 | 2001-06-23 11:34:41 +0000 | [diff] [blame] | 3142 | memcpy (buffer, NODE_NAME (node), NODE_LEN (node)); |
| 3143 | buffer += NODE_LEN (node); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3144 | |
| 3145 | /* Parameter names. */ |
| 3146 | if (macro->fun_like) |
| 3147 | { |
| 3148 | *buffer++ = '('; |
| 3149 | for (i = 0; i < macro->paramc; i++) |
| 3150 | { |
| 3151 | cpp_hashnode *param = macro->params[i]; |
| 3152 | |
| 3153 | if (param != pfile->spec_nodes.n__VA_ARGS__) |
| 3154 | { |
Neil Booth | a28c5035 | 2001-05-16 22:02:09 +0000 | [diff] [blame] | 3155 | memcpy (buffer, NODE_NAME (param), NODE_LEN (param)); |
| 3156 | buffer += NODE_LEN (param); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3157 | } |
| 3158 | |
| 3159 | if (i + 1 < macro->paramc) |
Kazu Hirata | df38348 | 2002-05-22 22:02:16 +0000 | [diff] [blame] | 3160 | /* Don't emit a space after the comma here; we're trying |
| 3161 | to emit a Dwarf-friendly definition, and the Dwarf spec |
| 3162 | forbids spaces in the argument list. */ |
Jim Blandy | 64d0826 | 2002-04-05 00:12:40 +0000 | [diff] [blame] | 3163 | *buffer++ = ','; |
Neil Booth | 28e0f04 | 2000-12-09 12:06:37 +0000 | [diff] [blame] | 3164 | else if (macro->variadic) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3165 | *buffer++ = '.', *buffer++ = '.', *buffer++ = '.'; |
| 3166 | } |
| 3167 | *buffer++ = ')'; |
| 3168 | } |
| 3169 | |
Jim Blandy | e37b38d | 2002-03-19 21:43:39 +0000 | [diff] [blame] | 3170 | /* The Dwarf spec requires a space after the macro name, even if the |
| 3171 | definition is the empty string. */ |
| 3172 | *buffer++ = ' '; |
| 3173 | |
Neil Booth | 278c466 | 2002-06-19 05:40:08 +0000 | [diff] [blame] | 3174 | if (CPP_OPTION (pfile, traditional)) |
| 3175 | buffer = _cpp_copy_replacement_text (macro, buffer); |
| 3176 | else if (macro->count) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3177 | /* Expansion tokens. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3178 | { |
Joseph Myers | aa50850 | 2009-04-19 18:10:56 +0100 | [diff] [blame] | 3179 | unsigned int count = macro_real_token_count (macro); |
| 3180 | for (i = 0; i < count; i++) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3181 | { |
Neil Booth | 601328b | 2002-05-16 05:53:24 +0000 | [diff] [blame] | 3182 | cpp_token *token = ¯o->exp.tokens[i]; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3183 | |
| 3184 | if (token->flags & PREV_WHITE) |
| 3185 | *buffer++ = ' '; |
| 3186 | if (token->flags & STRINGIFY_ARG) |
| 3187 | *buffer++ = '#'; |
| 3188 | |
| 3189 | if (token->type == CPP_MACRO_ARG) |
| 3190 | { |
Neil Booth | a28c5035 | 2001-05-16 22:02:09 +0000 | [diff] [blame] | 3191 | memcpy (buffer, |
Joseph Myers | 9a0c618 | 2009-05-10 15:27:32 +0100 | [diff] [blame] | 3192 | NODE_NAME (macro->params[token->val.macro_arg.arg_no - 1]), |
| 3193 | NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1])); |
| 3194 | buffer += NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1]); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3195 | } |
| 3196 | else |
Geoffrey Keating | 47e2049 | 2005-03-12 10:44:06 +0000 | [diff] [blame] | 3197 | buffer = cpp_spell_token (pfile, token, buffer, false); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 3198 | |
| 3199 | if (token->flags & PASTE_LEFT) |
| 3200 | { |
| 3201 | *buffer++ = ' '; |
| 3202 | *buffer++ = '#'; |
| 3203 | *buffer++ = '#'; |
| 3204 | /* Next has PREV_WHITE; see _cpp_create_definition. */ |
| 3205 | } |
| 3206 | } |
| 3207 | } |
| 3208 | |
| 3209 | *buffer = '\0'; |
| 3210 | return pfile->macro_buffer; |
| 3211 | } |