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" |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 27 | #include "hashtab.h" |
Jeffrey A Law | ab87f8c | 1999-01-27 01:43:17 +0000 | [diff] [blame] | 28 | #include "intl.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 | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 31 | #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) \ |
| 32 | ? CPP_BUFFER (pfile)->cur[N] : EOF) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 33 | #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N)) |
| 34 | #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile)) |
| 35 | #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile)) |
Zack Weinberg | 564ad5f | 2000-02-10 00:26:47 +0000 | [diff] [blame] | 36 | |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 37 | /* `struct directive' defines one #-directive, including how to handle it. */ |
| 38 | |
Zack Weinberg | ba412f1 | 2000-03-01 00:57:09 +0000 | [diff] [blame] | 39 | struct directive |
| 40 | { |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 41 | int (*func) /* Function to handle directive */ |
| 42 | PARAMS ((cpp_reader *)); |
Zack Weinberg | 07aa0b0 | 2000-04-01 22:55:25 +0000 | [diff] [blame^] | 43 | const char *name; /* Name of directive */ |
| 44 | unsigned short length; /* Length of name */ |
| 45 | unsigned short origin; /* Origin of this directive */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 46 | }; |
| 47 | |
Zack Weinberg | 88ae23e | 2000-03-08 23:35:19 +0000 | [diff] [blame] | 48 | /* Stack of conditionals currently in progress |
| 49 | (including both successful and failing conditionals). */ |
| 50 | |
| 51 | struct if_stack |
| 52 | { |
| 53 | struct if_stack *next; |
| 54 | int lineno; /* line number where condition started */ |
| 55 | int if_succeeded; /* truth of last condition in this group */ |
| 56 | const U_CHAR *control_macro; /* macro name for #ifndef around entire file */ |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 57 | int type; /* type of last directive seen in this group */ |
Zack Weinberg | 88ae23e | 2000-03-08 23:35:19 +0000 | [diff] [blame] | 58 | }; |
| 59 | typedef struct if_stack IF_STACK; |
| 60 | |
Zack Weinberg | 1316f1f | 2000-02-06 07:53:50 +0000 | [diff] [blame] | 61 | /* Forward declarations. */ |
| 62 | |
Zack Weinberg | 1316f1f | 2000-02-06 07:53:50 +0000 | [diff] [blame] | 63 | static void validate_else PARAMS ((cpp_reader *, const char *)); |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 64 | static int parse_ifdef PARAMS ((cpp_reader *, const char *)); |
| 65 | static unsigned int parse_include PARAMS ((cpp_reader *, const char *)); |
| 66 | static void conditional_skip PARAMS ((cpp_reader *, int, int, |
| 67 | U_CHAR *)); |
Zack Weinberg | 1316f1f | 2000-02-06 07:53:50 +0000 | [diff] [blame] | 68 | static void skip_if_group PARAMS ((cpp_reader *)); |
Zack Weinberg | 1316f1f | 2000-02-06 07:53:50 +0000 | [diff] [blame] | 69 | static void pass_thru_directive PARAMS ((const U_CHAR *, size_t, |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 70 | cpp_reader *, int)); |
Zack Weinberg | 1316f1f | 2000-02-06 07:53:50 +0000 | [diff] [blame] | 71 | static int read_line_number PARAMS ((cpp_reader *, int *)); |
Zack Weinberg | 1316f1f | 2000-02-06 07:53:50 +0000 | [diff] [blame] | 72 | static U_CHAR *detect_if_not_defined PARAMS ((cpp_reader *)); |
Zack Weinberg | 38b24ee | 2000-03-08 20:37:23 +0000 | [diff] [blame] | 73 | static int consider_directive_while_skipping |
| 74 | PARAMS ((cpp_reader *, IF_STACK *)); |
Zack Weinberg | ba412f1 | 2000-03-01 00:57:09 +0000 | [diff] [blame] | 75 | static int get_macro_name PARAMS ((cpp_reader *)); |
Kaveh R. Ghazi | 487a6e0 | 1998-05-19 08:42:48 +0000 | [diff] [blame] | 76 | |
Zack Weinberg | 07aa0b0 | 2000-04-01 22:55:25 +0000 | [diff] [blame^] | 77 | /* Values for the "origin" field of the table below. */ |
| 78 | enum { KANDR = 0, STDC89, EXTENSION }; |
| 79 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 80 | /* This is the table of directive handlers. It is ordered by |
| 81 | frequency of occurrence; the numbers at the end are directive |
| 82 | counts from all the source code I have lying around (egcs and libc |
| 83 | CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and |
| 84 | pcmcia-cs-3.0.9). |
Jeff Law | e9a25f7 | 1997-11-02 14:19:36 -0700 | [diff] [blame] | 85 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 86 | The entries with a dash and a name after the count are extensions, |
| 87 | of which all but #warning and #include_next are deprecated. The name |
| 88 | is where the extension appears to have come from. */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 89 | |
Zack Weinberg | 07aa0b0 | 2000-04-01 22:55:25 +0000 | [diff] [blame^] | 90 | /* #sccs is not always recognized. */ |
| 91 | #ifdef SCCS_DIRECTIVE |
| 92 | # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION) /* 0 - SVR2? */ |
| 93 | #else |
| 94 | # define SCCS_ENTRY /* nothing */ |
| 95 | #endif |
| 96 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 97 | #define DIRECTIVE_TABLE \ |
Zack Weinberg | 07aa0b0 | 2000-04-01 22:55:25 +0000 | [diff] [blame^] | 98 | D(define, T_DEFINE, KANDR) /* 270554 */ \ |
| 99 | D(include, T_INCLUDE, KANDR) /* 52262 */ \ |
| 100 | D(endif, T_ENDIF, KANDR) /* 45855 */ \ |
| 101 | D(ifdef, T_IFDEF, KANDR) /* 22000 */ \ |
| 102 | D(if, T_IF, KANDR) /* 18162 */ \ |
| 103 | D(else, T_ELSE, KANDR) /* 9863 */ \ |
| 104 | D(ifndef, T_IFNDEF, KANDR) /* 9675 */ \ |
| 105 | D(undef, T_UNDEF, KANDR) /* 4837 */ \ |
| 106 | D(line, T_LINE, KANDR) /* 2465 */ \ |
| 107 | D(elif, T_ELIF, KANDR) /* 610 */ \ |
| 108 | D(error, T_ERROR, STDC89) /* 475 */ \ |
| 109 | D(pragma, T_PRAGMA, STDC89) /* 195 */ \ |
| 110 | D(warning, T_WARNING, EXTENSION) /* 22 - GNU */ \ |
| 111 | D(include_next, T_INCLUDE_NEXT, EXTENSION) /* 19 - GNU */ \ |
| 112 | D(ident, T_IDENT, EXTENSION) /* 11 - SVR4 */ \ |
| 113 | D(import, T_IMPORT, EXTENSION) /* 0 - ObjC */ \ |
| 114 | D(assert, T_ASSERT, EXTENSION) /* 0 - SVR4 */ \ |
| 115 | D(unassert, T_UNASSERT, EXTENSION) /* 0 - SVR4 */ \ |
| 116 | SCCS_ENTRY |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 117 | |
| 118 | /* Use the table to generate a series of prototypes, an enum for the |
| 119 | directive names, and an array of directive handlers. */ |
| 120 | |
| 121 | /* The directive-processing functions are declared to return int |
| 122 | instead of void, because some old compilers have trouble with |
| 123 | pointers to functions returning void. */ |
| 124 | |
Zack Weinberg | 07aa0b0 | 2000-04-01 22:55:25 +0000 | [diff] [blame^] | 125 | #define D(name, t, o) static int CONCAT2(do_, name) PARAMS ((cpp_reader *)); |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 126 | DIRECTIVE_TABLE |
| 127 | #undef D |
| 128 | |
Zack Weinberg | 07aa0b0 | 2000-04-01 22:55:25 +0000 | [diff] [blame^] | 129 | #define D(n, tag, o) tag, |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 130 | enum |
| 131 | { |
| 132 | DIRECTIVE_TABLE |
| 133 | N_DIRECTIVES |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 134 | }; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 135 | #undef D |
| 136 | |
Zack Weinberg | 07aa0b0 | 2000-04-01 22:55:25 +0000 | [diff] [blame^] | 137 | #define D(name, t, origin) \ |
| 138 | { CONCAT2(do_, name), STRINGX(name), sizeof STRINGX(name) - 1, origin }, |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 139 | static const struct directive dtable[] = |
| 140 | { |
| 141 | DIRECTIVE_TABLE |
| 142 | }; |
| 143 | #undef D |
| 144 | #undef DIRECTIVE_TABLE |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 145 | |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 146 | /* Handle a possible # directive. |
| 147 | '#' has already been read. */ |
| 148 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 149 | int |
| 150 | _cpp_handle_directive (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 151 | cpp_reader *pfile; |
Zack Weinberg | 3fdc651 | 1999-03-16 13:10:15 +0000 | [diff] [blame] | 152 | { |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 153 | int c, i; |
Zack Weinberg | 07aa0b0 | 2000-04-01 22:55:25 +0000 | [diff] [blame^] | 154 | int hash_at_bol; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 155 | unsigned int len; |
Zack Weinberg | 941e09b | 1998-12-15 11:17:06 +0000 | [diff] [blame] | 156 | U_CHAR *ident; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 157 | long old_written = CPP_WRITTEN (pfile); |
| 158 | |
Zack Weinberg | ba412f1 | 2000-03-01 00:57:09 +0000 | [diff] [blame] | 159 | if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile))) |
| 160 | { |
| 161 | cpp_ice (pfile, "handle_directive called on macro buffer"); |
| 162 | return 0; |
| 163 | } |
| 164 | |
Zack Weinberg | 07aa0b0 | 2000-04-01 22:55:25 +0000 | [diff] [blame^] | 165 | /* -traditional directives are recognized only with the # in column 1. |
| 166 | XXX Layering violation. */ |
| 167 | hash_at_bol = (CPP_BUFFER (pfile)->cur - CPP_BUFFER (pfile)->line_base == 1); |
| 168 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 169 | _cpp_skip_hspace (pfile); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 170 | |
| 171 | c = PEEKC (); |
Zack Weinberg | 40c79d5 | 2000-01-12 00:35:36 +0000 | [diff] [blame] | 172 | /* # followed by a number is equivalent to #line. Do not recognize |
| 173 | this form in assembly language source files. Complain about this |
| 174 | form if we're being pedantic, but not if this is regurgitated |
| 175 | input (preprocessed or fed back in by the C++ frontend). */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 176 | if (c >= '0' && c <= '9') |
| 177 | { |
Zack Weinberg | ae79697 | 2000-03-31 23:16:11 +0000 | [diff] [blame] | 178 | if (CPP_OPTION (pfile, lang_asm)) |
Zack Weinberg | eaefae0 | 2000-02-06 08:24:22 +0000 | [diff] [blame] | 179 | return 0; |
Zack Weinberg | 40c79d5 | 2000-01-12 00:35:36 +0000 | [diff] [blame] | 180 | |
Jason Merrill | e6ad5e9 | 1999-09-23 20:28:40 +0000 | [diff] [blame] | 181 | if (CPP_PEDANTIC (pfile) |
Zack Weinberg | ae79697 | 2000-03-31 23:16:11 +0000 | [diff] [blame] | 182 | && ! CPP_OPTION (pfile, preprocessed) |
Jason Merrill | e6ad5e9 | 1999-09-23 20:28:40 +0000 | [diff] [blame] | 183 | && ! CPP_BUFFER (pfile)->manual_pop) |
Zack Weinberg | 07aa0b0 | 2000-04-01 22:55:25 +0000 | [diff] [blame^] | 184 | cpp_pedwarn (pfile, "# followed by integer"); |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 185 | do_line (pfile); |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 186 | return 1; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Zack Weinberg | 40c79d5 | 2000-01-12 00:35:36 +0000 | [diff] [blame] | 189 | /* If we are rescanning preprocessed input, don't obey any directives |
| 190 | other than # nnn. */ |
Zack Weinberg | ae79697 | 2000-03-31 23:16:11 +0000 | [diff] [blame] | 191 | if (CPP_OPTION (pfile, preprocessed)) |
Zack Weinberg | 40c79d5 | 2000-01-12 00:35:36 +0000 | [diff] [blame] | 192 | return 0; |
| 193 | |
Mike Stump | 0f41302 | 1996-07-03 22:07:53 +0000 | [diff] [blame] | 194 | /* Now find the directive name. */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 195 | CPP_PUTC (pfile, '#'); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 196 | _cpp_parse_name (pfile, GETC()); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 197 | ident = pfile->token_buffer + old_written + 1; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 198 | len = CPP_PWRITTEN (pfile) - ident; |
| 199 | if (len == 0) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 200 | { |
Zack Weinberg | 07aa0b0 | 2000-04-01 22:55:25 +0000 | [diff] [blame^] | 201 | /* A line of just # becomes blank. A line with something |
Zack Weinberg | c1212d2 | 2000-02-06 23:46:18 +0000 | [diff] [blame] | 202 | other than an identifier after the # is reparsed as a non- |
| 203 | directive line. */ |
Zack Weinberg | eaefae0 | 2000-02-06 08:24:22 +0000 | [diff] [blame] | 204 | CPP_SET_WRITTEN (pfile, old_written); |
| 205 | return (PEEKC() == '\n'); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Zack Weinberg | eaefae0 | 2000-02-06 08:24:22 +0000 | [diff] [blame] | 208 | /* Decode the keyword and call the appropriate expansion routine. */ |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 209 | for (i = 0; i < N_DIRECTIVES; i++) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 210 | { |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 211 | if (dtable[i].length == len |
| 212 | && !strncmp (dtable[i].name, ident, len)) |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 213 | break; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 214 | } |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 215 | if (i == N_DIRECTIVES) |
| 216 | /* # identifier, but not a legit directive. Pass onward as a |
| 217 | CPP_DIRECTIVE token anyway - let the consumer worry about it. */ |
| 218 | return 1; |
Jeff Law | e9a25f7 | 1997-11-02 14:19:36 -0700 | [diff] [blame] | 219 | |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 220 | CPP_SET_WRITTEN (pfile, old_written); |
Zack Weinberg | 5237f53 | 2000-02-02 21:41:35 +0000 | [diff] [blame] | 221 | |
| 222 | if (pfile->no_directives) |
| 223 | { |
| 224 | cpp_error (pfile, "`#%s' may not be used inside a macro argument", |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 225 | dtable[i].name); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 226 | _cpp_skip_rest_of_line (pfile); |
Zack Weinberg | 07aa0b0 | 2000-04-01 22:55:25 +0000 | [diff] [blame^] | 227 | return 1; |
Zack Weinberg | 5237f53 | 2000-02-02 21:41:35 +0000 | [diff] [blame] | 228 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 229 | |
Zack Weinberg | 07aa0b0 | 2000-04-01 22:55:25 +0000 | [diff] [blame^] | 230 | if (CPP_PEDANTIC (pfile) && dtable[i].origin == EXTENSION) |
| 231 | cpp_pedwarn (pfile, "ISO C does not allow #%s", dtable[i].name); |
| 232 | if (CPP_WTRADITIONAL (pfile)) |
| 233 | { |
| 234 | if (!hash_at_bol && dtable[i].origin == KANDR) |
| 235 | cpp_warning (pfile, "the # in #%s should be at the left margin", |
| 236 | dtable[i].name); |
| 237 | else if (hash_at_bol && dtable[i].origin != KANDR) |
| 238 | cpp_warning (pfile, |
| 239 | "the # in #%s should not be at the left margin", |
| 240 | dtable[i].name); |
| 241 | } |
| 242 | |
| 243 | if (CPP_TRADITIONAL (pfile) && !hash_at_bol) |
| 244 | return 0; |
| 245 | |
| 246 | (*dtable[i].func) (pfile); |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 247 | return 1; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | /* Pass a directive through to the output file. |
| 251 | BUF points to the contents of the directive, as a contiguous string. |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 252 | LEN is the length of the string pointed to by BUF. |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 253 | KEYWORD is the keyword-table entry for the directive. */ |
| 254 | |
| 255 | static void |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 256 | pass_thru_directive (buf, len, pfile, keyword) |
Kaveh R. Ghazi | bcc5cac | 1999-09-07 15:41:26 +0000 | [diff] [blame] | 257 | const U_CHAR *buf; |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 258 | size_t len; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 259 | cpp_reader *pfile; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 260 | int keyword; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 261 | { |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 262 | const struct directive *kt = &dtable[keyword]; |
| 263 | register unsigned klen = kt->length; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 264 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 265 | CPP_RESERVE (pfile, 1 + klen + len); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 266 | CPP_PUTC_Q (pfile, '#'); |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 267 | CPP_PUTS_Q (pfile, kt->name, klen); |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 268 | if (len != 0 && buf[0] != ' ') |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 269 | CPP_PUTC_Q (pfile, ' '); |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 270 | CPP_PUTS_Q (pfile, buf, len); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 271 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 272 | |
Zack Weinberg | ba412f1 | 2000-03-01 00:57:09 +0000 | [diff] [blame] | 273 | /* Subroutine of do_define: determine the name of the macro to be |
| 274 | defined. */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 275 | |
Zack Weinberg | ba412f1 | 2000-03-01 00:57:09 +0000 | [diff] [blame] | 276 | static int |
| 277 | get_macro_name (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 278 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 279 | { |
Zack Weinberg | ba412f1 | 2000-03-01 00:57:09 +0000 | [diff] [blame] | 280 | long here, len; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 281 | |
Zack Weinberg | ba412f1 | 2000-03-01 00:57:09 +0000 | [diff] [blame] | 282 | here = CPP_WRITTEN (pfile); |
| 283 | pfile->no_macro_expand++; |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 284 | if (_cpp_get_directive_token (pfile) != CPP_NAME) |
Zack Weinberg | ba412f1 | 2000-03-01 00:57:09 +0000 | [diff] [blame] | 285 | { |
| 286 | cpp_error (pfile, "`#define' must be followed by an identifier"); |
| 287 | goto invalid; |
| 288 | } |
| 289 | |
| 290 | len = CPP_WRITTEN (pfile) - here; |
| 291 | if (len == 7 && !strncmp (pfile->token_buffer + here, "defined", 7)) |
| 292 | { |
| 293 | cpp_error (pfile, "`defined' is not a legal macro name"); |
| 294 | goto invalid; |
| 295 | } |
| 296 | |
| 297 | pfile->no_macro_expand--; |
| 298 | return len; |
| 299 | |
| 300 | invalid: |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 301 | _cpp_skip_rest_of_line (pfile); |
Zack Weinberg | ba412f1 | 2000-03-01 00:57:09 +0000 | [diff] [blame] | 302 | pfile->no_macro_expand--; |
| 303 | return 0; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 306 | /* Process a #define command. */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 307 | |
| 308 | static int |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 309 | do_define (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 310 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 311 | { |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 312 | HASHNODE **slot; |
Zack Weinberg | ba412f1 | 2000-03-01 00:57:09 +0000 | [diff] [blame] | 313 | DEFINITION *def; |
Zack Weinberg | 941e09b | 1998-12-15 11:17:06 +0000 | [diff] [blame] | 314 | long here; |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 315 | unsigned long hash; |
Zack Weinberg | ba412f1 | 2000-03-01 00:57:09 +0000 | [diff] [blame] | 316 | int len, c; |
| 317 | int funlike = 0; |
| 318 | U_CHAR *sym; |
Zack Weinberg | 941e09b | 1998-12-15 11:17:06 +0000 | [diff] [blame] | 319 | |
| 320 | here = CPP_WRITTEN (pfile); |
Zack Weinberg | ba412f1 | 2000-03-01 00:57:09 +0000 | [diff] [blame] | 321 | len = get_macro_name (pfile); |
| 322 | if (len == 0) |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 323 | return 0; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 324 | |
Zack Weinberg | ba412f1 | 2000-03-01 00:57:09 +0000 | [diff] [blame] | 325 | /* Copy out the name so we can pop the token buffer. */ |
| 326 | len = CPP_WRITTEN (pfile) - here; |
| 327 | sym = (U_CHAR *) alloca (len + 1); |
| 328 | memcpy (sym, pfile->token_buffer + here, len); |
| 329 | sym[len] = '\0'; |
| 330 | CPP_SET_WRITTEN (pfile, here); |
| 331 | |
| 332 | /* If the next character, with no intervening whitespace, is '(', |
| 333 | then this is a function-like macro. */ |
| 334 | c = PEEKC (); |
| 335 | if (c == '(') |
| 336 | funlike = 1; |
| 337 | else if (c != '\n' && !is_hspace (c)) |
| 338 | /* Otherwise, C99 requires white space after the name. We treat it |
| 339 | as an object-like macro if this happens, with a warning. */ |
| 340 | cpp_pedwarn (pfile, "missing white space after `#define %.*s'", len, sym); |
| 341 | |
Zack Weinberg | b0699da | 2000-03-07 20:58:47 +0000 | [diff] [blame] | 342 | def = _cpp_create_definition (pfile, funlike); |
Zack Weinberg | ba412f1 | 2000-03-01 00:57:09 +0000 | [diff] [blame] | 343 | if (def == 0) |
| 344 | return 0; |
| 345 | |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 346 | slot = _cpp_lookup_slot (pfile, sym, len, 1, &hash); |
| 347 | if (*slot) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 348 | { |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 349 | int ok; |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 350 | HASHNODE *hp = *slot; |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 351 | |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 352 | /* Redefining a macro is ok if the definitions are the same. */ |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 353 | if (hp->type == T_MACRO) |
Zack Weinberg | b0699da | 2000-03-07 20:58:47 +0000 | [diff] [blame] | 354 | ok = ! _cpp_compare_defs (pfile, def, hp->value.defn); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 355 | /* Redefining a constant is ok with -D. */ |
Zack Weinberg | 0961816 | 1999-02-09 07:31:59 +0000 | [diff] [blame] | 356 | else if (hp->type == T_CONST || hp->type == T_STDC) |
Zack Weinberg | ae79697 | 2000-03-31 23:16:11 +0000 | [diff] [blame] | 357 | ok = ! pfile->done_initializing; |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 358 | /* Otherwise it's not ok. */ |
| 359 | else |
| 360 | ok = 0; |
Geoff Keating | fc009f9 | 1999-09-09 04:00:37 +0000 | [diff] [blame] | 361 | /* Print the warning or error if it's not ok. */ |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 362 | if (! ok) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 363 | { |
Geoff Keating | fc009f9 | 1999-09-09 04:00:37 +0000 | [diff] [blame] | 364 | if (hp->type == T_POISON) |
Zack Weinberg | ba412f1 | 2000-03-01 00:57:09 +0000 | [diff] [blame] | 365 | cpp_error (pfile, "redefining poisoned `%.*s'", len, sym); |
Geoff Keating | fc009f9 | 1999-09-09 04:00:37 +0000 | [diff] [blame] | 366 | else |
Zack Weinberg | ba412f1 | 2000-03-01 00:57:09 +0000 | [diff] [blame] | 367 | cpp_pedwarn (pfile, "`%.*s' redefined", len, sym); |
Zack Weinberg | ae79697 | 2000-03-31 23:16:11 +0000 | [diff] [blame] | 368 | if (hp->type == T_MACRO && pfile->done_initializing) |
Zack Weinberg | ba412f1 | 2000-03-01 00:57:09 +0000 | [diff] [blame] | 369 | { |
| 370 | DEFINITION *d = hp->value.defn; |
| 371 | cpp_pedwarn_with_file_and_line (pfile, d->file, d->line, d->col, |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 372 | "this is the location of the previous definition"); |
Zack Weinberg | ba412f1 | 2000-03-01 00:57:09 +0000 | [diff] [blame] | 373 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 374 | } |
Geoff Keating | fc009f9 | 1999-09-09 04:00:37 +0000 | [diff] [blame] | 375 | if (hp->type != T_POISON) |
| 376 | { |
| 377 | /* Replace the old definition. */ |
Zack Weinberg | f9ba428 | 2000-02-14 07:57:30 +0000 | [diff] [blame] | 378 | if (hp->type == T_MACRO) |
Zack Weinberg | b0699da | 2000-03-07 20:58:47 +0000 | [diff] [blame] | 379 | _cpp_free_definition (hp->value.defn); |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 380 | hp->type = T_MACRO; |
Zack Weinberg | ba412f1 | 2000-03-01 00:57:09 +0000 | [diff] [blame] | 381 | hp->value.defn = def; |
Geoff Keating | fc009f9 | 1999-09-09 04:00:37 +0000 | [diff] [blame] | 382 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 383 | } |
| 384 | else |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 385 | { |
| 386 | HASHNODE *hp = _cpp_make_hashnode (sym, len, T_MACRO, hash); |
| 387 | hp->value.defn = def; |
| 388 | *slot = hp; |
| 389 | } |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 390 | |
Zack Weinberg | ae79697 | 2000-03-31 23:16:11 +0000 | [diff] [blame] | 391 | if (CPP_OPTION (pfile, debug_output) |
| 392 | || CPP_OPTION (pfile, dump_macros) == dump_definitions) |
Zack Weinberg | b0699da | 2000-03-07 20:58:47 +0000 | [diff] [blame] | 393 | _cpp_dump_definition (pfile, sym, len, def); |
Zack Weinberg | ae79697 | 2000-03-31 23:16:11 +0000 | [diff] [blame] | 394 | else if (CPP_OPTION (pfile, dump_macros) == dump_names) |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 395 | pass_thru_directive (sym, len, pfile, T_DEFINE); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 396 | |
| 397 | return 0; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 398 | } |
| 399 | |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 400 | /* |
| 401 | * write out a #line command, for instance, after an #include file. |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 402 | * FILE_CHANGE says whether we are entering a file, leaving, or neither. |
| 403 | */ |
| 404 | |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 405 | void |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 406 | _cpp_output_line_command (pfile, file_change) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 407 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 408 | enum file_change_code file_change; |
| 409 | { |
Zack Weinberg | 80e9dcb | 1999-04-19 11:55:04 +0000 | [diff] [blame] | 410 | long line; |
Zack Weinberg | c45da1c | 2000-03-02 20:14:32 +0000 | [diff] [blame] | 411 | cpp_buffer *ip; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 412 | |
Zack Weinberg | ae79697 | 2000-03-31 23:16:11 +0000 | [diff] [blame] | 413 | if (CPP_OPTION (pfile, no_line_commands) |
| 414 | || CPP_OPTION (pfile, no_output)) |
Per Bothner | e2f79f3 | 1996-06-07 00:30:20 -0700 | [diff] [blame] | 415 | return; |
| 416 | |
Zack Weinberg | c45da1c | 2000-03-02 20:14:32 +0000 | [diff] [blame] | 417 | ip = cpp_file_buffer (pfile); |
| 418 | cpp_buf_line_and_col (ip, &line, NULL); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 419 | |
Zack Weinberg | 80e9dcb | 1999-04-19 11:55:04 +0000 | [diff] [blame] | 420 | /* If the current file has not changed, we omit the #line if it would |
| 421 | appear to be a no-op, and we output a few newlines instead |
| 422 | if we want to increase the line number by a small amount. |
| 423 | We cannot do this if pfile->lineno is zero, because that means we |
| 424 | haven't output any line commands yet. (The very first line command |
| 425 | output is a `same_file' command.) */ |
| 426 | if (file_change == same_file && pfile->lineno != 0) |
Zack Weinberg | 3fdc651 | 1999-03-16 13:10:15 +0000 | [diff] [blame] | 427 | { |
| 428 | if (line == pfile->lineno) |
| 429 | return; |
Richard Kenner | a8259c7 | 1995-07-01 11:57:25 -0400 | [diff] [blame] | 430 | |
Zack Weinberg | 3fdc651 | 1999-03-16 13:10:15 +0000 | [diff] [blame] | 431 | /* If the inherited line number is a little too small, |
| 432 | output some newlines instead of a #line command. */ |
| 433 | if (line > pfile->lineno && line < pfile->lineno + 8) |
| 434 | { |
| 435 | CPP_RESERVE (pfile, 20); |
| 436 | while (line > pfile->lineno) |
| 437 | { |
| 438 | CPP_PUTC_Q (pfile, '\n'); |
| 439 | pfile->lineno++; |
| 440 | } |
| 441 | return; |
| 442 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 443 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 444 | |
| 445 | CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50); |
Zack Weinberg | 3fdc651 | 1999-03-16 13:10:15 +0000 | [diff] [blame] | 446 | CPP_PUTS_Q (pfile, "# ", 2); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 447 | |
Jeff Law | e9a25f7 | 1997-11-02 14:19:36 -0700 | [diff] [blame] | 448 | sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 449 | CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile))); |
| 450 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 451 | _cpp_quote_string (pfile, ip->nominal_fname); |
Alexandre Oliva | 656ac11 | 2000-02-27 07:57:29 +0000 | [diff] [blame] | 452 | if (file_change != same_file && file_change != rename_file) |
Zack Weinberg | 3fdc651 | 1999-03-16 13:10:15 +0000 | [diff] [blame] | 453 | { |
| 454 | CPP_PUTC_Q (pfile, ' '); |
| 455 | CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2'); |
| 456 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 457 | /* Tell cc1 if following text comes from a system header file. */ |
Zack Weinberg | 3fdc651 | 1999-03-16 13:10:15 +0000 | [diff] [blame] | 458 | if (ip->system_header_p) |
| 459 | { |
| 460 | CPP_PUTC_Q (pfile, ' '); |
| 461 | CPP_PUTC_Q (pfile, '3'); |
| 462 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 463 | #ifndef NO_IMPLICIT_EXTERN_C |
| 464 | /* Tell cc1plus if following text should be treated as C. */ |
Zack Weinberg | ae79697 | 2000-03-31 23:16:11 +0000 | [diff] [blame] | 465 | if (ip->system_header_p == 2 && CPP_OPTION (pfile, cplusplus)) |
Zack Weinberg | 3fdc651 | 1999-03-16 13:10:15 +0000 | [diff] [blame] | 466 | { |
| 467 | CPP_PUTC_Q (pfile, ' '); |
| 468 | CPP_PUTC_Q (pfile, '4'); |
| 469 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 470 | #endif |
| 471 | CPP_PUTC_Q (pfile, '\n'); |
| 472 | pfile->lineno = line; |
| 473 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 474 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 475 | /* Handle #include and #import. */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 476 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 477 | static unsigned int |
| 478 | parse_include (pfile, name) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 479 | cpp_reader *pfile; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 480 | const char *name; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 481 | { |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 482 | long old_written = CPP_WRITTEN (pfile); |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 483 | enum cpp_token token; |
| 484 | int len; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 485 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 486 | pfile->parsing_include_directive++; |
| 487 | token = _cpp_get_directive_token (pfile); |
| 488 | pfile->parsing_include_directive--; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 489 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 490 | len = CPP_WRITTEN (pfile) - old_written; |
| 491 | |
| 492 | if (token == CPP_STRING) |
| 493 | ; /* No special treatment required. */ |
| 494 | #ifdef VMS |
| 495 | else if (token == CPP_NAME) |
Richard Kenner | cfb3ee1 | 1997-04-13 14:30:13 -0400 | [diff] [blame] | 496 | { |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 497 | /* Support '#include xyz' like VAX-C. It is taken as |
| 498 | '#include <xyz.h>' and generates a warning. */ |
| 499 | cpp_warning (pfile, "#%s filename is obsolete, use #%s <filename.h>", |
| 500 | name, name); |
| 501 | |
| 502 | /* Rewrite the token to <xyz.h>. */ |
| 503 | CPP_RESERVE (pfile, 4); |
| 504 | len += 4; |
| 505 | memmove (pfile->token_buffer + old_written + 1, |
| 506 | pfile->token_buffer + old_written, |
| 507 | CPP_WRITTEN (pfile) - old_written); |
| 508 | pfile->token_buffer[old_written] = '<'; |
| 509 | CPP_PUTS_Q (pfile, ".h>", 2); |
| 510 | } |
| 511 | #endif |
| 512 | else |
| 513 | { |
| 514 | cpp_error (pfile, "`#%s' expects \"FILENAME\" or <FILENAME>", name); |
| 515 | CPP_SET_WRITTEN (pfile, old_written); |
| 516 | _cpp_skip_rest_of_line (pfile); |
| 517 | return 0; |
Richard Kenner | cfb3ee1 | 1997-04-13 14:30:13 -0400 | [diff] [blame] | 518 | } |
| 519 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 520 | CPP_NUL_TERMINATE (pfile); |
| 521 | CPP_ADJUST_WRITTEN (pfile, 1); |
| 522 | |
| 523 | if (_cpp_get_directive_token (pfile) != CPP_VSPACE) |
| 524 | { |
| 525 | cpp_error (pfile, "junk at end of `#%s'", name); |
| 526 | _cpp_skip_rest_of_line (pfile); |
| 527 | } |
| 528 | |
| 529 | CPP_SET_WRITTEN (pfile, old_written); |
| 530 | |
| 531 | if (len == 0) |
| 532 | cpp_error (pfile, "empty file name in `#%s'", name); |
| 533 | |
| 534 | return len; |
| 535 | } |
| 536 | |
| 537 | static int |
| 538 | do_include (pfile) |
| 539 | cpp_reader *pfile; |
| 540 | { |
| 541 | unsigned int len; |
| 542 | char *token; |
| 543 | |
| 544 | len = parse_include (pfile, dtable[T_INCLUDE].name); |
Zack Weinberg | 29a72a4 | 2000-03-28 21:45:02 +0000 | [diff] [blame] | 545 | if (len == 0) |
| 546 | return 0; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 547 | token = alloca (len + 1); |
| 548 | strcpy (token, CPP_PWRITTEN (pfile)); |
| 549 | |
Zack Weinberg | ae79697 | 2000-03-31 23:16:11 +0000 | [diff] [blame] | 550 | if (CPP_OPTION (pfile, dump_includes)) |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 551 | pass_thru_directive (token, len, pfile, T_INCLUDE); |
| 552 | |
| 553 | _cpp_execute_include (pfile, token, len, 0, 0); |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | static int |
| 558 | do_import (pfile) |
| 559 | cpp_reader *pfile; |
| 560 | { |
| 561 | unsigned int len; |
| 562 | char *token; |
| 563 | |
Zack Weinberg | ae79697 | 2000-03-31 23:16:11 +0000 | [diff] [blame] | 564 | if (CPP_OPTION (pfile, warn_import) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 565 | && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning) |
| 566 | { |
| 567 | pfile->import_warning = 1; |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 568 | cpp_warning (pfile, |
| 569 | "#import is obsolete, use an #ifndef wrapper in the header file"); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 570 | } |
| 571 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 572 | len = parse_include (pfile, dtable[T_IMPORT].name); |
Zack Weinberg | 29a72a4 | 2000-03-28 21:45:02 +0000 | [diff] [blame] | 573 | if (len == 0) |
| 574 | return 0; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 575 | token = alloca (len + 1); |
| 576 | strcpy (token, CPP_PWRITTEN (pfile)); |
| 577 | |
Zack Weinberg | ae79697 | 2000-03-31 23:16:11 +0000 | [diff] [blame] | 578 | if (CPP_OPTION (pfile, dump_includes)) |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 579 | pass_thru_directive (token, len, pfile, T_IMPORT); |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 580 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 581 | _cpp_execute_include (pfile, token, len, 1, 0); |
| 582 | return 0; |
| 583 | } |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 584 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 585 | static int |
| 586 | do_include_next (pfile) |
| 587 | cpp_reader *pfile; |
| 588 | { |
| 589 | unsigned int len; |
| 590 | char *token; |
| 591 | struct file_name_list *search_start = 0; |
Zack Weinberg | f1a86df | 1998-12-07 13:35:20 +0000 | [diff] [blame] | 592 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 593 | len = parse_include (pfile, dtable[T_INCLUDE_NEXT].name); |
Zack Weinberg | 29a72a4 | 2000-03-28 21:45:02 +0000 | [diff] [blame] | 594 | if (len == 0) |
| 595 | return 0; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 596 | token = alloca (len + 1); |
| 597 | strcpy (token, CPP_PWRITTEN (pfile)); |
| 598 | |
Zack Weinberg | ae79697 | 2000-03-31 23:16:11 +0000 | [diff] [blame] | 599 | if (CPP_OPTION (pfile, dump_includes)) |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 600 | pass_thru_directive (token, len, pfile, T_INCLUDE_NEXT); |
Zack Weinberg | f1a86df | 1998-12-07 13:35:20 +0000 | [diff] [blame] | 601 | |
Zack Weinberg | 692b872 | 1998-12-16 13:23:47 +0000 | [diff] [blame] | 602 | /* For #include_next, skip in the search path past the dir in which the |
| 603 | containing file was found. Treat files specified using an absolute path |
| 604 | as if there are no more directories to search. Treat the primary source |
| 605 | file like any other included source, but generate a warning. */ |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 606 | if (CPP_PREV_BUFFER (CPP_BUFFER (pfile))) |
Zack Weinberg | 692b872 | 1998-12-16 13:23:47 +0000 | [diff] [blame] | 607 | { |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 608 | if (CPP_BUFFER (pfile)->ihash->foundhere != ABSOLUTE_PATH) |
| 609 | search_start = CPP_BUFFER (pfile)->ihash->foundhere->next; |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 610 | } |
| 611 | else |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 612 | cpp_warning (pfile, "#include_next in primary source file"); |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 613 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 614 | _cpp_execute_include (pfile, token, len, 0, search_start); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 615 | return 0; |
| 616 | } |
| 617 | |
Jason Merrill | d3a34a0 | 1999-08-14 00:42:07 +0000 | [diff] [blame] | 618 | /* Subroutine of do_line. Read next token from PFILE without adding it to |
| 619 | the output buffer. If it is a number between 1 and 4, store it in *NUM |
| 620 | and return 1; otherwise, return 0 and complain if we aren't at the end |
| 621 | of the directive. */ |
| 622 | |
| 623 | static int |
| 624 | read_line_number (pfile, num) |
| 625 | cpp_reader *pfile; |
| 626 | int *num; |
| 627 | { |
| 628 | long save_written = CPP_WRITTEN (pfile); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 629 | U_CHAR *p; |
| 630 | enum cpp_token token = _cpp_get_directive_token (pfile); |
Jason Merrill | d3a34a0 | 1999-08-14 00:42:07 +0000 | [diff] [blame] | 631 | CPP_SET_WRITTEN (pfile, save_written); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 632 | p = pfile->token_buffer + save_written; |
Jason Merrill | d3a34a0 | 1999-08-14 00:42:07 +0000 | [diff] [blame] | 633 | |
| 634 | if (token == CPP_NUMBER && *p >= '1' && *p <= '4' && p[1] == '\0') |
| 635 | { |
| 636 | *num = p[0] - '0'; |
| 637 | return 1; |
| 638 | } |
| 639 | else |
| 640 | { |
Zack Weinberg | ba412f1 | 2000-03-01 00:57:09 +0000 | [diff] [blame] | 641 | if (token != CPP_VSPACE && token != CPP_EOF) |
Jason Merrill | d3a34a0 | 1999-08-14 00:42:07 +0000 | [diff] [blame] | 642 | cpp_error (pfile, "invalid format `#line' command"); |
| 643 | return 0; |
| 644 | } |
| 645 | } |
| 646 | |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 647 | /* Interpret #line command. |
| 648 | Note that the filename string (if any) is treated as if it were an |
| 649 | include filename. That means no escape handling. */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 650 | |
| 651 | static int |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 652 | do_line (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 653 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 654 | { |
| 655 | cpp_buffer *ip = CPP_BUFFER (pfile); |
| 656 | int new_lineno; |
| 657 | long old_written = CPP_WRITTEN (pfile); |
| 658 | enum file_change_code file_change = same_file; |
| 659 | enum cpp_token token; |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 660 | char *x; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 661 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 662 | token = _cpp_get_directive_token (pfile); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 663 | |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 664 | if (token != CPP_NUMBER) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 665 | { |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 666 | cpp_error (pfile, "token after `#line' is not an integer"); |
| 667 | goto bad_line_directive; |
| 668 | } |
| 669 | |
| 670 | new_lineno = strtol (pfile->token_buffer + old_written, &x, 10); |
| 671 | if (x[0] != '\0') |
| 672 | { |
| 673 | cpp_error (pfile, "token after `#line' is not an integer"); |
| 674 | goto bad_line_directive; |
| 675 | } |
| 676 | CPP_SET_WRITTEN (pfile, old_written); |
| 677 | |
Alexandre Oliva | 7113a16 | 2000-02-16 08:43:57 +0000 | [diff] [blame] | 678 | if (CPP_PEDANTIC (pfile) && (new_lineno <= 0 || new_lineno > 32767)) |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 679 | cpp_pedwarn (pfile, "line number out of range in `#line' command"); |
| 680 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 681 | token = _cpp_get_directive_token (pfile); |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 682 | |
| 683 | if (token == CPP_STRING) |
| 684 | { |
| 685 | U_CHAR *fname = pfile->token_buffer + old_written + 1; |
| 686 | U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1; |
Jason Merrill | d3a34a0 | 1999-08-14 00:42:07 +0000 | [diff] [blame] | 687 | int action_number = 0; |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 688 | |
Alexandre Oliva | 656ac11 | 2000-02-27 07:57:29 +0000 | [diff] [blame] | 689 | file_change = rename_file; |
| 690 | |
Jason Merrill | d3a34a0 | 1999-08-14 00:42:07 +0000 | [diff] [blame] | 691 | if (read_line_number (pfile, &action_number)) |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 692 | { |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 693 | if (CPP_PEDANTIC (pfile)) |
| 694 | cpp_pedwarn (pfile, "garbage at end of `#line' command"); |
| 695 | |
Jason Merrill | d3a34a0 | 1999-08-14 00:42:07 +0000 | [diff] [blame] | 696 | if (action_number == 1) |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 697 | { |
Jason Merrill | d3a34a0 | 1999-08-14 00:42:07 +0000 | [diff] [blame] | 698 | file_change = enter_file; |
| 699 | read_line_number (pfile, &action_number); |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 700 | } |
Jason Merrill | d3a34a0 | 1999-08-14 00:42:07 +0000 | [diff] [blame] | 701 | else if (action_number == 2) |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 702 | { |
Jason Merrill | d3a34a0 | 1999-08-14 00:42:07 +0000 | [diff] [blame] | 703 | file_change = leave_file; |
| 704 | read_line_number (pfile, &action_number); |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 705 | } |
Jason Merrill | d3a34a0 | 1999-08-14 00:42:07 +0000 | [diff] [blame] | 706 | if (action_number == 3) |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 707 | { |
Jason Merrill | d3a34a0 | 1999-08-14 00:42:07 +0000 | [diff] [blame] | 708 | ip->system_header_p = 1; |
| 709 | read_line_number (pfile, &action_number); |
| 710 | } |
| 711 | if (action_number == 4) |
| 712 | { |
| 713 | ip->system_header_p = 2; |
| 714 | read_line_number (pfile, &action_number); |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | |
| 718 | *end_name = '\0'; |
| 719 | |
| 720 | if (strcmp (fname, ip->nominal_fname)) |
| 721 | { |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 722 | const char *newname, *oldname; |
Zack Weinberg | 38b24ee | 2000-03-08 20:37:23 +0000 | [diff] [blame] | 723 | if (!strcmp (fname, ip->ihash->name)) |
| 724 | newname = ip->ihash->name; |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 725 | else if (ip->last_nominal_fname |
| 726 | && !strcmp (fname, ip->last_nominal_fname)) |
| 727 | newname = ip->last_nominal_fname; |
| 728 | else |
| 729 | newname = xstrdup (fname); |
| 730 | |
| 731 | oldname = ip->nominal_fname; |
| 732 | ip->nominal_fname = newname; |
| 733 | |
| 734 | if (ip->last_nominal_fname |
| 735 | && ip->last_nominal_fname != oldname |
Zack Weinberg | 4d9a1b4 | 1999-02-15 14:04:21 +0000 | [diff] [blame] | 736 | && ip->last_nominal_fname != newname |
Zack Weinberg | 38b24ee | 2000-03-08 20:37:23 +0000 | [diff] [blame] | 737 | && ip->last_nominal_fname != ip->ihash->name) |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 738 | free ((void *) ip->last_nominal_fname); |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 739 | |
Zack Weinberg | 38b24ee | 2000-03-08 20:37:23 +0000 | [diff] [blame] | 740 | if (newname == ip->ihash->name) |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 741 | ip->last_nominal_fname = NULL; |
| 742 | else |
| 743 | ip->last_nominal_fname = oldname; |
| 744 | } |
| 745 | } |
| 746 | else if (token != CPP_VSPACE && token != CPP_EOF) |
| 747 | { |
| 748 | cpp_error (pfile, "token after `#line %d' is not a string", new_lineno); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 749 | goto bad_line_directive; |
| 750 | } |
| 751 | |
| 752 | /* The Newline at the end of this line remains to be processed. |
| 753 | To put the next line at the specified line number, |
| 754 | we must store a line number now that is one less. */ |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 755 | ip->lineno = new_lineno - 1; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 756 | CPP_SET_WRITTEN (pfile, old_written); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 757 | _cpp_output_line_command (pfile, file_change); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 758 | return 0; |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 759 | |
| 760 | bad_line_directive: |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 761 | _cpp_skip_rest_of_line (pfile); |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 762 | CPP_SET_WRITTEN (pfile, old_written); |
| 763 | return 0; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 764 | } |
| 765 | |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 766 | /* Remove the definition of a symbol from the symbol table. |
| 767 | According to the C standard, it is not an error to undef |
| 768 | something that has no definitions. */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 769 | static int |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 770 | do_undef (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 771 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 772 | { |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 773 | int len; |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 774 | HASHNODE **slot; |
Zack Weinberg | 941e09b | 1998-12-15 11:17:06 +0000 | [diff] [blame] | 775 | U_CHAR *buf, *name, *limit; |
| 776 | int c; |
| 777 | long here = CPP_WRITTEN (pfile); |
| 778 | enum cpp_token token; |
| 779 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 780 | _cpp_skip_hspace (pfile); |
Zack Weinberg | 941e09b | 1998-12-15 11:17:06 +0000 | [diff] [blame] | 781 | c = GETC(); |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 782 | if (! is_idstart(c)) |
Zack Weinberg | 941e09b | 1998-12-15 11:17:06 +0000 | [diff] [blame] | 783 | { |
| 784 | cpp_error (pfile, "token after #undef is not an identifier"); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 785 | _cpp_skip_rest_of_line (pfile); |
Zack Weinberg | 941e09b | 1998-12-15 11:17:06 +0000 | [diff] [blame] | 786 | return 1; |
| 787 | } |
| 788 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 789 | _cpp_parse_name (pfile, c); |
Zack Weinberg | 941e09b | 1998-12-15 11:17:06 +0000 | [diff] [blame] | 790 | buf = pfile->token_buffer + here; |
| 791 | limit = CPP_PWRITTEN(pfile); |
| 792 | |
| 793 | /* Copy out the token so we can pop the token buffer. */ |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 794 | len = limit - buf; |
| 795 | name = (U_CHAR *) alloca (len + 1); |
| 796 | memcpy (name, buf, len); |
Zack Weinberg | ba412f1 | 2000-03-01 00:57:09 +0000 | [diff] [blame] | 797 | name[len] = '\0'; |
Zack Weinberg | 941e09b | 1998-12-15 11:17:06 +0000 | [diff] [blame] | 798 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 799 | token = _cpp_get_directive_token (pfile); |
Zack Weinberg | ba412f1 | 2000-03-01 00:57:09 +0000 | [diff] [blame] | 800 | if (token != CPP_VSPACE) |
Zack Weinberg | 941e09b | 1998-12-15 11:17:06 +0000 | [diff] [blame] | 801 | { |
| 802 | cpp_pedwarn (pfile, "junk on line after #undef"); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 803 | _cpp_skip_rest_of_line (pfile); |
Zack Weinberg | 941e09b | 1998-12-15 11:17:06 +0000 | [diff] [blame] | 804 | } |
Zack Weinberg | 941e09b | 1998-12-15 11:17:06 +0000 | [diff] [blame] | 805 | CPP_SET_WRITTEN (pfile, here); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 806 | |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 807 | slot = _cpp_lookup_slot (pfile, name, len, 0, 0); |
| 808 | if (slot) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 809 | { |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 810 | HASHNODE *hp = *slot; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 811 | /* If we are generating additional info for debugging (with -g) we |
| 812 | need to pass through all effective #undef commands. */ |
Zack Weinberg | ae79697 | 2000-03-31 23:16:11 +0000 | [diff] [blame] | 813 | if (CPP_OPTION (pfile, debug_output)) |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 814 | pass_thru_directive (name, len, pfile, T_UNDEF); |
Geoff Keating | fc009f9 | 1999-09-09 04:00:37 +0000 | [diff] [blame] | 815 | if (hp->type == T_POISON) |
| 816 | cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name); |
| 817 | else |
| 818 | { |
| 819 | if (hp->type != T_MACRO) |
| 820 | cpp_warning (pfile, "undefining `%s'", hp->name); |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 821 | |
| 822 | htab_clear_slot (pfile->hashtab, (void **)slot); |
Geoff Keating | fc009f9 | 1999-09-09 04:00:37 +0000 | [diff] [blame] | 823 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 824 | } |
| 825 | |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 826 | return 0; |
| 827 | } |
Zack Weinberg | 941e09b | 1998-12-15 11:17:06 +0000 | [diff] [blame] | 828 | |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 829 | /* |
| 830 | * Report an error detected by the program we are processing. |
| 831 | * Use the text of the line in the error message. |
| 832 | * (We use error because it prints the filename & line#.) |
| 833 | */ |
| 834 | |
| 835 | static int |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 836 | do_error (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 837 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 838 | { |
Neil Booth | 7ceb359 | 2000-03-11 00:49:44 +0000 | [diff] [blame] | 839 | const U_CHAR *text, *limit; |
Zack Weinberg | 941e09b | 1998-12-15 11:17:06 +0000 | [diff] [blame] | 840 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 841 | _cpp_skip_hspace (pfile); |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 842 | text = CPP_BUFFER (pfile)->cur; |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 843 | _cpp_skip_rest_of_line (pfile); |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 844 | limit = CPP_BUFFER (pfile)->cur; |
| 845 | |
| 846 | cpp_error (pfile, "#error %.*s", (int)(limit - text), text); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 847 | return 0; |
| 848 | } |
| 849 | |
| 850 | /* |
| 851 | * Report a warning detected by the program we are processing. |
| 852 | * Use the text of the line in the warning message, then continue. |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 853 | */ |
| 854 | |
| 855 | static int |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 856 | do_warning (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 857 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 858 | { |
Neil Booth | 7ceb359 | 2000-03-11 00:49:44 +0000 | [diff] [blame] | 859 | const U_CHAR *text, *limit; |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 860 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 861 | _cpp_skip_hspace (pfile); |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 862 | text = CPP_BUFFER (pfile)->cur; |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 863 | _cpp_skip_rest_of_line (pfile); |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 864 | limit = CPP_BUFFER (pfile)->cur; |
Jeff Law | f5963e6 | 1998-05-05 17:18:02 -0600 | [diff] [blame] | 865 | |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 866 | cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 867 | return 0; |
| 868 | } |
| 869 | |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 870 | /* Report program identification. */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 871 | |
| 872 | static int |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 873 | do_ident (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 874 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 875 | { |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 876 | long old_written = CPP_WRITTEN (pfile); |
| 877 | |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 878 | CPP_PUTS (pfile, "#ident ", 7); |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 879 | |
| 880 | /* Next token should be a string constant. */ |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 881 | if (_cpp_get_directive_token (pfile) == CPP_STRING) |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 882 | /* And then a newline. */ |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 883 | if (_cpp_get_directive_token (pfile) == CPP_VSPACE) |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 884 | /* Good - ship it. */ |
| 885 | return 0; |
| 886 | |
| 887 | cpp_error (pfile, "invalid #ident"); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 888 | _cpp_skip_rest_of_line (pfile); |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 889 | CPP_SET_WRITTEN (pfile, old_written); /* discard directive */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 890 | |
| 891 | return 0; |
| 892 | } |
| 893 | |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 894 | /* Pragmata handling. We handle some of these, and pass the rest on |
| 895 | to the front end. C99 defines three pragmas and says that no macro |
| 896 | expansion is to be performed on them; whether or not macro |
| 897 | expansion happens for other pragmas is implementation defined. |
| 898 | This implementation never macro-expands the text after #pragma. |
| 899 | |
| 900 | We currently do not support the _Pragma operator. Support for that |
| 901 | has to be coordinated with the front end. Proposed implementation: |
| 902 | both #pragma blah blah and _Pragma("blah blah") become |
| 903 | __builtin_pragma(blah blah) and we teach the parser about that. */ |
| 904 | |
| 905 | /* Sub-handlers for the pragmas needing treatment here. |
| 906 | They return 1 if the token buffer is to be popped, 0 if not. */ |
| 907 | static int do_pragma_once PARAMS ((cpp_reader *)); |
| 908 | static int do_pragma_implementation PARAMS ((cpp_reader *)); |
| 909 | static int do_pragma_poison PARAMS ((cpp_reader *)); |
| 910 | static int do_pragma_default PARAMS ((cpp_reader *)); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 911 | |
| 912 | static int |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 913 | do_pragma (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 914 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 915 | { |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 916 | long here, key; |
Zack Weinberg | 941e09b | 1998-12-15 11:17:06 +0000 | [diff] [blame] | 917 | U_CHAR *buf; |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 918 | int pop; |
Alexandre Oliva | 0172e2b | 2000-02-27 06:24:27 +0000 | [diff] [blame] | 919 | enum cpp_token token; |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 920 | |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 921 | here = CPP_WRITTEN (pfile); |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 922 | CPP_PUTS (pfile, "#pragma ", 8); |
Zack Weinberg | add7091 | 1998-10-29 11:54:13 +0000 | [diff] [blame] | 923 | |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 924 | key = CPP_WRITTEN (pfile); |
| 925 | pfile->no_macro_expand++; |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 926 | token = _cpp_get_directive_token (pfile); |
Alexandre Oliva | 0172e2b | 2000-02-27 06:24:27 +0000 | [diff] [blame] | 927 | if (token != CPP_NAME) |
| 928 | { |
| 929 | if (token == CPP_VSPACE) |
| 930 | goto empty; |
| 931 | else |
| 932 | goto skip; |
| 933 | } |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 934 | |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 935 | buf = pfile->token_buffer + key; |
| 936 | CPP_PUTC (pfile, ' '); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 937 | |
Neil Booth | 7ceb359 | 2000-03-11 00:49:44 +0000 | [diff] [blame] | 938 | #define tokis(x) !strncmp((char *) buf, x, sizeof(x) - 1) |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 939 | if (tokis ("once")) |
| 940 | pop = do_pragma_once (pfile); |
| 941 | else if (tokis ("implementation")) |
| 942 | pop = do_pragma_implementation (pfile); |
| 943 | else if (tokis ("poison")) |
| 944 | pop = do_pragma_poison (pfile); |
| 945 | else |
| 946 | pop = do_pragma_default (pfile); |
| 947 | #undef tokis |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 948 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 949 | if (_cpp_get_directive_token (pfile) != CPP_VSPACE) |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 950 | goto skip; |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 951 | |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 952 | if (pop) |
| 953 | CPP_SET_WRITTEN (pfile, here); |
| 954 | pfile->no_macro_expand--; |
| 955 | return 0; |
Geoff Keating | fc009f9 | 1999-09-09 04:00:37 +0000 | [diff] [blame] | 956 | |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 957 | skip: |
| 958 | cpp_error (pfile, "malformed #pragma directive"); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 959 | _cpp_skip_rest_of_line (pfile); |
Alexandre Oliva | 0172e2b | 2000-02-27 06:24:27 +0000 | [diff] [blame] | 960 | empty: |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 961 | CPP_SET_WRITTEN (pfile, here); |
| 962 | pfile->no_macro_expand--; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 963 | return 0; |
| 964 | } |
| 965 | |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 966 | static int |
| 967 | do_pragma_default (pfile) |
| 968 | cpp_reader *pfile; |
| 969 | { |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 970 | while (_cpp_get_directive_token (pfile) != CPP_VSPACE) |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 971 | CPP_PUTC (pfile, ' '); |
| 972 | return 0; |
| 973 | } |
| 974 | |
| 975 | static int |
| 976 | do_pragma_once (pfile) |
| 977 | cpp_reader *pfile; |
| 978 | { |
| 979 | cpp_buffer *ip = CPP_BUFFER (pfile); |
| 980 | |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 981 | /* Allow #pragma once in system headers, since that's not the user's |
| 982 | fault. */ |
| 983 | if (!ip->system_header_p) |
| 984 | cpp_warning (pfile, "`#pragma once' is obsolete"); |
| 985 | |
Zack Weinberg | 38b24ee | 2000-03-08 20:37:23 +0000 | [diff] [blame] | 986 | if (CPP_PREV_BUFFER (ip) == NULL) |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 987 | cpp_warning (pfile, "`#pragma once' outside include file"); |
| 988 | else |
Neil Booth | 7ceb359 | 2000-03-11 00:49:44 +0000 | [diff] [blame] | 989 | ip->ihash->control_macro = (const U_CHAR *) ""; /* never repeat */ |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 990 | |
| 991 | return 1; |
| 992 | } |
| 993 | |
| 994 | static int |
| 995 | do_pragma_implementation (pfile) |
| 996 | cpp_reader *pfile; |
| 997 | { |
| 998 | /* Be quiet about `#pragma implementation' for a file only if it hasn't |
| 999 | been included yet. */ |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1000 | enum cpp_token token; |
| 1001 | long written = CPP_WRITTEN (pfile); |
| 1002 | U_CHAR *name; |
| 1003 | U_CHAR *copy; |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 1004 | size_t len; |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1005 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1006 | token = _cpp_get_directive_token (pfile); |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1007 | if (token == CPP_VSPACE) |
| 1008 | return 0; |
| 1009 | else if (token != CPP_STRING) |
| 1010 | { |
| 1011 | cpp_error (pfile, "malformed #pragma implementation"); |
| 1012 | return 1; |
| 1013 | } |
| 1014 | |
Zack Weinberg | 0e091b5 | 2000-03-13 17:25:36 +0000 | [diff] [blame] | 1015 | /* Trim the leading and trailing quote marks from the string. */ |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1016 | name = pfile->token_buffer + written + 1; |
Zack Weinberg | 0e091b5 | 2000-03-13 17:25:36 +0000 | [diff] [blame] | 1017 | len = CPP_PWRITTEN (pfile) - name; |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 1018 | copy = (U_CHAR *) alloca (len); |
| 1019 | memcpy (copy, name, len - 1); |
Zack Weinberg | 0e091b5 | 2000-03-13 17:25:36 +0000 | [diff] [blame] | 1020 | copy[len - 1] = '\0'; |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 1021 | |
Zack Weinberg | b0699da | 2000-03-07 20:58:47 +0000 | [diff] [blame] | 1022 | if (cpp_included (pfile, copy)) |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1023 | cpp_warning (pfile, |
| 1024 | "`#pragma implementation' for `%s' appears after file is included", |
| 1025 | copy); |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1026 | return 0; |
| 1027 | } |
| 1028 | |
| 1029 | static int |
| 1030 | do_pragma_poison (pfile) |
| 1031 | cpp_reader *pfile; |
| 1032 | { |
| 1033 | /* Poison these symbols so that all subsequent usage produces an |
| 1034 | error message. */ |
| 1035 | U_CHAR *p; |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 1036 | HASHNODE **slot; |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1037 | long written; |
| 1038 | size_t len; |
| 1039 | enum cpp_token token; |
| 1040 | int writeit; |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 1041 | unsigned long hash; |
| 1042 | |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1043 | /* As a rule, don't include #pragma poison commands in output, |
| 1044 | unless the user asks for them. */ |
Zack Weinberg | ae79697 | 2000-03-31 23:16:11 +0000 | [diff] [blame] | 1045 | writeit = (CPP_OPTION (pfile, debug_output) |
| 1046 | || CPP_OPTION (pfile, dump_macros) == dump_definitions |
| 1047 | || CPP_OPTION (pfile, dump_macros) == dump_names); |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1048 | |
| 1049 | for (;;) |
| 1050 | { |
| 1051 | written = CPP_WRITTEN (pfile); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1052 | token = _cpp_get_directive_token (pfile); |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1053 | if (token == CPP_VSPACE) |
| 1054 | break; |
| 1055 | if (token != CPP_NAME) |
| 1056 | { |
| 1057 | cpp_error (pfile, "invalid #pragma poison directive"); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1058 | _cpp_skip_rest_of_line (pfile); |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1059 | return 1; |
| 1060 | } |
| 1061 | |
| 1062 | p = pfile->token_buffer + written; |
| 1063 | len = strlen (p); |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 1064 | slot = _cpp_lookup_slot (pfile, p, len, 1, &hash); |
| 1065 | if (*slot) |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1066 | { |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 1067 | HASHNODE *hp = *slot; |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1068 | if (hp->type != T_POISON) |
| 1069 | { |
| 1070 | cpp_warning (pfile, "poisoning existing macro `%s'", p); |
Zack Weinberg | b0699da | 2000-03-07 20:58:47 +0000 | [diff] [blame] | 1071 | if (hp->type == T_MACRO) |
| 1072 | _cpp_free_definition (hp->value.defn); |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1073 | hp->value.defn = 0; |
| 1074 | hp->type = T_POISON; |
| 1075 | } |
| 1076 | } |
| 1077 | else |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 1078 | { |
| 1079 | HASHNODE *hp = _cpp_make_hashnode (p, len, T_POISON, hash); |
| 1080 | hp->value.cpval = 0; |
| 1081 | *slot = hp; |
| 1082 | } |
Zack Weinberg | a2a76ce | 2000-02-11 20:17:27 +0000 | [diff] [blame] | 1083 | if (writeit) |
| 1084 | CPP_PUTC (pfile, ' '); |
| 1085 | } |
| 1086 | return !writeit; |
| 1087 | } |
| 1088 | |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1089 | /* Just ignore #sccs, on systems where we define it at all. */ |
Zack Weinberg | 07aa0b0 | 2000-04-01 22:55:25 +0000 | [diff] [blame^] | 1090 | #ifdef SCCS_DIRECTIVE |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1091 | static int |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1092 | do_sccs (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1093 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1094 | { |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1095 | _cpp_skip_rest_of_line (pfile); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1096 | return 0; |
| 1097 | } |
Zack Weinberg | 07aa0b0 | 2000-04-01 22:55:25 +0000 | [diff] [blame^] | 1098 | #endif |
Jim Meyering | 1d0e51b | 1999-08-25 22:01:36 +0000 | [diff] [blame] | 1099 | |
| 1100 | /* We've found an `#if' directive. If the only thing before it in |
| 1101 | this file is white space, and if it is of the form |
| 1102 | `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro |
| 1103 | for inclusion of this file. (See redundant_include_p in cppfiles.c |
| 1104 | for an explanation of controlling macros.) If so, return a |
| 1105 | malloc'd copy of SYMBOL. Otherwise, return NULL. */ |
| 1106 | |
| 1107 | static U_CHAR * |
| 1108 | detect_if_not_defined (pfile) |
| 1109 | cpp_reader *pfile; |
| 1110 | { |
| 1111 | U_CHAR *control_macro = 0; |
| 1112 | |
| 1113 | if (pfile->only_seen_white == 2) |
| 1114 | { |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame] | 1115 | U_CHAR *ident; |
Jim Meyering | 1d0e51b | 1999-08-25 22:01:36 +0000 | [diff] [blame] | 1116 | enum cpp_token token; |
| 1117 | int base_offset; |
| 1118 | int token_offset; |
| 1119 | int need_rparen = 0; |
| 1120 | |
| 1121 | /* Save state required for restore. */ |
| 1122 | pfile->no_macro_expand++; |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1123 | CPP_SET_MARK (pfile); |
Jim Meyering | 1d0e51b | 1999-08-25 22:01:36 +0000 | [diff] [blame] | 1124 | base_offset = CPP_WRITTEN (pfile); |
| 1125 | |
| 1126 | /* Look for `!', */ |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1127 | if (_cpp_get_directive_token (pfile) != CPP_OTHER |
Jim Meyering | 1d0e51b | 1999-08-25 22:01:36 +0000 | [diff] [blame] | 1128 | || CPP_WRITTEN (pfile) != (size_t) base_offset + 1 |
| 1129 | || CPP_PWRITTEN (pfile)[-1] != '!') |
| 1130 | goto restore; |
| 1131 | |
| 1132 | /* ...then `defined', */ |
| 1133 | token_offset = CPP_WRITTEN (pfile); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1134 | token = _cpp_get_directive_token (pfile); |
Jim Meyering | 1d0e51b | 1999-08-25 22:01:36 +0000 | [diff] [blame] | 1135 | if (token != CPP_NAME) |
| 1136 | goto restore; |
| 1137 | ident = pfile->token_buffer + token_offset; |
| 1138 | CPP_NUL_TERMINATE (pfile); |
| 1139 | if (strcmp (ident, "defined")) |
| 1140 | goto restore; |
| 1141 | |
| 1142 | /* ...then an optional '(' and the name, */ |
| 1143 | token_offset = CPP_WRITTEN (pfile); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1144 | token = _cpp_get_directive_token (pfile); |
Jim Meyering | 1d0e51b | 1999-08-25 22:01:36 +0000 | [diff] [blame] | 1145 | if (token == CPP_LPAREN) |
| 1146 | { |
| 1147 | token_offset = CPP_WRITTEN (pfile); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1148 | token = _cpp_get_directive_token (pfile); |
Jim Meyering | 1d0e51b | 1999-08-25 22:01:36 +0000 | [diff] [blame] | 1149 | if (token != CPP_NAME) |
| 1150 | goto restore; |
| 1151 | need_rparen = 1; |
| 1152 | } |
| 1153 | else if (token != CPP_NAME) |
| 1154 | goto restore; |
| 1155 | |
| 1156 | ident = pfile->token_buffer + token_offset; |
| 1157 | CPP_NUL_TERMINATE (pfile); |
| 1158 | |
| 1159 | /* ...then the ')', if necessary, */ |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1160 | if ((!need_rparen || _cpp_get_directive_token (pfile) == CPP_RPAREN) |
Jim Meyering | 1d0e51b | 1999-08-25 22:01:36 +0000 | [diff] [blame] | 1161 | /* ...and make sure there's nothing else on the line. */ |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1162 | && _cpp_get_directive_token (pfile) == CPP_VSPACE) |
Neil Booth | 7ceb359 | 2000-03-11 00:49:44 +0000 | [diff] [blame] | 1163 | control_macro = (U_CHAR *) xstrdup (ident); |
Jim Meyering | 1d0e51b | 1999-08-25 22:01:36 +0000 | [diff] [blame] | 1164 | |
| 1165 | restore: |
| 1166 | CPP_SET_WRITTEN (pfile, base_offset); |
| 1167 | pfile->no_macro_expand--; |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1168 | CPP_GOTO_MARK (pfile); |
Jim Meyering | 1d0e51b | 1999-08-25 22:01:36 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | return control_macro; |
| 1172 | } |
| 1173 | |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1174 | /* |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1175 | * #if is straightforward; just call _cpp_parse_expr, then conditional_skip. |
Zack Weinberg | 88ae23e | 2000-03-08 23:35:19 +0000 | [diff] [blame] | 1176 | * Also, check for a reinclude preventer of the form #if !defined (MACRO). |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1177 | */ |
| 1178 | |
| 1179 | static int |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1180 | do_if (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1181 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1182 | { |
Jim Meyering | 1d0e51b | 1999-08-25 22:01:36 +0000 | [diff] [blame] | 1183 | U_CHAR *control_macro = detect_if_not_defined (pfile); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1184 | int value = _cpp_parse_expr (pfile); |
Jim Meyering | 1d0e51b | 1999-08-25 22:01:36 +0000 | [diff] [blame] | 1185 | conditional_skip (pfile, value == 0, T_IF, control_macro); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1186 | return 0; |
| 1187 | } |
| 1188 | |
| 1189 | /* |
| 1190 | * handle a #elif directive by not changing if_stack either. |
| 1191 | * see the comment above do_else. |
| 1192 | */ |
| 1193 | |
| 1194 | static int |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1195 | do_elif (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1196 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1197 | { |
Zack Weinberg | 40ea76d | 2000-02-06 07:30:25 +0000 | [diff] [blame] | 1198 | if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) |
| 1199 | { |
| 1200 | cpp_error (pfile, "`#elif' not within a conditional"); |
| 1201 | return 0; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1202 | } |
Zack Weinberg | 40ea76d | 2000-02-06 07:30:25 +0000 | [diff] [blame] | 1203 | else |
| 1204 | { |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1205 | if (pfile->if_stack->type == T_ELSE) |
Zack Weinberg | 40ea76d | 2000-02-06 07:30:25 +0000 | [diff] [blame] | 1206 | { |
| 1207 | cpp_error (pfile, "`#elif' after `#else'"); |
| 1208 | cpp_error_with_line (pfile, pfile->if_stack->lineno, -1, |
| 1209 | "the conditional began here"); |
| 1210 | } |
| 1211 | pfile->if_stack->type = T_ELIF; |
| 1212 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1213 | |
| 1214 | if (pfile->if_stack->if_succeeded) |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1215 | skip_if_group (pfile); |
Zack Weinberg | 40ea76d | 2000-02-06 07:30:25 +0000 | [diff] [blame] | 1216 | else |
| 1217 | { |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1218 | if (_cpp_parse_expr (pfile) == 0) |
Zack Weinberg | 40ea76d | 2000-02-06 07:30:25 +0000 | [diff] [blame] | 1219 | skip_if_group (pfile); |
| 1220 | else |
| 1221 | { |
| 1222 | ++pfile->if_stack->if_succeeded; /* continue processing input */ |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1223 | _cpp_output_line_command (pfile, same_file); |
Zack Weinberg | 40ea76d | 2000-02-06 07:30:25 +0000 | [diff] [blame] | 1224 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1225 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1226 | return 0; |
| 1227 | } |
| 1228 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1229 | /* Parse an #ifdef or #ifndef directive. Returns 1 for defined, 0 for |
| 1230 | not defined; the macro tested is left in the token buffer (but |
| 1231 | popped). */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1232 | |
| 1233 | static int |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1234 | parse_ifdef (pfile, name) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1235 | cpp_reader *pfile; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1236 | const char *name; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1237 | { |
Mike Stump | 0f41302 | 1996-07-03 22:07:53 +0000 | [diff] [blame] | 1238 | U_CHAR *ident; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1239 | unsigned int len; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1240 | enum cpp_token token; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1241 | long old_written = CPP_WRITTEN (pfile); |
| 1242 | int defined; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1243 | |
| 1244 | pfile->no_macro_expand++; |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1245 | token = _cpp_get_directive_token (pfile); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1246 | pfile->no_macro_expand--; |
| 1247 | |
| 1248 | ident = pfile->token_buffer + old_written; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1249 | len = CPP_WRITTEN (pfile) - old_written; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1250 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1251 | if (token == CPP_VSPACE) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1252 | { |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1253 | if (! CPP_TRADITIONAL (pfile)) |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1254 | cpp_pedwarn (pfile, "`#%s' with no argument", name); |
| 1255 | defined = 0; |
| 1256 | goto done; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1257 | } |
| 1258 | else if (token == CPP_NAME) |
| 1259 | { |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1260 | defined = cpp_defined (pfile, ident, len); |
| 1261 | CPP_NUL_TERMINATE (pfile); |
| 1262 | CPP_ADJUST_WRITTEN (pfile, 1); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1263 | } |
| 1264 | else |
| 1265 | { |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1266 | defined = 0; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1267 | if (! CPP_TRADITIONAL (pfile)) |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1268 | cpp_error (pfile, "`#%s' with invalid argument", name); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | if (!CPP_TRADITIONAL (pfile)) |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1272 | { |
| 1273 | if (_cpp_get_directive_token (pfile) == CPP_VSPACE) |
| 1274 | goto done; |
| 1275 | |
| 1276 | cpp_pedwarn (pfile, "garbage at end of `#%s' argument", name); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1277 | } |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1278 | _cpp_skip_rest_of_line (pfile); |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1279 | |
| 1280 | done: |
| 1281 | CPP_SET_WRITTEN (pfile, old_written); /* Pop */ |
| 1282 | return defined; |
| 1283 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1284 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1285 | /* #ifdef is dead simple. */ |
| 1286 | |
| 1287 | static int |
| 1288 | do_ifdef (pfile) |
| 1289 | cpp_reader *pfile; |
| 1290 | { |
| 1291 | int skip = ! parse_ifdef (pfile, dtable[T_IFDEF].name); |
| 1292 | conditional_skip (pfile, skip, T_IFDEF, 0); |
| 1293 | return 0; |
| 1294 | } |
| 1295 | |
| 1296 | /* #ifndef is a tad more complex, because we need to check for a |
| 1297 | no-reinclusion wrapper. */ |
| 1298 | |
| 1299 | static int |
| 1300 | do_ifndef (pfile) |
| 1301 | cpp_reader *pfile; |
| 1302 | { |
| 1303 | int start_of_file, skip; |
| 1304 | U_CHAR *control_macro = 0; |
| 1305 | |
| 1306 | start_of_file = pfile->only_seen_white == 2; |
| 1307 | skip = parse_ifdef (pfile, dtable[T_IFNDEF].name); |
| 1308 | |
| 1309 | if (start_of_file && !skip) |
| 1310 | control_macro = xstrdup (CPP_PWRITTEN (pfile)); |
| 1311 | |
| 1312 | conditional_skip (pfile, skip, T_IFNDEF, control_macro); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1313 | return 0; |
| 1314 | } |
| 1315 | |
| 1316 | /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead. |
| 1317 | If this is a #ifndef starting at the beginning of a file, |
| 1318 | CONTROL_MACRO is the macro name tested by the #ifndef. |
| 1319 | Otherwise, CONTROL_MACRO is 0. */ |
| 1320 | |
| 1321 | static void |
| 1322 | conditional_skip (pfile, skip, type, control_macro) |
| 1323 | cpp_reader *pfile; |
| 1324 | int skip; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1325 | int type; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1326 | U_CHAR *control_macro; |
| 1327 | { |
Zack Weinberg | 38b24ee | 2000-03-08 20:37:23 +0000 | [diff] [blame] | 1328 | IF_STACK *temp; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1329 | |
Zack Weinberg | 38b24ee | 2000-03-08 20:37:23 +0000 | [diff] [blame] | 1330 | temp = (IF_STACK *) xcalloc (1, sizeof (IF_STACK)); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1331 | temp->lineno = CPP_BUFFER (pfile)->lineno; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1332 | temp->next = pfile->if_stack; |
| 1333 | temp->control_macro = control_macro; |
| 1334 | pfile->if_stack = temp; |
| 1335 | |
| 1336 | pfile->if_stack->type = type; |
| 1337 | |
| 1338 | if (skip != 0) { |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1339 | skip_if_group (pfile); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1340 | return; |
| 1341 | } else { |
| 1342 | ++pfile->if_stack->if_succeeded; |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1343 | _cpp_output_line_command (pfile, same_file); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1344 | } |
| 1345 | } |
| 1346 | |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1347 | /* Subroutine of skip_if_group. Examine one preprocessing directive and |
| 1348 | return 0 if skipping should continue, 1 if it should halt. Also |
| 1349 | adjusts the if_stack as appropriate. |
| 1350 | The `#' has been read, but not the identifier. */ |
Mike Stump | 0f41302 | 1996-07-03 22:07:53 +0000 | [diff] [blame] | 1351 | |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1352 | static int |
| 1353 | consider_directive_while_skipping (pfile, stack) |
| 1354 | cpp_reader *pfile; |
Zack Weinberg | 38b24ee | 2000-03-08 20:37:23 +0000 | [diff] [blame] | 1355 | IF_STACK *stack; |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1356 | { |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1357 | long ident; |
Zack Weinberg | 2ac9349 | 1999-08-31 19:46:18 +0000 | [diff] [blame] | 1358 | const struct directive *kt; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1359 | int i; |
| 1360 | unsigned int len; |
Zack Weinberg | 38b24ee | 2000-03-08 20:37:23 +0000 | [diff] [blame] | 1361 | IF_STACK *temp; |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1362 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1363 | _cpp_skip_hspace (pfile); |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1364 | |
| 1365 | ident = CPP_WRITTEN (pfile); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1366 | _cpp_parse_name (pfile, GETC()); |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1367 | len = CPP_WRITTEN (pfile) - ident; |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1368 | |
| 1369 | CPP_SET_WRITTEN (pfile, ident); |
| 1370 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1371 | for (i = 0; i < N_DIRECTIVES; i++) |
| 1372 | { |
| 1373 | kt = &dtable[i]; |
| 1374 | if (kt->length == len |
| 1375 | && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0) |
| 1376 | switch (i) |
| 1377 | { |
| 1378 | case T_IF: |
| 1379 | case T_IFDEF: |
| 1380 | case T_IFNDEF: |
Zack Weinberg | 38b24ee | 2000-03-08 20:37:23 +0000 | [diff] [blame] | 1381 | temp = (IF_STACK *) xmalloc (sizeof (IF_STACK)); |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1382 | temp->next = pfile->if_stack; |
| 1383 | pfile->if_stack = temp; |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1384 | temp->type = i; |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1385 | return 0; |
| 1386 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1387 | case T_ELSE: |
Zack Weinberg | 75ec21d | 2000-01-27 22:29:07 +0000 | [diff] [blame] | 1388 | if (pfile->if_stack != stack) |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1389 | validate_else (pfile, dtable[i].name); |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1390 | /* fall through */ |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1391 | case T_ELIF: |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1392 | if (pfile->if_stack == stack) |
| 1393 | return 1; |
| 1394 | else |
| 1395 | { |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1396 | pfile->if_stack->type = i; |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1397 | return 0; |
| 1398 | } |
| 1399 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1400 | case T_ENDIF: |
| 1401 | if (pfile->if_stack != stack) |
| 1402 | validate_else (pfile, dtable[i].name); |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1403 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1404 | if (pfile->if_stack == stack) |
| 1405 | return 1; |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1406 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1407 | temp = pfile->if_stack; |
| 1408 | pfile->if_stack = temp->next; |
| 1409 | free (temp); |
| 1410 | return 0; |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1411 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1412 | default: |
| 1413 | return 0; |
| 1414 | } |
| 1415 | } |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1416 | |
| 1417 | /* Don't let erroneous code go by. */ |
Zack Weinberg | ae79697 | 2000-03-31 23:16:11 +0000 | [diff] [blame] | 1418 | if (!CPP_OPTION (pfile, lang_asm) && CPP_PEDANTIC (pfile)) |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1419 | cpp_pedwarn (pfile, "invalid preprocessor directive name"); |
| 1420 | return 0; |
| 1421 | } |
| 1422 | |
| 1423 | /* skip to #endif, #else, or #elif. adjust line numbers, etc. |
| 1424 | * leaves input ptr at the sharp sign found. |
| 1425 | */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1426 | static void |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1427 | skip_if_group (pfile) |
| 1428 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1429 | { |
| 1430 | int c; |
Zack Weinberg | 38b24ee | 2000-03-08 20:37:23 +0000 | [diff] [blame] | 1431 | IF_STACK *save_if_stack = pfile->if_stack; /* don't pop past here */ |
Neil Booth | 7ceb359 | 2000-03-11 00:49:44 +0000 | [diff] [blame] | 1432 | const U_CHAR *beg_of_line; |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1433 | long old_written; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1434 | |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1435 | old_written = CPP_WRITTEN (pfile); |
| 1436 | |
| 1437 | for (;;) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1438 | { |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1439 | beg_of_line = CPP_BUFFER (pfile)->cur; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1440 | |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1441 | if (! CPP_TRADITIONAL (pfile)) |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1442 | _cpp_skip_hspace (pfile); |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1443 | c = GETC(); |
| 1444 | if (c == '\n') |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1445 | { |
Zack Weinberg | 3fdc651 | 1999-03-16 13:10:15 +0000 | [diff] [blame] | 1446 | CPP_BUMP_LINE (pfile); |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1447 | continue; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1448 | } |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1449 | else if (c == '#') |
| 1450 | { |
| 1451 | if (consider_directive_while_skipping (pfile, save_if_stack)) |
| 1452 | break; |
| 1453 | } |
| 1454 | else if (c == EOF) |
| 1455 | return; /* Caller will issue error. */ |
| 1456 | |
| 1457 | FORWARD(-1); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1458 | _cpp_skip_rest_of_line (pfile); |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1459 | |
| 1460 | c = GETC(); |
| 1461 | if (c == EOF) |
| 1462 | return; /* Caller will issue error. */ |
| 1463 | else |
Zack Weinberg | ba412f1 | 2000-03-01 00:57:09 +0000 | [diff] [blame] | 1464 | CPP_BUMP_LINE (pfile); |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | /* Back up to the beginning of this line. Caller will process the |
| 1468 | directive. */ |
| 1469 | CPP_BUFFER (pfile)->cur = beg_of_line; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1470 | pfile->only_seen_white = 1; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | /* |
| 1474 | * handle a #else directive. Do this by just continuing processing |
| 1475 | * without changing if_stack ; this is so that the error message |
| 1476 | * for missing #endif's etc. will point to the original #if. It |
| 1477 | * is possible that something different would be better. |
| 1478 | */ |
| 1479 | |
| 1480 | static int |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1481 | do_else (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1482 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1483 | { |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1484 | validate_else (pfile, dtable[T_ELSE].name); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1485 | _cpp_skip_rest_of_line (pfile); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1486 | |
Zack Weinberg | 40ea76d | 2000-02-06 07:30:25 +0000 | [diff] [blame] | 1487 | if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) |
| 1488 | { |
| 1489 | cpp_error (pfile, "`#else' not within a conditional"); |
| 1490 | return 0; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1491 | } |
Zack Weinberg | 40ea76d | 2000-02-06 07:30:25 +0000 | [diff] [blame] | 1492 | else |
| 1493 | { |
| 1494 | /* #ifndef can't have its special treatment for containing the whole file |
| 1495 | if it has a #else clause. */ |
| 1496 | pfile->if_stack->control_macro = 0; |
| 1497 | |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1498 | if (pfile->if_stack->type == T_ELSE) |
Zack Weinberg | 40ea76d | 2000-02-06 07:30:25 +0000 | [diff] [blame] | 1499 | { |
| 1500 | cpp_error (pfile, "`#else' after `#else'"); |
| 1501 | cpp_error_with_line (pfile, pfile->if_stack->lineno, -1, |
| 1502 | "the conditional began here"); |
| 1503 | } |
| 1504 | pfile->if_stack->type = T_ELSE; |
| 1505 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1506 | |
| 1507 | if (pfile->if_stack->if_succeeded) |
Zack Weinberg | ed705a8 | 1999-01-04 12:38:22 +0000 | [diff] [blame] | 1508 | skip_if_group (pfile); |
Zack Weinberg | 40ea76d | 2000-02-06 07:30:25 +0000 | [diff] [blame] | 1509 | else |
| 1510 | { |
| 1511 | ++pfile->if_stack->if_succeeded; /* continue processing input */ |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1512 | _cpp_output_line_command (pfile, same_file); |
Zack Weinberg | 40ea76d | 2000-02-06 07:30:25 +0000 | [diff] [blame] | 1513 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1514 | return 0; |
| 1515 | } |
| 1516 | |
| 1517 | /* |
| 1518 | * unstack after #endif command |
| 1519 | */ |
| 1520 | |
| 1521 | static int |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1522 | do_endif (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1523 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1524 | { |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1525 | validate_else (pfile, dtable[T_ENDIF].name); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1526 | _cpp_skip_rest_of_line (pfile); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1527 | |
| 1528 | if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) |
Zack Weinberg | 6ee2c97 | 1999-09-11 05:38:06 +0000 | [diff] [blame] | 1529 | cpp_error (pfile, "`#endif' not within a conditional"); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1530 | else |
| 1531 | { |
Zack Weinberg | 38b24ee | 2000-03-08 20:37:23 +0000 | [diff] [blame] | 1532 | IF_STACK *temp = pfile->if_stack; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1533 | pfile->if_stack = temp->next; |
| 1534 | if (temp->control_macro != 0) |
| 1535 | { |
| 1536 | /* This #endif matched a #ifndef at the start of the file. |
| 1537 | See if it is at the end of the file. */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1538 | int c; |
| 1539 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1540 | CPP_SET_MARK (pfile); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1541 | |
| 1542 | for (;;) |
| 1543 | { |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1544 | _cpp_skip_hspace (pfile); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1545 | c = GETC (); |
| 1546 | if (c != '\n') |
| 1547 | break; |
| 1548 | } |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1549 | CPP_GOTO_MARK (pfile); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1550 | |
| 1551 | if (c == EOF) |
| 1552 | { |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 1553 | /* This #endif ends a #ifndef |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1554 | that contains all of the file (aside from whitespace). |
| 1555 | Arrange not to include the file again |
Zack Weinberg | 0b3d776 | 1998-11-25 11:56:54 +0000 | [diff] [blame] | 1556 | if the macro that was tested is defined. */ |
Zack Weinberg | 38b24ee | 2000-03-08 20:37:23 +0000 | [diff] [blame] | 1557 | CPP_BUFFER (pfile)->ihash->control_macro = temp->control_macro; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1558 | } |
| 1559 | } |
| 1560 | free (temp); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1561 | _cpp_output_line_command (pfile, same_file); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1562 | } |
| 1563 | return 0; |
| 1564 | } |
| 1565 | |
Zack Weinberg | 75ec21d | 2000-01-27 22:29:07 +0000 | [diff] [blame] | 1566 | /* Issue -pedantic warning for text which is not a comment following |
| 1567 | an #else or #endif. Do not warn in system headers, as this is harmless |
| 1568 | and very common on old systems. */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1569 | |
| 1570 | static void |
| 1571 | validate_else (pfile, directive) |
| 1572 | cpp_reader *pfile; |
Zack Weinberg | 2ac9349 | 1999-08-31 19:46:18 +0000 | [diff] [blame] | 1573 | const char *directive; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1574 | { |
Jason Merrill | 83ecd27 | 2000-03-03 00:09:22 +0000 | [diff] [blame] | 1575 | if (! CPP_PEDANTIC (pfile)) |
Zack Weinberg | 75ec21d | 2000-01-27 22:29:07 +0000 | [diff] [blame] | 1576 | return; |
| 1577 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1578 | _cpp_skip_hspace (pfile); |
Zack Weinberg | 75ec21d | 2000-01-27 22:29:07 +0000 | [diff] [blame] | 1579 | if (PEEKC () != '\n') |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1580 | cpp_pedwarn (pfile, |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1581 | "text following `#%s' violates ANSI standard", directive); |
Zack Weinberg | 6ee2c97 | 1999-09-11 05:38:06 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1584 | void |
| 1585 | _cpp_handle_eof (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1586 | cpp_reader *pfile; |
| 1587 | { |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1588 | cpp_buffer *next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile)); |
| 1589 | struct if_stack *ifs, *nifs; |
Zack Weinberg | 4d9a1b4 | 1999-02-15 14:04:21 +0000 | [diff] [blame] | 1590 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1591 | /* Unwind the conditional stack and generate error messages. */ |
| 1592 | for (ifs = pfile->if_stack; |
| 1593 | ifs != CPP_BUFFER (pfile)->if_stack; |
| 1594 | ifs = nifs) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1595 | { |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1596 | cpp_error_with_line (pfile, ifs->lineno, -1, |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1597 | "unterminated `#%s' conditional", |
| 1598 | dtable[ifs->type].name); |
Zack Weinberg | 4d9a1b4 | 1999-02-15 14:04:21 +0000 | [diff] [blame] | 1599 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1600 | nifs = ifs->next; |
| 1601 | free (ifs); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1602 | } |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1603 | pfile->if_stack = ifs; |
| 1604 | |
| 1605 | if (CPP_BUFFER (pfile)->nominal_fname && next_buf != NULL) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1606 | { |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1607 | /* We're about to return from an #include file. |
| 1608 | Emit #line information now (as part of the CPP_POP) result. |
| 1609 | But the #line refers to the file we will pop to. */ |
| 1610 | cpp_buffer *cur_buffer = CPP_BUFFER (pfile); |
| 1611 | CPP_BUFFER (pfile) = next_buf; |
| 1612 | pfile->input_stack_listing_current = 0; |
| 1613 | _cpp_output_line_command (pfile, leave_file); |
| 1614 | CPP_BUFFER (pfile) = cur_buffer; |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1615 | } |
| 1616 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1617 | CPP_BUFFER (pfile)->seen_eof = 1; |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1620 | static int |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1621 | do_assert (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1622 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1623 | { |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame] | 1624 | U_CHAR *sym; |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1625 | int ret, c; |
| 1626 | HASHNODE *base, *this; |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 1627 | HASHNODE **bslot, **tslot; |
| 1628 | size_t blen, tlen; |
| 1629 | unsigned long bhash, thash; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1630 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1631 | _cpp_skip_hspace (pfile); |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame] | 1632 | sym = CPP_PWRITTEN (pfile); /* remember where it starts */ |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1633 | ret = _cpp_parse_assertion (pfile); |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1634 | if (ret == 0) |
| 1635 | goto error; |
| 1636 | else if (ret == 1) |
| 1637 | { |
| 1638 | cpp_error (pfile, "missing token-sequence in `#assert'"); |
| 1639 | goto error; |
| 1640 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1641 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1642 | _cpp_skip_hspace (pfile); |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1643 | c = PEEKC(); |
| 1644 | if (c != EOF && c != '\n') |
| 1645 | { |
| 1646 | cpp_error (pfile, "junk at end of `#assert'"); |
| 1647 | goto error; |
| 1648 | } |
| 1649 | |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 1650 | tlen = strlen (sym); |
| 1651 | blen = (U_CHAR *) strchr (sym, '(') - sym; |
| 1652 | tslot = _cpp_lookup_slot (pfile, sym, tlen, 1, &thash); |
| 1653 | if (*tslot) |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1654 | { |
| 1655 | cpp_warning (pfile, "`%s' re-asserted", sym); |
| 1656 | goto error; |
| 1657 | } |
| 1658 | |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 1659 | bslot = _cpp_lookup_slot (pfile, sym, blen, 1, &bhash); |
| 1660 | if (! *bslot) |
| 1661 | *bslot = base = _cpp_make_hashnode (sym, blen, T_ASSERT, bhash); |
| 1662 | else |
| 1663 | { |
| 1664 | base = *bslot; |
| 1665 | if (base->type != T_ASSERT) |
| 1666 | { |
| 1667 | /* Token clash - but with what?! */ |
| 1668 | cpp_ice (pfile, "base->type != T_ASSERT in do_assert"); |
| 1669 | goto error; |
| 1670 | } |
| 1671 | } |
| 1672 | *tslot = this = _cpp_make_hashnode (sym, tlen, T_ASSERT, thash); |
| 1673 | this->value.aschain = base->value.aschain; |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1674 | base->value.aschain = this; |
| 1675 | |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame] | 1676 | pfile->limit = sym; /* Pop */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1677 | return 0; |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1678 | |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1679 | error: |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1680 | _cpp_skip_rest_of_line (pfile); |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame] | 1681 | pfile->limit = sym; /* Pop */ |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 1682 | return 0; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1683 | } |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1684 | |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1685 | static int |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1686 | do_unassert (pfile) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1687 | cpp_reader *pfile; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1688 | { |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1689 | int c, ret; |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame] | 1690 | U_CHAR *sym; |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1691 | long baselen, thislen; |
| 1692 | HASHNODE *base, *this, *next; |
| 1693 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1694 | _cpp_skip_hspace (pfile); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1695 | |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame] | 1696 | sym = CPP_PWRITTEN (pfile); /* remember where it starts */ |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1697 | ret = _cpp_parse_assertion (pfile); |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1698 | if (ret == 0) |
| 1699 | goto error; |
| 1700 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1701 | _cpp_skip_hspace (pfile); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1702 | c = PEEKC (); |
| 1703 | if (c != EOF && c != '\n') |
| 1704 | cpp_error (pfile, "junk at end of `#unassert'"); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1705 | |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1706 | thislen = strlen (sym); |
| 1707 | if (ret == 1) |
| 1708 | { |
Zack Weinberg | b0699da | 2000-03-07 20:58:47 +0000 | [diff] [blame] | 1709 | base = _cpp_lookup (pfile, sym, thislen); |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1710 | if (! base) |
| 1711 | goto error; /* It isn't an error to #undef what isn't #defined, |
| 1712 | so it isn't an error to #unassert what isn't |
| 1713 | #asserted either. */ |
| 1714 | |
| 1715 | for (this = base->value.aschain; this; this = next) |
| 1716 | { |
| 1717 | next = this->value.aschain; |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 1718 | htab_remove_elt (pfile->hashtab, this); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1719 | } |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 1720 | htab_remove_elt (pfile->hashtab, base); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1721 | } |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1722 | else |
| 1723 | { |
Neil Booth | 7ceb359 | 2000-03-11 00:49:44 +0000 | [diff] [blame] | 1724 | baselen = (U_CHAR *) strchr (sym, '(') - sym; |
Zack Weinberg | b0699da | 2000-03-07 20:58:47 +0000 | [diff] [blame] | 1725 | base = _cpp_lookup (pfile, sym, baselen); |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1726 | if (! base) goto error; |
Zack Weinberg | b0699da | 2000-03-07 20:58:47 +0000 | [diff] [blame] | 1727 | this = _cpp_lookup (pfile, sym, thislen); |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1728 | if (! this) goto error; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1729 | |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1730 | next = base; |
| 1731 | while (next->value.aschain != this) |
| 1732 | next = next->value.aschain; |
| 1733 | |
| 1734 | next->value.aschain = this->value.aschain; |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 1735 | htab_remove_elt (pfile->hashtab, this); |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1736 | |
| 1737 | if (base->value.aschain == NULL) |
Zack Weinberg | d35364d | 2000-03-12 23:46:05 +0000 | [diff] [blame] | 1738 | /* Last answer for this predicate deleted. */ |
| 1739 | htab_remove_elt (pfile->hashtab, base); |
Zack Weinberg | 7061aa5 | 1998-12-15 11:09:16 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame] | 1742 | pfile->limit = sym; /* Pop */ |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1743 | return 0; |
| 1744 | error: |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1745 | _cpp_skip_rest_of_line (pfile); |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame] | 1746 | pfile->limit = sym; /* Pop */ |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 1747 | return 0; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1748 | } |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1749 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1750 | /* These are for -D, -U, -A. */ |
| 1751 | |
| 1752 | /* Process the string STR as if it appeared as the body of a #define. |
| 1753 | If STR is just an identifier, define it with value 1. |
| 1754 | If STR has anything after the identifier, then it should |
| 1755 | be identifier=definition. */ |
| 1756 | |
| 1757 | void |
| 1758 | cpp_define (pfile, str) |
| 1759 | cpp_reader *pfile; |
| 1760 | const char *str; |
| 1761 | { |
| 1762 | char *buf, *p; |
| 1763 | size_t count; |
| 1764 | |
| 1765 | p = strchr (str, '='); |
| 1766 | /* Copy the entire option so we can modify it. |
| 1767 | Change the first "=" in the string to a space. If there is none, |
| 1768 | tack " 1" on the end. Then add a newline and a NUL. */ |
| 1769 | |
| 1770 | if (p) |
| 1771 | { |
| 1772 | count = strlen (str) + 2; |
| 1773 | buf = alloca (count); |
| 1774 | memcpy (buf, str, count - 2); |
| 1775 | buf[p - str] = ' '; |
| 1776 | buf[count - 2] = '\n'; |
| 1777 | buf[count - 1] = '\0'; |
| 1778 | } |
| 1779 | else |
| 1780 | { |
| 1781 | count = strlen (str) + 4; |
| 1782 | buf = alloca (count); |
| 1783 | memcpy (buf, str, count - 4); |
| 1784 | strcpy (&buf[count-4], " 1\n"); |
| 1785 | } |
| 1786 | |
| 1787 | if (cpp_push_buffer (pfile, buf, count - 1) != NULL) |
| 1788 | { |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1789 | do_define (pfile); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1790 | cpp_pop_buffer (pfile); |
| 1791 | } |
| 1792 | } |
| 1793 | |
| 1794 | /* Process MACRO as if it appeared as the body of an #undef. */ |
| 1795 | void |
| 1796 | cpp_undef (pfile, macro) |
| 1797 | cpp_reader *pfile; |
| 1798 | const char *macro; |
| 1799 | { |
| 1800 | /* Copy the string so we can append a newline. */ |
| 1801 | size_t len = strlen (macro); |
| 1802 | char *buf = alloca (len + 2); |
| 1803 | memcpy (buf, macro, len); |
| 1804 | buf[len] = '\n'; |
| 1805 | buf[len + 1] = '\0'; |
| 1806 | if (cpp_push_buffer (pfile, buf, len + 1)) |
| 1807 | { |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1808 | do_undef (pfile); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1809 | cpp_pop_buffer (pfile); |
| 1810 | } |
| 1811 | } |
| 1812 | |
| 1813 | /* Process the string STR as if it appeared as the body of a #assert. */ |
| 1814 | void |
| 1815 | cpp_assert (pfile, str) |
| 1816 | cpp_reader *pfile; |
| 1817 | const char *str; |
| 1818 | { |
| 1819 | if (cpp_push_buffer (pfile, str, strlen (str)) != NULL) |
| 1820 | { |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1821 | do_assert (pfile); |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1822 | cpp_pop_buffer (pfile); |
| 1823 | } |
| 1824 | } |
| 1825 | |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 1826 | /* Process STR as if it appeared as the body of an #unassert. */ |
| 1827 | void |
| 1828 | cpp_unassert (pfile, str) |
| 1829 | cpp_reader *pfile; |
Neil Booth | 7ceb359 | 2000-03-11 00:49:44 +0000 | [diff] [blame] | 1830 | const char *str; |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 1831 | { |
| 1832 | if (cpp_push_buffer (pfile, str, strlen (str)) != NULL) |
| 1833 | { |
Zack Weinberg | 168d373 | 2000-03-14 06:34:11 +0000 | [diff] [blame] | 1834 | do_unassert (pfile); |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 1835 | cpp_pop_buffer (pfile); |
| 1836 | } |
| 1837 | } |
| 1838 | |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1839 | /* Determine whether the identifier ID, of length LEN, is a defined macro. */ |
| 1840 | int |
| 1841 | cpp_defined (pfile, id, len) |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1842 | cpp_reader *pfile; |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1843 | const U_CHAR *id; |
| 1844 | int len; |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1845 | { |
Zack Weinberg | 45b966d | 2000-03-13 22:01:08 +0000 | [diff] [blame] | 1846 | HASHNODE *hp = _cpp_lookup (pfile, id, len); |
| 1847 | if (hp && hp->type == T_POISON) |
| 1848 | { |
| 1849 | cpp_error (pfile, "attempt to use poisoned `%s'", hp->name); |
| 1850 | return 0; |
| 1851 | } |
| 1852 | return (hp != NULL); |
Per Bothner | 7f2935c | 1995-03-16 13:59:07 -0800 | [diff] [blame] | 1853 | } |