mkdeps.c, mkdeps.h: New files.
[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 "mkdeps.h"
32
33 /* Predefined symbols, built-in macros, and the default include path. */
34
35 #ifndef GET_ENV_PATH_LIST
36 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
37 #endif
38
39 #ifndef STANDARD_INCLUDE_DIR
40 #define STANDARD_INCLUDE_DIR "/usr/include"
41 #endif
42
43 /* We let tm.h override the types used here, to handle trivial differences
44 such as the choice of unsigned int or long unsigned int for size_t.
45 When machines start needing nontrivial differences in the size type,
46 it would be best to do something here to figure out automatically
47 from other information what type to use. */
48
49 /* The string value for __SIZE_TYPE__. */
50
51 #ifndef SIZE_TYPE
52 #define SIZE_TYPE "long unsigned int"
53 #endif
54
55 /* The string value for __PTRDIFF_TYPE__. */
56
57 #ifndef PTRDIFF_TYPE
58 #define PTRDIFF_TYPE "long int"
59 #endif
60
61 /* The string value for __WCHAR_TYPE__. */
62
63 #ifndef WCHAR_TYPE
64 #define WCHAR_TYPE "int"
65 #endif
66
67 /* The string value for __USER_LABEL_PREFIX__ */
68
69 #ifndef USER_LABEL_PREFIX
70 #define USER_LABEL_PREFIX ""
71 #endif
72
73 /* The string value for __REGISTER_PREFIX__ */
74
75 #ifndef REGISTER_PREFIX
76 #define REGISTER_PREFIX ""
77 #endif
78
79 /* This is the default list of directories to search for include files.
80 It may be overridden by the various -I and -ixxx options.
81
82 #include "file" looks in the same directory as the current file,
83 then this list.
84 #include <file> just looks in this list.
85
86 All these directories are treated as `system' include directories
87 (they are not subject to pedantic warnings in some cases). */
88
89 struct default_include
90 {
91 const char *fname; /* The name of the directory. */
92 const char *component; /* The component containing the directory
93 (see update_path in prefix.c) */
94 int cplusplus; /* Only look here if we're compiling C++. */
95 int cxx_aware; /* Includes in this directory don't need to
96 be wrapped in extern "C" when compiling
97 C++. */
98 };
99
100 static const struct default_include include_defaults_array[]
101 #ifdef INCLUDE_DEFAULTS
102 = INCLUDE_DEFAULTS;
103 #else
104 = {
105 /* Pick up GNU C++ specific include files. */
106 { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1 },
107 #ifdef CROSS_COMPILE
108 /* This is the dir for fixincludes. Put it just before
109 the files that we fix. */
110 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
111 /* For cross-compilation, this dir name is generated
112 automatically in Makefile.in. */
113 { CROSS_INCLUDE_DIR, "GCC", 0, 0 },
114 #ifdef TOOL_INCLUDE_DIR
115 /* This is another place that the target system's headers might be. */
116 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
117 #endif
118 #else /* not CROSS_COMPILE */
119 #ifdef LOCAL_INCLUDE_DIR
120 /* This should be /usr/local/include and should come before
121 the fixincludes-fixed header files. */
122 { LOCAL_INCLUDE_DIR, 0, 0, 1 },
123 #endif
124 #ifdef TOOL_INCLUDE_DIR
125 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
126 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
127 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
128 #endif
129 /* This is the dir for fixincludes. Put it just before
130 the files that we fix. */
131 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
132 /* Some systems have an extra dir of include files. */
133 #ifdef SYSTEM_INCLUDE_DIR
134 { SYSTEM_INCLUDE_DIR, 0, 0, 0 },
135 #endif
136 #ifndef STANDARD_INCLUDE_COMPONENT
137 #define STANDARD_INCLUDE_COMPONENT 0
138 #endif
139 { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0 },
140 #endif /* not CROSS_COMPILE */
141 { 0, 0, 0, 0 }
142 };
143 #endif /* no INCLUDE_DEFAULTS */
144
145 /* Internal structures and prototypes. */
146
147 /* A `struct pending_option' remembers one -D, -A, -U, -include, or -imacros
148 switch. There are four lists: one for -D and -U, one for -A, one
149 for -include, one for -imacros. `undef' is set for -U, clear for
150 -D, ignored for the others.
151 (Future: add an equivalent of -U for -A) */
152 struct pending_option
153 {
154 struct pending_option *next;
155 char *arg;
156 int undef;
157 };
158
159 #ifdef __STDC__
160 #define APPEND(pend, list, elt) \
161 do { if (!(pend)->list##_head) (pend)->list##_head = (elt); \
162 else (pend)->list##_tail->next = (elt); \
163 (pend)->list##_tail = (elt); \
164 } while (0)
165 #else
166 #define APPEND(pend, list, elt) \
167 do { if (!(pend)->list/**/_head) (pend)->list/**/_head = (elt); \
168 else (pend)->list/**/_tail->next = (elt); \
169 (pend)->list/**/_tail = (elt); \
170 } while (0)
171 #endif
172
173 static void print_help PARAMS ((void));
174 static void path_include PARAMS ((cpp_reader *,
175 struct cpp_pending *,
176 char *, int));
177 static void initialize_builtins PARAMS ((cpp_reader *));
178 static void append_include_chain PARAMS ((cpp_reader *,
179 struct cpp_pending *,
180 char *, int, int));
181 static char *base_name PARAMS ((const char *));
182 static void dump_special_to_buffer PARAMS ((cpp_reader *, const char *));
183 static void initialize_dependency_output PARAMS ((cpp_reader *));
184 static void initialize_standard_includes PARAMS ((cpp_reader *));
185 static void new_pending_define PARAMS ((struct cpp_options *,
186 const char *));
187
188 /* Fourth argument to append_include_chain: chain to use */
189 enum { QUOTE = 0, BRACKET, SYSTEM, AFTER };
190
191 /* If we have designated initializers (GCC >2.7, or C99) this table
192 can be initialized, constant data. Otherwise, it has to be filled
193 in at runtime. */
194
195 #if (GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L)
196 #define init_IStable() /* nothing */
197 #define ISTABLE const unsigned char _cpp_IStable[256] = {
198 #define END };
199 #define s(p, v) [p] = v,
200 #else
201 #define ISTABLE unsigned char _cpp_IStable[256] = { 0 }; \
202 static void init_IStable PARAMS ((void)) { \
203 unsigned char *x = _cpp_IStable;
204 #define END }
205 #define s(p, v) x[p] = v;
206 #endif
207
208 #define A(x) s(x, ISidnum|ISidstart)
209 #define N(x) s(x, ISidnum|ISnumstart)
210 #define H(x) s(x, IShspace|ISspace)
211 #define S(x) s(x, ISspace)
212
213 ISTABLE
214 A('_')
215
216 A('a') A('b') A('c') A('d') A('e') A('f') A('g') A('h') A('i')
217 A('j') A('k') A('l') A('m') A('n') A('o') A('p') A('q') A('r')
218 A('s') A('t') A('u') A('v') A('w') A('x') A('y') A('z')
219
220 A('A') A('B') A('C') A('D') A('E') A('F') A('G') A('H') A('I')
221 A('J') A('K') A('L') A('M') A('N') A('O') A('P') A('Q') A('R')
222 A('S') A('T') A('U') A('V') A('W') A('X') A('Y') A('Z')
223
224 N('1') N('2') N('3') N('4') N('5') N('6') N('7') N('8') N('9') N('0')
225
226 H(' ') H('\t') H('\v') H('\f')
227
228 S('\n')
229 END
230
231 #undef A
232 #undef N
233 #undef H
234 #undef S
235 #undef s
236 #undef ISTABLE
237 #undef END
238
239 /* Given a colon-separated list of file names PATH,
240 add all the names to the search path for include files. */
241
242 static void
243 path_include (pfile, pend, list, path)
244 cpp_reader *pfile;
245 struct cpp_pending *pend;
246 char *list;
247 int path;
248 {
249 char *p, *q, *name;
250
251 p = list;
252
253 do
254 {
255 /* Find the end of this name. */
256 q = p;
257 while (*q != 0 && *q != PATH_SEPARATOR) q++;
258 if (q == p)
259 {
260 /* An empty name in the path stands for the current directory. */
261 name = (char *) xmalloc (2);
262 name[0] = '.';
263 name[1] = 0;
264 }
265 else
266 {
267 /* Otherwise use the directory that is named. */
268 name = (char *) xmalloc (q - p + 1);
269 memcpy (name, p, q - p);
270 name[q - p] = 0;
271 }
272
273 append_include_chain (pfile, pend, name, path, 0);
274
275 /* Advance past this name. */
276 if (*q == 0)
277 break;
278 p = q + 1;
279 }
280 while (1);
281 }
282
283 /* Append DIR to include path PATH. DIR must be permanently allocated
284 and writable. */
285 static void
286 append_include_chain (pfile, pend, dir, path, cxx_aware)
287 cpp_reader *pfile;
288 struct cpp_pending *pend;
289 char *dir;
290 int path;
291 int cxx_aware;
292 {
293 struct file_name_list *new;
294 struct stat st;
295 unsigned int len;
296
297 simplify_pathname (dir);
298 if (stat (dir, &st))
299 {
300 /* Dirs that don't exist are silently ignored. */
301 if (errno != ENOENT)
302 cpp_notice_from_errno (pfile, dir);
303 else if (CPP_OPTIONS (pfile)->verbose)
304 fprintf (stderr, _("ignoring nonexistent directory `%s'\n"), dir);
305 return;
306 }
307
308 if (!S_ISDIR (st.st_mode))
309 {
310 cpp_notice (pfile, "%s: Not a directory", dir);
311 return;
312 }
313
314 len = strlen (dir);
315 if (len > pfile->max_include_len)
316 pfile->max_include_len = len;
317
318 new = (struct file_name_list *)xmalloc (sizeof (struct file_name_list));
319 new->name = dir;
320 new->nlen = len;
321 new->ino = st.st_ino;
322 new->dev = st.st_dev;
323 if (path == SYSTEM)
324 new->sysp = cxx_aware ? 1 : 2;
325 else
326 new->sysp = 0;
327 new->name_map = NULL;
328 new->next = NULL;
329 new->alloc = NULL;
330
331 switch (path)
332 {
333 case QUOTE: APPEND (pend, quote, new); break;
334 case BRACKET: APPEND (pend, brack, new); break;
335 case SYSTEM: APPEND (pend, systm, new); break;
336 case AFTER: APPEND (pend, after, new); break;
337 }
338 }
339
340
341 /* Write out a #define command for the special named MACRO_NAME
342 to PFILE's token_buffer. */
343
344 static void
345 dump_special_to_buffer (pfile, macro_name)
346 cpp_reader *pfile;
347 const char *macro_name;
348 {
349 static const char define_directive[] = "#define ";
350 int macro_name_length = strlen (macro_name);
351 output_line_command (pfile, same_file);
352 CPP_RESERVE (pfile, sizeof(define_directive) + macro_name_length);
353 CPP_PUTS_Q (pfile, define_directive, sizeof(define_directive)-1);
354 CPP_PUTS_Q (pfile, macro_name, macro_name_length);
355 CPP_PUTC_Q (pfile, ' ');
356 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
357 CPP_PUTC (pfile, '\n');
358 }
359
360 /* Initialize a cpp_options structure. */
361 void
362 cpp_options_init (opts)
363 cpp_options *opts;
364 {
365 bzero ((char *) opts, sizeof (struct cpp_options));
366
367 opts->dollars_in_ident = 1;
368 opts->cplusplus_comments = 1;
369 opts->warn_import = 1;
370 opts->discard_comments = 1;
371
372 opts->pending =
373 (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
374 }
375
376 /* Initialize a cpp_reader structure. */
377 void
378 cpp_reader_init (pfile)
379 cpp_reader *pfile;
380 {
381 bzero ((char *) pfile, sizeof (cpp_reader));
382
383 pfile->token_buffer_size = 200;
384 pfile->token_buffer = (U_CHAR *) xmalloc (pfile->token_buffer_size);
385 CPP_SET_WRITTEN (pfile, 0);
386
387 pfile->hashtab = (HASHNODE **) xcalloc (HASHSIZE, sizeof (HASHNODE *));
388 }
389
390 /* Free resources used by PFILE.
391 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
392 void
393 cpp_cleanup (pfile)
394 cpp_reader *pfile;
395 {
396 int i;
397 while (CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile))
398 cpp_pop_buffer (pfile);
399
400 if (pfile->token_buffer)
401 {
402 free (pfile->token_buffer);
403 pfile->token_buffer = NULL;
404 }
405
406 if (pfile->input_buffer)
407 {
408 free (pfile->input_buffer);
409 free (pfile->input_speccase);
410 pfile->input_buffer = pfile->input_speccase = NULL;
411 pfile->input_buffer_len = 0;
412 }
413
414 if (pfile->deps)
415 deps_free (pfile->deps);
416
417 while (pfile->if_stack)
418 {
419 IF_STACK_FRAME *temp = pfile->if_stack;
420 pfile->if_stack = temp->next;
421 free (temp);
422 }
423
424 for (i = ALL_INCLUDE_HASHSIZE; --i >= 0; )
425 {
426 struct include_hash *imp = pfile->all_include_files[i];
427 while (imp)
428 {
429 struct include_hash *next = imp->next;
430
431 free ((PTR) imp->name);
432 free (imp);
433 imp = next;
434 }
435 pfile->all_include_files[i] = 0;
436 }
437
438 for (i = HASHSIZE; --i >= 0;)
439 {
440 while (pfile->hashtab[i])
441 delete_macro (pfile->hashtab[i]);
442 }
443 free (pfile->hashtab);
444 }
445
446
447 /* This structure defines one built-in macro. A node of type TYPE will
448 be entered in the macro hash table under the name NAME, with value
449 VALUE (if any). FLAGS tweaks the behavior a little:
450 DUMP write debug info for this macro
451 STDC define only if not -traditional
452 ULP value is the global user_label_prefix (which can't be
453 put directly into the table).
454 */
455
456 struct builtin
457 {
458 const char *name;
459 const char *value;
460 unsigned short type;
461 unsigned short flags;
462 };
463 #define DUMP 0x01
464 #define STDC 0x02
465 #define ULP 0x10
466
467 static const struct builtin builtin_array[] =
468 {
469 { "__TIME__", 0, T_TIME, DUMP },
470 { "__DATE__", 0, T_DATE, DUMP },
471 { "__FILE__", 0, T_FILE, 0 },
472 { "__BASE_FILE__", 0, T_BASE_FILE, 0 },
473 { "__LINE__", 0, T_SPECLINE, 0 },
474 { "__INCLUDE_LEVEL__", 0, T_INCLUDE_LEVEL, 0 },
475 { "__VERSION__", 0, T_VERSION, DUMP },
476 { "__STDC__", 0, T_STDC, DUMP|STDC },
477
478 { "__USER_LABEL_PREFIX__", 0, T_CONST, ULP },
479 { "__REGISTER_PREFIX__", REGISTER_PREFIX, T_CONST, 0 },
480 { "__HAVE_BUILTIN_SETJMP__", "1", T_CONST, 0 },
481 #ifndef NO_BUILTIN_SIZE_TYPE
482 { "__SIZE_TYPE__", SIZE_TYPE, T_CONST, DUMP },
483 #endif
484 #ifndef NO_BUILTIN_PTRDIFF_TYPE
485 { "__PTRDIFF_TYPE__", PTRDIFF_TYPE, T_CONST, DUMP },
486 #endif
487 #ifndef NO_BUILTIN_WCHAR_TYPE
488 { "__WCHAR_TYPE__", WCHAR_TYPE, T_CONST, DUMP },
489 #endif
490 { 0, 0, 0, 0 }
491 };
492
493 /* Subroutine of cpp_start_read; reads the builtins table above and
494 enters the macros into the hash table. */
495
496 static void
497 initialize_builtins (pfile)
498 cpp_reader *pfile;
499 {
500 int len;
501 const struct builtin *b;
502 const char *val;
503 for(b = builtin_array; b->name; b++)
504 {
505 if ((b->flags & STDC) && CPP_TRADITIONAL (pfile))
506 continue;
507
508 val = (b->flags & ULP) ? user_label_prefix : b->value;
509 len = strlen (b->name);
510
511 cpp_install (pfile, b->name, len, b->type, val);
512 if ((b->flags & DUMP) && CPP_OPTIONS (pfile)->debug_output)
513 dump_special_to_buffer (pfile, b->name);
514 }
515
516 }
517 #undef DUMP
518 #undef STDC
519 #undef ULP
520
521 /* Another subroutine of cpp_start_read. This one sets up to do
522 dependency-file output. */
523 static void
524 initialize_dependency_output (pfile)
525 cpp_reader *pfile;
526 {
527 cpp_options *opts = CPP_OPTIONS (pfile);
528 char *spec, *s, *output_file;
529
530 /* Either of two environment variables can specify output of deps.
531 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
532 where OUTPUT_FILE is the file to write deps info to
533 and DEPS_TARGET is the target to mention in the deps. */
534
535 if (opts->print_deps == 0)
536 {
537 spec = getenv ("DEPENDENCIES_OUTPUT");
538 if (spec)
539 opts->print_deps = 1;
540 else
541 {
542 spec = getenv ("SUNPRO_DEPENDENCIES");
543 if (spec)
544 opts->print_deps = 2;
545 else
546 return;
547 }
548
549 /* Find the space before the DEPS_TARGET, if there is one. */
550 s = strchr (spec, ' ');
551 if (s)
552 {
553 opts->deps_target = s + 1;
554 output_file = (char *) xmalloc (s - spec + 1);
555 memcpy (output_file, spec, s - spec);
556 output_file[s - spec] = 0;
557 }
558 else
559 {
560 opts->deps_target = 0;
561 output_file = spec;
562 }
563
564 opts->deps_file = output_file;
565 opts->print_deps_append = 1;
566 }
567
568 pfile->deps = deps_init ();
569
570 /* Print the expected object file name as the target of this Make-rule. */
571 if (opts->deps_target)
572 deps_add_target (pfile->deps, opts->deps_target);
573 else if (*opts->in_fname == 0)
574 deps_add_target (pfile->deps, "-");
575 else
576 deps_calc_target (pfile->deps, opts->in_fname);
577
578 if (opts->in_fname)
579 deps_add_dep (pfile->deps, opts->in_fname);
580 }
581
582 /* And another subroutine. This one sets up the standard include path. */
583 static void
584 initialize_standard_includes (pfile)
585 cpp_reader *pfile;
586 {
587 cpp_options *opts = CPP_OPTIONS (pfile);
588 char *path;
589 const struct default_include *p;
590 char *specd_prefix = opts->include_prefix;
591
592 /* Several environment variables may add to the include search path.
593 CPATH specifies an additional list of directories to be searched
594 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
595 etc. specify an additional list of directories to be searched as
596 if specified with -isystem, for the language indicated. */
597
598 GET_ENV_PATH_LIST (path, "CPATH");
599 if (path != 0 && *path != 0)
600 path_include (pfile, opts->pending, path, BRACKET);
601
602 switch ((opts->objc << 1) + opts->cplusplus)
603 {
604 case 0:
605 GET_ENV_PATH_LIST (path, "C_INCLUDE_PATH");
606 break;
607 case 1:
608 GET_ENV_PATH_LIST (path, "CPLUS_INCLUDE_PATH");
609 break;
610 case 2:
611 GET_ENV_PATH_LIST (path, "OBJC_INCLUDE_PATH");
612 break;
613 case 3:
614 GET_ENV_PATH_LIST (path, "OBJCPLUS_INCLUDE_PATH");
615 break;
616 }
617 if (path != 0 && *path != 0)
618 path_include (pfile, opts->pending, path, SYSTEM);
619
620 /* Search "translated" versions of GNU directories.
621 These have /usr/local/lib/gcc... replaced by specd_prefix. */
622 if (specd_prefix != 0)
623 {
624 char *default_prefix = alloca (sizeof GCC_INCLUDE_DIR - 7);
625 /* Remove the `include' from /usr/local/lib/gcc.../include.
626 GCC_INCLUDE_DIR will always end in /include. */
627 int default_len = sizeof GCC_INCLUDE_DIR - 8;
628 int specd_len = strlen (specd_prefix);
629
630 memcpy (default_prefix, GCC_INCLUDE_DIR, default_len);
631 default_prefix[default_len] = '\0';
632
633 for (p = include_defaults_array; p->fname; p++)
634 {
635 /* Some standard dirs are only for C++. */
636 if (!p->cplusplus
637 || (opts->cplusplus
638 && !opts->no_standard_cplusplus_includes))
639 {
640 /* Does this dir start with the prefix? */
641 if (!strncmp (p->fname, default_prefix, default_len))
642 {
643 /* Yes; change prefix and add to search list. */
644 int flen = strlen (p->fname);
645 int this_len = specd_len + flen - default_len;
646 char *str = (char *) xmalloc (this_len + 1);
647 memcpy (str, specd_prefix, specd_len);
648 memcpy (str + specd_len,
649 p->fname + default_len,
650 flen - default_len + 1);
651
652 append_include_chain (pfile, opts->pending,
653 str, SYSTEM, p->cxx_aware);
654 }
655 }
656 }
657 }
658
659 /* Search ordinary names for GNU include directories. */
660 for (p = include_defaults_array; p->fname; p++)
661 {
662 /* Some standard dirs are only for C++. */
663 if (!p->cplusplus
664 || (opts->cplusplus
665 && !opts->no_standard_cplusplus_includes))
666 {
667 /* XXX Potential memory leak! */
668 char *str = xstrdup (update_path (p->fname, p->component));
669 append_include_chain (pfile, opts->pending, str, SYSTEM,
670 p->cxx_aware);
671 }
672 }
673 }
674
675 /* This is called after options have been processed.
676 * Check options for consistency, and setup for processing input
677 * from the file named FNAME. (Use standard input if FNAME==NULL.)
678 * Return 1 on success, 0 on failure.
679 */
680
681 int
682 cpp_start_read (pfile, fname)
683 cpp_reader *pfile;
684 char *fname;
685 {
686 struct cpp_options *opts = CPP_OPTIONS (pfile);
687 struct pending_option *p, *q;
688
689 /* -MG doesn't select the form of output and must be specified with one of
690 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
691 inhibit compilation. */
692 if (opts->print_deps_missing_files
693 && (opts->print_deps == 0 || !opts->no_output))
694 {
695 cpp_fatal (pfile, "-MG must be specified with one of -M or -MM");
696 return 0;
697 }
698
699 /* Chill should not be used with -trigraphs. */
700 if (opts->chill && opts->trigraphs)
701 {
702 cpp_warning (pfile, "-lang-chill and -trigraphs are mutually exclusive");
703 opts->trigraphs = 0;
704 }
705
706 /* Set this if it hasn't been set already. */
707 if (user_label_prefix == NULL)
708 user_label_prefix = USER_LABEL_PREFIX;
709
710 /* Don't bother trying to do macro expansion if we've already done
711 preprocessing. */
712 if (opts->preprocessed)
713 pfile->no_macro_expand++;
714
715 /* Set up the IStable. This doesn't do anything if we were compiled
716 with a compiler that supports C99 designated initializers. */
717 init_IStable ();
718
719 /* Set up the include search path now. */
720 if (! opts->no_standard_includes)
721 initialize_standard_includes (pfile);
722
723 merge_include_chains (opts);
724
725 /* With -v, print the list of dirs to search. */
726 if (opts->verbose)
727 {
728 struct file_name_list *l;
729 fprintf (stderr, _("#include \"...\" search starts here:\n"));
730 for (l = opts->quote_include; l; l = l->next)
731 {
732 if (l == opts->bracket_include)
733 fprintf (stderr, _("#include <...> search starts here:\n"));
734 fprintf (stderr, " %s\n", l->name);
735 }
736 fprintf (stderr, _("End of search list.\n"));
737 }
738
739 initialize_dependency_output (pfile);
740
741 /* Open the main input file. This must be done before -D processing
742 so we have a buffer to stand on. */
743 if (opts->in_fname == NULL || *opts->in_fname == 0)
744 {
745 opts->in_fname = fname;
746 if (opts->in_fname == NULL)
747 opts->in_fname = "";
748 }
749
750 if (!cpp_read_file (pfile, fname))
751 return 0;
752
753 /* -D and friends may produce output, which should be identified
754 as line 0. */
755
756 CPP_BUFFER (pfile)->lineno = 0;
757
758 /* Install __LINE__, etc. */
759 initialize_builtins (pfile);
760
761 /* Do -U's, -D's and -A's in the order they were seen. */
762 p = opts->pending->define_head;
763 while (p)
764 {
765 if (p->undef)
766 cpp_undef (pfile, p->arg);
767 else
768 cpp_define (pfile, p->arg);
769
770 q = p->next;
771 free (p);
772 p = q;
773 }
774
775 p = opts->pending->assert_head;
776 while (p)
777 {
778 if (p->undef)
779 cpp_unassert (pfile, p->arg);
780 else
781 cpp_assert (pfile, p->arg);
782
783 q = p->next;
784 free (p);
785 p = q;
786 }
787
788 opts->done_initializing = 1;
789 CPP_BUFFER (pfile)->lineno = 1;
790
791 if (opts->preprocessed)
792 /* If we've already processed this code, we want to trust the #line
793 directives in the input. But we still need to update our line
794 counter accordingly. */
795 pfile->lineno = CPP_BUFFER (pfile)->lineno;
796 else
797 output_line_command (pfile, same_file);
798 pfile->only_seen_white = 2;
799
800 /* The -imacros files can be scanned now, but the -include files
801 have to be pushed onto the include stack and processed later,
802 in the main loop calling cpp_get_token. */
803
804 opts->no_output++;
805 p = opts->pending->imacros_head;
806 while (p)
807 {
808 if (cpp_read_file (pfile, p->arg))
809 cpp_scan_buffer (pfile);
810
811 q = p->next;
812 free (p);
813 p = q;
814 }
815 opts->no_output--;
816
817 p = opts->pending->include_head;
818 while (p)
819 {
820 if (cpp_read_file (pfile, p->arg))
821 output_line_command (pfile, enter_file);
822
823 q = p->next;
824 free (p);
825 p = q;
826 }
827
828 free (opts->pending);
829 opts->pending = NULL;
830
831 return 1;
832 }
833
834 /* This is called at the end of preprocessing. It pops the
835 last buffer and writes dependency output. It should also
836 clear macro definitions, such that you could call cpp_start_read
837 with a new filename to restart processing. */
838 void
839 cpp_finish (pfile)
840 cpp_reader *pfile;
841 {
842 struct cpp_options *opts = CPP_OPTIONS (pfile);
843
844 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) != CPP_NULL_BUFFER (pfile))
845 cpp_ice (pfile, "buffers still stacked in cpp_finish");
846 cpp_pop_buffer (pfile);
847
848 /* Don't write the deps file if preprocessing has failed. */
849 if (opts->print_deps && pfile->errors == 0)
850 {
851 /* Stream on which to print the dependency information. */
852 FILE *deps_stream = 0;
853
854 const char *deps_mode = opts->print_deps_append ? "a" : "w";
855 if (opts->deps_file == 0)
856 deps_stream = stdout;
857 else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
858 cpp_notice_from_errno (pfile, opts->deps_file);
859
860 if (deps_stream)
861 {
862 deps_write (pfile->deps, deps_stream, 72);
863 if (opts->deps_file)
864 {
865 if (ferror (deps_stream) || fclose (deps_stream) != 0)
866 cpp_fatal (pfile, "I/O error on output");
867 }
868 }
869 }
870
871 if (opts->dump_macros == dump_only)
872 {
873 int i;
874 HASHNODE *h;
875 for (i = HASHSIZE; --i >= 0;)
876 {
877 for (h = pfile->hashtab[i]; h; h = h->next)
878 if (h->type == T_MACRO)
879 {
880 dump_definition (pfile, h->name, h->length, h->value.defn);
881 CPP_PUTC (pfile, '\n');
882 }
883 }
884 }
885 }
886
887 static void
888 new_pending_define (opts, text)
889 struct cpp_options *opts;
890 const char *text;
891 {
892 struct pending_option *o = (struct pending_option *)
893 xmalloc (sizeof (struct pending_option));
894
895 o->arg = (char *) text;
896 o->next = NULL;
897 o->undef = 0;
898 APPEND (opts->pending, define, o);
899 }
900
901 /* Handle one command-line option in (argc, argv).
902 Can be called multiple times, to handle multiple sets of options.
903 Returns number of strings consumed. */
904
905 int
906 cpp_handle_option (pfile, argc, argv)
907 cpp_reader *pfile;
908 int argc;
909 char **argv;
910 {
911 struct cpp_options *opts = CPP_OPTIONS (pfile);
912 int i = 0;
913
914 if (argv[i][0] != '-')
915 {
916 if (opts->out_fname != NULL)
917 {
918 print_help ();
919 cpp_fatal (pfile, "Too many arguments");
920 }
921 else if (opts->in_fname != NULL)
922 opts->out_fname = argv[i];
923 else
924 opts->in_fname = argv[i];
925 }
926 else
927 switch (argv[i][1])
928 {
929 case 'f':
930 if (!strcmp (argv[i], "-fleading-underscore"))
931 user_label_prefix = "_";
932 else if (!strcmp (argv[i], "-fno-leading-underscore"))
933 user_label_prefix = "";
934 else if (!strcmp (argv[i], "-fpreprocessed"))
935 opts->preprocessed = 1;
936 else if (!strcmp (argv[i], "-fno-preprocessed"))
937 opts->preprocessed = 0;
938 else
939 {
940 return i;
941 }
942 break;
943
944 case 'I': /* Add directory to path for includes. */
945 if (!strcmp (argv[i] + 2, "-"))
946 {
947 /* -I- means:
948 Use the preceding -I directories for #include "..."
949 but not #include <...>.
950 Don't search the directory of the present file
951 for #include "...". (Note that -I. -I- is not the same as
952 the default setup; -I. uses the compiler's working dir.) */
953 if (! opts->ignore_srcdir)
954 {
955 opts->ignore_srcdir = 1;
956 opts->pending->quote_head = opts->pending->brack_head;
957 opts->pending->quote_tail = opts->pending->brack_tail;
958 opts->pending->brack_head = 0;
959 opts->pending->brack_tail = 0;
960 }
961 else
962 {
963 cpp_fatal (pfile, "-I- specified twice");
964 return argc;
965 }
966 }
967 else
968 {
969 char *fname;
970 if (argv[i][2] != 0)
971 fname = argv[i] + 2;
972 else if (i + 1 == argc)
973 goto missing_dirname;
974 else
975 fname = argv[++i];
976 append_include_chain (pfile, opts->pending,
977 xstrdup (fname), BRACKET, 0);
978 }
979 break;
980
981 case 'i':
982 /* Add directory to beginning of system include path, as a system
983 include directory. */
984 if (!strcmp (argv[i], "-isystem"))
985 {
986 if (i + 1 == argc)
987 goto missing_filename;
988 append_include_chain (pfile, opts->pending,
989 xstrdup (argv[++i]), SYSTEM, 0);
990 }
991 else if (!strcmp (argv[i], "-include"))
992 {
993 if (i + 1 == argc)
994 goto missing_filename;
995 else
996 {
997 struct pending_option *o = (struct pending_option *)
998 xmalloc (sizeof (struct pending_option));
999 o->arg = argv[++i];
1000
1001 /* This list has to be built in reverse order so that
1002 when cpp_start_read pushes all the -include files onto
1003 the buffer stack, they will be scanned in forward order. */
1004 o->next = opts->pending->include_head;
1005 opts->pending->include_head = o;
1006 }
1007 }
1008 else if (!strcmp (argv[i], "-imacros"))
1009 {
1010 if (i + 1 == argc)
1011 goto missing_filename;
1012 else
1013 {
1014 struct pending_option *o = (struct pending_option *)
1015 xmalloc (sizeof (struct pending_option));
1016 o->arg = argv[++i];
1017 o->next = NULL;
1018
1019 APPEND (opts->pending, imacros, o);
1020 }
1021 }
1022 /* Add directory to end of path for includes,
1023 with the default prefix at the front of its name. */
1024 else if (!strcmp (argv[i], "-iwithprefix"))
1025 {
1026 char *fname;
1027 int len;
1028 if (i + 1 == argc)
1029 goto missing_dirname;
1030 ++i;
1031 len = strlen (argv[i]);
1032
1033 if (opts->include_prefix != 0)
1034 {
1035 fname = xmalloc (opts->include_prefix_len + len + 1);
1036 memcpy (fname, opts->include_prefix, opts->include_prefix_len);
1037 memcpy (fname + opts->include_prefix_len, argv[i], len + 1);
1038 }
1039 else
1040 {
1041 fname = xmalloc (sizeof GCC_INCLUDE_DIR - 8 + len);
1042 memcpy (fname, GCC_INCLUDE_DIR, sizeof GCC_INCLUDE_DIR - 9);
1043 memcpy (fname + sizeof GCC_INCLUDE_DIR - 9, argv[i], len + 1);
1044 }
1045
1046 append_include_chain (pfile, opts->pending, fname, SYSTEM, 0);
1047 }
1048 /* Add directory to main path for includes,
1049 with the default prefix at the front of its name. */
1050 else if (!strcmp (argv[i], "-iwithprefixbefore"))
1051 {
1052 char *fname;
1053 int len;
1054 if (i + 1 == argc)
1055 goto missing_dirname;
1056 ++i;
1057 len = strlen (argv[i]);
1058
1059 if (opts->include_prefix != 0)
1060 {
1061 fname = xmalloc (opts->include_prefix_len + len + 1);
1062 memcpy (fname, opts->include_prefix, opts->include_prefix_len);
1063 memcpy (fname + opts->include_prefix_len, argv[i], len + 1);
1064 }
1065 else
1066 {
1067 fname = xmalloc (sizeof GCC_INCLUDE_DIR - 8 + len);
1068 memcpy (fname, GCC_INCLUDE_DIR, sizeof GCC_INCLUDE_DIR - 9);
1069 memcpy (fname + sizeof GCC_INCLUDE_DIR - 9, argv[i], len + 1);
1070 }
1071
1072 append_include_chain (pfile, opts->pending, fname, BRACKET, 0);
1073 }
1074 /* Add directory to end of path for includes. */
1075 else if (!strcmp (argv[i], "-idirafter"))
1076 {
1077 if (i + 1 == argc)
1078 goto missing_dirname;
1079 append_include_chain (pfile, opts->pending,
1080 xstrdup (argv[++i]), AFTER, 0);
1081 }
1082 else if (!strcmp (argv[i], "-iprefix"))
1083 {
1084 if (i + 1 == argc)
1085 goto missing_filename;
1086 else
1087 {
1088 opts->include_prefix = argv[++i];
1089 opts->include_prefix_len = strlen (argv[i]);
1090 }
1091 }
1092 break;
1093
1094 case 'o':
1095 if (opts->out_fname != NULL)
1096 {
1097 cpp_fatal (pfile, "Output filename specified twice");
1098 return argc;
1099 }
1100 if (i + 1 == argc)
1101 goto missing_filename;
1102 opts->out_fname = argv[++i];
1103 if (!strcmp (opts->out_fname, "-"))
1104 opts->out_fname = "";
1105 break;
1106
1107 case 'p':
1108 if (!strcmp (argv[i], "-pedantic"))
1109 opts->pedantic = 1;
1110 else if (!strcmp (argv[i], "-pedantic-errors"))
1111 {
1112 opts->pedantic = 1;
1113 opts->pedantic_errors = 1;
1114 }
1115 break;
1116
1117 case 't':
1118 if (!strcmp (argv[i], "-traditional"))
1119 {
1120 opts->traditional = 1;
1121 opts->cplusplus_comments = 0;
1122 opts->trigraphs = 0;
1123 opts->warn_trigraphs = 0;
1124 }
1125 else if (!strcmp (argv[i], "-trigraphs"))
1126 opts->trigraphs = 1;
1127 break;
1128
1129 case 'l':
1130 if (! strcmp (argv[i], "-lang-c"))
1131 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
1132 opts->c99 = 1, opts->objc = 0;
1133 if (! strcmp (argv[i], "-lang-c89"))
1134 {
1135 opts->cplusplus = 0, opts->cplusplus_comments = 0;
1136 opts->c89 = 1, opts->c99 = 0, opts->objc = 0;
1137 opts->trigraphs = 1;
1138 new_pending_define (opts, "__STRICT_ANSI__");
1139 }
1140 if (! strcmp (argv[i], "-lang-c++"))
1141 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
1142 opts->c99 = 0, opts->objc = 0;
1143 if (! strcmp (argv[i], "-lang-objc"))
1144 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
1145 opts->c99 = 0, opts->objc = 1;
1146 if (! strcmp (argv[i], "-lang-objc++"))
1147 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
1148 opts->c99 = 0, opts->objc = 1;
1149 if (! strcmp (argv[i], "-lang-asm"))
1150 opts->lang_asm = 1;
1151 if (! strcmp (argv[i], "-lang-fortran"))
1152 opts->lang_fortran = 1, opts->cplusplus_comments = 0;
1153 if (! strcmp (argv[i], "-lang-chill"))
1154 opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
1155 opts->traditional = 1;
1156 break;
1157
1158 case '+':
1159 opts->cplusplus = 1, opts->cplusplus_comments = 1;
1160 break;
1161
1162 case 's':
1163 if (!strcmp (argv[i], "-std=gnu89"))
1164 {
1165 opts->cplusplus = 0, opts->cplusplus_comments = 1;
1166 opts->c89 = 1, opts->c99 = 0, opts->objc = 0;
1167 }
1168 else if (!strcmp (argv[i], "-std=gnu9x")
1169 || !strcmp (argv[i], "-std=gnu99"))
1170 {
1171 opts->cplusplus = 0, opts->cplusplus_comments = 1;
1172 opts->c89 = 0, opts->c99 = 1, opts->objc = 0;
1173 new_pending_define (opts, "__STDC_VERSION__=199901L");
1174 }
1175 else if (!strcmp (argv[i], "-std=iso9899:1990")
1176 || !strcmp (argv[i], "-std=c89"))
1177 {
1178 opts->cplusplus = 0, opts->cplusplus_comments = 0;
1179 opts->c89 = 1, opts->c99 = 0, opts->objc = 0;
1180 opts->trigraphs = 1;
1181 new_pending_define (opts, "__STRICT_ANSI__");
1182 }
1183 else if (!strcmp (argv[i], "-std=iso9899:199409"))
1184 {
1185 opts->cplusplus = 0, opts->cplusplus_comments = 0;
1186 opts->c89 = 1, opts->c99 = 0, opts->objc = 0;
1187 opts->trigraphs = 1;
1188 new_pending_define (opts, "__STRICT_ANSI__");
1189 new_pending_define (opts, "__STDC_VERSION__=199409L");
1190 }
1191 else if (!strcmp (argv[i], "-std=iso9899:199x")
1192 || !strcmp (argv[i], "-std=iso9899:1999")
1193 || !strcmp (argv[i], "-std=c9x")
1194 || !strcmp (argv[i], "-std=c99"))
1195 {
1196 opts->cplusplus = 0, opts->cplusplus_comments = 1;
1197 opts->c89 = 0, opts->c99 = 1, opts->objc = 0;
1198 opts->trigraphs = 1;
1199 new_pending_define (opts, "__STRICT_ANSI__");
1200 new_pending_define (opts, "__STDC_VERSION__=199901L");
1201 }
1202 break;
1203
1204 case 'w':
1205 opts->inhibit_warnings = 1;
1206 break;
1207
1208 case 'W':
1209 if (!strcmp (argv[i], "-Wtrigraphs"))
1210 opts->warn_trigraphs = 1;
1211 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1212 opts->warn_trigraphs = 0;
1213 else if (!strcmp (argv[i], "-Wcomment"))
1214 opts->warn_comments = 1;
1215 else if (!strcmp (argv[i], "-Wno-comment"))
1216 opts->warn_comments = 0;
1217 else if (!strcmp (argv[i], "-Wcomments"))
1218 opts->warn_comments = 1;
1219 else if (!strcmp (argv[i], "-Wno-comments"))
1220 opts->warn_comments = 0;
1221 else if (!strcmp (argv[i], "-Wtraditional"))
1222 opts->warn_stringify = 1;
1223 else if (!strcmp (argv[i], "-Wno-traditional"))
1224 opts->warn_stringify = 0;
1225 else if (!strcmp (argv[i], "-Wundef"))
1226 opts->warn_undef = 1;
1227 else if (!strcmp (argv[i], "-Wno-undef"))
1228 opts->warn_undef = 0;
1229 else if (!strcmp (argv[i], "-Wimport"))
1230 opts->warn_import = 1;
1231 else if (!strcmp (argv[i], "-Wno-import"))
1232 opts->warn_import = 0;
1233 else if (!strcmp (argv[i], "-Werror"))
1234 opts->warnings_are_errors = 1;
1235 else if (!strcmp (argv[i], "-Wno-error"))
1236 opts->warnings_are_errors = 0;
1237 else if (!strcmp (argv[i], "-Wall"))
1238 {
1239 opts->warn_trigraphs = 1;
1240 opts->warn_comments = 1;
1241 }
1242 break;
1243
1244 case 'M':
1245 /* The style of the choices here is a bit mixed.
1246 The chosen scheme is a hybrid of keeping all options in one string
1247 and specifying each option in a separate argument:
1248 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
1249 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1250 -M[M][G][D file]. This is awkward to handle in specs, and is not
1251 as extensible. */
1252 /* ??? -MG must be specified in addition to one of -M or -MM.
1253 This can be relaxed in the future without breaking anything.
1254 The converse isn't true. */
1255
1256 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
1257 if (!strcmp (argv[i], "-MG"))
1258 {
1259 opts->print_deps_missing_files = 1;
1260 break;
1261 }
1262 if (!strcmp (argv[i], "-M"))
1263 opts->print_deps = 2;
1264 else if (!strcmp (argv[i], "-MM"))
1265 opts->print_deps = 1;
1266 else if (!strcmp (argv[i], "-MD"))
1267 opts->print_deps = 2;
1268 else if (!strcmp (argv[i], "-MMD"))
1269 opts->print_deps = 1;
1270 /* For -MD and -MMD options, write deps on file named by next arg. */
1271 if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD"))
1272 {
1273 if (i+1 == argc)
1274 goto missing_filename;
1275 opts->deps_file = argv[++i];
1276 }
1277 else
1278 {
1279 /* For -M and -MM, write deps on standard output
1280 and suppress the usual output. */
1281 opts->no_output = 1;
1282 }
1283 break;
1284
1285 case 'd':
1286 {
1287 char *p = argv[i] + 2;
1288 char c;
1289 while ((c = *p++) != 0)
1290 {
1291 /* Arg to -d specifies what parts of macros to dump */
1292 switch (c)
1293 {
1294 case 'M':
1295 opts->dump_macros = dump_only;
1296 opts->no_output = 1;
1297 break;
1298 case 'N':
1299 opts->dump_macros = dump_names;
1300 break;
1301 case 'D':
1302 opts->dump_macros = dump_definitions;
1303 break;
1304 case 'I':
1305 opts->dump_includes = 1;
1306 break;
1307 }
1308 }
1309 }
1310 break;
1311
1312 case 'g':
1313 if (argv[i][2] == '3')
1314 opts->debug_output = 1;
1315 break;
1316
1317 case '-':
1318 if (!strcmp (argv[i], "--help"))
1319 print_help ();
1320 else if (!strcmp (argv[i], "--version"))
1321 fprintf (stderr, _("GNU CPP version %s (cpplib)\n"), version_string);
1322 exit (0); /* XXX */
1323 break;
1324
1325 case 'v':
1326 fprintf (stderr, _("GNU CPP version %s (cpplib)\n"), version_string);
1327 #ifdef TARGET_VERSION
1328 TARGET_VERSION;
1329 #endif
1330 fputc ('\n', stderr);
1331 opts->verbose = 1;
1332 break;
1333
1334 case 'H':
1335 opts->print_include_names = 1;
1336 break;
1337
1338 case 'D':
1339 {
1340 const char *text;
1341 if (argv[i][2] != 0)
1342 text = argv[i] + 2;
1343 else if (i + 1 == argc)
1344 {
1345 cpp_fatal (pfile, "Macro name missing after -D option");
1346 return argc;
1347 }
1348 else
1349 text = argv[++i];
1350 new_pending_define (opts, text);
1351 }
1352 break;
1353
1354 case 'A':
1355 {
1356 char *p;
1357
1358 if (argv[i][2] != 0)
1359 p = argv[i] + 2;
1360 else if (i + 1 == argc)
1361 {
1362 cpp_fatal (pfile, "Assertion missing after -A option");
1363 return argc;
1364 }
1365 else
1366 p = argv[++i];
1367
1368 if (strcmp (p, "-"))
1369 {
1370 struct pending_option *o = (struct pending_option *)
1371 xmalloc (sizeof (struct pending_option));
1372
1373 o->arg = p;
1374 o->next = NULL;
1375 o->undef = 0;
1376 APPEND (opts->pending, assert, o);
1377 }
1378 else
1379 {
1380 /* -A- eliminates all predefined macros and assertions.
1381 Let's include also any that were specified earlier
1382 on the command line. That way we can get rid of any
1383 that were passed automatically in from GCC. */
1384 struct pending_option *o1, *o2;
1385
1386 o1 = opts->pending->define_head;
1387 while (o1)
1388 {
1389 o2 = o1->next;
1390 free (o1);
1391 o1 = o2;
1392 }
1393 o1 = opts->pending->assert_head;
1394 while (o1)
1395 {
1396 o2 = o1->next;
1397 free (o1);
1398 o1 = o2;
1399 }
1400 opts->pending->assert_head = NULL;
1401 opts->pending->assert_tail = NULL;
1402 opts->pending->define_head = NULL;
1403 opts->pending->define_tail = NULL;
1404 }
1405 }
1406 break;
1407
1408 case 'U':
1409 {
1410 struct pending_option *o = (struct pending_option *)
1411 xmalloc (sizeof (struct pending_option));
1412
1413 if (argv[i][2] != 0)
1414 o->arg = argv[i] + 2;
1415 else if (i + 1 == argc)
1416 {
1417 cpp_fatal (pfile, "Macro name missing after -U option");
1418 return argc;
1419 }
1420 else
1421 o->arg = argv[++i];
1422
1423 o->next = NULL;
1424 o->undef = 1;
1425 APPEND (opts->pending, define, o);
1426 }
1427 break;
1428
1429 case 'C':
1430 opts->discard_comments = 0;
1431 break;
1432
1433 case 'E': /* -E comes from cc -E; ignore it. */
1434 break;
1435
1436 case 'P':
1437 opts->no_line_commands = 1;
1438 break;
1439
1440 case '$': /* Don't include $ in identifiers. */
1441 opts->dollars_in_ident = 0;
1442 break;
1443
1444 case 'n':
1445 if (!strcmp (argv[i], "-nostdinc"))
1446 /* -nostdinc causes no default include directories.
1447 You must specify all include-file directories with -I. */
1448 opts->no_standard_includes = 1;
1449 else if (!strcmp (argv[i], "-nostdinc++"))
1450 /* -nostdinc++ causes no default C++-specific include directories. */
1451 opts->no_standard_cplusplus_includes = 1;
1452 break;
1453
1454 case 'r':
1455 if (!strcmp (argv[i], "-remap"))
1456 opts->remap = 1;
1457 break;
1458
1459 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
1460 if (opts->in_fname == NULL)
1461 opts->in_fname = "";
1462 else if (opts->out_fname == NULL)
1463 opts->out_fname = "";
1464 else
1465 return i; /* error */
1466 break;
1467
1468 default:
1469 return i;
1470 }
1471
1472 return i + 1;
1473
1474 missing_filename:
1475 cpp_fatal (pfile, "Filename missing after `%s' option", argv[i]);
1476 return argc;
1477 missing_dirname:
1478 cpp_fatal (pfile, "Directory name missing after `%s' option", argv[i]);
1479 return argc;
1480 }
1481
1482 /* Handle command-line options in (argc, argv).
1483 Can be called multiple times, to handle multiple sets of options.
1484 Returns if an unrecognized option is seen.
1485 Returns number of strings consumed. */
1486
1487 int
1488 cpp_handle_options (pfile, argc, argv)
1489 cpp_reader *pfile;
1490 int argc;
1491 char **argv;
1492 {
1493 int i;
1494 int strings_processed;
1495 for (i = 0; i < argc; i += strings_processed)
1496 {
1497 strings_processed = cpp_handle_option (pfile, argc - i, argv + i);
1498 if (strings_processed == 0)
1499 break;
1500 }
1501 return i;
1502 }
1503
1504 static void
1505 print_help ()
1506 {
1507 fprintf (stderr, _("Usage: %s [switches] input output\n"), progname);
1508 fputs (_("\
1509 Switches:\n\
1510 -include <file> Include the contents of <file> before other files\n\
1511 -imacros <file> Accept definition of macros in <file>\n\
1512 -iprefix <path> Specify <path> as a prefix for next two options\n\
1513 -iwithprefix <dir> Add <dir> to the end of the system include path\n\
1514 -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\
1515 -isystem <dir> Add <dir> to the start of the system include path\n\
1516 -idirafter <dir> Add <dir> to the end of the system include path\n\
1517 -I <dir> Add <dir> to the end of the main include path\n\
1518 -nostdinc Do not search system include directories\n\
1519 (dirs specified with -isystem will still be used)\n\
1520 -nostdinc++ Do not search system include directories for C++\n\
1521 -o <file> Put output into <file>\n\
1522 -pedantic Issue all warnings demanded by strict ANSI C\n\
1523 -traditional Follow K&R pre-processor behaviour\n\
1524 -trigraphs Support ANSI C trigraphs\n\
1525 -lang-c Assume that the input sources are in C\n\
1526 -lang-c89 Assume that the input sources are in C89\n\
1527 -lang-c++ Assume that the input sources are in C++\n\
1528 -lang-objc Assume that the input sources are in ObjectiveC\n\
1529 -lang-objc++ Assume that the input sources are in ObjectiveC++\n\
1530 -lang-asm Assume that the input sources are in assembler\n\
1531 -lang-fortran Assume that the input sources are in Fortran\n\
1532 -lang-chill Assume that the input sources are in Chill\n\
1533 -std=<std name> Specify the conformance standard; one of:\n\
1534 gnu89, gnu99, c89, c99, iso9899:1990,\n\
1535 iso9899:199409, iso9899:1999\n\
1536 -+ Allow parsing of C++ style features\n\
1537 -w Inhibit warning messages\n\
1538 -Wtrigraphs Warn if trigraphs are encountered\n\
1539 -Wno-trigraphs Do not warn about trigraphs\n\
1540 -Wcomment{s} Warn if one comment starts inside another\n\
1541 -Wno-comment{s} Do not warn about comments\n\
1542 -Wtraditional Warn if a macro argument is/would be turned into\n\
1543 a string if -traditional is specified\n\
1544 -Wno-traditional Do not warn about stringification\n\
1545 -Wundef Warn if an undefined macro is used by #if\n\
1546 -Wno-undef Do not warn about testing undefined macros\n\
1547 -Wimport Warn about the use of the #import directive\n\
1548 -Wno-import Do not warn about the use of #import\n\
1549 -Werror Treat all warnings as errors\n\
1550 -Wno-error Do not treat warnings as errors\n\
1551 -Wall Enable all preprocessor warnings\n\
1552 -M Generate make dependencies\n\
1553 -MM As -M, but ignore system header files\n\
1554 -MD As -M, but put output in a .d file\n\
1555 -MMD As -MD, but ignore system header files\n\
1556 -MG Treat missing header file as generated files\n\
1557 -g Include #define and #undef directives in the output\n\
1558 -D<macro> Define a <macro> with string '1' as its value\n\
1559 -D<macro>=<val> Define a <macro> with <val> as its value\n\
1560 -A<question> (<answer>) Assert the <answer> to <question>\n\
1561 -U<macro> Undefine <macro> \n\
1562 -v Display the version number\n\
1563 -H Print the name of header files as they are used\n\
1564 -C Do not discard comments\n\
1565 -dM Display a list of macro definitions active at end\n\
1566 -dD Preserve macro definitions in output\n\
1567 -dN As -dD except that only the names are preserved\n\
1568 -dI Include #include directives in the output\n\
1569 -P Do not generate #line directives\n\
1570 -$ Do not allow '$' in identifiers\n\
1571 -remap Remap file names when including files.\n\
1572 -h or --help Display this information\n\
1573 "), stdout);
1574 }