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