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