gcse.c (do_local_cprop): Do not extend lifetimes of registers set by do_local_cprop.
[official-gcc.git] / gcc / cppinit.c
blob72e26ce87de5c3f8be4968b625ad76b1f8c00115
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;
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 new->sysp = cxx_aware ? 1 : 2;
256 else
257 new->sysp = 0;
258 new->name_map = NULL;
259 new->next = NULL;
261 switch (path)
263 case BRACKET: APPEND (pend, brack, new); break;
264 case SYSTEM: APPEND (pend, systm, new); break;
265 case AFTER: APPEND (pend, after, new); break;
269 /* Handle a duplicated include path. PREV is the link in the chain
270 before the duplicate. The duplicate is removed from the chain and
271 freed. Returns PREV. */
272 static struct search_path *
273 remove_dup_dir (pfile, prev)
274 cpp_reader *pfile;
275 struct search_path *prev;
277 struct search_path *cur = prev->next;
279 if (CPP_OPTION (pfile, verbose))
280 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), cur->name);
282 prev->next = cur->next;
283 free ((PTR) cur->name);
284 free (cur);
286 return prev;
289 /* Remove duplicate directories from a chain. Returns the tail of the
290 chain, or NULL if the chain is empty. This algorithm is quadratic
291 in the number of -I switches, which is acceptable since there
292 aren't usually that many of them. */
293 static struct search_path *
294 remove_dup_dirs (pfile, head)
295 cpp_reader *pfile;
296 struct search_path *head;
298 struct search_path *prev = NULL, *cur, *other;
300 for (cur = head; cur; cur = cur->next)
302 for (other = head; other != cur; other = other->next)
303 if (INO_T_EQ (cur->ino, other->ino) && cur->dev == other->dev)
305 if (cur->sysp && !other->sysp)
307 cpp_error (pfile, DL_WARNING,
308 "changing search order for system directory \"%s\"",
309 cur->name);
310 if (strcmp (cur->name, other->name))
311 cpp_error (pfile, DL_WARNING,
312 " as it is the same as non-system directory \"%s\"",
313 other->name);
314 else
315 cpp_error (pfile, DL_WARNING,
316 " as it has already been specified as a non-system directory");
318 cur = remove_dup_dir (pfile, prev);
319 break;
321 prev = cur;
324 return prev;
327 /* Merge the four include chains together in the order quote, bracket,
328 system, after. Remove duplicate dirs (as determined by
329 INO_T_EQ()). The system_include and after_include chains are never
330 referred to again after this function; all access is through the
331 bracket_include path. */
332 static void
333 merge_include_chains (pfile)
334 cpp_reader *pfile;
336 struct search_path *quote, *brack, *systm, *qtail;
338 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
340 quote = pend->quote_head;
341 brack = pend->brack_head;
342 systm = pend->systm_head;
343 qtail = pend->quote_tail;
345 /* Paste together bracket, system, and after include chains. */
346 if (systm)
347 pend->systm_tail->next = pend->after_head;
348 else
349 systm = pend->after_head;
351 if (brack)
352 pend->brack_tail->next = systm;
353 else
354 brack = systm;
356 /* This is a bit tricky. First we drop dupes from the quote-include
357 list. Then we drop dupes from the bracket-include list.
358 Finally, if qtail and brack are the same directory, we cut out
359 brack and move brack up to point to qtail.
361 We can't just merge the lists and then uniquify them because
362 then we may lose directories from the <> search path that should
363 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
364 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
365 -Ibar -I- -Ifoo -Iquux. */
367 remove_dup_dirs (pfile, brack);
368 qtail = remove_dup_dirs (pfile, quote);
370 if (quote)
372 qtail->next = brack;
374 /* If brack == qtail, remove brack as it's simpler. */
375 if (brack && INO_T_EQ (qtail->ino, brack->ino)
376 && qtail->dev == brack->dev)
377 brack = remove_dup_dir (pfile, qtail);
379 else
380 quote = brack;
382 CPP_OPTION (pfile, quote_include) = quote;
383 CPP_OPTION (pfile, bracket_include) = brack;
386 /* A set of booleans indicating what CPP features each source language
387 requires. */
388 struct lang_flags
390 char c99;
391 char cplusplus;
392 char extended_numbers;
393 char trigraphs;
394 char dollars_in_ident;
395 char cplusplus_comments;
396 char digraphs;
399 /* ??? Enable $ in identifiers in assembly? */
400 static const struct lang_flags lang_defaults[] =
401 { /* c99 c++ xnum trig dollar c++comm digr */
402 /* GNUC89 */ { 0, 0, 1, 0, 1, 1, 1 },
403 /* GNUC99 */ { 1, 0, 1, 0, 1, 1, 1 },
404 /* STDC89 */ { 0, 0, 0, 1, 0, 0, 0 },
405 /* STDC94 */ { 0, 0, 0, 1, 0, 0, 1 },
406 /* STDC99 */ { 1, 0, 1, 1, 0, 1, 1 },
407 /* GNUCXX */ { 0, 1, 1, 0, 1, 1, 1 },
408 /* CXX98 */ { 0, 1, 1, 1, 0, 1, 1 },
409 /* ASM */ { 0, 0, 1, 0, 0, 1, 0 }
412 /* Sets internal flags correctly for a given language. */
413 static void
414 set_lang (pfile, lang)
415 cpp_reader *pfile;
416 enum c_lang lang;
418 const struct lang_flags *l = &lang_defaults[(int) lang];
420 CPP_OPTION (pfile, lang) = lang;
422 CPP_OPTION (pfile, c99) = l->c99;
423 CPP_OPTION (pfile, cplusplus) = l->cplusplus;
424 CPP_OPTION (pfile, extended_numbers) = l->extended_numbers;
425 CPP_OPTION (pfile, trigraphs) = l->trigraphs;
426 CPP_OPTION (pfile, dollars_in_ident) = l->dollars_in_ident;
427 CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments;
428 CPP_OPTION (pfile, digraphs) = l->digraphs;
431 #ifdef HOST_EBCDIC
432 static int opt_comp PARAMS ((const void *, const void *));
434 /* Run-time sorting of options array. */
435 static int
436 opt_comp (p1, p2)
437 const void *p1, *p2;
439 return strcmp (((struct cl_option *) p1)->opt_text,
440 ((struct cl_option *) p2)->opt_text);
442 #endif
444 /* init initializes library global state. It might not need to
445 do anything depending on the platform and compiler. */
446 static void
447 init_library ()
449 static int initialized = 0;
451 if (! initialized)
453 initialized = 1;
455 #ifdef HOST_EBCDIC
456 /* For non-ASCII hosts, the cl_options array needs to be sorted at
457 runtime. */
458 qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
459 #endif
461 /* Set up the trigraph map. This doesn't need to do anything if
462 we were compiled with a compiler that supports C99 designated
463 initializers. */
464 init_trigraph_map ();
468 /* Initialize a cpp_reader structure. */
469 cpp_reader *
470 cpp_create_reader (lang)
471 enum c_lang lang;
473 cpp_reader *pfile;
475 /* Initialise this instance of the library if it hasn't been already. */
476 init_library ();
478 pfile = (cpp_reader *) xcalloc (1, sizeof (cpp_reader));
480 set_lang (pfile, lang);
481 CPP_OPTION (pfile, warn_import) = 1;
482 CPP_OPTION (pfile, warn_multichar) = 1;
483 CPP_OPTION (pfile, discard_comments) = 1;
484 CPP_OPTION (pfile, discard_comments_in_macro_exp) = 1;
485 CPP_OPTION (pfile, show_column) = 1;
486 CPP_OPTION (pfile, tabstop) = 8;
487 CPP_OPTION (pfile, operator_names) = 1;
488 CPP_OPTION (pfile, warn_endif_labels) = 1;
490 CPP_OPTION (pfile, pending) =
491 (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
493 /* Default CPP arithmetic to something sensible for the host for the
494 benefit of dumb users like fix-header. */
495 CPP_OPTION (pfile, precision) = CHAR_BIT * sizeof (long);
496 CPP_OPTION (pfile, char_precision) = CHAR_BIT;
497 CPP_OPTION (pfile, wchar_precision) = CHAR_BIT * sizeof (int);
498 CPP_OPTION (pfile, int_precision) = CHAR_BIT * sizeof (int);
499 CPP_OPTION (pfile, unsigned_char) = 0;
500 CPP_OPTION (pfile, unsigned_wchar) = 1;
502 /* It's simplest to just create this struct whether or not it will
503 be needed. */
504 pfile->deps = deps_init ();
506 /* Initialise the line map. Start at logical line 1, so we can use
507 a line number of zero for special states. */
508 init_line_maps (&pfile->line_maps);
509 pfile->line = 1;
511 /* Initialize lexer state. */
512 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
514 /* Set up static tokens. */
515 pfile->avoid_paste.type = CPP_PADDING;
516 pfile->avoid_paste.val.source = NULL;
517 pfile->eof.type = CPP_EOF;
518 pfile->eof.flags = 0;
520 /* Create a token buffer for the lexer. */
521 _cpp_init_tokenrun (&pfile->base_run, 250);
522 pfile->cur_run = &pfile->base_run;
523 pfile->cur_token = pfile->base_run.base;
525 /* Initialise the base context. */
526 pfile->context = &pfile->base_context;
527 pfile->base_context.macro = 0;
528 pfile->base_context.prev = pfile->base_context.next = 0;
530 /* Aligned and unaligned storage. */
531 pfile->a_buff = _cpp_get_buff (pfile, 0);
532 pfile->u_buff = _cpp_get_buff (pfile, 0);
534 /* The expression parser stack. */
535 _cpp_expand_op_stack (pfile);
537 /* Initialise the buffer obstack. */
538 gcc_obstack_init (&pfile->buffer_ob);
540 _cpp_init_includes (pfile);
542 return pfile;
545 /* Free resources used by PFILE. Accessing PFILE after this function
546 returns leads to undefined behaviour. Returns the error count. */
548 cpp_destroy (pfile)
549 cpp_reader *pfile;
551 int result;
552 struct search_path *dir, *dirn;
553 cpp_context *context, *contextn;
554 tokenrun *run, *runn;
556 free_chain (CPP_OPTION (pfile, pending)->include_head);
557 free (CPP_OPTION (pfile, pending));
558 free (pfile->op_stack);
560 while (CPP_BUFFER (pfile) != NULL)
561 _cpp_pop_buffer (pfile);
563 if (pfile->out.base)
564 free (pfile->out.base);
566 if (pfile->macro_buffer)
568 free ((PTR) pfile->macro_buffer);
569 pfile->macro_buffer = NULL;
570 pfile->macro_buffer_len = 0;
573 deps_free (pfile->deps);
574 obstack_free (&pfile->buffer_ob, 0);
576 _cpp_destroy_hashtable (pfile);
577 _cpp_cleanup_includes (pfile);
579 _cpp_free_buff (pfile->a_buff);
580 _cpp_free_buff (pfile->u_buff);
581 _cpp_free_buff (pfile->free_buffs);
583 for (run = &pfile->base_run; run; run = runn)
585 runn = run->next;
586 free (run->base);
587 if (run != &pfile->base_run)
588 free (run);
591 for (dir = CPP_OPTION (pfile, quote_include); dir; dir = dirn)
593 dirn = dir->next;
594 free ((PTR) dir->name);
595 free (dir);
598 for (context = pfile->base_context.next; context; context = contextn)
600 contextn = context->next;
601 free (context);
604 free_line_maps (&pfile->line_maps);
606 result = pfile->errors;
607 free (pfile);
609 return result;
612 /* This structure defines one built-in identifier. A node will be
613 entered in the hash table under the name NAME, with value VALUE.
615 There are two tables of these. builtin_array holds all the
616 "builtin" macros: these are handled by builtin_macro() in
617 cppmacro.c. Builtin is somewhat of a misnomer -- the property of
618 interest is that these macros require special code to compute their
619 expansions. The value is a "builtin_type" enumerator.
621 operator_array holds the C++ named operators. These are keywords
622 which act as aliases for punctuators. In C++, they cannot be
623 altered through #define, and #if recognizes them as operators. In
624 C, these are not entered into the hash table at all (but see
625 <iso646.h>). The value is a token-type enumerator. */
626 struct builtin
628 const uchar *name;
629 unsigned short len;
630 unsigned short value;
633 #define B(n, t) { DSC(n), t }
634 static const struct builtin builtin_array[] =
636 B("__TIME__", BT_TIME),
637 B("__DATE__", BT_DATE),
638 B("__FILE__", BT_FILE),
639 B("__BASE_FILE__", BT_BASE_FILE),
640 B("__LINE__", BT_SPECLINE),
641 B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL),
642 /* Keep builtins not used for -traditional-cpp at the end, and
643 update init_builtins() if any more are added. */
644 B("_Pragma", BT_PRAGMA),
645 B("__STDC__", BT_STDC),
648 static const struct builtin operator_array[] =
650 B("and", CPP_AND_AND),
651 B("and_eq", CPP_AND_EQ),
652 B("bitand", CPP_AND),
653 B("bitor", CPP_OR),
654 B("compl", CPP_COMPL),
655 B("not", CPP_NOT),
656 B("not_eq", CPP_NOT_EQ),
657 B("or", CPP_OR_OR),
658 B("or_eq", CPP_OR_EQ),
659 B("xor", CPP_XOR),
660 B("xor_eq", CPP_XOR_EQ)
662 #undef B
664 /* Mark the C++ named operators in the hash table. */
665 static void
666 mark_named_operators (pfile)
667 cpp_reader *pfile;
669 const struct builtin *b;
671 for (b = operator_array;
672 b < (operator_array + ARRAY_SIZE (operator_array));
673 b++)
675 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
676 hp->flags |= NODE_OPERATOR;
677 hp->value.operator = b->value;
681 /* Subroutine of cpp_read_main_file; reads the builtins table above and
682 enters them, and language-specific macros, into the hash table. */
683 static void
684 init_builtins (pfile)
685 cpp_reader *pfile;
687 const struct builtin *b;
688 size_t n = ARRAY_SIZE (builtin_array);
690 if (CPP_OPTION (pfile, traditional))
691 n -= 2;
693 for(b = builtin_array; b < builtin_array + n; b++)
695 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
696 hp->type = NT_MACRO;
697 hp->flags |= NODE_BUILTIN | NODE_WARN;
698 hp->value.builtin = b->value;
701 if (CPP_OPTION (pfile, cplusplus))
702 _cpp_define_builtin (pfile, "__cplusplus 1");
703 else if (CPP_OPTION (pfile, objc))
704 _cpp_define_builtin (pfile, "__OBJC__ 1");
705 else if (CPP_OPTION (pfile, lang) == CLK_ASM)
706 _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
708 if (CPP_OPTION (pfile, lang) == CLK_STDC94)
709 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
710 else if (CPP_OPTION (pfile, c99))
711 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
713 if (pfile->cb.register_builtins)
714 (*pfile->cb.register_builtins) (pfile);
717 /* And another subroutine. This one sets up the standard include path. */
718 static void
719 init_standard_includes (pfile)
720 cpp_reader *pfile;
722 char *path;
723 const struct default_include *p;
724 const char *specd_prefix = CPP_OPTION (pfile, include_prefix);
726 /* Several environment variables may add to the include search path.
727 CPATH specifies an additional list of directories to be searched
728 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
729 etc. specify an additional list of directories to be searched as
730 if specified with -isystem, for the language indicated. */
732 GET_ENV_PATH_LIST (path, "CPATH");
733 if (path != 0 && *path != 0)
734 path_include (pfile, path, BRACKET);
736 switch ((CPP_OPTION (pfile, objc) << 1) + CPP_OPTION (pfile, cplusplus))
738 case 0:
739 GET_ENV_PATH_LIST (path, "C_INCLUDE_PATH");
740 break;
741 case 1:
742 GET_ENV_PATH_LIST (path, "CPLUS_INCLUDE_PATH");
743 break;
744 case 2:
745 GET_ENV_PATH_LIST (path, "OBJC_INCLUDE_PATH");
746 break;
747 case 3:
748 GET_ENV_PATH_LIST (path, "OBJCPLUS_INCLUDE_PATH");
749 break;
751 if (path != 0 && *path != 0)
752 path_include (pfile, path, SYSTEM);
754 /* Search "translated" versions of GNU directories.
755 These have /usr/local/lib/gcc... replaced by specd_prefix. */
756 if (specd_prefix != 0 && cpp_GCC_INCLUDE_DIR_len)
758 /* Remove the `include' from /usr/local/lib/gcc.../include.
759 GCC_INCLUDE_DIR will always end in /include. */
760 int default_len = cpp_GCC_INCLUDE_DIR_len;
761 char *default_prefix = (char *) alloca (default_len + 1);
762 int specd_len = strlen (specd_prefix);
764 memcpy (default_prefix, cpp_GCC_INCLUDE_DIR, default_len);
765 default_prefix[default_len] = '\0';
767 for (p = cpp_include_defaults; p->fname; p++)
769 /* Some standard dirs are only for C++. */
770 if (!p->cplusplus
771 || (CPP_OPTION (pfile, cplusplus)
772 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
774 /* Does this dir start with the prefix? */
775 if (!memcmp (p->fname, default_prefix, default_len))
777 /* Yes; change prefix and add to search list. */
778 int flen = strlen (p->fname);
779 int this_len = specd_len + flen - default_len;
780 char *str = (char *) xmalloc (this_len + 1);
781 memcpy (str, specd_prefix, specd_len);
782 memcpy (str + specd_len,
783 p->fname + default_len,
784 flen - default_len + 1);
786 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
792 /* Search ordinary names for GNU include directories. */
793 for (p = cpp_include_defaults; p->fname; p++)
795 /* Some standard dirs are only for C++. */
796 if (!p->cplusplus
797 || (CPP_OPTION (pfile, cplusplus)
798 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
800 char *str = update_path (p->fname, p->component);
801 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
806 /* Pushes a command line -imacro and -include file indicated by P onto
807 the buffer stack. Returns non-zero if successful. */
808 static bool
809 push_include (pfile, p)
810 cpp_reader *pfile;
811 struct pending_option *p;
813 cpp_token header;
815 /* Later: maybe update this to use the #include "" search path
816 if cpp_read_file fails. */
817 header.type = CPP_STRING;
818 header.val.str.text = (const unsigned char *) p->arg;
819 header.val.str.len = strlen (p->arg);
820 /* Make the command line directive take up a line. */
821 pfile->line++;
823 return _cpp_execute_include (pfile, &header, IT_CMDLINE);
826 /* Frees a pending_option chain. */
827 static void
828 free_chain (head)
829 struct pending_option *head;
831 struct pending_option *next;
833 while (head)
835 next = head->next;
836 free (head);
837 head = next;
841 /* Sanity-checks are dependent on command-line options, so it is
842 called as a subroutine of cpp_read_main_file (). */
843 #if ENABLE_CHECKING
844 static void sanity_checks PARAMS ((cpp_reader *));
845 static void sanity_checks (pfile)
846 cpp_reader *pfile;
848 cppchar_t test = 0;
849 size_t max_precision = 2 * CHAR_BIT * sizeof (cpp_num_part);
851 /* Sanity checks for assumptions about CPP arithmetic and target
852 type precisions made by cpplib. */
853 test--;
854 if (test < 1)
855 cpp_error (pfile, DL_ICE, "cppchar_t must be an unsigned type");
857 if (CPP_OPTION (pfile, precision) > max_precision)
858 cpp_error (pfile, DL_ICE,
859 "preprocessor arithmetic has maximum precision of %lu bits; target requires %lu bits",
860 (unsigned long) max_precision,
861 (unsigned long) CPP_OPTION (pfile, precision));
863 if (CPP_OPTION (pfile, precision) < CPP_OPTION (pfile, int_precision))
864 cpp_error (pfile, DL_ICE,
865 "CPP arithmetic must be at least as precise as a target int");
867 if (CPP_OPTION (pfile, char_precision) < 8)
868 cpp_error (pfile, DL_ICE, "target char is less than 8 bits wide");
870 if (CPP_OPTION (pfile, wchar_precision) < CPP_OPTION (pfile, char_precision))
871 cpp_error (pfile, DL_ICE,
872 "target wchar_t is narrower than target char");
874 if (CPP_OPTION (pfile, int_precision) < CPP_OPTION (pfile, char_precision))
875 cpp_error (pfile, DL_ICE,
876 "target int is narrower than target char");
878 /* This is assumed in eval_token() and could be fixed if necessary. */
879 if (sizeof (cppchar_t) > sizeof (cpp_num_part))
880 cpp_error (pfile, DL_ICE, "CPP half-integer narrower than CPP character");
882 if (CPP_OPTION (pfile, wchar_precision) > BITS_PER_CPPCHAR_T)
883 cpp_error (pfile, DL_ICE,
884 "CPP on this host cannot handle wide character constants over %lu bits, but the target requires %lu bits",
885 (unsigned long) BITS_PER_CPPCHAR_T,
886 (unsigned long) CPP_OPTION (pfile, wchar_precision));
888 #else
889 # define sanity_checks(PFILE)
890 #endif
892 /* This is called after options have been parsed, and partially
893 processed. Setup for processing input from the file named FNAME,
894 or stdin if it is the empty string. Return the original filename
895 on success (e.g. foo.i->foo.c), or NULL on failure. */
896 const char *
897 cpp_read_main_file (pfile, fname, table)
898 cpp_reader *pfile;
899 const char *fname;
900 hash_table *table;
902 sanity_checks (pfile);
904 /* The front ends don't set up the hash table until they have
905 finished processing the command line options, so initializing the
906 hashtable is deferred until now. */
907 _cpp_init_hashtable (pfile, table);
909 /* Set up the include search path now. */
910 if (! CPP_OPTION (pfile, no_standard_includes))
911 init_standard_includes (pfile);
913 merge_include_chains (pfile);
915 /* With -v, print the list of dirs to search. */
916 if (CPP_OPTION (pfile, verbose))
918 struct search_path *l;
919 fprintf (stderr, _("#include \"...\" search starts here:\n"));
920 for (l = CPP_OPTION (pfile, quote_include); l; l = l->next)
922 if (l == CPP_OPTION (pfile, bracket_include))
923 fprintf (stderr, _("#include <...> search starts here:\n"));
924 fprintf (stderr, " %s\n", l->name);
926 fprintf (stderr, _("End of search list.\n"));
929 if (CPP_OPTION (pfile, print_deps))
930 /* Set the default target (if there is none already). */
931 deps_add_default_target (pfile->deps, fname);
933 /* Open the main input file. */
934 if (!_cpp_read_file (pfile, fname))
935 return NULL;
937 /* Set this after cpp_post_options so the client can change the
938 option if it wishes, and after stacking the main file so we don't
939 trace the main file. */
940 pfile->line_maps.trace_includes = CPP_OPTION (pfile, print_include_names);
942 /* For foo.i, read the original filename foo.c now, for the benefit
943 of the front ends. */
944 if (CPP_OPTION (pfile, preprocessed))
945 read_original_filename (pfile);
946 /* Overlay an empty buffer to seed traditional preprocessing. */
947 else if (CPP_OPTION (pfile, traditional)
948 && !CPP_OPTION (pfile, preprocess_only))
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 init_builtins (pfile);
1003 _cpp_do_file_change (pfile, LC_RENAME, _("<command line>"), 1, 0);
1004 for (p = CPP_OPTION (pfile, pending)->directive_head; p; p = p->next)
1005 (*p->handler) (pfile, p->arg);
1007 /* Scan -imacros files after -D, -U, but before -include.
1008 pfile->next_include_file is NULL, so _cpp_pop_buffer does not
1009 push -include files. */
1010 for (p = CPP_OPTION (pfile, pending)->imacros_head; p; p = p->next)
1011 if (push_include (pfile, p))
1012 cpp_scan_nooutput (pfile);
1014 pfile->next_include_file = &CPP_OPTION (pfile, pending)->include_head;
1015 _cpp_maybe_push_include_file (pfile);
1018 free_chain (CPP_OPTION (pfile, pending)->imacros_head);
1019 free_chain (CPP_OPTION (pfile, pending)->directive_head);
1022 /* Push the next buffer on the stack given by -include, if any. */
1023 void
1024 _cpp_maybe_push_include_file (pfile)
1025 cpp_reader *pfile;
1027 if (pfile->next_include_file)
1029 struct pending_option *head = *pfile->next_include_file;
1031 while (head && !push_include (pfile, head))
1032 head = head->next;
1034 if (head)
1035 pfile->next_include_file = &head->next;
1036 else
1038 /* All done; restore the line map from <command line>. */
1039 _cpp_do_file_change (pfile, LC_RENAME,
1040 pfile->line_maps.maps[0].to_file, 1, 0);
1041 /* Don't come back here again. */
1042 pfile->next_include_file = NULL;
1047 /* Use mkdeps.c to output dependency information. */
1048 static void
1049 output_deps (pfile)
1050 cpp_reader *pfile;
1052 /* Stream on which to print the dependency information. */
1053 FILE *deps_stream = 0;
1054 const char *const deps_mode =
1055 CPP_OPTION (pfile, print_deps_append) ? "a" : "w";
1057 if (CPP_OPTION (pfile, deps_file)[0] == '\0')
1058 deps_stream = stdout;
1059 else
1061 deps_stream = fopen (CPP_OPTION (pfile, deps_file), deps_mode);
1062 if (deps_stream == 0)
1064 cpp_errno (pfile, DL_ERROR, CPP_OPTION (pfile, deps_file));
1065 return;
1069 deps_write (pfile->deps, deps_stream, 72);
1071 if (CPP_OPTION (pfile, deps_phony_targets))
1072 deps_phony_targets (pfile->deps, deps_stream);
1074 /* Don't close stdout. */
1075 if (deps_stream != stdout)
1077 if (ferror (deps_stream) || fclose (deps_stream) != 0)
1078 cpp_error (pfile, DL_ERROR, "I/O error on output");
1082 /* This is called at the end of preprocessing. It pops the
1083 last buffer and writes dependency output. It should also
1084 clear macro definitions, such that you could call cpp_start_read
1085 with a new filename to restart processing. */
1086 void
1087 cpp_finish (pfile)
1088 cpp_reader *pfile;
1090 /* cpplex.c leaves the final buffer on the stack. This it so that
1091 it returns an unending stream of CPP_EOFs to the client. If we
1092 popped the buffer, we'd dereference a NULL buffer pointer and
1093 segfault. It's nice to allow the client to do worry-free excess
1094 cpp_get_token calls. */
1095 while (pfile->buffer)
1096 _cpp_pop_buffer (pfile);
1098 /* Don't write the deps file if preprocessing has failed. */
1099 if (CPP_OPTION (pfile, print_deps) && pfile->errors == 0)
1100 output_deps (pfile);
1102 /* Report on headers that could use multiple include guards. */
1103 if (CPP_OPTION (pfile, print_include_names))
1104 _cpp_report_missing_guards (pfile);
1107 /* Add a directive to be handled later in the initialization phase. */
1108 static void
1109 new_pending_directive (pend, text, handler)
1110 struct cpp_pending *pend;
1111 const char *text;
1112 cl_directive_handler handler;
1114 struct pending_option *o = (struct pending_option *)
1115 xmalloc (sizeof (struct pending_option));
1117 o->arg = text;
1118 o->next = NULL;
1119 o->handler = handler;
1120 APPEND (pend, directive, o);
1123 /* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
1124 I.e. a const string initializer with parens around it. That is
1125 what N_("string") resolves to, so we make no_* be macros instead. */
1126 #define no_arg N_("argument missing after %s")
1127 #define no_ass N_("assertion missing after %s")
1128 #define no_dir N_("directory name missing after %s")
1129 #define no_fil N_("file name missing after %s")
1130 #define no_mac N_("macro name missing after %s")
1131 #define no_pth N_("path name missing after %s")
1132 #define no_num N_("number missing after %s")
1133 #define no_tgt N_("target missing after %s")
1135 /* This is the list of all command line options, with the leading
1136 "-" removed. It must be sorted in ASCII collating order. */
1137 #define COMMAND_LINE_OPTIONS \
1138 DEF_OPT("$", 0, OPT_dollar) \
1139 DEF_OPT("-help", 0, OPT__help) \
1140 DEF_OPT("-target-help", 0, OPT_target__help) \
1141 DEF_OPT("-version", 0, OPT__version) \
1142 DEF_OPT("A", no_ass, OPT_A) \
1143 DEF_OPT("C", 0, OPT_C) \
1144 DEF_OPT("CC", 0, OPT_CC) \
1145 DEF_OPT("D", no_mac, OPT_D) \
1146 DEF_OPT("H", 0, OPT_H) \
1147 DEF_OPT("I", no_dir, OPT_I) \
1148 DEF_OPT("M", 0, OPT_M) \
1149 DEF_OPT("MD", no_fil, OPT_MD) \
1150 DEF_OPT("MF", no_fil, OPT_MF) \
1151 DEF_OPT("MG", 0, OPT_MG) \
1152 DEF_OPT("MM", 0, OPT_MM) \
1153 DEF_OPT("MMD", no_fil, OPT_MMD) \
1154 DEF_OPT("MP", 0, OPT_MP) \
1155 DEF_OPT("MQ", no_tgt, OPT_MQ) \
1156 DEF_OPT("MT", no_tgt, OPT_MT) \
1157 DEF_OPT("P", 0, OPT_P) \
1158 DEF_OPT("U", no_mac, OPT_U) \
1159 DEF_OPT("Wall", 0, OPT_Wall) \
1160 DEF_OPT("Wcomment", 0, OPT_Wcomment) \
1161 DEF_OPT("Wcomments", 0, OPT_Wcomments) \
1162 DEF_OPT("Wendif-labels", 0, OPT_Wendif_labels) \
1163 DEF_OPT("Werror", 0, OPT_Werror) \
1164 DEF_OPT("Wimport", 0, OPT_Wimport) \
1165 DEF_OPT("Wno-comment", 0, OPT_Wno_comment) \
1166 DEF_OPT("Wno-comments", 0, OPT_Wno_comments) \
1167 DEF_OPT("Wno-endif-labels", 0, OPT_Wno_endif_labels) \
1168 DEF_OPT("Wno-error", 0, OPT_Wno_error) \
1169 DEF_OPT("Wno-import", 0, OPT_Wno_import) \
1170 DEF_OPT("Wno-system-headers", 0, OPT_Wno_system_headers) \
1171 DEF_OPT("Wno-traditional", 0, OPT_Wno_traditional) \
1172 DEF_OPT("Wno-trigraphs", 0, OPT_Wno_trigraphs) \
1173 DEF_OPT("Wno-undef", 0, OPT_Wno_undef) \
1174 DEF_OPT("Wsystem-headers", 0, OPT_Wsystem_headers) \
1175 DEF_OPT("Wtraditional", 0, OPT_Wtraditional) \
1176 DEF_OPT("Wtrigraphs", 0, OPT_Wtrigraphs) \
1177 DEF_OPT("Wundef", 0, OPT_Wundef) \
1178 DEF_OPT("d", no_arg, OPT_d) \
1179 DEF_OPT("fno-operator-names", 0, OPT_fno_operator_names) \
1180 DEF_OPT("fno-preprocessed", 0, OPT_fno_preprocessed) \
1181 DEF_OPT("fno-show-column", 0, OPT_fno_show_column) \
1182 DEF_OPT("fpreprocessed", 0, OPT_fpreprocessed) \
1183 DEF_OPT("fshow-column", 0, OPT_fshow_column) \
1184 DEF_OPT("ftabstop=", no_num, OPT_ftabstop) \
1185 DEF_OPT("h", 0, OPT_h) \
1186 DEF_OPT("idirafter", no_dir, OPT_idirafter) \
1187 DEF_OPT("imacros", no_fil, OPT_imacros) \
1188 DEF_OPT("include", no_fil, OPT_include) \
1189 DEF_OPT("iprefix", no_pth, OPT_iprefix) \
1190 DEF_OPT("isystem", no_dir, OPT_isystem) \
1191 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix) \
1192 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore) \
1193 DEF_OPT("lang-asm", 0, OPT_lang_asm) \
1194 DEF_OPT("lang-c", 0, OPT_lang_c) \
1195 DEF_OPT("lang-c++", 0, OPT_lang_cplusplus) \
1196 DEF_OPT("lang-c89", 0, OPT_lang_c89) \
1197 DEF_OPT("lang-objc", 0, OPT_lang_objc) \
1198 DEF_OPT("nostdinc", 0, OPT_nostdinc) \
1199 DEF_OPT("nostdinc++", 0, OPT_nostdincplusplus) \
1200 DEF_OPT("o", no_fil, OPT_o) \
1201 DEF_OPT("pedantic", 0, OPT_pedantic) \
1202 DEF_OPT("pedantic-errors", 0, OPT_pedantic_errors) \
1203 DEF_OPT("remap", 0, OPT_remap) \
1204 DEF_OPT("std=c++98", 0, OPT_std_cplusplus98) \
1205 DEF_OPT("std=c89", 0, OPT_std_c89) \
1206 DEF_OPT("std=c99", 0, OPT_std_c99) \
1207 DEF_OPT("std=c9x", 0, OPT_std_c9x) \
1208 DEF_OPT("std=gnu89", 0, OPT_std_gnu89) \
1209 DEF_OPT("std=gnu99", 0, OPT_std_gnu99) \
1210 DEF_OPT("std=gnu9x", 0, OPT_std_gnu9x) \
1211 DEF_OPT("std=iso9899:1990", 0, OPT_std_iso9899_1990) \
1212 DEF_OPT("std=iso9899:199409", 0, OPT_std_iso9899_199409) \
1213 DEF_OPT("std=iso9899:1999", 0, OPT_std_iso9899_1999) \
1214 DEF_OPT("std=iso9899:199x", 0, OPT_std_iso9899_199x) \
1215 DEF_OPT("traditional-cpp", 0, OPT_traditional_cpp) \
1216 DEF_OPT("trigraphs", 0, OPT_trigraphs) \
1217 DEF_OPT("v", 0, OPT_v) \
1218 DEF_OPT("w", 0, OPT_w)
1220 #define DEF_OPT(text, msg, code) code,
1221 enum opt_code
1223 COMMAND_LINE_OPTIONS
1224 N_OPTS
1226 #undef DEF_OPT
1228 struct cl_option
1230 const char *opt_text;
1231 const char *msg;
1232 size_t opt_len;
1233 enum opt_code opt_code;
1236 #define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
1237 #ifdef HOST_EBCDIC
1238 static struct cl_option cl_options[] =
1239 #else
1240 static const struct cl_option cl_options[] =
1241 #endif
1243 COMMAND_LINE_OPTIONS
1245 #undef DEF_OPT
1246 #undef COMMAND_LINE_OPTIONS
1248 /* Perform a binary search to find which, if any, option the given
1249 command-line matches. Returns its index in the option array,
1250 negative on failure. Complications arise since some options can be
1251 suffixed with an argument, and multiple complete matches can occur,
1252 e.g. -pedantic and -pedantic-errors. */
1253 static int
1254 parse_option (input)
1255 const char *input;
1257 unsigned int md, mn, mx;
1258 size_t opt_len;
1259 int comp;
1261 mn = 0;
1262 mx = N_OPTS;
1264 while (mx > mn)
1266 md = (mn + mx) / 2;
1268 opt_len = cl_options[md].opt_len;
1269 comp = memcmp (input, cl_options[md].opt_text, opt_len);
1271 if (comp > 0)
1272 mn = md + 1;
1273 else if (comp < 0)
1274 mx = md;
1275 else
1277 if (input[opt_len] == '\0')
1278 return md;
1279 /* We were passed more text. If the option takes an argument,
1280 we may match a later option or we may have been passed the
1281 argument. The longest possible option match succeeds.
1282 If the option takes no arguments we have not matched and
1283 continue the search (e.g. input="stdc++" match was "stdc"). */
1284 mn = md + 1;
1285 if (cl_options[md].msg)
1287 /* Scan forwards. If we get an exact match, return it.
1288 Otherwise, return the longest option-accepting match.
1289 This loops no more than twice with current options. */
1290 mx = md;
1291 for (; mn < (unsigned int) N_OPTS; mn++)
1293 opt_len = cl_options[mn].opt_len;
1294 if (memcmp (input, cl_options[mn].opt_text, opt_len))
1295 break;
1296 if (input[opt_len] == '\0')
1297 return mn;
1298 if (cl_options[mn].msg)
1299 mx = mn;
1301 return mx;
1306 return -1;
1309 /* Handle one command-line option in (argc, argv).
1310 Can be called multiple times, to handle multiple sets of options.
1311 Returns number of strings consumed. */
1313 cpp_handle_option (pfile, argc, argv)
1314 cpp_reader *pfile;
1315 int argc;
1316 char **argv;
1318 int i = 0;
1319 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
1321 /* Interpret "-" or a non-option as a file name. */
1322 if (argv[i][0] != '-' || argv[i][1] == '\0')
1324 if (CPP_OPTION (pfile, in_fname) == NULL)
1325 CPP_OPTION (pfile, in_fname) = argv[i];
1326 else if (CPP_OPTION (pfile, out_fname) == NULL)
1327 CPP_OPTION (pfile, out_fname) = argv[i];
1328 else
1329 cpp_error (pfile, DL_ERROR,
1330 "too many filenames. Type %s --help for usage info",
1331 progname);
1333 else
1335 enum opt_code opt_code;
1336 int opt_index;
1337 const char *arg = 0;
1339 /* Skip over '-'. */
1340 opt_index = parse_option (&argv[i][1]);
1341 if (opt_index < 0)
1342 return i;
1344 opt_code = cl_options[opt_index].opt_code;
1345 if (cl_options[opt_index].msg)
1347 arg = &argv[i][cl_options[opt_index].opt_len + 1];
1348 if (arg[0] == '\0')
1350 arg = argv[++i];
1351 if (!arg)
1353 cpp_error (pfile, DL_ERROR,
1354 cl_options[opt_index].msg, argv[i - 1]);
1355 return argc;
1360 switch (opt_code)
1362 case N_OPTS: /* Shut GCC up. */
1363 break;
1364 case OPT_fno_operator_names:
1365 CPP_OPTION (pfile, operator_names) = 0;
1366 break;
1367 case OPT_fpreprocessed:
1368 CPP_OPTION (pfile, preprocessed) = 1;
1369 break;
1370 case OPT_fno_preprocessed:
1371 CPP_OPTION (pfile, preprocessed) = 0;
1372 break;
1373 case OPT_fshow_column:
1374 CPP_OPTION (pfile, show_column) = 1;
1375 break;
1376 case OPT_fno_show_column:
1377 CPP_OPTION (pfile, show_column) = 0;
1378 break;
1379 case OPT_ftabstop:
1380 /* Silently ignore empty string, non-longs and silly values. */
1381 if (arg[0] != '\0')
1383 char *endptr;
1384 long tabstop = strtol (arg, &endptr, 10);
1385 if (*endptr == '\0' && tabstop >= 1 && tabstop <= 100)
1386 CPP_OPTION (pfile, tabstop) = tabstop;
1388 break;
1389 case OPT_w:
1390 CPP_OPTION (pfile, inhibit_warnings) = 1;
1391 break;
1392 case OPT_h:
1393 case OPT__help:
1394 print_help ();
1395 /* fall through */
1396 case OPT_target__help:
1397 case OPT__version:
1398 /* Nothing to do for these cases, but we need to be sure
1399 help_only is set. */
1400 CPP_OPTION (pfile, help_only) = 1;
1401 break;
1402 case OPT_v:
1403 CPP_OPTION (pfile, verbose) = 1;
1404 break;
1406 case OPT_C:
1407 CPP_OPTION (pfile, discard_comments) = 0;
1408 break;
1409 case OPT_CC:
1410 CPP_OPTION (pfile, discard_comments) = 0;
1411 CPP_OPTION (pfile, discard_comments_in_macro_exp) = 0;
1412 break;
1413 case OPT_P:
1414 CPP_OPTION (pfile, no_line_commands) = 1;
1415 break;
1416 case OPT_dollar: /* Don't include $ in identifiers. */
1417 CPP_OPTION (pfile, dollars_in_ident) = 0;
1418 break;
1419 case OPT_H:
1420 CPP_OPTION (pfile, print_include_names) = 1;
1421 break;
1422 case OPT_D:
1423 new_pending_directive (pend, arg, cpp_define);
1424 break;
1425 case OPT_pedantic_errors:
1426 CPP_OPTION (pfile, pedantic_errors) = 1;
1427 /* fall through */
1428 case OPT_pedantic:
1429 CPP_OPTION (pfile, pedantic) = 1;
1430 CPP_OPTION (pfile, warn_endif_labels) = 1;
1431 break;
1432 case OPT_trigraphs:
1433 CPP_OPTION (pfile, trigraphs) = 1;
1434 break;
1435 case OPT_remap:
1436 CPP_OPTION (pfile, remap) = 1;
1437 break;
1438 case OPT_traditional_cpp:
1439 CPP_OPTION (pfile, traditional) = 1;
1440 break;
1441 case OPT_iprefix:
1442 CPP_OPTION (pfile, include_prefix) = arg;
1443 CPP_OPTION (pfile, include_prefix_len) = strlen (arg);
1444 break;
1445 case OPT_lang_c:
1446 set_lang (pfile, CLK_GNUC89);
1447 break;
1448 case OPT_lang_cplusplus:
1449 set_lang (pfile, CLK_GNUCXX);
1450 break;
1451 case OPT_lang_objc:
1452 CPP_OPTION (pfile, objc) = 1;
1453 break;
1454 case OPT_lang_asm:
1455 set_lang (pfile, CLK_ASM);
1456 break;
1457 case OPT_std_cplusplus98:
1458 set_lang (pfile, CLK_CXX98);
1459 break;
1460 case OPT_std_gnu89:
1461 set_lang (pfile, CLK_GNUC89);
1462 break;
1463 case OPT_std_gnu9x:
1464 case OPT_std_gnu99:
1465 set_lang (pfile, CLK_GNUC99);
1466 break;
1467 case OPT_std_iso9899_199409:
1468 set_lang (pfile, CLK_STDC94);
1469 break;
1470 case OPT_std_iso9899_1990:
1471 case OPT_std_c89:
1472 case OPT_lang_c89:
1473 set_lang (pfile, CLK_STDC89);
1474 break;
1475 case OPT_std_iso9899_199x:
1476 case OPT_std_iso9899_1999:
1477 case OPT_std_c9x:
1478 case OPT_std_c99:
1479 set_lang (pfile, CLK_STDC99);
1480 break;
1481 case OPT_nostdinc:
1482 /* -nostdinc causes no default include directories.
1483 You must specify all include-file directories with -I. */
1484 CPP_OPTION (pfile, no_standard_includes) = 1;
1485 break;
1486 case OPT_nostdincplusplus:
1487 /* -nostdinc++ causes no default C++-specific include directories. */
1488 CPP_OPTION (pfile, no_standard_cplusplus_includes) = 1;
1489 break;
1490 case OPT_o:
1491 if (CPP_OPTION (pfile, out_fname) == NULL)
1492 CPP_OPTION (pfile, out_fname) = arg;
1493 else
1495 cpp_error (pfile, DL_ERROR, "output filename specified twice");
1496 return argc;
1498 break;
1499 case OPT_d:
1500 /* Args to -d specify what parts of macros to dump.
1501 Silently ignore unrecognised options; they may
1502 be aimed at the compiler proper. */
1504 char c;
1506 while ((c = *arg++) != '\0')
1507 switch (c)
1509 case 'M':
1510 CPP_OPTION (pfile, dump_macros) = dump_only;
1511 break;
1512 case 'N':
1513 CPP_OPTION (pfile, dump_macros) = dump_names;
1514 break;
1515 case 'D':
1516 CPP_OPTION (pfile, dump_macros) = dump_definitions;
1517 break;
1518 case 'I':
1519 CPP_OPTION (pfile, dump_includes) = 1;
1520 break;
1523 break;
1525 case OPT_MG:
1526 CPP_OPTION (pfile, print_deps_missing_files) = 1;
1527 break;
1528 case OPT_M:
1529 /* When doing dependencies with -M or -MM, suppress normal
1530 preprocessed output, but still do -dM etc. as software
1531 depends on this. Preprocessed output occurs if -MD, -MMD
1532 or environment var dependency generation is used. */
1533 CPP_OPTION (pfile, print_deps) = 2;
1534 CPP_OPTION (pfile, no_output) = 1;
1535 CPP_OPTION (pfile, inhibit_warnings) = 1;
1536 break;
1537 case OPT_MM:
1538 CPP_OPTION (pfile, print_deps) = 1;
1539 CPP_OPTION (pfile, no_output) = 1;
1540 CPP_OPTION (pfile, inhibit_warnings) = 1;
1541 break;
1542 case OPT_MF:
1543 CPP_OPTION (pfile, deps_file) = arg;
1544 break;
1545 case OPT_MP:
1546 CPP_OPTION (pfile, deps_phony_targets) = 1;
1547 break;
1548 case OPT_MQ:
1549 case OPT_MT:
1550 /* Add a target. -MQ quotes for Make. */
1551 deps_add_target (pfile->deps, arg, opt_code == OPT_MQ);
1552 break;
1554 case OPT_MD:
1555 CPP_OPTION (pfile, print_deps) = 2;
1556 CPP_OPTION (pfile, deps_file) = arg;
1557 break;
1558 case OPT_MMD:
1559 CPP_OPTION (pfile, print_deps) = 1;
1560 CPP_OPTION (pfile, deps_file) = arg;
1561 break;
1563 case OPT_A:
1564 if (arg[0] == '-')
1566 /* -A with an argument beginning with '-' acts as
1567 #unassert on whatever immediately follows the '-'.
1568 If "-" is the whole argument, we eliminate all
1569 predefined macros and assertions, including those
1570 that were specified earlier on the command line.
1571 That way we can get rid of any that were passed
1572 automatically in from GCC. */
1574 if (arg[1] == '\0')
1576 free_chain (pend->directive_head);
1577 pend->directive_head = NULL;
1578 pend->directive_tail = NULL;
1580 else
1581 new_pending_directive (pend, arg + 1, cpp_unassert);
1583 else
1584 new_pending_directive (pend, arg, cpp_assert);
1585 break;
1586 case OPT_U:
1587 new_pending_directive (pend, arg, cpp_undef);
1588 break;
1589 case OPT_I: /* Add directory to path for includes. */
1590 if (!strcmp (arg, "-"))
1592 /* -I- means:
1593 Use the preceding -I directories for #include "..."
1594 but not #include <...>.
1595 Don't search the directory of the present file
1596 for #include "...". (Note that -I. -I- is not the same as
1597 the default setup; -I. uses the compiler's working dir.) */
1598 if (! CPP_OPTION (pfile, ignore_srcdir))
1600 pend->quote_head = pend->brack_head;
1601 pend->quote_tail = pend->brack_tail;
1602 pend->brack_head = 0;
1603 pend->brack_tail = 0;
1604 CPP_OPTION (pfile, ignore_srcdir) = 1;
1606 else
1608 cpp_error (pfile, DL_ERROR, "-I- specified twice");
1609 return argc;
1612 else
1613 append_include_chain (pfile, xstrdup (arg), BRACKET, 0);
1614 break;
1615 case OPT_isystem:
1616 /* Add directory to beginning of system include path, as a system
1617 include directory. */
1618 append_include_chain (pfile, xstrdup (arg), SYSTEM, 0);
1619 break;
1620 case OPT_include:
1621 case OPT_imacros:
1623 struct pending_option *o = (struct pending_option *)
1624 xmalloc (sizeof (struct pending_option));
1625 o->arg = arg;
1626 o->next = NULL;
1628 if (opt_code == OPT_include)
1629 APPEND (pend, include, o);
1630 else
1631 APPEND (pend, imacros, o);
1633 break;
1634 case OPT_iwithprefix:
1635 /* Add directory to end of path for includes,
1636 with the default prefix at the front of its name. */
1637 /* fall through */
1638 case OPT_iwithprefixbefore:
1639 /* Add directory to main path for includes,
1640 with the default prefix at the front of its name. */
1642 char *fname;
1643 int len;
1645 len = strlen (arg);
1647 if (CPP_OPTION (pfile, include_prefix) != 0)
1649 size_t ipl = CPP_OPTION (pfile, include_prefix_len);
1650 fname = xmalloc (ipl + len + 1);
1651 memcpy (fname, CPP_OPTION (pfile, include_prefix), ipl);
1652 memcpy (fname + ipl, arg, len + 1);
1654 else if (cpp_GCC_INCLUDE_DIR_len)
1656 fname = xmalloc (cpp_GCC_INCLUDE_DIR_len + len + 1);
1657 memcpy (fname, cpp_GCC_INCLUDE_DIR, cpp_GCC_INCLUDE_DIR_len);
1658 memcpy (fname + cpp_GCC_INCLUDE_DIR_len, arg, len + 1);
1660 else
1661 fname = xstrdup (arg);
1663 append_include_chain (pfile, fname,
1664 opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0);
1666 break;
1667 case OPT_idirafter:
1668 /* Add directory to end of path for includes. */
1669 append_include_chain (pfile, xstrdup (arg), AFTER, 0);
1670 break;
1672 case OPT_Wall:
1673 CPP_OPTION (pfile, warn_trigraphs) = 1;
1674 CPP_OPTION (pfile, warn_comments) = 1;
1675 CPP_OPTION (pfile, warn_num_sign_change) = 1;
1676 break;
1678 case OPT_Wtraditional:
1679 CPP_OPTION (pfile, warn_traditional) = 1;
1680 break;
1681 case OPT_Wno_traditional:
1682 CPP_OPTION (pfile, warn_traditional) = 0;
1683 break;
1685 case OPT_Wtrigraphs:
1686 CPP_OPTION (pfile, warn_trigraphs) = 1;
1687 break;
1688 case OPT_Wno_trigraphs:
1689 CPP_OPTION (pfile, warn_trigraphs) = 0;
1690 break;
1692 case OPT_Wcomment:
1693 case OPT_Wcomments:
1694 CPP_OPTION (pfile, warn_comments) = 1;
1695 break;
1696 case OPT_Wno_comment:
1697 case OPT_Wno_comments:
1698 CPP_OPTION (pfile, warn_comments) = 0;
1699 break;
1701 case OPT_Wundef:
1702 CPP_OPTION (pfile, warn_undef) = 1;
1703 break;
1704 case OPT_Wno_undef:
1705 CPP_OPTION (pfile, warn_undef) = 0;
1706 break;
1708 case OPT_Wimport:
1709 CPP_OPTION (pfile, warn_import) = 1;
1710 break;
1711 case OPT_Wno_import:
1712 CPP_OPTION (pfile, warn_import) = 0;
1713 break;
1715 case OPT_Wendif_labels:
1716 CPP_OPTION (pfile, warn_endif_labels) = 1;
1717 break;
1718 case OPT_Wno_endif_labels:
1719 CPP_OPTION (pfile, warn_endif_labels) = 0;
1720 break;
1722 case OPT_Werror:
1723 CPP_OPTION (pfile, warnings_are_errors) = 1;
1724 break;
1725 case OPT_Wno_error:
1726 CPP_OPTION (pfile, warnings_are_errors) = 0;
1727 break;
1729 case OPT_Wsystem_headers:
1730 CPP_OPTION (pfile, warn_system_headers) = 1;
1731 break;
1732 case OPT_Wno_system_headers:
1733 CPP_OPTION (pfile, warn_system_headers) = 0;
1734 break;
1737 return i + 1;
1740 /* Handle command-line options in (argc, argv).
1741 Can be called multiple times, to handle multiple sets of options.
1742 Returns if an unrecognized option is seen.
1743 Returns number of strings consumed. */
1745 cpp_handle_options (pfile, argc, argv)
1746 cpp_reader *pfile;
1747 int argc;
1748 char **argv;
1750 int i;
1751 int strings_processed;
1753 for (i = 0; i < argc; i += strings_processed)
1755 strings_processed = cpp_handle_option (pfile, argc - i, argv + i);
1756 if (strings_processed == 0)
1757 break;
1760 return i;
1763 /* Extra processing when all options are parsed, after all calls to
1764 cpp_handle_option[s]. Consistency checks etc. */
1765 void
1766 cpp_post_options (pfile)
1767 cpp_reader *pfile;
1769 /* Canonicalize in_fname and out_fname. We guarantee they are not
1770 NULL, and that the empty string represents stdin / stdout. */
1771 if (CPP_OPTION (pfile, in_fname) == NULL
1772 || !strcmp (CPP_OPTION (pfile, in_fname), "-"))
1773 CPP_OPTION (pfile, in_fname) = "";
1775 if (CPP_OPTION (pfile, out_fname) == NULL
1776 || !strcmp (CPP_OPTION (pfile, out_fname), "-"))
1777 CPP_OPTION (pfile, out_fname) = "";
1779 /* -Wtraditional is not useful in C++ mode. */
1780 if (CPP_OPTION (pfile, cplusplus))
1781 CPP_OPTION (pfile, warn_traditional) = 0;
1783 /* The compiler front ends override this, but I think this is the
1784 appropriate setting for the library. */
1785 CPP_OPTION (pfile, warn_long_long) = (CPP_OPTION (pfile, pedantic)
1786 && !CPP_OPTION (pfile, c99));
1788 /* Permanently disable macro expansion if we are rescanning
1789 preprocessed text. Read preprocesed source in ISO mode. */
1790 if (CPP_OPTION (pfile, preprocessed))
1792 pfile->state.prevent_expansion = 1;
1793 CPP_OPTION (pfile, traditional) = 0;
1796 /* Traditional CPP does not accurately track column information. */
1797 if (CPP_OPTION (pfile, traditional))
1798 CPP_OPTION (pfile, show_column) = 0;
1800 /* -dM and dependencies suppress normal output; do it here so that
1801 the last -d[MDN] switch overrides earlier ones. */
1802 if (CPP_OPTION (pfile, dump_macros) == dump_only)
1803 CPP_OPTION (pfile, no_output) = 1;
1805 /* Disable -dD, -dN and -dI if normal output is suppressed. Allow
1806 -dM since at least glibc relies on -M -dM to work. */
1807 if (CPP_OPTION (pfile, no_output))
1809 if (CPP_OPTION (pfile, dump_macros) != dump_only)
1810 CPP_OPTION (pfile, dump_macros) = dump_none;
1811 CPP_OPTION (pfile, dump_includes) = 0;
1814 /* Intialize, and check environment variables for, dependency
1815 output. */
1816 init_dependency_output (pfile);
1818 /* If we're not outputting dependencies, complain if other -M
1819 options have been given. */
1820 if (!CPP_OPTION (pfile, print_deps)
1821 && (CPP_OPTION (pfile, print_deps_missing_files)
1822 || CPP_OPTION (pfile, deps_file)
1823 || CPP_OPTION (pfile, deps_phony_targets)))
1824 cpp_error (pfile, DL_ERROR,
1825 "you must additionally specify either -M or -MM");
1828 /* Set up dependency-file output. On exit, if print_deps is non-zero
1829 then deps_file is not NULL; stdout is the empty string. */
1830 static void
1831 init_dependency_output (pfile)
1832 cpp_reader *pfile;
1834 char *spec, *s, *output_file;
1836 /* Either of two environment variables can specify output of deps.
1837 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1838 where OUTPUT_FILE is the file to write deps info to
1839 and DEPS_TARGET is the target to mention in the deps. */
1841 if (CPP_OPTION (pfile, print_deps) == 0)
1843 spec = getenv ("DEPENDENCIES_OUTPUT");
1844 if (spec)
1845 CPP_OPTION (pfile, print_deps) = 1;
1846 else
1848 spec = getenv ("SUNPRO_DEPENDENCIES");
1849 if (spec)
1850 CPP_OPTION (pfile, print_deps) = 2;
1851 else
1852 return;
1855 /* Find the space before the DEPS_TARGET, if there is one. */
1856 s = strchr (spec, ' ');
1857 if (s)
1859 /* Let the caller perform MAKE quoting. */
1860 deps_add_target (pfile->deps, s + 1, 0);
1861 output_file = (char *) xmalloc (s - spec + 1);
1862 memcpy (output_file, spec, s - spec);
1863 output_file[s - spec] = 0;
1865 else
1866 output_file = spec;
1868 /* Command line -MF overrides environment variables and default. */
1869 if (CPP_OPTION (pfile, deps_file) == 0)
1870 CPP_OPTION (pfile, deps_file) = output_file;
1872 CPP_OPTION (pfile, print_deps_append) = 1;
1874 else if (CPP_OPTION (pfile, deps_file) == 0)
1875 /* If -M or -MM was seen without -MF, default output to wherever
1876 was specified with -o. out_fname is non-NULL here. */
1877 CPP_OPTION (pfile, deps_file) = CPP_OPTION (pfile, out_fname);
1880 /* Handle --help output. */
1881 static void
1882 print_help ()
1884 /* To keep the lines from getting too long for some compilers, limit
1885 to about 500 characters (6 lines) per chunk. */
1886 fputs (_("\
1887 Switches:\n\
1888 -include <file> Include the contents of <file> before other files\n\
1889 -imacros <file> Accept definition of macros in <file>\n\
1890 -iprefix <path> Specify <path> as a prefix for next two options\n\
1891 -iwithprefix <dir> Add <dir> to the end of the system include path\n\
1892 -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\
1893 -isystem <dir> Add <dir> to the start of the system include path\n\
1894 "), stdout);
1895 fputs (_("\
1896 -idirafter <dir> Add <dir> to the end of the system include path\n\
1897 -I <dir> Add <dir> to the end of the main include path\n\
1898 -I- Fine-grained include path control; see info docs\n\
1899 -nostdinc Do not search system include directories\n\
1900 (dirs specified with -isystem will still be used)\n\
1901 -nostdinc++ Do not search system include directories for C++\n\
1902 -o <file> Put output into <file>\n\
1903 "), stdout);
1904 fputs (_("\
1905 -pedantic Issue all warnings demanded by strict ISO C\n\
1906 -pedantic-errors Issue -pedantic warnings as errors instead\n\
1907 -trigraphs Support ISO C trigraphs\n\
1908 -lang-c Assume that the input sources are in C\n\
1909 -lang-c89 Assume that the input sources are in C89\n\
1910 "), stdout);
1911 fputs (_("\
1912 -lang-c++ Assume that the input sources are in C++\n\
1913 -lang-objc Assume that the input sources are in ObjectiveC\n\
1914 -lang-asm Assume that the input sources are in assembler\n\
1915 "), stdout);
1916 fputs (_("\
1917 -std=<std name> Specify the conformance standard; one of:\n\
1918 gnu89, gnu99, c89, c99, iso9899:1990,\n\
1919 iso9899:199409, iso9899:1999\n\
1920 -w Inhibit warning messages\n\
1921 -Wtrigraphs Warn if trigraphs are encountered\n\
1922 -Wno-trigraphs Do not warn about trigraphs\n\
1923 -Wcomment{s} Warn if one comment starts inside another\n\
1924 "), stdout);
1925 fputs (_("\
1926 -Wno-comment{s} Do not warn about comments\n\
1927 -Wtraditional Warn about features not present in traditional C\n\
1928 -Wno-traditional Do not warn about traditional C\n\
1929 -Wundef Warn if an undefined macro is used by #if\n\
1930 -Wno-undef Do not warn about testing undefined macros\n\
1931 -Wimport Warn about the use of the #import directive\n\
1932 "), stdout);
1933 fputs (_("\
1934 -Wno-import Do not warn about the use of #import\n\
1935 -Werror Treat all warnings as errors\n\
1936 -Wno-error Do not treat warnings as errors\n\
1937 -Wsystem-headers Do not suppress warnings from system headers\n\
1938 -Wno-system-headers Suppress warnings from system headers\n\
1939 -Wall Enable all preprocessor warnings\n\
1940 "), stdout);
1941 fputs (_("\
1942 -M Generate make dependencies\n\
1943 -MM As -M, but ignore system header files\n\
1944 -MD Generate make dependencies and compile\n\
1945 -MMD As -MD, but ignore system header files\n\
1946 -MF <file> Write dependency output to the given file\n\
1947 -MG Treat missing header file as generated files\n\
1948 "), stdout);
1949 fputs (_("\
1950 -MP Generate phony targets for all headers\n\
1951 -MQ <target> Add a MAKE-quoted target\n\
1952 -MT <target> Add an unquoted target\n\
1953 "), stdout);
1954 fputs (_("\
1955 -D<macro> Define a <macro> with string '1' as its value\n\
1956 -D<macro>=<val> Define a <macro> with <val> as its value\n\
1957 -A<question>=<answer> Assert the <answer> to <question>\n\
1958 -A-<question>=<answer> Disable the <answer> to <question>\n\
1959 -U<macro> Undefine <macro> \n\
1960 -v Display the version number\n\
1961 "), stdout);
1962 fputs (_("\
1963 -H Print the name of header files as they are used\n\
1964 -C Do not discard comments\n\
1965 -dM Display a list of macro definitions active at end\n\
1966 -dD Preserve macro definitions in output\n\
1967 -dN As -dD except that only the names are preserved\n\
1968 -dI Include #include directives in the output\n\
1969 "), stdout);
1970 fputs (_("\
1971 -fpreprocessed Treat the input file as already preprocessed\n\
1972 -ftabstop=<number> Distance between tab stops for column reporting\n\
1973 -P Do not generate #line directives\n\
1974 -$ Do not allow '$' in identifiers\n\
1975 -remap Remap file names when including files\n\
1976 --version Display version information\n\
1977 -h or --help Display this information\n\
1978 "), stdout);