* config/m68k/netbsd-elf.h (LONG_DOUBLE_TYPE_SIZE): Compute
[official-gcc.git] / gcc / cppinit.c
blob41842cb38329fc69fe16c7a6b1f33e409c5931aa
1 /* CPP Library.
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
11 later version.
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. */
22 #include "config.h"
23 #include "system.h"
24 #include "cpplib.h"
25 #include "cpphash.h"
26 #include "prefix.h"
27 #include "intl.h"
28 #include "version.h"
29 #include "mkdeps.h"
30 #include "cppdefault.h"
32 /* Predefined symbols, built-in macros, and the default include path. */
34 #ifndef GET_ENV_PATH_LIST
35 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
36 #endif
38 /* Windows does not natively support inodes, and neither does MSDOS.
39 Cygwin's emulation can generate non-unique inodes, so don't use it.
40 VMS has non-numeric inodes. */
41 #ifdef VMS
42 # define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
43 # define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
44 #else
45 # if (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
46 # define INO_T_EQ(A, B) 0
47 # else
48 # define INO_T_EQ(A, B) ((A) == (B))
49 # endif
50 # define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
51 #endif
53 /* Internal structures and prototypes. */
55 /* A `struct pending_option' remembers one -D, -A, -U, -include, or
56 -imacros switch. */
57 typedef void (* cl_directive_handler) PARAMS ((cpp_reader *, const char *));
58 struct pending_option
60 struct pending_option *next;
61 const char *arg;
62 cl_directive_handler handler;
65 /* The `pending' structure accumulates all the options that are not
66 actually processed until we hit cpp_read_main_file. It consists of
67 several lists, one for each type of option. We keep both head and
68 tail pointers for quick insertion. */
69 struct cpp_pending
71 struct pending_option *directive_head, *directive_tail;
73 struct search_path *quote_head, *quote_tail;
74 struct search_path *brack_head, *brack_tail;
75 struct search_path *systm_head, *systm_tail;
76 struct search_path *after_head, *after_tail;
78 struct pending_option *imacros_head, *imacros_tail;
79 struct pending_option *include_head, *include_tail;
82 #ifdef __STDC__
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); \
87 } while (0)
88 #else
89 #define APPEND(pend, list, elt) \
90 do { if (!(pend)->list/**/_head) (pend)->list/**/_head = (elt); \
91 else (pend)->list/**/_tail->next = (elt); \
92 (pend)->list/**/_tail = (elt); \
93 } while (0)
94 #endif
96 static void print_help PARAMS ((void));
97 static void path_include PARAMS ((cpp_reader *,
98 char *, int));
99 static void init_library PARAMS ((void));
100 static void init_builtins PARAMS ((cpp_reader *));
101 static void mark_named_operators PARAMS ((cpp_reader *));
102 static void append_include_chain PARAMS ((cpp_reader *,
103 char *, int, int));
104 static struct search_path * remove_dup_dir PARAMS ((cpp_reader *,
105 struct search_path *));
106 static struct search_path * remove_dup_dirs PARAMS ((cpp_reader *,
107 struct search_path *));
108 static void merge_include_chains PARAMS ((cpp_reader *));
109 static bool push_include PARAMS ((cpp_reader *,
110 struct pending_option *));
111 static void free_chain PARAMS ((struct pending_option *));
112 static void set_lang PARAMS ((cpp_reader *, enum c_lang));
113 static void init_dependency_output PARAMS ((cpp_reader *));
114 static void init_standard_includes PARAMS ((cpp_reader *));
115 static void read_original_filename PARAMS ((cpp_reader *));
116 static void new_pending_directive PARAMS ((struct cpp_pending *,
117 const char *,
118 cl_directive_handler));
119 static void output_deps PARAMS ((cpp_reader *));
120 static int parse_option PARAMS ((const char *));
122 /* Fourth argument to append_include_chain: chain to use.
123 Note it's never asked to append to the quote chain. */
124 enum { BRACKET = 0, SYSTEM, AFTER };
126 /* If we have designated initializers (GCC >2.7) these tables can be
127 initialized, constant data. Otherwise, they have to be filled in at
128 runtime. */
129 #if HAVE_DESIGNATED_INITIALIZERS
131 #define init_trigraph_map() /* Nothing. */
132 #define TRIGRAPH_MAP \
133 __extension__ const uchar _cpp_trigraph_map[UCHAR_MAX + 1] = {
135 #define END };
136 #define s(p, v) [p] = v,
138 #else
140 #define TRIGRAPH_MAP uchar _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
141 static void init_trigraph_map PARAMS ((void)) { \
142 unsigned char *x = _cpp_trigraph_map;
144 #define END }
145 #define s(p, v) x[p] = v;
147 #endif
149 TRIGRAPH_MAP
150 s('=', '#') s(')', ']') s('!', '|')
151 s('(', '[') s('\'', '^') s('>', '}')
152 s('/', '\\') s('<', '{') s('-', '~')
155 #undef s
156 #undef END
157 #undef TRIGRAPH_MAP
159 /* Given a colon-separated list of file names PATH,
160 add all the names to the search path for include files. */
161 static void
162 path_include (pfile, list, path)
163 cpp_reader *pfile;
164 char *list;
165 int path;
167 char *p, *q, *name;
169 p = list;
173 /* Find the end of this name. */
174 q = p;
175 while (*q != 0 && *q != PATH_SEPARATOR) q++;
176 if (q == p)
178 /* An empty name in the path stands for the current directory. */
179 name = (char *) xmalloc (2);
180 name[0] = '.';
181 name[1] = 0;
183 else
185 /* Otherwise use the directory that is named. */
186 name = (char *) xmalloc (q - p + 1);
187 memcpy (name, p, q - p);
188 name[q - p] = 0;
191 append_include_chain (pfile, name, path, 0);
193 /* Advance past this name. */
194 if (*q == 0)
195 break;
196 p = q + 1;
198 while (1);
201 /* Append DIR to include path PATH. DIR must be allocated on the
202 heap; this routine takes responsibility for freeing it. CXX_AWARE
203 is non-zero if the header contains extern "C" guards for C++,
204 otherwise it is zero. */
205 static void
206 append_include_chain (pfile, dir, path, cxx_aware)
207 cpp_reader *pfile;
208 char *dir;
209 int path;
210 int cxx_aware ATTRIBUTE_UNUSED;
212 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
213 struct search_path *new;
214 struct stat st;
215 unsigned int len;
217 if (*dir == '\0')
219 free (dir);
220 dir = xstrdup (".");
222 _cpp_simplify_pathname (dir);
224 if (stat (dir, &st))
226 /* Dirs that don't exist are silently ignored. */
227 if (errno != ENOENT)
228 cpp_errno (pfile, DL_ERROR, dir);
229 else if (CPP_OPTION (pfile, verbose))
230 fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"), dir);
231 free (dir);
232 return;
235 if (!S_ISDIR (st.st_mode))
237 cpp_error_with_line (pfile, DL_ERROR, 0, 0, "%s: Not a directory", dir);
238 free (dir);
239 return;
242 len = strlen (dir);
243 if (len > pfile->max_include_len)
244 pfile->max_include_len = len;
246 new = (struct search_path *) xmalloc (sizeof (struct search_path));
247 new->name = dir;
248 new->len = len;
249 INO_T_COPY (new->ino, st.st_ino);
250 new->dev = st.st_dev;
251 /* Both systm and after include file lists should be treated as system
252 include files since these two lists are really just a concatenation
253 of one "system" list. */
254 if (path == SYSTEM || path == AFTER)
255 #ifdef NO_IMPLICIT_EXTERN_C
256 new->sysp = 1;
257 #else
258 new->sysp = cxx_aware ? 1 : 2;
259 #endif
260 else
261 new->sysp = 0;
262 new->name_map = NULL;
263 new->next = NULL;
265 switch (path)
267 case BRACKET: APPEND (pend, brack, new); break;
268 case SYSTEM: APPEND (pend, systm, new); break;
269 case AFTER: APPEND (pend, after, new); break;
273 /* Handle a duplicated include path. PREV is the link in the chain
274 before the duplicate. The duplicate is removed from the chain and
275 freed. Returns PREV. */
276 static struct search_path *
277 remove_dup_dir (pfile, prev)
278 cpp_reader *pfile;
279 struct search_path *prev;
281 struct search_path *cur = prev->next;
283 if (CPP_OPTION (pfile, verbose))
284 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), cur->name);
286 prev->next = cur->next;
287 free ((PTR) cur->name);
288 free (cur);
290 return prev;
293 /* Remove duplicate directories from a chain. Returns the tail of the
294 chain, or NULL if the chain is empty. This algorithm is quadratic
295 in the number of -I switches, which is acceptable since there
296 aren't usually that many of them. */
297 static struct search_path *
298 remove_dup_dirs (pfile, head)
299 cpp_reader *pfile;
300 struct search_path *head;
302 struct search_path *prev = NULL, *cur, *other;
304 for (cur = head; cur; cur = cur->next)
306 for (other = head; other != cur; other = other->next)
307 if (INO_T_EQ (cur->ino, other->ino) && cur->dev == other->dev)
309 if (cur->sysp && !other->sysp)
311 cpp_error (pfile, DL_WARNING,
312 "changing search order for system directory \"%s\"",
313 cur->name);
314 if (strcmp (cur->name, other->name))
315 cpp_error (pfile, DL_WARNING,
316 " as it is the same as non-system directory \"%s\"",
317 other->name);
318 else
319 cpp_error (pfile, DL_WARNING,
320 " as it has already been specified as a non-system directory");
322 cur = remove_dup_dir (pfile, prev);
323 break;
325 prev = cur;
328 return prev;
331 /* Merge the four include chains together in the order quote, bracket,
332 system, after. Remove duplicate dirs (as determined by
333 INO_T_EQ()). The system_include and after_include chains are never
334 referred to again after this function; all access is through the
335 bracket_include path. */
336 static void
337 merge_include_chains (pfile)
338 cpp_reader *pfile;
340 struct search_path *quote, *brack, *systm, *qtail;
342 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
344 quote = pend->quote_head;
345 brack = pend->brack_head;
346 systm = pend->systm_head;
347 qtail = pend->quote_tail;
349 /* Paste together bracket, system, and after include chains. */
350 if (systm)
351 pend->systm_tail->next = pend->after_head;
352 else
353 systm = pend->after_head;
355 if (brack)
356 pend->brack_tail->next = systm;
357 else
358 brack = systm;
360 /* This is a bit tricky. First we drop dupes from the quote-include
361 list. Then we drop dupes from the bracket-include list.
362 Finally, if qtail and brack are the same directory, we cut out
363 brack and move brack up to point to qtail.
365 We can't just merge the lists and then uniquify them because
366 then we may lose directories from the <> search path that should
367 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
368 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
369 -Ibar -I- -Ifoo -Iquux. */
371 remove_dup_dirs (pfile, brack);
372 qtail = remove_dup_dirs (pfile, quote);
374 if (quote)
376 qtail->next = brack;
378 /* If brack == qtail, remove brack as it's simpler. */
379 if (brack && INO_T_EQ (qtail->ino, brack->ino)
380 && qtail->dev == brack->dev)
381 brack = remove_dup_dir (pfile, qtail);
383 else
384 quote = brack;
386 CPP_OPTION (pfile, quote_include) = quote;
387 CPP_OPTION (pfile, bracket_include) = brack;
390 /* A set of booleans indicating what CPP features each source language
391 requires. */
392 struct lang_flags
394 char c99;
395 char cplusplus;
396 char extended_numbers;
397 char trigraphs;
398 char dollars_in_ident;
399 char cplusplus_comments;
400 char digraphs;
403 /* ??? Enable $ in identifiers in assembly? */
404 static const struct lang_flags lang_defaults[] =
405 { /* c99 c++ xnum trig dollar c++comm digr */
406 /* GNUC89 */ { 0, 0, 1, 0, 1, 1, 1 },
407 /* GNUC99 */ { 1, 0, 1, 0, 1, 1, 1 },
408 /* STDC89 */ { 0, 0, 0, 1, 0, 0, 0 },
409 /* STDC94 */ { 0, 0, 0, 1, 0, 0, 1 },
410 /* STDC99 */ { 1, 0, 1, 1, 0, 1, 1 },
411 /* GNUCXX */ { 0, 1, 1, 0, 1, 1, 1 },
412 /* CXX98 */ { 0, 1, 1, 1, 0, 1, 1 },
413 /* ASM */ { 0, 0, 1, 0, 0, 1, 0 }
416 /* Sets internal flags correctly for a given language. */
417 static void
418 set_lang (pfile, lang)
419 cpp_reader *pfile;
420 enum c_lang lang;
422 const struct lang_flags *l = &lang_defaults[(int) lang];
424 CPP_OPTION (pfile, lang) = lang;
426 CPP_OPTION (pfile, c99) = l->c99;
427 CPP_OPTION (pfile, cplusplus) = l->cplusplus;
428 CPP_OPTION (pfile, extended_numbers) = l->extended_numbers;
429 CPP_OPTION (pfile, trigraphs) = l->trigraphs;
430 CPP_OPTION (pfile, dollars_in_ident) = l->dollars_in_ident;
431 CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments;
432 CPP_OPTION (pfile, digraphs) = l->digraphs;
435 #ifdef HOST_EBCDIC
436 static int opt_comp PARAMS ((const void *, const void *));
438 /* Run-time sorting of options array. */
439 static int
440 opt_comp (p1, p2)
441 const void *p1, *p2;
443 return strcmp (((struct cl_option *) p1)->opt_text,
444 ((struct cl_option *) p2)->opt_text);
446 #endif
448 /* init initializes library global state. It might not need to
449 do anything depending on the platform and compiler. */
450 static void
451 init_library ()
453 static int initialized = 0;
455 if (! initialized)
457 initialized = 1;
459 #ifdef HOST_EBCDIC
460 /* For non-ASCII hosts, the cl_options array needs to be sorted at
461 runtime. */
462 qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
463 #endif
465 /* Set up the trigraph map. This doesn't need to do anything if
466 we were compiled with a compiler that supports C99 designated
467 initializers. */
468 init_trigraph_map ();
472 /* Initialize a cpp_reader structure. */
473 cpp_reader *
474 cpp_create_reader (lang)
475 enum c_lang lang;
477 cpp_reader *pfile;
479 /* Initialise this instance of the library if it hasn't been already. */
480 init_library ();
482 pfile = (cpp_reader *) xcalloc (1, sizeof (cpp_reader));
484 set_lang (pfile, lang);
485 CPP_OPTION (pfile, warn_import) = 1;
486 CPP_OPTION (pfile, warn_multichar) = 1;
487 CPP_OPTION (pfile, discard_comments) = 1;
488 CPP_OPTION (pfile, discard_comments_in_macro_exp) = 1;
489 CPP_OPTION (pfile, show_column) = 1;
490 CPP_OPTION (pfile, tabstop) = 8;
491 CPP_OPTION (pfile, operator_names) = 1;
492 CPP_OPTION (pfile, warn_endif_labels) = 1;
494 CPP_OPTION (pfile, pending) =
495 (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
497 /* Default CPP arithmetic to something sensible for the host for the
498 benefit of dumb users like fix-header. */
499 CPP_OPTION (pfile, precision) = CHAR_BIT * sizeof (long);
500 CPP_OPTION (pfile, char_precision) = CHAR_BIT;
501 CPP_OPTION (pfile, wchar_precision) = CHAR_BIT * sizeof (int);
502 CPP_OPTION (pfile, int_precision) = CHAR_BIT * sizeof (int);
503 CPP_OPTION (pfile, unsigned_char) = 0;
504 CPP_OPTION (pfile, unsigned_wchar) = 1;
506 /* It's simplest to just create this struct whether or not it will
507 be needed. */
508 pfile->deps = deps_init ();
510 /* Initialise the line map. Start at logical line 1, so we can use
511 a line number of zero for special states. */
512 init_line_maps (&pfile->line_maps);
513 pfile->trad_line = pfile->line = 1;
515 /* Initialize lexer state. */
516 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
518 /* Set up static tokens. */
519 pfile->date.type = CPP_EOF;
520 pfile->avoid_paste.type = CPP_PADDING;
521 pfile->avoid_paste.val.source = NULL;
522 pfile->eof.type = CPP_EOF;
523 pfile->eof.flags = 0;
525 /* Create a token buffer for the lexer. */
526 _cpp_init_tokenrun (&pfile->base_run, 250);
527 pfile->cur_run = &pfile->base_run;
528 pfile->cur_token = pfile->base_run.base;
530 /* Initialise the base context. */
531 pfile->context = &pfile->base_context;
532 pfile->base_context.macro = 0;
533 pfile->base_context.prev = pfile->base_context.next = 0;
535 /* Aligned and unaligned storage. */
536 pfile->a_buff = _cpp_get_buff (pfile, 0);
537 pfile->u_buff = _cpp_get_buff (pfile, 0);
539 /* The expression parser stack. */
540 _cpp_expand_op_stack (pfile);
542 /* Initialise the buffer obstack. */
543 gcc_obstack_init (&pfile->buffer_ob);
545 _cpp_init_includes (pfile);
547 return pfile;
550 /* Free resources used by PFILE. Accessing PFILE after this function
551 returns leads to undefined behaviour. Returns the error count. */
553 cpp_destroy (pfile)
554 cpp_reader *pfile;
556 int result;
557 struct search_path *dir, *dirn;
558 cpp_context *context, *contextn;
559 tokenrun *run, *runn;
561 free_chain (CPP_OPTION (pfile, pending)->include_head);
562 free (CPP_OPTION (pfile, pending));
563 free (pfile->op_stack);
565 while (CPP_BUFFER (pfile) != NULL)
566 _cpp_pop_buffer (pfile);
568 if (pfile->trad_out_base)
569 free (pfile->trad_out_base);
571 if (pfile->macro_buffer)
573 free ((PTR) pfile->macro_buffer);
574 pfile->macro_buffer = NULL;
575 pfile->macro_buffer_len = 0;
578 deps_free (pfile->deps);
579 obstack_free (&pfile->buffer_ob, 0);
581 _cpp_destroy_hashtable (pfile);
582 _cpp_cleanup_includes (pfile);
584 _cpp_free_buff (pfile->a_buff);
585 _cpp_free_buff (pfile->u_buff);
586 _cpp_free_buff (pfile->free_buffs);
588 for (run = &pfile->base_run; run; run = runn)
590 runn = run->next;
591 free (run->base);
592 if (run != &pfile->base_run)
593 free (run);
596 for (dir = CPP_OPTION (pfile, quote_include); dir; dir = dirn)
598 dirn = dir->next;
599 free ((PTR) dir->name);
600 free (dir);
603 for (context = pfile->base_context.next; context; context = contextn)
605 contextn = context->next;
606 free (context);
609 free_line_maps (&pfile->line_maps);
611 result = pfile->errors;
612 free (pfile);
614 return result;
617 /* This structure defines one built-in identifier. A node will be
618 entered in the hash table under the name NAME, with value VALUE.
620 There are two tables of these. builtin_array holds all the
621 "builtin" macros: these are handled by builtin_macro() in
622 cppmacro.c. Builtin is somewhat of a misnomer -- the property of
623 interest is that these macros require special code to compute their
624 expansions. The value is a "builtin_type" enumerator.
626 operator_array holds the C++ named operators. These are keywords
627 which act as aliases for punctuators. In C++, they cannot be
628 altered through #define, and #if recognizes them as operators. In
629 C, these are not entered into the hash table at all (but see
630 <iso646.h>). The value is a token-type enumerator. */
631 struct builtin
633 const uchar *name;
634 unsigned short len;
635 unsigned short value;
638 #define B(n, t) { DSC(n), t }
639 static const struct builtin builtin_array[] =
641 B("__TIME__", BT_TIME),
642 B("__DATE__", BT_DATE),
643 B("__FILE__", BT_FILE),
644 B("__BASE_FILE__", BT_BASE_FILE),
645 B("__LINE__", BT_SPECLINE),
646 B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL),
647 B("_Pragma", BT_PRAGMA),
648 B("__STDC__", BT_STDC),
651 static const struct builtin operator_array[] =
653 B("and", CPP_AND_AND),
654 B("and_eq", CPP_AND_EQ),
655 B("bitand", CPP_AND),
656 B("bitor", CPP_OR),
657 B("compl", CPP_COMPL),
658 B("not", CPP_NOT),
659 B("not_eq", CPP_NOT_EQ),
660 B("or", CPP_OR_OR),
661 B("or_eq", CPP_OR_EQ),
662 B("xor", CPP_XOR),
663 B("xor_eq", CPP_XOR_EQ)
665 #undef B
667 /* Mark the C++ named operators in the hash table. */
668 static void
669 mark_named_operators (pfile)
670 cpp_reader *pfile;
672 const struct builtin *b;
674 for (b = operator_array;
675 b < (operator_array + ARRAY_SIZE (operator_array));
676 b++)
678 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
679 hp->flags |= NODE_OPERATOR;
680 hp->value.operator = b->value;
684 /* Subroutine of cpp_read_main_file; reads the builtins table above and
685 enters them, and language-specific macros, into the hash table. */
686 static void
687 init_builtins (pfile)
688 cpp_reader *pfile;
690 const struct builtin *b;
692 for(b = builtin_array;
693 b < (builtin_array + ARRAY_SIZE (builtin_array));
694 b++)
696 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
697 hp->type = NT_MACRO;
698 hp->flags |= NODE_BUILTIN | NODE_WARN;
699 hp->value.builtin = b->value;
702 if (CPP_OPTION (pfile, cplusplus))
703 _cpp_define_builtin (pfile, "__cplusplus 1");
704 else if (CPP_OPTION (pfile, objc))
705 _cpp_define_builtin (pfile, "__OBJC__ 1");
706 else if (CPP_OPTION (pfile, lang) == CLK_ASM)
707 _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
709 if (CPP_OPTION (pfile, lang) == CLK_STDC94)
710 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
711 else if (CPP_OPTION (pfile, c99))
712 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
714 if (pfile->cb.register_builtins)
715 (*pfile->cb.register_builtins) (pfile);
718 /* And another subroutine. This one sets up the standard include path. */
719 static void
720 init_standard_includes (pfile)
721 cpp_reader *pfile;
723 char *path;
724 const struct default_include *p;
725 const char *specd_prefix = CPP_OPTION (pfile, include_prefix);
727 /* Several environment variables may add to the include search path.
728 CPATH specifies an additional list of directories to be searched
729 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
730 etc. specify an additional list of directories to be searched as
731 if specified with -isystem, for the language indicated. */
733 GET_ENV_PATH_LIST (path, "CPATH");
734 if (path != 0 && *path != 0)
735 path_include (pfile, path, BRACKET);
737 switch ((CPP_OPTION (pfile, objc) << 1) + CPP_OPTION (pfile, cplusplus))
739 case 0:
740 GET_ENV_PATH_LIST (path, "C_INCLUDE_PATH");
741 break;
742 case 1:
743 GET_ENV_PATH_LIST (path, "CPLUS_INCLUDE_PATH");
744 break;
745 case 2:
746 GET_ENV_PATH_LIST (path, "OBJC_INCLUDE_PATH");
747 break;
748 case 3:
749 GET_ENV_PATH_LIST (path, "OBJCPLUS_INCLUDE_PATH");
750 break;
752 if (path != 0 && *path != 0)
753 path_include (pfile, path, SYSTEM);
755 /* Search "translated" versions of GNU directories.
756 These have /usr/local/lib/gcc... replaced by specd_prefix. */
757 if (specd_prefix != 0 && cpp_GCC_INCLUDE_DIR_len)
759 /* Remove the `include' from /usr/local/lib/gcc.../include.
760 GCC_INCLUDE_DIR will always end in /include. */
761 int default_len = cpp_GCC_INCLUDE_DIR_len;
762 char *default_prefix = (char *) alloca (default_len + 1);
763 int specd_len = strlen (specd_prefix);
765 memcpy (default_prefix, cpp_GCC_INCLUDE_DIR, default_len);
766 default_prefix[default_len] = '\0';
768 for (p = cpp_include_defaults; p->fname; p++)
770 /* Some standard dirs are only for C++. */
771 if (!p->cplusplus
772 || (CPP_OPTION (pfile, cplusplus)
773 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
775 /* Does this dir start with the prefix? */
776 if (!memcmp (p->fname, default_prefix, default_len))
778 /* Yes; change prefix and add to search list. */
779 int flen = strlen (p->fname);
780 int this_len = specd_len + flen - default_len;
781 char *str = (char *) xmalloc (this_len + 1);
782 memcpy (str, specd_prefix, specd_len);
783 memcpy (str + specd_len,
784 p->fname + default_len,
785 flen - default_len + 1);
787 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
793 /* Search ordinary names for GNU include directories. */
794 for (p = cpp_include_defaults; p->fname; p++)
796 /* Some standard dirs are only for C++. */
797 if (!p->cplusplus
798 || (CPP_OPTION (pfile, cplusplus)
799 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
801 char *str = update_path (p->fname, p->component);
802 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
807 /* Pushes a command line -imacro and -include file indicated by P onto
808 the buffer stack. Returns non-zero if successful. */
809 static bool
810 push_include (pfile, p)
811 cpp_reader *pfile;
812 struct pending_option *p;
814 cpp_token header;
816 /* Later: maybe update this to use the #include "" search path
817 if cpp_read_file fails. */
818 header.type = CPP_STRING;
819 header.val.str.text = (const unsigned char *) p->arg;
820 header.val.str.len = strlen (p->arg);
821 /* Make the command line directive take up a line. */
822 pfile->line++;
824 return _cpp_execute_include (pfile, &header, IT_CMDLINE);
827 /* Frees a pending_option chain. */
828 static void
829 free_chain (head)
830 struct pending_option *head;
832 struct pending_option *next;
834 while (head)
836 next = head->next;
837 free (head);
838 head = next;
842 /* Sanity-checks are dependent on command-line options, so it is
843 called as a subroutine of cpp_read_main_file (). */
844 #if ENABLE_CHECKING
845 static void sanity_checks PARAMS ((cpp_reader *));
846 static void sanity_checks (pfile)
847 cpp_reader *pfile;
849 cppchar_t test = 0;
850 size_t max_precision = 2 * CHAR_BIT * sizeof (cpp_num_part);
852 /* Sanity checks for assumptions about CPP arithmetic and target
853 type precisions made by cpplib. */
854 test--;
855 if (test < 1)
856 cpp_error (pfile, DL_ICE, "cppchar_t must be an unsigned type");
858 if (CPP_OPTION (pfile, precision) > max_precision)
859 cpp_error (pfile, DL_ICE,
860 "preprocessor arithmetic has maximum precision of %lu bits; target requires %lu bits",
861 (unsigned long) max_precision,
862 (unsigned long) CPP_OPTION (pfile, precision));
864 if (CPP_OPTION (pfile, precision) < CPP_OPTION (pfile, int_precision))
865 cpp_error (pfile, DL_ICE,
866 "CPP arithmetic must be at least as precise as a target int");
868 if (CPP_OPTION (pfile, char_precision) < 8)
869 cpp_error (pfile, DL_ICE, "target char is less than 8 bits wide");
871 if (CPP_OPTION (pfile, wchar_precision) < CPP_OPTION (pfile, char_precision))
872 cpp_error (pfile, DL_ICE,
873 "target wchar_t is narrower than target char");
875 if (CPP_OPTION (pfile, int_precision) < CPP_OPTION (pfile, char_precision))
876 cpp_error (pfile, DL_ICE,
877 "target int is narrower than target char");
879 /* This is assumed in eval_token() and could be fixed if necessary. */
880 if (sizeof (cppchar_t) > sizeof (cpp_num_part))
881 cpp_error (pfile, DL_ICE, "CPP half-integer narrower than CPP character");
883 if (CPP_OPTION (pfile, wchar_precision) > BITS_PER_CPPCHAR_T)
884 cpp_error (pfile, DL_ICE,
885 "CPP on this host cannot handle wide character constants over %lu bits, but the target requires %lu bits",
886 (unsigned long) BITS_PER_CPPCHAR_T,
887 (unsigned long) CPP_OPTION (pfile, wchar_precision));
889 #else
890 # define sanity_checks(PFILE)
891 #endif
893 /* This is called after options have been parsed, and partially
894 processed. Setup for processing input from the file named FNAME,
895 or stdin if it is the empty string. Return the original filename
896 on success (e.g. foo.i->foo.c), or NULL on failure. */
897 const char *
898 cpp_read_main_file (pfile, fname, table)
899 cpp_reader *pfile;
900 const char *fname;
901 hash_table *table;
903 sanity_checks (pfile);
905 /* The front ends don't set up the hash table until they have
906 finished processing the command line options, so initializing the
907 hashtable is deferred until now. */
908 _cpp_init_hashtable (pfile, table);
910 /* Set up the include search path now. */
911 if (! CPP_OPTION (pfile, no_standard_includes))
912 init_standard_includes (pfile);
914 merge_include_chains (pfile);
916 /* With -v, print the list of dirs to search. */
917 if (CPP_OPTION (pfile, verbose))
919 struct search_path *l;
920 fprintf (stderr, _("#include \"...\" search starts here:\n"));
921 for (l = CPP_OPTION (pfile, quote_include); l; l = l->next)
923 if (l == CPP_OPTION (pfile, bracket_include))
924 fprintf (stderr, _("#include <...> search starts here:\n"));
925 fprintf (stderr, " %s\n", l->name);
927 fprintf (stderr, _("End of search list.\n"));
930 if (CPP_OPTION (pfile, print_deps))
931 /* Set the default target (if there is none already). */
932 deps_add_default_target (pfile->deps, fname);
934 /* Open the main input file. */
935 if (!_cpp_read_file (pfile, fname))
936 return NULL;
938 /* Set this after cpp_post_options so the client can change the
939 option if it wishes, and after stacking the main file so we don't
940 trace the main file. */
941 pfile->line_maps.trace_includes = CPP_OPTION (pfile, print_include_names);
943 /* For foo.i, read the original filename foo.c now, for the benefit
944 of the front ends. */
945 if (CPP_OPTION (pfile, preprocessed))
946 read_original_filename (pfile);
947 /* Overlay an empty buffer to seed traditional preprocessing. */
948 else if (CPP_OPTION (pfile, traditional))
949 _cpp_overlay_buffer (pfile, U"", 0);
951 return pfile->map->to_file;
954 /* For preprocessed files, if the first tokens are of the form # NUM.
955 handle the directive so we know the original file name. This will
956 generate file_change callbacks, which the front ends must handle
957 appropriately given their state of initialization. */
958 static void
959 read_original_filename (pfile)
960 cpp_reader *pfile;
962 const cpp_token *token, *token1;
964 /* Lex ahead; if the first tokens are of the form # NUM, then
965 process the directive, otherwise back up. */
966 token = _cpp_lex_direct (pfile);
967 if (token->type == CPP_HASH)
969 token1 = _cpp_lex_direct (pfile);
970 _cpp_backup_tokens (pfile, 1);
972 /* If it's a #line directive, handle it. */
973 if (token1->type == CPP_NUMBER)
975 _cpp_handle_directive (pfile, token->flags & PREV_WHITE);
976 return;
980 /* Backup as if nothing happened. */
981 _cpp_backup_tokens (pfile, 1);
984 /* Handle pending command line options: -D, -U, -A, -imacros and
985 -include. This should be called after debugging has been properly
986 set up in the front ends. */
987 void
988 cpp_finish_options (pfile)
989 cpp_reader *pfile;
991 /* Mark named operators before handling command line macros. */
992 if (CPP_OPTION (pfile, cplusplus) && CPP_OPTION (pfile, operator_names))
993 mark_named_operators (pfile);
995 /* Install builtins and process command line macros etc. in the order
996 they appeared, but only if not already preprocessed. */
997 if (! CPP_OPTION (pfile, preprocessed))
999 struct pending_option *p;
1001 _cpp_do_file_change (pfile, LC_RENAME, _("<built-in>"), 1, 0);
1002 if (!CPP_OPTION (pfile, traditional) /* REMOVEME */)
1003 init_builtins (pfile);
1004 _cpp_do_file_change (pfile, LC_RENAME, _("<command line>"), 1, 0);
1005 if (!CPP_OPTION (pfile, traditional) /* REMOVEME */)
1006 for (p = CPP_OPTION (pfile, pending)->directive_head; p; p = p->next)
1007 (*p->handler) (pfile, p->arg);
1009 /* Scan -imacros files after -D, -U, but before -include.
1010 pfile->next_include_file is NULL, so _cpp_pop_buffer does not
1011 push -include files. */
1012 for (p = CPP_OPTION (pfile, pending)->imacros_head; p; p = p->next)
1013 if (push_include (pfile, p))
1014 cpp_scan_nooutput (pfile);
1016 pfile->next_include_file = &CPP_OPTION (pfile, pending)->include_head;
1017 _cpp_maybe_push_include_file (pfile);
1020 free_chain (CPP_OPTION (pfile, pending)->imacros_head);
1021 free_chain (CPP_OPTION (pfile, pending)->directive_head);
1024 /* Push the next buffer on the stack given by -include, if any. */
1025 void
1026 _cpp_maybe_push_include_file (pfile)
1027 cpp_reader *pfile;
1029 if (pfile->next_include_file)
1031 struct pending_option *head = *pfile->next_include_file;
1033 while (head && !push_include (pfile, head))
1034 head = head->next;
1036 if (head)
1037 pfile->next_include_file = &head->next;
1038 else
1040 /* All done; restore the line map from <command line>. */
1041 _cpp_do_file_change (pfile, LC_RENAME,
1042 pfile->line_maps.maps[0].to_file, 1, 0);
1043 /* Don't come back here again. */
1044 pfile->next_include_file = NULL;
1049 /* Use mkdeps.c to output dependency information. */
1050 static void
1051 output_deps (pfile)
1052 cpp_reader *pfile;
1054 /* Stream on which to print the dependency information. */
1055 FILE *deps_stream = 0;
1056 const char *const deps_mode =
1057 CPP_OPTION (pfile, print_deps_append) ? "a" : "w";
1059 if (CPP_OPTION (pfile, deps_file)[0] == '\0')
1060 deps_stream = stdout;
1061 else
1063 deps_stream = fopen (CPP_OPTION (pfile, deps_file), deps_mode);
1064 if (deps_stream == 0)
1066 cpp_errno (pfile, DL_ERROR, CPP_OPTION (pfile, deps_file));
1067 return;
1071 deps_write (pfile->deps, deps_stream, 72);
1073 if (CPP_OPTION (pfile, deps_phony_targets))
1074 deps_phony_targets (pfile->deps, deps_stream);
1076 /* Don't close stdout. */
1077 if (deps_stream != stdout)
1079 if (ferror (deps_stream) || fclose (deps_stream) != 0)
1080 cpp_error (pfile, DL_ERROR, "I/O error on output");
1084 /* This is called at the end of preprocessing. It pops the
1085 last buffer and writes dependency output. It should also
1086 clear macro definitions, such that you could call cpp_start_read
1087 with a new filename to restart processing. */
1088 void
1089 cpp_finish (pfile)
1090 cpp_reader *pfile;
1092 /* cpplex.c leaves the final buffer on the stack. This it so that
1093 it returns an unending stream of CPP_EOFs to the client. If we
1094 popped the buffer, we'd dereference a NULL buffer pointer and
1095 segfault. It's nice to allow the client to do worry-free excess
1096 cpp_get_token calls. */
1097 while (pfile->buffer)
1098 _cpp_pop_buffer (pfile);
1100 /* Don't write the deps file if preprocessing has failed. */
1101 if (CPP_OPTION (pfile, print_deps) && pfile->errors == 0)
1102 output_deps (pfile);
1104 /* Report on headers that could use multiple include guards. */
1105 if (CPP_OPTION (pfile, print_include_names))
1106 _cpp_report_missing_guards (pfile);
1109 /* Add a directive to be handled later in the initialization phase. */
1110 static void
1111 new_pending_directive (pend, text, handler)
1112 struct cpp_pending *pend;
1113 const char *text;
1114 cl_directive_handler handler;
1116 struct pending_option *o = (struct pending_option *)
1117 xmalloc (sizeof (struct pending_option));
1119 o->arg = text;
1120 o->next = NULL;
1121 o->handler = handler;
1122 APPEND (pend, directive, o);
1125 /* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
1126 I.e. a const string initializer with parens around it. That is
1127 what N_("string") resolves to, so we make no_* be macros instead. */
1128 #define no_arg N_("argument missing after %s")
1129 #define no_ass N_("assertion missing after %s")
1130 #define no_dir N_("directory name missing after %s")
1131 #define no_fil N_("file name missing after %s")
1132 #define no_mac N_("macro name missing after %s")
1133 #define no_pth N_("path name missing after %s")
1134 #define no_num N_("number missing after %s")
1135 #define no_tgt N_("target missing after %s")
1137 /* This is the list of all command line options, with the leading
1138 "-" removed. It must be sorted in ASCII collating order. */
1139 #define COMMAND_LINE_OPTIONS \
1140 DEF_OPT("$", 0, OPT_dollar) \
1141 DEF_OPT("-help", 0, OPT__help) \
1142 DEF_OPT("-target-help", 0, OPT_target__help) \
1143 DEF_OPT("-version", 0, OPT__version) \
1144 DEF_OPT("A", no_ass, OPT_A) \
1145 DEF_OPT("C", 0, OPT_C) \
1146 DEF_OPT("CC", 0, OPT_CC) \
1147 DEF_OPT("D", no_mac, OPT_D) \
1148 DEF_OPT("H", 0, OPT_H) \
1149 DEF_OPT("I", no_dir, OPT_I) \
1150 DEF_OPT("M", 0, OPT_M) \
1151 DEF_OPT("MD", no_fil, OPT_MD) \
1152 DEF_OPT("MF", no_fil, OPT_MF) \
1153 DEF_OPT("MG", 0, OPT_MG) \
1154 DEF_OPT("MM", 0, OPT_MM) \
1155 DEF_OPT("MMD", no_fil, OPT_MMD) \
1156 DEF_OPT("MP", 0, OPT_MP) \
1157 DEF_OPT("MQ", no_tgt, OPT_MQ) \
1158 DEF_OPT("MT", no_tgt, OPT_MT) \
1159 DEF_OPT("P", 0, OPT_P) \
1160 DEF_OPT("U", no_mac, OPT_U) \
1161 DEF_OPT("W", no_arg, OPT_W) /* arg optional */ \
1162 DEF_OPT("d", no_arg, OPT_d) \
1163 DEF_OPT("fno-operator-names", 0, OPT_fno_operator_names) \
1164 DEF_OPT("fno-preprocessed", 0, OPT_fno_preprocessed) \
1165 DEF_OPT("fno-show-column", 0, OPT_fno_show_column) \
1166 DEF_OPT("fpreprocessed", 0, OPT_fpreprocessed) \
1167 DEF_OPT("fshow-column", 0, OPT_fshow_column) \
1168 DEF_OPT("ftabstop=", no_num, OPT_ftabstop) \
1169 DEF_OPT("h", 0, OPT_h) \
1170 DEF_OPT("idirafter", no_dir, OPT_idirafter) \
1171 DEF_OPT("imacros", no_fil, OPT_imacros) \
1172 DEF_OPT("include", no_fil, OPT_include) \
1173 DEF_OPT("iprefix", no_pth, OPT_iprefix) \
1174 DEF_OPT("isystem", no_dir, OPT_isystem) \
1175 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix) \
1176 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore) \
1177 DEF_OPT("lang-asm", 0, OPT_lang_asm) \
1178 DEF_OPT("lang-c", 0, OPT_lang_c) \
1179 DEF_OPT("lang-c++", 0, OPT_lang_cplusplus) \
1180 DEF_OPT("lang-c89", 0, OPT_lang_c89) \
1181 DEF_OPT("lang-objc", 0, OPT_lang_objc) \
1182 DEF_OPT("nostdinc", 0, OPT_nostdinc) \
1183 DEF_OPT("nostdinc++", 0, OPT_nostdincplusplus) \
1184 DEF_OPT("o", no_fil, OPT_o) \
1185 DEF_OPT("pedantic", 0, OPT_pedantic) \
1186 DEF_OPT("pedantic-errors", 0, OPT_pedantic_errors) \
1187 DEF_OPT("remap", 0, OPT_remap) \
1188 DEF_OPT("std=c++98", 0, OPT_std_cplusplus98) \
1189 DEF_OPT("std=c89", 0, OPT_std_c89) \
1190 DEF_OPT("std=c99", 0, OPT_std_c99) \
1191 DEF_OPT("std=c9x", 0, OPT_std_c9x) \
1192 DEF_OPT("std=gnu89", 0, OPT_std_gnu89) \
1193 DEF_OPT("std=gnu99", 0, OPT_std_gnu99) \
1194 DEF_OPT("std=gnu9x", 0, OPT_std_gnu9x) \
1195 DEF_OPT("std=iso9899:1990", 0, OPT_std_iso9899_1990) \
1196 DEF_OPT("std=iso9899:199409", 0, OPT_std_iso9899_199409) \
1197 DEF_OPT("std=iso9899:1999", 0, OPT_std_iso9899_1999) \
1198 DEF_OPT("std=iso9899:199x", 0, OPT_std_iso9899_199x) \
1199 DEF_OPT("traditional-cpp", 0, OPT_traditional_cpp) \
1200 DEF_OPT("trigraphs", 0, OPT_trigraphs) \
1201 DEF_OPT("v", 0, OPT_v) \
1202 DEF_OPT("version", 0, OPT_version) \
1203 DEF_OPT("w", 0, OPT_w)
1205 #define DEF_OPT(text, msg, code) code,
1206 enum opt_code
1208 COMMAND_LINE_OPTIONS
1209 N_OPTS
1211 #undef DEF_OPT
1213 struct cl_option
1215 const char *opt_text;
1216 const char *msg;
1217 size_t opt_len;
1218 enum opt_code opt_code;
1221 #define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
1222 #ifdef HOST_EBCDIC
1223 static struct cl_option cl_options[] =
1224 #else
1225 static const struct cl_option cl_options[] =
1226 #endif
1228 COMMAND_LINE_OPTIONS
1230 #undef DEF_OPT
1231 #undef COMMAND_LINE_OPTIONS
1233 /* Perform a binary search to find which, if any, option the given
1234 command-line matches. Returns its index in the option array,
1235 negative on failure. Complications arise since some options can be
1236 suffixed with an argument, and multiple complete matches can occur,
1237 e.g. -iwithprefix and -iwithprefixbefore. Moreover, we need to
1238 accept options beginning with -W that we do not recognise, but not
1239 to swallow any subsequent command line argument; this is handled as
1240 special cases in cpp_handle_option. */
1241 static int
1242 parse_option (input)
1243 const char *input;
1245 unsigned int md, mn, mx;
1246 size_t opt_len;
1247 int comp;
1249 mn = 0;
1250 mx = N_OPTS;
1252 while (mx > mn)
1254 md = (mn + mx) / 2;
1256 opt_len = cl_options[md].opt_len;
1257 comp = memcmp (input, cl_options[md].opt_text, opt_len);
1259 if (comp > 0)
1260 mn = md + 1;
1261 else if (comp < 0)
1262 mx = md;
1263 else
1265 if (input[opt_len] == '\0')
1266 return md;
1267 /* We were passed more text. If the option takes an argument,
1268 we may match a later option or we may have been passed the
1269 argument. The longest possible option match succeeds.
1270 If the option takes no arguments we have not matched and
1271 continue the search (e.g. input="stdc++" match was "stdc"). */
1272 mn = md + 1;
1273 if (cl_options[md].msg)
1275 /* Scan forwards. If we get an exact match, return it.
1276 Otherwise, return the longest option-accepting match.
1277 This loops no more than twice with current options. */
1278 mx = md;
1279 for (; mn < (unsigned int) N_OPTS; mn++)
1281 opt_len = cl_options[mn].opt_len;
1282 if (memcmp (input, cl_options[mn].opt_text, opt_len))
1283 break;
1284 if (input[opt_len] == '\0')
1285 return mn;
1286 if (cl_options[mn].msg)
1287 mx = mn;
1289 return mx;
1294 return -1;
1297 /* Handle one command-line option in (argc, argv).
1298 Can be called multiple times, to handle multiple sets of options.
1299 If ignore is non-zero, this will ignore unrecognized -W* options.
1300 Returns number of strings consumed. */
1302 cpp_handle_option (pfile, argc, argv, ignore)
1303 cpp_reader *pfile;
1304 int argc;
1305 char **argv;
1306 int ignore;
1308 int i = 0;
1309 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
1311 /* Interpret "-" or a non-option as a file name. */
1312 if (argv[i][0] != '-' || argv[i][1] == '\0')
1314 if (CPP_OPTION (pfile, in_fname) == NULL)
1315 CPP_OPTION (pfile, in_fname) = argv[i];
1316 else if (CPP_OPTION (pfile, out_fname) == NULL)
1317 CPP_OPTION (pfile, out_fname) = argv[i];
1318 else
1319 cpp_error (pfile, DL_ERROR,
1320 "too many filenames. Type %s --help for usage info",
1321 progname);
1323 else
1325 enum opt_code opt_code;
1326 int opt_index;
1327 const char *arg = 0;
1329 /* Skip over '-'. */
1330 opt_index = parse_option (&argv[i][1]);
1331 if (opt_index < 0)
1332 return i;
1334 opt_code = cl_options[opt_index].opt_code;
1335 if (cl_options[opt_index].msg)
1337 arg = &argv[i][cl_options[opt_index].opt_len + 1];
1339 /* Yuk. Special case for -W as it must not swallow
1340 up any following argument. If this becomes common, add
1341 another field to the cl_options table. */
1342 if (arg[0] == '\0' && opt_code != OPT_W)
1344 arg = argv[++i];
1345 if (!arg)
1347 cpp_error (pfile, DL_ERROR,
1348 cl_options[opt_index].msg, argv[i - 1]);
1349 return argc;
1354 switch (opt_code)
1356 case N_OPTS: /* Shut GCC up. */
1357 break;
1358 case OPT_fno_operator_names:
1359 CPP_OPTION (pfile, operator_names) = 0;
1360 break;
1361 case OPT_fpreprocessed:
1362 CPP_OPTION (pfile, preprocessed) = 1;
1363 break;
1364 case OPT_fno_preprocessed:
1365 CPP_OPTION (pfile, preprocessed) = 0;
1366 break;
1367 case OPT_fshow_column:
1368 CPP_OPTION (pfile, show_column) = 1;
1369 break;
1370 case OPT_fno_show_column:
1371 CPP_OPTION (pfile, show_column) = 0;
1372 break;
1373 case OPT_ftabstop:
1374 /* Silently ignore empty string, non-longs and silly values. */
1375 if (arg[0] != '\0')
1377 char *endptr;
1378 long tabstop = strtol (arg, &endptr, 10);
1379 if (*endptr == '\0' && tabstop >= 1 && tabstop <= 100)
1380 CPP_OPTION (pfile, tabstop) = tabstop;
1382 break;
1383 case OPT_w:
1384 CPP_OPTION (pfile, inhibit_warnings) = 1;
1385 break;
1386 case OPT_h:
1387 case OPT__help:
1388 print_help ();
1389 CPP_OPTION (pfile, help_only) = 1;
1390 break;
1391 case OPT_target__help:
1392 /* Print if any target specific options. cpplib has none, but
1393 make sure help_only gets set. */
1394 CPP_OPTION (pfile, help_only) = 1;
1395 break;
1397 /* --version inhibits compilation, -version doesn't. -v means
1398 verbose and -version. Historical reasons, don't ask. */
1399 case OPT__version:
1400 CPP_OPTION (pfile, help_only) = 1;
1401 pfile->print_version = 1;
1402 break;
1403 case OPT_v:
1404 CPP_OPTION (pfile, verbose) = 1;
1405 pfile->print_version = 1;
1406 break;
1407 case OPT_version:
1408 pfile->print_version = 1;
1409 break;
1411 case OPT_C:
1412 CPP_OPTION (pfile, discard_comments) = 0;
1413 break;
1414 case OPT_CC:
1415 CPP_OPTION (pfile, discard_comments) = 0;
1416 CPP_OPTION (pfile, discard_comments_in_macro_exp) = 0;
1417 break;
1418 case OPT_P:
1419 CPP_OPTION (pfile, no_line_commands) = 1;
1420 break;
1421 case OPT_dollar: /* Don't include $ in identifiers. */
1422 CPP_OPTION (pfile, dollars_in_ident) = 0;
1423 break;
1424 case OPT_H:
1425 CPP_OPTION (pfile, print_include_names) = 1;
1426 break;
1427 case OPT_D:
1428 new_pending_directive (pend, arg, cpp_define);
1429 break;
1430 case OPT_pedantic_errors:
1431 CPP_OPTION (pfile, pedantic_errors) = 1;
1432 /* fall through */
1433 case OPT_pedantic:
1434 CPP_OPTION (pfile, pedantic) = 1;
1435 CPP_OPTION (pfile, warn_endif_labels) = 1;
1436 break;
1437 case OPT_trigraphs:
1438 CPP_OPTION (pfile, trigraphs) = 1;
1439 break;
1440 case OPT_remap:
1441 CPP_OPTION (pfile, remap) = 1;
1442 break;
1443 case OPT_traditional_cpp:
1444 CPP_OPTION (pfile, traditional) = 1;
1445 break;
1446 case OPT_iprefix:
1447 CPP_OPTION (pfile, include_prefix) = arg;
1448 CPP_OPTION (pfile, include_prefix_len) = strlen (arg);
1449 break;
1450 case OPT_lang_c:
1451 set_lang (pfile, CLK_GNUC89);
1452 break;
1453 case OPT_lang_cplusplus:
1454 set_lang (pfile, CLK_GNUCXX);
1455 break;
1456 case OPT_lang_objc:
1457 CPP_OPTION (pfile, objc) = 1;
1458 break;
1459 case OPT_lang_asm:
1460 set_lang (pfile, CLK_ASM);
1461 break;
1462 case OPT_std_cplusplus98:
1463 set_lang (pfile, CLK_CXX98);
1464 break;
1465 case OPT_std_gnu89:
1466 set_lang (pfile, CLK_GNUC89);
1467 break;
1468 case OPT_std_gnu9x:
1469 case OPT_std_gnu99:
1470 set_lang (pfile, CLK_GNUC99);
1471 break;
1472 case OPT_std_iso9899_199409:
1473 set_lang (pfile, CLK_STDC94);
1474 break;
1475 case OPT_std_iso9899_1990:
1476 case OPT_std_c89:
1477 case OPT_lang_c89:
1478 set_lang (pfile, CLK_STDC89);
1479 break;
1480 case OPT_std_iso9899_199x:
1481 case OPT_std_iso9899_1999:
1482 case OPT_std_c9x:
1483 case OPT_std_c99:
1484 set_lang (pfile, CLK_STDC99);
1485 break;
1486 case OPT_nostdinc:
1487 /* -nostdinc causes no default include directories.
1488 You must specify all include-file directories with -I. */
1489 CPP_OPTION (pfile, no_standard_includes) = 1;
1490 break;
1491 case OPT_nostdincplusplus:
1492 /* -nostdinc++ causes no default C++-specific include directories. */
1493 CPP_OPTION (pfile, no_standard_cplusplus_includes) = 1;
1494 break;
1495 case OPT_o:
1496 if (CPP_OPTION (pfile, out_fname) == NULL)
1497 CPP_OPTION (pfile, out_fname) = arg;
1498 else
1500 cpp_error (pfile, DL_ERROR, "output filename specified twice");
1501 return argc;
1503 break;
1504 case OPT_d:
1505 /* Args to -d specify what parts of macros to dump.
1506 Silently ignore unrecognised options; they may
1507 be aimed at the compiler proper. */
1509 char c;
1511 while ((c = *arg++) != '\0')
1512 switch (c)
1514 case 'M':
1515 CPP_OPTION (pfile, dump_macros) = dump_only;
1516 break;
1517 case 'N':
1518 CPP_OPTION (pfile, dump_macros) = dump_names;
1519 break;
1520 case 'D':
1521 CPP_OPTION (pfile, dump_macros) = dump_definitions;
1522 break;
1523 case 'I':
1524 CPP_OPTION (pfile, dump_includes) = 1;
1525 break;
1528 break;
1530 case OPT_MG:
1531 CPP_OPTION (pfile, print_deps_missing_files) = 1;
1532 break;
1533 case OPT_M:
1534 /* When doing dependencies with -M or -MM, suppress normal
1535 preprocessed output, but still do -dM etc. as software
1536 depends on this. Preprocessed output occurs if -MD, -MMD
1537 or environment var dependency generation is used. */
1538 CPP_OPTION (pfile, print_deps) = 2;
1539 CPP_OPTION (pfile, no_output) = 1;
1540 break;
1541 case OPT_MM:
1542 CPP_OPTION (pfile, print_deps) = 1;
1543 CPP_OPTION (pfile, no_output) = 1;
1544 break;
1545 case OPT_MF:
1546 CPP_OPTION (pfile, deps_file) = arg;
1547 break;
1548 case OPT_MP:
1549 CPP_OPTION (pfile, deps_phony_targets) = 1;
1550 break;
1551 case OPT_MQ:
1552 case OPT_MT:
1553 /* Add a target. -MQ quotes for Make. */
1554 deps_add_target (pfile->deps, arg, opt_code == OPT_MQ);
1555 break;
1557 case OPT_MD:
1558 CPP_OPTION (pfile, print_deps) = 2;
1559 CPP_OPTION (pfile, deps_file) = arg;
1560 break;
1561 case OPT_MMD:
1562 CPP_OPTION (pfile, print_deps) = 1;
1563 CPP_OPTION (pfile, deps_file) = arg;
1564 break;
1566 case OPT_A:
1567 if (arg[0] == '-')
1569 /* -A with an argument beginning with '-' acts as
1570 #unassert on whatever immediately follows the '-'.
1571 If "-" is the whole argument, we eliminate all
1572 predefined macros and assertions, including those
1573 that were specified earlier on the command line.
1574 That way we can get rid of any that were passed
1575 automatically in from GCC. */
1577 if (arg[1] == '\0')
1579 free_chain (pend->directive_head);
1580 pend->directive_head = NULL;
1581 pend->directive_tail = NULL;
1583 else
1584 new_pending_directive (pend, arg + 1, cpp_unassert);
1586 else
1587 new_pending_directive (pend, arg, cpp_assert);
1588 break;
1589 case OPT_U:
1590 new_pending_directive (pend, arg, cpp_undef);
1591 break;
1592 case OPT_I: /* Add directory to path for includes. */
1593 if (!strcmp (arg, "-"))
1595 /* -I- means:
1596 Use the preceding -I directories for #include "..."
1597 but not #include <...>.
1598 Don't search the directory of the present file
1599 for #include "...". (Note that -I. -I- is not the same as
1600 the default setup; -I. uses the compiler's working dir.) */
1601 if (! CPP_OPTION (pfile, ignore_srcdir))
1603 pend->quote_head = pend->brack_head;
1604 pend->quote_tail = pend->brack_tail;
1605 pend->brack_head = 0;
1606 pend->brack_tail = 0;
1607 CPP_OPTION (pfile, ignore_srcdir) = 1;
1609 else
1611 cpp_error (pfile, DL_ERROR, "-I- specified twice");
1612 return argc;
1615 else
1616 append_include_chain (pfile, xstrdup (arg), BRACKET, 0);
1617 break;
1618 case OPT_isystem:
1619 /* Add directory to beginning of system include path, as a system
1620 include directory. */
1621 append_include_chain (pfile, xstrdup (arg), SYSTEM, 0);
1622 break;
1623 case OPT_include:
1624 case OPT_imacros:
1626 struct pending_option *o = (struct pending_option *)
1627 xmalloc (sizeof (struct pending_option));
1628 o->arg = arg;
1629 o->next = NULL;
1631 if (opt_code == OPT_include)
1632 APPEND (pend, include, o);
1633 else
1634 APPEND (pend, imacros, o);
1636 break;
1637 case OPT_iwithprefix:
1638 /* Add directory to end of path for includes,
1639 with the default prefix at the front of its name. */
1640 /* fall through */
1641 case OPT_iwithprefixbefore:
1642 /* Add directory to main path for includes,
1643 with the default prefix at the front of its name. */
1645 char *fname;
1646 int len;
1648 len = strlen (arg);
1650 if (CPP_OPTION (pfile, include_prefix) != 0)
1652 size_t ipl = CPP_OPTION (pfile, include_prefix_len);
1653 fname = xmalloc (ipl + len + 1);
1654 memcpy (fname, CPP_OPTION (pfile, include_prefix), ipl);
1655 memcpy (fname + ipl, arg, len + 1);
1657 else if (cpp_GCC_INCLUDE_DIR_len)
1659 fname = xmalloc (cpp_GCC_INCLUDE_DIR_len + len + 1);
1660 memcpy (fname, cpp_GCC_INCLUDE_DIR, cpp_GCC_INCLUDE_DIR_len);
1661 memcpy (fname + cpp_GCC_INCLUDE_DIR_len, arg, len + 1);
1663 else
1664 fname = xstrdup (arg);
1666 append_include_chain (pfile, fname,
1667 opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0);
1669 break;
1670 case OPT_idirafter:
1671 /* Add directory to end of path for includes. */
1672 append_include_chain (pfile, xstrdup (arg), AFTER, 0);
1673 break;
1674 case OPT_W:
1675 /* Silently ignore unrecognised options. */
1676 if (!strcmp (argv[i], "-Wall"))
1678 CPP_OPTION (pfile, warn_trigraphs) = 1;
1679 CPP_OPTION (pfile, warn_comments) = 1;
1681 else if (!strcmp (argv[i], "-Wtraditional"))
1682 CPP_OPTION (pfile, warn_traditional) = 1;
1683 else if (!strcmp (argv[i], "-Wtrigraphs"))
1684 CPP_OPTION (pfile, warn_trigraphs) = 1;
1685 else if (!strcmp (argv[i], "-Wcomment"))
1686 CPP_OPTION (pfile, warn_comments) = 1;
1687 else if (!strcmp (argv[i], "-Wcomments"))
1688 CPP_OPTION (pfile, warn_comments) = 1;
1689 else if (!strcmp (argv[i], "-Wundef"))
1690 CPP_OPTION (pfile, warn_undef) = 1;
1691 else if (!strcmp (argv[i], "-Wimport"))
1692 CPP_OPTION (pfile, warn_import) = 1;
1693 else if (!strcmp (argv[i], "-Werror"))
1694 CPP_OPTION (pfile, warnings_are_errors) = 1;
1695 else if (!strcmp (argv[i], "-Wsystem-headers"))
1696 CPP_OPTION (pfile, warn_system_headers) = 1;
1697 else if (!strcmp (argv[i], "-Wendif-labels"))
1698 CPP_OPTION (pfile, warn_endif_labels) = 1;
1699 else if (!strcmp (argv[i], "-Wno-traditional"))
1700 CPP_OPTION (pfile, warn_traditional) = 0;
1701 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1702 CPP_OPTION (pfile, warn_trigraphs) = 0;
1703 else if (!strcmp (argv[i], "-Wno-comment"))
1704 CPP_OPTION (pfile, warn_comments) = 0;
1705 else if (!strcmp (argv[i], "-Wno-comments"))
1706 CPP_OPTION (pfile, warn_comments) = 0;
1707 else if (!strcmp (argv[i], "-Wno-undef"))
1708 CPP_OPTION (pfile, warn_undef) = 0;
1709 else if (!strcmp (argv[i], "-Wno-import"))
1710 CPP_OPTION (pfile, warn_import) = 0;
1711 else if (!strcmp (argv[i], "-Wno-error"))
1712 CPP_OPTION (pfile, warnings_are_errors) = 0;
1713 else if (!strcmp (argv[i], "-Wno-system-headers"))
1714 CPP_OPTION (pfile, warn_system_headers) = 0;
1715 else if (!strcmp (argv[i], "-Wno-endif-labels"))
1716 CPP_OPTION (pfile, warn_endif_labels) = 0;
1717 else if (! ignore)
1718 return i;
1719 break;
1722 return i + 1;
1725 /* Handle command-line options in (argc, argv).
1726 Can be called multiple times, to handle multiple sets of options.
1727 Returns if an unrecognized option is seen.
1728 Returns number of strings consumed. */
1730 cpp_handle_options (pfile, argc, argv)
1731 cpp_reader *pfile;
1732 int argc;
1733 char **argv;
1735 int i;
1736 int strings_processed;
1738 for (i = 0; i < argc; i += strings_processed)
1740 strings_processed = cpp_handle_option (pfile, argc - i, argv + i, 1);
1741 if (strings_processed == 0)
1742 break;
1745 return i;
1748 /* Extra processing when all options are parsed, after all calls to
1749 cpp_handle_option[s]. Consistency checks etc. */
1750 void
1751 cpp_post_options (pfile)
1752 cpp_reader *pfile;
1754 if (pfile->print_version)
1756 fprintf (stderr, _("GNU CPP version %s (cpplib)"), version_string);
1757 #ifdef TARGET_VERSION
1758 TARGET_VERSION;
1759 #endif
1760 fputc ('\n', stderr);
1763 /* Canonicalize in_fname and out_fname. We guarantee they are not
1764 NULL, and that the empty string represents stdin / stdout. */
1765 if (CPP_OPTION (pfile, in_fname) == NULL
1766 || !strcmp (CPP_OPTION (pfile, in_fname), "-"))
1767 CPP_OPTION (pfile, in_fname) = "";
1769 if (CPP_OPTION (pfile, out_fname) == NULL
1770 || !strcmp (CPP_OPTION (pfile, out_fname), "-"))
1771 CPP_OPTION (pfile, out_fname) = "";
1773 /* -Wtraditional is not useful in C++ mode. */
1774 if (CPP_OPTION (pfile, cplusplus))
1775 CPP_OPTION (pfile, warn_traditional) = 0;
1777 CPP_OPTION (pfile, warn_long_long) = (CPP_OPTION (pfile, pedantic)
1778 && !CPP_OPTION (pfile, c99));
1780 /* Permanently disable macro expansion if we are rescanning
1781 preprocessed text. Read preprocesed source in ISO mode. */
1782 if (CPP_OPTION (pfile, preprocessed))
1784 pfile->state.prevent_expansion = 1;
1785 CPP_OPTION (pfile, traditional) = 0;
1788 /* Traditional CPP does not accurately track column information. */
1789 if (CPP_OPTION (pfile, traditional))
1790 CPP_OPTION (pfile, show_column) = 0;
1792 /* -dM makes no normal output. This is set here so that -dM -dD
1793 works as expected. */
1794 if (CPP_OPTION (pfile, dump_macros) == dump_only)
1795 CPP_OPTION (pfile, no_output) = 1;
1797 /* Disable -dD, -dN and -dI if we should make no normal output
1798 (such as with -M). Allow -M -dM since some software relies on
1799 this. */
1800 if (CPP_OPTION (pfile, no_output))
1802 if (CPP_OPTION (pfile, dump_macros) != dump_only)
1803 CPP_OPTION (pfile, dump_macros) = dump_none;
1804 CPP_OPTION (pfile, dump_includes) = 0;
1807 /* We need to do this after option processing and before
1808 cpp_start_read, as cppmain.c relies on the options->no_output to
1809 set its callbacks correctly before calling cpp_start_read. */
1810 init_dependency_output (pfile);
1812 /* After checking the environment variables, check if -M or -MM has
1813 not been specified, but other -M options have. */
1814 if (CPP_OPTION (pfile, print_deps) == 0 &&
1815 (CPP_OPTION (pfile, print_deps_missing_files)
1816 || CPP_OPTION (pfile, deps_file)
1817 || CPP_OPTION (pfile, deps_phony_targets)))
1818 cpp_error (pfile, DL_ERROR,
1819 "you must additionally specify either -M or -MM");
1822 /* Set up dependency-file output. On exit, if print_deps is non-zero
1823 then deps_file is not NULL; stdout is the empty string. */
1824 static void
1825 init_dependency_output (pfile)
1826 cpp_reader *pfile;
1828 char *spec, *s, *output_file;
1830 /* Either of two environment variables can specify output of deps.
1831 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1832 where OUTPUT_FILE is the file to write deps info to
1833 and DEPS_TARGET is the target to mention in the deps. */
1835 if (CPP_OPTION (pfile, print_deps) == 0)
1837 spec = getenv ("DEPENDENCIES_OUTPUT");
1838 if (spec)
1839 CPP_OPTION (pfile, print_deps) = 1;
1840 else
1842 spec = getenv ("SUNPRO_DEPENDENCIES");
1843 if (spec)
1844 CPP_OPTION (pfile, print_deps) = 2;
1845 else
1846 return;
1849 /* Find the space before the DEPS_TARGET, if there is one. */
1850 s = strchr (spec, ' ');
1851 if (s)
1853 /* Let the caller perform MAKE quoting. */
1854 deps_add_target (pfile->deps, s + 1, 0);
1855 output_file = (char *) xmalloc (s - spec + 1);
1856 memcpy (output_file, spec, s - spec);
1857 output_file[s - spec] = 0;
1859 else
1860 output_file = spec;
1862 /* Command line -MF overrides environment variables and default. */
1863 if (CPP_OPTION (pfile, deps_file) == 0)
1864 CPP_OPTION (pfile, deps_file) = output_file;
1866 CPP_OPTION (pfile, print_deps_append) = 1;
1868 else if (CPP_OPTION (pfile, deps_file) == 0)
1869 /* If -M or -MM was seen without -MF, default output to wherever
1870 was specified with -o. out_fname is non-NULL here. */
1871 CPP_OPTION (pfile, deps_file) = CPP_OPTION (pfile, out_fname);
1874 /* Handle --help output. */
1875 static void
1876 print_help ()
1878 /* To keep the lines from getting too long for some compilers, limit
1879 to about 500 characters (6 lines) per chunk. */
1880 fputs (_("\
1881 Switches:\n\
1882 -include <file> Include the contents of <file> before other files\n\
1883 -imacros <file> Accept definition of macros in <file>\n\
1884 -iprefix <path> Specify <path> as a prefix for next two options\n\
1885 -iwithprefix <dir> Add <dir> to the end of the system include path\n\
1886 -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\
1887 -isystem <dir> Add <dir> to the start of the system include path\n\
1888 "), stdout);
1889 fputs (_("\
1890 -idirafter <dir> Add <dir> to the end of the system include path\n\
1891 -I <dir> Add <dir> to the end of the main include path\n\
1892 -I- Fine-grained include path control; see info docs\n\
1893 -nostdinc Do not search system include directories\n\
1894 (dirs specified with -isystem will still be used)\n\
1895 -nostdinc++ Do not search system include directories for C++\n\
1896 -o <file> Put output into <file>\n\
1897 "), stdout);
1898 fputs (_("\
1899 -pedantic Issue all warnings demanded by strict ISO C\n\
1900 -pedantic-errors Issue -pedantic warnings as errors instead\n\
1901 -trigraphs Support ISO C trigraphs\n\
1902 -lang-c Assume that the input sources are in C\n\
1903 -lang-c89 Assume that the input sources are in C89\n\
1904 "), stdout);
1905 fputs (_("\
1906 -lang-c++ Assume that the input sources are in C++\n\
1907 -lang-objc Assume that the input sources are in ObjectiveC\n\
1908 -lang-asm Assume that the input sources are in assembler\n\
1909 "), stdout);
1910 fputs (_("\
1911 -std=<std name> Specify the conformance standard; one of:\n\
1912 gnu89, gnu99, c89, c99, iso9899:1990,\n\
1913 iso9899:199409, iso9899:1999\n\
1914 -w Inhibit warning messages\n\
1915 -Wtrigraphs Warn if trigraphs are encountered\n\
1916 -Wno-trigraphs Do not warn about trigraphs\n\
1917 -Wcomment{s} Warn if one comment starts inside another\n\
1918 "), stdout);
1919 fputs (_("\
1920 -Wno-comment{s} Do not warn about comments\n\
1921 -Wtraditional Warn about features not present in traditional C\n\
1922 -Wno-traditional Do not warn about traditional C\n\
1923 -Wundef Warn if an undefined macro is used by #if\n\
1924 -Wno-undef Do not warn about testing undefined macros\n\
1925 -Wimport Warn about the use of the #import directive\n\
1926 "), stdout);
1927 fputs (_("\
1928 -Wno-import Do not warn about the use of #import\n\
1929 -Werror Treat all warnings as errors\n\
1930 -Wno-error Do not treat warnings as errors\n\
1931 -Wsystem-headers Do not suppress warnings from system headers\n\
1932 -Wno-system-headers Suppress warnings from system headers\n\
1933 -Wall Enable all preprocessor warnings\n\
1934 "), stdout);
1935 fputs (_("\
1936 -M Generate make dependencies\n\
1937 -MM As -M, but ignore system header files\n\
1938 -MD Generate make dependencies and compile\n\
1939 -MMD As -MD, but ignore system header files\n\
1940 -MF <file> Write dependency output to the given file\n\
1941 -MG Treat missing header file as generated files\n\
1942 "), stdout);
1943 fputs (_("\
1944 -MP Generate phony targets for all headers\n\
1945 -MQ <target> Add a MAKE-quoted target\n\
1946 -MT <target> Add an unquoted target\n\
1947 "), stdout);
1948 fputs (_("\
1949 -D<macro> Define a <macro> with string '1' as its value\n\
1950 -D<macro>=<val> Define a <macro> with <val> as its value\n\
1951 -A<question>=<answer> Assert the <answer> to <question>\n\
1952 -A-<question>=<answer> Disable the <answer> to <question>\n\
1953 -U<macro> Undefine <macro> \n\
1954 -v Display the version number\n\
1955 "), stdout);
1956 fputs (_("\
1957 -H Print the name of header files as they are used\n\
1958 -C Do not discard comments\n\
1959 -dM Display a list of macro definitions active at end\n\
1960 -dD Preserve macro definitions in output\n\
1961 -dN As -dD except that only the names are preserved\n\
1962 -dI Include #include directives in the output\n\
1963 "), stdout);
1964 fputs (_("\
1965 -fpreprocessed Treat the input file as already preprocessed\n\
1966 -ftabstop=<number> Distance between tab stops for column reporting\n\
1967 -P Do not generate #line directives\n\
1968 -$ Do not allow '$' in identifiers\n\
1969 -remap Remap file names when including files\n\
1970 --version Display version information\n\
1971 -h or --help Display this information\n\
1972 "), stdout);