]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cpplib.c
* Forgot to update ChangeLog.
[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
79bd622b 31/* Chained list of answers to an assertion. */
32struct answer
33{
34 struct answer *next;
35 unsigned int count;
36 cpp_token first[1];
37};
38
1e8b9746 39/* Stack of conditionals currently in progress
40 (including both successful and failing conditionals). */
41
42struct if_stack
43{
44 struct if_stack *next;
79bd622b 45 cpp_lexer_pos pos; /* line and column where condition started */
46 const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
c4357c92 47 int was_skipping; /* value of pfile->skipping before this if */
4bf559f2 48 int type; /* type of last directive seen in this group */
1e8b9746 49};
1e8b9746 50
79bd622b 51/* Values for the origin field of struct directive. KANDR directives
52 come from traditional (K&R) C. STDC89 directives come from the
53 1989 C standard. EXTENSION directives are extensions. */
54#define KANDR 0
55#define STDC89 1
56#define EXTENSION 2
57
58/* Values for the flags field of struct directive. COND indicates a
59 conditional; IF_COND an opening conditional. INCL means to treat
60 "..." and <...> as q-char and h-char sequences respectively. IN_I
61 means this directive should be handled even if -fpreprocessed is in
62 effect (these are the directives with callback hooks). */
63#define COND (1 << 0)
64#define IF_COND (1 << 1)
65#define INCL (1 << 2)
66#define IN_I (1 << 3)
67
68/* Defines one #-directive, including how to handle it. */
69typedef void (*directive_handler) PARAMS ((cpp_reader *));
70typedef struct directive directive;
71struct directive
72{
73 directive_handler handler; /* Function to handle directive. */
74 const U_CHAR *name; /* Name of directive. */
75 unsigned short length; /* Length of name. */
76 unsigned char origin; /* Origin of directive. */
77 unsigned char flags; /* Flags describing this directive. */
78};
79
08c74ec4 80/* Forward declarations. */
81
79bd622b 82static void skip_rest_of_line PARAMS ((cpp_reader *));
83static void check_eol PARAMS ((cpp_reader *));
84static void run_directive PARAMS ((cpp_reader *, int,
63588b30 85 const char *, size_t,
79bd622b 86 const char *));
87static int glue_header_name PARAMS ((cpp_reader *, cpp_token *));
88static int parse_include PARAMS ((cpp_reader *, cpp_token *));
f80e83a9 89static void push_conditional PARAMS ((cpp_reader *, int, int,
90 const cpp_hashnode *));
f51c2148 91static int read_line_number PARAMS ((cpp_reader *, int *));
92static int strtoul_for_line PARAMS ((const U_CHAR *, unsigned int,
f80e83a9 93 unsigned long *));
f3b83e9b 94static void do_diagnostic PARAMS ((cpp_reader *, enum error_type));
f51c2148 95static cpp_hashnode *
79bd622b 96 lex_macro_node PARAMS ((cpp_reader *));
f51c2148 97static void unwind_if_stack PARAMS ((cpp_reader *, cpp_buffer *));
79bd622b 98static void do_pragma_once PARAMS ((cpp_reader *));
99static void do_pragma_poison PARAMS ((cpp_reader *));
100static void do_pragma_system_header PARAMS ((cpp_reader *));
101static void do_pragma_dependency PARAMS ((cpp_reader *));
396ffa86 102static int get__Pragma_string PARAMS ((cpp_reader *, cpp_token *));
103static unsigned char *destringize PARAMS ((const cpp_string *,
104 unsigned int *));
79bd622b 105static int parse_answer PARAMS ((cpp_reader *, struct answer **, int));
106static cpp_hashnode *parse_assertion PARAMS ((cpp_reader *, struct answer **,
107 int));
108static struct answer ** find_answer PARAMS ((cpp_hashnode *,
109 const struct answer *));
a4b4a940 110
4bf559f2 111/* This is the table of directive handlers. It is ordered by
112 frequency of occurrence; the numbers at the end are directive
113 counts from all the source code I have lying around (egcs and libc
114 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
79bd622b 115 pcmcia-cs-3.0.9). This is no longer important as directive lookup
116 is now O(1). All extensions other than #warning and #include_next
117 are deprecated. The name is where the extension appears to have
118 come from. */
4bf559f2 119
79bd622b 120#define DIRECTIVE_TABLE \
121D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
122D(include, T_INCLUDE, KANDR, INCL) /* 52262 */ \
123D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
124D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
125D(if, T_IF, KANDR, COND | IF_COND) /* 18162 */ \
126D(else, T_ELSE, KANDR, COND) /* 9863 */ \
127D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
128D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
129D(line, T_LINE, KANDR, IN_I) /* 2465 */ \
130D(elif, T_ELIF, KANDR, COND) /* 610 */ \
131D(error, T_ERROR, STDC89, 0) /* 475 */ \
132D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
133D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
134D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL) /* 19 */ \
135D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
136D(import, T_IMPORT, EXTENSION, INCL) /* 0 ObjC */ \
137D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
138D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
139SCCS_ENTRY /* 0 SVR4? */
4bf559f2 140
ef33b55c 141/* #sccs is not always recognized. */
142#ifdef SCCS_DIRECTIVE
f80e83a9 143# define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0)
ef33b55c 144#else
145# define SCCS_ENTRY /* nothing */
146#endif
147
4bf559f2 148/* Use the table to generate a series of prototypes, an enum for the
149 directive names, and an array of directive handlers. */
150
151/* The directive-processing functions are declared to return int
152 instead of void, because some old compilers have trouble with
153 pointers to functions returning void. */
154
36d1fa08 155/* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
69461e0d 156#define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *));
4bf559f2 157DIRECTIVE_TABLE
158#undef D
159
f80e83a9 160#define D(n, tag, o, f) tag,
4bf559f2 161enum
162{
79bd622b 163 T_BAD_DIRECTIVE,
4bf559f2 164 DIRECTIVE_TABLE
165 N_DIRECTIVES
1d6fa33f 166};
4bf559f2 167#undef D
168
36d1fa08 169/* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
f80e83a9 170#define D(name, t, origin, flags) \
e057cf7c 171{ CONCAT2(do_,name), (const U_CHAR *) STRINGX(name), \
f80e83a9 172 sizeof STRINGX(name) - 1, origin, flags },
79bd622b 173static const directive dtable[] =
4bf559f2 174{
175DIRECTIVE_TABLE
176};
177#undef D
178#undef DIRECTIVE_TABLE
1d6fa33f 179
79bd622b 180/* Skip any remaining tokens in a directive. */
181static void
182skip_rest_of_line (pfile)
f80e83a9 183 cpp_reader *pfile;
6060326b 184{
79bd622b 185 cpp_token token;
186
187 /* Discard all stacked contexts. */
188 while (pfile->context != &pfile->base_context)
189 _cpp_pop_context (pfile);
190
191 /* Sweep up all tokens remaining on the line. We need to read
192 tokens from lookahead, but cannot just drop the lookahead buffers
193 because they may be saving tokens prior to this directive for an
3f1753f5 194 external client. So we use _cpp_get_token, with macros disabled. */
79bd622b 195 pfile->state.prevent_expansion++;
196 while (!pfile->state.skip_newlines)
3f1753f5 197 _cpp_get_token (pfile, &token);
79bd622b 198 pfile->state.prevent_expansion--;
199}
6060326b 200
79bd622b 201/* Ensure there are no stray tokens at the end of a directive. */
202static void
203check_eol (pfile)
204 cpp_reader *pfile;
205{
206 if (!pfile->state.skip_newlines)
338fa5f7 207 {
79bd622b 208 cpp_token token;
338fa5f7 209
79bd622b 210 _cpp_lex_token (pfile, &token);
211 if (token.type != CPP_EOF)
212 cpp_pedwarn (pfile, "extra tokens at end of #%s directive",
213 pfile->directive->name);
338fa5f7 214 }
79bd622b 215}
216
217/* Check if a token's name matches that of a known directive. Put in
218 this file to save exporting dtable and other unneeded information. */
219int
220_cpp_handle_directive (pfile, indented)
221 cpp_reader *pfile;
222 int indented;
223{
224 const directive *dir = 0;
225 cpp_token dname;
226 int not_asm = 1;
338fa5f7 227
79bd622b 228 /* Some handlers need the position of the # for diagnostics. */
229 pfile->directive_pos = pfile->lexer_pos;
6060326b 230
79bd622b 231 /* We're now in a directive. This ensures we get pedantic warnings
232 about /v and /f in whitespace. */
233 pfile->state.in_directive = 1;
234 pfile->state.save_comments = 0;
338fa5f7 235
79bd622b 236 /* Lex the directive name directly. */
237 _cpp_lex_token (pfile, &dname);
338fa5f7 238
79bd622b 239 if (dname.type == CPP_NAME)
338fa5f7 240 {
79bd622b 241 unsigned int index = dname.val.node->directive_index;
242 if (index)
243 dir = &dtable[index - 1];
338fa5f7 244 }
79bd622b 245 else if (dname.type == CPP_NUMBER)
338fa5f7 246 {
79bd622b 247 /* # followed by a number is equivalent to #line. Do not
248 recognize this form in assembly language source files or
249 skipped conditional groups. Complain about this form if
250 we're being pedantic, but not if this is regurgitated input
251 (preprocessed or fed back in by the C++ frontend). */
252 if (!pfile->skipping && !CPP_OPTION (pfile, lang_asm))
338fa5f7 253 {
79bd622b 254 dir = &dtable[T_LINE];
255 _cpp_push_token (pfile, &dname, &pfile->directive_pos);
256 if (CPP_PEDANTIC (pfile) && CPP_BUFFER (pfile)->inc
257 && ! CPP_OPTION (pfile, preprocessed))
258 cpp_pedwarn (pfile, "# followed by integer");
338fa5f7 259 }
79bd622b 260 }
338fa5f7 261
79bd622b 262 pfile->directive = dir;
263 if (dir)
264 {
265 /* Make sure we lex headers correctly, whether skipping or not. */
266 pfile->state.angled_headers = dir->flags & INCL;
338fa5f7 267
79bd622b 268 /* If we are rescanning preprocessed input, only directives tagged
269 with IN_I are honored, and the warnings below are suppressed. */
270 if (! CPP_OPTION (pfile, preprocessed) || dir->flags & IN_I)
271 {
272 /* Traditionally, a directive is ignored unless its # is in
273 column 1. Therefore in code intended to work with K+R
274 compilers, directives added by C89 must have their #
275 indented, and directives present in traditional C must
276 not. This is true even of directives in skipped
277 conditional blocks. */
278 if (CPP_WTRADITIONAL (pfile))
279 {
280 if (indented && dir->origin == KANDR)
281 cpp_warning (pfile,
282 "traditional C ignores #%s with the # indented",
283 dir->name);
284 else if (!indented && dir->origin != KANDR)
285 cpp_warning (pfile,
286 "suggest hiding #%s from traditional C with an indented #",
287 dir->name);
288 }
289
290 /* If we are skipping a failed conditional group, all
291 non-conditional directives are ignored. */
292 if (!pfile->skipping || (dir->flags & COND))
293 {
294 /* Issue -pedantic warnings for extensions. */
295 if (CPP_PEDANTIC (pfile) && dir->origin == EXTENSION)
296 cpp_pedwarn (pfile, "#%s is a GCC extension", dir->name);
297
298 /* If we have a directive that is not an opening
299 conditional, invalidate any control macro. */
300 if (! (dir->flags & IF_COND))
301 pfile->mi_state = MI_FAILED;
302
303 (*dir->handler) (pfile);
304 }
305 }
306 }
307 else if (dname.type == CPP_EOF)
308 {
309 /* The null directive. */
310 if (indented && CPP_WTRADITIONAL (pfile))
311 cpp_warning (pfile, "traditional C ignores #\\n with the # indented");
312 }
313 else
314 {
315 /* An unknown directive. Don't complain about it in assembly
316 source: we don't know where the comments are, and # may
317 introduce assembler pseudo-ops. Don't complain about invalid
318 directives in skipped conditional groups (6.10 p4). */
319 if (CPP_OPTION (pfile, lang_asm))
320 {
321 /* Output the # and lookahead token for the assembler. */
322 not_asm = 0;
323 _cpp_push_token (pfile, &dname, &pfile->directive_pos);
324 }
325 else if (!pfile->skipping)
326 cpp_error (pfile, "invalid preprocessing directive #%s",
327 cpp_token_as_text (pfile, &dname));
338fa5f7 328 }
329
79bd622b 330 /* Save the lookahead token for assembler. */
331 if (not_asm)
332 skip_rest_of_line (pfile);
333 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
334 pfile->state.in_directive = 0;
335 pfile->state.angled_headers = 0;
336 pfile->directive = 0;
338fa5f7 337
79bd622b 338 return not_asm;
f80e83a9 339}
1d6fa33f 340
79bd622b 341/* Directive handler wrapper used by the command line option
342 processor. */
343static void
344run_directive (pfile, dir_no, buf, count, name)
1d6fa33f 345 cpp_reader *pfile;
79bd622b 346 int dir_no;
347 const char *buf;
348 size_t count;
349 const char *name;
e3630c4b 350{
396ffa86 351 if (cpp_push_buffer (pfile, (const U_CHAR *) buf, count) != NULL)
79bd622b 352 {
353 const struct directive *dir = &dtable[dir_no];
354
355 if (name)
356 CPP_BUFFER (pfile)->nominal_fname = name;
357 else
358 CPP_BUFFER (pfile)->nominal_fname = _("<command line>");
79bd622b 359
396ffa86 360 /* A kludge to avoid line markers for _Pragma. */
361 if (dir_no == T_PRAGMA)
362 pfile->lexer_pos.output_line = CPP_BUFFER (pfile)->prev->lineno;
363
364 /* For _Pragma, the text is passed through preprocessing stage 3
365 only, i.e. no trigraphs, no escaped newline removal, and no
366 macro expansion. Do the same for command-line directives. */
367 pfile->buffer->from_stage3 = 1;
79bd622b 368 pfile->state.in_directive = 1;
369 pfile->directive = dir;
396ffa86 370 pfile->state.prevent_expansion++;
79bd622b 371 (void) (*dir->handler) (pfile);
396ffa86 372 pfile->state.prevent_expansion--;
79bd622b 373 pfile->directive = 0;
374 pfile->state.in_directive = 0;
52eeb475 375
79bd622b 376 skip_rest_of_line (pfile);
396ffa86 377 if (pfile->buffer->cur != pfile->buffer->rlimit)
378 cpp_error (pfile, "extra text after end of #%s directive",
379 dtable[dir_no].name);
79bd622b 380 cpp_pop_buffer (pfile);
381 }
382}
383
384/* Checks for validity the macro name in #define, #undef, #ifdef and
385 #ifndef directives. */
f80e83a9 386static cpp_hashnode *
79bd622b 387lex_macro_node (pfile)
f80e83a9 388 cpp_reader *pfile;
389{
79bd622b 390 cpp_token token;
c030ed73 391
79bd622b 392 /* Lex the macro name directly. */
393 _cpp_lex_token (pfile, &token);
c4357c92 394
31674461 395 /* The token immediately after #define must be an identifier. That
396 identifier is not allowed to be "defined". See predefined macro
397 names (6.10.8.4). In C++, it is not allowed to be any of the
398 <iso646.h> macro names (which are keywords in C++) either. */
399
79bd622b 400 if (token.type != CPP_NAME)
1d6fa33f 401 {
79bd622b 402 if (token.type == CPP_EOF)
403 cpp_error (pfile, "no macro name given in #%s directive",
404 pfile->directive->name);
405 else if (token.flags & NAMED_OP)
406 cpp_error (pfile,
407 "\"%s\" cannot be used as a macro name as it is an operator in C++",
408 token.val.node->name);
31674461 409 else
79bd622b 410 cpp_error (pfile, "macro names must be identifiers");
71aa9da4 411 }
79bd622b 412 else
ef33b55c 413 {
79bd622b 414 cpp_hashnode *node = token.val.node;
415
416 /* In Objective C, some keywords begin with '@', but general
417 identifiers do not, and you're not allowed to #define them. */
418 if (node == pfile->spec_nodes.n_defined || node->name[0] == '@')
419 cpp_error (pfile, "\"%s\" cannot be used as a macro name", node->name);
420 else if (!(node->flags & NODE_POISONED))
421 return node;
ef33b55c 422 }
423
79bd622b 424 return 0;
1d6fa33f 425}
1d6fa33f 426
79bd622b 427/* Process a #define directive. Most work is done in cppmacro.c. */
69461e0d 428static void
4bf559f2 429do_define (pfile)
1d6fa33f 430 cpp_reader *pfile;
1d6fa33f 431{
79bd622b 432 cpp_hashnode *node = lex_macro_node (pfile);
c030ed73 433
79bd622b 434 if (node)
435 {
436 /* Use the permanent pool for storage. */
437 pfile->string_pool = &pfile->ident_pool;
438
439 if (_cpp_create_definition (pfile, node))
440 if (pfile->cb.define)
441 (*pfile->cb.define) (pfile, node);
442
443 /* Revert to the temporary pool. */
444 pfile->string_pool = &pfile->temp_string_pool;
445 }
f80e83a9 446}
447
79bd622b 448/* Handle #undef. Marks the identifier NT_VOID in the hash table. */
69461e0d 449static void
f80e83a9 450do_undef (pfile)
451 cpp_reader *pfile;
452{
79bd622b 453 cpp_hashnode *node = lex_macro_node (pfile);
2c63d6c8 454
f80e83a9 455 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
456 is not currently defined as a macro name. */
79bd622b 457 if (node && node->type == NT_MACRO)
2c63d6c8 458 {
6cae2504 459 if (pfile->cb.undef)
460 (*pfile->cb.undef) (pfile, node);
2c63d6c8 461
79bd622b 462 if (node->flags & NODE_BUILTIN)
f80e83a9 463 cpp_warning (pfile, "undefining \"%s\"", node->name);
2c63d6c8 464
f80e83a9 465 _cpp_free_definition (node);
2c63d6c8 466 }
79bd622b 467 check_eol (pfile);
1d6fa33f 468}
469
79bd622b 470/* Helper routine used by parse_include. Reinterpret the current line
471 as an h-char-sequence (< ... >); we are looking at the first token
472 after the <. Returns zero on success. */
473static int
474glue_header_name (pfile, header)
475 cpp_reader *pfile;
476 cpp_token *header;
477{
478 cpp_token token;
479 unsigned char *buffer, *token_mem;
480 size_t len, total_len = 0, capacity = 1024;
481
482 /* To avoid lexed tokens overwriting our glued name, we can only
483 allocate from the string pool once we've lexed everything. */
484
485 buffer = (unsigned char *) xmalloc (capacity);
486 for (;;)
487 {
488 _cpp_get_token (pfile, &token);
489
490 if (token.type == CPP_GREATER || token.type == CPP_EOF)
491 break;
492
493 len = cpp_token_len (&token);
494 if (total_len + len > capacity)
495 {
496 capacity = (capacity + len) * 2;
497 buffer = (unsigned char *) realloc (buffer, capacity);
498 }
499
500 if (token.flags & PREV_WHITE)
501 buffer[total_len++] = ' ';
502
503 total_len = cpp_spell_token (pfile, &token, &buffer[total_len]) - buffer;
504 }
505
506 if (token.type == CPP_EOF)
507 cpp_error (pfile, "missing terminating > character");
508 else
509 {
510 token_mem = _cpp_pool_alloc (pfile->string_pool, total_len);
511 memcpy (token_mem, buffer, total_len);
512
513 header->type = CPP_HEADER_NAME;
514 header->flags &= ~PREV_WHITE;
515 header->val.str.len = total_len;
516 header->val.str.text = token_mem;
517 }
f80e83a9 518
79bd622b 519 free ((PTR) buffer);
520 return token.type == CPP_EOF;
521}
1d6fa33f 522
79bd622b 523/* Parse the header name of #include, #include_next, #import and
524 #pragma dependency. Returns zero on success. */
f80e83a9 525static int
79bd622b 526parse_include (pfile, header)
1d6fa33f 527 cpp_reader *pfile;
79bd622b 528 cpp_token *header;
1d6fa33f 529{
79bd622b 530 int is_pragma = pfile->directive == &dtable[T_PRAGMA];
531 const unsigned char *dir;
1d6fa33f 532
79bd622b 533 if (is_pragma)
534 dir = U"pragma dependency";
535 else
536 dir = pfile->directive->name;
537
538 /* Allow macro expansion. */
539 cpp_get_token (pfile, header);
540 if (header->type != CPP_STRING && header->type != CPP_HEADER_NAME)
1d6fa33f 541 {
79bd622b 542 if (header->type != CPP_LESS)
f80e83a9 543 {
544 cpp_error (pfile, "#%s expects \"FILENAME\" or <FILENAME>", dir);
545 return 1;
546 }
79bd622b 547 if (glue_header_name (pfile, header))
548 return 1;
1d6fa33f 549 }
79bd622b 550
551 if (header->val.str.len == 0)
1d6fa33f 552 {
f80e83a9 553 cpp_error (pfile, "empty file name in #%s", dir);
554 return 1;
1d6fa33f 555 }
1d6fa33f 556
79bd622b 557 if (!is_pragma)
558 {
559 check_eol (pfile);
560 /* Get out of macro context, if we are. */
561 skip_rest_of_line (pfile);
562 if (pfile->cb.include)
563 (*pfile->cb.include) (pfile, dir, header);
564 }
6cae2504 565
f80e83a9 566 return 0;
4bf559f2 567}
d453a374 568
69461e0d 569static void
4bf559f2 570do_include (pfile)
571 cpp_reader *pfile;
572{
79bd622b 573 cpp_token header;
1d6fa33f 574
79bd622b 575 if (!parse_include (pfile, &header))
576 _cpp_execute_include (pfile, &header, 0, 0);
4bf559f2 577}
c4e0ec82 578
69461e0d 579static void
4bf559f2 580do_import (pfile)
581 cpp_reader *pfile;
582{
79bd622b 583 cpp_token header;
4bf559f2 584
02c09dab 585 if (!pfile->import_warning && CPP_OPTION (pfile, warn_import))
1d6fa33f 586 {
4bf559f2 587 pfile->import_warning = 1;
588 cpp_warning (pfile,
589 "#import is obsolete, use an #ifndef wrapper in the header file");
1d6fa33f 590 }
1d6fa33f 591
79bd622b 592 if (!parse_include (pfile, &header))
593 _cpp_execute_include (pfile, &header, 1, 0);
4bf559f2 594}
1d6fa33f 595
69461e0d 596static void
4bf559f2 597do_include_next (pfile)
598 cpp_reader *pfile;
599{
79bd622b 600 cpp_token header;
4bf559f2 601 struct file_name_list *search_start = 0;
1d6fa33f 602
79bd622b 603 if (parse_include (pfile, &header))
69461e0d 604 return;
4bf559f2 605
f80e83a9 606 /* For #include_next, skip in the search path past the dir in which
607 the current file was found. If this is the last directory in the
608 search path, don't include anything. If the current file was
609 specified with an absolute path, use the normal search logic. If
610 this is the primary source file, use the normal search logic and
611 generate a warning. */
4bf559f2 612 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
1d6fa33f 613 {
c95d8aaa 614 if (CPP_BUFFER (pfile)->inc->foundhere)
f80e83a9 615 {
616 search_start = CPP_BUFFER (pfile)->inc->foundhere->next;
617 if (!search_start)
69461e0d 618 return;
f80e83a9 619 }
1d6fa33f 620 }
4bf559f2 621 else
622 cpp_warning (pfile, "#include_next in primary source file");
623
79bd622b 624 _cpp_execute_include (pfile, &header, 0, search_start);
1d6fa33f 625}
1d6fa33f 626
314ab6d5 627/* Subroutine of do_line. Read next token from PFILE without adding it to
628 the output buffer. If it is a number between 1 and 4, store it in *NUM
629 and return 1; otherwise, return 0 and complain if we aren't at the end
630 of the directive. */
631
632static int
633read_line_number (pfile, num)
634 cpp_reader *pfile;
635 int *num;
636{
79bd622b 637 cpp_token token;
638 unsigned int val;
314ab6d5 639
79bd622b 640 _cpp_lex_token (pfile, &token);
641 if (token.type == CPP_NUMBER && token.val.str.len == 1)
314ab6d5 642 {
79bd622b 643 val = token.val.str.text[0] - '1';
644 if (val <= 3)
645 {
646 *num = val + 1;
647 return 1;
648 }
314ab6d5 649 }
79bd622b 650
651 if (token.type != CPP_EOF)
652 cpp_error (pfile, "invalid format #line");
653 return 0;
314ab6d5 654}
655
f80e83a9 656/* Another subroutine of do_line. Convert a number in STR, of length
657 LEN, to binary; store it in NUMP, and return 0 if the number was
e0a859f1 658 well-formed, 1 if not. Temporary, hopefully. */
f80e83a9 659static int
660strtoul_for_line (str, len, nump)
661 const U_CHAR *str;
662 unsigned int len;
663 unsigned long *nump;
664{
665 unsigned long reg = 0;
666 U_CHAR c;
667 while (len--)
668 {
669 c = *str++;
670 if (!ISDIGIT (c))
671 return 1;
672 reg *= 10;
673 reg += c - '0';
674 }
675 *nump = reg;
676 return 0;
677}
678
6d71dc85 679/* Interpret #line command.
680 Note that the filename string (if any) is treated as if it were an
681 include filename. That means no escape handling. */
1d6fa33f 682
69461e0d 683static void
4bf559f2 684do_line (pfile)
1d6fa33f 685 cpp_reader *pfile;
1d6fa33f 686{
6d71dc85 687 cpp_buffer *ip = CPP_BUFFER (pfile);
79bd622b 688 unsigned long new_lineno;
f80e83a9 689 /* C99 raised the minimum limit on #line numbers. */
690 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
0653b94e 691 int enter = 0, leave = 0, rename = 0;
79bd622b 692 cpp_token token;
693
694 /* #line commands expand macros. */
695 _cpp_get_token (pfile, &token);
696 if (token.type != CPP_NUMBER
697 || strtoul_for_line (token.val.str.text, token.val.str.len, &new_lineno))
1d6fa33f 698 {
79bd622b 699 cpp_error (pfile, "\"%s\" after #line is not a positive integer",
700 cpp_token_as_text (pfile, &token));
0653b94e 701 return;
6d71dc85 702 }
1d6fa33f 703
f80e83a9 704 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
705 cpp_pedwarn (pfile, "line number out of range");
1d6fa33f 706
79bd622b 707 _cpp_get_token (pfile, &token);
2e398cc7 708
79bd622b 709 if (token.type != CPP_EOF)
a852e3b1 710 {
79bd622b 711 char *fname;
712 unsigned int len;
713 int action_number = 0;
a852e3b1 714
79bd622b 715 if (token.type != CPP_STRING)
716 {
717 cpp_error (pfile, "\"%s\" is not a valid filename",
718 cpp_token_as_text (pfile, &token));
719 return;
720 }
1d6fa33f 721
79bd622b 722 len = token.val.str.len;
723 fname = alloca (len + 1);
724 memcpy (fname, token.val.str.text, len);
725 fname[len] = '\0';
726
727 if (strcmp (fname, ip->nominal_fname))
728 {
729 rename = 1;
730 if (!strcmp (fname, ip->inc->name))
731 ip->nominal_fname = ip->inc->name;
732 else
733 ip->nominal_fname = _cpp_fake_include (pfile, fname);
734 }
616814f8 735
79bd622b 736 if (read_line_number (pfile, &action_number) != 0)
737 {
738 if (CPP_PEDANTIC (pfile))
739 cpp_pedwarn (pfile, "extra tokens at end of #line directive");
c030ed73 740
79bd622b 741 if (action_number == 1)
742 {
743 enter = 1;
744 cpp_make_system_header (pfile, ip, 0);
745 read_line_number (pfile, &action_number);
746 }
747 else if (action_number == 2)
748 {
749 leave = 1;
750 cpp_make_system_header (pfile, ip, 0);
751 read_line_number (pfile, &action_number);
752 }
753 if (action_number == 3)
754 {
755 cpp_make_system_header (pfile, ip, 1);
756 read_line_number (pfile, &action_number);
757 }
758 if (action_number == 4)
759 {
760 cpp_make_system_header (pfile, ip, 2);
761 read_line_number (pfile, &action_number);
762 }
763 }
764 check_eol (pfile);
f80e83a9 765 }
1d6fa33f 766
79bd622b 767 /* Our line number is incremented after the directive is processed. */
768 ip->lineno = new_lineno - 1;
769 pfile->lexer_pos.output_line = ip->lineno;
6cae2504 770 if (enter && pfile->cb.enter_file)
771 (*pfile->cb.enter_file) (pfile);
772 if (leave && pfile->cb.leave_file)
773 (*pfile->cb.leave_file) (pfile);
0653b94e 774 if (rename && pfile->cb.rename_file)
775 (*pfile->cb.rename_file) (pfile);
1d6fa33f 776}
616814f8 777
1d6fa33f 778/*
f3b83e9b 779 * Report a warning or error detected by the program we are
780 * processing. Use the directive's tokens in the error message.
1d6fa33f 781 */
782
69461e0d 783static void
f3b83e9b 784do_diagnostic (pfile, code)
1d6fa33f 785 cpp_reader *pfile;
f3b83e9b 786 enum error_type code;
1d6fa33f 787{
79bd622b 788 if (_cpp_begin_message (pfile, code, NULL, 0))
6cae2504 789 {
79bd622b 790 fprintf (stderr, "#%s ", pfile->directive->name);
791 pfile->state.prevent_expansion++;
792 cpp_output_line (pfile, stderr);
793 pfile->state.prevent_expansion--;
6cae2504 794 }
1d6fa33f 795}
796
f3b83e9b 797static void
798do_error (pfile)
799 cpp_reader *pfile;
800{
801 do_diagnostic (pfile, ERROR);
802}
1d6fa33f 803
69461e0d 804static void
4bf559f2 805do_warning (pfile)
1d6fa33f 806 cpp_reader *pfile;
1d6fa33f 807{
f3b83e9b 808 do_diagnostic (pfile, WARNING);
1d6fa33f 809}
810
4e29fcb2 811/* Report program identification. */
1d6fa33f 812
69461e0d 813static void
4bf559f2 814do_ident (pfile)
1d6fa33f 815 cpp_reader *pfile;
1d6fa33f 816{
79bd622b 817 cpp_token str;
6cae2504 818
79bd622b 819 _cpp_get_token (pfile, &str);
820 if (str.type != CPP_STRING)
821 cpp_error (pfile, "invalid #ident");
822 else if (pfile->cb.ident)
823 (*pfile->cb.ident) (pfile, &str.val.str);
4e29fcb2 824
79bd622b 825 check_eol (pfile);
1d6fa33f 826}
827
4e29fcb2 828/* Pragmata handling. We handle some of these, and pass the rest on
829 to the front end. C99 defines three pragmas and says that no macro
830 expansion is to be performed on them; whether or not macro
831 expansion happens for other pragmas is implementation defined.
832 This implementation never macro-expands the text after #pragma.
833
834 We currently do not support the _Pragma operator. Support for that
835 has to be coordinated with the front end. Proposed implementation:
836 both #pragma blah blah and _Pragma("blah blah") become
837 __builtin_pragma(blah blah) and we teach the parser about that. */
838
839/* Sub-handlers for the pragmas needing treatment here.
840 They return 1 if the token buffer is to be popped, 0 if not. */
3f075661 841struct pragma_entry
842{
6cae2504 843 struct pragma_entry *next;
f80e83a9 844 const char *name;
6cae2504 845 size_t len;
846 int isnspace;
847 union {
848 void (*handler) PARAMS ((cpp_reader *));
849 struct pragma_entry *space;
850 } u;
3f075661 851};
852
6cae2504 853void
854cpp_register_pragma (pfile, space, name, handler)
855 cpp_reader *pfile;
856 const char *space;
857 const char *name;
858 void (*handler) PARAMS ((cpp_reader *));
3f075661 859{
6cae2504 860 struct pragma_entry **x, *new;
861 size_t len;
3f075661 862
6cae2504 863 x = &pfile->pragmas;
864 if (space)
865 {
866 struct pragma_entry *p = pfile->pragmas;
867 len = strlen (space);
868 while (p)
869 {
870 if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
871 {
872 x = &p->u.space;
873 goto found;
874 }
875 p = p->next;
876 }
877 cpp_ice (pfile, "unknown #pragma namespace %s", space);
878 return;
879 }
880
881 found:
882 new = xnew (struct pragma_entry);
883 new->name = name;
884 new->len = strlen (name);
885 new->isnspace = 0;
886 new->u.handler = handler;
887
888 new->next = *x;
889 *x = new;
890}
3f075661 891
6cae2504 892void
893cpp_register_pragma_space (pfile, space)
3f075661 894 cpp_reader *pfile;
6cae2504 895 const char *space;
3f075661 896{
6cae2504 897 struct pragma_entry *new;
898 const struct pragma_entry *p = pfile->pragmas;
899 size_t len = strlen (space);
900
901 while (p)
902 {
903 if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
c7125d9d 904 /* Multiple different callers are allowed to register the same
905 namespace. */
906 return;
6cae2504 907 p = p->next;
908 }
909
910 new = xnew (struct pragma_entry);
911 new->name = space;
912 new->len = len;
913 new->isnspace = 1;
914 new->u.space = 0;
915
916 new->next = pfile->pragmas;
917 pfile->pragmas = new;
918}
76faa4c0 919
6cae2504 920void
921_cpp_init_internal_pragmas (pfile)
922 cpp_reader *pfile;
923{
924 /* top level */
925 cpp_register_pragma (pfile, 0, "poison", do_pragma_poison);
926 cpp_register_pragma (pfile, 0, "once", do_pragma_once);
927
928 /* GCC namespace */
929 cpp_register_pragma_space (pfile, "GCC");
930
931 cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison);
932 cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header);
933 cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency);
3f075661 934}
1d6fa33f 935
69461e0d 936static void
4bf559f2 937do_pragma (pfile)
1d6fa33f 938 cpp_reader *pfile;
1d6fa33f 939{
6cae2504 940 const struct pragma_entry *p;
79bd622b 941 cpp_token tok;
6cae2504 942 const cpp_hashnode *node;
943 const U_CHAR *name;
944 size_t len;
79bd622b 945 int drop = 0;
2e398cc7 946
6cae2504 947 p = pfile->pragmas;
79bd622b 948 pfile->state.prevent_expansion++;
949 cpp_start_lookahead (pfile);
6cae2504 950
951 new_space:
79bd622b 952 cpp_get_token (pfile, &tok);
953 if (tok.type == CPP_NAME)
c0b823af 954 {
79bd622b 955 node = tok.val.node;
956 name = node->name;
957 len = node->length;
958 while (p)
6cae2504 959 {
79bd622b 960 if (strlen (p->name) == len && !memcmp (p->name, name, len))
6cae2504 961 {
79bd622b 962 if (p->isnspace)
963 {
964 p = p->u.space;
965 goto new_space;
966 }
967 else
968 {
969 (*p->u.handler) (pfile);
970 drop = 1;
971 break;
972 }
6cae2504 973 }
79bd622b 974 p = p->next;
6cae2504 975 }
6cae2504 976 }
f80e83a9 977
79bd622b 978 cpp_stop_lookahead (pfile, drop);
979 pfile->state.prevent_expansion--;
980
981 if (!drop && pfile->cb.def_pragma)
6cae2504 982 (*pfile->cb.def_pragma) (pfile);
3f075661 983}
984
6cae2504 985static void
4e29fcb2 986do_pragma_once (pfile)
987 cpp_reader *pfile;
988{
989 cpp_buffer *ip = CPP_BUFFER (pfile);
990
02c09dab 991 cpp_warning (pfile, "#pragma once is obsolete");
992
5b201908 993 if (CPP_PREV_BUFFER (ip) == NULL)
79bd622b 994 cpp_warning (pfile, "#pragma once in main file");
4e29fcb2 995 else
c95d8aaa 996 ip->inc->cmacro = NEVER_REREAD;
79bd622b 997
998 check_eol (pfile);
4e29fcb2 999}
c9d838e1 1000
6cae2504 1001static void
4e29fcb2 1002do_pragma_poison (pfile)
1003 cpp_reader *pfile;
1004{
1005 /* Poison these symbols so that all subsequent usage produces an
1006 error message. */
79bd622b 1007 cpp_token tok;
c4abf88d 1008 cpp_hashnode *hp;
4e29fcb2 1009
79bd622b 1010 pfile->state.poisoned_ok = 1;
4e29fcb2 1011 for (;;)
1012 {
79bd622b 1013 _cpp_lex_token (pfile, &tok);
1014 if (tok.type == CPP_EOF)
4e29fcb2 1015 break;
79bd622b 1016 if (tok.type != CPP_NAME)
c9d838e1 1017 {
79bd622b 1018 cpp_error (pfile, "invalid #pragma GCC poison directive");
1019 break;
c9d838e1 1020 }
1021
79bd622b 1022 hp = tok.val.node;
1023 if (hp->flags & NODE_POISONED)
1024 continue;
1025
1026 if (hp->type == NT_MACRO)
1027 cpp_warning (pfile, "poisoning existing macro \"%s\"", hp->name);
1028 _cpp_free_definition (hp);
1029 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
c9d838e1 1030 }
79bd622b 1031 pfile->state.poisoned_ok = 0;
6cae2504 1032
79bd622b 1033#if 0 /* Doesn't quite work yet. */
1034 if (tok.type == CPP_EOF && pfile->cb.poison)
6cae2504 1035 (*pfile->cb.poison) (pfile);
79bd622b 1036#endif
1d6fa33f 1037}
e35f919d 1038
1039/* Mark the current header as a system header. This will suppress
1040 some categories of warnings (notably those from -pedantic). It is
1041 intended for use in system libraries that cannot be implemented in
1042 conforming C, but cannot be certain that their headers appear in a
1043 system include directory. To prevent abuse, it is rejected in the
1044 primary source file. */
6cae2504 1045static void
e35f919d 1046do_pragma_system_header (pfile)
1047 cpp_reader *pfile;
1048{
f80e83a9 1049 cpp_buffer *ip = CPP_BUFFER (pfile);
e35f919d 1050 if (CPP_PREV_BUFFER (ip) == NULL)
1051 cpp_warning (pfile, "#pragma system_header outside include file");
1052 else
6af24d0a 1053 cpp_make_system_header (pfile, ip, 1);
79bd622b 1054
1055 check_eol (pfile);
e35f919d 1056}
e485814b 1057
1058/* Check the modified date of the current include file against a specified
1059 file. Issue a diagnostic, if the specified file is newer. We use this to
1060 determine if a fixed header should be refixed. */
6cae2504 1061static void
e485814b 1062do_pragma_dependency (pfile)
1063 cpp_reader *pfile;
1064{
79bd622b 1065 cpp_token header, msg;
1066 int ordering;
f80e83a9 1067
79bd622b 1068 if (parse_include (pfile, &header))
6cae2504 1069 return;
f80e83a9 1070
79bd622b 1071 ordering = _cpp_compare_file_date (pfile, &header);
e485814b 1072 if (ordering < 0)
79bd622b 1073 cpp_warning (pfile, "cannot find source %s",
1074 cpp_token_as_text (pfile, &header));
e485814b 1075 else if (ordering > 0)
1076 {
79bd622b 1077 cpp_warning (pfile, "current file is older than %s",
1078 cpp_token_as_text (pfile, &header));
1079 cpp_start_lookahead (pfile);
1080 cpp_get_token (pfile, &msg);
1081 cpp_stop_lookahead (pfile, msg.type == CPP_EOF);
1082 if (msg.type != CPP_EOF && _cpp_begin_message (pfile, WARNING, NULL, 0))
1083 cpp_output_line (pfile, stderr);
e485814b 1084 }
e485814b 1085}
1086
396ffa86 1087/* Check syntax is "(string-literal)". Returns 0 on success. */
1088static int
1089get__Pragma_string (pfile, string)
1090 cpp_reader *pfile;
1091 cpp_token *string;
1092{
1093 cpp_token paren;
1094
1095 cpp_get_token (pfile, &paren);
1096 if (paren.type != CPP_OPEN_PAREN)
1097 return 1;
1098
1099 cpp_get_token (pfile, string);
1100 if (string->type != CPP_STRING && string->type != CPP_WSTRING)
1101 return 1;
1102
1103 cpp_get_token (pfile, &paren);
1104 return paren.type != CPP_CLOSE_PAREN;
1105}
1106
1107/* Returns a malloced buffer containing a destringized cpp_string by
1108 removing the first \ of \" and \\ sequences. */
1109static unsigned char *
1110destringize (in, len)
1111 const cpp_string *in;
1112 unsigned int *len;
1113{
1114 const unsigned char *src, *limit;
1115 unsigned char *dest, *result;
1116
1117 dest = result = (unsigned char *) xmalloc (in->len);
1118 for (src = in->text, limit = src + in->len; src < limit;)
1119 {
1120 /* We know there is a character following the backslash. */
1121 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1122 src++;
1123 *dest++ = *src++;
1124 }
1125
1126 *len = dest - result;
1127 return result;
1128}
1129
1130void
1131_cpp_do__Pragma (pfile)
1132 cpp_reader *pfile;
1133{
1134 cpp_token string;
1135 unsigned char *buffer;
1136 unsigned int len;
1137
1138 if (get__Pragma_string (pfile, &string))
1139 {
1140 cpp_error (pfile, "_Pragma takes a parenthesized string literal");
1141 return;
1142 }
1143
1144 buffer = destringize (&string.val.str, &len);
1145 run_directive (pfile, T_PRAGMA, (char *) buffer, len, _("<_Pragma>"));
1146 free ((PTR) buffer);
1147}
1148
1d6fa33f 1149/* Just ignore #sccs, on systems where we define it at all. */
ef33b55c 1150#ifdef SCCS_DIRECTIVE
69461e0d 1151static void
4bf559f2 1152do_sccs (pfile)
f80e83a9 1153 cpp_reader *pfile ATTRIBUTE_UNUSED;
1d6fa33f 1154{
1d6fa33f 1155}
ef33b55c 1156#endif
20534e99 1157
69461e0d 1158static void
4bf559f2 1159do_ifdef (pfile)
1160 cpp_reader *pfile;
1161{
79bd622b 1162 int skip = 1;
f80e83a9 1163
1164 if (! pfile->skipping)
79bd622b 1165 {
1166 const cpp_hashnode *node = lex_macro_node (pfile);
f80e83a9 1167
79bd622b 1168 if (node)
1169 skip = node->type != NT_MACRO;
1170 }
4bf559f2 1171
79bd622b 1172 push_conditional (pfile, skip, T_IFDEF, 0);
1173}
4bf559f2 1174
69461e0d 1175static void
4bf559f2 1176do_ifndef (pfile)
1177 cpp_reader *pfile;
1178{
79bd622b 1179 int skip = 1;
76faa4c0 1180 const cpp_hashnode *node = 0;
4bf559f2 1181
f80e83a9 1182 if (! pfile->skipping)
b970ec42 1183 {
79bd622b 1184 node = lex_macro_node (pfile);
1185 if (node)
1186 skip = node->type == NT_MACRO;
b970ec42 1187 }
f80e83a9 1188
79bd622b 1189 push_conditional (pfile, skip, T_IFNDEF, node);
1d6fa33f 1190}
1191
79bd622b 1192/* #if cooperates with parse_defined to handle multiple-include
1193 optimisations. If macro expansions or identifiers appear in the
1194 expression, we cannot treat it as a controlling conditional, since
1195 their values could change in the future. */
1d6fa33f 1196
69461e0d 1197static void
c4357c92 1198do_if (pfile)
1d6fa33f 1199 cpp_reader *pfile;
1d6fa33f 1200{
79bd622b 1201 int skip = 1;
c4357c92 1202 const cpp_hashnode *cmacro = 0;
1d6fa33f 1203
79bd622b 1204 if (!pfile->skipping)
c4357c92 1205 {
79bd622b 1206 /* Controlling macro of #if ! defined () */
1207 pfile->mi_ind_cmacro = 0;
1208 skip = _cpp_parse_expr (pfile) == 0;
1209 cmacro = pfile->mi_ind_cmacro;
c4357c92 1210 }
79bd622b 1211
1212 push_conditional (pfile, skip, T_IF, cmacro);
1d6fa33f 1213}
1214
c4357c92 1215/* #else flips pfile->skipping and continues without changing
1216 if_stack; this is so that the error message for missing #endif's
1217 etc. will point to the original #if. */
8e33b875 1218
69461e0d 1219static void
c4357c92 1220do_else (pfile)
1221 cpp_reader *pfile;
8e33b875 1222{
c4357c92 1223 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
c030ed73 1224
c4357c92 1225 if (ifs == NULL)
79bd622b 1226 cpp_error (pfile, "#else without #if");
1227 else
c030ed73 1228 {
79bd622b 1229 if (ifs->type == T_ELSE)
1230 {
1231 cpp_error (pfile, "#else after #else");
1232 cpp_error_with_line (pfile, ifs->pos.line, ifs->pos.col,
1233 "the conditional began here");
1234 }
1d6fa33f 1235
79bd622b 1236 /* Invalidate any controlling macro. */
1237 ifs->mi_cmacro = 0;
1238
1239 ifs->type = T_ELSE;
1240 if (! ifs->was_skipping)
1241 {
1242 /* If pfile->skipping is 2, one of the blocks in an #if
1243 #elif ... chain succeeded, so we skip the else block. */
1244 if (pfile->skipping < 2)
1245 pfile->skipping = ! pfile->skipping;
1246 }
c030ed73 1247 }
79bd622b 1248
1249 check_eol (pfile);
1d6fa33f 1250}
1251
79bd622b 1252/* handle a #elif directive by not changing if_stack either. see the
1253 comment above do_else. */
1d6fa33f 1254
69461e0d 1255static void
c4357c92 1256do_elif (pfile)
1d6fa33f 1257 cpp_reader *pfile;
1d6fa33f 1258{
c4357c92 1259 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1d6fa33f 1260
c4357c92 1261 if (ifs == NULL)
0b60628c 1262 {
c4357c92 1263 cpp_error (pfile, "#elif without #if");
69461e0d 1264 return;
0b60628c 1265 }
79bd622b 1266
c4357c92 1267 if (ifs->type == T_ELSE)
0b60628c 1268 {
c4357c92 1269 cpp_error (pfile, "#elif after #else");
79bd622b 1270 cpp_error_with_line (pfile, ifs->pos.line, ifs->pos.col,
f80e83a9 1271 "the conditional began here");
c4357c92 1272 }
0b60628c 1273
79bd622b 1274 /* Invalidate any controlling macro. */
1275 ifs->mi_cmacro = 0;
1276
c4357c92 1277 ifs->type = T_ELIF;
1278 if (ifs->was_skipping)
69461e0d 1279 return; /* Don't evaluate a nested #if */
f80e83a9 1280
1281 if (pfile->skipping != 1)
c4357c92 1282 {
c4357c92 1283 pfile->skipping = 2; /* one block succeeded, so don't do any others */
69461e0d 1284 return;
0b60628c 1285 }
1d6fa33f 1286
f80e83a9 1287 pfile->skipping = ! _cpp_parse_expr (pfile);
1d6fa33f 1288}
1289
c4357c92 1290/* #endif pops the if stack and resets pfile->skipping. */
1d6fa33f 1291
69461e0d 1292static void
4bf559f2 1293do_endif (pfile)
1d6fa33f 1294 cpp_reader *pfile;
1d6fa33f 1295{
c4357c92 1296 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1297
c4357c92 1298 if (ifs == NULL)
1299 cpp_error (pfile, "#endif without #if");
1d6fa33f 1300 else
1301 {
79bd622b 1302 /* If potential control macro, we go back outside again. */
1303 if (ifs->next == 0 && ifs->mi_cmacro)
1304 {
1305 pfile->mi_state = MI_OUTSIDE;
1306 pfile->mi_cmacro = ifs->mi_cmacro;
1307 }
1308
c4357c92 1309 CPP_BUFFER (pfile)->if_stack = ifs->next;
1310 pfile->skipping = ifs->was_skipping;
f51c2148 1311 obstack_free (pfile->buffer_ob, ifs);
1d6fa33f 1312 }
1d6fa33f 1313
79bd622b 1314 check_eol (pfile);
1315}
f80e83a9 1316
c4357c92 1317/* Push an if_stack entry and set pfile->skipping accordingly.
1318 If this is a #ifndef starting at the beginning of a file,
1319 CMACRO is the macro name tested by the #ifndef. */
1320
1321static void
1322push_conditional (pfile, skip, type, cmacro)
1323 cpp_reader *pfile;
1324 int skip;
1325 int type;
1326 const cpp_hashnode *cmacro;
1327{
1328 struct if_stack *ifs;
1329
f51c2148 1330 ifs = xobnew (pfile->buffer_ob, struct if_stack);
79bd622b 1331 ifs->pos = pfile->directive_pos;
c4357c92 1332 ifs->next = CPP_BUFFER (pfile)->if_stack;
c4357c92 1333 ifs->was_skipping = pfile->skipping;
1334 ifs->type = type;
79bd622b 1335 if (pfile->mi_state == MI_OUTSIDE && pfile->mi_cmacro == 0)
1336 ifs->mi_cmacro = cmacro;
1337 else
1338 ifs->mi_cmacro = 0;
c4357c92 1339
1340 if (!pfile->skipping)
1341 pfile->skipping = skip;
1342
1343 CPP_BUFFER (pfile)->if_stack = ifs;
1344}
1345
f80e83a9 1346/* Called when we reach the end of a file. Walk back up the
d1678bc6 1347 conditional stack till we reach its level at entry to this file,
c4357c92 1348 issuing error messages. Then force skipping off. */
f51c2148 1349static void
1350unwind_if_stack (pfile, pbuf)
05dba164 1351 cpp_reader *pfile;
d1678bc6 1352 cpp_buffer *pbuf;
05dba164 1353{
79bd622b 1354 struct if_stack *ifs;
1355
1356 /* No need to free stack - they'll all go away with the buffer. */
1357 for (ifs = pbuf->if_stack; ifs; ifs = ifs->next)
1358 cpp_error_with_line (pfile, ifs->pos.line, ifs->pos.col,
1359 "unterminated #%s", dtable[ifs->type].name);
05dba164 1360
c4357c92 1361 pfile->skipping = 0;
4896abfd 1362}
69bcbfc6 1363
79bd622b 1364/* Read the tokens of the answer into the macro pool. Only commit the
1365 memory if we intend it as permanent storage, i.e. the #assert case.
1366 Returns 0 on success. */
1367
1368static int
1369parse_answer (pfile, answerp, type)
1d6fa33f 1370 cpp_reader *pfile;
f80e83a9 1371 struct answer **answerp;
79bd622b 1372 int type;
1d6fa33f 1373{
79bd622b 1374 cpp_token paren, *token;
1375 struct answer *answer;
1376
1377 if (POOL_FRONT (&pfile->macro_pool) + sizeof (struct answer) >
1378 POOL_LIMIT (&pfile->macro_pool))
1379 _cpp_next_chunk (&pfile->macro_pool, sizeof (struct answer), 0);
1380 answer = (struct answer *) POOL_FRONT (&pfile->macro_pool);
1381 answer->count = 0;
1382
1383 /* In a conditional, it is legal to not have an open paren. We
1384 should save the following token in this case. */
1385 if (type == T_IF)
1386 cpp_start_lookahead (pfile);
1387 cpp_get_token (pfile, &paren);
1388 if (type == T_IF)
1389 cpp_stop_lookahead (pfile, paren.type == CPP_OPEN_PAREN);
1390
1391 /* If not a paren, see if we're OK. */
1392 if (paren.type != CPP_OPEN_PAREN)
f80e83a9 1393 {
79bd622b 1394 /* In a conditional no answer is a test for any answer. It
1395 could be followed by any token. */
1396 if (type == T_IF)
1397 return 0;
1398
1399 /* #unassert with no answer is valid - it removes all answers. */
1400 if (type == T_UNASSERT && paren.type == CPP_EOF)
1401 return 0;
bce8e0c0 1402
f80e83a9 1403 cpp_error (pfile, "missing '(' after predicate");
79bd622b 1404 return 1;
f80e83a9 1405 }
69bcbfc6 1406
f80e83a9 1407 for (;;)
1408 {
79bd622b 1409 token = &answer->first[answer->count];
1410 /* Check we have room for the token. */
1411 if ((unsigned char *) (token + 1) >= POOL_LIMIT (&pfile->macro_pool))
f80e83a9 1412 {
79bd622b 1413 _cpp_next_chunk (&pfile->macro_pool, sizeof (cpp_token),
1414 (unsigned char **) &answer);
1415 token = &answer->first[answer->count];
f80e83a9 1416 }
79bd622b 1417
1418 _cpp_get_token (pfile, token);
f80e83a9 1419 if (token->type == CPP_CLOSE_PAREN)
1420 break;
3ba8b497 1421
79bd622b 1422 if (token->type == CPP_EOF)
f80e83a9 1423 {
79bd622b 1424 cpp_error (pfile, "missing ')' to complete answer");
1425 return 1;
f80e83a9 1426 }
79bd622b 1427 answer->count++;
f80e83a9 1428 }
bce8e0c0 1429
79bd622b 1430 if (answer->count == 0)
69bcbfc6 1431 {
f80e83a9 1432 cpp_error (pfile, "predicate's answer is empty");
79bd622b 1433 return 1;
1d6fa33f 1434 }
f80e83a9 1435
1436 /* Drop whitespace at start. */
79bd622b 1437 answer->first->flags &= ~PREV_WHITE;
1438 *answerp = answer;
f80e83a9 1439
79bd622b 1440 if (type == T_ASSERT || type == T_UNASSERT)
1441 check_eol (pfile);
1442 return 0;
1443}
f80e83a9 1444
79bd622b 1445/* Parses an assertion, returning a pointer to the hash node of the
1446 predicate, or 0 on error. If an answer was supplied, it is placed
1447 in ANSWERP, otherwise it is set to 0. We use _cpp_get_raw_token,
1448 since we cannot assume tokens are consecutive in a #if statement
1449 (we may be in a macro), and we don't want to macro expand. */
1450static cpp_hashnode *
1451parse_assertion (pfile, answerp, type)
1452 cpp_reader *pfile;
1453 struct answer **answerp;
1454 int type;
1455{
1456 cpp_hashnode *result = 0;
1457 cpp_token predicate;
1458
1459 /* We don't expand predicates or answers. */
1460 pfile->state.prevent_expansion++;
1461
1462 /* Use the permanent pool for storage (for the answers). */
1463 pfile->string_pool = &pfile->ident_pool;
1464
1465 *answerp = 0;
1466 _cpp_get_token (pfile, &predicate);
1467 if (predicate.type == CPP_EOF)
1468 cpp_error (pfile, "assertion without predicate");
1469 else if (predicate.type != CPP_NAME)
1470 cpp_error (pfile, "predicate must be an identifier");
1471 else if (parse_answer (pfile, answerp, type) == 0)
1472 {
1473 unsigned int len = predicate.val.node->length;
1474 unsigned char *sym = alloca (len + 1);
f80e83a9 1475
79bd622b 1476 /* Prefix '#' to get it out of macro namespace. */
1477 sym[0] = '#';
1478 memcpy (sym + 1, predicate.val.node->name, len);
1479 result = cpp_lookup (pfile, sym, len + 1);
1480 }
69bcbfc6 1481
79bd622b 1482 pfile->string_pool = &pfile->temp_string_pool;
1483 pfile->state.prevent_expansion--;
1484 return result;
1d6fa33f 1485}
69bcbfc6 1486
f80e83a9 1487/* Returns a pointer to the pointer to the answer in the answer chain,
1488 or a pointer to NULL if the answer is not in the chain. */
79bd622b 1489static struct answer **
1490find_answer (node, candidate)
f80e83a9 1491 cpp_hashnode *node;
79bd622b 1492 const struct answer *candidate;
1d6fa33f 1493{
79bd622b 1494 unsigned int i;
f80e83a9 1495 struct answer **result;
1d6fa33f 1496
f80e83a9 1497 for (result = &node->value.answers; *result; result = &(*result)->next)
79bd622b 1498 {
1499 struct answer *answer = *result;
1500
1501 if (answer->count == candidate->count)
1502 {
1503 for (i = 0; i < answer->count; i++)
1504 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1505 break;
1506
1507 if (i == answer->count)
1508 break;
1509 }
1510 }
c030ed73 1511
f80e83a9 1512 return result;
1513}
bce8e0c0 1514
79bd622b 1515/* Test an assertion within a preprocessor conditional. Returns
1516 non-zero on failure, zero on success. On success, the result of
1517 the test is written into VALUE. */
1518int
1519_cpp_test_assertion (pfile, value)
1520 cpp_reader *pfile;
1521 int *value;
1522{
1523 struct answer *answer;
1524 cpp_hashnode *node;
1525
1526 node = parse_assertion (pfile, &answer, T_IF);
1527 if (node)
1528 *value = (node->type == NT_ASSERTION &&
1529 (answer == 0 || *find_answer (node, answer) != 0));
1530
1531 /* We don't commit the memory for the answer - it's temporary only. */
1532 return node == 0;
1533}
1534
69461e0d 1535static void
f80e83a9 1536do_assert (pfile)
1537 cpp_reader *pfile;
1538{
1539 struct answer *new_answer;
1540 cpp_hashnode *node;
1541
79bd622b 1542 node = parse_assertion (pfile, &new_answer, T_ASSERT);
f80e83a9 1543 if (node)
c030ed73 1544 {
79bd622b 1545 /* Place the new answer in the answer list. First check there
1546 is not a duplicate. */
f80e83a9 1547 new_answer->next = 0;
79bd622b 1548 if (node->type == NT_ASSERTION)
f80e83a9 1549 {
79bd622b 1550 if (*find_answer (node, new_answer))
1551 {
1552 cpp_warning (pfile, "\"%s\" re-asserted", node->name + 1);
1553 return;
1554 }
f80e83a9 1555 new_answer->next = node->value.answers;
1556 }
79bd622b 1557 node->type = NT_ASSERTION;
f80e83a9 1558 node->value.answers = new_answer;
79bd622b 1559 POOL_COMMIT (&pfile->macro_pool, (sizeof (struct answer)
1560 + (new_answer->count - 1)
1561 * sizeof (cpp_token)));
c030ed73 1562 }
f80e83a9 1563}
bce8e0c0 1564
69461e0d 1565static void
f80e83a9 1566do_unassert (pfile)
1567 cpp_reader *pfile;
1568{
1569 cpp_hashnode *node;
79bd622b 1570 struct answer *answer;
f80e83a9 1571
79bd622b 1572 node = parse_assertion (pfile, &answer, T_UNASSERT);
1573 /* It isn't an error to #unassert something that isn't asserted. */
1574 if (node && node->type == NT_ASSERTION)
69bcbfc6 1575 {
79bd622b 1576 if (answer)
bce8e0c0 1577 {
79bd622b 1578 struct answer **p = find_answer (node, answer), *temp;
f80e83a9 1579
79bd622b 1580 /* Remove the answer from the list. */
1581 temp = *p;
1582 if (temp)
1583 *p = temp->next;
3ba8b497 1584
79bd622b 1585 /* Did we free the last answer? */
1586 if (node->value.answers == 0)
1587 node->type = NT_VOID;
1588 }
1589 else
1590 _cpp_free_definition (node);
f80e83a9 1591 }
79bd622b 1592
1593 /* We don't commit the memory for the answer - it's temporary only. */
1d6fa33f 1594}
1d6fa33f 1595
0578f103 1596/* These are for -D, -U, -A. */
1597
1598/* Process the string STR as if it appeared as the body of a #define.
1599 If STR is just an identifier, define it with value 1.
1600 If STR has anything after the identifier, then it should
1601 be identifier=definition. */
1602
9fa36617 1603void
0578f103 1604cpp_define (pfile, str)
9fa36617 1605 cpp_reader *pfile;
8d7a2585 1606 const char *str;
9fa36617 1607{
0578f103 1608 char *buf, *p;
1609 size_t count;
1610
1611 p = strchr (str, '=');
1612 /* Copy the entire option so we can modify it.
1613 Change the first "=" in the string to a space. If there is none,
1614 tack " 1" on the end. Then add a newline and a NUL. */
1615
1616 if (p)
9fa36617 1617 {
0578f103 1618 count = strlen (str) + 2;
58f9db87 1619 buf = (char *) alloca (count);
0578f103 1620 memcpy (buf, str, count - 2);
1621 buf[p - str] = ' ';
1622 buf[count - 2] = '\n';
1623 buf[count - 1] = '\0';
1624 }
1625 else
1626 {
1627 count = strlen (str) + 4;
58f9db87 1628 buf = (char *) alloca (count);
0578f103 1629 memcpy (buf, str, count - 4);
1630 strcpy (&buf[count-4], " 1\n");
9fa36617 1631 }
eb3948d1 1632
79bd622b 1633 run_directive (pfile, T_DEFINE, buf, count - 1, 0);
a336277c 1634}
1635
1636/* Slight variant of the above for use by initialize_builtins, which (a)
1637 knows how to set up the buffer itself, (b) needs a different "filename"
1638 tag. */
1639void
1640_cpp_define_builtin (pfile, str)
1641 cpp_reader *pfile;
1642 const char *str;
1643{
79bd622b 1644 run_directive (pfile, T_DEFINE, str, strlen (str), _("<builtin>"));
0578f103 1645}
a92771b8 1646
0578f103 1647/* Process MACRO as if it appeared as the body of an #undef. */
1648void
1649cpp_undef (pfile, macro)
1d6fa33f 1650 cpp_reader *pfile;
0578f103 1651 const char *macro;
1d6fa33f 1652{
79bd622b 1653 run_directive (pfile, T_UNDEF, macro, strlen (macro), 0);
1d6fa33f 1654}
1655
0578f103 1656/* Process the string STR as if it appeared as the body of a #assert. */
1657void
1658cpp_assert (pfile, str)
1659 cpp_reader *pfile;
1660 const char *str;
1661{
79bd622b 1662 run_directive (pfile, T_ASSERT, str, strlen (str), 0);
0578f103 1663}
1d6fa33f 1664
0578f103 1665/* Process STR as if it appeared as the body of an #unassert. */
1666void
1667cpp_unassert (pfile, str)
1d6fa33f 1668 cpp_reader *pfile;
0578f103 1669 const char *str;
1d6fa33f 1670{
79bd622b 1671 run_directive (pfile, T_UNASSERT, str, strlen (str), 0);
0578f103 1672}
e3630c4b 1673
0578f103 1674/* Determine whether the identifier ID, of length LEN, is a defined macro. */
1675int
1676cpp_defined (pfile, id, len)
1677 cpp_reader *pfile;
1678 const U_CHAR *id;
1679 int len;
1680{
c4abf88d 1681 cpp_hashnode *hp = cpp_lookup (pfile, id, len);
79bd622b 1682
1683 /* If it's of type NT_MACRO, it cannot be poisoned. */
1684 return hp->type == NT_MACRO;
1d6fa33f 1685}
f51c2148 1686
79bd622b 1687/* Allocate a new cpp_buffer for PFILE, and push it on the input
1688 buffer stack. If BUFFER != NULL, then use the LENGTH characters in
1689 BUFFER as the new input buffer. Return the new buffer, or NULL on
1690 failure. */
f51c2148 1691
1692cpp_buffer *
1693cpp_push_buffer (pfile, buffer, length)
1694 cpp_reader *pfile;
1695 const U_CHAR *buffer;
1696 long length;
1697{
1698 cpp_buffer *buf = CPP_BUFFER (pfile);
1699 cpp_buffer *new;
1700 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
1701 {
63e1abce 1702 cpp_fatal (pfile, "#include nested too deeply");
f51c2148 1703 return NULL;
1704 }
79bd622b 1705
f51c2148 1706 new = xobnew (pfile->buffer_ob, cpp_buffer);
79bd622b 1707 /* Clears, amongst other things, if_stack and mi_cmacro. */
f51c2148 1708 memset (new, 0, sizeof (cpp_buffer));
1709
79bd622b 1710 pfile->lexer_pos.output_line = 1;
241e762e 1711 new->line_base = new->buf = new->cur = buffer;
f51c2148 1712 new->rlimit = buffer + length;
1713 new->prev = buf;
338fa5f7 1714 new->pfile = pfile;
396ffa86 1715 /* Preprocessed files don't do trigraph and escaped newline processing. */
1716 new->from_stage3 = CPP_OPTION (pfile, preprocessed);
f669338a 1717 /* No read ahead or extra char initially. */
338fa5f7 1718 new->read_ahead = EOF;
f669338a 1719 new->extra_char = EOF;
79bd622b 1720 pfile->state.skip_newlines = 1;
f51c2148 1721
1722 CPP_BUFFER (pfile) = new;
1723 return new;
1724}
1725
1726cpp_buffer *
1727cpp_pop_buffer (pfile)
1728 cpp_reader *pfile;
1729{
6cae2504 1730 int wfb;
f51c2148 1731 cpp_buffer *buf = CPP_BUFFER (pfile);
1732
1733 unwind_if_stack (pfile, buf);
6cae2504 1734 wfb = (buf->inc != 0);
1735 if (wfb)
241e762e 1736 _cpp_pop_file_buffer (pfile, buf);
f51c2148 1737
1738 CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
1739 obstack_free (pfile->buffer_ob, buf);
1740 pfile->buffer_stack_depth--;
6cae2504 1741
79bd622b 1742 if (CPP_BUFFER (pfile) && wfb && pfile->cb.leave_file)
6cae2504 1743 (*pfile->cb.leave_file) (pfile);
1744
f51c2148 1745 return CPP_BUFFER (pfile);
1746}
1747
1748#define obstack_chunk_alloc xmalloc
1749#define obstack_chunk_free free
1750void
1751_cpp_init_stacks (pfile)
1752 cpp_reader *pfile;
1753{
76faa4c0 1754 int i;
79bd622b 1755 cpp_hashnode *node;
76faa4c0 1756
f51c2148 1757 pfile->buffer_ob = xnew (struct obstack);
1758 obstack_init (pfile->buffer_ob);
76faa4c0 1759
79bd622b 1760 /* Register the directives. */
1761 for (i = 1; i < N_DIRECTIVES; i++)
1762 {
1763 node = cpp_lookup (pfile, dtable[i - 1].name, dtable[i - 1].length);
1764 node->directive_index = i;
1765 }
f51c2148 1766}
1767
1768void
1769_cpp_cleanup_stacks (pfile)
1770 cpp_reader *pfile;
1771{
1772 obstack_free (pfile->buffer_ob, 0);
1773 free (pfile->buffer_ob);
1774}