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