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