Neil Booth | a949941 | 2000-11-09 21:18:15 +0000 | [diff] [blame] | 1 | /* CPP Library. (Directive handling.) |
Jeff Law | 5e7b4e2 | 2000-02-25 22:59:31 -0700 | [diff] [blame] | 2 | Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, |
Tom Tromey | 705e2d2 | 2007-01-04 15:32:26 +0000 | [diff] [blame] | 3 | 1999, 2000, 2001, 2002, 2003, 2004, 2005, |
| 4 | 2007 Free Software Foundation, Inc. |
Richard Kenner | 4c8cc61 | 1997-02-16 08:08:25 -0500 | [diff] [blame] | 5 | Contributed by Per Bothner, 1994-95. |
Richard Kenner | d8bfa78 | 1997-01-03 08:19:34 -0500 | [diff] [blame] | 6 | Based on CCCP program by Paul Rubin, June 1986 |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 7 | Adapted to ANSI C, Richard Stallman, Jan 1987 |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify it |
| 10 | under the terms of the GNU General Public License as published by the |
| 11 | Free Software Foundation; either version 2, or (at your option) any |
| 12 | later version. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with this program; if not, write to the Free Software |
Kelley Cook | 200031d | 2005-06-29 02:34:39 +0000 | [diff] [blame] | 21 | Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 22 | |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 23 | #include "config.h" |
Kaveh R. Ghazi | b04cd507 | 1998-03-30 12:05:54 +0000 | [diff] [blame] | 24 | #include "system.h" |
Jeff Law | 956d695 | 1997-12-06 17:31:01 -0700 | [diff] [blame] | 25 | #include "cpplib.h" |
Paolo Bonzini | 4f4e53dd | 2004-05-24 10:50:45 +0000 | [diff] [blame] | 26 | #include "internal.h" |
Zack Weinberg | c6e8380 | 2004-06-05 20:58:06 +0000 | [diff] [blame] | 27 | #include "mkdeps.h" |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 28 | #include "obstack.h" |
Jeff Law | 956d695 | 1997-12-06 17:31:01 -0700 | [diff] [blame] | 29 | |
Zack Weinberg | 88ae23e | 2000-03-08 23:35:19 +0000 | [diff] [blame] | 30 | /* Stack of conditionals currently in progress |
| 31 | (including both successful and failing conditionals). */ |
Zack Weinberg | 88ae23e | 2000-03-08 23:35:19 +0000 | [diff] [blame] | 32 | struct if_stack |
| 33 | { |
| 34 | struct if_stack *next; |
Neil Booth | 5041042 | 2001-09-15 10:18:03 +0000 | [diff] [blame] | 35 | unsigned int line; /* Line where condition started. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 36 | const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */ |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 37 | bool skip_elses; /* Can future #else / #elif be skipped? */ |
| 38 | bool was_skipping; /* If were skipping on entry. */ |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 39 | int type; /* Most recent conditional for diagnostics. */ |
Zack Weinberg | 88ae23e | 2000-03-08 23:35:19 +0000 | [diff] [blame] | 40 | }; |
Zack Weinberg | 88ae23e | 2000-03-08 23:35:19 +0000 | [diff] [blame] | 41 | |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 42 | /* Contains a registered pragma or pragma namespace. */ |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 43 | typedef void (*pragma_cb) (cpp_reader *); |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 44 | struct pragma_entry |
| 45 | { |
| 46 | struct pragma_entry *next; |
Neil Booth | 4b115ff | 2001-10-14 23:04:13 +0000 | [diff] [blame] | 47 | const cpp_hashnode *pragma; /* Name and length. */ |
Zack Weinberg | 21b1149 | 2004-09-09 19:16:56 +0000 | [diff] [blame] | 48 | bool is_nspace; |
| 49 | bool is_internal; |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 50 | bool is_deferred; |
| 51 | bool allow_expansion; |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 52 | union { |
| 53 | pragma_cb handler; |
| 54 | struct pragma_entry *space; |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 55 | unsigned int ident; |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 56 | } u; |
| 57 | }; |
| 58 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 59 | /* Values for the origin field of struct directive. KANDR directives |
| 60 | come from traditional (K&R) C. STDC89 directives come from the |
| 61 | 1989 C standard. EXTENSION directives are extensions. */ |
| 62 | #define KANDR 0 |
| 63 | #define STDC89 1 |
| 64 | #define EXTENSION 2 |
| 65 | |
| 66 | /* Values for the flags field of struct directive. COND indicates a |
| 67 | conditional; IF_COND an opening conditional. INCL means to treat |
| 68 | "..." and <...> as q-char and h-char sequences respectively. IN_I |
| 69 | means this directive should be handled even if -fpreprocessed is in |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 70 | effect (these are the directives with callback hooks). |
| 71 | |
Neil Booth | d97371e | 2002-06-18 06:27:40 +0000 | [diff] [blame] | 72 | EXPAND is set on directives that are always macro-expanded. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 73 | #define COND (1 << 0) |
| 74 | #define IF_COND (1 << 1) |
| 75 | #define INCL (1 << 2) |
| 76 | #define IN_I (1 << 3) |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 77 | #define EXPAND (1 << 4) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 78 | |
| 79 | /* Defines one #-directive, including how to handle it. */ |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 80 | typedef void (*directive_handler) (cpp_reader *); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 81 | typedef struct directive directive; |
| 82 | struct directive |
| 83 | { |
| 84 | directive_handler handler; /* Function to handle directive. */ |
Neil Booth | 562a5c2 | 2002-04-21 18:46:42 +0000 | [diff] [blame] | 85 | const uchar *name; /* Name of directive. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 86 | unsigned short length; /* Length of name. */ |
| 87 | unsigned char origin; /* Origin of directive. */ |
| 88 | unsigned char flags; /* Flags describing this directive. */ |
| 89 | }; |
| 90 | |
Zack Weinberg | 1316f1f | 2000-02-06 07:53:50 +0000 | [diff] [blame] | 91 | /* Forward declarations. */ |
| 92 | |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 93 | static void skip_rest_of_line (cpp_reader *); |
| 94 | static void check_eol (cpp_reader *); |
| 95 | static void start_directive (cpp_reader *); |
| 96 | static void prepare_directive_trad (cpp_reader *); |
| 97 | static void end_directive (cpp_reader *, int); |
| 98 | static void directive_diagnostics (cpp_reader *, const directive *, int); |
| 99 | static void run_directive (cpp_reader *, int, const char *, size_t); |
| 100 | static char *glue_header_name (cpp_reader *); |
Ian Lance Taylor | cbc43ae | 2005-10-04 18:06:19 +0000 | [diff] [blame] | 101 | static const char *parse_include (cpp_reader *, int *, const cpp_token ***); |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 102 | static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *); |
| 103 | static unsigned int read_flag (cpp_reader *, unsigned int); |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 104 | static int strtoul_for_line (const uchar *, unsigned int, unsigned long *); |
| 105 | static void do_diagnostic (cpp_reader *, int, int); |
Tom Tromey | ee1c2a1 | 2007-01-12 19:46:49 +0000 | [diff] [blame] | 106 | static cpp_hashnode *lex_macro_node (cpp_reader *, bool); |
Geoffrey Keating | d1bd0de | 2003-07-11 08:33:21 +0000 | [diff] [blame] | 107 | static int undefine_macros (cpp_reader *, cpp_hashnode *, void *); |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 108 | static void do_include_common (cpp_reader *, enum include_type); |
| 109 | static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *, |
| 110 | const cpp_hashnode *); |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 111 | static int count_registered_pragmas (struct pragma_entry *); |
| 112 | static char ** save_registered_pragmas (struct pragma_entry *, char **); |
| 113 | static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *, |
| 114 | char **); |
| 115 | static void do_pragma_once (cpp_reader *); |
| 116 | static void do_pragma_poison (cpp_reader *); |
| 117 | static void do_pragma_system_header (cpp_reader *); |
| 118 | static void do_pragma_dependency (cpp_reader *); |
| 119 | static void do_linemarker (cpp_reader *); |
| 120 | static const cpp_token *get_token_no_padding (cpp_reader *); |
| 121 | static const cpp_token *get__Pragma_string (cpp_reader *); |
| 122 | static void destringize_and_run (cpp_reader *, const cpp_string *); |
| 123 | static int parse_answer (cpp_reader *, struct answer **, int); |
| 124 | static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int); |
| 125 | static struct answer ** find_answer (cpp_hashnode *, const struct answer *); |
| 126 | static void handle_assertion (cpp_reader *, const char *, int); |
Kaveh R. Ghazi | 487a6e0 | 1998-05-19 08:42:48 +0000 | [diff] [blame] | 127 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 128 | /* This is the table of directive handlers. It is ordered by |
| 129 | frequency of occurrence; the numbers at the end are directive |
| 130 | counts from all the source code I have lying around (egcs and libc |
| 131 | CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 132 | pcmcia-cs-3.0.9). This is no longer important as directive lookup |
| 133 | is now O(1). All extensions other than #warning and #include_next |
| 134 | are deprecated. The name is where the extension appears to have |
| 135 | come from. */ |
Jeff Law | e9a25f7 | 1997-11-02 14:19:36 -0700 | [diff] [blame] | 136 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 137 | #define DIRECTIVE_TABLE \ |
| 138 | D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \ |
Neil Booth | d97371e | 2002-06-18 06:27:40 +0000 | [diff] [blame] | 139 | D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 140 | D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \ |
| 141 | D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \ |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 142 | D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 143 | D(else, T_ELSE, KANDR, COND) /* 9863 */ \ |
| 144 | D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \ |
| 145 | D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \ |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 146 | D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \ |
| 147 | D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 148 | D(error, T_ERROR, STDC89, 0) /* 475 */ \ |
| 149 | D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \ |
| 150 | D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \ |
Neil Booth | d97371e | 2002-06-18 06:27:40 +0000 | [diff] [blame] | 151 | D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 152 | D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \ |
Neil Booth | d97371e | 2002-06-18 06:27:40 +0000 | [diff] [blame] | 153 | D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 154 | D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \ |
| 155 | D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \ |
Zack Weinberg | 1ed17cd | 2005-05-12 18:31:38 +0000 | [diff] [blame] | 156 | D(sccs, T_SCCS, EXTENSION, IN_I) /* 0 SVR4? */ |
| 157 | |
| 158 | /* #sccs is synonymous with #ident. */ |
| 159 | #define do_sccs do_ident |
Zack Weinberg | 07aa0b0 | 2000-04-01 22:55:25 +0000 | [diff] [blame] | 160 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 161 | /* Use the table to generate a series of prototypes, an enum for the |
| 162 | directive names, and an array of directive handlers. */ |
| 163 | |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 164 | #define D(name, t, o, f) static void do_##name (cpp_reader *); |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 165 | DIRECTIVE_TABLE |
| 166 | #undef D |
| 167 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 168 | #define D(n, tag, o, f) tag, |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 169 | enum |
| 170 | { |
| 171 | DIRECTIVE_TABLE |
| 172 | N_DIRECTIVES |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 173 | }; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 174 | #undef D |
| 175 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 176 | #define D(name, t, origin, flags) \ |
Kaveh R. Ghazi | 9a23858 | 2003-06-16 19:14:22 +0000 | [diff] [blame] | 177 | { do_##name, (const uchar *) #name, \ |
| 178 | sizeof #name - 1, origin, flags }, |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 179 | static const directive dtable[] = |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 180 | { |
| 181 | DIRECTIVE_TABLE |
| 182 | }; |
| 183 | #undef D |
| 184 | #undef DIRECTIVE_TABLE |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 185 | |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 186 | /* Wrapper struct directive for linemarkers. |
| 187 | The origin is more or less true - the original K+R cpp |
| 188 | did use this notation in its preprocessed output. */ |
| 189 | static const directive linemarker_dir = |
| 190 | { |
| 191 | do_linemarker, U"#", 1, KANDR, IN_I |
| 192 | }; |
| 193 | |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 194 | #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF) |
Neil Booth | 67821e3 | 2001-08-05 17:31:25 +0000 | [diff] [blame] | 195 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 196 | /* Skip any remaining tokens in a directive. */ |
| 197 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 198 | skip_rest_of_line (cpp_reader *pfile) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 199 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 200 | /* Discard all stacked contexts. */ |
Neil Booth | 8128ccc | 2002-11-18 20:43:40 +0000 | [diff] [blame] | 201 | while (pfile->context->prev) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 202 | _cpp_pop_context (pfile); |
| 203 | |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 204 | /* Sweep up all tokens remaining on the line. */ |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 205 | if (! SEEN_EOL ()) |
| 206 | while (_cpp_lex_token (pfile)->type != CPP_EOF) |
| 207 | ; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /* Ensure there are no stray tokens at the end of a directive. */ |
| 211 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 212 | check_eol (cpp_reader *pfile) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 213 | { |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 214 | if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 215 | cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive", |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 216 | pfile->directive->name); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Ian Lance Taylor | cbc43ae | 2005-10-04 18:06:19 +0000 | [diff] [blame] | 219 | /* Ensure there are no stray tokens other than comments at the end of |
| 220 | a directive, and gather the comments. */ |
| 221 | static const cpp_token ** |
| 222 | check_eol_return_comments (cpp_reader *pfile) |
| 223 | { |
| 224 | size_t c; |
| 225 | size_t capacity = 8; |
| 226 | const cpp_token **buf; |
| 227 | |
| 228 | buf = XNEWVEC (const cpp_token *, capacity); |
| 229 | c = 0; |
| 230 | if (! SEEN_EOL ()) |
| 231 | { |
| 232 | while (1) |
| 233 | { |
| 234 | const cpp_token *tok; |
| 235 | |
| 236 | tok = _cpp_lex_token (pfile); |
| 237 | if (tok->type == CPP_EOF) |
| 238 | break; |
| 239 | if (tok->type != CPP_COMMENT) |
| 240 | cpp_error (pfile, CPP_DL_PEDWARN, |
| 241 | "extra tokens at end of #%s directive", |
| 242 | pfile->directive->name); |
| 243 | else |
| 244 | { |
| 245 | if (c + 1 >= capacity) |
| 246 | { |
| 247 | capacity *= 2; |
| 248 | buf = XRESIZEVEC (const cpp_token *, buf, capacity); |
| 249 | } |
| 250 | buf[c] = tok; |
| 251 | ++c; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | buf[c] = NULL; |
| 256 | return buf; |
| 257 | } |
| 258 | |
Neil Booth | fe6c2db | 2000-11-15 19:25:22 +0000 | [diff] [blame] | 259 | /* Called when entering a directive, _Pragma or command-line directive. */ |
| 260 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 261 | start_directive (cpp_reader *pfile) |
Zack Weinberg | c5a0473 | 2000-04-25 19:32:36 +0000 | [diff] [blame] | 262 | { |
Neil Booth | 7f2f1a6 | 2000-11-14 18:32:06 +0000 | [diff] [blame] | 263 | /* Setup in-directive state. */ |
| 264 | pfile->state.in_directive = 1; |
| 265 | pfile->state.save_comments = 0; |
Zack Weinberg | 21b1149 | 2004-09-09 19:16:56 +0000 | [diff] [blame] | 266 | pfile->directive_result.type = CPP_PADDING; |
Neil Booth | 7f2f1a6 | 2000-11-14 18:32:06 +0000 | [diff] [blame] | 267 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 268 | /* Some handlers need the position of the # for diagnostics. */ |
Per Bothner | 500bee0 | 2004-04-22 19:22:27 -0700 | [diff] [blame] | 269 | pfile->directive_line = pfile->line_table->highest_line; |
Neil Booth | fe6c2db | 2000-11-15 19:25:22 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /* Called when leaving a directive, _Pragma or command-line directive. */ |
| 273 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 274 | end_directive (cpp_reader *pfile, int skip_line) |
Neil Booth | fe6c2db | 2000-11-15 19:25:22 +0000 | [diff] [blame] | 275 | { |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 276 | if (pfile->state.in_deferred_pragma) |
| 277 | ; |
| 278 | else if (CPP_OPTION (pfile, traditional)) |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 279 | { |
Neil Booth | d97371e | 2002-06-18 06:27:40 +0000 | [diff] [blame] | 280 | /* Revert change of prepare_directive_trad. */ |
| 281 | pfile->state.prevent_expansion--; |
| 282 | |
Neil Booth | b66377c | 2002-06-13 21:16:00 +0000 | [diff] [blame] | 283 | if (pfile->directive != &dtable[T_DEFINE]) |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 284 | _cpp_remove_overlay (pfile); |
| 285 | } |
Neil Booth | fe6c2db | 2000-11-15 19:25:22 +0000 | [diff] [blame] | 286 | /* We don't skip for an assembler #. */ |
Neil Booth | b66377c | 2002-06-13 21:16:00 +0000 | [diff] [blame] | 287 | else if (skip_line) |
Neil Booth | 67821e3 | 2001-08-05 17:31:25 +0000 | [diff] [blame] | 288 | { |
| 289 | skip_rest_of_line (pfile); |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 290 | if (!pfile->keep_tokens) |
| 291 | { |
| 292 | pfile->cur_run = &pfile->base_run; |
| 293 | pfile->cur_token = pfile->base_run.base; |
| 294 | } |
Neil Booth | 67821e3 | 2001-08-05 17:31:25 +0000 | [diff] [blame] | 295 | } |
Neil Booth | fe6c2db | 2000-11-15 19:25:22 +0000 | [diff] [blame] | 296 | |
| 297 | /* Restore state. */ |
Neil Booth | fe6c2db | 2000-11-15 19:25:22 +0000 | [diff] [blame] | 298 | pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments); |
| 299 | pfile->state.in_directive = 0; |
Neil Booth | d97371e | 2002-06-18 06:27:40 +0000 | [diff] [blame] | 300 | pfile->state.in_expression = 0; |
Neil Booth | fe6c2db | 2000-11-15 19:25:22 +0000 | [diff] [blame] | 301 | pfile->state.angled_headers = 0; |
| 302 | pfile->directive = 0; |
| 303 | } |
| 304 | |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 305 | /* Prepare to handle the directive in pfile->directive. */ |
| 306 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 307 | prepare_directive_trad (cpp_reader *pfile) |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 308 | { |
Neil Booth | 951a076 | 2002-06-27 06:01:58 +0000 | [diff] [blame] | 309 | if (pfile->directive != &dtable[T_DEFINE]) |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 310 | { |
Neil Booth | b66377c | 2002-06-13 21:16:00 +0000 | [diff] [blame] | 311 | bool no_expand = (pfile->directive |
| 312 | && ! (pfile->directive->flags & EXPAND)); |
Neil Booth | 974c43f | 2002-06-13 06:25:28 +0000 | [diff] [blame] | 313 | bool was_skipping = pfile->state.skipping; |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 314 | |
Neil Booth | d97371e | 2002-06-18 06:27:40 +0000 | [diff] [blame] | 315 | pfile->state.in_expression = (pfile->directive == &dtable[T_IF] |
| 316 | || pfile->directive == &dtable[T_ELIF]); |
Neil Booth | 45f2492 | 2003-12-12 07:00:29 +0000 | [diff] [blame] | 317 | if (pfile->state.in_expression) |
| 318 | pfile->state.skipping = false; |
| 319 | |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 320 | if (no_expand) |
| 321 | pfile->state.prevent_expansion++; |
Zack Weinberg | 4383964 | 2003-07-13 17:34:18 +0000 | [diff] [blame] | 322 | _cpp_scan_out_logical_line (pfile, NULL); |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 323 | if (no_expand) |
| 324 | pfile->state.prevent_expansion--; |
Neil Booth | 45f2492 | 2003-12-12 07:00:29 +0000 | [diff] [blame] | 325 | |
Neil Booth | 974c43f | 2002-06-13 06:25:28 +0000 | [diff] [blame] | 326 | pfile->state.skipping = was_skipping; |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 327 | _cpp_overlay_buffer (pfile, pfile->out.base, |
| 328 | pfile->out.cur - pfile->out.base); |
| 329 | } |
Neil Booth | d97371e | 2002-06-18 06:27:40 +0000 | [diff] [blame] | 330 | |
| 331 | /* Stop ISO C from expanding anything. */ |
| 332 | pfile->state.prevent_expansion++; |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Kazu Hirata | da7d830 | 2002-09-22 02:03:17 +0000 | [diff] [blame] | 335 | /* Output diagnostics for a directive DIR. INDENTED is nonzero if |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 336 | the '#' was indented. */ |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 337 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 338 | directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented) |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 339 | { |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 340 | /* Issue -pedantic warnings for extensions. */ |
| 341 | if (CPP_PEDANTIC (pfile) |
| 342 | && ! pfile->state.skipping |
| 343 | && dir->origin == EXTENSION) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 344 | cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name); |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 345 | |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 346 | /* Traditionally, a directive is ignored unless its # is in |
| 347 | column 1. Therefore in code intended to work with K+R |
| 348 | compilers, directives added by C89 must have their # |
| 349 | indented, and directives present in traditional C must not. |
| 350 | This is true even of directives in skipped conditional |
| 351 | blocks. #elif cannot be used at all. */ |
| 352 | if (CPP_WTRADITIONAL (pfile)) |
| 353 | { |
| 354 | if (dir == &dtable[T_ELIF]) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 355 | cpp_error (pfile, CPP_DL_WARNING, |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 356 | "suggest not using #elif in traditional C"); |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 357 | else if (indented && dir->origin == KANDR) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 358 | cpp_error (pfile, CPP_DL_WARNING, |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 359 | "traditional C ignores #%s with the # indented", |
| 360 | dir->name); |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 361 | else if (!indented && dir->origin != KANDR) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 362 | cpp_error (pfile, CPP_DL_WARNING, |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 363 | "suggest hiding #%s from traditional C with an indented #", |
| 364 | dir->name); |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | |
Kazu Hirata | da7d830 | 2002-09-22 02:03:17 +0000 | [diff] [blame] | 368 | /* Check if we have a known directive. INDENTED is nonzero if the |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 369 | '#' of the directive was indented. This function is in this file |
Gabriel Dos Reis | a2566ae | 2005-01-02 01:32:21 +0000 | [diff] [blame] | 370 | to save unnecessarily exporting dtable etc. to lex.c. Returns |
Kazu Hirata | da7d830 | 2002-09-22 02:03:17 +0000 | [diff] [blame] | 371 | nonzero if the line of tokens has been handled, zero if we should |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 372 | continue processing the line. */ |
Neil Booth | fe6c2db | 2000-11-15 19:25:22 +0000 | [diff] [blame] | 373 | int |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 374 | _cpp_handle_directive (cpp_reader *pfile, int indented) |
Neil Booth | fe6c2db | 2000-11-15 19:25:22 +0000 | [diff] [blame] | 375 | { |
Neil Booth | fe6c2db | 2000-11-15 19:25:22 +0000 | [diff] [blame] | 376 | const directive *dir = 0; |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 377 | const cpp_token *dname; |
Neil Booth | e808ec9 | 2002-02-27 07:24:53 +0000 | [diff] [blame] | 378 | bool was_parsing_args = pfile->state.parsing_args; |
Zack Weinberg | c6e8380 | 2004-06-05 20:58:06 +0000 | [diff] [blame] | 379 | bool was_discarding_output = pfile->state.discarding_output; |
Neil Booth | fe6c2db | 2000-11-15 19:25:22 +0000 | [diff] [blame] | 380 | int skip = 1; |
| 381 | |
Zack Weinberg | c6e8380 | 2004-06-05 20:58:06 +0000 | [diff] [blame] | 382 | if (was_discarding_output) |
| 383 | pfile->state.prevent_expansion = 0; |
| 384 | |
Neil Booth | e808ec9 | 2002-02-27 07:24:53 +0000 | [diff] [blame] | 385 | if (was_parsing_args) |
| 386 | { |
| 387 | if (CPP_OPTION (pfile, pedantic)) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 388 | cpp_error (pfile, CPP_DL_PEDWARN, |
Neil Booth | e808ec9 | 2002-02-27 07:24:53 +0000 | [diff] [blame] | 389 | "embedding a directive within macro arguments is not portable"); |
| 390 | pfile->state.parsing_args = 0; |
| 391 | pfile->state.prevent_expansion = 0; |
| 392 | } |
Neil Booth | fe6c2db | 2000-11-15 19:25:22 +0000 | [diff] [blame] | 393 | start_directive (pfile); |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 394 | dname = _cpp_lex_token (pfile); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 395 | |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 396 | if (dname->type == CPP_NAME) |
Neil Booth | 0d9f234 | 2000-09-18 18:43:05 +0000 | [diff] [blame] | 397 | { |
Zack Weinberg | 4977bab | 2002-12-16 18:23:00 +0000 | [diff] [blame] | 398 | if (dname->val.node->is_directive) |
| 399 | dir = &dtable[dname->val.node->directive_index]; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 400 | } |
Kazu Hirata | 05713b8 | 2002-09-15 18:24:08 +0000 | [diff] [blame] | 401 | /* We do not recognize the # followed by a number extension in |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 402 | assembler code. */ |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 403 | else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 404 | { |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 405 | dir = &linemarker_dir; |
| 406 | if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed) |
| 407 | && ! pfile->state.skipping) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 408 | cpp_error (pfile, CPP_DL_PEDWARN, |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 409 | "style of line directive is a GCC extension"); |
Neil Booth | 0d9f234 | 2000-09-18 18:43:05 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 412 | if (dir) |
Neil Booth | 0d9f234 | 2000-09-18 18:43:05 +0000 | [diff] [blame] | 413 | { |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 414 | /* If we have a directive that is not an opening conditional, |
| 415 | invalidate any control macro. */ |
| 416 | if (! (dir->flags & IF_COND)) |
| 417 | pfile->mi_valid = false; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 418 | |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 419 | /* Kluge alert. In order to be sure that code like this |
| 420 | |
| 421 | #define HASH # |
| 422 | HASH define foo bar |
| 423 | |
| 424 | does not cause '#define foo bar' to get executed when |
| 425 | compiled with -save-temps, we recognize directives in |
Gabriel Dos Reis | a2566ae | 2005-01-02 01:32:21 +0000 | [diff] [blame] | 426 | -fpreprocessed mode only if the # is in column 1. macro.c |
Ollie Wild | ccfc4c9 | 2007-07-30 18:29:20 +0000 | [diff] [blame^] | 427 | puts a space in front of any '#' at the start of a macro. |
| 428 | |
| 429 | We exclude the -fdirectives-only case because macro expansion |
| 430 | has not been performed yet, and block comments can cause spaces |
| 431 | to preceed the directive. */ |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 432 | if (CPP_OPTION (pfile, preprocessed) |
Ollie Wild | ccfc4c9 | 2007-07-30 18:29:20 +0000 | [diff] [blame^] | 433 | && !CPP_OPTION (pfile, directives_only) |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 434 | && (indented || !(dir->flags & IN_I))) |
Zack Weinberg | 6d4587f | 2001-05-10 00:07:23 +0000 | [diff] [blame] | 435 | { |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 436 | skip = 0; |
| 437 | dir = 0; |
Zack Weinberg | 6d4587f | 2001-05-10 00:07:23 +0000 | [diff] [blame] | 438 | } |
| 439 | else |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 440 | { |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 441 | /* In failed conditional groups, all non-conditional |
| 442 | directives are ignored. Before doing that, whether |
| 443 | skipping or not, we should lex angle-bracketed headers |
| 444 | correctly, and maybe output some diagnostics. */ |
| 445 | pfile->state.angled_headers = dir->flags & INCL; |
Zack Weinberg | a8d0dda | 2003-02-21 18:06:30 +0000 | [diff] [blame] | 446 | pfile->state.directive_wants_padding = dir->flags & INCL; |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 447 | if (! CPP_OPTION (pfile, preprocessed)) |
| 448 | directive_diagnostics (pfile, dir, indented); |
| 449 | if (pfile->state.skipping && !(dir->flags & COND)) |
| 450 | dir = 0; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 451 | } |
| 452 | } |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 453 | else if (dname->type == CPP_EOF) |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 454 | ; /* CPP_EOF is the "null directive". */ |
| 455 | else |
Neil Booth | 0d9f234 | 2000-09-18 18:43:05 +0000 | [diff] [blame] | 456 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 457 | /* An unknown directive. Don't complain about it in assembly |
| 458 | source: we don't know where the comments are, and # may |
| 459 | introduce assembler pseudo-ops. Don't complain about invalid |
| 460 | directives in skipped conditional groups (6.10 p4). */ |
Neil Booth | bdb05a7 | 2000-11-26 17:31:13 +0000 | [diff] [blame] | 461 | if (CPP_OPTION (pfile, lang) == CLK_ASM) |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 462 | skip = 0; |
| 463 | else if (!pfile->state.skipping) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 464 | cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s", |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 465 | cpp_token_as_text (pfile, dname)); |
Neil Booth | 0d9f234 | 2000-09-18 18:43:05 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Neil Booth | d1a5868 | 2002-06-28 06:26:54 +0000 | [diff] [blame] | 468 | pfile->directive = dir; |
| 469 | if (CPP_OPTION (pfile, traditional)) |
| 470 | prepare_directive_trad (pfile); |
| 471 | |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 472 | if (dir) |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 473 | pfile->directive->handler (pfile); |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 474 | else if (skip == 0) |
| 475 | _cpp_backup_tokens (pfile, 1); |
| 476 | |
| 477 | end_directive (pfile, skip); |
Neil Booth | e808ec9 | 2002-02-27 07:24:53 +0000 | [diff] [blame] | 478 | if (was_parsing_args) |
| 479 | { |
| 480 | /* Restore state when within macro args. */ |
| 481 | pfile->state.parsing_args = 2; |
| 482 | pfile->state.prevent_expansion = 1; |
Neil Booth | e808ec9 | 2002-02-27 07:24:53 +0000 | [diff] [blame] | 483 | } |
Zack Weinberg | c6e8380 | 2004-06-05 20:58:06 +0000 | [diff] [blame] | 484 | if (was_discarding_output) |
| 485 | pfile->state.prevent_expansion = 1; |
Neil Booth | fe6c2db | 2000-11-15 19:25:22 +0000 | [diff] [blame] | 486 | return skip; |
Zack Weinberg | c5a0473 | 2000-04-25 19:32:36 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 489 | /* Directive handler wrapper used by the command line option |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 490 | processor. BUF is \n terminated. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 491 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 492 | run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count) |
Zack Weinberg | 3fdc651 | 1999-03-16 13:10:15 +0000 | [diff] [blame] | 493 | { |
Neil Booth | 562a5c2 | 2002-04-21 18:46:42 +0000 | [diff] [blame] | 494 | cpp_push_buffer (pfile, (const uchar *) buf, count, |
Per Bothner | 40de9f7 | 2003-10-02 07:30:34 +0000 | [diff] [blame] | 495 | /* from_stage3 */ true); |
Neil Booth | 0bda476 | 2000-12-11 07:45:16 +0000 | [diff] [blame] | 496 | start_directive (pfile); |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 497 | |
| 498 | /* This is a short-term fix to prevent a leading '#' being |
| 499 | interpreted as a directive. */ |
| 500 | _cpp_clean_line (pfile); |
| 501 | |
Neil Booth | f71aebb | 2001-05-27 18:06:00 +0000 | [diff] [blame] | 502 | pfile->directive = &dtable[dir_no]; |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 503 | if (CPP_OPTION (pfile, traditional)) |
| 504 | prepare_directive_trad (pfile); |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 505 | pfile->directive->handler (pfile); |
Neil Booth | 0bda476 | 2000-12-11 07:45:16 +0000 | [diff] [blame] | 506 | end_directive (pfile, 1); |
Neil Booth | ef6e958 | 2001-08-04 12:01:59 +0000 | [diff] [blame] | 507 | _cpp_pop_buffer (pfile); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 508 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 509 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 510 | /* Checks for validity the macro name in #define, #undef, #ifdef and |
Tom Tromey | ee1c2a1 | 2007-01-12 19:46:49 +0000 | [diff] [blame] | 511 | #ifndef directives. IS_DEF_OR_UNDEF is true if this call is |
| 512 | processing a #define or #undefine directive, and false |
| 513 | otherwise. */ |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 514 | static cpp_hashnode * |
Tom Tromey | ee1c2a1 | 2007-01-12 19:46:49 +0000 | [diff] [blame] | 515 | lex_macro_node (cpp_reader *pfile, bool is_def_or_undef) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 516 | { |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 517 | const cpp_token *token = _cpp_lex_token (pfile); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 518 | |
Zack Weinberg | 92936ec | 2000-07-19 20:18:08 +0000 | [diff] [blame] | 519 | /* The token immediately after #define must be an identifier. That |
Zack Weinberg | b8363a2 | 2001-07-01 18:48:13 +0000 | [diff] [blame] | 520 | identifier may not be "defined", per C99 6.10.8p4. |
| 521 | In C++, it may not be any of the "named operators" either, |
| 522 | per C++98 [lex.digraph], [lex.key]. |
| 523 | Finally, the identifier may not have been poisoned. (In that case |
Neil Booth | 1d63a28 | 2002-06-28 20:27:14 +0000 | [diff] [blame] | 524 | the lexer has issued the error message for us.) */ |
Zack Weinberg | b8363a2 | 2001-07-01 18:48:13 +0000 | [diff] [blame] | 525 | |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 526 | if (token->type == CPP_NAME) |
| 527 | { |
| 528 | cpp_hashnode *node = token->val.node; |
| 529 | |
Tom Tromey | ee1c2a1 | 2007-01-12 19:46:49 +0000 | [diff] [blame] | 530 | if (is_def_or_undef && node == pfile->spec_nodes.n_defined) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 531 | cpp_error (pfile, CPP_DL_ERROR, |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 532 | "\"defined\" cannot be used as a macro name"); |
| 533 | else if (! (node->flags & NODE_POISONED)) |
| 534 | return node; |
| 535 | } |
| 536 | else if (token->flags & NAMED_OP) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 537 | cpp_error (pfile, CPP_DL_ERROR, |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 538 | "\"%s\" cannot be used as a macro name as it is an operator in C++", |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 539 | NODE_NAME (token->val.node)); |
| 540 | else if (token->type == CPP_EOF) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 541 | cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive", |
Neil Booth | 1a76916 | 2002-06-11 05:36:17 +0000 | [diff] [blame] | 542 | pfile->directive->name); |
| 543 | else |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 544 | cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers"); |
Zack Weinberg | b8363a2 | 2001-07-01 18:48:13 +0000 | [diff] [blame] | 545 | |
Neil Booth | cbc69f8 | 2002-06-05 20:27:12 +0000 | [diff] [blame] | 546 | return NULL; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 547 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 548 | |
Gabriel Dos Reis | a2566ae | 2005-01-02 01:32:21 +0000 | [diff] [blame] | 549 | /* Process a #define directive. Most work is done in macro.c. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 550 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 551 | do_define (cpp_reader *pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 552 | { |
Tom Tromey | ee1c2a1 | 2007-01-12 19:46:49 +0000 | [diff] [blame] | 553 | cpp_hashnode *node = lex_macro_node (pfile, true); |
Zack Weinberg | ff2b53e | 2000-04-06 07:56:14 +0000 | [diff] [blame] | 554 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 555 | if (node) |
| 556 | { |
Neil Booth | 1d63a28 | 2002-06-28 20:27:14 +0000 | [diff] [blame] | 557 | /* If we have been requested to expand comments into macros, |
| 558 | then re-enable saving of comments. */ |
| 559 | pfile->state.save_comments = |
| 560 | ! CPP_OPTION (pfile, discard_comments_in_macro_exp); |
| 561 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 562 | if (_cpp_create_definition (pfile, node)) |
| 563 | if (pfile->cb.define) |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 564 | pfile->cb.define (pfile, pfile->directive_line, node); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 565 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 566 | } |
| 567 | |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 568 | /* Handle #undef. Mark the identifier NT_VOID in the hash table. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 569 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 570 | do_undef (cpp_reader *pfile) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 571 | { |
Tom Tromey | ee1c2a1 | 2007-01-12 19:46:49 +0000 | [diff] [blame] | 572 | cpp_hashnode *node = lex_macro_node (pfile, true); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 573 | |
Neil Booth | 45f2492 | 2003-12-12 07:00:29 +0000 | [diff] [blame] | 574 | if (node) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 575 | { |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 576 | if (pfile->cb.undef) |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 577 | pfile->cb.undef (pfile, pfile->directive_line, node); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 578 | |
Neil Booth | 45f2492 | 2003-12-12 07:00:29 +0000 | [diff] [blame] | 579 | /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified |
| 580 | identifier is not currently defined as a macro name. */ |
| 581 | if (node->type == NT_MACRO) |
| 582 | { |
| 583 | if (node->flags & NODE_WARN) |
| 584 | cpp_error (pfile, CPP_DL_WARNING, |
| 585 | "undefining \"%s\"", NODE_NAME (node)); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 586 | |
Neil Booth | 45f2492 | 2003-12-12 07:00:29 +0000 | [diff] [blame] | 587 | if (CPP_OPTION (pfile, warn_unused_macros)) |
| 588 | _cpp_warn_if_unused_macro (pfile, node, NULL); |
Neil Booth | a69cbaa | 2002-07-23 22:57:49 +0000 | [diff] [blame] | 589 | |
Neil Booth | 45f2492 | 2003-12-12 07:00:29 +0000 | [diff] [blame] | 590 | _cpp_free_definition (node); |
| 591 | } |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 592 | } |
Neil Booth | 45f2492 | 2003-12-12 07:00:29 +0000 | [diff] [blame] | 593 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 594 | check_eol (pfile); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Geoffrey Keating | d1bd0de | 2003-07-11 08:33:21 +0000 | [diff] [blame] | 597 | /* Undefine a single macro/assertion/whatever. */ |
| 598 | |
| 599 | static int |
Zack Weinberg | c6e8380 | 2004-06-05 20:58:06 +0000 | [diff] [blame] | 600 | undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h, |
Geoffrey Keating | d1bd0de | 2003-07-11 08:33:21 +0000 | [diff] [blame] | 601 | void *data_p ATTRIBUTE_UNUSED) |
| 602 | { |
Zack Weinberg | c6e8380 | 2004-06-05 20:58:06 +0000 | [diff] [blame] | 603 | /* Body of _cpp_free_definition inlined here for speed. |
| 604 | Macros and assertions no longer have anything to free. */ |
| 605 | h->type = NT_VOID; |
| 606 | h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED); |
Geoffrey Keating | d1bd0de | 2003-07-11 08:33:21 +0000 | [diff] [blame] | 607 | return 1; |
| 608 | } |
| 609 | |
| 610 | /* Undefine all macros and assertions. */ |
| 611 | |
| 612 | void |
| 613 | cpp_undef_all (cpp_reader *pfile) |
| 614 | { |
| 615 | cpp_forall_identifiers (pfile, undefine_macros, NULL); |
| 616 | } |
| 617 | |
| 618 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 619 | /* Helper routine used by parse_include. Reinterpret the current line |
| 620 | as an h-char-sequence (< ... >); we are looking at the first token |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 621 | after the <. Returns a malloced filename. */ |
| 622 | static char * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 623 | glue_header_name (cpp_reader *pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 624 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 625 | const cpp_token *token; |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 626 | char *buffer; |
Neil Booth | 2450e0b | 2002-02-23 20:21:39 +0000 | [diff] [blame] | 627 | size_t len, total_len = 0, capacity = 1024; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 628 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 629 | /* To avoid lexed tokens overwriting our glued name, we can only |
| 630 | allocate from the string pool once we've lexed everything. */ |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 631 | buffer = XNEWVEC (char, capacity); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 632 | for (;;) |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 633 | { |
Zack Weinberg | a8d0dda | 2003-02-21 18:06:30 +0000 | [diff] [blame] | 634 | token = get_token_no_padding (pfile); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 635 | |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 636 | if (token->type == CPP_GREATER) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 637 | break; |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 638 | if (token->type == CPP_EOF) |
| 639 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 640 | cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character"); |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 641 | break; |
| 642 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 643 | |
Neil Booth | 5932565 | 2003-04-24 20:03:57 +0000 | [diff] [blame] | 644 | len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */ |
Neil Booth | 2450e0b | 2002-02-23 20:21:39 +0000 | [diff] [blame] | 645 | if (total_len + len > capacity) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 646 | { |
Neil Booth | 2450e0b | 2002-02-23 20:21:39 +0000 | [diff] [blame] | 647 | capacity = (capacity + len) * 2; |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 648 | buffer = XRESIZEVEC (char, buffer, capacity); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 651 | if (token->flags & PREV_WHITE) |
Neil Booth | 2450e0b | 2002-02-23 20:21:39 +0000 | [diff] [blame] | 652 | buffer[total_len++] = ' '; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 653 | |
Geoffrey Keating | 47e2049 | 2005-03-12 10:44:06 +0000 | [diff] [blame] | 654 | total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len], |
| 655 | true) |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 656 | - (uchar *) buffer); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 659 | buffer[total_len] = '\0'; |
| 660 | return buffer; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 663 | /* Returns the file name of #include, #include_next, #import and |
| 664 | #pragma dependency. The string is malloced and the caller should |
| 665 | free it. Returns NULL on error. */ |
| 666 | static const char * |
Ian Lance Taylor | cbc43ae | 2005-10-04 18:06:19 +0000 | [diff] [blame] | 667 | parse_include (cpp_reader *pfile, int *pangle_brackets, |
| 668 | const cpp_token ***buf) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 669 | { |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 670 | char *fname; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 671 | const cpp_token *header; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 672 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 673 | /* Allow macro expansion. */ |
Zack Weinberg | a8d0dda | 2003-02-21 18:06:30 +0000 | [diff] [blame] | 674 | header = get_token_no_padding (pfile); |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 675 | if (header->type == CPP_STRING || header->type == CPP_HEADER_NAME) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 676 | { |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 677 | fname = XNEWVEC (char, header->val.str.len - 1); |
Neil Booth | 6338b35 | 2003-04-23 22:44:06 +0000 | [diff] [blame] | 678 | memcpy (fname, header->val.str.text + 1, header->val.str.len - 2); |
| 679 | fname[header->val.str.len - 2] = '\0'; |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 680 | *pangle_brackets = header->type == CPP_HEADER_NAME; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 681 | } |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 682 | else if (header->type == CPP_LESS) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 683 | { |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 684 | fname = glue_header_name (pfile); |
| 685 | *pangle_brackets = 1; |
| 686 | } |
| 687 | else |
| 688 | { |
| 689 | const unsigned char *dir; |
| 690 | |
| 691 | if (pfile->directive == &dtable[T_PRAGMA]) |
| 692 | dir = U"pragma dependency"; |
| 693 | else |
| 694 | dir = pfile->directive->name; |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 695 | cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>", |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 696 | dir); |
| 697 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 698 | return NULL; |
Richard Kenner | cfb3ee1 | 1997-04-13 14:30:13 -0400 | [diff] [blame] | 699 | } |
| 700 | |
Ian Lance Taylor | cbc43ae | 2005-10-04 18:06:19 +0000 | [diff] [blame] | 701 | if (buf == NULL || CPP_OPTION (pfile, discard_comments)) |
| 702 | check_eol (pfile); |
| 703 | else |
| 704 | { |
| 705 | /* If we are not discarding comments, then gather them while |
| 706 | doing the eol check. */ |
| 707 | *buf = check_eol_return_comments (pfile); |
| 708 | } |
| 709 | |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 710 | return fname; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Neil Booth | ba133c9 | 2001-03-15 07:57:13 +0000 | [diff] [blame] | 713 | /* Handle #include, #include_next and #import. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 714 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 715 | do_include_common (cpp_reader *pfile, enum include_type type) |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 716 | { |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 717 | const char *fname; |
| 718 | int angle_brackets; |
Ian Lance Taylor | cbc43ae | 2005-10-04 18:06:19 +0000 | [diff] [blame] | 719 | const cpp_token **buf = NULL; |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 720 | |
Ian Lance Taylor | cbc43ae | 2005-10-04 18:06:19 +0000 | [diff] [blame] | 721 | /* Re-enable saving of comments if requested, so that the include |
| 722 | callback can dump comments which follow #include. */ |
| 723 | pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments); |
| 724 | |
| 725 | fname = parse_include (pfile, &angle_brackets, &buf); |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 726 | if (!fname) |
Ian Lance Taylor | cbc43ae | 2005-10-04 18:06:19 +0000 | [diff] [blame] | 727 | { |
| 728 | if (buf) |
| 729 | XDELETEVEC (buf); |
| 730 | return; |
| 731 | } |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 732 | |
Nathanael Nerode | 2830382 | 2004-11-28 22:28:13 +0000 | [diff] [blame] | 733 | if (!*fname) |
| 734 | { |
| 735 | cpp_error (pfile, CPP_DL_ERROR, "empty filename in #%s", |
| 736 | pfile->directive->name); |
Ian Lance Taylor | cbc43ae | 2005-10-04 18:06:19 +0000 | [diff] [blame] | 737 | XDELETEVEC (fname); |
| 738 | if (buf) |
| 739 | XDELETEVEC (buf); |
Nathanael Nerode | 2830382 | 2004-11-28 22:28:13 +0000 | [diff] [blame] | 740 | return; |
| 741 | } |
| 742 | |
Zack Weinberg | 3963c2e | 2003-02-12 17:01:53 +0000 | [diff] [blame] | 743 | /* Prevent #include recursion. */ |
Per Bothner | 50f59cd | 2004-01-19 21:30:18 -0800 | [diff] [blame] | 744 | if (pfile->line_table->depth >= CPP_STACK_MAX) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 745 | cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply"); |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 746 | else |
Neil Booth | 09b8225 | 2001-07-29 22:27:20 +0000 | [diff] [blame] | 747 | { |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 748 | /* Get out of macro context, if we are. */ |
| 749 | skip_rest_of_line (pfile); |
| 750 | |
| 751 | if (pfile->cb.include) |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 752 | pfile->cb.include (pfile, pfile->directive_line, |
Ian Lance Taylor | cbc43ae | 2005-10-04 18:06:19 +0000 | [diff] [blame] | 753 | pfile->directive->name, fname, angle_brackets, |
| 754 | buf); |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 755 | |
Neil Booth | 8f9b400 | 2003-07-29 22:26:13 +0000 | [diff] [blame] | 756 | _cpp_stack_include (pfile, fname, angle_brackets, type); |
Neil Booth | 09b8225 | 2001-07-29 22:27:20 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Ian Lance Taylor | cbc43ae | 2005-10-04 18:06:19 +0000 | [diff] [blame] | 759 | XDELETEVEC (fname); |
| 760 | if (buf) |
| 761 | XDELETEVEC (buf); |
Neil Booth | ba133c9 | 2001-03-15 07:57:13 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 765 | do_include (cpp_reader *pfile) |
Neil Booth | ba133c9 | 2001-03-15 07:57:13 +0000 | [diff] [blame] | 766 | { |
| 767 | do_include_common (pfile, IT_INCLUDE); |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 770 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 771 | do_import (cpp_reader *pfile) |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 772 | { |
Neil Booth | ba133c9 | 2001-03-15 07:57:13 +0000 | [diff] [blame] | 773 | do_include_common (pfile, IT_IMPORT); |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 774 | } |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 775 | |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 776 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 777 | do_include_next (cpp_reader *pfile) |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 778 | { |
Zack Weinberg | 3963c2e | 2003-02-12 17:01:53 +0000 | [diff] [blame] | 779 | enum include_type type = IT_INCLUDE_NEXT; |
| 780 | |
| 781 | /* If this is the primary source file, warn and use the normal |
| 782 | search logic. */ |
Tom Tromey | 705e2d2 | 2007-01-04 15:32:26 +0000 | [diff] [blame] | 783 | if (cpp_in_primary_file (pfile)) |
Zack Weinberg | 3963c2e | 2003-02-12 17:01:53 +0000 | [diff] [blame] | 784 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 785 | cpp_error (pfile, CPP_DL_WARNING, |
Zack Weinberg | 3963c2e | 2003-02-12 17:01:53 +0000 | [diff] [blame] | 786 | "#include_next in primary source file"); |
| 787 | type = IT_INCLUDE; |
| 788 | } |
| 789 | do_include_common (pfile, type); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 790 | } |
| 791 | |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 792 | /* Subroutine of do_linemarker. Read possible flags after file name. |
| 793 | LAST is the last flag seen; 0 if this is the first flag. Return the |
| 794 | flag if it is valid, 0 at the end of the directive. Otherwise |
| 795 | complain. */ |
Neil Booth | 642ce43 | 2000-12-07 23:17:56 +0000 | [diff] [blame] | 796 | static unsigned int |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 797 | read_flag (cpp_reader *pfile, unsigned int last) |
Jason Merrill | d3a34a0 | 1999-08-14 00:42:07 +0000 | [diff] [blame] | 798 | { |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 799 | const cpp_token *token = _cpp_lex_token (pfile); |
Jason Merrill | d3a34a0 | 1999-08-14 00:42:07 +0000 | [diff] [blame] | 800 | |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 801 | if (token->type == CPP_NUMBER && token->val.str.len == 1) |
Jason Merrill | d3a34a0 | 1999-08-14 00:42:07 +0000 | [diff] [blame] | 802 | { |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 803 | unsigned int flag = token->val.str.text[0] - '0'; |
Neil Booth | 28e0f04 | 2000-12-09 12:06:37 +0000 | [diff] [blame] | 804 | |
| 805 | if (flag > last && flag <= 4 |
| 806 | && (flag != 4 || last == 3) |
| 807 | && (flag != 2 || last == 0)) |
| 808 | return flag; |
Jason Merrill | d3a34a0 | 1999-08-14 00:42:07 +0000 | [diff] [blame] | 809 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 810 | |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 811 | if (token->type != CPP_EOF) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 812 | cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive", |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 813 | cpp_token_as_text (pfile, token)); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 814 | return 0; |
Jason Merrill | d3a34a0 | 1999-08-14 00:42:07 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 817 | /* Subroutine of do_line and do_linemarker. Convert a number in STR, |
| 818 | of length LEN, to binary; store it in NUMP, and return 0 if the |
| 819 | number was well-formed, 1 if not. Temporary, hopefully. */ |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 820 | static int |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 821 | strtoul_for_line (const uchar *str, unsigned int len, long unsigned int *nump) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 822 | { |
| 823 | unsigned long reg = 0; |
Neil Booth | 562a5c2 | 2002-04-21 18:46:42 +0000 | [diff] [blame] | 824 | uchar c; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 825 | while (len--) |
| 826 | { |
| 827 | c = *str++; |
| 828 | if (!ISDIGIT (c)) |
| 829 | return 1; |
| 830 | reg *= 10; |
| 831 | reg += c - '0'; |
| 832 | } |
| 833 | *nump = reg; |
| 834 | return 0; |
| 835 | } |
| 836 | |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 837 | /* Interpret #line command. |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 838 | Note that the filename string (if any) is a true string constant |
| 839 | (escapes are interpreted), unlike in #line. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 840 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 841 | do_line (cpp_reader *pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 842 | { |
Per Bothner | 500bee0 | 2004-04-22 19:22:27 -0700 | [diff] [blame] | 843 | const struct line_maps *line_table = pfile->line_table; |
| 844 | const struct line_map *map = &line_table->maps[line_table->used - 1]; |
Devang Patel | 2203a88 | 2005-02-28 11:04:19 -0800 | [diff] [blame] | 845 | |
| 846 | /* skip_rest_of_line() may cause line table to be realloc()ed so note down |
| 847 | sysp right now. */ |
| 848 | |
| 849 | unsigned char map_sysp = map->sysp; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 850 | const cpp_token *token; |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 851 | const char *new_file = map->to_file; |
Neil Booth | bb74c96 | 2001-08-17 22:23:49 +0000 | [diff] [blame] | 852 | unsigned long new_lineno; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 853 | |
Neil Booth | 27e2564 | 2000-11-27 08:00:04 +0000 | [diff] [blame] | 854 | /* C99 raised the minimum limit on #line numbers. */ |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 855 | unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767; |
Neil Booth | 18a9d8f | 2001-09-16 11:23:56 +0000 | [diff] [blame] | 856 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 857 | /* #line commands expand macros. */ |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 858 | token = cpp_get_token (pfile); |
| 859 | if (token->type != CPP_NUMBER |
| 860 | || strtoul_for_line (token->val.str.text, token->val.str.len, |
| 861 | &new_lineno)) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 862 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 863 | cpp_error (pfile, CPP_DL_ERROR, |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 864 | "\"%s\" after #line is not a positive integer", |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 865 | cpp_token_as_text (pfile, token)); |
Zack Weinberg | 9ec7291 | 2000-08-09 19:41:12 +0000 | [diff] [blame] | 866 | return; |
Kazu Hirata | df38348 | 2002-05-22 22:02:16 +0000 | [diff] [blame] | 867 | } |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 868 | |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 869 | if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap)) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 870 | cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range"); |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 871 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 872 | token = cpp_get_token (pfile); |
| 873 | if (token->type == CPP_STRING) |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 874 | { |
Zack Weinberg | e6cc3a2 | 2003-07-05 00:24:00 +0000 | [diff] [blame] | 875 | cpp_string s = { 0, 0 }; |
Eric Christopher | 423e95e | 2004-02-12 02:25:03 +0000 | [diff] [blame] | 876 | if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1, |
| 877 | &s, false)) |
Zack Weinberg | e6cc3a2 | 2003-07-05 00:24:00 +0000 | [diff] [blame] | 878 | new_file = (const char *)s.text; |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 879 | check_eol (pfile); |
| 880 | } |
| 881 | else if (token->type != CPP_EOF) |
| 882 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 883 | cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename", |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 884 | cpp_token_as_text (pfile, token)); |
| 885 | return; |
| 886 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 887 | |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 888 | skip_rest_of_line (pfile); |
| 889 | _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno, |
Devang Patel | 2203a88 | 2005-02-28 11:04:19 -0800 | [diff] [blame] | 890 | map_sysp); |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | /* Interpret the # 44 "file" [flags] notation, which has slightly |
| 894 | different syntax and semantics from #line: Flags are allowed, |
| 895 | and we never complain about the line number being too big. */ |
| 896 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 897 | do_linemarker (cpp_reader *pfile) |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 898 | { |
Per Bothner | 500bee0 | 2004-04-22 19:22:27 -0700 | [diff] [blame] | 899 | const struct line_maps *line_table = pfile->line_table; |
| 900 | const struct line_map *map = &line_table->maps[line_table->used - 1]; |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 901 | const cpp_token *token; |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 902 | const char *new_file = map->to_file; |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 903 | unsigned long new_lineno; |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 904 | unsigned int new_sysp = map->sysp; |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 905 | enum lc_reason reason = LC_RENAME; |
| 906 | int flag; |
| 907 | |
| 908 | /* Back up so we can get the number again. Putting this in |
| 909 | _cpp_handle_directive risks two calls to _cpp_backup_tokens in |
| 910 | some circumstances, which can segfault. */ |
| 911 | _cpp_backup_tokens (pfile, 1); |
| 912 | |
| 913 | /* #line commands expand macros. */ |
| 914 | token = cpp_get_token (pfile); |
| 915 | if (token->type != CPP_NUMBER |
| 916 | || strtoul_for_line (token->val.str.text, token->val.str.len, |
| 917 | &new_lineno)) |
| 918 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 919 | cpp_error (pfile, CPP_DL_ERROR, |
| 920 | "\"%s\" after # is not a positive integer", |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 921 | cpp_token_as_text (pfile, token)); |
| 922 | return; |
Kazu Hirata | df38348 | 2002-05-22 22:02:16 +0000 | [diff] [blame] | 923 | } |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 924 | |
| 925 | token = cpp_get_token (pfile); |
| 926 | if (token->type == CPP_STRING) |
| 927 | { |
Zack Weinberg | e6cc3a2 | 2003-07-05 00:24:00 +0000 | [diff] [blame] | 928 | cpp_string s = { 0, 0 }; |
Eric Christopher | 423e95e | 2004-02-12 02:25:03 +0000 | [diff] [blame] | 929 | if (cpp_interpret_string_notranslate (pfile, &token->val.str, |
| 930 | 1, &s, false)) |
Zack Weinberg | e6cc3a2 | 2003-07-05 00:24:00 +0000 | [diff] [blame] | 931 | new_file = (const char *)s.text; |
Eric Christopher | cf551fb | 2004-01-16 22:37:49 +0000 | [diff] [blame] | 932 | |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 933 | new_sysp = 0; |
| 934 | flag = read_flag (pfile, 0); |
| 935 | if (flag == 1) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 936 | { |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 937 | reason = LC_ENTER; |
| 938 | /* Fake an include for cpp_included (). */ |
| 939 | _cpp_fake_include (pfile, new_file); |
| 940 | flag = read_flag (pfile, flag); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 941 | } |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 942 | else if (flag == 2) |
| 943 | { |
| 944 | reason = LC_LEAVE; |
| 945 | flag = read_flag (pfile, flag); |
| 946 | } |
| 947 | if (flag == 3) |
| 948 | { |
| 949 | new_sysp = 1; |
| 950 | flag = read_flag (pfile, flag); |
| 951 | if (flag == 4) |
| 952 | new_sysp = 2; |
| 953 | } |
Jakub Jelinek | 9d30f27 | 2006-12-29 09:15:08 +0100 | [diff] [blame] | 954 | pfile->buffer->sysp = new_sysp; |
Zack Weinberg | dcc229e | 2002-03-14 18:17:18 +0000 | [diff] [blame] | 955 | |
Neil Booth | fde8434 | 2001-08-06 21:07:41 +0000 | [diff] [blame] | 956 | check_eol (pfile); |
Zack Weinberg | ff2b53e | 2000-04-06 07:56:14 +0000 | [diff] [blame] | 957 | } |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 958 | else if (token->type != CPP_EOF) |
Neil Booth | 27e2564 | 2000-11-27 08:00:04 +0000 | [diff] [blame] | 959 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 960 | cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename", |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 961 | cpp_token_as_text (pfile, token)); |
Neil Booth | 27e2564 | 2000-11-27 08:00:04 +0000 | [diff] [blame] | 962 | return; |
| 963 | } |
Zack Weinberg | 941e09b | 1998-12-15 11:17:06 +0000 | [diff] [blame] | 964 | |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 965 | skip_rest_of_line (pfile); |
Neil Booth | bb74c96 | 2001-08-17 22:23:49 +0000 | [diff] [blame] | 966 | _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp); |
Neil Booth | 27e2564 | 2000-11-27 08:00:04 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Neil Booth | 67821e3 | 2001-08-05 17:31:25 +0000 | [diff] [blame] | 969 | /* Arrange the file_change callback. pfile->line has changed to |
Neil Booth | 47d89cf | 2001-08-11 07:33:39 +0000 | [diff] [blame] | 970 | FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system |
Joseph Myers | a1f300c | 2001-11-23 02:05:19 +0000 | [diff] [blame] | 971 | header, 2 for a system header that needs to be extern "C" protected, |
Neil Booth | 47d89cf | 2001-08-11 07:33:39 +0000 | [diff] [blame] | 972 | and zero otherwise. */ |
Neil Booth | eb1f4d9 | 2000-12-18 19:00:26 +0000 | [diff] [blame] | 973 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 974 | _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason, |
| 975 | const char *to_file, unsigned int file_line, |
| 976 | unsigned int sysp) |
Neil Booth | 27e2564 | 2000-11-27 08:00:04 +0000 | [diff] [blame] | 977 | { |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 978 | const struct line_map *map = linemap_add (pfile->line_table, reason, sysp, |
| 979 | to_file, file_line); |
Per Bothner | 500bee0 | 2004-04-22 19:22:27 -0700 | [diff] [blame] | 980 | if (map != NULL) |
| 981 | linemap_line_start (pfile->line_table, map->to_line, 127); |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 982 | |
Neil Booth | eb1f4d9 | 2000-12-18 19:00:26 +0000 | [diff] [blame] | 983 | if (pfile->cb.file_change) |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 984 | pfile->cb.file_change (pfile, map); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 985 | } |
Zack Weinberg | 941e09b | 1998-12-15 11:17:06 +0000 | [diff] [blame] | 986 | |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 987 | /* Report a warning or error detected by the program we are |
| 988 | processing. Use the directive's tokens in the error message. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 989 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 990 | do_diagnostic (cpp_reader *pfile, int code, int print_dir) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 991 | { |
Per Bothner | 12f9df4 | 2004-02-11 07:29:30 -0800 | [diff] [blame] | 992 | if (_cpp_begin_message (pfile, code, pfile->cur_token[-1].src_loc, 0)) |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 993 | { |
Neil Booth | 29b1074 | 2000-11-13 18:40:37 +0000 | [diff] [blame] | 994 | if (print_dir) |
| 995 | fprintf (stderr, "#%s ", pfile->directive->name); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 996 | pfile->state.prevent_expansion++; |
| 997 | cpp_output_line (pfile, stderr); |
| 998 | pfile->state.prevent_expansion--; |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 999 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1000 | } |
| 1001 | |
Neil Booth | 838f313 | 2000-09-24 10:42:09 +0000 | [diff] [blame] | 1002 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1003 | do_error (cpp_reader *pfile) |
Neil Booth | 838f313 | 2000-09-24 10:42:09 +0000 | [diff] [blame] | 1004 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1005 | do_diagnostic (pfile, CPP_DL_ERROR, 1); |
Neil Booth | 838f313 | 2000-09-24 10:42:09 +0000 | [diff] [blame] | 1006 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1007 | |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1008 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1009 | do_warning (cpp_reader *pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1010 | { |
Neil Booth | 2f87897 | 2001-04-08 10:01:18 +0000 | [diff] [blame] | 1011 | /* We want #warning diagnostics to be emitted in system headers too. */ |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1012 | do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1013 | } |
| 1014 | |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1015 | /* Report program identification. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1016 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1017 | do_ident (cpp_reader *pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1018 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1019 | const cpp_token *str = cpp_get_token (pfile); |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1020 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1021 | if (str->type != CPP_STRING) |
Zack Weinberg | 1ed17cd | 2005-05-12 18:31:38 +0000 | [diff] [blame] | 1022 | cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive", |
| 1023 | pfile->directive->name); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1024 | else if (pfile->cb.ident) |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1025 | pfile->cb.ident (pfile, pfile->directive_line, &str->val.str); |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1026 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1027 | check_eol (pfile); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1028 | } |
| 1029 | |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1030 | /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the |
| 1031 | matching entry, or NULL if none is found. The returned entry could |
| 1032 | be the start of a namespace chain, or a pragma. */ |
| 1033 | static struct pragma_entry * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1034 | lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma) |
Nathan Sidwell | 8244337 | 2000-06-23 10:56:09 +0000 | [diff] [blame] | 1035 | { |
Neil Booth | 4b115ff | 2001-10-14 23:04:13 +0000 | [diff] [blame] | 1036 | while (chain && chain->pragma != pragma) |
| 1037 | chain = chain->next; |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1038 | |
| 1039 | return chain; |
| 1040 | } |
| 1041 | |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1042 | /* Create and insert a blank pragma entry at the beginning of a |
| 1043 | singly-linked CHAIN. */ |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1044 | static struct pragma_entry * |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1045 | new_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain) |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1046 | { |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 1047 | struct pragma_entry *new_entry; |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1048 | |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 1049 | new_entry = (struct pragma_entry *) |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1050 | _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry)); |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1051 | |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1052 | memset (new_entry, 0, sizeof (struct pragma_entry)); |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 1053 | new_entry->next = *chain; |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1054 | |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 1055 | *chain = new_entry; |
| 1056 | return new_entry; |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | /* Register a pragma NAME in namespace SPACE. If SPACE is null, it |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1060 | goes in the global namespace. */ |
| 1061 | static struct pragma_entry * |
| 1062 | register_pragma_1 (cpp_reader *pfile, const char *space, const char *name, |
| 1063 | bool allow_name_expansion) |
Nathan Sidwell | 8244337 | 2000-06-23 10:56:09 +0000 | [diff] [blame] | 1064 | { |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1065 | struct pragma_entry **chain = &pfile->pragmas; |
| 1066 | struct pragma_entry *entry; |
Neil Booth | 4b115ff | 2001-10-14 23:04:13 +0000 | [diff] [blame] | 1067 | const cpp_hashnode *node; |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1068 | |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1069 | if (space) |
| 1070 | { |
Neil Booth | 4b115ff | 2001-10-14 23:04:13 +0000 | [diff] [blame] | 1071 | node = cpp_lookup (pfile, U space, strlen (space)); |
| 1072 | entry = lookup_pragma_entry (*chain, node); |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1073 | if (!entry) |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1074 | { |
| 1075 | entry = new_pragma_entry (pfile, chain); |
| 1076 | entry->pragma = node; |
| 1077 | entry->is_nspace = true; |
| 1078 | entry->allow_expansion = allow_name_expansion; |
| 1079 | } |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1080 | else if (!entry->is_nspace) |
| 1081 | goto clash; |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1082 | else if (entry->allow_expansion != allow_name_expansion) |
| 1083 | { |
| 1084 | cpp_error (pfile, CPP_DL_ICE, |
| 1085 | "registering pragmas in namespace \"%s\" with mismatched " |
| 1086 | "name expansion", space); |
| 1087 | return NULL; |
| 1088 | } |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1089 | chain = &entry->u.space; |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1090 | } |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1091 | else if (allow_name_expansion) |
| 1092 | { |
| 1093 | cpp_error (pfile, CPP_DL_ICE, |
| 1094 | "registering pragma \"%s\" with name expansion " |
| 1095 | "and no namespace", name); |
| 1096 | return NULL; |
| 1097 | } |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1098 | |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1099 | /* Check for duplicates. */ |
Neil Booth | 4b115ff | 2001-10-14 23:04:13 +0000 | [diff] [blame] | 1100 | node = cpp_lookup (pfile, U name, strlen (name)); |
| 1101 | entry = lookup_pragma_entry (*chain, node); |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1102 | if (entry == NULL) |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1103 | { |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1104 | entry = new_pragma_entry (pfile, chain); |
| 1105 | entry->pragma = node; |
| 1106 | return entry; |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1107 | } |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1108 | |
| 1109 | if (entry->is_nspace) |
| 1110 | clash: |
| 1111 | cpp_error (pfile, CPP_DL_ICE, |
| 1112 | "registering \"%s\" as both a pragma and a pragma namespace", |
| 1113 | NODE_NAME (node)); |
| 1114 | else if (space) |
| 1115 | cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered", |
| 1116 | space, name); |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1117 | else |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1118 | cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name); |
| 1119 | |
| 1120 | return NULL; |
| 1121 | } |
| 1122 | |
| 1123 | /* Register a cpplib internal pragma SPACE NAME with HANDLER. */ |
| 1124 | static void |
| 1125 | register_pragma_internal (cpp_reader *pfile, const char *space, |
| 1126 | const char *name, pragma_cb handler) |
| 1127 | { |
| 1128 | struct pragma_entry *entry; |
| 1129 | |
| 1130 | entry = register_pragma_1 (pfile, space, name, false); |
| 1131 | entry->is_internal = true; |
| 1132 | entry->u.handler = handler; |
Zack Weinberg | 21b1149 | 2004-09-09 19:16:56 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | /* Register a pragma NAME in namespace SPACE. If SPACE is null, it |
| 1136 | goes in the global namespace. HANDLER is the handler it will call, |
Daniel Jacobowitz | b5b3e36 | 2004-11-23 23:25:40 +0000 | [diff] [blame] | 1137 | which must be non-NULL. If ALLOW_EXPANSION is set, allow macro |
| 1138 | expansion while parsing pragma NAME. This function is exported |
| 1139 | from libcpp. */ |
Zack Weinberg | 21b1149 | 2004-09-09 19:16:56 +0000 | [diff] [blame] | 1140 | void |
| 1141 | cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name, |
Daniel Jacobowitz | b5b3e36 | 2004-11-23 23:25:40 +0000 | [diff] [blame] | 1142 | pragma_cb handler, bool allow_expansion) |
Zack Weinberg | 21b1149 | 2004-09-09 19:16:56 +0000 | [diff] [blame] | 1143 | { |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1144 | struct pragma_entry *entry; |
| 1145 | |
| 1146 | if (!handler) |
| 1147 | { |
| 1148 | cpp_error (pfile, CPP_DL_ICE, "registering pragma with NULL handler"); |
| 1149 | return; |
| 1150 | } |
| 1151 | |
| 1152 | entry = register_pragma_1 (pfile, space, name, false); |
| 1153 | if (entry) |
| 1154 | { |
| 1155 | entry->allow_expansion = allow_expansion; |
| 1156 | entry->u.handler = handler; |
| 1157 | } |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1158 | } |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1159 | |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1160 | /* Similarly, but create mark the pragma for deferred processing. |
| 1161 | When found, a CPP_PRAGMA token will be insertted into the stream |
| 1162 | with IDENT in the token->u.pragma slot. */ |
| 1163 | void |
| 1164 | cpp_register_deferred_pragma (cpp_reader *pfile, const char *space, |
| 1165 | const char *name, unsigned int ident, |
| 1166 | bool allow_expansion, bool allow_name_expansion) |
| 1167 | { |
| 1168 | struct pragma_entry *entry; |
| 1169 | |
| 1170 | entry = register_pragma_1 (pfile, space, name, allow_name_expansion); |
| 1171 | if (entry) |
| 1172 | { |
| 1173 | entry->is_deferred = true; |
| 1174 | entry->allow_expansion = allow_expansion; |
| 1175 | entry->u.ident = ident; |
| 1176 | } |
| 1177 | } |
| 1178 | |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1179 | /* Register the pragmas the preprocessor itself handles. */ |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1180 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1181 | _cpp_init_internal_pragmas (cpp_reader *pfile) |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1182 | { |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1183 | /* Pragmas in the global namespace. */ |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1184 | register_pragma_internal (pfile, 0, "once", do_pragma_once); |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1185 | |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1186 | /* New GCC-specific pragmas should be put in the GCC namespace. */ |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1187 | register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison); |
| 1188 | register_pragma_internal (pfile, "GCC", "system_header", |
| 1189 | do_pragma_system_header); |
| 1190 | register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency); |
Nathan Sidwell | 8244337 | 2000-06-23 10:56:09 +0000 | [diff] [blame] | 1191 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1192 | |
Geoffrey Keating | 17211ab | 2003-01-10 02:22:34 +0000 | [diff] [blame] | 1193 | /* Return the number of registered pragmas in PE. */ |
| 1194 | |
| 1195 | static int |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1196 | count_registered_pragmas (struct pragma_entry *pe) |
Geoffrey Keating | 17211ab | 2003-01-10 02:22:34 +0000 | [diff] [blame] | 1197 | { |
| 1198 | int ct = 0; |
| 1199 | for (; pe != NULL; pe = pe->next) |
| 1200 | { |
| 1201 | if (pe->is_nspace) |
| 1202 | ct += count_registered_pragmas (pe->u.space); |
| 1203 | ct++; |
| 1204 | } |
| 1205 | return ct; |
| 1206 | } |
| 1207 | |
| 1208 | /* Save into SD the names of the registered pragmas referenced by PE, |
| 1209 | and return a pointer to the next free space in SD. */ |
| 1210 | |
| 1211 | static char ** |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1212 | save_registered_pragmas (struct pragma_entry *pe, char **sd) |
Geoffrey Keating | 17211ab | 2003-01-10 02:22:34 +0000 | [diff] [blame] | 1213 | { |
| 1214 | for (; pe != NULL; pe = pe->next) |
| 1215 | { |
| 1216 | if (pe->is_nspace) |
| 1217 | sd = save_registered_pragmas (pe->u.space, sd); |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 1218 | *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident), |
| 1219 | HT_LEN (&pe->pragma->ident), |
| 1220 | HT_LEN (&pe->pragma->ident) + 1); |
Geoffrey Keating | 17211ab | 2003-01-10 02:22:34 +0000 | [diff] [blame] | 1221 | } |
| 1222 | return sd; |
| 1223 | } |
| 1224 | |
| 1225 | /* Return a newly-allocated array which saves the names of the |
| 1226 | registered pragmas. */ |
| 1227 | |
| 1228 | char ** |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1229 | _cpp_save_pragma_names (cpp_reader *pfile) |
Geoffrey Keating | 17211ab | 2003-01-10 02:22:34 +0000 | [diff] [blame] | 1230 | { |
| 1231 | int ct = count_registered_pragmas (pfile->pragmas); |
Bernardo Innocenti | 72bb2c3 | 2004-07-24 20:04:42 +0200 | [diff] [blame] | 1232 | char **result = XNEWVEC (char *, ct); |
Geoffrey Keating | 17211ab | 2003-01-10 02:22:34 +0000 | [diff] [blame] | 1233 | (void) save_registered_pragmas (pfile->pragmas, result); |
| 1234 | return result; |
| 1235 | } |
| 1236 | |
| 1237 | /* Restore from SD the names of the registered pragmas referenced by PE, |
| 1238 | and return a pointer to the next unused name in SD. */ |
| 1239 | |
| 1240 | static char ** |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1241 | restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe, |
| 1242 | char **sd) |
Geoffrey Keating | 17211ab | 2003-01-10 02:22:34 +0000 | [diff] [blame] | 1243 | { |
| 1244 | for (; pe != NULL; pe = pe->next) |
| 1245 | { |
| 1246 | if (pe->is_nspace) |
| 1247 | sd = restore_registered_pragmas (pfile, pe->u.space, sd); |
| 1248 | pe->pragma = cpp_lookup (pfile, U *sd, strlen (*sd)); |
| 1249 | free (*sd); |
| 1250 | sd++; |
| 1251 | } |
| 1252 | return sd; |
| 1253 | } |
| 1254 | |
| 1255 | /* Restore the names of the registered pragmas from SAVED. */ |
| 1256 | |
| 1257 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1258 | _cpp_restore_pragma_names (cpp_reader *pfile, char **saved) |
Geoffrey Keating | 17211ab | 2003-01-10 02:22:34 +0000 | [diff] [blame] | 1259 | { |
| 1260 | (void) restore_registered_pragmas (pfile, pfile->pragmas, saved); |
| 1261 | free (saved); |
| 1262 | } |
| 1263 | |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1264 | /* Pragmata handling. We handle some, and pass the rest on to the |
| 1265 | front end. C99 defines three pragmas and says that no macro |
| 1266 | expansion is to be performed on them; whether or not macro |
| 1267 | expansion happens for other pragmas is implementation defined. |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1268 | This implementation allows for a mix of both, since GCC did not |
| 1269 | traditionally macro expand its (few) pragmas, whereas OpenMP |
| 1270 | specifies that macro expansion should happen. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1271 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1272 | do_pragma (cpp_reader *pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1273 | { |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1274 | const struct pragma_entry *p = NULL; |
Alexandre Oliva | e2e1fa5 | 2003-09-24 23:53:07 +0000 | [diff] [blame] | 1275 | const cpp_token *token, *pragma_token = pfile->cur_token; |
Jakub Jelinek | 1c90c6f | 2006-06-09 23:13:25 +0200 | [diff] [blame] | 1276 | cpp_token ns_token; |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1277 | unsigned int count = 1; |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 1278 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1279 | pfile->state.prevent_expansion++; |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1280 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1281 | token = cpp_get_token (pfile); |
Jakub Jelinek | 1c90c6f | 2006-06-09 23:13:25 +0200 | [diff] [blame] | 1282 | ns_token = *token; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1283 | if (token->type == CPP_NAME) |
Alexandre Oliva | 0172e2b | 2000-02-27 06:24:27 +0000 | [diff] [blame] | 1284 | { |
Neil Booth | 4b115ff | 2001-10-14 23:04:13 +0000 | [diff] [blame] | 1285 | p = lookup_pragma_entry (pfile->pragmas, token->val.node); |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1286 | if (p && p->is_nspace) |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1287 | { |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1288 | bool allow_name_expansion = p->allow_expansion; |
| 1289 | if (allow_name_expansion) |
| 1290 | pfile->state.prevent_expansion--; |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1291 | token = cpp_get_token (pfile); |
| 1292 | if (token->type == CPP_NAME) |
Neil Booth | 4b115ff | 2001-10-14 23:04:13 +0000 | [diff] [blame] | 1293 | p = lookup_pragma_entry (p->u.space, token->val.node); |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1294 | else |
| 1295 | p = NULL; |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1296 | if (allow_name_expansion) |
| 1297 | pfile->state.prevent_expansion++; |
| 1298 | count = 2; |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1299 | } |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
Zack Weinberg | 3da3d58 | 2004-10-27 17:29:29 +0000 | [diff] [blame] | 1302 | if (p) |
Alexandre Oliva | e2e1fa5 | 2003-09-24 23:53:07 +0000 | [diff] [blame] | 1303 | { |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1304 | if (p->is_deferred) |
Zack Weinberg | 3da3d58 | 2004-10-27 17:29:29 +0000 | [diff] [blame] | 1305 | { |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1306 | pfile->directive_result.src_loc = pragma_token->src_loc; |
| 1307 | pfile->directive_result.type = CPP_PRAGMA; |
| 1308 | pfile->directive_result.flags = pragma_token->flags; |
| 1309 | pfile->directive_result.val.pragma = p->u.ident; |
| 1310 | pfile->state.in_deferred_pragma = true; |
| 1311 | pfile->state.pragma_allow_expansion = p->allow_expansion; |
| 1312 | if (!p->allow_expansion) |
Daniel Jacobowitz | b5b3e36 | 2004-11-23 23:25:40 +0000 | [diff] [blame] | 1313 | pfile->state.prevent_expansion++; |
Zack Weinberg | 3da3d58 | 2004-10-27 17:29:29 +0000 | [diff] [blame] | 1314 | } |
| 1315 | else |
| 1316 | { |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1317 | /* Since the handler below doesn't get the line number, that |
| 1318 | it might need for diagnostics, make sure it has the right |
| 1319 | numbers in place. */ |
| 1320 | if (pfile->cb.line_change) |
| 1321 | (*pfile->cb.line_change) (pfile, pragma_token, false); |
| 1322 | if (p->allow_expansion) |
| 1323 | pfile->state.prevent_expansion--; |
| 1324 | (*p->u.handler) (pfile); |
| 1325 | if (p->allow_expansion) |
| 1326 | pfile->state.prevent_expansion++; |
Zack Weinberg | 3da3d58 | 2004-10-27 17:29:29 +0000 | [diff] [blame] | 1327 | } |
Zack Weinberg | 21b1149 | 2004-09-09 19:16:56 +0000 | [diff] [blame] | 1328 | } |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 1329 | else if (pfile->cb.def_pragma) |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 1330 | { |
Jakub Jelinek | 1c90c6f | 2006-06-09 23:13:25 +0200 | [diff] [blame] | 1331 | if (count == 1 || pfile->context->prev == NULL) |
| 1332 | _cpp_backup_tokens (pfile, count); |
| 1333 | else |
| 1334 | { |
| 1335 | /* Invalid name comes from macro expansion, _cpp_backup_tokens |
| 1336 | won't allow backing 2 tokens. */ |
| 1337 | /* ??? The token buffer is leaked. Perhaps if def_pragma hook |
| 1338 | reads both tokens, we could perhaps free it, but if it doesn't, |
| 1339 | we don't know the exact lifespan. */ |
| 1340 | cpp_token *toks = XNEWVEC (cpp_token, 2); |
| 1341 | toks[0] = ns_token; |
| 1342 | toks[0].flags |= NO_EXPAND; |
| 1343 | toks[1] = *token; |
| 1344 | toks[1].flags |= NO_EXPAND; |
| 1345 | _cpp_push_token_context (pfile, NULL, toks, 2); |
| 1346 | } |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1347 | pfile->cb.def_pragma (pfile, pfile->directive_line); |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 1348 | } |
Neil Booth | a5da89c | 2001-10-14 17:44:00 +0000 | [diff] [blame] | 1349 | |
Neil Booth | 9729389 | 2001-09-14 22:04:46 +0000 | [diff] [blame] | 1350 | pfile->state.prevent_expansion--; |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 1353 | /* Handle #pragma once. */ |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1354 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1355 | do_pragma_once (cpp_reader *pfile) |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1356 | { |
Tom Tromey | 705e2d2 | 2007-01-04 15:32:26 +0000 | [diff] [blame] | 1357 | if (cpp_in_primary_file (pfile)) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1358 | cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file"); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1359 | |
| 1360 | check_eol (pfile); |
Neil Booth | 49634b3 | 2003-08-02 16:29:46 +0000 | [diff] [blame] | 1361 | _cpp_mark_file_once_only (pfile, pfile->buffer->file); |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
Neil Booth | c3bf3e6 | 2002-05-09 17:14:22 +0000 | [diff] [blame] | 1364 | /* Handle #pragma GCC poison, to poison one or more identifiers so |
| 1365 | that the lexer produces a hard error for each subsequent usage. */ |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1366 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1367 | do_pragma_poison (cpp_reader *pfile) |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1368 | { |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 1369 | const cpp_token *tok; |
Zack Weinberg | f8f769e | 2000-05-28 05:56:38 +0000 | [diff] [blame] | 1370 | cpp_hashnode *hp; |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1371 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1372 | pfile->state.poisoned_ok = 1; |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1373 | for (;;) |
| 1374 | { |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 1375 | tok = _cpp_lex_token (pfile); |
| 1376 | if (tok->type == CPP_EOF) |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1377 | break; |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 1378 | if (tok->type != CPP_NAME) |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1379 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1380 | cpp_error (pfile, CPP_DL_ERROR, |
| 1381 | "invalid #pragma GCC poison directive"); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1382 | break; |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
Neil Booth | 345894b | 2001-09-16 13:44:29 +0000 | [diff] [blame] | 1385 | hp = tok->val.node; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1386 | if (hp->flags & NODE_POISONED) |
| 1387 | continue; |
| 1388 | |
| 1389 | if (hp->type == NT_MACRO) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1390 | cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"", |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 1391 | NODE_NAME (hp)); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1392 | _cpp_free_definition (hp); |
| 1393 | hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC; |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1394 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1395 | pfile->state.poisoned_ok = 0; |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1396 | } |
Zack Weinberg | 2c0b35c | 2000-05-17 18:07:16 +0000 | [diff] [blame] | 1397 | |
| 1398 | /* Mark the current header as a system header. This will suppress |
| 1399 | some categories of warnings (notably those from -pedantic). It is |
| 1400 | intended for use in system libraries that cannot be implemented in |
| 1401 | conforming C, but cannot be certain that their headers appear in a |
| 1402 | system include directory. To prevent abuse, it is rejected in the |
| 1403 | primary source file. */ |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1404 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1405 | do_pragma_system_header (cpp_reader *pfile) |
Zack Weinberg | 2c0b35c | 2000-05-17 18:07:16 +0000 | [diff] [blame] | 1406 | { |
Tom Tromey | 705e2d2 | 2007-01-04 15:32:26 +0000 | [diff] [blame] | 1407 | if (cpp_in_primary_file (pfile)) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1408 | cpp_error (pfile, CPP_DL_WARNING, |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 1409 | "#pragma system_header ignored outside include file"); |
Zack Weinberg | 2c0b35c | 2000-05-17 18:07:16 +0000 | [diff] [blame] | 1410 | else |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 1411 | { |
| 1412 | check_eol (pfile); |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 1413 | skip_rest_of_line (pfile); |
Neil Booth | d82fc10 | 2001-08-02 23:03:31 +0000 | [diff] [blame] | 1414 | cpp_make_system_header (pfile, 1, 0); |
| 1415 | } |
Zack Weinberg | 2c0b35c | 2000-05-17 18:07:16 +0000 | [diff] [blame] | 1416 | } |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 1417 | |
| 1418 | /* Check the modified date of the current include file against a specified |
| 1419 | file. Issue a diagnostic, if the specified file is newer. We use this to |
| 1420 | determine if a fixed header should be refixed. */ |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1421 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1422 | do_pragma_dependency (cpp_reader *pfile) |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 1423 | { |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 1424 | const char *fname; |
| 1425 | int angle_brackets, ordering; |
Kazu Hirata | df38348 | 2002-05-22 22:02:16 +0000 | [diff] [blame] | 1426 | |
Ian Lance Taylor | cbc43ae | 2005-10-04 18:06:19 +0000 | [diff] [blame] | 1427 | fname = parse_include (pfile, &angle_brackets, NULL); |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 1428 | if (!fname) |
Zack Weinberg | 58fea6a | 2000-08-02 01:13:45 +0000 | [diff] [blame] | 1429 | return; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1430 | |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 1431 | ordering = _cpp_compare_file_date (pfile, fname, angle_brackets); |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 1432 | if (ordering < 0) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1433 | cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname); |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 1434 | else if (ordering > 0) |
| 1435 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1436 | cpp_error (pfile, CPP_DL_WARNING, |
| 1437 | "current file is older than %s", fname); |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1438 | if (cpp_get_token (pfile)->type != CPP_EOF) |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 1439 | { |
| 1440 | _cpp_backup_tokens (pfile, 1); |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1441 | do_diagnostic (pfile, CPP_DL_WARNING, 0); |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 1442 | } |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 1443 | } |
Neil Booth | 74eb4b3 | 2003-04-21 19:21:59 +0000 | [diff] [blame] | 1444 | |
Kaveh R. Ghazi | fad205f | 2003-06-16 21:41:10 +0000 | [diff] [blame] | 1445 | free ((void *) fname); |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1448 | /* Get a token but skip padding. */ |
| 1449 | static const cpp_token * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1450 | get_token_no_padding (cpp_reader *pfile) |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1451 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1452 | for (;;) |
| 1453 | { |
| 1454 | const cpp_token *result = cpp_get_token (pfile); |
| 1455 | if (result->type != CPP_PADDING) |
| 1456 | return result; |
| 1457 | } |
| 1458 | } |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1459 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1460 | /* Check syntax is "(string-literal)". Returns the string on success, |
| 1461 | or NULL on failure. */ |
| 1462 | static const cpp_token * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1463 | get__Pragma_string (cpp_reader *pfile) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1464 | { |
| 1465 | const cpp_token *string; |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1466 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1467 | if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN) |
| 1468 | return NULL; |
| 1469 | |
| 1470 | string = get_token_no_padding (pfile); |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1471 | if (string->type != CPP_STRING && string->type != CPP_WSTRING) |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1472 | return NULL; |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1473 | |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1474 | if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN) |
| 1475 | return NULL; |
| 1476 | |
| 1477 | return string; |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Neil Booth | 8706281 | 2001-10-20 09:00:53 +0000 | [diff] [blame] | 1480 | /* Destringize IN into a temporary buffer, by removing the first \ of |
| 1481 | \" and \\ sequences, and process the result as a #pragma directive. */ |
| 1482 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1483 | destringize_and_run (cpp_reader *pfile, const cpp_string *in) |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1484 | { |
| 1485 | const unsigned char *src, *limit; |
Neil Booth | 8706281 | 2001-10-20 09:00:53 +0000 | [diff] [blame] | 1486 | char *dest, *result; |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1487 | cpp_context *saved_context; |
| 1488 | cpp_token *saved_cur_token; |
| 1489 | tokenrun *saved_cur_run; |
| 1490 | cpp_token *toks; |
| 1491 | int count; |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1492 | |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 1493 | dest = result = (char *) alloca (in->len - 1); |
Neil Booth | 6338b35 | 2003-04-23 22:44:06 +0000 | [diff] [blame] | 1494 | src = in->text + 1 + (in->text[0] == 'L'); |
| 1495 | limit = in->text + in->len - 1; |
| 1496 | while (src < limit) |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1497 | { |
| 1498 | /* We know there is a character following the backslash. */ |
| 1499 | if (*src == '\\' && (src[1] == '\\' || src[1] == '"')) |
| 1500 | src++; |
| 1501 | *dest++ = *src++; |
| 1502 | } |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 1503 | *dest = '\n'; |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1504 | |
Neil Booth | 8128ccc | 2002-11-18 20:43:40 +0000 | [diff] [blame] | 1505 | /* Ugh; an awful kludge. We are really not set up to be lexing |
| 1506 | tokens when in the middle of a macro expansion. Use a new |
| 1507 | context to force cpp_get_token to lex, and so skip_rest_of_line |
| 1508 | doesn't go beyond the end of the text. Also, remember the |
| 1509 | current lexing position so we can return to it later. |
Neil Booth | 79ba5e3 | 2002-09-03 21:55:40 +0000 | [diff] [blame] | 1510 | |
Neil Booth | 8128ccc | 2002-11-18 20:43:40 +0000 | [diff] [blame] | 1511 | Something like line-at-a-time lexing should remove the need for |
| 1512 | this. */ |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1513 | saved_context = pfile->context; |
| 1514 | saved_cur_token = pfile->cur_token; |
| 1515 | saved_cur_run = pfile->cur_run; |
Neil Booth | 8128ccc | 2002-11-18 20:43:40 +0000 | [diff] [blame] | 1516 | |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1517 | pfile->context = XNEW (cpp_context); |
| 1518 | pfile->context->macro = 0; |
| 1519 | pfile->context->prev = 0; |
Jakub Jelinek | 1c90c6f | 2006-06-09 23:13:25 +0200 | [diff] [blame] | 1520 | pfile->context->next = 0; |
Neil Booth | 79ba5e3 | 2002-09-03 21:55:40 +0000 | [diff] [blame] | 1521 | |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1522 | /* Inline run_directive, since we need to delay the _cpp_pop_buffer |
| 1523 | until we've read all of the tokens that we want. */ |
| 1524 | cpp_push_buffer (pfile, (const uchar *) result, dest - result, |
| 1525 | /* from_stage3 */ true); |
| 1526 | /* ??? Antique Disgusting Hack. What does this do? */ |
| 1527 | if (pfile->buffer->prev) |
| 1528 | pfile->buffer->file = pfile->buffer->prev->file; |
Neil Booth | 79ba5e3 | 2002-09-03 21:55:40 +0000 | [diff] [blame] | 1529 | |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1530 | start_directive (pfile); |
| 1531 | _cpp_clean_line (pfile); |
| 1532 | do_pragma (pfile); |
| 1533 | end_directive (pfile, 1); |
Neil Booth | 79ba5e3 | 2002-09-03 21:55:40 +0000 | [diff] [blame] | 1534 | |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1535 | /* We always insert at least one token, the directive result. It'll |
| 1536 | either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we |
| 1537 | need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */ |
Neil Booth | 79ba5e3 | 2002-09-03 21:55:40 +0000 | [diff] [blame] | 1538 | |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1539 | /* If we're not handling the pragma internally, read all of the tokens from |
| 1540 | the string buffer now, while the string buffer is still installed. */ |
| 1541 | /* ??? Note that the token buffer allocated here is leaked. It's not clear |
| 1542 | to me what the true lifespan of the tokens are. It would appear that |
| 1543 | the lifespan is the entire parse of the main input stream, in which case |
| 1544 | this may not be wrong. */ |
| 1545 | if (pfile->directive_result.type == CPP_PRAGMA) |
| 1546 | { |
| 1547 | int maxcount; |
Neil Booth | 79ba5e3 | 2002-09-03 21:55:40 +0000 | [diff] [blame] | 1548 | |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1549 | count = 1; |
| 1550 | maxcount = 50; |
| 1551 | toks = XNEWVEC (cpp_token, maxcount); |
| 1552 | toks[0] = pfile->directive_result; |
| 1553 | |
| 1554 | do |
| 1555 | { |
| 1556 | if (count == maxcount) |
| 1557 | { |
| 1558 | maxcount = maxcount * 3 / 2; |
| 1559 | toks = XRESIZEVEC (cpp_token, toks, maxcount); |
| 1560 | } |
Jakub Jelinek | 1c90c6f | 2006-06-09 23:13:25 +0200 | [diff] [blame] | 1561 | toks[count] = *cpp_get_token (pfile); |
| 1562 | /* Macros have been already expanded by cpp_get_token |
| 1563 | if the pragma allowed expansion. */ |
| 1564 | toks[count++].flags |= NO_EXPAND; |
Richard Henderson | bc4071d | 2006-01-04 08:33:38 -0800 | [diff] [blame] | 1565 | } |
| 1566 | while (toks[count-1].type != CPP_PRAGMA_EOL); |
| 1567 | } |
| 1568 | else |
| 1569 | { |
| 1570 | count = 1; |
| 1571 | toks = XNEW (cpp_token); |
| 1572 | toks[0] = pfile->directive_result; |
| 1573 | |
| 1574 | /* If we handled the entire pragma internally, make sure we get the |
| 1575 | line number correct for the next token. */ |
| 1576 | if (pfile->cb.line_change) |
| 1577 | pfile->cb.line_change (pfile, pfile->cur_token, false); |
| 1578 | } |
| 1579 | |
| 1580 | /* Finish inlining run_directive. */ |
| 1581 | pfile->buffer->file = NULL; |
| 1582 | _cpp_pop_buffer (pfile); |
| 1583 | |
| 1584 | /* Reset the old macro state before ... */ |
| 1585 | XDELETE (pfile->context); |
| 1586 | pfile->context = saved_context; |
| 1587 | pfile->cur_token = saved_cur_token; |
| 1588 | pfile->cur_run = saved_cur_run; |
| 1589 | |
| 1590 | /* ... inserting the new tokens we collected. */ |
| 1591 | _cpp_push_token_context (pfile, NULL, toks, count); |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
Neil Booth | 8706281 | 2001-10-20 09:00:53 +0000 | [diff] [blame] | 1594 | /* Handle the _Pragma operator. */ |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1595 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1596 | _cpp_do__Pragma (cpp_reader *pfile) |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1597 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1598 | const cpp_token *string = get__Pragma_string (pfile); |
Zack Weinberg | 21b1149 | 2004-09-09 19:16:56 +0000 | [diff] [blame] | 1599 | pfile->directive_result.type = CPP_PADDING; |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1600 | |
Neil Booth | 79ba5e3 | 2002-09-03 21:55:40 +0000 | [diff] [blame] | 1601 | if (string) |
| 1602 | destringize_and_run (pfile, &string->val.str); |
| 1603 | else |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1604 | cpp_error (pfile, CPP_DL_ERROR, |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 1605 | "_Pragma takes a parenthesized string literal"); |
Neil Booth | a5c3ccc | 2000-10-30 22:29:00 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 1608 | /* Handle #ifdef. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1609 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1610 | do_ifdef (cpp_reader *pfile) |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1611 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1612 | int skip = 1; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1613 | |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1614 | if (! pfile->state.skipping) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1615 | { |
Tom Tromey | ee1c2a1 | 2007-01-12 19:46:49 +0000 | [diff] [blame] | 1616 | const cpp_hashnode *node = lex_macro_node (pfile, false); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1617 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1618 | if (node) |
Neil Booth | a69cbaa | 2002-07-23 22:57:49 +0000 | [diff] [blame] | 1619 | { |
| 1620 | skip = node->type != NT_MACRO; |
| 1621 | _cpp_mark_macro_used (node); |
| 1622 | check_eol (pfile); |
| 1623 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | push_conditional (pfile, skip, T_IFDEF, 0); |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 1629 | /* Handle #ifndef. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1630 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1631 | do_ifndef (cpp_reader *pfile) |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1632 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1633 | int skip = 1; |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame] | 1634 | const cpp_hashnode *node = 0; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1635 | |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1636 | if (! pfile->state.skipping) |
Jakub Jelinek | 5af7e2c | 2000-06-08 00:27:57 +0200 | [diff] [blame] | 1637 | { |
Tom Tromey | ee1c2a1 | 2007-01-12 19:46:49 +0000 | [diff] [blame] | 1638 | node = lex_macro_node (pfile, false); |
Geoffrey Keating | b43db0b | 2000-12-02 22:28:44 +0000 | [diff] [blame] | 1639 | |
| 1640 | if (node) |
Neil Booth | a69cbaa | 2002-07-23 22:57:49 +0000 | [diff] [blame] | 1641 | { |
| 1642 | skip = node->type == NT_MACRO; |
| 1643 | _cpp_mark_macro_used (node); |
| 1644 | check_eol (pfile); |
| 1645 | } |
Jakub Jelinek | 5af7e2c | 2000-06-08 00:27:57 +0200 | [diff] [blame] | 1646 | } |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1647 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1648 | push_conditional (pfile, skip, T_IFNDEF, node); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1649 | } |
| 1650 | |
Neil Booth | 6d18adb | 2001-07-29 17:27:57 +0000 | [diff] [blame] | 1651 | /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in |
| 1652 | pfile->mi_ind_cmacro so we can handle multiple-include |
Kazu Hirata | 6356f89 | 2003-06-12 19:01:08 +0000 | [diff] [blame] | 1653 | optimizations. If macro expansion occurs in the expression, we |
Neil Booth | 6d18adb | 2001-07-29 17:27:57 +0000 | [diff] [blame] | 1654 | cannot treat it as a controlling conditional, since the expansion |
| 1655 | could change in the future. That is handled by cpp_get_token. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1656 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1657 | do_if (cpp_reader *pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1658 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1659 | int skip = 1; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1660 | |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1661 | if (! pfile->state.skipping) |
Neil Booth | 87ed109 | 2002-04-28 19:42:54 +0000 | [diff] [blame] | 1662 | skip = _cpp_parse_expr (pfile) == false; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1663 | |
Neil Booth | 6d18adb | 2001-07-29 17:27:57 +0000 | [diff] [blame] | 1664 | push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1665 | } |
| 1666 | |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1667 | /* Flip skipping state if appropriate and continue without changing |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1668 | if_stack; this is so that the error message for missing #endif's |
| 1669 | etc. will point to the original #if. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1670 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1671 | do_else (cpp_reader *pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1672 | { |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1673 | cpp_buffer *buffer = pfile->buffer; |
| 1674 | struct if_stack *ifs = buffer->if_stack; |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1675 | |
| 1676 | if (ifs == NULL) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1677 | cpp_error (pfile, CPP_DL_ERROR, "#else without #if"); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1678 | else |
Zack Weinberg | 40ea76d | 2000-02-06 07:30:25 +0000 | [diff] [blame] | 1679 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1680 | if (ifs->type == T_ELSE) |
| 1681 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1682 | cpp_error (pfile, CPP_DL_ERROR, "#else after #else"); |
| 1683 | cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1684 | "the conditional began here"); |
| 1685 | } |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1686 | ifs->type = T_ELSE; |
| 1687 | |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1688 | /* Skip any future (erroneous) #elses or #elifs. */ |
| 1689 | pfile->state.skipping = ifs->skip_elses; |
| 1690 | ifs->skip_elses = true; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1691 | |
| 1692 | /* Invalidate any controlling macro. */ |
| 1693 | ifs->mi_cmacro = 0; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1694 | |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1695 | /* Only check EOL if was not originally skipping. */ |
Phil Edwards | 909de5d | 2002-03-22 21:59:04 +0000 | [diff] [blame] | 1696 | if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels)) |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1697 | check_eol (pfile); |
| 1698 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1699 | } |
| 1700 | |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 1701 | /* Handle a #elif directive by not changing if_stack either. See the |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1702 | comment above do_else. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1703 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1704 | do_elif (cpp_reader *pfile) |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1705 | { |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1706 | cpp_buffer *buffer = pfile->buffer; |
| 1707 | struct if_stack *ifs = buffer->if_stack; |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1708 | |
| 1709 | if (ifs == NULL) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1710 | cpp_error (pfile, CPP_DL_ERROR, "#elif without #if"); |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1711 | else |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1712 | { |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1713 | if (ifs->type == T_ELSE) |
| 1714 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1715 | cpp_error (pfile, CPP_DL_ERROR, "#elif after #else"); |
| 1716 | cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1717 | "the conditional began here"); |
| 1718 | } |
| 1719 | ifs->type = T_ELIF; |
| 1720 | |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1721 | /* Only evaluate this if we aren't skipping elses. During |
| 1722 | evaluation, set skipping to false to get lexer warnings. */ |
| 1723 | if (ifs->skip_elses) |
| 1724 | pfile->state.skipping = 1; |
| 1725 | else |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1726 | { |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1727 | pfile->state.skipping = 0; |
| 1728 | pfile->state.skipping = ! _cpp_parse_expr (pfile); |
| 1729 | ifs->skip_elses = ! pfile->state.skipping; |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1730 | } |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1731 | |
| 1732 | /* Invalidate any controlling macro. */ |
| 1733 | ifs->mi_cmacro = 0; |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1734 | } |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1737 | /* #endif pops the if stack and resets pfile->state.skipping. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1738 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1739 | do_endif (cpp_reader *pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1740 | { |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1741 | cpp_buffer *buffer = pfile->buffer; |
| 1742 | struct if_stack *ifs = buffer->if_stack; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1743 | |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1744 | if (ifs == NULL) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1745 | cpp_error (pfile, CPP_DL_ERROR, "#endif without #if"); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1746 | else |
| 1747 | { |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1748 | /* Only check EOL if was not originally skipping. */ |
Phil Edwards | 909de5d | 2002-03-22 21:59:04 +0000 | [diff] [blame] | 1749 | if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels)) |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1750 | check_eol (pfile); |
| 1751 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1752 | /* If potential control macro, we go back outside again. */ |
| 1753 | if (ifs->next == 0 && ifs->mi_cmacro) |
| 1754 | { |
Neil Booth | 6d18adb | 2001-07-29 17:27:57 +0000 | [diff] [blame] | 1755 | pfile->mi_valid = true; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1756 | pfile->mi_cmacro = ifs->mi_cmacro; |
| 1757 | } |
| 1758 | |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1759 | buffer->if_stack = ifs->next; |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1760 | pfile->state.skipping = ifs->was_skipping; |
Neil Booth | 2a967f3 | 2001-05-20 06:26:45 +0000 | [diff] [blame] | 1761 | obstack_free (&pfile->buffer_ob, ifs); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1762 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1763 | } |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1764 | |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 1765 | /* Push an if_stack entry for a preprocessor conditional, and set |
| 1766 | pfile->state.skipping to SKIP. If TYPE indicates the conditional |
| 1767 | is #if or #ifndef, CMACRO is a potentially controlling macro, and |
| 1768 | we need to check here that we are at the top of the file. */ |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1769 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1770 | push_conditional (cpp_reader *pfile, int skip, int type, |
| 1771 | const cpp_hashnode *cmacro) |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1772 | { |
| 1773 | struct if_stack *ifs; |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1774 | cpp_buffer *buffer = pfile->buffer; |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1775 | |
Bernardo Innocenti | 72bb2c3 | 2004-07-24 20:04:42 +0200 | [diff] [blame] | 1776 | ifs = XOBNEW (&pfile->buffer_ob, struct if_stack); |
Neil Booth | 5041042 | 2001-09-15 10:18:03 +0000 | [diff] [blame] | 1777 | ifs->line = pfile->directive_line; |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1778 | ifs->next = buffer->if_stack; |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1779 | ifs->skip_elses = pfile->state.skipping || !skip; |
| 1780 | ifs->was_skipping = pfile->state.skipping; |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1781 | ifs->type = type; |
Neil Booth | 6d18adb | 2001-07-29 17:27:57 +0000 | [diff] [blame] | 1782 | /* This condition is effectively a test for top-of-file. */ |
| 1783 | if (pfile->mi_valid && pfile->mi_cmacro == 0) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1784 | ifs->mi_cmacro = cmacro; |
| 1785 | else |
| 1786 | ifs->mi_cmacro = 0; |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1787 | |
Neil Booth | cef0d19 | 2001-07-26 06:02:47 +0000 | [diff] [blame] | 1788 | pfile->state.skipping = skip; |
Neil Booth | b528a07 | 2000-11-12 11:46:21 +0000 | [diff] [blame] | 1789 | buffer->if_stack = ifs; |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 1792 | /* Read the tokens of the answer into the macro pool, in a directive |
| 1793 | of type TYPE. Only commit the memory if we intend it as permanent |
| 1794 | storage, i.e. the #assert case. Returns 0 on success, and sets |
| 1795 | ANSWERP to point to the answer. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1796 | static int |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1797 | parse_answer (cpp_reader *pfile, struct answer **answerp, int type) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1798 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1799 | const cpp_token *paren; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1800 | struct answer *answer; |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1801 | unsigned int acount; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1802 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1803 | /* In a conditional, it is legal to not have an open paren. We |
| 1804 | should save the following token in this case. */ |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1805 | paren = cpp_get_token (pfile); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1806 | |
| 1807 | /* If not a paren, see if we're OK. */ |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1808 | if (paren->type != CPP_OPEN_PAREN) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1809 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1810 | /* In a conditional no answer is a test for any answer. It |
| 1811 | could be followed by any token. */ |
| 1812 | if (type == T_IF) |
Neil Booth | bdcbe49 | 2001-09-13 20:05:17 +0000 | [diff] [blame] | 1813 | { |
| 1814 | _cpp_backup_tokens (pfile, 1); |
| 1815 | return 0; |
| 1816 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1817 | |
| 1818 | /* #unassert with no answer is valid - it removes all answers. */ |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1819 | if (type == T_UNASSERT && paren->type == CPP_EOF) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1820 | return 0; |
| 1821 | |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1822 | cpp_error (pfile, CPP_DL_ERROR, "missing '(' after predicate"); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1823 | return 1; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1824 | } |
| 1825 | |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1826 | for (acount = 0;; acount++) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1827 | { |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1828 | size_t room_needed; |
| 1829 | const cpp_token *token = cpp_get_token (pfile); |
| 1830 | cpp_token *dest; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1831 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1832 | if (token->type == CPP_CLOSE_PAREN) |
| 1833 | break; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1834 | |
| 1835 | if (token->type == CPP_EOF) |
| 1836 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1837 | cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer"); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1838 | return 1; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1839 | } |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1840 | |
| 1841 | /* struct answer includes the space for one token. */ |
| 1842 | room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token)); |
| 1843 | |
| 1844 | if (BUFF_ROOM (pfile->a_buff) < room_needed) |
| 1845 | _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer)); |
| 1846 | |
| 1847 | dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount]; |
| 1848 | *dest = *token; |
| 1849 | |
| 1850 | /* Drop whitespace at start, for answer equivalence purposes. */ |
| 1851 | if (acount == 0) |
| 1852 | dest->flags &= ~PREV_WHITE; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1853 | } |
| 1854 | |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1855 | if (acount == 0) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1856 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1857 | cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty"); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1858 | return 1; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1861 | answer = (struct answer *) BUFF_FRONT (pfile->a_buff); |
| 1862 | answer->count = acount; |
| 1863 | answer->next = NULL; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1864 | *answerp = answer; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1865 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1866 | return 0; |
| 1867 | } |
| 1868 | |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 1869 | /* Parses an assertion directive of type TYPE, returning a pointer to |
| 1870 | the hash node of the predicate, or 0 on error. If an answer was |
| 1871 | supplied, it is placed in ANSWERP, otherwise it is set to 0. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1872 | static cpp_hashnode * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1873 | parse_assertion (cpp_reader *pfile, struct answer **answerp, int type) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1874 | { |
| 1875 | cpp_hashnode *result = 0; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1876 | const cpp_token *predicate; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1877 | |
| 1878 | /* We don't expand predicates or answers. */ |
| 1879 | pfile->state.prevent_expansion++; |
| 1880 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1881 | *answerp = 0; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1882 | predicate = cpp_get_token (pfile); |
| 1883 | if (predicate->type == CPP_EOF) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1884 | cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate"); |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1885 | else if (predicate->type != CPP_NAME) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1886 | cpp_error (pfile, CPP_DL_ERROR, "predicate must be an identifier"); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1887 | else if (parse_answer (pfile, answerp, type) == 0) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1888 | { |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1889 | unsigned int len = NODE_LEN (predicate->val.node); |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 1890 | unsigned char *sym = (unsigned char *) alloca (len + 1); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1891 | |
| 1892 | /* Prefix '#' to get it out of macro namespace. */ |
| 1893 | sym[0] = '#'; |
Neil Booth | 4ed5bcf | 2001-09-24 22:53:12 +0000 | [diff] [blame] | 1894 | memcpy (sym + 1, NODE_NAME (predicate->val.node), len); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1895 | result = cpp_lookup (pfile, sym, len + 1); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1898 | pfile->state.prevent_expansion--; |
| 1899 | return result; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 1902 | /* Returns a pointer to the pointer to CANDIDATE in the answer chain, |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1903 | or a pointer to NULL if the answer is not in the chain. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1904 | static struct answer ** |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1905 | find_answer (cpp_hashnode *node, const struct answer *candidate) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1906 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1907 | unsigned int i; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1908 | struct answer **result; |
| 1909 | |
| 1910 | for (result = &node->value.answers; *result; result = &(*result)->next) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1911 | { |
| 1912 | struct answer *answer = *result; |
| 1913 | |
| 1914 | if (answer->count == candidate->count) |
| 1915 | { |
| 1916 | for (i = 0; i < answer->count; i++) |
| 1917 | if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i])) |
| 1918 | break; |
| 1919 | |
| 1920 | if (i == answer->count) |
| 1921 | break; |
| 1922 | } |
| 1923 | } |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1924 | |
| 1925 | return result; |
| 1926 | } |
| 1927 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1928 | /* Test an assertion within a preprocessor conditional. Returns |
Kazu Hirata | da7d830 | 2002-09-22 02:03:17 +0000 | [diff] [blame] | 1929 | nonzero on failure, zero on success. On success, the result of |
Hans-Peter Nilsson | 2402645 | 2002-11-29 22:41:04 +0000 | [diff] [blame] | 1930 | the test is written into VALUE, otherwise the value 0. */ |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1931 | int |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1932 | _cpp_test_assertion (cpp_reader *pfile, unsigned int *value) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1933 | { |
| 1934 | struct answer *answer; |
| 1935 | cpp_hashnode *node; |
| 1936 | |
| 1937 | node = parse_assertion (pfile, &answer, T_IF); |
Hans-Peter Nilsson | 2402645 | 2002-11-29 22:41:04 +0000 | [diff] [blame] | 1938 | |
| 1939 | /* For recovery, an erroneous assertion expression is handled as a |
| 1940 | failing assertion. */ |
| 1941 | *value = 0; |
| 1942 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1943 | if (node) |
| 1944 | *value = (node->type == NT_ASSERTION && |
| 1945 | (answer == 0 || *find_answer (node, answer) != 0)); |
Neil Booth | 9131890 | 2002-05-26 18:42:21 +0000 | [diff] [blame] | 1946 | else if (pfile->cur_token[-1].type == CPP_EOF) |
| 1947 | _cpp_backup_tokens (pfile, 1); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1948 | |
| 1949 | /* We don't commit the memory for the answer - it's temporary only. */ |
| 1950 | return node == 0; |
| 1951 | } |
| 1952 | |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 1953 | /* Handle #assert. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1954 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 1955 | do_assert (cpp_reader *pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1956 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1957 | struct answer *new_answer; |
| 1958 | cpp_hashnode *node; |
Kazu Hirata | df38348 | 2002-05-22 22:02:16 +0000 | [diff] [blame] | 1959 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1960 | node = parse_assertion (pfile, &new_answer, T_ASSERT); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1961 | if (node) |
| 1962 | { |
Geoffrey Keating | d804416 | 2004-06-09 20:10:13 +0000 | [diff] [blame] | 1963 | size_t answer_size; |
| 1964 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1965 | /* Place the new answer in the answer list. First check there |
| 1966 | is not a duplicate. */ |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1967 | new_answer->next = 0; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1968 | if (node->type == NT_ASSERTION) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1969 | { |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1970 | if (*find_answer (node, new_answer)) |
| 1971 | { |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 1972 | cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted", |
Neil Booth | ebef4e8 | 2002-04-14 18:42:47 +0000 | [diff] [blame] | 1973 | NODE_NAME (node) + 1); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1974 | return; |
| 1975 | } |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1976 | new_answer->next = node->value.answers; |
| 1977 | } |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1978 | |
Geoffrey Keating | d804416 | 2004-06-09 20:10:13 +0000 | [diff] [blame] | 1979 | answer_size = sizeof (struct answer) + ((new_answer->count - 1) |
| 1980 | * sizeof (cpp_token)); |
| 1981 | /* Commit or allocate storage for the object. */ |
| 1982 | if (pfile->hash_table->alloc_subobject) |
| 1983 | { |
| 1984 | struct answer *temp_answer = new_answer; |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 1985 | new_answer = (struct answer *) pfile->hash_table->alloc_subobject |
| 1986 | (answer_size); |
Geoffrey Keating | d804416 | 2004-06-09 20:10:13 +0000 | [diff] [blame] | 1987 | memcpy (new_answer, temp_answer, answer_size); |
| 1988 | } |
| 1989 | else |
| 1990 | BUFF_FRONT (pfile->a_buff) += answer_size; |
| 1991 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 1992 | node->type = NT_ASSERTION; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1993 | node->value.answers = new_answer; |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 1994 | check_eol (pfile); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1995 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1996 | } |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1997 | |
Neil Booth | 5d8ebbd | 2002-01-03 21:43:09 +0000 | [diff] [blame] | 1998 | /* Handle #unassert. */ |
Zack Weinberg | 711b882 | 2000-07-18 00:59:49 +0000 | [diff] [blame] | 1999 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2000 | do_unassert (cpp_reader *pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 2001 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 2002 | cpp_hashnode *node; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2003 | struct answer *answer; |
Kazu Hirata | df38348 | 2002-05-22 22:02:16 +0000 | [diff] [blame] | 2004 | |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2005 | node = parse_assertion (pfile, &answer, T_UNASSERT); |
| 2006 | /* It isn't an error to #unassert something that isn't asserted. */ |
| 2007 | if (node && node->type == NT_ASSERTION) |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 2008 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 2009 | if (answer) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2010 | { |
| 2011 | struct answer **p = find_answer (node, answer), *temp; |
| 2012 | |
| 2013 | /* Remove the answer from the list. */ |
| 2014 | temp = *p; |
| 2015 | if (temp) |
| 2016 | *p = temp->next; |
| 2017 | |
| 2018 | /* Did we free the last answer? */ |
| 2019 | if (node->value.answers == 0) |
| 2020 | node->type = NT_VOID; |
Neil Booth | 8c3b269 | 2001-09-30 10:03:11 +0000 | [diff] [blame] | 2021 | |
| 2022 | check_eol (pfile); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2023 | } |
| 2024 | else |
| 2025 | _cpp_free_definition (node); |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 2026 | } |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2027 | |
| 2028 | /* We don't commit the memory for the answer - it's temporary only. */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 2029 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 2030 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2031 | /* These are for -D, -U, -A. */ |
| 2032 | |
| 2033 | /* Process the string STR as if it appeared as the body of a #define. |
| 2034 | If STR is just an identifier, define it with value 1. |
| 2035 | If STR has anything after the identifier, then it should |
Kazu Hirata | ec5c56d | 2001-08-01 17:57:27 +0000 | [diff] [blame] | 2036 | be identifier=definition. */ |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2037 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2038 | cpp_define (cpp_reader *pfile, const char *str) |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2039 | { |
| 2040 | char *buf, *p; |
| 2041 | size_t count; |
| 2042 | |
Kazu Hirata | df38348 | 2002-05-22 22:02:16 +0000 | [diff] [blame] | 2043 | /* Copy the entire option so we can modify it. |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2044 | Change the first "=" in the string to a space. If there is none, |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2045 | tack " 1" on the end. */ |
| 2046 | |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2047 | count = strlen (str); |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 2048 | buf = (char *) alloca (count + 3); |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2049 | memcpy (buf, str, count); |
| 2050 | |
| 2051 | p = strchr (str, '='); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2052 | if (p) |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2053 | buf[p - str] = ' '; |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2054 | else |
| 2055 | { |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2056 | buf[count++] = ' '; |
| 2057 | buf[count++] = '1'; |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2058 | } |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 2059 | buf[count] = '\n'; |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2060 | |
Neil Booth | 29401c3 | 2001-08-22 20:37:20 +0000 | [diff] [blame] | 2061 | run_directive (pfile, T_DEFINE, buf, count); |
Zack Weinberg | 2c8f051 | 2000-08-29 18:37:37 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
Neil Booth | ad2a084 | 2000-12-17 00:13:54 +0000 | [diff] [blame] | 2064 | /* Slight variant of the above for use by initialize_builtins. */ |
Zack Weinberg | 2c8f051 | 2000-08-29 18:37:37 +0000 | [diff] [blame] | 2065 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2066 | _cpp_define_builtin (cpp_reader *pfile, const char *str) |
Zack Weinberg | 2c8f051 | 2000-08-29 18:37:37 +0000 | [diff] [blame] | 2067 | { |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 2068 | size_t len = strlen (str); |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 2069 | char *buf = (char *) alloca (len + 1); |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 2070 | memcpy (buf, str, len); |
| 2071 | buf[len] = '\n'; |
| 2072 | run_directive (pfile, T_DEFINE, buf, len); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2073 | } |
| 2074 | |
| 2075 | /* Process MACRO as if it appeared as the body of an #undef. */ |
| 2076 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2077 | cpp_undef (cpp_reader *pfile, const char *macro) |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2078 | { |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 2079 | size_t len = strlen (macro); |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 2080 | char *buf = (char *) alloca (len + 1); |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 2081 | memcpy (buf, macro, len); |
| 2082 | buf[len] = '\n'; |
| 2083 | run_directive (pfile, T_UNDEF, buf, len); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
Richard Henderson | 121de39 | 2007-03-30 14:12:53 -0700 | [diff] [blame] | 2086 | /* Like lex_macro_node, but read the input from STR. */ |
| 2087 | static cpp_hashnode * |
| 2088 | lex_macro_node_from_str (cpp_reader *pfile, const char *str) |
| 2089 | { |
| 2090 | size_t len = strlen (str); |
Michael Meissner | 4cd9707 | 2007-03-30 22:40:19 +0000 | [diff] [blame] | 2091 | uchar *buf = (uchar *) alloca (len + 1); |
Richard Henderson | 121de39 | 2007-03-30 14:12:53 -0700 | [diff] [blame] | 2092 | cpp_hashnode *node; |
| 2093 | |
| 2094 | memcpy (buf, str, len); |
| 2095 | buf[len] = '\n'; |
| 2096 | cpp_push_buffer (pfile, buf, len, true); |
| 2097 | node = lex_macro_node (pfile, true); |
| 2098 | _cpp_pop_buffer (pfile); |
| 2099 | |
| 2100 | return node; |
| 2101 | } |
| 2102 | |
| 2103 | /* If STR is a defined macro, return its definition node, else return NULL. */ |
| 2104 | cpp_macro * |
| 2105 | cpp_push_definition (cpp_reader *pfile, const char *str) |
| 2106 | { |
| 2107 | cpp_hashnode *node = lex_macro_node_from_str (pfile, str); |
| 2108 | if (node && node->type == NT_MACRO) |
| 2109 | return node->value.macro; |
| 2110 | else |
| 2111 | return NULL; |
| 2112 | } |
| 2113 | |
| 2114 | /* Replace a previous definition DFN of the macro STR. If DFN is NULL, |
| 2115 | then the macro should be undefined. */ |
| 2116 | void |
| 2117 | cpp_pop_definition (cpp_reader *pfile, const char *str, cpp_macro *dfn) |
| 2118 | { |
| 2119 | cpp_hashnode *node = lex_macro_node_from_str (pfile, str); |
| 2120 | if (node == NULL) |
| 2121 | return; |
| 2122 | |
| 2123 | if (node->type == NT_MACRO) |
| 2124 | { |
| 2125 | if (pfile->cb.undef) |
| 2126 | pfile->cb.undef (pfile, pfile->directive_line, node); |
| 2127 | if (CPP_OPTION (pfile, warn_unused_macros)) |
| 2128 | _cpp_warn_if_unused_macro (pfile, node, NULL); |
| 2129 | } |
| 2130 | if (node->type != NT_VOID) |
| 2131 | _cpp_free_definition (node); |
| 2132 | |
| 2133 | if (dfn) |
| 2134 | { |
| 2135 | node->type = NT_MACRO; |
| 2136 | node->value.macro = dfn; |
| 2137 | if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_"))) |
| 2138 | node->flags |= NODE_WARN; |
| 2139 | |
| 2140 | if (pfile->cb.define) |
| 2141 | pfile->cb.define (pfile, pfile->directive_line, node); |
| 2142 | } |
| 2143 | } |
| 2144 | |
Kazu Hirata | ec5c56d | 2001-08-01 17:57:27 +0000 | [diff] [blame] | 2145 | /* Process the string STR as if it appeared as the body of a #assert. */ |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2146 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2147 | cpp_assert (cpp_reader *pfile, const char *str) |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2148 | { |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2149 | handle_assertion (pfile, str, T_ASSERT); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 2150 | } |
| 2151 | |
Kazu Hirata | ec5c56d | 2001-08-01 17:57:27 +0000 | [diff] [blame] | 2152 | /* Process STR as if it appeared as the body of an #unassert. */ |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 2153 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2154 | cpp_unassert (cpp_reader *pfile, const char *str) |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 2155 | { |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2156 | handle_assertion (pfile, str, T_UNASSERT); |
Kazu Hirata | df38348 | 2002-05-22 22:02:16 +0000 | [diff] [blame] | 2157 | } |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 2158 | |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2159 | /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */ |
| 2160 | static void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2161 | handle_assertion (cpp_reader *pfile, const char *str, int type) |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2162 | { |
| 2163 | size_t count = strlen (str); |
| 2164 | const char *p = strchr (str, '='); |
| 2165 | |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 2166 | /* Copy the entire option so we can modify it. Change the first |
| 2167 | "=" in the string to a '(', and tack a ')' on the end. */ |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 2168 | char *buf = (char *) alloca (count + 2); |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 2169 | |
| 2170 | memcpy (buf, str, count); |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2171 | if (p) |
| 2172 | { |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2173 | buf[p - str] = '('; |
| 2174 | buf[count++] = ')'; |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2175 | } |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 2176 | buf[count] = '\n'; |
| 2177 | str = buf; |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2178 | |
Neil Booth | 29401c3 | 2001-08-22 20:37:20 +0000 | [diff] [blame] | 2179 | run_directive (pfile, type, str, count); |
Neil Booth | 8636812 | 2000-10-31 23:34:59 +0000 | [diff] [blame] | 2180 | } |
| 2181 | |
Neil Booth | 7e96d76 | 2001-01-13 01:00:01 +0000 | [diff] [blame] | 2182 | /* The number of errors for a given reader. */ |
| 2183 | unsigned int |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2184 | cpp_errors (cpp_reader *pfile) |
Neil Booth | 7e96d76 | 2001-01-13 01:00:01 +0000 | [diff] [blame] | 2185 | { |
| 2186 | return pfile->errors; |
| 2187 | } |
| 2188 | |
| 2189 | /* The options structure. */ |
| 2190 | cpp_options * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2191 | cpp_get_options (cpp_reader *pfile) |
Neil Booth | 7e96d76 | 2001-01-13 01:00:01 +0000 | [diff] [blame] | 2192 | { |
| 2193 | return &pfile->opts; |
| 2194 | } |
| 2195 | |
| 2196 | /* The callbacks structure. */ |
| 2197 | cpp_callbacks * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2198 | cpp_get_callbacks (cpp_reader *pfile) |
Neil Booth | 7e96d76 | 2001-01-13 01:00:01 +0000 | [diff] [blame] | 2199 | { |
| 2200 | return &pfile->cb; |
| 2201 | } |
| 2202 | |
| 2203 | /* Copy the given callbacks structure to our own. */ |
| 2204 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2205 | cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb) |
Neil Booth | 7e96d76 | 2001-01-13 01:00:01 +0000 | [diff] [blame] | 2206 | { |
| 2207 | pfile->cb = *cb; |
| 2208 | } |
| 2209 | |
Zack Weinberg | c6e8380 | 2004-06-05 20:58:06 +0000 | [diff] [blame] | 2210 | /* The dependencies structure. (Creates one if it hasn't already been.) */ |
| 2211 | struct deps * |
| 2212 | cpp_get_deps (cpp_reader *pfile) |
| 2213 | { |
| 2214 | if (!pfile->deps) |
| 2215 | pfile->deps = deps_init (); |
| 2216 | return pfile->deps; |
| 2217 | } |
| 2218 | |
Neil Booth | eb1f4d9 | 2000-12-18 19:00:26 +0000 | [diff] [blame] | 2219 | /* Push a new buffer on the buffer stack. Returns the new buffer; it |
| 2220 | doesn't fail. It does not generate a file change call back; that |
| 2221 | is the responsibility of the caller. */ |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 2222 | cpp_buffer * |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2223 | cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len, |
Per Bothner | 40de9f7 | 2003-10-02 07:30:34 +0000 | [diff] [blame] | 2224 | int from_stage3) |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 2225 | { |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 2226 | cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer); |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2227 | |
Neil Booth | fde8434 | 2001-08-06 21:07:41 +0000 | [diff] [blame] | 2228 | /* Clears, amongst other things, if_stack and mi_cmacro. */ |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 2229 | memset (new_buffer, 0, sizeof (cpp_buffer)); |
Neil Booth | ad2a084 | 2000-12-17 00:13:54 +0000 | [diff] [blame] | 2230 | |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 2231 | new_buffer->next_line = new_buffer->buf = buffer; |
| 2232 | new_buffer->rlimit = buffer + len; |
| 2233 | new_buffer->from_stage3 = from_stage3; |
| 2234 | new_buffer->prev = pfile->buffer; |
| 2235 | new_buffer->need_line = true; |
Neil Booth | 0bda476 | 2000-12-11 07:45:16 +0000 | [diff] [blame] | 2236 | |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 2237 | pfile->buffer = new_buffer; |
Eric Christopher | cf551fb | 2004-01-16 22:37:49 +0000 | [diff] [blame] | 2238 | |
Gabriel Dos Reis | c3f829c | 2005-05-28 15:52:48 +0000 | [diff] [blame] | 2239 | return new_buffer; |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 2240 | } |
| 2241 | |
Neil Booth | af0d16c | 2002-04-22 17:48:02 +0000 | [diff] [blame] | 2242 | /* Pops a single buffer, with a file change call-back if appropriate. |
| 2243 | Then pushes the next -include file, if any remain. */ |
Neil Booth | ef6e958 | 2001-08-04 12:01:59 +0000 | [diff] [blame] | 2244 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2245 | _cpp_pop_buffer (cpp_reader *pfile) |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 2246 | { |
Neil Booth | fde8434 | 2001-08-06 21:07:41 +0000 | [diff] [blame] | 2247 | cpp_buffer *buffer = pfile->buffer; |
Neil Booth | 8f9b400 | 2003-07-29 22:26:13 +0000 | [diff] [blame] | 2248 | struct _cpp_file *inc = buffer->file; |
Neil Booth | ad2a084 | 2000-12-17 00:13:54 +0000 | [diff] [blame] | 2249 | struct if_stack *ifs; |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 2250 | |
Neil Booth | fde8434 | 2001-08-06 21:07:41 +0000 | [diff] [blame] | 2251 | /* Walk back up the conditional stack till we reach its level at |
| 2252 | entry to this file, issuing error messages. */ |
| 2253 | for (ifs = buffer->if_stack; ifs; ifs = ifs->next) |
John David Anglin | 0527bc4 | 2003-11-01 22:56:54 +0000 | [diff] [blame] | 2254 | cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, |
Neil Booth | fde8434 | 2001-08-06 21:07:41 +0000 | [diff] [blame] | 2255 | "unterminated #%s", dtable[ifs->type].name); |
| 2256 | |
Neil Booth | 9729389 | 2001-09-14 22:04:46 +0000 | [diff] [blame] | 2257 | /* In case of a missing #endif. */ |
Neil Booth | 67821e3 | 2001-08-05 17:31:25 +0000 | [diff] [blame] | 2258 | pfile->state.skipping = 0; |
Neil Booth | 29401c3 | 2001-08-22 20:37:20 +0000 | [diff] [blame] | 2259 | |
Neil Booth | af0d16c | 2002-04-22 17:48:02 +0000 | [diff] [blame] | 2260 | /* _cpp_do_file_change expects pfile->buffer to be the new one. */ |
Neil Booth | 29401c3 | 2001-08-22 20:37:20 +0000 | [diff] [blame] | 2261 | pfile->buffer = buffer->prev; |
| 2262 | |
Neil Booth | 26aea07 | 2003-04-19 00:22:51 +0000 | [diff] [blame] | 2263 | free (buffer->notes); |
| 2264 | |
Neil Booth | af0d16c | 2002-04-22 17:48:02 +0000 | [diff] [blame] | 2265 | /* Free the buffer object now; we may want to push a new buffer |
| 2266 | in _cpp_push_next_include_file. */ |
| 2267 | obstack_free (&pfile->buffer_ob, buffer); |
Neil Booth | 29401c3 | 2001-08-22 20:37:20 +0000 | [diff] [blame] | 2268 | |
Neil Booth | af0d16c | 2002-04-22 17:48:02 +0000 | [diff] [blame] | 2269 | if (inc) |
| 2270 | { |
| 2271 | _cpp_pop_file_buffer (pfile, inc); |
| 2272 | |
Per Bothner | 40de9f7 | 2003-10-02 07:30:34 +0000 | [diff] [blame] | 2273 | _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0); |
Neil Booth | af0d16c | 2002-04-22 17:48:02 +0000 | [diff] [blame] | 2274 | } |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
Kazu Hirata | 05713b8 | 2002-09-15 18:24:08 +0000 | [diff] [blame] | 2277 | /* Enter all recognized directives in the hash table. */ |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 2278 | void |
Zack Weinberg | 6cf87ca | 2003-06-17 06:17:44 +0000 | [diff] [blame] | 2279 | _cpp_init_directives (cpp_reader *pfile) |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 2280 | { |
Neil Booth | 766ee68 | 2001-01-29 19:20:12 +0000 | [diff] [blame] | 2281 | unsigned int i; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2282 | cpp_hashnode *node; |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame] | 2283 | |
John David Anglin | 37b8524 | 2001-03-02 01:11:50 +0000 | [diff] [blame] | 2284 | for (i = 0; i < (unsigned int) N_DIRECTIVES; i++) |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2285 | { |
Neil Booth | 766ee68 | 2001-01-29 19:20:12 +0000 | [diff] [blame] | 2286 | node = cpp_lookup (pfile, dtable[i].name, dtable[i].length); |
Zack Weinberg | 4977bab | 2002-12-16 18:23:00 +0000 | [diff] [blame] | 2287 | node->is_directive = 1; |
| 2288 | node->directive_index = i; |
Neil Booth | 93c80368 | 2000-10-28 17:59:06 +0000 | [diff] [blame] | 2289 | } |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 2290 | } |