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