* cppfiles.c (open_include_file): If open(2) returns EMFILE or
[official-gcc.git] / gcc / cppinit.c
blob0cbccaedbd8ca476c369b944a3f61af486d44762
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 "hashtab.h"
25 #include "splay-tree.h"
26 #include "cpplib.h"
27 #include "cpphash.h"
28 #include "output.h"
29 #include "prefix.h"
30 #include "intl.h"
31 #include "version.h"
32 #include "mkdeps.h"
34 /* Predefined symbols, built-in macros, and the default include path. */
36 #ifndef GET_ENV_PATH_LIST
37 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
38 #endif
40 /* Windows does not natively support inodes, and neither does MSDOS.
41 Cygwin's emulation can generate non-unique inodes, so don't use it.
42 VMS has non-numeric inodes. */
43 #ifdef VMS
44 #define INO_T_EQ(a, b) (!memcmp (&(a), &(b), sizeof (a)))
45 #elif (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
46 #define INO_T_EQ(a, b) 0
47 #else
48 #define INO_T_EQ(a, b) ((a) == (b))
49 #endif
51 #ifndef STANDARD_INCLUDE_DIR
52 #define STANDARD_INCLUDE_DIR "/usr/include"
53 #endif
55 /* We let tm.h override the types used here, to handle trivial differences
56 such as the choice of unsigned int or long unsigned int for size_t.
57 When machines start needing nontrivial differences in the size type,
58 it would be best to do something here to figure out automatically
59 from other information what type to use. */
61 /* The string value for __SIZE_TYPE__. */
63 #ifndef SIZE_TYPE
64 #define SIZE_TYPE "long unsigned int"
65 #endif
67 /* The string value for __PTRDIFF_TYPE__. */
69 #ifndef PTRDIFF_TYPE
70 #define PTRDIFF_TYPE "long int"
71 #endif
73 /* The string value for __WCHAR_TYPE__. */
75 #ifndef WCHAR_TYPE
76 #define WCHAR_TYPE "int"
77 #endif
79 /* The string value for __USER_LABEL_PREFIX__ */
81 #ifndef USER_LABEL_PREFIX
82 #define USER_LABEL_PREFIX ""
83 #endif
85 /* The string value for __REGISTER_PREFIX__ */
87 #ifndef REGISTER_PREFIX
88 #define REGISTER_PREFIX ""
89 #endif
91 /* This is the default list of directories to search for include files.
92 It may be overridden by the various -I and -ixxx options.
94 #include "file" looks in the same directory as the current file,
95 then this list.
96 #include <file> just looks in this list.
98 All these directories are treated as `system' include directories
99 (they are not subject to pedantic warnings in some cases). */
101 struct default_include
103 const char *fname; /* The name of the directory. */
104 const char *component; /* The component containing the directory
105 (see update_path in prefix.c) */
106 int cplusplus; /* Only look here if we're compiling C++. */
107 int cxx_aware; /* Includes in this directory don't need to
108 be wrapped in extern "C" when compiling
109 C++. */
112 #ifndef STANDARD_INCLUDE_COMPONENT
113 #define STANDARD_INCLUDE_COMPONENT 0
114 #endif
116 #ifdef CROSS_COMPILE
117 #undef LOCAL_INCLUDE_DIR
118 #undef SYSTEM_INCLUDE_DIR
119 #undef STANDARD_INCLUDE_DIR
120 #else
121 #undef CROSS_INCLUDE_DIR
122 #endif
124 static const struct default_include include_defaults_array[]
125 #ifdef INCLUDE_DEFAULTS
126 = INCLUDE_DEFAULTS;
127 #else
129 #ifdef GPLUSPLUS_INCLUDE_DIR
130 /* Pick up GNU C++ specific include files. */
131 { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1 },
132 #endif
133 #ifdef LOCAL_INCLUDE_DIR
134 /* /usr/local/include comes before the fixincluded header files. */
135 { LOCAL_INCLUDE_DIR, 0, 0, 1 },
136 #endif
137 #ifdef GCC_INCLUDE_DIR
138 /* This is the dir for fixincludes and for gcc's private headers. */
139 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
140 #endif
141 #ifdef CROSS_INCLUDE_DIR
142 /* One place the target system's headers might be. */
143 { CROSS_INCLUDE_DIR, "GCC", 0, 0 },
144 #endif
145 #ifdef TOOL_INCLUDE_DIR
146 /* Another place the target system's headers might be. */
147 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
148 #endif
149 #ifdef SYSTEM_INCLUDE_DIR
150 /* Some systems have an extra dir of include files. */
151 { SYSTEM_INCLUDE_DIR, 0, 0, 0 },
152 #endif
153 #ifdef STANDARD_INCLUDE_DIR
154 /* /usr/include comes dead last. */
155 { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0 },
156 #endif
157 { 0, 0, 0, 0 }
159 #endif /* no INCLUDE_DEFAULTS */
161 /* Internal structures and prototypes. */
163 /* A `struct pending_option' remembers one -D, -A, -U, -include, or -imacros
164 switch. There are four lists: one for -D and -U, one for -A, one
165 for -include, one for -imacros. `undef' is set for -U, clear for
166 -D, ignored for the others.
167 (Future: add an equivalent of -U for -A) */
169 typedef void (* cl_directive_handler) PARAMS ((cpp_reader *, const char *));
170 struct pending_option
172 struct pending_option *next;
173 const char *arg;
174 cl_directive_handler handler;
177 /* The `pending' structure accumulates all the options that are not
178 actually processed until we hit cpp_start_read. It consists of
179 several lists, one for each type of option. We keep both head and
180 tail pointers for quick insertion. */
181 struct cpp_pending
183 struct pending_option *directive_head, *directive_tail;
185 struct file_name_list *quote_head, *quote_tail;
186 struct file_name_list *brack_head, *brack_tail;
187 struct file_name_list *systm_head, *systm_tail;
188 struct file_name_list *after_head, *after_tail;
190 struct pending_option *imacros_head, *imacros_tail;
191 struct pending_option *include_head, *include_tail;
194 #ifdef __STDC__
195 #define APPEND(pend, list, elt) \
196 do { if (!(pend)->list##_head) (pend)->list##_head = (elt); \
197 else (pend)->list##_tail->next = (elt); \
198 (pend)->list##_tail = (elt); \
199 } while (0)
200 #else
201 #define APPEND(pend, list, elt) \
202 do { if (!(pend)->list/**/_head) (pend)->list/**/_head = (elt); \
203 else (pend)->list/**/_tail->next = (elt); \
204 (pend)->list/**/_tail = (elt); \
205 } while (0)
206 #endif
208 static void print_help PARAMS ((void));
209 static void path_include PARAMS ((cpp_reader *,
210 struct cpp_pending *,
211 char *, int));
212 static void initialize_builtins PARAMS ((cpp_reader *));
213 static void append_include_chain PARAMS ((cpp_reader *,
214 struct cpp_pending *,
215 char *, int, int));
216 static void merge_include_chains PARAMS ((cpp_reader *));
218 static void dump_special_to_buffer PARAMS ((cpp_reader *, const U_CHAR *,
219 size_t));
220 static void initialize_dependency_output PARAMS ((cpp_reader *));
221 static void initialize_standard_includes PARAMS ((cpp_reader *));
222 static void new_pending_directive PARAMS ((struct cpp_pending *,
223 const char *,
224 cl_directive_handler));
225 #ifdef HOST_EBCDIC
226 static int opt_comp PARAMS ((const void *, const void *));
227 #endif
228 static int parse_option PARAMS ((const char *));
229 static int handle_option PARAMS ((cpp_reader *, int, char **));
230 static int report_missing_guard PARAMS ((splay_tree_node, void *));
232 /* Fourth argument to append_include_chain: chain to use */
233 enum { QUOTE = 0, BRACKET, SYSTEM, AFTER };
235 /* If we have designated initializers (GCC >2.7) this table can be
236 initialized, constant data. Otherwise, it has to be filled in at
237 runtime. */
239 #if (GCC_VERSION >= 2007)
240 #define init_IStable() /* nothing */
241 #define ISTABLE __extension__ const unsigned char _cpp_IStable[256] = {
242 #define END };
243 #define s(p, v) [p] = v,
244 #else
245 #define ISTABLE unsigned char _cpp_IStable[256] = { 0 }; \
246 static void init_IStable PARAMS ((void)) { \
247 unsigned char *x = _cpp_IStable;
248 #define END }
249 #define s(p, v) x[p] = v;
250 #endif
252 #define A(x) s(x, ISidnum|ISidstart)
253 #define N(x) s(x, ISidnum|ISnumstart)
254 #define H(x) s(x, IShspace|ISspace)
255 #define S(x) s(x, ISspace)
257 ISTABLE
258 A('_')
260 A('a') A('b') A('c') A('d') A('e') A('f') A('g') A('h') A('i')
261 A('j') A('k') A('l') A('m') A('n') A('o') A('p') A('q') A('r')
262 A('s') A('t') A('u') A('v') A('w') A('x') A('y') A('z')
264 A('A') A('B') A('C') A('D') A('E') A('F') A('G') A('H') A('I')
265 A('J') A('K') A('L') A('M') A('N') A('O') A('P') A('Q') A('R')
266 A('S') A('T') A('U') A('V') A('W') A('X') A('Y') A('Z')
268 N('1') N('2') N('3') N('4') N('5') N('6') N('7') N('8') N('9') N('0')
270 H('\0') H(' ') H('\t') H('\v') H('\f')
272 S('\n')
275 #undef A
276 #undef N
277 #undef H
278 #undef S
279 #undef s
280 #undef ISTABLE
281 #undef END
283 /* Given a colon-separated list of file names PATH,
284 add all the names to the search path for include files. */
286 static void
287 path_include (pfile, pend, list, path)
288 cpp_reader *pfile;
289 struct cpp_pending *pend;
290 char *list;
291 int path;
293 char *p, *q, *name;
295 p = list;
299 /* Find the end of this name. */
300 q = p;
301 while (*q != 0 && *q != PATH_SEPARATOR) q++;
302 if (q == p)
304 /* An empty name in the path stands for the current directory. */
305 name = (char *) xmalloc (2);
306 name[0] = '.';
307 name[1] = 0;
309 else
311 /* Otherwise use the directory that is named. */
312 name = (char *) xmalloc (q - p + 1);
313 memcpy (name, p, q - p);
314 name[q - p] = 0;
317 append_include_chain (pfile, pend, name, path, 0);
319 /* Advance past this name. */
320 if (*q == 0)
321 break;
322 p = q + 1;
324 while (1);
327 /* Append DIR to include path PATH. DIR must be permanently allocated
328 and writable. */
329 static void
330 append_include_chain (pfile, pend, dir, path, cxx_aware)
331 cpp_reader *pfile;
332 struct cpp_pending *pend;
333 char *dir;
334 int path;
335 int cxx_aware;
337 struct file_name_list *new;
338 struct stat st;
339 unsigned int len;
341 _cpp_simplify_pathname (dir);
342 if (stat (dir, &st))
344 /* Dirs that don't exist are silently ignored. */
345 if (errno != ENOENT)
346 cpp_notice_from_errno (pfile, dir);
347 else if (CPP_OPTION (pfile, verbose))
348 fprintf (stderr, _("ignoring nonexistent directory `%s'\n"), dir);
349 return;
352 if (!S_ISDIR (st.st_mode))
354 cpp_notice (pfile, "%s: Not a directory", dir);
355 return;
358 len = strlen (dir);
359 if (len > pfile->max_include_len)
360 pfile->max_include_len = len;
362 new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
363 new->name = dir;
364 new->nlen = len;
365 new->ino = st.st_ino;
366 new->dev = st.st_dev;
367 if (path == SYSTEM)
368 new->sysp = cxx_aware ? 1 : 2;
369 else
370 new->sysp = 0;
371 new->name_map = NULL;
372 new->next = NULL;
373 new->alloc = NULL;
375 switch (path)
377 case QUOTE: APPEND (pend, quote, new); break;
378 case BRACKET: APPEND (pend, brack, new); break;
379 case SYSTEM: APPEND (pend, systm, new); break;
380 case AFTER: APPEND (pend, after, new); break;
384 /* Merge the four include chains together in the order quote, bracket,
385 system, after. Remove duplicate dirs (as determined by
386 INO_T_EQ()). The system_include and after_include chains are never
387 referred to again after this function; all access is through the
388 bracket_include path.
390 For the future: Check if the directory is empty (but
391 how?) and possibly preload the include hash. */
393 static void
394 merge_include_chains (pfile)
395 cpp_reader *pfile;
397 struct file_name_list *prev, *cur, *other;
398 struct file_name_list *quote, *brack, *systm, *after;
399 struct file_name_list *qtail, *btail, *stail, *atail;
401 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
403 qtail = pend->quote_tail;
404 btail = pend->brack_tail;
405 stail = pend->systm_tail;
406 atail = pend->after_tail;
408 quote = pend->quote_head;
409 brack = pend->brack_head;
410 systm = pend->systm_head;
411 after = pend->after_head;
413 /* Paste together bracket, system, and after include chains. */
414 if (stail)
415 stail->next = after;
416 else
417 systm = after;
418 if (btail)
419 btail->next = systm;
420 else
421 brack = systm;
423 /* This is a bit tricky.
424 First we drop dupes from the quote-include list.
425 Then we drop dupes from the bracket-include list.
426 Finally, if qtail and brack are the same directory,
427 we cut out qtail.
429 We can't just merge the lists and then uniquify them because
430 then we may lose directories from the <> search path that should
431 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
432 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
433 -Ibar -I- -Ifoo -Iquux.
435 Note that this algorithm is quadratic in the number of -I switches,
436 which is acceptable since there aren't usually that many of them. */
438 for (cur = quote, prev = NULL; cur; cur = cur->next)
440 for (other = quote; other != cur; other = other->next)
441 if (INO_T_EQ (cur->ino, other->ino)
442 && cur->dev == other->dev)
444 if (CPP_OPTION (pfile, verbose))
445 fprintf (stderr, _("ignoring duplicate directory `%s'\n"),
446 cur->name);
448 prev->next = cur->next;
449 free (cur->name);
450 free (cur);
451 cur = prev;
452 break;
454 prev = cur;
456 qtail = prev;
458 for (cur = brack; cur; cur = cur->next)
460 for (other = brack; other != cur; other = other->next)
461 if (INO_T_EQ (cur->ino, other->ino)
462 && cur->dev == other->dev)
464 if (CPP_OPTION (pfile, verbose))
465 fprintf (stderr, _("ignoring duplicate directory `%s'\n"),
466 cur->name);
468 prev->next = cur->next;
469 free (cur->name);
470 free (cur);
471 cur = prev;
472 break;
474 prev = cur;
477 if (quote)
479 if (INO_T_EQ (qtail->ino, brack->ino) && qtail->dev == brack->dev)
481 if (quote == qtail)
483 if (CPP_OPTION (pfile, verbose))
484 fprintf (stderr, _("ignoring duplicate directory `%s'\n"),
485 quote->name);
487 free (quote->name);
488 free (quote);
489 quote = brack;
491 else
493 cur = quote;
494 while (cur->next != qtail)
495 cur = cur->next;
496 cur->next = brack;
497 if (CPP_OPTION (pfile, verbose))
498 fprintf (stderr, _("ignoring duplicate directory `%s'\n"),
499 qtail->name);
501 free (qtail->name);
502 free (qtail);
505 else
506 qtail->next = brack;
508 else
509 quote = brack;
511 CPP_OPTION (pfile, quote_include) = quote;
512 CPP_OPTION (pfile, bracket_include) = brack;
516 /* Write out a #define command for the special named MACRO_NAME
517 to PFILE's token_buffer. */
519 static void
520 dump_special_to_buffer (pfile, macro_name, macro_len)
521 cpp_reader *pfile;
522 const U_CHAR *macro_name;
523 size_t macro_len;
525 static const char define_directive[] = "#define ";
526 CPP_RESERVE (pfile, sizeof(define_directive) + macro_len);
527 CPP_PUTS_Q (pfile, define_directive, sizeof(define_directive)-1);
528 CPP_PUTS_Q (pfile, macro_name, macro_len);
529 CPP_PUTC_Q (pfile, ' ');
530 _cpp_expand_to_buffer (pfile, macro_name, macro_len);
531 CPP_PUTC (pfile, '\n');
534 /* Initialize a cpp_reader structure. */
535 void
536 cpp_reader_init (pfile)
537 cpp_reader *pfile;
539 memset ((char *) pfile, 0, sizeof (cpp_reader));
541 pfile->token_buffer_size = 200;
542 pfile->token_buffer = (U_CHAR *) xmalloc (pfile->token_buffer_size);
543 CPP_SET_WRITTEN (pfile, 0);
545 CPP_OPTION (pfile, dollars_in_ident) = 1;
546 CPP_OPTION (pfile, cplusplus_comments) = 1;
547 CPP_OPTION (pfile, warn_import) = 1;
548 CPP_OPTION (pfile, discard_comments) = 1;
549 CPP_OPTION (pfile, show_column) = 1;
550 CPP_OPTION (pfile, tabstop) = 8;
552 CPP_OPTION (pfile, pending) =
553 (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
555 _cpp_init_macro_hash (pfile);
556 _cpp_init_include_table (pfile);
559 /* Initialize a cpp_printer structure. As a side effect, open the
560 output file. */
561 cpp_printer *
562 cpp_printer_init (pfile, print)
563 cpp_reader *pfile;
564 cpp_printer *print;
566 memset (print, '\0', sizeof (cpp_printer));
567 if (CPP_OPTION (pfile, out_fname) == NULL)
568 CPP_OPTION (pfile, out_fname) = "";
570 if (CPP_OPTION (pfile, out_fname)[0] == '\0')
571 print->outf = stdout;
572 else
574 print->outf = fopen (CPP_OPTION (pfile, out_fname), "w");
575 if (! print->outf)
577 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));
578 return NULL;
581 return print;
584 /* Free resources used by PFILE.
585 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
586 void
587 cpp_cleanup (pfile)
588 cpp_reader *pfile;
590 while (CPP_BUFFER (pfile) != NULL)
591 cpp_pop_buffer (pfile);
593 if (pfile->token_buffer)
595 free (pfile->token_buffer);
596 pfile->token_buffer = NULL;
599 if (pfile->input_buffer)
601 free (pfile->input_buffer);
602 pfile->input_buffer = NULL;
603 pfile->input_buffer_len = 0;
606 if (pfile->deps)
607 deps_free (pfile->deps);
609 htab_delete (pfile->hashtab);
610 splay_tree_delete (pfile->all_include_files);
614 /* This structure defines one built-in macro. A node of type TYPE will
615 be entered in the macro hash table under the name NAME, with value
616 VALUE (if any). FLAGS tweaks the behavior a little:
617 DUMP write debug info for this macro
618 VERS value is the global version_string, quoted
619 ULP value is the global user_label_prefix
622 struct builtin
624 const U_CHAR *name;
625 const U_CHAR *value;
626 unsigned short type;
627 unsigned short flags;
628 unsigned int len;
630 #define DUMP 0x01
631 #define VERS 0x02
632 #define ULP 0x04
634 #define B(n, t) { U n, 0, t, 0, sizeof n - 1 }
635 #define C(n, v) { U n, U v, T_CONST, DUMP, sizeof n - 1 }
636 #define X(n, v, t, f) { U n, U v, t, DUMP|f, sizeof n - 1 }
637 static const struct builtin builtin_array[] =
639 B("__TIME__", T_TIME),
640 B("__DATE__", T_DATE),
641 B("__FILE__", T_FILE),
642 B("__BASE_FILE__", T_BASE_FILE),
643 B("__LINE__", T_SPECLINE),
644 B("__INCLUDE_LEVEL__", T_INCLUDE_LEVEL),
646 X("__VERSION__", 0, T_XCONST, VERS),
647 X("__USER_LABEL_PREFIX__", 0, T_CONST, ULP),
648 X("__STDC__", "1", T_STDC, 0),
649 C("__REGISTER_PREFIX__", REGISTER_PREFIX),
650 C("__HAVE_BUILTIN_SETJMP__", "1"),
651 #ifndef NO_BUILTIN_SIZE_TYPE
652 C("__SIZE_TYPE__", SIZE_TYPE),
653 #endif
654 #ifndef NO_BUILTIN_PTRDIFF_TYPE
655 C("__PTRDIFF_TYPE__", PTRDIFF_TYPE),
656 #endif
657 #ifndef NO_BUILTIN_WCHAR_TYPE
658 C("__WCHAR_TYPE__", WCHAR_TYPE),
659 #endif
661 #undef B
662 #undef C
663 #undef X
664 #define builtin_array_end \
665 builtin_array + sizeof(builtin_array)/sizeof(struct builtin)
667 /* Subroutine of cpp_start_read; reads the builtins table above and
668 enters the macros into the hash table. */
669 static void
670 initialize_builtins (pfile)
671 cpp_reader *pfile;
673 const struct builtin *b;
674 const U_CHAR *val;
675 cpp_hashnode *hp;
676 for(b = builtin_array; b < builtin_array_end; b++)
678 if (b->type == T_STDC && CPP_TRADITIONAL (pfile))
679 continue;
681 if (b->flags & ULP)
682 val = (const U_CHAR *) user_label_prefix;
683 else if (b->flags & VERS)
685 val = (const U_CHAR *) xmalloc (strlen (version_string) + 3);
686 sprintf ((char *)val, "\"%s\"", version_string);
688 else
689 val = b->value;
691 hp = cpp_lookup (pfile, b->name, b->len);
692 hp->value.cpval = val;
693 hp->type = b->type;
695 if ((b->flags & DUMP) && CPP_OPTION (pfile, debug_output))
696 dump_special_to_buffer (pfile, b->name, b->len);
699 #undef DUMP
700 #undef STDC
701 #undef VERS
702 #undef ULP
704 /* Another subroutine of cpp_start_read. This one sets up to do
705 dependency-file output. */
706 static void
707 initialize_dependency_output (pfile)
708 cpp_reader *pfile;
710 char *spec, *s, *output_file;
712 /* Either of two environment variables can specify output of deps.
713 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
714 where OUTPUT_FILE is the file to write deps info to
715 and DEPS_TARGET is the target to mention in the deps. */
717 if (CPP_OPTION (pfile, print_deps) == 0)
719 spec = getenv ("DEPENDENCIES_OUTPUT");
720 if (spec)
721 CPP_OPTION (pfile, print_deps) = 1;
722 else
724 spec = getenv ("SUNPRO_DEPENDENCIES");
725 if (spec)
726 CPP_OPTION (pfile, print_deps) = 2;
727 else
728 return;
731 /* Find the space before the DEPS_TARGET, if there is one. */
732 s = strchr (spec, ' ');
733 if (s)
735 CPP_OPTION (pfile, deps_target) = s + 1;
736 output_file = (char *) xmalloc (s - spec + 1);
737 memcpy (output_file, spec, s - spec);
738 output_file[s - spec] = 0;
740 else
742 CPP_OPTION (pfile, deps_target) = 0;
743 output_file = spec;
746 CPP_OPTION (pfile, deps_file) = output_file;
747 CPP_OPTION (pfile, print_deps_append) = 1;
750 pfile->deps = deps_init ();
752 /* Print the expected object file name as the target of this Make-rule. */
753 if (CPP_OPTION (pfile, deps_target))
754 deps_add_target (pfile->deps, CPP_OPTION (pfile, deps_target));
755 else if (*CPP_OPTION (pfile, in_fname) == 0)
756 deps_add_target (pfile->deps, "-");
757 else
758 deps_calc_target (pfile->deps, CPP_OPTION (pfile, in_fname));
760 if (CPP_OPTION (pfile, in_fname))
761 deps_add_dep (pfile->deps, CPP_OPTION (pfile, in_fname));
764 /* And another subroutine. This one sets up the standard include path. */
765 static void
766 initialize_standard_includes (pfile)
767 cpp_reader *pfile;
769 char *path;
770 const struct default_include *p;
771 const char *specd_prefix = CPP_OPTION (pfile, include_prefix);
773 /* Several environment variables may add to the include search path.
774 CPATH specifies an additional list of directories to be searched
775 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
776 etc. specify an additional list of directories to be searched as
777 if specified with -isystem, for the language indicated. */
779 GET_ENV_PATH_LIST (path, "CPATH");
780 if (path != 0 && *path != 0)
781 path_include (pfile, CPP_OPTION (pfile, pending), path, BRACKET);
783 switch ((CPP_OPTION (pfile, objc) << 1) + CPP_OPTION (pfile, cplusplus))
785 case 0:
786 GET_ENV_PATH_LIST (path, "C_INCLUDE_PATH");
787 break;
788 case 1:
789 GET_ENV_PATH_LIST (path, "CPLUS_INCLUDE_PATH");
790 break;
791 case 2:
792 GET_ENV_PATH_LIST (path, "OBJC_INCLUDE_PATH");
793 break;
794 case 3:
795 GET_ENV_PATH_LIST (path, "OBJCPLUS_INCLUDE_PATH");
796 break;
798 if (path != 0 && *path != 0)
799 path_include (pfile, CPP_OPTION (pfile, pending), path, SYSTEM);
801 /* Search "translated" versions of GNU directories.
802 These have /usr/local/lib/gcc... replaced by specd_prefix. */
803 if (specd_prefix != 0)
805 char *default_prefix = (char *) alloca (sizeof GCC_INCLUDE_DIR - 7);
806 /* Remove the `include' from /usr/local/lib/gcc.../include.
807 GCC_INCLUDE_DIR will always end in /include. */
808 int default_len = sizeof GCC_INCLUDE_DIR - 8;
809 int specd_len = strlen (specd_prefix);
811 memcpy (default_prefix, GCC_INCLUDE_DIR, default_len);
812 default_prefix[default_len] = '\0';
814 for (p = include_defaults_array; p->fname; p++)
816 /* Some standard dirs are only for C++. */
817 if (!p->cplusplus
818 || (CPP_OPTION (pfile, cplusplus)
819 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
821 /* Does this dir start with the prefix? */
822 if (!strncmp (p->fname, default_prefix, default_len))
824 /* Yes; change prefix and add to search list. */
825 int flen = strlen (p->fname);
826 int this_len = specd_len + flen - default_len;
827 char *str = (char *) xmalloc (this_len + 1);
828 memcpy (str, specd_prefix, specd_len);
829 memcpy (str + specd_len,
830 p->fname + default_len,
831 flen - default_len + 1);
833 append_include_chain (pfile, CPP_OPTION (pfile, pending),
834 str, SYSTEM, p->cxx_aware);
840 /* Search ordinary names for GNU include directories. */
841 for (p = include_defaults_array; p->fname; p++)
843 /* Some standard dirs are only for C++. */
844 if (!p->cplusplus
845 || (CPP_OPTION (pfile, cplusplus)
846 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
848 /* XXX Potential memory leak! */
849 char *str = xstrdup (update_path (p->fname, p->component));
850 append_include_chain (pfile, CPP_OPTION (pfile, pending),
851 str, SYSTEM, p->cxx_aware);
856 /* This is called after options have been processed.
857 * Check options for consistency, and setup for processing input
858 * from the file named FNAME. (Use standard input if FNAME==NULL.)
859 * Return 1 on success, 0 on failure.
863 cpp_start_read (pfile, print, fname)
864 cpp_reader *pfile;
865 cpp_printer *print;
866 const char *fname;
868 struct pending_option *p, *q;
870 /* -MG doesn't select the form of output and must be specified with one of
871 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
872 inhibit compilation. */
873 if (CPP_OPTION (pfile, print_deps_missing_files)
874 && (CPP_OPTION (pfile, print_deps) == 0
875 || !CPP_OPTION (pfile, no_output)))
877 cpp_fatal (pfile, "-MG must be specified with one of -M or -MM");
878 return 0;
881 /* Chill should not be used with -trigraphs. */
882 if (CPP_OPTION (pfile, chill) && CPP_OPTION (pfile, trigraphs))
884 cpp_warning (pfile, "-lang-chill and -trigraphs are mutually exclusive");
885 CPP_OPTION (pfile, trigraphs) = 0;
888 /* -Wtraditional is not useful in C++ mode. */
889 if (CPP_OPTION (pfile, cplusplus))
890 CPP_OPTION (pfile, warn_traditional) = 0;
892 /* Set this if it hasn't been set already. */
893 if (user_label_prefix == NULL)
894 user_label_prefix = USER_LABEL_PREFIX;
896 /* Don't bother trying to do macro expansion if we've already done
897 preprocessing. */
898 if (CPP_OPTION (pfile, preprocessed))
899 pfile->no_macro_expand++;
901 /* Set up the IStable. This doesn't do anything if we were compiled
902 with a compiler that supports C99 designated initializers. */
903 init_IStable ();
905 /* Set up the tables used by read_and_prescan. */
906 _cpp_init_input_buffer (pfile);
908 /* Set up the include search path now. */
909 if (! CPP_OPTION (pfile, no_standard_includes))
910 initialize_standard_includes (pfile);
912 merge_include_chains (pfile);
914 /* With -v, print the list of dirs to search. */
915 if (CPP_OPTION (pfile, verbose))
917 struct file_name_list *l;
918 fprintf (stderr, _("#include \"...\" search starts here:\n"));
919 for (l = CPP_OPTION (pfile, quote_include); l; l = l->next)
921 if (l == CPP_OPTION (pfile, bracket_include))
922 fprintf (stderr, _("#include <...> search starts here:\n"));
923 fprintf (stderr, " %s\n", l->name);
925 fprintf (stderr, _("End of search list.\n"));
928 /* Open the main input file. This must be done early, so we have a
929 buffer to stand on. */
930 if (CPP_OPTION (pfile, in_fname) == NULL
931 || *CPP_OPTION (pfile, in_fname) == 0)
933 CPP_OPTION (pfile, in_fname) = fname;
934 if (CPP_OPTION (pfile, in_fname) == NULL)
935 CPP_OPTION (pfile, in_fname) = "";
937 if (CPP_OPTION (pfile, out_fname) == NULL)
938 CPP_OPTION (pfile, out_fname) = "";
940 if (!cpp_read_file (pfile, fname))
941 return 0;
943 initialize_dependency_output (pfile);
945 /* -D and friends may produce output, which should be identified
946 as line 0. */
948 CPP_BUFFER (pfile)->lineno = 0;
950 if (print)
952 print->lineno = 0;
953 print->last_fname = CPP_BUFFER (pfile)->nominal_fname;
954 print->last_bsd = pfile->buffer_stack_depth;
955 print->written = CPP_WRITTEN (pfile);
958 /* Install __LINE__, etc. */
959 initialize_builtins (pfile);
961 /* Do -U's, -D's and -A's in the order they were seen. */
962 p = CPP_OPTION (pfile, pending)->directive_head;
963 while (p)
965 (*p->handler) (pfile, p->arg);
966 q = p->next;
967 free (p);
968 p = q;
970 pfile->done_initializing = 1;
971 pfile->only_seen_white = 2;
972 CPP_BUFFER (pfile)->lineno = 1;
973 if (print && ! CPP_OPTION (pfile, no_output))
974 cpp_output_tokens (pfile, print);
976 /* The -imacros files can be scanned now, but the -include files
977 have to be pushed onto the include stack and processed later,
978 in the main loop calling cpp_get_token. */
980 p = CPP_OPTION (pfile, pending)->imacros_head;
981 while (p)
983 if (cpp_read_file (pfile, p->arg))
984 cpp_scan_buffer_nooutput (pfile);
985 q = p->next;
986 free (p);
987 p = q;
990 p = CPP_OPTION (pfile, pending)->include_head;
991 while (p)
993 if (cpp_read_file (pfile, p->arg)
994 && print && ! CPP_OPTION (pfile, no_output))
995 cpp_output_tokens (pfile, print);
996 q = p->next;
997 free (p);
998 p = q;
1001 free (CPP_OPTION (pfile, pending));
1002 CPP_OPTION (pfile, pending) = NULL;
1004 return 1;
1007 static int
1008 report_missing_guard (n, b)
1009 splay_tree_node n;
1010 void *b;
1012 struct include_file *f = (struct include_file *) n->value;
1013 int *bannerp = (int *)b;
1015 if (f && f->cmacro == 0 && f->include_count == 1)
1017 if (*bannerp == 0)
1019 fputs (_("Multiple include guards may be useful for:\n"), stderr);
1020 *bannerp = 1;
1022 fputs (f->name, stderr);
1023 putc ('\n', stderr);
1025 return 0;
1028 /* This is called at the end of preprocessing. It pops the
1029 last buffer and writes dependency output. It should also
1030 clear macro definitions, such that you could call cpp_start_read
1031 with a new filename to restart processing. */
1032 void
1033 cpp_finish (pfile, print)
1034 cpp_reader *pfile;
1035 cpp_printer *print;
1037 if (CPP_BUFFER (pfile))
1039 cpp_ice (pfile, "buffers still stacked in cpp_finish");
1040 while (CPP_BUFFER (pfile))
1041 cpp_pop_buffer (pfile);
1044 /* Don't write the deps file if preprocessing has failed. */
1045 if (CPP_OPTION (pfile, print_deps) && pfile->errors == 0)
1047 /* Stream on which to print the dependency information. */
1048 FILE *deps_stream = 0;
1049 const char *deps_mode
1050 = CPP_OPTION (pfile, print_deps_append) ? "a" : "w";
1051 if (CPP_OPTION (pfile, deps_file) == 0)
1052 deps_stream = stdout;
1053 else
1055 deps_stream = fopen (CPP_OPTION (pfile, deps_file), deps_mode);
1056 if (deps_stream == 0)
1057 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, deps_file));
1059 if (deps_stream)
1061 deps_write (pfile->deps, deps_stream, 72);
1062 if (CPP_OPTION (pfile, deps_file))
1064 if (ferror (deps_stream) || fclose (deps_stream) != 0)
1065 cpp_fatal (pfile, "I/O error on output");
1070 if (CPP_OPTION (pfile, dump_macros) == dump_only)
1071 _cpp_dump_macro_hash (pfile);
1073 /* Flush any pending output. */
1074 if (print)
1076 cpp_output_tokens (pfile, print);
1077 if (ferror (print->outf) || fclose (print->outf))
1078 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));
1081 /* Report on headers that could use multiple include guards. */
1082 if (CPP_OPTION (pfile, print_include_names))
1084 int banner = 0;
1085 splay_tree_foreach (pfile->all_include_files, report_missing_guard,
1086 (void *) &banner);
1090 static void
1091 new_pending_directive (pend, text, handler)
1092 struct cpp_pending *pend;
1093 const char *text;
1094 cl_directive_handler handler;
1096 struct pending_option *o = (struct pending_option *)
1097 xmalloc (sizeof (struct pending_option));
1099 o->arg = text;
1100 o->next = NULL;
1101 o->handler = handler;
1102 APPEND (pend, directive, o);
1105 /* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
1106 I.e. a const string initializer with parens around it. That is
1107 what N_("string") resolves to, so we make no_* be macros instead. */
1108 #define no_arg N_("Argument missing after %s")
1109 #define no_ass N_("Assertion missing after %s")
1110 #define no_dir N_("Directory name missing after %s")
1111 #define no_fil N_("File name missing after %s")
1112 #define no_mac N_("Macro name missing after %s")
1113 #define no_pth N_("Path name missing after %s")
1114 #define no_num N_("Number missing after %s")
1116 /* This is the list of all command line options, with the leading
1117 "-" removed. It must be sorted in ASCII collating order. */
1118 #define COMMAND_LINE_OPTIONS \
1119 DEF_OPT("", 0, OPT_stdin_stdout) \
1120 DEF_OPT("$", 0, OPT_dollar) \
1121 DEF_OPT("+", 0, OPT_plus) \
1122 DEF_OPT("-help", 0, OPT__help) \
1123 DEF_OPT("-version", 0, OPT__version) \
1124 DEF_OPT("A", no_ass, OPT_A) \
1125 DEF_OPT("C", 0, OPT_C) \
1126 DEF_OPT("D", no_mac, OPT_D) \
1127 DEF_OPT("H", 0, OPT_H) \
1128 DEF_OPT("I", no_dir, OPT_I) \
1129 DEF_OPT("M", 0, OPT_M) \
1130 DEF_OPT("MD", no_fil, OPT_MD) \
1131 DEF_OPT("MG", 0, OPT_MG) \
1132 DEF_OPT("MM", 0, OPT_MM) \
1133 DEF_OPT("MMD", no_fil, OPT_MMD) \
1134 DEF_OPT("P", 0, OPT_P) \
1135 DEF_OPT("U", no_mac, OPT_U) \
1136 DEF_OPT("W", no_arg, OPT_W) /* arg optional */ \
1137 DEF_OPT("d", no_arg, OPT_d) \
1138 DEF_OPT("fleading-underscore", 0, OPT_fleading_underscore) \
1139 DEF_OPT("fno-leading-underscore", 0, OPT_fno_leading_underscore) \
1140 DEF_OPT("fno-preprocessed", 0, OPT_fno_preprocessed) \
1141 DEF_OPT("fno-show-column", 0, OPT_fno_show_column) \
1142 DEF_OPT("fpreprocessed", 0, OPT_fpreprocessed) \
1143 DEF_OPT("fshow-column", 0, OPT_fshow_column) \
1144 DEF_OPT("ftabstop=", no_num, OPT_ftabstop) \
1145 DEF_OPT("g", no_arg, OPT_g) /* arg optional */ \
1146 DEF_OPT("h", 0, OPT_h) \
1147 DEF_OPT("idirafter", no_dir, OPT_idirafter) \
1148 DEF_OPT("imacros", no_fil, OPT_imacros) \
1149 DEF_OPT("include", no_fil, OPT_include) \
1150 DEF_OPT("iprefix", no_pth, OPT_iprefix) \
1151 DEF_OPT("isystem", no_dir, OPT_isystem) \
1152 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix) \
1153 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore) \
1154 DEF_OPT("lang-asm", 0, OPT_lang_asm) \
1155 DEF_OPT("lang-c", 0, OPT_lang_c) \
1156 DEF_OPT("lang-c++", 0, OPT_lang_cplusplus) \
1157 DEF_OPT("lang-c89", 0, OPT_lang_c89) \
1158 DEF_OPT("lang-chill", 0, OPT_lang_chill) \
1159 DEF_OPT("lang-fortran", 0, OPT_lang_fortran) \
1160 DEF_OPT("lang-objc", 0, OPT_lang_objc) \
1161 DEF_OPT("lang-objc++", 0, OPT_lang_objcplusplus) \
1162 DEF_OPT("nostdinc", 0, OPT_nostdinc) \
1163 DEF_OPT("nostdinc++", 0, OPT_nostdincplusplus) \
1164 DEF_OPT("o", no_fil, OPT_o) \
1165 DEF_OPT("pedantic", 0, OPT_pedantic) \
1166 DEF_OPT("pedantic-errors", 0, OPT_pedantic_errors) \
1167 DEF_OPT("remap", 0, OPT_remap) \
1168 DEF_OPT("std=c89", 0, OPT_std_c89) \
1169 DEF_OPT("std=c99", 0, OPT_std_c99) \
1170 DEF_OPT("std=c9x", 0, OPT_std_c9x) \
1171 DEF_OPT("std=gnu89", 0, OPT_std_gnu89) \
1172 DEF_OPT("std=gnu99", 0, OPT_std_gnu99) \
1173 DEF_OPT("std=gnu9x", 0, OPT_std_gnu9x) \
1174 DEF_OPT("std=iso9899:1990", 0, OPT_std_iso9899_1990) \
1175 DEF_OPT("std=iso9899:199409", 0, OPT_std_iso9899_199409) \
1176 DEF_OPT("std=iso9899:1999", 0, OPT_std_iso9899_1999) \
1177 DEF_OPT("std=iso9899:199x", 0, OPT_std_iso9899_199x) \
1178 DEF_OPT("traditional", 0, OPT_traditional) \
1179 DEF_OPT("trigraphs", 0, OPT_trigraphs) \
1180 DEF_OPT("v", 0, OPT_v) \
1181 DEF_OPT("w", 0, OPT_w)
1183 #define DEF_OPT(text, msg, code) code,
1184 enum opt_code
1186 COMMAND_LINE_OPTIONS
1187 N_OPTS
1189 #undef DEF_OPT
1191 struct cl_option
1193 const char *opt_text;
1194 const char *msg;
1195 size_t opt_len;
1196 enum opt_code opt_code;
1199 #define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
1200 #ifdef HOST_EBCDIC
1201 static struct cl_option cl_options[] =
1202 #else
1203 static const struct cl_option cl_options[] =
1204 #endif
1206 COMMAND_LINE_OPTIONS
1208 #undef DEF_OPT
1209 #undef COMMAND_LINE_OPTIONS
1211 /* Perform a binary search to find which, if any, option the given
1212 command-line matches. Returns its index in the option array,
1213 negative on failure. Complications arise since some options can be
1214 suffixed with an argument, and multiple complete matches can occur,
1215 e.g. -iwithprefix and -iwithprefixbefore. Moreover, we want to
1216 accept options beginning with -g and -W that we do not recognise,
1217 but not to swallow any subsequent command line argument; these are
1218 handled as special cases in cpp_handle_option */
1219 static int
1220 parse_option (input)
1221 const char *input;
1223 unsigned int md, mn, mx;
1224 size_t opt_len;
1225 int comp;
1227 mn = 0;
1228 mx = N_OPTS;
1230 while (mx > mn)
1232 md = (mn + mx) / 2;
1234 opt_len = cl_options[md].opt_len;
1235 comp = strncmp (input, cl_options[md].opt_text, opt_len);
1237 if (comp > 0)
1238 mn = md + 1;
1239 else if (comp < 0)
1240 mx = md;
1241 else
1243 if (input[opt_len] == '\0')
1244 return md;
1245 /* We were passed more text. If the option takes an argument,
1246 we may match a later option or we may have been passed the
1247 argument. The longest possible option match succeeds.
1248 If the option takes no arguments we have not matched and
1249 continue the search (e.g. input="stdc++" match was "stdc") */
1250 mn = md + 1;
1251 if (cl_options[md].msg)
1253 /* Scan forwards. If we get an exact match, return it.
1254 Otherwise, return the longest option-accepting match.
1255 This loops no more than twice with current options */
1256 mx = md;
1257 for (; mn < N_OPTS; mn++)
1259 opt_len = cl_options[mn].opt_len;
1260 if (strncmp (input, cl_options[mn].opt_text, opt_len))
1261 break;
1262 if (input[opt_len] == '\0')
1263 return mn;
1264 if (cl_options[mn].msg)
1265 mx = mn;
1267 return mx;
1272 return -1;
1275 /* Handle one command-line option in (argc, argv).
1276 Can be called multiple times, to handle multiple sets of options.
1277 Returns number of strings consumed. */
1279 static int
1280 handle_option (pfile, argc, argv)
1281 cpp_reader *pfile;
1282 int argc;
1283 char **argv;
1285 int i = 0;
1286 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
1288 if (argv[i][0] != '-')
1290 if (CPP_OPTION (pfile, out_fname) != NULL)
1291 cpp_fatal (pfile, "Too many arguments. Type %s --help for usage info",
1292 progname);
1293 else if (CPP_OPTION (pfile, in_fname) != NULL)
1294 CPP_OPTION (pfile, out_fname) = argv[i];
1295 else
1296 CPP_OPTION (pfile, in_fname) = argv[i];
1298 else
1300 enum opt_code opt_code;
1301 int opt_index;
1302 const char *arg = 0;
1304 /* Skip over '-' */
1305 opt_index = parse_option (&argv[i][1]);
1306 if (opt_index < 0)
1307 return i;
1309 opt_code = cl_options[opt_index].opt_code;
1310 if (cl_options[opt_index].msg)
1312 arg = &argv[i][cl_options[opt_index].opt_len + 1];
1314 /* Yuk. Special case for -g and -W as they must not swallow
1315 up any following argument. If this becomes common, add
1316 another field to the cl_options table */
1317 if (arg[0] == '\0' && !(opt_code == OPT_g || opt_code == OPT_W))
1319 arg = argv[++i];
1320 if (!arg)
1322 cpp_fatal (pfile, cl_options[opt_index].msg, argv[i - 1]);
1323 return argc;
1328 switch (opt_code)
1330 case N_OPTS: /* shut GCC up */
1331 break;
1332 case OPT_fleading_underscore:
1333 user_label_prefix = "_";
1334 break;
1335 case OPT_fno_leading_underscore:
1336 user_label_prefix = "";
1337 break;
1338 case OPT_fpreprocessed:
1339 CPP_OPTION (pfile, preprocessed) = 1;
1340 break;
1341 case OPT_fno_preprocessed:
1342 CPP_OPTION (pfile, preprocessed) = 0;
1343 break;
1344 case OPT_fshow_column:
1345 CPP_OPTION (pfile, show_column) = 1;
1346 break;
1347 case OPT_fno_show_column:
1348 CPP_OPTION (pfile, show_column) = 0;
1349 break;
1350 case OPT_ftabstop:
1351 /* Silently ignore empty string, non-longs and silly values. */
1352 if (arg[0] != '\0')
1354 char *endptr;
1355 long tabstop = strtol (arg, &endptr, 10);
1356 if (*endptr == '\0' && tabstop >= 1 && tabstop <= 100)
1357 CPP_OPTION (pfile, tabstop) = tabstop;
1359 break;
1360 case OPT_w:
1361 CPP_OPTION (pfile, inhibit_warnings) = 1;
1362 break;
1363 case OPT_g: /* Silently ignore anything but -g3 */
1364 if (!strcmp(&argv[i][2], "3"))
1365 CPP_OPTION (pfile, debug_output) = 1;
1366 break;
1367 case OPT_h:
1368 case OPT__help:
1369 print_help ();
1370 exit (0); /* XXX */
1371 break;
1372 case OPT__version:
1373 fprintf (stderr, _("GNU CPP version %s (cpplib)\n"), version_string);
1374 exit (0); /* XXX */
1375 break;
1376 case OPT_C:
1377 CPP_OPTION (pfile, discard_comments) = 0;
1378 break;
1379 case OPT_P:
1380 CPP_OPTION (pfile, no_line_commands) = 1;
1381 break;
1382 case OPT_dollar: /* Don't include $ in identifiers. */
1383 CPP_OPTION (pfile, dollars_in_ident) = 0;
1384 break;
1385 case OPT_H:
1386 CPP_OPTION (pfile, print_include_names) = 1;
1387 break;
1388 case OPT_D:
1389 new_pending_directive (pend, arg, cpp_define);
1390 break;
1391 case OPT_pedantic_errors:
1392 CPP_OPTION (pfile, pedantic_errors) = 1;
1393 /* fall through */
1394 case OPT_pedantic:
1395 CPP_OPTION (pfile, pedantic) = 1;
1396 break;
1397 case OPT_traditional:
1398 CPP_OPTION (pfile, traditional) = 1;
1399 CPP_OPTION (pfile, cplusplus_comments) = 0;
1400 CPP_OPTION (pfile, trigraphs) = 0;
1401 CPP_OPTION (pfile, warn_trigraphs) = 0;
1402 break;
1403 case OPT_trigraphs:
1404 CPP_OPTION (pfile, trigraphs) = 1;
1405 break;
1406 case OPT_plus:
1407 CPP_OPTION (pfile, cplusplus) = 1;
1408 CPP_OPTION (pfile, cplusplus_comments) = 1;
1409 break;
1410 case OPT_remap:
1411 CPP_OPTION (pfile, remap) = 1;
1412 break;
1413 case OPT_iprefix:
1414 CPP_OPTION (pfile, include_prefix) = arg;
1415 CPP_OPTION (pfile, include_prefix_len) = strlen (arg);
1416 break;
1417 case OPT_lang_c:
1418 CPP_OPTION (pfile, cplusplus) = 0;
1419 CPP_OPTION (pfile, cplusplus_comments) = 1;
1420 CPP_OPTION (pfile, c89) = 0;
1421 CPP_OPTION (pfile, c99) = 1;
1422 CPP_OPTION (pfile, objc) = 0;
1423 break;
1424 case OPT_lang_c89:
1425 CPP_OPTION (pfile, cplusplus) = 0;
1426 CPP_OPTION (pfile, cplusplus_comments) = 0;
1427 CPP_OPTION (pfile, c89) = 1;
1428 CPP_OPTION (pfile, c99) = 0;
1429 CPP_OPTION (pfile, objc) = 0;
1430 CPP_OPTION (pfile, trigraphs) = 1;
1431 new_pending_directive (pend, "__STRICT_ANSI__", cpp_define);
1432 break;
1433 case OPT_lang_cplusplus:
1434 CPP_OPTION (pfile, cplusplus) = 1;
1435 CPP_OPTION (pfile, cplusplus_comments) = 1;
1436 CPP_OPTION (pfile, c89) = 0;
1437 CPP_OPTION (pfile, c99) = 0;
1438 CPP_OPTION (pfile, objc) = 0;
1439 new_pending_directive (pend, "__cplusplus", cpp_define);
1440 break;
1441 case OPT_lang_objcplusplus:
1442 CPP_OPTION (pfile, cplusplus) = 1;
1443 new_pending_directive (pend, "__cplusplus", cpp_define);
1444 /* fall through */
1445 case OPT_lang_objc:
1446 CPP_OPTION (pfile, cplusplus_comments) = 1;
1447 CPP_OPTION (pfile, c89) = 0;
1448 CPP_OPTION (pfile, c99) = 0;
1449 CPP_OPTION (pfile, objc) = 1;
1450 new_pending_directive (pend, "__OBJC__", cpp_define);
1451 break;
1452 case OPT_lang_asm:
1453 CPP_OPTION (pfile, lang_asm) = 1;
1454 CPP_OPTION (pfile, dollars_in_ident) = 0;
1455 new_pending_directive (pend, "__ASSEMBLER__", cpp_define);
1456 break;
1457 case OPT_lang_fortran:
1458 CPP_OPTION (pfile, lang_fortran) = 1;
1459 CPP_OPTION (pfile, traditional) = 1;
1460 CPP_OPTION (pfile, cplusplus_comments) = 0;
1461 new_pending_directive (pend, "_LANGUAGE_FORTRAN", cpp_define);
1462 break;
1463 case OPT_lang_chill:
1464 CPP_OPTION (pfile, objc) = 0;
1465 CPP_OPTION (pfile, cplusplus) = 0;
1466 CPP_OPTION (pfile, chill) = 1;
1467 CPP_OPTION (pfile, traditional) = 1;
1468 break;
1469 case OPT_nostdinc:
1470 /* -nostdinc causes no default include directories.
1471 You must specify all include-file directories with -I. */
1472 CPP_OPTION (pfile, no_standard_includes) = 1;
1473 break;
1474 case OPT_nostdincplusplus:
1475 /* -nostdinc++ causes no default C++-specific include directories. */
1476 CPP_OPTION (pfile, no_standard_cplusplus_includes) = 1;
1477 break;
1478 case OPT_std_gnu89:
1479 CPP_OPTION (pfile, cplusplus) = 0;
1480 CPP_OPTION (pfile, cplusplus_comments) = 1;
1481 CPP_OPTION (pfile, c89) = 1;
1482 CPP_OPTION (pfile, c99) = 0;
1483 CPP_OPTION (pfile, objc) = 0;
1484 break;
1485 case OPT_std_gnu9x:
1486 case OPT_std_gnu99:
1487 CPP_OPTION (pfile, cplusplus) = 0;
1488 CPP_OPTION (pfile, cplusplus_comments) = 1;
1489 CPP_OPTION (pfile, c89) = 0;
1490 CPP_OPTION (pfile, c99) = 1;
1491 CPP_OPTION (pfile, objc) = 0;
1492 new_pending_directive (CPP_OPTION (pfile, pending),
1493 "__STDC_VERSION__=199901L", cpp_define);
1494 break;
1495 case OPT_std_iso9899_199409:
1496 new_pending_directive (CPP_OPTION (pfile, pending),
1497 "__STDC_VERSION__=199409L", cpp_define);
1498 /* Fall through */
1499 case OPT_std_iso9899_1990:
1500 case OPT_std_c89:
1501 CPP_OPTION (pfile, cplusplus) = 0;
1502 CPP_OPTION (pfile, cplusplus_comments) = 0;
1503 CPP_OPTION (pfile, c89) = 1;
1504 CPP_OPTION (pfile, c99) = 0;
1505 CPP_OPTION (pfile, objc) = 0;
1506 CPP_OPTION (pfile, trigraphs) = 1;
1507 new_pending_directive (CPP_OPTION (pfile, pending),
1508 "__STRICT_ANSI__", cpp_define);
1509 break;
1510 case OPT_std_iso9899_199x:
1511 case OPT_std_iso9899_1999:
1512 case OPT_std_c9x:
1513 case OPT_std_c99:
1514 CPP_OPTION (pfile, cplusplus) = 0;
1515 CPP_OPTION (pfile, cplusplus_comments) = 1;
1516 CPP_OPTION (pfile, c89) = 0;
1517 CPP_OPTION (pfile, c99) = 1;
1518 CPP_OPTION (pfile, objc) = 0;
1519 CPP_OPTION (pfile, trigraphs) = 1;
1520 new_pending_directive (CPP_OPTION (pfile, pending),
1521 "__STRICT_ANSI__", cpp_define);
1522 new_pending_directive (CPP_OPTION (pfile, pending),
1523 "__STDC_VERSION__=199901L", cpp_define);
1524 break;
1525 case OPT_o:
1526 if (CPP_OPTION (pfile, out_fname) != NULL)
1528 cpp_fatal (pfile, "Output filename specified twice");
1529 return argc;
1531 CPP_OPTION (pfile, out_fname) = arg;
1532 if (!strcmp (CPP_OPTION (pfile, out_fname), "-"))
1533 CPP_OPTION (pfile, out_fname) = "";
1534 break;
1535 case OPT_v:
1536 fprintf (stderr, _("GNU CPP version %s (cpplib)\n"), version_string);
1537 #ifdef TARGET_VERSION
1538 TARGET_VERSION;
1539 #endif
1540 fputc ('\n', stderr);
1541 CPP_OPTION (pfile, verbose) = 1;
1542 break;
1543 case OPT_stdin_stdout:
1544 /* JF handle '-' as file name meaning stdin or stdout */
1545 if (CPP_OPTION (pfile, in_fname) == NULL)
1546 CPP_OPTION (pfile, in_fname) = "";
1547 else if (CPP_OPTION (pfile, out_fname) == NULL)
1548 CPP_OPTION (pfile, out_fname) = "";
1549 break;
1550 case OPT_d:
1551 /* Args to -d specify what parts of macros to dump.
1552 Silently ignore unrecognised options; they may
1553 be aimed at the compiler proper. */
1555 char c;
1557 while ((c = *arg++) != '\0')
1558 switch (c)
1560 case 'M':
1561 CPP_OPTION (pfile, dump_macros) = dump_only;
1562 CPP_OPTION (pfile, no_output) = 1;
1563 break;
1564 case 'N':
1565 CPP_OPTION (pfile, dump_macros) = dump_names;
1566 break;
1567 case 'D':
1568 CPP_OPTION (pfile, dump_macros) = dump_definitions;
1569 break;
1570 case 'I':
1571 CPP_OPTION (pfile, dump_includes) = 1;
1572 break;
1575 break;
1576 /* The style of the choices here is a bit mixed.
1577 The chosen scheme is a hybrid of keeping all options in one string
1578 and specifying each option in a separate argument:
1579 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
1580 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1581 -M[M][G][D file]. This is awkward to handle in specs, and is not
1582 as extensible. */
1583 /* ??? -MG must be specified in addition to one of -M or -MM.
1584 This can be relaxed in the future without breaking anything.
1585 The converse isn't true. */
1587 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
1588 case OPT_MG:
1589 CPP_OPTION (pfile, print_deps_missing_files) = 1;
1590 break;
1591 case OPT_M:
1592 case OPT_MD:
1593 case OPT_MM:
1594 case OPT_MMD:
1595 if (opt_code == OPT_M || opt_code == OPT_MD)
1596 CPP_OPTION (pfile, print_deps) = 2;
1597 else
1598 CPP_OPTION (pfile, print_deps) = 1;
1600 /* For -MD and -MMD options, write deps on file named by next arg */
1601 /* For -M and -MM, write deps on standard output
1602 and suppress the usual output. */
1603 if (opt_code == OPT_MD || opt_code == OPT_MMD)
1604 CPP_OPTION (pfile, deps_file) = arg;
1605 else
1606 CPP_OPTION (pfile, no_output) = 1;
1607 break;
1608 case OPT_A:
1609 if (arg[0] == '-')
1611 /* -A with an argument beginning with '-' acts as
1612 #unassert on whatever immediately follows the '-'.
1613 If "-" is the whole argument, we eliminate all
1614 predefined macros and assertions, including those
1615 that were specified earlier on the command line.
1616 That way we can get rid of any that were passed
1617 automatically in from GCC. */
1619 if (arg[1] == '\0')
1621 struct pending_option *o1, *o2;
1623 o1 = CPP_OPTION (pfile, pending)->directive_head;
1624 while (o1)
1626 o2 = o1->next;
1627 free (o1);
1628 o1 = o2;
1630 CPP_OPTION (pfile, pending)->directive_head = NULL;
1631 CPP_OPTION (pfile, pending)->directive_tail = NULL;
1633 else
1634 new_pending_directive (CPP_OPTION (pfile, pending),
1635 arg + 1, cpp_unassert);
1637 else
1638 new_pending_directive (CPP_OPTION (pfile, pending),
1639 arg, cpp_assert);
1640 break;
1641 case OPT_U:
1642 new_pending_directive (CPP_OPTION (pfile, pending), arg, cpp_undef);
1643 break;
1644 case OPT_I: /* Add directory to path for includes. */
1645 if (!strcmp (arg, "-"))
1647 /* -I- means:
1648 Use the preceding -I directories for #include "..."
1649 but not #include <...>.
1650 Don't search the directory of the present file
1651 for #include "...". (Note that -I. -I- is not the same as
1652 the default setup; -I. uses the compiler's working dir.) */
1653 if (! CPP_OPTION (pfile, ignore_srcdir))
1655 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
1656 pend->quote_head = pend->brack_head;
1657 pend->quote_tail = pend->brack_tail;
1658 pend->brack_head = 0;
1659 pend->brack_tail = 0;
1660 CPP_OPTION (pfile, ignore_srcdir) = 1;
1662 else
1664 cpp_fatal (pfile, "-I- specified twice");
1665 return argc;
1668 else
1669 append_include_chain (pfile, CPP_OPTION (pfile, pending),
1670 xstrdup (arg), BRACKET, 0);
1671 break;
1672 case OPT_isystem:
1673 /* Add directory to beginning of system include path, as a system
1674 include directory. */
1675 append_include_chain (pfile, CPP_OPTION (pfile, pending),
1676 xstrdup (arg), SYSTEM, 0);
1677 break;
1678 case OPT_include:
1680 struct pending_option *o = (struct pending_option *)
1681 xmalloc (sizeof (struct pending_option));
1682 o->arg = arg;
1684 /* This list has to be built in reverse order so that
1685 when cpp_start_read pushes all the -include files onto
1686 the buffer stack, they will be scanned in forward order. */
1687 o->next = CPP_OPTION (pfile, pending)->include_head;
1688 CPP_OPTION (pfile, pending)->include_head = o;
1690 break;
1691 case OPT_imacros:
1693 struct pending_option *o = (struct pending_option *)
1694 xmalloc (sizeof (struct pending_option));
1695 o->arg = arg;
1696 o->next = NULL;
1698 APPEND (CPP_OPTION (pfile, pending), imacros, o);
1700 break;
1701 case OPT_iwithprefix:
1702 /* Add directory to end of path for includes,
1703 with the default prefix at the front of its name. */
1704 /* fall through */
1705 case OPT_iwithprefixbefore:
1706 /* Add directory to main path for includes,
1707 with the default prefix at the front of its name. */
1709 char *fname;
1710 int len;
1712 len = strlen (arg);
1714 if (CPP_OPTION (pfile, include_prefix) != 0)
1716 size_t ipl = CPP_OPTION (pfile, include_prefix_len);
1717 fname = xmalloc (ipl + len + 1);
1718 memcpy (fname, CPP_OPTION (pfile, include_prefix), ipl);
1719 memcpy (fname + ipl, arg, len + 1);
1721 else
1723 fname = xmalloc (sizeof GCC_INCLUDE_DIR - 8 + len);
1724 memcpy (fname, GCC_INCLUDE_DIR, sizeof GCC_INCLUDE_DIR - 9);
1725 memcpy (fname + sizeof GCC_INCLUDE_DIR - 9, arg, len + 1);
1728 append_include_chain (pfile, CPP_OPTION (pfile, pending), fname,
1729 opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0);
1731 break;
1732 case OPT_idirafter:
1733 /* Add directory to end of path for includes. */
1734 append_include_chain (pfile, CPP_OPTION (pfile, pending),
1735 xstrdup (arg), AFTER, 0);
1736 break;
1737 case OPT_W:
1738 /* Silently ignore unrecognised options */
1739 if (!strcmp (argv[i], "-Wall"))
1741 CPP_OPTION (pfile, warn_trigraphs) = 1;
1742 CPP_OPTION (pfile, warn_comments) = 1;
1744 else if (!strcmp (argv[i], "-Wtraditional"))
1745 CPP_OPTION (pfile, warn_traditional) = 1;
1746 else if (!strcmp (argv[i], "-Wtrigraphs"))
1747 CPP_OPTION (pfile, warn_trigraphs) = 1;
1748 else if (!strcmp (argv[i], "-Wcomment"))
1749 CPP_OPTION (pfile, warn_comments) = 1;
1750 else if (!strcmp (argv[i], "-Wcomments"))
1751 CPP_OPTION (pfile, warn_comments) = 1;
1752 else if (!strcmp (argv[i], "-Wundef"))
1753 CPP_OPTION (pfile, warn_undef) = 1;
1754 else if (!strcmp (argv[i], "-Wimport"))
1755 CPP_OPTION (pfile, warn_import) = 1;
1756 else if (!strcmp (argv[i], "-Werror"))
1757 CPP_OPTION (pfile, warnings_are_errors) = 1;
1758 else if (!strcmp (argv[i], "-Wno-traditional"))
1759 CPP_OPTION (pfile, warn_traditional) = 0;
1760 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1761 CPP_OPTION (pfile, warn_trigraphs) = 0;
1762 else if (!strcmp (argv[i], "-Wno-comment"))
1763 CPP_OPTION (pfile, warn_comments) = 0;
1764 else if (!strcmp (argv[i], "-Wno-comments"))
1765 CPP_OPTION (pfile, warn_comments) = 0;
1766 else if (!strcmp (argv[i], "-Wno-undef"))
1767 CPP_OPTION (pfile, warn_undef) = 0;
1768 else if (!strcmp (argv[i], "-Wno-import"))
1769 CPP_OPTION (pfile, warn_import) = 0;
1770 else if (!strcmp (argv[i], "-Wno-error"))
1771 CPP_OPTION (pfile, warnings_are_errors) = 0;
1772 break;
1775 return i + 1;
1778 #ifdef HOST_EBCDIC
1779 static int
1780 opt_comp (const void *p1, const void *p2)
1782 return strcmp (((struct cl_option *)p1)->opt_text,
1783 ((struct cl_option *)p2)->opt_text);
1785 #endif
1787 /* Handle command-line options in (argc, argv).
1788 Can be called multiple times, to handle multiple sets of options.
1789 Returns if an unrecognized option is seen.
1790 Returns number of strings consumed. */
1792 cpp_handle_options (pfile, argc, argv)
1793 cpp_reader *pfile;
1794 int argc;
1795 char **argv;
1797 int i;
1798 int strings_processed;
1800 #ifdef HOST_EBCDIC
1801 static int opts_sorted = 0;
1803 if (!opts_sorted)
1805 opts_sorted = 1;
1806 /* For non-ASCII hosts, the array needs to be sorted at runtime */
1807 qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
1809 #endif
1811 for (i = 0; i < argc; i += strings_processed)
1813 strings_processed = handle_option (pfile, argc - i, argv + i);
1814 if (strings_processed == 0)
1815 break;
1817 return i;
1820 static void
1821 print_help ()
1823 fprintf (stderr, _("Usage: %s [switches] input output\n"), progname);
1824 fputs (_("\
1825 Switches:\n\
1826 -include <file> Include the contents of <file> before other files\n\
1827 -imacros <file> Accept definition of macros in <file>\n\
1828 -iprefix <path> Specify <path> as a prefix for next two options\n\
1829 -iwithprefix <dir> Add <dir> to the end of the system include path\n\
1830 -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\
1831 -isystem <dir> Add <dir> to the start of the system include path\n\
1832 -idirafter <dir> Add <dir> to the end of the system include path\n\
1833 -I <dir> Add <dir> to the end of the main include path\n\
1834 -I- Fine-grained include path control; see info docs\n\
1835 -nostdinc Do not search system include directories\n\
1836 (dirs specified with -isystem will still be used)\n\
1837 -nostdinc++ Do not search system include directories for C++\n\
1838 -o <file> Put output into <file>\n\
1839 -pedantic Issue all warnings demanded by strict ANSI C\n\
1840 -pedantic-errors Issue -pedantic warnings as errors instead\n\
1841 -traditional Follow K&R pre-processor behaviour\n\
1842 -trigraphs Support ANSI C trigraphs\n\
1843 -lang-c Assume that the input sources are in C\n\
1844 -lang-c89 Assume that the input sources are in C89\n\
1845 -lang-c++ Assume that the input sources are in C++\n\
1846 -lang-objc Assume that the input sources are in ObjectiveC\n\
1847 -lang-objc++ Assume that the input sources are in ObjectiveC++\n\
1848 -lang-asm Assume that the input sources are in assembler\n\
1849 -lang-fortran Assume that the input sources are in Fortran\n\
1850 -lang-chill Assume that the input sources are in Chill\n\
1851 -std=<std name> Specify the conformance standard; one of:\n\
1852 gnu89, gnu99, c89, c99, iso9899:1990,\n\
1853 iso9899:199409, iso9899:1999\n\
1854 -+ Allow parsing of C++ style features\n\
1855 -w Inhibit warning messages\n\
1856 -Wtrigraphs Warn if trigraphs are encountered\n\
1857 -Wno-trigraphs Do not warn about trigraphs\n\
1858 -Wcomment{s} Warn if one comment starts inside another\n\
1859 -Wno-comment{s} Do not warn about comments\n\
1860 -Wtraditional Warn if a macro argument is/would be turned into\n\
1861 a string if -traditional is specified\n\
1862 -Wno-traditional Do not warn about stringification\n\
1863 -Wundef Warn if an undefined macro is used by #if\n\
1864 -Wno-undef Do not warn about testing undefined macros\n\
1865 -Wimport Warn about the use of the #import directive\n\
1866 -Wno-import Do not warn about the use of #import\n\
1867 -Werror Treat all warnings as errors\n\
1868 -Wno-error Do not treat warnings as errors\n\
1869 -Wall Enable all preprocessor warnings\n\
1870 -M Generate make dependencies\n\
1871 -MM As -M, but ignore system header files\n\
1872 -MD As -M, but put output in a .d file\n\
1873 -MMD As -MD, but ignore system header files\n\
1874 -MG Treat missing header file as generated files\n\
1875 -g3 Include #define and #undef directives in the output\n\
1876 -D<macro> Define a <macro> with string '1' as its value\n\
1877 -D<macro>=<val> Define a <macro> with <val> as its value\n\
1878 -A<question> (<answer>) Assert the <answer> to <question>\n\
1879 -A-<question> (<answer>) Disable the <answer> to <question>\n\
1880 -U<macro> Undefine <macro> \n\
1881 -v Display the version number\n\
1882 -H Print the name of header files as they are used\n\
1883 -C Do not discard comments\n\
1884 -dM Display a list of macro definitions active at end\n\
1885 -dD Preserve macro definitions in output\n\
1886 -dN As -dD except that only the names are preserved\n\
1887 -dI Include #include directives in the output\n\
1888 -ftabstop=<number> Distance between tab stops for column reporting\n\
1889 -P Do not generate #line directives\n\
1890 -$ Do not allow '$' in identifiers\n\
1891 -remap Remap file names when including files.\n\
1892 --version Display version information\n\
1893 -h or --help Display this information\n\
1894 "), stdout);