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