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