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