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. */
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))
37 /* `struct directive' defines one #-directive, including how to handle it. */
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. */
48 /* Stack of conditionals currently in progress
49 (including both successful and failing conditionals). */
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 */
59 typedef struct if_stack IF_STACK
;
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. */
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
*));
79 static int do_sccs
PARAMS ((cpp_reader
*, const struct directive
*));
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
*));
85 /* Forward declarations. */
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,
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
*,
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). */
108 static const struct directive directive_table
[] = {
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 */
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? */
133 { -1, 0, "", T_UNUSED
}
136 /* Handle a possible # directive.
137 '#' has already been read. */
140 _cpp_handle_directive (pfile
)
144 register const struct directive
*kt
;
147 long old_written
= CPP_WRITTEN (pfile
);
149 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
151 cpp_ice (pfile
, "handle_directive called on macro buffer");
155 _cpp_skip_hspace (pfile
);
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')
164 if (CPP_OPTIONS (pfile
)->lang_asm
)
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
);
175 /* If we are rescanning preprocessed input, don't obey any directives
177 if (CPP_OPTIONS (pfile
)->preprocessed
)
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)
187 /* A line of just `#' becomes blank. A line with something
188 other than an identifier after the # is reparsed as a non-
190 CPP_SET_WRITTEN (pfile
, old_written
);
191 return (PEEKC() == '\n');
194 /* Decode the keyword and call the appropriate expansion routine. */
195 for (kt
= directive_table
; ; kt
++)
198 /* # identifier, but not a legit directive. Pass onward as a
199 CPP_DIRECTIVE token anyway - let the consumer worry about it. */
201 if (kt
->length
== ident_length
202 && !strncmp (kt
->name
, ident
, ident_length
))
206 CPP_SET_WRITTEN (pfile
, old_written
);
208 if (pfile
->no_directives
)
210 cpp_error (pfile
, "`#%s' may not be used inside a macro argument",
212 _cpp_skip_rest_of_line (pfile
);
215 (*kt
->func
) (pfile
, kt
);
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. */
226 pass_thru_directive (buf
, len
, pfile
, keyword
)
230 const struct directive
*keyword
;
232 register unsigned keyword_length
= keyword
->length
;
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
);
242 /* Subroutine of do_define: determine the name of the macro to be
246 get_macro_name (pfile
)
251 here
= CPP_WRITTEN (pfile
);
252 pfile
->no_macro_expand
++;
253 if (_cpp_get_directive_token (pfile
) != CPP_NAME
)
255 cpp_error (pfile
, "`#define' must be followed by an identifier");
259 len
= CPP_WRITTEN (pfile
) - here
;
260 if (len
== 7 && !strncmp (pfile
->token_buffer
+ here
, "defined", 7))
262 cpp_error (pfile
, "`defined' is not a legal macro name");
266 pfile
->no_macro_expand
--;
270 _cpp_skip_rest_of_line (pfile
);
271 pfile
->no_macro_expand
--;
275 /* Process a #define command.
276 KEYWORD is the keyword-table entry for #define,
277 or NULL for a "predefined" macro. */
280 do_define (pfile
, keyword
)
282 const struct directive
*keyword ATTRIBUTE_UNUSED
;
292 here
= CPP_WRITTEN (pfile
);
293 len
= get_macro_name (pfile
);
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
);
302 CPP_SET_WRITTEN (pfile
, here
);
304 /* If the next character, with no intervening whitespace, is '(',
305 then this is a function-like macro. */
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
);
314 def
= _cpp_create_definition (pfile
, funlike
);
318 slot
= _cpp_lookup_slot (pfile
, sym
, len
, 1, &hash
);
322 HASHNODE
*hp
= *slot
;
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. */
333 /* Print the warning or error if it's not ok. */
336 if (hp
->type
== T_POISON
)
337 cpp_error (pfile
, "redefining poisoned `%.*s'", len
, sym
);
339 cpp_pedwarn (pfile
, "`%.*s' redefined", len
, sym
);
340 if (hp
->type
== T_MACRO
&& CPP_OPTIONS (pfile
)->done_initializing
)
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");
347 if (hp
->type
!= T_POISON
)
349 /* Replace the old definition. */
350 if (hp
->type
== T_MACRO
)
351 _cpp_free_definition (hp
->value
.defn
);
353 hp
->value
.defn
= def
;
358 HASHNODE
*hp
= _cpp_make_hashnode (sym
, len
, T_MACRO
, hash
);
359 hp
->value
.defn
= def
;
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
);
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.
378 _cpp_output_line_command (pfile
, file_change
)
380 enum file_change_code file_change
;
385 if (CPP_OPTIONS (pfile
)->no_line_commands
386 || CPP_OPTIONS (pfile
)->no_output
)
389 ip
= cpp_file_buffer (pfile
);
390 cpp_buf_line_and_col (ip
, &line
, NULL
);
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)
400 if (line
== pfile
->lineno
)
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)
407 CPP_RESERVE (pfile
, 20);
408 while (line
> pfile
->lineno
)
410 CPP_PUTC_Q (pfile
, '\n');
417 CPP_RESERVE (pfile
, 4 * strlen (ip
->nominal_fname
) + 50);
418 CPP_PUTS_Q (pfile
, "# ", 2);
420 sprintf ((char *) CPP_PWRITTEN (pfile
), "%ld ", line
);
421 CPP_ADJUST_WRITTEN (pfile
, strlen (CPP_PWRITTEN (pfile
)));
423 _cpp_quote_string (pfile
, ip
->nominal_fname
);
424 if (file_change
!= same_file
&& file_change
!= rename_file
)
426 CPP_PUTC_Q (pfile
, ' ');
427 CPP_PUTC_Q (pfile
, file_change
== enter_file
? '1' : '2');
429 /* Tell cc1 if following text comes from a system header file. */
430 if (ip
->system_header_p
)
432 CPP_PUTC_Q (pfile
, ' ');
433 CPP_PUTC_Q (pfile
, '3');
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
)
439 CPP_PUTC_Q (pfile
, ' ');
440 CPP_PUTC_Q (pfile
, '4');
443 CPP_PUTC_Q (pfile
, '\n');
444 pfile
->lineno
= line
;
447 /* Handle #include and #import. */
450 do_include (pfile
, keyword
)
452 const struct directive
*keyword
;
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? */
462 enum cpp_token token
;
464 /* Chain of dirs to search */
466 struct file_name_list
*search_start
;
468 long old_written
= CPP_WRITTEN (pfile
);
472 if (CPP_PEDANTIC (pfile
))
475 cpp_pedwarn (pfile
, "ANSI C does not allow `#import'");
477 cpp_pedwarn (pfile
, "ANSI C does not allow `#include_next'");
480 if (importing
&& CPP_OPTIONS (pfile
)->warn_import
481 && !CPP_OPTIONS (pfile
)->inhibit_warnings
482 && !CPP_BUFFER (pfile
)->system_header_p
&& !pfile
->import_warning
)
484 pfile
->import_warning
= 1;
486 "#import is obsolete, use an #ifndef wrapper in the header file");
489 pfile
->parsing_include_directive
++;
490 token
= _cpp_get_directive_token (pfile
);
491 pfile
->parsing_include_directive
--;
493 if (token
== CPP_STRING
)
495 if (pfile
->token_buffer
[old_written
] == '<')
499 else if (token
== CPP_NAME
)
501 /* Support '#include xyz' like VAX-C. It is taken as
502 '#include <xyz.h>' and generates a warning. */
504 "`#include filename' is obsolete, use `#include <filename.h>'");
507 /* Append the missing `.h' to the name. */
508 CPP_PUTS (pfile
, ".h", 2);
514 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
515 CPP_SET_WRITTEN (pfile
, old_written
);
516 _cpp_skip_rest_of_line (pfile
);
520 flen
= CPP_WRITTEN (pfile
) - old_written
;
521 ftok
= (unsigned char *) alloca (flen
+ 1);
522 memcpy (ftok
, pfile
->token_buffer
+ old_written
, flen
);
525 if (_cpp_get_directive_token (pfile
) != CPP_VSPACE
)
527 cpp_error (pfile
, "junk at end of `#include'");
528 _cpp_skip_rest_of_line (pfile
);
531 CPP_SET_WRITTEN (pfile
, old_written
);
535 cpp_error (pfile
, "empty file name in `#%s'", keyword
->name
);
539 if (CPP_OPTIONS (pfile
)->dump_includes
)
540 pass_thru_directive (ftok
,
543 - ((token
== CPP_NAME
) ? 2 : 0)
548 if (token
== CPP_STRING
)
558 fp
= cpp_file_buffer (pfile
);
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
))
566 if (fp
->ihash
->foundhere
!= ABSOLUTE_PATH
)
567 search_start
= fp
->ihash
->foundhere
->next
;
572 cpp_warning (pfile
, "#include_next in primary source file");
575 search_start
= CPP_OPTIONS (pfile
)->bracket_include
;
578 if (!CPP_OPTIONS (pfile
)->ignore_srcdir
)
581 search_start
= fp
->actual_dir
;
584 search_start
= CPP_OPTIONS (pfile
)->quote_include
;
590 cpp_error (pfile
, "No include path in which to find %s", ftok
);
594 fd
= _cpp_find_include_file (pfile
, ftok
, search_start
, &ihash
, &before
);
601 if (CPP_OPTIONS (pfile
)->print_deps_missing_files
602 && CPP_PRINT_DEPS (pfile
) > (angle_brackets
||
603 (pfile
->system_include_depth
> 0)))
606 deps_add_dep (pfile
->deps
, ftok
);
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
;
616 ptr
= CPP_OPTIONS (pfile
)->quote_include
;
618 p
= (char *) alloca (strlen (ptr
->name
)
619 + strlen (ftok
) + 2);
620 if (*ptr
->name
!= '\0')
622 strcpy (p
, ptr
->name
);
626 deps_add_dep (pfile
->deps
, p
);
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
);
640 cpp_error_from_errno (pfile
, ftok
);
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
);
650 /* Handle -H option. */
651 if (CPP_OPTIONS(pfile
)->print_include_names
)
653 fp
= CPP_BUFFER (pfile
);
654 while ((fp
= CPP_PREV_BUFFER (fp
)) != NULL
)
656 fprintf (stderr
, " %s\n", ihash
->name
);
659 /* Actually process the file */
662 ihash
->control_macro
= (const U_CHAR
*) "";
664 if (_cpp_read_include_file (pfile
, fd
, ihash
))
666 _cpp_output_line_command (pfile
, enter_file
);
668 pfile
->system_include_depth
++; /* Decremented in file_cleanup. */
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
679 read_line_number (pfile
, num
)
683 long save_written
= CPP_WRITTEN (pfile
);
685 enum cpp_token token
= _cpp_get_directive_token (pfile
);
686 CPP_SET_WRITTEN (pfile
, save_written
);
687 p
= pfile
->token_buffer
+ save_written
;
689 if (token
== CPP_NUMBER
&& *p
>= '1' && *p
<= '4' && p
[1] == '\0')
696 if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
)
697 cpp_error (pfile
, "invalid format `#line' command");
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. */
707 do_line (pfile
, keyword
)
709 const struct directive
*keyword ATTRIBUTE_UNUSED
;
711 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
713 long old_written
= CPP_WRITTEN (pfile
);
714 enum file_change_code file_change
= same_file
;
715 enum cpp_token token
;
718 token
= _cpp_get_directive_token (pfile
);
720 if (token
!= CPP_NUMBER
)
722 cpp_error (pfile
, "token after `#line' is not an integer");
723 goto bad_line_directive
;
726 new_lineno
= strtol (pfile
->token_buffer
+ old_written
, &x
, 10);
729 cpp_error (pfile
, "token after `#line' is not an integer");
730 goto bad_line_directive
;
732 CPP_SET_WRITTEN (pfile
, old_written
);
734 if (CPP_PEDANTIC (pfile
) && (new_lineno
<= 0 || new_lineno
> 32767))
735 cpp_pedwarn (pfile
, "line number out of range in `#line' command");
737 token
= _cpp_get_directive_token (pfile
);
739 if (token
== CPP_STRING
)
741 U_CHAR
*fname
= pfile
->token_buffer
+ old_written
+ 1;
742 U_CHAR
*end_name
= CPP_PWRITTEN (pfile
) - 1;
743 int action_number
= 0;
745 file_change
= rename_file
;
747 if (read_line_number (pfile
, &action_number
))
749 if (CPP_PEDANTIC (pfile
))
750 cpp_pedwarn (pfile
, "garbage at end of `#line' command");
752 if (action_number
== 1)
754 file_change
= enter_file
;
755 read_line_number (pfile
, &action_number
);
757 else if (action_number
== 2)
759 file_change
= leave_file
;
760 read_line_number (pfile
, &action_number
);
762 if (action_number
== 3)
764 ip
->system_header_p
= 1;
765 read_line_number (pfile
, &action_number
);
767 if (action_number
== 4)
769 ip
->system_header_p
= 2;
770 read_line_number (pfile
, &action_number
);
776 if (strcmp (fname
, ip
->nominal_fname
))
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
;
785 newname
= xstrdup (fname
);
787 oldname
= ip
->nominal_fname
;
788 ip
->nominal_fname
= newname
;
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
);
796 if (newname
== ip
->ihash
->name
)
797 ip
->last_nominal_fname
= NULL
;
799 ip
->last_nominal_fname
= oldname
;
802 else if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
)
804 cpp_error (pfile
, "token after `#line %d' is not a string", new_lineno
);
805 goto bad_line_directive
;
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
);
817 _cpp_skip_rest_of_line (pfile
);
818 CPP_SET_WRITTEN (pfile
, old_written
);
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. */
826 do_undef (pfile
, keyword
)
828 const struct directive
*keyword
;
832 U_CHAR
*buf
, *name
, *limit
;
834 long here
= CPP_WRITTEN (pfile
);
835 enum cpp_token token
;
837 _cpp_skip_hspace (pfile
);
841 cpp_error (pfile
, "token after #undef is not an identifier");
842 _cpp_skip_rest_of_line (pfile
);
846 _cpp_parse_name (pfile
, c
);
847 buf
= pfile
->token_buffer
+ here
;
848 limit
= CPP_PWRITTEN(pfile
);
850 /* Copy out the token so we can pop the token buffer. */
852 name
= (U_CHAR
*) alloca (len
+ 1);
853 memcpy (name
, buf
, len
);
856 token
= _cpp_get_directive_token (pfile
);
857 if (token
!= CPP_VSPACE
)
859 cpp_pedwarn (pfile
, "junk on line after #undef");
860 _cpp_skip_rest_of_line (pfile
);
862 CPP_SET_WRITTEN (pfile
, here
);
864 slot
= _cpp_lookup_slot (pfile
, name
, len
, 0, 0);
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
);
876 if (hp
->type
!= T_MACRO
)
877 cpp_warning (pfile
, "undefining `%s'", hp
->name
);
879 htab_clear_slot (pfile
->hashtab
, (void **)slot
);
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#.)
893 do_error (pfile
, keyword
)
895 const struct directive
*keyword ATTRIBUTE_UNUSED
;
897 const U_CHAR
*text
, *limit
;
899 _cpp_skip_hspace (pfile
);
900 text
= CPP_BUFFER (pfile
)->cur
;
901 _cpp_skip_rest_of_line (pfile
);
902 limit
= CPP_BUFFER (pfile
)->cur
;
904 cpp_error (pfile
, "#error %.*s", (int)(limit
- text
), text
);
909 * Report a warning detected by the program we are processing.
910 * Use the text of the line in the warning message, then continue.
914 do_warning (pfile
, keyword
)
916 const struct directive
*keyword ATTRIBUTE_UNUSED
;
918 const U_CHAR
*text
, *limit
;
920 _cpp_skip_hspace (pfile
);
921 text
= CPP_BUFFER (pfile
)->cur
;
922 _cpp_skip_rest_of_line (pfile
);
923 limit
= CPP_BUFFER (pfile
)->cur
;
925 if (CPP_PEDANTIC (pfile
))
926 cpp_pedwarn (pfile
, "ANSI C does not allow `#warning'");
928 cpp_warning (pfile
, "#warning %.*s", (int)(limit
- text
), text
);
932 /* Report program identification. */
935 do_ident (pfile
, keyword
)
937 const struct directive
*keyword ATTRIBUTE_UNUSED
;
939 long old_written
= CPP_WRITTEN (pfile
);
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'");
945 CPP_PUTS (pfile
, "#ident ", 7);
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. */
954 cpp_error (pfile
, "invalid #ident");
955 _cpp_skip_rest_of_line (pfile
);
956 CPP_SET_WRITTEN (pfile
, old_written
); /* discard directive */
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.
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. */
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
*));
980 do_pragma (pfile
, keyword
)
982 const struct directive
*keyword ATTRIBUTE_UNUSED
;
987 enum cpp_token token
;
989 here
= CPP_WRITTEN (pfile
);
990 CPP_PUTS (pfile
, "#pragma ", 8);
992 key
= CPP_WRITTEN (pfile
);
993 pfile
->no_macro_expand
++;
994 token
= _cpp_get_directive_token (pfile
);
995 if (token
!= CPP_NAME
)
997 if (token
== CPP_VSPACE
)
1003 buf
= pfile
->token_buffer
+ key
;
1004 CPP_PUTC (pfile
, ' ');
1006 #define tokis(x) !strncmp((char *) buf, x, sizeof(x) - 1)
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
);
1014 pop
= do_pragma_default (pfile
);
1017 if (_cpp_get_directive_token (pfile
) != CPP_VSPACE
)
1021 CPP_SET_WRITTEN (pfile
, here
);
1022 pfile
->no_macro_expand
--;
1026 cpp_error (pfile
, "malformed #pragma directive");
1027 _cpp_skip_rest_of_line (pfile
);
1029 CPP_SET_WRITTEN (pfile
, here
);
1030 pfile
->no_macro_expand
--;
1035 do_pragma_default (pfile
)
1038 while (_cpp_get_directive_token (pfile
) != CPP_VSPACE
)
1039 CPP_PUTC (pfile
, ' ');
1044 do_pragma_once (pfile
)
1047 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
1049 /* Allow #pragma once in system headers, since that's not the user's
1051 if (!ip
->system_header_p
)
1052 cpp_warning (pfile
, "`#pragma once' is obsolete");
1054 if (CPP_PREV_BUFFER (ip
) == NULL
)
1055 cpp_warning (pfile
, "`#pragma once' outside include file");
1057 ip
->ihash
->control_macro
= (const U_CHAR
*) ""; /* never repeat */
1063 do_pragma_implementation (pfile
)
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
);
1074 token
= _cpp_get_directive_token (pfile
);
1075 if (token
== CPP_VSPACE
)
1077 else if (token
!= CPP_STRING
)
1079 cpp_error (pfile
, "malformed #pragma implementation");
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';
1090 if (cpp_included (pfile
, copy
))
1092 "`#pragma implementation' for `%s' appears after file is included",
1098 do_pragma_poison (pfile
)
1101 /* Poison these symbols so that all subsequent usage produces an
1107 enum cpp_token token
;
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
);
1119 written
= CPP_WRITTEN (pfile
);
1120 token
= _cpp_get_directive_token (pfile
);
1121 if (token
== CPP_VSPACE
)
1123 if (token
!= CPP_NAME
)
1125 cpp_error (pfile
, "invalid #pragma poison directive");
1126 _cpp_skip_rest_of_line (pfile
);
1130 p
= pfile
->token_buffer
+ written
;
1132 slot
= _cpp_lookup_slot (pfile
, p
, len
, 1, &hash
);
1135 HASHNODE
*hp
= *slot
;
1136 if (hp
->type
!= T_POISON
)
1138 cpp_warning (pfile
, "poisoning existing macro `%s'", p
);
1139 if (hp
->type
== T_MACRO
)
1140 _cpp_free_definition (hp
->value
.defn
);
1142 hp
->type
= T_POISON
;
1147 HASHNODE
*hp
= _cpp_make_hashnode (p
, len
, T_POISON
, hash
);
1148 hp
->value
.cpval
= 0;
1152 CPP_PUTC (pfile
, ' ');
1157 #ifdef SCCS_DIRECTIVE
1158 /* Just ignore #sccs, on systems where we define it at all. */
1161 do_sccs (pfile
, keyword
)
1163 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1165 if (CPP_PEDANTIC (pfile
))
1166 cpp_pedwarn (pfile
, "ANSI C does not allow `#sccs'");
1167 _cpp_skip_rest_of_line (pfile
);
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. */
1180 detect_if_not_defined (pfile
)
1183 U_CHAR
*control_macro
= 0;
1185 if (pfile
->only_seen_white
== 2)
1188 enum cpp_token token
;
1191 int need_rparen
= 0;
1193 /* Save state required for restore. */
1194 pfile
->no_macro_expand
++;
1195 CPP_SET_MARK (pfile
);
1196 base_offset
= CPP_WRITTEN (pfile
);
1199 if (_cpp_get_directive_token (pfile
) != CPP_OTHER
1200 || CPP_WRITTEN (pfile
) != (size_t) base_offset
+ 1
1201 || CPP_PWRITTEN (pfile
)[-1] != '!')
1204 /* ...then `defined', */
1205 token_offset
= CPP_WRITTEN (pfile
);
1206 token
= _cpp_get_directive_token (pfile
);
1207 if (token
!= CPP_NAME
)
1209 ident
= pfile
->token_buffer
+ token_offset
;
1210 CPP_NUL_TERMINATE (pfile
);
1211 if (strcmp (ident
, "defined"))
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
)
1219 token_offset
= CPP_WRITTEN (pfile
);
1220 token
= _cpp_get_directive_token (pfile
);
1221 if (token
!= CPP_NAME
)
1225 else if (token
!= CPP_NAME
)
1228 ident
= pfile
->token_buffer
+ token_offset
;
1229 CPP_NUL_TERMINATE (pfile
);
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
);
1238 CPP_SET_WRITTEN (pfile
, base_offset
);
1239 pfile
->no_macro_expand
--;
1240 CPP_GOTO_MARK (pfile
);
1243 return control_macro
;
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).
1252 do_if (pfile
, keyword
)
1254 const struct directive
*keyword ATTRIBUTE_UNUSED
;
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
);
1263 * handle a #elif directive by not changing if_stack either.
1264 * see the comment above do_else.
1268 do_elif (pfile
, keyword
)
1270 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1272 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
1274 cpp_error (pfile
, "`#elif' not within a conditional");
1279 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
)
1281 cpp_error (pfile
, "`#elif' after `#else'");
1282 cpp_error_with_line (pfile
, pfile
->if_stack
->lineno
, -1,
1283 "the conditional began here");
1285 pfile
->if_stack
->type
= T_ELIF
;
1288 if (pfile
->if_stack
->if_succeeded
)
1289 skip_if_group (pfile
);
1292 if (_cpp_parse_expr (pfile
) == 0)
1293 skip_if_group (pfile
);
1296 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
1297 _cpp_output_line_command (pfile
, same_file
);
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.
1310 do_ifdef (pfile
, keyword
)
1312 const struct directive
*keyword
;
1317 enum cpp_token token
;
1318 int start_of_file
= 0;
1319 U_CHAR
*control_macro
= 0;
1320 int old_written
= CPP_WRITTEN (pfile
);
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;
1326 pfile
->no_macro_expand
++;
1327 token
= _cpp_get_directive_token (pfile
);
1328 pfile
->no_macro_expand
--;
1330 ident
= pfile
->token_buffer
+ old_written
;
1331 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
1332 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
1334 if (token
== CPP_VSPACE
|| token
== CPP_POP
|| token
== CPP_EOF
)
1336 skip
= (keyword
->type
== T_IFDEF
);
1337 if (! CPP_TRADITIONAL (pfile
))
1338 cpp_pedwarn (pfile
, "`#%s' with no argument", keyword
->name
);
1340 else if (token
== CPP_NAME
)
1342 skip
= cpp_defined (pfile
, ident
, ident_length
);
1343 if (keyword
->type
== T_IFDEF
)
1346 if (start_of_file
&& !skip
)
1348 control_macro
= (U_CHAR
*) xmalloc (ident_length
+ 1);
1349 memcpy (control_macro
, ident
, ident_length
+ 1);
1354 skip
= (keyword
->type
== T_IFDEF
);
1355 if (! CPP_TRADITIONAL (pfile
))
1356 cpp_error (pfile
, "`#%s' with invalid argument", keyword
->name
);
1359 if (!CPP_TRADITIONAL (pfile
))
1361 _cpp_skip_hspace (pfile
);
1363 if (c
!= EOF
&& c
!= '\n')
1364 cpp_pedwarn (pfile
, "garbage at end of `#%s' argument", keyword
->name
);
1366 _cpp_skip_rest_of_line (pfile
);
1368 conditional_skip (pfile
, skip
, T_IF
, control_macro
);
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. */
1378 conditional_skip (pfile
, skip
, type
, control_macro
)
1381 enum node_type type
;
1382 U_CHAR
*control_macro
;
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
;
1392 pfile
->if_stack
->type
= type
;
1395 skip_if_group (pfile
);
1398 ++pfile
->if_stack
->if_succeeded
;
1399 _cpp_output_line_command (pfile
, same_file
);
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. */
1409 consider_directive_while_skipping (pfile
, stack
)
1413 long ident_len
, ident
;
1414 const struct directive
*kt
;
1417 _cpp_skip_hspace (pfile
);
1419 ident
= CPP_WRITTEN (pfile
);
1420 _cpp_parse_name (pfile
, GETC());
1421 ident_len
= CPP_WRITTEN (pfile
) - ident
;
1423 CPP_SET_WRITTEN (pfile
, ident
);
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)
1433 temp
= (IF_STACK
*) xmalloc (sizeof (IF_STACK
));
1434 temp
->next
= pfile
->if_stack
;
1435 pfile
->if_stack
= temp
;
1436 temp
->type
= kt
->type
;
1440 if (pfile
->if_stack
!= stack
)
1441 validate_else (pfile
, "#else");
1444 if (pfile
->if_stack
== stack
)
1448 pfile
->if_stack
->type
= kt
->type
;
1453 if (pfile
->if_stack
!= stack
)
1454 validate_else (pfile
, "#endif");
1456 if (pfile
->if_stack
== stack
)
1459 temp
= pfile
->if_stack
;
1460 pfile
->if_stack
= temp
->next
;
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");
1474 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
1475 * leaves input ptr at the sharp sign found.
1478 skip_if_group (pfile
)
1482 IF_STACK
*save_if_stack
= pfile
->if_stack
; /* don't pop past here */
1483 const U_CHAR
*beg_of_line
;
1486 old_written
= CPP_WRITTEN (pfile
);
1490 beg_of_line
= CPP_BUFFER (pfile
)->cur
;
1492 if (! CPP_TRADITIONAL (pfile
))
1493 _cpp_skip_hspace (pfile
);
1497 CPP_BUMP_LINE (pfile
);
1502 if (consider_directive_while_skipping (pfile
, save_if_stack
))
1506 return; /* Caller will issue error. */
1509 _cpp_skip_rest_of_line (pfile
);
1513 return; /* Caller will issue error. */
1515 CPP_BUMP_LINE (pfile
);
1518 /* Back up to the beginning of this line. Caller will process the
1520 CPP_BUFFER (pfile
)->cur
= beg_of_line
;
1521 pfile
->only_seen_white
= 1;
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.
1532 do_else (pfile
, keyword
)
1534 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1536 validate_else (pfile
, "#else");
1537 _cpp_skip_rest_of_line (pfile
);
1539 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
1541 cpp_error (pfile
, "`#else' not within a conditional");
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;
1550 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
)
1552 cpp_error (pfile
, "`#else' after `#else'");
1553 cpp_error_with_line (pfile
, pfile
->if_stack
->lineno
, -1,
1554 "the conditional began here");
1556 pfile
->if_stack
->type
= T_ELSE
;
1559 if (pfile
->if_stack
->if_succeeded
)
1560 skip_if_group (pfile
);
1563 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
1564 _cpp_output_line_command (pfile
, same_file
);
1570 * unstack after #endif command
1574 do_endif (pfile
, keyword
)
1576 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1578 validate_else (pfile
, "#endif");
1579 _cpp_skip_rest_of_line (pfile
);
1581 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
1582 cpp_error (pfile
, "`#endif' not within a conditional");
1585 IF_STACK
*temp
= pfile
->if_stack
;
1586 pfile
->if_stack
= temp
->next
;
1587 if (temp
->control_macro
!= 0)
1589 /* This #endif matched a #ifndef at the start of the file.
1590 See if it is at the end of the file. */
1593 CPP_SET_MARK (pfile
);
1597 _cpp_skip_hspace (pfile
);
1602 CPP_GOTO_MARK (pfile
);
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
;
1614 _cpp_output_line_command (pfile
, same_file
);
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. */
1624 validate_else (pfile
, directive
)
1626 const char *directive
;
1628 if (! CPP_PEDANTIC (pfile
))
1631 _cpp_skip_hspace (pfile
);
1632 if (PEEKC () != '\n')
1634 "text following `%s' violates ANSI standard", directive
);
1637 /* Convert T_IF, etc. to a string. Used in error messages. */
1639 if_directive_name (pfile
, ifs
)
1641 struct if_stack
*ifs
;
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";
1651 cpp_ice (pfile
, "impossible if_stack->type value %d", ifs
->type
);
1657 _cpp_handle_eof (pfile
)
1660 cpp_buffer
*next_buf
= CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
1661 struct if_stack
*ifs
, *nifs
;
1663 /* Unwind the conditional stack and generate error messages. */
1664 for (ifs
= pfile
->if_stack
;
1665 ifs
!= CPP_BUFFER (pfile
)->if_stack
;
1668 cpp_error_with_line (pfile
, ifs
->lineno
, -1,
1669 "unterminated `%s' conditional",
1670 if_directive_name (pfile
, ifs
));
1675 pfile
->if_stack
= ifs
;
1677 if (CPP_BUFFER (pfile
)->nominal_fname
&& next_buf
!= NULL
)
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
;
1689 CPP_BUFFER (pfile
)->seen_eof
= 1;
1693 do_assert (pfile
, keyword
)
1695 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1699 HASHNODE
*base
, *this;
1700 HASHNODE
**bslot
, **tslot
;
1702 unsigned long bhash
, thash
;
1704 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
)
1705 cpp_pedwarn (pfile
, "ANSI C does not allow `#assert'");
1707 _cpp_skip_hspace (pfile
);
1708 sym
= CPP_PWRITTEN (pfile
); /* remember where it starts */
1709 ret
= _cpp_parse_assertion (pfile
);
1714 cpp_error (pfile
, "missing token-sequence in `#assert'");
1718 _cpp_skip_hspace (pfile
);
1720 if (c
!= EOF
&& c
!= '\n')
1722 cpp_error (pfile
, "junk at end of `#assert'");
1726 tlen
= strlen (sym
);
1727 blen
= (U_CHAR
*) strchr (sym
, '(') - sym
;
1728 tslot
= _cpp_lookup_slot (pfile
, sym
, tlen
, 1, &thash
);
1731 cpp_warning (pfile
, "`%s' re-asserted", sym
);
1735 bslot
= _cpp_lookup_slot (pfile
, sym
, blen
, 1, &bhash
);
1737 *bslot
= base
= _cpp_make_hashnode (sym
, blen
, T_ASSERT
, bhash
);
1741 if (base
->type
!= T_ASSERT
)
1743 /* Token clash - but with what?! */
1744 cpp_ice (pfile
, "base->type != T_ASSERT in do_assert");
1748 *tslot
= this = _cpp_make_hashnode (sym
, tlen
, T_ASSERT
, thash
);
1749 this->value
.aschain
= base
->value
.aschain
;
1750 base
->value
.aschain
= this;
1752 pfile
->limit
= sym
; /* Pop */
1756 _cpp_skip_rest_of_line (pfile
);
1757 pfile
->limit
= sym
; /* Pop */
1762 do_unassert (pfile
, keyword
)
1764 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1768 long baselen
, thislen
;
1769 HASHNODE
*base
, *this, *next
;
1771 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
)
1772 cpp_pedwarn (pfile
, "ANSI C does not allow `#unassert'");
1774 _cpp_skip_hspace (pfile
);
1776 sym
= CPP_PWRITTEN (pfile
); /* remember where it starts */
1777 ret
= _cpp_parse_assertion (pfile
);
1781 _cpp_skip_hspace (pfile
);
1783 if (c
!= EOF
&& c
!= '\n')
1784 cpp_error (pfile
, "junk at end of `#unassert'");
1786 thislen
= strlen (sym
);
1789 base
= _cpp_lookup (pfile
, sym
, thislen
);
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. */
1795 for (this = base
->value
.aschain
; this; this = next
)
1797 next
= this->value
.aschain
;
1798 htab_remove_elt (pfile
->hashtab
, this);
1800 htab_remove_elt (pfile
->hashtab
, base
);
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
;
1811 while (next
->value
.aschain
!= this)
1812 next
= next
->value
.aschain
;
1814 next
->value
.aschain
= this->value
.aschain
;
1815 htab_remove_elt (pfile
->hashtab
, this);
1817 if (base
->value
.aschain
== NULL
)
1818 /* Last answer for this predicate deleted. */
1819 htab_remove_elt (pfile
->hashtab
, base
);
1822 pfile
->limit
= sym
; /* Pop */
1825 _cpp_skip_rest_of_line (pfile
);
1826 pfile
->limit
= sym
; /* Pop */
1830 /* These are for -D, -U, -A. */
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. */
1838 cpp_define (pfile
, str
)
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. */
1852 count
= strlen (str
) + 2;
1853 buf
= alloca (count
);
1854 memcpy (buf
, str
, count
- 2);
1856 buf
[count
- 2] = '\n';
1857 buf
[count
- 1] = '\0';
1861 count
= strlen (str
) + 4;
1862 buf
= alloca (count
);
1863 memcpy (buf
, str
, count
- 4);
1864 strcpy (&buf
[count
-4], " 1\n");
1867 if (cpp_push_buffer (pfile
, buf
, count
- 1) != NULL
)
1869 do_define (pfile
, NULL
);
1870 cpp_pop_buffer (pfile
);
1874 /* Process MACRO as if it appeared as the body of an #undef. */
1876 cpp_undef (pfile
, macro
)
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
);
1885 buf
[len
+ 1] = '\0';
1886 if (cpp_push_buffer (pfile
, buf
, len
+ 1))
1888 do_undef (pfile
, NULL
);
1889 cpp_pop_buffer (pfile
);
1893 /* Process the string STR as if it appeared as the body of a #assert. */
1895 cpp_assert (pfile
, str
)
1899 if (cpp_push_buffer (pfile
, str
, strlen (str
)) != NULL
)
1901 do_assert (pfile
, NULL
);
1902 cpp_pop_buffer (pfile
);
1906 /* Process STR as if it appeared as the body of an #unassert. */
1908 cpp_unassert (pfile
, str
)
1912 if (cpp_push_buffer (pfile
, str
, strlen (str
)) != NULL
)
1914 do_unassert (pfile
, NULL
);
1915 cpp_pop_buffer (pfile
);
1919 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
1921 cpp_defined (pfile
, id
, len
)
1926 HASHNODE
*hp
= _cpp_lookup (pfile
, id
, len
);
1927 if (hp
&& hp
->type
== T_POISON
)
1929 cpp_error (pfile
, "attempt to use poisoned `%s'", hp
->name
);
1932 return (hp
!= NULL
);