cfganal.c (set_edge_can_fallthru_flag): Clear the EDGE_CAN_FALLTHRU flag before setti...
[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, 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
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 "coretypes.h"
25 #include "tm.h"
26 #include "cpplib.h"
27 #include "cpphash.h"
28 #include "prefix.h"
29 #include "intl.h"
30 #include "mkdeps.h"
31 #include "cppdefault.h"
32
33 /* Windows does not natively support inodes, and neither does MSDOS.
34 Cygwin's emulation can generate non-unique inodes, so don't use it.
35 VMS has non-numeric inodes. */
36 #ifdef VMS
37 # define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
38 # define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
39 #else
40 # if (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
41 # define INO_T_EQ(A, B) 0
42 # else
43 # define INO_T_EQ(A, B) ((A) == (B))
44 # endif
45 # define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
46 #endif
47
48 /* Internal structures and prototypes. */
49
50 /* A `struct pending_option' remembers one -D, -A, -U, -include, or
51 -imacros switch. */
52 typedef void (* cl_directive_handler) PARAMS ((cpp_reader *, const char *));
53 struct pending_option
54 {
55 struct pending_option *next;
56 const char *arg;
57 cl_directive_handler handler;
58 };
59
60 /* The `pending' structure accumulates all the options that are not
61 actually processed until we hit cpp_read_main_file. It consists of
62 several lists, one for each type of option. We keep both head and
63 tail pointers for quick insertion. */
64 struct cpp_pending
65 {
66 struct pending_option *directive_head, *directive_tail;
67
68 struct search_path *quote_head, *quote_tail;
69 struct search_path *brack_head, *brack_tail;
70 struct search_path *systm_head, *systm_tail;
71 struct search_path *after_head, *after_tail;
72
73 struct pending_option *imacros_head, *imacros_tail;
74 struct pending_option *include_head, *include_tail;
75 };
76
77 #ifdef __STDC__
78 #define APPEND(pend, list, elt) \
79 do { if (!(pend)->list##_head) (pend)->list##_head = (elt); \
80 else (pend)->list##_tail->next = (elt); \
81 (pend)->list##_tail = (elt); \
82 } while (0)
83 #else
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 #endif
90
91 static void path_include PARAMS ((cpp_reader *,
92 const char *, int));
93 static void init_library PARAMS ((void));
94 static void init_builtins PARAMS ((cpp_reader *));
95 static void mark_named_operators PARAMS ((cpp_reader *));
96 static void append_include_chain PARAMS ((cpp_reader *,
97 char *, int, int));
98 static struct search_path * remove_dup_dir PARAMS ((cpp_reader *,
99 struct search_path *,
100 struct search_path **));
101 static struct search_path * remove_dup_nonsys_dirs PARAMS ((cpp_reader *,
102 struct search_path **,
103 struct search_path *));
104 static struct search_path * remove_dup_dirs PARAMS ((cpp_reader *,
105 struct search_path **));
106 static void merge_include_chains PARAMS ((cpp_reader *));
107 static bool push_include PARAMS ((cpp_reader *,
108 struct pending_option *));
109 static void free_chain PARAMS ((struct pending_option *));
110 static void init_standard_includes PARAMS ((cpp_reader *));
111 static void read_original_filename PARAMS ((cpp_reader *));
112 static void new_pending_directive PARAMS ((struct cpp_pending *,
113 const char *,
114 cl_directive_handler));
115 static int parse_option PARAMS ((const char *));
116 static void post_options PARAMS ((cpp_reader *));
117
118 /* Fourth argument to append_include_chain: chain to use.
119 Note it's never asked to append to the quote chain. */
120 enum { BRACKET = 0, SYSTEM, AFTER };
121
122 /* If we have designated initializers (GCC >2.7) these tables can be
123 initialized, constant data. Otherwise, they have to be filled in at
124 runtime. */
125 #if HAVE_DESIGNATED_INITIALIZERS
126
127 #define init_trigraph_map() /* Nothing. */
128 #define TRIGRAPH_MAP \
129 __extension__ const uchar _cpp_trigraph_map[UCHAR_MAX + 1] = {
130
131 #define END };
132 #define s(p, v) [p] = v,
133
134 #else
135
136 #define TRIGRAPH_MAP uchar _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
137 static void init_trigraph_map PARAMS ((void)) { \
138 unsigned char *x = _cpp_trigraph_map;
139
140 #define END }
141 #define s(p, v) x[p] = v;
142
143 #endif
144
145 TRIGRAPH_MAP
146 s('=', '#') s(')', ']') s('!', '|')
147 s('(', '[') s('\'', '^') s('>', '}')
148 s('/', '\\') s('<', '{') s('-', '~')
149 END
150
151 #undef s
152 #undef END
153 #undef TRIGRAPH_MAP
154
155 /* Read ENV_VAR for a colon-separated list of file names; and
156 add all the names to the search path for include files. */
157 static void
158 path_include (pfile, env_var, path)
159 cpp_reader *pfile;
160 const char *env_var;
161 int path;
162 {
163 char *p, *q, *name;
164
165 GET_ENVIRONMENT (q, env_var);
166 if (!q)
167 return;
168
169 for (p = q; *q; p = q + 1)
170 {
171 /* Find the end of this name. */
172 q = p;
173 while (*q != 0 && *q != PATH_SEPARATOR) q++;
174 if (q == p)
175 {
176 /* An empty name in the path stands for the current directory. */
177 name = (char *) xmalloc (2);
178 name[0] = '.';
179 name[1] = 0;
180 }
181 else
182 {
183 /* Otherwise use the directory that is named. */
184 name = (char *) xmalloc (q - p + 1);
185 memcpy (name, p, q - p);
186 name[q - p] = 0;
187 }
188
189 append_include_chain (pfile, name, path, path == SYSTEM);
190 }
191 }
192
193 /* Append DIR to include path PATH. DIR must be allocated on the
194 heap; this routine takes responsibility for freeing it. CXX_AWARE
195 is nonzero if the header contains extern "C" guards for C++,
196 otherwise it is zero. */
197 static void
198 append_include_chain (pfile, dir, path, cxx_aware)
199 cpp_reader *pfile;
200 char *dir;
201 int path;
202 int cxx_aware;
203 {
204 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
205 struct search_path *new;
206 struct stat st;
207 unsigned int len;
208
209 if (*dir == '\0')
210 {
211 free (dir);
212 dir = xstrdup (".");
213 }
214 _cpp_simplify_pathname (dir);
215
216 if (stat (dir, &st))
217 {
218 /* Dirs that don't exist are silently ignored. */
219 if (errno != ENOENT)
220 cpp_errno (pfile, DL_ERROR, dir);
221 else if (CPP_OPTION (pfile, verbose))
222 fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"), dir);
223 free (dir);
224 return;
225 }
226
227 if (!S_ISDIR (st.st_mode))
228 {
229 cpp_error_with_line (pfile, DL_ERROR, 0, 0, "%s: Not a directory", dir);
230 free (dir);
231 return;
232 }
233
234 len = strlen (dir);
235 if (len > pfile->max_include_len)
236 pfile->max_include_len = len;
237
238 new = (struct search_path *) xmalloc (sizeof (struct search_path));
239 new->name = dir;
240 new->len = len;
241 INO_T_COPY (new->ino, st.st_ino);
242 new->dev = st.st_dev;
243 /* Both systm and after include file lists should be treated as system
244 include files since these two lists are really just a concatenation
245 of one "system" list. */
246 if (path == SYSTEM || path == AFTER)
247 new->sysp = cxx_aware ? 1 : 2;
248 else
249 new->sysp = 0;
250 new->name_map = NULL;
251 new->next = NULL;
252
253 switch (path)
254 {
255 case BRACKET: APPEND (pend, brack, new); break;
256 case SYSTEM: APPEND (pend, systm, new); break;
257 case AFTER: APPEND (pend, after, new); break;
258 }
259 }
260
261 /* Handle a duplicated include path. PREV is the link in the chain
262 before the duplicate, or NULL if the duplicate is at the head of
263 the chain. The duplicate is removed from the chain and freed.
264 Returns PREV. */
265 static struct search_path *
266 remove_dup_dir (pfile, prev, head_ptr)
267 cpp_reader *pfile;
268 struct search_path *prev;
269 struct search_path **head_ptr;
270 {
271 struct search_path *cur;
272
273 if (prev != NULL)
274 {
275 cur = prev->next;
276 prev->next = cur->next;
277 }
278 else
279 {
280 cur = *head_ptr;
281 *head_ptr = cur->next;
282 }
283
284 if (CPP_OPTION (pfile, verbose))
285 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), cur->name);
286
287 free ((PTR) cur->name);
288 free (cur);
289
290 return prev;
291 }
292
293 /* Remove duplicate non-system directories for which there is an equivalent
294 system directory latter in the chain. The range for removal is between
295 *HEAD_PTR and END. Returns the directory before END, or NULL if none.
296 This algorithm is quadratic in the number system directories, which is
297 acceptable since there aren't usually that many of them. */
298 static struct search_path *
299 remove_dup_nonsys_dirs (pfile, head_ptr, end)
300 cpp_reader *pfile;
301 struct search_path **head_ptr;
302 struct search_path *end;
303 {
304 int sysdir = 0;
305 struct search_path *prev = NULL, *cur, *other;
306
307 for (cur = *head_ptr; cur; cur = cur->next)
308 {
309 if (cur->sysp)
310 {
311 sysdir = 1;
312 for (other = *head_ptr, prev = NULL;
313 other != end;
314 other = other ? other->next : *head_ptr)
315 {
316 if (!other->sysp
317 && INO_T_EQ (cur->ino, other->ino)
318 && cur->dev == other->dev)
319 {
320 other = remove_dup_dir (pfile, prev, head_ptr);
321 if (CPP_OPTION (pfile, verbose))
322 fprintf (stderr,
323 _(" as it is a non-system directory that duplicates a system directory\n"));
324 }
325 prev = other;
326 }
327 }
328 }
329
330 if (!sysdir)
331 for (cur = *head_ptr; cur != end; cur = cur->next)
332 prev = cur;
333
334 return prev;
335 }
336
337 /* Remove duplicate directories from a chain. Returns the tail of the
338 chain, or NULL if the chain is empty. This algorithm is quadratic
339 in the number of -I switches, which is acceptable since there
340 aren't usually that many of them. */
341 static struct search_path *
342 remove_dup_dirs (pfile, head_ptr)
343 cpp_reader *pfile;
344 struct search_path **head_ptr;
345 {
346 struct search_path *prev = NULL, *cur, *other;
347
348 for (cur = *head_ptr; cur; cur = cur->next)
349 {
350 for (other = *head_ptr; other != cur; other = other->next)
351 if (INO_T_EQ (cur->ino, other->ino) && cur->dev == other->dev)
352 {
353 cur = remove_dup_dir (pfile, prev, head_ptr);
354 break;
355 }
356 prev = cur;
357 }
358
359 return prev;
360 }
361
362 /* Merge the four include chains together in the order quote, bracket,
363 system, after. Remove duplicate dirs (as determined by
364 INO_T_EQ()). The system_include and after_include chains are never
365 referred to again after this function; all access is through the
366 bracket_include path. */
367 static void
368 merge_include_chains (pfile)
369 cpp_reader *pfile;
370 {
371 struct search_path *quote, *brack, *systm, *qtail;
372
373 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
374
375 quote = pend->quote_head;
376 brack = pend->brack_head;
377 systm = pend->systm_head;
378 qtail = pend->quote_tail;
379
380 /* Paste together bracket, system, and after include chains. */
381 if (systm)
382 pend->systm_tail->next = pend->after_head;
383 else
384 systm = pend->after_head;
385
386 if (brack)
387 pend->brack_tail->next = systm;
388 else
389 brack = systm;
390
391 /* This is a bit tricky. First we drop non-system dupes of system
392 directories from the merged bracket-include list. Next we drop
393 dupes from the bracket and quote include lists. Then we drop
394 non-system dupes from the merged quote-include list. Finally,
395 if qtail and brack are the same directory, we cut out brack and
396 move brack up to point to qtail.
397
398 We can't just merge the lists and then uniquify them because
399 then we may lose directories from the <> search path that should
400 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
401 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
402 -Ibar -I- -Ifoo -Iquux. */
403
404 remove_dup_nonsys_dirs (pfile, &brack, systm);
405 remove_dup_dirs (pfile, &brack);
406
407 if (quote)
408 {
409 qtail = remove_dup_dirs (pfile, &quote);
410 qtail->next = brack;
411
412 qtail = remove_dup_nonsys_dirs (pfile, &quote, brack);
413
414 /* If brack == qtail, remove brack as it's simpler. */
415 if (qtail && brack && INO_T_EQ (qtail->ino, brack->ino)
416 && qtail->dev == brack->dev)
417 brack = remove_dup_dir (pfile, qtail, &quote);
418 }
419 else
420 quote = brack;
421
422 CPP_OPTION (pfile, quote_include) = quote;
423 CPP_OPTION (pfile, bracket_include) = brack;
424 }
425
426 /* A set of booleans indicating what CPP features each source language
427 requires. */
428 struct lang_flags
429 {
430 char c99;
431 char cplusplus;
432 char extended_numbers;
433 char std;
434 char dollars_in_ident;
435 char cplusplus_comments;
436 char digraphs;
437 };
438
439 /* ??? Enable $ in identifiers in assembly? */
440 static const struct lang_flags lang_defaults[] =
441 { /* c99 c++ xnum std dollar c++comm digr */
442 /* GNUC89 */ { 0, 0, 1, 0, 1, 1, 1 },
443 /* GNUC99 */ { 1, 0, 1, 0, 1, 1, 1 },
444 /* STDC89 */ { 0, 0, 0, 1, 0, 0, 0 },
445 /* STDC94 */ { 0, 0, 0, 1, 0, 0, 1 },
446 /* STDC99 */ { 1, 0, 1, 1, 0, 1, 1 },
447 /* GNUCXX */ { 0, 1, 1, 0, 1, 1, 1 },
448 /* CXX98 */ { 0, 1, 1, 1, 0, 1, 1 },
449 /* ASM */ { 0, 0, 1, 0, 0, 1, 0 }
450 };
451
452 /* Sets internal flags correctly for a given language. */
453 void
454 cpp_set_lang (pfile, lang)
455 cpp_reader *pfile;
456 enum c_lang lang;
457 {
458 const struct lang_flags *l = &lang_defaults[(int) lang];
459
460 CPP_OPTION (pfile, lang) = lang;
461
462 CPP_OPTION (pfile, c99) = l->c99;
463 CPP_OPTION (pfile, cplusplus) = l->cplusplus;
464 CPP_OPTION (pfile, extended_numbers) = l->extended_numbers;
465 CPP_OPTION (pfile, std) = l->std;
466 CPP_OPTION (pfile, trigraphs) = l->std;
467 CPP_OPTION (pfile, dollars_in_ident) = l->dollars_in_ident;
468 CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments;
469 CPP_OPTION (pfile, digraphs) = l->digraphs;
470 }
471
472 #ifdef HOST_EBCDIC
473 static int opt_comp PARAMS ((const void *, const void *));
474
475 /* Run-time sorting of options array. */
476 static int
477 opt_comp (p1, p2)
478 const void *p1, *p2;
479 {
480 return strcmp (((struct cl_option *) p1)->opt_text,
481 ((struct cl_option *) p2)->opt_text);
482 }
483 #endif
484
485 /* init initializes library global state. It might not need to
486 do anything depending on the platform and compiler. */
487 static void
488 init_library ()
489 {
490 static int initialized = 0;
491
492 if (! initialized)
493 {
494 initialized = 1;
495
496 #ifdef HOST_EBCDIC
497 /* For non-ASCII hosts, the cl_options array needs to be sorted at
498 runtime. */
499 qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
500 #endif
501
502 /* Set up the trigraph map. This doesn't need to do anything if
503 we were compiled with a compiler that supports C99 designated
504 initializers. */
505 init_trigraph_map ();
506 }
507 }
508
509 /* Initialize a cpp_reader structure. */
510 cpp_reader *
511 cpp_create_reader (lang)
512 enum c_lang lang;
513 {
514 cpp_reader *pfile;
515
516 /* Initialize this instance of the library if it hasn't been already. */
517 init_library ();
518
519 pfile = (cpp_reader *) xcalloc (1, sizeof (cpp_reader));
520
521 cpp_set_lang (pfile, lang);
522 CPP_OPTION (pfile, warn_import) = 1;
523 CPP_OPTION (pfile, warn_multichar) = 1;
524 CPP_OPTION (pfile, discard_comments) = 1;
525 CPP_OPTION (pfile, discard_comments_in_macro_exp) = 1;
526 CPP_OPTION (pfile, show_column) = 1;
527 CPP_OPTION (pfile, tabstop) = 8;
528 CPP_OPTION (pfile, operator_names) = 1;
529 CPP_OPTION (pfile, warn_endif_labels) = 1;
530 CPP_OPTION (pfile, warn_long_long) = !CPP_OPTION (pfile, c99);
531
532 CPP_OPTION (pfile, pending) =
533 (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
534
535 /* Default CPP arithmetic to something sensible for the host for the
536 benefit of dumb users like fix-header. */
537 CPP_OPTION (pfile, precision) = CHAR_BIT * sizeof (long);
538 CPP_OPTION (pfile, char_precision) = CHAR_BIT;
539 CPP_OPTION (pfile, wchar_precision) = CHAR_BIT * sizeof (int);
540 CPP_OPTION (pfile, int_precision) = CHAR_BIT * sizeof (int);
541 CPP_OPTION (pfile, unsigned_char) = 0;
542 CPP_OPTION (pfile, unsigned_wchar) = 1;
543
544 /* Initialize the line map. Start at logical line 1, so we can use
545 a line number of zero for special states. */
546 init_line_maps (&pfile->line_maps);
547 pfile->line = 1;
548
549 /* Initialize lexer state. */
550 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
551
552 /* Set up static tokens. */
553 pfile->avoid_paste.type = CPP_PADDING;
554 pfile->avoid_paste.val.source = NULL;
555 pfile->eof.type = CPP_EOF;
556 pfile->eof.flags = 0;
557
558 /* Create a token buffer for the lexer. */
559 _cpp_init_tokenrun (&pfile->base_run, 250);
560 pfile->cur_run = &pfile->base_run;
561 pfile->cur_token = pfile->base_run.base;
562
563 /* Initialize the base context. */
564 pfile->context = &pfile->base_context;
565 pfile->base_context.macro = 0;
566 pfile->base_context.prev = pfile->base_context.next = 0;
567
568 /* Aligned and unaligned storage. */
569 pfile->a_buff = _cpp_get_buff (pfile, 0);
570 pfile->u_buff = _cpp_get_buff (pfile, 0);
571
572 /* The expression parser stack. */
573 _cpp_expand_op_stack (pfile);
574
575 /* Initialize the buffer obstack. */
576 gcc_obstack_init (&pfile->buffer_ob);
577
578 _cpp_init_includes (pfile);
579
580 return pfile;
581 }
582
583 /* Free resources used by PFILE. Accessing PFILE after this function
584 returns leads to undefined behavior. Returns the error count. */
585 void
586 cpp_destroy (pfile)
587 cpp_reader *pfile;
588 {
589 struct search_path *dir, *dirn;
590 cpp_context *context, *contextn;
591 tokenrun *run, *runn;
592
593 free_chain (CPP_OPTION (pfile, pending)->include_head);
594 free (CPP_OPTION (pfile, pending));
595 free (pfile->op_stack);
596
597 while (CPP_BUFFER (pfile) != NULL)
598 _cpp_pop_buffer (pfile);
599
600 if (pfile->out.base)
601 free (pfile->out.base);
602
603 if (pfile->macro_buffer)
604 {
605 free ((PTR) pfile->macro_buffer);
606 pfile->macro_buffer = NULL;
607 pfile->macro_buffer_len = 0;
608 }
609
610 if (pfile->deps)
611 deps_free (pfile->deps);
612 obstack_free (&pfile->buffer_ob, 0);
613
614 _cpp_destroy_hashtable (pfile);
615 _cpp_cleanup_includes (pfile);
616
617 _cpp_free_buff (pfile->a_buff);
618 _cpp_free_buff (pfile->u_buff);
619 _cpp_free_buff (pfile->free_buffs);
620
621 for (run = &pfile->base_run; run; run = runn)
622 {
623 runn = run->next;
624 free (run->base);
625 if (run != &pfile->base_run)
626 free (run);
627 }
628
629 for (dir = CPP_OPTION (pfile, quote_include); dir; dir = dirn)
630 {
631 dirn = dir->next;
632 free ((PTR) dir->name);
633 free (dir);
634 }
635
636 for (context = pfile->base_context.next; context; context = contextn)
637 {
638 contextn = context->next;
639 free (context);
640 }
641
642 free_line_maps (&pfile->line_maps);
643 free (pfile);
644 }
645
646 /* This structure defines one built-in identifier. A node will be
647 entered in the hash table under the name NAME, with value VALUE.
648
649 There are two tables of these. builtin_array holds all the
650 "builtin" macros: these are handled by builtin_macro() in
651 cppmacro.c. Builtin is somewhat of a misnomer -- the property of
652 interest is that these macros require special code to compute their
653 expansions. The value is a "builtin_type" enumerator.
654
655 operator_array holds the C++ named operators. These are keywords
656 which act as aliases for punctuators. In C++, they cannot be
657 altered through #define, and #if recognizes them as operators. In
658 C, these are not entered into the hash table at all (but see
659 <iso646.h>). The value is a token-type enumerator. */
660 struct builtin
661 {
662 const uchar *name;
663 unsigned short len;
664 unsigned short value;
665 };
666
667 #define B(n, t) { DSC(n), t }
668 static const struct builtin builtin_array[] =
669 {
670 B("__TIME__", BT_TIME),
671 B("__DATE__", BT_DATE),
672 B("__FILE__", BT_FILE),
673 B("__BASE_FILE__", BT_BASE_FILE),
674 B("__LINE__", BT_SPECLINE),
675 B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL),
676 /* Keep builtins not used for -traditional-cpp at the end, and
677 update init_builtins() if any more are added. */
678 B("_Pragma", BT_PRAGMA),
679 B("__STDC__", BT_STDC),
680 };
681
682 static const struct builtin operator_array[] =
683 {
684 B("and", CPP_AND_AND),
685 B("and_eq", CPP_AND_EQ),
686 B("bitand", CPP_AND),
687 B("bitor", CPP_OR),
688 B("compl", CPP_COMPL),
689 B("not", CPP_NOT),
690 B("not_eq", CPP_NOT_EQ),
691 B("or", CPP_OR_OR),
692 B("or_eq", CPP_OR_EQ),
693 B("xor", CPP_XOR),
694 B("xor_eq", CPP_XOR_EQ)
695 };
696 #undef B
697
698 /* Mark the C++ named operators in the hash table. */
699 static void
700 mark_named_operators (pfile)
701 cpp_reader *pfile;
702 {
703 const struct builtin *b;
704
705 for (b = operator_array;
706 b < (operator_array + ARRAY_SIZE (operator_array));
707 b++)
708 {
709 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
710 hp->flags |= NODE_OPERATOR;
711 hp->is_directive = 0;
712 hp->directive_index = b->value;
713 }
714 }
715
716 /* Subroutine of cpp_read_main_file; reads the builtins table above and
717 enters them, and language-specific macros, into the hash table. */
718 static void
719 init_builtins (pfile)
720 cpp_reader *pfile;
721 {
722 const struct builtin *b;
723 size_t n = ARRAY_SIZE (builtin_array);
724
725 if (CPP_OPTION (pfile, traditional))
726 n -= 2;
727
728 for(b = builtin_array; b < builtin_array + n; b++)
729 {
730 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
731 hp->type = NT_MACRO;
732 hp->flags |= NODE_BUILTIN | NODE_WARN;
733 hp->value.builtin = b->value;
734 }
735
736 if (CPP_OPTION (pfile, cplusplus))
737 _cpp_define_builtin (pfile, "__cplusplus 1");
738 else if (CPP_OPTION (pfile, lang) == CLK_ASM)
739 _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
740 else if (CPP_OPTION (pfile, lang) == CLK_STDC94)
741 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
742 else if (CPP_OPTION (pfile, c99))
743 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
744
745 if (CPP_OPTION (pfile, objc))
746 _cpp_define_builtin (pfile, "__OBJC__ 1");
747
748 if (pfile->cb.register_builtins)
749 (*pfile->cb.register_builtins) (pfile);
750 }
751
752 /* And another subroutine. This one sets up the standard include path. */
753 static void
754 init_standard_includes (pfile)
755 cpp_reader *pfile;
756 {
757 const struct default_include *p;
758 const char *specd_prefix = CPP_OPTION (pfile, include_prefix);
759
760 /* Search "translated" versions of GNU directories.
761 These have /usr/local/lib/gcc... replaced by specd_prefix. */
762 if (specd_prefix != 0 && cpp_GCC_INCLUDE_DIR_len)
763 {
764 /* Remove the `include' from /usr/local/lib/gcc.../include.
765 GCC_INCLUDE_DIR will always end in /include. */
766 int default_len = cpp_GCC_INCLUDE_DIR_len;
767 char *default_prefix = (char *) alloca (default_len + 1);
768 int specd_len = strlen (specd_prefix);
769
770 memcpy (default_prefix, cpp_GCC_INCLUDE_DIR, default_len);
771 default_prefix[default_len] = '\0';
772
773 for (p = cpp_include_defaults; p->fname; p++)
774 {
775 /* Some standard dirs are only for C++. */
776 if (!p->cplusplus
777 || (CPP_OPTION (pfile, cplusplus)
778 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
779 {
780 /* Does this dir start with the prefix? */
781 if (!strncmp (p->fname, default_prefix, default_len))
782 {
783 /* Yes; change prefix and add to search list. */
784 int flen = strlen (p->fname);
785 int this_len = specd_len + flen - default_len;
786 char *str = (char *) xmalloc (this_len + 1);
787 memcpy (str, specd_prefix, specd_len);
788 memcpy (str + specd_len,
789 p->fname + default_len,
790 flen - default_len + 1);
791
792 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
793 }
794 }
795 }
796 }
797
798 /* Search ordinary names for GNU include directories. */
799 for (p = cpp_include_defaults; p->fname; p++)
800 {
801 /* Some standard dirs are only for C++. */
802 if (!p->cplusplus
803 || (CPP_OPTION (pfile, cplusplus)
804 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
805 {
806 char *str = update_path (p->fname, p->component);
807 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
808 }
809 }
810 }
811
812 /* Pushes a command line -imacro and -include file indicated by P onto
813 the buffer stack. Returns nonzero if successful. */
814 static bool
815 push_include (pfile, p)
816 cpp_reader *pfile;
817 struct pending_option *p;
818 {
819 cpp_token header;
820
821 /* Later: maybe update this to use the #include "" search path
822 if cpp_read_file fails. */
823 header.type = CPP_STRING;
824 header.val.str.text = (const unsigned char *) p->arg;
825 header.val.str.len = strlen (p->arg);
826 /* Make the command line directive take up a line. */
827 pfile->line++;
828
829 return _cpp_execute_include (pfile, &header, IT_CMDLINE);
830 }
831
832 /* Frees a pending_option chain. */
833 static void
834 free_chain (head)
835 struct pending_option *head;
836 {
837 struct pending_option *next;
838
839 while (head)
840 {
841 next = head->next;
842 free (head);
843 head = next;
844 }
845 }
846
847 /* Sanity-checks are dependent on command-line options, so it is
848 called as a subroutine of cpp_read_main_file (). */
849 #if ENABLE_CHECKING
850 static void sanity_checks PARAMS ((cpp_reader *));
851 static void sanity_checks (pfile)
852 cpp_reader *pfile;
853 {
854 cppchar_t test = 0;
855 size_t max_precision = 2 * CHAR_BIT * sizeof (cpp_num_part);
856
857 /* Sanity checks for assumptions about CPP arithmetic and target
858 type precisions made by cpplib. */
859 test--;
860 if (test < 1)
861 cpp_error (pfile, DL_ICE, "cppchar_t must be an unsigned type");
862
863 if (CPP_OPTION (pfile, precision) > max_precision)
864 cpp_error (pfile, DL_ICE,
865 "preprocessor arithmetic has maximum precision of %lu bits; target requires %lu bits",
866 (unsigned long) max_precision,
867 (unsigned long) CPP_OPTION (pfile, precision));
868
869 if (CPP_OPTION (pfile, precision) < CPP_OPTION (pfile, int_precision))
870 cpp_error (pfile, DL_ICE,
871 "CPP arithmetic must be at least as precise as a target int");
872
873 if (CPP_OPTION (pfile, char_precision) < 8)
874 cpp_error (pfile, DL_ICE, "target char is less than 8 bits wide");
875
876 if (CPP_OPTION (pfile, wchar_precision) < CPP_OPTION (pfile, char_precision))
877 cpp_error (pfile, DL_ICE,
878 "target wchar_t is narrower than target char");
879
880 if (CPP_OPTION (pfile, int_precision) < CPP_OPTION (pfile, char_precision))
881 cpp_error (pfile, DL_ICE,
882 "target int is narrower than target char");
883
884 /* This is assumed in eval_token() and could be fixed if necessary. */
885 if (sizeof (cppchar_t) > sizeof (cpp_num_part))
886 cpp_error (pfile, DL_ICE, "CPP half-integer narrower than CPP character");
887
888 if (CPP_OPTION (pfile, wchar_precision) > BITS_PER_CPPCHAR_T)
889 cpp_error (pfile, DL_ICE,
890 "CPP on this host cannot handle wide character constants over %lu bits, but the target requires %lu bits",
891 (unsigned long) BITS_PER_CPPCHAR_T,
892 (unsigned long) CPP_OPTION (pfile, wchar_precision));
893 }
894 #else
895 # define sanity_checks(PFILE)
896 #endif
897
898 /* Add a dependency target. Can be called any number of times before
899 cpp_read_main_file(). If no targets have been added before
900 cpp_read_main_file(), then the default target is used. */
901 void
902 cpp_add_dependency_target (pfile, target, quote)
903 cpp_reader *pfile;
904 const char *target;
905 int quote;
906 {
907 if (!pfile->deps)
908 pfile->deps = deps_init ();
909
910 deps_add_target (pfile->deps, target, quote);
911 }
912
913 /* This is called after options have been parsed, and partially
914 processed. Setup for processing input from the file named FNAME,
915 or stdin if it is the empty string. Return the original filename
916 on success (e.g. foo.i->foo.c), or NULL on failure. */
917 const char *
918 cpp_read_main_file (pfile, fname, table)
919 cpp_reader *pfile;
920 const char *fname;
921 hash_table *table;
922 {
923 static const char *const lang_env_vars[] =
924 { "C_INCLUDE_PATH", "CPLUS_INCLUDE_PATH",
925 "OBJC_INCLUDE_PATH", "OBJCPLUS_INCLUDE_PATH" };
926 size_t lang;
927
928 sanity_checks (pfile);
929
930 post_options (pfile);
931
932 /* The front ends don't set up the hash table until they have
933 finished processing the command line options, so initializing the
934 hashtable is deferred until now. */
935 _cpp_init_hashtable (pfile, table);
936
937 /* Several environment variables may add to the include search path.
938 CPATH specifies an additional list of directories to be searched
939 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
940 etc. specify an additional list of directories to be searched as
941 if specified with -isystem, for the language indicated. */
942 path_include (pfile, "CPATH", BRACKET);
943 lang = (CPP_OPTION (pfile, objc) << 1) + CPP_OPTION (pfile, cplusplus);
944 path_include (pfile, lang_env_vars[lang], SYSTEM);
945
946 /* Set up the include search path now. */
947 if (! CPP_OPTION (pfile, no_standard_includes))
948 init_standard_includes (pfile);
949
950 merge_include_chains (pfile);
951
952 /* With -v, print the list of dirs to search. */
953 if (CPP_OPTION (pfile, verbose))
954 {
955 struct search_path *l;
956 fprintf (stderr, _("#include \"...\" search starts here:\n"));
957 for (l = CPP_OPTION (pfile, quote_include); l; l = l->next)
958 {
959 if (l == CPP_OPTION (pfile, bracket_include))
960 fprintf (stderr, _("#include <...> search starts here:\n"));
961 fprintf (stderr, " %s\n", l->name);
962 }
963 fprintf (stderr, _("End of search list.\n"));
964 }
965
966 if (CPP_OPTION (pfile, deps.style) != DEPS_NONE)
967 {
968 if (!pfile->deps)
969 pfile->deps = deps_init ();
970
971 /* Set the default target (if there is none already). */
972 deps_add_default_target (pfile->deps, fname);
973 }
974
975 /* Open the main input file. */
976 if (!_cpp_read_file (pfile, fname))
977 return NULL;
978
979 /* Set this here so the client can change the option if it wishes,
980 and after stacking the main file so we don't trace the main
981 file. */
982 pfile->line_maps.trace_includes = CPP_OPTION (pfile, print_include_names);
983
984 /* For foo.i, read the original filename foo.c now, for the benefit
985 of the front ends. */
986 if (CPP_OPTION (pfile, preprocessed))
987 read_original_filename (pfile);
988
989 return pfile->map->to_file;
990 }
991
992 /* For preprocessed files, if the first tokens are of the form # NUM.
993 handle the directive so we know the original file name. This will
994 generate file_change callbacks, which the front ends must handle
995 appropriately given their state of initialization. */
996 static void
997 read_original_filename (pfile)
998 cpp_reader *pfile;
999 {
1000 const cpp_token *token, *token1;
1001
1002 /* Lex ahead; if the first tokens are of the form # NUM, then
1003 process the directive, otherwise back up. */
1004 token = _cpp_lex_direct (pfile);
1005 if (token->type == CPP_HASH)
1006 {
1007 token1 = _cpp_lex_direct (pfile);
1008 _cpp_backup_tokens (pfile, 1);
1009
1010 /* If it's a #line directive, handle it. */
1011 if (token1->type == CPP_NUMBER)
1012 {
1013 _cpp_handle_directive (pfile, token->flags & PREV_WHITE);
1014 return;
1015 }
1016 }
1017
1018 /* Backup as if nothing happened. */
1019 _cpp_backup_tokens (pfile, 1);
1020 }
1021
1022 /* Handle pending command line options: -D, -U, -A, -imacros and
1023 -include. This should be called after debugging has been properly
1024 set up in the front ends. */
1025 void
1026 cpp_finish_options (pfile)
1027 cpp_reader *pfile;
1028 {
1029 /* Mark named operators before handling command line macros. */
1030 if (CPP_OPTION (pfile, cplusplus) && CPP_OPTION (pfile, operator_names))
1031 mark_named_operators (pfile);
1032
1033 /* Install builtins and process command line macros etc. in the order
1034 they appeared, but only if not already preprocessed. */
1035 if (! CPP_OPTION (pfile, preprocessed))
1036 {
1037 struct pending_option *p;
1038
1039 _cpp_do_file_change (pfile, LC_RENAME, _("<built-in>"), 1, 0);
1040 init_builtins (pfile);
1041 _cpp_do_file_change (pfile, LC_RENAME, _("<command line>"), 1, 0);
1042 for (p = CPP_OPTION (pfile, pending)->directive_head; p; p = p->next)
1043 (*p->handler) (pfile, p->arg);
1044
1045 /* Scan -imacros files after -D, -U, but before -include.
1046 pfile->next_include_file is NULL, so _cpp_pop_buffer does not
1047 push -include files. */
1048 for (p = CPP_OPTION (pfile, pending)->imacros_head; p; p = p->next)
1049 if (push_include (pfile, p))
1050 cpp_scan_nooutput (pfile);
1051
1052 pfile->next_include_file = &CPP_OPTION (pfile, pending)->include_head;
1053 _cpp_maybe_push_include_file (pfile);
1054 }
1055
1056 pfile->first_unused_line = pfile->line;
1057
1058 free_chain (CPP_OPTION (pfile, pending)->imacros_head);
1059 free_chain (CPP_OPTION (pfile, pending)->directive_head);
1060 }
1061
1062 /* Push the next buffer on the stack given by -include, if any. */
1063 void
1064 _cpp_maybe_push_include_file (pfile)
1065 cpp_reader *pfile;
1066 {
1067 if (pfile->next_include_file)
1068 {
1069 struct pending_option *head = *pfile->next_include_file;
1070
1071 while (head && !push_include (pfile, head))
1072 head = head->next;
1073
1074 if (head)
1075 pfile->next_include_file = &head->next;
1076 else
1077 {
1078 /* All done; restore the line map from <command line>. */
1079 _cpp_do_file_change (pfile, LC_RENAME,
1080 pfile->line_maps.maps[0].to_file, 1, 0);
1081 /* Don't come back here again. */
1082 pfile->next_include_file = NULL;
1083 }
1084 }
1085 }
1086
1087 /* This is called at the end of preprocessing. It pops the last
1088 buffer and writes dependency output, and returns the number of
1089 errors.
1090
1091 Maybe it should also reset state, such that you could call
1092 cpp_start_read with a new filename to restart processing. */
1093 int
1094 cpp_finish (pfile, deps_stream)
1095 cpp_reader *pfile;
1096 FILE *deps_stream;
1097 {
1098 /* Warn about unused macros before popping the final buffer. */
1099 if (CPP_OPTION (pfile, warn_unused_macros))
1100 cpp_forall_identifiers (pfile, _cpp_warn_if_unused_macro, NULL);
1101
1102 /* cpplex.c leaves the final buffer on the stack. This it so that
1103 it returns an unending stream of CPP_EOFs to the client. If we
1104 popped the buffer, we'd dereference a NULL buffer pointer and
1105 segfault. It's nice to allow the client to do worry-free excess
1106 cpp_get_token calls. */
1107 while (pfile->buffer)
1108 _cpp_pop_buffer (pfile);
1109
1110 /* Don't write the deps file if there are errors. */
1111 if (CPP_OPTION (pfile, deps.style) != DEPS_NONE
1112 && deps_stream && pfile->errors == 0)
1113 {
1114 deps_write (pfile->deps, deps_stream, 72);
1115
1116 if (CPP_OPTION (pfile, deps.phony_targets))
1117 deps_phony_targets (pfile->deps, deps_stream);
1118 }
1119
1120 /* Report on headers that could use multiple include guards. */
1121 if (CPP_OPTION (pfile, print_include_names))
1122 _cpp_report_missing_guards (pfile);
1123
1124 return pfile->errors;
1125 }
1126
1127 /* Add a directive to be handled later in the initialization phase. */
1128 static void
1129 new_pending_directive (pend, text, handler)
1130 struct cpp_pending *pend;
1131 const char *text;
1132 cl_directive_handler handler;
1133 {
1134 struct pending_option *o = (struct pending_option *)
1135 xmalloc (sizeof (struct pending_option));
1136
1137 o->arg = text;
1138 o->next = NULL;
1139 o->handler = handler;
1140 APPEND (pend, directive, o);
1141 }
1142
1143 /* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
1144 I.e. a const string initializer with parens around it. That is
1145 what N_("string") resolves to, so we make no_* be macros instead. */
1146 #define no_ass N_("assertion missing after %s")
1147 #define no_dir N_("directory name missing after %s")
1148 #define no_fil N_("file name missing after %s")
1149 #define no_mac N_("macro name missing after %s")
1150 #define no_pth N_("path name missing after %s")
1151
1152 /* This is the list of all command line options, with the leading
1153 "-" removed. It must be sorted in ASCII collating order. */
1154 #define COMMAND_LINE_OPTIONS \
1155 DEF_OPT("A", no_ass, OPT_A) \
1156 DEF_OPT("D", no_mac, OPT_D) \
1157 DEF_OPT("I", no_dir, OPT_I) \
1158 DEF_OPT("U", no_mac, OPT_U) \
1159 DEF_OPT("idirafter", no_dir, OPT_idirafter) \
1160 DEF_OPT("imacros", no_fil, OPT_imacros) \
1161 DEF_OPT("include", no_fil, OPT_include) \
1162 DEF_OPT("iprefix", no_pth, OPT_iprefix) \
1163 DEF_OPT("isystem", no_dir, OPT_isystem) \
1164 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix) \
1165 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore)
1166
1167 #define DEF_OPT(text, msg, code) code,
1168 enum opt_code
1169 {
1170 COMMAND_LINE_OPTIONS
1171 N_OPTS
1172 };
1173 #undef DEF_OPT
1174
1175 struct cl_option
1176 {
1177 const char *opt_text;
1178 const char *msg;
1179 size_t opt_len;
1180 enum opt_code opt_code;
1181 };
1182
1183 #define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
1184 #ifdef HOST_EBCDIC
1185 static struct cl_option cl_options[] =
1186 #else
1187 static const struct cl_option cl_options[] =
1188 #endif
1189 {
1190 COMMAND_LINE_OPTIONS
1191 };
1192 #undef DEF_OPT
1193 #undef COMMAND_LINE_OPTIONS
1194
1195 /* Perform a binary search to find which, if any, option the given
1196 command-line matches. Returns its index in the option array,
1197 negative on failure. Complications arise since some options can be
1198 suffixed with an argument, and multiple complete matches can occur,
1199 e.g. -pedantic and -pedantic-errors. */
1200 static int
1201 parse_option (input)
1202 const char *input;
1203 {
1204 unsigned int md, mn, mx;
1205 size_t opt_len;
1206 int comp;
1207
1208 mn = 0;
1209 mx = N_OPTS;
1210
1211 while (mx > mn)
1212 {
1213 md = (mn + mx) / 2;
1214
1215 opt_len = cl_options[md].opt_len;
1216 comp = strncmp (input, cl_options[md].opt_text, opt_len);
1217
1218 if (comp > 0)
1219 mn = md + 1;
1220 else if (comp < 0)
1221 mx = md;
1222 else
1223 {
1224 if (input[opt_len] == '\0')
1225 return md;
1226 /* We were passed more text. If the option takes an argument,
1227 we may match a later option or we may have been passed the
1228 argument. The longest possible option match succeeds.
1229 If the option takes no arguments we have not matched and
1230 continue the search (e.g. input="stdc++" match was "stdc"). */
1231 mn = md + 1;
1232 if (cl_options[md].msg)
1233 {
1234 /* Scan forwards. If we get an exact match, return it.
1235 Otherwise, return the longest option-accepting match.
1236 This loops no more than twice with current options. */
1237 mx = md;
1238 for (; mn < (unsigned int) N_OPTS; mn++)
1239 {
1240 opt_len = cl_options[mn].opt_len;
1241 if (strncmp (input, cl_options[mn].opt_text, opt_len))
1242 break;
1243 if (input[opt_len] == '\0')
1244 return mn;
1245 if (cl_options[mn].msg)
1246 mx = mn;
1247 }
1248 return mx;
1249 }
1250 }
1251 }
1252
1253 return -1;
1254 }
1255
1256 /* Handle one command-line option in (argc, argv).
1257 Can be called multiple times, to handle multiple sets of options.
1258 Returns number of strings consumed. */
1259 int
1260 cpp_handle_option (pfile, argc, argv)
1261 cpp_reader *pfile;
1262 int argc;
1263 char **argv;
1264 {
1265 int i = 0;
1266 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
1267
1268 {
1269 enum opt_code opt_code;
1270 int opt_index;
1271 const char *arg = 0;
1272
1273 /* Skip over '-'. */
1274 opt_index = parse_option (&argv[i][1]);
1275 if (opt_index < 0)
1276 return i;
1277
1278 opt_code = cl_options[opt_index].opt_code;
1279 if (cl_options[opt_index].msg)
1280 {
1281 arg = &argv[i][cl_options[opt_index].opt_len + 1];
1282 if (arg[0] == '\0')
1283 {
1284 arg = argv[++i];
1285 if (!arg)
1286 {
1287 cpp_error (pfile, DL_ERROR,
1288 cl_options[opt_index].msg, argv[i - 1]);
1289 return argc;
1290 }
1291 }
1292 }
1293
1294 switch (opt_code)
1295 {
1296 case N_OPTS: /* Shut GCC up. */
1297 break;
1298
1299 case OPT_D:
1300 new_pending_directive (pend, arg, cpp_define);
1301 break;
1302 case OPT_iprefix:
1303 CPP_OPTION (pfile, include_prefix) = arg;
1304 CPP_OPTION (pfile, include_prefix_len) = strlen (arg);
1305 break;
1306
1307 case OPT_A:
1308 if (arg[0] == '-')
1309 {
1310 /* -A with an argument beginning with '-' acts as
1311 #unassert on whatever immediately follows the '-'.
1312 If "-" is the whole argument, we eliminate all
1313 predefined macros and assertions, including those
1314 that were specified earlier on the command line.
1315 That way we can get rid of any that were passed
1316 automatically in from GCC. */
1317
1318 if (arg[1] == '\0')
1319 {
1320 free_chain (pend->directive_head);
1321 pend->directive_head = NULL;
1322 pend->directive_tail = NULL;
1323 }
1324 else
1325 new_pending_directive (pend, arg + 1, cpp_unassert);
1326 }
1327 else
1328 new_pending_directive (pend, arg, cpp_assert);
1329 break;
1330 case OPT_U:
1331 new_pending_directive (pend, arg, cpp_undef);
1332 break;
1333 case OPT_I: /* Add directory to path for includes. */
1334 if (!strcmp (arg, "-"))
1335 {
1336 /* -I- means:
1337 Use the preceding -I directories for #include "..."
1338 but not #include <...>.
1339 Don't search the directory of the present file
1340 for #include "...". (Note that -I. -I- is not the same as
1341 the default setup; -I. uses the compiler's working dir.) */
1342 if (! CPP_OPTION (pfile, ignore_srcdir))
1343 {
1344 pend->quote_head = pend->brack_head;
1345 pend->quote_tail = pend->brack_tail;
1346 pend->brack_head = 0;
1347 pend->brack_tail = 0;
1348 CPP_OPTION (pfile, ignore_srcdir) = 1;
1349 }
1350 else
1351 {
1352 cpp_error (pfile, DL_ERROR, "-I- specified twice");
1353 return argc;
1354 }
1355 }
1356 else
1357 append_include_chain (pfile, xstrdup (arg), BRACKET, 0);
1358 break;
1359 case OPT_isystem:
1360 /* Add directory to beginning of system include path, as a system
1361 include directory. */
1362 append_include_chain (pfile, xstrdup (arg), SYSTEM, 0);
1363 break;
1364 case OPT_include:
1365 case OPT_imacros:
1366 {
1367 struct pending_option *o = (struct pending_option *)
1368 xmalloc (sizeof (struct pending_option));
1369 o->arg = arg;
1370 o->next = NULL;
1371
1372 if (opt_code == OPT_include)
1373 APPEND (pend, include, o);
1374 else
1375 APPEND (pend, imacros, o);
1376 }
1377 break;
1378 case OPT_iwithprefix:
1379 /* Add directory to end of path for includes,
1380 with the default prefix at the front of its name. */
1381 /* fall through */
1382 case OPT_iwithprefixbefore:
1383 /* Add directory to main path for includes,
1384 with the default prefix at the front of its name. */
1385 {
1386 char *fname;
1387 int len;
1388
1389 len = strlen (arg);
1390
1391 if (CPP_OPTION (pfile, include_prefix) != 0)
1392 {
1393 size_t ipl = CPP_OPTION (pfile, include_prefix_len);
1394 fname = xmalloc (ipl + len + 1);
1395 memcpy (fname, CPP_OPTION (pfile, include_prefix), ipl);
1396 memcpy (fname + ipl, arg, len + 1);
1397 }
1398 else if (cpp_GCC_INCLUDE_DIR_len)
1399 {
1400 fname = xmalloc (cpp_GCC_INCLUDE_DIR_len + len + 1);
1401 memcpy (fname, cpp_GCC_INCLUDE_DIR, cpp_GCC_INCLUDE_DIR_len);
1402 memcpy (fname + cpp_GCC_INCLUDE_DIR_len, arg, len + 1);
1403 }
1404 else
1405 fname = xstrdup (arg);
1406
1407 append_include_chain (pfile, fname,
1408 opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0);
1409 }
1410 break;
1411 case OPT_idirafter:
1412 /* Add directory to end of path for includes. */
1413 append_include_chain (pfile, xstrdup (arg), AFTER, 0);
1414 break;
1415 }
1416 }
1417 return i + 1;
1418 }
1419
1420 /* Handle command-line options in (argc, argv).
1421 Can be called multiple times, to handle multiple sets of options.
1422 Returns if an unrecognized option is seen.
1423 Returns number of strings consumed. */
1424 int
1425 cpp_handle_options (pfile, argc, argv)
1426 cpp_reader *pfile;
1427 int argc;
1428 char **argv;
1429 {
1430 int i;
1431 int strings_processed;
1432
1433 for (i = 0; i < argc; i += strings_processed)
1434 {
1435 strings_processed = cpp_handle_option (pfile, argc - i, argv + i);
1436 if (strings_processed == 0)
1437 break;
1438 }
1439
1440 return i;
1441 }
1442
1443 static void
1444 post_options (pfile)
1445 cpp_reader *pfile;
1446 {
1447 /* -Wtraditional is not useful in C++ mode. */
1448 if (CPP_OPTION (pfile, cplusplus))
1449 CPP_OPTION (pfile, warn_traditional) = 0;
1450
1451 /* Permanently disable macro expansion if we are rescanning
1452 preprocessed text. Read preprocesed source in ISO mode. */
1453 if (CPP_OPTION (pfile, preprocessed))
1454 {
1455 pfile->state.prevent_expansion = 1;
1456 CPP_OPTION (pfile, traditional) = 0;
1457 }
1458
1459 /* Traditional CPP does not accurately track column information. */
1460 if (CPP_OPTION (pfile, traditional))
1461 CPP_OPTION (pfile, show_column) = 0;
1462 }