Add D30V options
[official-gcc.git] / gcc / cppinit.c
blob5f5c6201423757f5c4d3df6dcc72392ccd215774
1 /* CPP Library.
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 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 #elif (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
45 #define INO_T_EQ(a, b) 0
46 #else
47 #define INO_T_EQ(a, b) ((a) == (b))
48 #endif
50 /* Internal structures and prototypes. */
52 /* A `struct pending_option' remembers one -D, -A, -U, -include, or -imacros
53 switch. There are four lists: one for -D and -U, one for -A, one
54 for -include, one for -imacros. `undef' is set for -U, clear for
55 -D, ignored for the others.
56 (Future: add an equivalent of -U for -A) */
58 typedef void (* cl_directive_handler) PARAMS ((cpp_reader *, const char *));
59 struct pending_option
61 struct pending_option *next;
62 const char *arg;
63 cl_directive_handler handler;
66 /* The `pending' structure accumulates all the options that are not
67 actually processed until we hit cpp_start_read. It consists of
68 several lists, one for each type of option. We keep both head and
69 tail pointers for quick insertion. */
70 struct cpp_pending
72 struct pending_option *directive_head, *directive_tail;
74 struct file_name_list *quote_head, *quote_tail;
75 struct file_name_list *brack_head, *brack_tail;
76 struct file_name_list *systm_head, *systm_tail;
77 struct file_name_list *after_head, *after_tail;
79 struct pending_option *imacros_head, *imacros_tail;
80 struct pending_option *include_head, *include_tail;
83 #ifdef __STDC__
84 #define APPEND(pend, list, elt) \
85 do { if (!(pend)->list##_head) (pend)->list##_head = (elt); \
86 else (pend)->list##_tail->next = (elt); \
87 (pend)->list##_tail = (elt); \
88 } while (0)
89 #else
90 #define APPEND(pend, list, elt) \
91 do { if (!(pend)->list/**/_head) (pend)->list/**/_head = (elt); \
92 else (pend)->list/**/_tail->next = (elt); \
93 (pend)->list/**/_tail = (elt); \
94 } while (0)
95 #endif
97 static void print_help PARAMS ((void));
98 static void path_include PARAMS ((cpp_reader *,
99 struct cpp_pending *,
100 char *, int));
101 static void initialize_builtins PARAMS ((cpp_reader *));
102 static void append_include_chain PARAMS ((cpp_reader *,
103 struct cpp_pending *,
104 char *, int, int));
105 static void merge_include_chains PARAMS ((cpp_reader *));
107 static void initialize_dependency_output PARAMS ((cpp_reader *));
108 static void initialize_standard_includes PARAMS ((cpp_reader *));
109 static void new_pending_directive PARAMS ((struct cpp_pending *,
110 const char *,
111 cl_directive_handler));
112 #ifdef HOST_EBCDIC
113 static int opt_comp PARAMS ((const void *, const void *));
114 static void sort_options PARAMS ((void));
115 #endif
116 static int parse_option PARAMS ((const char *));
117 static int dump_macros_helper PARAMS ((cpp_reader *, cpp_hashnode *));
119 /* Fourth argument to append_include_chain: chain to use */
120 enum { QUOTE = 0, BRACKET, SYSTEM, AFTER };
122 /* If we have designated initializers (GCC >2.7) this table can be
123 initialized, constant data. Otherwise, it has to be filled in at
124 runtime. */
126 #if (GCC_VERSION >= 2007)
127 #define init_IStable() /* nothing */
128 #define ISTABLE __extension__ const unsigned char _cpp_IStable[256] = {
129 #define END };
130 #define s(p, v) [p] = v,
131 #else
132 #define ISTABLE unsigned char _cpp_IStable[256] = { 0 }; \
133 static void init_IStable PARAMS ((void)) { \
134 unsigned char *x = _cpp_IStable;
135 #define END }
136 #define s(p, v) x[p] = v;
137 #endif
139 #define A(x) s(x, ISidnum|ISidstart)
140 #define N(x) s(x, ISidnum|ISnumstart)
141 #define H(x) s(x, IShspace|ISspace)
142 #define V(x) s(x, ISvspace|ISspace)
143 #define S(x) s(x, ISspace)
145 ISTABLE
146 A('_')
148 A('a') A('b') A('c') A('d') A('e') A('f') A('g') A('h') A('i')
149 A('j') A('k') A('l') A('m') A('n') A('o') A('p') A('q') A('r')
150 A('s') A('t') A('u') A('v') A('w') A('x') A('y') A('z')
152 A('A') A('B') A('C') A('D') A('E') A('F') A('G') A('H') A('I')
153 A('J') A('K') A('L') A('M') A('N') A('O') A('P') A('Q') A('R')
154 A('S') A('T') A('U') A('V') A('W') A('X') A('Y') A('Z')
156 N('1') N('2') N('3') N('4') N('5') N('6') N('7') N('8') N('9') N('0')
158 H(' ') H('\t')
160 V('\n') V('\r')
162 S('\0') S('\v') S('\f')
165 #undef A
166 #undef N
167 #undef H
168 #undef V
169 #undef S
170 #undef s
171 #undef ISTABLE
172 #undef END
174 /* Given a colon-separated list of file names PATH,
175 add all the names to the search path for include files. */
177 static void
178 path_include (pfile, pend, list, path)
179 cpp_reader *pfile;
180 struct cpp_pending *pend;
181 char *list;
182 int path;
184 char *p, *q, *name;
186 p = list;
190 /* Find the end of this name. */
191 q = p;
192 while (*q != 0 && *q != PATH_SEPARATOR) q++;
193 if (q == p)
195 /* An empty name in the path stands for the current directory. */
196 name = (char *) xmalloc (2);
197 name[0] = '.';
198 name[1] = 0;
200 else
202 /* Otherwise use the directory that is named. */
203 name = (char *) xmalloc (q - p + 1);
204 memcpy (name, p, q - p);
205 name[q - p] = 0;
208 append_include_chain (pfile, pend, name, path, 0);
210 /* Advance past this name. */
211 if (*q == 0)
212 break;
213 p = q + 1;
215 while (1);
218 /* Append DIR to include path PATH. DIR must be permanently allocated
219 and writable. */
220 static void
221 append_include_chain (pfile, pend, dir, path, cxx_aware)
222 cpp_reader *pfile;
223 struct cpp_pending *pend;
224 char *dir;
225 int path;
226 int cxx_aware;
228 struct file_name_list *new;
229 struct stat st;
230 unsigned int len;
232 _cpp_simplify_pathname (dir);
233 if (stat (dir, &st))
235 /* Dirs that don't exist are silently ignored. */
236 if (errno != ENOENT)
237 cpp_notice_from_errno (pfile, dir);
238 else if (CPP_OPTION (pfile, verbose))
239 fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"), dir);
240 return;
243 if (!S_ISDIR (st.st_mode))
245 cpp_notice (pfile, "%s: Not a directory", dir);
246 return;
249 len = strlen (dir);
250 if (len > pfile->max_include_len)
251 pfile->max_include_len = len;
253 new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
254 new->name = dir;
255 new->nlen = len;
256 new->ino = st.st_ino;
257 new->dev = st.st_dev;
258 if (path == SYSTEM)
259 new->sysp = cxx_aware ? 1 : 2;
260 else
261 new->sysp = 0;
262 new->name_map = NULL;
263 new->next = NULL;
264 new->alloc = NULL;
266 switch (path)
268 case QUOTE: APPEND (pend, quote, new); break;
269 case BRACKET: APPEND (pend, brack, new); break;
270 case SYSTEM: APPEND (pend, systm, new); break;
271 case AFTER: APPEND (pend, after, new); break;
275 /* Merge the four include chains together in the order quote, bracket,
276 system, after. Remove duplicate dirs (as determined by
277 INO_T_EQ()). The system_include and after_include chains are never
278 referred to again after this function; all access is through the
279 bracket_include path.
281 For the future: Check if the directory is empty (but
282 how?) and possibly preload the include hash. */
284 static void
285 merge_include_chains (pfile)
286 cpp_reader *pfile;
288 struct file_name_list *prev, *cur, *other;
289 struct file_name_list *quote, *brack, *systm, *after;
290 struct file_name_list *qtail, *btail, *stail, *atail;
292 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
294 qtail = pend->quote_tail;
295 btail = pend->brack_tail;
296 stail = pend->systm_tail;
297 atail = pend->after_tail;
299 quote = pend->quote_head;
300 brack = pend->brack_head;
301 systm = pend->systm_head;
302 after = pend->after_head;
304 /* Paste together bracket, system, and after include chains. */
305 if (stail)
306 stail->next = after;
307 else
308 systm = after;
309 if (btail)
310 btail->next = systm;
311 else
312 brack = systm;
314 /* This is a bit tricky.
315 First we drop dupes from the quote-include list.
316 Then we drop dupes from the bracket-include list.
317 Finally, if qtail and brack are the same directory,
318 we cut out qtail.
320 We can't just merge the lists and then uniquify them because
321 then we may lose directories from the <> search path that should
322 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
323 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
324 -Ibar -I- -Ifoo -Iquux.
326 Note that this algorithm is quadratic in the number of -I switches,
327 which is acceptable since there aren't usually that many of them. */
329 for (cur = quote, prev = NULL; cur; cur = cur->next)
331 for (other = quote; other != cur; other = other->next)
332 if (INO_T_EQ (cur->ino, other->ino)
333 && cur->dev == other->dev)
335 if (CPP_OPTION (pfile, verbose))
336 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"),
337 cur->name);
339 prev->next = cur->next;
340 free (cur->name);
341 free (cur);
342 cur = prev;
343 break;
345 prev = cur;
347 qtail = prev;
349 for (cur = brack; cur; cur = cur->next)
351 for (other = brack; other != cur; other = other->next)
352 if (INO_T_EQ (cur->ino, other->ino)
353 && cur->dev == other->dev)
355 if (CPP_OPTION (pfile, verbose))
356 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"),
357 cur->name);
359 prev->next = cur->next;
360 free (cur->name);
361 free (cur);
362 cur = prev;
363 break;
365 prev = cur;
368 if (quote)
370 if (INO_T_EQ (qtail->ino, brack->ino) && qtail->dev == brack->dev)
372 if (quote == qtail)
374 if (CPP_OPTION (pfile, verbose))
375 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"),
376 quote->name);
378 free (quote->name);
379 free (quote);
380 quote = brack;
382 else
384 cur = quote;
385 while (cur->next != qtail)
386 cur = cur->next;
387 cur->next = brack;
388 if (CPP_OPTION (pfile, verbose))
389 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"),
390 qtail->name);
392 free (qtail->name);
393 free (qtail);
396 else
397 qtail->next = brack;
399 else
400 quote = brack;
402 CPP_OPTION (pfile, quote_include) = quote;
403 CPP_OPTION (pfile, bracket_include) = brack;
407 /* Initialize a cpp_reader structure. */
408 void
409 cpp_reader_init (pfile)
410 cpp_reader *pfile;
412 #ifdef HOST_EBCDIC
413 sort_options ();
414 #endif
416 memset ((char *) pfile, 0, sizeof (cpp_reader));
418 pfile->token_buffer_size = 200;
419 pfile->token_buffer = (U_CHAR *) xmalloc (pfile->token_buffer_size);
420 CPP_SET_WRITTEN (pfile, 0);
422 CPP_OPTION (pfile, dollars_in_ident) = 1;
423 CPP_OPTION (pfile, cplusplus_comments) = 1;
424 CPP_OPTION (pfile, warn_import) = 1;
425 CPP_OPTION (pfile, warn_paste) = 1;
426 CPP_OPTION (pfile, digraphs) = 1;
427 CPP_OPTION (pfile, discard_comments) = 1;
428 CPP_OPTION (pfile, show_column) = 1;
429 CPP_OPTION (pfile, tabstop) = 8;
431 CPP_OPTION (pfile, pending) =
432 (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
434 _cpp_init_macros (pfile);
435 _cpp_init_stacks (pfile);
436 _cpp_init_includes (pfile);
439 /* Initialize a cpp_printer structure. As a side effect, open the
440 output file. */
441 cpp_printer *
442 cpp_printer_init (pfile, print)
443 cpp_reader *pfile;
444 cpp_printer *print;
446 memset (print, '\0', sizeof (cpp_printer));
447 if (CPP_OPTION (pfile, out_fname) == NULL)
448 CPP_OPTION (pfile, out_fname) = "";
450 if (CPP_OPTION (pfile, out_fname)[0] == '\0')
451 print->outf = stdout;
452 else
454 print->outf = fopen (CPP_OPTION (pfile, out_fname), "w");
455 if (! print->outf)
457 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));
458 return NULL;
461 return print;
464 /* Free resources used by PFILE.
465 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
466 void
467 cpp_cleanup (pfile)
468 cpp_reader *pfile;
470 while (CPP_BUFFER (pfile) != NULL)
471 cpp_pop_buffer (pfile);
473 if (pfile->token_buffer)
475 free (pfile->token_buffer);
476 pfile->token_buffer = NULL;
479 if (pfile->deps)
480 deps_free (pfile->deps);
482 if (pfile->spec_nodes)
483 free (pfile->spec_nodes);
485 _cpp_free_temp_tokens (pfile);
486 _cpp_cleanup_includes (pfile);
487 _cpp_cleanup_stacks (pfile);
488 _cpp_cleanup_macros (pfile);
492 /* This structure defines one built-in macro. A node of type TYPE will
493 be entered in the macro hash table under the name NAME, with value
494 VALUE (if any). If TYPE is T_OPERATOR, the CODE field is used instead.
496 Two values are not compile time constants, so we tag
497 them in the FLAGS field instead:
498 VERS value is the global version_string, quoted
499 ULP value is the global user_label_prefix
501 Also, macros with CPLUS set in the flags field are entered only for C++.
504 struct builtin
506 const U_CHAR *name;
507 const char *value;
508 unsigned char code;
509 unsigned char type;
510 unsigned short flags;
511 unsigned int len;
513 #define VERS 0x01
514 #define ULP 0x02
515 #define CPLUS 0x04
517 #define B(n, t) { U n, 0, 0, t, 0, sizeof n - 1 }
518 #define C(n, v) { U n, v, 0, T_MACRO, 0, sizeof n - 1 }
519 #define X(n, f) { U n, 0, 0, T_MACRO, f, sizeof n - 1 }
520 #define O(n, c, f) { U n, 0, c, T_OPERATOR, f, sizeof n - 1 }
521 static const struct builtin builtin_array[] =
523 B("__TIME__", T_TIME),
524 B("__DATE__", T_DATE),
525 B("__FILE__", T_FILE),
526 B("__BASE_FILE__", T_BASE_FILE),
527 B("__LINE__", T_SPECLINE),
528 B("__INCLUDE_LEVEL__", T_INCLUDE_LEVEL),
529 B("__STDC__", T_STDC),
531 X("__VERSION__", VERS),
532 X("__USER_LABEL_PREFIX__", ULP),
533 C("__REGISTER_PREFIX__", REGISTER_PREFIX),
534 C("__HAVE_BUILTIN_SETJMP__", "1"),
535 #ifndef NO_BUILTIN_SIZE_TYPE
536 C("__SIZE_TYPE__", SIZE_TYPE),
537 #endif
538 #ifndef NO_BUILTIN_PTRDIFF_TYPE
539 C("__PTRDIFF_TYPE__", PTRDIFF_TYPE),
540 #endif
541 #ifndef NO_BUILTIN_WCHAR_TYPE
542 C("__WCHAR_TYPE__", WCHAR_TYPE),
543 #endif
545 /* Named operators known to the preprocessor. These cannot be #defined
546 and always have their stated meaning. They are treated like normal
547 string tokens except for the type code and the meaning. Most of them
548 are only for C++ (but see iso646.h). */
549 O("defined", CPP_DEFINED, 0),
550 O("and", CPP_AND_AND, CPLUS),
551 O("and_eq", CPP_AND_EQ, CPLUS),
552 O("bitand", CPP_AND, CPLUS),
553 O("bitor", CPP_OR, CPLUS),
554 O("compl", CPP_COMPL, CPLUS),
555 O("not", CPP_NOT, CPLUS),
556 O("not_eq", CPP_NOT_EQ, CPLUS),
557 O("or", CPP_OR_OR, CPLUS),
558 O("or_eq", CPP_OR_EQ, CPLUS),
559 O("xor", CPP_XOR, CPLUS),
560 O("xor_eq", CPP_XOR_EQ, CPLUS),
562 #undef B
563 #undef C
564 #undef X
565 #define builtin_array_end \
566 builtin_array + sizeof(builtin_array)/sizeof(struct builtin)
568 /* Subroutine of cpp_start_read; reads the builtins table above and
569 enters the macros into the hash table. */
570 static void
571 initialize_builtins (pfile)
572 cpp_reader *pfile;
574 const struct builtin *b;
575 for(b = builtin_array; b < builtin_array_end; b++)
577 if (b->flags & CPLUS && ! CPP_OPTION (pfile, cplusplus))
578 continue;
580 if (b->type == T_MACRO)
582 const char *val;
583 char *str;
585 if (b->flags & VERS)
587 /* Allocate enough space for 'name="value"\0'. */
588 str = xmalloc (b->len + strlen (version_string) + 4);
589 sprintf (str, "%s=\"%s\"", b->name, version_string);
591 else
593 if (b->flags & ULP)
594 val = user_label_prefix;
595 else
596 val = b->value;
598 /* Allocate enough space for "name=value\0". */
599 str = xmalloc (b->len + strlen (val) + 2);
600 sprintf(str, "%s=%s", b->name, val);
602 cpp_define (pfile, str);
604 else
606 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
607 hp->type = b->type;
608 if (b->type == T_OPERATOR)
609 hp->value.code = b->code;
613 #undef VERS
614 #undef ULP
615 #undef builtin_array_end
617 /* Another subroutine of cpp_start_read. This one sets up to do
618 dependency-file output. */
619 static void
620 initialize_dependency_output (pfile)
621 cpp_reader *pfile;
623 char *spec, *s, *output_file;
625 /* Either of two environment variables can specify output of deps.
626 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
627 where OUTPUT_FILE is the file to write deps info to
628 and DEPS_TARGET is the target to mention in the deps. */
630 if (CPP_OPTION (pfile, print_deps) == 0)
632 spec = getenv ("DEPENDENCIES_OUTPUT");
633 if (spec)
634 CPP_OPTION (pfile, print_deps) = 1;
635 else
637 spec = getenv ("SUNPRO_DEPENDENCIES");
638 if (spec)
639 CPP_OPTION (pfile, print_deps) = 2;
640 else
641 return;
644 /* Find the space before the DEPS_TARGET, if there is one. */
645 s = strchr (spec, ' ');
646 if (s)
648 CPP_OPTION (pfile, deps_target) = s + 1;
649 output_file = (char *) xmalloc (s - spec + 1);
650 memcpy (output_file, spec, s - spec);
651 output_file[s - spec] = 0;
653 else
655 CPP_OPTION (pfile, deps_target) = 0;
656 output_file = spec;
659 CPP_OPTION (pfile, deps_file) = output_file;
660 CPP_OPTION (pfile, print_deps_append) = 1;
663 pfile->deps = deps_init ();
665 /* Print the expected object file name as the target of this Make-rule. */
666 if (CPP_OPTION (pfile, deps_target))
667 deps_add_target (pfile->deps, CPP_OPTION (pfile, deps_target));
668 else if (*CPP_OPTION (pfile, in_fname) == 0)
669 deps_add_target (pfile->deps, "-");
670 else
671 deps_calc_target (pfile->deps, CPP_OPTION (pfile, in_fname));
673 if (CPP_OPTION (pfile, in_fname))
674 deps_add_dep (pfile->deps, CPP_OPTION (pfile, in_fname));
677 /* And another subroutine. This one sets up the standard include path. */
678 static void
679 initialize_standard_includes (pfile)
680 cpp_reader *pfile;
682 char *path;
683 const struct default_include *p;
684 const char *specd_prefix = CPP_OPTION (pfile, include_prefix);
686 /* Several environment variables may add to the include search path.
687 CPATH specifies an additional list of directories to be searched
688 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
689 etc. specify an additional list of directories to be searched as
690 if specified with -isystem, for the language indicated. */
692 GET_ENV_PATH_LIST (path, "CPATH");
693 if (path != 0 && *path != 0)
694 path_include (pfile, CPP_OPTION (pfile, pending), path, BRACKET);
696 switch ((CPP_OPTION (pfile, objc) << 1) + CPP_OPTION (pfile, cplusplus))
698 case 0:
699 GET_ENV_PATH_LIST (path, "C_INCLUDE_PATH");
700 break;
701 case 1:
702 GET_ENV_PATH_LIST (path, "CPLUS_INCLUDE_PATH");
703 break;
704 case 2:
705 GET_ENV_PATH_LIST (path, "OBJC_INCLUDE_PATH");
706 break;
707 case 3:
708 GET_ENV_PATH_LIST (path, "OBJCPLUS_INCLUDE_PATH");
709 break;
711 if (path != 0 && *path != 0)
712 path_include (pfile, CPP_OPTION (pfile, pending), path, SYSTEM);
714 /* Search "translated" versions of GNU directories.
715 These have /usr/local/lib/gcc... replaced by specd_prefix. */
716 if (specd_prefix != 0 && cpp_GCC_INCLUDE_DIR_len)
718 /* Remove the `include' from /usr/local/lib/gcc.../include.
719 GCC_INCLUDE_DIR will always end in /include. */
720 int default_len = cpp_GCC_INCLUDE_DIR_len;
721 char *default_prefix = (char *) alloca (default_len + 1);
722 int specd_len = strlen (specd_prefix);
724 memcpy (default_prefix, cpp_GCC_INCLUDE_DIR, default_len);
725 default_prefix[default_len] = '\0';
727 for (p = cpp_include_defaults; p->fname; p++)
729 /* Some standard dirs are only for C++. */
730 if (!p->cplusplus
731 || (CPP_OPTION (pfile, cplusplus)
732 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
734 /* Does this dir start with the prefix? */
735 if (!strncmp (p->fname, default_prefix, default_len))
737 /* Yes; change prefix and add to search list. */
738 int flen = strlen (p->fname);
739 int this_len = specd_len + flen - default_len;
740 char *str = (char *) xmalloc (this_len + 1);
741 memcpy (str, specd_prefix, specd_len);
742 memcpy (str + specd_len,
743 p->fname + default_len,
744 flen - default_len + 1);
746 append_include_chain (pfile, CPP_OPTION (pfile, pending),
747 str, SYSTEM, p->cxx_aware);
753 /* Search ordinary names for GNU include directories. */
754 for (p = cpp_include_defaults; p->fname; p++)
756 /* Some standard dirs are only for C++. */
757 if (!p->cplusplus
758 || (CPP_OPTION (pfile, cplusplus)
759 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
761 /* XXX Potential memory leak! */
762 char *str = xstrdup (update_path (p->fname, p->component));
763 append_include_chain (pfile, CPP_OPTION (pfile, pending),
764 str, SYSTEM, p->cxx_aware);
769 /* This is called after options have been processed.
770 * Check options for consistency, and setup for processing input
771 * from the file named FNAME. (Use standard input if FNAME==NULL.)
772 * Return 1 on success, 0 on failure.
776 cpp_start_read (pfile, print, fname)
777 cpp_reader *pfile;
778 cpp_printer *print;
779 const char *fname;
781 struct pending_option *p, *q;
783 /* -MG doesn't select the form of output and must be specified with one of
784 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
785 inhibit compilation. */
786 if (CPP_OPTION (pfile, print_deps_missing_files)
787 && (CPP_OPTION (pfile, print_deps) == 0
788 || !CPP_OPTION (pfile, no_output)))
790 cpp_fatal (pfile, "-MG must be specified with one of -M or -MM");
791 return 0;
794 /* -Wtraditional is not useful in C++ mode. */
795 if (CPP_OPTION (pfile, cplusplus))
796 CPP_OPTION (pfile, warn_traditional) = 0;
798 /* Do not warn about illegal token pasting if -lang-asm. */
799 if (CPP_OPTION (pfile, lang_asm))
800 CPP_OPTION (pfile, warn_paste) = 0;
802 /* Set this if it hasn't been set already. */
803 if (user_label_prefix == NULL)
804 user_label_prefix = USER_LABEL_PREFIX;
806 /* Figure out if we need to save function macro parameter spellings.
807 We don't use CPP_PEDANTIC() here because that depends on whether
808 or not the current file is a system header, and there is no
809 current file yet. */
810 pfile->save_parameter_spellings =
811 CPP_OPTION (pfile, pedantic)
812 || CPP_OPTION (pfile, debug_output)
813 || CPP_OPTION (pfile, dump_macros) == dump_definitions
814 || CPP_OPTION (pfile, dump_macros) == dump_only;
816 /* Set up the IStable. This doesn't do anything if we were compiled
817 with a compiler that supports C99 designated initializers. */
818 init_IStable ();
820 /* Set up the tables used by read_and_prescan. */
821 _cpp_init_input_buffer (pfile);
823 /* Set up the include search path now. */
824 if (! CPP_OPTION (pfile, no_standard_includes))
825 initialize_standard_includes (pfile);
827 merge_include_chains (pfile);
829 /* With -v, print the list of dirs to search. */
830 if (CPP_OPTION (pfile, verbose))
832 struct file_name_list *l;
833 fprintf (stderr, _("#include \"...\" search starts here:\n"));
834 for (l = CPP_OPTION (pfile, quote_include); l; l = l->next)
836 if (l == CPP_OPTION (pfile, bracket_include))
837 fprintf (stderr, _("#include <...> search starts here:\n"));
838 fprintf (stderr, " %s\n", l->name);
840 fprintf (stderr, _("End of search list.\n"));
843 /* Open the main input file. This must be done early, so we have a
844 buffer to stand on. */
845 if (CPP_OPTION (pfile, in_fname) == NULL
846 || *CPP_OPTION (pfile, in_fname) == 0)
848 CPP_OPTION (pfile, in_fname) = fname;
849 if (CPP_OPTION (pfile, in_fname) == NULL)
850 CPP_OPTION (pfile, in_fname) = "";
852 if (CPP_OPTION (pfile, out_fname) == NULL)
853 CPP_OPTION (pfile, out_fname) = "";
855 if (!cpp_read_file (pfile, fname))
856 return 0;
858 initialize_dependency_output (pfile);
860 /* -D and friends may produce output, which should be identified
861 as line 0. */
863 CPP_BUFFER (pfile)->lineno = 0;
864 if (print)
866 print->last_fname = CPP_BUFFER (pfile)->nominal_fname;
867 print->last_id = pfile->include_depth;
868 print->written = CPP_WRITTEN (pfile);
869 print->lineno = 0;
872 /* Install __LINE__, etc. */
873 initialize_builtins (pfile);
875 /* Do -U's, -D's and -A's in the order they were seen. */
876 p = CPP_OPTION (pfile, pending)->directive_head;
877 while (p)
879 (*p->handler) (pfile, p->arg);
880 q = p->next;
881 free (p);
882 p = q;
884 pfile->done_initializing = 1;
886 /* Now flush any output recorded during initialization, and advance
887 to line 1 of the main input file. */
888 CPP_BUFFER (pfile)->lineno = 1;
890 if (print && ! CPP_OPTION (pfile, no_output))
891 cpp_output_tokens (pfile, print, 1);
893 /* The -imacros files can be scanned now, but the -include files
894 have to be pushed onto the include stack and processed later,
895 in the main loop calling cpp_get_token. */
897 p = CPP_OPTION (pfile, pending)->imacros_head;
898 while (p)
900 if (cpp_read_file (pfile, p->arg))
901 cpp_scan_buffer_nooutput (pfile);
902 q = p->next;
903 free (p);
904 p = q;
907 p = CPP_OPTION (pfile, pending)->include_head;
908 while (p)
910 if (cpp_read_file (pfile, p->arg)
911 && print && ! CPP_OPTION (pfile, no_output))
912 cpp_output_tokens (pfile, print, 1); /* record entry to file */
913 q = p->next;
914 free (p);
915 p = q;
918 free (CPP_OPTION (pfile, pending));
919 CPP_OPTION (pfile, pending) = NULL;
921 return 1;
925 /* Dump out the hash table. */
926 static int
927 dump_macros_helper (pfile, hp)
928 cpp_reader *pfile;
929 cpp_hashnode *hp;
931 if (hp->type == T_MACRO)
932 _cpp_dump_definition (pfile, hp);
933 return 1;
936 /* This is called at the end of preprocessing. It pops the
937 last buffer and writes dependency output. It should also
938 clear macro definitions, such that you could call cpp_start_read
939 with a new filename to restart processing. */
940 void
941 cpp_finish (pfile, print)
942 cpp_reader *pfile;
943 cpp_printer *print;
945 if (CPP_BUFFER (pfile))
947 cpp_ice (pfile, "buffers still stacked in cpp_finish");
948 while (CPP_BUFFER (pfile))
949 cpp_pop_buffer (pfile);
952 /* Don't write the deps file if preprocessing has failed. */
953 if (CPP_OPTION (pfile, print_deps) && pfile->errors == 0)
955 /* Stream on which to print the dependency information. */
956 FILE *deps_stream = 0;
957 const char *deps_mode
958 = CPP_OPTION (pfile, print_deps_append) ? "a" : "w";
959 if (CPP_OPTION (pfile, deps_file) == 0)
960 deps_stream = stdout;
961 else
963 deps_stream = fopen (CPP_OPTION (pfile, deps_file), deps_mode);
964 if (deps_stream == 0)
965 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, deps_file));
967 if (deps_stream)
969 deps_write (pfile->deps, deps_stream, 72);
970 if (CPP_OPTION (pfile, deps_file))
972 if (ferror (deps_stream) || fclose (deps_stream) != 0)
973 cpp_fatal (pfile, "I/O error on output");
978 if (CPP_OPTION (pfile, dump_macros) == dump_only)
979 cpp_forall_identifiers (pfile, dump_macros_helper);
981 /* Flush any pending output. */
982 if (print)
984 cpp_output_tokens (pfile, print, print->lineno);
985 if (ferror (print->outf) || fclose (print->outf))
986 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));
989 /* Report on headers that could use multiple include guards. */
990 if (CPP_OPTION (pfile, print_include_names))
991 _cpp_report_missing_guards (pfile);
994 static void
995 new_pending_directive (pend, text, handler)
996 struct cpp_pending *pend;
997 const char *text;
998 cl_directive_handler handler;
1000 struct pending_option *o = (struct pending_option *)
1001 xmalloc (sizeof (struct pending_option));
1003 o->arg = text;
1004 o->next = NULL;
1005 o->handler = handler;
1006 APPEND (pend, directive, o);
1009 /* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
1010 I.e. a const string initializer with parens around it. That is
1011 what N_("string") resolves to, so we make no_* be macros instead. */
1012 #define no_arg N_("Argument missing after %s")
1013 #define no_ass N_("Assertion missing after %s")
1014 #define no_dir N_("Directory name missing after %s")
1015 #define no_fil N_("File name missing after %s")
1016 #define no_mac N_("Macro name missing after %s")
1017 #define no_pth N_("Path name missing after %s")
1018 #define no_num N_("Number missing after %s")
1020 /* This is the list of all command line options, with the leading
1021 "-" removed. It must be sorted in ASCII collating order. */
1022 #define COMMAND_LINE_OPTIONS \
1023 DEF_OPT("", 0, OPT_stdin_stdout) \
1024 DEF_OPT("$", 0, OPT_dollar) \
1025 DEF_OPT("+", 0, OPT_plus) \
1026 DEF_OPT("-help", 0, OPT__help) \
1027 DEF_OPT("-version", 0, OPT__version) \
1028 DEF_OPT("A", no_ass, OPT_A) \
1029 DEF_OPT("C", 0, OPT_C) \
1030 DEF_OPT("D", no_mac, OPT_D) \
1031 DEF_OPT("H", 0, OPT_H) \
1032 DEF_OPT("I", no_dir, OPT_I) \
1033 DEF_OPT("M", 0, OPT_M) \
1034 DEF_OPT("MD", no_fil, OPT_MD) \
1035 DEF_OPT("MG", 0, OPT_MG) \
1036 DEF_OPT("MM", 0, OPT_MM) \
1037 DEF_OPT("MMD", no_fil, OPT_MMD) \
1038 DEF_OPT("P", 0, OPT_P) \
1039 DEF_OPT("U", no_mac, OPT_U) \
1040 DEF_OPT("W", no_arg, OPT_W) /* arg optional */ \
1041 DEF_OPT("d", no_arg, OPT_d) \
1042 DEF_OPT("fleading-underscore", 0, OPT_fleading_underscore) \
1043 DEF_OPT("fno-leading-underscore", 0, OPT_fno_leading_underscore) \
1044 DEF_OPT("fno-preprocessed", 0, OPT_fno_preprocessed) \
1045 DEF_OPT("fno-show-column", 0, OPT_fno_show_column) \
1046 DEF_OPT("fpreprocessed", 0, OPT_fpreprocessed) \
1047 DEF_OPT("fshow-column", 0, OPT_fshow_column) \
1048 DEF_OPT("ftabstop=", no_num, OPT_ftabstop) \
1049 DEF_OPT("g", no_arg, OPT_g) /* arg optional */ \
1050 DEF_OPT("h", 0, OPT_h) \
1051 DEF_OPT("idirafter", no_dir, OPT_idirafter) \
1052 DEF_OPT("imacros", no_fil, OPT_imacros) \
1053 DEF_OPT("include", no_fil, OPT_include) \
1054 DEF_OPT("iprefix", no_pth, OPT_iprefix) \
1055 DEF_OPT("isystem", no_dir, OPT_isystem) \
1056 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix) \
1057 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore) \
1058 DEF_OPT("lang-asm", 0, OPT_lang_asm) \
1059 DEF_OPT("lang-c", 0, OPT_lang_c) \
1060 DEF_OPT("lang-c++", 0, OPT_lang_cplusplus) \
1061 DEF_OPT("lang-c89", 0, OPT_lang_c89) \
1062 DEF_OPT("lang-objc", 0, OPT_lang_objc) \
1063 DEF_OPT("lang-objc++", 0, OPT_lang_objcplusplus) \
1064 DEF_OPT("nostdinc", 0, OPT_nostdinc) \
1065 DEF_OPT("nostdinc++", 0, OPT_nostdincplusplus) \
1066 DEF_OPT("o", no_fil, OPT_o) \
1067 DEF_OPT("pedantic", 0, OPT_pedantic) \
1068 DEF_OPT("pedantic-errors", 0, OPT_pedantic_errors) \
1069 DEF_OPT("remap", 0, OPT_remap) \
1070 DEF_OPT("std=c89", 0, OPT_std_c89) \
1071 DEF_OPT("std=c99", 0, OPT_std_c99) \
1072 DEF_OPT("std=c9x", 0, OPT_std_c9x) \
1073 DEF_OPT("std=gnu89", 0, OPT_std_gnu89) \
1074 DEF_OPT("std=gnu99", 0, OPT_std_gnu99) \
1075 DEF_OPT("std=gnu9x", 0, OPT_std_gnu9x) \
1076 DEF_OPT("std=iso9899:1990", 0, OPT_std_iso9899_1990) \
1077 DEF_OPT("std=iso9899:199409", 0, OPT_std_iso9899_199409) \
1078 DEF_OPT("std=iso9899:1999", 0, OPT_std_iso9899_1999) \
1079 DEF_OPT("std=iso9899:199x", 0, OPT_std_iso9899_199x) \
1080 DEF_OPT("trigraphs", 0, OPT_trigraphs) \
1081 DEF_OPT("v", 0, OPT_v) \
1082 DEF_OPT("w", 0, OPT_w)
1084 #define DEF_OPT(text, msg, code) code,
1085 enum opt_code
1087 COMMAND_LINE_OPTIONS
1088 N_OPTS
1090 #undef DEF_OPT
1092 struct cl_option
1094 const char *opt_text;
1095 const char *msg;
1096 size_t opt_len;
1097 enum opt_code opt_code;
1100 #define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
1101 #ifdef HOST_EBCDIC
1102 static struct cl_option cl_options[] =
1103 #else
1104 static const struct cl_option cl_options[] =
1105 #endif
1107 COMMAND_LINE_OPTIONS
1109 #undef DEF_OPT
1110 #undef COMMAND_LINE_OPTIONS
1112 #ifdef HOST_EBCDIC
1113 static void
1114 sort_options (void)
1116 static int opts_sorted = 0;
1118 if (!opts_sorted)
1120 opts_sorted = 1;
1121 /* For non-ASCII hosts, the array needs to be sorted at runtime */
1122 qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
1125 #endif
1128 /* Perform a binary search to find which, if any, option the given
1129 command-line matches. Returns its index in the option array,
1130 negative on failure. Complications arise since some options can be
1131 suffixed with an argument, and multiple complete matches can occur,
1132 e.g. -iwithprefix and -iwithprefixbefore. Moreover, we want to
1133 accept options beginning with -g and -W that we do not recognise,
1134 but not to swallow any subsequent command line argument; these are
1135 handled as special cases in cpp_handle_option */
1136 static int
1137 parse_option (input)
1138 const char *input;
1140 unsigned int md, mn, mx;
1141 size_t opt_len;
1142 int comp;
1144 mn = 0;
1145 mx = N_OPTS;
1147 while (mx > mn)
1149 md = (mn + mx) / 2;
1151 opt_len = cl_options[md].opt_len;
1152 comp = strncmp (input, cl_options[md].opt_text, opt_len);
1154 if (comp > 0)
1155 mn = md + 1;
1156 else if (comp < 0)
1157 mx = md;
1158 else
1160 if (input[opt_len] == '\0')
1161 return md;
1162 /* We were passed more text. If the option takes an argument,
1163 we may match a later option or we may have been passed the
1164 argument. The longest possible option match succeeds.
1165 If the option takes no arguments we have not matched and
1166 continue the search (e.g. input="stdc++" match was "stdc") */
1167 mn = md + 1;
1168 if (cl_options[md].msg)
1170 /* Scan forwards. If we get an exact match, return it.
1171 Otherwise, return the longest option-accepting match.
1172 This loops no more than twice with current options */
1173 mx = md;
1174 for (; mn < N_OPTS; mn++)
1176 opt_len = cl_options[mn].opt_len;
1177 if (strncmp (input, cl_options[mn].opt_text, opt_len))
1178 break;
1179 if (input[opt_len] == '\0')
1180 return mn;
1181 if (cl_options[mn].msg)
1182 mx = mn;
1184 return mx;
1189 return -1;
1192 /* Handle one command-line option in (argc, argv).
1193 Can be called multiple times, to handle multiple sets of options.
1194 Returns number of strings consumed. */
1197 cpp_handle_option (pfile, argc, argv)
1198 cpp_reader *pfile;
1199 int argc;
1200 char **argv;
1202 int i = 0;
1203 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
1205 if (argv[i][0] != '-')
1207 if (CPP_OPTION (pfile, out_fname) != NULL)
1208 cpp_fatal (pfile, "Too many arguments. Type %s --help for usage info",
1209 progname);
1210 else if (CPP_OPTION (pfile, in_fname) != NULL)
1211 CPP_OPTION (pfile, out_fname) = argv[i];
1212 else
1213 CPP_OPTION (pfile, in_fname) = argv[i];
1215 else
1217 enum opt_code opt_code;
1218 int opt_index;
1219 const char *arg = 0;
1221 /* Skip over '-' */
1222 opt_index = parse_option (&argv[i][1]);
1223 if (opt_index < 0)
1224 return i;
1226 opt_code = cl_options[opt_index].opt_code;
1227 if (cl_options[opt_index].msg)
1229 arg = &argv[i][cl_options[opt_index].opt_len + 1];
1231 /* Yuk. Special case for -g and -W as they must not swallow
1232 up any following argument. If this becomes common, add
1233 another field to the cl_options table */
1234 if (arg[0] == '\0' && !(opt_code == OPT_g || opt_code == OPT_W))
1236 arg = argv[++i];
1237 if (!arg)
1239 cpp_fatal (pfile, cl_options[opt_index].msg, argv[i - 1]);
1240 return argc;
1245 switch (opt_code)
1247 case N_OPTS: /* shut GCC up */
1248 break;
1249 case OPT_fleading_underscore:
1250 user_label_prefix = "_";
1251 break;
1252 case OPT_fno_leading_underscore:
1253 user_label_prefix = "";
1254 break;
1255 case OPT_fpreprocessed:
1256 CPP_OPTION (pfile, preprocessed) = 1;
1257 break;
1258 case OPT_fno_preprocessed:
1259 CPP_OPTION (pfile, preprocessed) = 0;
1260 break;
1261 case OPT_fshow_column:
1262 CPP_OPTION (pfile, show_column) = 1;
1263 break;
1264 case OPT_fno_show_column:
1265 CPP_OPTION (pfile, show_column) = 0;
1266 break;
1267 case OPT_ftabstop:
1268 /* Silently ignore empty string, non-longs and silly values. */
1269 if (arg[0] != '\0')
1271 char *endptr;
1272 long tabstop = strtol (arg, &endptr, 10);
1273 if (*endptr == '\0' && tabstop >= 1 && tabstop <= 100)
1274 CPP_OPTION (pfile, tabstop) = tabstop;
1276 break;
1277 case OPT_w:
1278 CPP_OPTION (pfile, inhibit_warnings) = 1;
1279 break;
1280 case OPT_g: /* Silently ignore anything but -g3 */
1281 if (!strcmp(&argv[i][2], "3"))
1282 CPP_OPTION (pfile, debug_output) = 1;
1283 break;
1284 case OPT_h:
1285 case OPT__help:
1286 print_help ();
1287 exit (0); /* XXX */
1288 break;
1289 case OPT__version:
1290 fprintf (stderr, _("GNU CPP version %s (cpplib)\n"), version_string);
1291 exit (0); /* XXX */
1292 break;
1293 case OPT_C:
1294 CPP_OPTION (pfile, discard_comments) = 0;
1295 break;
1296 case OPT_P:
1297 CPP_OPTION (pfile, no_line_commands) = 1;
1298 break;
1299 case OPT_dollar: /* Don't include $ in identifiers. */
1300 CPP_OPTION (pfile, dollars_in_ident) = 0;
1301 break;
1302 case OPT_H:
1303 CPP_OPTION (pfile, print_include_names) = 1;
1304 break;
1305 case OPT_D:
1306 new_pending_directive (pend, arg, cpp_define);
1307 break;
1308 case OPT_pedantic_errors:
1309 CPP_OPTION (pfile, pedantic_errors) = 1;
1310 /* fall through */
1311 case OPT_pedantic:
1312 CPP_OPTION (pfile, pedantic) = 1;
1313 break;
1314 case OPT_trigraphs:
1315 CPP_OPTION (pfile, trigraphs) = 1;
1316 break;
1317 case OPT_plus:
1318 CPP_OPTION (pfile, cplusplus) = 1;
1319 CPP_OPTION (pfile, cplusplus_comments) = 1;
1320 break;
1321 case OPT_remap:
1322 CPP_OPTION (pfile, remap) = 1;
1323 break;
1324 case OPT_iprefix:
1325 CPP_OPTION (pfile, include_prefix) = arg;
1326 CPP_OPTION (pfile, include_prefix_len) = strlen (arg);
1327 break;
1328 case OPT_lang_c:
1329 CPP_OPTION (pfile, cplusplus) = 0;
1330 CPP_OPTION (pfile, cplusplus_comments) = 1;
1331 CPP_OPTION (pfile, c89) = 0;
1332 CPP_OPTION (pfile, c99) = 1;
1333 CPP_OPTION (pfile, digraphs) = 1;
1334 CPP_OPTION (pfile, objc) = 0;
1335 break;
1336 case OPT_lang_cplusplus:
1337 CPP_OPTION (pfile, cplusplus) = 1;
1338 CPP_OPTION (pfile, cplusplus_comments) = 1;
1339 CPP_OPTION (pfile, c89) = 0;
1340 CPP_OPTION (pfile, c99) = 0;
1341 CPP_OPTION (pfile, objc) = 0;
1342 CPP_OPTION (pfile, digraphs) = 1;
1343 new_pending_directive (pend, "__cplusplus", cpp_define);
1344 break;
1345 case OPT_lang_objcplusplus:
1346 CPP_OPTION (pfile, cplusplus) = 1;
1347 new_pending_directive (pend, "__cplusplus", cpp_define);
1348 /* fall through */
1349 case OPT_lang_objc:
1350 CPP_OPTION (pfile, cplusplus_comments) = 1;
1351 CPP_OPTION (pfile, c89) = 0;
1352 CPP_OPTION (pfile, c99) = 0;
1353 CPP_OPTION (pfile, objc) = 1;
1354 new_pending_directive (pend, "__OBJC__", cpp_define);
1355 break;
1356 case OPT_lang_asm:
1357 CPP_OPTION (pfile, lang_asm) = 1;
1358 CPP_OPTION (pfile, dollars_in_ident) = 0;
1359 new_pending_directive (pend, "__ASSEMBLER__", cpp_define);
1360 break;
1361 case OPT_nostdinc:
1362 /* -nostdinc causes no default include directories.
1363 You must specify all include-file directories with -I. */
1364 CPP_OPTION (pfile, no_standard_includes) = 1;
1365 break;
1366 case OPT_nostdincplusplus:
1367 /* -nostdinc++ causes no default C++-specific include directories. */
1368 CPP_OPTION (pfile, no_standard_cplusplus_includes) = 1;
1369 break;
1370 case OPT_std_gnu89:
1371 CPP_OPTION (pfile, cplusplus) = 0;
1372 CPP_OPTION (pfile, cplusplus_comments) = 1;
1373 CPP_OPTION (pfile, c89) = 1;
1374 CPP_OPTION (pfile, c99) = 0;
1375 CPP_OPTION (pfile, objc) = 0;
1376 CPP_OPTION (pfile, digraphs) = 1;
1377 break;
1378 case OPT_std_gnu9x:
1379 case OPT_std_gnu99:
1380 CPP_OPTION (pfile, cplusplus) = 0;
1381 CPP_OPTION (pfile, cplusplus_comments) = 1;
1382 CPP_OPTION (pfile, c89) = 0;
1383 CPP_OPTION (pfile, c99) = 1;
1384 CPP_OPTION (pfile, digraphs) = 1;
1385 CPP_OPTION (pfile, objc) = 0;
1386 new_pending_directive (CPP_OPTION (pfile, pending),
1387 "__STDC_VERSION__=199901L", cpp_define);
1388 break;
1389 case OPT_std_iso9899_199409:
1390 new_pending_directive (CPP_OPTION (pfile, pending),
1391 "__STDC_VERSION__=199409L", cpp_define);
1392 /* Fall through */
1393 case OPT_std_iso9899_1990:
1394 case OPT_std_c89:
1395 case OPT_lang_c89:
1396 CPP_OPTION (pfile, cplusplus) = 0;
1397 CPP_OPTION (pfile, cplusplus_comments) = 0;
1398 CPP_OPTION (pfile, c89) = 1;
1399 CPP_OPTION (pfile, c99) = 0;
1400 CPP_OPTION (pfile, objc) = 0;
1401 CPP_OPTION (pfile, digraphs) = opt_code == OPT_std_iso9899_199409;
1402 CPP_OPTION (pfile, trigraphs) = 1;
1403 new_pending_directive (pend, "__STRICT_ANSI__", cpp_define);
1404 break;
1405 case OPT_std_iso9899_199x:
1406 case OPT_std_iso9899_1999:
1407 case OPT_std_c9x:
1408 case OPT_std_c99:
1409 CPP_OPTION (pfile, cplusplus) = 0;
1410 CPP_OPTION (pfile, cplusplus_comments) = 1;
1411 CPP_OPTION (pfile, c89) = 0;
1412 CPP_OPTION (pfile, c99) = 1;
1413 CPP_OPTION (pfile, objc) = 0;
1414 CPP_OPTION (pfile, digraphs) = 1;
1415 CPP_OPTION (pfile, trigraphs) = 1;
1416 new_pending_directive (CPP_OPTION (pfile, pending),
1417 "__STRICT_ANSI__", cpp_define);
1418 new_pending_directive (CPP_OPTION (pfile, pending),
1419 "__STDC_VERSION__=199901L", cpp_define);
1420 break;
1421 case OPT_o:
1422 if (CPP_OPTION (pfile, out_fname) != NULL)
1424 cpp_fatal (pfile, "Output filename specified twice");
1425 return argc;
1427 CPP_OPTION (pfile, out_fname) = arg;
1428 if (!strcmp (CPP_OPTION (pfile, out_fname), "-"))
1429 CPP_OPTION (pfile, out_fname) = "";
1430 break;
1431 case OPT_v:
1432 fprintf (stderr, _("GNU CPP version %s (cpplib)\n"), version_string);
1433 #ifdef TARGET_VERSION
1434 TARGET_VERSION;
1435 #endif
1436 fputc ('\n', stderr);
1437 CPP_OPTION (pfile, verbose) = 1;
1438 break;
1439 case OPT_stdin_stdout:
1440 /* JF handle '-' as file name meaning stdin or stdout */
1441 if (CPP_OPTION (pfile, in_fname) == NULL)
1442 CPP_OPTION (pfile, in_fname) = "";
1443 else if (CPP_OPTION (pfile, out_fname) == NULL)
1444 CPP_OPTION (pfile, out_fname) = "";
1445 break;
1446 case OPT_d:
1447 /* Args to -d specify what parts of macros to dump.
1448 Silently ignore unrecognised options; they may
1449 be aimed at the compiler proper. */
1451 char c;
1453 while ((c = *arg++) != '\0')
1454 switch (c)
1456 case 'M':
1457 CPP_OPTION (pfile, dump_macros) = dump_only;
1458 CPP_OPTION (pfile, no_output) = 1;
1459 break;
1460 case 'N':
1461 CPP_OPTION (pfile, dump_macros) = dump_names;
1462 break;
1463 case 'D':
1464 CPP_OPTION (pfile, dump_macros) = dump_definitions;
1465 break;
1466 case 'I':
1467 CPP_OPTION (pfile, dump_includes) = 1;
1468 break;
1471 break;
1472 /* The style of the choices here is a bit mixed.
1473 The chosen scheme is a hybrid of keeping all options in one string
1474 and specifying each option in a separate argument:
1475 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
1476 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1477 -M[M][G][D file]. This is awkward to handle in specs, and is not
1478 as extensible. */
1479 /* ??? -MG must be specified in addition to one of -M or -MM.
1480 This can be relaxed in the future without breaking anything.
1481 The converse isn't true. */
1483 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
1484 case OPT_MG:
1485 CPP_OPTION (pfile, print_deps_missing_files) = 1;
1486 break;
1487 case OPT_M:
1488 case OPT_MD:
1489 case OPT_MM:
1490 case OPT_MMD:
1491 if (opt_code == OPT_M || opt_code == OPT_MD)
1492 CPP_OPTION (pfile, print_deps) = 2;
1493 else
1494 CPP_OPTION (pfile, print_deps) = 1;
1496 /* For -MD and -MMD options, write deps on file named by next arg */
1497 /* For -M and -MM, write deps on standard output
1498 and suppress the usual output. */
1499 if (opt_code == OPT_MD || opt_code == OPT_MMD)
1500 CPP_OPTION (pfile, deps_file) = arg;
1501 else
1502 CPP_OPTION (pfile, no_output) = 1;
1503 break;
1504 case OPT_A:
1505 if (arg[0] == '-')
1507 /* -A with an argument beginning with '-' acts as
1508 #unassert on whatever immediately follows the '-'.
1509 If "-" is the whole argument, we eliminate all
1510 predefined macros and assertions, including those
1511 that were specified earlier on the command line.
1512 That way we can get rid of any that were passed
1513 automatically in from GCC. */
1515 if (arg[1] == '\0')
1517 struct pending_option *o1, *o2;
1519 o1 = CPP_OPTION (pfile, pending)->directive_head;
1520 while (o1)
1522 o2 = o1->next;
1523 free (o1);
1524 o1 = o2;
1526 CPP_OPTION (pfile, pending)->directive_head = NULL;
1527 CPP_OPTION (pfile, pending)->directive_tail = NULL;
1529 else
1530 new_pending_directive (CPP_OPTION (pfile, pending),
1531 arg + 1, cpp_unassert);
1533 else
1534 new_pending_directive (CPP_OPTION (pfile, pending),
1535 arg, cpp_assert);
1536 break;
1537 case OPT_U:
1538 new_pending_directive (CPP_OPTION (pfile, pending), arg, cpp_undef);
1539 break;
1540 case OPT_I: /* Add directory to path for includes. */
1541 if (!strcmp (arg, "-"))
1543 /* -I- means:
1544 Use the preceding -I directories for #include "..."
1545 but not #include <...>.
1546 Don't search the directory of the present file
1547 for #include "...". (Note that -I. -I- is not the same as
1548 the default setup; -I. uses the compiler's working dir.) */
1549 if (! CPP_OPTION (pfile, ignore_srcdir))
1551 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
1552 pend->quote_head = pend->brack_head;
1553 pend->quote_tail = pend->brack_tail;
1554 pend->brack_head = 0;
1555 pend->brack_tail = 0;
1556 CPP_OPTION (pfile, ignore_srcdir) = 1;
1558 else
1560 cpp_fatal (pfile, "-I- specified twice");
1561 return argc;
1564 else
1565 append_include_chain (pfile, CPP_OPTION (pfile, pending),
1566 xstrdup (arg), BRACKET, 0);
1567 break;
1568 case OPT_isystem:
1569 /* Add directory to beginning of system include path, as a system
1570 include directory. */
1571 append_include_chain (pfile, CPP_OPTION (pfile, pending),
1572 xstrdup (arg), SYSTEM, 0);
1573 break;
1574 case OPT_include:
1576 struct pending_option *o = (struct pending_option *)
1577 xmalloc (sizeof (struct pending_option));
1578 o->arg = arg;
1580 /* This list has to be built in reverse order so that
1581 when cpp_start_read pushes all the -include files onto
1582 the buffer stack, they will be scanned in forward order. */
1583 o->next = CPP_OPTION (pfile, pending)->include_head;
1584 CPP_OPTION (pfile, pending)->include_head = o;
1586 break;
1587 case OPT_imacros:
1589 struct pending_option *o = (struct pending_option *)
1590 xmalloc (sizeof (struct pending_option));
1591 o->arg = arg;
1592 o->next = NULL;
1594 APPEND (CPP_OPTION (pfile, pending), imacros, o);
1596 break;
1597 case OPT_iwithprefix:
1598 /* Add directory to end of path for includes,
1599 with the default prefix at the front of its name. */
1600 /* fall through */
1601 case OPT_iwithprefixbefore:
1602 /* Add directory to main path for includes,
1603 with the default prefix at the front of its name. */
1605 char *fname;
1606 int len;
1608 len = strlen (arg);
1610 if (CPP_OPTION (pfile, include_prefix) != 0)
1612 size_t ipl = CPP_OPTION (pfile, include_prefix_len);
1613 fname = xmalloc (ipl + len + 1);
1614 memcpy (fname, CPP_OPTION (pfile, include_prefix), ipl);
1615 memcpy (fname + ipl, arg, len + 1);
1617 else if (cpp_GCC_INCLUDE_DIR_len)
1619 fname = xmalloc (cpp_GCC_INCLUDE_DIR_len + len + 1);
1620 memcpy (fname, cpp_GCC_INCLUDE_DIR, cpp_GCC_INCLUDE_DIR_len);
1621 memcpy (fname + cpp_GCC_INCLUDE_DIR_len, arg, len + 1);
1623 else
1624 fname = xstrdup (arg);
1626 append_include_chain (pfile, CPP_OPTION (pfile, pending), fname,
1627 opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0);
1629 break;
1630 case OPT_idirafter:
1631 /* Add directory to end of path for includes. */
1632 append_include_chain (pfile, CPP_OPTION (pfile, pending),
1633 xstrdup (arg), AFTER, 0);
1634 break;
1635 case OPT_W:
1636 /* Silently ignore unrecognised options */
1637 if (!strcmp (argv[i], "-Wall"))
1639 CPP_OPTION (pfile, warn_trigraphs) = 1;
1640 CPP_OPTION (pfile, warn_comments) = 1;
1642 else if (!strcmp (argv[i], "-Wtraditional"))
1643 CPP_OPTION (pfile, warn_traditional) = 1;
1644 else if (!strcmp (argv[i], "-Wtrigraphs"))
1645 CPP_OPTION (pfile, warn_trigraphs) = 1;
1646 else if (!strcmp (argv[i], "-Wcomment"))
1647 CPP_OPTION (pfile, warn_comments) = 1;
1648 else if (!strcmp (argv[i], "-Wcomments"))
1649 CPP_OPTION (pfile, warn_comments) = 1;
1650 else if (!strcmp (argv[i], "-Wundef"))
1651 CPP_OPTION (pfile, warn_undef) = 1;
1652 else if (!strcmp (argv[i], "-Wimport"))
1653 CPP_OPTION (pfile, warn_import) = 1;
1654 else if (!strcmp (argv[i], "-Wpaste"))
1655 CPP_OPTION (pfile, warn_paste) = 1;
1656 else if (!strcmp (argv[i], "-Werror"))
1657 CPP_OPTION (pfile, warnings_are_errors) = 1;
1658 else if (!strcmp (argv[i], "-Wno-traditional"))
1659 CPP_OPTION (pfile, warn_traditional) = 0;
1660 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1661 CPP_OPTION (pfile, warn_trigraphs) = 0;
1662 else if (!strcmp (argv[i], "-Wno-comment"))
1663 CPP_OPTION (pfile, warn_comments) = 0;
1664 else if (!strcmp (argv[i], "-Wno-comments"))
1665 CPP_OPTION (pfile, warn_comments) = 0;
1666 else if (!strcmp (argv[i], "-Wno-undef"))
1667 CPP_OPTION (pfile, warn_undef) = 0;
1668 else if (!strcmp (argv[i], "-Wno-import"))
1669 CPP_OPTION (pfile, warn_import) = 0;
1670 else if (!strcmp (argv[i], "-Wno-paste"))
1671 CPP_OPTION (pfile, warn_paste) = 0;
1672 else if (!strcmp (argv[i], "-Wno-error"))
1673 CPP_OPTION (pfile, warnings_are_errors) = 0;
1674 break;
1677 return i + 1;
1680 #ifdef HOST_EBCDIC
1681 static int
1682 opt_comp (const void *p1, const void *p2)
1684 return strcmp (((struct cl_option *)p1)->opt_text,
1685 ((struct cl_option *)p2)->opt_text);
1687 #endif
1689 /* Handle command-line options in (argc, argv).
1690 Can be called multiple times, to handle multiple sets of options.
1691 Returns if an unrecognized option is seen.
1692 Returns number of strings consumed. */
1694 cpp_handle_options (pfile, argc, argv)
1695 cpp_reader *pfile;
1696 int argc;
1697 char **argv;
1699 int i;
1700 int strings_processed;
1702 for (i = 0; i < argc; i += strings_processed)
1704 strings_processed = cpp_handle_option (pfile, argc - i, argv + i);
1705 if (strings_processed == 0)
1706 break;
1708 return i;
1711 static void
1712 print_help ()
1714 fprintf (stderr, _("Usage: %s [switches] input output\n"), progname);
1715 /* To keep the lines from getting too long for some compilers, limit
1716 to about 500 characters (6 lines) per chunk. */
1717 fputs (_("\
1718 Switches:\n\
1719 -include <file> Include the contents of <file> before other files\n\
1720 -imacros <file> Accept definition of macros in <file>\n\
1721 -iprefix <path> Specify <path> as a prefix for next two options\n\
1722 -iwithprefix <dir> Add <dir> to the end of the system include path\n\
1723 -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\
1724 -isystem <dir> Add <dir> to the start of the system include path\n\
1725 "), stdout);
1726 fputs (_("\
1727 -idirafter <dir> Add <dir> to the end of the system include path\n\
1728 -I <dir> Add <dir> to the end of the main include path\n\
1729 -I- Fine-grained include path control; see info docs\n\
1730 -nostdinc Do not search system include directories\n\
1731 (dirs specified with -isystem will still be used)\n\
1732 -nostdinc++ Do not search system include directories for C++\n\
1733 -o <file> Put output into <file>\n\
1734 "), stdout);
1735 fputs (_("\
1736 -pedantic Issue all warnings demanded by strict ISO C\n\
1737 -pedantic-errors Issue -pedantic warnings as errors instead\n\
1738 -trigraphs Support ISO C trigraphs\n\
1739 -lang-c Assume that the input sources are in C\n\
1740 -lang-c89 Assume that the input sources are in C89\n\
1741 "), stdout);
1742 fputs (_("\
1743 -lang-c++ Assume that the input sources are in C++\n\
1744 -lang-objc Assume that the input sources are in ObjectiveC\n\
1745 -lang-objc++ Assume that the input sources are in ObjectiveC++\n\
1746 -lang-asm Assume that the input sources are in assembler\n\
1747 "), stdout);
1748 fputs (_("\
1749 -std=<std name> Specify the conformance standard; one of:\n\
1750 gnu89, gnu99, c89, c99, iso9899:1990,\n\
1751 iso9899:199409, iso9899:1999\n\
1752 -+ Allow parsing of C++ style features\n\
1753 -w Inhibit warning messages\n\
1754 -Wtrigraphs Warn if trigraphs are encountered\n\
1755 -Wno-trigraphs Do not warn about trigraphs\n\
1756 -Wcomment{s} Warn if one comment starts inside another\n\
1757 "), stdout);
1758 fputs (_("\
1759 -Wno-comment{s} Do not warn about comments\n\
1760 -Wtraditional Warn about features not present in traditional C\n\
1761 -Wno-traditional Do not warn about traditional C\n\
1762 -Wundef Warn if an undefined macro is used by #if\n\
1763 -Wno-undef Do not warn about testing undefined macros\n\
1764 -Wimport Warn about the use of the #import directive\n\
1765 "), stdout);
1766 fputs (_("\
1767 -Wno-import Do not warn about the use of #import\n\
1768 -Werror Treat all warnings as errors\n\
1769 -Wno-error Do not treat warnings as errors\n\
1770 -Wall Enable all preprocessor warnings\n\
1771 -M Generate make dependencies\n\
1772 -MM As -M, but ignore system header files\n\
1773 "), stdout);
1774 fputs (_("\
1775 -MD As -M, but put output in a .d file\n\
1776 -MMD As -MD, but ignore system header files\n\
1777 -MG Treat missing header file as generated files\n\
1778 -g3 Include #define and #undef directives in the output\n\
1779 -D<macro> Define a <macro> with string '1' as its value\n\
1780 -D<macro>=<val> Define a <macro> with <val> as its value\n\
1781 "), stdout);
1782 fputs (_("\
1783 -A<question> (<answer>) Assert the <answer> to <question>\n\
1784 -A-<question> (<answer>) Disable the <answer> to <question>\n\
1785 -U<macro> Undefine <macro> \n\
1786 -v Display the version number\n\
1787 -H Print the name of header files as they are used\n\
1788 -C Do not discard comments\n\
1789 "), stdout);
1790 fputs (_("\
1791 -dM Display a list of macro definitions active at end\n\
1792 -dD Preserve macro definitions in output\n\
1793 -dN As -dD except that only the names are preserved\n\
1794 -dI Include #include directives in the output\n\
1795 -ftabstop=<number> Distance between tab stops for column reporting\n\
1796 -P Do not generate #line directives\n\
1797 -$ Do not allow '$' in identifiers\n\
1798 "), stdout);
1799 fputs (_("\
1800 -remap Remap file names when including files.\n\
1801 --version Display version information\n\
1802 -h or --help Display this information\n\
1803 "), stdout);