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