Use MODE_BASE_REG_CLASS in legitimize macros.
[official-gcc.git] / gcc / cppinit.c
blobc0f0348ff65444f5e9cdee56fc832295504d7896
1 /* CPP Library.
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 Contributed by Per Bothner, 1994-95.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "cpplib.h"
25 #include "cpphash.h"
26 #include "prefix.h"
27 #include "intl.h"
28 #include "version.h"
29 #include "mkdeps.h"
30 #include "cppdefault.h"
31 #include "except.h" /* for USING_SJLJ_EXCEPTIONS */
33 /* Predefined symbols, built-in macros, and the default include path. */
35 #ifndef GET_ENV_PATH_LIST
36 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
37 #endif
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 # define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
45 #else
46 # if (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
47 # define INO_T_EQ(A, B) 0
48 # else
49 # define INO_T_EQ(A, B) ((A) == (B))
50 # endif
51 # define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
52 #endif
54 /* Internal structures and prototypes. */
56 /* A `struct pending_option' remembers one -D, -A, -U, -include, or
57 -imacros switch. */
58 typedef void (* cl_directive_handler) PARAMS ((cpp_reader *, const char *));
59 struct pending_option
61 struct pending_option *next;
62 const char *arg;
63 cl_directive_handler handler;
66 /* The `pending' structure accumulates all the options that are not
67 actually processed until we hit cpp_read_main_file. It consists of
68 several lists, one for each type of option. We keep both head and
69 tail pointers for quick insertion. */
70 struct cpp_pending
72 struct pending_option *directive_head, *directive_tail;
74 struct search_path *quote_head, *quote_tail;
75 struct search_path *brack_head, *brack_tail;
76 struct search_path *systm_head, *systm_tail;
77 struct search_path *after_head, *after_tail;
79 struct pending_option *imacros_head, *imacros_tail;
80 struct pending_option *include_head, *include_tail;
83 #ifdef __STDC__
84 #define APPEND(pend, list, elt) \
85 do { if (!(pend)->list##_head) (pend)->list##_head = (elt); \
86 else (pend)->list##_tail->next = (elt); \
87 (pend)->list##_tail = (elt); \
88 } while (0)
89 #else
90 #define APPEND(pend, list, elt) \
91 do { if (!(pend)->list/**/_head) (pend)->list/**/_head = (elt); \
92 else (pend)->list/**/_tail->next = (elt); \
93 (pend)->list/**/_tail = (elt); \
94 } while (0)
95 #endif
97 static void print_help PARAMS ((void));
98 static void path_include PARAMS ((cpp_reader *,
99 char *, int));
100 static void init_library PARAMS ((void));
101 static void init_builtins PARAMS ((cpp_reader *));
102 static void append_include_chain PARAMS ((cpp_reader *,
103 char *, int, int));
104 static struct search_path * remove_dup_dir PARAMS ((cpp_reader *,
105 struct search_path *));
106 static struct search_path * remove_dup_dirs PARAMS ((cpp_reader *,
107 struct search_path *));
108 static void merge_include_chains PARAMS ((cpp_reader *));
109 static bool push_include PARAMS ((cpp_reader *,
110 struct pending_option *));
111 static void free_chain PARAMS ((struct pending_option *));
112 static void set_lang PARAMS ((cpp_reader *, enum c_lang));
113 static void init_dependency_output PARAMS ((cpp_reader *));
114 static void init_standard_includes PARAMS ((cpp_reader *));
115 static void read_original_filename PARAMS ((cpp_reader *));
116 static void new_pending_directive PARAMS ((struct cpp_pending *,
117 const char *,
118 cl_directive_handler));
119 static void output_deps PARAMS ((cpp_reader *));
120 static int parse_option PARAMS ((const char *));
122 /* Fourth argument to append_include_chain: chain to use.
123 Note it's never asked to append to the quote chain. */
124 enum { BRACKET = 0, SYSTEM, AFTER };
126 /* If we have designated initializers (GCC >2.7) these tables can be
127 initialized, constant data. Otherwise, they have to be filled in at
128 runtime. */
129 #if HAVE_DESIGNATED_INITIALIZERS
131 #define init_trigraph_map() /* Nothing. */
132 #define TRIGRAPH_MAP \
133 __extension__ const U_CHAR _cpp_trigraph_map[UCHAR_MAX + 1] = {
135 #define END };
136 #define s(p, v) [p] = v,
138 #else
140 #define TRIGRAPH_MAP U_CHAR _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
141 static void init_trigraph_map PARAMS ((void)) { \
142 unsigned char *x = _cpp_trigraph_map;
144 #define END }
145 #define s(p, v) x[p] = v;
147 #endif
149 TRIGRAPH_MAP
150 s('=', '#') s(')', ']') s('!', '|')
151 s('(', '[') s('\'', '^') s('>', '}')
152 s('/', '\\') s('<', '{') s('-', '~')
155 #undef s
156 #undef END
157 #undef TRIGRAPH_MAP
159 /* Given a colon-separated list of file names PATH,
160 add all the names to the search path for include files. */
161 static void
162 path_include (pfile, list, path)
163 cpp_reader *pfile;
164 char *list;
165 int path;
167 char *p, *q, *name;
169 p = list;
173 /* Find the end of this name. */
174 q = p;
175 while (*q != 0 && *q != PATH_SEPARATOR) q++;
176 if (q == p)
178 /* An empty name in the path stands for the current directory. */
179 name = (char *) xmalloc (2);
180 name[0] = '.';
181 name[1] = 0;
183 else
185 /* Otherwise use the directory that is named. */
186 name = (char *) xmalloc (q - p + 1);
187 memcpy (name, p, q - p);
188 name[q - p] = 0;
191 append_include_chain (pfile, name, path, 0);
193 /* Advance past this name. */
194 if (*q == 0)
195 break;
196 p = q + 1;
198 while (1);
201 /* Append DIR to include path PATH. DIR must be allocated on the
202 heap; this routine takes responsibility for freeing it. CXX_AWARE
203 is non-zero if the header contains extern "C" guards for C++,
204 otherwise it is zero. */
205 static void
206 append_include_chain (pfile, dir, path, cxx_aware)
207 cpp_reader *pfile;
208 char *dir;
209 int path;
210 int cxx_aware ATTRIBUTE_UNUSED;
212 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
213 struct search_path *new;
214 struct stat st;
215 unsigned int len;
217 if (*dir == '\0')
219 free (dir);
220 dir = xstrdup (".");
222 _cpp_simplify_pathname (dir);
224 if (stat (dir, &st))
226 /* Dirs that don't exist are silently ignored. */
227 if (errno != ENOENT)
228 cpp_notice_from_errno (pfile, dir);
229 else if (CPP_OPTION (pfile, verbose))
230 fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"), dir);
231 free (dir);
232 return;
235 if (!S_ISDIR (st.st_mode))
237 cpp_notice (pfile, "%s: Not a directory", dir);
238 free (dir);
239 return;
242 len = strlen (dir);
243 if (len > pfile->max_include_len)
244 pfile->max_include_len = len;
246 new = (struct search_path *) xmalloc (sizeof (struct search_path));
247 new->name = dir;
248 new->len = len;
249 INO_T_COPY (new->ino, st.st_ino);
250 new->dev = st.st_dev;
251 /* Both systm and after include file lists should be treated as system
252 include files since these two lists are really just a concatenation
253 of one "system" list. */
254 if (path == SYSTEM || path == AFTER)
255 #ifdef NO_IMPLICIT_EXTERN_C
256 new->sysp = 1;
257 #else
258 new->sysp = cxx_aware ? 1 : 2;
259 #endif
260 else
261 new->sysp = 0;
262 new->name_map = NULL;
263 new->next = NULL;
265 switch (path)
267 case BRACKET: APPEND (pend, brack, new); break;
268 case SYSTEM: APPEND (pend, systm, new); break;
269 case AFTER: APPEND (pend, after, new); break;
273 /* Handle a duplicated include path. PREV is the link in the chain
274 before the duplicate. The duplicate is removed from the chain and
275 freed. Returns PREV. */
276 static struct search_path *
277 remove_dup_dir (pfile, prev)
278 cpp_reader *pfile;
279 struct search_path *prev;
281 struct search_path *cur = prev->next;
283 if (CPP_OPTION (pfile, verbose))
284 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), cur->name);
286 prev->next = cur->next;
287 free ((PTR) cur->name);
288 free (cur);
290 return prev;
293 /* Remove duplicate directories from a chain. Returns the tail of the
294 chain, or NULL if the chain is empty. This algorithm is quadratic
295 in the number of -I switches, which is acceptable since there
296 aren't usually that many of them. */
297 static struct search_path *
298 remove_dup_dirs (pfile, head)
299 cpp_reader *pfile;
300 struct search_path *head;
302 struct search_path *prev = NULL, *cur, *other;
304 for (cur = head; cur; cur = cur->next)
306 for (other = head; other != cur; other = other->next)
307 if (INO_T_EQ (cur->ino, other->ino) && cur->dev == other->dev)
309 if (cur->sysp && !other->sysp)
311 cpp_warning (pfile,
312 "changing search order for system directory \"%s\"",
313 cur->name);
314 if (strcmp (cur->name, other->name))
315 cpp_warning (pfile,
316 " as it is the same as non-system directory \"%s\"",
317 other->name);
318 else
319 cpp_warning (pfile,
320 " as it has already been specified as a non-system directory");
322 cur = remove_dup_dir (pfile, prev);
323 break;
325 prev = cur;
328 return prev;
331 /* Merge the four include chains together in the order quote, bracket,
332 system, after. Remove duplicate dirs (as determined by
333 INO_T_EQ()). The system_include and after_include chains are never
334 referred to again after this function; all access is through the
335 bracket_include path. */
336 static void
337 merge_include_chains (pfile)
338 cpp_reader *pfile;
340 struct search_path *quote, *brack, *systm, *qtail;
342 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
344 quote = pend->quote_head;
345 brack = pend->brack_head;
346 systm = pend->systm_head;
347 qtail = pend->quote_tail;
349 /* Paste together bracket, system, and after include chains. */
350 if (systm)
351 pend->systm_tail->next = pend->after_head;
352 else
353 systm = pend->after_head;
355 if (brack)
356 pend->brack_tail->next = systm;
357 else
358 brack = systm;
360 /* This is a bit tricky. First we drop dupes from the quote-include
361 list. Then we drop dupes from the bracket-include list.
362 Finally, if qtail and brack are the same directory, we cut out
363 brack and move brack up to point to qtail.
365 We can't just merge the lists and then uniquify them because
366 then we may lose directories from the <> search path that should
367 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
368 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
369 -Ibar -I- -Ifoo -Iquux. */
371 remove_dup_dirs (pfile, brack);
372 qtail = remove_dup_dirs (pfile, quote);
374 if (quote)
376 qtail->next = brack;
378 /* If brack == qtail, remove brack as it's simpler. */
379 if (INO_T_EQ (qtail->ino, brack->ino) && qtail->dev == brack->dev)
380 brack = remove_dup_dir (pfile, qtail);
382 else
383 quote = brack;
385 CPP_OPTION (pfile, quote_include) = quote;
386 CPP_OPTION (pfile, bracket_include) = brack;
389 /* A set of booleans indicating what CPP features each source language
390 requires. */
391 struct lang_flags
393 char c99;
394 char objc;
395 char cplusplus;
396 char extended_numbers;
397 char trigraphs;
398 char dollars_in_ident;
399 char cplusplus_comments;
400 char digraphs;
403 /* ??? Enable $ in identifiers in assembly? */
404 static const struct lang_flags lang_defaults[] =
405 { /* c99 objc c++ xnum trig dollar c++comm digr */
406 /* GNUC89 */ { 0, 0, 0, 1, 0, 1, 1, 1 },
407 /* GNUC99 */ { 1, 0, 0, 1, 0, 1, 1, 1 },
408 /* STDC89 */ { 0, 0, 0, 0, 1, 0, 0, 0 },
409 /* STDC94 */ { 0, 0, 0, 0, 1, 0, 0, 1 },
410 /* STDC99 */ { 1, 0, 0, 1, 1, 0, 1, 1 },
411 /* GNUCXX */ { 0, 0, 1, 1, 0, 1, 1, 1 },
412 /* CXX98 */ { 0, 0, 1, 1, 1, 0, 1, 1 },
413 /* OBJC */ { 0, 1, 0, 1, 0, 1, 1, 1 },
414 /* OBJCXX */ { 0, 1, 1, 1, 0, 1, 1, 1 },
415 /* ASM */ { 0, 0, 0, 1, 0, 0, 1, 0 }
418 /* Sets internal flags correctly for a given language. */
419 static void
420 set_lang (pfile, lang)
421 cpp_reader *pfile;
422 enum c_lang lang;
424 const struct lang_flags *l = &lang_defaults[(int) lang];
426 CPP_OPTION (pfile, lang) = lang;
428 CPP_OPTION (pfile, c99) = l->c99;
429 CPP_OPTION (pfile, objc) = l->objc;
430 CPP_OPTION (pfile, cplusplus) = l->cplusplus;
431 CPP_OPTION (pfile, extended_numbers) = l->extended_numbers;
432 CPP_OPTION (pfile, trigraphs) = l->trigraphs;
433 CPP_OPTION (pfile, dollars_in_ident) = l->dollars_in_ident;
434 CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments;
435 CPP_OPTION (pfile, digraphs) = l->digraphs;
438 #ifdef HOST_EBCDIC
439 static int opt_comp PARAMS ((const void *, const void *));
441 /* Run-time sorting of options array. */
442 static int
443 opt_comp (p1, p2)
444 const void *p1, *p2;
446 return strcmp (((struct cl_option *) p1)->opt_text,
447 ((struct cl_option *) p2)->opt_text);
449 #endif
451 /* init initializes library global state. It might not need to
452 do anything depending on the platform and compiler. */
453 static void
454 init_library ()
456 static int initialized = 0;
458 if (! initialized)
460 initialized = 1;
462 #ifdef HOST_EBCDIC
463 /* For non-ASCII hosts, the cl_options array needs to be sorted at
464 runtime. */
465 qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
466 #endif
468 /* Set up the trigraph map. This doesn't need to do anything if
469 we were compiled with a compiler that supports C99 designated
470 initializers. */
471 init_trigraph_map ();
475 /* Initialize a cpp_reader structure. */
476 cpp_reader *
477 cpp_create_reader (lang)
478 enum c_lang lang;
480 cpp_reader *pfile;
482 /* Initialise this instance of the library if it hasn't been already. */
483 init_library ();
485 pfile = (cpp_reader *) xcalloc (1, sizeof (cpp_reader));
487 set_lang (pfile, lang);
488 CPP_OPTION (pfile, warn_import) = 1;
489 CPP_OPTION (pfile, discard_comments) = 1;
490 CPP_OPTION (pfile, show_column) = 1;
491 CPP_OPTION (pfile, tabstop) = 8;
492 CPP_OPTION (pfile, operator_names) = 1;
494 CPP_OPTION (pfile, pending) =
495 (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
497 /* It's simplest to just create this struct whether or not it will
498 be needed. */
499 pfile->deps = deps_init ();
501 /* Initialise the line map. Start at logical line 1, so we can use
502 a line number of zero for special states. */
503 init_line_maps (&pfile->line_maps);
504 pfile->line = 1;
506 /* Initialize lexer state. */
507 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
509 /* Set up static tokens. */
510 pfile->date.type = CPP_EOF;
511 pfile->avoid_paste.type = CPP_PADDING;
512 pfile->avoid_paste.val.source = NULL;
513 pfile->eof.type = CPP_EOF;
514 pfile->eof.flags = 0;
516 /* Create a token buffer for the lexer. */
517 _cpp_init_tokenrun (&pfile->base_run, 250);
518 pfile->cur_run = &pfile->base_run;
519 pfile->cur_token = pfile->base_run.base;
521 /* Initialise the base context. */
522 pfile->context = &pfile->base_context;
523 pfile->base_context.macro = 0;
524 pfile->base_context.prev = pfile->base_context.next = 0;
526 /* Aligned and unaligned storage. */
527 pfile->a_buff = _cpp_get_buff (pfile, 0);
528 pfile->u_buff = _cpp_get_buff (pfile, 0);
530 /* Initialise the buffer obstack. */
531 gcc_obstack_init (&pfile->buffer_ob);
533 _cpp_init_includes (pfile);
535 return pfile;
538 /* Free resources used by PFILE. Accessing PFILE after this function
539 returns leads to undefined behaviour. Returns the error count. */
541 cpp_destroy (pfile)
542 cpp_reader *pfile;
544 int result;
545 struct search_path *dir, *dirn;
546 cpp_context *context, *contextn;
547 tokenrun *run, *runn;
549 while (CPP_BUFFER (pfile) != NULL)
550 _cpp_pop_buffer (pfile);
552 if (pfile->macro_buffer)
554 free ((PTR) pfile->macro_buffer);
555 pfile->macro_buffer = NULL;
556 pfile->macro_buffer_len = 0;
559 deps_free (pfile->deps);
560 obstack_free (&pfile->buffer_ob, 0);
562 _cpp_destroy_hashtable (pfile);
563 _cpp_cleanup_includes (pfile);
565 _cpp_free_buff (pfile->a_buff);
566 _cpp_free_buff (pfile->u_buff);
567 _cpp_free_buff (pfile->free_buffs);
569 for (run = &pfile->base_run; run; run = runn)
571 runn = run->next;
572 free (run->base);
573 if (run != &pfile->base_run)
574 free (run);
577 for (dir = CPP_OPTION (pfile, quote_include); dir; dir = dirn)
579 dirn = dir->next;
580 free ((PTR) dir->name);
581 free (dir);
584 for (context = pfile->base_context.next; context; context = contextn)
586 contextn = context->next;
587 free (context);
590 free_line_maps (&pfile->line_maps);
592 result = pfile->errors;
593 free (pfile);
595 return result;
599 /* This structure defines one built-in identifier. A node will be
600 entered in the hash table under the name NAME, with value VALUE (if
601 any). If flags has OPERATOR, the node's operator field is used; if
602 flags has BUILTIN the node's builtin field is used. Macros that are
603 known at build time should not be flagged BUILTIN, as then they do
604 not appear in macro dumps with e.g. -dM or -dD.
606 Two values are not compile time constants, so we tag
607 them in the FLAGS field instead:
608 VERS value is the global version_string, quoted
609 ULP value is the global user_label_prefix
611 Also, macros with CPLUS set in the flags field are entered only for C++. */
612 struct builtin
614 const U_CHAR *name;
615 const char *value;
616 unsigned char builtin;
617 unsigned char operator;
618 unsigned short flags;
619 unsigned short len;
621 #define VERS 0x01
622 #define ULP 0x02
623 #define CPLUS 0x04
624 #define BUILTIN 0x08
625 #define OPERATOR 0x10
627 #define B(n, t) { U n, 0, t, 0, BUILTIN, sizeof n - 1 }
628 #define C(n, v) { U n, v, 0, 0, 0, sizeof n - 1 }
629 #define X(n, f) { U n, 0, 0, 0, f, sizeof n - 1 }
630 #define O(n, c, f) { U n, 0, 0, c, OPERATOR | f, sizeof n - 1 }
631 static const struct builtin builtin_array[] =
633 B("__TIME__", BT_TIME),
634 B("__DATE__", BT_DATE),
635 B("__FILE__", BT_FILE),
636 B("__BASE_FILE__", BT_BASE_FILE),
637 B("__LINE__", BT_SPECLINE),
638 B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL),
639 B("_Pragma", BT_PRAGMA),
641 X("__VERSION__", VERS),
642 X("__USER_LABEL_PREFIX__", ULP),
643 C("__REGISTER_PREFIX__", REGISTER_PREFIX),
644 C("__HAVE_BUILTIN_SETJMP__", "1"),
645 #if USING_SJLJ_EXCEPTIONS
646 /* libgcc needs to know this. */
647 C("__USING_SJLJ_EXCEPTIONS__","1"),
648 #endif
649 #ifndef NO_BUILTIN_SIZE_TYPE
650 C("__SIZE_TYPE__", SIZE_TYPE),
651 #endif
652 #ifndef NO_BUILTIN_PTRDIFF_TYPE
653 C("__PTRDIFF_TYPE__", PTRDIFF_TYPE),
654 #endif
655 #ifndef NO_BUILTIN_WCHAR_TYPE
656 C("__WCHAR_TYPE__", WCHAR_TYPE),
657 #endif
658 #ifndef NO_BUILTIN_WINT_TYPE
659 C("__WINT_TYPE__", WINT_TYPE),
660 #endif
661 #ifdef STDC_0_IN_SYSTEM_HEADERS
662 B("__STDC__", BT_STDC),
663 #else
664 C("__STDC__", "1"),
665 #endif
667 /* Named operators known to the preprocessor. These cannot be #defined
668 and always have their stated meaning. They are treated like normal
669 identifiers except for the type code and the meaning. Most of them
670 are only for C++ (but see iso646.h). */
671 O("and", CPP_AND_AND, CPLUS),
672 O("and_eq", CPP_AND_EQ, CPLUS),
673 O("bitand", CPP_AND, CPLUS),
674 O("bitor", CPP_OR, CPLUS),
675 O("compl", CPP_COMPL, CPLUS),
676 O("not", CPP_NOT, CPLUS),
677 O("not_eq", CPP_NOT_EQ, CPLUS),
678 O("or", CPP_OR_OR, CPLUS),
679 O("or_eq", CPP_OR_EQ, CPLUS),
680 O("xor", CPP_XOR, CPLUS),
681 O("xor_eq", CPP_XOR_EQ, CPLUS)
683 #undef B
684 #undef C
685 #undef X
686 #undef O
687 #define builtin_array_end \
688 builtin_array + sizeof(builtin_array)/sizeof(struct builtin)
690 /* Subroutine of cpp_read_main_file; reads the builtins table above and
691 enters them, and language-specific macros, into the hash table. */
692 static void
693 init_builtins (pfile)
694 cpp_reader *pfile;
696 const struct builtin *b;
698 for(b = builtin_array; b < builtin_array_end; b++)
700 if ((b->flags & CPLUS) && ! CPP_OPTION (pfile, cplusplus))
701 continue;
703 if ((b->flags & OPERATOR) && ! CPP_OPTION (pfile, operator_names))
704 continue;
706 if (b->flags & (OPERATOR | BUILTIN))
708 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
709 if (b->flags & OPERATOR)
711 hp->flags |= NODE_OPERATOR;
712 hp->value.operator = b->operator;
714 else
716 hp->type = NT_MACRO;
717 hp->flags |= NODE_BUILTIN | NODE_WARN;
718 hp->value.builtin = b->builtin;
721 else /* A standard macro of some kind. */
723 const char *val;
724 char *str;
726 if (b->flags & VERS)
728 /* Allocate enough space for 'name "value"\n\0'. */
729 str = alloca (b->len + strlen (version_string) + 5);
730 sprintf (str, "%s \"%s\"\n", b->name, version_string);
732 else
734 if (b->flags & ULP)
735 val = CPP_OPTION (pfile, user_label_prefix);
736 else
737 val = b->value;
739 /* Allocate enough space for "name value\n\0". */
740 str = alloca (b->len + strlen (val) + 3);
741 sprintf(str, "%s %s\n", b->name, val);
744 _cpp_define_builtin (pfile, str);
748 if (CPP_OPTION (pfile, cplusplus))
750 _cpp_define_builtin (pfile, "__cplusplus 1");
751 if (SUPPORTS_ONE_ONLY)
752 _cpp_define_builtin (pfile, "__GXX_WEAK__ 1");
753 else
754 _cpp_define_builtin (pfile, "__GXX_WEAK__ 0");
756 if (CPP_OPTION (pfile, objc))
757 _cpp_define_builtin (pfile, "__OBJC__ 1");
759 if (CPP_OPTION (pfile, lang) == CLK_STDC94)
760 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
761 else if (CPP_OPTION (pfile, c99))
762 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
764 if (CPP_OPTION (pfile, lang) == CLK_STDC89
765 || CPP_OPTION (pfile, lang) == CLK_STDC94
766 || CPP_OPTION (pfile, lang) == CLK_STDC99)
767 _cpp_define_builtin (pfile, "__STRICT_ANSI__ 1");
768 else if (CPP_OPTION (pfile, lang) == CLK_ASM)
769 _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
771 #undef BUILTIN
772 #undef OPERATOR
773 #undef VERS
774 #undef ULP
775 #undef CPLUS
776 #undef builtin_array_end
778 /* And another subroutine. This one sets up the standard include path. */
779 static void
780 init_standard_includes (pfile)
781 cpp_reader *pfile;
783 char *path;
784 const struct default_include *p;
785 const char *specd_prefix = CPP_OPTION (pfile, include_prefix);
787 /* Several environment variables may add to the include search path.
788 CPATH specifies an additional list of directories to be searched
789 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
790 etc. specify an additional list of directories to be searched as
791 if specified with -isystem, for the language indicated. */
793 GET_ENV_PATH_LIST (path, "CPATH");
794 if (path != 0 && *path != 0)
795 path_include (pfile, path, BRACKET);
797 switch ((CPP_OPTION (pfile, objc) << 1) + CPP_OPTION (pfile, cplusplus))
799 case 0:
800 GET_ENV_PATH_LIST (path, "C_INCLUDE_PATH");
801 break;
802 case 1:
803 GET_ENV_PATH_LIST (path, "CPLUS_INCLUDE_PATH");
804 break;
805 case 2:
806 GET_ENV_PATH_LIST (path, "OBJC_INCLUDE_PATH");
807 break;
808 case 3:
809 GET_ENV_PATH_LIST (path, "OBJCPLUS_INCLUDE_PATH");
810 break;
812 if (path != 0 && *path != 0)
813 path_include (pfile, path, SYSTEM);
815 /* Search "translated" versions of GNU directories.
816 These have /usr/local/lib/gcc... replaced by specd_prefix. */
817 if (specd_prefix != 0 && cpp_GCC_INCLUDE_DIR_len)
819 /* Remove the `include' from /usr/local/lib/gcc.../include.
820 GCC_INCLUDE_DIR will always end in /include. */
821 int default_len = cpp_GCC_INCLUDE_DIR_len;
822 char *default_prefix = (char *) alloca (default_len + 1);
823 int specd_len = strlen (specd_prefix);
825 memcpy (default_prefix, cpp_GCC_INCLUDE_DIR, default_len);
826 default_prefix[default_len] = '\0';
828 for (p = cpp_include_defaults; p->fname; p++)
830 /* Some standard dirs are only for C++. */
831 if (!p->cplusplus
832 || (CPP_OPTION (pfile, cplusplus)
833 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
835 /* Does this dir start with the prefix? */
836 if (!memcmp (p->fname, default_prefix, default_len))
838 /* Yes; change prefix and add to search list. */
839 int flen = strlen (p->fname);
840 int this_len = specd_len + flen - default_len;
841 char *str = (char *) xmalloc (this_len + 1);
842 memcpy (str, specd_prefix, specd_len);
843 memcpy (str + specd_len,
844 p->fname + default_len,
845 flen - default_len + 1);
847 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
853 /* Search ordinary names for GNU include directories. */
854 for (p = cpp_include_defaults; p->fname; p++)
856 /* Some standard dirs are only for C++. */
857 if (!p->cplusplus
858 || (CPP_OPTION (pfile, cplusplus)
859 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
861 char *str = update_path (p->fname, p->component);
862 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
867 /* Pushes a command line -imacro and -include file indicated by P onto
868 the buffer stack. Returns non-zero if successful. */
869 static bool
870 push_include (pfile, p)
871 cpp_reader *pfile;
872 struct pending_option *p;
874 cpp_token header;
876 /* Later: maybe update this to use the #include "" search path
877 if cpp_read_file fails. */
878 header.type = CPP_STRING;
879 header.val.str.text = (const unsigned char *) p->arg;
880 header.val.str.len = strlen (p->arg);
881 /* Make the command line directive take up a line. */
882 pfile->line++;
884 return _cpp_execute_include (pfile, &header, IT_CMDLINE);
887 /* Frees a pending_option chain. */
888 static void
889 free_chain (head)
890 struct pending_option *head;
892 struct pending_option *next;
894 while (head)
896 next = head->next;
897 free (head);
898 head = next;
902 /* This is called after options have been parsed, and partially
903 processed. Setup for processing input from the file named FNAME,
904 or stdin if it is the empty string. Return the original filename
905 on success (e.g. foo.i->foo.c), or NULL on failure. */
906 const char *
907 cpp_read_main_file (pfile, fname, table)
908 cpp_reader *pfile;
909 const char *fname;
910 hash_table *table;
912 /* The front ends don't set up the hash table until they have
913 finished processing the command line options, so initializing the
914 hashtable is deferred until now. */
915 _cpp_init_hashtable (pfile, table);
917 /* Set up the include search path now. */
918 if (! CPP_OPTION (pfile, no_standard_includes))
919 init_standard_includes (pfile);
921 merge_include_chains (pfile);
923 /* With -v, print the list of dirs to search. */
924 if (CPP_OPTION (pfile, verbose))
926 struct search_path *l;
927 fprintf (stderr, _("#include \"...\" search starts here:\n"));
928 for (l = CPP_OPTION (pfile, quote_include); l; l = l->next)
930 if (l == CPP_OPTION (pfile, bracket_include))
931 fprintf (stderr, _("#include <...> search starts here:\n"));
932 fprintf (stderr, " %s\n", l->name);
934 fprintf (stderr, _("End of search list.\n"));
937 if (CPP_OPTION (pfile, print_deps))
938 /* Set the default target (if there is none already). */
939 deps_add_default_target (pfile->deps, fname);
941 /* Open the main input file. */
942 if (!_cpp_read_file (pfile, fname))
943 return NULL;
945 /* Set this after cpp_post_options so the client can change the
946 option if it wishes, and after stacking the main file so we don't
947 trace the main file. */
948 pfile->line_maps.trace_includes = CPP_OPTION (pfile, print_include_names);
950 /* For foo.i, read the original filename foo.c now, for the benefit
951 of the front ends. */
952 if (CPP_OPTION (pfile, preprocessed))
953 read_original_filename (pfile);
955 return pfile->map->to_file;
958 /* For preprocessed files, if the first tokens are of the form # NUM.
959 handle the directive so we know the original file name. This will
960 generate file_change callbacks, which the front ends must handle
961 appropriately given their state of initialization. */
962 static void
963 read_original_filename (pfile)
964 cpp_reader *pfile;
966 const cpp_token *token, *token1;
968 /* Lex ahead; if the first tokens are of the form # NUM, then
969 process the directive, otherwise back up. */
970 token = _cpp_lex_direct (pfile);
971 if (token->type == CPP_HASH)
973 token1 = _cpp_lex_direct (pfile);
974 _cpp_backup_tokens (pfile, 1);
976 /* If it's a #line directive, handle it. */
977 if (token1->type == CPP_NUMBER)
979 _cpp_handle_directive (pfile, token->flags & PREV_WHITE);
980 return;
984 /* Backup as if nothing happened. */
985 _cpp_backup_tokens (pfile, 1);
988 /* Handle pending command line options: -D, -U, -A, -imacros and
989 -include. This should be called after debugging has been properly
990 set up in the front ends. */
991 void
992 cpp_finish_options (pfile)
993 cpp_reader *pfile;
995 /* Install builtins and process command line macros etc. in the order
996 they appeared, but only if not already preprocessed. */
997 if (! CPP_OPTION (pfile, preprocessed))
999 struct pending_option *p;
1001 _cpp_do_file_change (pfile, LC_RENAME, _("<built-in>"), 1, 0);
1002 init_builtins (pfile);
1003 _cpp_do_file_change (pfile, LC_RENAME, _("<command line>"), 1, 0);
1004 for (p = CPP_OPTION (pfile, pending)->directive_head; p; p = p->next)
1005 (*p->handler) (pfile, p->arg);
1007 /* Scan -imacros files after command line defines, but before
1008 files given with -include. */
1009 while ((p = CPP_OPTION (pfile, pending)->imacros_head) != NULL)
1011 if (push_include (pfile, p))
1013 pfile->buffer->return_at_eof = true;
1014 cpp_scan_nooutput (pfile);
1016 CPP_OPTION (pfile, pending)->imacros_head = p->next;
1017 free (p);
1021 free_chain (CPP_OPTION (pfile, pending)->directive_head);
1022 _cpp_push_next_buffer (pfile);
1025 /* Called to push the next buffer on the stack given by -include. If
1026 there are none, free the pending structure and restore the line map
1027 for the main file. */
1028 bool
1029 _cpp_push_next_buffer (pfile)
1030 cpp_reader *pfile;
1032 bool pushed = false;
1034 /* This is't pretty; we'd rather not be relying on this as a boolean
1035 for reverting the line map. Further, we only free the chains in
1036 this conditional, so an early call to cpp_finish / cpp_destroy
1037 will leak that memory. */
1038 if (CPP_OPTION (pfile, pending)
1039 && CPP_OPTION (pfile, pending)->imacros_head == NULL)
1041 while (!pushed)
1043 struct pending_option *p = CPP_OPTION (pfile, pending)->include_head;
1045 if (p == NULL)
1046 break;
1047 if (! CPP_OPTION (pfile, preprocessed))
1048 pushed = push_include (pfile, p);
1049 CPP_OPTION (pfile, pending)->include_head = p->next;
1050 free (p);
1053 if (!pushed)
1055 free (CPP_OPTION (pfile, pending));
1056 CPP_OPTION (pfile, pending) = NULL;
1058 /* Restore the line map for the main file. */
1059 if (! CPP_OPTION (pfile, preprocessed))
1060 _cpp_do_file_change (pfile, LC_RENAME,
1061 pfile->line_maps.maps[0].to_file, 1, 0);
1065 return pushed;
1068 /* Use mkdeps.c to output dependency information. */
1069 static void
1070 output_deps (pfile)
1071 cpp_reader *pfile;
1073 /* Stream on which to print the dependency information. */
1074 FILE *deps_stream = 0;
1075 const char *const deps_mode =
1076 CPP_OPTION (pfile, print_deps_append) ? "a" : "w";
1078 if (CPP_OPTION (pfile, deps_file) == 0)
1079 deps_stream = stdout;
1080 else
1082 deps_stream = fopen (CPP_OPTION (pfile, deps_file), deps_mode);
1083 if (deps_stream == 0)
1085 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, deps_file));
1086 return;
1090 deps_write (pfile->deps, deps_stream, 72);
1092 if (CPP_OPTION (pfile, deps_phony_targets))
1093 deps_phony_targets (pfile->deps, deps_stream);
1095 /* Don't close stdout. */
1096 if (CPP_OPTION (pfile, deps_file))
1098 if (ferror (deps_stream) || fclose (deps_stream) != 0)
1099 cpp_fatal (pfile, "I/O error on output");
1103 /* This is called at the end of preprocessing. It pops the
1104 last buffer and writes dependency output. It should also
1105 clear macro definitions, such that you could call cpp_start_read
1106 with a new filename to restart processing. */
1107 void
1108 cpp_finish (pfile)
1109 cpp_reader *pfile;
1111 /* cpplex.c leaves the final buffer on the stack. This it so that
1112 it returns an unending stream of CPP_EOFs to the client. If we
1113 popped the buffer, we'd dereference a NULL buffer pointer and
1114 segfault. It's nice to allow the client to do worry-free excess
1115 cpp_get_token calls. */
1116 while (pfile->buffer)
1117 _cpp_pop_buffer (pfile);
1119 /* Don't write the deps file if preprocessing has failed. */
1120 if (CPP_OPTION (pfile, print_deps) && pfile->errors == 0)
1121 output_deps (pfile);
1123 /* Report on headers that could use multiple include guards. */
1124 if (CPP_OPTION (pfile, print_include_names))
1125 _cpp_report_missing_guards (pfile);
1128 /* Add a directive to be handled later in the initialization phase. */
1129 static void
1130 new_pending_directive (pend, text, handler)
1131 struct cpp_pending *pend;
1132 const char *text;
1133 cl_directive_handler handler;
1135 struct pending_option *o = (struct pending_option *)
1136 xmalloc (sizeof (struct pending_option));
1138 o->arg = text;
1139 o->next = NULL;
1140 o->handler = handler;
1141 APPEND (pend, directive, o);
1144 /* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
1145 I.e. a const string initializer with parens around it. That is
1146 what N_("string") resolves to, so we make no_* be macros instead. */
1147 #define no_arg N_("argument missing after %s")
1148 #define no_ass N_("assertion missing after %s")
1149 #define no_dir N_("directory name missing after %s")
1150 #define no_fil N_("file name missing after %s")
1151 #define no_mac N_("macro name missing after %s")
1152 #define no_pth N_("path name missing after %s")
1153 #define no_num N_("number missing after %s")
1154 #define no_tgt N_("target missing after %s")
1156 /* This is the list of all command line options, with the leading
1157 "-" removed. It must be sorted in ASCII collating order. */
1158 #define COMMAND_LINE_OPTIONS \
1159 DEF_OPT("$", 0, OPT_dollar) \
1160 DEF_OPT("+", 0, OPT_plus) \
1161 DEF_OPT("-help", 0, OPT__help) \
1162 DEF_OPT("-target-help", 0, OPT_target__help) \
1163 DEF_OPT("-version", 0, OPT__version) \
1164 DEF_OPT("A", no_ass, OPT_A) \
1165 DEF_OPT("C", 0, OPT_C) \
1166 DEF_OPT("D", no_mac, OPT_D) \
1167 DEF_OPT("H", 0, OPT_H) \
1168 DEF_OPT("I", no_dir, OPT_I) \
1169 DEF_OPT("M", 0, OPT_M) \
1170 DEF_OPT("MD", no_fil, OPT_MD) \
1171 DEF_OPT("MF", no_fil, OPT_MF) \
1172 DEF_OPT("MG", 0, OPT_MG) \
1173 DEF_OPT("MM", 0, OPT_MM) \
1174 DEF_OPT("MMD", no_fil, OPT_MMD) \
1175 DEF_OPT("MP", 0, OPT_MP) \
1176 DEF_OPT("MQ", no_tgt, OPT_MQ) \
1177 DEF_OPT("MT", no_tgt, OPT_MT) \
1178 DEF_OPT("P", 0, OPT_P) \
1179 DEF_OPT("U", no_mac, OPT_U) \
1180 DEF_OPT("W", no_arg, OPT_W) /* arg optional */ \
1181 DEF_OPT("d", no_arg, OPT_d) \
1182 DEF_OPT("fleading-underscore", 0, OPT_fleading_underscore) \
1183 DEF_OPT("fno-leading-underscore", 0, OPT_fno_leading_underscore) \
1184 DEF_OPT("fno-operator-names", 0, OPT_fno_operator_names) \
1185 DEF_OPT("fno-preprocessed", 0, OPT_fno_preprocessed) \
1186 DEF_OPT("fno-show-column", 0, OPT_fno_show_column) \
1187 DEF_OPT("fpreprocessed", 0, OPT_fpreprocessed) \
1188 DEF_OPT("fshow-column", 0, OPT_fshow_column) \
1189 DEF_OPT("ftabstop=", no_num, OPT_ftabstop) \
1190 DEF_OPT("h", 0, OPT_h) \
1191 DEF_OPT("idirafter", no_dir, OPT_idirafter) \
1192 DEF_OPT("imacros", no_fil, OPT_imacros) \
1193 DEF_OPT("include", no_fil, OPT_include) \
1194 DEF_OPT("iprefix", no_pth, OPT_iprefix) \
1195 DEF_OPT("isystem", no_dir, OPT_isystem) \
1196 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix) \
1197 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore) \
1198 DEF_OPT("lang-asm", 0, OPT_lang_asm) \
1199 DEF_OPT("lang-c", 0, OPT_lang_c) \
1200 DEF_OPT("lang-c++", 0, OPT_lang_cplusplus) \
1201 DEF_OPT("lang-c89", 0, OPT_lang_c89) \
1202 DEF_OPT("lang-objc", 0, OPT_lang_objc) \
1203 DEF_OPT("lang-objc++", 0, OPT_lang_objcplusplus) \
1204 DEF_OPT("nostdinc", 0, OPT_nostdinc) \
1205 DEF_OPT("nostdinc++", 0, OPT_nostdincplusplus) \
1206 DEF_OPT("o", no_fil, OPT_o) \
1207 DEF_OPT("pedantic", 0, OPT_pedantic) \
1208 DEF_OPT("pedantic-errors", 0, OPT_pedantic_errors) \
1209 DEF_OPT("remap", 0, OPT_remap) \
1210 DEF_OPT("std=c++98", 0, OPT_std_cplusplus98) \
1211 DEF_OPT("std=c89", 0, OPT_std_c89) \
1212 DEF_OPT("std=c99", 0, OPT_std_c99) \
1213 DEF_OPT("std=c9x", 0, OPT_std_c9x) \
1214 DEF_OPT("std=gnu89", 0, OPT_std_gnu89) \
1215 DEF_OPT("std=gnu99", 0, OPT_std_gnu99) \
1216 DEF_OPT("std=gnu9x", 0, OPT_std_gnu9x) \
1217 DEF_OPT("std=iso9899:1990", 0, OPT_std_iso9899_1990) \
1218 DEF_OPT("std=iso9899:199409", 0, OPT_std_iso9899_199409) \
1219 DEF_OPT("std=iso9899:1999", 0, OPT_std_iso9899_1999) \
1220 DEF_OPT("std=iso9899:199x", 0, OPT_std_iso9899_199x) \
1221 DEF_OPT("trigraphs", 0, OPT_trigraphs) \
1222 DEF_OPT("v", 0, OPT_v) \
1223 DEF_OPT("version", 0, OPT_version) \
1224 DEF_OPT("w", 0, OPT_w)
1226 #define DEF_OPT(text, msg, code) code,
1227 enum opt_code
1229 COMMAND_LINE_OPTIONS
1230 N_OPTS
1232 #undef DEF_OPT
1234 struct cl_option
1236 const char *opt_text;
1237 const char *msg;
1238 size_t opt_len;
1239 enum opt_code opt_code;
1242 #define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
1243 #ifdef HOST_EBCDIC
1244 static struct cl_option cl_options[] =
1245 #else
1246 static const struct cl_option cl_options[] =
1247 #endif
1249 COMMAND_LINE_OPTIONS
1251 #undef DEF_OPT
1252 #undef COMMAND_LINE_OPTIONS
1254 /* Perform a binary search to find which, if any, option the given
1255 command-line matches. Returns its index in the option array,
1256 negative on failure. Complications arise since some options can be
1257 suffixed with an argument, and multiple complete matches can occur,
1258 e.g. -iwithprefix and -iwithprefixbefore. Moreover, we need to
1259 accept options beginning with -W that we do not recognise, but not
1260 to swallow any subsequent command line argument; this is handled as
1261 special cases in cpp_handle_option. */
1262 static int
1263 parse_option (input)
1264 const char *input;
1266 unsigned int md, mn, mx;
1267 size_t opt_len;
1268 int comp;
1270 mn = 0;
1271 mx = N_OPTS;
1273 while (mx > mn)
1275 md = (mn + mx) / 2;
1277 opt_len = cl_options[md].opt_len;
1278 comp = memcmp (input, cl_options[md].opt_text, opt_len);
1280 if (comp > 0)
1281 mn = md + 1;
1282 else if (comp < 0)
1283 mx = md;
1284 else
1286 if (input[opt_len] == '\0')
1287 return md;
1288 /* We were passed more text. If the option takes an argument,
1289 we may match a later option or we may have been passed the
1290 argument. The longest possible option match succeeds.
1291 If the option takes no arguments we have not matched and
1292 continue the search (e.g. input="stdc++" match was "stdc"). */
1293 mn = md + 1;
1294 if (cl_options[md].msg)
1296 /* Scan forwards. If we get an exact match, return it.
1297 Otherwise, return the longest option-accepting match.
1298 This loops no more than twice with current options. */
1299 mx = md;
1300 for (; mn < (unsigned int) N_OPTS; mn++)
1302 opt_len = cl_options[mn].opt_len;
1303 if (memcmp (input, cl_options[mn].opt_text, opt_len))
1304 break;
1305 if (input[opt_len] == '\0')
1306 return mn;
1307 if (cl_options[mn].msg)
1308 mx = mn;
1310 return mx;
1315 return -1;
1318 /* Handle one command-line option in (argc, argv).
1319 Can be called multiple times, to handle multiple sets of options.
1320 Returns number of strings consumed. */
1322 cpp_handle_option (pfile, argc, argv)
1323 cpp_reader *pfile;
1324 int argc;
1325 char **argv;
1327 int i = 0;
1328 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
1330 /* Interpret "-" or a non-option as a file name. */
1331 if (argv[i][0] != '-' || argv[i][1] == '\0')
1333 if (CPP_OPTION (pfile, in_fname) == NULL)
1334 CPP_OPTION (pfile, in_fname) = argv[i];
1335 else if (CPP_OPTION (pfile, out_fname) == NULL)
1336 CPP_OPTION (pfile, out_fname) = argv[i];
1337 else
1338 cpp_fatal (pfile, "too many filenames. Type %s --help for usage info",
1339 progname);
1341 else
1343 enum opt_code opt_code;
1344 int opt_index;
1345 const char *arg = 0;
1347 /* Skip over '-'. */
1348 opt_index = parse_option (&argv[i][1]);
1349 if (opt_index < 0)
1350 return i;
1352 opt_code = cl_options[opt_index].opt_code;
1353 if (cl_options[opt_index].msg)
1355 arg = &argv[i][cl_options[opt_index].opt_len + 1];
1357 /* Yuk. Special case for -W as it must not swallow
1358 up any following argument. If this becomes common, add
1359 another field to the cl_options table. */
1360 if (arg[0] == '\0' && opt_code != OPT_W)
1362 arg = argv[++i];
1363 if (!arg)
1365 cpp_fatal (pfile, cl_options[opt_index].msg, argv[i - 1]);
1366 return argc;
1371 switch (opt_code)
1373 case N_OPTS: /* Shut GCC up. */
1374 break;
1375 case OPT_fleading_underscore:
1376 CPP_OPTION (pfile, user_label_prefix) = "_";
1377 break;
1378 case OPT_fno_leading_underscore:
1379 CPP_OPTION (pfile, user_label_prefix) = "";
1380 break;
1381 case OPT_fno_operator_names:
1382 CPP_OPTION (pfile, operator_names) = 0;
1383 break;
1384 case OPT_fpreprocessed:
1385 CPP_OPTION (pfile, preprocessed) = 1;
1386 break;
1387 case OPT_fno_preprocessed:
1388 CPP_OPTION (pfile, preprocessed) = 0;
1389 break;
1390 case OPT_fshow_column:
1391 CPP_OPTION (pfile, show_column) = 1;
1392 break;
1393 case OPT_fno_show_column:
1394 CPP_OPTION (pfile, show_column) = 0;
1395 break;
1396 case OPT_ftabstop:
1397 /* Silently ignore empty string, non-longs and silly values. */
1398 if (arg[0] != '\0')
1400 char *endptr;
1401 long tabstop = strtol (arg, &endptr, 10);
1402 if (*endptr == '\0' && tabstop >= 1 && tabstop <= 100)
1403 CPP_OPTION (pfile, tabstop) = tabstop;
1405 break;
1406 case OPT_w:
1407 CPP_OPTION (pfile, inhibit_warnings) = 1;
1408 break;
1409 case OPT_h:
1410 case OPT__help:
1411 print_help ();
1412 CPP_OPTION (pfile, help_only) = 1;
1413 break;
1414 case OPT_target__help:
1415 /* Print if any target specific options. cpplib has none, but
1416 make sure help_only gets set. */
1417 CPP_OPTION (pfile, help_only) = 1;
1418 break;
1420 /* --version inhibits compilation, -version doesn't. -v means
1421 verbose and -version. Historical reasons, don't ask. */
1422 case OPT__version:
1423 CPP_OPTION (pfile, help_only) = 1;
1424 pfile->print_version = 1;
1425 break;
1426 case OPT_v:
1427 CPP_OPTION (pfile, verbose) = 1;
1428 pfile->print_version = 1;
1429 break;
1430 case OPT_version:
1431 pfile->print_version = 1;
1432 break;
1434 case OPT_C:
1435 CPP_OPTION (pfile, discard_comments) = 0;
1436 break;
1437 case OPT_P:
1438 CPP_OPTION (pfile, no_line_commands) = 1;
1439 break;
1440 case OPT_dollar: /* Don't include $ in identifiers. */
1441 CPP_OPTION (pfile, dollars_in_ident) = 0;
1442 break;
1443 case OPT_H:
1444 CPP_OPTION (pfile, print_include_names) = 1;
1445 break;
1446 case OPT_D:
1447 new_pending_directive (pend, arg, cpp_define);
1448 break;
1449 case OPT_pedantic_errors:
1450 CPP_OPTION (pfile, pedantic_errors) = 1;
1451 /* fall through */
1452 case OPT_pedantic:
1453 CPP_OPTION (pfile, pedantic) = 1;
1454 break;
1455 case OPT_trigraphs:
1456 CPP_OPTION (pfile, trigraphs) = 1;
1457 break;
1458 case OPT_plus:
1459 CPP_OPTION (pfile, cplusplus) = 1;
1460 CPP_OPTION (pfile, cplusplus_comments) = 1;
1461 break;
1462 case OPT_remap:
1463 CPP_OPTION (pfile, remap) = 1;
1464 break;
1465 case OPT_iprefix:
1466 CPP_OPTION (pfile, include_prefix) = arg;
1467 CPP_OPTION (pfile, include_prefix_len) = strlen (arg);
1468 break;
1469 case OPT_lang_c:
1470 set_lang (pfile, CLK_GNUC89);
1471 break;
1472 case OPT_lang_cplusplus:
1473 set_lang (pfile, CLK_GNUCXX);
1474 break;
1475 case OPT_lang_objc:
1476 set_lang (pfile, CLK_OBJC);
1477 break;
1478 case OPT_lang_objcplusplus:
1479 set_lang (pfile, CLK_OBJCXX);
1480 break;
1481 case OPT_lang_asm:
1482 set_lang (pfile, CLK_ASM);
1483 break;
1484 case OPT_std_cplusplus98:
1485 set_lang (pfile, CLK_CXX98);
1486 break;
1487 case OPT_std_gnu89:
1488 set_lang (pfile, CLK_GNUC89);
1489 break;
1490 case OPT_std_gnu9x:
1491 case OPT_std_gnu99:
1492 set_lang (pfile, CLK_GNUC99);
1493 break;
1494 case OPT_std_iso9899_199409:
1495 set_lang (pfile, CLK_STDC94);
1496 break;
1497 case OPT_std_iso9899_1990:
1498 case OPT_std_c89:
1499 case OPT_lang_c89:
1500 set_lang (pfile, CLK_STDC89);
1501 break;
1502 case OPT_std_iso9899_199x:
1503 case OPT_std_iso9899_1999:
1504 case OPT_std_c9x:
1505 case OPT_std_c99:
1506 set_lang (pfile, CLK_STDC99);
1507 break;
1508 case OPT_nostdinc:
1509 /* -nostdinc causes no default include directories.
1510 You must specify all include-file directories with -I. */
1511 CPP_OPTION (pfile, no_standard_includes) = 1;
1512 break;
1513 case OPT_nostdincplusplus:
1514 /* -nostdinc++ causes no default C++-specific include directories. */
1515 CPP_OPTION (pfile, no_standard_cplusplus_includes) = 1;
1516 break;
1517 case OPT_o:
1518 if (CPP_OPTION (pfile, out_fname) == NULL)
1519 CPP_OPTION (pfile, out_fname) = arg;
1520 else
1522 cpp_fatal (pfile, "output filename specified twice");
1523 return argc;
1525 break;
1526 case OPT_d:
1527 /* Args to -d specify what parts of macros to dump.
1528 Silently ignore unrecognised options; they may
1529 be aimed at the compiler proper. */
1531 char c;
1533 while ((c = *arg++) != '\0')
1534 switch (c)
1536 case 'M':
1537 CPP_OPTION (pfile, dump_macros) = dump_only;
1538 CPP_OPTION (pfile, no_output) = 1;
1539 break;
1540 case 'N':
1541 CPP_OPTION (pfile, dump_macros) = dump_names;
1542 break;
1543 case 'D':
1544 CPP_OPTION (pfile, dump_macros) = dump_definitions;
1545 break;
1546 case 'I':
1547 CPP_OPTION (pfile, dump_includes) = 1;
1548 break;
1551 break;
1553 case OPT_MG:
1554 CPP_OPTION (pfile, print_deps_missing_files) = 1;
1555 break;
1556 case OPT_M:
1557 CPP_OPTION (pfile, print_deps) = 2;
1558 break;
1559 case OPT_MM:
1560 CPP_OPTION (pfile, print_deps) = 1;
1561 break;
1562 case OPT_MF:
1563 CPP_OPTION (pfile, deps_file) = arg;
1564 break;
1565 case OPT_MP:
1566 CPP_OPTION (pfile, deps_phony_targets) = 1;
1567 break;
1568 case OPT_MQ:
1569 case OPT_MT:
1570 /* Add a target. -MQ quotes for Make. */
1571 deps_add_target (pfile->deps, arg, opt_code == OPT_MQ);
1572 break;
1574 /* -MD and -MMD for cpp0 are deprecated and undocumented
1575 (use -M or -MM with -MF instead), and probably should be
1576 removed with the next major GCC version. For the moment
1577 we allow these for the benefit of Automake 1.4, which
1578 uses these when dependency tracking is enabled. Automake
1579 1.5 will fix this. */
1580 case OPT_MD:
1581 CPP_OPTION (pfile, print_deps) = 2;
1582 CPP_OPTION (pfile, deps_file) = arg;
1583 break;
1584 case OPT_MMD:
1585 CPP_OPTION (pfile, print_deps) = 1;
1586 CPP_OPTION (pfile, deps_file) = arg;
1587 break;
1589 case OPT_A:
1590 if (arg[0] == '-')
1592 /* -A with an argument beginning with '-' acts as
1593 #unassert on whatever immediately follows the '-'.
1594 If "-" is the whole argument, we eliminate all
1595 predefined macros and assertions, including those
1596 that were specified earlier on the command line.
1597 That way we can get rid of any that were passed
1598 automatically in from GCC. */
1600 if (arg[1] == '\0')
1602 free_chain (pend->directive_head);
1603 pend->directive_head = NULL;
1604 pend->directive_tail = NULL;
1606 else
1607 new_pending_directive (pend, arg + 1, cpp_unassert);
1609 else
1610 new_pending_directive (pend, arg, cpp_assert);
1611 break;
1612 case OPT_U:
1613 new_pending_directive (pend, arg, cpp_undef);
1614 break;
1615 case OPT_I: /* Add directory to path for includes. */
1616 if (!strcmp (arg, "-"))
1618 /* -I- means:
1619 Use the preceding -I directories for #include "..."
1620 but not #include <...>.
1621 Don't search the directory of the present file
1622 for #include "...". (Note that -I. -I- is not the same as
1623 the default setup; -I. uses the compiler's working dir.) */
1624 if (! CPP_OPTION (pfile, ignore_srcdir))
1626 pend->quote_head = pend->brack_head;
1627 pend->quote_tail = pend->brack_tail;
1628 pend->brack_head = 0;
1629 pend->brack_tail = 0;
1630 CPP_OPTION (pfile, ignore_srcdir) = 1;
1632 else
1634 cpp_fatal (pfile, "-I- specified twice");
1635 return argc;
1638 else
1639 append_include_chain (pfile, xstrdup (arg), BRACKET, 0);
1640 break;
1641 case OPT_isystem:
1642 /* Add directory to beginning of system include path, as a system
1643 include directory. */
1644 append_include_chain (pfile, xstrdup (arg), SYSTEM, 0);
1645 break;
1646 case OPT_include:
1647 case OPT_imacros:
1649 struct pending_option *o = (struct pending_option *)
1650 xmalloc (sizeof (struct pending_option));
1651 o->arg = arg;
1652 o->next = NULL;
1654 if (opt_code == OPT_include)
1655 APPEND (pend, include, o);
1656 else
1657 APPEND (pend, imacros, o);
1659 break;
1660 case OPT_iwithprefix:
1661 /* Add directory to end of path for includes,
1662 with the default prefix at the front of its name. */
1663 /* fall through */
1664 case OPT_iwithprefixbefore:
1665 /* Add directory to main path for includes,
1666 with the default prefix at the front of its name. */
1668 char *fname;
1669 int len;
1671 len = strlen (arg);
1673 if (CPP_OPTION (pfile, include_prefix) != 0)
1675 size_t ipl = CPP_OPTION (pfile, include_prefix_len);
1676 fname = xmalloc (ipl + len + 1);
1677 memcpy (fname, CPP_OPTION (pfile, include_prefix), ipl);
1678 memcpy (fname + ipl, arg, len + 1);
1680 else if (cpp_GCC_INCLUDE_DIR_len)
1682 fname = xmalloc (cpp_GCC_INCLUDE_DIR_len + len + 1);
1683 memcpy (fname, cpp_GCC_INCLUDE_DIR, cpp_GCC_INCLUDE_DIR_len);
1684 memcpy (fname + cpp_GCC_INCLUDE_DIR_len, arg, len + 1);
1686 else
1687 fname = xstrdup (arg);
1689 append_include_chain (pfile, fname,
1690 opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0);
1692 break;
1693 case OPT_idirafter:
1694 /* Add directory to end of path for includes. */
1695 append_include_chain (pfile, xstrdup (arg), AFTER, 0);
1696 break;
1697 case OPT_W:
1698 /* Silently ignore unrecognised options. */
1699 if (!strcmp (argv[i], "-Wall"))
1701 CPP_OPTION (pfile, warn_trigraphs) = 1;
1702 CPP_OPTION (pfile, warn_comments) = 1;
1704 else if (!strcmp (argv[i], "-Wtraditional"))
1705 CPP_OPTION (pfile, warn_traditional) = 1;
1706 else if (!strcmp (argv[i], "-Wtrigraphs"))
1707 CPP_OPTION (pfile, warn_trigraphs) = 1;
1708 else if (!strcmp (argv[i], "-Wcomment"))
1709 CPP_OPTION (pfile, warn_comments) = 1;
1710 else if (!strcmp (argv[i], "-Wcomments"))
1711 CPP_OPTION (pfile, warn_comments) = 1;
1712 else if (!strcmp (argv[i], "-Wundef"))
1713 CPP_OPTION (pfile, warn_undef) = 1;
1714 else if (!strcmp (argv[i], "-Wimport"))
1715 CPP_OPTION (pfile, warn_import) = 1;
1716 else if (!strcmp (argv[i], "-Werror"))
1717 CPP_OPTION (pfile, warnings_are_errors) = 1;
1718 else if (!strcmp (argv[i], "-Wsystem-headers"))
1719 CPP_OPTION (pfile, warn_system_headers) = 1;
1720 else if (!strcmp (argv[i], "-Wno-traditional"))
1721 CPP_OPTION (pfile, warn_traditional) = 0;
1722 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1723 CPP_OPTION (pfile, warn_trigraphs) = 0;
1724 else if (!strcmp (argv[i], "-Wno-comment"))
1725 CPP_OPTION (pfile, warn_comments) = 0;
1726 else if (!strcmp (argv[i], "-Wno-comments"))
1727 CPP_OPTION (pfile, warn_comments) = 0;
1728 else if (!strcmp (argv[i], "-Wno-undef"))
1729 CPP_OPTION (pfile, warn_undef) = 0;
1730 else if (!strcmp (argv[i], "-Wno-import"))
1731 CPP_OPTION (pfile, warn_import) = 0;
1732 else if (!strcmp (argv[i], "-Wno-error"))
1733 CPP_OPTION (pfile, warnings_are_errors) = 0;
1734 else if (!strcmp (argv[i], "-Wno-system-headers"))
1735 CPP_OPTION (pfile, warn_system_headers) = 0;
1736 break;
1739 return i + 1;
1742 /* Handle command-line options in (argc, argv).
1743 Can be called multiple times, to handle multiple sets of options.
1744 Returns if an unrecognized option is seen.
1745 Returns number of strings consumed. */
1747 cpp_handle_options (pfile, argc, argv)
1748 cpp_reader *pfile;
1749 int argc;
1750 char **argv;
1752 int i;
1753 int strings_processed;
1755 for (i = 0; i < argc; i += strings_processed)
1757 strings_processed = cpp_handle_option (pfile, argc - i, argv + i);
1758 if (strings_processed == 0)
1759 break;
1762 return i;
1765 /* Extra processing when all options are parsed, after all calls to
1766 cpp_handle_option[s]. Consistency checks etc. */
1767 void
1768 cpp_post_options (pfile)
1769 cpp_reader *pfile;
1771 if (pfile->print_version)
1773 fprintf (stderr, _("GNU CPP version %s (cpplib)"), version_string);
1774 #ifdef TARGET_VERSION
1775 TARGET_VERSION;
1776 #endif
1777 fputc ('\n', stderr);
1780 /* Canonicalize in_fname and out_fname. We guarantee they are not
1781 NULL, and that the empty string represents stdin / stdout. */
1782 if (CPP_OPTION (pfile, in_fname) == NULL
1783 || !strcmp (CPP_OPTION (pfile, in_fname), "-"))
1784 CPP_OPTION (pfile, in_fname) = "";
1786 if (CPP_OPTION (pfile, out_fname) == NULL
1787 || !strcmp (CPP_OPTION (pfile, out_fname), "-"))
1788 CPP_OPTION (pfile, out_fname) = "";
1790 /* -Wtraditional is not useful in C++ mode. */
1791 if (CPP_OPTION (pfile, cplusplus))
1792 CPP_OPTION (pfile, warn_traditional) = 0;
1794 /* Set this if it hasn't been set already. */
1795 if (CPP_OPTION (pfile, user_label_prefix) == NULL)
1796 CPP_OPTION (pfile, user_label_prefix) = USER_LABEL_PREFIX;
1798 /* Permanently disable macro expansion if we are rescanning
1799 preprocessed text. */
1800 if (CPP_OPTION (pfile, preprocessed))
1801 pfile->state.prevent_expansion = 1;
1803 /* We need to do this after option processing and before
1804 cpp_start_read, as cppmain.c relies on the options->no_output to
1805 set its callbacks correctly before calling cpp_start_read. */
1806 init_dependency_output (pfile);
1808 /* After checking the environment variables, check if -M or -MM has
1809 not been specified, but other -M options have. */
1810 if (CPP_OPTION (pfile, print_deps) == 0 &&
1811 (CPP_OPTION (pfile, print_deps_missing_files)
1812 || CPP_OPTION (pfile, deps_file)
1813 || CPP_OPTION (pfile, deps_phony_targets)))
1814 cpp_fatal (pfile, "you must additionally specify either -M or -MM");
1817 /* Set up dependency-file output. */
1818 static void
1819 init_dependency_output (pfile)
1820 cpp_reader *pfile;
1822 char *spec, *s, *output_file;
1824 /* Either of two environment variables can specify output of deps.
1825 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1826 where OUTPUT_FILE is the file to write deps info to
1827 and DEPS_TARGET is the target to mention in the deps. */
1829 if (CPP_OPTION (pfile, print_deps) == 0)
1831 spec = getenv ("DEPENDENCIES_OUTPUT");
1832 if (spec)
1833 CPP_OPTION (pfile, print_deps) = 1;
1834 else
1836 spec = getenv ("SUNPRO_DEPENDENCIES");
1837 if (spec)
1838 CPP_OPTION (pfile, print_deps) = 2;
1839 else
1840 return;
1843 /* Find the space before the DEPS_TARGET, if there is one. */
1844 s = strchr (spec, ' ');
1845 if (s)
1847 /* Let the caller perform MAKE quoting. */
1848 deps_add_target (pfile->deps, s + 1, 0);
1849 output_file = (char *) xmalloc (s - spec + 1);
1850 memcpy (output_file, spec, s - spec);
1851 output_file[s - spec] = 0;
1853 else
1854 output_file = spec;
1856 /* Command line overrides environment variables. */
1857 if (CPP_OPTION (pfile, deps_file) == 0)
1858 CPP_OPTION (pfile, deps_file) = output_file;
1859 CPP_OPTION (pfile, print_deps_append) = 1;
1862 /* If dependencies go to standard output, or -MG is used, we should
1863 suppress output, including -dM, -dI etc. */
1864 if (CPP_OPTION (pfile, deps_file) == 0
1865 || CPP_OPTION (pfile, print_deps_missing_files))
1867 CPP_OPTION (pfile, no_output) = 1;
1868 CPP_OPTION (pfile, dump_macros) = 0;
1869 CPP_OPTION (pfile, dump_includes) = 0;
1873 /* Handle --help output. */
1874 static void
1875 print_help ()
1877 fprintf (stderr, _("Usage: %s [switches] input output\n"), progname);
1878 /* To keep the lines from getting too long for some compilers, limit
1879 to about 500 characters (6 lines) per chunk. */
1880 fputs (_("\
1881 Switches:\n\
1882 -include <file> Include the contents of <file> before other files\n\
1883 -imacros <file> Accept definition of macros in <file>\n\
1884 -iprefix <path> Specify <path> as a prefix for next two options\n\
1885 -iwithprefix <dir> Add <dir> to the end of the system include path\n\
1886 -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\
1887 -isystem <dir> Add <dir> to the start of the system include path\n\
1888 "), stdout);
1889 fputs (_("\
1890 -idirafter <dir> Add <dir> to the end of the system include path\n\
1891 -I <dir> Add <dir> to the end of the main include path\n\
1892 -I- Fine-grained include path control; see info docs\n\
1893 -nostdinc Do not search system include directories\n\
1894 (dirs specified with -isystem will still be used)\n\
1895 -nostdinc++ Do not search system include directories for C++\n\
1896 -o <file> Put output into <file>\n\
1897 "), stdout);
1898 fputs (_("\
1899 -pedantic Issue all warnings demanded by strict ISO C\n\
1900 -pedantic-errors Issue -pedantic warnings as errors instead\n\
1901 -trigraphs Support ISO C trigraphs\n\
1902 -lang-c Assume that the input sources are in C\n\
1903 -lang-c89 Assume that the input sources are in C89\n\
1904 "), stdout);
1905 fputs (_("\
1906 -lang-c++ Assume that the input sources are in C++\n\
1907 -lang-objc Assume that the input sources are in ObjectiveC\n\
1908 -lang-objc++ Assume that the input sources are in ObjectiveC++\n\
1909 -lang-asm Assume that the input sources are in assembler\n\
1910 "), stdout);
1911 fputs (_("\
1912 -std=<std name> Specify the conformance standard; one of:\n\
1913 gnu89, gnu99, c89, c99, iso9899:1990,\n\
1914 iso9899:199409, iso9899:1999\n\
1915 -+ Allow parsing of C++ style features\n\
1916 -w Inhibit warning messages\n\
1917 -Wtrigraphs Warn if trigraphs are encountered\n\
1918 -Wno-trigraphs Do not warn about trigraphs\n\
1919 -Wcomment{s} Warn if one comment starts inside another\n\
1920 "), stdout);
1921 fputs (_("\
1922 -Wno-comment{s} Do not warn about comments\n\
1923 -Wtraditional Warn about features not present in traditional C\n\
1924 -Wno-traditional Do not warn about traditional C\n\
1925 -Wundef Warn if an undefined macro is used by #if\n\
1926 -Wno-undef Do not warn about testing undefined macros\n\
1927 -Wimport Warn about the use of the #import directive\n\
1928 "), stdout);
1929 fputs (_("\
1930 -Wno-import Do not warn about the use of #import\n\
1931 -Werror Treat all warnings as errors\n\
1932 -Wno-error Do not treat warnings as errors\n\
1933 -Wsystem-headers Do not suppress warnings from system headers\n\
1934 -Wno-system-headers Suppress warnings from system headers\n\
1935 -Wall Enable all preprocessor warnings\n\
1936 "), stdout);
1937 fputs (_("\
1938 -M Generate make dependencies\n\
1939 -MM As -M, but ignore system header files\n\
1940 -MF <file> Write dependency output to the given file\n\
1941 -MG Treat missing header file as generated files\n\
1942 "), stdout);
1943 fputs (_("\
1944 -MP Generate phony targets for all headers\n\
1945 -MQ <target> Add a MAKE-quoted target\n\
1946 -MT <target> Add an unquoted target\n\
1947 "), stdout);
1948 fputs (_("\
1949 -D<macro> Define a <macro> with string '1' as its value\n\
1950 -D<macro>=<val> Define a <macro> with <val> as its value\n\
1951 -A<question> (<answer>) Assert the <answer> to <question>\n\
1952 -A-<question> (<answer>) Disable the <answer> to <question>\n\
1953 -U<macro> Undefine <macro> \n\
1954 -v Display the version number\n\
1955 "), stdout);
1956 fputs (_("\
1957 -H Print the name of header files as they are used\n\
1958 -C Do not discard comments\n\
1959 -dM Display a list of macro definitions active at end\n\
1960 -dD Preserve macro definitions in output\n\
1961 -dN As -dD except that only the names are preserved\n\
1962 -dI Include #include directives in the output\n\
1963 "), stdout);
1964 fputs (_("\
1965 -fpreprocessed Treat the input file as already preprocessed\n\
1966 -ftabstop=<number> Distance between tab stops for column reporting\n\
1967 -P Do not generate #line directives\n\
1968 -$ Do not allow '$' in identifiers\n\
1969 -remap Remap file names when including files.\n\
1970 --version Display version information\n\
1971 -h or --help Display this information\n\
1972 "), stdout);