cppinit.c (cpp_start_read): Call initialize_dependency_output only after reading...
[gcc.git] / gcc / cppinit.c
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
7
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.
12
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.
17
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. */
21
22 #include "config.h"
23 #include "system.h"
24
25 #include "cpplib.h"
26 #include "cpphash.h"
27 #include "output.h"
28 #include "prefix.h"
29 #include "intl.h"
30 #include "version.h"
31 #include "hashtab.h"
32 #include "mkdeps.h"
33
34 /* Predefined symbols, built-in macros, and the default include path. */
35
36 #ifndef GET_ENV_PATH_LIST
37 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
38 #endif
39
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
50
51 #ifndef STANDARD_INCLUDE_DIR
52 #define STANDARD_INCLUDE_DIR "/usr/include"
53 #endif
54
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. */
60
61 /* The string value for __SIZE_TYPE__. */
62
63 #ifndef SIZE_TYPE
64 #define SIZE_TYPE "long unsigned int"
65 #endif
66
67 /* The string value for __PTRDIFF_TYPE__. */
68
69 #ifndef PTRDIFF_TYPE
70 #define PTRDIFF_TYPE "long int"
71 #endif
72
73 /* The string value for __WCHAR_TYPE__. */
74
75 #ifndef WCHAR_TYPE
76 #define WCHAR_TYPE "int"
77 #endif
78
79 /* The string value for __USER_LABEL_PREFIX__ */
80
81 #ifndef USER_LABEL_PREFIX
82 #define USER_LABEL_PREFIX ""
83 #endif
84
85 /* The string value for __REGISTER_PREFIX__ */
86
87 #ifndef REGISTER_PREFIX
88 #define REGISTER_PREFIX ""
89 #endif
90
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.
93
94 #include "file" looks in the same directory as the current file,
95 then this list.
96 #include <file> just looks in this list.
97
98 All these directories are treated as `system' include directories
99 (they are not subject to pedantic warnings in some cases). */
100
101 struct default_include
102 {
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++. */
110 };
111
112 #ifndef STANDARD_INCLUDE_COMPONENT
113 #define STANDARD_INCLUDE_COMPONENT 0
114 #endif
115
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
123
124 static const struct default_include include_defaults_array[]
125 #ifdef INCLUDE_DEFAULTS
126 = INCLUDE_DEFAULTS;
127 #else
128 = {
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 }
158 };
159 #endif /* no INCLUDE_DEFAULTS */
160
161 /* Internal structures and prototypes. */
162
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) */
168
169 typedef void (* cl_directive_handler) PARAMS ((cpp_reader *, const char *));
170 struct pending_option
171 {
172 struct pending_option *next;
173 const char *arg;
174 cl_directive_handler handler;
175 };
176
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
182 {
183 struct pending_option *directive_head, *directive_tail;
184
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;
189
190 struct pending_option *imacros_head, *imacros_tail;
191 struct pending_option *include_head, *include_tail;
192 };
193
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
207
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 ((struct cpp_options *));
217
218 static void dump_special_to_buffer PARAMS ((cpp_reader *, const char *));
219 static void initialize_dependency_output PARAMS ((cpp_reader *));
220 static void initialize_standard_includes PARAMS ((cpp_reader *));
221 static void new_pending_directive PARAMS ((struct cpp_options *,
222 const char *,
223 cl_directive_handler));
224 #ifdef HOST_EBCDIC
225 static int opt_comp PARAMS ((const void *, const void *));
226 #endif
227 static int parse_option PARAMS ((const char *));
228 static int handle_option PARAMS ((cpp_reader *, int, char **));
229
230 /* Fourth argument to append_include_chain: chain to use */
231 enum { QUOTE = 0, BRACKET, SYSTEM, AFTER };
232
233 /* If we have designated initializers (GCC >2.7, or C99) this table
234 can be initialized, constant data. Otherwise, it has to be filled
235 in at runtime. */
236
237 #if (GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L)
238 #define init_IStable() /* nothing */
239 #define ISTABLE const unsigned char _cpp_IStable[256] = {
240 #define END };
241 #define s(p, v) [p] = v,
242 #else
243 #define ISTABLE unsigned char _cpp_IStable[256] = { 0 }; \
244 static void init_IStable PARAMS ((void)) { \
245 unsigned char *x = _cpp_IStable;
246 #define END }
247 #define s(p, v) x[p] = v;
248 #endif
249
250 #define A(x) s(x, ISidnum|ISidstart)
251 #define N(x) s(x, ISidnum|ISnumstart)
252 #define H(x) s(x, IShspace|ISspace)
253 #define S(x) s(x, ISspace)
254
255 ISTABLE
256 A('_')
257
258 A('a') A('b') A('c') A('d') A('e') A('f') A('g') A('h') A('i')
259 A('j') A('k') A('l') A('m') A('n') A('o') A('p') A('q') A('r')
260 A('s') A('t') A('u') A('v') A('w') A('x') A('y') A('z')
261
262 A('A') A('B') A('C') A('D') A('E') A('F') A('G') A('H') A('I')
263 A('J') A('K') A('L') A('M') A('N') A('O') A('P') A('Q') A('R')
264 A('S') A('T') A('U') A('V') A('W') A('X') A('Y') A('Z')
265
266 N('1') N('2') N('3') N('4') N('5') N('6') N('7') N('8') N('9') N('0')
267
268 H(' ') H('\t') H('\v') H('\f')
269
270 S('\n')
271 END
272
273 #undef A
274 #undef N
275 #undef H
276 #undef S
277 #undef s
278 #undef ISTABLE
279 #undef END
280
281 /* Given a colon-separated list of file names PATH,
282 add all the names to the search path for include files. */
283
284 static void
285 path_include (pfile, pend, list, path)
286 cpp_reader *pfile;
287 struct cpp_pending *pend;
288 char *list;
289 int path;
290 {
291 char *p, *q, *name;
292
293 p = list;
294
295 do
296 {
297 /* Find the end of this name. */
298 q = p;
299 while (*q != 0 && *q != PATH_SEPARATOR) q++;
300 if (q == p)
301 {
302 /* An empty name in the path stands for the current directory. */
303 name = (char *) xmalloc (2);
304 name[0] = '.';
305 name[1] = 0;
306 }
307 else
308 {
309 /* Otherwise use the directory that is named. */
310 name = (char *) xmalloc (q - p + 1);
311 memcpy (name, p, q - p);
312 name[q - p] = 0;
313 }
314
315 append_include_chain (pfile, pend, name, path, 0);
316
317 /* Advance past this name. */
318 if (*q == 0)
319 break;
320 p = q + 1;
321 }
322 while (1);
323 }
324
325 /* Append DIR to include path PATH. DIR must be permanently allocated
326 and writable. */
327 static void
328 append_include_chain (pfile, pend, dir, path, cxx_aware)
329 cpp_reader *pfile;
330 struct cpp_pending *pend;
331 char *dir;
332 int path;
333 int cxx_aware;
334 {
335 struct file_name_list *new;
336 struct stat st;
337 unsigned int len;
338
339 _cpp_simplify_pathname (dir);
340 if (stat (dir, &st))
341 {
342 /* Dirs that don't exist are silently ignored. */
343 if (errno != ENOENT)
344 cpp_notice_from_errno (pfile, dir);
345 else if (CPP_OPTIONS (pfile)->verbose)
346 fprintf (stderr, _("ignoring nonexistent directory `%s'\n"), dir);
347 return;
348 }
349
350 if (!S_ISDIR (st.st_mode))
351 {
352 cpp_notice (pfile, "%s: Not a directory", dir);
353 return;
354 }
355
356 len = strlen (dir);
357 if (len > pfile->max_include_len)
358 pfile->max_include_len = len;
359
360 new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
361 new->name = dir;
362 new->nlen = len;
363 new->ino = st.st_ino;
364 new->dev = st.st_dev;
365 if (path == SYSTEM)
366 new->sysp = cxx_aware ? 1 : 2;
367 else
368 new->sysp = 0;
369 new->name_map = NULL;
370 new->next = NULL;
371 new->alloc = NULL;
372
373 switch (path)
374 {
375 case QUOTE: APPEND (pend, quote, new); break;
376 case BRACKET: APPEND (pend, brack, new); break;
377 case SYSTEM: APPEND (pend, systm, new); break;
378 case AFTER: APPEND (pend, after, new); break;
379 }
380 }
381
382 /* Merge the four include chains together in the order quote, bracket,
383 system, after. Remove duplicate dirs (as determined by
384 INO_T_EQ()). The system_include and after_include chains are never
385 referred to again after this function; all access is through the
386 bracket_include path.
387
388 For the future: Check if the directory is empty (but
389 how?) and possibly preload the include hash. */
390
391 static void
392 merge_include_chains (opts)
393 struct cpp_options *opts;
394 {
395 struct file_name_list *prev, *cur, *other;
396 struct file_name_list *quote, *brack, *systm, *after;
397 struct file_name_list *qtail, *btail, *stail, *atail;
398
399 qtail = opts->pending->quote_tail;
400 btail = opts->pending->brack_tail;
401 stail = opts->pending->systm_tail;
402 atail = opts->pending->after_tail;
403
404 quote = opts->pending->quote_head;
405 brack = opts->pending->brack_head;
406 systm = opts->pending->systm_head;
407 after = opts->pending->after_head;
408
409 /* Paste together bracket, system, and after include chains. */
410 if (stail)
411 stail->next = after;
412 else
413 systm = after;
414 if (btail)
415 btail->next = systm;
416 else
417 brack = systm;
418
419 /* This is a bit tricky.
420 First we drop dupes from the quote-include list.
421 Then we drop dupes from the bracket-include list.
422 Finally, if qtail and brack are the same directory,
423 we cut out qtail.
424
425 We can't just merge the lists and then uniquify them because
426 then we may lose directories from the <> search path that should
427 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
428 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
429 -Ibar -I- -Ifoo -Iquux.
430
431 Note that this algorithm is quadratic in the number of -I switches,
432 which is acceptable since there aren't usually that many of them. */
433
434 for (cur = quote, prev = NULL; cur; cur = cur->next)
435 {
436 for (other = quote; other != cur; other = other->next)
437 if (INO_T_EQ (cur->ino, other->ino)
438 && cur->dev == other->dev)
439 {
440 if (opts->verbose)
441 fprintf (stderr, _("ignoring duplicate directory `%s'\n"),
442 cur->name);
443
444 prev->next = cur->next;
445 free (cur->name);
446 free (cur);
447 cur = prev;
448 break;
449 }
450 prev = cur;
451 }
452 qtail = prev;
453
454 for (cur = brack; cur; cur = cur->next)
455 {
456 for (other = brack; other != cur; other = other->next)
457 if (INO_T_EQ (cur->ino, other->ino)
458 && cur->dev == other->dev)
459 {
460 if (opts->verbose)
461 fprintf (stderr, _("ignoring duplicate directory `%s'\n"),
462 cur->name);
463
464 prev->next = cur->next;
465 free (cur->name);
466 free (cur);
467 cur = prev;
468 break;
469 }
470 prev = cur;
471 }
472
473 if (quote)
474 {
475 if (INO_T_EQ (qtail->ino, brack->ino) && qtail->dev == brack->dev)
476 {
477 if (quote == qtail)
478 {
479 if (opts->verbose)
480 fprintf (stderr, _("ignoring duplicate directory `%s'\n"),
481 quote->name);
482
483 free (quote->name);
484 free (quote);
485 quote = brack;
486 }
487 else
488 {
489 cur = quote;
490 while (cur->next != qtail)
491 cur = cur->next;
492 cur->next = brack;
493 if (opts->verbose)
494 fprintf (stderr, _("ignoring duplicate directory `%s'\n"),
495 qtail->name);
496
497 free (qtail->name);
498 free (qtail);
499 }
500 }
501 else
502 qtail->next = brack;
503 }
504 else
505 quote = brack;
506
507 opts->quote_include = quote;
508 opts->bracket_include = brack;
509 }
510
511
512 /* Write out a #define command for the special named MACRO_NAME
513 to PFILE's token_buffer. */
514
515 static void
516 dump_special_to_buffer (pfile, macro_name)
517 cpp_reader *pfile;
518 const char *macro_name;
519 {
520 static const char define_directive[] = "#define ";
521 int macro_name_length = strlen (macro_name);
522 _cpp_output_line_command (pfile, same_file);
523 CPP_RESERVE (pfile, sizeof(define_directive) + macro_name_length);
524 CPP_PUTS_Q (pfile, define_directive, sizeof(define_directive)-1);
525 CPP_PUTS_Q (pfile, macro_name, macro_name_length);
526 CPP_PUTC_Q (pfile, ' ');
527 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
528 CPP_PUTC (pfile, '\n');
529 }
530
531 /* Initialize a cpp_options structure. */
532 void
533 cpp_options_init (opts)
534 cpp_options *opts;
535 {
536 memset ((char *) opts, 0, sizeof (struct cpp_options));
537
538 opts->dollars_in_ident = 1;
539 opts->cplusplus_comments = 1;
540 opts->warn_import = 1;
541 opts->discard_comments = 1;
542
543 opts->pending =
544 (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
545 }
546
547 /* Initialize a cpp_reader structure. */
548 void
549 cpp_reader_init (pfile)
550 cpp_reader *pfile;
551 {
552 memset ((char *) pfile, 0, sizeof (cpp_reader));
553
554 pfile->token_buffer_size = 200;
555 pfile->token_buffer = (U_CHAR *) xmalloc (pfile->token_buffer_size);
556 CPP_SET_WRITTEN (pfile, 0);
557
558 _cpp_init_macro_hash (pfile);
559 _cpp_init_include_hash (pfile);
560 }
561
562 /* Free resources used by PFILE.
563 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
564 void
565 cpp_cleanup (pfile)
566 cpp_reader *pfile;
567 {
568 while (CPP_BUFFER (pfile) != NULL)
569 cpp_pop_buffer (pfile);
570
571 if (pfile->token_buffer)
572 {
573 free (pfile->token_buffer);
574 pfile->token_buffer = NULL;
575 }
576
577 if (pfile->input_buffer)
578 {
579 free (pfile->input_buffer);
580 free (pfile->input_speccase);
581 pfile->input_buffer = pfile->input_speccase = NULL;
582 pfile->input_buffer_len = 0;
583 }
584
585 if (pfile->deps)
586 deps_free (pfile->deps);
587
588 htab_delete (pfile->hashtab);
589 htab_delete (pfile->all_include_files);
590 }
591
592
593 /* This structure defines one built-in macro. A node of type TYPE will
594 be entered in the macro hash table under the name NAME, with value
595 VALUE (if any). FLAGS tweaks the behavior a little:
596 DUMP write debug info for this macro
597 STDC define only if not -traditional
598 ULP value is the global user_label_prefix (which can't be
599 put directly into the table).
600 */
601
602 struct builtin
603 {
604 const char *name;
605 const char *value;
606 unsigned short type;
607 unsigned short flags;
608 };
609 #define DUMP 0x01
610 #define STDC 0x02
611 #define VERS 0x04
612 #define ULP 0x08
613
614 static const struct builtin builtin_array[] =
615 {
616 { "__TIME__", 0, T_TIME, DUMP },
617 { "__DATE__", 0, T_DATE, DUMP },
618 { "__FILE__", 0, T_FILE, 0 },
619 { "__BASE_FILE__", 0, T_BASE_FILE, 0 },
620 { "__LINE__", 0, T_SPECLINE, 0 },
621 { "__INCLUDE_LEVEL__", 0, T_INCLUDE_LEVEL, 0 },
622 { "__VERSION__", 0, T_VERSION, DUMP|VERS },
623 { "__STDC__", 0, T_STDC, DUMP|STDC },
624
625 { "__USER_LABEL_PREFIX__", 0, T_CONST, ULP },
626 { "__REGISTER_PREFIX__", REGISTER_PREFIX, T_CONST, 0 },
627 { "__HAVE_BUILTIN_SETJMP__", "1", T_CONST, 0 },
628 #ifndef NO_BUILTIN_SIZE_TYPE
629 { "__SIZE_TYPE__", SIZE_TYPE, T_CONST, DUMP },
630 #endif
631 #ifndef NO_BUILTIN_PTRDIFF_TYPE
632 { "__PTRDIFF_TYPE__", PTRDIFF_TYPE, T_CONST, DUMP },
633 #endif
634 #ifndef NO_BUILTIN_WCHAR_TYPE
635 { "__WCHAR_TYPE__", WCHAR_TYPE, T_CONST, DUMP },
636 #endif
637 { 0, 0, 0, 0 }
638 };
639
640 /* Subroutine of cpp_start_read; reads the builtins table above and
641 enters the macros into the hash table. */
642 static void
643 initialize_builtins (pfile)
644 cpp_reader *pfile;
645 {
646 int len;
647 const struct builtin *b;
648 const char *val;
649 HASHNODE *hp;
650 for(b = builtin_array; b->name; b++)
651 {
652 if ((b->flags & STDC) && CPP_TRADITIONAL (pfile))
653 continue;
654
655 if (b->flags & ULP)
656 val = user_label_prefix;
657 else if (b->flags & VERS)
658 val = version_string;
659 else
660 val = b->value;
661
662 len = strlen (b->name);
663 hp = _cpp_make_hashnode (b->name, len, b->type,
664 _cpp_calc_hash (b->name, len));
665 hp->value.cpval = val;
666 *(htab_find_slot (pfile->hashtab, (void *)hp, 1)) = hp;
667
668 if ((b->flags & DUMP) && CPP_OPTIONS (pfile)->debug_output)
669 dump_special_to_buffer (pfile, b->name);
670 }
671
672 }
673 #undef DUMP
674 #undef STDC
675 #undef VERS
676 #undef ULP
677
678 /* Another subroutine of cpp_start_read. This one sets up to do
679 dependency-file output. */
680 static void
681 initialize_dependency_output (pfile)
682 cpp_reader *pfile;
683 {
684 cpp_options *opts = CPP_OPTIONS (pfile);
685 char *spec, *s, *output_file;
686
687 /* Either of two environment variables can specify output of deps.
688 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
689 where OUTPUT_FILE is the file to write deps info to
690 and DEPS_TARGET is the target to mention in the deps. */
691
692 if (opts->print_deps == 0)
693 {
694 spec = getenv ("DEPENDENCIES_OUTPUT");
695 if (spec)
696 opts->print_deps = 1;
697 else
698 {
699 spec = getenv ("SUNPRO_DEPENDENCIES");
700 if (spec)
701 opts->print_deps = 2;
702 else
703 return;
704 }
705
706 /* Find the space before the DEPS_TARGET, if there is one. */
707 s = strchr (spec, ' ');
708 if (s)
709 {
710 opts->deps_target = s + 1;
711 output_file = (char *) xmalloc (s - spec + 1);
712 memcpy (output_file, spec, s - spec);
713 output_file[s - spec] = 0;
714 }
715 else
716 {
717 opts->deps_target = 0;
718 output_file = spec;
719 }
720
721 opts->deps_file = output_file;
722 opts->print_deps_append = 1;
723 }
724
725 pfile->deps = deps_init ();
726
727 /* Print the expected object file name as the target of this Make-rule. */
728 if (opts->deps_target)
729 deps_add_target (pfile->deps, opts->deps_target);
730 else if (*opts->in_fname == 0)
731 deps_add_target (pfile->deps, "-");
732 else
733 deps_calc_target (pfile->deps, opts->in_fname);
734
735 if (opts->in_fname)
736 deps_add_dep (pfile->deps, opts->in_fname);
737 }
738
739 /* And another subroutine. This one sets up the standard include path. */
740 static void
741 initialize_standard_includes (pfile)
742 cpp_reader *pfile;
743 {
744 cpp_options *opts = CPP_OPTIONS (pfile);
745 char *path;
746 const struct default_include *p;
747 const char *specd_prefix = opts->include_prefix;
748
749 /* Several environment variables may add to the include search path.
750 CPATH specifies an additional list of directories to be searched
751 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
752 etc. specify an additional list of directories to be searched as
753 if specified with -isystem, for the language indicated. */
754
755 GET_ENV_PATH_LIST (path, "CPATH");
756 if (path != 0 && *path != 0)
757 path_include (pfile, opts->pending, path, BRACKET);
758
759 switch ((opts->objc << 1) + opts->cplusplus)
760 {
761 case 0:
762 GET_ENV_PATH_LIST (path, "C_INCLUDE_PATH");
763 break;
764 case 1:
765 GET_ENV_PATH_LIST (path, "CPLUS_INCLUDE_PATH");
766 break;
767 case 2:
768 GET_ENV_PATH_LIST (path, "OBJC_INCLUDE_PATH");
769 break;
770 case 3:
771 GET_ENV_PATH_LIST (path, "OBJCPLUS_INCLUDE_PATH");
772 break;
773 }
774 if (path != 0 && *path != 0)
775 path_include (pfile, opts->pending, path, SYSTEM);
776
777 /* Search "translated" versions of GNU directories.
778 These have /usr/local/lib/gcc... replaced by specd_prefix. */
779 if (specd_prefix != 0)
780 {
781 char *default_prefix = alloca (sizeof GCC_INCLUDE_DIR - 7);
782 /* Remove the `include' from /usr/local/lib/gcc.../include.
783 GCC_INCLUDE_DIR will always end in /include. */
784 int default_len = sizeof GCC_INCLUDE_DIR - 8;
785 int specd_len = strlen (specd_prefix);
786
787 memcpy (default_prefix, GCC_INCLUDE_DIR, default_len);
788 default_prefix[default_len] = '\0';
789
790 for (p = include_defaults_array; p->fname; p++)
791 {
792 /* Some standard dirs are only for C++. */
793 if (!p->cplusplus
794 || (opts->cplusplus
795 && !opts->no_standard_cplusplus_includes))
796 {
797 /* Does this dir start with the prefix? */
798 if (!strncmp (p->fname, default_prefix, default_len))
799 {
800 /* Yes; change prefix and add to search list. */
801 int flen = strlen (p->fname);
802 int this_len = specd_len + flen - default_len;
803 char *str = (char *) xmalloc (this_len + 1);
804 memcpy (str, specd_prefix, specd_len);
805 memcpy (str + specd_len,
806 p->fname + default_len,
807 flen - default_len + 1);
808
809 append_include_chain (pfile, opts->pending,
810 str, SYSTEM, p->cxx_aware);
811 }
812 }
813 }
814 }
815
816 /* Search ordinary names for GNU include directories. */
817 for (p = include_defaults_array; p->fname; p++)
818 {
819 /* Some standard dirs are only for C++. */
820 if (!p->cplusplus
821 || (opts->cplusplus
822 && !opts->no_standard_cplusplus_includes))
823 {
824 /* XXX Potential memory leak! */
825 char *str = xstrdup (update_path (p->fname, p->component));
826 append_include_chain (pfile, opts->pending, str, SYSTEM,
827 p->cxx_aware);
828 }
829 }
830 }
831
832 /* This is called after options have been processed.
833 * Check options for consistency, and setup for processing input
834 * from the file named FNAME. (Use standard input if FNAME==NULL.)
835 * Return 1 on success, 0 on failure.
836 */
837
838 int
839 cpp_start_read (pfile, fname)
840 cpp_reader *pfile;
841 const char *fname;
842 {
843 struct cpp_options *opts = CPP_OPTIONS (pfile);
844 struct pending_option *p, *q;
845
846 /* -MG doesn't select the form of output and must be specified with one of
847 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
848 inhibit compilation. */
849 if (opts->print_deps_missing_files
850 && (opts->print_deps == 0 || !opts->no_output))
851 {
852 cpp_fatal (pfile, "-MG must be specified with one of -M or -MM");
853 return 0;
854 }
855
856 /* Chill should not be used with -trigraphs. */
857 if (opts->chill && opts->trigraphs)
858 {
859 cpp_warning (pfile, "-lang-chill and -trigraphs are mutually exclusive");
860 opts->trigraphs = 0;
861 }
862
863 /* Set this if it hasn't been set already. */
864 if (user_label_prefix == NULL)
865 user_label_prefix = USER_LABEL_PREFIX;
866
867 /* Don't bother trying to do macro expansion if we've already done
868 preprocessing. */
869 if (opts->preprocessed)
870 pfile->no_macro_expand++;
871
872 /* Set up the IStable. This doesn't do anything if we were compiled
873 with a compiler that supports C99 designated initializers. */
874 init_IStable ();
875
876 /* Set up the tables used by read_and_prescan. */
877 _cpp_init_input_buffer (pfile);
878
879 /* Set up the include search path now. */
880 if (! opts->no_standard_includes)
881 initialize_standard_includes (pfile);
882
883 merge_include_chains (opts);
884
885 /* With -v, print the list of dirs to search. */
886 if (opts->verbose)
887 {
888 struct file_name_list *l;
889 fprintf (stderr, _("#include \"...\" search starts here:\n"));
890 for (l = opts->quote_include; l; l = l->next)
891 {
892 if (l == opts->bracket_include)
893 fprintf (stderr, _("#include <...> search starts here:\n"));
894 fprintf (stderr, " %s\n", l->name);
895 }
896 fprintf (stderr, _("End of search list.\n"));
897 }
898
899 /* Open the main input file. This must be done early, so we have a
900 buffer to stand on. */
901 if (opts->in_fname == NULL || *opts->in_fname == 0)
902 {
903 opts->in_fname = fname;
904 if (opts->in_fname == NULL)
905 opts->in_fname = "";
906 }
907
908 if (!cpp_read_file (pfile, fname))
909 return 0;
910
911 initialize_dependency_output (pfile);
912
913 /* -D and friends may produce output, which should be identified
914 as line 0. */
915
916 CPP_BUFFER (pfile)->lineno = 0;
917
918 /* Install __LINE__, etc. */
919 initialize_builtins (pfile);
920
921 /* Do -U's, -D's and -A's in the order they were seen. */
922 p = opts->pending->directive_head;
923 while (p)
924 {
925 (*p->handler) (pfile, p->arg);
926 q = p->next;
927 free (p);
928 p = q;
929 }
930
931 opts->done_initializing = 1;
932 CPP_BUFFER (pfile)->lineno = 1;
933
934 if (opts->preprocessed)
935 /* If we've already processed this code, we want to trust the #line
936 directives in the input. But we still need to update our line
937 counter accordingly. */
938 pfile->lineno = CPP_BUFFER (pfile)->lineno;
939 else
940 _cpp_output_line_command (pfile, same_file);
941 pfile->only_seen_white = 2;
942
943 /* The -imacros files can be scanned now, but the -include files
944 have to be pushed onto the include stack and processed later,
945 in the main loop calling cpp_get_token. */
946
947 opts->no_output++;
948 p = opts->pending->imacros_head;
949 while (p)
950 {
951 if (cpp_read_file (pfile, p->arg))
952 cpp_scan_buffer (pfile);
953
954 q = p->next;
955 free (p);
956 p = q;
957 }
958 opts->no_output--;
959
960 p = opts->pending->include_head;
961 while (p)
962 {
963 if (cpp_read_file (pfile, p->arg))
964 _cpp_output_line_command (pfile, enter_file);
965
966 q = p->next;
967 free (p);
968 p = q;
969 }
970
971 free (opts->pending);
972 opts->pending = NULL;
973
974 return 1;
975 }
976
977 /* This is called at the end of preprocessing. It pops the
978 last buffer and writes dependency output. It should also
979 clear macro definitions, such that you could call cpp_start_read
980 with a new filename to restart processing. */
981 void
982 cpp_finish (pfile)
983 cpp_reader *pfile;
984 {
985 struct cpp_options *opts = CPP_OPTIONS (pfile);
986
987 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
988 cpp_ice (pfile, "buffers still stacked in cpp_finish");
989 while (CPP_BUFFER (pfile))
990 cpp_pop_buffer (pfile);
991
992 /* Don't write the deps file if preprocessing has failed. */
993 if (opts->print_deps && pfile->errors == 0)
994 {
995 /* Stream on which to print the dependency information. */
996 FILE *deps_stream = 0;
997
998 const char *deps_mode = opts->print_deps_append ? "a" : "w";
999 if (opts->deps_file == 0)
1000 deps_stream = stdout;
1001 else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
1002 cpp_notice_from_errno (pfile, opts->deps_file);
1003
1004 if (deps_stream)
1005 {
1006 deps_write (pfile->deps, deps_stream, 72);
1007 if (opts->deps_file)
1008 {
1009 if (ferror (deps_stream) || fclose (deps_stream) != 0)
1010 cpp_fatal (pfile, "I/O error on output");
1011 }
1012 }
1013 }
1014
1015 if (opts->dump_macros == dump_only)
1016 _cpp_dump_macro_hash (pfile);
1017 }
1018
1019 static void
1020 new_pending_directive (opts, text, handler)
1021 struct cpp_options *opts;
1022 const char *text;
1023 cl_directive_handler handler;
1024 {
1025 struct pending_option *o = (struct pending_option *)
1026 xmalloc (sizeof (struct pending_option));
1027
1028 o->arg = text;
1029 o->next = NULL;
1030 o->handler = handler;
1031 APPEND (opts->pending, directive, o);
1032 }
1033
1034 enum opt_code
1035 {
1036 OPT_stdin_stdout = 0, OPT_dollar, OPT_plus,
1037 OPT__help, OPT__version,
1038 OPT_A, OPT_C, OPT_D, OPT_H, OPT_I, OPT_M,
1039 OPT_MD, OPT_MG, OPT_MM, OPT_MMD,
1040 OPT_P, OPT_U, OPT_W,
1041 OPT_d,
1042 OPT_fleading_underscore, OPT_fno_leading_underscore,
1043 OPT_fpreprocessed, OPT_fno_preprocessed,
1044 OPT_g, OPT_h,
1045 OPT_idirafter, OPT_imacros, OPT_include,
1046 OPT_iprefix, OPT_isystem, OPT_iwithprefix, OPT_iwithprefixbefore,
1047 OPT_lang_asm, OPT_lang_c, OPT_lang_cplusplus, OPT_lang_c89,
1048 OPT_lang_chill, OPT_lang_fortran, OPT_lang_objc, OPT_lang_objcplusplus,
1049 OPT_nostdinc, OPT_nostdincplusplus,
1050 OPT_o,
1051 OPT_pedantic, OPT_pedantic_errors, OPT_remap,
1052 OPT_std_c89, OPT_std_c99, OPT_std_c9x, OPT_std_gnu89, OPT_std_gnu99,
1053 OPT_std_gnu9x, OPT_std_iso9899_1990, OPT_std_iso9899_199409,
1054 OPT_std_iso9899_1999, OPT_std_iso9899_199x,
1055 OPT_traditional, OPT_trigraphs,
1056 OPT_v, OPT_w,
1057 N_OPTS
1058 };
1059
1060 struct cl_option
1061 {
1062 const char *opt_text;
1063 const char *msg;
1064 size_t opt_len;
1065 enum opt_code opt_code;
1066 };
1067
1068 /* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
1069 I.e. a const string initializer with parens around it. That is
1070 what N_("string") resolves to, so we make no_* be macros instead. */
1071 #define no_arg N_("Argument missing after `%s' option")
1072 #define no_ass N_("Assertion missing after `%s' option")
1073 #define no_dir N_("Directory name missing after `%s' option")
1074 #define no_fil N_("File name missing after `%s' option")
1075 #define no_mac N_("Macro name missing after `%s' option")
1076 #define no_pth N_("Path name missing after `%s' option")
1077
1078 /* This list must be ASCII sorted. Make enum order above match this. */
1079 #define DEF_OPT(text, msg, code) {text, msg, sizeof(text) - 1, code}
1080
1081 #ifdef HOST_EBCDIC
1082 static struct cl_option cl_options[] =
1083 #else
1084 static const struct cl_option cl_options[] =
1085 #endif
1086 {
1087 DEF_OPT("", 0, OPT_stdin_stdout),
1088 DEF_OPT("$", 0, OPT_dollar),
1089 DEF_OPT("+", 0, OPT_plus),
1090 DEF_OPT("-help", 0, OPT__help),
1091 DEF_OPT("-version", 0, OPT__version),
1092 DEF_OPT("A", no_ass, OPT_A),
1093 DEF_OPT("C", 0, OPT_C),
1094 DEF_OPT("D", no_mac, OPT_D),
1095 DEF_OPT("H", 0, OPT_H),
1096 DEF_OPT("I", no_dir, OPT_I),
1097 DEF_OPT("M", 0, OPT_M),
1098 DEF_OPT("MD", no_fil, OPT_MD),
1099 DEF_OPT("MG", 0, OPT_MG),
1100 DEF_OPT("MM", 0, OPT_MM),
1101 DEF_OPT("MMD", no_fil, OPT_MMD),
1102 DEF_OPT("P", 0, OPT_P),
1103 DEF_OPT("U", no_mac, OPT_U),
1104 /* NB: Immed arg only, and not reqd */
1105 DEF_OPT("W", no_arg, OPT_W),
1106 DEF_OPT("d", no_arg, OPT_d),
1107 DEF_OPT("fleading-underscore", 0, OPT_fleading_underscore),
1108 DEF_OPT("fno-leading-underscore", 0, OPT_fno_leading_underscore),
1109 DEF_OPT("fpreprocessed", 0, OPT_fpreprocessed),
1110 DEF_OPT("fno-preprocessed", 0, OPT_fno_preprocessed),
1111 /* NB: Immed arg only, and not reqd */
1112 DEF_OPT("g", no_arg, OPT_g),
1113 DEF_OPT("h", 0, OPT_h),
1114 DEF_OPT("idirafter", no_dir, OPT_idirafter),
1115 DEF_OPT("imacros", no_fil, OPT_imacros),
1116 DEF_OPT("include", no_fil, OPT_include),
1117 DEF_OPT("iprefix", no_pth, OPT_iprefix),
1118 DEF_OPT("isystem", no_dir, OPT_isystem),
1119 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix),
1120 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore),
1121 DEF_OPT("lang-asm", 0, OPT_lang_asm),
1122 DEF_OPT("lang-c", 0, OPT_lang_c),
1123 DEF_OPT("lang-c++", 0, OPT_lang_cplusplus),
1124 DEF_OPT("lang-c89", 0, OPT_lang_c89),
1125 DEF_OPT("lang-chill", 0, OPT_lang_chill),
1126 DEF_OPT("lang-fortran", 0, OPT_lang_fortran),
1127 DEF_OPT("lang-objc", 0, OPT_lang_objc),
1128 DEF_OPT("lang-objc++", 0, OPT_lang_objcplusplus),
1129 DEF_OPT("nostdinc", 0, OPT_nostdinc),
1130 DEF_OPT("nostdinc++", 0, OPT_nostdincplusplus),
1131 DEF_OPT("o", no_fil, OPT_o),
1132 DEF_OPT("pedantic", 0, OPT_pedantic),
1133 DEF_OPT("pedantic-errors", 0, OPT_pedantic_errors),
1134 DEF_OPT("remap", 0, OPT_remap),
1135 DEF_OPT("std=c89", 0, OPT_std_c89),
1136 DEF_OPT("std=c99", 0, OPT_std_c99),
1137 DEF_OPT("std=c9x", 0, OPT_std_c9x),
1138 DEF_OPT("std=gnu89", 0, OPT_std_gnu89),
1139 DEF_OPT("std=gnu99", 0, OPT_std_gnu99),
1140 DEF_OPT("std=gnu9x", 0, OPT_std_gnu9x),
1141 DEF_OPT("std=iso9899:1990", 0, OPT_std_iso9899_1990),
1142 DEF_OPT("std=iso9899:199409", 0, OPT_std_iso9899_199409),
1143 DEF_OPT("std=iso9899:1999", 0, OPT_std_iso9899_1999),
1144 DEF_OPT("std=iso9899:199x", 0, OPT_std_iso9899_199x),
1145 DEF_OPT("traditional", 0, OPT_traditional),
1146 DEF_OPT("trigraphs", 0, OPT_trigraphs),
1147 DEF_OPT("v", 0, OPT_v),
1148 DEF_OPT("w", 0, OPT_w)
1149 };
1150 #undef DEF_OPT
1151
1152 /* Perform a binary search to find which, if any, option the given
1153 command-line matches. Returns its index in the option array,
1154 negative on failure. Complications arise since some options can be
1155 suffixed with an argument, and multiple complete matches can occur,
1156 e.g. -iwithprefix and -iwithprefixbefore. Moreover, we want to
1157 accept options beginning with -g and -W that we do not recognise,
1158 but not to swallow any subsequent command line argument; these are
1159 handled as special cases in cpp_handle_option */
1160 static int
1161 parse_option (input)
1162 const char *input;
1163 {
1164 unsigned int md, mn, mx;
1165 size_t opt_len;
1166 int comp;
1167
1168 mn = 0;
1169 mx = N_OPTS;
1170
1171 while (mx > mn)
1172 {
1173 md = (mn + mx) / 2;
1174
1175 opt_len = cl_options[md].opt_len;
1176 comp = strncmp (input, cl_options[md].opt_text, opt_len);
1177
1178 if (comp > 0)
1179 mn = md + 1;
1180 else if (comp < 0)
1181 mx = md;
1182 else
1183 {
1184 if (input[opt_len] == '\0')
1185 return md;
1186 /* We were passed more text. If the option takes an argument,
1187 we may match a later option or we may have been passed the
1188 argument. The longest possible option match succeeds.
1189 If the option takes no arguments we have not matched and
1190 continue the search (e.g. input="stdc++" match was "stdc") */
1191 mn = md + 1;
1192 if (cl_options[md].msg)
1193 {
1194 /* Scan forwards. If we get an exact match, return it.
1195 Otherwise, return the longest option-accepting match.
1196 This loops no more than twice with current options */
1197 mx = md;
1198 for (; mn < N_OPTS; mn++)
1199 {
1200 opt_len = cl_options[mn].opt_len;
1201 if (strncmp (input, cl_options[mn].opt_text, opt_len))
1202 break;
1203 if (input[opt_len] == '\0')
1204 return mn;
1205 if (cl_options[mn].msg)
1206 mx = mn;
1207 }
1208 return mx;
1209 }
1210 }
1211 }
1212
1213 return -1;
1214 }
1215
1216 /* Handle one command-line option in (argc, argv).
1217 Can be called multiple times, to handle multiple sets of options.
1218 Returns number of strings consumed. */
1219
1220 static int
1221 handle_option (pfile, argc, argv)
1222 cpp_reader *pfile;
1223 int argc;
1224 char **argv;
1225 {
1226 struct cpp_options *opts = CPP_OPTIONS (pfile);
1227 int i = 0;
1228
1229 if (argv[i][0] != '-')
1230 {
1231 if (opts->out_fname != NULL)
1232 {
1233 print_help ();
1234 cpp_fatal (pfile, "Too many arguments");
1235 }
1236 else if (opts->in_fname != NULL)
1237 opts->out_fname = argv[i];
1238 else
1239 opts->in_fname = argv[i];
1240 }
1241 else
1242 {
1243 enum opt_code opt_code;
1244 int opt_index;
1245 const char *arg = 0;
1246
1247 /* Skip over '-' */
1248 opt_index = parse_option (&argv[i][1]);
1249 if (opt_index < 0)
1250 return i;
1251
1252 opt_code = cl_options[opt_index].opt_code;
1253 if (cl_options[opt_index].msg)
1254 {
1255 arg = &argv[i][cl_options[opt_index].opt_len + 1];
1256
1257 /* Yuk. Special case for -g and -W as they must not swallow
1258 up any following argument. If this becomes common, add
1259 another field to the cl_options table */
1260 if (arg[0] == '\0' && !(opt_code == OPT_g || opt_code == OPT_W))
1261 {
1262 arg = argv[++i];
1263 if (!arg)
1264 {
1265 cpp_fatal (pfile, _(cl_options[opt_index].msg), argv[i - 1]);
1266 return argc;
1267 }
1268 }
1269 }
1270
1271 switch (opt_code)
1272 {
1273 case N_OPTS: /* shut GCC up */
1274 break;
1275 case OPT_fleading_underscore:
1276 user_label_prefix = "_";
1277 break;
1278 case OPT_fno_leading_underscore:
1279 user_label_prefix = "";
1280 break;
1281 case OPT_fpreprocessed:
1282 opts->preprocessed = 1;
1283 break;
1284 case OPT_fno_preprocessed:
1285 opts->preprocessed = 0;
1286 break;
1287 case OPT_w:
1288 opts->inhibit_warnings = 1;
1289 break;
1290 case OPT_g: /* Silently ignore anything but -g3 */
1291 if (!strcmp(&argv[i][2], "3"))
1292 opts->debug_output = 1;
1293 break;
1294 case OPT_h:
1295 case OPT__help:
1296 print_help ();
1297 exit (0); /* XXX */
1298 break;
1299 case OPT__version:
1300 fprintf (stderr, _("GNU CPP version %s (cpplib)\n"), version_string);
1301 exit (0); /* XXX */
1302 break;
1303 case OPT_C:
1304 opts->discard_comments = 0;
1305 break;
1306 case OPT_P:
1307 opts->no_line_commands = 1;
1308 break;
1309 case OPT_dollar: /* Don't include $ in identifiers. */
1310 opts->dollars_in_ident = 0;
1311 break;
1312 case OPT_H:
1313 opts->print_include_names = 1;
1314 break;
1315 case OPT_D:
1316 new_pending_directive (opts, arg, cpp_define);
1317 break;
1318 case OPT_pedantic_errors:
1319 opts->pedantic_errors = 1;
1320 /* fall through */
1321 case OPT_pedantic:
1322 opts->pedantic = 1;
1323 break;
1324 case OPT_traditional:
1325 opts->traditional = 1;
1326 opts->cplusplus_comments = 0;
1327 opts->trigraphs = 0;
1328 opts->warn_trigraphs = 0;
1329 break;
1330 case OPT_trigraphs:
1331 opts->trigraphs = 1;
1332 break;
1333 case OPT_plus:
1334 opts->cplusplus = 1;
1335 opts->cplusplus_comments = 1;
1336 break;
1337 case OPT_remap:
1338 opts->remap = 1;
1339 break;
1340 case OPT_iprefix:
1341 opts->include_prefix = arg;
1342 opts->include_prefix_len = strlen (arg);
1343 break;
1344 case OPT_lang_c:
1345 opts->cplusplus = 0, opts->cplusplus_comments = 1;
1346 opts->c89 = 0, opts->c99 = 1, opts->objc = 0;
1347 break;
1348 case OPT_lang_c89:
1349 opts->cplusplus = 0, opts->cplusplus_comments = 0;
1350 opts->c89 = 1, opts->c99 = 0, opts->objc = 0;
1351 opts->trigraphs = 1;
1352 new_pending_directive (opts, "__STRICT_ANSI__", cpp_define);
1353 break;
1354 case OPT_lang_cplusplus:
1355 opts->cplusplus = 1, opts->cplusplus_comments = 1;
1356 opts->c89 = 0, opts->c99 = 0, opts->objc = 0;
1357 break;
1358 case OPT_lang_objc:
1359 case OPT_lang_objcplusplus:
1360 opts->cplusplus = opt_code == OPT_lang_objcplusplus;
1361 opts->cplusplus_comments = 1;
1362 opts->c89 = 0, opts->c99 = 0, opts->objc = 1;
1363 break;
1364 case OPT_lang_asm:
1365 opts->lang_asm = 1;
1366 break;
1367 case OPT_lang_fortran:
1368 opts->lang_fortran = 1, opts->cplusplus_comments = 0;
1369 break;
1370 case OPT_lang_chill:
1371 opts->objc = 0, opts->cplusplus = 0;
1372 opts->chill = 1, opts->traditional = 1;
1373 break;
1374 case OPT_nostdinc:
1375 /* -nostdinc causes no default include directories.
1376 You must specify all include-file directories with -I. */
1377 opts->no_standard_includes = 1;
1378 break;
1379 case OPT_nostdincplusplus:
1380 /* -nostdinc++ causes no default C++-specific include directories. */
1381 opts->no_standard_cplusplus_includes = 1;
1382 break;
1383 case OPT_std_gnu89:
1384 opts->cplusplus = 0, opts->cplusplus_comments = 1;
1385 opts->c89 = 1, opts->c99 = 0, opts->objc = 0;
1386 break;
1387 case OPT_std_gnu9x:
1388 case OPT_std_gnu99:
1389 opts->cplusplus = 0, opts->cplusplus_comments = 1;
1390 opts->c89 = 0, opts->c99 = 1, opts->objc = 0;
1391 new_pending_directive (opts, "__STDC_VERSION__=199901L", cpp_define);
1392 break;
1393 case OPT_std_iso9899_199409:
1394 new_pending_directive (opts, "__STDC_VERSION__=199409L", cpp_define);
1395 /* Fall through */
1396 case OPT_std_iso9899_1990:
1397 case OPT_std_c89:
1398 opts->cplusplus = 0, opts->cplusplus_comments = 0;
1399 opts->c89 = 1, opts->c99 = 0, opts->objc = 0;
1400 opts->trigraphs = 1;
1401 new_pending_directive (opts, "__STRICT_ANSI__", cpp_define);
1402 break;
1403 case OPT_std_iso9899_199x:
1404 case OPT_std_iso9899_1999:
1405 case OPT_std_c9x:
1406 case OPT_std_c99:
1407 opts->cplusplus = 0, opts->cplusplus_comments = 1;
1408 opts->c89 = 0, opts->c99 = 1, opts->objc = 0;
1409 opts->trigraphs = 1;
1410 new_pending_directive (opts, "__STRICT_ANSI__", cpp_define);
1411 new_pending_directive (opts, "__STDC_VERSION__=199901L", cpp_define);
1412 break;
1413 case OPT_o:
1414 if (opts->out_fname != NULL)
1415 {
1416 cpp_fatal (pfile, "Output filename specified twice");
1417 return argc;
1418 }
1419 opts->out_fname = arg;
1420 if (!strcmp (opts->out_fname, "-"))
1421 opts->out_fname = "";
1422 break;
1423 case OPT_v:
1424 fprintf (stderr, _("GNU CPP version %s (cpplib)\n"), version_string);
1425 #ifdef TARGET_VERSION
1426 TARGET_VERSION;
1427 #endif
1428 fputc ('\n', stderr);
1429 opts->verbose = 1;
1430 break;
1431 case OPT_stdin_stdout:
1432 /* JF handle '-' as file name meaning stdin or stdout */
1433 if (opts->in_fname == NULL)
1434 opts->in_fname = "";
1435 else if (opts->out_fname == NULL)
1436 opts->out_fname = "";
1437 break;
1438 case OPT_d:
1439 /* Args to -d specify what parts of macros to dump.
1440 Silently ignore unrecognised options; they may
1441 be aimed at the compiler proper. */
1442 {
1443 char c;
1444
1445 while ((c = *arg++) != '\0')
1446 switch (c)
1447 {
1448 case 'M':
1449 opts->dump_macros = dump_only;
1450 opts->no_output = 1;
1451 break;
1452 case 'N':
1453 opts->dump_macros = dump_names;
1454 break;
1455 case 'D':
1456 opts->dump_macros = dump_definitions;
1457 break;
1458 case 'I':
1459 opts->dump_includes = 1;
1460 break;
1461 }
1462 }
1463 break;
1464 /* The style of the choices here is a bit mixed.
1465 The chosen scheme is a hybrid of keeping all options in one string
1466 and specifying each option in a separate argument:
1467 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
1468 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1469 -M[M][G][D file]. This is awkward to handle in specs, and is not
1470 as extensible. */
1471 /* ??? -MG must be specified in addition to one of -M or -MM.
1472 This can be relaxed in the future without breaking anything.
1473 The converse isn't true. */
1474
1475 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
1476 case OPT_MG:
1477 opts->print_deps_missing_files = 1;
1478 break;
1479 case OPT_M:
1480 case OPT_MD:
1481 case OPT_MM:
1482 case OPT_MMD:
1483 if (opt_code == OPT_M || opt_code == OPT_MD)
1484 opts->print_deps = 2;
1485 else
1486 opts->print_deps = 1;
1487
1488 /* For -MD and -MMD options, write deps on file named by next arg */
1489 /* For -M and -MM, write deps on standard output
1490 and suppress the usual output. */
1491 if (opt_code == OPT_MD || opt_code == OPT_MMD)
1492 opts->deps_file = arg;
1493 else
1494 opts->no_output = 1;
1495 break;
1496 case OPT_A:
1497 if (arg[0] == '-')
1498 {
1499 /* -A with an argument beginning with '-' acts as
1500 #unassert on whatever immediately follows the '-'.
1501 If "-" is the whole argument, we eliminate all
1502 predefined macros and assertions, including those
1503 that were specified earlier on the command line.
1504 That way we can get rid of any that were passed
1505 automatically in from GCC. */
1506
1507 if (arg[1] == '\0')
1508 {
1509 struct pending_option *o1, *o2;
1510
1511 o1 = opts->pending->directive_head;
1512 while (o1)
1513 {
1514 o2 = o1->next;
1515 free (o1);
1516 o1 = o2;
1517 }
1518 opts->pending->directive_head = NULL;
1519 opts->pending->directive_tail = NULL;
1520 }
1521 else
1522 new_pending_directive (opts, arg + 1, cpp_unassert);
1523 }
1524 else
1525 new_pending_directive (opts, arg, cpp_assert);
1526 break;
1527 case OPT_U:
1528 new_pending_directive (opts, arg, cpp_undef);
1529 break;
1530 case OPT_I: /* Add directory to path for includes. */
1531 if (!strcmp (arg, "-"))
1532 {
1533 /* -I- means:
1534 Use the preceding -I directories for #include "..."
1535 but not #include <...>.
1536 Don't search the directory of the present file
1537 for #include "...". (Note that -I. -I- is not the same as
1538 the default setup; -I. uses the compiler's working dir.) */
1539 if (! opts->ignore_srcdir)
1540 {
1541 opts->ignore_srcdir = 1;
1542 opts->pending->quote_head = opts->pending->brack_head;
1543 opts->pending->quote_tail = opts->pending->brack_tail;
1544 opts->pending->brack_head = 0;
1545 opts->pending->brack_tail = 0;
1546 }
1547 else
1548 {
1549 cpp_fatal (pfile, "-I- specified twice");
1550 return argc;
1551 }
1552 }
1553 else
1554 append_include_chain (pfile, opts->pending,
1555 xstrdup (arg), BRACKET, 0);
1556 break;
1557 case OPT_isystem:
1558 /* Add directory to beginning of system include path, as a system
1559 include directory. */
1560 append_include_chain (pfile, opts->pending,
1561 xstrdup (arg), SYSTEM, 0);
1562 break;
1563 case OPT_include:
1564 {
1565 struct pending_option *o = (struct pending_option *)
1566 xmalloc (sizeof (struct pending_option));
1567 o->arg = arg;
1568
1569 /* This list has to be built in reverse order so that
1570 when cpp_start_read pushes all the -include files onto
1571 the buffer stack, they will be scanned in forward order. */
1572 o->next = opts->pending->include_head;
1573 opts->pending->include_head = o;
1574 }
1575 break;
1576 case OPT_imacros:
1577 {
1578 struct pending_option *o = (struct pending_option *)
1579 xmalloc (sizeof (struct pending_option));
1580 o->arg = arg;
1581 o->next = NULL;
1582
1583 APPEND (opts->pending, imacros, o);
1584 }
1585 break;
1586 case OPT_iwithprefix:
1587 /* Add directory to end of path for includes,
1588 with the default prefix at the front of its name. */
1589 /* fall through */
1590 case OPT_iwithprefixbefore:
1591 /* Add directory to main path for includes,
1592 with the default prefix at the front of its name. */
1593 {
1594 char *fname;
1595 int len;
1596
1597 len = strlen (arg);
1598
1599 if (opts->include_prefix != 0)
1600 {
1601 fname = xmalloc (opts->include_prefix_len + len + 1);
1602 memcpy (fname, opts->include_prefix, opts->include_prefix_len);
1603 memcpy (fname + opts->include_prefix_len, arg, len + 1);
1604 }
1605 else
1606 {
1607 fname = xmalloc (sizeof GCC_INCLUDE_DIR - 8 + len);
1608 memcpy (fname, GCC_INCLUDE_DIR, sizeof GCC_INCLUDE_DIR - 9);
1609 memcpy (fname + sizeof GCC_INCLUDE_DIR - 9, arg, len + 1);
1610 }
1611
1612 append_include_chain (pfile, opts->pending, fname,
1613 opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0);
1614 }
1615 break;
1616 case OPT_idirafter:
1617 /* Add directory to end of path for includes. */
1618 append_include_chain (pfile, opts->pending,
1619 xstrdup (arg), AFTER, 0);
1620 break;
1621 case OPT_W:
1622 /* Silently ignore unrecognised options */
1623 if (!strcmp (argv[i], "-Wall"))
1624 {
1625 opts->warn_trigraphs = 1;
1626 opts->warn_comments = 1;
1627 }
1628 else if (!strcmp (argv[i], "-Wtraditional"))
1629 opts->warn_stringify = 1;
1630 else if (!strcmp (argv[i], "-Wtrigraphs"))
1631 opts->warn_trigraphs = 1;
1632 else if (!strcmp (argv[i], "-Wcomment"))
1633 opts->warn_comments = 1;
1634 else if (!strcmp (argv[i], "-Wcomments"))
1635 opts->warn_comments = 1;
1636 else if (!strcmp (argv[i], "-Wundef"))
1637 opts->warn_undef = 1;
1638 else if (!strcmp (argv[i], "-Wimport"))
1639 opts->warn_import = 1;
1640 else if (!strcmp (argv[i], "-Werror"))
1641 opts->warnings_are_errors = 1;
1642 else if (!strcmp (argv[i], "-Wno-traditional"))
1643 opts->warn_stringify = 0;
1644 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1645 opts->warn_trigraphs = 0;
1646 else if (!strcmp (argv[i], "-Wno-comment"))
1647 opts->warn_comments = 0;
1648 else if (!strcmp (argv[i], "-Wno-comments"))
1649 opts->warn_comments = 0;
1650 else if (!strcmp (argv[i], "-Wno-undef"))
1651 opts->warn_undef = 0;
1652 else if (!strcmp (argv[i], "-Wno-import"))
1653 opts->warn_import = 0;
1654 else if (!strcmp (argv[i], "-Wno-error"))
1655 opts->warnings_are_errors = 0;
1656 break;
1657 }
1658 }
1659 return i + 1;
1660 }
1661
1662 #ifdef HOST_EBCDIC
1663 static int
1664 opt_comp (const void *p1, const void *p2)
1665 {
1666 return strcmp (((struct cl_option *)p1)->opt_text,
1667 ((struct cl_option *)p2)->opt_text);
1668 }
1669 #endif
1670
1671 /* Handle command-line options in (argc, argv).
1672 Can be called multiple times, to handle multiple sets of options.
1673 Returns if an unrecognized option is seen.
1674 Returns number of strings consumed. */
1675 int
1676 cpp_handle_options (pfile, argc, argv)
1677 cpp_reader *pfile;
1678 int argc;
1679 char **argv;
1680 {
1681 int i;
1682 int strings_processed;
1683
1684 #ifdef HOST_EBCDIC
1685 static int opts_sorted = 0;
1686
1687 if (!opts_sorted)
1688 {
1689 opts_sorted = 1;
1690 /* For non-ASCII hosts, the array needs to be sorted at runtime */
1691 qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
1692 }
1693 #endif
1694
1695 for (i = 0; i < argc; i += strings_processed)
1696 {
1697 strings_processed = handle_option (pfile, argc - i, argv + i);
1698 if (strings_processed == 0)
1699 break;
1700 }
1701 return i;
1702 }
1703
1704 static void
1705 print_help ()
1706 {
1707 fprintf (stderr, _("Usage: %s [switches] input output\n"), progname);
1708 fputs (_("\
1709 Switches:\n\
1710 -include <file> Include the contents of <file> before other files\n\
1711 -imacros <file> Accept definition of macros in <file>\n\
1712 -iprefix <path> Specify <path> as a prefix for next two options\n\
1713 -iwithprefix <dir> Add <dir> to the end of the system include path\n\
1714 -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\
1715 -isystem <dir> Add <dir> to the start of the system include path\n\
1716 -idirafter <dir> Add <dir> to the end of the system include path\n\
1717 -I <dir> Add <dir> to the end of the main include path\n\
1718 -I- Fine-grained include path control; see info docs\n\
1719 -nostdinc Do not search system include directories\n\
1720 (dirs specified with -isystem will still be used)\n\
1721 -nostdinc++ Do not search system include directories for C++\n\
1722 -o <file> Put output into <file>\n\
1723 -pedantic Issue all warnings demanded by strict ANSI C\n\
1724 -pedantic-errors Issue -pedantic warnings as errors instead\n\
1725 -traditional Follow K&R pre-processor behaviour\n\
1726 -trigraphs Support ANSI C trigraphs\n\
1727 -lang-c Assume that the input sources are in C\n\
1728 -lang-c89 Assume that the input sources are in C89\n\
1729 -lang-c++ Assume that the input sources are in C++\n\
1730 -lang-objc Assume that the input sources are in ObjectiveC\n\
1731 -lang-objc++ Assume that the input sources are in ObjectiveC++\n\
1732 -lang-asm Assume that the input sources are in assembler\n\
1733 -lang-fortran Assume that the input sources are in Fortran\n\
1734 -lang-chill Assume that the input sources are in Chill\n\
1735 -std=<std name> Specify the conformance standard; one of:\n\
1736 gnu89, gnu99, c89, c99, iso9899:1990,\n\
1737 iso9899:199409, iso9899:1999\n\
1738 -+ Allow parsing of C++ style features\n\
1739 -w Inhibit warning messages\n\
1740 -Wtrigraphs Warn if trigraphs are encountered\n\
1741 -Wno-trigraphs Do not warn about trigraphs\n\
1742 -Wcomment{s} Warn if one comment starts inside another\n\
1743 -Wno-comment{s} Do not warn about comments\n\
1744 -Wtraditional Warn if a macro argument is/would be turned into\n\
1745 a string if -traditional is specified\n\
1746 -Wno-traditional Do not warn about stringification\n\
1747 -Wundef Warn if an undefined macro is used by #if\n\
1748 -Wno-undef Do not warn about testing undefined macros\n\
1749 -Wimport Warn about the use of the #import directive\n\
1750 -Wno-import Do not warn about the use of #import\n\
1751 -Werror Treat all warnings as errors\n\
1752 -Wno-error Do not treat warnings as errors\n\
1753 -Wall Enable all preprocessor warnings\n\
1754 -M Generate make dependencies\n\
1755 -MM As -M, but ignore system header files\n\
1756 -MD As -M, but put output in a .d file\n\
1757 -MMD As -MD, but ignore system header files\n\
1758 -MG Treat missing header file as generated files\n\
1759 -g3 Include #define and #undef directives in the output\n\
1760 -D<macro> Define a <macro> with string '1' as its value\n\
1761 -D<macro>=<val> Define a <macro> with <val> as its value\n\
1762 -A<question> (<answer>) Assert the <answer> to <question>\n\
1763 -A-<question> (<answer>) Disable the <answer> to <question>\n\
1764 -U<macro> Undefine <macro> \n\
1765 -v Display the version number\n\
1766 -H Print the name of header files as they are used\n\
1767 -C Do not discard comments\n\
1768 -dM Display a list of macro definitions active at end\n\
1769 -dD Preserve macro definitions in output\n\
1770 -dN As -dD except that only the names are preserved\n\
1771 -dI Include #include directives in the output\n\
1772 -P Do not generate #line directives\n\
1773 -$ Do not allow '$' in identifiers\n\
1774 -remap Remap file names when including files.\n\
1775 --version Display version information\n\
1776 -h or --help Display this information\n\
1777 "), stdout);
1778 }