Merge trunk version 188721 into gupc branch.
[official-gcc.git] / libcpp / init.c
blob2e3b7ec186f22c539294b2e8a88c7906e624bc00
1 /* CPP Library.
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008,
4 2009, 2010, 2011 Free Software Foundation, Inc.
5 Contributed by Per Bothner, 1994-95.
6 Based on CCCP program by Paul Rubin, June 1986
7 Adapted to ANSI C, Richard Stallman, Jan 1987
9 This program is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 3, or (at your option) any
12 later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "cpplib.h"
26 #include "internal.h"
27 #include "mkdeps.h"
28 #include "localedir.h"
29 #include "filenames.h"
31 static void init_library (void);
32 static void mark_named_operators (cpp_reader *, int);
33 static void read_original_filename (cpp_reader *);
34 static void read_original_directory (cpp_reader *);
35 static void post_options (cpp_reader *);
37 /* If we have designated initializers (GCC >2.7) these tables can be
38 initialized, constant data. Otherwise, they have to be filled in at
39 runtime. */
40 #if HAVE_DESIGNATED_INITIALIZERS
42 #define init_trigraph_map() /* Nothing. */
43 #define TRIGRAPH_MAP \
44 __extension__ const uchar _cpp_trigraph_map[UCHAR_MAX + 1] = {
46 #define END };
47 #define s(p, v) [p] = v,
49 #else
51 #define TRIGRAPH_MAP uchar _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
52 static void init_trigraph_map (void) { \
53 unsigned char *x = _cpp_trigraph_map;
55 #define END }
56 #define s(p, v) x[p] = v;
58 #endif
60 TRIGRAPH_MAP
61 s('=', '#') s(')', ']') s('!', '|')
62 s('(', '[') s('\'', '^') s('>', '}')
63 s('/', '\\') s('<', '{') s('-', '~')
64 END
66 #undef s
67 #undef END
68 #undef TRIGRAPH_MAP
70 /* A set of booleans indicating what CPP features each source language
71 requires. */
72 struct lang_flags
74 char c99;
75 char cplusplus;
76 char extended_numbers;
77 char extended_identifiers;
78 char std;
79 char cplusplus_comments;
80 char digraphs;
81 char uliterals;
82 char rliterals;
83 char user_literals;
86 static const struct lang_flags lang_defaults[] =
87 { /* c99 c++ xnum xid std // digr ulit rlit user_literals */
88 /* GNUC89 */ { 0, 0, 1, 0, 0, 1, 1, 0, 0, 0 },
89 /* GNUC99 */ { 1, 0, 1, 0, 0, 1, 1, 1, 1, 0 },
90 /* GNUC11 */ { 1, 0, 1, 0, 0, 1, 1, 1, 1, 0 },
91 /* STDC89 */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
92 /* STDC94 */ { 0, 0, 0, 0, 1, 0, 1, 0, 0, 0 },
93 /* STDC99 */ { 1, 0, 1, 0, 1, 1, 1, 0, 0, 0 },
94 /* STDC11 */ { 1, 0, 1, 0, 1, 1, 1, 1, 0, 0 },
95 /* UPC */ { 1, 0, 1, 0, 0, 1, 1, 1, 1, 0 },
96 /* GNUCXX */ { 0, 1, 1, 0, 0, 1, 1, 0, 0, 0 },
97 /* CXX98 */ { 0, 1, 1, 0, 1, 1, 1, 0, 0, 0 },
98 /* GNUCXX11 */ { 1, 1, 1, 0, 0, 1, 1, 1, 1, 1 },
99 /* CXX11 */ { 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 },
100 /* ASM */ { 0, 0, 1, 0, 0, 1, 0, 0, 0, 0 }
101 /* xid should be 1 for GNUC99, STDC99, GNUCXX, CXX98, GNUCXX11, and
102 CXX11 when no longer experimental (when all uses of identifiers
103 in the compiler have been audited for correct handling of
104 extended identifiers). */
107 /* Sets internal flags correctly for a given language. */
108 void
109 cpp_set_lang (cpp_reader *pfile, enum c_lang lang)
111 const struct lang_flags *l = &lang_defaults[(int) lang];
113 CPP_OPTION (pfile, lang) = lang;
115 CPP_OPTION (pfile, c99) = l->c99;
116 CPP_OPTION (pfile, cplusplus) = l->cplusplus;
117 CPP_OPTION (pfile, extended_numbers) = l->extended_numbers;
118 CPP_OPTION (pfile, extended_identifiers) = l->extended_identifiers;
119 CPP_OPTION (pfile, std) = l->std;
120 CPP_OPTION (pfile, trigraphs) = l->std;
121 CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments;
122 CPP_OPTION (pfile, digraphs) = l->digraphs;
123 CPP_OPTION (pfile, uliterals) = l->uliterals;
124 CPP_OPTION (pfile, rliterals) = l->rliterals;
125 CPP_OPTION (pfile, user_literals) = l->user_literals;
128 /* Initialize library global state. */
129 static void
130 init_library (void)
132 static int initialized = 0;
134 if (! initialized)
136 initialized = 1;
138 _cpp_init_lexer ();
140 /* Set up the trigraph map. This doesn't need to do anything if
141 we were compiled with a compiler that supports C99 designated
142 initializers. */
143 init_trigraph_map ();
145 #ifdef ENABLE_NLS
146 (void) bindtextdomain (PACKAGE, LOCALEDIR);
147 #endif
151 /* Initialize a cpp_reader structure. */
152 cpp_reader *
153 cpp_create_reader (enum c_lang lang, hash_table *table,
154 struct line_maps *line_table)
156 cpp_reader *pfile;
158 /* Initialize this instance of the library if it hasn't been already. */
159 init_library ();
161 pfile = XCNEW (cpp_reader);
162 memset (&pfile->base_context, 0, sizeof (pfile->base_context));
164 cpp_set_lang (pfile, lang);
165 CPP_OPTION (pfile, warn_multichar) = 1;
166 CPP_OPTION (pfile, discard_comments) = 1;
167 CPP_OPTION (pfile, discard_comments_in_macro_exp) = 1;
168 CPP_OPTION (pfile, tabstop) = 8;
169 CPP_OPTION (pfile, operator_names) = 1;
170 CPP_OPTION (pfile, warn_trigraphs) = 2;
171 CPP_OPTION (pfile, warn_endif_labels) = 1;
172 CPP_OPTION (pfile, cpp_warn_deprecated) = 1;
173 CPP_OPTION (pfile, cpp_warn_long_long) = 0;
174 CPP_OPTION (pfile, dollars_in_ident) = 1;
175 CPP_OPTION (pfile, warn_dollars) = 1;
176 CPP_OPTION (pfile, warn_variadic_macros) = 1;
177 CPP_OPTION (pfile, warn_builtin_macro_redefined) = 1;
178 /* By default, track locations of tokens resulting from macro
179 expansion. The '2' means, track the locations with the highest
180 accuracy. Read the comments for struct
181 cpp_options::track_macro_expansion to learn about the other
182 values. */
183 CPP_OPTION (pfile, track_macro_expansion) = 2;
184 CPP_OPTION (pfile, warn_normalize) = normalized_C;
185 CPP_OPTION (pfile, warn_literal_suffix) = 1;
187 /* Default CPP arithmetic to something sensible for the host for the
188 benefit of dumb users like fix-header. */
189 CPP_OPTION (pfile, precision) = CHAR_BIT * sizeof (long);
190 CPP_OPTION (pfile, char_precision) = CHAR_BIT;
191 CPP_OPTION (pfile, wchar_precision) = CHAR_BIT * sizeof (int);
192 CPP_OPTION (pfile, int_precision) = CHAR_BIT * sizeof (int);
193 CPP_OPTION (pfile, unsigned_char) = 0;
194 CPP_OPTION (pfile, unsigned_wchar) = 1;
195 CPP_OPTION (pfile, bytes_big_endian) = 1; /* does not matter */
197 /* Default to no charset conversion. */
198 CPP_OPTION (pfile, narrow_charset) = _cpp_default_encoding ();
199 CPP_OPTION (pfile, wide_charset) = 0;
201 /* Default the input character set to UTF-8. */
202 CPP_OPTION (pfile, input_charset) = _cpp_default_encoding ();
204 /* A fake empty "directory" used as the starting point for files
205 looked up without a search path. Name cannot be '/' because we
206 don't want to prepend anything at all to filenames using it. All
207 other entries are correct zero-initialized. */
208 pfile->no_search_path.name = (char *) "";
210 /* Initialize the line map. */
211 pfile->line_table = line_table;
213 /* Initialize lexer state. */
214 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
216 /* Set up static tokens. */
217 pfile->avoid_paste.type = CPP_PADDING;
218 pfile->avoid_paste.val.source = NULL;
219 pfile->eof.type = CPP_EOF;
220 pfile->eof.flags = 0;
222 /* Create a token buffer for the lexer. */
223 _cpp_init_tokenrun (&pfile->base_run, 250);
224 pfile->cur_run = &pfile->base_run;
225 pfile->cur_token = pfile->base_run.base;
227 /* Initialize the base context. */
228 pfile->context = &pfile->base_context;
229 pfile->base_context.c.macro = 0;
230 pfile->base_context.prev = pfile->base_context.next = 0;
232 /* Aligned and unaligned storage. */
233 pfile->a_buff = _cpp_get_buff (pfile, 0);
234 pfile->u_buff = _cpp_get_buff (pfile, 0);
236 /* Initialize table for push_macro/pop_macro. */
237 pfile->pushed_macros = 0;
239 /* Do not force token locations by default. */
240 pfile->forced_token_location_p = NULL;
242 /* The expression parser stack. */
243 _cpp_expand_op_stack (pfile);
245 /* Initialize the buffer obstack. */
246 _obstack_begin (&pfile->buffer_ob, 0, 0,
247 (void *(*) (long)) xmalloc,
248 (void (*) (void *)) free);
250 _cpp_init_files (pfile);
252 _cpp_init_hashtable (pfile, table);
254 return pfile;
257 /* Set the line_table entry in PFILE. This is called after reading a
258 PCH file, as the old line_table will be incorrect. */
259 void
260 cpp_set_line_map (cpp_reader *pfile, struct line_maps *line_table)
262 pfile->line_table = line_table;
265 /* Free resources used by PFILE. Accessing PFILE after this function
266 returns leads to undefined behavior. Returns the error count. */
267 void
268 cpp_destroy (cpp_reader *pfile)
270 cpp_context *context, *contextn;
271 struct def_pragma_macro *pmacro;
272 tokenrun *run, *runn;
273 int i;
275 free (pfile->op_stack);
277 while (CPP_BUFFER (pfile) != NULL)
278 _cpp_pop_buffer (pfile);
280 free (pfile->out.base);
282 if (pfile->macro_buffer)
284 free (pfile->macro_buffer);
285 pfile->macro_buffer = NULL;
286 pfile->macro_buffer_len = 0;
289 if (pfile->deps)
290 deps_free (pfile->deps);
291 obstack_free (&pfile->buffer_ob, 0);
293 _cpp_destroy_hashtable (pfile);
294 _cpp_cleanup_files (pfile);
295 _cpp_destroy_iconv (pfile);
297 _cpp_free_buff (pfile->a_buff);
298 _cpp_free_buff (pfile->u_buff);
299 _cpp_free_buff (pfile->free_buffs);
301 for (run = &pfile->base_run; run; run = runn)
303 runn = run->next;
304 free (run->base);
305 if (run != &pfile->base_run)
306 free (run);
309 for (context = pfile->base_context.next; context; context = contextn)
311 contextn = context->next;
312 free (context);
315 if (pfile->comments.entries)
317 for (i = 0; i < pfile->comments.count; i++)
318 free (pfile->comments.entries[i].comment);
320 free (pfile->comments.entries);
322 if (pfile->pushed_macros)
326 pmacro = pfile->pushed_macros;
327 pfile->pushed_macros = pmacro->next;
328 free (pmacro->name);
329 free (pmacro);
331 while (pfile->pushed_macros);
334 free (pfile);
337 /* This structure defines one built-in identifier. A node will be
338 entered in the hash table under the name NAME, with value VALUE.
340 There are two tables of these. builtin_array holds all the
341 "builtin" macros: these are handled by builtin_macro() in
342 macro.c. Builtin is somewhat of a misnomer -- the property of
343 interest is that these macros require special code to compute their
344 expansions. The value is a "cpp_builtin_type" enumerator.
346 operator_array holds the C++ named operators. These are keywords
347 which act as aliases for punctuators. In C++, they cannot be
348 altered through #define, and #if recognizes them as operators. In
349 C, these are not entered into the hash table at all (but see
350 <iso646.h>). The value is a token-type enumerator. */
351 struct builtin_macro
353 const uchar *const name;
354 const unsigned short len;
355 const unsigned short value;
356 const bool always_warn_if_redefined;
359 #define B(n, t, f) { DSC(n), t, f }
360 static const struct builtin_macro builtin_array[] =
362 B("__TIMESTAMP__", BT_TIMESTAMP, false),
363 B("__TIME__", BT_TIME, false),
364 B("__DATE__", BT_DATE, false),
365 B("__FILE__", BT_FILE, false),
366 B("__BASE_FILE__", BT_BASE_FILE, false),
367 B("__LINE__", BT_SPECLINE, true),
368 B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL, true),
369 B("__COUNTER__", BT_COUNTER, true),
370 /* Keep builtins not used for -traditional-cpp at the end, and
371 update init_builtins() if any more are added. */
372 B("_Pragma", BT_PRAGMA, true),
373 B("__STDC__", BT_STDC, true),
375 #undef B
377 struct builtin_operator
379 const uchar *const name;
380 const unsigned short len;
381 const unsigned short value;
384 #define B(n, t) { DSC(n), t }
385 static const struct builtin_operator operator_array[] =
387 B("and", CPP_AND_AND),
388 B("and_eq", CPP_AND_EQ),
389 B("bitand", CPP_AND),
390 B("bitor", CPP_OR),
391 B("compl", CPP_COMPL),
392 B("not", CPP_NOT),
393 B("not_eq", CPP_NOT_EQ),
394 B("or", CPP_OR_OR),
395 B("or_eq", CPP_OR_EQ),
396 B("xor", CPP_XOR),
397 B("xor_eq", CPP_XOR_EQ)
399 #undef B
401 /* Mark the C++ named operators in the hash table. */
402 static void
403 mark_named_operators (cpp_reader *pfile, int flags)
405 const struct builtin_operator *b;
407 for (b = operator_array;
408 b < (operator_array + ARRAY_SIZE (operator_array));
409 b++)
411 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
412 hp->flags |= flags;
413 hp->is_directive = 0;
414 hp->directive_index = b->value;
418 /* Helper function of cpp_type2name. Return the string associated with
419 named operator TYPE. */
420 const char *
421 cpp_named_operator2name (enum cpp_ttype type)
423 const struct builtin_operator *b;
425 for (b = operator_array;
426 b < (operator_array + ARRAY_SIZE (operator_array));
427 b++)
429 if (type == b->value)
430 return (const char *) b->name;
433 return NULL;
436 void
437 cpp_init_special_builtins (cpp_reader *pfile)
439 const struct builtin_macro *b;
440 size_t n = ARRAY_SIZE (builtin_array);
442 if (CPP_OPTION (pfile, traditional))
443 n -= 2;
444 else if (! CPP_OPTION (pfile, stdc_0_in_system_headers)
445 || CPP_OPTION (pfile, std))
446 n--;
448 for (b = builtin_array; b < builtin_array + n; b++)
450 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
451 hp->type = NT_MACRO;
452 hp->flags |= NODE_BUILTIN;
453 if (b->always_warn_if_redefined
454 || CPP_OPTION (pfile, warn_builtin_macro_redefined))
455 hp->flags |= NODE_WARN;
456 hp->value.builtin = (enum cpp_builtin_type) b->value;
460 /* Read the builtins table above and enter them, and language-specific
461 macros, into the hash table. HOSTED is true if this is a hosted
462 environment. */
463 void
464 cpp_init_builtins (cpp_reader *pfile, int hosted)
466 cpp_init_special_builtins (pfile);
468 if (!CPP_OPTION (pfile, traditional)
469 && (! CPP_OPTION (pfile, stdc_0_in_system_headers)
470 || CPP_OPTION (pfile, std)))
471 _cpp_define_builtin (pfile, "__STDC__ 1");
473 if (CPP_OPTION (pfile, cplusplus))
475 if (CPP_OPTION (pfile, lang) == CLK_CXX11
476 || CPP_OPTION (pfile, lang) == CLK_GNUCXX11)
477 _cpp_define_builtin (pfile, "__cplusplus 201103L");
478 else
479 _cpp_define_builtin (pfile, "__cplusplus 199711L");
481 else if (CPP_OPTION (pfile, lang) == CLK_ASM)
482 _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
483 else if (CPP_OPTION (pfile, lang) == CLK_STDC94)
484 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
485 else if (CPP_OPTION (pfile, lang) == CLK_STDC11
486 || CPP_OPTION (pfile, lang) == CLK_GNUC11)
487 _cpp_define_builtin (pfile, "__STDC_VERSION__ 201112L");
488 else if (CPP_OPTION (pfile, c99))
489 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
491 if (CPP_OPTION (pfile, uliterals)
492 && !CPP_OPTION (pfile, cplusplus))
494 _cpp_define_builtin (pfile, "__STDC_UTF_16__ 1");
495 _cpp_define_builtin (pfile, "__STDC_UTF_32__ 1");
498 if (hosted)
499 _cpp_define_builtin (pfile, "__STDC_HOSTED__ 1");
500 else
501 _cpp_define_builtin (pfile, "__STDC_HOSTED__ 0");
503 if (CPP_OPTION (pfile, objc))
504 _cpp_define_builtin (pfile, "__OBJC__ 1");
507 /* Sanity-checks are dependent on command-line options, so it is
508 called as a subroutine of cpp_read_main_file (). */
509 #if ENABLE_CHECKING
510 static void sanity_checks (cpp_reader *);
511 static void sanity_checks (cpp_reader *pfile)
513 cppchar_t test = 0;
514 size_t max_precision = 2 * CHAR_BIT * sizeof (cpp_num_part);
516 /* Sanity checks for assumptions about CPP arithmetic and target
517 type precisions made by cpplib. */
518 test--;
519 if (test < 1)
520 cpp_error (pfile, CPP_DL_ICE, "cppchar_t must be an unsigned type");
522 if (CPP_OPTION (pfile, precision) > max_precision)
523 cpp_error (pfile, CPP_DL_ICE,
524 "preprocessor arithmetic has maximum precision of %lu bits;"
525 " target requires %lu bits",
526 (unsigned long) max_precision,
527 (unsigned long) CPP_OPTION (pfile, precision));
529 if (CPP_OPTION (pfile, precision) < CPP_OPTION (pfile, int_precision))
530 cpp_error (pfile, CPP_DL_ICE,
531 "CPP arithmetic must be at least as precise as a target int");
533 if (CPP_OPTION (pfile, char_precision) < 8)
534 cpp_error (pfile, CPP_DL_ICE, "target char is less than 8 bits wide");
536 if (CPP_OPTION (pfile, wchar_precision) < CPP_OPTION (pfile, char_precision))
537 cpp_error (pfile, CPP_DL_ICE,
538 "target wchar_t is narrower than target char");
540 if (CPP_OPTION (pfile, int_precision) < CPP_OPTION (pfile, char_precision))
541 cpp_error (pfile, CPP_DL_ICE,
542 "target int is narrower than target char");
544 /* This is assumed in eval_token() and could be fixed if necessary. */
545 if (sizeof (cppchar_t) > sizeof (cpp_num_part))
546 cpp_error (pfile, CPP_DL_ICE,
547 "CPP half-integer narrower than CPP character");
549 if (CPP_OPTION (pfile, wchar_precision) > BITS_PER_CPPCHAR_T)
550 cpp_error (pfile, CPP_DL_ICE,
551 "CPP on this host cannot handle wide character constants over"
552 " %lu bits, but the target requires %lu bits",
553 (unsigned long) BITS_PER_CPPCHAR_T,
554 (unsigned long) CPP_OPTION (pfile, wchar_precision));
556 #else
557 # define sanity_checks(PFILE)
558 #endif
560 /* This is called after options have been parsed, and partially
561 processed. */
562 void
563 cpp_post_options (cpp_reader *pfile)
565 int flags;
567 sanity_checks (pfile);
569 post_options (pfile);
571 /* Mark named operators before handling command line macros. */
572 flags = 0;
573 if (CPP_OPTION (pfile, cplusplus) && CPP_OPTION (pfile, operator_names))
574 flags |= NODE_OPERATOR;
575 if (CPP_OPTION (pfile, warn_cxx_operator_names))
576 flags |= NODE_DIAGNOSTIC | NODE_WARN_OPERATOR;
577 if (flags != 0)
578 mark_named_operators (pfile, flags);
581 /* Setup for processing input from the file named FNAME, or stdin if
582 it is the empty string. Return the original filename
583 on success (e.g. foo.i->foo.c), or NULL on failure. */
584 const char *
585 cpp_read_main_file (cpp_reader *pfile, const char *fname)
587 if (CPP_OPTION (pfile, deps.style) != DEPS_NONE)
589 if (!pfile->deps)
590 pfile->deps = deps_init ();
592 /* Set the default target (if there is none already). */
593 deps_add_default_target (pfile->deps, fname);
596 pfile->main_file
597 = _cpp_find_file (pfile, fname, &pfile->no_search_path, false, 0);
598 if (_cpp_find_failed (pfile->main_file))
599 return NULL;
601 _cpp_stack_file (pfile, pfile->main_file, false);
603 /* For foo.i, read the original filename foo.c now, for the benefit
604 of the front ends. */
605 if (CPP_OPTION (pfile, preprocessed))
607 read_original_filename (pfile);
608 fname =
609 ORDINARY_MAP_FILE_NAME
610 ((LINEMAPS_LAST_ORDINARY_MAP (pfile->line_table)));
612 return fname;
615 /* For preprocessed files, if the first tokens are of the form # NUM.
616 handle the directive so we know the original file name. This will
617 generate file_change callbacks, which the front ends must handle
618 appropriately given their state of initialization. */
619 static void
620 read_original_filename (cpp_reader *pfile)
622 const cpp_token *token, *token1;
624 /* Lex ahead; if the first tokens are of the form # NUM, then
625 process the directive, otherwise back up. */
626 token = _cpp_lex_direct (pfile);
627 if (token->type == CPP_HASH)
629 pfile->state.in_directive = 1;
630 token1 = _cpp_lex_direct (pfile);
631 _cpp_backup_tokens (pfile, 1);
632 pfile->state.in_directive = 0;
634 /* If it's a #line directive, handle it. */
635 if (token1->type == CPP_NUMBER
636 && _cpp_handle_directive (pfile, token->flags & PREV_WHITE))
638 read_original_directory (pfile);
639 return;
643 /* Backup as if nothing happened. */
644 _cpp_backup_tokens (pfile, 1);
647 /* For preprocessed files, if the tokens following the first filename
648 line is of the form # <line> "/path/name//", handle the
649 directive so we know the original current directory. */
650 static void
651 read_original_directory (cpp_reader *pfile)
653 const cpp_token *hash, *token;
655 /* Lex ahead; if the first tokens are of the form # NUM, then
656 process the directive, otherwise back up. */
657 hash = _cpp_lex_direct (pfile);
658 if (hash->type != CPP_HASH)
660 _cpp_backup_tokens (pfile, 1);
661 return;
664 token = _cpp_lex_direct (pfile);
666 if (token->type != CPP_NUMBER)
668 _cpp_backup_tokens (pfile, 2);
669 return;
672 token = _cpp_lex_direct (pfile);
674 if (token->type != CPP_STRING
675 || ! (token->val.str.len >= 5
676 && IS_DIR_SEPARATOR (token->val.str.text[token->val.str.len-2])
677 && IS_DIR_SEPARATOR (token->val.str.text[token->val.str.len-3])))
679 _cpp_backup_tokens (pfile, 3);
680 return;
683 if (pfile->cb.dir_change)
685 char *debugdir = (char *) alloca (token->val.str.len - 3);
687 memcpy (debugdir, (const char *) token->val.str.text + 1,
688 token->val.str.len - 4);
689 debugdir[token->val.str.len - 4] = '\0';
691 pfile->cb.dir_change (pfile, debugdir);
695 /* This is called at the end of preprocessing. It pops the last
696 buffer and writes dependency output.
698 Maybe it should also reset state, such that you could call
699 cpp_start_read with a new filename to restart processing. */
700 void
701 cpp_finish (cpp_reader *pfile, FILE *deps_stream)
703 /* Warn about unused macros before popping the final buffer. */
704 if (CPP_OPTION (pfile, warn_unused_macros))
705 cpp_forall_identifiers (pfile, _cpp_warn_if_unused_macro, NULL);
707 /* lex.c leaves the final buffer on the stack. This it so that
708 it returns an unending stream of CPP_EOFs to the client. If we
709 popped the buffer, we'd dereference a NULL buffer pointer and
710 segfault. It's nice to allow the client to do worry-free excess
711 cpp_get_token calls. */
712 while (pfile->buffer)
713 _cpp_pop_buffer (pfile);
715 if (CPP_OPTION (pfile, deps.style) != DEPS_NONE
716 && deps_stream)
718 deps_write (pfile->deps, deps_stream, 72);
720 if (CPP_OPTION (pfile, deps.phony_targets))
721 deps_phony_targets (pfile->deps, deps_stream);
724 /* Report on headers that could use multiple include guards. */
725 if (CPP_OPTION (pfile, print_include_names))
726 _cpp_report_missing_guards (pfile);
729 static void
730 post_options (cpp_reader *pfile)
732 /* -Wtraditional is not useful in C++ mode. */
733 if (CPP_OPTION (pfile, cplusplus))
734 CPP_OPTION (pfile, cpp_warn_traditional) = 0;
736 /* Permanently disable macro expansion if we are rescanning
737 preprocessed text. Read preprocesed source in ISO mode. */
738 if (CPP_OPTION (pfile, preprocessed))
740 if (!CPP_OPTION (pfile, directives_only))
741 pfile->state.prevent_expansion = 1;
742 CPP_OPTION (pfile, traditional) = 0;
745 if (CPP_OPTION (pfile, warn_trigraphs) == 2)
746 CPP_OPTION (pfile, warn_trigraphs) = !CPP_OPTION (pfile, trigraphs);
748 if (CPP_OPTION (pfile, traditional))
750 CPP_OPTION (pfile, cplusplus_comments) = 0;
752 CPP_OPTION (pfile, trigraphs) = 0;
753 CPP_OPTION (pfile, warn_trigraphs) = 0;