Don't use #if inside C test expression.
[gcc.git] / gcc / cppinit.c
1 /* CPP Library.
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 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 #include "cpplib.h"
25 #include "cpphash.h"
26 #include "output.h"
27 #include "prefix.h"
28 #include "intl.h"
29 #include "version.h"
30 #include "mkdeps.h"
31 #include "cppdefault.h"
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 /* Windows does not natively support inodes, and neither does MSDOS.
40 Cygwin's emulation can generate non-unique inodes, so don't use it.
41 VMS has non-numeric inodes. */
42 #ifdef VMS
43 # define INO_T_EQ(a, b) (!memcmp (&(a), &(b), sizeof (a)))
44 #else
45 # if (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
46 # define INO_T_EQ(a, b) 0
47 # else
48 # define INO_T_EQ(a, b) ((a) == (b))
49 # endif
50 #endif
51
52 /* Internal structures and prototypes. */
53
54 /* A `struct pending_option' remembers one -D, -A, -U, -include, or
55 -imacros switch. */
56
57 typedef void (* cl_directive_handler) PARAMS ((cpp_reader *, const char *));
58 struct pending_option
59 {
60 struct pending_option *next;
61 const char *arg;
62 cl_directive_handler handler;
63 };
64
65 /* The `pending' structure accumulates all the options that are not
66 actually processed until we hit cpp_start_read. It consists of
67 several lists, one for each type of option. We keep both head and
68 tail pointers for quick insertion. */
69 struct cpp_pending
70 {
71 struct pending_option *directive_head, *directive_tail;
72
73 struct search_path *quote_head, *quote_tail;
74 struct search_path *brack_head, *brack_tail;
75 struct search_path *systm_head, *systm_tail;
76 struct search_path *after_head, *after_tail;
77
78 struct pending_option *imacros_head, *imacros_tail;
79 struct pending_option *include_head, *include_tail;
80 };
81
82 #ifdef __STDC__
83 #define APPEND(pend, list, elt) \
84 do { if (!(pend)->list##_head) (pend)->list##_head = (elt); \
85 else (pend)->list##_tail->next = (elt); \
86 (pend)->list##_tail = (elt); \
87 } while (0)
88 #else
89 #define APPEND(pend, list, elt) \
90 do { if (!(pend)->list/**/_head) (pend)->list/**/_head = (elt); \
91 else (pend)->list/**/_tail->next = (elt); \
92 (pend)->list/**/_tail = (elt); \
93 } while (0)
94 #endif
95
96 static void print_help PARAMS ((void));
97 static void path_include PARAMS ((cpp_reader *,
98 char *, int));
99 static void init_library PARAMS ((void));
100 static void init_builtins PARAMS ((cpp_reader *));
101 static void append_include_chain PARAMS ((cpp_reader *,
102 char *, int, int));
103 static struct search_path * remove_dup_dir PARAMS ((cpp_reader *,
104 struct search_path *));
105 static struct search_path * remove_dup_dirs PARAMS ((cpp_reader *,
106 struct search_path *));
107 static void merge_include_chains PARAMS ((cpp_reader *));
108 static void do_includes PARAMS ((cpp_reader *,
109 struct pending_option *,
110 int));
111 static void set_lang PARAMS ((cpp_reader *, enum c_lang));
112 static void init_dependency_output PARAMS ((cpp_reader *));
113 static void init_standard_includes PARAMS ((cpp_reader *));
114 static void new_pending_directive PARAMS ((struct cpp_pending *,
115 const char *,
116 cl_directive_handler));
117 static void output_deps PARAMS ((cpp_reader *));
118 static int parse_option PARAMS ((const char *));
119
120 /* Fourth argument to append_include_chain: chain to use.
121 Note it's never asked to append to the quote chain. */
122 enum { BRACKET = 0, SYSTEM, AFTER };
123
124 /* If we have designated initializers (GCC >2.7) these tables can be
125 initialized, constant data. Otherwise, they have to be filled in at
126 runtime. */
127 #if HAVE_DESIGNATED_INITIALIZERS
128
129 #define init_trigraph_map() /* Nothing. */
130 #define TRIGRAPH_MAP \
131 __extension__ const U_CHAR _cpp_trigraph_map[UCHAR_MAX + 1] = {
132
133 #define END };
134 #define s(p, v) [p] = v,
135
136 #else
137
138 #define TRIGRAPH_MAP U_CHAR _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
139 static void init_trigraph_map PARAMS ((void)) { \
140 unsigned char *x = _cpp_trigraph_map;
141
142 #define END }
143 #define s(p, v) x[p] = v;
144
145 #endif
146
147 TRIGRAPH_MAP
148 s('=', '#') s(')', ']') s('!', '|')
149 s('(', '[') s('\'', '^') s('>', '}')
150 s('/', '\\') s('<', '{') s('-', '~')
151 END
152
153 #undef s
154 #undef END
155 #undef TRIGRAPH_MAP
156
157 /* Given a colon-separated list of file names PATH,
158 add all the names to the search path for include files. */
159
160 static void
161 path_include (pfile, list, path)
162 cpp_reader *pfile;
163 char *list;
164 int path;
165 {
166 char *p, *q, *name;
167
168 p = list;
169
170 do
171 {
172 /* Find the end of this name. */
173 q = p;
174 while (*q != 0 && *q != PATH_SEPARATOR) q++;
175 if (q == p)
176 {
177 /* An empty name in the path stands for the current directory. */
178 name = (char *) xmalloc (2);
179 name[0] = '.';
180 name[1] = 0;
181 }
182 else
183 {
184 /* Otherwise use the directory that is named. */
185 name = (char *) xmalloc (q - p + 1);
186 memcpy (name, p, q - p);
187 name[q - p] = 0;
188 }
189
190 append_include_chain (pfile, name, path, 0);
191
192 /* Advance past this name. */
193 if (*q == 0)
194 break;
195 p = q + 1;
196 }
197 while (1);
198 }
199
200 /* Append DIR to include path PATH. DIR must be permanently allocated
201 and writable. */
202 static void
203 append_include_chain (pfile, dir, path, cxx_aware)
204 cpp_reader *pfile;
205 char *dir;
206 int path;
207 int cxx_aware ATTRIBUTE_UNUSED;
208 {
209 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
210 struct search_path *new;
211 struct stat st;
212 unsigned int len;
213
214 if (*dir == '\0')
215 dir = xstrdup (".");
216 _cpp_simplify_pathname (dir);
217 if (stat (dir, &st))
218 {
219 /* Dirs that don't exist are silently ignored. */
220 if (errno != ENOENT)
221 cpp_notice_from_errno (pfile, dir);
222 else if (CPP_OPTION (pfile, verbose))
223 fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"), dir);
224 return;
225 }
226
227 if (!S_ISDIR (st.st_mode))
228 {
229 cpp_notice (pfile, "%s: Not a directory", dir);
230 return;
231 }
232
233 len = strlen (dir);
234 if (len > pfile->max_include_len)
235 pfile->max_include_len = len;
236
237 new = (struct search_path *) xmalloc (sizeof (struct search_path));
238 new->name = dir;
239 new->len = len;
240 new->ino = st.st_ino;
241 new->dev = st.st_dev;
242 /* Both systm and after include file lists should be treated as system
243 include files since these two lists are really just a concatenation
244 of one "system" list. */
245 if (path == SYSTEM || path == AFTER)
246 #ifdef NO_IMPLICIT_EXTERN_C
247 new->sysp = 1;
248 #else
249 new->sysp = cxx_aware ? 1 : 2;
250 #endif
251 else
252 new->sysp = 0;
253 new->name_map = NULL;
254 new->next = NULL;
255
256 switch (path)
257 {
258 case BRACKET: APPEND (pend, brack, new); break;
259 case SYSTEM: APPEND (pend, systm, new); break;
260 case AFTER: APPEND (pend, after, new); break;
261 }
262 }
263
264 /* Handle a duplicated include path. PREV is the link in the chain
265 before the duplicate. The duplicate is removed from the chain and
266 freed. Returns PREV. */
267 static struct search_path *
268 remove_dup_dir (pfile, prev)
269 cpp_reader *pfile;
270 struct search_path *prev;
271 {
272 struct search_path *cur = prev->next;
273
274 if (CPP_OPTION (pfile, verbose))
275 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), cur->name);
276
277 prev->next = cur->next;
278 free ((PTR) cur->name);
279 free (cur);
280
281 return prev;
282 }
283
284 /* Remove duplicate directories from a chain. Returns the tail of the
285 chain, or NULL if the chain is empty. This algorithm is quadratic
286 in the number of -I switches, which is acceptable since there
287 aren't usually that many of them. */
288 static struct search_path *
289 remove_dup_dirs (pfile, head)
290 cpp_reader *pfile;
291 struct search_path *head;
292 {
293 struct search_path *prev = NULL, *cur, *other;
294
295 for (cur = head; cur; cur = cur->next)
296 {
297 for (other = head; other != cur; other = other->next)
298 if (INO_T_EQ (cur->ino, other->ino) && cur->dev == other->dev)
299 {
300 if (cur->sysp && !other->sysp)
301 {
302 cpp_warning (pfile,
303 "changing search order for system directory \"%s\"",
304 cur->name);
305 if (strcmp (cur->name, other->name))
306 cpp_warning (pfile,
307 " as it is the same as non-system directory \"%s\"",
308 other->name);
309 else
310 cpp_warning (pfile,
311 " as it has already been specified as a non-system directory");
312 }
313 cur = remove_dup_dir (pfile, prev);
314 break;
315 }
316 prev = cur;
317 }
318
319 return prev;
320 }
321
322 /* Merge the four include chains together in the order quote, bracket,
323 system, after. Remove duplicate dirs (as determined by
324 INO_T_EQ()). The system_include and after_include chains are never
325 referred to again after this function; all access is through the
326 bracket_include path.
327
328 For the future: Check if the directory is empty (but
329 how?) and possibly preload the include hash. */
330
331 static void
332 merge_include_chains (pfile)
333 cpp_reader *pfile;
334 {
335 struct search_path *quote, *brack, *systm, *qtail;
336
337 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
338
339 quote = pend->quote_head;
340 brack = pend->brack_head;
341 systm = pend->systm_head;
342 qtail = pend->quote_tail;
343
344 /* Paste together bracket, system, and after include chains. */
345 if (systm)
346 pend->systm_tail->next = pend->after_head;
347 else
348 systm = pend->after_head;
349
350 if (brack)
351 pend->brack_tail->next = systm;
352 else
353 brack = systm;
354
355 /* This is a bit tricky. First we drop dupes from the quote-include
356 list. Then we drop dupes from the bracket-include list.
357 Finally, if qtail and brack are the same directory, we cut out
358 brack and move brack up to point to qtail.
359
360 We can't just merge the lists and then uniquify them because
361 then we may lose directories from the <> search path that should
362 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
363 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
364 -Ibar -I- -Ifoo -Iquux. */
365
366 remove_dup_dirs (pfile, brack);
367 qtail = remove_dup_dirs (pfile, quote);
368
369 if (quote)
370 {
371 qtail->next = brack;
372
373 /* If brack == qtail, remove brack as it's simpler. */
374 if (INO_T_EQ (qtail->ino, brack->ino) && qtail->dev == brack->dev)
375 brack = remove_dup_dir (pfile, qtail);
376 }
377 else
378 quote = brack;
379
380 CPP_OPTION (pfile, quote_include) = quote;
381 CPP_OPTION (pfile, bracket_include) = brack;
382 }
383
384 /* Sets internal flags correctly for a given language, and defines
385 macros if necessary. */
386
387 struct lang_flags
388 {
389 char c99;
390 char objc;
391 char cplusplus;
392 char extended_numbers;
393 char trigraphs;
394 char dollars_in_ident;
395 char cplusplus_comments;
396 char digraphs;
397 };
398
399 /* ??? Enable $ in identifiers in assembly? */
400 static const struct lang_flags lang_defaults[] =
401 { /* c99 objc c++ xnum trig dollar c++comm digr */
402 /* GNUC89 */ { 0, 0, 0, 1, 0, 1, 1, 1 },
403 /* GNUC99 */ { 1, 0, 0, 1, 0, 1, 1, 1 },
404 /* STDC89 */ { 0, 0, 0, 0, 1, 0, 0, 0 },
405 /* STDC94 */ { 0, 0, 0, 0, 1, 0, 0, 1 },
406 /* STDC99 */ { 1, 0, 0, 1, 1, 0, 1, 1 },
407 /* GNUCXX */ { 0, 0, 1, 1, 0, 1, 1, 1 },
408 /* CXX98 */ { 0, 0, 1, 1, 1, 0, 1, 1 },
409 /* OBJC */ { 0, 1, 0, 1, 0, 1, 1, 1 },
410 /* OBJCXX */ { 0, 1, 1, 1, 0, 1, 1, 1 },
411 /* ASM */ { 0, 0, 0, 1, 0, 0, 1, 0 }
412 };
413
414 static void
415 set_lang (pfile, lang)
416 cpp_reader *pfile;
417 enum c_lang lang;
418 {
419 const struct lang_flags *l = &lang_defaults[(int) lang];
420
421 CPP_OPTION (pfile, lang) = lang;
422
423 CPP_OPTION (pfile, c99) = l->c99;
424 CPP_OPTION (pfile, objc) = l->objc;
425 CPP_OPTION (pfile, cplusplus) = l->cplusplus;
426 CPP_OPTION (pfile, extended_numbers) = l->extended_numbers;
427 CPP_OPTION (pfile, trigraphs) = l->trigraphs;
428 CPP_OPTION (pfile, dollars_in_ident) = l->dollars_in_ident;
429 CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments;
430 CPP_OPTION (pfile, digraphs) = l->digraphs;
431 }
432
433 #ifdef HOST_EBCDIC
434 static int opt_comp PARAMS ((const void *, const void *));
435
436 /* Run-time sorting of options array. */
437 static int
438 opt_comp (p1, p2)
439 const void *p1, *p2;
440 {
441 return strcmp (((struct cl_option *) p1)->opt_text,
442 ((struct cl_option *) p2)->opt_text);
443 }
444 #endif
445
446 /* init initializes library global state. It might not need to
447 do anything depending on the platform and compiler. */
448
449 static void
450 init_library ()
451 {
452 static int initialized = 0;
453
454 if (! initialized)
455 {
456 initialized = 1;
457
458 #ifdef HOST_EBCDIC
459 /* For non-ASCII hosts, the cl_options array needs to be sorted at
460 runtime. */
461 qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
462 #endif
463
464 /* Set up the trigraph map. This doesn't need to do anything if
465 we were compiled with a compiler that supports C99 designated
466 initializers. */
467 init_trigraph_map ();
468 }
469 }
470
471 /* Initialize a cpp_reader structure. */
472 cpp_reader *
473 cpp_create_reader (table, lang)
474 hash_table *table;
475 enum c_lang lang;
476 {
477 struct spec_nodes *s;
478 cpp_reader *pfile;
479
480 /* Initialise this instance of the library if it hasn't been already. */
481 init_library ();
482
483 pfile = (cpp_reader *) xcalloc (1, sizeof (cpp_reader));
484
485 set_lang (pfile, lang);
486 CPP_OPTION (pfile, warn_import) = 1;
487 CPP_OPTION (pfile, discard_comments) = 1;
488 CPP_OPTION (pfile, show_column) = 1;
489 CPP_OPTION (pfile, tabstop) = 8;
490 CPP_OPTION (pfile, operator_names) = 1;
491
492 CPP_OPTION (pfile, pending) =
493 (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
494
495 /* It's simplest to just create this struct whether or not it will
496 be needed. */
497 pfile->deps = deps_init ();
498
499 /* Initialise the line map. */
500 init_line_maps (&pfile->line_maps);
501
502 /* Initialize lexer state. */
503 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
504
505 /* Indicate date and time not yet calculated. */
506 pfile->date.type = CPP_EOF;
507
508 /* Initialise the base context. */
509 pfile->context = &pfile->base_context;
510 pfile->base_context.macro = 0;
511 pfile->base_context.prev = pfile->base_context.next = 0;
512
513 /* Identifier pool initially 8K. Unaligned, permanent pool. */
514 _cpp_init_pool (&pfile->ident_pool, 8 * 1024, 1, 0);
515
516 /* Argument pool initially 8K. Aligned, temporary pool. */
517 _cpp_init_pool (&pfile->argument_pool, 8 * 1024, 0, 1);
518
519 /* Macro pool initially 8K. Aligned, permanent pool. */
520 _cpp_init_pool (&pfile->macro_pool, 8 * 1024, 0, 0);
521
522 /* Initialise the buffer obstack. */
523 gcc_obstack_init (&pfile->buffer_ob);
524
525 /* Initialise the hashtable. */
526 _cpp_init_hashtable (pfile, table);
527
528 _cpp_init_directives (pfile);
529 _cpp_init_includes (pfile);
530 _cpp_init_internal_pragmas (pfile);
531
532 /* Initialize the special nodes. */
533 s = &pfile->spec_nodes;
534 s->n_L = cpp_lookup (pfile, DSC("L"));
535 s->n_defined = cpp_lookup (pfile, DSC("defined"));
536 s->n_true = cpp_lookup (pfile, DSC("true"));
537 s->n_false = cpp_lookup (pfile, DSC("false"));
538 s->n__Pragma = cpp_lookup (pfile, DSC("_Pragma"));
539 s->n__STRICT_ANSI__ = cpp_lookup (pfile, DSC("__STRICT_ANSI__"));
540 s->n__CHAR_UNSIGNED__ = cpp_lookup (pfile, DSC("__CHAR_UNSIGNED__"));
541 s->n__VA_ARGS__ = cpp_lookup (pfile, DSC("__VA_ARGS__"));
542 s->n__VA_ARGS__->flags |= NODE_DIAGNOSTIC;
543
544 return pfile;
545 }
546
547 /* Free resources used by PFILE. Accessing PFILE after this function
548 returns leads to undefined behaviour. */
549 int
550 cpp_destroy (pfile)
551 cpp_reader *pfile;
552 {
553 int result;
554 struct search_path *dir, *dirn;
555 cpp_context *context, *contextn;
556
557 while (CPP_BUFFER (pfile) != NULL)
558 _cpp_pop_buffer (pfile);
559
560 if (pfile->macro_buffer)
561 {
562 free ((PTR) pfile->macro_buffer);
563 pfile->macro_buffer = NULL;
564 pfile->macro_buffer_len = 0;
565 }
566
567 deps_free (pfile->deps);
568 obstack_free (&pfile->buffer_ob, 0);
569
570 _cpp_destroy_hashtable (pfile);
571 _cpp_cleanup_includes (pfile);
572 _cpp_free_lookaheads (pfile);
573
574 _cpp_free_pool (&pfile->ident_pool);
575 _cpp_free_pool (&pfile->macro_pool);
576 _cpp_free_pool (&pfile->argument_pool);
577
578 for (dir = CPP_OPTION (pfile, quote_include); dir; dir = dirn)
579 {
580 dirn = dir->next;
581 free ((PTR) dir->name);
582 free (dir);
583 }
584
585 for (context = pfile->base_context.next; context; context = contextn)
586 {
587 contextn = context->next;
588 free (context);
589 }
590
591 free_line_maps (&pfile->line_maps);
592
593 result = pfile->errors;
594 free (pfile);
595
596 return result;
597 }
598
599
600 /* This structure defines one built-in identifier. A node will be
601 entered in the hash table under the name NAME, with value VALUE (if
602 any). If flags has OPERATOR, the node's operator field is used; if
603 flags has BUILTIN the node's builtin field is used. Macros that are
604 known at build time should not be flagged BUILTIN, as then they do
605 not appear in macro dumps with e.g. -dM or -dD.
606
607 Two values are not compile time constants, so we tag
608 them in the FLAGS field instead:
609 VERS value is the global version_string, quoted
610 ULP value is the global user_label_prefix
611
612 Also, macros with CPLUS set in the flags field are entered only for C++. */
613
614 struct builtin
615 {
616 const U_CHAR *name;
617 const char *value;
618 unsigned char builtin;
619 unsigned char operator;
620 unsigned short flags;
621 unsigned short len;
622 };
623 #define VERS 0x01
624 #define ULP 0x02
625 #define CPLUS 0x04
626 #define BUILTIN 0x08
627 #define OPERATOR 0x10
628
629 #define B(n, t) { U n, 0, t, 0, BUILTIN, sizeof n - 1 }
630 #define C(n, v) { U n, v, 0, 0, 0, sizeof n - 1 }
631 #define X(n, f) { U n, 0, 0, 0, f, sizeof n - 1 }
632 #define O(n, c, f) { U n, 0, 0, c, OPERATOR | f, sizeof n - 1 }
633 static const struct builtin builtin_array[] =
634 {
635 B("__TIME__", BT_TIME),
636 B("__DATE__", BT_DATE),
637 B("__FILE__", BT_FILE),
638 B("__BASE_FILE__", BT_BASE_FILE),
639 B("__LINE__", BT_SPECLINE),
640 B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL),
641
642 X("__VERSION__", VERS),
643 X("__USER_LABEL_PREFIX__", ULP),
644 C("__REGISTER_PREFIX__", REGISTER_PREFIX),
645 C("__HAVE_BUILTIN_SETJMP__", "1"),
646 #ifndef NO_BUILTIN_SIZE_TYPE
647 C("__SIZE_TYPE__", SIZE_TYPE),
648 #endif
649 #ifndef NO_BUILTIN_PTRDIFF_TYPE
650 C("__PTRDIFF_TYPE__", PTRDIFF_TYPE),
651 #endif
652 #ifndef NO_BUILTIN_WCHAR_TYPE
653 C("__WCHAR_TYPE__", WCHAR_TYPE),
654 #endif
655 #ifndef NO_BUILTIN_WINT_TYPE
656 C("__WINT_TYPE__", WINT_TYPE),
657 #endif
658 #ifdef STDC_0_IN_SYSTEM_HEADERS
659 B("__STDC__", BT_STDC),
660 #else
661 C("__STDC__", "1"),
662 #endif
663
664 /* Named operators known to the preprocessor. These cannot be #defined
665 and always have their stated meaning. They are treated like normal
666 identifiers except for the type code and the meaning. Most of them
667 are only for C++ (but see iso646.h). */
668 O("and", CPP_AND_AND, CPLUS),
669 O("and_eq", CPP_AND_EQ, CPLUS),
670 O("bitand", CPP_AND, CPLUS),
671 O("bitor", CPP_OR, CPLUS),
672 O("compl", CPP_COMPL, CPLUS),
673 O("not", CPP_NOT, CPLUS),
674 O("not_eq", CPP_NOT_EQ, CPLUS),
675 O("or", CPP_OR_OR, CPLUS),
676 O("or_eq", CPP_OR_EQ, CPLUS),
677 O("xor", CPP_XOR, CPLUS),
678 O("xor_eq", CPP_XOR_EQ, CPLUS)
679 };
680 #undef B
681 #undef C
682 #undef X
683 #undef O
684 #define builtin_array_end \
685 builtin_array + sizeof(builtin_array)/sizeof(struct builtin)
686
687 /* Subroutine of cpp_start_read; reads the builtins table above and
688 enters the macros into the hash table. */
689 static void
690 init_builtins (pfile)
691 cpp_reader *pfile;
692 {
693 const struct builtin *b;
694
695 for(b = builtin_array; b < builtin_array_end; b++)
696 {
697 if ((b->flags & CPLUS) && ! CPP_OPTION (pfile, cplusplus))
698 continue;
699
700 if ((b->flags & OPERATOR) && ! CPP_OPTION (pfile, operator_names))
701 continue;
702
703 if (b->flags & (OPERATOR | BUILTIN))
704 {
705 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
706 if (b->flags & OPERATOR)
707 {
708 hp->flags |= NODE_OPERATOR;
709 hp->value.operator = b->operator;
710 }
711 else
712 {
713 hp->type = NT_MACRO;
714 hp->flags |= NODE_BUILTIN | NODE_WARN;
715 hp->value.builtin = b->builtin;
716 }
717 }
718 else /* A standard macro of some kind. */
719 {
720 const char *val;
721 char *str;
722
723 if (b->flags & VERS)
724 {
725 /* Allocate enough space for 'name "value"\n\0'. */
726 str = alloca (b->len + strlen (version_string) + 5);
727 sprintf (str, "%s \"%s\"\n", b->name, version_string);
728 }
729 else
730 {
731 if (b->flags & ULP)
732 val = CPP_OPTION (pfile, user_label_prefix);
733 else
734 val = b->value;
735
736 /* Allocate enough space for "name value\n\0". */
737 str = alloca (b->len + strlen (val) + 3);
738 sprintf(str, "%s %s\n", b->name, val);
739 }
740
741 _cpp_define_builtin (pfile, str);
742 }
743 }
744
745 if (CPP_OPTION (pfile, cplusplus))
746 {
747 _cpp_define_builtin (pfile, "__cplusplus 1");
748 if (SUPPORTS_ONE_ONLY)
749 _cpp_define_builtin (pfile, "__GXX_WEAK__ 1");
750 else
751 _cpp_define_builtin (pfile, "__GXX_WEAK__ 0");
752 }
753 if (CPP_OPTION (pfile, objc))
754 _cpp_define_builtin (pfile, "__OBJC__ 1");
755
756 if (CPP_OPTION (pfile, lang) == CLK_STDC94)
757 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
758 else if (CPP_OPTION (pfile, c99))
759 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
760
761 if (CPP_OPTION (pfile, lang) == CLK_STDC89
762 || CPP_OPTION (pfile, lang) == CLK_STDC94
763 || CPP_OPTION (pfile, lang) == CLK_STDC99)
764 _cpp_define_builtin (pfile, "__STRICT_ANSI__ 1");
765 else if (CPP_OPTION (pfile, lang) == CLK_ASM)
766 _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
767 }
768 #undef BUILTIN
769 #undef OPERATOR
770 #undef VERS
771 #undef ULP
772 #undef CPLUS
773 #undef builtin_array_end
774
775 /* And another subroutine. This one sets up the standard include path. */
776 static void
777 init_standard_includes (pfile)
778 cpp_reader *pfile;
779 {
780 char *path;
781 const struct default_include *p;
782 const char *specd_prefix = CPP_OPTION (pfile, include_prefix);
783
784 /* Several environment variables may add to the include search path.
785 CPATH specifies an additional list of directories to be searched
786 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
787 etc. specify an additional list of directories to be searched as
788 if specified with -isystem, for the language indicated. */
789
790 GET_ENV_PATH_LIST (path, "CPATH");
791 if (path != 0 && *path != 0)
792 path_include (pfile, path, BRACKET);
793
794 switch ((CPP_OPTION (pfile, objc) << 1) + CPP_OPTION (pfile, cplusplus))
795 {
796 case 0:
797 GET_ENV_PATH_LIST (path, "C_INCLUDE_PATH");
798 break;
799 case 1:
800 GET_ENV_PATH_LIST (path, "CPLUS_INCLUDE_PATH");
801 break;
802 case 2:
803 GET_ENV_PATH_LIST (path, "OBJC_INCLUDE_PATH");
804 break;
805 case 3:
806 GET_ENV_PATH_LIST (path, "OBJCPLUS_INCLUDE_PATH");
807 break;
808 }
809 if (path != 0 && *path != 0)
810 path_include (pfile, path, SYSTEM);
811
812 /* Search "translated" versions of GNU directories.
813 These have /usr/local/lib/gcc... replaced by specd_prefix. */
814 if (specd_prefix != 0 && cpp_GCC_INCLUDE_DIR_len)
815 {
816 /* Remove the `include' from /usr/local/lib/gcc.../include.
817 GCC_INCLUDE_DIR will always end in /include. */
818 int default_len = cpp_GCC_INCLUDE_DIR_len;
819 char *default_prefix = (char *) alloca (default_len + 1);
820 int specd_len = strlen (specd_prefix);
821
822 memcpy (default_prefix, cpp_GCC_INCLUDE_DIR, default_len);
823 default_prefix[default_len] = '\0';
824
825 for (p = cpp_include_defaults; p->fname; p++)
826 {
827 /* Some standard dirs are only for C++. */
828 if (!p->cplusplus
829 || (CPP_OPTION (pfile, cplusplus)
830 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
831 {
832 /* Does this dir start with the prefix? */
833 if (!memcmp (p->fname, default_prefix, default_len))
834 {
835 /* Yes; change prefix and add to search list. */
836 int flen = strlen (p->fname);
837 int this_len = specd_len + flen - default_len;
838 char *str = (char *) xmalloc (this_len + 1);
839 memcpy (str, specd_prefix, specd_len);
840 memcpy (str + specd_len,
841 p->fname + default_len,
842 flen - default_len + 1);
843
844 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
845 }
846 }
847 }
848 }
849
850 /* Search ordinary names for GNU include directories. */
851 for (p = cpp_include_defaults; p->fname; p++)
852 {
853 /* Some standard dirs are only for C++. */
854 if (!p->cplusplus
855 || (CPP_OPTION (pfile, cplusplus)
856 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
857 {
858 char *str = xstrdup (update_path (p->fname, p->component));
859 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
860 }
861 }
862 }
863
864 /* Handles -imacro and -include from the command line. */
865 static void
866 do_includes (pfile, p, scan)
867 cpp_reader *pfile;
868 struct pending_option *p;
869 int scan;
870 {
871 while (p)
872 {
873 struct pending_option *q;
874
875 /* Don't handle if -fpreprocessed. Later: maybe update this to
876 use the #include "" search path if cpp_read_file fails. */
877 if (CPP_OPTION (pfile, preprocessed))
878 cpp_error (pfile, "-include and -imacros cannot be used with -fpreprocessed");
879 else
880 {
881 cpp_token header;
882 header.type = CPP_STRING;
883 header.val.str.text = (const unsigned char *) p->arg;
884 header.val.str.len = strlen (p->arg);
885 if (_cpp_execute_include (pfile, &header, IT_CMDLINE) && scan)
886 {
887 pfile->buffer->return_at_eof = true;
888 cpp_scan_nooutput (pfile);
889 }
890 }
891 q = p->next;
892 free (p);
893 p = q;
894 }
895 }
896
897 /* This is called after options have been processed. Setup for
898 processing input from the file named FNAME, or stdin if it is the
899 empty string. Return 1 on success, 0 on failure. */
900 int
901 cpp_start_read (pfile, fname)
902 cpp_reader *pfile;
903 const char *fname;
904 {
905 struct pending_option *p, *q;
906
907 /* Set up the include search path now. */
908 if (! CPP_OPTION (pfile, no_standard_includes))
909 init_standard_includes (pfile);
910
911 merge_include_chains (pfile);
912
913 /* With -v, print the list of dirs to search. */
914 if (CPP_OPTION (pfile, verbose))
915 {
916 struct search_path *l;
917 fprintf (stderr, _("#include \"...\" search starts here:\n"));
918 for (l = CPP_OPTION (pfile, quote_include); l; l = l->next)
919 {
920 if (l == CPP_OPTION (pfile, bracket_include))
921 fprintf (stderr, _("#include <...> search starts here:\n"));
922 fprintf (stderr, " %s\n", l->name);
923 }
924 fprintf (stderr, _("End of search list.\n"));
925 }
926
927 if (CPP_OPTION (pfile, print_deps))
928 /* Set the default target (if there is none already). */
929 deps_add_default_target (pfile->deps, fname);
930
931 /* Open the main input file. This must be done early, so we have a
932 buffer to stand on. */
933 if (!_cpp_read_file (pfile, fname))
934 return 0;
935
936 /* If already preprocessed, don't install __LINE__, etc., and ignore
937 command line definitions and assertions. Handle -U's, -D's and
938 -A's in the order they were seen. */
939 if (! CPP_OPTION (pfile, preprocessed))
940 init_builtins (pfile);
941
942 p = CPP_OPTION (pfile, pending)->directive_head;
943 while (p)
944 {
945 if (! CPP_OPTION (pfile, preprocessed))
946 (*p->handler) (pfile, p->arg);
947 q = p->next;
948 free (p);
949 p = q;
950 }
951
952 /* This was zero when the initial buffer was stacked; so we must
953 make up for a non-existent new line, as well as the intervening
954 macro definitions, by setting it to 1. */
955 pfile->line = 1;
956
957 /* The -imacros files can be scanned now, but the -include files
958 have to be pushed onto the buffer stack and processed later,
959 otherwise cppmain.c won't see the tokens. include_head was built
960 up as a stack, and popping this stack onto the buffer stack means
961 we preserve the order of the command line. */
962 do_includes (pfile, CPP_OPTION (pfile, pending)->imacros_head, 1);
963 do_includes (pfile, CPP_OPTION (pfile, pending)->include_head, 0);
964
965 free (CPP_OPTION (pfile, pending));
966 CPP_OPTION (pfile, pending) = NULL;
967
968 return 1;
969 }
970
971 /* Use mkdeps.c to output dependency information. */
972 static void
973 output_deps (pfile)
974 cpp_reader *pfile;
975 {
976 /* Stream on which to print the dependency information. */
977 FILE *deps_stream = 0;
978 const char *deps_mode = CPP_OPTION (pfile, print_deps_append) ? "a" : "w";
979
980 if (CPP_OPTION (pfile, deps_file) == 0)
981 deps_stream = stdout;
982 else
983 {
984 deps_stream = fopen (CPP_OPTION (pfile, deps_file), deps_mode);
985 if (deps_stream == 0)
986 {
987 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, deps_file));
988 return;
989 }
990 }
991
992 deps_write (pfile->deps, deps_stream, 72);
993
994 if (CPP_OPTION (pfile, deps_phony_targets))
995 deps_phony_targets (pfile->deps, deps_stream);
996
997 /* Don't close stdout. */
998 if (CPP_OPTION (pfile, deps_file))
999 {
1000 if (ferror (deps_stream) || fclose (deps_stream) != 0)
1001 cpp_fatal (pfile, "I/O error on output");
1002 }
1003 }
1004
1005 /* This is called at the end of preprocessing. It pops the
1006 last buffer and writes dependency output. It should also
1007 clear macro definitions, such that you could call cpp_start_read
1008 with a new filename to restart processing. */
1009 void
1010 cpp_finish (pfile)
1011 cpp_reader *pfile;
1012 {
1013 if (CPP_BUFFER (pfile))
1014 {
1015 cpp_ice (pfile, "buffers still stacked in cpp_finish");
1016 while (CPP_BUFFER (pfile))
1017 _cpp_pop_buffer (pfile);
1018 }
1019
1020 /* Don't write the deps file if preprocessing has failed. */
1021 if (CPP_OPTION (pfile, print_deps) && pfile->errors == 0)
1022 output_deps (pfile);
1023
1024 /* Report on headers that could use multiple include guards. */
1025 if (CPP_OPTION (pfile, print_include_names))
1026 _cpp_report_missing_guards (pfile);
1027 }
1028
1029 static void
1030 new_pending_directive (pend, text, handler)
1031 struct cpp_pending *pend;
1032 const char *text;
1033 cl_directive_handler handler;
1034 {
1035 struct pending_option *o = (struct pending_option *)
1036 xmalloc (sizeof (struct pending_option));
1037
1038 o->arg = text;
1039 o->next = NULL;
1040 o->handler = handler;
1041 APPEND (pend, directive, o);
1042 }
1043
1044 /* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
1045 I.e. a const string initializer with parens around it. That is
1046 what N_("string") resolves to, so we make no_* be macros instead. */
1047 #define no_arg N_("Argument missing after %s")
1048 #define no_ass N_("Assertion missing after %s")
1049 #define no_dir N_("Directory name missing after %s")
1050 #define no_fil N_("File name missing after %s")
1051 #define no_mac N_("Macro name missing after %s")
1052 #define no_pth N_("Path name missing after %s")
1053 #define no_num N_("Number missing after %s")
1054 #define no_tgt N_("Target missing after %s")
1055
1056 /* This is the list of all command line options, with the leading
1057 "-" removed. It must be sorted in ASCII collating order. */
1058 #define COMMAND_LINE_OPTIONS \
1059 DEF_OPT("$", 0, OPT_dollar) \
1060 DEF_OPT("+", 0, OPT_plus) \
1061 DEF_OPT("-help", 0, OPT__help) \
1062 DEF_OPT("-target-help", 0, OPT_target__help) \
1063 DEF_OPT("-version", 0, OPT__version) \
1064 DEF_OPT("A", no_ass, OPT_A) \
1065 DEF_OPT("C", 0, OPT_C) \
1066 DEF_OPT("D", no_mac, OPT_D) \
1067 DEF_OPT("H", 0, OPT_H) \
1068 DEF_OPT("I", no_dir, OPT_I) \
1069 DEF_OPT("M", 0, OPT_M) \
1070 DEF_OPT("MD", no_fil, OPT_MD) \
1071 DEF_OPT("MF", no_fil, OPT_MF) \
1072 DEF_OPT("MG", 0, OPT_MG) \
1073 DEF_OPT("MM", 0, OPT_MM) \
1074 DEF_OPT("MMD", no_fil, OPT_MMD) \
1075 DEF_OPT("MP", 0, OPT_MP) \
1076 DEF_OPT("MQ", no_tgt, OPT_MQ) \
1077 DEF_OPT("MT", no_tgt, OPT_MT) \
1078 DEF_OPT("P", 0, OPT_P) \
1079 DEF_OPT("U", no_mac, OPT_U) \
1080 DEF_OPT("W", no_arg, OPT_W) /* arg optional */ \
1081 DEF_OPT("d", no_arg, OPT_d) \
1082 DEF_OPT("fleading-underscore", 0, OPT_fleading_underscore) \
1083 DEF_OPT("fno-leading-underscore", 0, OPT_fno_leading_underscore) \
1084 DEF_OPT("fno-operator-names", 0, OPT_fno_operator_names) \
1085 DEF_OPT("fno-preprocessed", 0, OPT_fno_preprocessed) \
1086 DEF_OPT("fno-show-column", 0, OPT_fno_show_column) \
1087 DEF_OPT("fpreprocessed", 0, OPT_fpreprocessed) \
1088 DEF_OPT("fshow-column", 0, OPT_fshow_column) \
1089 DEF_OPT("ftabstop=", no_num, OPT_ftabstop) \
1090 DEF_OPT("h", 0, OPT_h) \
1091 DEF_OPT("idirafter", no_dir, OPT_idirafter) \
1092 DEF_OPT("imacros", no_fil, OPT_imacros) \
1093 DEF_OPT("include", no_fil, OPT_include) \
1094 DEF_OPT("iprefix", no_pth, OPT_iprefix) \
1095 DEF_OPT("isystem", no_dir, OPT_isystem) \
1096 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix) \
1097 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore) \
1098 DEF_OPT("lang-asm", 0, OPT_lang_asm) \
1099 DEF_OPT("lang-c", 0, OPT_lang_c) \
1100 DEF_OPT("lang-c++", 0, OPT_lang_cplusplus) \
1101 DEF_OPT("lang-c89", 0, OPT_lang_c89) \
1102 DEF_OPT("lang-objc", 0, OPT_lang_objc) \
1103 DEF_OPT("lang-objc++", 0, OPT_lang_objcplusplus) \
1104 DEF_OPT("nostdinc", 0, OPT_nostdinc) \
1105 DEF_OPT("nostdinc++", 0, OPT_nostdincplusplus) \
1106 DEF_OPT("o", no_fil, OPT_o) \
1107 DEF_OPT("pedantic", 0, OPT_pedantic) \
1108 DEF_OPT("pedantic-errors", 0, OPT_pedantic_errors) \
1109 DEF_OPT("remap", 0, OPT_remap) \
1110 DEF_OPT("std=c++98", 0, OPT_std_cplusplus98) \
1111 DEF_OPT("std=c89", 0, OPT_std_c89) \
1112 DEF_OPT("std=c99", 0, OPT_std_c99) \
1113 DEF_OPT("std=c9x", 0, OPT_std_c9x) \
1114 DEF_OPT("std=gnu89", 0, OPT_std_gnu89) \
1115 DEF_OPT("std=gnu99", 0, OPT_std_gnu99) \
1116 DEF_OPT("std=gnu9x", 0, OPT_std_gnu9x) \
1117 DEF_OPT("std=iso9899:1990", 0, OPT_std_iso9899_1990) \
1118 DEF_OPT("std=iso9899:199409", 0, OPT_std_iso9899_199409) \
1119 DEF_OPT("std=iso9899:1999", 0, OPT_std_iso9899_1999) \
1120 DEF_OPT("std=iso9899:199x", 0, OPT_std_iso9899_199x) \
1121 DEF_OPT("trigraphs", 0, OPT_trigraphs) \
1122 DEF_OPT("v", 0, OPT_v) \
1123 DEF_OPT("version", 0, OPT_version) \
1124 DEF_OPT("w", 0, OPT_w)
1125
1126 #define DEF_OPT(text, msg, code) code,
1127 enum opt_code
1128 {
1129 COMMAND_LINE_OPTIONS
1130 N_OPTS
1131 };
1132 #undef DEF_OPT
1133
1134 struct cl_option
1135 {
1136 const char *opt_text;
1137 const char *msg;
1138 size_t opt_len;
1139 enum opt_code opt_code;
1140 };
1141
1142 #define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
1143 #ifdef HOST_EBCDIC
1144 static struct cl_option cl_options[] =
1145 #else
1146 static const struct cl_option cl_options[] =
1147 #endif
1148 {
1149 COMMAND_LINE_OPTIONS
1150 };
1151 #undef DEF_OPT
1152 #undef COMMAND_LINE_OPTIONS
1153
1154 /* Perform a binary search to find which, if any, option the given
1155 command-line matches. Returns its index in the option array,
1156 negative on failure. Complications arise since some options can be
1157 suffixed with an argument, and multiple complete matches can occur,
1158 e.g. -iwithprefix and -iwithprefixbefore. Moreover, we need to
1159 accept options beginning with -W that we do not recognise, but not
1160 to swallow any subsequent command line argument; this is handled as
1161 special cases in cpp_handle_option. */
1162 static int
1163 parse_option (input)
1164 const char *input;
1165 {
1166 unsigned int md, mn, mx;
1167 size_t opt_len;
1168 int comp;
1169
1170 mn = 0;
1171 mx = N_OPTS;
1172
1173 while (mx > mn)
1174 {
1175 md = (mn + mx) / 2;
1176
1177 opt_len = cl_options[md].opt_len;
1178 comp = memcmp (input, cl_options[md].opt_text, opt_len);
1179
1180 if (comp > 0)
1181 mn = md + 1;
1182 else if (comp < 0)
1183 mx = md;
1184 else
1185 {
1186 if (input[opt_len] == '\0')
1187 return md;
1188 /* We were passed more text. If the option takes an argument,
1189 we may match a later option or we may have been passed the
1190 argument. The longest possible option match succeeds.
1191 If the option takes no arguments we have not matched and
1192 continue the search (e.g. input="stdc++" match was "stdc"). */
1193 mn = md + 1;
1194 if (cl_options[md].msg)
1195 {
1196 /* Scan forwards. If we get an exact match, return it.
1197 Otherwise, return the longest option-accepting match.
1198 This loops no more than twice with current options. */
1199 mx = md;
1200 for (; mn < (unsigned int) N_OPTS; mn++)
1201 {
1202 opt_len = cl_options[mn].opt_len;
1203 if (memcmp (input, cl_options[mn].opt_text, opt_len))
1204 break;
1205 if (input[opt_len] == '\0')
1206 return mn;
1207 if (cl_options[mn].msg)
1208 mx = mn;
1209 }
1210 return mx;
1211 }
1212 }
1213 }
1214
1215 return -1;
1216 }
1217
1218 /* Handle one command-line option in (argc, argv).
1219 Can be called multiple times, to handle multiple sets of options.
1220 Returns number of strings consumed. */
1221
1222 int
1223 cpp_handle_option (pfile, argc, argv)
1224 cpp_reader *pfile;
1225 int argc;
1226 char **argv;
1227 {
1228 int i = 0;
1229 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
1230
1231 /* Interpret "-" or a non-option as a file name. */
1232 if (argv[i][0] != '-' || argv[i][1] == '\0')
1233 {
1234 if (CPP_OPTION (pfile, in_fname) == NULL)
1235 CPP_OPTION (pfile, in_fname) = argv[i];
1236 else if (CPP_OPTION (pfile, out_fname) == NULL)
1237 CPP_OPTION (pfile, out_fname) = argv[i];
1238 else
1239 cpp_fatal (pfile, "Too many filenames. Type %s --help for usage info",
1240 progname);
1241 }
1242 else
1243 {
1244 enum opt_code opt_code;
1245 int opt_index;
1246 const char *arg = 0;
1247
1248 /* Skip over '-'. */
1249 opt_index = parse_option (&argv[i][1]);
1250 if (opt_index < 0)
1251 return i;
1252
1253 opt_code = cl_options[opt_index].opt_code;
1254 if (cl_options[opt_index].msg)
1255 {
1256 arg = &argv[i][cl_options[opt_index].opt_len + 1];
1257
1258 /* Yuk. Special case for -W as it must not swallow
1259 up any following argument. If this becomes common, add
1260 another field to the cl_options table. */
1261 if (arg[0] == '\0' && opt_code != OPT_W)
1262 {
1263 arg = argv[++i];
1264 if (!arg)
1265 {
1266 cpp_fatal (pfile, cl_options[opt_index].msg, argv[i - 1]);
1267 return argc;
1268 }
1269 }
1270 }
1271
1272 switch (opt_code)
1273 {
1274 case N_OPTS: /* Shut GCC up. */
1275 break;
1276 case OPT_fleading_underscore:
1277 CPP_OPTION (pfile, user_label_prefix) = "_";
1278 break;
1279 case OPT_fno_leading_underscore:
1280 CPP_OPTION (pfile, user_label_prefix) = "";
1281 break;
1282 case OPT_fno_operator_names:
1283 CPP_OPTION (pfile, operator_names) = 0;
1284 break;
1285 case OPT_fpreprocessed:
1286 CPP_OPTION (pfile, preprocessed) = 1;
1287 break;
1288 case OPT_fno_preprocessed:
1289 CPP_OPTION (pfile, preprocessed) = 0;
1290 break;
1291 case OPT_fshow_column:
1292 CPP_OPTION (pfile, show_column) = 1;
1293 break;
1294 case OPT_fno_show_column:
1295 CPP_OPTION (pfile, show_column) = 0;
1296 break;
1297 case OPT_ftabstop:
1298 /* Silently ignore empty string, non-longs and silly values. */
1299 if (arg[0] != '\0')
1300 {
1301 char *endptr;
1302 long tabstop = strtol (arg, &endptr, 10);
1303 if (*endptr == '\0' && tabstop >= 1 && tabstop <= 100)
1304 CPP_OPTION (pfile, tabstop) = tabstop;
1305 }
1306 break;
1307 case OPT_w:
1308 CPP_OPTION (pfile, inhibit_warnings) = 1;
1309 break;
1310 case OPT_h:
1311 case OPT__help:
1312 print_help ();
1313 CPP_OPTION (pfile, help_only) = 1;
1314 break;
1315 case OPT_target__help:
1316 /* Print if any target specific options. cpplib has none, but
1317 make sure help_only gets set. */
1318 CPP_OPTION (pfile, help_only) = 1;
1319 break;
1320
1321 /* --version inhibits compilation, -version doesn't. -v means
1322 verbose and -version. Historical reasons, don't ask. */
1323 case OPT__version:
1324 CPP_OPTION (pfile, help_only) = 1;
1325 pfile->print_version = 1;
1326 break;
1327 case OPT_v:
1328 CPP_OPTION (pfile, verbose) = 1;
1329 pfile->print_version = 1;
1330 break;
1331 case OPT_version:
1332 pfile->print_version = 1;
1333 break;
1334
1335 case OPT_C:
1336 CPP_OPTION (pfile, discard_comments) = 0;
1337 break;
1338 case OPT_P:
1339 CPP_OPTION (pfile, no_line_commands) = 1;
1340 break;
1341 case OPT_dollar: /* Don't include $ in identifiers. */
1342 CPP_OPTION (pfile, dollars_in_ident) = 0;
1343 break;
1344 case OPT_H:
1345 CPP_OPTION (pfile, print_include_names) = 1;
1346 break;
1347 case OPT_D:
1348 new_pending_directive (pend, arg, cpp_define);
1349 break;
1350 case OPT_pedantic_errors:
1351 CPP_OPTION (pfile, pedantic_errors) = 1;
1352 /* fall through */
1353 case OPT_pedantic:
1354 CPP_OPTION (pfile, pedantic) = 1;
1355 break;
1356 case OPT_trigraphs:
1357 CPP_OPTION (pfile, trigraphs) = 1;
1358 break;
1359 case OPT_plus:
1360 CPP_OPTION (pfile, cplusplus) = 1;
1361 CPP_OPTION (pfile, cplusplus_comments) = 1;
1362 break;
1363 case OPT_remap:
1364 CPP_OPTION (pfile, remap) = 1;
1365 break;
1366 case OPT_iprefix:
1367 CPP_OPTION (pfile, include_prefix) = arg;
1368 CPP_OPTION (pfile, include_prefix_len) = strlen (arg);
1369 break;
1370 case OPT_lang_c:
1371 set_lang (pfile, CLK_GNUC89);
1372 break;
1373 case OPT_lang_cplusplus:
1374 set_lang (pfile, CLK_GNUCXX);
1375 break;
1376 case OPT_lang_objc:
1377 set_lang (pfile, CLK_OBJC);
1378 break;
1379 case OPT_lang_objcplusplus:
1380 set_lang (pfile, CLK_OBJCXX);
1381 break;
1382 case OPT_lang_asm:
1383 set_lang (pfile, CLK_ASM);
1384 break;
1385 case OPT_std_cplusplus98:
1386 set_lang (pfile, CLK_CXX98);
1387 break;
1388 case OPT_std_gnu89:
1389 set_lang (pfile, CLK_GNUC89);
1390 break;
1391 case OPT_std_gnu9x:
1392 case OPT_std_gnu99:
1393 set_lang (pfile, CLK_GNUC99);
1394 break;
1395 case OPT_std_iso9899_199409:
1396 set_lang (pfile, CLK_STDC94);
1397 break;
1398 case OPT_std_iso9899_1990:
1399 case OPT_std_c89:
1400 case OPT_lang_c89:
1401 set_lang (pfile, CLK_STDC89);
1402 break;
1403 case OPT_std_iso9899_199x:
1404 case OPT_std_iso9899_1999:
1405 case OPT_std_c9x:
1406 case OPT_std_c99:
1407 set_lang (pfile, CLK_STDC99);
1408 break;
1409 case OPT_nostdinc:
1410 /* -nostdinc causes no default include directories.
1411 You must specify all include-file directories with -I. */
1412 CPP_OPTION (pfile, no_standard_includes) = 1;
1413 break;
1414 case OPT_nostdincplusplus:
1415 /* -nostdinc++ causes no default C++-specific include directories. */
1416 CPP_OPTION (pfile, no_standard_cplusplus_includes) = 1;
1417 break;
1418 case OPT_o:
1419 if (CPP_OPTION (pfile, out_fname) == NULL)
1420 CPP_OPTION (pfile, out_fname) = arg;
1421 else
1422 {
1423 cpp_fatal (pfile, "Output filename specified twice");
1424 return argc;
1425 }
1426 break;
1427 case OPT_d:
1428 /* Args to -d specify what parts of macros to dump.
1429 Silently ignore unrecognised options; they may
1430 be aimed at the compiler proper. */
1431 {
1432 char c;
1433
1434 while ((c = *arg++) != '\0')
1435 switch (c)
1436 {
1437 case 'M':
1438 CPP_OPTION (pfile, dump_macros) = dump_only;
1439 CPP_OPTION (pfile, no_output) = 1;
1440 break;
1441 case 'N':
1442 CPP_OPTION (pfile, dump_macros) = dump_names;
1443 break;
1444 case 'D':
1445 CPP_OPTION (pfile, dump_macros) = dump_definitions;
1446 break;
1447 case 'I':
1448 CPP_OPTION (pfile, dump_includes) = 1;
1449 break;
1450 }
1451 }
1452 break;
1453
1454 case OPT_MG:
1455 CPP_OPTION (pfile, print_deps_missing_files) = 1;
1456 break;
1457 case OPT_M:
1458 CPP_OPTION (pfile, print_deps) = 2;
1459 break;
1460 case OPT_MM:
1461 CPP_OPTION (pfile, print_deps) = 1;
1462 break;
1463 case OPT_MF:
1464 CPP_OPTION (pfile, deps_file) = arg;
1465 break;
1466 case OPT_MP:
1467 CPP_OPTION (pfile, deps_phony_targets) = 1;
1468 break;
1469 case OPT_MQ:
1470 case OPT_MT:
1471 /* Add a target. -MQ quotes for Make. */
1472 deps_add_target (pfile->deps, arg, opt_code == OPT_MQ);
1473 break;
1474
1475 /* -MD and -MMD for cpp0 are deprecated and undocumented
1476 (use -M or -MM with -MF instead), and probably should be
1477 removed with the next major GCC version. For the moment
1478 we allow these for the benefit of Automake 1.4, which
1479 uses these when dependency tracking is enabled. Automake
1480 1.5 will fix this. */
1481 case OPT_MD:
1482 CPP_OPTION (pfile, print_deps) = 2;
1483 CPP_OPTION (pfile, deps_file) = arg;
1484 break;
1485 case OPT_MMD:
1486 CPP_OPTION (pfile, print_deps) = 1;
1487 CPP_OPTION (pfile, deps_file) = arg;
1488 break;
1489
1490 case OPT_A:
1491 if (arg[0] == '-')
1492 {
1493 /* -A with an argument beginning with '-' acts as
1494 #unassert on whatever immediately follows the '-'.
1495 If "-" is the whole argument, we eliminate all
1496 predefined macros and assertions, including those
1497 that were specified earlier on the command line.
1498 That way we can get rid of any that were passed
1499 automatically in from GCC. */
1500
1501 if (arg[1] == '\0')
1502 {
1503 struct pending_option *o1, *o2;
1504
1505 o1 = pend->directive_head;
1506 while (o1)
1507 {
1508 o2 = o1->next;
1509 free (o1);
1510 o1 = o2;
1511 }
1512 pend->directive_head = NULL;
1513 pend->directive_tail = NULL;
1514 }
1515 else
1516 new_pending_directive (pend, arg + 1, cpp_unassert);
1517 }
1518 else
1519 new_pending_directive (pend, arg, cpp_assert);
1520 break;
1521 case OPT_U:
1522 new_pending_directive (pend, arg, cpp_undef);
1523 break;
1524 case OPT_I: /* Add directory to path for includes. */
1525 if (!strcmp (arg, "-"))
1526 {
1527 /* -I- means:
1528 Use the preceding -I directories for #include "..."
1529 but not #include <...>.
1530 Don't search the directory of the present file
1531 for #include "...". (Note that -I. -I- is not the same as
1532 the default setup; -I. uses the compiler's working dir.) */
1533 if (! CPP_OPTION (pfile, ignore_srcdir))
1534 {
1535 pend->quote_head = pend->brack_head;
1536 pend->quote_tail = pend->brack_tail;
1537 pend->brack_head = 0;
1538 pend->brack_tail = 0;
1539 CPP_OPTION (pfile, ignore_srcdir) = 1;
1540 }
1541 else
1542 {
1543 cpp_fatal (pfile, "-I- specified twice");
1544 return argc;
1545 }
1546 }
1547 else
1548 append_include_chain (pfile, xstrdup (arg), BRACKET, 0);
1549 break;
1550 case OPT_isystem:
1551 /* Add directory to beginning of system include path, as a system
1552 include directory. */
1553 append_include_chain (pfile, xstrdup (arg), SYSTEM, 0);
1554 break;
1555 case OPT_include:
1556 {
1557 struct pending_option *o = (struct pending_option *)
1558 xmalloc (sizeof (struct pending_option));
1559 o->arg = arg;
1560
1561 /* This list has to be built in reverse order so that
1562 when cpp_start_read pushes all the -include files onto
1563 the buffer stack, they will be scanned in forward order. */
1564 o->next = pend->include_head;
1565 pend->include_head = o;
1566 }
1567 break;
1568 case OPT_imacros:
1569 {
1570 struct pending_option *o = (struct pending_option *)
1571 xmalloc (sizeof (struct pending_option));
1572 o->arg = arg;
1573 o->next = NULL;
1574
1575 APPEND (pend, imacros, o);
1576 }
1577 break;
1578 case OPT_iwithprefix:
1579 /* Add directory to end of path for includes,
1580 with the default prefix at the front of its name. */
1581 /* fall through */
1582 case OPT_iwithprefixbefore:
1583 /* Add directory to main path for includes,
1584 with the default prefix at the front of its name. */
1585 {
1586 char *fname;
1587 int len;
1588
1589 len = strlen (arg);
1590
1591 if (CPP_OPTION (pfile, include_prefix) != 0)
1592 {
1593 size_t ipl = CPP_OPTION (pfile, include_prefix_len);
1594 fname = xmalloc (ipl + len + 1);
1595 memcpy (fname, CPP_OPTION (pfile, include_prefix), ipl);
1596 memcpy (fname + ipl, arg, len + 1);
1597 }
1598 else if (cpp_GCC_INCLUDE_DIR_len)
1599 {
1600 fname = xmalloc (cpp_GCC_INCLUDE_DIR_len + len + 1);
1601 memcpy (fname, cpp_GCC_INCLUDE_DIR, cpp_GCC_INCLUDE_DIR_len);
1602 memcpy (fname + cpp_GCC_INCLUDE_DIR_len, arg, len + 1);
1603 }
1604 else
1605 fname = xstrdup (arg);
1606
1607 append_include_chain (pfile, fname,
1608 opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0);
1609 }
1610 break;
1611 case OPT_idirafter:
1612 /* Add directory to end of path for includes. */
1613 append_include_chain (pfile, xstrdup (arg), AFTER, 0);
1614 break;
1615 case OPT_W:
1616 /* Silently ignore unrecognised options. */
1617 if (!strcmp (argv[i], "-Wall"))
1618 {
1619 CPP_OPTION (pfile, warn_trigraphs) = 1;
1620 CPP_OPTION (pfile, warn_comments) = 1;
1621 }
1622 else if (!strcmp (argv[i], "-Wtraditional"))
1623 CPP_OPTION (pfile, warn_traditional) = 1;
1624 else if (!strcmp (argv[i], "-Wtrigraphs"))
1625 CPP_OPTION (pfile, warn_trigraphs) = 1;
1626 else if (!strcmp (argv[i], "-Wcomment"))
1627 CPP_OPTION (pfile, warn_comments) = 1;
1628 else if (!strcmp (argv[i], "-Wcomments"))
1629 CPP_OPTION (pfile, warn_comments) = 1;
1630 else if (!strcmp (argv[i], "-Wundef"))
1631 CPP_OPTION (pfile, warn_undef) = 1;
1632 else if (!strcmp (argv[i], "-Wimport"))
1633 CPP_OPTION (pfile, warn_import) = 1;
1634 else if (!strcmp (argv[i], "-Werror"))
1635 CPP_OPTION (pfile, warnings_are_errors) = 1;
1636 else if (!strcmp (argv[i], "-Wsystem-headers"))
1637 CPP_OPTION (pfile, warn_system_headers) = 1;
1638 else if (!strcmp (argv[i], "-Wno-traditional"))
1639 CPP_OPTION (pfile, warn_traditional) = 0;
1640 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1641 CPP_OPTION (pfile, warn_trigraphs) = 0;
1642 else if (!strcmp (argv[i], "-Wno-comment"))
1643 CPP_OPTION (pfile, warn_comments) = 0;
1644 else if (!strcmp (argv[i], "-Wno-comments"))
1645 CPP_OPTION (pfile, warn_comments) = 0;
1646 else if (!strcmp (argv[i], "-Wno-undef"))
1647 CPP_OPTION (pfile, warn_undef) = 0;
1648 else if (!strcmp (argv[i], "-Wno-import"))
1649 CPP_OPTION (pfile, warn_import) = 0;
1650 else if (!strcmp (argv[i], "-Wno-error"))
1651 CPP_OPTION (pfile, warnings_are_errors) = 0;
1652 else if (!strcmp (argv[i], "-Wno-system-headers"))
1653 CPP_OPTION (pfile, warn_system_headers) = 0;
1654 break;
1655 }
1656 }
1657 return i + 1;
1658 }
1659
1660 /* Handle command-line options in (argc, argv).
1661 Can be called multiple times, to handle multiple sets of options.
1662 Returns if an unrecognized option is seen.
1663 Returns number of strings consumed. */
1664 int
1665 cpp_handle_options (pfile, argc, argv)
1666 cpp_reader *pfile;
1667 int argc;
1668 char **argv;
1669 {
1670 int i;
1671 int strings_processed;
1672
1673 for (i = 0; i < argc; i += strings_processed)
1674 {
1675 strings_processed = cpp_handle_option (pfile, argc - i, argv + i);
1676 if (strings_processed == 0)
1677 break;
1678 }
1679
1680 return i;
1681 }
1682
1683 /* Extra processing when all options are parsed, after all calls to
1684 cpp_handle_option[s]. Consistency checks etc. */
1685 void
1686 cpp_post_options (pfile)
1687 cpp_reader *pfile;
1688 {
1689 if (pfile->print_version)
1690 {
1691 fprintf (stderr, _("GNU CPP version %s (cpplib)"), version_string);
1692 #ifdef TARGET_VERSION
1693 TARGET_VERSION;
1694 #endif
1695 fputc ('\n', stderr);
1696 }
1697
1698 /* Canonicalize in_fname and out_fname. We guarantee they are not
1699 NULL, and that the empty string represents stdin / stdout. */
1700 if (CPP_OPTION (pfile, in_fname) == NULL
1701 || !strcmp (CPP_OPTION (pfile, in_fname), "-"))
1702 CPP_OPTION (pfile, in_fname) = "";
1703
1704 if (CPP_OPTION (pfile, out_fname) == NULL
1705 || !strcmp (CPP_OPTION (pfile, out_fname), "-"))
1706 CPP_OPTION (pfile, out_fname) = "";
1707
1708 /* -Wtraditional is not useful in C++ mode. */
1709 if (CPP_OPTION (pfile, cplusplus))
1710 CPP_OPTION (pfile, warn_traditional) = 0;
1711
1712 /* Set this if it hasn't been set already. */
1713 if (CPP_OPTION (pfile, user_label_prefix) == NULL)
1714 CPP_OPTION (pfile, user_label_prefix) = USER_LABEL_PREFIX;
1715
1716 /* Permanently disable macro expansion if we are rescanning
1717 preprocessed text. */
1718 if (CPP_OPTION (pfile, preprocessed))
1719 pfile->state.prevent_expansion = 1;
1720
1721 /* We need to do this after option processing and before
1722 cpp_start_read, as cppmain.c relies on the options->no_output to
1723 set its callbacks correctly before calling cpp_start_read. */
1724 init_dependency_output (pfile);
1725
1726 /* After checking the environment variables, check if -M or -MM has
1727 not been specified, but other -M options have. */
1728 if (CPP_OPTION (pfile, print_deps) == 0 &&
1729 (CPP_OPTION (pfile, print_deps_missing_files)
1730 || CPP_OPTION (pfile, deps_file)
1731 || CPP_OPTION (pfile, deps_phony_targets)))
1732 cpp_fatal (pfile, "you must additionally specify either -M or -MM");
1733 }
1734
1735 /* Set up dependency-file output. */
1736 static void
1737 init_dependency_output (pfile)
1738 cpp_reader *pfile;
1739 {
1740 char *spec, *s, *output_file;
1741
1742 /* Either of two environment variables can specify output of deps.
1743 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1744 where OUTPUT_FILE is the file to write deps info to
1745 and DEPS_TARGET is the target to mention in the deps. */
1746
1747 if (CPP_OPTION (pfile, print_deps) == 0)
1748 {
1749 spec = getenv ("DEPENDENCIES_OUTPUT");
1750 if (spec)
1751 CPP_OPTION (pfile, print_deps) = 1;
1752 else
1753 {
1754 spec = getenv ("SUNPRO_DEPENDENCIES");
1755 if (spec)
1756 CPP_OPTION (pfile, print_deps) = 2;
1757 else
1758 return;
1759 }
1760
1761 /* Find the space before the DEPS_TARGET, if there is one. */
1762 s = strchr (spec, ' ');
1763 if (s)
1764 {
1765 /* Let the caller perform MAKE quoting. */
1766 deps_add_target (pfile->deps, s + 1, 0);
1767 output_file = (char *) xmalloc (s - spec + 1);
1768 memcpy (output_file, spec, s - spec);
1769 output_file[s - spec] = 0;
1770 }
1771 else
1772 output_file = spec;
1773
1774 /* Command line overrides environment variables. */
1775 if (CPP_OPTION (pfile, deps_file) == 0)
1776 CPP_OPTION (pfile, deps_file) = output_file;
1777 CPP_OPTION (pfile, print_deps_append) = 1;
1778 }
1779
1780 /* If dependencies go to standard output, or -MG is used, we should
1781 suppress output, including -dM, -dI etc. */
1782 if (CPP_OPTION (pfile, deps_file) == 0
1783 || CPP_OPTION (pfile, print_deps_missing_files))
1784 {
1785 CPP_OPTION (pfile, no_output) = 1;
1786 CPP_OPTION (pfile, dump_macros) = 0;
1787 CPP_OPTION (pfile, dump_includes) = 0;
1788 }
1789 }
1790
1791 static void
1792 print_help ()
1793 {
1794 fprintf (stderr, _("Usage: %s [switches] input output\n"), progname);
1795 /* To keep the lines from getting too long for some compilers, limit
1796 to about 500 characters (6 lines) per chunk. */
1797 fputs (_("\
1798 Switches:\n\
1799 -include <file> Include the contents of <file> before other files\n\
1800 -imacros <file> Accept definition of macros in <file>\n\
1801 -iprefix <path> Specify <path> as a prefix for next two options\n\
1802 -iwithprefix <dir> Add <dir> to the end of the system include path\n\
1803 -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\
1804 -isystem <dir> Add <dir> to the start of the system include path\n\
1805 "), stdout);
1806 fputs (_("\
1807 -idirafter <dir> Add <dir> to the end of the system include path\n\
1808 -I <dir> Add <dir> to the end of the main include path\n\
1809 -I- Fine-grained include path control; see info docs\n\
1810 -nostdinc Do not search system include directories\n\
1811 (dirs specified with -isystem will still be used)\n\
1812 -nostdinc++ Do not search system include directories for C++\n\
1813 -o <file> Put output into <file>\n\
1814 "), stdout);
1815 fputs (_("\
1816 -pedantic Issue all warnings demanded by strict ISO C\n\
1817 -pedantic-errors Issue -pedantic warnings as errors instead\n\
1818 -trigraphs Support ISO C trigraphs\n\
1819 -lang-c Assume that the input sources are in C\n\
1820 -lang-c89 Assume that the input sources are in C89\n\
1821 "), stdout);
1822 fputs (_("\
1823 -lang-c++ Assume that the input sources are in C++\n\
1824 -lang-objc Assume that the input sources are in ObjectiveC\n\
1825 -lang-objc++ Assume that the input sources are in ObjectiveC++\n\
1826 -lang-asm Assume that the input sources are in assembler\n\
1827 "), stdout);
1828 fputs (_("\
1829 -std=<std name> Specify the conformance standard; one of:\n\
1830 gnu89, gnu99, c89, c99, iso9899:1990,\n\
1831 iso9899:199409, iso9899:1999\n\
1832 -+ Allow parsing of C++ style features\n\
1833 -w Inhibit warning messages\n\
1834 -Wtrigraphs Warn if trigraphs are encountered\n\
1835 -Wno-trigraphs Do not warn about trigraphs\n\
1836 -Wcomment{s} Warn if one comment starts inside another\n\
1837 "), stdout);
1838 fputs (_("\
1839 -Wno-comment{s} Do not warn about comments\n\
1840 -Wtraditional Warn about features not present in traditional C\n\
1841 -Wno-traditional Do not warn about traditional C\n\
1842 -Wundef Warn if an undefined macro is used by #if\n\
1843 -Wno-undef Do not warn about testing undefined macros\n\
1844 -Wimport Warn about the use of the #import directive\n\
1845 "), stdout);
1846 fputs (_("\
1847 -Wno-import Do not warn about the use of #import\n\
1848 -Werror Treat all warnings as errors\n\
1849 -Wno-error Do not treat warnings as errors\n\
1850 -Wsystem-headers Do not suppress warnings from system headers\n\
1851 -Wno-system-headers Suppress warnings from system headers\n\
1852 -Wall Enable all preprocessor warnings\n\
1853 "), stdout);
1854 fputs (_("\
1855 -M Generate make dependencies\n\
1856 -MM As -M, but ignore system header files\n\
1857 -MF <file> Write dependency output to the given file\n\
1858 -MG Treat missing header file as generated files\n\
1859 "), stdout);
1860 fputs (_("\
1861 -MP Generate phony targets for all headers\n\
1862 -MQ <target> Add a MAKE-quoted target\n\
1863 -MT <target> Add an unquoted target\n\
1864 "), stdout);
1865 fputs (_("\
1866 -D<macro> Define a <macro> with string '1' as its value\n\
1867 -D<macro>=<val> Define a <macro> with <val> as its value\n\
1868 -A<question> (<answer>) Assert the <answer> to <question>\n\
1869 -A-<question> (<answer>) Disable the <answer> to <question>\n\
1870 -U<macro> Undefine <macro> \n\
1871 -v Display the version number\n\
1872 "), stdout);
1873 fputs (_("\
1874 -H Print the name of header files as they are used\n\
1875 -C Do not discard comments\n\
1876 -dM Display a list of macro definitions active at end\n\
1877 -dD Preserve macro definitions in output\n\
1878 -dN As -dD except that only the names are preserved\n\
1879 -dI Include #include directives in the output\n\
1880 "), stdout);
1881 fputs (_("\
1882 -fpreprocessed Treat the input file as already preprocessed\n\
1883 -ftabstop=<number> Distance between tab stops for column reporting\n\
1884 -P Do not generate #line directives\n\
1885 -$ Do not allow '$' in identifiers\n\
1886 -remap Remap file names when including files.\n\
1887 --version Display version information\n\
1888 -h or --help Display this information\n\
1889 "), stdout);
1890 }