Regenerate after this patch:
[official-gcc.git] / gcc / cppinit.c
blobdb8b6e92c76ac7e3d110e02a6923364bbf1a87ab
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 #endif
115 static int parse_option PARAMS ((const char *));
116 static int handle_option PARAMS ((cpp_reader *, int, char **));
118 /* Fourth argument to append_include_chain: chain to use */
119 enum { QUOTE = 0, BRACKET, SYSTEM, AFTER };
121 /* If we have designated initializers (GCC >2.7) this table can be
122 initialized, constant data. Otherwise, it has to be filled in at
123 runtime. */
125 #if (GCC_VERSION >= 2007)
126 #define init_IStable() /* nothing */
127 #define ISTABLE __extension__ const unsigned char _cpp_IStable[256] = {
128 #define END };
129 #define s(p, v) [p] = v,
130 #else
131 #define ISTABLE unsigned char _cpp_IStable[256] = { 0 }; \
132 static void init_IStable PARAMS ((void)) { \
133 unsigned char *x = _cpp_IStable;
134 #define END }
135 #define s(p, v) x[p] = v;
136 #endif
138 #define A(x) s(x, ISidnum|ISidstart)
139 #define N(x) s(x, ISidnum|ISnumstart)
140 #define H(x) s(x, IShspace|ISspace)
141 #define V(x) s(x, ISvspace|ISspace)
142 #define S(x) s(x, ISspace)
144 ISTABLE
145 A('_')
147 A('a') A('b') A('c') A('d') A('e') A('f') A('g') A('h') A('i')
148 A('j') A('k') A('l') A('m') A('n') A('o') A('p') A('q') A('r')
149 A('s') A('t') A('u') A('v') A('w') A('x') A('y') A('z')
151 A('A') A('B') A('C') A('D') A('E') A('F') A('G') A('H') A('I')
152 A('J') A('K') A('L') A('M') A('N') A('O') A('P') A('Q') A('R')
153 A('S') A('T') A('U') A('V') A('W') A('X') A('Y') A('Z')
155 N('1') N('2') N('3') N('4') N('5') N('6') N('7') N('8') N('9') N('0')
157 H(' ') H('\t')
159 V('\n') V('\r')
161 S('\0') S('\v') S('\f')
164 #undef A
165 #undef N
166 #undef H
167 #undef V
168 #undef S
169 #undef s
170 #undef ISTABLE
171 #undef END
173 /* Given a colon-separated list of file names PATH,
174 add all the names to the search path for include files. */
176 static void
177 path_include (pfile, pend, list, path)
178 cpp_reader *pfile;
179 struct cpp_pending *pend;
180 char *list;
181 int path;
183 char *p, *q, *name;
185 p = list;
189 /* Find the end of this name. */
190 q = p;
191 while (*q != 0 && *q != PATH_SEPARATOR) q++;
192 if (q == p)
194 /* An empty name in the path stands for the current directory. */
195 name = (char *) xmalloc (2);
196 name[0] = '.';
197 name[1] = 0;
199 else
201 /* Otherwise use the directory that is named. */
202 name = (char *) xmalloc (q - p + 1);
203 memcpy (name, p, q - p);
204 name[q - p] = 0;
207 append_include_chain (pfile, pend, name, path, 0);
209 /* Advance past this name. */
210 if (*q == 0)
211 break;
212 p = q + 1;
214 while (1);
217 /* Append DIR to include path PATH. DIR must be permanently allocated
218 and writable. */
219 static void
220 append_include_chain (pfile, pend, dir, path, cxx_aware)
221 cpp_reader *pfile;
222 struct cpp_pending *pend;
223 char *dir;
224 int path;
225 int cxx_aware;
227 struct file_name_list *new;
228 struct stat st;
229 unsigned int len;
231 _cpp_simplify_pathname (dir);
232 if (stat (dir, &st))
234 /* Dirs that don't exist are silently ignored. */
235 if (errno != ENOENT)
236 cpp_notice_from_errno (pfile, dir);
237 else if (CPP_OPTION (pfile, verbose))
238 fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"), dir);
239 return;
242 if (!S_ISDIR (st.st_mode))
244 cpp_notice (pfile, "%s: Not a directory", dir);
245 return;
248 len = strlen (dir);
249 if (len > pfile->max_include_len)
250 pfile->max_include_len = len;
252 new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
253 new->name = dir;
254 new->nlen = len;
255 new->ino = st.st_ino;
256 new->dev = st.st_dev;
257 if (path == SYSTEM)
258 new->sysp = cxx_aware ? 1 : 2;
259 else
260 new->sysp = 0;
261 new->name_map = NULL;
262 new->next = NULL;
263 new->alloc = NULL;
265 switch (path)
267 case QUOTE: APPEND (pend, quote, new); break;
268 case BRACKET: APPEND (pend, brack, new); break;
269 case SYSTEM: APPEND (pend, systm, new); break;
270 case AFTER: APPEND (pend, after, new); break;
274 /* Merge the four include chains together in the order quote, bracket,
275 system, after. Remove duplicate dirs (as determined by
276 INO_T_EQ()). The system_include and after_include chains are never
277 referred to again after this function; all access is through the
278 bracket_include path.
280 For the future: Check if the directory is empty (but
281 how?) and possibly preload the include hash. */
283 static void
284 merge_include_chains (pfile)
285 cpp_reader *pfile;
287 struct file_name_list *prev, *cur, *other;
288 struct file_name_list *quote, *brack, *systm, *after;
289 struct file_name_list *qtail, *btail, *stail, *atail;
291 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
293 qtail = pend->quote_tail;
294 btail = pend->brack_tail;
295 stail = pend->systm_tail;
296 atail = pend->after_tail;
298 quote = pend->quote_head;
299 brack = pend->brack_head;
300 systm = pend->systm_head;
301 after = pend->after_head;
303 /* Paste together bracket, system, and after include chains. */
304 if (stail)
305 stail->next = after;
306 else
307 systm = after;
308 if (btail)
309 btail->next = systm;
310 else
311 brack = systm;
313 /* This is a bit tricky.
314 First we drop dupes from the quote-include list.
315 Then we drop dupes from the bracket-include list.
316 Finally, if qtail and brack are the same directory,
317 we cut out qtail.
319 We can't just merge the lists and then uniquify them because
320 then we may lose directories from the <> search path that should
321 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
322 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
323 -Ibar -I- -Ifoo -Iquux.
325 Note that this algorithm is quadratic in the number of -I switches,
326 which is acceptable since there aren't usually that many of them. */
328 for (cur = quote, prev = NULL; cur; cur = cur->next)
330 for (other = quote; other != cur; other = other->next)
331 if (INO_T_EQ (cur->ino, other->ino)
332 && cur->dev == other->dev)
334 if (CPP_OPTION (pfile, verbose))
335 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"),
336 cur->name);
338 prev->next = cur->next;
339 free (cur->name);
340 free (cur);
341 cur = prev;
342 break;
344 prev = cur;
346 qtail = prev;
348 for (cur = brack; cur; cur = cur->next)
350 for (other = brack; other != cur; other = other->next)
351 if (INO_T_EQ (cur->ino, other->ino)
352 && cur->dev == other->dev)
354 if (CPP_OPTION (pfile, verbose))
355 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"),
356 cur->name);
358 prev->next = cur->next;
359 free (cur->name);
360 free (cur);
361 cur = prev;
362 break;
364 prev = cur;
367 if (quote)
369 if (INO_T_EQ (qtail->ino, brack->ino) && qtail->dev == brack->dev)
371 if (quote == qtail)
373 if (CPP_OPTION (pfile, verbose))
374 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"),
375 quote->name);
377 free (quote->name);
378 free (quote);
379 quote = brack;
381 else
383 cur = quote;
384 while (cur->next != qtail)
385 cur = cur->next;
386 cur->next = brack;
387 if (CPP_OPTION (pfile, verbose))
388 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"),
389 qtail->name);
391 free (qtail->name);
392 free (qtail);
395 else
396 qtail->next = brack;
398 else
399 quote = brack;
401 CPP_OPTION (pfile, quote_include) = quote;
402 CPP_OPTION (pfile, bracket_include) = brack;
406 /* Initialize a cpp_reader structure. */
407 void
408 cpp_reader_init (pfile)
409 cpp_reader *pfile;
411 memset ((char *) pfile, 0, sizeof (cpp_reader));
413 pfile->token_buffer_size = 200;
414 pfile->token_buffer = (U_CHAR *) xmalloc (pfile->token_buffer_size);
415 CPP_SET_WRITTEN (pfile, 0);
417 CPP_OPTION (pfile, dollars_in_ident) = 1;
418 CPP_OPTION (pfile, cplusplus_comments) = 1;
419 CPP_OPTION (pfile, warn_import) = 1;
420 CPP_OPTION (pfile, warn_paste) = 1;
421 CPP_OPTION (pfile, digraphs) = 1;
422 CPP_OPTION (pfile, discard_comments) = 1;
423 CPP_OPTION (pfile, show_column) = 1;
424 CPP_OPTION (pfile, tabstop) = 8;
426 CPP_OPTION (pfile, pending) =
427 (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
429 _cpp_init_macros (pfile);
430 _cpp_init_stacks (pfile);
431 _cpp_init_includes (pfile);
434 /* Initialize a cpp_printer structure. As a side effect, open the
435 output file. */
436 cpp_printer *
437 cpp_printer_init (pfile, print)
438 cpp_reader *pfile;
439 cpp_printer *print;
441 memset (print, '\0', sizeof (cpp_printer));
442 if (CPP_OPTION (pfile, out_fname) == NULL)
443 CPP_OPTION (pfile, out_fname) = "";
445 if (CPP_OPTION (pfile, out_fname)[0] == '\0')
446 print->outf = stdout;
447 else
449 print->outf = fopen (CPP_OPTION (pfile, out_fname), "w");
450 if (! print->outf)
452 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));
453 return NULL;
456 return print;
459 /* Free resources used by PFILE.
460 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
461 void
462 cpp_cleanup (pfile)
463 cpp_reader *pfile;
465 while (CPP_BUFFER (pfile) != NULL)
466 cpp_pop_buffer (pfile);
468 if (pfile->token_buffer)
470 free (pfile->token_buffer);
471 pfile->token_buffer = NULL;
474 if (pfile->deps)
475 deps_free (pfile->deps);
477 if (pfile->spec_nodes)
478 free (pfile->spec_nodes);
480 _cpp_free_temp_tokens (pfile);
481 _cpp_cleanup_includes (pfile);
482 _cpp_cleanup_stacks (pfile);
483 _cpp_cleanup_macros (pfile);
487 /* This structure defines one built-in macro. A node of type TYPE will
488 be entered in the macro hash table under the name NAME, with value
489 VALUE (if any). Two values are not compile time constants, so we tag
490 them in the FLAGS field instead:
491 VERS value is the global version_string, quoted
492 ULP value is the global user_label_prefix
495 struct builtin
497 const U_CHAR *name;
498 const char *value;
499 unsigned short type;
500 unsigned short flags;
501 unsigned int len;
503 #define VERS 0x01
504 #define ULP 0x02
506 #define B(n, t) { U n, 0, t, 0, sizeof n - 1 }
507 #define C(n, v) { U n, v, T_MACRO, 0, sizeof n - 1 }
508 #define X(n, f) { U n, 0, T_MACRO, f, sizeof n - 1 }
509 static const struct builtin builtin_array[] =
511 B("__TIME__", T_TIME),
512 B("__DATE__", T_DATE),
513 B("__FILE__", T_FILE),
514 B("__BASE_FILE__", T_BASE_FILE),
515 B("__LINE__", T_SPECLINE),
516 B("__INCLUDE_LEVEL__", T_INCLUDE_LEVEL),
517 B("__STDC__", T_STDC),
519 X("__VERSION__", VERS),
520 X("__USER_LABEL_PREFIX__", ULP),
521 C("__REGISTER_PREFIX__", REGISTER_PREFIX),
522 C("__HAVE_BUILTIN_SETJMP__", "1"),
523 #ifndef NO_BUILTIN_SIZE_TYPE
524 C("__SIZE_TYPE__", SIZE_TYPE),
525 #endif
526 #ifndef NO_BUILTIN_PTRDIFF_TYPE
527 C("__PTRDIFF_TYPE__", PTRDIFF_TYPE),
528 #endif
529 #ifndef NO_BUILTIN_WCHAR_TYPE
530 C("__WCHAR_TYPE__", WCHAR_TYPE),
531 #endif
533 #undef B
534 #undef C
535 #undef X
536 #define builtin_array_end \
537 builtin_array + sizeof(builtin_array)/sizeof(struct builtin)
539 /* Subroutine of cpp_start_read; reads the builtins table above and
540 enters the macros into the hash table. */
541 static void
542 initialize_builtins (pfile)
543 cpp_reader *pfile;
545 const struct builtin *b;
546 for(b = builtin_array; b < builtin_array_end; b++)
548 if (b->type == T_MACRO)
550 const char *val;
551 char *str;
553 if (b->flags & VERS)
555 /* Allocate enough space for 'name="value"\0'. */
556 str = xmalloc (b->len + strlen (version_string) + 4);
557 sprintf (str, "%s=\"%s\"", b->name, version_string);
559 else
561 if (b->flags & ULP)
562 val = user_label_prefix;
563 else
564 val = b->value;
566 /* Allocate enough space for "name=value\0". */
567 str = xmalloc (b->len + strlen (val) + 2);
568 sprintf(str, "%s=%s", b->name, val);
570 cpp_define (pfile, str);
572 else
574 cpp_hashnode *hp;
576 if (b->type == T_STDC && CPP_TRADITIONAL (pfile))
577 continue;
579 hp = cpp_lookup (pfile, b->name, b->len);
580 hp->type = b->type;
584 #undef VERS
585 #undef ULP
586 #undef builtin_array_end
588 /* Another subroutine of cpp_start_read. This one sets up to do
589 dependency-file output. */
590 static void
591 initialize_dependency_output (pfile)
592 cpp_reader *pfile;
594 char *spec, *s, *output_file;
596 /* Either of two environment variables can specify output of deps.
597 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
598 where OUTPUT_FILE is the file to write deps info to
599 and DEPS_TARGET is the target to mention in the deps. */
601 if (CPP_OPTION (pfile, print_deps) == 0)
603 spec = getenv ("DEPENDENCIES_OUTPUT");
604 if (spec)
605 CPP_OPTION (pfile, print_deps) = 1;
606 else
608 spec = getenv ("SUNPRO_DEPENDENCIES");
609 if (spec)
610 CPP_OPTION (pfile, print_deps) = 2;
611 else
612 return;
615 /* Find the space before the DEPS_TARGET, if there is one. */
616 s = strchr (spec, ' ');
617 if (s)
619 CPP_OPTION (pfile, deps_target) = s + 1;
620 output_file = (char *) xmalloc (s - spec + 1);
621 memcpy (output_file, spec, s - spec);
622 output_file[s - spec] = 0;
624 else
626 CPP_OPTION (pfile, deps_target) = 0;
627 output_file = spec;
630 CPP_OPTION (pfile, deps_file) = output_file;
631 CPP_OPTION (pfile, print_deps_append) = 1;
634 pfile->deps = deps_init ();
636 /* Print the expected object file name as the target of this Make-rule. */
637 if (CPP_OPTION (pfile, deps_target))
638 deps_add_target (pfile->deps, CPP_OPTION (pfile, deps_target));
639 else if (*CPP_OPTION (pfile, in_fname) == 0)
640 deps_add_target (pfile->deps, "-");
641 else
642 deps_calc_target (pfile->deps, CPP_OPTION (pfile, in_fname));
644 if (CPP_OPTION (pfile, in_fname))
645 deps_add_dep (pfile->deps, CPP_OPTION (pfile, in_fname));
648 /* And another subroutine. This one sets up the standard include path. */
649 static void
650 initialize_standard_includes (pfile)
651 cpp_reader *pfile;
653 char *path;
654 const struct default_include *p;
655 const char *specd_prefix = CPP_OPTION (pfile, include_prefix);
657 /* Several environment variables may add to the include search path.
658 CPATH specifies an additional list of directories to be searched
659 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
660 etc. specify an additional list of directories to be searched as
661 if specified with -isystem, for the language indicated. */
663 GET_ENV_PATH_LIST (path, "CPATH");
664 if (path != 0 && *path != 0)
665 path_include (pfile, CPP_OPTION (pfile, pending), path, BRACKET);
667 switch ((CPP_OPTION (pfile, objc) << 1) + CPP_OPTION (pfile, cplusplus))
669 case 0:
670 GET_ENV_PATH_LIST (path, "C_INCLUDE_PATH");
671 break;
672 case 1:
673 GET_ENV_PATH_LIST (path, "CPLUS_INCLUDE_PATH");
674 break;
675 case 2:
676 GET_ENV_PATH_LIST (path, "OBJC_INCLUDE_PATH");
677 break;
678 case 3:
679 GET_ENV_PATH_LIST (path, "OBJCPLUS_INCLUDE_PATH");
680 break;
682 if (path != 0 && *path != 0)
683 path_include (pfile, CPP_OPTION (pfile, pending), path, SYSTEM);
685 /* Search "translated" versions of GNU directories.
686 These have /usr/local/lib/gcc... replaced by specd_prefix. */
687 if (specd_prefix != 0 && cpp_GCC_INCLUDE_DIR_len)
689 /* Remove the `include' from /usr/local/lib/gcc.../include.
690 GCC_INCLUDE_DIR will always end in /include. */
691 int default_len = cpp_GCC_INCLUDE_DIR_len;
692 char *default_prefix = (char *) alloca (default_len + 1);
693 int specd_len = strlen (specd_prefix);
695 memcpy (default_prefix, cpp_GCC_INCLUDE_DIR, default_len);
696 default_prefix[default_len] = '\0';
698 for (p = cpp_include_defaults; p->fname; p++)
700 /* Some standard dirs are only for C++. */
701 if (!p->cplusplus
702 || (CPP_OPTION (pfile, cplusplus)
703 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
705 /* Does this dir start with the prefix? */
706 if (!strncmp (p->fname, default_prefix, default_len))
708 /* Yes; change prefix and add to search list. */
709 int flen = strlen (p->fname);
710 int this_len = specd_len + flen - default_len;
711 char *str = (char *) xmalloc (this_len + 1);
712 memcpy (str, specd_prefix, specd_len);
713 memcpy (str + specd_len,
714 p->fname + default_len,
715 flen - default_len + 1);
717 append_include_chain (pfile, CPP_OPTION (pfile, pending),
718 str, SYSTEM, p->cxx_aware);
724 /* Search ordinary names for GNU include directories. */
725 for (p = cpp_include_defaults; p->fname; p++)
727 /* Some standard dirs are only for C++. */
728 if (!p->cplusplus
729 || (CPP_OPTION (pfile, cplusplus)
730 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
732 /* XXX Potential memory leak! */
733 char *str = xstrdup (update_path (p->fname, p->component));
734 append_include_chain (pfile, CPP_OPTION (pfile, pending),
735 str, SYSTEM, p->cxx_aware);
740 /* This is called after options have been processed.
741 * Check options for consistency, and setup for processing input
742 * from the file named FNAME. (Use standard input if FNAME==NULL.)
743 * Return 1 on success, 0 on failure.
747 cpp_start_read (pfile, print, fname)
748 cpp_reader *pfile;
749 cpp_printer *print;
750 const char *fname;
752 struct pending_option *p, *q;
754 /* -MG doesn't select the form of output and must be specified with one of
755 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
756 inhibit compilation. */
757 if (CPP_OPTION (pfile, print_deps_missing_files)
758 && (CPP_OPTION (pfile, print_deps) == 0
759 || !CPP_OPTION (pfile, no_output)))
761 cpp_fatal (pfile, "-MG must be specified with one of -M or -MM");
762 return 0;
765 /* Chill should not be used with -trigraphs. */
766 if (CPP_OPTION (pfile, chill) && CPP_OPTION (pfile, trigraphs))
768 cpp_warning (pfile, "-lang-chill and -trigraphs are mutually exclusive");
769 CPP_OPTION (pfile, trigraphs) = 0;
772 /* -Wtraditional is not useful in C++ mode. */
773 if (CPP_OPTION (pfile, cplusplus))
774 CPP_OPTION (pfile, warn_traditional) = 0;
776 /* Do not warn about illegal token pasting if -traditional,
777 -lang-fortran, or -lang-asm. */
778 if (CPP_OPTION (pfile, traditional)
779 || CPP_OPTION (pfile, lang_fortran)
780 || CPP_OPTION (pfile, lang_asm))
781 CPP_OPTION (pfile, warn_paste) = 0;
783 /* Set this if it hasn't been set already. */
784 if (user_label_prefix == NULL)
785 user_label_prefix = USER_LABEL_PREFIX;
787 /* Figure out if we need to save function macro parameter spellings.
788 We don't use CPP_PEDANTIC() here because that depends on whether
789 or not the current file is a system header, and there is no
790 current file yet. */
791 pfile->save_parameter_spellings =
792 CPP_OPTION (pfile, pedantic)
793 || CPP_OPTION (pfile, debug_output)
794 || CPP_OPTION (pfile, dump_macros) == dump_definitions
795 || CPP_OPTION (pfile, dump_macros) == dump_only;
797 /* Set up the IStable. This doesn't do anything if we were compiled
798 with a compiler that supports C99 designated initializers. */
799 init_IStable ();
801 /* Set up the tables used by read_and_prescan. */
802 _cpp_init_input_buffer (pfile);
804 /* Set up the include search path now. */
805 if (! CPP_OPTION (pfile, no_standard_includes))
806 initialize_standard_includes (pfile);
808 merge_include_chains (pfile);
810 /* With -v, print the list of dirs to search. */
811 if (CPP_OPTION (pfile, verbose))
813 struct file_name_list *l;
814 fprintf (stderr, _("#include \"...\" search starts here:\n"));
815 for (l = CPP_OPTION (pfile, quote_include); l; l = l->next)
817 if (l == CPP_OPTION (pfile, bracket_include))
818 fprintf (stderr, _("#include <...> search starts here:\n"));
819 fprintf (stderr, " %s\n", l->name);
821 fprintf (stderr, _("End of search list.\n"));
824 /* Open the main input file. This must be done early, so we have a
825 buffer to stand on. */
826 if (CPP_OPTION (pfile, in_fname) == NULL
827 || *CPP_OPTION (pfile, in_fname) == 0)
829 CPP_OPTION (pfile, in_fname) = fname;
830 if (CPP_OPTION (pfile, in_fname) == NULL)
831 CPP_OPTION (pfile, in_fname) = "";
833 if (CPP_OPTION (pfile, out_fname) == NULL)
834 CPP_OPTION (pfile, out_fname) = "";
836 if (!cpp_read_file (pfile, fname))
837 return 0;
839 initialize_dependency_output (pfile);
841 /* -D and friends may produce output, which should be identified
842 as line 0. */
844 CPP_BUFFER (pfile)->lineno = 0;
845 if (print)
847 print->last_fname = CPP_BUFFER (pfile)->nominal_fname;
848 print->last_id = pfile->include_depth;
849 print->written = CPP_WRITTEN (pfile);
850 print->lineno = 0;
853 /* Install __LINE__, etc. */
854 initialize_builtins (pfile);
856 /* Do -U's, -D's and -A's in the order they were seen. */
857 p = CPP_OPTION (pfile, pending)->directive_head;
858 while (p)
860 (*p->handler) (pfile, p->arg);
861 q = p->next;
862 free (p);
863 p = q;
865 pfile->done_initializing = 1;
867 /* Now flush any output recorded during initialization, and advance
868 to line 1 of the main input file. */
869 CPP_BUFFER (pfile)->lineno = 1;
871 if (print && ! CPP_OPTION (pfile, no_output))
872 cpp_output_tokens (pfile, print, 1);
874 /* The -imacros files can be scanned now, but the -include files
875 have to be pushed onto the include stack and processed later,
876 in the main loop calling cpp_get_token. */
878 p = CPP_OPTION (pfile, pending)->imacros_head;
879 while (p)
881 if (cpp_read_file (pfile, p->arg))
882 cpp_scan_buffer_nooutput (pfile);
883 q = p->next;
884 free (p);
885 p = q;
888 p = CPP_OPTION (pfile, pending)->include_head;
889 while (p)
891 if (cpp_read_file (pfile, p->arg)
892 && print && ! CPP_OPTION (pfile, no_output))
893 cpp_output_tokens (pfile, print, 1); /* record entry to file */
894 q = p->next;
895 free (p);
896 p = q;
899 free (CPP_OPTION (pfile, pending));
900 CPP_OPTION (pfile, pending) = NULL;
902 return 1;
905 /* This is called at the end of preprocessing. It pops the
906 last buffer and writes dependency output. It should also
907 clear macro definitions, such that you could call cpp_start_read
908 with a new filename to restart processing. */
909 void
910 cpp_finish (pfile, print)
911 cpp_reader *pfile;
912 cpp_printer *print;
914 if (CPP_BUFFER (pfile))
916 cpp_ice (pfile, "buffers still stacked in cpp_finish");
917 while (CPP_BUFFER (pfile))
918 cpp_pop_buffer (pfile);
921 /* Don't write the deps file if preprocessing has failed. */
922 if (CPP_OPTION (pfile, print_deps) && pfile->errors == 0)
924 /* Stream on which to print the dependency information. */
925 FILE *deps_stream = 0;
926 const char *deps_mode
927 = CPP_OPTION (pfile, print_deps_append) ? "a" : "w";
928 if (CPP_OPTION (pfile, deps_file) == 0)
929 deps_stream = stdout;
930 else
932 deps_stream = fopen (CPP_OPTION (pfile, deps_file), deps_mode);
933 if (deps_stream == 0)
934 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, deps_file));
936 if (deps_stream)
938 deps_write (pfile->deps, deps_stream, 72);
939 if (CPP_OPTION (pfile, deps_file))
941 if (ferror (deps_stream) || fclose (deps_stream) != 0)
942 cpp_fatal (pfile, "I/O error on output");
947 if (CPP_OPTION (pfile, dump_macros) == dump_only)
948 _cpp_dump_macro_hash (pfile);
950 /* Flush any pending output. */
951 if (print)
953 cpp_output_tokens (pfile, print, print->lineno);
954 if (ferror (print->outf) || fclose (print->outf))
955 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));
958 /* Report on headers that could use multiple include guards. */
959 if (CPP_OPTION (pfile, print_include_names))
960 _cpp_report_missing_guards (pfile);
963 static void
964 new_pending_directive (pend, text, handler)
965 struct cpp_pending *pend;
966 const char *text;
967 cl_directive_handler handler;
969 struct pending_option *o = (struct pending_option *)
970 xmalloc (sizeof (struct pending_option));
972 o->arg = text;
973 o->next = NULL;
974 o->handler = handler;
975 APPEND (pend, directive, o);
978 /* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
979 I.e. a const string initializer with parens around it. That is
980 what N_("string") resolves to, so we make no_* be macros instead. */
981 #define no_arg N_("Argument missing after %s")
982 #define no_ass N_("Assertion missing after %s")
983 #define no_dir N_("Directory name missing after %s")
984 #define no_fil N_("File name missing after %s")
985 #define no_mac N_("Macro name missing after %s")
986 #define no_pth N_("Path name missing after %s")
987 #define no_num N_("Number missing after %s")
989 /* This is the list of all command line options, with the leading
990 "-" removed. It must be sorted in ASCII collating order. */
991 #define COMMAND_LINE_OPTIONS \
992 DEF_OPT("", 0, OPT_stdin_stdout) \
993 DEF_OPT("$", 0, OPT_dollar) \
994 DEF_OPT("+", 0, OPT_plus) \
995 DEF_OPT("-help", 0, OPT__help) \
996 DEF_OPT("-version", 0, OPT__version) \
997 DEF_OPT("A", no_ass, OPT_A) \
998 DEF_OPT("C", 0, OPT_C) \
999 DEF_OPT("D", no_mac, OPT_D) \
1000 DEF_OPT("H", 0, OPT_H) \
1001 DEF_OPT("I", no_dir, OPT_I) \
1002 DEF_OPT("M", 0, OPT_M) \
1003 DEF_OPT("MD", no_fil, OPT_MD) \
1004 DEF_OPT("MG", 0, OPT_MG) \
1005 DEF_OPT("MM", 0, OPT_MM) \
1006 DEF_OPT("MMD", no_fil, OPT_MMD) \
1007 DEF_OPT("P", 0, OPT_P) \
1008 DEF_OPT("U", no_mac, OPT_U) \
1009 DEF_OPT("W", no_arg, OPT_W) /* arg optional */ \
1010 DEF_OPT("d", no_arg, OPT_d) \
1011 DEF_OPT("fleading-underscore", 0, OPT_fleading_underscore) \
1012 DEF_OPT("fno-leading-underscore", 0, OPT_fno_leading_underscore) \
1013 DEF_OPT("fno-preprocessed", 0, OPT_fno_preprocessed) \
1014 DEF_OPT("fno-show-column", 0, OPT_fno_show_column) \
1015 DEF_OPT("fpreprocessed", 0, OPT_fpreprocessed) \
1016 DEF_OPT("fshow-column", 0, OPT_fshow_column) \
1017 DEF_OPT("ftabstop=", no_num, OPT_ftabstop) \
1018 DEF_OPT("g", no_arg, OPT_g) /* arg optional */ \
1019 DEF_OPT("h", 0, OPT_h) \
1020 DEF_OPT("idirafter", no_dir, OPT_idirafter) \
1021 DEF_OPT("imacros", no_fil, OPT_imacros) \
1022 DEF_OPT("include", no_fil, OPT_include) \
1023 DEF_OPT("iprefix", no_pth, OPT_iprefix) \
1024 DEF_OPT("isystem", no_dir, OPT_isystem) \
1025 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix) \
1026 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore) \
1027 DEF_OPT("lang-asm", 0, OPT_lang_asm) \
1028 DEF_OPT("lang-c", 0, OPT_lang_c) \
1029 DEF_OPT("lang-c++", 0, OPT_lang_cplusplus) \
1030 DEF_OPT("lang-c89", 0, OPT_lang_c89) \
1031 DEF_OPT("lang-chill", 0, OPT_lang_chill) \
1032 DEF_OPT("lang-fortran", 0, OPT_lang_fortran) \
1033 DEF_OPT("lang-objc", 0, OPT_lang_objc) \
1034 DEF_OPT("lang-objc++", 0, OPT_lang_objcplusplus) \
1035 DEF_OPT("nostdinc", 0, OPT_nostdinc) \
1036 DEF_OPT("nostdinc++", 0, OPT_nostdincplusplus) \
1037 DEF_OPT("o", no_fil, OPT_o) \
1038 DEF_OPT("pedantic", 0, OPT_pedantic) \
1039 DEF_OPT("pedantic-errors", 0, OPT_pedantic_errors) \
1040 DEF_OPT("remap", 0, OPT_remap) \
1041 DEF_OPT("std=c89", 0, OPT_std_c89) \
1042 DEF_OPT("std=c99", 0, OPT_std_c99) \
1043 DEF_OPT("std=c9x", 0, OPT_std_c9x) \
1044 DEF_OPT("std=gnu89", 0, OPT_std_gnu89) \
1045 DEF_OPT("std=gnu99", 0, OPT_std_gnu99) \
1046 DEF_OPT("std=gnu9x", 0, OPT_std_gnu9x) \
1047 DEF_OPT("std=iso9899:1990", 0, OPT_std_iso9899_1990) \
1048 DEF_OPT("std=iso9899:199409", 0, OPT_std_iso9899_199409) \
1049 DEF_OPT("std=iso9899:1999", 0, OPT_std_iso9899_1999) \
1050 DEF_OPT("std=iso9899:199x", 0, OPT_std_iso9899_199x) \
1051 DEF_OPT("traditional", 0, OPT_traditional) \
1052 DEF_OPT("trigraphs", 0, OPT_trigraphs) \
1053 DEF_OPT("v", 0, OPT_v) \
1054 DEF_OPT("w", 0, OPT_w)
1056 #define DEF_OPT(text, msg, code) code,
1057 enum opt_code
1059 COMMAND_LINE_OPTIONS
1060 N_OPTS
1062 #undef DEF_OPT
1064 struct cl_option
1066 const char *opt_text;
1067 const char *msg;
1068 size_t opt_len;
1069 enum opt_code opt_code;
1072 #define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
1073 #ifdef HOST_EBCDIC
1074 static struct cl_option cl_options[] =
1075 #else
1076 static const struct cl_option cl_options[] =
1077 #endif
1079 COMMAND_LINE_OPTIONS
1081 #undef DEF_OPT
1082 #undef COMMAND_LINE_OPTIONS
1084 /* Perform a binary search to find which, if any, option the given
1085 command-line matches. Returns its index in the option array,
1086 negative on failure. Complications arise since some options can be
1087 suffixed with an argument, and multiple complete matches can occur,
1088 e.g. -iwithprefix and -iwithprefixbefore. Moreover, we want to
1089 accept options beginning with -g and -W that we do not recognise,
1090 but not to swallow any subsequent command line argument; these are
1091 handled as special cases in cpp_handle_option */
1092 static int
1093 parse_option (input)
1094 const char *input;
1096 unsigned int md, mn, mx;
1097 size_t opt_len;
1098 int comp;
1100 mn = 0;
1101 mx = N_OPTS;
1103 while (mx > mn)
1105 md = (mn + mx) / 2;
1107 opt_len = cl_options[md].opt_len;
1108 comp = strncmp (input, cl_options[md].opt_text, opt_len);
1110 if (comp > 0)
1111 mn = md + 1;
1112 else if (comp < 0)
1113 mx = md;
1114 else
1116 if (input[opt_len] == '\0')
1117 return md;
1118 /* We were passed more text. If the option takes an argument,
1119 we may match a later option or we may have been passed the
1120 argument. The longest possible option match succeeds.
1121 If the option takes no arguments we have not matched and
1122 continue the search (e.g. input="stdc++" match was "stdc") */
1123 mn = md + 1;
1124 if (cl_options[md].msg)
1126 /* Scan forwards. If we get an exact match, return it.
1127 Otherwise, return the longest option-accepting match.
1128 This loops no more than twice with current options */
1129 mx = md;
1130 for (; mn < N_OPTS; mn++)
1132 opt_len = cl_options[mn].opt_len;
1133 if (strncmp (input, cl_options[mn].opt_text, opt_len))
1134 break;
1135 if (input[opt_len] == '\0')
1136 return mn;
1137 if (cl_options[mn].msg)
1138 mx = mn;
1140 return mx;
1145 return -1;
1148 /* Handle one command-line option in (argc, argv).
1149 Can be called multiple times, to handle multiple sets of options.
1150 Returns number of strings consumed. */
1152 static int
1153 handle_option (pfile, argc, argv)
1154 cpp_reader *pfile;
1155 int argc;
1156 char **argv;
1158 int i = 0;
1159 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
1161 if (argv[i][0] != '-')
1163 if (CPP_OPTION (pfile, out_fname) != NULL)
1164 cpp_fatal (pfile, "Too many arguments. Type %s --help for usage info",
1165 progname);
1166 else if (CPP_OPTION (pfile, in_fname) != NULL)
1167 CPP_OPTION (pfile, out_fname) = argv[i];
1168 else
1169 CPP_OPTION (pfile, in_fname) = argv[i];
1171 else
1173 enum opt_code opt_code;
1174 int opt_index;
1175 const char *arg = 0;
1177 /* Skip over '-' */
1178 opt_index = parse_option (&argv[i][1]);
1179 if (opt_index < 0)
1180 return i;
1182 opt_code = cl_options[opt_index].opt_code;
1183 if (cl_options[opt_index].msg)
1185 arg = &argv[i][cl_options[opt_index].opt_len + 1];
1187 /* Yuk. Special case for -g and -W as they must not swallow
1188 up any following argument. If this becomes common, add
1189 another field to the cl_options table */
1190 if (arg[0] == '\0' && !(opt_code == OPT_g || opt_code == OPT_W))
1192 arg = argv[++i];
1193 if (!arg)
1195 cpp_fatal (pfile, cl_options[opt_index].msg, argv[i - 1]);
1196 return argc;
1201 switch (opt_code)
1203 case N_OPTS: /* shut GCC up */
1204 break;
1205 case OPT_fleading_underscore:
1206 user_label_prefix = "_";
1207 break;
1208 case OPT_fno_leading_underscore:
1209 user_label_prefix = "";
1210 break;
1211 case OPT_fpreprocessed:
1212 CPP_OPTION (pfile, preprocessed) = 1;
1213 break;
1214 case OPT_fno_preprocessed:
1215 CPP_OPTION (pfile, preprocessed) = 0;
1216 break;
1217 case OPT_fshow_column:
1218 CPP_OPTION (pfile, show_column) = 1;
1219 break;
1220 case OPT_fno_show_column:
1221 CPP_OPTION (pfile, show_column) = 0;
1222 break;
1223 case OPT_ftabstop:
1224 /* Silently ignore empty string, non-longs and silly values. */
1225 if (arg[0] != '\0')
1227 char *endptr;
1228 long tabstop = strtol (arg, &endptr, 10);
1229 if (*endptr == '\0' && tabstop >= 1 && tabstop <= 100)
1230 CPP_OPTION (pfile, tabstop) = tabstop;
1232 break;
1233 case OPT_w:
1234 CPP_OPTION (pfile, inhibit_warnings) = 1;
1235 break;
1236 case OPT_g: /* Silently ignore anything but -g3 */
1237 if (!strcmp(&argv[i][2], "3"))
1238 CPP_OPTION (pfile, debug_output) = 1;
1239 break;
1240 case OPT_h:
1241 case OPT__help:
1242 print_help ();
1243 exit (0); /* XXX */
1244 break;
1245 case OPT__version:
1246 fprintf (stderr, _("GNU CPP version %s (cpplib)\n"), version_string);
1247 exit (0); /* XXX */
1248 break;
1249 case OPT_C:
1250 CPP_OPTION (pfile, discard_comments) = 0;
1251 break;
1252 case OPT_P:
1253 CPP_OPTION (pfile, no_line_commands) = 1;
1254 break;
1255 case OPT_dollar: /* Don't include $ in identifiers. */
1256 CPP_OPTION (pfile, dollars_in_ident) = 0;
1257 break;
1258 case OPT_H:
1259 CPP_OPTION (pfile, print_include_names) = 1;
1260 break;
1261 case OPT_D:
1262 new_pending_directive (pend, arg, cpp_define);
1263 break;
1264 case OPT_pedantic_errors:
1265 CPP_OPTION (pfile, pedantic_errors) = 1;
1266 /* fall through */
1267 case OPT_pedantic:
1268 CPP_OPTION (pfile, pedantic) = 1;
1269 break;
1270 case OPT_traditional:
1271 CPP_OPTION (pfile, traditional) = 1;
1272 CPP_OPTION (pfile, cplusplus_comments) = 0;
1273 CPP_OPTION (pfile, trigraphs) = 0;
1274 CPP_OPTION (pfile, digraphs) = 0;
1275 CPP_OPTION (pfile, warn_trigraphs) = 0;
1276 break;
1277 case OPT_trigraphs:
1278 CPP_OPTION (pfile, trigraphs) = 1;
1279 break;
1280 case OPT_plus:
1281 CPP_OPTION (pfile, cplusplus) = 1;
1282 CPP_OPTION (pfile, cplusplus_comments) = 1;
1283 break;
1284 case OPT_remap:
1285 CPP_OPTION (pfile, remap) = 1;
1286 break;
1287 case OPT_iprefix:
1288 CPP_OPTION (pfile, include_prefix) = arg;
1289 CPP_OPTION (pfile, include_prefix_len) = strlen (arg);
1290 break;
1291 case OPT_lang_c:
1292 CPP_OPTION (pfile, cplusplus) = 0;
1293 CPP_OPTION (pfile, cplusplus_comments) = 1;
1294 CPP_OPTION (pfile, c89) = 0;
1295 CPP_OPTION (pfile, c99) = 1;
1296 CPP_OPTION (pfile, digraphs) = 1;
1297 CPP_OPTION (pfile, objc) = 0;
1298 break;
1299 case OPT_lang_cplusplus:
1300 CPP_OPTION (pfile, cplusplus) = 1;
1301 CPP_OPTION (pfile, cplusplus_comments) = 1;
1302 CPP_OPTION (pfile, c89) = 0;
1303 CPP_OPTION (pfile, c99) = 0;
1304 CPP_OPTION (pfile, objc) = 0;
1305 CPP_OPTION (pfile, digraphs) = 1;
1306 new_pending_directive (pend, "__cplusplus", cpp_define);
1307 break;
1308 case OPT_lang_objcplusplus:
1309 CPP_OPTION (pfile, cplusplus) = 1;
1310 new_pending_directive (pend, "__cplusplus", cpp_define);
1311 /* fall through */
1312 case OPT_lang_objc:
1313 CPP_OPTION (pfile, cplusplus_comments) = 1;
1314 CPP_OPTION (pfile, c89) = 0;
1315 CPP_OPTION (pfile, c99) = 0;
1316 CPP_OPTION (pfile, objc) = 1;
1317 new_pending_directive (pend, "__OBJC__", cpp_define);
1318 break;
1319 case OPT_lang_asm:
1320 CPP_OPTION (pfile, lang_asm) = 1;
1321 CPP_OPTION (pfile, dollars_in_ident) = 0;
1322 new_pending_directive (pend, "__ASSEMBLER__", cpp_define);
1323 break;
1324 case OPT_lang_fortran:
1325 CPP_OPTION (pfile, lang_fortran) = 1;
1326 CPP_OPTION (pfile, traditional) = 1;
1327 CPP_OPTION (pfile, cplusplus_comments) = 0;
1328 new_pending_directive (pend, "_LANGUAGE_FORTRAN", cpp_define);
1329 break;
1330 case OPT_lang_chill:
1331 CPP_OPTION (pfile, objc) = 0;
1332 CPP_OPTION (pfile, cplusplus) = 0;
1333 CPP_OPTION (pfile, chill) = 1;
1334 CPP_OPTION (pfile, traditional) = 1;
1335 break;
1336 case OPT_nostdinc:
1337 /* -nostdinc causes no default include directories.
1338 You must specify all include-file directories with -I. */
1339 CPP_OPTION (pfile, no_standard_includes) = 1;
1340 break;
1341 case OPT_nostdincplusplus:
1342 /* -nostdinc++ causes no default C++-specific include directories. */
1343 CPP_OPTION (pfile, no_standard_cplusplus_includes) = 1;
1344 break;
1345 case OPT_std_gnu89:
1346 CPP_OPTION (pfile, cplusplus) = 0;
1347 CPP_OPTION (pfile, cplusplus_comments) = 1;
1348 CPP_OPTION (pfile, c89) = 1;
1349 CPP_OPTION (pfile, c99) = 0;
1350 CPP_OPTION (pfile, objc) = 0;
1351 CPP_OPTION (pfile, digraphs) = 1;
1352 break;
1353 case OPT_std_gnu9x:
1354 case OPT_std_gnu99:
1355 CPP_OPTION (pfile, cplusplus) = 0;
1356 CPP_OPTION (pfile, cplusplus_comments) = 1;
1357 CPP_OPTION (pfile, c89) = 0;
1358 CPP_OPTION (pfile, c99) = 1;
1359 CPP_OPTION (pfile, digraphs) = 1;
1360 CPP_OPTION (pfile, objc) = 0;
1361 new_pending_directive (CPP_OPTION (pfile, pending),
1362 "__STDC_VERSION__=199901L", cpp_define);
1363 break;
1364 case OPT_std_iso9899_199409:
1365 new_pending_directive (CPP_OPTION (pfile, pending),
1366 "__STDC_VERSION__=199409L", cpp_define);
1367 /* Fall through */
1368 case OPT_std_iso9899_1990:
1369 case OPT_std_c89:
1370 case OPT_lang_c89:
1371 CPP_OPTION (pfile, cplusplus) = 0;
1372 CPP_OPTION (pfile, cplusplus_comments) = 0;
1373 CPP_OPTION (pfile, c89) = 1;
1374 CPP_OPTION (pfile, c99) = 0;
1375 CPP_OPTION (pfile, objc) = 0;
1376 CPP_OPTION (pfile, digraphs) = opt_code == OPT_std_iso9899_199409;
1377 CPP_OPTION (pfile, trigraphs) = 1;
1378 new_pending_directive (pend, "__STRICT_ANSI__", cpp_define);
1379 break;
1380 case OPT_std_iso9899_199x:
1381 case OPT_std_iso9899_1999:
1382 case OPT_std_c9x:
1383 case OPT_std_c99:
1384 CPP_OPTION (pfile, cplusplus) = 0;
1385 CPP_OPTION (pfile, cplusplus_comments) = 1;
1386 CPP_OPTION (pfile, c89) = 0;
1387 CPP_OPTION (pfile, c99) = 1;
1388 CPP_OPTION (pfile, objc) = 0;
1389 CPP_OPTION (pfile, digraphs) = 1;
1390 CPP_OPTION (pfile, trigraphs) = 1;
1391 new_pending_directive (CPP_OPTION (pfile, pending),
1392 "__STRICT_ANSI__", cpp_define);
1393 new_pending_directive (CPP_OPTION (pfile, pending),
1394 "__STDC_VERSION__=199901L", cpp_define);
1395 break;
1396 case OPT_o:
1397 if (CPP_OPTION (pfile, out_fname) != NULL)
1399 cpp_fatal (pfile, "Output filename specified twice");
1400 return argc;
1402 CPP_OPTION (pfile, out_fname) = arg;
1403 if (!strcmp (CPP_OPTION (pfile, out_fname), "-"))
1404 CPP_OPTION (pfile, out_fname) = "";
1405 break;
1406 case OPT_v:
1407 fprintf (stderr, _("GNU CPP version %s (cpplib)\n"), version_string);
1408 #ifdef TARGET_VERSION
1409 TARGET_VERSION;
1410 #endif
1411 fputc ('\n', stderr);
1412 CPP_OPTION (pfile, verbose) = 1;
1413 break;
1414 case OPT_stdin_stdout:
1415 /* JF handle '-' as file name meaning stdin or stdout */
1416 if (CPP_OPTION (pfile, in_fname) == NULL)
1417 CPP_OPTION (pfile, in_fname) = "";
1418 else if (CPP_OPTION (pfile, out_fname) == NULL)
1419 CPP_OPTION (pfile, out_fname) = "";
1420 break;
1421 case OPT_d:
1422 /* Args to -d specify what parts of macros to dump.
1423 Silently ignore unrecognised options; they may
1424 be aimed at the compiler proper. */
1426 char c;
1428 while ((c = *arg++) != '\0')
1429 switch (c)
1431 case 'M':
1432 CPP_OPTION (pfile, dump_macros) = dump_only;
1433 CPP_OPTION (pfile, no_output) = 1;
1434 break;
1435 case 'N':
1436 CPP_OPTION (pfile, dump_macros) = dump_names;
1437 break;
1438 case 'D':
1439 CPP_OPTION (pfile, dump_macros) = dump_definitions;
1440 break;
1441 case 'I':
1442 CPP_OPTION (pfile, dump_includes) = 1;
1443 break;
1446 break;
1447 /* The style of the choices here is a bit mixed.
1448 The chosen scheme is a hybrid of keeping all options in one string
1449 and specifying each option in a separate argument:
1450 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
1451 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1452 -M[M][G][D file]. This is awkward to handle in specs, and is not
1453 as extensible. */
1454 /* ??? -MG must be specified in addition to one of -M or -MM.
1455 This can be relaxed in the future without breaking anything.
1456 The converse isn't true. */
1458 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
1459 case OPT_MG:
1460 CPP_OPTION (pfile, print_deps_missing_files) = 1;
1461 break;
1462 case OPT_M:
1463 case OPT_MD:
1464 case OPT_MM:
1465 case OPT_MMD:
1466 if (opt_code == OPT_M || opt_code == OPT_MD)
1467 CPP_OPTION (pfile, print_deps) = 2;
1468 else
1469 CPP_OPTION (pfile, print_deps) = 1;
1471 /* For -MD and -MMD options, write deps on file named by next arg */
1472 /* For -M and -MM, write deps on standard output
1473 and suppress the usual output. */
1474 if (opt_code == OPT_MD || opt_code == OPT_MMD)
1475 CPP_OPTION (pfile, deps_file) = arg;
1476 else
1477 CPP_OPTION (pfile, no_output) = 1;
1478 break;
1479 case OPT_A:
1480 if (arg[0] == '-')
1482 /* -A with an argument beginning with '-' acts as
1483 #unassert on whatever immediately follows the '-'.
1484 If "-" is the whole argument, we eliminate all
1485 predefined macros and assertions, including those
1486 that were specified earlier on the command line.
1487 That way we can get rid of any that were passed
1488 automatically in from GCC. */
1490 if (arg[1] == '\0')
1492 struct pending_option *o1, *o2;
1494 o1 = CPP_OPTION (pfile, pending)->directive_head;
1495 while (o1)
1497 o2 = o1->next;
1498 free (o1);
1499 o1 = o2;
1501 CPP_OPTION (pfile, pending)->directive_head = NULL;
1502 CPP_OPTION (pfile, pending)->directive_tail = NULL;
1504 else
1505 new_pending_directive (CPP_OPTION (pfile, pending),
1506 arg + 1, cpp_unassert);
1508 else
1509 new_pending_directive (CPP_OPTION (pfile, pending),
1510 arg, cpp_assert);
1511 break;
1512 case OPT_U:
1513 new_pending_directive (CPP_OPTION (pfile, pending), arg, cpp_undef);
1514 break;
1515 case OPT_I: /* Add directory to path for includes. */
1516 if (!strcmp (arg, "-"))
1518 /* -I- means:
1519 Use the preceding -I directories for #include "..."
1520 but not #include <...>.
1521 Don't search the directory of the present file
1522 for #include "...". (Note that -I. -I- is not the same as
1523 the default setup; -I. uses the compiler's working dir.) */
1524 if (! CPP_OPTION (pfile, ignore_srcdir))
1526 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
1527 pend->quote_head = pend->brack_head;
1528 pend->quote_tail = pend->brack_tail;
1529 pend->brack_head = 0;
1530 pend->brack_tail = 0;
1531 CPP_OPTION (pfile, ignore_srcdir) = 1;
1533 else
1535 cpp_fatal (pfile, "-I- specified twice");
1536 return argc;
1539 else
1540 append_include_chain (pfile, CPP_OPTION (pfile, pending),
1541 xstrdup (arg), BRACKET, 0);
1542 break;
1543 case OPT_isystem:
1544 /* Add directory to beginning of system include path, as a system
1545 include directory. */
1546 append_include_chain (pfile, CPP_OPTION (pfile, pending),
1547 xstrdup (arg), SYSTEM, 0);
1548 break;
1549 case OPT_include:
1551 struct pending_option *o = (struct pending_option *)
1552 xmalloc (sizeof (struct pending_option));
1553 o->arg = arg;
1555 /* This list has to be built in reverse order so that
1556 when cpp_start_read pushes all the -include files onto
1557 the buffer stack, they will be scanned in forward order. */
1558 o->next = CPP_OPTION (pfile, pending)->include_head;
1559 CPP_OPTION (pfile, pending)->include_head = o;
1561 break;
1562 case OPT_imacros:
1564 struct pending_option *o = (struct pending_option *)
1565 xmalloc (sizeof (struct pending_option));
1566 o->arg = arg;
1567 o->next = NULL;
1569 APPEND (CPP_OPTION (pfile, pending), imacros, o);
1571 break;
1572 case OPT_iwithprefix:
1573 /* Add directory to end of path for includes,
1574 with the default prefix at the front of its name. */
1575 /* fall through */
1576 case OPT_iwithprefixbefore:
1577 /* Add directory to main path for includes,
1578 with the default prefix at the front of its name. */
1580 char *fname;
1581 int len;
1583 len = strlen (arg);
1585 if (CPP_OPTION (pfile, include_prefix) != 0)
1587 size_t ipl = CPP_OPTION (pfile, include_prefix_len);
1588 fname = xmalloc (ipl + len + 1);
1589 memcpy (fname, CPP_OPTION (pfile, include_prefix), ipl);
1590 memcpy (fname + ipl, arg, len + 1);
1592 else if (cpp_GCC_INCLUDE_DIR_len)
1594 fname = xmalloc (cpp_GCC_INCLUDE_DIR_len + len + 1);
1595 memcpy (fname, cpp_GCC_INCLUDE_DIR, cpp_GCC_INCLUDE_DIR_len);
1596 memcpy (fname + cpp_GCC_INCLUDE_DIR_len, arg, len + 1);
1598 else
1599 fname = xstrdup (arg);
1601 append_include_chain (pfile, CPP_OPTION (pfile, pending), fname,
1602 opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0);
1604 break;
1605 case OPT_idirafter:
1606 /* Add directory to end of path for includes. */
1607 append_include_chain (pfile, CPP_OPTION (pfile, pending),
1608 xstrdup (arg), AFTER, 0);
1609 break;
1610 case OPT_W:
1611 /* Silently ignore unrecognised options */
1612 if (!strcmp (argv[i], "-Wall"))
1614 CPP_OPTION (pfile, warn_trigraphs) = 1;
1615 CPP_OPTION (pfile, warn_comments) = 1;
1617 else if (!strcmp (argv[i], "-Wtraditional"))
1618 CPP_OPTION (pfile, warn_traditional) = 1;
1619 else if (!strcmp (argv[i], "-Wtrigraphs"))
1620 CPP_OPTION (pfile, warn_trigraphs) = 1;
1621 else if (!strcmp (argv[i], "-Wcomment"))
1622 CPP_OPTION (pfile, warn_comments) = 1;
1623 else if (!strcmp (argv[i], "-Wcomments"))
1624 CPP_OPTION (pfile, warn_comments) = 1;
1625 else if (!strcmp (argv[i], "-Wundef"))
1626 CPP_OPTION (pfile, warn_undef) = 1;
1627 else if (!strcmp (argv[i], "-Wimport"))
1628 CPP_OPTION (pfile, warn_import) = 1;
1629 else if (!strcmp (argv[i], "-Wpaste"))
1630 CPP_OPTION (pfile, warn_paste) = 1;
1631 else if (!strcmp (argv[i], "-Werror"))
1632 CPP_OPTION (pfile, warnings_are_errors) = 1;
1633 else if (!strcmp (argv[i], "-Wno-traditional"))
1634 CPP_OPTION (pfile, warn_traditional) = 0;
1635 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1636 CPP_OPTION (pfile, warn_trigraphs) = 0;
1637 else if (!strcmp (argv[i], "-Wno-comment"))
1638 CPP_OPTION (pfile, warn_comments) = 0;
1639 else if (!strcmp (argv[i], "-Wno-comments"))
1640 CPP_OPTION (pfile, warn_comments) = 0;
1641 else if (!strcmp (argv[i], "-Wno-undef"))
1642 CPP_OPTION (pfile, warn_undef) = 0;
1643 else if (!strcmp (argv[i], "-Wno-import"))
1644 CPP_OPTION (pfile, warn_import) = 0;
1645 else if (!strcmp (argv[i], "-Wno-paste"))
1646 CPP_OPTION (pfile, warn_paste) = 0;
1647 else if (!strcmp (argv[i], "-Wno-error"))
1648 CPP_OPTION (pfile, warnings_are_errors) = 0;
1649 break;
1652 return i + 1;
1655 #ifdef HOST_EBCDIC
1656 static int
1657 opt_comp (const void *p1, const void *p2)
1659 return strcmp (((struct cl_option *)p1)->opt_text,
1660 ((struct cl_option *)p2)->opt_text);
1662 #endif
1664 /* Handle command-line options in (argc, argv).
1665 Can be called multiple times, to handle multiple sets of options.
1666 Returns if an unrecognized option is seen.
1667 Returns number of strings consumed. */
1669 cpp_handle_options (pfile, argc, argv)
1670 cpp_reader *pfile;
1671 int argc;
1672 char **argv;
1674 int i;
1675 int strings_processed;
1677 #ifdef HOST_EBCDIC
1678 static int opts_sorted = 0;
1680 if (!opts_sorted)
1682 opts_sorted = 1;
1683 /* For non-ASCII hosts, the array needs to be sorted at runtime */
1684 qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
1686 #endif
1688 for (i = 0; i < argc; i += strings_processed)
1690 strings_processed = handle_option (pfile, argc - i, argv + i);
1691 if (strings_processed == 0)
1692 break;
1694 return i;
1697 static void
1698 print_help ()
1700 fprintf (stderr, _("Usage: %s [switches] input output\n"), progname);
1701 /* To keep the lines from getting too long for some compilers, limit
1702 to about 500 characters (6 lines) per chunk. */
1703 fputs (_("\
1704 Switches:\n\
1705 -include <file> Include the contents of <file> before other files\n\
1706 -imacros <file> Accept definition of macros in <file>\n\
1707 -iprefix <path> Specify <path> as a prefix for next two options\n\
1708 -iwithprefix <dir> Add <dir> to the end of the system include path\n\
1709 -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\
1710 -isystem <dir> Add <dir> to the start of the system include path\n\
1711 "), stdout);
1712 fputs (_("\
1713 -idirafter <dir> Add <dir> to the end of the system include path\n\
1714 -I <dir> Add <dir> to the end of the main include path\n\
1715 -I- Fine-grained include path control; see info docs\n\
1716 -nostdinc Do not search system include directories\n\
1717 (dirs specified with -isystem will still be used)\n\
1718 -nostdinc++ Do not search system include directories for C++\n\
1719 -o <file> Put output into <file>\n\
1720 "), stdout);
1721 fputs (_("\
1722 -pedantic Issue all warnings demanded by strict ISO C\n\
1723 -pedantic-errors Issue -pedantic warnings as errors instead\n\
1724 -traditional Follow K&R pre-processor behaviour\n\
1725 -trigraphs Support ISO C trigraphs\n\
1726 -lang-c Assume that the input sources are in C\n\
1727 -lang-c89 Assume that the input sources are in C89\n\
1728 -lang-c++ Assume that the input sources are in C++\n\
1729 "), stdout);
1730 fputs (_("\
1731 -lang-objc Assume that the input sources are in ObjectiveC\n\
1732 -lang-objc++ Assume that the input sources are in ObjectiveC++\n\
1733 -lang-asm Assume that the input sources are in assembler\n\
1734 -lang-fortran Assume that the input sources are in Fortran\n\
1735 -lang-chill Assume that the input sources are in Chill\n\
1736 "), stdout);
1737 fputs (_("\
1738 -std=<std name> Specify the conformance standard; one of:\n\
1739 gnu89, gnu99, c89, c99, iso9899:1990,\n\
1740 iso9899:199409, iso9899:1999\n\
1741 -+ Allow parsing of C++ style features\n\
1742 -w Inhibit warning messages\n\
1743 -Wtrigraphs Warn if trigraphs are encountered\n\
1744 -Wno-trigraphs Do not warn about trigraphs\n\
1745 -Wcomment{s} Warn if one comment starts inside another\n\
1746 "), stdout);
1747 fputs (_("\
1748 -Wno-comment{s} Do not warn about comments\n\
1749 -Wtraditional Warn if a macro argument is/would be turned into\n\
1750 a string if -traditional is specified\n\
1751 -Wno-traditional Do not warn about stringification\n\
1752 -Wundef Warn if an undefined macro is used by #if\n\
1753 -Wno-undef Do not warn about testing undefined macros\n\
1754 -Wimport Warn about the use of the #import directive\n\
1755 "), stdout);
1756 fputs (_("\
1757 -Wno-import Do not warn about the use of #import\n\
1758 -Werror Treat all warnings as errors\n\
1759 -Wno-error Do not treat warnings as errors\n\
1760 -Wall Enable all preprocessor warnings\n\
1761 -M Generate make dependencies\n\
1762 -MM As -M, but ignore system header files\n\
1763 "), stdout);
1764 fputs (_("\
1765 -MD As -M, but put output in a .d file\n\
1766 -MMD As -MD, but ignore system header files\n\
1767 -MG Treat missing header file as generated files\n\
1768 -g3 Include #define and #undef directives in the output\n\
1769 -D<macro> Define a <macro> with string '1' as its value\n\
1770 -D<macro>=<val> Define a <macro> with <val> as its value\n\
1771 "), stdout);
1772 fputs (_("\
1773 -A<question> (<answer>) Assert the <answer> to <question>\n\
1774 -A-<question> (<answer>) Disable the <answer> to <question>\n\
1775 -U<macro> Undefine <macro> \n\
1776 -v Display the version number\n\
1777 -H Print the name of header files as they are used\n\
1778 -C Do not discard comments\n\
1779 "), stdout);
1780 fputs (_("\
1781 -dM Display a list of macro definitions active at end\n\
1782 -dD Preserve macro definitions in output\n\
1783 -dN As -dD except that only the names are preserved\n\
1784 -dI Include #include directives in the output\n\
1785 -ftabstop=<number> Distance between tab stops for column reporting\n\
1786 -P Do not generate #line directives\n\
1787 -$ Do not allow '$' in identifiers\n\
1788 "), stdout);
1789 fputs (_("\
1790 -remap Remap file names when including files.\n\
1791 --version Display version information\n\
1792 -h or --help Display this information\n\
1793 "), stdout);