]> gcc.gnu.org Git - gcc.git/blame - gcc/cpplib.c
Additional ENABLE_CHECKING code.
[gcc.git] / gcc / cpplib.c
CommitLineData
7f2935c7 1/* CPP Library.
5e7b4e25
JL
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4c8cc616 4 Contributed by Per Bothner, 1994-95.
d8bfa78c 5 Based on CCCP program by Paul Rubin, June 1986
7f2935c7
PB
6 Adapted to ANSI C, Richard Stallman, Jan 1987
7
8This program is free software; you can redistribute it and/or modify it
9under the terms of the GNU General Public License as published by the
10Free Software Foundation; either version 2, or (at your option) any
11later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
956d6950 20Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
7f2935c7 21
956d6950 22#include "config.h"
b04cd507 23#include "system.h"
7f2935c7 24
956d6950
JL
25#include "cpplib.h"
26#include "cpphash.h"
ab87f8c8 27#include "intl.h"
c71f835b 28#include "obstack.h"
07aa0b04 29#include "symcat.h"
7f2935c7 30
88ae23e7
ZW
31/* Stack of conditionals currently in progress
32 (including both successful and failing conditionals). */
33
34struct if_stack
35{
36 struct if_stack *next;
041c3194
ZW
37 unsigned int lineno; /* line number where condition started */
38 unsigned int colno; /* and column */
ea4a453b
ZW
39 int was_skipping; /* value of pfile->skipping before this if */
40 const cpp_hashnode *cmacro; /* macro name for #ifndef around entire file */
168d3732 41 int type; /* type of last directive seen in this group */
88ae23e7 42};
88ae23e7 43
1316f1f7
ZW
44/* Forward declarations. */
45
041c3194 46static void validate_else PARAMS ((cpp_reader *, const U_CHAR *));
c71f835b 47static int parse_include PARAMS ((cpp_reader *, const U_CHAR *, int,
041c3194
ZW
48 const U_CHAR **, unsigned int *,
49 int *));
50static void push_conditional PARAMS ((cpp_reader *, int, int,
51 const cpp_hashnode *));
52static void pass_thru_directive PARAMS ((cpp_reader *));
c71f835b
ZW
53static int read_line_number PARAMS ((cpp_reader *, int *));
54static int strtoul_for_line PARAMS ((const U_CHAR *, unsigned int,
041c3194 55 unsigned long *));
ff2b53ef 56
c71f835b
ZW
57static const cpp_hashnode *
58 parse_ifdef PARAMS ((cpp_reader *, const U_CHAR *));
59static const cpp_hashnode *
60 detect_if_not_defined PARAMS ((cpp_reader *));
61static cpp_hashnode *
62 get_define_node PARAMS ((cpp_reader *));
63static void dump_macro_name PARAMS ((cpp_reader *, cpp_hashnode *));
64static void unwind_if_stack PARAMS ((cpp_reader *, cpp_buffer *));
d481b69b 65
041c3194
ZW
66/* Utility. */
67#define str_match(sym, len, str) \
68((len) == (sizeof (str) - 1) && !ustrncmp ((sym), U(str), sizeof (str) - 1))
07aa0b04 69
168d3732
ZW
70/* This is the table of directive handlers. It is ordered by
71 frequency of occurrence; the numbers at the end are directive
72 counts from all the source code I have lying around (egcs and libc
73 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
74 pcmcia-cs-3.0.9).
75
76 The entries with a dash and a name after the count are extensions,
77 of which all but #warning and #include_next are deprecated. The name
78 is where the extension appears to have come from. */
79
07aa0b04
ZW
80/* #sccs is not always recognized. */
81#ifdef SCCS_DIRECTIVE
041c3194 82# define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0)
07aa0b04
ZW
83#else
84# define SCCS_ENTRY /* nothing */
85#endif
86
041c3194
ZW
87#define DIRECTIVE_TABLE \
88D(define, T_DEFINE = 0, KANDR, COMMENTS) /* 270554 */ \
89D(include, T_INCLUDE, KANDR, EXPAND | INCL) /* 52262 */ \
90D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
91D(ifdef, T_IFDEF, KANDR, COND) /* 22000 */ \
92D(if, T_IF, KANDR, COND | EXPAND) /* 18162 */ \
93D(else, T_ELSE, KANDR, COND) /* 9863 */ \
94D(ifndef, T_IFNDEF, KANDR, COND) /* 9675 */ \
95D(undef, T_UNDEF, KANDR, 0) /* 4837 */ \
96D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
97D(elif, T_ELIF, KANDR, COND | EXPAND) /* 610 */ \
98D(error, T_ERROR, STDC89, 0) /* 475 */ \
99D(pragma, T_PRAGMA, STDC89, 0) /* 195 */ \
100D(warning, T_WARNING, EXTENSION, 0) /* 22 GNU */ \
101D(include_next, T_INCLUDE_NEXT, EXTENSION, EXPAND | INCL) /* 19 GNU */ \
102D(ident, T_IDENT, EXTENSION, 0) /* 11 SVR4 */ \
103D(import, T_IMPORT, EXTENSION, EXPAND | INCL) /* 0 ObjC */ \
104D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
105D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
106SCCS_ENTRY /* 0 SVR2? */
168d3732
ZW
107
108/* Use the table to generate a series of prototypes, an enum for the
109 directive names, and an array of directive handlers. */
110
111/* The directive-processing functions are declared to return int
112 instead of void, because some old compilers have trouble with
113 pointers to functions returning void. */
114
7c32404c 115/* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
711b8824 116#define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *));
168d3732
ZW
117DIRECTIVE_TABLE
118#undef D
119
041c3194 120#define D(n, tag, o, f) tag,
168d3732
ZW
121enum
122{
123 DIRECTIVE_TABLE
124 N_DIRECTIVES
7f2935c7 125};
168d3732
ZW
126#undef D
127
7c32404c 128/* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
041c3194 129#define D(name, t, origin, flags) \
12cf91fe 130{ CONCAT2(do_,name), (const U_CHAR *) STRINGX(name), \
041c3194 131 sizeof STRINGX(name) - 1, origin, flags },
168d3732
ZW
132static const struct directive dtable[] =
133{
134DIRECTIVE_TABLE
135};
136#undef D
137#undef DIRECTIVE_TABLE
7f2935c7 138
c5a04734
ZW
139/* Check if a token's name matches that of a known directive. Put in
140 this file to save exporting dtable and other unneeded information. */
041c3194
ZW
141const struct directive *
142_cpp_check_directive (pfile, token, bol)
143 cpp_reader *pfile;
144 const cpp_token *token;
145 int bol;
c5a04734 146{
c5a04734
ZW
147 unsigned int i;
148
041c3194
ZW
149 /* If we are rescanning preprocessed input, don't obey any directives
150 other than # nnn. */
151 if (CPP_OPTION (pfile, preprocessed))
152 return 0;
c5a04734
ZW
153
154 for (i = 0; i < N_DIRECTIVES; i++)
bfb9dc7f 155 if (pfile->spec_nodes->dirs[i] == token->val.node)
c5a04734 156 {
041c3194
ZW
157 /* If we are skipping a failed conditional group, all non-conditional
158 directives are ignored. */
159 if (pfile->skipping && !(dtable[i].flags & COND))
160 return 0;
161
162 /* In -traditional mode, a directive is ignored unless its #
163 is in column 1. */
164 if (!bol && dtable[i].origin == KANDR && CPP_WTRADITIONAL (pfile))
165 cpp_warning (pfile, "traditional C ignores #%s with the # indented",
166 dtable[i].name);
041c3194
ZW
167
168 /* Issue -pedantic warnings for extended directives. */
169 if (CPP_PEDANTIC (pfile) && dtable[i].origin == EXTENSION)
170 cpp_pedwarn (pfile, "ISO C does not allow #%s", dtable[i].name);
171
172 /* -Wtraditional gives warnings about directives with inappropriate
173 indentation of #. */
174 if (bol && dtable[i].origin != KANDR && CPP_WTRADITIONAL (pfile))
175 cpp_warning (pfile,
176 "suggest hiding #%s from traditional C with an indented #",
177 dtable[i].name);
178
179 return &dtable[i];
c5a04734 180 }
c5a04734 181
041c3194
ZW
182 return 0;
183}
7f2935c7 184
041c3194
ZW
185const struct directive *
186_cpp_check_linemarker (pfile, token, bol)
7f2935c7 187 cpp_reader *pfile;
041c3194
ZW
188 const cpp_token *token ATTRIBUTE_UNUSED;
189 int bol;
3fdc651f 190{
40c79d58 191 /* # followed by a number is equivalent to #line. Do not recognize
ea4a453b
ZW
192 this form in assembly language source files or skipped
193 conditional groups. Complain about this form if we're being
194 pedantic, but not if this is regurgitated input (preprocessed or
195 fed back in by the C++ frontend). */
041c3194
ZW
196 if (pfile->skipping || CPP_OPTION (pfile, lang_asm))
197 return 0;
7f2935c7 198
041c3194
ZW
199 if (CPP_PEDANTIC (pfile) && CPP_BUFFER (pfile)->inc
200 && ! CPP_OPTION (pfile, preprocessed))
201 cpp_pedwarn (pfile, "# followed by integer");
202
203 /* In -traditional mode, a directive is ignored unless its #
204 is in column 1. */
205 if (!bol && CPP_WTRADITIONAL (pfile))
206 cpp_warning (pfile, "traditional C ignores #%s with the # indented",
207 dtable[T_LINE].name);
f9a0e96c 208
041c3194
ZW
209 return &dtable[T_LINE];
210}
40c79d58 211
041c3194
ZW
212static void
213dump_macro_name (pfile, node)
214 cpp_reader *pfile;
215 cpp_hashnode *node;
216{
217 CPP_PUTS (pfile, "#define ", sizeof "#define " - 1);
218 CPP_PUTS (pfile, node->name, node->length);
219}
ff2b53ef 220
041c3194
ZW
221/* Pass the current directive through to the output file. */
222static void
223pass_thru_directive (pfile)
224 cpp_reader *pfile;
225{
226 /* XXX This output may be genuinely needed even when there is no
227 printer. */
228 if (! pfile->printer)
229 return;
230 /* Flush first (temporary). */
231 cpp_output_tokens (pfile, pfile->printer, pfile->token_list.line);
232 _cpp_dump_list (pfile, &pfile->token_list, pfile->first_directive_token, 1);
233}
7f2935c7 234
041c3194
ZW
235static cpp_hashnode *
236get_define_node (pfile)
237 cpp_reader *pfile;
238{
239 cpp_hashnode *node;
240 const cpp_token *token;
ff2b53ef 241
041c3194 242 /* Skip any -C comments. */
417f3e3a 243 while ((token = _cpp_get_token (pfile))->type == CPP_COMMENT)
041c3194 244 ;
ea4a453b 245
041c3194 246 if (token->type != CPP_NAME)
7f2935c7 247 {
bfb9dc7f 248 cpp_error_with_line (pfile, token->line, token->col,
041c3194 249 "macro names must be identifiers");
ff2b53ef 250 return 0;
7f2935c7 251 }
5237f531 252
041c3194
ZW
253 /* That identifier is not allowed to be "defined". See predefined
254 macro names (6.10.8.4). */
bfb9dc7f
ZW
255 node = token->val.node;
256
257 if (node == pfile->spec_nodes->n_defined)
5237f531 258 {
041c3194
ZW
259 cpp_error_with_line (pfile, pfile->token_list.line, token->col,
260 "\"defined\" is not a legal macro name");
261 return 0;
5237f531 262 }
7f2935c7 263
041c3194
ZW
264 /* Check for poisoned identifiers now. */
265 if (node->type == T_POISON)
07aa0b04 266 {
bfb9dc7f 267 cpp_error (pfile, "attempt to use poisoned \"%s\"", node->name);
041c3194 268 return 0;
07aa0b04
ZW
269 }
270
041c3194 271 return node;
7f2935c7 272}
7f2935c7 273
168d3732 274/* Process a #define command. */
711b8824 275static void
168d3732 276do_define (pfile)
7f2935c7 277 cpp_reader *pfile;
7f2935c7 278{
f8f769ea 279 cpp_hashnode *node;
ff2b53ef 280
041c3194
ZW
281 if ((node = get_define_node (pfile)))
282 if (_cpp_create_definition (pfile, node))
283 {
284 if (CPP_OPTION (pfile, debug_output)
285 || CPP_OPTION (pfile, dump_macros) == dump_definitions)
286 _cpp_dump_definition (pfile, node);
287 else if (CPP_OPTION (pfile, dump_macros) == dump_names)
288 dump_macro_name (pfile, node);
289 }
041c3194
ZW
290}
291
292/* Remove the definition of a symbol from the symbol table. */
711b8824 293static void
041c3194
ZW
294do_undef (pfile)
295 cpp_reader *pfile;
296{
297 cpp_hashnode *node = get_define_node (pfile);
941e09b6 298
417f3e3a 299 if (_cpp_get_token (pfile)->type != CPP_EOF)
041c3194 300 cpp_pedwarn (pfile, "junk on line after #undef");
9e62c811 301
041c3194
ZW
302 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
303 is not currently defined as a macro name. */
304 if (node && node->type != T_VOID)
9e62c811 305 {
041c3194
ZW
306 /* If we are generating additional info for debugging (with -g) we
307 need to pass through all effective #undef commands. */
308 if (CPP_OPTION (pfile, debug_output)
309 || CPP_OPTION (pfile, dump_macros) == dump_definitions
310 || CPP_OPTION (pfile, dump_macros) == dump_names)
311 pass_thru_directive (pfile);
9e62c811 312
041c3194
ZW
313 if (node->type != T_MACRO)
314 cpp_warning (pfile, "undefining \"%s\"", node->name);
9e62c811 315
041c3194
ZW
316 _cpp_free_definition (node);
317 node->type = T_VOID;
9e62c811 318 }
7f2935c7
PB
319}
320
041c3194 321
45b966db 322/* Handle #include and #import. */
7f2935c7 323
041c3194
ZW
324static int
325parse_include (pfile, dir, trail, strp, lenp, abp)
7f2935c7 326 cpp_reader *pfile;
041c3194 327 const U_CHAR *dir;
f3f751ad 328 int trail;
041c3194
ZW
329 const U_CHAR **strp;
330 unsigned int *lenp;
331 int *abp;
7f2935c7 332{
417f3e3a 333 const cpp_token *name = _cpp_get_token (pfile);
7f2935c7 334
041c3194 335 if (name->type != CPP_STRING && name->type != CPP_HEADER_NAME)
7f2935c7 336 {
041c3194
ZW
337 if (name->type == CPP_LESS)
338 name = _cpp_glue_header_name (pfile);
339 else
340 {
341 cpp_error (pfile, "#%s expects \"FILENAME\" or <FILENAME>", dir);
342 return 1;
343 }
7f2935c7 344 }
bfb9dc7f 345 if (name->val.str.len == 0)
7f2935c7 346 {
041c3194
ZW
347 cpp_error (pfile, "empty file name in #%s", dir);
348 return 1;
7f2935c7 349 }
7f2935c7 350
417f3e3a 351 if (!trail && _cpp_get_token (pfile)->type != CPP_EOF)
041c3194 352 cpp_error (pfile, "junk at end of #%s", dir);
7f2935c7 353
bfb9dc7f
ZW
354 *lenp = name->val.str.len;
355 *strp = name->val.str.text;
041c3194
ZW
356 *abp = (name->type == CPP_HEADER_NAME);
357 return 0;
168d3732 358}
3caee4a8 359
711b8824 360static void
168d3732
ZW
361do_include (pfile)
362 cpp_reader *pfile;
363{
364 unsigned int len;
041c3194
ZW
365 const U_CHAR *str;
366 int ab;
3caee4a8 367
041c3194 368 if (parse_include (pfile, dtable[T_INCLUDE].name, 0, &str, &len, &ab))
711b8824 369 return;
7f2935c7 370
041c3194
ZW
371 _cpp_execute_include (pfile, str, len, 0, 0, ab);
372 if (CPP_OPTION (pfile, dump_includes))
373 pass_thru_directive (pfile);
168d3732 374}
e8037d57 375
711b8824 376static void
168d3732
ZW
377do_import (pfile)
378 cpp_reader *pfile;
379{
380 unsigned int len;
041c3194
ZW
381 const U_CHAR *str;
382 int ab;
168d3732 383
ae79697b 384 if (CPP_OPTION (pfile, warn_import)
c31a6508 385 && !CPP_IN_SYSTEM_HEADER (pfile) && !pfile->import_warning)
7f2935c7 386 {
168d3732
ZW
387 pfile->import_warning = 1;
388 cpp_warning (pfile,
389 "#import is obsolete, use an #ifndef wrapper in the header file");
7f2935c7 390 }
7f2935c7 391
041c3194 392 if (parse_include (pfile, dtable[T_IMPORT].name, 0, &str, &len, &ab))
711b8824 393 return;
7f2935c7 394
041c3194
ZW
395 _cpp_execute_include (pfile, str, len, 1, 0, ab);
396 if (CPP_OPTION (pfile, dump_includes))
397 pass_thru_directive (pfile);
168d3732 398}
7f2935c7 399
711b8824 400static void
168d3732
ZW
401do_include_next (pfile)
402 cpp_reader *pfile;
403{
404 unsigned int len;
041c3194 405 const U_CHAR *str;
168d3732 406 struct file_name_list *search_start = 0;
041c3194 407 int ab;
7f2935c7 408
041c3194 409 if (parse_include (pfile, dtable[T_INCLUDE_NEXT].name, 0, &str, &len, &ab))
711b8824 410 return;
168d3732 411
041c3194
ZW
412 /* For #include_next, skip in the search path past the dir in which
413 the current file was found. If this is the last directory in the
414 search path, don't include anything. If the current file was
415 specified with an absolute path, use the normal search logic. If
416 this is the primary source file, use the normal search logic and
417 generate a warning. */
168d3732 418 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
7f2935c7 419 {
c31a6508 420 if (CPP_BUFFER (pfile)->inc->foundhere)
041c3194
ZW
421 {
422 search_start = CPP_BUFFER (pfile)->inc->foundhere->next;
423 if (!search_start)
711b8824 424 return;
041c3194 425 }
7f2935c7 426 }
168d3732
ZW
427 else
428 cpp_warning (pfile, "#include_next in primary source file");
429
041c3194
ZW
430 _cpp_execute_include (pfile, str, len, 0, search_start, ab);
431 if (CPP_OPTION (pfile, dump_includes))
432 pass_thru_directive (pfile);
7f2935c7 433}
7f2935c7 434
d3a34a0a
JM
435/* Subroutine of do_line. Read next token from PFILE without adding it to
436 the output buffer. If it is a number between 1 and 4, store it in *NUM
437 and return 1; otherwise, return 0 and complain if we aren't at the end
438 of the directive. */
439
440static int
441read_line_number (pfile, num)
442 cpp_reader *pfile;
443 int *num;
444{
417f3e3a 445 const cpp_token *tok = _cpp_get_token (pfile);
041c3194 446 enum cpp_ttype type = tok->type;
bfb9dc7f
ZW
447 const U_CHAR *p = tok->val.str.text;
448 unsigned int len = tok->val.str.len;
d3a34a0a 449
041c3194 450 if (type == CPP_NUMBER && len == 1 && p[0] >= '1' && p[0] <= '4')
d3a34a0a
JM
451 {
452 *num = p[0] - '0';
453 return 1;
454 }
455 else
456 {
cf00a885 457 if (type != CPP_EOF)
ea4a453b 458 cpp_error (pfile, "invalid format #line");
d3a34a0a
JM
459 return 0;
460 }
461}
462
041c3194
ZW
463/* Another subroutine of do_line. Convert a number in STR, of length
464 LEN, to binary; store it in NUMP, and return 0 if the number was
465 legal, 1 if not. Temporary, hopefully. */
466static int
467strtoul_for_line (str, len, nump)
468 const U_CHAR *str;
469 unsigned int len;
470 unsigned long *nump;
471{
472 unsigned long reg = 0;
473 U_CHAR c;
474 while (len--)
475 {
476 c = *str++;
477 if (!ISDIGIT (c))
478 return 1;
479 reg *= 10;
480 reg += c - '0';
481 }
482 *nump = reg;
483 return 0;
484}
485
6de1e2a9
ZW
486/* Interpret #line command.
487 Note that the filename string (if any) is treated as if it were an
488 include filename. That means no escape handling. */
7f2935c7 489
711b8824 490static void
168d3732 491do_line (pfile)
7f2935c7 492 cpp_reader *pfile;
7f2935c7 493{
6de1e2a9 494 cpp_buffer *ip = CPP_BUFFER (pfile);
041c3194
ZW
495 unsigned long new_lineno, old_lineno;
496 /* C99 raised the minimum limit on #line numbers. */
497 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
498 int action_number = 0;
499 enum cpp_ttype type;
500 const U_CHAR *str;
501 char *fname;
502 unsigned int len;
503 const cpp_token *tok;
7f2935c7 504
417f3e3a 505 tok = _cpp_get_token (pfile);
041c3194 506 type = tok->type;
bfb9dc7f
ZW
507 str = tok->val.str.text;
508 len = tok->val.str.len;
cfb3ee16 509
041c3194 510 if (type != CPP_NUMBER || strtoul_for_line (str, len, &new_lineno))
7f2935c7 511 {
041c3194
ZW
512 cpp_error (pfile, "token after #line is not a positive integer");
513 goto done;
6de1e2a9 514 }
7f2935c7 515
041c3194
ZW
516 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
517 cpp_pedwarn (pfile, "line number out of range");
7f2935c7 518
041c3194
ZW
519 old_lineno = ip->lineno;
520 ip->lineno = new_lineno;
417f3e3a 521 tok = _cpp_get_token (pfile);
041c3194 522 type = tok->type;
bfb9dc7f
ZW
523 str = tok->val.str.text;
524 len = tok->val.str.len;
add7091b 525
cf00a885 526 if (type == CPP_EOF)
041c3194
ZW
527 goto done;
528 else if (type != CPP_STRING)
5538ada6 529 {
ea4a453b 530 cpp_error (pfile, "second token after #line is not a string");
041c3194
ZW
531 ip->lineno = old_lineno; /* malformed #line should have no effect */
532 goto done;
5538ada6
ZW
533 }
534
041c3194
ZW
535 fname = alloca (len + 1);
536 memcpy (fname, str, len);
537 fname[len] = '\0';
538
539 if (strcmp (fname, ip->nominal_fname))
540 {
541 if (!strcmp (fname, ip->inc->name))
542 ip->nominal_fname = ip->inc->name;
543 else
544 ip->nominal_fname = _cpp_fake_include (pfile, fname);
545 }
7f2935c7 546
041c3194 547 if (read_line_number (pfile, &action_number) == 0)
711b8824 548 return;
941e09b6 549
041c3194
ZW
550 if (CPP_PEDANTIC (pfile))
551 cpp_pedwarn (pfile, "garbage at end of #line");
ff2b53ef 552
041c3194
ZW
553 /* This is somewhat questionable: change the buffer stack
554 depth so that output_line_command thinks we've stacked
555 another buffer. */
556 if (action_number == 1)
ff2b53ef 557 {
041c3194
ZW
558 pfile->buffer_stack_depth++;
559 cpp_make_system_header (pfile, ip, 0);
560 read_line_number (pfile, &action_number);
ff2b53ef 561 }
041c3194 562 else if (action_number == 2)
7f2935c7 563 {
041c3194
ZW
564 pfile->buffer_stack_depth--;
565 cpp_make_system_header (pfile, ip, 0);
566 read_line_number (pfile, &action_number);
7f2935c7 567 }
041c3194
ZW
568 if (action_number == 3)
569 {
570 cpp_make_system_header (pfile, ip, 1);
571 read_line_number (pfile, &action_number);
572 }
573 if (action_number == 4)
574 {
575 cpp_make_system_header (pfile, ip, 2);
576 read_line_number (pfile, &action_number);
577 }
7f2935c7 578
041c3194 579 done:
711b8824 580 return;
7f2935c7 581}
941e09b6 582
7f2935c7
PB
583/*
584 * Report an error detected by the program we are processing.
585 * Use the text of the line in the error message.
586 * (We use error because it prints the filename & line#.)
587 */
588
711b8824 589static void
168d3732 590do_error (pfile)
7f2935c7 591 cpp_reader *pfile;
7f2935c7 592{
041c3194 593 U_CHAR *text, *limit;
941e09b6 594
041c3194
ZW
595 text = pfile->limit;
596 _cpp_dump_list (pfile, &pfile->token_list, pfile->first_directive_token, 0);
597 limit = pfile->limit;
598 pfile->limit = text;
599 cpp_error (pfile, "%.*s", (int)(limit - text), text);
7f2935c7
PB
600}
601
602/*
603 * Report a warning detected by the program we are processing.
604 * Use the text of the line in the warning message, then continue.
7f2935c7
PB
605 */
606
711b8824 607static void
168d3732 608do_warning (pfile)
7f2935c7 609 cpp_reader *pfile;
7f2935c7 610{
041c3194 611 U_CHAR *text, *limit;
a2a76ce7 612
041c3194
ZW
613 text = pfile->limit;
614 _cpp_dump_list (pfile, &pfile->token_list, pfile->first_directive_token, 0);
615 limit = pfile->limit;
616 pfile->limit = text;
617 cpp_warning (pfile, "%.*s", (int)(limit - text), text);
7f2935c7
PB
618}
619
a2a76ce7 620/* Report program identification. */
7f2935c7 621
711b8824 622static void
168d3732 623do_ident (pfile)
7f2935c7 624 cpp_reader *pfile;
7f2935c7 625{
a2a76ce7 626 /* Next token should be a string constant. */
417f3e3a 627 if (_cpp_get_token (pfile)->type == CPP_STRING)
a2a76ce7 628 /* And then a newline. */
417f3e3a 629 if (_cpp_get_token (pfile)->type == CPP_EOF)
041c3194
ZW
630 {
631 /* Good - ship it. */
632 pass_thru_directive (pfile);
711b8824 633 return;
041c3194 634 }
a2a76ce7
ZW
635
636 cpp_error (pfile, "invalid #ident");
7f2935c7
PB
637}
638
a2a76ce7
ZW
639/* Pragmata handling. We handle some of these, and pass the rest on
640 to the front end. C99 defines three pragmas and says that no macro
641 expansion is to be performed on them; whether or not macro
642 expansion happens for other pragmas is implementation defined.
643 This implementation never macro-expands the text after #pragma.
644
645 We currently do not support the _Pragma operator. Support for that
646 has to be coordinated with the front end. Proposed implementation:
647 both #pragma blah blah and _Pragma("blah blah") become
648 __builtin_pragma(blah blah) and we teach the parser about that. */
649
650/* Sub-handlers for the pragmas needing treatment here.
651 They return 1 if the token buffer is to be popped, 0 if not. */
82443371
NS
652struct pragma_entry
653{
041c3194 654 const char *name;
82443371
NS
655 int (*handler) PARAMS ((cpp_reader *));
656};
657
658static int pragma_dispatch
bfb9dc7f 659 PARAMS ((cpp_reader *, const struct pragma_entry *, const cpp_hashnode *));
a2a76ce7
ZW
660static int do_pragma_once PARAMS ((cpp_reader *));
661static int do_pragma_implementation PARAMS ((cpp_reader *));
662static int do_pragma_poison PARAMS ((cpp_reader *));
2c0b35cb 663static int do_pragma_system_header PARAMS ((cpp_reader *));
82443371 664static int do_pragma_gcc PARAMS ((cpp_reader *));
f3f751ad 665static int do_pragma_dependency PARAMS ((cpp_reader *));
82443371
NS
666
667static const struct pragma_entry top_pragmas[] =
668{
669 {"once", do_pragma_once},
670 {"implementation", do_pragma_implementation},
671 {"poison", do_pragma_poison},
82443371 672 {"GCC", do_pragma_gcc},
041c3194 673 {NULL, NULL}
82443371
NS
674};
675
676static const struct pragma_entry gcc_pragmas[] =
677{
678 {"implementation", do_pragma_implementation},
679 {"poison", do_pragma_poison},
680 {"system_header", do_pragma_system_header},
f3f751ad 681 {"dependency", do_pragma_dependency},
041c3194 682 {NULL, NULL}
82443371
NS
683};
684
bfb9dc7f 685static int pragma_dispatch (pfile, table, node)
82443371
NS
686 cpp_reader *pfile;
687 const struct pragma_entry *table;
bfb9dc7f 688 const cpp_hashnode *node;
82443371 689{
bfb9dc7f
ZW
690 const U_CHAR *p = node->name;
691 size_t len = node->length;
692
82443371
NS
693 for (; table->name; table++)
694 if (strlen (table->name) == len && !memcmp (p, table->name, len))
695 return (*table->handler) (pfile);
041c3194 696 return 0;
82443371 697}
7f2935c7 698
711b8824 699static void
168d3732 700do_pragma (pfile)
7f2935c7 701 cpp_reader *pfile;
7f2935c7 702{
041c3194 703 const cpp_token *tok;
a2a76ce7 704 int pop;
add7091b 705
417f3e3a 706 tok = _cpp_get_token (pfile);
041c3194 707 if (tok->type == CPP_EOF)
711b8824 708 return;
041c3194 709 else if (tok->type != CPP_NAME)
0172e2bc 710 {
041c3194 711 cpp_error (pfile, "malformed #pragma directive");
711b8824 712 return;
0172e2bc 713 }
a2a76ce7 714
bfb9dc7f 715 pop = pragma_dispatch (pfile, top_pragmas, tok->val.node);
041c3194
ZW
716 if (!pop)
717 pass_thru_directive (pfile);
a2a76ce7
ZW
718}
719
82443371
NS
720static int
721do_pragma_gcc (pfile)
722 cpp_reader *pfile;
723{
041c3194
ZW
724 const cpp_token *tok;
725
417f3e3a 726 tok = _cpp_get_token (pfile);
041c3194
ZW
727 if (tok->type == CPP_EOF)
728 return 1;
729 else if (tok->type != CPP_NAME)
730 return 0;
82443371 731
bfb9dc7f 732 return pragma_dispatch (pfile, gcc_pragmas, tok->val.node);
82443371
NS
733}
734
a2a76ce7
ZW
735static int
736do_pragma_once (pfile)
737 cpp_reader *pfile;
738{
739 cpp_buffer *ip = CPP_BUFFER (pfile);
740
a2a76ce7
ZW
741 /* Allow #pragma once in system headers, since that's not the user's
742 fault. */
c31a6508 743 if (!CPP_IN_SYSTEM_HEADER (pfile))
ea4a453b 744 cpp_warning (pfile, "#pragma once is obsolete");
a2a76ce7 745
38b24ee2 746 if (CPP_PREV_BUFFER (ip) == NULL)
ea4a453b 747 cpp_warning (pfile, "#pragma once outside include file");
a2a76ce7 748 else
c31a6508 749 ip->inc->cmacro = NEVER_REREAD;
fc009f96 750
a2a76ce7
ZW
751 return 1;
752}
fc009f96 753
a2a76ce7
ZW
754static int
755do_pragma_implementation (pfile)
756 cpp_reader *pfile;
757{
758 /* Be quiet about `#pragma implementation' for a file only if it hasn't
759 been included yet. */
417f3e3a 760 const cpp_token *tok = _cpp_get_token (pfile);
12cf91fe 761 char *copy;
fc009f96 762
041c3194 763 if (tok->type == CPP_EOF)
a2a76ce7 764 return 0;
041c3194 765 else if (tok->type != CPP_STRING
417f3e3a 766 || _cpp_get_token (pfile)->type != CPP_EOF)
a2a76ce7
ZW
767 {
768 cpp_error (pfile, "malformed #pragma implementation");
769 return 1;
770 }
fc009f96 771
041c3194 772 /* Make a NUL-terminated copy of the string. */
bfb9dc7f
ZW
773 copy = alloca (tok->val.str.len + 1);
774 memcpy (copy, tok->val.str.text, tok->val.str.len);
775 copy[tok->val.str.len] = '\0';
d35364d1 776
b0699dad 777 if (cpp_included (pfile, copy))
a2a76ce7 778 cpp_warning (pfile,
ea4a453b 779 "#pragma implementation for %s appears after file is included",
a2a76ce7 780 copy);
a2a76ce7
ZW
781 return 0;
782}
fc009f96 783
a2a76ce7
ZW
784static int
785do_pragma_poison (pfile)
786 cpp_reader *pfile;
787{
788 /* Poison these symbols so that all subsequent usage produces an
789 error message. */
041c3194 790 const cpp_token *tok;
f8f769ea 791 cpp_hashnode *hp;
a2a76ce7 792 int writeit;
d35364d1 793
a2a76ce7
ZW
794 /* As a rule, don't include #pragma poison commands in output,
795 unless the user asks for them. */
ae79697b
ZW
796 writeit = (CPP_OPTION (pfile, debug_output)
797 || CPP_OPTION (pfile, dump_macros) == dump_definitions
798 || CPP_OPTION (pfile, dump_macros) == dump_names);
a2a76ce7
ZW
799
800 for (;;)
801 {
417f3e3a 802 tok = _cpp_get_token (pfile);
041c3194 803 if (tok->type == CPP_EOF)
a2a76ce7 804 break;
041c3194 805 if (tok->type != CPP_NAME)
fc009f96 806 {
a2a76ce7 807 cpp_error (pfile, "invalid #pragma poison directive");
a2a76ce7 808 return 1;
fc009f96
GK
809 }
810
bfb9dc7f 811 hp = tok->val.node;
a7abcbbf
ZW
812 if (hp->type == T_POISON)
813 ; /* It is allowed to poison the same identifier twice. */
a2a76ce7 814 else
d35364d1 815 {
a7abcbbf 816 if (hp->type != T_VOID)
041c3194 817 cpp_warning (pfile, "poisoning existing macro \"%s\"", hp->name);
a7abcbbf
ZW
818 _cpp_free_definition (hp);
819 hp->type = T_POISON;
d35364d1 820 }
fc009f96 821 }
a2a76ce7 822 return !writeit;
7f2935c7 823}
2c0b35cb
ZW
824
825/* Mark the current header as a system header. This will suppress
826 some categories of warnings (notably those from -pedantic). It is
827 intended for use in system libraries that cannot be implemented in
828 conforming C, but cannot be certain that their headers appear in a
829 system include directory. To prevent abuse, it is rejected in the
830 primary source file. */
831static int
832do_pragma_system_header (pfile)
833 cpp_reader *pfile;
834{
041c3194 835 cpp_buffer *ip = CPP_BUFFER (pfile);
2c0b35cb
ZW
836 if (CPP_PREV_BUFFER (ip) == NULL)
837 cpp_warning (pfile, "#pragma system_header outside include file");
838 else
e605b040 839 cpp_make_system_header (pfile, ip, 1);
2c0b35cb
ZW
840
841 return 1;
842}
f3f751ad
NS
843
844/* Check the modified date of the current include file against a specified
845 file. Issue a diagnostic, if the specified file is newer. We use this to
846 determine if a fixed header should be refixed. */
847static int
848do_pragma_dependency (pfile)
849 cpp_reader *pfile;
850{
041c3194
ZW
851 const U_CHAR *name;
852 unsigned int len;
853 int ordering, ab;
854 char left, right;
855
856 if (parse_include (pfile, U"pragma dependency", 1, &name, &len, &ab))
857 return 1;
858
859 left = ab ? '<' : '"';
860 right = ab ? '>' : '"';
861
862 ordering = _cpp_compare_file_date (pfile, name, len, ab);
f3f751ad 863 if (ordering < 0)
041c3194 864 cpp_warning (pfile, "cannot find source %c%s%c", left, name, right);
f3f751ad
NS
865 else if (ordering > 0)
866 {
417f3e3a 867 const cpp_token *msg = _cpp_get_token (pfile);
f3f751ad 868
ecd36557
NS
869 cpp_warning (pfile, "current file is older than %c%.*s%c",
870 left, (int)len, name, right);
041c3194
ZW
871 if (msg->type != CPP_EOF)
872 {
873 U_CHAR *text, *limit;
874
875 text = pfile->limit;
876 _cpp_dump_list (pfile, &pfile->token_list, msg, 0);
877 limit = pfile->limit;
878 pfile->limit = text;
ecd36557
NS
879 while (*text == ' ')
880 /* There must be something non-whitespace after. */
881 text++;
041c3194
ZW
882 cpp_warning (pfile, "%.*s", (int)(limit - text), text);
883 }
f3f751ad 884 }
f3f751ad
NS
885 return 1;
886}
887
7f2935c7 888/* Just ignore #sccs, on systems where we define it at all. */
07aa0b04 889#ifdef SCCS_DIRECTIVE
711b8824 890static void
168d3732 891do_sccs (pfile)
041c3194 892 cpp_reader *pfile ATTRIBUTE_UNUSED;
7f2935c7 893{
7f2935c7 894}
07aa0b04 895#endif
1d0e51ba
JM
896
897/* We've found an `#if' directive. If the only thing before it in
898 this file is white space, and if it is of the form
899 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
900 for inclusion of this file. (See redundant_include_p in cppfiles.c
ea4a453b
ZW
901 for an explanation of controlling macros.) If so, return the
902 hash node for SYMBOL. Otherwise, return NULL. */
1d0e51ba 903
ea4a453b 904static const cpp_hashnode *
1d0e51ba
JM
905detect_if_not_defined (pfile)
906 cpp_reader *pfile;
907{
041c3194
ZW
908 const cpp_token *token;
909 cpp_hashnode *cmacro = 0;
910
911 /* We are guaranteed that tokens are consecutive and end in CPP_EOF. */
912 token = pfile->first_directive_token + 2;
913
914 if (token->type != CPP_NOT)
915 return 0;
9cc6e05f 916
041c3194
ZW
917 token++;
918 if (token->type != CPP_NAME
bfb9dc7f 919 || token->val.node != pfile->spec_nodes->n_defined)
041c3194
ZW
920 return 0;
921
922 token++;
923 if (token->type == CPP_OPEN_PAREN)
924 token++;
9cc6e05f 925
041c3194
ZW
926 if (token->type != CPP_NAME)
927 return 0;
9cc6e05f 928
bfb9dc7f 929 cmacro = token->val.node;
9cc6e05f 930
041c3194
ZW
931 if (token[-1].type == CPP_OPEN_PAREN)
932 {
933 token++;
934 if (token->type != CPP_CLOSE_PAREN)
935 return 0;
936 }
9cc6e05f 937
041c3194
ZW
938 token++;
939 if (token->type != CPP_EOF)
940 return 0;
1d0e51ba 941
ea4a453b 942 return cmacro;
7f2935c7
PB
943}
944
041c3194
ZW
945/* Parse an #ifdef or #ifndef directive. Returns the hash node of the
946 macro being tested, and issues various error messages. */
7f2935c7 947
ea4a453b 948static const cpp_hashnode *
168d3732 949parse_ifdef (pfile, name)
7f2935c7 950 cpp_reader *pfile;
12cf91fe 951 const U_CHAR *name;
7f2935c7 952{
041c3194 953 enum cpp_ttype type;
ea4a453b 954 const cpp_hashnode *node = 0;
7f2935c7 955
417f3e3a 956 const cpp_token *token = _cpp_get_token (pfile);
041c3194 957 type = token->type;
7f2935c7 958
f9a0e96c
ZW
959 if (type == CPP_EOF)
960 cpp_pedwarn (pfile, "#%s with no argument", name);
961 else if (type != CPP_NAME)
962 cpp_pedwarn (pfile, "#%s with invalid argument", name);
963 else if (_cpp_get_token (pfile)->type != CPP_EOF)
964 cpp_pedwarn (pfile, "garbage at end of #%s", name);
7f2935c7 965
041c3194 966 if (type == CPP_NAME)
bfb9dc7f 967 node = token->val.node;
041c3194 968 if (node && node->type == T_POISON)
bfb9dc7f
ZW
969 {
970 cpp_error (pfile, "attempt to use poisoned identifier \"%s\"",
971 node->name);
972 node = 0;
973 }
f9a0e96c 974
ea4a453b 975 return node;
168d3732 976}
7f2935c7 977
168d3732
ZW
978/* #ifdef is dead simple. */
979
711b8824 980static void
168d3732
ZW
981do_ifdef (pfile)
982 cpp_reader *pfile;
983{
041c3194
ZW
984 const cpp_hashnode *node = 0;
985
986 if (! pfile->skipping)
bfb9dc7f 987 node = parse_ifdef (pfile, dtable[T_IFDEF].name);
041c3194 988
bfb9dc7f 989 push_conditional (pfile, !(node && node->type != T_VOID), T_IFDEF, 0);
168d3732
ZW
990}
991
992/* #ifndef is a tad more complex, because we need to check for a
993 no-reinclusion wrapper. */
994
711b8824 995static void
168d3732
ZW
996do_ifndef (pfile)
997 cpp_reader *pfile;
998{
041c3194 999 int start_of_file = 0;
bfb9dc7f 1000 const cpp_hashnode *node = 0;
168d3732 1001
041c3194 1002 if (! pfile->skipping)
5af7e2c2 1003 {
041c3194 1004 start_of_file = (pfile->token_list.flags & BEG_OF_FILE);
bfb9dc7f 1005 node = parse_ifdef (pfile, dtable[T_IFNDEF].name);
5af7e2c2 1006 }
041c3194 1007
bfb9dc7f
ZW
1008 push_conditional (pfile, node && node->type != T_VOID,
1009 T_IFNDEF, start_of_file ? node : 0);
7f2935c7
PB
1010}
1011
ea4a453b
ZW
1012/* #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1013 Also, check for a reinclude preventer of the form #if !defined (MACRO). */
7f2935c7 1014
711b8824 1015static void
ea4a453b 1016do_if (pfile)
7f2935c7 1017 cpp_reader *pfile;
7f2935c7 1018{
ea4a453b
ZW
1019 const cpp_hashnode *cmacro = 0;
1020 int value = 0;
7f2935c7 1021
ea4a453b
ZW
1022 if (! pfile->skipping)
1023 {
bfb9dc7f
ZW
1024 if (pfile->token_list.flags & BEG_OF_FILE)
1025 cmacro = detect_if_not_defined (pfile);
ea4a453b 1026 value = _cpp_parse_expr (pfile);
ea4a453b
ZW
1027 }
1028 push_conditional (pfile, value == 0, T_IF, cmacro);
7f2935c7
PB
1029}
1030
ea4a453b
ZW
1031/* #else flips pfile->skipping and continues without changing
1032 if_stack; this is so that the error message for missing #endif's
1033 etc. will point to the original #if. */
ed705a82 1034
711b8824 1035static void
ea4a453b
ZW
1036do_else (pfile)
1037 cpp_reader *pfile;
ed705a82 1038{
ea4a453b 1039 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
ea4a453b 1040 validate_else (pfile, dtable[T_ELSE].name);
ff2b53ef 1041
ea4a453b 1042 if (ifs == NULL)
ff2b53ef 1043 {
ea4a453b 1044 cpp_error (pfile, "#else without #if");
711b8824 1045 return;
ff2b53ef 1046 }
ea4a453b 1047 if (ifs->type == T_ELSE)
ff2b53ef 1048 {
ea4a453b 1049 cpp_error (pfile, "#else after #else");
041c3194
ZW
1050 cpp_error_with_line (pfile, ifs->lineno, ifs->colno,
1051 "the conditional began here");
ff2b53ef 1052 }
7f2935c7 1053
ea4a453b
ZW
1054 /* #ifndef can't have its special treatment for containing the whole file
1055 if it has a #else clause. */
1056 ifs->cmacro = 0;
ea4a453b
ZW
1057 ifs->type = T_ELSE;
1058 if (! ifs->was_skipping)
ed705a82 1059 {
ea4a453b
ZW
1060 /* If pfile->skipping is 2, one of the blocks in an #if/#elif/... chain
1061 succeeded, so we mustn't do the else block. */
1062 if (pfile->skipping < 2)
1063 pfile->skipping = ! pfile->skipping;
ff2b53ef 1064 }
7f2935c7
PB
1065}
1066
1067/*
ea4a453b
ZW
1068 * handle a #elif directive by not changing if_stack either.
1069 * see the comment above do_else.
7f2935c7
PB
1070 */
1071
711b8824 1072static void
ea4a453b 1073do_elif (pfile)
7f2935c7 1074 cpp_reader *pfile;
7f2935c7 1075{
ea4a453b 1076 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
7f2935c7 1077
ea4a453b 1078 if (ifs == NULL)
40ea76de 1079 {
ea4a453b 1080 cpp_error (pfile, "#elif without #if");
711b8824 1081 return;
40ea76de 1082 }
ea4a453b 1083 if (ifs->type == T_ELSE)
40ea76de 1084 {
ea4a453b 1085 cpp_error (pfile, "#elif after #else");
041c3194
ZW
1086 cpp_error_with_line (pfile, ifs->lineno, ifs->colno,
1087 "the conditional began here");
ea4a453b 1088 }
40ea76de 1089
ea4a453b
ZW
1090 ifs->type = T_ELIF;
1091 if (ifs->was_skipping)
711b8824 1092 return; /* Don't evaluate a nested #if */
041c3194
ZW
1093
1094 if (pfile->skipping != 1)
ea4a453b 1095 {
ea4a453b 1096 pfile->skipping = 2; /* one block succeeded, so don't do any others */
711b8824 1097 return;
40ea76de 1098 }
7f2935c7 1099
041c3194 1100 pfile->skipping = ! _cpp_parse_expr (pfile);
7f2935c7
PB
1101}
1102
ea4a453b 1103/* #endif pops the if stack and resets pfile->skipping. */
7f2935c7 1104
711b8824 1105static void
168d3732 1106do_endif (pfile)
7f2935c7 1107 cpp_reader *pfile;
7f2935c7 1108{
ea4a453b
ZW
1109 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1110
168d3732 1111 validate_else (pfile, dtable[T_ENDIF].name);
7f2935c7 1112
ea4a453b
ZW
1113 if (ifs == NULL)
1114 cpp_error (pfile, "#endif without #if");
7f2935c7
PB
1115 else
1116 {
ea4a453b
ZW
1117 CPP_BUFFER (pfile)->if_stack = ifs->next;
1118 pfile->skipping = ifs->was_skipping;
1119 pfile->potential_control_macro = ifs->cmacro;
c71f835b 1120 obstack_free (pfile->buffer_ob, ifs);
7f2935c7 1121 }
7f2935c7
PB
1122}
1123
041c3194 1124
ea4a453b
ZW
1125/* Push an if_stack entry and set pfile->skipping accordingly.
1126 If this is a #ifndef starting at the beginning of a file,
1127 CMACRO is the macro name tested by the #ifndef. */
1128
1129static void
1130push_conditional (pfile, skip, type, cmacro)
1131 cpp_reader *pfile;
1132 int skip;
1133 int type;
1134 const cpp_hashnode *cmacro;
1135{
1136 struct if_stack *ifs;
1137
c71f835b 1138 ifs = xobnew (pfile->buffer_ob, struct if_stack);
041c3194 1139 ifs->lineno = _cpp_get_line (pfile, &ifs->colno);
ea4a453b
ZW
1140 ifs->next = CPP_BUFFER (pfile)->if_stack;
1141 ifs->cmacro = cmacro;
1142 ifs->was_skipping = pfile->skipping;
1143 ifs->type = type;
1144
1145 if (!pfile->skipping)
1146 pfile->skipping = skip;
1147
1148 CPP_BUFFER (pfile)->if_stack = ifs;
1149}
1150
75ec21db 1151/* Issue -pedantic warning for text which is not a comment following
ea4a453b 1152 an #else or #endif. */
7f2935c7
PB
1153
1154static void
1155validate_else (pfile, directive)
1156 cpp_reader *pfile;
12cf91fe 1157 const U_CHAR *directive;
7f2935c7 1158{
417f3e3a 1159 if (CPP_PEDANTIC (pfile) && _cpp_get_token (pfile)->type != CPP_EOF)
041c3194 1160 cpp_pedwarn (pfile, "ISO C forbids text after #%s", directive);
6ee2c979
ZW
1161}
1162
041c3194 1163/* Called when we reach the end of a file. Walk back up the
c56c2073 1164 conditional stack till we reach its level at entry to this file,
ea4a453b 1165 issuing error messages. Then force skipping off. */
c71f835b
ZW
1166static void
1167unwind_if_stack (pfile, pbuf)
ba412f14 1168 cpp_reader *pfile;
c56c2073 1169 cpp_buffer *pbuf;
ba412f14 1170{
45b966db 1171 struct if_stack *ifs, *nifs;
ba412f14 1172
ea4a453b 1173 for (ifs = pbuf->if_stack; ifs; ifs = nifs)
782331f4 1174 {
041c3194 1175 cpp_error_with_line (pfile, ifs->lineno, ifs->colno, "unterminated #%s",
168d3732 1176 dtable[ifs->type].name);
45b966db 1177 nifs = ifs->next;
c71f835b 1178 /* No need to free - they'll all go away with the buffer. */
7061aa5a 1179 }
ea4a453b 1180 pfile->skipping = 0;
782331f4 1181}
7061aa5a 1182
041c3194
ZW
1183/* Parses an assertion, returning a pointer to the hash node of the
1184 predicate, or 0 on error. If an answer was supplied, it is
1185 allocated and placed in ANSWERP, otherwise it is set to 0. We use
1186 _cpp_get_raw_token, since we cannot assume tokens are consecutive
1187 in a #if statement (we may be in a macro), and we don't want to
1188 macro expand. */
1189cpp_hashnode *
1190_cpp_parse_assertion (pfile, answerp)
7f2935c7 1191 cpp_reader *pfile;
041c3194 1192 struct answer **answerp;
7f2935c7 1193{
041c3194
ZW
1194 struct answer *answer = 0;
1195 cpp_toklist *list;
e23c0ba3 1196 U_CHAR *sym;
041c3194
ZW
1197 const cpp_token *token, *predicate;
1198 const struct directive *d = pfile->token_list.directive;
1199 unsigned int len = 0;
7f2935c7 1200
041c3194
ZW
1201 predicate = _cpp_get_raw_token (pfile);
1202 if (predicate->type == CPP_EOF)
1203 {
1204 cpp_error (pfile, "assertion without predicate");
1205 return 0;
1206 }
1207 else if (predicate->type != CPP_NAME)
1208 {
1209 cpp_error (pfile, "predicate must be an identifier");
1210 return 0;
1211 }
15dad1d9 1212
041c3194
ZW
1213 token = _cpp_get_raw_token (pfile);
1214 if (token->type != CPP_OPEN_PAREN)
1215 {
1216 /* #unassert and #if are OK without predicate. */
1217 if (d == &dtable[T_UNASSERT])
1218 {
1219 if (token->type == CPP_EOF)
1220 goto lookup_node;
1221 }
1222 else if (d != &dtable[T_ASSERT])
1223 {
1224 _cpp_push_token (pfile, token);
1225 goto lookup_node;
1226 }
1227 cpp_error (pfile, "missing '(' after predicate");
1228 return 0;
1229 }
7061aa5a 1230
041c3194
ZW
1231 /* Allocate a struct answer, and copy the answer to it. */
1232 answer = (struct answer *) xmalloc (sizeof (struct answer));
1233 list = &answer->list;
1234 _cpp_init_toklist (list, NO_DUMMY_TOKEN);
15dad1d9 1235
041c3194
ZW
1236 for (;;)
1237 {
1238 cpp_token *dest;
15dad1d9 1239
041c3194 1240 token = _cpp_get_raw_token (pfile);
15dad1d9 1241
041c3194
ZW
1242 if (token->type == CPP_EOF)
1243 {
1244 cpp_error (pfile, "missing ')' to complete answer");
1245 goto error;
1246 }
1247 if (token->type == CPP_CLOSE_PAREN)
1248 break;
a7abcbbf 1249
041c3194
ZW
1250 /* Copy the token. */
1251 _cpp_expand_token_space (list, 1);
1252 dest = &list->tokens[list->tokens_used++];
1253 *dest = *token;
15dad1d9 1254
96be6998 1255 if (TOKEN_SPELL (token) == SPELL_STRING)
041c3194 1256 {
bfb9dc7f
ZW
1257 _cpp_expand_name_space (list, token->val.str.len);
1258 dest->val.str.text = list->namebuf + list->name_used;
041c3194 1259 memcpy (list->namebuf + list->name_used,
bfb9dc7f
ZW
1260 token->val.str.text, token->val.str.len);
1261 list->name_used += token->val.str.len;
041c3194
ZW
1262 }
1263 }
15dad1d9 1264
041c3194 1265 if (list->tokens_used == 0)
7061aa5a 1266 {
041c3194
ZW
1267 cpp_error (pfile, "predicate's answer is empty");
1268 goto error;
7f2935c7 1269 }
041c3194
ZW
1270
1271 /* Drop whitespace at start. */
1272 list->tokens[0].flags &= ~PREV_WHITE;
1273
1274 if ((d == &dtable[T_ASSERT] || d == &dtable[T_UNASSERT])
1275 && token[1].type != CPP_EOF)
bfab56e7 1276 {
041c3194
ZW
1277 cpp_error (pfile, "junk at end of assertion");
1278 goto error;
bfab56e7 1279 }
041c3194
ZW
1280
1281 lookup_node:
1282 *answerp = answer;
bfb9dc7f 1283 len = predicate->val.node->length;
385d061b 1284 sym = alloca (len + 1);
041c3194
ZW
1285
1286 /* Prefix '#' to get it out of macro namespace. */
1287 sym[0] = '#';
bfb9dc7f 1288 memcpy (sym + 1, predicate->val.node->name, len);
09074fda 1289 return cpp_lookup (pfile, sym, len + 1);
7061aa5a 1290
7f2935c7 1291 error:
041c3194 1292 FREE_ANSWER (answer);
3caee4a8 1293 return 0;
7f2935c7 1294}
7061aa5a 1295
041c3194
ZW
1296/* Returns a pointer to the pointer to the answer in the answer chain,
1297 or a pointer to NULL if the answer is not in the chain. */
1298struct answer **
c71f835b 1299_cpp_find_answer (node, candidate)
041c3194
ZW
1300 cpp_hashnode *node;
1301 const cpp_toklist *candidate;
7f2935c7 1302{
041c3194 1303 struct answer **result;
7f2935c7 1304
041c3194
ZW
1305 for (result = &node->value.answers; *result; result = &(*result)->next)
1306 if (_cpp_equiv_toklists (&(*result)->list, candidate))
1307 break;
ff2b53ef 1308
041c3194
ZW
1309 return result;
1310}
15dad1d9 1311
041c3194
ZW
1312#define WARNING(msgid) do { cpp_warning(pfile, msgid); goto error; } while (0)
1313#define ERROR(msgid) do { cpp_error(pfile, msgid); goto error; } while (0)
1314#define ICE(msgid) do { cpp_ice(pfile, msgid); goto error; } while (0)
711b8824 1315static void
041c3194
ZW
1316do_assert (pfile)
1317 cpp_reader *pfile;
1318{
1319 struct answer *new_answer;
1320 cpp_hashnode *node;
1321
1322 node = _cpp_parse_assertion (pfile, &new_answer);
1323 if (node)
ff2b53ef 1324 {
041c3194
ZW
1325 new_answer->next = 0;
1326 new_answer->list.line = pfile->token_list.line;
1327 new_answer->list.file = pfile->token_list.file;
15dad1d9 1328
041c3194
ZW
1329 if (node->type == T_ASSERTION)
1330 {
c71f835b 1331 if (*_cpp_find_answer (node, &new_answer->list))
041c3194
ZW
1332 goto err;
1333 new_answer->next = node->value.answers;
1334 }
1335 node->type = T_ASSERTION;
1336 node->value.answers = new_answer;
ff2b53ef 1337 }
711b8824 1338 return;
7f2935c7 1339
041c3194
ZW
1340 err:
1341 cpp_warning (pfile, "\"%.*s\" re-asserted",
1342 node->length - 1, node->name + 1);
1343 FREE_ANSWER (new_answer);
041c3194 1344}
15dad1d9 1345
711b8824 1346static void
041c3194
ZW
1347do_unassert (pfile)
1348 cpp_reader *pfile;
1349{
1350 cpp_hashnode *node;
1351 struct answer *answer, *temp, *next;
1352
1353 node = _cpp_parse_assertion (pfile, &answer);
1354 if (node)
7061aa5a 1355 {
041c3194
ZW
1356 /* It isn't an error to #unassert something that isn't asserted. */
1357 if (node->type == T_ASSERTION)
15dad1d9 1358 {
041c3194
ZW
1359 if (answer)
1360 {
c71f835b 1361 struct answer **p = _cpp_find_answer (node, &answer->list);
041c3194
ZW
1362
1363 temp = *p;
1364 if (temp)
1365 {
1366 *p = temp->next;
1367 FREE_ANSWER (temp);
1368 }
1369 if (node->value.answers == 0)
1370 node->type = T_VOID;
1371 }
1372 else
1373 {
1374 for (temp = node->value.answers; temp; temp = next)
1375 {
1376 next = temp->next;
1377 FREE_ANSWER (temp);
1378 }
1379 node->type = T_VOID;
1380 }
15dad1d9 1381 }
a7abcbbf 1382
041c3194
ZW
1383 if (answer)
1384 FREE_ANSWER (answer);
1385 }
7f2935c7 1386}
7f2935c7 1387
45b966db
ZW
1388/* These are for -D, -U, -A. */
1389
1390/* Process the string STR as if it appeared as the body of a #define.
1391 If STR is just an identifier, define it with value 1.
1392 If STR has anything after the identifier, then it should
1393 be identifier=definition. */
1394
0b22d65c 1395void
45b966db 1396cpp_define (pfile, str)
0b22d65c 1397 cpp_reader *pfile;
7ceb3598 1398 const char *str;
0b22d65c 1399{
45b966db
ZW
1400 char *buf, *p;
1401 size_t count;
1402
1403 p = strchr (str, '=');
1404 /* Copy the entire option so we can modify it.
1405 Change the first "=" in the string to a space. If there is none,
1406 tack " 1" on the end. Then add a newline and a NUL. */
1407
1408 if (p)
0b22d65c 1409 {
45b966db 1410 count = strlen (str) + 2;
5f6d3823 1411 buf = (char *) alloca (count);
45b966db
ZW
1412 memcpy (buf, str, count - 2);
1413 buf[p - str] = ' ';
1414 buf[count - 2] = '\n';
1415 buf[count - 1] = '\0';
1416 }
1417 else
1418 {
1419 count = strlen (str) + 4;
5f6d3823 1420 buf = (char *) alloca (count);
45b966db
ZW
1421 memcpy (buf, str, count - 4);
1422 strcpy (&buf[count-4], " 1\n");
0b22d65c 1423 }
cf4ed945 1424
041c3194 1425 _cpp_run_directive (pfile, &dtable[T_DEFINE], buf, count - 1);
45b966db 1426}
0f41302f 1427
45b966db
ZW
1428/* Process MACRO as if it appeared as the body of an #undef. */
1429void
1430cpp_undef (pfile, macro)
7f2935c7 1431 cpp_reader *pfile;
45b966db 1432 const char *macro;
7f2935c7 1433{
041c3194 1434 _cpp_run_directive (pfile, &dtable[T_UNDEF], macro, strlen (macro));
7f2935c7
PB
1435}
1436
45b966db
ZW
1437/* Process the string STR as if it appeared as the body of a #assert. */
1438void
1439cpp_assert (pfile, str)
1440 cpp_reader *pfile;
1441 const char *str;
1442{
041c3194 1443 _cpp_run_directive (pfile, &dtable[T_ASSERT], str, strlen (str));
45b966db 1444}
7f2935c7 1445
45b966db
ZW
1446/* Process STR as if it appeared as the body of an #unassert. */
1447void
1448cpp_unassert (pfile, str)
7f2935c7 1449 cpp_reader *pfile;
45b966db 1450 const char *str;
7f2935c7 1451{
041c3194 1452 _cpp_run_directive (pfile, &dtable[T_UNASSERT], str, strlen (str));
45b966db 1453}
3fdc651f 1454
45b966db
ZW
1455/* Determine whether the identifier ID, of length LEN, is a defined macro. */
1456int
1457cpp_defined (pfile, id, len)
1458 cpp_reader *pfile;
1459 const U_CHAR *id;
1460 int len;
1461{
f8f769ea 1462 cpp_hashnode *hp = cpp_lookup (pfile, id, len);
a7abcbbf 1463 if (hp->type == T_POISON)
45b966db 1464 {
041c3194 1465 cpp_error (pfile, "attempt to use poisoned \"%s\"", hp->name);
45b966db
ZW
1466 return 0;
1467 }
a7abcbbf 1468 return (hp->type != T_VOID);
7f2935c7 1469}
c71f835b
ZW
1470
1471/* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1472 If BUFFER != NULL, then use the LENGTH characters in BUFFER
1473 as the new input buffer.
1474 Return the new buffer, or NULL on failure. */
1475
1476cpp_buffer *
1477cpp_push_buffer (pfile, buffer, length)
1478 cpp_reader *pfile;
1479 const U_CHAR *buffer;
1480 long length;
1481{
1482 cpp_buffer *buf = CPP_BUFFER (pfile);
1483 cpp_buffer *new;
1484 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
1485 {
417f3e3a 1486 cpp_fatal (pfile, "#include nested too deep");
c71f835b
ZW
1487 return NULL;
1488 }
417f3e3a
ZW
1489 if (pfile->cur_context > 0)
1490 {
1491 cpp_ice (pfile, "buffer pushed with contexts stacked");
1492 _cpp_skip_rest_of_line (pfile);
1493 }
c71f835b
ZW
1494
1495 new = xobnew (pfile->buffer_ob, cpp_buffer);
1496 memset (new, 0, sizeof (cpp_buffer));
1497
f9a0e96c 1498 new->line_base = new->buf = new->cur = buffer;
c71f835b
ZW
1499 new->rlimit = buffer + length;
1500 new->prev = buf;
1501
1502 CPP_BUFFER (pfile) = new;
1503 return new;
1504}
1505
1506cpp_buffer *
1507cpp_pop_buffer (pfile)
1508 cpp_reader *pfile;
1509{
1510 cpp_buffer *buf = CPP_BUFFER (pfile);
1511
1512 unwind_if_stack (pfile, buf);
c71f835b 1513 if (buf->inc)
f9a0e96c 1514 _cpp_pop_file_buffer (pfile, buf);
c71f835b
ZW
1515
1516 CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
1517 obstack_free (pfile->buffer_ob, buf);
1518 pfile->buffer_stack_depth--;
1519 return CPP_BUFFER (pfile);
1520}
1521
1522#define obstack_chunk_alloc xmalloc
1523#define obstack_chunk_free free
bfb9dc7f 1524#define DSC(x) U x, sizeof x - 1
c71f835b
ZW
1525void
1526_cpp_init_stacks (pfile)
1527 cpp_reader *pfile;
1528{
bfb9dc7f
ZW
1529 int i;
1530 struct spec_nodes *s;
1531
c71f835b
ZW
1532 pfile->buffer_ob = xnew (struct obstack);
1533 obstack_init (pfile->buffer_ob);
bfb9dc7f
ZW
1534
1535 /* Perhaps not the ideal place to put this. */
1536 pfile->spec_nodes = s = xnew (struct spec_nodes);
1537 s->n_L = cpp_lookup (pfile, DSC("L"));
1538 s->n_defined = cpp_lookup (pfile, DSC("defined"));
1539 s->n__STRICT_ANSI__ = cpp_lookup (pfile, DSC("__STRICT_ANSI__"));
1540 s->n__CHAR_UNSIGNED__ = cpp_lookup (pfile, DSC("__CHAR_UNSIGNED__"));
1541 s->n__VA_ARGS__ = cpp_lookup (pfile, DSC("__VA_ARGS__"));
1542 for (i = 0; i < N_DIRECTIVES; i++)
1543 s->dirs[i] = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
c71f835b
ZW
1544}
1545
1546void
1547_cpp_cleanup_stacks (pfile)
1548 cpp_reader *pfile;
1549{
1550 obstack_free (pfile->buffer_ob, 0);
1551 free (pfile->buffer_ob);
1552}
This page took 1.104519 seconds and 5 git commands to generate.