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