2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 Contributed by Per Bothner, 1994-95.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
30 #include "cppdefault.h"
32 /* Windows does not natively support inodes, and neither does MSDOS.
33 Cygwin's emulation can generate non-unique inodes, so don't use it.
34 VMS has non-numeric inodes. */
36 # define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
37 # define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
39 # if (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
40 # define INO_T_EQ(A, B) 0
42 # define INO_T_EQ(A, B) ((A) == (B))
44 # define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
47 /* Internal structures and prototypes. */
49 /* A `struct pending_option' remembers one -D, -A, -U, -include, or
51 typedef void (* cl_directive_handler) PARAMS ((cpp_reader *, const char *));
54 struct pending_option *next;
56 cl_directive_handler handler;
59 /* The `pending' structure accumulates all the options that are not
60 actually processed until we hit cpp_read_main_file. It consists of
61 several lists, one for each type of option. We keep both head and
62 tail pointers for quick insertion. */
65 struct pending_option *directive_head, *directive_tail;
67 struct search_path *quote_head, *quote_tail;
68 struct search_path *brack_head, *brack_tail;
69 struct search_path *systm_head, *systm_tail;
70 struct search_path *after_head, *after_tail;
72 struct pending_option *imacros_head, *imacros_tail;
73 struct pending_option *include_head, *include_tail;
77 #define APPEND(pend, list, elt) \
78 do { if (!(pend)->list##_head) (pend)->list##_head = (elt); \
79 else (pend)->list##_tail->next = (elt); \
80 (pend)->list##_tail = (elt); \
83 #define APPEND(pend, list, elt) \
84 do { if (!(pend)->list/**/_head) (pend)->list/**/_head = (elt); \
85 else (pend)->list/**/_tail->next = (elt); \
86 (pend)->list/**/_tail = (elt); \
90 static void path_include PARAMS ((cpp_reader *,
92 static void init_library PARAMS ((void));
93 static void init_builtins PARAMS ((cpp_reader *));
94 static void mark_named_operators PARAMS ((cpp_reader *));
95 static void append_include_chain PARAMS ((cpp_reader *,
97 static struct search_path * remove_dup_dir PARAMS ((cpp_reader *,
98 struct search_path *));
99 static struct search_path * remove_dup_dirs PARAMS ((cpp_reader *,
100 struct search_path *));
101 static void merge_include_chains PARAMS ((cpp_reader *));
102 static bool push_include PARAMS ((cpp_reader *,
103 struct pending_option *));
104 static void free_chain PARAMS ((struct pending_option *));
105 static void init_dependency_output PARAMS ((cpp_reader *));
106 static void init_standard_includes PARAMS ((cpp_reader *));
107 static void read_original_filename PARAMS ((cpp_reader *));
108 static void new_pending_directive PARAMS ((struct cpp_pending *,
110 cl_directive_handler));
111 static void output_deps PARAMS ((cpp_reader *));
112 static int parse_option PARAMS ((const char *));
114 /* Fourth argument to append_include_chain: chain to use.
115 Note it's never asked to append to the quote chain. */
116 enum { BRACKET = 0, SYSTEM, AFTER };
118 /* If we have designated initializers (GCC >2.7) these tables can be
119 initialized, constant data. Otherwise, they have to be filled in at
121 #if HAVE_DESIGNATED_INITIALIZERS
123 #define init_trigraph_map() /* Nothing. */
124 #define TRIGRAPH_MAP \
125 __extension__ const uchar _cpp_trigraph_map[UCHAR_MAX + 1] = {
128 #define s(p, v) [p] = v,
132 #define TRIGRAPH_MAP uchar _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
133 static void init_trigraph_map PARAMS ((void)) { \
134 unsigned char *x = _cpp_trigraph_map;
137 #define s(p, v) x[p] = v;
142 s('=', '#') s(')', ']') s('!', '|')
143 s('(', '[') s('\'', '^') s('>', '}')
144 s('/', '\\') s('<', '{') s('-', '~')
151 /* Given a colon-separated list of file names PATH,
152 add all the names to the search path for include files. */
154 path_include (pfile, list, path)
165 /* Find the end of this name. */
167 while (*q != 0 && *q != PATH_SEPARATOR) q++;
170 /* An empty name in the path stands for the current directory. */
171 name = (char *) xmalloc (2);
177 /* Otherwise use the directory that is named. */
178 name = (char *) xmalloc (q - p + 1);
179 memcpy (name, p, q - p);
183 append_include_chain (pfile, name, path, 0);
185 /* Advance past this name. */
193 /* Append DIR to include path PATH. DIR must be allocated on the
194 heap; this routine takes responsibility for freeing it. CXX_AWARE
195 is non-zero if the header contains extern "C" guards for C++,
196 otherwise it is zero. */
198 append_include_chain (pfile, dir, path, cxx_aware)
204 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
205 struct search_path *new;
214 _cpp_simplify_pathname (dir);
218 /* Dirs that don't exist are silently ignored. */
220 cpp_errno (pfile, DL_ERROR, dir);
221 else if (CPP_OPTION (pfile, verbose))
222 fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"), dir);
227 if (!S_ISDIR (st.st_mode))
229 cpp_error_with_line (pfile, DL_ERROR, 0, 0, "%s: Not a directory", dir);
235 if (len > pfile->max_include_len)
236 pfile->max_include_len = len;
238 new = (struct search_path *) xmalloc (sizeof (struct search_path));
241 INO_T_COPY (new->ino, st.st_ino);
242 new->dev = st.st_dev;
243 /* Both systm and after include file lists should be treated as system
244 include files since these two lists are really just a concatenation
245 of one "system" list. */
246 if (path == SYSTEM || path == AFTER)
247 new->sysp = cxx_aware ? 1 : 2;
250 new->name_map = NULL;
255 case BRACKET: APPEND (pend, brack, new); break;
256 case SYSTEM: APPEND (pend, systm, new); break;
257 case AFTER: APPEND (pend, after, new); break;
261 /* Handle a duplicated include path. PREV is the link in the chain
262 before the duplicate. The duplicate is removed from the chain and
263 freed. Returns PREV. */
264 static struct search_path *
265 remove_dup_dir (pfile, prev)
267 struct search_path *prev;
269 struct search_path *cur = prev->next;
271 if (CPP_OPTION (pfile, verbose))
272 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), cur->name);
274 prev->next = cur->next;
275 free ((PTR) cur->name);
281 /* Remove duplicate directories from a chain. Returns the tail of the
282 chain, or NULL if the chain is empty. This algorithm is quadratic
283 in the number of -I switches, which is acceptable since there
284 aren't usually that many of them. */
285 static struct search_path *
286 remove_dup_dirs (pfile, head)
288 struct search_path *head;
290 struct search_path *prev = NULL, *cur, *other;
292 for (cur = head; cur; cur = cur->next)
294 for (other = head; other != cur; other = other->next)
295 if (INO_T_EQ (cur->ino, other->ino) && cur->dev == other->dev)
297 if (cur->sysp && !other->sysp)
299 cpp_error (pfile, DL_WARNING,
300 "changing search order for system directory \"%s\"",
302 if (strcmp (cur->name, other->name))
303 cpp_error (pfile, DL_WARNING,
304 " as it is the same as non-system directory \"%s\"",
307 cpp_error (pfile, DL_WARNING,
308 " as it has already been specified as a non-system directory");
310 cur = remove_dup_dir (pfile, prev);
319 /* Merge the four include chains together in the order quote, bracket,
320 system, after. Remove duplicate dirs (as determined by
321 INO_T_EQ()). The system_include and after_include chains are never
322 referred to again after this function; all access is through the
323 bracket_include path. */
325 merge_include_chains (pfile)
328 struct search_path *quote, *brack, *systm, *qtail;
330 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
332 quote = pend->quote_head;
333 brack = pend->brack_head;
334 systm = pend->systm_head;
335 qtail = pend->quote_tail;
337 /* Paste together bracket, system, and after include chains. */
339 pend->systm_tail->next = pend->after_head;
341 systm = pend->after_head;
344 pend->brack_tail->next = systm;
348 /* This is a bit tricky. First we drop dupes from the quote-include
349 list. Then we drop dupes from the bracket-include list.
350 Finally, if qtail and brack are the same directory, we cut out
351 brack and move brack up to point to qtail.
353 We can't just merge the lists and then uniquify them because
354 then we may lose directories from the <> search path that should
355 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
356 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
357 -Ibar -I- -Ifoo -Iquux. */
359 remove_dup_dirs (pfile, brack);
360 qtail = remove_dup_dirs (pfile, quote);
366 /* If brack == qtail, remove brack as it's simpler. */
367 if (brack && INO_T_EQ (qtail->ino, brack->ino)
368 && qtail->dev == brack->dev)
369 brack = remove_dup_dir (pfile, qtail);
374 CPP_OPTION (pfile, quote_include) = quote;
375 CPP_OPTION (pfile, bracket_include) = brack;
378 /* A set of booleans indicating what CPP features each source language
384 char extended_numbers;
386 char dollars_in_ident;
387 char cplusplus_comments;
391 /* ??? Enable $ in identifiers in assembly? */
392 static const struct lang_flags lang_defaults[] =
393 { /* c99 c++ xnum std dollar c++comm digr */
394 /* GNUC89 */ { 0, 0, 1, 0, 1, 1, 1 },
395 /* GNUC99 */ { 1, 0, 1, 0, 1, 1, 1 },
396 /* STDC89 */ { 0, 0, 0, 1, 0, 0, 0 },
397 /* STDC94 */ { 0, 0, 0, 1, 0, 0, 1 },
398 /* STDC99 */ { 1, 0, 1, 1, 0, 1, 1 },
399 /* GNUCXX */ { 0, 1, 1, 0, 1, 1, 1 },
400 /* CXX98 */ { 0, 1, 1, 1, 0, 1, 1 },
401 /* ASM */ { 0, 0, 1, 0, 0, 1, 0 }
404 /* Sets internal flags correctly for a given language. */
406 cpp_set_lang (pfile, lang)
410 const struct lang_flags *l = &lang_defaults[(int) lang];
412 CPP_OPTION (pfile, lang) = lang;
414 CPP_OPTION (pfile, c99) = l->c99;
415 CPP_OPTION (pfile, cplusplus) = l->cplusplus;
416 CPP_OPTION (pfile, extended_numbers) = l->extended_numbers;
417 CPP_OPTION (pfile, std) = l->std;
418 CPP_OPTION (pfile, trigraphs) = l->std;
419 CPP_OPTION (pfile, dollars_in_ident) = l->dollars_in_ident;
420 CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments;
421 CPP_OPTION (pfile, digraphs) = l->digraphs;
425 static int opt_comp PARAMS ((const void *, const void *));
427 /* Run-time sorting of options array. */
432 return strcmp (((struct cl_option *) p1)->opt_text,
433 ((struct cl_option *) p2)->opt_text);
437 /* init initializes library global state. It might not need to
438 do anything depending on the platform and compiler. */
442 static int initialized = 0;
449 /* For non-ASCII hosts, the cl_options array needs to be sorted at
451 qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
454 /* Set up the trigraph map. This doesn't need to do anything if
455 we were compiled with a compiler that supports C99 designated
457 init_trigraph_map ();
461 /* Initialize a cpp_reader structure. */
463 cpp_create_reader (lang)
468 /* Initialise this instance of the library if it hasn't been already. */
471 pfile = (cpp_reader *) xcalloc (1, sizeof (cpp_reader));
473 cpp_set_lang (pfile, lang);
474 CPP_OPTION (pfile, warn_import) = 1;
475 CPP_OPTION (pfile, warn_multichar) = 1;
476 CPP_OPTION (pfile, discard_comments) = 1;
477 CPP_OPTION (pfile, discard_comments_in_macro_exp) = 1;
478 CPP_OPTION (pfile, show_column) = 1;
479 CPP_OPTION (pfile, tabstop) = 8;
480 CPP_OPTION (pfile, operator_names) = 1;
481 CPP_OPTION (pfile, warn_endif_labels) = 1;
483 CPP_OPTION (pfile, pending) =
484 (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
486 /* Default CPP arithmetic to something sensible for the host for the
487 benefit of dumb users like fix-header. */
488 CPP_OPTION (pfile, precision) = CHAR_BIT * sizeof (long);
489 CPP_OPTION (pfile, char_precision) = CHAR_BIT;
490 CPP_OPTION (pfile, wchar_precision) = CHAR_BIT * sizeof (int);
491 CPP_OPTION (pfile, int_precision) = CHAR_BIT * sizeof (int);
492 CPP_OPTION (pfile, unsigned_char) = 0;
493 CPP_OPTION (pfile, unsigned_wchar) = 1;
495 /* It's simplest to just create this struct whether or not it will
497 pfile->deps = deps_init ();
499 /* Initialise the line map. Start at logical line 1, so we can use
500 a line number of zero for special states. */
501 init_line_maps (&pfile->line_maps);
504 /* Initialize lexer state. */
505 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
507 /* Set up static tokens. */
508 pfile->avoid_paste.type = CPP_PADDING;
509 pfile->avoid_paste.val.source = NULL;
510 pfile->eof.type = CPP_EOF;
511 pfile->eof.flags = 0;
513 /* Create a token buffer for the lexer. */
514 _cpp_init_tokenrun (&pfile->base_run, 250);
515 pfile->cur_run = &pfile->base_run;
516 pfile->cur_token = pfile->base_run.base;
518 /* Initialise the base context. */
519 pfile->context = &pfile->base_context;
520 pfile->base_context.macro = 0;
521 pfile->base_context.prev = pfile->base_context.next = 0;
523 /* Aligned and unaligned storage. */
524 pfile->a_buff = _cpp_get_buff (pfile, 0);
525 pfile->u_buff = _cpp_get_buff (pfile, 0);
527 /* The expression parser stack. */
528 _cpp_expand_op_stack (pfile);
530 /* Initialise the buffer obstack. */
531 gcc_obstack_init (&pfile->buffer_ob);
533 _cpp_init_includes (pfile);
538 /* Free resources used by PFILE. Accessing PFILE after this function
539 returns leads to undefined behaviour. Returns the error count. */
545 struct search_path *dir, *dirn;
546 cpp_context *context, *contextn;
547 tokenrun *run, *runn;
549 free_chain (CPP_OPTION (pfile, pending)->include_head);
550 free (CPP_OPTION (pfile, pending));
551 free (pfile->op_stack);
553 while (CPP_BUFFER (pfile) != NULL)
554 _cpp_pop_buffer (pfile);
557 free (pfile->out.base);
559 if (pfile->macro_buffer)
561 free ((PTR) pfile->macro_buffer);
562 pfile->macro_buffer = NULL;
563 pfile->macro_buffer_len = 0;
566 deps_free (pfile->deps);
567 obstack_free (&pfile->buffer_ob, 0);
569 _cpp_destroy_hashtable (pfile);
570 _cpp_cleanup_includes (pfile);
572 _cpp_free_buff (pfile->a_buff);
573 _cpp_free_buff (pfile->u_buff);
574 _cpp_free_buff (pfile->free_buffs);
576 for (run = &pfile->base_run; run; run = runn)
580 if (run != &pfile->base_run)
584 for (dir = CPP_OPTION (pfile, quote_include); dir; dir = dirn)
587 free ((PTR) dir->name);
591 for (context = pfile->base_context.next; context; context = contextn)
593 contextn = context->next;
597 free_line_maps (&pfile->line_maps);
599 result = pfile->errors;
605 /* This structure defines one built-in identifier. A node will be
606 entered in the hash table under the name NAME, with value VALUE.
608 There are two tables of these. builtin_array holds all the
609 "builtin" macros: these are handled by builtin_macro() in
610 cppmacro.c. Builtin is somewhat of a misnomer -- the property of
611 interest is that these macros require special code to compute their
612 expansions. The value is a "builtin_type" enumerator.
614 operator_array holds the C++ named operators. These are keywords
615 which act as aliases for punctuators. In C++, they cannot be
616 altered through #define, and #if recognizes them as operators. In
617 C, these are not entered into the hash table at all (but see
618 <iso646.h>). The value is a token-type enumerator. */
623 unsigned short value;
626 #define B(n, t) { DSC(n), t }
627 static const struct builtin builtin_array[] =
629 B("__TIME__", BT_TIME),
630 B("__DATE__", BT_DATE),
631 B("__FILE__", BT_FILE),
632 B("__BASE_FILE__", BT_BASE_FILE),
633 B("__LINE__", BT_SPECLINE),
634 B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL),
635 /* Keep builtins not used for -traditional-cpp at the end, and
636 update init_builtins() if any more are added. */
637 B("_Pragma", BT_PRAGMA),
638 B("__STDC__", BT_STDC),
641 static const struct builtin operator_array[] =
643 B("and", CPP_AND_AND),
644 B("and_eq", CPP_AND_EQ),
645 B("bitand", CPP_AND),
647 B("compl", CPP_COMPL),
649 B("not_eq", CPP_NOT_EQ),
651 B("or_eq", CPP_OR_EQ),
653 B("xor_eq", CPP_XOR_EQ)
657 /* Mark the C++ named operators in the hash table. */
659 mark_named_operators (pfile)
662 const struct builtin *b;
664 for (b = operator_array;
665 b < (operator_array + ARRAY_SIZE (operator_array));
668 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
669 hp->flags |= NODE_OPERATOR;
670 hp->value.operator = b->value;
674 /* Subroutine of cpp_read_main_file; reads the builtins table above and
675 enters them, and language-specific macros, into the hash table. */
677 init_builtins (pfile)
680 const struct builtin *b;
681 size_t n = ARRAY_SIZE (builtin_array);
683 if (CPP_OPTION (pfile, traditional))
686 for(b = builtin_array; b < builtin_array + n; b++)
688 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
690 hp->flags |= NODE_BUILTIN | NODE_WARN;
691 hp->value.builtin = b->value;
694 if (CPP_OPTION (pfile, cplusplus))
695 _cpp_define_builtin (pfile, "__cplusplus 1");
696 else if (CPP_OPTION (pfile, lang) == CLK_ASM)
697 _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
698 else if (CPP_OPTION (pfile, lang) == CLK_STDC94)
699 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
700 else if (CPP_OPTION (pfile, c99))
701 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
703 if (CPP_OPTION (pfile, objc))
704 _cpp_define_builtin (pfile, "__OBJC__ 1");
706 if (pfile->cb.register_builtins)
707 (*pfile->cb.register_builtins) (pfile);
710 /* And another subroutine. This one sets up the standard include path. */
712 init_standard_includes (pfile)
716 const struct default_include *p;
717 const char *specd_prefix = CPP_OPTION (pfile, include_prefix);
719 /* Several environment variables may add to the include search path.
720 CPATH specifies an additional list of directories to be searched
721 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
722 etc. specify an additional list of directories to be searched as
723 if specified with -isystem, for the language indicated. */
725 GET_ENVIRONMENT (path, "CPATH");
726 if (path != 0 && *path != 0)
727 path_include (pfile, path, BRACKET);
729 switch ((CPP_OPTION (pfile, objc) << 1) + CPP_OPTION (pfile, cplusplus))
732 GET_ENVIRONMENT (path, "C_INCLUDE_PATH");
735 GET_ENVIRONMENT (path, "CPLUS_INCLUDE_PATH");
738 GET_ENVIRONMENT (path, "OBJC_INCLUDE_PATH");
741 GET_ENVIRONMENT (path, "OBJCPLUS_INCLUDE_PATH");
744 if (path != 0 && *path != 0)
745 path_include (pfile, path, SYSTEM);
747 /* Search "translated" versions of GNU directories.
748 These have /usr/local/lib/gcc... replaced by specd_prefix. */
749 if (specd_prefix != 0 && cpp_GCC_INCLUDE_DIR_len)
751 /* Remove the `include' from /usr/local/lib/gcc.../include.
752 GCC_INCLUDE_DIR will always end in /include. */
753 int default_len = cpp_GCC_INCLUDE_DIR_len;
754 char *default_prefix = (char *) alloca (default_len + 1);
755 int specd_len = strlen (specd_prefix);
757 memcpy (default_prefix, cpp_GCC_INCLUDE_DIR, default_len);
758 default_prefix[default_len] = '\0';
760 for (p = cpp_include_defaults; p->fname; p++)
762 /* Some standard dirs are only for C++. */
764 || (CPP_OPTION (pfile, cplusplus)
765 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
767 /* Does this dir start with the prefix? */
768 if (!memcmp (p->fname, default_prefix, default_len))
770 /* Yes; change prefix and add to search list. */
771 int flen = strlen (p->fname);
772 int this_len = specd_len + flen - default_len;
773 char *str = (char *) xmalloc (this_len + 1);
774 memcpy (str, specd_prefix, specd_len);
775 memcpy (str + specd_len,
776 p->fname + default_len,
777 flen - default_len + 1);
779 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
785 /* Search ordinary names for GNU include directories. */
786 for (p = cpp_include_defaults; p->fname; p++)
788 /* Some standard dirs are only for C++. */
790 || (CPP_OPTION (pfile, cplusplus)
791 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
793 char *str = update_path (p->fname, p->component);
794 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
799 /* Pushes a command line -imacro and -include file indicated by P onto
800 the buffer stack. Returns non-zero if successful. */
802 push_include (pfile, p)
804 struct pending_option *p;
808 /* Later: maybe update this to use the #include "" search path
809 if cpp_read_file fails. */
810 header.type = CPP_STRING;
811 header.val.str.text = (const unsigned char *) p->arg;
812 header.val.str.len = strlen (p->arg);
813 /* Make the command line directive take up a line. */
816 return _cpp_execute_include (pfile, &header, IT_CMDLINE);
819 /* Frees a pending_option chain. */
822 struct pending_option *head;
824 struct pending_option *next;
834 /* Sanity-checks are dependent on command-line options, so it is
835 called as a subroutine of cpp_read_main_file (). */
837 static void sanity_checks PARAMS ((cpp_reader *));
838 static void sanity_checks (pfile)
842 size_t max_precision = 2 * CHAR_BIT * sizeof (cpp_num_part);
844 /* Sanity checks for assumptions about CPP arithmetic and target
845 type precisions made by cpplib. */
848 cpp_error (pfile, DL_ICE, "cppchar_t must be an unsigned type");
850 if (CPP_OPTION (pfile, precision) > max_precision)
851 cpp_error (pfile, DL_ICE,
852 "preprocessor arithmetic has maximum precision of %lu bits; target requires %lu bits",
853 (unsigned long) max_precision,
854 (unsigned long) CPP_OPTION (pfile, precision));
856 if (CPP_OPTION (pfile, precision) < CPP_OPTION (pfile, int_precision))
857 cpp_error (pfile, DL_ICE,
858 "CPP arithmetic must be at least as precise as a target int");
860 if (CPP_OPTION (pfile, char_precision) < 8)
861 cpp_error (pfile, DL_ICE, "target char is less than 8 bits wide");
863 if (CPP_OPTION (pfile, wchar_precision) < CPP_OPTION (pfile, char_precision))
864 cpp_error (pfile, DL_ICE,
865 "target wchar_t is narrower than target char");
867 if (CPP_OPTION (pfile, int_precision) < CPP_OPTION (pfile, char_precision))
868 cpp_error (pfile, DL_ICE,
869 "target int is narrower than target char");
871 /* This is assumed in eval_token() and could be fixed if necessary. */
872 if (sizeof (cppchar_t) > sizeof (cpp_num_part))
873 cpp_error (pfile, DL_ICE, "CPP half-integer narrower than CPP character");
875 if (CPP_OPTION (pfile, wchar_precision) > BITS_PER_CPPCHAR_T)
876 cpp_error (pfile, DL_ICE,
877 "CPP on this host cannot handle wide character constants over %lu bits, but the target requires %lu bits",
878 (unsigned long) BITS_PER_CPPCHAR_T,
879 (unsigned long) CPP_OPTION (pfile, wchar_precision));
882 # define sanity_checks(PFILE)
885 /* This is called after options have been parsed, and partially
886 processed. Setup for processing input from the file named FNAME,
887 or stdin if it is the empty string. Return the original filename
888 on success (e.g. foo.i->foo.c), or NULL on failure. */
890 cpp_read_main_file (pfile, fname, table)
895 sanity_checks (pfile);
897 /* The front ends don't set up the hash table until they have
898 finished processing the command line options, so initializing the
899 hashtable is deferred until now. */
900 _cpp_init_hashtable (pfile, table);
902 /* Set up the include search path now. */
903 if (! CPP_OPTION (pfile, no_standard_includes))
904 init_standard_includes (pfile);
906 merge_include_chains (pfile);
908 /* With -v, print the list of dirs to search. */
909 if (CPP_OPTION (pfile, verbose))
911 struct search_path *l;
912 fprintf (stderr, _("#include \"...\" search starts here:\n"));
913 for (l = CPP_OPTION (pfile, quote_include); l; l = l->next)
915 if (l == CPP_OPTION (pfile, bracket_include))
916 fprintf (stderr, _("#include <...> search starts here:\n"));
917 fprintf (stderr, " %s\n", l->name);
919 fprintf (stderr, _("End of search list.\n"));
922 if (CPP_OPTION (pfile, print_deps))
923 /* Set the default target (if there is none already). */
924 deps_add_default_target (pfile->deps, fname);
926 /* Open the main input file. */
927 if (!_cpp_read_file (pfile, fname))
930 /* Set this after cpp_post_options so the client can change the
931 option if it wishes, and after stacking the main file so we don't
932 trace the main file. */
933 pfile->line_maps.trace_includes = CPP_OPTION (pfile, print_include_names);
935 /* For foo.i, read the original filename foo.c now, for the benefit
936 of the front ends. */
937 if (CPP_OPTION (pfile, preprocessed))
938 read_original_filename (pfile);
939 /* Overlay an empty buffer to seed traditional preprocessing. */
940 else if (CPP_OPTION (pfile, traditional)
941 && !CPP_OPTION (pfile, preprocess_only))
942 _cpp_overlay_buffer (pfile, U"", 0);
944 return pfile->map->to_file;
947 /* For preprocessed files, if the first tokens are of the form # NUM.
948 handle the directive so we know the original file name. This will
949 generate file_change callbacks, which the front ends must handle
950 appropriately given their state of initialization. */
952 read_original_filename (pfile)
955 const cpp_token *token, *token1;
957 /* Lex ahead; if the first tokens are of the form # NUM, then
958 process the directive, otherwise back up. */
959 token = _cpp_lex_direct (pfile);
960 if (token->type == CPP_HASH)
962 token1 = _cpp_lex_direct (pfile);
963 _cpp_backup_tokens (pfile, 1);
965 /* If it's a #line directive, handle it. */
966 if (token1->type == CPP_NUMBER)
968 _cpp_handle_directive (pfile, token->flags & PREV_WHITE);
973 /* Backup as if nothing happened. */
974 _cpp_backup_tokens (pfile, 1);
977 /* Handle pending command line options: -D, -U, -A, -imacros and
978 -include. This should be called after debugging has been properly
979 set up in the front ends. */
981 cpp_finish_options (pfile)
984 /* Mark named operators before handling command line macros. */
985 if (CPP_OPTION (pfile, cplusplus) && CPP_OPTION (pfile, operator_names))
986 mark_named_operators (pfile);
988 /* Install builtins and process command line macros etc. in the order
989 they appeared, but only if not already preprocessed. */
990 if (! CPP_OPTION (pfile, preprocessed))
992 struct pending_option *p;
994 _cpp_do_file_change (pfile, LC_RENAME, _("<built-in>"), 1, 0);
995 init_builtins (pfile);
996 _cpp_do_file_change (pfile, LC_RENAME, _("<command line>"), 1, 0);
997 for (p = CPP_OPTION (pfile, pending)->directive_head; p; p = p->next)
998 (*p->handler) (pfile, p->arg);
1000 /* Scan -imacros files after -D, -U, but before -include.
1001 pfile->next_include_file is NULL, so _cpp_pop_buffer does not
1002 push -include files. */
1003 for (p = CPP_OPTION (pfile, pending)->imacros_head; p; p = p->next)
1004 if (push_include (pfile, p))
1005 cpp_scan_nooutput (pfile);
1007 pfile->next_include_file = &CPP_OPTION (pfile, pending)->include_head;
1008 _cpp_maybe_push_include_file (pfile);
1011 pfile->first_unused_line = pfile->line;
1013 free_chain (CPP_OPTION (pfile, pending)->imacros_head);
1014 free_chain (CPP_OPTION (pfile, pending)->directive_head);
1017 /* Push the next buffer on the stack given by -include, if any. */
1019 _cpp_maybe_push_include_file (pfile)
1022 if (pfile->next_include_file)
1024 struct pending_option *head = *pfile->next_include_file;
1026 while (head && !push_include (pfile, head))
1030 pfile->next_include_file = &head->next;
1033 /* All done; restore the line map from <command line>. */
1034 _cpp_do_file_change (pfile, LC_RENAME,
1035 pfile->line_maps.maps[0].to_file, 1, 0);
1036 /* Don't come back here again. */
1037 pfile->next_include_file = NULL;
1042 /* Use mkdeps.c to output dependency information. */
1047 /* Stream on which to print the dependency information. */
1048 FILE *deps_stream = 0;
1049 const char *const deps_mode =
1050 CPP_OPTION (pfile, print_deps_append) ? "a" : "w";
1052 if (CPP_OPTION (pfile, deps_file)[0] == '\0')
1053 deps_stream = stdout;
1056 deps_stream = fopen (CPP_OPTION (pfile, deps_file), deps_mode);
1057 if (deps_stream == 0)
1059 cpp_errno (pfile, DL_ERROR, CPP_OPTION (pfile, deps_file));
1064 deps_write (pfile->deps, deps_stream, 72);
1066 if (CPP_OPTION (pfile, deps_phony_targets))
1067 deps_phony_targets (pfile->deps, deps_stream);
1069 /* Don't close stdout. */
1070 if (deps_stream != stdout)
1072 if (ferror (deps_stream) || fclose (deps_stream) != 0)
1073 cpp_error (pfile, DL_ERROR, "I/O error on output");
1077 /* This is called at the end of preprocessing. It pops the
1078 last buffer and writes dependency output. It should also
1079 clear macro definitions, such that you could call cpp_start_read
1080 with a new filename to restart processing. */
1085 /* Warn about unused macros before popping the final buffer. */
1086 if (CPP_OPTION (pfile, warn_unused_macros))
1087 cpp_forall_identifiers (pfile, _cpp_warn_if_unused_macro, NULL);
1089 /* cpplex.c leaves the final buffer on the stack. This it so that
1090 it returns an unending stream of CPP_EOFs to the client. If we
1091 popped the buffer, we'd dereference a NULL buffer pointer and
1092 segfault. It's nice to allow the client to do worry-free excess
1093 cpp_get_token calls. */
1094 while (pfile->buffer)
1095 _cpp_pop_buffer (pfile);
1097 /* Don't write the deps file if preprocessing has failed. */
1098 if (CPP_OPTION (pfile, print_deps) && pfile->errors == 0)
1099 output_deps (pfile);
1101 /* Report on headers that could use multiple include guards. */
1102 if (CPP_OPTION (pfile, print_include_names))
1103 _cpp_report_missing_guards (pfile);
1106 /* Add a directive to be handled later in the initialization phase. */
1108 new_pending_directive (pend, text, handler)
1109 struct cpp_pending *pend;
1111 cl_directive_handler handler;
1113 struct pending_option *o = (struct pending_option *)
1114 xmalloc (sizeof (struct pending_option));
1118 o->handler = handler;
1119 APPEND (pend, directive, o);
1122 /* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
1123 I.e. a const string initializer with parens around it. That is
1124 what N_("string") resolves to, so we make no_* be macros instead. */
1125 #define no_ass N_("assertion missing after %s")
1126 #define no_dir N_("directory name missing after %s")
1127 #define no_fil N_("file name missing after %s")
1128 #define no_mac N_("macro name missing after %s")
1129 #define no_pth N_("path name missing after %s")
1130 #define no_tgt N_("target missing after %s")
1132 /* This is the list of all command line options, with the leading
1133 "-" removed. It must be sorted in ASCII collating order. */
1134 #define COMMAND_LINE_OPTIONS \
1135 DEF_OPT("A", no_ass, OPT_A) \
1136 DEF_OPT("D", no_mac, OPT_D) \
1137 DEF_OPT("I", no_dir, OPT_I) \
1138 DEF_OPT("M", 0, OPT_M) \
1139 DEF_OPT("MD", no_fil, OPT_MD) \
1140 DEF_OPT("MF", no_fil, OPT_MF) \
1141 DEF_OPT("MG", 0, OPT_MG) \
1142 DEF_OPT("MM", 0, OPT_MM) \
1143 DEF_OPT("MMD", no_fil, OPT_MMD) \
1144 DEF_OPT("MP", 0, OPT_MP) \
1145 DEF_OPT("MQ", no_tgt, OPT_MQ) \
1146 DEF_OPT("MT", no_tgt, OPT_MT) \
1147 DEF_OPT("U", no_mac, OPT_U) \
1148 DEF_OPT("idirafter", no_dir, OPT_idirafter) \
1149 DEF_OPT("imacros", no_fil, OPT_imacros) \
1150 DEF_OPT("include", no_fil, OPT_include) \
1151 DEF_OPT("iprefix", no_pth, OPT_iprefix) \
1152 DEF_OPT("isystem", no_dir, OPT_isystem) \
1153 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix) \
1154 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore)
1156 #define DEF_OPT(text, msg, code) code,
1159 COMMAND_LINE_OPTIONS
1166 const char *opt_text;
1169 enum opt_code opt_code;
1172 #define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
1174 static struct cl_option cl_options[] =
1176 static const struct cl_option cl_options[] =
1179 COMMAND_LINE_OPTIONS
1182 #undef COMMAND_LINE_OPTIONS
1184 /* Perform a binary search to find which, if any, option the given
1185 command-line matches. Returns its index in the option array,
1186 negative on failure. Complications arise since some options can be
1187 suffixed with an argument, and multiple complete matches can occur,
1188 e.g. -pedantic and -pedantic-errors. */
1190 parse_option (input)
1193 unsigned int md, mn, mx;
1204 opt_len = cl_options[md].opt_len;
1205 comp = memcmp (input, cl_options[md].opt_text, opt_len);
1213 if (input[opt_len] == '\0')
1215 /* We were passed more text. If the option takes an argument,
1216 we may match a later option or we may have been passed the
1217 argument. The longest possible option match succeeds.
1218 If the option takes no arguments we have not matched and
1219 continue the search (e.g. input="stdc++" match was "stdc"). */
1221 if (cl_options[md].msg)
1223 /* Scan forwards. If we get an exact match, return it.
1224 Otherwise, return the longest option-accepting match.
1225 This loops no more than twice with current options. */
1227 for (; mn < (unsigned int) N_OPTS; mn++)
1229 opt_len = cl_options[mn].opt_len;
1230 if (memcmp (input, cl_options[mn].opt_text, opt_len))
1232 if (input[opt_len] == '\0')
1234 if (cl_options[mn].msg)
1245 /* Handle one command-line option in (argc, argv).
1246 Can be called multiple times, to handle multiple sets of options.
1247 Returns number of strings consumed. */
1249 cpp_handle_option (pfile, argc, argv)
1255 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
1258 enum opt_code opt_code;
1260 const char *arg = 0;
1262 /* Skip over '-'. */
1263 opt_index = parse_option (&argv[i][1]);
1267 opt_code = cl_options[opt_index].opt_code;
1268 if (cl_options[opt_index].msg)
1270 arg = &argv[i][cl_options[opt_index].opt_len + 1];
1276 cpp_error (pfile, DL_ERROR,
1277 cl_options[opt_index].msg, argv[i - 1]);
1285 case N_OPTS: /* Shut GCC up. */
1289 new_pending_directive (pend, arg, cpp_define);
1292 CPP_OPTION (pfile, include_prefix) = arg;
1293 CPP_OPTION (pfile, include_prefix_len) = strlen (arg);
1297 CPP_OPTION (pfile, print_deps_missing_files) = 1;
1300 /* When doing dependencies with -M or -MM, suppress normal
1301 preprocessed output, but still do -dM etc. as software
1302 depends on this. Preprocessed output occurs if -MD, -MMD
1303 or environment var dependency generation is used. */
1304 CPP_OPTION (pfile, print_deps) = 2;
1305 CPP_OPTION (pfile, no_output) = 1;
1306 CPP_OPTION (pfile, inhibit_warnings) = 1;
1309 CPP_OPTION (pfile, print_deps) = 1;
1310 CPP_OPTION (pfile, no_output) = 1;
1311 CPP_OPTION (pfile, inhibit_warnings) = 1;
1314 CPP_OPTION (pfile, deps_file) = arg;
1317 CPP_OPTION (pfile, deps_phony_targets) = 1;
1321 /* Add a target. -MQ quotes for Make. */
1322 deps_add_target (pfile->deps, arg, opt_code == OPT_MQ);
1326 CPP_OPTION (pfile, print_deps) = 2;
1327 CPP_OPTION (pfile, deps_file) = arg;
1330 CPP_OPTION (pfile, print_deps) = 1;
1331 CPP_OPTION (pfile, deps_file) = arg;
1337 /* -A with an argument beginning with '-' acts as
1338 #unassert on whatever immediately follows the '-'.
1339 If "-" is the whole argument, we eliminate all
1340 predefined macros and assertions, including those
1341 that were specified earlier on the command line.
1342 That way we can get rid of any that were passed
1343 automatically in from GCC. */
1347 free_chain (pend->directive_head);
1348 pend->directive_head = NULL;
1349 pend->directive_tail = NULL;
1352 new_pending_directive (pend, arg + 1, cpp_unassert);
1355 new_pending_directive (pend, arg, cpp_assert);
1358 new_pending_directive (pend, arg, cpp_undef);
1360 case OPT_I: /* Add directory to path for includes. */
1361 if (!strcmp (arg, "-"))
1364 Use the preceding -I directories for #include "..."
1365 but not #include <...>.
1366 Don't search the directory of the present file
1367 for #include "...". (Note that -I. -I- is not the same as
1368 the default setup; -I. uses the compiler's working dir.) */
1369 if (! CPP_OPTION (pfile, ignore_srcdir))
1371 pend->quote_head = pend->brack_head;
1372 pend->quote_tail = pend->brack_tail;
1373 pend->brack_head = 0;
1374 pend->brack_tail = 0;
1375 CPP_OPTION (pfile, ignore_srcdir) = 1;
1379 cpp_error (pfile, DL_ERROR, "-I- specified twice");
1384 append_include_chain (pfile, xstrdup (arg), BRACKET, 0);
1387 /* Add directory to beginning of system include path, as a system
1388 include directory. */
1389 append_include_chain (pfile, xstrdup (arg), SYSTEM, 0);
1394 struct pending_option *o = (struct pending_option *)
1395 xmalloc (sizeof (struct pending_option));
1399 if (opt_code == OPT_include)
1400 APPEND (pend, include, o);
1402 APPEND (pend, imacros, o);
1405 case OPT_iwithprefix:
1406 /* Add directory to end of path for includes,
1407 with the default prefix at the front of its name. */
1409 case OPT_iwithprefixbefore:
1410 /* Add directory to main path for includes,
1411 with the default prefix at the front of its name. */
1418 if (CPP_OPTION (pfile, include_prefix) != 0)
1420 size_t ipl = CPP_OPTION (pfile, include_prefix_len);
1421 fname = xmalloc (ipl + len + 1);
1422 memcpy (fname, CPP_OPTION (pfile, include_prefix), ipl);
1423 memcpy (fname + ipl, arg, len + 1);
1425 else if (cpp_GCC_INCLUDE_DIR_len)
1427 fname = xmalloc (cpp_GCC_INCLUDE_DIR_len + len + 1);
1428 memcpy (fname, cpp_GCC_INCLUDE_DIR, cpp_GCC_INCLUDE_DIR_len);
1429 memcpy (fname + cpp_GCC_INCLUDE_DIR_len, arg, len + 1);
1432 fname = xstrdup (arg);
1434 append_include_chain (pfile, fname,
1435 opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0);
1439 /* Add directory to end of path for includes. */
1440 append_include_chain (pfile, xstrdup (arg), AFTER, 0);
1447 /* Handle command-line options in (argc, argv).
1448 Can be called multiple times, to handle multiple sets of options.
1449 Returns if an unrecognized option is seen.
1450 Returns number of strings consumed. */
1452 cpp_handle_options (pfile, argc, argv)
1458 int strings_processed;
1460 for (i = 0; i < argc; i += strings_processed)
1462 strings_processed = cpp_handle_option (pfile, argc - i, argv + i);
1463 if (strings_processed == 0)
1470 /* Extra processing when all options are parsed, after all calls to
1471 cpp_handle_option[s]. Consistency checks etc. */
1473 cpp_post_options (pfile)
1476 /* Canonicalize in_fname and out_fname. We guarantee they are not
1477 NULL, and that the empty string represents stdin / stdout. */
1478 if (CPP_OPTION (pfile, in_fname) == NULL
1479 || !strcmp (CPP_OPTION (pfile, in_fname), "-"))
1480 CPP_OPTION (pfile, in_fname) = "";
1482 if (CPP_OPTION (pfile, out_fname) == NULL
1483 || !strcmp (CPP_OPTION (pfile, out_fname), "-"))
1484 CPP_OPTION (pfile, out_fname) = "";
1486 /* -Wtraditional is not useful in C++ mode. */
1487 if (CPP_OPTION (pfile, cplusplus))
1488 CPP_OPTION (pfile, warn_traditional) = 0;
1490 /* The compiler front ends override this, but I think this is the
1491 appropriate setting for the library. */
1492 CPP_OPTION (pfile, warn_long_long)
1493 = ((CPP_OPTION (pfile, pedantic) && !CPP_OPTION (pfile, c99))
1494 || CPP_OPTION (pfile, warn_traditional));
1496 /* Permanently disable macro expansion if we are rescanning
1497 preprocessed text. Read preprocesed source in ISO mode. */
1498 if (CPP_OPTION (pfile, preprocessed))
1500 pfile->state.prevent_expansion = 1;
1501 CPP_OPTION (pfile, traditional) = 0;
1504 /* Traditional CPP does not accurately track column information. */
1505 if (CPP_OPTION (pfile, traditional))
1506 CPP_OPTION (pfile, show_column) = 0;
1508 /* -dM and dependencies suppress normal output; do it here so that
1509 the last -d[MDN] switch overrides earlier ones. */
1510 if (CPP_OPTION (pfile, dump_macros) == dump_only)
1511 CPP_OPTION (pfile, no_output) = 1;
1513 /* Disable -dD, -dN and -dI if normal output is suppressed. Allow
1514 -dM since at least glibc relies on -M -dM to work. */
1515 if (CPP_OPTION (pfile, no_output))
1517 if (CPP_OPTION (pfile, dump_macros) != dump_only)
1518 CPP_OPTION (pfile, dump_macros) = dump_none;
1519 CPP_OPTION (pfile, dump_includes) = 0;
1522 /* Intialize, and check environment variables for, dependency
1524 init_dependency_output (pfile);
1526 /* If we're not outputting dependencies, complain if other -M
1527 options have been given. */
1528 if (!CPP_OPTION (pfile, print_deps)
1529 && (CPP_OPTION (pfile, print_deps_missing_files)
1530 || CPP_OPTION (pfile, deps_file)
1531 || CPP_OPTION (pfile, deps_phony_targets)))
1532 cpp_error (pfile, DL_ERROR,
1533 "you must additionally specify either -M or -MM");
1536 /* Set up dependency-file output. On exit, if print_deps is non-zero
1537 then deps_file is not NULL; stdout is the empty string. */
1539 init_dependency_output (pfile)
1542 char *spec, *s, *output_file;
1544 /* Either of two environment variables can specify output of deps.
1545 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1546 where OUTPUT_FILE is the file to write deps info to
1547 and DEPS_TARGET is the target to mention in the deps. */
1549 if (CPP_OPTION (pfile, print_deps) == 0)
1551 spec = getenv ("DEPENDENCIES_OUTPUT");
1553 CPP_OPTION (pfile, print_deps) = 1;
1556 spec = getenv ("SUNPRO_DEPENDENCIES");
1558 CPP_OPTION (pfile, print_deps) = 2;
1563 /* Find the space before the DEPS_TARGET, if there is one. */
1564 s = strchr (spec, ' ');
1567 /* Let the caller perform MAKE quoting. */
1568 deps_add_target (pfile->deps, s + 1, 0);
1569 output_file = (char *) xmalloc (s - spec + 1);
1570 memcpy (output_file, spec, s - spec);
1571 output_file[s - spec] = 0;
1576 /* Command line -MF overrides environment variables and default. */
1577 if (CPP_OPTION (pfile, deps_file) == 0)
1578 CPP_OPTION (pfile, deps_file) = output_file;
1580 CPP_OPTION (pfile, print_deps_append) = 1;
1582 else if (CPP_OPTION (pfile, deps_file) == 0)
1583 /* If -M or -MM was seen without -MF, default output to wherever
1584 was specified with -o. out_fname is non-NULL here. */
1585 CPP_OPTION (pfile, deps_file) = CPP_OPTION (pfile, out_fname);