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 /* `struct directive' defines one #-directive, including how to handle it. */
35 int (*func
) /* Function to handle directive */
36 PARAMS ((cpp_reader
*));
37 const char *name
; /* Name of directive */
38 unsigned short length
; /* Length of name */
39 unsigned short origin
; /* Origin of this directive */
42 /* Stack of conditionals currently in progress
43 (including both successful and failing conditionals). */
47 struct if_stack
*next
;
48 int lineno
; /* line number where condition started */
49 int if_succeeded
; /* truth of last condition in this group */
50 const U_CHAR
*control_macro
; /* macro name for #ifndef around entire file */
51 int type
; /* type of last directive seen in this group */
53 typedef struct if_stack IF_STACK
;
55 /* Forward declarations. */
57 static void validate_else
PARAMS ((cpp_reader
*, const char *));
58 static int parse_ifdef
PARAMS ((cpp_reader
*, const char *));
59 static unsigned int parse_include
PARAMS ((cpp_reader
*, const char *));
60 static int conditional_skip
PARAMS ((cpp_reader
*, int, int,
62 static int skip_if_group
PARAMS ((cpp_reader
*));
63 static void pass_thru_directive
PARAMS ((const U_CHAR
*, size_t,
65 static int read_line_number
PARAMS ((cpp_reader
*, int *));
66 static U_CHAR
*detect_if_not_defined
PARAMS ((cpp_reader
*));
67 static int consider_directive_while_skipping
68 PARAMS ((cpp_reader
*, IF_STACK
*));
69 static int get_macro_name
PARAMS ((cpp_reader
*));
71 /* Values for the "origin" field of the table below. KANDR and COND
72 directives come from traditional (K&R) C. The difference is, if we
73 care about it while skipping a failed conditional block, its origin
74 is COND. STDC89 directives come from the 1989 C standard.
75 EXTENSION directives are extensions, with origins noted below. */
76 enum { KANDR
= 0, COND
, STDC89
, EXTENSION
};
78 #define TRAD_DIRECT_P(x) ((x) == KANDR || (x) == COND)
80 /* This is the table of directive handlers. It is ordered by
81 frequency of occurrence; the numbers at the end are directive
82 counts from all the source code I have lying around (egcs and libc
83 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
86 The entries with a dash and a name after the count are extensions,
87 of which all but #warning and #include_next are deprecated. The name
88 is where the extension appears to have come from. */
90 /* #sccs is not always recognized. */
92 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION) /* 0 - SVR2? */
94 # define SCCS_ENTRY /* nothing */
97 #define DIRECTIVE_TABLE \
98 D(define, T_DEFINE = 0, KANDR) /* 270554 */ \
99 D(include, T_INCLUDE, KANDR) /* 52262 */ \
100 D(endif, T_ENDIF, COND) /* 45855 */ \
101 D(ifdef, T_IFDEF, COND) /* 22000 */ \
102 D(if, T_IF, COND) /* 18162 */ \
103 D(else, T_ELSE, COND) /* 9863 */ \
104 D(ifndef, T_IFNDEF, COND) /* 9675 */ \
105 D(undef, T_UNDEF, KANDR) /* 4837 */ \
106 D(line, T_LINE, KANDR) /* 2465 */ \
107 D(elif, T_ELIF, COND) /* 610 */ \
108 D(error, T_ERROR, STDC89) /* 475 */ \
109 D(pragma, T_PRAGMA, STDC89) /* 195 */ \
110 D(warning, T_WARNING, EXTENSION) /* 22 - GNU */ \
111 D(include_next, T_INCLUDE_NEXT, EXTENSION) /* 19 - GNU */ \
112 D(ident, T_IDENT, EXTENSION) /* 11 - SVR4 */ \
113 D(import, T_IMPORT, EXTENSION) /* 0 - ObjC */ \
114 D(assert, T_ASSERT, EXTENSION) /* 0 - SVR4 */ \
115 D(unassert, T_UNASSERT, EXTENSION) /* 0 - SVR4 */ \
118 /* Use the table to generate a series of prototypes, an enum for the
119 directive names, and an array of directive handlers. */
121 /* The directive-processing functions are declared to return int
122 instead of void, because some old compilers have trouble with
123 pointers to functions returning void. */
125 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
126 #define D(name, t, o) static int CONCAT2(do_,name) PARAMS ((cpp_reader *));
130 #define D(n, tag, o) tag,
138 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
139 #define D(name, t, origin) \
140 { CONCAT2(do_,name), STRINGX(name), sizeof STRINGX(name) - 1, origin },
141 static const struct directive dtable
[] =
146 #undef DIRECTIVE_TABLE
148 /* Handle a possible # directive.
149 '#' has already been read. */
152 _cpp_handle_directive (pfile
)
159 long old_written
= CPP_WRITTEN (pfile
);
162 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
164 cpp_ice (pfile
, "handle_directive called on macro buffer");
168 /* -traditional directives are recognized only with the # in column 1. */
169 hash_at_bol
= CPP_IN_COLUMN_1 (pfile
);
171 /* Scan the next token, then pretend we didn't. */
172 CPP_SET_MARK (pfile
);
173 pfile
->no_macro_expand
++;
174 tok
= _cpp_get_directive_token (pfile
);
175 pfile
->no_macro_expand
--;
177 ident
= pfile
->token_buffer
+ old_written
;
178 len
= CPP_PWRITTEN (pfile
) - ident
;
179 CPP_SET_WRITTEN (pfile
, old_written
);
180 CPP_GOTO_MARK (pfile
);
182 /* # followed by a number is equivalent to #line. Do not recognize
183 this form in assembly language source files. Complain about this
184 form if we're being pedantic, but not if this is regurgitated
185 input (preprocessed or fed back in by the C++ frontend). */
186 if (tok
== CPP_NUMBER
)
188 if (CPP_OPTION (pfile
, lang_asm
))
191 if (CPP_PEDANTIC (pfile
)
192 && ! CPP_OPTION (pfile
, preprocessed
)
193 && ! CPP_BUFFER (pfile
)->manual_pop
)
194 cpp_pedwarn (pfile
, "# followed by integer");
199 /* If we are rescanning preprocessed input, don't obey any directives
201 else if (CPP_OPTION (pfile
, preprocessed
))
204 /* A line of just # becomes blank. */
205 else if (tok
== CPP_VSPACE
)
208 /* A NAME token might in fact be a directive! */
209 else if (tok
== CPP_NAME
)
211 for (i
= 0; i
< N_DIRECTIVES
; i
++)
213 if (dtable
[i
].length
== len
214 && !strncmp (dtable
[i
].name
, ident
, len
))
217 /* Don't complain about invalid directives in assembly source,
218 we don't know where the comments are, and # may introduce
219 assembler pseudo-ops. */
220 if (!CPP_OPTION (pfile
, lang_asm
))
221 cpp_error (pfile
, "invalid preprocessing directive #%s", ident
);
224 /* And anything else means the # wasn't a directive marker. */
230 /* In -traditional mode, a directive is ignored unless its # is in
232 if (CPP_TRADITIONAL (pfile
) && !hash_at_bol
)
234 if (CPP_WTRADITIONAL (pfile
))
235 cpp_warning (pfile
, "ignoring #%s because of its indented #",
240 /* no_directives is set when we are parsing macro arguments. Directives
241 in macro arguments are undefined behavior (C99 6.10.3.11); this
242 implementation chooses to make them hard errors. */
243 if (pfile
->no_directives
)
245 cpp_error (pfile
, "#%s may not be used inside a macro argument",
247 _cpp_skip_rest_of_line (pfile
);
251 /* Issue -pedantic warnings for extended directives. */
252 if (CPP_PEDANTIC (pfile
) && dtable
[i
].origin
== EXTENSION
)
253 cpp_pedwarn (pfile
, "ISO C does not allow #%s", dtable
[i
].name
);
255 /* -Wtraditional gives warnings about directives with inappropriate
257 if (CPP_WTRADITIONAL (pfile
))
259 if (!hash_at_bol
&& TRAD_DIRECT_P (dtable
[i
].origin
))
260 cpp_warning (pfile
, "traditional C ignores #%s with the # indented",
262 else if (hash_at_bol
&& ! TRAD_DIRECT_P (dtable
[i
].origin
))
264 "suggest hiding #%s from traditional C with an indented #",
268 /* Unfortunately, it's necessary to scan the directive name again,
269 now we know we're going to consume it. FIXME. */
271 pfile
->no_macro_expand
++;
272 _cpp_get_directive_token (pfile
);
273 pfile
->no_macro_expand
--;
274 CPP_SET_WRITTEN (pfile
, old_written
);
276 /* Some directives (e.g. #if) may return a request to execute
277 another directive handler immediately. No directive ever
278 requests that #define be executed immediately, so it is safe for
279 the loop to terminate when some function returns 0 (== T_DEFINE). */
280 while ((i
= dtable
[i
].func (pfile
)));
284 /* Pass a directive through to the output file.
285 BUF points to the contents of the directive, as a contiguous string.
286 LEN is the length of the string pointed to by BUF.
287 KEYWORD is the keyword-table entry for the directive. */
290 pass_thru_directive (buf
, len
, pfile
, keyword
)
296 const struct directive
*kt
= &dtable
[keyword
];
297 register unsigned klen
= kt
->length
;
299 CPP_RESERVE (pfile
, 1 + klen
+ len
);
300 CPP_PUTC_Q (pfile
, '#');
301 CPP_PUTS_Q (pfile
, kt
->name
, klen
);
302 if (len
!= 0 && buf
[0] != ' ')
303 CPP_PUTC_Q (pfile
, ' ');
304 CPP_PUTS_Q (pfile
, buf
, len
);
307 /* Subroutine of do_define: determine the name of the macro to be
311 get_macro_name (pfile
)
316 here
= CPP_WRITTEN (pfile
);
317 if (_cpp_get_directive_token (pfile
) != CPP_NAME
)
319 cpp_error (pfile
, "`#define' must be followed by an identifier");
323 len
= CPP_WRITTEN (pfile
) - here
;
324 if (len
== 7 && !strncmp (pfile
->token_buffer
+ here
, "defined", 7))
326 cpp_error (pfile
, "`defined' is not a legal macro name");
333 _cpp_skip_rest_of_line (pfile
);
337 /* Process a #define command. */
348 int funlike
= 0, empty
= 0;
350 enum cpp_ttype token
;
352 pfile
->no_macro_expand
++;
353 pfile
->parsing_define_directive
++;
354 CPP_OPTION (pfile
, discard_comments
)++;
355 CPP_OPTION (pfile
, no_line_commands
)++;
357 here
= CPP_WRITTEN (pfile
);
358 len
= get_macro_name (pfile
);
362 /* Copy out the name so we can pop the token buffer. */
363 len
= CPP_WRITTEN (pfile
) - here
;
364 sym
= (U_CHAR
*) alloca (len
+ 1);
365 memcpy (sym
, pfile
->token_buffer
+ here
, len
);
368 /* If the next character, with no intervening whitespace, is '(',
369 then this is a function-like macro.
370 XXX Layering violation. */
371 CPP_SET_MARK (pfile
);
372 token
= _cpp_get_directive_token (pfile
);
373 if (token
== CPP_VSPACE
)
374 empty
= 0; /* Empty definition of object like macro. */
375 else if (token
== CPP_LPAREN
&& ADJACENT_TO_MARK (pfile
))
377 else if (ADJACENT_TO_MARK (pfile
))
378 /* If this is an object-like macro, C99 requires white space after
380 cpp_pedwarn (pfile
, "missing white space after `#define %.*s'", len
, sym
);
381 CPP_GOTO_MARK (pfile
);
382 CPP_SET_WRITTEN (pfile
, here
);
386 def
= _cpp_create_definition (pfile
, funlike
);
391 slot
= _cpp_lookup_slot (pfile
, sym
, len
, 1, &hash
);
395 HASHNODE
*hp
= *slot
;
397 /* Redefining a macro is ok if the definitions are the same. */
398 if (hp
->type
== T_MACRO
)
399 ok
= ! empty
&& ! _cpp_compare_defs (pfile
, def
, hp
->value
.defn
);
400 else if (hp
->type
== T_EMPTY
)
402 /* Redefining a constant is ok with -D. */
403 else if (hp
->type
== T_CONST
|| hp
->type
== T_STDC
)
404 ok
= ! pfile
->done_initializing
;
405 /* Otherwise it's not ok. */
408 /* Print the warning or error if it's not ok. */
411 if (hp
->type
== T_POISON
)
412 cpp_error (pfile
, "redefining poisoned `%.*s'", len
, sym
);
414 cpp_pedwarn (pfile
, "`%.*s' redefined", len
, sym
);
415 if (hp
->type
== T_MACRO
&& pfile
->done_initializing
)
417 DEFINITION
*d
= hp
->value
.defn
;
418 cpp_pedwarn_with_file_and_line (pfile
, d
->file
, d
->line
, d
->col
,
419 "this is the location of the previous definition");
422 if (hp
->type
!= T_POISON
)
424 /* Replace the old definition. */
425 if (hp
->type
== T_MACRO
)
426 _cpp_free_definition (hp
->value
.defn
);
435 hp
->value
.defn
= def
;
441 HASHNODE
*hp
= _cpp_make_hashnode (sym
, len
, T_MACRO
, hash
);
442 hp
->value
.defn
= def
;
446 if (CPP_OPTION (pfile
, debug_output
)
447 || CPP_OPTION (pfile
, dump_macros
) == dump_definitions
)
448 _cpp_dump_definition (pfile
, sym
, len
, def
);
449 else if (CPP_OPTION (pfile
, dump_macros
) == dump_names
)
450 pass_thru_directive (sym
, len
, pfile
, T_DEFINE
);
453 pfile
->no_macro_expand
--;
454 pfile
->parsing_define_directive
--;
455 CPP_OPTION (pfile
, discard_comments
)--;
456 CPP_OPTION (pfile
, no_line_commands
)--;
461 * write out a #line command, for instance, after an #include file.
462 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
466 _cpp_output_line_command (pfile
, file_change
)
468 enum file_change_code file_change
;
473 if (CPP_OPTION (pfile
, no_line_commands
)
474 || CPP_OPTION (pfile
, no_output
))
477 ip
= cpp_file_buffer (pfile
);
480 /* If the current file has not changed, we omit the #line if it would
481 appear to be a no-op, and we output a few newlines instead
482 if we want to increase the line number by a small amount.
483 We cannot do this if pfile->lineno is zero, because that means we
484 haven't output any line commands yet. (The very first line command
485 output is a `same_file' command.) */
486 if (file_change
== same_file
&& pfile
->lineno
!= 0)
488 if (line
== pfile
->lineno
)
491 /* If the inherited line number is a little too small,
492 output some newlines instead of a #line command. */
493 if (line
> pfile
->lineno
&& line
< pfile
->lineno
+ 8)
495 CPP_RESERVE (pfile
, 20);
496 while (line
> pfile
->lineno
)
498 CPP_PUTC_Q (pfile
, '\n');
505 CPP_RESERVE (pfile
, 4 * strlen (ip
->nominal_fname
) + 50);
506 CPP_PUTS_Q (pfile
, "# ", 2);
508 sprintf ((char *) CPP_PWRITTEN (pfile
), "%u ", line
);
509 CPP_ADJUST_WRITTEN (pfile
, strlen (CPP_PWRITTEN (pfile
)));
511 _cpp_quote_string (pfile
, ip
->nominal_fname
);
512 if (file_change
!= same_file
&& file_change
!= rename_file
)
514 CPP_PUTC_Q (pfile
, ' ');
515 CPP_PUTC_Q (pfile
, file_change
== enter_file
? '1' : '2');
517 /* Tell cc1 if following text comes from a system header file. */
518 if (ip
->system_header_p
)
520 CPP_PUTC_Q (pfile
, ' ');
521 CPP_PUTC_Q (pfile
, '3');
523 #ifndef NO_IMPLICIT_EXTERN_C
524 /* Tell cc1plus if following text should be treated as C. */
525 if (ip
->system_header_p
== 2 && CPP_OPTION (pfile
, cplusplus
))
527 CPP_PUTC_Q (pfile
, ' ');
528 CPP_PUTC_Q (pfile
, '4');
531 CPP_PUTC_Q (pfile
, '\n');
532 pfile
->lineno
= line
;
535 /* Handle #include and #import. */
538 parse_include (pfile
, name
)
542 long old_written
= CPP_WRITTEN (pfile
);
543 enum cpp_ttype token
;
546 pfile
->parsing_include_directive
++;
547 token
= _cpp_get_directive_token (pfile
);
548 pfile
->parsing_include_directive
--;
550 len
= CPP_WRITTEN (pfile
) - old_written
;
552 if (token
== CPP_STRING
)
553 ; /* No special treatment required. */
555 else if (token
== CPP_NAME
)
557 /* Support '#include xyz' like VAX-C. It is taken as
558 '#include <xyz.h>' and generates a warning. */
559 cpp_warning (pfile
, "#%s filename is obsolete, use #%s <filename.h>",
562 /* Rewrite the token to <xyz.h>. */
563 CPP_RESERVE (pfile
, 4);
565 memmove (pfile
->token_buffer
+ old_written
+ 1,
566 pfile
->token_buffer
+ old_written
,
567 CPP_WRITTEN (pfile
) - old_written
);
568 pfile
->token_buffer
[old_written
] = '<';
569 CPP_PUTS_Q (pfile
, ".h>", 2);
574 cpp_error (pfile
, "`#%s' expects \"FILENAME\" or <FILENAME>", name
);
575 CPP_SET_WRITTEN (pfile
, old_written
);
576 _cpp_skip_rest_of_line (pfile
);
580 CPP_NUL_TERMINATE (pfile
);
581 CPP_ADJUST_WRITTEN (pfile
, 1);
583 if (_cpp_get_directive_token (pfile
) != CPP_VSPACE
)
585 cpp_error (pfile
, "junk at end of `#%s'", name
);
586 _cpp_skip_rest_of_line (pfile
);
589 CPP_SET_WRITTEN (pfile
, old_written
);
592 cpp_error (pfile
, "empty file name in `#%s'", name
);
604 len
= parse_include (pfile
, dtable
[T_INCLUDE
].name
);
607 token
= alloca (len
+ 1);
608 strcpy (token
, CPP_PWRITTEN (pfile
));
610 if (CPP_OPTION (pfile
, dump_includes
))
611 pass_thru_directive (token
, len
, pfile
, T_INCLUDE
);
613 _cpp_execute_include (pfile
, token
, len
, 0, 0);
624 if (CPP_OPTION (pfile
, warn_import
)
625 && !CPP_BUFFER (pfile
)->system_header_p
&& !pfile
->import_warning
)
627 pfile
->import_warning
= 1;
629 "#import is obsolete, use an #ifndef wrapper in the header file");
632 len
= parse_include (pfile
, dtable
[T_IMPORT
].name
);
635 token
= alloca (len
+ 1);
636 strcpy (token
, CPP_PWRITTEN (pfile
));
638 if (CPP_OPTION (pfile
, dump_includes
))
639 pass_thru_directive (token
, len
, pfile
, T_IMPORT
);
641 _cpp_execute_include (pfile
, token
, len
, 1, 0);
646 do_include_next (pfile
)
651 struct file_name_list
*search_start
= 0;
653 len
= parse_include (pfile
, dtable
[T_INCLUDE_NEXT
].name
);
656 token
= alloca (len
+ 1);
657 strcpy (token
, CPP_PWRITTEN (pfile
));
659 if (CPP_OPTION (pfile
, dump_includes
))
660 pass_thru_directive (token
, len
, pfile
, T_INCLUDE_NEXT
);
662 /* For #include_next, skip in the search path past the dir in which the
663 containing file was found. Treat files specified using an absolute path
664 as if there are no more directories to search. Treat the primary source
665 file like any other included source, but generate a warning. */
666 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
)))
668 if (CPP_BUFFER (pfile
)->ihash
->foundhere
!= ABSOLUTE_PATH
)
669 search_start
= CPP_BUFFER (pfile
)->ihash
->foundhere
->next
;
672 cpp_warning (pfile
, "#include_next in primary source file");
674 _cpp_execute_include (pfile
, token
, len
, 0, search_start
);
678 /* Subroutine of do_line. Read next token from PFILE without adding it to
679 the output buffer. If it is a number between 1 and 4, store it in *NUM
680 and return 1; otherwise, return 0 and complain if we aren't at the end
684 read_line_number (pfile
, num
)
688 long save_written
= CPP_WRITTEN (pfile
);
690 enum cpp_ttype token
= _cpp_get_directive_token (pfile
);
691 CPP_SET_WRITTEN (pfile
, save_written
);
692 p
= pfile
->token_buffer
+ save_written
;
694 if (token
== CPP_NUMBER
&& *p
>= '1' && *p
<= '4' && p
[1] == '\0')
701 if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
)
702 cpp_error (pfile
, "invalid format `#line' command");
707 /* Interpret #line command.
708 Note that the filename string (if any) is treated as if it were an
709 include filename. That means no escape handling. */
715 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
717 long old_written
= CPP_WRITTEN (pfile
);
718 enum file_change_code file_change
= same_file
;
719 enum cpp_ttype token
;
722 token
= _cpp_get_directive_token (pfile
);
724 if (token
!= CPP_NUMBER
)
726 cpp_error (pfile
, "token after `#line' is not an integer");
727 goto bad_line_directive
;
730 new_lineno
= strtol (pfile
->token_buffer
+ old_written
, &x
, 10);
733 cpp_error (pfile
, "token after `#line' is not an integer");
734 goto bad_line_directive
;
736 CPP_SET_WRITTEN (pfile
, old_written
);
738 if (CPP_PEDANTIC (pfile
) && (new_lineno
<= 0 || new_lineno
> 32767))
739 cpp_pedwarn (pfile
, "line number out of range in `#line' command");
741 token
= _cpp_get_directive_token (pfile
);
743 if (token
== CPP_STRING
)
745 U_CHAR
*fname
= pfile
->token_buffer
+ old_written
+ 1;
746 U_CHAR
*end_name
= CPP_PWRITTEN (pfile
) - 1;
747 int action_number
= 0;
749 file_change
= rename_file
;
751 if (read_line_number (pfile
, &action_number
))
753 if (CPP_PEDANTIC (pfile
))
754 cpp_pedwarn (pfile
, "garbage at end of `#line' command");
756 if (action_number
== 1)
758 file_change
= enter_file
;
759 read_line_number (pfile
, &action_number
);
761 else if (action_number
== 2)
763 file_change
= leave_file
;
764 read_line_number (pfile
, &action_number
);
766 if (action_number
== 3)
768 ip
->system_header_p
= 1;
769 read_line_number (pfile
, &action_number
);
771 if (action_number
== 4)
773 ip
->system_header_p
= 2;
774 read_line_number (pfile
, &action_number
);
780 if (strcmp (fname
, ip
->nominal_fname
))
782 const char *newname
, *oldname
;
783 if (!strcmp (fname
, ip
->ihash
->name
))
784 newname
= ip
->ihash
->name
;
785 else if (ip
->last_nominal_fname
786 && !strcmp (fname
, ip
->last_nominal_fname
))
787 newname
= ip
->last_nominal_fname
;
789 newname
= xstrdup (fname
);
791 oldname
= ip
->nominal_fname
;
792 ip
->nominal_fname
= newname
;
794 if (ip
->last_nominal_fname
795 && ip
->last_nominal_fname
!= oldname
796 && ip
->last_nominal_fname
!= newname
797 && ip
->last_nominal_fname
!= ip
->ihash
->name
)
798 free ((void *) ip
->last_nominal_fname
);
800 if (newname
== ip
->ihash
->name
)
801 ip
->last_nominal_fname
= NULL
;
803 ip
->last_nominal_fname
= oldname
;
806 else if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
)
808 cpp_error (pfile
, "token after `#line %d' is not a string", new_lineno
);
809 goto bad_line_directive
;
812 /* The Newline at the end of this line remains to be processed.
813 To put the next line at the specified line number,
814 we must store a line number now that is one less. */
815 ip
->lineno
= new_lineno
- 1;
816 CPP_SET_WRITTEN (pfile
, old_written
);
817 _cpp_output_line_command (pfile
, file_change
);
821 _cpp_skip_rest_of_line (pfile
);
822 CPP_SET_WRITTEN (pfile
, old_written
);
826 /* Remove the definition of a symbol from the symbol table.
827 According to the C standard, it is not an error to undef
828 something that has no definitions. */
836 long here
= CPP_WRITTEN (pfile
);
837 enum cpp_ttype token
;
839 pfile
->no_macro_expand
++;
840 token
= _cpp_get_directive_token (pfile
);
841 pfile
->no_macro_expand
--;
843 if (token
!= CPP_NAME
)
845 cpp_error (pfile
, "token after #undef is not an identifier");
846 _cpp_skip_rest_of_line (pfile
);
849 len
= CPP_WRITTEN (pfile
) - here
;
851 token
= _cpp_get_directive_token (pfile
);
852 if (token
!= CPP_VSPACE
)
854 cpp_pedwarn (pfile
, "junk on line after #undef");
855 _cpp_skip_rest_of_line (pfile
);
858 name
= pfile
->token_buffer
+ here
;
859 CPP_SET_WRITTEN (pfile
, here
);
861 slot
= _cpp_lookup_slot (pfile
, name
, len
, 0, 0);
864 HASHNODE
*hp
= *slot
;
865 if (hp
->type
== T_POISON
)
866 cpp_error (pfile
, "cannot undefine poisoned `%s'", hp
->name
);
869 /* If we are generating additional info for debugging (with -g) we
870 need to pass through all effective #undef commands. */
871 if (CPP_OPTION (pfile
, debug_output
))
872 pass_thru_directive (hp
->name
, len
, pfile
, T_UNDEF
);
874 if (hp
->type
!= T_MACRO
)
875 cpp_warning (pfile
, "undefining `%s'", hp
->name
);
877 htab_clear_slot (pfile
->hashtab
, (void **)slot
);
885 * Report an error detected by the program we are processing.
886 * Use the text of the line in the error message.
887 * (We use error because it prints the filename & line#.)
894 const U_CHAR
*text
, *limit
;
896 _cpp_skip_hspace (pfile
);
897 text
= CPP_BUFFER (pfile
)->cur
;
898 _cpp_skip_rest_of_line (pfile
);
899 limit
= CPP_BUFFER (pfile
)->cur
;
901 cpp_error (pfile
, "#error %.*s", (int)(limit
- text
), text
);
906 * Report a warning detected by the program we are processing.
907 * Use the text of the line in the warning message, then continue.
914 const U_CHAR
*text
, *limit
;
916 _cpp_skip_hspace (pfile
);
917 text
= CPP_BUFFER (pfile
)->cur
;
918 _cpp_skip_rest_of_line (pfile
);
919 limit
= CPP_BUFFER (pfile
)->cur
;
921 cpp_warning (pfile
, "#warning %.*s", (int)(limit
- text
), text
);
925 /* Report program identification. */
931 long old_written
= CPP_WRITTEN (pfile
);
933 CPP_PUTS (pfile
, "#ident ", 7);
935 /* Next token should be a string constant. */
936 if (_cpp_get_directive_token (pfile
) == CPP_STRING
)
937 /* And then a newline. */
938 if (_cpp_get_directive_token (pfile
) == CPP_VSPACE
)
939 /* Good - ship it. */
942 cpp_error (pfile
, "invalid #ident");
943 _cpp_skip_rest_of_line (pfile
);
944 CPP_SET_WRITTEN (pfile
, old_written
); /* discard directive */
949 /* Pragmata handling. We handle some of these, and pass the rest on
950 to the front end. C99 defines three pragmas and says that no macro
951 expansion is to be performed on them; whether or not macro
952 expansion happens for other pragmas is implementation defined.
953 This implementation never macro-expands the text after #pragma.
955 We currently do not support the _Pragma operator. Support for that
956 has to be coordinated with the front end. Proposed implementation:
957 both #pragma blah blah and _Pragma("blah blah") become
958 __builtin_pragma(blah blah) and we teach the parser about that. */
960 /* Sub-handlers for the pragmas needing treatment here.
961 They return 1 if the token buffer is to be popped, 0 if not. */
962 static int do_pragma_once
PARAMS ((cpp_reader
*));
963 static int do_pragma_implementation
PARAMS ((cpp_reader
*));
964 static int do_pragma_poison
PARAMS ((cpp_reader
*));
965 static int do_pragma_default
PARAMS ((cpp_reader
*));
974 enum cpp_ttype token
;
976 here
= CPP_WRITTEN (pfile
);
977 CPP_PUTS (pfile
, "#pragma ", 8);
979 key
= CPP_WRITTEN (pfile
);
980 pfile
->no_macro_expand
++;
981 token
= _cpp_get_directive_token (pfile
);
982 if (token
!= CPP_NAME
)
984 if (token
== CPP_VSPACE
)
990 buf
= pfile
->token_buffer
+ key
;
991 CPP_PUTC (pfile
, ' ');
993 #define tokis(x) !strncmp((char *) buf, x, sizeof(x) - 1)
995 pop
= do_pragma_once (pfile
);
996 else if (tokis ("implementation"))
997 pop
= do_pragma_implementation (pfile
);
998 else if (tokis ("poison"))
999 pop
= do_pragma_poison (pfile
);
1001 pop
= do_pragma_default (pfile
);
1004 if (_cpp_get_directive_token (pfile
) != CPP_VSPACE
)
1008 CPP_SET_WRITTEN (pfile
, here
);
1009 pfile
->no_macro_expand
--;
1013 cpp_error (pfile
, "malformed #pragma directive");
1014 _cpp_skip_rest_of_line (pfile
);
1016 CPP_SET_WRITTEN (pfile
, here
);
1017 pfile
->no_macro_expand
--;
1022 do_pragma_default (pfile
)
1025 while (_cpp_get_directive_token (pfile
) != CPP_VSPACE
)
1026 CPP_PUTC (pfile
, ' ');
1031 do_pragma_once (pfile
)
1034 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
1036 /* Allow #pragma once in system headers, since that's not the user's
1038 if (!ip
->system_header_p
)
1039 cpp_warning (pfile
, "`#pragma once' is obsolete");
1041 if (CPP_PREV_BUFFER (ip
) == NULL
)
1042 cpp_warning (pfile
, "`#pragma once' outside include file");
1044 ip
->ihash
->control_macro
= (const U_CHAR
*) ""; /* never repeat */
1050 do_pragma_implementation (pfile
)
1053 /* Be quiet about `#pragma implementation' for a file only if it hasn't
1054 been included yet. */
1055 enum cpp_ttype token
;
1056 long written
= CPP_WRITTEN (pfile
);
1061 token
= _cpp_get_directive_token (pfile
);
1062 if (token
== CPP_VSPACE
)
1064 else if (token
!= CPP_STRING
)
1066 cpp_error (pfile
, "malformed #pragma implementation");
1070 /* Trim the leading and trailing quote marks from the string. */
1071 name
= pfile
->token_buffer
+ written
+ 1;
1072 len
= CPP_PWRITTEN (pfile
) - name
;
1073 copy
= (U_CHAR
*) alloca (len
);
1074 memcpy (copy
, name
, len
- 1);
1075 copy
[len
- 1] = '\0';
1077 if (cpp_included (pfile
, copy
))
1079 "`#pragma implementation' for `%s' appears after file is included",
1085 do_pragma_poison (pfile
)
1088 /* Poison these symbols so that all subsequent usage produces an
1094 enum cpp_ttype token
;
1098 /* As a rule, don't include #pragma poison commands in output,
1099 unless the user asks for them. */
1100 writeit
= (CPP_OPTION (pfile
, debug_output
)
1101 || CPP_OPTION (pfile
, dump_macros
) == dump_definitions
1102 || CPP_OPTION (pfile
, dump_macros
) == dump_names
);
1106 written
= CPP_WRITTEN (pfile
);
1107 token
= _cpp_get_directive_token (pfile
);
1108 if (token
== CPP_VSPACE
)
1110 if (token
!= CPP_NAME
)
1112 cpp_error (pfile
, "invalid #pragma poison directive");
1113 _cpp_skip_rest_of_line (pfile
);
1117 p
= pfile
->token_buffer
+ written
;
1119 slot
= _cpp_lookup_slot (pfile
, p
, len
, 1, &hash
);
1122 HASHNODE
*hp
= *slot
;
1123 if (hp
->type
!= T_POISON
)
1125 cpp_warning (pfile
, "poisoning existing macro `%s'", p
);
1126 if (hp
->type
== T_MACRO
)
1127 _cpp_free_definition (hp
->value
.defn
);
1129 hp
->type
= T_POISON
;
1134 HASHNODE
*hp
= _cpp_make_hashnode (p
, len
, T_POISON
, hash
);
1135 hp
->value
.cpval
= 0;
1139 CPP_PUTC (pfile
, ' ');
1144 /* Just ignore #sccs, on systems where we define it at all. */
1145 #ifdef SCCS_DIRECTIVE
1150 _cpp_skip_rest_of_line (pfile
);
1155 /* We've found an `#if' directive. If the only thing before it in
1156 this file is white space, and if it is of the form
1157 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1158 for inclusion of this file. (See redundant_include_p in cppfiles.c
1159 for an explanation of controlling macros.) If so, return a
1160 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1163 detect_if_not_defined (pfile
)
1166 U_CHAR
*control_macro
= 0;
1168 if (pfile
->only_seen_white
== 2)
1171 enum cpp_ttype token
;
1174 int need_rparen
= 0;
1176 /* Save state required for restore. */
1177 pfile
->no_macro_expand
++;
1178 CPP_SET_MARK (pfile
);
1179 base_offset
= CPP_WRITTEN (pfile
);
1182 if (_cpp_get_directive_token (pfile
) != CPP_OTHER
1183 || CPP_WRITTEN (pfile
) != (size_t) base_offset
+ 1
1184 || CPP_PWRITTEN (pfile
)[-1] != '!')
1187 /* ...then `defined', */
1188 token_offset
= CPP_WRITTEN (pfile
);
1189 token
= _cpp_get_directive_token (pfile
);
1190 if (token
!= CPP_NAME
)
1192 ident
= pfile
->token_buffer
+ token_offset
;
1193 CPP_NUL_TERMINATE (pfile
);
1194 if (strcmp (ident
, "defined"))
1197 /* ...then an optional '(' and the name, */
1198 token_offset
= CPP_WRITTEN (pfile
);
1199 token
= _cpp_get_directive_token (pfile
);
1200 if (token
== CPP_LPAREN
)
1202 token_offset
= CPP_WRITTEN (pfile
);
1203 token
= _cpp_get_directive_token (pfile
);
1204 if (token
!= CPP_NAME
)
1208 else if (token
!= CPP_NAME
)
1211 ident
= pfile
->token_buffer
+ token_offset
;
1212 CPP_NUL_TERMINATE (pfile
);
1214 /* ...then the ')', if necessary, */
1215 if ((!need_rparen
|| _cpp_get_directive_token (pfile
) == CPP_RPAREN
)
1216 /* ...and make sure there's nothing else on the line. */
1217 && _cpp_get_directive_token (pfile
) == CPP_VSPACE
)
1218 control_macro
= (U_CHAR
*) xstrdup (ident
);
1221 CPP_SET_WRITTEN (pfile
, base_offset
);
1222 pfile
->no_macro_expand
--;
1223 CPP_GOTO_MARK (pfile
);
1226 return control_macro
;
1230 * #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1231 * Also, check for a reinclude preventer of the form #if !defined (MACRO).
1238 U_CHAR
*control_macro
= detect_if_not_defined (pfile
);
1239 int value
= _cpp_parse_expr (pfile
);
1240 return conditional_skip (pfile
, value
== 0, T_IF
, control_macro
);
1244 * handle a #elif directive by not changing if_stack either.
1245 * see the comment above do_else.
1252 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
1254 cpp_error (pfile
, "`#elif' not within a conditional");
1259 if (pfile
->if_stack
->type
== T_ELSE
)
1261 cpp_error (pfile
, "`#elif' after `#else'");
1262 cpp_error_with_line (pfile
, pfile
->if_stack
->lineno
, -1,
1263 "the conditional began here");
1265 pfile
->if_stack
->type
= T_ELIF
;
1268 if (pfile
->if_stack
->if_succeeded
)
1270 _cpp_skip_rest_of_line (pfile
);
1271 return skip_if_group (pfile
);
1273 if (_cpp_parse_expr (pfile
) == 0)
1274 return skip_if_group (pfile
);
1276 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
1280 /* Parse an #ifdef or #ifndef directive. Returns 1 for defined, 0 for
1281 not defined; the macro tested is left in the token buffer (but
1285 parse_ifdef (pfile
, name
)
1291 enum cpp_ttype token
;
1292 long old_written
= CPP_WRITTEN (pfile
);
1295 pfile
->no_macro_expand
++;
1296 token
= _cpp_get_directive_token (pfile
);
1297 pfile
->no_macro_expand
--;
1299 ident
= pfile
->token_buffer
+ old_written
;
1300 len
= CPP_WRITTEN (pfile
) - old_written
;
1302 if (token
== CPP_VSPACE
)
1304 if (! CPP_TRADITIONAL (pfile
))
1305 cpp_pedwarn (pfile
, "`#%s' with no argument", name
);
1309 else if (token
== CPP_NAME
)
1311 defined
= cpp_defined (pfile
, ident
, len
);
1312 CPP_NUL_TERMINATE (pfile
);
1313 CPP_ADJUST_WRITTEN (pfile
, 1);
1318 if (! CPP_TRADITIONAL (pfile
))
1319 cpp_error (pfile
, "`#%s' with invalid argument", name
);
1322 if (!CPP_TRADITIONAL (pfile
))
1324 if (_cpp_get_directive_token (pfile
) == CPP_VSPACE
)
1327 cpp_pedwarn (pfile
, "garbage at end of `#%s' argument", name
);
1329 _cpp_skip_rest_of_line (pfile
);
1332 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
1336 /* #ifdef is dead simple. */
1342 int skip
= ! parse_ifdef (pfile
, dtable
[T_IFDEF
].name
);
1343 return conditional_skip (pfile
, skip
, T_IFDEF
, 0);
1346 /* #ifndef is a tad more complex, because we need to check for a
1347 no-reinclusion wrapper. */
1353 int start_of_file
, skip
;
1354 U_CHAR
*control_macro
= 0;
1356 start_of_file
= pfile
->only_seen_white
== 2;
1357 skip
= parse_ifdef (pfile
, dtable
[T_IFNDEF
].name
);
1359 if (start_of_file
&& !skip
)
1360 control_macro
= (U_CHAR
*) xstrdup (CPP_PWRITTEN (pfile
));
1362 return conditional_skip (pfile
, skip
, T_IFNDEF
, control_macro
);
1365 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1366 If this is a #ifndef starting at the beginning of a file,
1367 CONTROL_MACRO is the macro name tested by the #ifndef.
1368 Otherwise, CONTROL_MACRO is 0. */
1371 conditional_skip (pfile
, skip
, type
, control_macro
)
1375 U_CHAR
*control_macro
;
1379 temp
= (IF_STACK
*) xcalloc (1, sizeof (IF_STACK
));
1380 temp
->lineno
= CPP_BUFFER (pfile
)->lineno
;
1381 temp
->next
= pfile
->if_stack
;
1382 temp
->control_macro
= control_macro
;
1383 pfile
->if_stack
= temp
;
1385 pfile
->if_stack
->type
= type
;
1388 return skip_if_group (pfile
);
1390 ++pfile
->if_stack
->if_succeeded
;
1394 /* Subroutine of skip_if_group. Examine one preprocessing directive
1395 and return 0 if skipping should continue, or the directive number
1396 of the directive that ends the block if it should halt.
1398 Also adjusts the if_stack as appropriate. The `#' has been read,
1399 but not the identifier. */
1402 consider_directive_while_skipping (pfile
, stack
)
1411 /* -traditional directives are recognized only with the # in column 1. */
1412 hash_at_bol
= CPP_IN_COLUMN_1 (pfile
);
1414 ident
= CPP_WRITTEN (pfile
);
1415 if (_cpp_get_directive_token (pfile
) != CPP_NAME
)
1417 len
= CPP_WRITTEN (pfile
) - ident
;
1419 for (i
= 0; i
< N_DIRECTIVES
; i
++)
1421 if (dtable
[i
].length
== len
1422 && !strncmp (dtable
[i
].name
, pfile
->token_buffer
+ ident
, len
))
1423 goto real_directive
;
1429 /* If it's not a directive of interest to us, return now. */
1430 if (dtable
[i
].origin
!= COND
)
1433 /* First, deal with -traditional and -Wtraditional.
1434 All COND directives are from K+R. */
1438 if (CPP_TRADITIONAL (pfile
))
1440 if (CPP_WTRADITIONAL (pfile
))
1441 cpp_warning (pfile
, "ignoring #%s because of its indented #",
1445 if (CPP_WTRADITIONAL (pfile
))
1446 cpp_warning (pfile
, "traditional C ignores %s with the # indented",
1453 cpp_ice (pfile
, "non COND directive in switch in c_d_w_s");
1459 temp
= (IF_STACK
*) xcalloc (1, sizeof (IF_STACK
));
1460 temp
->lineno
= CPP_BUFFER (pfile
)->lineno
;
1461 temp
->next
= pfile
->if_stack
;
1463 pfile
->if_stack
= temp
;
1467 if (pfile
->if_stack
!= stack
)
1468 validate_else (pfile
, dtable
[i
].name
);
1471 if (pfile
->if_stack
== stack
)
1474 pfile
->if_stack
->type
= i
;
1478 if (pfile
->if_stack
!= stack
)
1479 validate_else (pfile
, dtable
[i
].name
);
1481 if (pfile
->if_stack
== stack
)
1484 temp
= pfile
->if_stack
;
1485 pfile
->if_stack
= temp
->next
;
1491 /* Skip to #endif, #else, or #elif. Consumes the directive that
1492 causes it to stop, but not its argument. Returns the number of
1493 that directive, which must be passed back up to
1494 _cpp_handle_directive, which will execute it. */
1496 skip_if_group (pfile
)
1499 enum cpp_ttype token
;
1500 IF_STACK
*save_if_stack
= pfile
->if_stack
; /* don't pop past here */
1504 /* We are no longer at the start of the file. */
1505 pfile
->only_seen_white
= 0;
1507 old_written
= CPP_WRITTEN (pfile
);
1508 pfile
->no_macro_expand
++;
1509 CPP_OPTION (pfile
, no_line_commands
)++;
1512 /* We are at the end of a line. Only cpp_get_token knows how to
1513 advance the line number correctly. */
1514 token
= cpp_get_token (pfile
);
1515 if (token
== CPP_POP
)
1516 break; /* Caller will issue error. */
1518 else if (token
!= CPP_VSPACE
)
1519 cpp_ice (pfile
, "cpp_get_token returned %d in skip_if_group", token
);
1520 CPP_SET_WRITTEN (pfile
, old_written
);
1522 token
= _cpp_get_directive_token (pfile
);
1524 if (token
== CPP_DIRECTIVE
)
1526 ret
= consider_directive_while_skipping (pfile
, save_if_stack
);
1531 if (token
!= CPP_VSPACE
)
1532 _cpp_skip_rest_of_line (pfile
);
1534 CPP_SET_WRITTEN (pfile
, old_written
);
1535 pfile
->no_macro_expand
--;
1536 CPP_OPTION (pfile
, no_line_commands
)--;
1541 * handle a #else directive. Do this by just continuing processing
1542 * without changing if_stack ; this is so that the error message
1543 * for missing #endif's etc. will point to the original #if. It
1544 * is possible that something different would be better.
1551 validate_else (pfile
, dtable
[T_ELSE
].name
);
1552 _cpp_skip_rest_of_line (pfile
);
1554 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
1556 cpp_error (pfile
, "`#else' not within a conditional");
1561 /* #ifndef can't have its special treatment for containing the whole file
1562 if it has a #else clause. */
1563 pfile
->if_stack
->control_macro
= 0;
1565 if (pfile
->if_stack
->type
== T_ELSE
)
1567 cpp_error (pfile
, "`#else' after `#else'");
1568 cpp_error_with_line (pfile
, pfile
->if_stack
->lineno
, -1,
1569 "the conditional began here");
1571 pfile
->if_stack
->type
= T_ELSE
;
1574 if (pfile
->if_stack
->if_succeeded
)
1575 return skip_if_group (pfile
);
1577 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
1582 * unstack after #endif command
1589 validate_else (pfile
, dtable
[T_ENDIF
].name
);
1590 _cpp_skip_rest_of_line (pfile
);
1592 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
1593 cpp_error (pfile
, "`#endif' not within a conditional");
1596 IF_STACK
*temp
= pfile
->if_stack
;
1597 pfile
->if_stack
= temp
->next
;
1598 if (temp
->control_macro
!= 0)
1599 pfile
->potential_control_macro
= temp
->control_macro
;
1605 /* Issue -pedantic warning for text which is not a comment following
1606 an #else or #endif. Do not warn in system headers, as this is harmless
1607 and very common on old systems. */
1610 validate_else (pfile
, directive
)
1612 const char *directive
;
1615 if (! CPP_PEDANTIC (pfile
))
1618 old_written
= CPP_WRITTEN (pfile
);
1619 pfile
->no_macro_expand
++;
1620 if (_cpp_get_directive_token (pfile
) != CPP_VSPACE
)
1622 "text following `#%s' violates ANSI standard", directive
);
1623 CPP_SET_WRITTEN (pfile
, old_written
);
1624 pfile
->no_macro_expand
--;
1628 _cpp_handle_eof (pfile
)
1631 cpp_buffer
*next_buf
= CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
1632 struct if_stack
*ifs
, *nifs
;
1634 /* Unwind the conditional stack and generate error messages. */
1635 for (ifs
= pfile
->if_stack
;
1636 ifs
!= CPP_BUFFER (pfile
)->if_stack
;
1639 cpp_error_with_line (pfile
, ifs
->lineno
, -1,
1640 "unterminated `#%s' conditional",
1641 dtable
[ifs
->type
].name
);
1646 pfile
->if_stack
= ifs
;
1648 if (pfile
->potential_control_macro
)
1650 CPP_BUFFER (pfile
)->ihash
->control_macro
1651 = pfile
->potential_control_macro
;
1652 pfile
->potential_control_macro
= 0;
1655 if (CPP_BUFFER (pfile
)->nominal_fname
&& next_buf
!= NULL
)
1657 /* We're about to return from an #include file.
1658 Emit #line information now (as part of the CPP_POP) result.
1659 But the #line refers to the file we will pop to. */
1660 cpp_buffer
*cur_buffer
= CPP_BUFFER (pfile
);
1661 CPP_BUFFER (pfile
) = next_buf
;
1662 pfile
->input_stack_listing_current
= 0;
1663 _cpp_output_line_command (pfile
, leave_file
);
1664 CPP_BUFFER (pfile
) = cur_buffer
;
1667 CPP_BUFFER (pfile
)->seen_eof
= 1;
1677 HASHNODE
*base
, *this;
1678 HASHNODE
**bslot
, **tslot
;
1680 unsigned long bhash
, thash
;
1682 old_written
= CPP_WRITTEN (pfile
); /* remember where it starts */
1683 ret
= _cpp_parse_assertion (pfile
);
1688 cpp_error (pfile
, "missing token-sequence in #assert");
1691 tlen
= CPP_WRITTEN (pfile
) - old_written
;
1693 if (_cpp_get_directive_token (pfile
) != CPP_VSPACE
)
1695 cpp_error (pfile
, "junk at end of #assert");
1699 sym
= pfile
->token_buffer
+ old_written
;
1700 blen
= (U_CHAR
*) strchr (sym
, '(') - sym
;
1701 tslot
= _cpp_lookup_slot (pfile
, sym
, tlen
, 1, &thash
);
1704 cpp_warning (pfile
, "%s re-asserted", sym
);
1708 bslot
= _cpp_lookup_slot (pfile
, sym
, blen
, 1, &bhash
);
1711 *bslot
= base
= _cpp_make_hashnode (sym
, blen
, T_ASSERT
, bhash
);
1712 base
->value
.aschain
= 0;
1717 if (base
->type
!= T_ASSERT
)
1719 /* Token clash - but with what?! */
1720 cpp_ice (pfile
, "base->type != T_ASSERT in do_assert");
1724 *tslot
= this = _cpp_make_hashnode (sym
, tlen
, T_ASSERT
, thash
);
1725 this->value
.aschain
= base
->value
.aschain
;
1726 base
->value
.aschain
= this;
1729 _cpp_skip_rest_of_line (pfile
);
1730 CPP_SET_WRITTEN (pfile
, old_written
);
1741 long baselen
, thislen
;
1742 HASHNODE
*base
, *this, *next
;
1744 old_written
= CPP_WRITTEN (pfile
);
1745 ret
= _cpp_parse_assertion (pfile
);
1748 thislen
= CPP_WRITTEN (pfile
) - old_written
;
1750 if (_cpp_get_directive_token (pfile
) != CPP_VSPACE
)
1752 cpp_error (pfile
, "junk at end of #unassert");
1755 sym
= pfile
->token_buffer
+ old_written
;
1756 CPP_SET_WRITTEN (pfile
, old_written
);
1760 base
= _cpp_lookup (pfile
, sym
, thislen
);
1762 goto error
; /* It isn't an error to #undef what isn't #defined,
1763 so it isn't an error to #unassert what isn't
1764 #asserted either. */
1766 for (this = base
->value
.aschain
; this; this = next
)
1768 next
= this->value
.aschain
;
1769 htab_remove_elt (pfile
->hashtab
, this);
1771 htab_remove_elt (pfile
->hashtab
, base
);
1775 baselen
= (U_CHAR
*) strchr (sym
, '(') - sym
;
1776 base
= _cpp_lookup (pfile
, sym
, baselen
);
1777 if (! base
) goto error
;
1778 this = _cpp_lookup (pfile
, sym
, thislen
);
1779 if (! this) goto error
;
1782 while (next
->value
.aschain
!= this)
1783 next
= next
->value
.aschain
;
1785 next
->value
.aschain
= this->value
.aschain
;
1786 htab_remove_elt (pfile
->hashtab
, this);
1788 if (base
->value
.aschain
== NULL
)
1789 /* Last answer for this predicate deleted. */
1790 htab_remove_elt (pfile
->hashtab
, base
);
1795 _cpp_skip_rest_of_line (pfile
);
1796 CPP_SET_WRITTEN (pfile
, old_written
);
1800 /* These are for -D, -U, -A. */
1802 /* Process the string STR as if it appeared as the body of a #define.
1803 If STR is just an identifier, define it with value 1.
1804 If STR has anything after the identifier, then it should
1805 be identifier=definition. */
1808 cpp_define (pfile
, str
)
1815 p
= strchr (str
, '=');
1816 /* Copy the entire option so we can modify it.
1817 Change the first "=" in the string to a space. If there is none,
1818 tack " 1" on the end. Then add a newline and a NUL. */
1822 count
= strlen (str
) + 2;
1823 buf
= alloca (count
);
1824 memcpy (buf
, str
, count
- 2);
1826 buf
[count
- 2] = '\n';
1827 buf
[count
- 1] = '\0';
1831 count
= strlen (str
) + 4;
1832 buf
= alloca (count
);
1833 memcpy (buf
, str
, count
- 4);
1834 strcpy (&buf
[count
-4], " 1\n");
1837 if (cpp_push_buffer (pfile
, buf
, count
- 1) != NULL
)
1840 cpp_pop_buffer (pfile
);
1844 /* Process MACRO as if it appeared as the body of an #undef. */
1846 cpp_undef (pfile
, macro
)
1850 /* Copy the string so we can append a newline. */
1851 size_t len
= strlen (macro
);
1852 char *buf
= alloca (len
+ 2);
1853 memcpy (buf
, macro
, len
);
1855 buf
[len
+ 1] = '\0';
1856 if (cpp_push_buffer (pfile
, buf
, len
+ 1))
1859 cpp_pop_buffer (pfile
);
1863 /* Process the string STR as if it appeared as the body of a #assert. */
1865 cpp_assert (pfile
, str
)
1869 if (cpp_push_buffer (pfile
, str
, strlen (str
)) != NULL
)
1872 cpp_pop_buffer (pfile
);
1876 /* Process STR as if it appeared as the body of an #unassert. */
1878 cpp_unassert (pfile
, str
)
1882 if (cpp_push_buffer (pfile
, str
, strlen (str
)) != NULL
)
1884 do_unassert (pfile
);
1885 cpp_pop_buffer (pfile
);
1889 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
1891 cpp_defined (pfile
, id
, len
)
1896 HASHNODE
*hp
= _cpp_lookup (pfile
, id
, len
);
1897 if (hp
&& hp
->type
== T_POISON
)
1899 cpp_error (pfile
, "attempt to use poisoned `%s'", hp
->name
);
1902 return (hp
!= NULL
);