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