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