Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1 | /* CPP Library. |
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, |
| 3 | 1999, 2000 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" |
Jeffrey A Law | ab87f8c | 1999-01-27 01:43:17 +0000 | [diff] [blame] | 27 | #include "intl.h" |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 28 | #include "obstack.h" |
Zack Weinberg | 07aa0b0 | 2000-04-01 22:55:25 +0000 | [diff] [blame] | 29 | #include "symcat.h" |
Jeff Law | 956d695 | 1997-12-06 17:31:01 -0700 | [diff] [blame] | 30 | |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 31 | #ifdef HAVE_MMAP_FILE |
| 32 | # include <sys/mman.h> |
| 33 | #endif |
| 34 | |
Zack Weinberg | 88ae23e | 2000-03-08 23:35:19 +0000 | [diff] [blame] | 35 | /* Stack of conditionals currently in progress |
| 36 | (including both successful and failing conditionals). */ |
| 37 | |
| 38 | struct if_stack |
| 39 | { |
| 40 | struct if_stack *next; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 41 | unsigned int lineno; /* line number where condition started */ |
| 42 | unsigned int colno; /* and column */ |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 43 | int was_skipping; /* value of pfile->skipping before this if */ |
| 44 | const cpp_hashnode *cmacro; /* macro name for #ifndef around entire file */ |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 45 | int type; /* type of last directive seen in this group */ |
Zack Weinberg | 88ae23e | 2000-03-08 23:35:19 +0000 | [diff] [blame] | 46 | }; |
Zack Weinberg | 88ae23e | 2000-03-08 23:35:19 +0000 | [diff] [blame] | 47 | |
Zack Weinberg | 1316f1f | 2000-02-06 07:53:50 +0000 | [diff] [blame] | 48 | /* Forward declarations. */ |
| 49 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 50 | static void validate_else PARAMS ((cpp_reader *, const U_CHAR *)); |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 51 | static int parse_include PARAMS ((cpp_reader *, const U_CHAR *, int, |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 52 | const U_CHAR **, unsigned int *, |
| 53 | int *)); |
| 54 | static void push_conditional PARAMS ((cpp_reader *, int, int, |
| 55 | const cpp_hashnode *)); |
| 56 | static void pass_thru_directive PARAMS ((cpp_reader *)); |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 57 | static int read_line_number PARAMS ((cpp_reader *, int *)); |
| 58 | static int strtoul_for_line PARAMS ((const U_CHAR *, unsigned int, |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 59 | unsigned long *)); |
| 60 | |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 61 | static const cpp_hashnode * |
| 62 | parse_ifdef PARAMS ((cpp_reader *, const U_CHAR *)); |
| 63 | static const cpp_hashnode * |
| 64 | detect_if_not_defined PARAMS ((cpp_reader *)); |
| 65 | static cpp_hashnode * |
| 66 | get_define_node PARAMS ((cpp_reader *)); |
| 67 | static void dump_macro_name PARAMS ((cpp_reader *, cpp_hashnode *)); |
| 68 | static void unwind_if_stack PARAMS ((cpp_reader *, cpp_buffer *)); |
Kaveh R. Ghazi | 487a6e0 | 1998-05-19 08:42:48 +0000 | [diff] [blame] | 69 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 70 | /* Utility. */ |
| 71 | #define str_match(sym, len, str) \ |
| 72 | ((len) == (sizeof (str) - 1) && !ustrncmp ((sym), U(str), sizeof (str) - 1)) |
Zack Weinberg | 07aa0b0 | 2000-04-01 22:55:25 +0000 | [diff] [blame] | 73 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 74 | /* This is the table of directive handlers. It is ordered by |
| 75 | frequency of occurrence; the numbers at the end are directive |
| 76 | counts from all the source code I have lying around (egcs and libc |
| 77 | CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and |
| 78 | pcmcia-cs-3.0.9). |
Jeff Law | e9a25f7 | 1997-11-02 14:19:36 -0700 | [diff] [blame] | 79 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 80 | The entries with a dash and a name after the count are extensions, |
| 81 | of which all but #warning and #include_next are deprecated. The name |
| 82 | is where the extension appears to have come from. */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 83 | |
Zack Weinberg | 07aa0b0 | 2000-04-01 22:55:25 +0000 | [diff] [blame] | 84 | /* #sccs is not always recognized. */ |
| 85 | #ifdef SCCS_DIRECTIVE |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 86 | # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0) |
Zack Weinberg | 07aa0b0 | 2000-04-01 22:55:25 +0000 | [diff] [blame] | 87 | #else |
| 88 | # define SCCS_ENTRY /* nothing */ |
| 89 | #endif |
| 90 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 91 | #define DIRECTIVE_TABLE \ |
| 92 | D(define, T_DEFINE = 0, KANDR, COMMENTS) /* 270554 */ \ |
| 93 | D(include, T_INCLUDE, KANDR, EXPAND | INCL) /* 52262 */ \ |
| 94 | D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \ |
| 95 | D(ifdef, T_IFDEF, KANDR, COND) /* 22000 */ \ |
| 96 | D(if, T_IF, KANDR, COND | EXPAND) /* 18162 */ \ |
| 97 | D(else, T_ELSE, KANDR, COND) /* 9863 */ \ |
| 98 | D(ifndef, T_IFNDEF, KANDR, COND) /* 9675 */ \ |
| 99 | D(undef, T_UNDEF, KANDR, 0) /* 4837 */ \ |
| 100 | D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \ |
| 101 | D(elif, T_ELIF, KANDR, COND | EXPAND) /* 610 */ \ |
| 102 | D(error, T_ERROR, STDC89, 0) /* 475 */ \ |
| 103 | D(pragma, T_PRAGMA, STDC89, 0) /* 195 */ \ |
| 104 | D(warning, T_WARNING, EXTENSION, 0) /* 22 GNU */ \ |
| 105 | D(include_next, T_INCLUDE_NEXT, EXTENSION, EXPAND | INCL) /* 19 GNU */ \ |
| 106 | D(ident, T_IDENT, EXTENSION, 0) /* 11 SVR4 */ \ |
| 107 | D(import, T_IMPORT, EXTENSION, EXPAND | INCL) /* 0 ObjC */ \ |
| 108 | D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \ |
| 109 | D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \ |
| 110 | SCCS_ENTRY /* 0 SVR2? */ |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 111 | |
| 112 | /* Use the table to generate a series of prototypes, an enum for the |
| 113 | directive names, and an array of directive handlers. */ |
| 114 | |
| 115 | /* The directive-processing functions are declared to return int |
| 116 | instead of void, because some old compilers have trouble with |
| 117 | pointers to functions returning void. */ |
| 118 | |
Kaveh R. Ghazi | 7c32404 | 2000-04-04 21:07:17 +0000 | [diff] [blame] | 119 | /* 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] | 120 | #define D(name, t, o, f) static int CONCAT2(do_,name) PARAMS ((cpp_reader *)); |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 121 | DIRECTIVE_TABLE |
| 122 | #undef D |
| 123 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 124 | #define D(n, tag, o, f) tag, |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 125 | enum |
| 126 | { |
| 127 | DIRECTIVE_TABLE |
| 128 | N_DIRECTIVES |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 129 | }; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 130 | #undef D |
| 131 | |
Kaveh R. Ghazi | 7c32404 | 2000-04-04 21:07:17 +0000 | [diff] [blame] | 132 | /* 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] | 133 | #define D(name, t, origin, flags) \ |
Zack Weinberg | 12cf91f | 2000-05-04 04:38:01 +0000 | [diff] [blame] | 134 | { CONCAT2(do_,name), (const U_CHAR *) STRINGX(name), \ |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 135 | sizeof STRINGX(name) - 1, origin, flags }, |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 136 | static const struct directive dtable[] = |
| 137 | { |
| 138 | DIRECTIVE_TABLE |
| 139 | }; |
| 140 | #undef D |
| 141 | #undef DIRECTIVE_TABLE |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 142 | |
Zack Weinberg | c5a0473 | 2000-04-25 19:32:36 +0000 | [diff] [blame] | 143 | /* Check if a token's name matches that of a known directive. Put in |
| 144 | this file to save exporting dtable and other unneeded information. */ |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 145 | const struct directive * |
| 146 | _cpp_check_directive (pfile, token, bol) |
| 147 | cpp_reader *pfile; |
| 148 | const cpp_token *token; |
| 149 | int bol; |
Zack Weinberg | c5a0473 | 2000-04-25 19:32:36 +0000 | [diff] [blame] | 150 | { |
Zack Weinberg | c5a0473 | 2000-04-25 19:32:36 +0000 | [diff] [blame] | 151 | unsigned int i; |
| 152 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 153 | /* If we are rescanning preprocessed input, don't obey any directives |
| 154 | other than # nnn. */ |
| 155 | if (CPP_OPTION (pfile, preprocessed)) |
| 156 | return 0; |
Zack Weinberg | c5a0473 | 2000-04-25 19:32:36 +0000 | [diff] [blame] | 157 | |
| 158 | for (i = 0; i < N_DIRECTIVES; i++) |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 159 | if (pfile->spec_nodes->dirs[i] == token->val.node) |
Zack Weinberg | c5a0473 | 2000-04-25 19:32:36 +0000 | [diff] [blame] | 160 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 161 | /* If we are skipping a failed conditional group, all non-conditional |
| 162 | directives are ignored. */ |
| 163 | if (pfile->skipping && !(dtable[i].flags & COND)) |
| 164 | return 0; |
| 165 | |
| 166 | /* In -traditional mode, a directive is ignored unless its # |
| 167 | is in column 1. */ |
| 168 | if (!bol && dtable[i].origin == KANDR && CPP_WTRADITIONAL (pfile)) |
| 169 | cpp_warning (pfile, "traditional C ignores #%s with the # indented", |
| 170 | dtable[i].name); |
| 171 | |
| 172 | if (!bol && CPP_TRADITIONAL (pfile)) |
| 173 | return 0; |
| 174 | |
| 175 | /* Issue -pedantic warnings for extended directives. */ |
| 176 | if (CPP_PEDANTIC (pfile) && dtable[i].origin == EXTENSION) |
| 177 | cpp_pedwarn (pfile, "ISO C does not allow #%s", dtable[i].name); |
| 178 | |
| 179 | /* -Wtraditional gives warnings about directives with inappropriate |
| 180 | indentation of #. */ |
| 181 | if (bol && dtable[i].origin != KANDR && CPP_WTRADITIONAL (pfile)) |
| 182 | cpp_warning (pfile, |
| 183 | "suggest hiding #%s from traditional C with an indented #", |
| 184 | dtable[i].name); |
| 185 | |
| 186 | return &dtable[i]; |
Zack Weinberg | c5a0473 | 2000-04-25 19:32:36 +0000 | [diff] [blame] | 187 | } |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 188 | |
| 189 | return 0; |
Zack Weinberg | c5a0473 | 2000-04-25 19:32:36 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 192 | const struct directive * |
| 193 | _cpp_check_linemarker (pfile, token, bol) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 194 | cpp_reader *pfile; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 195 | const cpp_token *token ATTRIBUTE_UNUSED; |
| 196 | int bol; |
Zack Weinberg | 3fdc651 | 1999-03-16 13:10:15 +0000 | [diff] [blame] | 197 | { |
Zack Weinberg | 40c79d5 | 2000-01-12 00:35:36 +0000 | [diff] [blame] | 198 | /* # followed by a number is equivalent to #line. Do not recognize |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 199 | this form in assembly language source files or skipped |
| 200 | conditional groups. Complain about this form if we're being |
| 201 | pedantic, but not if this is regurgitated input (preprocessed or |
| 202 | fed back in by the C++ frontend). */ |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 203 | if (pfile->skipping || CPP_OPTION (pfile, lang_asm)) |
Zack Weinberg | 40c79d5 | 2000-01-12 00:35:36 +0000 | [diff] [blame] | 204 | return 0; |
| 205 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 206 | if (CPP_PEDANTIC (pfile) && CPP_BUFFER (pfile)->inc |
| 207 | && ! CPP_OPTION (pfile, preprocessed)) |
| 208 | cpp_pedwarn (pfile, "# followed by integer"); |
Jeff Law | e9a25f7 | 1997-11-02 14:19:36 -0700 | [diff] [blame] | 209 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 210 | /* In -traditional mode, a directive is ignored unless its # |
| 211 | is in column 1. */ |
| 212 | if (!bol && CPP_WTRADITIONAL (pfile)) |
| 213 | cpp_warning (pfile, "traditional C ignores #%s with the # indented", |
| 214 | dtable[T_LINE].name); |
| 215 | |
| 216 | if (!bol && CPP_TRADITIONAL (pfile)) |
Zack Weinberg | ff2b53e | 2000-04-06 07:56:14 +0000 | [diff] [blame] | 217 | return 0; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 218 | |
| 219 | return &dtable[T_LINE]; |
| 220 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 221 | |
| 222 | static void |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 223 | dump_macro_name (pfile, node) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 224 | cpp_reader *pfile; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 225 | cpp_hashnode *node; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 226 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 227 | CPP_PUTS (pfile, "#define ", sizeof "#define " - 1); |
| 228 | CPP_PUTS (pfile, node->name, node->length); |
| 229 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 230 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 231 | /* Pass the current directive through to the output file. */ |
| 232 | static void |
| 233 | pass_thru_directive (pfile) |
| 234 | cpp_reader *pfile; |
| 235 | { |
| 236 | /* XXX This output may be genuinely needed even when there is no |
| 237 | printer. */ |
| 238 | if (! pfile->printer) |
| 239 | return; |
| 240 | /* Flush first (temporary). */ |
| 241 | cpp_output_tokens (pfile, pfile->printer, pfile->token_list.line); |
| 242 | _cpp_dump_list (pfile, &pfile->token_list, pfile->first_directive_token, 1); |
| 243 | } |
| 244 | |
| 245 | static cpp_hashnode * |
| 246 | get_define_node (pfile) |
| 247 | cpp_reader *pfile; |
| 248 | { |
| 249 | cpp_hashnode *node; |
| 250 | const cpp_token *token; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 251 | |
| 252 | /* Skip any -C comments. */ |
| 253 | while ((token = cpp_get_token (pfile))->type == CPP_COMMENT) |
| 254 | ; |
| 255 | |
| 256 | if (token->type != CPP_NAME) |
| 257 | { |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 258 | cpp_error_with_line (pfile, token->line, token->col, |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 259 | "macro names must be identifiers"); |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | /* That identifier is not allowed to be "defined". See predefined |
| 264 | macro names (6.10.8.4). */ |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 265 | node = token->val.node; |
| 266 | |
| 267 | if (node == pfile->spec_nodes->n_defined) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 268 | { |
| 269 | cpp_error_with_line (pfile, pfile->token_list.line, token->col, |
| 270 | "\"defined\" is not a legal macro name"); |
| 271 | return 0; |
| 272 | } |
| 273 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 274 | /* Check for poisoned identifiers now. */ |
| 275 | if (node->type == T_POISON) |
| 276 | { |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 277 | cpp_error (pfile, "attempt to use poisoned \"%s\"", node->name); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | return node; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 282 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 283 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 284 | /* Process a #define command. */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 285 | static int |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 286 | do_define (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 287 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 288 | { |
Zack Weinberg | f8f769e | 2000-05-28 05:56:38 +0000 | [diff] [blame] | 289 | cpp_hashnode *node; |
Zack Weinberg | ff2b53e | 2000-04-06 07:56:14 +0000 | [diff] [blame] | 290 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 291 | if ((node = get_define_node (pfile))) |
| 292 | if (_cpp_create_definition (pfile, node)) |
| 293 | { |
| 294 | if (CPP_OPTION (pfile, debug_output) |
| 295 | || CPP_OPTION (pfile, dump_macros) == dump_definitions) |
| 296 | _cpp_dump_definition (pfile, node); |
| 297 | else if (CPP_OPTION (pfile, dump_macros) == dump_names) |
| 298 | dump_macro_name (pfile, node); |
| 299 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 300 | return 0; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 301 | } |
| 302 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 303 | /* Remove the definition of a symbol from the symbol table. */ |
| 304 | static int |
| 305 | do_undef (pfile) |
| 306 | cpp_reader *pfile; |
| 307 | { |
| 308 | cpp_hashnode *node = get_define_node (pfile); |
| 309 | |
| 310 | if (cpp_get_token (pfile)->type != CPP_EOF) |
| 311 | cpp_pedwarn (pfile, "junk on line after #undef"); |
| 312 | |
| 313 | /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier |
| 314 | is not currently defined as a macro name. */ |
| 315 | if (node && node->type != T_VOID) |
| 316 | { |
| 317 | /* If we are generating additional info for debugging (with -g) we |
| 318 | need to pass through all effective #undef commands. */ |
| 319 | if (CPP_OPTION (pfile, debug_output) |
| 320 | || CPP_OPTION (pfile, dump_macros) == dump_definitions |
| 321 | || CPP_OPTION (pfile, dump_macros) == dump_names) |
| 322 | pass_thru_directive (pfile); |
| 323 | |
| 324 | if (node->type != T_MACRO) |
| 325 | cpp_warning (pfile, "undefining \"%s\"", node->name); |
| 326 | |
| 327 | _cpp_free_definition (node); |
| 328 | node->type = T_VOID; |
| 329 | } |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 335 | /* Handle #include and #import. */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 336 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 337 | static int |
| 338 | parse_include (pfile, dir, trail, strp, lenp, abp) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 339 | cpp_reader *pfile; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 340 | const U_CHAR *dir; |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 341 | int trail; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 342 | const U_CHAR **strp; |
| 343 | unsigned int *lenp; |
| 344 | int *abp; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 345 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 346 | const cpp_token *name = cpp_get_token (pfile); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 347 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 348 | if (name->type != CPP_STRING && name->type != CPP_HEADER_NAME) |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 349 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 350 | if (name->type == CPP_LESS) |
| 351 | name = _cpp_glue_header_name (pfile); |
| 352 | else |
| 353 | { |
| 354 | cpp_error (pfile, "#%s expects \"FILENAME\" or <FILENAME>", dir); |
| 355 | return 1; |
| 356 | } |
| 357 | } |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 358 | if (name->val.str.len == 0) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 359 | { |
| 360 | cpp_error (pfile, "empty file name in #%s", dir); |
| 361 | return 1; |
Richard Kenner | cfb3ee1 | 1997-04-13 14:30:13 -0400 | [diff] [blame] | 362 | } |
| 363 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 364 | if (!trail && cpp_get_token (pfile)->type != CPP_EOF) |
| 365 | cpp_error (pfile, "junk at end of #%s", dir); |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 366 | |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 367 | *lenp = name->val.str.len; |
| 368 | *strp = name->val.str.text; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 369 | *abp = (name->type == CPP_HEADER_NAME); |
| 370 | return 0; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | static int |
| 374 | do_include (pfile) |
| 375 | cpp_reader *pfile; |
| 376 | { |
| 377 | unsigned int len; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 378 | const U_CHAR *str; |
| 379 | int ab; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 380 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 381 | if (parse_include (pfile, dtable[T_INCLUDE].name, 0, &str, &len, &ab)) |
Zack Weinberg | 29a72a4 | 2000-03-28 21:45:02 +0000 | [diff] [blame] | 382 | return 0; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 383 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 384 | _cpp_execute_include (pfile, str, len, 0, 0, ab); |
| 385 | if (CPP_OPTION (pfile, dump_includes)) |
| 386 | pass_thru_directive (pfile); |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | static int |
| 391 | do_import (pfile) |
| 392 | cpp_reader *pfile; |
| 393 | { |
| 394 | unsigned int len; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 395 | const U_CHAR *str; |
| 396 | int ab; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 397 | |
Zack Weinberg | ae79697 | 2000-03-31 23:16:11 +0000 | [diff] [blame] | 398 | if (CPP_OPTION (pfile, warn_import) |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 399 | && !CPP_IN_SYSTEM_HEADER (pfile) && !pfile->import_warning) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 400 | { |
| 401 | pfile->import_warning = 1; |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 402 | cpp_warning (pfile, |
| 403 | "#import is obsolete, use an #ifndef wrapper in the header file"); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 406 | if (parse_include (pfile, dtable[T_IMPORT].name, 0, &str, &len, &ab)) |
Zack Weinberg | 29a72a4 | 2000-03-28 21:45:02 +0000 | [diff] [blame] | 407 | return 0; |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 408 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 409 | _cpp_execute_include (pfile, str, len, 1, 0, ab); |
| 410 | if (CPP_OPTION (pfile, dump_includes)) |
| 411 | pass_thru_directive (pfile); |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 412 | return 0; |
| 413 | } |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 414 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 415 | static int |
| 416 | do_include_next (pfile) |
| 417 | cpp_reader *pfile; |
| 418 | { |
| 419 | unsigned int len; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 420 | const U_CHAR *str; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 421 | struct file_name_list *search_start = 0; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 422 | int ab; |
Zack Weinberg | f1a86df | 1998-12-07 13:35:20 +0000 | [diff] [blame] | 423 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 424 | if (parse_include (pfile, dtable[T_INCLUDE_NEXT].name, 0, &str, &len, &ab)) |
Zack Weinberg | 29a72a4 | 2000-03-28 21:45:02 +0000 | [diff] [blame] | 425 | return 0; |
Zack Weinberg | f1a86df | 1998-12-07 13:35:20 +0000 | [diff] [blame] | 426 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 427 | /* For #include_next, skip in the search path past the dir in which |
| 428 | the current file was found. If this is the last directory in the |
| 429 | search path, don't include anything. If the current file was |
| 430 | specified with an absolute path, use the normal search logic. If |
| 431 | this is the primary source file, use the normal search logic and |
| 432 | generate a warning. */ |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 433 | if (CPP_PREV_BUFFER (CPP_BUFFER (pfile))) |
Zack Weinberg | 692b872 | 1998-12-16 13:23:47 +0000 | [diff] [blame] | 434 | { |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 435 | if (CPP_BUFFER (pfile)->inc->foundhere) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 436 | { |
| 437 | search_start = CPP_BUFFER (pfile)->inc->foundhere->next; |
| 438 | if (!search_start) |
| 439 | return 0; |
| 440 | } |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 441 | } |
| 442 | else |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 443 | cpp_warning (pfile, "#include_next in primary source file"); |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 444 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 445 | _cpp_execute_include (pfile, str, len, 0, search_start, ab); |
| 446 | if (CPP_OPTION (pfile, dump_includes)) |
| 447 | pass_thru_directive (pfile); |
| 448 | |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 449 | return 0; |
| 450 | } |
| 451 | |
Jason Merrill | d3a34a0 | 1999-08-14 00:42:07 +0000 | [diff] [blame] | 452 | /* Subroutine of do_line. Read next token from PFILE without adding it to |
| 453 | the output buffer. If it is a number between 1 and 4, store it in *NUM |
| 454 | and return 1; otherwise, return 0 and complain if we aren't at the end |
| 455 | of the directive. */ |
| 456 | |
| 457 | static int |
| 458 | read_line_number (pfile, num) |
| 459 | cpp_reader *pfile; |
| 460 | int *num; |
| 461 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 462 | const cpp_token *tok = cpp_get_token (pfile); |
| 463 | enum cpp_ttype type = tok->type; |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 464 | const U_CHAR *p = tok->val.str.text; |
| 465 | unsigned int len = tok->val.str.len; |
Jason Merrill | d3a34a0 | 1999-08-14 00:42:07 +0000 | [diff] [blame] | 466 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 467 | if (type == CPP_NUMBER && len == 1 && p[0] >= '1' && p[0] <= '4') |
Jason Merrill | d3a34a0 | 1999-08-14 00:42:07 +0000 | [diff] [blame] | 468 | { |
| 469 | *num = p[0] - '0'; |
| 470 | return 1; |
| 471 | } |
| 472 | else |
| 473 | { |
Zack Weinberg | cf00a88 | 2000-07-08 02:33:00 +0000 | [diff] [blame] | 474 | if (type != CPP_EOF) |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 475 | cpp_error (pfile, "invalid format #line"); |
Jason Merrill | d3a34a0 | 1999-08-14 00:42:07 +0000 | [diff] [blame] | 476 | return 0; |
| 477 | } |
| 478 | } |
| 479 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 480 | /* Another subroutine of do_line. Convert a number in STR, of length |
| 481 | LEN, to binary; store it in NUMP, and return 0 if the number was |
| 482 | legal, 1 if not. Temporary, hopefully. */ |
| 483 | static int |
| 484 | strtoul_for_line (str, len, nump) |
| 485 | const U_CHAR *str; |
| 486 | unsigned int len; |
| 487 | unsigned long *nump; |
| 488 | { |
| 489 | unsigned long reg = 0; |
| 490 | U_CHAR c; |
| 491 | while (len--) |
| 492 | { |
| 493 | c = *str++; |
| 494 | if (!ISDIGIT (c)) |
| 495 | return 1; |
| 496 | reg *= 10; |
| 497 | reg += c - '0'; |
| 498 | } |
| 499 | *nump = reg; |
| 500 | return 0; |
| 501 | } |
| 502 | |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 503 | /* Interpret #line command. |
| 504 | Note that the filename string (if any) is treated as if it were an |
| 505 | include filename. That means no escape handling. */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 506 | |
| 507 | static int |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 508 | do_line (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 509 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 510 | { |
| 511 | cpp_buffer *ip = CPP_BUFFER (pfile); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 512 | unsigned long new_lineno, old_lineno; |
| 513 | /* C99 raised the minimum limit on #line numbers. */ |
| 514 | unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767; |
| 515 | int action_number = 0; |
| 516 | enum cpp_ttype type; |
| 517 | const U_CHAR *str; |
| 518 | char *fname; |
| 519 | unsigned int len; |
| 520 | const cpp_token *tok; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 521 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 522 | tok = cpp_get_token (pfile); |
| 523 | type = tok->type; |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 524 | str = tok->val.str.text; |
| 525 | len = tok->val.str.len; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 526 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 527 | if (type != CPP_NUMBER || strtoul_for_line (str, len, &new_lineno)) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 528 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 529 | cpp_error (pfile, "token after #line is not a positive integer"); |
| 530 | goto done; |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 531 | } |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 532 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 533 | if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap)) |
| 534 | cpp_pedwarn (pfile, "line number out of range"); |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 535 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 536 | old_lineno = ip->lineno; |
| 537 | ip->lineno = new_lineno; |
| 538 | tok = cpp_get_token (pfile); |
| 539 | type = tok->type; |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 540 | str = tok->val.str.text; |
| 541 | len = tok->val.str.len; |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 542 | |
Zack Weinberg | cf00a88 | 2000-07-08 02:33:00 +0000 | [diff] [blame] | 543 | if (type == CPP_EOF) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 544 | goto done; |
| 545 | else if (type != CPP_STRING) |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 546 | { |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 547 | cpp_error (pfile, "second token after #line is not a string"); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 548 | ip->lineno = old_lineno; /* malformed #line should have no effect */ |
| 549 | goto done; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 550 | } |
| 551 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 552 | fname = alloca (len + 1); |
| 553 | memcpy (fname, str, len); |
| 554 | fname[len] = '\0'; |
| 555 | |
| 556 | if (strcmp (fname, ip->nominal_fname)) |
Zack Weinberg | ff2b53e | 2000-04-06 07:56:14 +0000 | [diff] [blame] | 557 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 558 | if (!strcmp (fname, ip->inc->name)) |
| 559 | ip->nominal_fname = ip->inc->name; |
| 560 | else |
| 561 | ip->nominal_fname = _cpp_fake_include (pfile, fname); |
Zack Weinberg | ff2b53e | 2000-04-06 07:56:14 +0000 | [diff] [blame] | 562 | } |
Zack Weinberg | 941e09b | 1998-12-15 11:17:06 +0000 | [diff] [blame] | 563 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 564 | if (read_line_number (pfile, &action_number) == 0) |
| 565 | return 0; |
Zack Weinberg | ff2b53e | 2000-04-06 07:56:14 +0000 | [diff] [blame] | 566 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 567 | if (CPP_PEDANTIC (pfile)) |
| 568 | cpp_pedwarn (pfile, "garbage at end of #line"); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 569 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 570 | /* This is somewhat questionable: change the buffer stack |
| 571 | depth so that output_line_command thinks we've stacked |
| 572 | another buffer. */ |
| 573 | if (action_number == 1) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 574 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 575 | pfile->buffer_stack_depth++; |
| 576 | cpp_make_system_header (pfile, ip, 0); |
| 577 | read_line_number (pfile, &action_number); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 578 | } |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 579 | else if (action_number == 2) |
| 580 | { |
| 581 | pfile->buffer_stack_depth--; |
| 582 | cpp_make_system_header (pfile, ip, 0); |
| 583 | read_line_number (pfile, &action_number); |
| 584 | } |
| 585 | if (action_number == 3) |
| 586 | { |
| 587 | cpp_make_system_header (pfile, ip, 1); |
| 588 | read_line_number (pfile, &action_number); |
| 589 | } |
| 590 | if (action_number == 4) |
| 591 | { |
| 592 | cpp_make_system_header (pfile, ip, 2); |
| 593 | read_line_number (pfile, &action_number); |
| 594 | } |
| 595 | return 0; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 596 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 597 | done: |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 598 | return 0; |
| 599 | } |
Zack Weinberg | 941e09b | 1998-12-15 11:17:06 +0000 | [diff] [blame] | 600 | |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 601 | /* |
| 602 | * Report an error detected by the program we are processing. |
| 603 | * Use the text of the line in the error message. |
| 604 | * (We use error because it prints the filename & line#.) |
| 605 | */ |
| 606 | |
| 607 | static int |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 608 | do_error (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 609 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 610 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 611 | U_CHAR *text, *limit; |
Zack Weinberg | 941e09b | 1998-12-15 11:17:06 +0000 | [diff] [blame] | 612 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 613 | text = pfile->limit; |
| 614 | _cpp_dump_list (pfile, &pfile->token_list, pfile->first_directive_token, 0); |
| 615 | limit = pfile->limit; |
| 616 | pfile->limit = text; |
| 617 | cpp_error (pfile, "%.*s", (int)(limit - text), text); |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 618 | |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | /* |
| 623 | * Report a warning detected by the program we are processing. |
| 624 | * Use the text of the line in the warning message, then continue. |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 625 | */ |
| 626 | |
| 627 | static int |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 628 | do_warning (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 629 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 630 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 631 | U_CHAR *text, *limit; |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 632 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 633 | text = pfile->limit; |
| 634 | _cpp_dump_list (pfile, &pfile->token_list, pfile->first_directive_token, 0); |
| 635 | limit = pfile->limit; |
| 636 | pfile->limit = text; |
| 637 | cpp_warning (pfile, "%.*s", (int)(limit - text), text); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 638 | return 0; |
| 639 | } |
| 640 | |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 641 | /* Report program identification. */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 642 | |
| 643 | static int |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 644 | do_ident (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 645 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 646 | { |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 647 | /* Next token should be a string constant. */ |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 648 | if (cpp_get_token (pfile)->type == CPP_STRING) |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 649 | /* And then a newline. */ |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 650 | if (cpp_get_token (pfile)->type == CPP_EOF) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 651 | { |
| 652 | /* Good - ship it. */ |
| 653 | pass_thru_directive (pfile); |
| 654 | return 0; |
| 655 | } |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 656 | |
| 657 | cpp_error (pfile, "invalid #ident"); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 658 | return 0; |
| 659 | } |
| 660 | |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 661 | /* Pragmata handling. We handle some of these, and pass the rest on |
| 662 | to the front end. C99 defines three pragmas and says that no macro |
| 663 | expansion is to be performed on them; whether or not macro |
| 664 | expansion happens for other pragmas is implementation defined. |
| 665 | This implementation never macro-expands the text after #pragma. |
| 666 | |
| 667 | We currently do not support the _Pragma operator. Support for that |
| 668 | has to be coordinated with the front end. Proposed implementation: |
| 669 | both #pragma blah blah and _Pragma("blah blah") become |
| 670 | __builtin_pragma(blah blah) and we teach the parser about that. */ |
| 671 | |
| 672 | /* Sub-handlers for the pragmas needing treatment here. |
| 673 | They return 1 if the token buffer is to be popped, 0 if not. */ |
Nathan Sidwell | 8244337 | 2000-06-23 10:56:09 +0000 | [diff] [blame] | 674 | struct pragma_entry |
| 675 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 676 | const char *name; |
Nathan Sidwell | 8244337 | 2000-06-23 10:56:09 +0000 | [diff] [blame] | 677 | int (*handler) PARAMS ((cpp_reader *)); |
| 678 | }; |
| 679 | |
| 680 | static int pragma_dispatch |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 681 | PARAMS ((cpp_reader *, const struct pragma_entry *, const cpp_hashnode *)); |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 682 | static int do_pragma_once PARAMS ((cpp_reader *)); |
| 683 | static int do_pragma_implementation PARAMS ((cpp_reader *)); |
| 684 | static int do_pragma_poison PARAMS ((cpp_reader *)); |
Zack Weinberg | 2c0b35c | 2000-05-17 18:07:16 +0000 | [diff] [blame] | 685 | static int do_pragma_system_header PARAMS ((cpp_reader *)); |
Nathan Sidwell | 8244337 | 2000-06-23 10:56:09 +0000 | [diff] [blame] | 686 | static int do_pragma_gcc PARAMS ((cpp_reader *)); |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 687 | static int do_pragma_dependency PARAMS ((cpp_reader *)); |
Nathan Sidwell | 8244337 | 2000-06-23 10:56:09 +0000 | [diff] [blame] | 688 | |
| 689 | static const struct pragma_entry top_pragmas[] = |
| 690 | { |
| 691 | {"once", do_pragma_once}, |
| 692 | {"implementation", do_pragma_implementation}, |
| 693 | {"poison", do_pragma_poison}, |
Nathan Sidwell | 8244337 | 2000-06-23 10:56:09 +0000 | [diff] [blame] | 694 | {"GCC", do_pragma_gcc}, |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 695 | {NULL, NULL} |
Nathan Sidwell | 8244337 | 2000-06-23 10:56:09 +0000 | [diff] [blame] | 696 | }; |
| 697 | |
| 698 | static const struct pragma_entry gcc_pragmas[] = |
| 699 | { |
| 700 | {"implementation", do_pragma_implementation}, |
| 701 | {"poison", do_pragma_poison}, |
| 702 | {"system_header", do_pragma_system_header}, |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 703 | {"dependency", do_pragma_dependency}, |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 704 | {NULL, NULL} |
Nathan Sidwell | 8244337 | 2000-06-23 10:56:09 +0000 | [diff] [blame] | 705 | }; |
| 706 | |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 707 | static int pragma_dispatch (pfile, table, node) |
Nathan Sidwell | 8244337 | 2000-06-23 10:56:09 +0000 | [diff] [blame] | 708 | cpp_reader *pfile; |
| 709 | const struct pragma_entry *table; |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 710 | const cpp_hashnode *node; |
Nathan Sidwell | 8244337 | 2000-06-23 10:56:09 +0000 | [diff] [blame] | 711 | { |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 712 | const U_CHAR *p = node->name; |
| 713 | size_t len = node->length; |
| 714 | |
Nathan Sidwell | 8244337 | 2000-06-23 10:56:09 +0000 | [diff] [blame] | 715 | for (; table->name; table++) |
| 716 | if (strlen (table->name) == len && !memcmp (p, table->name, len)) |
| 717 | return (*table->handler) (pfile); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 718 | return 0; |
Nathan Sidwell | 8244337 | 2000-06-23 10:56:09 +0000 | [diff] [blame] | 719 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 720 | |
| 721 | static int |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 722 | do_pragma (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 723 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 724 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 725 | const cpp_token *tok; |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 726 | int pop; |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 727 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 728 | tok = cpp_get_token (pfile); |
| 729 | if (tok->type == CPP_EOF) |
| 730 | return 0; |
| 731 | else if (tok->type != CPP_NAME) |
Alexandre Oliva | 0172e2b | 2000-02-27 06:24:27 +0000 | [diff] [blame] | 732 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 733 | cpp_error (pfile, "malformed #pragma directive"); |
| 734 | return 0; |
Alexandre Oliva | 0172e2b | 2000-02-27 06:24:27 +0000 | [diff] [blame] | 735 | } |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 736 | |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 737 | pop = pragma_dispatch (pfile, top_pragmas, tok->val.node); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 738 | if (!pop) |
| 739 | pass_thru_directive (pfile); |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | static int |
Nathan Sidwell | 8244337 | 2000-06-23 10:56:09 +0000 | [diff] [blame] | 744 | do_pragma_gcc (pfile) |
| 745 | cpp_reader *pfile; |
| 746 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 747 | const cpp_token *tok; |
Nathan Sidwell | 8244337 | 2000-06-23 10:56:09 +0000 | [diff] [blame] | 748 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 749 | tok = cpp_get_token (pfile); |
| 750 | if (tok->type == CPP_EOF) |
| 751 | return 1; |
| 752 | else if (tok->type != CPP_NAME) |
| 753 | return 0; |
Nathan Sidwell | 8244337 | 2000-06-23 10:56:09 +0000 | [diff] [blame] | 754 | |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 755 | return pragma_dispatch (pfile, gcc_pragmas, tok->val.node); |
Nathan Sidwell | 8244337 | 2000-06-23 10:56:09 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | static int |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 759 | do_pragma_once (pfile) |
| 760 | cpp_reader *pfile; |
| 761 | { |
| 762 | cpp_buffer *ip = CPP_BUFFER (pfile); |
| 763 | |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 764 | /* Allow #pragma once in system headers, since that's not the user's |
| 765 | fault. */ |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 766 | if (!CPP_IN_SYSTEM_HEADER (pfile)) |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 767 | cpp_warning (pfile, "#pragma once is obsolete"); |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 768 | |
Zack Weinberg | 38b24ee | 2000-03-08 20:37:23 +0000 | [diff] [blame] | 769 | if (CPP_PREV_BUFFER (ip) == NULL) |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 770 | cpp_warning (pfile, "#pragma once outside include file"); |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 771 | else |
Zack Weinberg | c31a650 | 2000-06-21 18:33:51 +0000 | [diff] [blame] | 772 | ip->inc->cmacro = NEVER_REREAD; |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 773 | |
| 774 | return 1; |
| 775 | } |
| 776 | |
| 777 | static int |
| 778 | do_pragma_implementation (pfile) |
| 779 | cpp_reader *pfile; |
| 780 | { |
| 781 | /* Be quiet about `#pragma implementation' for a file only if it hasn't |
| 782 | been included yet. */ |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 783 | const cpp_token *tok = cpp_get_token (pfile); |
Zack Weinberg | 12cf91f | 2000-05-04 04:38:01 +0000 | [diff] [blame] | 784 | char *copy; |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 785 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 786 | if (tok->type == CPP_EOF) |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 787 | return 0; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 788 | else if (tok->type != CPP_STRING |
| 789 | || cpp_get_token (pfile)->type != CPP_EOF) |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 790 | { |
| 791 | cpp_error (pfile, "malformed #pragma implementation"); |
| 792 | return 1; |
| 793 | } |
| 794 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 795 | /* Make a NUL-terminated copy of the string. */ |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 796 | copy = alloca (tok->val.str.len + 1); |
| 797 | memcpy (copy, tok->val.str.text, tok->val.str.len); |
| 798 | copy[tok->val.str.len] = '\0'; |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 799 | |
Zack Weinberg | b0699da | 2000-03-07 20:58:47 +0000 | [diff] [blame] | 800 | if (cpp_included (pfile, copy)) |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 801 | cpp_warning (pfile, |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 802 | "#pragma implementation for %s appears after file is included", |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 803 | copy); |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 804 | return 0; |
| 805 | } |
| 806 | |
| 807 | static int |
| 808 | do_pragma_poison (pfile) |
| 809 | cpp_reader *pfile; |
| 810 | { |
| 811 | /* Poison these symbols so that all subsequent usage produces an |
| 812 | error message. */ |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 813 | const cpp_token *tok; |
Zack Weinberg | f8f769e | 2000-05-28 05:56:38 +0000 | [diff] [blame] | 814 | cpp_hashnode *hp; |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 815 | int writeit; |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 816 | |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 817 | /* As a rule, don't include #pragma poison commands in output, |
| 818 | unless the user asks for them. */ |
Zack Weinberg | ae79697 | 2000-03-31 23:16:11 +0000 | [diff] [blame] | 819 | writeit = (CPP_OPTION (pfile, debug_output) |
| 820 | || CPP_OPTION (pfile, dump_macros) == dump_definitions |
| 821 | || CPP_OPTION (pfile, dump_macros) == dump_names); |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 822 | |
| 823 | for (;;) |
| 824 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 825 | tok = cpp_get_token (pfile); |
| 826 | if (tok->type == CPP_EOF) |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 827 | break; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 828 | if (tok->type != CPP_NAME) |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 829 | { |
| 830 | cpp_error (pfile, "invalid #pragma poison directive"); |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 831 | return 1; |
| 832 | } |
| 833 | |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 834 | hp = tok->val.node; |
Zack Weinberg | a7abcbb | 2000-05-01 18:20:10 +0000 | [diff] [blame] | 835 | if (hp->type == T_POISON) |
| 836 | ; /* It is allowed to poison the same identifier twice. */ |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 837 | else |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 838 | { |
Zack Weinberg | a7abcbb | 2000-05-01 18:20:10 +0000 | [diff] [blame] | 839 | if (hp->type != T_VOID) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 840 | cpp_warning (pfile, "poisoning existing macro \"%s\"", hp->name); |
Zack Weinberg | a7abcbb | 2000-05-01 18:20:10 +0000 | [diff] [blame] | 841 | _cpp_free_definition (hp); |
| 842 | hp->type = T_POISON; |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 843 | } |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 844 | } |
| 845 | return !writeit; |
| 846 | } |
Zack Weinberg | 2c0b35c | 2000-05-17 18:07:16 +0000 | [diff] [blame] | 847 | |
| 848 | /* Mark the current header as a system header. This will suppress |
| 849 | some categories of warnings (notably those from -pedantic). It is |
| 850 | intended for use in system libraries that cannot be implemented in |
| 851 | conforming C, but cannot be certain that their headers appear in a |
| 852 | system include directory. To prevent abuse, it is rejected in the |
| 853 | primary source file. */ |
| 854 | static int |
| 855 | do_pragma_system_header (pfile) |
| 856 | cpp_reader *pfile; |
| 857 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 858 | cpp_buffer *ip = CPP_BUFFER (pfile); |
Zack Weinberg | 2c0b35c | 2000-05-17 18:07:16 +0000 | [diff] [blame] | 859 | if (CPP_PREV_BUFFER (ip) == NULL) |
| 860 | cpp_warning (pfile, "#pragma system_header outside include file"); |
| 861 | else |
Zack Weinberg | e605b04 | 2000-06-21 23:08:17 +0000 | [diff] [blame] | 862 | cpp_make_system_header (pfile, ip, 1); |
Zack Weinberg | 2c0b35c | 2000-05-17 18:07:16 +0000 | [diff] [blame] | 863 | |
| 864 | return 1; |
| 865 | } |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 866 | |
| 867 | /* Check the modified date of the current include file against a specified |
| 868 | file. Issue a diagnostic, if the specified file is newer. We use this to |
| 869 | determine if a fixed header should be refixed. */ |
| 870 | static int |
| 871 | do_pragma_dependency (pfile) |
| 872 | cpp_reader *pfile; |
| 873 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 874 | const U_CHAR *name; |
| 875 | unsigned int len; |
| 876 | int ordering, ab; |
| 877 | char left, right; |
| 878 | |
| 879 | if (parse_include (pfile, U"pragma dependency", 1, &name, &len, &ab)) |
| 880 | return 1; |
| 881 | |
| 882 | left = ab ? '<' : '"'; |
| 883 | right = ab ? '>' : '"'; |
| 884 | |
| 885 | ordering = _cpp_compare_file_date (pfile, name, len, ab); |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 886 | if (ordering < 0) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 887 | cpp_warning (pfile, "cannot find source %c%s%c", left, name, right); |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 888 | else if (ordering > 0) |
| 889 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 890 | const cpp_token *msg = cpp_get_token (pfile); |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 891 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 892 | cpp_warning (pfile, "current file is older than %c%s%c", |
| 893 | left, name, right); |
| 894 | if (msg->type != CPP_EOF) |
| 895 | { |
| 896 | U_CHAR *text, *limit; |
| 897 | |
| 898 | text = pfile->limit; |
| 899 | _cpp_dump_list (pfile, &pfile->token_list, msg, 0); |
| 900 | limit = pfile->limit; |
| 901 | pfile->limit = text; |
| 902 | cpp_warning (pfile, "%.*s", (int)(limit - text), text); |
| 903 | } |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 904 | } |
Nathan Sidwell | f3f751a | 2000-06-30 09:47:49 +0000 | [diff] [blame] | 905 | return 1; |
| 906 | } |
| 907 | |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 908 | /* Just ignore #sccs, on systems where we define it at all. */ |
Zack Weinberg | 07aa0b0 | 2000-04-01 22:55:25 +0000 | [diff] [blame] | 909 | #ifdef SCCS_DIRECTIVE |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 910 | static int |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 911 | do_sccs (pfile) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 912 | cpp_reader *pfile ATTRIBUTE_UNUSED; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 913 | { |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 914 | return 0; |
| 915 | } |
Zack Weinberg | 07aa0b0 | 2000-04-01 22:55:25 +0000 | [diff] [blame] | 916 | #endif |
Jim Meyering | 1d0e51b | 1999-08-25 22:01:36 +0000 | [diff] [blame] | 917 | |
| 918 | /* We've found an `#if' directive. If the only thing before it in |
| 919 | this file is white space, and if it is of the form |
| 920 | `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro |
| 921 | for inclusion of this file. (See redundant_include_p in cppfiles.c |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 922 | for an explanation of controlling macros.) If so, return the |
| 923 | hash node for SYMBOL. Otherwise, return NULL. */ |
Jim Meyering | 1d0e51b | 1999-08-25 22:01:36 +0000 | [diff] [blame] | 924 | |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 925 | static const cpp_hashnode * |
Jim Meyering | 1d0e51b | 1999-08-25 22:01:36 +0000 | [diff] [blame] | 926 | detect_if_not_defined (pfile) |
| 927 | cpp_reader *pfile; |
| 928 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 929 | const cpp_token *token; |
| 930 | cpp_hashnode *cmacro = 0; |
Jim Meyering | 1d0e51b | 1999-08-25 22:01:36 +0000 | [diff] [blame] | 931 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 932 | /* We are guaranteed that tokens are consecutive and end in CPP_EOF. */ |
| 933 | token = pfile->first_directive_token + 2; |
Zack Weinberg | 9cc6e05 | 2000-04-18 06:43:41 +0000 | [diff] [blame] | 934 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 935 | if (token->type != CPP_NOT) |
| 936 | return 0; |
Zack Weinberg | 9cc6e05 | 2000-04-18 06:43:41 +0000 | [diff] [blame] | 937 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 938 | token++; |
| 939 | if (token->type != CPP_NAME |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 940 | || token->val.node != pfile->spec_nodes->n_defined) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 941 | return 0; |
Zack Weinberg | 9cc6e05 | 2000-04-18 06:43:41 +0000 | [diff] [blame] | 942 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 943 | token++; |
| 944 | if (token->type == CPP_OPEN_PAREN) |
| 945 | token++; |
Zack Weinberg | 9cc6e05 | 2000-04-18 06:43:41 +0000 | [diff] [blame] | 946 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 947 | if (token->type != CPP_NAME) |
| 948 | return 0; |
| 949 | |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 950 | cmacro = token->val.node; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 951 | |
| 952 | if (token[-1].type == CPP_OPEN_PAREN) |
Jim Meyering | 1d0e51b | 1999-08-25 22:01:36 +0000 | [diff] [blame] | 953 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 954 | token++; |
| 955 | if (token->type != CPP_CLOSE_PAREN) |
| 956 | return 0; |
Jim Meyering | 1d0e51b | 1999-08-25 22:01:36 +0000 | [diff] [blame] | 957 | } |
Zack Weinberg | 9cc6e05 | 2000-04-18 06:43:41 +0000 | [diff] [blame] | 958 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 959 | token++; |
| 960 | if (token->type != CPP_EOF) |
| 961 | return 0; |
Jim Meyering | 1d0e51b | 1999-08-25 22:01:36 +0000 | [diff] [blame] | 962 | |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 963 | return cmacro; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 964 | } |
| 965 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 966 | /* Parse an #ifdef or #ifndef directive. Returns the hash node of the |
| 967 | macro being tested, and issues various error messages. */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 968 | |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 969 | static const cpp_hashnode * |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 970 | parse_ifdef (pfile, name) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 971 | cpp_reader *pfile; |
Zack Weinberg | 12cf91f | 2000-05-04 04:38:01 +0000 | [diff] [blame] | 972 | const U_CHAR *name; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 973 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 974 | enum cpp_ttype type; |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 975 | const cpp_hashnode *node = 0; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 976 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 977 | const cpp_token *token = cpp_get_token (pfile); |
| 978 | type = token->type; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 979 | |
| 980 | if (!CPP_TRADITIONAL (pfile)) |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 981 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 982 | if (type == CPP_EOF) |
| 983 | cpp_pedwarn (pfile, "#%s with no argument", name); |
| 984 | else if (type != CPP_NAME) |
| 985 | cpp_pedwarn (pfile, "#%s with invalid argument", name); |
| 986 | else if (cpp_get_token (pfile)->type != CPP_EOF) |
| 987 | cpp_pedwarn (pfile, "garbage at end of #%s", name); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 988 | } |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 989 | |
| 990 | if (type == CPP_NAME) |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 991 | node = token->val.node; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 992 | if (node && node->type == T_POISON) |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 993 | { |
| 994 | cpp_error (pfile, "attempt to use poisoned identifier \"%s\"", |
| 995 | node->name); |
| 996 | node = 0; |
| 997 | } |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 998 | |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 999 | return node; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1000 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1001 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1002 | /* #ifdef is dead simple. */ |
| 1003 | |
| 1004 | static int |
| 1005 | do_ifdef (pfile) |
| 1006 | cpp_reader *pfile; |
| 1007 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1008 | const cpp_hashnode *node = 0; |
| 1009 | |
| 1010 | if (! pfile->skipping) |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 1011 | node = parse_ifdef (pfile, dtable[T_IFDEF].name); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1012 | |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 1013 | push_conditional (pfile, !(node && node->type != T_VOID), T_IFDEF, 0); |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1014 | return 0; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | /* #ifndef is a tad more complex, because we need to check for a |
| 1018 | no-reinclusion wrapper. */ |
| 1019 | |
| 1020 | static int |
| 1021 | do_ifndef (pfile) |
| 1022 | cpp_reader *pfile; |
| 1023 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1024 | int start_of_file = 0; |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 1025 | const cpp_hashnode *node = 0; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1026 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1027 | if (! pfile->skipping) |
Jakub Jelinek | 5af7e2c | 2000-06-08 00:27:57 +0200 | [diff] [blame] | 1028 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1029 | start_of_file = (pfile->token_list.flags & BEG_OF_FILE); |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 1030 | node = parse_ifdef (pfile, dtable[T_IFNDEF].name); |
Jakub Jelinek | 5af7e2c | 2000-06-08 00:27:57 +0200 | [diff] [blame] | 1031 | } |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1032 | |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 1033 | push_conditional (pfile, node && node->type != T_VOID, |
| 1034 | T_IFNDEF, start_of_file ? node : 0); |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1035 | return 0; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1036 | } |
| 1037 | |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1038 | /* #if is straightforward; just call _cpp_parse_expr, then conditional_skip. |
| 1039 | Also, check for a reinclude preventer of the form #if !defined (MACRO). */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1040 | |
Zack Weinberg | ff2b53e | 2000-04-06 07:56:14 +0000 | [diff] [blame] | 1041 | static int |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1042 | do_if (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1043 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1044 | { |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1045 | const cpp_hashnode *cmacro = 0; |
| 1046 | int value = 0; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1047 | |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1048 | if (! pfile->skipping) |
| 1049 | { |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 1050 | if (pfile->token_list.flags & BEG_OF_FILE) |
| 1051 | cmacro = detect_if_not_defined (pfile); |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1052 | value = _cpp_parse_expr (pfile); |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1053 | } |
| 1054 | push_conditional (pfile, value == 0, T_IF, cmacro); |
Zack Weinberg | ff2b53e | 2000-04-06 07:56:14 +0000 | [diff] [blame] | 1055 | return 0; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1056 | } |
| 1057 | |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1058 | /* #else flips pfile->skipping and continues without changing |
| 1059 | if_stack; this is so that the error message for missing #endif's |
| 1060 | etc. will point to the original #if. */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1061 | |
| 1062 | static int |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1063 | do_else (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1064 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1065 | { |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1066 | struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack; |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1067 | validate_else (pfile, dtable[T_ELSE].name); |
| 1068 | |
| 1069 | if (ifs == NULL) |
Zack Weinberg | 40ea76d | 2000-02-06 07:30:25 +0000 | [diff] [blame] | 1070 | { |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1071 | cpp_error (pfile, "#else without #if"); |
Zack Weinberg | 40ea76d | 2000-02-06 07:30:25 +0000 | [diff] [blame] | 1072 | return 0; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1073 | } |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1074 | if (ifs->type == T_ELSE) |
Zack Weinberg | 40ea76d | 2000-02-06 07:30:25 +0000 | [diff] [blame] | 1075 | { |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1076 | cpp_error (pfile, "#else after #else"); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1077 | cpp_error_with_line (pfile, ifs->lineno, ifs->colno, |
| 1078 | "the conditional began here"); |
Zack Weinberg | 40ea76d | 2000-02-06 07:30:25 +0000 | [diff] [blame] | 1079 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1080 | |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1081 | /* #ifndef can't have its special treatment for containing the whole file |
| 1082 | if it has a #else clause. */ |
| 1083 | ifs->cmacro = 0; |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1084 | ifs->type = T_ELSE; |
| 1085 | if (! ifs->was_skipping) |
| 1086 | { |
| 1087 | /* If pfile->skipping is 2, one of the blocks in an #if/#elif/... chain |
| 1088 | succeeded, so we mustn't do the else block. */ |
| 1089 | if (pfile->skipping < 2) |
| 1090 | pfile->skipping = ! pfile->skipping; |
| 1091 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1092 | return 0; |
| 1093 | } |
| 1094 | |
| 1095 | /* |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1096 | * handle a #elif directive by not changing if_stack either. |
| 1097 | * see the comment above do_else. |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1098 | */ |
| 1099 | |
| 1100 | static int |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1101 | do_elif (pfile) |
| 1102 | cpp_reader *pfile; |
| 1103 | { |
| 1104 | struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack; |
| 1105 | |
| 1106 | if (ifs == NULL) |
| 1107 | { |
| 1108 | cpp_error (pfile, "#elif without #if"); |
| 1109 | return 0; |
| 1110 | } |
| 1111 | if (ifs->type == T_ELSE) |
| 1112 | { |
| 1113 | cpp_error (pfile, "#elif after #else"); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1114 | cpp_error_with_line (pfile, ifs->lineno, ifs->colno, |
| 1115 | "the conditional began here"); |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | ifs->type = T_ELIF; |
| 1119 | if (ifs->was_skipping) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1120 | return 0; /* Don't evaluate a nested #if */ |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1121 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1122 | if (pfile->skipping != 1) |
| 1123 | { |
| 1124 | pfile->skipping = 2; /* one block succeeded, so don't do any others */ |
| 1125 | return 0; |
| 1126 | } |
| 1127 | |
| 1128 | pfile->skipping = ! _cpp_parse_expr (pfile); |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1129 | return 0; |
| 1130 | } |
| 1131 | |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1132 | /* #endif pops the if stack and resets pfile->skipping. */ |
| 1133 | |
| 1134 | static int |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1135 | do_endif (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1136 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1137 | { |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1138 | struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1139 | |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1140 | validate_else (pfile, dtable[T_ENDIF].name); |
| 1141 | |
| 1142 | if (ifs == NULL) |
| 1143 | cpp_error (pfile, "#endif without #if"); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1144 | else |
| 1145 | { |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1146 | CPP_BUFFER (pfile)->if_stack = ifs->next; |
| 1147 | pfile->skipping = ifs->was_skipping; |
| 1148 | pfile->potential_control_macro = ifs->cmacro; |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 1149 | obstack_free (pfile->buffer_ob, ifs); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1150 | } |
| 1151 | return 0; |
| 1152 | } |
| 1153 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1154 | |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1155 | /* Push an if_stack entry and set pfile->skipping accordingly. |
| 1156 | If this is a #ifndef starting at the beginning of a file, |
| 1157 | CMACRO is the macro name tested by the #ifndef. */ |
| 1158 | |
| 1159 | static void |
| 1160 | push_conditional (pfile, skip, type, cmacro) |
| 1161 | cpp_reader *pfile; |
| 1162 | int skip; |
| 1163 | int type; |
| 1164 | const cpp_hashnode *cmacro; |
| 1165 | { |
| 1166 | struct if_stack *ifs; |
| 1167 | |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 1168 | ifs = xobnew (pfile->buffer_ob, struct if_stack); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1169 | ifs->lineno = _cpp_get_line (pfile, &ifs->colno); |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1170 | ifs->next = CPP_BUFFER (pfile)->if_stack; |
| 1171 | ifs->cmacro = cmacro; |
| 1172 | ifs->was_skipping = pfile->skipping; |
| 1173 | ifs->type = type; |
| 1174 | |
| 1175 | if (!pfile->skipping) |
| 1176 | pfile->skipping = skip; |
| 1177 | |
| 1178 | CPP_BUFFER (pfile)->if_stack = ifs; |
| 1179 | } |
| 1180 | |
Zack Weinberg | 75ec21d | 2000-01-27 22:29:07 +0000 | [diff] [blame] | 1181 | /* Issue -pedantic warning for text which is not a comment following |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1182 | an #else or #endif. */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1183 | |
| 1184 | static void |
| 1185 | validate_else (pfile, directive) |
| 1186 | cpp_reader *pfile; |
Zack Weinberg | 12cf91f | 2000-05-04 04:38:01 +0000 | [diff] [blame] | 1187 | const U_CHAR *directive; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1188 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1189 | if (CPP_PEDANTIC (pfile) && cpp_get_token (pfile)->type != CPP_EOF) |
| 1190 | cpp_pedwarn (pfile, "ISO C forbids text after #%s", directive); |
Zack Weinberg | 6ee2c97 | 1999-09-11 05:38:06 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1193 | /* Called when we reach the end of a file. Walk back up the |
Zack Weinberg | c56c207 | 2000-05-02 16:09:12 +0000 | [diff] [blame] | 1194 | conditional stack till we reach its level at entry to this file, |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1195 | issuing error messages. Then force skipping off. */ |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 1196 | static void |
| 1197 | unwind_if_stack (pfile, pbuf) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1198 | cpp_reader *pfile; |
Zack Weinberg | c56c207 | 2000-05-02 16:09:12 +0000 | [diff] [blame] | 1199 | cpp_buffer *pbuf; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1200 | { |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1201 | struct if_stack *ifs, *nifs; |
Zack Weinberg | 4d9a1b4 | 1999-02-15 14:04:21 +0000 | [diff] [blame] | 1202 | |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1203 | for (ifs = pbuf->if_stack; ifs; ifs = nifs) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1204 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1205 | cpp_error_with_line (pfile, ifs->lineno, ifs->colno, "unterminated #%s", |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1206 | dtable[ifs->type].name); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1207 | nifs = ifs->next; |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 1208 | /* No need to free - they'll all go away with the buffer. */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1209 | } |
Zack Weinberg | ea4a453 | 2000-05-29 16:19:32 +0000 | [diff] [blame] | 1210 | pfile->skipping = 0; |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1213 | /* Parses an assertion, returning a pointer to the hash node of the |
| 1214 | predicate, or 0 on error. If an answer was supplied, it is |
| 1215 | allocated and placed in ANSWERP, otherwise it is set to 0. We use |
| 1216 | _cpp_get_raw_token, since we cannot assume tokens are consecutive |
| 1217 | in a #if statement (we may be in a macro), and we don't want to |
| 1218 | macro expand. */ |
| 1219 | cpp_hashnode * |
| 1220 | _cpp_parse_assertion (pfile, answerp) |
| 1221 | cpp_reader *pfile; |
| 1222 | struct answer **answerp; |
| 1223 | { |
| 1224 | struct answer *answer = 0; |
| 1225 | cpp_toklist *list; |
| 1226 | U_CHAR *sym; |
| 1227 | const cpp_token *token, *predicate; |
| 1228 | const struct directive *d = pfile->token_list.directive; |
| 1229 | unsigned int len = 0; |
| 1230 | |
| 1231 | predicate = _cpp_get_raw_token (pfile); |
| 1232 | if (predicate->type == CPP_EOF) |
| 1233 | { |
| 1234 | cpp_error (pfile, "assertion without predicate"); |
| 1235 | return 0; |
| 1236 | } |
| 1237 | else if (predicate->type != CPP_NAME) |
| 1238 | { |
| 1239 | cpp_error (pfile, "predicate must be an identifier"); |
| 1240 | return 0; |
| 1241 | } |
| 1242 | |
| 1243 | token = _cpp_get_raw_token (pfile); |
| 1244 | if (token->type != CPP_OPEN_PAREN) |
| 1245 | { |
| 1246 | /* #unassert and #if are OK without predicate. */ |
| 1247 | if (d == &dtable[T_UNASSERT]) |
| 1248 | { |
| 1249 | if (token->type == CPP_EOF) |
| 1250 | goto lookup_node; |
| 1251 | } |
| 1252 | else if (d != &dtable[T_ASSERT]) |
| 1253 | { |
| 1254 | _cpp_push_token (pfile, token); |
| 1255 | goto lookup_node; |
| 1256 | } |
| 1257 | cpp_error (pfile, "missing '(' after predicate"); |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
| 1261 | /* Allocate a struct answer, and copy the answer to it. */ |
| 1262 | answer = (struct answer *) xmalloc (sizeof (struct answer)); |
| 1263 | list = &answer->list; |
| 1264 | _cpp_init_toklist (list, NO_DUMMY_TOKEN); |
| 1265 | |
| 1266 | for (;;) |
| 1267 | { |
| 1268 | cpp_token *dest; |
| 1269 | |
| 1270 | token = _cpp_get_raw_token (pfile); |
| 1271 | |
| 1272 | if (token->type == CPP_EOF) |
| 1273 | { |
| 1274 | cpp_error (pfile, "missing ')' to complete answer"); |
| 1275 | goto error; |
| 1276 | } |
| 1277 | if (token->type == CPP_CLOSE_PAREN) |
| 1278 | break; |
| 1279 | |
| 1280 | /* Copy the token. */ |
| 1281 | _cpp_expand_token_space (list, 1); |
| 1282 | dest = &list->tokens[list->tokens_used++]; |
| 1283 | *dest = *token; |
| 1284 | |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 1285 | if (token_spellings[token->type].type == SPELL_STRING) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1286 | { |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 1287 | _cpp_expand_name_space (list, token->val.str.len); |
| 1288 | dest->val.str.text = list->namebuf + list->name_used; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1289 | memcpy (list->namebuf + list->name_used, |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 1290 | token->val.str.text, token->val.str.len); |
| 1291 | list->name_used += token->val.str.len; |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | if (list->tokens_used == 0) |
| 1296 | { |
| 1297 | cpp_error (pfile, "predicate's answer is empty"); |
| 1298 | goto error; |
| 1299 | } |
| 1300 | |
| 1301 | /* Drop whitespace at start. */ |
| 1302 | list->tokens[0].flags &= ~PREV_WHITE; |
| 1303 | |
| 1304 | if ((d == &dtable[T_ASSERT] || d == &dtable[T_UNASSERT]) |
| 1305 | && token[1].type != CPP_EOF) |
| 1306 | { |
| 1307 | cpp_error (pfile, "junk at end of assertion"); |
| 1308 | goto error; |
| 1309 | } |
| 1310 | |
| 1311 | lookup_node: |
| 1312 | *answerp = answer; |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 1313 | len = predicate->val.node->length; |
Zack Weinberg | 385d061 | 2000-07-05 18:12:42 +0000 | [diff] [blame] | 1314 | sym = alloca (len + 1); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1315 | |
| 1316 | /* Prefix '#' to get it out of macro namespace. */ |
| 1317 | sym[0] = '#'; |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 1318 | memcpy (sym + 1, predicate->val.node->name, len); |
Neil Booth | 09074fd | 2000-07-06 09:51:57 +0000 | [diff] [blame] | 1319 | return cpp_lookup (pfile, sym, len + 1); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1320 | |
| 1321 | error: |
| 1322 | FREE_ANSWER (answer); |
| 1323 | return 0; |
| 1324 | } |
| 1325 | |
| 1326 | /* Returns a pointer to the pointer to the answer in the answer chain, |
| 1327 | or a pointer to NULL if the answer is not in the chain. */ |
| 1328 | struct answer ** |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 1329 | _cpp_find_answer (node, candidate) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1330 | cpp_hashnode *node; |
| 1331 | const cpp_toklist *candidate; |
| 1332 | { |
| 1333 | struct answer **result; |
| 1334 | |
| 1335 | for (result = &node->value.answers; *result; result = &(*result)->next) |
| 1336 | if (_cpp_equiv_toklists (&(*result)->list, candidate)) |
| 1337 | break; |
| 1338 | |
| 1339 | return result; |
| 1340 | } |
| 1341 | |
Zack Weinberg | 15dad1d | 2000-05-18 15:55:46 +0000 | [diff] [blame] | 1342 | #define WARNING(msgid) do { cpp_warning(pfile, msgid); goto error; } while (0) |
| 1343 | #define ERROR(msgid) do { cpp_error(pfile, msgid); goto error; } while (0) |
| 1344 | #define ICE(msgid) do { cpp_ice(pfile, msgid); goto error; } while (0) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1345 | static int |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1346 | do_assert (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1347 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1348 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1349 | struct answer *new_answer; |
| 1350 | cpp_hashnode *node; |
Zack Weinberg | 15dad1d | 2000-05-18 15:55:46 +0000 | [diff] [blame] | 1351 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1352 | node = _cpp_parse_assertion (pfile, &new_answer); |
| 1353 | if (node) |
| 1354 | { |
| 1355 | new_answer->next = 0; |
| 1356 | new_answer->list.line = pfile->token_list.line; |
| 1357 | new_answer->list.file = pfile->token_list.file; |
| 1358 | |
| 1359 | if (node->type == T_ASSERTION) |
| 1360 | { |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 1361 | if (*_cpp_find_answer (node, &new_answer->list)) |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1362 | goto err; |
| 1363 | new_answer->next = node->value.answers; |
| 1364 | } |
| 1365 | node->type = T_ASSERTION; |
| 1366 | node->value.answers = new_answer; |
| 1367 | } |
Zack Weinberg | 15dad1d | 2000-05-18 15:55:46 +0000 | [diff] [blame] | 1368 | return 0; |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1369 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1370 | err: |
| 1371 | cpp_warning (pfile, "\"%.*s\" re-asserted", |
| 1372 | node->length - 1, node->name + 1); |
| 1373 | FREE_ANSWER (new_answer); |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 1374 | return 0; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1375 | } |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1376 | |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1377 | static int |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1378 | do_unassert (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1379 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1380 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1381 | cpp_hashnode *node; |
| 1382 | struct answer *answer, *temp, *next; |
| 1383 | |
| 1384 | node = _cpp_parse_assertion (pfile, &answer); |
| 1385 | if (node) |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1386 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1387 | /* It isn't an error to #unassert something that isn't asserted. */ |
| 1388 | if (node->type == T_ASSERTION) |
Zack Weinberg | 15dad1d | 2000-05-18 15:55:46 +0000 | [diff] [blame] | 1389 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1390 | if (answer) |
| 1391 | { |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 1392 | struct answer **p = _cpp_find_answer (node, &answer->list); |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1393 | |
| 1394 | temp = *p; |
| 1395 | if (temp) |
| 1396 | { |
| 1397 | *p = temp->next; |
| 1398 | FREE_ANSWER (temp); |
| 1399 | } |
| 1400 | if (node->value.answers == 0) |
| 1401 | node->type = T_VOID; |
| 1402 | } |
| 1403 | else |
| 1404 | { |
| 1405 | for (temp = node->value.answers; temp; temp = next) |
| 1406 | { |
| 1407 | next = temp->next; |
| 1408 | FREE_ANSWER (temp); |
| 1409 | } |
| 1410 | node->type = T_VOID; |
| 1411 | } |
Zack Weinberg | 15dad1d | 2000-05-18 15:55:46 +0000 | [diff] [blame] | 1412 | } |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1413 | |
| 1414 | if (answer) |
| 1415 | FREE_ANSWER (answer); |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1416 | } |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 1417 | return 0; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1418 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1419 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1420 | /* These are for -D, -U, -A. */ |
| 1421 | |
| 1422 | /* Process the string STR as if it appeared as the body of a #define. |
| 1423 | If STR is just an identifier, define it with value 1. |
| 1424 | If STR has anything after the identifier, then it should |
| 1425 | be identifier=definition. */ |
| 1426 | |
| 1427 | void |
| 1428 | cpp_define (pfile, str) |
| 1429 | cpp_reader *pfile; |
| 1430 | const char *str; |
| 1431 | { |
| 1432 | char *buf, *p; |
| 1433 | size_t count; |
| 1434 | |
| 1435 | p = strchr (str, '='); |
| 1436 | /* Copy the entire option so we can modify it. |
| 1437 | Change the first "=" in the string to a space. If there is none, |
| 1438 | tack " 1" on the end. Then add a newline and a NUL. */ |
| 1439 | |
| 1440 | if (p) |
| 1441 | { |
| 1442 | count = strlen (str) + 2; |
Dave Pitts | 5f6d382 | 2000-04-16 17:54:25 -0700 | [diff] [blame] | 1443 | buf = (char *) alloca (count); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1444 | memcpy (buf, str, count - 2); |
| 1445 | buf[p - str] = ' '; |
| 1446 | buf[count - 2] = '\n'; |
| 1447 | buf[count - 1] = '\0'; |
| 1448 | } |
| 1449 | else |
| 1450 | { |
| 1451 | count = strlen (str) + 4; |
Dave Pitts | 5f6d382 | 2000-04-16 17:54:25 -0700 | [diff] [blame] | 1452 | buf = (char *) alloca (count); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1453 | memcpy (buf, str, count - 4); |
| 1454 | strcpy (&buf[count-4], " 1\n"); |
| 1455 | } |
| 1456 | |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1457 | _cpp_run_directive (pfile, &dtable[T_DEFINE], buf, count - 1); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | /* Process MACRO as if it appeared as the body of an #undef. */ |
| 1461 | void |
| 1462 | cpp_undef (pfile, macro) |
| 1463 | cpp_reader *pfile; |
| 1464 | const char *macro; |
| 1465 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1466 | _cpp_run_directive (pfile, &dtable[T_UNDEF], macro, strlen (macro)); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | /* Process the string STR as if it appeared as the body of a #assert. */ |
| 1470 | void |
| 1471 | cpp_assert (pfile, str) |
| 1472 | cpp_reader *pfile; |
| 1473 | const char *str; |
| 1474 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1475 | _cpp_run_directive (pfile, &dtable[T_ASSERT], str, strlen (str)); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 1478 | /* Process STR as if it appeared as the body of an #unassert. */ |
| 1479 | void |
| 1480 | cpp_unassert (pfile, str) |
| 1481 | cpp_reader *pfile; |
Neil Booth | 7ceb359 | 2000-03-11 00:49:44 +0000 | [diff] [blame] | 1482 | const char *str; |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 1483 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1484 | _cpp_run_directive (pfile, &dtable[T_UNASSERT], str, strlen (str)); |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1487 | /* Determine whether the identifier ID, of length LEN, is a defined macro. */ |
| 1488 | int |
| 1489 | cpp_defined (pfile, id, len) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1490 | cpp_reader *pfile; |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1491 | const U_CHAR *id; |
| 1492 | int len; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1493 | { |
Zack Weinberg | f8f769e | 2000-05-28 05:56:38 +0000 | [diff] [blame] | 1494 | cpp_hashnode *hp = cpp_lookup (pfile, id, len); |
Zack Weinberg | a7abcbb | 2000-05-01 18:20:10 +0000 | [diff] [blame] | 1495 | if (hp->type == T_POISON) |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1496 | { |
Zack Weinberg | 041c319 | 2000-07-04 01:58:21 +0000 | [diff] [blame] | 1497 | cpp_error (pfile, "attempt to use poisoned \"%s\"", hp->name); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1498 | return 0; |
| 1499 | } |
Zack Weinberg | a7abcbb | 2000-05-01 18:20:10 +0000 | [diff] [blame] | 1500 | return (hp->type != T_VOID); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1501 | } |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 1502 | |
| 1503 | /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack. |
| 1504 | If BUFFER != NULL, then use the LENGTH characters in BUFFER |
| 1505 | as the new input buffer. |
| 1506 | Return the new buffer, or NULL on failure. */ |
| 1507 | |
| 1508 | cpp_buffer * |
| 1509 | cpp_push_buffer (pfile, buffer, length) |
| 1510 | cpp_reader *pfile; |
| 1511 | const U_CHAR *buffer; |
| 1512 | long length; |
| 1513 | { |
| 1514 | cpp_buffer *buf = CPP_BUFFER (pfile); |
| 1515 | cpp_buffer *new; |
| 1516 | if (++pfile->buffer_stack_depth == CPP_STACK_MAX) |
| 1517 | { |
| 1518 | cpp_fatal (pfile, "#include recursion too deep"); |
| 1519 | return NULL; |
| 1520 | } |
| 1521 | |
| 1522 | new = xobnew (pfile->buffer_ob, cpp_buffer); |
| 1523 | memset (new, 0, sizeof (cpp_buffer)); |
| 1524 | |
| 1525 | new->buf = new->cur = buffer; |
| 1526 | new->rlimit = buffer + length; |
| 1527 | new->prev = buf; |
| 1528 | |
| 1529 | CPP_BUFFER (pfile) = new; |
| 1530 | return new; |
| 1531 | } |
| 1532 | |
| 1533 | cpp_buffer * |
| 1534 | cpp_pop_buffer (pfile) |
| 1535 | cpp_reader *pfile; |
| 1536 | { |
| 1537 | cpp_buffer *buf = CPP_BUFFER (pfile); |
| 1538 | |
| 1539 | unwind_if_stack (pfile, buf); |
| 1540 | #ifdef HAVE_MMAP_FILE |
| 1541 | if (buf->mapped) |
| 1542 | munmap ((caddr_t) buf->buf, buf->rlimit - buf->buf); |
| 1543 | else |
| 1544 | #endif |
| 1545 | if (buf->inc) |
| 1546 | free ((PTR) buf->buf); |
| 1547 | |
| 1548 | if (buf->inc) |
| 1549 | { |
| 1550 | if (pfile->system_include_depth) |
| 1551 | pfile->system_include_depth--; |
| 1552 | if (pfile->include_depth) |
| 1553 | pfile->include_depth--; |
| 1554 | if (pfile->potential_control_macro) |
| 1555 | { |
| 1556 | if (buf->inc->cmacro != NEVER_REREAD) |
| 1557 | buf->inc->cmacro = pfile->potential_control_macro; |
| 1558 | pfile->potential_control_macro = 0; |
| 1559 | } |
| 1560 | pfile->input_stack_listing_current = 0; |
| 1561 | /* If the file will not be included again, then close it. */ |
| 1562 | if (DO_NOT_REREAD (buf->inc)) |
| 1563 | { |
| 1564 | close (buf->inc->fd); |
| 1565 | buf->inc->fd = -1; |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf); |
| 1570 | obstack_free (pfile->buffer_ob, buf); |
| 1571 | pfile->buffer_stack_depth--; |
| 1572 | return CPP_BUFFER (pfile); |
| 1573 | } |
| 1574 | |
| 1575 | #define obstack_chunk_alloc xmalloc |
| 1576 | #define obstack_chunk_free free |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 1577 | #define DSC(x) U x, sizeof x - 1 |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 1578 | void |
| 1579 | _cpp_init_stacks (pfile) |
| 1580 | cpp_reader *pfile; |
| 1581 | { |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 1582 | int i; |
| 1583 | struct spec_nodes *s; |
| 1584 | |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 1585 | pfile->buffer_ob = xnew (struct obstack); |
| 1586 | obstack_init (pfile->buffer_ob); |
Zack Weinberg | bfb9dc7 | 2000-07-08 19:00:39 +0000 | [diff] [blame^] | 1587 | |
| 1588 | /* Perhaps not the ideal place to put this. */ |
| 1589 | pfile->spec_nodes = s = xnew (struct spec_nodes); |
| 1590 | s->n_L = cpp_lookup (pfile, DSC("L")); |
| 1591 | s->n_defined = cpp_lookup (pfile, DSC("defined")); |
| 1592 | s->n__STRICT_ANSI__ = cpp_lookup (pfile, DSC("__STRICT_ANSI__")); |
| 1593 | s->n__CHAR_UNSIGNED__ = cpp_lookup (pfile, DSC("__CHAR_UNSIGNED__")); |
| 1594 | s->n__VA_ARGS__ = cpp_lookup (pfile, DSC("__VA_ARGS__")); |
| 1595 | for (i = 0; i < N_DIRECTIVES; i++) |
| 1596 | s->dirs[i] = cpp_lookup (pfile, dtable[i].name, dtable[i].length); |
Zack Weinberg | c71f835 | 2000-07-05 05:33:57 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | void |
| 1600 | _cpp_cleanup_stacks (pfile) |
| 1601 | cpp_reader *pfile; |
| 1602 | { |
| 1603 | obstack_free (pfile->buffer_ob, 0); |
| 1604 | free (pfile->buffer_ob); |
| 1605 | } |