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
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
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.
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. */
30 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) \
31 ? CPP_BUFFER (pfile)->cur[N] : EOF)
32 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
33 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
34 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
36 /* `struct directive' defines one #-directive, including how to handle it. */
40 unsigned int length; /* Length of name */
41 const char *name; /* Name of directive */
42 int (*func) /* Function to handle directive */
43 PARAMS ((cpp_reader *));
46 /* Stack of conditionals currently in progress
47 (including both successful and failing conditionals). */
51 struct if_stack *next;
52 int lineno; /* line number where condition started */
53 int if_succeeded; /* truth of last condition in this group */
54 const U_CHAR *control_macro; /* macro name for #ifndef around entire file */
55 int type; /* type of last directive seen in this group */
57 typedef struct if_stack IF_STACK;
59 /* Forward declarations. */
61 static void validate_else PARAMS ((cpp_reader *, const char *));
62 static int parse_ifdef PARAMS ((cpp_reader *, const char *));
63 static unsigned int parse_include PARAMS ((cpp_reader *, const char *));
64 static void conditional_skip PARAMS ((cpp_reader *, int, int,
66 static void skip_if_group PARAMS ((cpp_reader *));
67 static void pass_thru_directive PARAMS ((const U_CHAR *, size_t,
69 static int read_line_number PARAMS ((cpp_reader *, int *));
70 static U_CHAR *detect_if_not_defined PARAMS ((cpp_reader *));
71 static int consider_directive_while_skipping
72 PARAMS ((cpp_reader *, IF_STACK *));
73 static int get_macro_name PARAMS ((cpp_reader *));
75 /* This is the table of directive handlers. It is ordered by
76 frequency of occurrence; the numbers at the end are directive
77 counts from all the source code I have lying around (egcs and libc
78 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
81 The entries with a dash and a name after the count are extensions,
82 of which all but #warning and #include_next are deprecated. The name
83 is where the extension appears to have come from. */
85 #define DIRECTIVE_TABLE \
86 D("define", do_define, T_DEFINE) /* 270554 */ \
87 D("include", do_include, T_INCLUDE) /* 52262 */ \
88 D("endif", do_endif, T_ENDIF) /* 45855 */ \
89 D("ifdef", do_ifdef, T_IFDEF) /* 22000 */ \
90 D("if", do_if, T_IF) /* 18162 */ \
91 D("else", do_else, T_ELSE) /* 9863 */ \
92 D("ifndef", do_ifndef, T_IFNDEF) /* 9675 */ \
93 D("undef", do_undef, T_UNDEF) /* 4837 */ \
94 D("line", do_line, T_LINE) /* 2465 */ \
95 D("elif", do_elif, T_ELIF) /* 610 */ \
96 D("error", do_error, T_ERROR) /* 475 */ \
97 D("pragma", do_pragma, T_PRAGMA) /* 195 */ \
98 D("warning", do_warning, T_WARNING) /* 22 - GNU */ \
99 D("include_next", do_include_next, T_INCLUDE_NEXT) /* 19 - GNU */ \
100 D("ident", do_ident, T_IDENT) /* 11 - SVR4 */ \
101 D("import", do_import, T_IMPORT) /* 0 - ObjC */ \
102 D("assert", do_assert, T_ASSERT) /* 0 - SVR4 */ \
103 D("unassert", do_unassert, T_UNASSERT) /* 0 - SVR4 */ \
104 D("sccs", do_sccs, T_SCCS) /* 0 - SVR2? */
106 /* Use the table to generate a series of prototypes, an enum for the
107 directive names, and an array of directive handlers. */
109 /* The directive-processing functions are declared to return int
110 instead of void, because some old compilers have trouble with
111 pointers to functions returning void. */
113 #define D(name, fun, tag) static int fun PARAMS ((cpp_reader *));
117 #define D(name, fun, tag) tag,
125 #define D(name, fun, tag) { sizeof name - 1, name, fun },
126 static const struct directive dtable[] =
131 #undef DIRECTIVE_TABLE
133 /* Handle a possible # directive.
134 '#' has already been read. */
137 _cpp_handle_directive (pfile)
143 long old_written = CPP_WRITTEN (pfile);
145 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
147 cpp_ice (pfile, "handle_directive called on macro buffer");
151 _cpp_skip_hspace (pfile);
154 /* # followed by a number is equivalent to #line. Do not recognize
155 this form in assembly language source files. Complain about this
156 form if we're being pedantic, but not if this is regurgitated
157 input (preprocessed or fed back in by the C++ frontend). */
158 if (c >= '0' && c <= '9')
160 if (CPP_OPTION (pfile, lang_asm))
163 if (CPP_PEDANTIC (pfile)
164 && ! CPP_OPTION (pfile, preprocessed)
165 && ! CPP_BUFFER (pfile)->manual_pop)
166 cpp_pedwarn (pfile, "`#' followed by integer");
171 /* If we are rescanning preprocessed input, don't obey any directives
173 if (CPP_OPTION (pfile, preprocessed))
176 /* Now find the directive name. */
177 CPP_PUTC (pfile, '#');
178 _cpp_parse_name (pfile, GETC());
179 ident = pfile->token_buffer + old_written + 1;
180 len = CPP_PWRITTEN (pfile) - ident;
183 /* A line of just `#' becomes blank. A line with something
184 other than an identifier after the # is reparsed as a non-
186 CPP_SET_WRITTEN (pfile, old_written);
187 return (PEEKC() == '\n');
190 /* Decode the keyword and call the appropriate expansion routine. */
191 for (i = 0; i < N_DIRECTIVES; i++)
193 if (dtable[i].length == len
194 && !strncmp (dtable[i].name, ident, len))
197 if (i == N_DIRECTIVES)
198 /* # identifier, but not a legit directive. Pass onward as a
199 CPP_DIRECTIVE token anyway - let the consumer worry about it. */
202 CPP_SET_WRITTEN (pfile, old_written);
204 if (pfile->no_directives)
206 cpp_error (pfile, "`#%s' may not be used inside a macro argument",
208 _cpp_skip_rest_of_line (pfile);
211 (*dtable[i].func) (pfile);
216 /* Pass a directive through to the output file.
217 BUF points to the contents of the directive, as a contiguous string.
218 LEN is the length of the string pointed to by BUF.
219 KEYWORD is the keyword-table entry for the directive. */
222 pass_thru_directive (buf, len, pfile, keyword)
228 const struct directive *kt = &dtable[keyword];
229 register unsigned klen = kt->length;
231 CPP_RESERVE (pfile, 1 + klen + len);
232 CPP_PUTC_Q (pfile, '#');
233 CPP_PUTS_Q (pfile, kt->name, klen);
234 if (len != 0 && buf[0] != ' ')
235 CPP_PUTC_Q (pfile, ' ');
236 CPP_PUTS_Q (pfile, buf, len);
239 /* Subroutine of do_define: determine the name of the macro to be
243 get_macro_name (pfile)
248 here = CPP_WRITTEN (pfile);
249 pfile->no_macro_expand++;
250 if (_cpp_get_directive_token (pfile) != CPP_NAME)
252 cpp_error (pfile, "`#define' must be followed by an identifier");
256 len = CPP_WRITTEN (pfile) - here;
257 if (len == 7 && !strncmp (pfile->token_buffer + here, "defined", 7))
259 cpp_error (pfile, "`defined' is not a legal macro name");
263 pfile->no_macro_expand--;
267 _cpp_skip_rest_of_line (pfile);
268 pfile->no_macro_expand--;
272 /* Process a #define command. */
286 here = CPP_WRITTEN (pfile);
287 len = get_macro_name (pfile);
291 /* Copy out the name so we can pop the token buffer. */
292 len = CPP_WRITTEN (pfile) - here;
293 sym = (U_CHAR *) alloca (len + 1);
294 memcpy (sym, pfile->token_buffer + here, len);
296 CPP_SET_WRITTEN (pfile, here);
298 /* If the next character, with no intervening whitespace, is '(',
299 then this is a function-like macro. */
303 else if (c != '\n' && !is_hspace (c))
304 /* Otherwise, C99 requires white space after the name. We treat it
305 as an object-like macro if this happens, with a warning. */
306 cpp_pedwarn (pfile, "missing white space after `#define %.*s'", len, sym);
308 def = _cpp_create_definition (pfile, funlike);
312 slot = _cpp_lookup_slot (pfile, sym, len, 1, &hash);
316 HASHNODE *hp = *slot;
318 /* Redefining a macro is ok if the definitions are the same. */
319 if (hp->type == T_MACRO)
320 ok = ! _cpp_compare_defs (pfile, def, hp->value.defn);
321 /* Redefining a constant is ok with -D. */
322 else if (hp->type == T_CONST || hp->type == T_STDC)
323 ok = ! pfile->done_initializing;
324 /* Otherwise it's not ok. */
327 /* Print the warning or error if it's not ok. */
330 if (hp->type == T_POISON)
331 cpp_error (pfile, "redefining poisoned `%.*s'", len, sym);
333 cpp_pedwarn (pfile, "`%.*s' redefined", len, sym);
334 if (hp->type == T_MACRO && pfile->done_initializing)
336 DEFINITION *d = hp->value.defn;
337 cpp_pedwarn_with_file_and_line (pfile, d->file, d->line, d->col,
338 "this is the location of the previous definition");
341 if (hp->type != T_POISON)
343 /* Replace the old definition. */
344 if (hp->type == T_MACRO)
345 _cpp_free_definition (hp->value.defn);
347 hp->value.defn = def;
352 HASHNODE *hp = _cpp_make_hashnode (sym, len, T_MACRO, hash);
353 hp->value.defn = def;
357 if (CPP_OPTION (pfile, debug_output)
358 || CPP_OPTION (pfile, dump_macros) == dump_definitions)
359 _cpp_dump_definition (pfile, sym, len, def);
360 else if (CPP_OPTION (pfile, dump_macros) == dump_names)
361 pass_thru_directive (sym, len, pfile, T_DEFINE);
367 * write out a #line command, for instance, after an #include file.
368 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
372 _cpp_output_line_command (pfile, file_change)
374 enum file_change_code file_change;
379 if (CPP_OPTION (pfile, no_line_commands)
380 || CPP_OPTION (pfile, no_output))
383 ip = cpp_file_buffer (pfile);
384 cpp_buf_line_and_col (ip, &line, NULL);
386 /* If the current file has not changed, we omit the #line if it would
387 appear to be a no-op, and we output a few newlines instead
388 if we want to increase the line number by a small amount.
389 We cannot do this if pfile->lineno is zero, because that means we
390 haven't output any line commands yet. (The very first line command
391 output is a `same_file' command.) */
392 if (file_change == same_file && pfile->lineno != 0)
394 if (line == pfile->lineno)
397 /* If the inherited line number is a little too small,
398 output some newlines instead of a #line command. */
399 if (line > pfile->lineno && line < pfile->lineno + 8)
401 CPP_RESERVE (pfile, 20);
402 while (line > pfile->lineno)
404 CPP_PUTC_Q (pfile, '\n');
411 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
412 CPP_PUTS_Q (pfile, "# ", 2);
414 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
415 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
417 _cpp_quote_string (pfile, ip->nominal_fname);
418 if (file_change != same_file && file_change != rename_file)
420 CPP_PUTC_Q (pfile, ' ');
421 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
423 /* Tell cc1 if following text comes from a system header file. */
424 if (ip->system_header_p)
426 CPP_PUTC_Q (pfile, ' ');
427 CPP_PUTC_Q (pfile, '3');
429 #ifndef NO_IMPLICIT_EXTERN_C
430 /* Tell cc1plus if following text should be treated as C. */
431 if (ip->system_header_p == 2 && CPP_OPTION (pfile, cplusplus))
433 CPP_PUTC_Q (pfile, ' ');
434 CPP_PUTC_Q (pfile, '4');
437 CPP_PUTC_Q (pfile, '\n');
438 pfile->lineno = line;
441 /* Handle #include and #import. */
444 parse_include (pfile, name)
448 long old_written = CPP_WRITTEN (pfile);
449 enum cpp_token token;
452 pfile->parsing_include_directive++;
453 token = _cpp_get_directive_token (pfile);
454 pfile->parsing_include_directive--;
456 len = CPP_WRITTEN (pfile) - old_written;
458 if (token == CPP_STRING)
459 ; /* No special treatment required. */
461 else if (token == CPP_NAME)
463 /* Support '#include xyz' like VAX-C. It is taken as
464 '#include <xyz.h>' and generates a warning. */
465 cpp_warning (pfile, "#%s filename is obsolete, use #%s <filename.h>",
468 /* Rewrite the token to <xyz.h>. */
469 CPP_RESERVE (pfile, 4);
471 memmove (pfile->token_buffer + old_written + 1,
472 pfile->token_buffer + old_written,
473 CPP_WRITTEN (pfile) - old_written);
474 pfile->token_buffer[old_written] = '<';
475 CPP_PUTS_Q (pfile, ".h>", 2);
480 cpp_error (pfile, "`#%s' expects \"FILENAME\" or <FILENAME>", name);
481 CPP_SET_WRITTEN (pfile, old_written);
482 _cpp_skip_rest_of_line (pfile);
486 CPP_NUL_TERMINATE (pfile);
487 CPP_ADJUST_WRITTEN (pfile, 1);
489 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
491 cpp_error (pfile, "junk at end of `#%s'", name);
492 _cpp_skip_rest_of_line (pfile);
495 CPP_SET_WRITTEN (pfile, old_written);
498 cpp_error (pfile, "empty file name in `#%s'", name);
510 len = parse_include (pfile, dtable[T_INCLUDE].name);
513 token = alloca (len + 1);
514 strcpy (token, CPP_PWRITTEN (pfile));
516 if (CPP_OPTION (pfile, dump_includes))
517 pass_thru_directive (token, len, pfile, T_INCLUDE);
519 _cpp_execute_include (pfile, token, len, 0, 0);
530 if (CPP_PEDANTIC (pfile))
531 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
533 if (CPP_OPTION (pfile, warn_import)
534 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
536 pfile->import_warning = 1;
538 "#import is obsolete, use an #ifndef wrapper in the header file");
541 len = parse_include (pfile, dtable[T_IMPORT].name);
544 token = alloca (len + 1);
545 strcpy (token, CPP_PWRITTEN (pfile));
547 if (CPP_OPTION (pfile, dump_includes))
548 pass_thru_directive (token, len, pfile, T_IMPORT);
550 _cpp_execute_include (pfile, token, len, 1, 0);
555 do_include_next (pfile)
560 struct file_name_list *search_start = 0;
562 if (CPP_PEDANTIC (pfile))
563 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
565 len = parse_include (pfile, dtable[T_INCLUDE_NEXT].name);
568 token = alloca (len + 1);
569 strcpy (token, CPP_PWRITTEN (pfile));
571 if (CPP_OPTION (pfile, dump_includes))
572 pass_thru_directive (token, len, pfile, T_INCLUDE_NEXT);
574 /* For #include_next, skip in the search path past the dir in which the
575 containing file was found. Treat files specified using an absolute path
576 as if there are no more directories to search. Treat the primary source
577 file like any other included source, but generate a warning. */
578 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
580 if (CPP_BUFFER (pfile)->ihash->foundhere != ABSOLUTE_PATH)
581 search_start = CPP_BUFFER (pfile)->ihash->foundhere->next;
584 cpp_warning (pfile, "#include_next in primary source file");
586 _cpp_execute_include (pfile, token, len, 0, search_start);
590 /* Subroutine of do_line. Read next token from PFILE without adding it to
591 the output buffer. If it is a number between 1 and 4, store it in *NUM
592 and return 1; otherwise, return 0 and complain if we aren't at the end
596 read_line_number (pfile, num)
600 long save_written = CPP_WRITTEN (pfile);
602 enum cpp_token token = _cpp_get_directive_token (pfile);
603 CPP_SET_WRITTEN (pfile, save_written);
604 p = pfile->token_buffer + save_written;
606 if (token == CPP_NUMBER && *p >= '1' && *p <= '4' && p[1] == '\0')
613 if (token != CPP_VSPACE && token != CPP_EOF)
614 cpp_error (pfile, "invalid format `#line' command");
619 /* Interpret #line command.
620 Note that the filename string (if any) is treated as if it were an
621 include filename. That means no escape handling. */
627 cpp_buffer *ip = CPP_BUFFER (pfile);
629 long old_written = CPP_WRITTEN (pfile);
630 enum file_change_code file_change = same_file;
631 enum cpp_token token;
634 token = _cpp_get_directive_token (pfile);
636 if (token != CPP_NUMBER)
638 cpp_error (pfile, "token after `#line' is not an integer");
639 goto bad_line_directive;
642 new_lineno = strtol (pfile->token_buffer + old_written, &x, 10);
645 cpp_error (pfile, "token after `#line' is not an integer");
646 goto bad_line_directive;
648 CPP_SET_WRITTEN (pfile, old_written);
650 if (CPP_PEDANTIC (pfile) && (new_lineno <= 0 || new_lineno > 32767))
651 cpp_pedwarn (pfile, "line number out of range in `#line' command");
653 token = _cpp_get_directive_token (pfile);
655 if (token == CPP_STRING)
657 U_CHAR *fname = pfile->token_buffer + old_written + 1;
658 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
659 int action_number = 0;
661 file_change = rename_file;
663 if (read_line_number (pfile, &action_number))
665 if (CPP_PEDANTIC (pfile))
666 cpp_pedwarn (pfile, "garbage at end of `#line' command");
668 if (action_number == 1)
670 file_change = enter_file;
671 read_line_number (pfile, &action_number);
673 else if (action_number == 2)
675 file_change = leave_file;
676 read_line_number (pfile, &action_number);
678 if (action_number == 3)
680 ip->system_header_p = 1;
681 read_line_number (pfile, &action_number);
683 if (action_number == 4)
685 ip->system_header_p = 2;
686 read_line_number (pfile, &action_number);
692 if (strcmp (fname, ip->nominal_fname))
694 const char *newname, *oldname;
695 if (!strcmp (fname, ip->ihash->name))
696 newname = ip->ihash->name;
697 else if (ip->last_nominal_fname
698 && !strcmp (fname, ip->last_nominal_fname))
699 newname = ip->last_nominal_fname;
701 newname = xstrdup (fname);
703 oldname = ip->nominal_fname;
704 ip->nominal_fname = newname;
706 if (ip->last_nominal_fname
707 && ip->last_nominal_fname != oldname
708 && ip->last_nominal_fname != newname
709 && ip->last_nominal_fname != ip->ihash->name)
710 free ((void *) ip->last_nominal_fname);
712 if (newname == ip->ihash->name)
713 ip->last_nominal_fname = NULL;
715 ip->last_nominal_fname = oldname;
718 else if (token != CPP_VSPACE && token != CPP_EOF)
720 cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
721 goto bad_line_directive;
724 /* The Newline at the end of this line remains to be processed.
725 To put the next line at the specified line number,
726 we must store a line number now that is one less. */
727 ip->lineno = new_lineno - 1;
728 CPP_SET_WRITTEN (pfile, old_written);
729 _cpp_output_line_command (pfile, file_change);
733 _cpp_skip_rest_of_line (pfile);
734 CPP_SET_WRITTEN (pfile, old_written);
738 /* Remove the definition of a symbol from the symbol table.
739 According to the C standard, it is not an error to undef
740 something that has no definitions. */
747 U_CHAR *buf, *name, *limit;
749 long here = CPP_WRITTEN (pfile);
750 enum cpp_token token;
752 _cpp_skip_hspace (pfile);
756 cpp_error (pfile, "token after #undef is not an identifier");
757 _cpp_skip_rest_of_line (pfile);
761 _cpp_parse_name (pfile, c);
762 buf = pfile->token_buffer + here;
763 limit = CPP_PWRITTEN(pfile);
765 /* Copy out the token so we can pop the token buffer. */
767 name = (U_CHAR *) alloca (len + 1);
768 memcpy (name, buf, len);
771 token = _cpp_get_directive_token (pfile);
772 if (token != CPP_VSPACE)
774 cpp_pedwarn (pfile, "junk on line after #undef");
775 _cpp_skip_rest_of_line (pfile);
777 CPP_SET_WRITTEN (pfile, here);
779 slot = _cpp_lookup_slot (pfile, name, len, 0, 0);
782 HASHNODE *hp = *slot;
783 /* If we are generating additional info for debugging (with -g) we
784 need to pass through all effective #undef commands. */
785 if (CPP_OPTION (pfile, debug_output))
786 pass_thru_directive (name, len, pfile, T_UNDEF);
787 if (hp->type == T_POISON)
788 cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
791 if (hp->type != T_MACRO)
792 cpp_warning (pfile, "undefining `%s'", hp->name);
794 htab_clear_slot (pfile->hashtab, (void **)slot);
802 * Report an error detected by the program we are processing.
803 * Use the text of the line in the error message.
804 * (We use error because it prints the filename & line#.)
811 const U_CHAR *text, *limit;
813 _cpp_skip_hspace (pfile);
814 text = CPP_BUFFER (pfile)->cur;
815 _cpp_skip_rest_of_line (pfile);
816 limit = CPP_BUFFER (pfile)->cur;
818 cpp_error (pfile, "#error %.*s", (int)(limit - text), text);
823 * Report a warning detected by the program we are processing.
824 * Use the text of the line in the warning message, then continue.
831 const U_CHAR *text, *limit;
833 _cpp_skip_hspace (pfile);
834 text = CPP_BUFFER (pfile)->cur;
835 _cpp_skip_rest_of_line (pfile);
836 limit = CPP_BUFFER (pfile)->cur;
838 if (CPP_PEDANTIC (pfile))
839 cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
841 cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text);
845 /* Report program identification. */
851 long old_written = CPP_WRITTEN (pfile);
853 /* Allow #ident in system headers, since that's not user's fault. */
854 if (CPP_PEDANTIC (pfile))
855 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
857 CPP_PUTS (pfile, "#ident ", 7);
859 /* Next token should be a string constant. */
860 if (_cpp_get_directive_token (pfile) == CPP_STRING)
861 /* And then a newline. */
862 if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
863 /* Good - ship it. */
866 cpp_error (pfile, "invalid #ident");
867 _cpp_skip_rest_of_line (pfile);
868 CPP_SET_WRITTEN (pfile, old_written); /* discard directive */
873 /* Pragmata handling. We handle some of these, and pass the rest on
874 to the front end. C99 defines three pragmas and says that no macro
875 expansion is to be performed on them; whether or not macro
876 expansion happens for other pragmas is implementation defined.
877 This implementation never macro-expands the text after #pragma.
879 We currently do not support the _Pragma operator. Support for that
880 has to be coordinated with the front end. Proposed implementation:
881 both #pragma blah blah and _Pragma("blah blah") become
882 __builtin_pragma(blah blah) and we teach the parser about that. */
884 /* Sub-handlers for the pragmas needing treatment here.
885 They return 1 if the token buffer is to be popped, 0 if not. */
886 static int do_pragma_once PARAMS ((cpp_reader *));
887 static int do_pragma_implementation PARAMS ((cpp_reader *));
888 static int do_pragma_poison PARAMS ((cpp_reader *));
889 static int do_pragma_default PARAMS ((cpp_reader *));
898 enum cpp_token token;
900 here = CPP_WRITTEN (pfile);
901 CPP_PUTS (pfile, "#pragma ", 8);
903 key = CPP_WRITTEN (pfile);
904 pfile->no_macro_expand++;
905 token = _cpp_get_directive_token (pfile);
906 if (token != CPP_NAME)
908 if (token == CPP_VSPACE)
914 buf = pfile->token_buffer + key;
915 CPP_PUTC (pfile, ' ');
917 #define tokis(x) !strncmp((char *) buf, x, sizeof(x) - 1)
919 pop = do_pragma_once (pfile);
920 else if (tokis ("implementation"))
921 pop = do_pragma_implementation (pfile);
922 else if (tokis ("poison"))
923 pop = do_pragma_poison (pfile);
925 pop = do_pragma_default (pfile);
928 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
932 CPP_SET_WRITTEN (pfile, here);
933 pfile->no_macro_expand--;
937 cpp_error (pfile, "malformed #pragma directive");
938 _cpp_skip_rest_of_line (pfile);
940 CPP_SET_WRITTEN (pfile, here);
941 pfile->no_macro_expand--;
946 do_pragma_default (pfile)
949 while (_cpp_get_directive_token (pfile) != CPP_VSPACE)
950 CPP_PUTC (pfile, ' ');
955 do_pragma_once (pfile)
958 cpp_buffer *ip = CPP_BUFFER (pfile);
960 /* Allow #pragma once in system headers, since that's not the user's
962 if (!ip->system_header_p)
963 cpp_warning (pfile, "`#pragma once' is obsolete");
965 if (CPP_PREV_BUFFER (ip) == NULL)
966 cpp_warning (pfile, "`#pragma once' outside include file");
968 ip->ihash->control_macro = (const U_CHAR *) ""; /* never repeat */
974 do_pragma_implementation (pfile)
977 /* Be quiet about `#pragma implementation' for a file only if it hasn't
978 been included yet. */
979 enum cpp_token token;
980 long written = CPP_WRITTEN (pfile);
985 token = _cpp_get_directive_token (pfile);
986 if (token == CPP_VSPACE)
988 else if (token != CPP_STRING)
990 cpp_error (pfile, "malformed #pragma implementation");
994 /* Trim the leading and trailing quote marks from the string. */
995 name = pfile->token_buffer + written + 1;
996 len = CPP_PWRITTEN (pfile) - name;
997 copy = (U_CHAR *) alloca (len);
998 memcpy (copy, name, len - 1);
999 copy[len - 1] = '\0';
1001 if (cpp_included (pfile, copy))
1003 "`#pragma implementation' for `%s' appears after file is included",
1009 do_pragma_poison (pfile)
1012 /* Poison these symbols so that all subsequent usage produces an
1018 enum cpp_token token;
1022 /* As a rule, don't include #pragma poison commands in output,
1023 unless the user asks for them. */
1024 writeit = (CPP_OPTION (pfile, debug_output)
1025 || CPP_OPTION (pfile, dump_macros) == dump_definitions
1026 || CPP_OPTION (pfile, dump_macros) == dump_names);
1030 written = CPP_WRITTEN (pfile);
1031 token = _cpp_get_directive_token (pfile);
1032 if (token == CPP_VSPACE)
1034 if (token != CPP_NAME)
1036 cpp_error (pfile, "invalid #pragma poison directive");
1037 _cpp_skip_rest_of_line (pfile);
1041 p = pfile->token_buffer + written;
1043 slot = _cpp_lookup_slot (pfile, p, len, 1, &hash);
1046 HASHNODE *hp = *slot;
1047 if (hp->type != T_POISON)
1049 cpp_warning (pfile, "poisoning existing macro `%s'", p);
1050 if (hp->type == T_MACRO)
1051 _cpp_free_definition (hp->value.defn);
1053 hp->type = T_POISON;
1058 HASHNODE *hp = _cpp_make_hashnode (p, len, T_POISON, hash);
1059 hp->value.cpval = 0;
1063 CPP_PUTC (pfile, ' ');
1068 /* Just ignore #sccs, on systems where we define it at all. */
1073 #ifdef SCCS_DIRECTIVE
1074 if (CPP_PEDANTIC (pfile))
1075 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
1077 cpp_error (pfile, "undefined or invalid # directive `sccs'");
1079 _cpp_skip_rest_of_line (pfile);
1084 /* We've found an `#if' directive. If the only thing before it in
1085 this file is white space, and if it is of the form
1086 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1087 for inclusion of this file. (See redundant_include_p in cppfiles.c
1088 for an explanation of controlling macros.) If so, return a
1089 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1092 detect_if_not_defined (pfile)
1095 U_CHAR *control_macro = 0;
1097 if (pfile->only_seen_white == 2)
1100 enum cpp_token token;
1103 int need_rparen = 0;
1105 /* Save state required for restore. */
1106 pfile->no_macro_expand++;
1107 CPP_SET_MARK (pfile);
1108 base_offset = CPP_WRITTEN (pfile);
1111 if (_cpp_get_directive_token (pfile) != CPP_OTHER
1112 || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1113 || CPP_PWRITTEN (pfile)[-1] != '!')
1116 /* ...then `defined', */
1117 token_offset = CPP_WRITTEN (pfile);
1118 token = _cpp_get_directive_token (pfile);
1119 if (token != CPP_NAME)
1121 ident = pfile->token_buffer + token_offset;
1122 CPP_NUL_TERMINATE (pfile);
1123 if (strcmp (ident, "defined"))
1126 /* ...then an optional '(' and the name, */
1127 token_offset = CPP_WRITTEN (pfile);
1128 token = _cpp_get_directive_token (pfile);
1129 if (token == CPP_LPAREN)
1131 token_offset = CPP_WRITTEN (pfile);
1132 token = _cpp_get_directive_token (pfile);
1133 if (token != CPP_NAME)
1137 else if (token != CPP_NAME)
1140 ident = pfile->token_buffer + token_offset;
1141 CPP_NUL_TERMINATE (pfile);
1143 /* ...then the ')', if necessary, */
1144 if ((!need_rparen || _cpp_get_directive_token (pfile) == CPP_RPAREN)
1145 /* ...and make sure there's nothing else on the line. */
1146 && _cpp_get_directive_token (pfile) == CPP_VSPACE)
1147 control_macro = (U_CHAR *) xstrdup (ident);
1150 CPP_SET_WRITTEN (pfile, base_offset);
1151 pfile->no_macro_expand--;
1152 CPP_GOTO_MARK (pfile);
1155 return control_macro;
1159 * #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1160 * Also, check for a reinclude preventer of the form #if !defined (MACRO).
1167 U_CHAR *control_macro = detect_if_not_defined (pfile);
1168 int value = _cpp_parse_expr (pfile);
1169 conditional_skip (pfile, value == 0, T_IF, control_macro);
1174 * handle a #elif directive by not changing if_stack either.
1175 * see the comment above do_else.
1182 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1184 cpp_error (pfile, "`#elif' not within a conditional");
1189 if (pfile->if_stack->type == T_ELSE)
1191 cpp_error (pfile, "`#elif' after `#else'");
1192 cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
1193 "the conditional began here");
1195 pfile->if_stack->type = T_ELIF;
1198 if (pfile->if_stack->if_succeeded)
1199 skip_if_group (pfile);
1202 if (_cpp_parse_expr (pfile) == 0)
1203 skip_if_group (pfile);
1206 ++pfile->if_stack->if_succeeded; /* continue processing input */
1207 _cpp_output_line_command (pfile, same_file);
1213 /* Parse an #ifdef or #ifndef directive. Returns 1 for defined, 0 for
1214 not defined; the macro tested is left in the token buffer (but
1218 parse_ifdef (pfile, name)
1224 enum cpp_token token;
1225 long old_written = CPP_WRITTEN (pfile);
1228 pfile->no_macro_expand++;
1229 token = _cpp_get_directive_token (pfile);
1230 pfile->no_macro_expand--;
1232 ident = pfile->token_buffer + old_written;
1233 len = CPP_WRITTEN (pfile) - old_written;
1235 if (token == CPP_VSPACE)
1237 if (! CPP_TRADITIONAL (pfile))
1238 cpp_pedwarn (pfile, "`#%s' with no argument", name);
1242 else if (token == CPP_NAME)
1244 defined = cpp_defined (pfile, ident, len);
1245 CPP_NUL_TERMINATE (pfile);
1246 CPP_ADJUST_WRITTEN (pfile, 1);
1251 if (! CPP_TRADITIONAL (pfile))
1252 cpp_error (pfile, "`#%s' with invalid argument", name);
1255 if (!CPP_TRADITIONAL (pfile))
1257 if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
1260 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", name);
1262 _cpp_skip_rest_of_line (pfile);
1265 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1269 /* #ifdef is dead simple. */
1275 int skip = ! parse_ifdef (pfile, dtable[T_IFDEF].name);
1276 conditional_skip (pfile, skip, T_IFDEF, 0);
1280 /* #ifndef is a tad more complex, because we need to check for a
1281 no-reinclusion wrapper. */
1287 int start_of_file, skip;
1288 U_CHAR *control_macro = 0;
1290 start_of_file = pfile->only_seen_white == 2;
1291 skip = parse_ifdef (pfile, dtable[T_IFNDEF].name);
1293 if (start_of_file && !skip)
1294 control_macro = xstrdup (CPP_PWRITTEN (pfile));
1296 conditional_skip (pfile, skip, T_IFNDEF, control_macro);
1300 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1301 If this is a #ifndef starting at the beginning of a file,
1302 CONTROL_MACRO is the macro name tested by the #ifndef.
1303 Otherwise, CONTROL_MACRO is 0. */
1306 conditional_skip (pfile, skip, type, control_macro)
1310 U_CHAR *control_macro;
1314 temp = (IF_STACK *) xcalloc (1, sizeof (IF_STACK));
1315 temp->lineno = CPP_BUFFER (pfile)->lineno;
1316 temp->next = pfile->if_stack;
1317 temp->control_macro = control_macro;
1318 pfile->if_stack = temp;
1320 pfile->if_stack->type = type;
1323 skip_if_group (pfile);
1326 ++pfile->if_stack->if_succeeded;
1327 _cpp_output_line_command (pfile, same_file);
1331 /* Subroutine of skip_if_group. Examine one preprocessing directive and
1332 return 0 if skipping should continue, 1 if it should halt. Also
1333 adjusts the if_stack as appropriate.
1334 The `#' has been read, but not the identifier. */
1337 consider_directive_while_skipping (pfile, stack)
1342 const struct directive *kt;
1347 _cpp_skip_hspace (pfile);
1349 ident = CPP_WRITTEN (pfile);
1350 _cpp_parse_name (pfile, GETC());
1351 len = CPP_WRITTEN (pfile) - ident;
1353 CPP_SET_WRITTEN (pfile, ident);
1355 for (i = 0; i < N_DIRECTIVES; i++)
1358 if (kt->length == len
1359 && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
1365 temp = (IF_STACK *) xmalloc (sizeof (IF_STACK));
1366 temp->next = pfile->if_stack;
1367 pfile->if_stack = temp;
1372 if (pfile->if_stack != stack)
1373 validate_else (pfile, dtable[i].name);
1376 if (pfile->if_stack == stack)
1380 pfile->if_stack->type = i;
1385 if (pfile->if_stack != stack)
1386 validate_else (pfile, dtable[i].name);
1388 if (pfile->if_stack == stack)
1391 temp = pfile->if_stack;
1392 pfile->if_stack = temp->next;
1401 /* Don't let erroneous code go by. */
1402 if (!CPP_OPTION (pfile, lang_asm) && CPP_PEDANTIC (pfile))
1403 cpp_pedwarn (pfile, "invalid preprocessor directive name");
1407 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
1408 * leaves input ptr at the sharp sign found.
1411 skip_if_group (pfile)
1415 IF_STACK *save_if_stack = pfile->if_stack; /* don't pop past here */
1416 const U_CHAR *beg_of_line;
1419 old_written = CPP_WRITTEN (pfile);
1423 beg_of_line = CPP_BUFFER (pfile)->cur;
1425 if (! CPP_TRADITIONAL (pfile))
1426 _cpp_skip_hspace (pfile);
1430 CPP_BUMP_LINE (pfile);
1435 if (consider_directive_while_skipping (pfile, save_if_stack))
1439 return; /* Caller will issue error. */
1442 _cpp_skip_rest_of_line (pfile);
1446 return; /* Caller will issue error. */
1448 CPP_BUMP_LINE (pfile);
1451 /* Back up to the beginning of this line. Caller will process the
1453 CPP_BUFFER (pfile)->cur = beg_of_line;
1454 pfile->only_seen_white = 1;
1458 * handle a #else directive. Do this by just continuing processing
1459 * without changing if_stack ; this is so that the error message
1460 * for missing #endif's etc. will point to the original #if. It
1461 * is possible that something different would be better.
1468 validate_else (pfile, dtable[T_ELSE].name);
1469 _cpp_skip_rest_of_line (pfile);
1471 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1473 cpp_error (pfile, "`#else' not within a conditional");
1478 /* #ifndef can't have its special treatment for containing the whole file
1479 if it has a #else clause. */
1480 pfile->if_stack->control_macro = 0;
1482 if (pfile->if_stack->type == T_ELSE)
1484 cpp_error (pfile, "`#else' after `#else'");
1485 cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
1486 "the conditional began here");
1488 pfile->if_stack->type = T_ELSE;
1491 if (pfile->if_stack->if_succeeded)
1492 skip_if_group (pfile);
1495 ++pfile->if_stack->if_succeeded; /* continue processing input */
1496 _cpp_output_line_command (pfile, same_file);
1502 * unstack after #endif command
1509 validate_else (pfile, dtable[T_ENDIF].name);
1510 _cpp_skip_rest_of_line (pfile);
1512 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1513 cpp_error (pfile, "`#endif' not within a conditional");
1516 IF_STACK *temp = pfile->if_stack;
1517 pfile->if_stack = temp->next;
1518 if (temp->control_macro != 0)
1520 /* This #endif matched a #ifndef at the start of the file.
1521 See if it is at the end of the file. */
1524 CPP_SET_MARK (pfile);
1528 _cpp_skip_hspace (pfile);
1533 CPP_GOTO_MARK (pfile);
1537 /* This #endif ends a #ifndef
1538 that contains all of the file (aside from whitespace).
1539 Arrange not to include the file again
1540 if the macro that was tested is defined. */
1541 CPP_BUFFER (pfile)->ihash->control_macro = temp->control_macro;
1545 _cpp_output_line_command (pfile, same_file);
1550 /* Issue -pedantic warning for text which is not a comment following
1551 an #else or #endif. Do not warn in system headers, as this is harmless
1552 and very common on old systems. */
1555 validate_else (pfile, directive)
1557 const char *directive;
1559 if (! CPP_PEDANTIC (pfile))
1562 _cpp_skip_hspace (pfile);
1563 if (PEEKC () != '\n')
1565 "text following `#%s' violates ANSI standard", directive);
1569 _cpp_handle_eof (pfile)
1572 cpp_buffer *next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
1573 struct if_stack *ifs, *nifs;
1575 /* Unwind the conditional stack and generate error messages. */
1576 for (ifs = pfile->if_stack;
1577 ifs != CPP_BUFFER (pfile)->if_stack;
1580 cpp_error_with_line (pfile, ifs->lineno, -1,
1581 "unterminated `#%s' conditional",
1582 dtable[ifs->type].name);
1587 pfile->if_stack = ifs;
1589 if (CPP_BUFFER (pfile)->nominal_fname && next_buf != NULL)
1591 /* We're about to return from an #include file.
1592 Emit #line information now (as part of the CPP_POP) result.
1593 But the #line refers to the file we will pop to. */
1594 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
1595 CPP_BUFFER (pfile) = next_buf;
1596 pfile->input_stack_listing_current = 0;
1597 _cpp_output_line_command (pfile, leave_file);
1598 CPP_BUFFER (pfile) = cur_buffer;
1601 CPP_BUFFER (pfile)->seen_eof = 1;
1610 HASHNODE *base, *this;
1611 HASHNODE **bslot, **tslot;
1613 unsigned long bhash, thash;
1615 if (CPP_PEDANTIC (pfile) && pfile->done_initializing)
1616 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
1618 _cpp_skip_hspace (pfile);
1619 sym = CPP_PWRITTEN (pfile); /* remember where it starts */
1620 ret = _cpp_parse_assertion (pfile);
1625 cpp_error (pfile, "missing token-sequence in `#assert'");
1629 _cpp_skip_hspace (pfile);
1631 if (c != EOF && c != '\n')
1633 cpp_error (pfile, "junk at end of `#assert'");
1637 tlen = strlen (sym);
1638 blen = (U_CHAR *) strchr (sym, '(') - sym;
1639 tslot = _cpp_lookup_slot (pfile, sym, tlen, 1, &thash);
1642 cpp_warning (pfile, "`%s' re-asserted", sym);
1646 bslot = _cpp_lookup_slot (pfile, sym, blen, 1, &bhash);
1648 *bslot = base = _cpp_make_hashnode (sym, blen, T_ASSERT, bhash);
1652 if (base->type != T_ASSERT)
1654 /* Token clash - but with what?! */
1655 cpp_ice (pfile, "base->type != T_ASSERT in do_assert");
1659 *tslot = this = _cpp_make_hashnode (sym, tlen, T_ASSERT, thash);
1660 this->value.aschain = base->value.aschain;
1661 base->value.aschain = this;
1663 pfile->limit = sym; /* Pop */
1667 _cpp_skip_rest_of_line (pfile);
1668 pfile->limit = sym; /* Pop */
1678 long baselen, thislen;
1679 HASHNODE *base, *this, *next;
1681 if (CPP_PEDANTIC (pfile) && pfile->done_initializing)
1682 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
1684 _cpp_skip_hspace (pfile);
1686 sym = CPP_PWRITTEN (pfile); /* remember where it starts */
1687 ret = _cpp_parse_assertion (pfile);
1691 _cpp_skip_hspace (pfile);
1693 if (c != EOF && c != '\n')
1694 cpp_error (pfile, "junk at end of `#unassert'");
1696 thislen = strlen (sym);
1699 base = _cpp_lookup (pfile, sym, thislen);
1701 goto error; /* It isn't an error to #undef what isn't #defined,
1702 so it isn't an error to #unassert what isn't
1703 #asserted either. */
1705 for (this = base->value.aschain; this; this = next)
1707 next = this->value.aschain;
1708 htab_remove_elt (pfile->hashtab, this);
1710 htab_remove_elt (pfile->hashtab, base);
1714 baselen = (U_CHAR *) strchr (sym, '(') - sym;
1715 base = _cpp_lookup (pfile, sym, baselen);
1716 if (! base) goto error;
1717 this = _cpp_lookup (pfile, sym, thislen);
1718 if (! this) goto error;
1721 while (next->value.aschain != this)
1722 next = next->value.aschain;
1724 next->value.aschain = this->value.aschain;
1725 htab_remove_elt (pfile->hashtab, this);
1727 if (base->value.aschain == NULL)
1728 /* Last answer for this predicate deleted. */
1729 htab_remove_elt (pfile->hashtab, base);
1732 pfile->limit = sym; /* Pop */
1735 _cpp_skip_rest_of_line (pfile);
1736 pfile->limit = sym; /* Pop */
1740 /* These are for -D, -U, -A. */
1742 /* Process the string STR as if it appeared as the body of a #define.
1743 If STR is just an identifier, define it with value 1.
1744 If STR has anything after the identifier, then it should
1745 be identifier=definition. */
1748 cpp_define (pfile, str)
1755 p = strchr (str, '=');
1756 /* Copy the entire option so we can modify it.
1757 Change the first "=" in the string to a space. If there is none,
1758 tack " 1" on the end. Then add a newline and a NUL. */
1762 count = strlen (str) + 2;
1763 buf = alloca (count);
1764 memcpy (buf, str, count - 2);
1766 buf[count - 2] = '\n';
1767 buf[count - 1] = '\0';
1771 count = strlen (str) + 4;
1772 buf = alloca (count);
1773 memcpy (buf, str, count - 4);
1774 strcpy (&buf[count-4], " 1\n");
1777 if (cpp_push_buffer (pfile, buf, count - 1) != NULL)
1780 cpp_pop_buffer (pfile);
1784 /* Process MACRO as if it appeared as the body of an #undef. */
1786 cpp_undef (pfile, macro)
1790 /* Copy the string so we can append a newline. */
1791 size_t len = strlen (macro);
1792 char *buf = alloca (len + 2);
1793 memcpy (buf, macro, len);
1795 buf[len + 1] = '\0';
1796 if (cpp_push_buffer (pfile, buf, len + 1))
1799 cpp_pop_buffer (pfile);
1803 /* Process the string STR as if it appeared as the body of a #assert. */
1805 cpp_assert (pfile, str)
1809 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
1812 cpp_pop_buffer (pfile);
1816 /* Process STR as if it appeared as the body of an #unassert. */
1818 cpp_unassert (pfile, str)
1822 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
1824 do_unassert (pfile);
1825 cpp_pop_buffer (pfile);
1829 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
1831 cpp_defined (pfile, id, len)
1836 HASHNODE *hp = _cpp_lookup (pfile, id, len);
1837 if (hp && hp->type == T_POISON)
1839 cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
1842 return (hp != NULL);