* Makefile.in (rtlanal.o): Depend on $(TM_P_H).
[official-gcc.git] / gcc / cppinit.c
blobd17dcac01c1ff974d198f503068cad268f9a4e80
1 /* CPP Library.
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 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 "output.h"
27 #include "prefix.h"
28 #include "intl.h"
29 #include "version.h"
30 #include "mkdeps.h"
31 #include "cppdefault.h"
33 /* Predefined symbols, built-in macros, and the default include path. */
35 #ifndef GET_ENV_PATH_LIST
36 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
37 #endif
39 /* Windows does not natively support inodes, and neither does MSDOS.
40 Cygwin's emulation can generate non-unique inodes, so don't use it.
41 VMS has non-numeric inodes. */
42 #ifdef VMS
43 # define INO_T_EQ(a, b) (!memcmp (&(a), &(b), sizeof (a)))
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 #endif
52 /* Internal structures and prototypes. */
54 /* A `struct pending_option' remembers one -D, -A, -U, -include, or
55 -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_start_read. 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 append_include_chain PARAMS ((cpp_reader *,
102 char *, int, int));
103 static struct search_path * remove_dup_dir PARAMS ((cpp_reader *,
104 struct search_path *));
105 static struct search_path * remove_dup_dirs PARAMS ((cpp_reader *,
106 struct search_path *));
107 static void merge_include_chains PARAMS ((cpp_reader *));
108 static bool push_include PARAMS ((cpp_reader *,
109 struct pending_option *));
110 static void free_chain PARAMS ((struct pending_option *));
111 static void set_lang PARAMS ((cpp_reader *, enum c_lang));
112 static void init_dependency_output PARAMS ((cpp_reader *));
113 static void init_standard_includes PARAMS ((cpp_reader *));
114 static void new_pending_directive PARAMS ((struct cpp_pending *,
115 const char *,
116 cl_directive_handler));
117 static void output_deps PARAMS ((cpp_reader *));
118 static int parse_option PARAMS ((const char *));
120 /* Fourth argument to append_include_chain: chain to use.
121 Note it's never asked to append to the quote chain. */
122 enum { BRACKET = 0, SYSTEM, AFTER };
124 /* If we have designated initializers (GCC >2.7) these tables can be
125 initialized, constant data. Otherwise, they have to be filled in at
126 runtime. */
127 #if HAVE_DESIGNATED_INITIALIZERS
129 #define init_trigraph_map() /* Nothing. */
130 #define TRIGRAPH_MAP \
131 __extension__ const U_CHAR _cpp_trigraph_map[UCHAR_MAX + 1] = {
133 #define END };
134 #define s(p, v) [p] = v,
136 #else
138 #define TRIGRAPH_MAP U_CHAR _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
139 static void init_trigraph_map PARAMS ((void)) { \
140 unsigned char *x = _cpp_trigraph_map;
142 #define END }
143 #define s(p, v) x[p] = v;
145 #endif
147 TRIGRAPH_MAP
148 s('=', '#') s(')', ']') s('!', '|')
149 s('(', '[') s('\'', '^') s('>', '}')
150 s('/', '\\') s('<', '{') s('-', '~')
153 #undef s
154 #undef END
155 #undef TRIGRAPH_MAP
157 /* Given a colon-separated list of file names PATH,
158 add all the names to the search path for include files. */
160 static void
161 path_include (pfile, list, path)
162 cpp_reader *pfile;
163 char *list;
164 int path;
166 char *p, *q, *name;
168 p = list;
172 /* Find the end of this name. */
173 q = p;
174 while (*q != 0 && *q != PATH_SEPARATOR) q++;
175 if (q == p)
177 /* An empty name in the path stands for the current directory. */
178 name = (char *) xmalloc (2);
179 name[0] = '.';
180 name[1] = 0;
182 else
184 /* Otherwise use the directory that is named. */
185 name = (char *) xmalloc (q - p + 1);
186 memcpy (name, p, q - p);
187 name[q - p] = 0;
190 append_include_chain (pfile, name, path, 0);
192 /* Advance past this name. */
193 if (*q == 0)
194 break;
195 p = q + 1;
197 while (1);
200 /* Append DIR to include path PATH. DIR must be allocated on the
201 heap; this routine takes responsibility for freeing it. */
202 static void
203 append_include_chain (pfile, dir, path, cxx_aware)
204 cpp_reader *pfile;
205 char *dir;
206 int path;
207 int cxx_aware ATTRIBUTE_UNUSED;
209 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
210 struct search_path *new;
211 struct stat st;
212 unsigned int len;
214 if (*dir == '\0')
216 free (dir);
217 dir = xstrdup (".");
219 _cpp_simplify_pathname (dir);
221 if (stat (dir, &st))
223 /* Dirs that don't exist are silently ignored. */
224 if (errno != ENOENT)
225 cpp_notice_from_errno (pfile, dir);
226 else if (CPP_OPTION (pfile, verbose))
227 fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"), dir);
228 free (dir);
229 return;
232 if (!S_ISDIR (st.st_mode))
234 cpp_notice (pfile, "%s: Not a directory", dir);
235 free (dir);
236 return;
239 len = strlen (dir);
240 if (len > pfile->max_include_len)
241 pfile->max_include_len = len;
243 new = (struct search_path *) xmalloc (sizeof (struct search_path));
244 new->name = dir;
245 new->len = len;
246 new->ino = st.st_ino;
247 new->dev = st.st_dev;
248 /* Both systm and after include file lists should be treated as system
249 include files since these two lists are really just a concatenation
250 of one "system" list. */
251 if (path == SYSTEM || path == AFTER)
252 #ifdef NO_IMPLICIT_EXTERN_C
253 new->sysp = 1;
254 #else
255 new->sysp = cxx_aware ? 1 : 2;
256 #endif
257 else
258 new->sysp = 0;
259 new->name_map = NULL;
260 new->next = NULL;
262 switch (path)
264 case BRACKET: APPEND (pend, brack, new); break;
265 case SYSTEM: APPEND (pend, systm, new); break;
266 case AFTER: APPEND (pend, after, new); break;
270 /* Handle a duplicated include path. PREV is the link in the chain
271 before the duplicate. The duplicate is removed from the chain and
272 freed. Returns PREV. */
273 static struct search_path *
274 remove_dup_dir (pfile, prev)
275 cpp_reader *pfile;
276 struct search_path *prev;
278 struct search_path *cur = prev->next;
280 if (CPP_OPTION (pfile, verbose))
281 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), cur->name);
283 prev->next = cur->next;
284 free ((PTR) cur->name);
285 free (cur);
287 return prev;
290 /* Remove duplicate directories from a chain. Returns the tail of the
291 chain, or NULL if the chain is empty. This algorithm is quadratic
292 in the number of -I switches, which is acceptable since there
293 aren't usually that many of them. */
294 static struct search_path *
295 remove_dup_dirs (pfile, head)
296 cpp_reader *pfile;
297 struct search_path *head;
299 struct search_path *prev = NULL, *cur, *other;
301 for (cur = head; cur; cur = cur->next)
303 for (other = head; other != cur; other = other->next)
304 if (INO_T_EQ (cur->ino, other->ino) && cur->dev == other->dev)
306 if (cur->sysp && !other->sysp)
308 cpp_warning (pfile,
309 "changing search order for system directory \"%s\"",
310 cur->name);
311 if (strcmp (cur->name, other->name))
312 cpp_warning (pfile,
313 " as it is the same as non-system directory \"%s\"",
314 other->name);
315 else
316 cpp_warning (pfile,
317 " as it has already been specified as a non-system directory");
319 cur = remove_dup_dir (pfile, prev);
320 break;
322 prev = cur;
325 return prev;
328 /* Merge the four include chains together in the order quote, bracket,
329 system, after. Remove duplicate dirs (as determined by
330 INO_T_EQ()). The system_include and after_include chains are never
331 referred to again after this function; all access is through the
332 bracket_include path.
334 For the future: Check if the directory is empty (but
335 how?) and possibly preload the include hash. */
337 static void
338 merge_include_chains (pfile)
339 cpp_reader *pfile;
341 struct search_path *quote, *brack, *systm, *qtail;
343 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
345 quote = pend->quote_head;
346 brack = pend->brack_head;
347 systm = pend->systm_head;
348 qtail = pend->quote_tail;
350 /* Paste together bracket, system, and after include chains. */
351 if (systm)
352 pend->systm_tail->next = pend->after_head;
353 else
354 systm = pend->after_head;
356 if (brack)
357 pend->brack_tail->next = systm;
358 else
359 brack = systm;
361 /* This is a bit tricky. First we drop dupes from the quote-include
362 list. Then we drop dupes from the bracket-include list.
363 Finally, if qtail and brack are the same directory, we cut out
364 brack and move brack up to point to qtail.
366 We can't just merge the lists and then uniquify them because
367 then we may lose directories from the <> search path that should
368 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
369 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
370 -Ibar -I- -Ifoo -Iquux. */
372 remove_dup_dirs (pfile, brack);
373 qtail = remove_dup_dirs (pfile, quote);
375 if (quote)
377 qtail->next = brack;
379 /* If brack == qtail, remove brack as it's simpler. */
380 if (INO_T_EQ (qtail->ino, brack->ino) && 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 /* Sets internal flags correctly for a given language, and defines
391 macros if necessary. */
393 struct lang_flags
395 char c99;
396 char objc;
397 char cplusplus;
398 char extended_numbers;
399 char trigraphs;
400 char dollars_in_ident;
401 char cplusplus_comments;
402 char digraphs;
405 /* ??? Enable $ in identifiers in assembly? */
406 static const struct lang_flags lang_defaults[] =
407 { /* c99 objc c++ xnum trig dollar c++comm digr */
408 /* GNUC89 */ { 0, 0, 0, 1, 0, 1, 1, 1 },
409 /* GNUC99 */ { 1, 0, 0, 1, 0, 1, 1, 1 },
410 /* STDC89 */ { 0, 0, 0, 0, 1, 0, 0, 0 },
411 /* STDC94 */ { 0, 0, 0, 0, 1, 0, 0, 1 },
412 /* STDC99 */ { 1, 0, 0, 1, 1, 0, 1, 1 },
413 /* GNUCXX */ { 0, 0, 1, 1, 0, 1, 1, 1 },
414 /* CXX98 */ { 0, 0, 1, 1, 1, 0, 1, 1 },
415 /* OBJC */ { 0, 1, 0, 1, 0, 1, 1, 1 },
416 /* OBJCXX */ { 0, 1, 1, 1, 0, 1, 1, 1 },
417 /* ASM */ { 0, 0, 0, 1, 0, 0, 1, 0 }
420 static void
421 set_lang (pfile, lang)
422 cpp_reader *pfile;
423 enum c_lang lang;
425 const struct lang_flags *l = &lang_defaults[(int) lang];
427 CPP_OPTION (pfile, lang) = lang;
429 CPP_OPTION (pfile, c99) = l->c99;
430 CPP_OPTION (pfile, objc) = l->objc;
431 CPP_OPTION (pfile, cplusplus) = l->cplusplus;
432 CPP_OPTION (pfile, extended_numbers) = l->extended_numbers;
433 CPP_OPTION (pfile, trigraphs) = l->trigraphs;
434 CPP_OPTION (pfile, dollars_in_ident) = l->dollars_in_ident;
435 CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments;
436 CPP_OPTION (pfile, digraphs) = l->digraphs;
439 #ifdef HOST_EBCDIC
440 static int opt_comp PARAMS ((const void *, const void *));
442 /* Run-time sorting of options array. */
443 static int
444 opt_comp (p1, p2)
445 const void *p1, *p2;
447 return strcmp (((struct cl_option *) p1)->opt_text,
448 ((struct cl_option *) p2)->opt_text);
450 #endif
452 /* init initializes library global state. It might not need to
453 do anything depending on the platform and compiler. */
455 static void
456 init_library ()
458 static int initialized = 0;
460 if (! initialized)
462 initialized = 1;
464 #ifdef HOST_EBCDIC
465 /* For non-ASCII hosts, the cl_options array needs to be sorted at
466 runtime. */
467 qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
468 #endif
470 /* Set up the trigraph map. This doesn't need to do anything if
471 we were compiled with a compiler that supports C99 designated
472 initializers. */
473 init_trigraph_map ();
477 /* Initialize a cpp_reader structure. */
478 cpp_reader *
479 cpp_create_reader (table, lang)
480 hash_table *table;
481 enum c_lang lang;
483 struct spec_nodes *s;
484 cpp_reader *pfile;
486 /* Initialise this instance of the library if it hasn't been already. */
487 init_library ();
489 pfile = (cpp_reader *) xcalloc (1, sizeof (cpp_reader));
491 set_lang (pfile, lang);
492 CPP_OPTION (pfile, warn_import) = 1;
493 CPP_OPTION (pfile, discard_comments) = 1;
494 CPP_OPTION (pfile, show_column) = 1;
495 CPP_OPTION (pfile, tabstop) = 8;
496 CPP_OPTION (pfile, operator_names) = 1;
498 CPP_OPTION (pfile, pending) =
499 (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
501 /* It's simplest to just create this struct whether or not it will
502 be needed. */
503 pfile->deps = deps_init ();
505 /* Initialise the line map. Start at logical line 1, so we can use
506 a line number of zero for special states. */
507 init_line_maps (&pfile->line_maps);
508 pfile->line = 1;
510 /* Initialize lexer state. */
511 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
513 /* Set up static tokens. */
514 pfile->date.type = CPP_EOF;
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 /* Initialise the buffer obstack. */
535 gcc_obstack_init (&pfile->buffer_ob);
537 /* Initialise the hashtable. */
538 _cpp_init_hashtable (pfile, table);
540 _cpp_init_directives (pfile);
541 _cpp_init_includes (pfile);
542 _cpp_init_internal_pragmas (pfile);
544 /* Initialize the special nodes. */
545 s = &pfile->spec_nodes;
546 s->n_L = cpp_lookup (pfile, DSC("L"));
547 s->n_defined = cpp_lookup (pfile, DSC("defined"));
548 s->n_true = cpp_lookup (pfile, DSC("true"));
549 s->n_false = cpp_lookup (pfile, DSC("false"));
550 s->n__STRICT_ANSI__ = cpp_lookup (pfile, DSC("__STRICT_ANSI__"));
551 s->n__CHAR_UNSIGNED__ = cpp_lookup (pfile, DSC("__CHAR_UNSIGNED__"));
552 s->n__VA_ARGS__ = cpp_lookup (pfile, DSC("__VA_ARGS__"));
553 s->n__VA_ARGS__->flags |= NODE_DIAGNOSTIC;
555 return pfile;
558 /* Free resources used by PFILE. Accessing PFILE after this function
559 returns leads to undefined behaviour. */
561 cpp_destroy (pfile)
562 cpp_reader *pfile;
564 int result;
565 struct search_path *dir, *dirn;
566 cpp_context *context, *contextn;
567 tokenrun *run, *runn;
569 while (CPP_BUFFER (pfile) != NULL)
570 _cpp_pop_buffer (pfile);
572 if (pfile->macro_buffer)
574 free ((PTR) pfile->macro_buffer);
575 pfile->macro_buffer = NULL;
576 pfile->macro_buffer_len = 0;
579 deps_free (pfile->deps);
580 obstack_free (&pfile->buffer_ob, 0);
582 _cpp_destroy_hashtable (pfile);
583 _cpp_cleanup_includes (pfile);
585 _cpp_free_buff (pfile->a_buff);
586 _cpp_free_buff (pfile->u_buff);
587 _cpp_free_buff (pfile->free_buffs);
589 for (run = &pfile->base_run; run; run = runn)
591 runn = run->next;
592 free (run->base);
593 if (run != &pfile->base_run)
594 free (run);
597 for (dir = CPP_OPTION (pfile, quote_include); dir; dir = dirn)
599 dirn = dir->next;
600 free ((PTR) dir->name);
601 free (dir);
604 for (context = pfile->base_context.next; context; context = contextn)
606 contextn = context->next;
607 free (context);
610 free_line_maps (&pfile->line_maps);
612 result = pfile->errors;
613 free (pfile);
615 return result;
619 /* This structure defines one built-in identifier. A node will be
620 entered in the hash table under the name NAME, with value VALUE (if
621 any). If flags has OPERATOR, the node's operator field is used; if
622 flags has BUILTIN the node's builtin field is used. Macros that are
623 known at build time should not be flagged BUILTIN, as then they do
624 not appear in macro dumps with e.g. -dM or -dD.
626 Two values are not compile time constants, so we tag
627 them in the FLAGS field instead:
628 VERS value is the global version_string, quoted
629 ULP value is the global user_label_prefix
631 Also, macros with CPLUS set in the flags field are entered only for C++. */
633 struct builtin
635 const U_CHAR *name;
636 const char *value;
637 unsigned char builtin;
638 unsigned char operator;
639 unsigned short flags;
640 unsigned short len;
642 #define VERS 0x01
643 #define ULP 0x02
644 #define CPLUS 0x04
645 #define BUILTIN 0x08
646 #define OPERATOR 0x10
648 #define B(n, t) { U n, 0, t, 0, BUILTIN, sizeof n - 1 }
649 #define C(n, v) { U n, v, 0, 0, 0, sizeof n - 1 }
650 #define X(n, f) { U n, 0, 0, 0, f, sizeof n - 1 }
651 #define O(n, c, f) { U n, 0, 0, c, OPERATOR | f, sizeof n - 1 }
652 static const struct builtin builtin_array[] =
654 B("__TIME__", BT_TIME),
655 B("__DATE__", BT_DATE),
656 B("__FILE__", BT_FILE),
657 B("__BASE_FILE__", BT_BASE_FILE),
658 B("__LINE__", BT_SPECLINE),
659 B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL),
660 B("_Pragma", BT_PRAGMA),
662 X("__VERSION__", VERS),
663 X("__USER_LABEL_PREFIX__", ULP),
664 C("__REGISTER_PREFIX__", REGISTER_PREFIX),
665 C("__HAVE_BUILTIN_SETJMP__", "1"),
666 #ifndef NO_BUILTIN_SIZE_TYPE
667 C("__SIZE_TYPE__", SIZE_TYPE),
668 #endif
669 #ifndef NO_BUILTIN_PTRDIFF_TYPE
670 C("__PTRDIFF_TYPE__", PTRDIFF_TYPE),
671 #endif
672 #ifndef NO_BUILTIN_WCHAR_TYPE
673 C("__WCHAR_TYPE__", WCHAR_TYPE),
674 #endif
675 #ifndef NO_BUILTIN_WINT_TYPE
676 C("__WINT_TYPE__", WINT_TYPE),
677 #endif
678 #ifdef STDC_0_IN_SYSTEM_HEADERS
679 B("__STDC__", BT_STDC),
680 #else
681 C("__STDC__", "1"),
682 #endif
684 /* Named operators known to the preprocessor. These cannot be #defined
685 and always have their stated meaning. They are treated like normal
686 identifiers except for the type code and the meaning. Most of them
687 are only for C++ (but see iso646.h). */
688 O("and", CPP_AND_AND, CPLUS),
689 O("and_eq", CPP_AND_EQ, CPLUS),
690 O("bitand", CPP_AND, CPLUS),
691 O("bitor", CPP_OR, CPLUS),
692 O("compl", CPP_COMPL, CPLUS),
693 O("not", CPP_NOT, CPLUS),
694 O("not_eq", CPP_NOT_EQ, CPLUS),
695 O("or", CPP_OR_OR, CPLUS),
696 O("or_eq", CPP_OR_EQ, CPLUS),
697 O("xor", CPP_XOR, CPLUS),
698 O("xor_eq", CPP_XOR_EQ, CPLUS)
700 #undef B
701 #undef C
702 #undef X
703 #undef O
704 #define builtin_array_end \
705 builtin_array + sizeof(builtin_array)/sizeof(struct builtin)
707 /* Subroutine of cpp_start_read; reads the builtins table above and
708 enters the macros into the hash table. */
709 static void
710 init_builtins (pfile)
711 cpp_reader *pfile;
713 const struct builtin *b;
715 for(b = builtin_array; b < builtin_array_end; b++)
717 if ((b->flags & CPLUS) && ! CPP_OPTION (pfile, cplusplus))
718 continue;
720 if ((b->flags & OPERATOR) && ! CPP_OPTION (pfile, operator_names))
721 continue;
723 if (b->flags & (OPERATOR | BUILTIN))
725 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
726 if (b->flags & OPERATOR)
728 hp->flags |= NODE_OPERATOR;
729 hp->value.operator = b->operator;
731 else
733 hp->type = NT_MACRO;
734 hp->flags |= NODE_BUILTIN | NODE_WARN;
735 hp->value.builtin = b->builtin;
738 else /* A standard macro of some kind. */
740 const char *val;
741 char *str;
743 if (b->flags & VERS)
745 /* Allocate enough space for 'name "value"\n\0'. */
746 str = alloca (b->len + strlen (version_string) + 5);
747 sprintf (str, "%s \"%s\"\n", b->name, version_string);
749 else
751 if (b->flags & ULP)
752 val = CPP_OPTION (pfile, user_label_prefix);
753 else
754 val = b->value;
756 /* Allocate enough space for "name value\n\0". */
757 str = alloca (b->len + strlen (val) + 3);
758 sprintf(str, "%s %s\n", b->name, val);
761 _cpp_define_builtin (pfile, str);
765 if (CPP_OPTION (pfile, cplusplus))
767 _cpp_define_builtin (pfile, "__cplusplus 1");
768 if (SUPPORTS_ONE_ONLY)
769 _cpp_define_builtin (pfile, "__GXX_WEAK__ 1");
770 else
771 _cpp_define_builtin (pfile, "__GXX_WEAK__ 0");
773 if (CPP_OPTION (pfile, objc))
774 _cpp_define_builtin (pfile, "__OBJC__ 1");
776 if (CPP_OPTION (pfile, lang) == CLK_STDC94)
777 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
778 else if (CPP_OPTION (pfile, c99))
779 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
781 if (CPP_OPTION (pfile, lang) == CLK_STDC89
782 || CPP_OPTION (pfile, lang) == CLK_STDC94
783 || CPP_OPTION (pfile, lang) == CLK_STDC99)
784 _cpp_define_builtin (pfile, "__STRICT_ANSI__ 1");
785 else if (CPP_OPTION (pfile, lang) == CLK_ASM)
786 _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
788 #undef BUILTIN
789 #undef OPERATOR
790 #undef VERS
791 #undef ULP
792 #undef CPLUS
793 #undef builtin_array_end
795 /* And another subroutine. This one sets up the standard include path. */
796 static void
797 init_standard_includes (pfile)
798 cpp_reader *pfile;
800 char *path;
801 const struct default_include *p;
802 const char *specd_prefix = CPP_OPTION (pfile, include_prefix);
804 /* Several environment variables may add to the include search path.
805 CPATH specifies an additional list of directories to be searched
806 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
807 etc. specify an additional list of directories to be searched as
808 if specified with -isystem, for the language indicated. */
810 GET_ENV_PATH_LIST (path, "CPATH");
811 if (path != 0 && *path != 0)
812 path_include (pfile, path, BRACKET);
814 switch ((CPP_OPTION (pfile, objc) << 1) + CPP_OPTION (pfile, cplusplus))
816 case 0:
817 GET_ENV_PATH_LIST (path, "C_INCLUDE_PATH");
818 break;
819 case 1:
820 GET_ENV_PATH_LIST (path, "CPLUS_INCLUDE_PATH");
821 break;
822 case 2:
823 GET_ENV_PATH_LIST (path, "OBJC_INCLUDE_PATH");
824 break;
825 case 3:
826 GET_ENV_PATH_LIST (path, "OBJCPLUS_INCLUDE_PATH");
827 break;
829 if (path != 0 && *path != 0)
830 path_include (pfile, path, SYSTEM);
832 /* Search "translated" versions of GNU directories.
833 These have /usr/local/lib/gcc... replaced by specd_prefix. */
834 if (specd_prefix != 0 && cpp_GCC_INCLUDE_DIR_len)
836 /* Remove the `include' from /usr/local/lib/gcc.../include.
837 GCC_INCLUDE_DIR will always end in /include. */
838 int default_len = cpp_GCC_INCLUDE_DIR_len;
839 char *default_prefix = (char *) alloca (default_len + 1);
840 int specd_len = strlen (specd_prefix);
842 memcpy (default_prefix, cpp_GCC_INCLUDE_DIR, default_len);
843 default_prefix[default_len] = '\0';
845 for (p = cpp_include_defaults; p->fname; p++)
847 /* Some standard dirs are only for C++. */
848 if (!p->cplusplus
849 || (CPP_OPTION (pfile, cplusplus)
850 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
852 /* Does this dir start with the prefix? */
853 if (!memcmp (p->fname, default_prefix, default_len))
855 /* Yes; change prefix and add to search list. */
856 int flen = strlen (p->fname);
857 int this_len = specd_len + flen - default_len;
858 char *str = (char *) xmalloc (this_len + 1);
859 memcpy (str, specd_prefix, specd_len);
860 memcpy (str + specd_len,
861 p->fname + default_len,
862 flen - default_len + 1);
864 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
870 /* Search ordinary names for GNU include directories. */
871 for (p = cpp_include_defaults; p->fname; p++)
873 /* Some standard dirs are only for C++. */
874 if (!p->cplusplus
875 || (CPP_OPTION (pfile, cplusplus)
876 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
878 char *str = update_path (p->fname, p->component);
879 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
884 /* Pushes a -imacro and -include file given on the command line onto
885 the buffer stack. Returns non-zero if successful. */
886 static bool
887 push_include (pfile, p)
888 cpp_reader *pfile;
889 struct pending_option *p;
891 cpp_token header;
893 /* Later: maybe update this to use the #include "" search path
894 if cpp_read_file fails. */
895 header.type = CPP_STRING;
896 header.val.str.text = (const unsigned char *) p->arg;
897 header.val.str.len = strlen (p->arg);
898 /* Make the command line directive take up a line. */
899 pfile->line++;
901 return _cpp_execute_include (pfile, &header, IT_CMDLINE);
904 /* Frees a pending_option chain. */
905 static void
906 free_chain (head)
907 struct pending_option *head;
909 struct pending_option *next;
911 while (head)
913 next = head->next;
914 free (head);
915 head = next;
919 /* This is called after options have been processed. Setup for
920 processing input from the file named FNAME, or stdin if it is the
921 empty string. Return 1 on success, 0 on failure. */
923 cpp_start_read (pfile, fname)
924 cpp_reader *pfile;
925 const char *fname;
927 /* Set up the include search path now. */
928 if (! CPP_OPTION (pfile, no_standard_includes))
929 init_standard_includes (pfile);
931 merge_include_chains (pfile);
933 /* With -v, print the list of dirs to search. */
934 if (CPP_OPTION (pfile, verbose))
936 struct search_path *l;
937 fprintf (stderr, _("#include \"...\" search starts here:\n"));
938 for (l = CPP_OPTION (pfile, quote_include); l; l = l->next)
940 if (l == CPP_OPTION (pfile, bracket_include))
941 fprintf (stderr, _("#include <...> search starts here:\n"));
942 fprintf (stderr, " %s\n", l->name);
944 fprintf (stderr, _("End of search list.\n"));
947 if (CPP_OPTION (pfile, print_deps))
948 /* Set the default target (if there is none already). */
949 deps_add_default_target (pfile->deps, fname);
951 /* Open the main input file. This must be done early, so we have a
952 buffer to stand on. */
953 if (!_cpp_read_file (pfile, fname))
954 return 0;
956 /* Set this after cpp_post_options so the client can change the
957 option if it wishes, and after stacking the main file so we don't
958 trace the main file. */
959 pfile->line_maps.trace_includes = CPP_OPTION (pfile, print_include_names);
961 /* Install builtins and process command line macros etc. in the order
962 they appeared, but only if not already preprocessed. */
963 if (! CPP_OPTION (pfile, preprocessed))
965 struct pending_option *p;
967 _cpp_do_file_change (pfile, LC_RENAME, _("<builtin>"), 1, 0);
968 init_builtins (pfile);
969 _cpp_do_file_change (pfile, LC_RENAME, _("<command line>"), 1, 0);
970 for (p = CPP_OPTION (pfile, pending)->directive_head; p; p = p->next)
971 (*p->handler) (pfile, p->arg);
973 /* Scan -imacros files after command line defines, but before
974 files given with -include. */
975 while ((p = CPP_OPTION (pfile, pending)->imacros_head) != NULL)
977 if (push_include (pfile, p))
979 pfile->buffer->return_at_eof = true;
980 cpp_scan_nooutput (pfile);
982 CPP_OPTION (pfile, pending)->imacros_head = p->next;
983 free (p);
987 free_chain (CPP_OPTION (pfile, pending)->directive_head);
988 _cpp_push_next_buffer (pfile);
990 return 1;
993 /* Called to push the next buffer on the stack given by -include. If
994 there are none, free the pending structure and restore the line map
995 for the main file. */
996 bool
997 _cpp_push_next_buffer (pfile)
998 cpp_reader *pfile;
1000 bool pushed = false;
1002 /* This is't pretty; we'd rather not be relying on this as a boolean
1003 for reverting the line map. Further, we only free the chains in
1004 this conditional, so an early call to cpp_finish / cpp_destroy
1005 will leak that memory. */
1006 if (CPP_OPTION (pfile, pending)
1007 && CPP_OPTION (pfile, pending)->imacros_head == NULL)
1009 while (!pushed)
1011 struct pending_option *p = CPP_OPTION (pfile, pending)->include_head;
1013 if (p == NULL)
1014 break;
1015 if (! CPP_OPTION (pfile, preprocessed))
1016 pushed = push_include (pfile, p);
1017 CPP_OPTION (pfile, pending)->include_head = p->next;
1018 free (p);
1021 if (!pushed)
1023 free (CPP_OPTION (pfile, pending));
1024 CPP_OPTION (pfile, pending) = NULL;
1026 /* Restore the line map for the main file. */
1027 if (! CPP_OPTION (pfile, preprocessed))
1028 _cpp_do_file_change (pfile, LC_RENAME,
1029 pfile->line_maps.maps[0].to_file, 1, 0);
1033 return pushed;
1036 /* Use mkdeps.c to output dependency information. */
1037 static void
1038 output_deps (pfile)
1039 cpp_reader *pfile;
1041 /* Stream on which to print the dependency information. */
1042 FILE *deps_stream = 0;
1043 const char *const deps_mode =
1044 CPP_OPTION (pfile, print_deps_append) ? "a" : "w";
1046 if (CPP_OPTION (pfile, deps_file) == 0)
1047 deps_stream = stdout;
1048 else
1050 deps_stream = fopen (CPP_OPTION (pfile, deps_file), deps_mode);
1051 if (deps_stream == 0)
1053 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, deps_file));
1054 return;
1058 deps_write (pfile->deps, deps_stream, 72);
1060 if (CPP_OPTION (pfile, deps_phony_targets))
1061 deps_phony_targets (pfile->deps, deps_stream);
1063 /* Don't close stdout. */
1064 if (CPP_OPTION (pfile, deps_file))
1066 if (ferror (deps_stream) || fclose (deps_stream) != 0)
1067 cpp_fatal (pfile, "I/O error on output");
1071 /* This is called at the end of preprocessing. It pops the
1072 last buffer and writes dependency output. It should also
1073 clear macro definitions, such that you could call cpp_start_read
1074 with a new filename to restart processing. */
1075 void
1076 cpp_finish (pfile)
1077 cpp_reader *pfile;
1079 /* cpplex.c leaves the final buffer on the stack. This it so that
1080 it returns an unending stream of CPP_EOFs to the client. If we
1081 popped the buffer, we'd derefence a NULL buffer pointer and
1082 segfault. It's nice to allow the client to do worry-free excess
1083 cpp_get_token calls. */
1084 while (pfile->buffer)
1085 _cpp_pop_buffer (pfile);
1087 /* Don't write the deps file if preprocessing has failed. */
1088 if (CPP_OPTION (pfile, print_deps) && pfile->errors == 0)
1089 output_deps (pfile);
1091 /* Report on headers that could use multiple include guards. */
1092 if (CPP_OPTION (pfile, print_include_names))
1093 _cpp_report_missing_guards (pfile);
1096 static void
1097 new_pending_directive (pend, text, handler)
1098 struct cpp_pending *pend;
1099 const char *text;
1100 cl_directive_handler handler;
1102 struct pending_option *o = (struct pending_option *)
1103 xmalloc (sizeof (struct pending_option));
1105 o->arg = text;
1106 o->next = NULL;
1107 o->handler = handler;
1108 APPEND (pend, directive, o);
1111 /* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
1112 I.e. a const string initializer with parens around it. That is
1113 what N_("string") resolves to, so we make no_* be macros instead. */
1114 #define no_arg N_("Argument missing after %s")
1115 #define no_ass N_("Assertion missing after %s")
1116 #define no_dir N_("Directory name missing after %s")
1117 #define no_fil N_("File name missing after %s")
1118 #define no_mac N_("Macro name missing after %s")
1119 #define no_pth N_("Path name missing after %s")
1120 #define no_num N_("Number missing after %s")
1121 #define no_tgt N_("Target missing after %s")
1123 /* This is the list of all command line options, with the leading
1124 "-" removed. It must be sorted in ASCII collating order. */
1125 #define COMMAND_LINE_OPTIONS \
1126 DEF_OPT("$", 0, OPT_dollar) \
1127 DEF_OPT("+", 0, OPT_plus) \
1128 DEF_OPT("-help", 0, OPT__help) \
1129 DEF_OPT("-target-help", 0, OPT_target__help) \
1130 DEF_OPT("-version", 0, OPT__version) \
1131 DEF_OPT("A", no_ass, OPT_A) \
1132 DEF_OPT("C", 0, OPT_C) \
1133 DEF_OPT("D", no_mac, OPT_D) \
1134 DEF_OPT("H", 0, OPT_H) \
1135 DEF_OPT("I", no_dir, OPT_I) \
1136 DEF_OPT("M", 0, OPT_M) \
1137 DEF_OPT("MD", no_fil, OPT_MD) \
1138 DEF_OPT("MF", no_fil, OPT_MF) \
1139 DEF_OPT("MG", 0, OPT_MG) \
1140 DEF_OPT("MM", 0, OPT_MM) \
1141 DEF_OPT("MMD", no_fil, OPT_MMD) \
1142 DEF_OPT("MP", 0, OPT_MP) \
1143 DEF_OPT("MQ", no_tgt, OPT_MQ) \
1144 DEF_OPT("MT", no_tgt, OPT_MT) \
1145 DEF_OPT("P", 0, OPT_P) \
1146 DEF_OPT("U", no_mac, OPT_U) \
1147 DEF_OPT("W", no_arg, OPT_W) /* arg optional */ \
1148 DEF_OPT("d", no_arg, OPT_d) \
1149 DEF_OPT("fleading-underscore", 0, OPT_fleading_underscore) \
1150 DEF_OPT("fno-leading-underscore", 0, OPT_fno_leading_underscore) \
1151 DEF_OPT("fno-operator-names", 0, OPT_fno_operator_names) \
1152 DEF_OPT("fno-preprocessed", 0, OPT_fno_preprocessed) \
1153 DEF_OPT("fno-show-column", 0, OPT_fno_show_column) \
1154 DEF_OPT("fpreprocessed", 0, OPT_fpreprocessed) \
1155 DEF_OPT("fshow-column", 0, OPT_fshow_column) \
1156 DEF_OPT("ftabstop=", no_num, OPT_ftabstop) \
1157 DEF_OPT("h", 0, OPT_h) \
1158 DEF_OPT("idirafter", no_dir, OPT_idirafter) \
1159 DEF_OPT("imacros", no_fil, OPT_imacros) \
1160 DEF_OPT("include", no_fil, OPT_include) \
1161 DEF_OPT("iprefix", no_pth, OPT_iprefix) \
1162 DEF_OPT("isystem", no_dir, OPT_isystem) \
1163 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix) \
1164 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore) \
1165 DEF_OPT("lang-asm", 0, OPT_lang_asm) \
1166 DEF_OPT("lang-c", 0, OPT_lang_c) \
1167 DEF_OPT("lang-c++", 0, OPT_lang_cplusplus) \
1168 DEF_OPT("lang-c89", 0, OPT_lang_c89) \
1169 DEF_OPT("lang-objc", 0, OPT_lang_objc) \
1170 DEF_OPT("lang-objc++", 0, OPT_lang_objcplusplus) \
1171 DEF_OPT("nostdinc", 0, OPT_nostdinc) \
1172 DEF_OPT("nostdinc++", 0, OPT_nostdincplusplus) \
1173 DEF_OPT("o", no_fil, OPT_o) \
1174 DEF_OPT("pedantic", 0, OPT_pedantic) \
1175 DEF_OPT("pedantic-errors", 0, OPT_pedantic_errors) \
1176 DEF_OPT("remap", 0, OPT_remap) \
1177 DEF_OPT("std=c++98", 0, OPT_std_cplusplus98) \
1178 DEF_OPT("std=c89", 0, OPT_std_c89) \
1179 DEF_OPT("std=c99", 0, OPT_std_c99) \
1180 DEF_OPT("std=c9x", 0, OPT_std_c9x) \
1181 DEF_OPT("std=gnu89", 0, OPT_std_gnu89) \
1182 DEF_OPT("std=gnu99", 0, OPT_std_gnu99) \
1183 DEF_OPT("std=gnu9x", 0, OPT_std_gnu9x) \
1184 DEF_OPT("std=iso9899:1990", 0, OPT_std_iso9899_1990) \
1185 DEF_OPT("std=iso9899:199409", 0, OPT_std_iso9899_199409) \
1186 DEF_OPT("std=iso9899:1999", 0, OPT_std_iso9899_1999) \
1187 DEF_OPT("std=iso9899:199x", 0, OPT_std_iso9899_199x) \
1188 DEF_OPT("trigraphs", 0, OPT_trigraphs) \
1189 DEF_OPT("v", 0, OPT_v) \
1190 DEF_OPT("version", 0, OPT_version) \
1191 DEF_OPT("w", 0, OPT_w)
1193 #define DEF_OPT(text, msg, code) code,
1194 enum opt_code
1196 COMMAND_LINE_OPTIONS
1197 N_OPTS
1199 #undef DEF_OPT
1201 struct cl_option
1203 const char *opt_text;
1204 const char *msg;
1205 size_t opt_len;
1206 enum opt_code opt_code;
1209 #define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
1210 #ifdef HOST_EBCDIC
1211 static struct cl_option cl_options[] =
1212 #else
1213 static const struct cl_option cl_options[] =
1214 #endif
1216 COMMAND_LINE_OPTIONS
1218 #undef DEF_OPT
1219 #undef COMMAND_LINE_OPTIONS
1221 /* Perform a binary search to find which, if any, option the given
1222 command-line matches. Returns its index in the option array,
1223 negative on failure. Complications arise since some options can be
1224 suffixed with an argument, and multiple complete matches can occur,
1225 e.g. -iwithprefix and -iwithprefixbefore. Moreover, we need to
1226 accept options beginning with -W that we do not recognise, but not
1227 to swallow any subsequent command line argument; this is handled as
1228 special cases in cpp_handle_option. */
1229 static int
1230 parse_option (input)
1231 const char *input;
1233 unsigned int md, mn, mx;
1234 size_t opt_len;
1235 int comp;
1237 mn = 0;
1238 mx = N_OPTS;
1240 while (mx > mn)
1242 md = (mn + mx) / 2;
1244 opt_len = cl_options[md].opt_len;
1245 comp = memcmp (input, cl_options[md].opt_text, opt_len);
1247 if (comp > 0)
1248 mn = md + 1;
1249 else if (comp < 0)
1250 mx = md;
1251 else
1253 if (input[opt_len] == '\0')
1254 return md;
1255 /* We were passed more text. If the option takes an argument,
1256 we may match a later option or we may have been passed the
1257 argument. The longest possible option match succeeds.
1258 If the option takes no arguments we have not matched and
1259 continue the search (e.g. input="stdc++" match was "stdc"). */
1260 mn = md + 1;
1261 if (cl_options[md].msg)
1263 /* Scan forwards. If we get an exact match, return it.
1264 Otherwise, return the longest option-accepting match.
1265 This loops no more than twice with current options. */
1266 mx = md;
1267 for (; mn < (unsigned int) N_OPTS; mn++)
1269 opt_len = cl_options[mn].opt_len;
1270 if (memcmp (input, cl_options[mn].opt_text, opt_len))
1271 break;
1272 if (input[opt_len] == '\0')
1273 return mn;
1274 if (cl_options[mn].msg)
1275 mx = mn;
1277 return mx;
1282 return -1;
1285 /* Handle one command-line option in (argc, argv).
1286 Can be called multiple times, to handle multiple sets of options.
1287 Returns number of strings consumed. */
1290 cpp_handle_option (pfile, argc, argv)
1291 cpp_reader *pfile;
1292 int argc;
1293 char **argv;
1295 int i = 0;
1296 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
1298 /* Interpret "-" or a non-option as a file name. */
1299 if (argv[i][0] != '-' || argv[i][1] == '\0')
1301 if (CPP_OPTION (pfile, in_fname) == NULL)
1302 CPP_OPTION (pfile, in_fname) = argv[i];
1303 else if (CPP_OPTION (pfile, out_fname) == NULL)
1304 CPP_OPTION (pfile, out_fname) = argv[i];
1305 else
1306 cpp_fatal (pfile, "Too many filenames. Type %s --help for usage info",
1307 progname);
1309 else
1311 enum opt_code opt_code;
1312 int opt_index;
1313 const char *arg = 0;
1315 /* Skip over '-'. */
1316 opt_index = parse_option (&argv[i][1]);
1317 if (opt_index < 0)
1318 return i;
1320 opt_code = cl_options[opt_index].opt_code;
1321 if (cl_options[opt_index].msg)
1323 arg = &argv[i][cl_options[opt_index].opt_len + 1];
1325 /* Yuk. Special case for -W as it must not swallow
1326 up any following argument. If this becomes common, add
1327 another field to the cl_options table. */
1328 if (arg[0] == '\0' && opt_code != OPT_W)
1330 arg = argv[++i];
1331 if (!arg)
1333 cpp_fatal (pfile, cl_options[opt_index].msg, argv[i - 1]);
1334 return argc;
1339 switch (opt_code)
1341 case N_OPTS: /* Shut GCC up. */
1342 break;
1343 case OPT_fleading_underscore:
1344 CPP_OPTION (pfile, user_label_prefix) = "_";
1345 break;
1346 case OPT_fno_leading_underscore:
1347 CPP_OPTION (pfile, user_label_prefix) = "";
1348 break;
1349 case OPT_fno_operator_names:
1350 CPP_OPTION (pfile, operator_names) = 0;
1351 break;
1352 case OPT_fpreprocessed:
1353 CPP_OPTION (pfile, preprocessed) = 1;
1354 break;
1355 case OPT_fno_preprocessed:
1356 CPP_OPTION (pfile, preprocessed) = 0;
1357 break;
1358 case OPT_fshow_column:
1359 CPP_OPTION (pfile, show_column) = 1;
1360 break;
1361 case OPT_fno_show_column:
1362 CPP_OPTION (pfile, show_column) = 0;
1363 break;
1364 case OPT_ftabstop:
1365 /* Silently ignore empty string, non-longs and silly values. */
1366 if (arg[0] != '\0')
1368 char *endptr;
1369 long tabstop = strtol (arg, &endptr, 10);
1370 if (*endptr == '\0' && tabstop >= 1 && tabstop <= 100)
1371 CPP_OPTION (pfile, tabstop) = tabstop;
1373 break;
1374 case OPT_w:
1375 CPP_OPTION (pfile, inhibit_warnings) = 1;
1376 break;
1377 case OPT_h:
1378 case OPT__help:
1379 print_help ();
1380 CPP_OPTION (pfile, help_only) = 1;
1381 break;
1382 case OPT_target__help:
1383 /* Print if any target specific options. cpplib has none, but
1384 make sure help_only gets set. */
1385 CPP_OPTION (pfile, help_only) = 1;
1386 break;
1388 /* --version inhibits compilation, -version doesn't. -v means
1389 verbose and -version. Historical reasons, don't ask. */
1390 case OPT__version:
1391 CPP_OPTION (pfile, help_only) = 1;
1392 pfile->print_version = 1;
1393 break;
1394 case OPT_v:
1395 CPP_OPTION (pfile, verbose) = 1;
1396 pfile->print_version = 1;
1397 break;
1398 case OPT_version:
1399 pfile->print_version = 1;
1400 break;
1402 case OPT_C:
1403 CPP_OPTION (pfile, discard_comments) = 0;
1404 break;
1405 case OPT_P:
1406 CPP_OPTION (pfile, no_line_commands) = 1;
1407 break;
1408 case OPT_dollar: /* Don't include $ in identifiers. */
1409 CPP_OPTION (pfile, dollars_in_ident) = 0;
1410 break;
1411 case OPT_H:
1412 CPP_OPTION (pfile, print_include_names) = 1;
1413 break;
1414 case OPT_D:
1415 new_pending_directive (pend, arg, cpp_define);
1416 break;
1417 case OPT_pedantic_errors:
1418 CPP_OPTION (pfile, pedantic_errors) = 1;
1419 /* fall through */
1420 case OPT_pedantic:
1421 CPP_OPTION (pfile, pedantic) = 1;
1422 break;
1423 case OPT_trigraphs:
1424 CPP_OPTION (pfile, trigraphs) = 1;
1425 break;
1426 case OPT_plus:
1427 CPP_OPTION (pfile, cplusplus) = 1;
1428 CPP_OPTION (pfile, cplusplus_comments) = 1;
1429 break;
1430 case OPT_remap:
1431 CPP_OPTION (pfile, remap) = 1;
1432 break;
1433 case OPT_iprefix:
1434 CPP_OPTION (pfile, include_prefix) = arg;
1435 CPP_OPTION (pfile, include_prefix_len) = strlen (arg);
1436 break;
1437 case OPT_lang_c:
1438 set_lang (pfile, CLK_GNUC89);
1439 break;
1440 case OPT_lang_cplusplus:
1441 set_lang (pfile, CLK_GNUCXX);
1442 break;
1443 case OPT_lang_objc:
1444 set_lang (pfile, CLK_OBJC);
1445 break;
1446 case OPT_lang_objcplusplus:
1447 set_lang (pfile, CLK_OBJCXX);
1448 break;
1449 case OPT_lang_asm:
1450 set_lang (pfile, CLK_ASM);
1451 break;
1452 case OPT_std_cplusplus98:
1453 set_lang (pfile, CLK_CXX98);
1454 break;
1455 case OPT_std_gnu89:
1456 set_lang (pfile, CLK_GNUC89);
1457 break;
1458 case OPT_std_gnu9x:
1459 case OPT_std_gnu99:
1460 set_lang (pfile, CLK_GNUC99);
1461 break;
1462 case OPT_std_iso9899_199409:
1463 set_lang (pfile, CLK_STDC94);
1464 break;
1465 case OPT_std_iso9899_1990:
1466 case OPT_std_c89:
1467 case OPT_lang_c89:
1468 set_lang (pfile, CLK_STDC89);
1469 break;
1470 case OPT_std_iso9899_199x:
1471 case OPT_std_iso9899_1999:
1472 case OPT_std_c9x:
1473 case OPT_std_c99:
1474 set_lang (pfile, CLK_STDC99);
1475 break;
1476 case OPT_nostdinc:
1477 /* -nostdinc causes no default include directories.
1478 You must specify all include-file directories with -I. */
1479 CPP_OPTION (pfile, no_standard_includes) = 1;
1480 break;
1481 case OPT_nostdincplusplus:
1482 /* -nostdinc++ causes no default C++-specific include directories. */
1483 CPP_OPTION (pfile, no_standard_cplusplus_includes) = 1;
1484 break;
1485 case OPT_o:
1486 if (CPP_OPTION (pfile, out_fname) == NULL)
1487 CPP_OPTION (pfile, out_fname) = arg;
1488 else
1490 cpp_fatal (pfile, "Output filename specified twice");
1491 return argc;
1493 break;
1494 case OPT_d:
1495 /* Args to -d specify what parts of macros to dump.
1496 Silently ignore unrecognised options; they may
1497 be aimed at the compiler proper. */
1499 char c;
1501 while ((c = *arg++) != '\0')
1502 switch (c)
1504 case 'M':
1505 CPP_OPTION (pfile, dump_macros) = dump_only;
1506 CPP_OPTION (pfile, no_output) = 1;
1507 break;
1508 case 'N':
1509 CPP_OPTION (pfile, dump_macros) = dump_names;
1510 break;
1511 case 'D':
1512 CPP_OPTION (pfile, dump_macros) = dump_definitions;
1513 break;
1514 case 'I':
1515 CPP_OPTION (pfile, dump_includes) = 1;
1516 break;
1519 break;
1521 case OPT_MG:
1522 CPP_OPTION (pfile, print_deps_missing_files) = 1;
1523 break;
1524 case OPT_M:
1525 CPP_OPTION (pfile, print_deps) = 2;
1526 break;
1527 case OPT_MM:
1528 CPP_OPTION (pfile, print_deps) = 1;
1529 break;
1530 case OPT_MF:
1531 CPP_OPTION (pfile, deps_file) = arg;
1532 break;
1533 case OPT_MP:
1534 CPP_OPTION (pfile, deps_phony_targets) = 1;
1535 break;
1536 case OPT_MQ:
1537 case OPT_MT:
1538 /* Add a target. -MQ quotes for Make. */
1539 deps_add_target (pfile->deps, arg, opt_code == OPT_MQ);
1540 break;
1542 /* -MD and -MMD for cpp0 are deprecated and undocumented
1543 (use -M or -MM with -MF instead), and probably should be
1544 removed with the next major GCC version. For the moment
1545 we allow these for the benefit of Automake 1.4, which
1546 uses these when dependency tracking is enabled. Automake
1547 1.5 will fix this. */
1548 case OPT_MD:
1549 CPP_OPTION (pfile, print_deps) = 2;
1550 CPP_OPTION (pfile, deps_file) = arg;
1551 break;
1552 case OPT_MMD:
1553 CPP_OPTION (pfile, print_deps) = 1;
1554 CPP_OPTION (pfile, deps_file) = arg;
1555 break;
1557 case OPT_A:
1558 if (arg[0] == '-')
1560 /* -A with an argument beginning with '-' acts as
1561 #unassert on whatever immediately follows the '-'.
1562 If "-" is the whole argument, we eliminate all
1563 predefined macros and assertions, including those
1564 that were specified earlier on the command line.
1565 That way we can get rid of any that were passed
1566 automatically in from GCC. */
1568 if (arg[1] == '\0')
1570 free_chain (pend->directive_head);
1571 pend->directive_head = NULL;
1572 pend->directive_tail = NULL;
1574 else
1575 new_pending_directive (pend, arg + 1, cpp_unassert);
1577 else
1578 new_pending_directive (pend, arg, cpp_assert);
1579 break;
1580 case OPT_U:
1581 new_pending_directive (pend, arg, cpp_undef);
1582 break;
1583 case OPT_I: /* Add directory to path for includes. */
1584 if (!strcmp (arg, "-"))
1586 /* -I- means:
1587 Use the preceding -I directories for #include "..."
1588 but not #include <...>.
1589 Don't search the directory of the present file
1590 for #include "...". (Note that -I. -I- is not the same as
1591 the default setup; -I. uses the compiler's working dir.) */
1592 if (! CPP_OPTION (pfile, ignore_srcdir))
1594 pend->quote_head = pend->brack_head;
1595 pend->quote_tail = pend->brack_tail;
1596 pend->brack_head = 0;
1597 pend->brack_tail = 0;
1598 CPP_OPTION (pfile, ignore_srcdir) = 1;
1600 else
1602 cpp_fatal (pfile, "-I- specified twice");
1603 return argc;
1606 else
1607 append_include_chain (pfile, xstrdup (arg), BRACKET, 0);
1608 break;
1609 case OPT_isystem:
1610 /* Add directory to beginning of system include path, as a system
1611 include directory. */
1612 append_include_chain (pfile, xstrdup (arg), SYSTEM, 0);
1613 break;
1614 case OPT_include:
1615 case OPT_imacros:
1617 struct pending_option *o = (struct pending_option *)
1618 xmalloc (sizeof (struct pending_option));
1619 o->arg = arg;
1620 o->next = NULL;
1622 if (opt_code == OPT_include)
1623 APPEND (pend, include, o);
1624 else
1625 APPEND (pend, imacros, o);
1627 break;
1628 case OPT_iwithprefix:
1629 /* Add directory to end of path for includes,
1630 with the default prefix at the front of its name. */
1631 /* fall through */
1632 case OPT_iwithprefixbefore:
1633 /* Add directory to main path for includes,
1634 with the default prefix at the front of its name. */
1636 char *fname;
1637 int len;
1639 len = strlen (arg);
1641 if (CPP_OPTION (pfile, include_prefix) != 0)
1643 size_t ipl = CPP_OPTION (pfile, include_prefix_len);
1644 fname = xmalloc (ipl + len + 1);
1645 memcpy (fname, CPP_OPTION (pfile, include_prefix), ipl);
1646 memcpy (fname + ipl, arg, len + 1);
1648 else if (cpp_GCC_INCLUDE_DIR_len)
1650 fname = xmalloc (cpp_GCC_INCLUDE_DIR_len + len + 1);
1651 memcpy (fname, cpp_GCC_INCLUDE_DIR, cpp_GCC_INCLUDE_DIR_len);
1652 memcpy (fname + cpp_GCC_INCLUDE_DIR_len, arg, len + 1);
1654 else
1655 fname = xstrdup (arg);
1657 append_include_chain (pfile, fname,
1658 opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0);
1660 break;
1661 case OPT_idirafter:
1662 /* Add directory to end of path for includes. */
1663 append_include_chain (pfile, xstrdup (arg), AFTER, 0);
1664 break;
1665 case OPT_W:
1666 /* Silently ignore unrecognised options. */
1667 if (!strcmp (argv[i], "-Wall"))
1669 CPP_OPTION (pfile, warn_trigraphs) = 1;
1670 CPP_OPTION (pfile, warn_comments) = 1;
1672 else if (!strcmp (argv[i], "-Wtraditional"))
1673 CPP_OPTION (pfile, warn_traditional) = 1;
1674 else if (!strcmp (argv[i], "-Wtrigraphs"))
1675 CPP_OPTION (pfile, warn_trigraphs) = 1;
1676 else if (!strcmp (argv[i], "-Wcomment"))
1677 CPP_OPTION (pfile, warn_comments) = 1;
1678 else if (!strcmp (argv[i], "-Wcomments"))
1679 CPP_OPTION (pfile, warn_comments) = 1;
1680 else if (!strcmp (argv[i], "-Wundef"))
1681 CPP_OPTION (pfile, warn_undef) = 1;
1682 else if (!strcmp (argv[i], "-Wimport"))
1683 CPP_OPTION (pfile, warn_import) = 1;
1684 else if (!strcmp (argv[i], "-Werror"))
1685 CPP_OPTION (pfile, warnings_are_errors) = 1;
1686 else if (!strcmp (argv[i], "-Wsystem-headers"))
1687 CPP_OPTION (pfile, warn_system_headers) = 1;
1688 else if (!strcmp (argv[i], "-Wno-traditional"))
1689 CPP_OPTION (pfile, warn_traditional) = 0;
1690 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1691 CPP_OPTION (pfile, warn_trigraphs) = 0;
1692 else if (!strcmp (argv[i], "-Wno-comment"))
1693 CPP_OPTION (pfile, warn_comments) = 0;
1694 else if (!strcmp (argv[i], "-Wno-comments"))
1695 CPP_OPTION (pfile, warn_comments) = 0;
1696 else if (!strcmp (argv[i], "-Wno-undef"))
1697 CPP_OPTION (pfile, warn_undef) = 0;
1698 else if (!strcmp (argv[i], "-Wno-import"))
1699 CPP_OPTION (pfile, warn_import) = 0;
1700 else if (!strcmp (argv[i], "-Wno-error"))
1701 CPP_OPTION (pfile, warnings_are_errors) = 0;
1702 else if (!strcmp (argv[i], "-Wno-system-headers"))
1703 CPP_OPTION (pfile, warn_system_headers) = 0;
1704 break;
1707 return i + 1;
1710 /* Handle command-line options in (argc, argv).
1711 Can be called multiple times, to handle multiple sets of options.
1712 Returns if an unrecognized option is seen.
1713 Returns number of strings consumed. */
1715 cpp_handle_options (pfile, argc, argv)
1716 cpp_reader *pfile;
1717 int argc;
1718 char **argv;
1720 int i;
1721 int strings_processed;
1723 for (i = 0; i < argc; i += strings_processed)
1725 strings_processed = cpp_handle_option (pfile, argc - i, argv + i);
1726 if (strings_processed == 0)
1727 break;
1730 return i;
1733 /* Extra processing when all options are parsed, after all calls to
1734 cpp_handle_option[s]. Consistency checks etc. */
1735 void
1736 cpp_post_options (pfile)
1737 cpp_reader *pfile;
1739 if (pfile->print_version)
1741 fprintf (stderr, _("GNU CPP version %s (cpplib)"), version_string);
1742 #ifdef TARGET_VERSION
1743 TARGET_VERSION;
1744 #endif
1745 fputc ('\n', stderr);
1748 /* Canonicalize in_fname and out_fname. We guarantee they are not
1749 NULL, and that the empty string represents stdin / stdout. */
1750 if (CPP_OPTION (pfile, in_fname) == NULL
1751 || !strcmp (CPP_OPTION (pfile, in_fname), "-"))
1752 CPP_OPTION (pfile, in_fname) = "";
1754 if (CPP_OPTION (pfile, out_fname) == NULL
1755 || !strcmp (CPP_OPTION (pfile, out_fname), "-"))
1756 CPP_OPTION (pfile, out_fname) = "";
1758 /* -Wtraditional is not useful in C++ mode. */
1759 if (CPP_OPTION (pfile, cplusplus))
1760 CPP_OPTION (pfile, warn_traditional) = 0;
1762 /* Set this if it hasn't been set already. */
1763 if (CPP_OPTION (pfile, user_label_prefix) == NULL)
1764 CPP_OPTION (pfile, user_label_prefix) = USER_LABEL_PREFIX;
1766 /* Permanently disable macro expansion if we are rescanning
1767 preprocessed text. */
1768 if (CPP_OPTION (pfile, preprocessed))
1769 pfile->state.prevent_expansion = 1;
1771 /* We need to do this after option processing and before
1772 cpp_start_read, as cppmain.c relies on the options->no_output to
1773 set its callbacks correctly before calling cpp_start_read. */
1774 init_dependency_output (pfile);
1776 /* After checking the environment variables, check if -M or -MM has
1777 not been specified, but other -M options have. */
1778 if (CPP_OPTION (pfile, print_deps) == 0 &&
1779 (CPP_OPTION (pfile, print_deps_missing_files)
1780 || CPP_OPTION (pfile, deps_file)
1781 || CPP_OPTION (pfile, deps_phony_targets)))
1782 cpp_fatal (pfile, "you must additionally specify either -M or -MM");
1785 /* Set up dependency-file output. */
1786 static void
1787 init_dependency_output (pfile)
1788 cpp_reader *pfile;
1790 char *spec, *s, *output_file;
1792 /* Either of two environment variables can specify output of deps.
1793 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1794 where OUTPUT_FILE is the file to write deps info to
1795 and DEPS_TARGET is the target to mention in the deps. */
1797 if (CPP_OPTION (pfile, print_deps) == 0)
1799 spec = getenv ("DEPENDENCIES_OUTPUT");
1800 if (spec)
1801 CPP_OPTION (pfile, print_deps) = 1;
1802 else
1804 spec = getenv ("SUNPRO_DEPENDENCIES");
1805 if (spec)
1806 CPP_OPTION (pfile, print_deps) = 2;
1807 else
1808 return;
1811 /* Find the space before the DEPS_TARGET, if there is one. */
1812 s = strchr (spec, ' ');
1813 if (s)
1815 /* Let the caller perform MAKE quoting. */
1816 deps_add_target (pfile->deps, s + 1, 0);
1817 output_file = (char *) xmalloc (s - spec + 1);
1818 memcpy (output_file, spec, s - spec);
1819 output_file[s - spec] = 0;
1821 else
1822 output_file = spec;
1824 /* Command line overrides environment variables. */
1825 if (CPP_OPTION (pfile, deps_file) == 0)
1826 CPP_OPTION (pfile, deps_file) = output_file;
1827 CPP_OPTION (pfile, print_deps_append) = 1;
1830 /* If dependencies go to standard output, or -MG is used, we should
1831 suppress output, including -dM, -dI etc. */
1832 if (CPP_OPTION (pfile, deps_file) == 0
1833 || CPP_OPTION (pfile, print_deps_missing_files))
1835 CPP_OPTION (pfile, no_output) = 1;
1836 CPP_OPTION (pfile, dump_macros) = 0;
1837 CPP_OPTION (pfile, dump_includes) = 0;
1841 static void
1842 print_help ()
1844 fprintf (stderr, _("Usage: %s [switches] input output\n"), progname);
1845 /* To keep the lines from getting too long for some compilers, limit
1846 to about 500 characters (6 lines) per chunk. */
1847 fputs (_("\
1848 Switches:\n\
1849 -include <file> Include the contents of <file> before other files\n\
1850 -imacros <file> Accept definition of macros in <file>\n\
1851 -iprefix <path> Specify <path> as a prefix for next two options\n\
1852 -iwithprefix <dir> Add <dir> to the end of the system include path\n\
1853 -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\
1854 -isystem <dir> Add <dir> to the start of the system include path\n\
1855 "), stdout);
1856 fputs (_("\
1857 -idirafter <dir> Add <dir> to the end of the system include path\n\
1858 -I <dir> Add <dir> to the end of the main include path\n\
1859 -I- Fine-grained include path control; see info docs\n\
1860 -nostdinc Do not search system include directories\n\
1861 (dirs specified with -isystem will still be used)\n\
1862 -nostdinc++ Do not search system include directories for C++\n\
1863 -o <file> Put output into <file>\n\
1864 "), stdout);
1865 fputs (_("\
1866 -pedantic Issue all warnings demanded by strict ISO C\n\
1867 -pedantic-errors Issue -pedantic warnings as errors instead\n\
1868 -trigraphs Support ISO C trigraphs\n\
1869 -lang-c Assume that the input sources are in C\n\
1870 -lang-c89 Assume that the input sources are in C89\n\
1871 "), stdout);
1872 fputs (_("\
1873 -lang-c++ Assume that the input sources are in C++\n\
1874 -lang-objc Assume that the input sources are in ObjectiveC\n\
1875 -lang-objc++ Assume that the input sources are in ObjectiveC++\n\
1876 -lang-asm Assume that the input sources are in assembler\n\
1877 "), stdout);
1878 fputs (_("\
1879 -std=<std name> Specify the conformance standard; one of:\n\
1880 gnu89, gnu99, c89, c99, iso9899:1990,\n\
1881 iso9899:199409, iso9899:1999\n\
1882 -+ Allow parsing of C++ style features\n\
1883 -w Inhibit warning messages\n\
1884 -Wtrigraphs Warn if trigraphs are encountered\n\
1885 -Wno-trigraphs Do not warn about trigraphs\n\
1886 -Wcomment{s} Warn if one comment starts inside another\n\
1887 "), stdout);
1888 fputs (_("\
1889 -Wno-comment{s} Do not warn about comments\n\
1890 -Wtraditional Warn about features not present in traditional C\n\
1891 -Wno-traditional Do not warn about traditional C\n\
1892 -Wundef Warn if an undefined macro is used by #if\n\
1893 -Wno-undef Do not warn about testing undefined macros\n\
1894 -Wimport Warn about the use of the #import directive\n\
1895 "), stdout);
1896 fputs (_("\
1897 -Wno-import Do not warn about the use of #import\n\
1898 -Werror Treat all warnings as errors\n\
1899 -Wno-error Do not treat warnings as errors\n\
1900 -Wsystem-headers Do not suppress warnings from system headers\n\
1901 -Wno-system-headers Suppress warnings from system headers\n\
1902 -Wall Enable all preprocessor warnings\n\
1903 "), stdout);
1904 fputs (_("\
1905 -M Generate make dependencies\n\
1906 -MM As -M, but ignore system header files\n\
1907 -MF <file> Write dependency output to the given file\n\
1908 -MG Treat missing header file as generated files\n\
1909 "), stdout);
1910 fputs (_("\
1911 -MP Generate phony targets for all headers\n\
1912 -MQ <target> Add a MAKE-quoted target\n\
1913 -MT <target> Add an unquoted target\n\
1914 "), stdout);
1915 fputs (_("\
1916 -D<macro> Define a <macro> with string '1' as its value\n\
1917 -D<macro>=<val> Define a <macro> with <val> as its value\n\
1918 -A<question> (<answer>) Assert the <answer> to <question>\n\
1919 -A-<question> (<answer>) Disable the <answer> to <question>\n\
1920 -U<macro> Undefine <macro> \n\
1921 -v Display the version number\n\
1922 "), stdout);
1923 fputs (_("\
1924 -H Print the name of header files as they are used\n\
1925 -C Do not discard comments\n\
1926 -dM Display a list of macro definitions active at end\n\
1927 -dD Preserve macro definitions in output\n\
1928 -dN As -dD except that only the names are preserved\n\
1929 -dI Include #include directives in the output\n\
1930 "), stdout);
1931 fputs (_("\
1932 -fpreprocessed Treat the input file as already preprocessed\n\
1933 -ftabstop=<number> Distance between tab stops for column reporting\n\
1934 -P Do not generate #line directives\n\
1935 -$ Do not allow '$' in identifiers\n\
1936 -remap Remap file names when including files.\n\
1937 --version Display version information\n\
1938 -h or --help Display this information\n\
1939 "), stdout);