2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 Contributed by Per Bothner, 1994-95.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 #include "coretypes.h"
31 #include "cppdefault.h"
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. */
37 # define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
38 # define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
40 # if (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
41 # define INO_T_EQ(A, B) 0
43 # define INO_T_EQ(A, B) ((A) == (B))
45 # define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
48 /* Internal structures and prototypes. */
50 /* A `struct pending_option' remembers one -D, -A, -U, -include, or
52 typedef void (* cl_directive_handler
) PARAMS ((cpp_reader
*, const char *));
55 struct pending_option
*next
;
57 cl_directive_handler handler
;
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. */
66 struct pending_option
*directive_head
, *directive_tail
;
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
;
73 struct pending_option
*imacros_head
, *imacros_tail
;
74 struct pending_option
*include_head
, *include_tail
;
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); \
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); \
91 static void path_include
PARAMS ((cpp_reader
*,
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
*,
98 static struct search_path
* remove_dup_dir
PARAMS ((cpp_reader
*,
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
*,
114 cl_directive_handler
));
115 static int parse_option
PARAMS ((const char *));
116 static void post_options
PARAMS ((cpp_reader
*));
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
};
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
125 #if HAVE_DESIGNATED_INITIALIZERS
127 #define init_trigraph_map() /* Nothing. */
128 #define TRIGRAPH_MAP \
129 __extension__ const uchar _cpp_trigraph_map[UCHAR_MAX + 1] = {
132 #define s(p, v) [p] = v,
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;
141 #define s(p, v) x[p] = v;
146 s('=', '#') s(')', ']') s('!', '|')
147 s('(', '[') s('\'', '^') s('>', '}')
148 s('/', '\\') s('<', '{') s('-', '~')
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. */
158 path_include (pfile
, env_var
, path
)
165 GET_ENVIRONMENT (q
, env_var
);
169 for (p
= q
; *q
; p
= q
+ 1)
171 /* Find the end of this name. */
173 while (*q
!= 0 && *q
!= PATH_SEPARATOR
) q
++;
176 /* An empty name in the path stands for the current directory. */
177 name
= (char *) xmalloc (2);
183 /* Otherwise use the directory that is named. */
184 name
= (char *) xmalloc (q
- p
+ 1);
185 memcpy (name
, p
, q
- p
);
189 append_include_chain (pfile
, name
, path
, path
== SYSTEM
);
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. */
198 append_include_chain (pfile
, dir
, path
, cxx_aware
)
204 struct cpp_pending
*pend
= CPP_OPTION (pfile
, pending
);
205 struct search_path
*new;
214 _cpp_simplify_pathname (dir
);
218 /* Dirs that don't exist are silently ignored. */
220 cpp_errno (pfile
, DL_ERROR
, dir
);
221 else if (CPP_OPTION (pfile
, verbose
))
222 fprintf (stderr
, _("ignoring nonexistent directory \"%s\"\n"), dir
);
227 if (!S_ISDIR (st
.st_mode
))
229 cpp_error_with_line (pfile
, DL_ERROR
, 0, 0, "%s: Not a directory", dir
);
235 if (len
> pfile
->max_include_len
)
236 pfile
->max_include_len
= len
;
238 new = (struct search_path
*) xmalloc (sizeof (struct search_path
));
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;
250 new->name_map
= NULL
;
255 case BRACKET
: APPEND (pend
, brack
, new); break;
256 case SYSTEM
: APPEND (pend
, systm
, new); break;
257 case AFTER
: APPEND (pend
, after
, new); break;
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.
265 static struct search_path
*
266 remove_dup_dir (pfile
, prev
, head_ptr
)
268 struct search_path
*prev
;
269 struct search_path
**head_ptr
;
271 struct search_path
*cur
;
276 prev
->next
= cur
->next
;
281 *head_ptr
= cur
->next
;
284 if (CPP_OPTION (pfile
, verbose
))
285 fprintf (stderr
, _("ignoring duplicate directory \"%s\"\n"), cur
->name
);
287 free ((PTR
) cur
->name
);
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
)
301 struct search_path
**head_ptr
;
302 struct search_path
*end
;
305 struct search_path
*prev
= NULL
, *cur
, *other
;
307 for (cur
= *head_ptr
; cur
; cur
= cur
->next
)
312 for (other
= *head_ptr
, prev
= NULL
;
314 other
= other
? other
->next
: *head_ptr
)
317 && INO_T_EQ (cur
->ino
, other
->ino
)
318 && cur
->dev
== other
->dev
)
320 other
= remove_dup_dir (pfile
, prev
, head_ptr
);
321 if (CPP_OPTION (pfile
, verbose
))
323 _(" as it is a non-system directory that duplicates a system directory\n"));
331 for (cur
= *head_ptr
; cur
!= end
; cur
= cur
->next
)
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
)
344 struct search_path
**head_ptr
;
346 struct search_path
*prev
= NULL
, *cur
, *other
;
348 for (cur
= *head_ptr
; cur
; cur
= cur
->next
)
350 for (other
= *head_ptr
; other
!= cur
; other
= other
->next
)
351 if (INO_T_EQ (cur
->ino
, other
->ino
) && cur
->dev
== other
->dev
)
353 cur
= remove_dup_dir (pfile
, prev
, head_ptr
);
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. */
368 merge_include_chains (pfile
)
371 struct search_path
*quote
, *brack
, *systm
, *qtail
;
373 struct cpp_pending
*pend
= CPP_OPTION (pfile
, pending
);
375 quote
= pend
->quote_head
;
376 brack
= pend
->brack_head
;
377 systm
= pend
->systm_head
;
378 qtail
= pend
->quote_tail
;
380 /* Paste together bracket, system, and after include chains. */
382 pend
->systm_tail
->next
= pend
->after_head
;
384 systm
= pend
->after_head
;
387 pend
->brack_tail
->next
= systm
;
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.
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. */
404 remove_dup_nonsys_dirs (pfile
, &brack
, systm
);
405 remove_dup_dirs (pfile
, &brack
);
409 qtail
= remove_dup_dirs (pfile
, "e
);
412 qtail
= remove_dup_nonsys_dirs (pfile
, "e
, brack
);
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
, "e
);
422 CPP_OPTION (pfile
, quote_include
) = quote
;
423 CPP_OPTION (pfile
, bracket_include
) = brack
;
426 /* A set of booleans indicating what CPP features each source language
432 char extended_numbers
;
434 char dollars_in_ident
;
435 char cplusplus_comments
;
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 }
452 /* Sets internal flags correctly for a given language. */
454 cpp_set_lang (pfile
, lang
)
458 const struct lang_flags
*l
= &lang_defaults
[(int) lang
];
460 CPP_OPTION (pfile
, lang
) = lang
;
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
;
473 static int opt_comp
PARAMS ((const void *, const void *));
475 /* Run-time sorting of options array. */
480 return strcmp (((struct cl_option
*) p1
)->opt_text
,
481 ((struct cl_option
*) p2
)->opt_text
);
485 /* init initializes library global state. It might not need to
486 do anything depending on the platform and compiler. */
490 static int initialized
= 0;
497 /* For non-ASCII hosts, the cl_options array needs to be sorted at
499 qsort (cl_options
, N_OPTS
, sizeof (struct cl_option
), opt_comp
);
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
505 init_trigraph_map ();
509 /* Initialize a cpp_reader structure. */
511 cpp_create_reader (lang
)
516 /* Initialize this instance of the library if it hasn't been already. */
519 pfile
= (cpp_reader
*) xcalloc (1, sizeof (cpp_reader
));
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_deprecated
) = 1;
531 CPP_OPTION (pfile
, warn_long_long
) = !CPP_OPTION (pfile
, c99
);
533 CPP_OPTION (pfile
, pending
) =
534 (struct cpp_pending
*) xcalloc (1, sizeof (struct cpp_pending
));
536 /* Default CPP arithmetic to something sensible for the host for the
537 benefit of dumb users like fix-header. */
538 CPP_OPTION (pfile
, precision
) = CHAR_BIT
* sizeof (long);
539 CPP_OPTION (pfile
, char_precision
) = CHAR_BIT
;
540 CPP_OPTION (pfile
, wchar_precision
) = CHAR_BIT
* sizeof (int);
541 CPP_OPTION (pfile
, int_precision
) = CHAR_BIT
* sizeof (int);
542 CPP_OPTION (pfile
, unsigned_char
) = 0;
543 CPP_OPTION (pfile
, unsigned_wchar
) = 1;
545 /* Initialize the line map. Start at logical line 1, so we can use
546 a line number of zero for special states. */
547 init_line_maps (&pfile
->line_maps
);
550 /* Initialize lexer state. */
551 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
553 /* Set up static tokens. */
554 pfile
->avoid_paste
.type
= CPP_PADDING
;
555 pfile
->avoid_paste
.val
.source
= NULL
;
556 pfile
->eof
.type
= CPP_EOF
;
557 pfile
->eof
.flags
= 0;
559 /* Create a token buffer for the lexer. */
560 _cpp_init_tokenrun (&pfile
->base_run
, 250);
561 pfile
->cur_run
= &pfile
->base_run
;
562 pfile
->cur_token
= pfile
->base_run
.base
;
564 /* Initialize the base context. */
565 pfile
->context
= &pfile
->base_context
;
566 pfile
->base_context
.macro
= 0;
567 pfile
->base_context
.prev
= pfile
->base_context
.next
= 0;
569 /* Aligned and unaligned storage. */
570 pfile
->a_buff
= _cpp_get_buff (pfile
, 0);
571 pfile
->u_buff
= _cpp_get_buff (pfile
, 0);
573 /* The expression parser stack. */
574 _cpp_expand_op_stack (pfile
);
576 /* Initialize the buffer obstack. */
577 gcc_obstack_init (&pfile
->buffer_ob
);
579 _cpp_init_includes (pfile
);
584 /* Free resources used by PFILE. Accessing PFILE after this function
585 returns leads to undefined behavior. Returns the error count. */
590 struct search_path
*dir
, *dirn
;
591 cpp_context
*context
, *contextn
;
592 tokenrun
*run
, *runn
;
594 free_chain (CPP_OPTION (pfile
, pending
)->include_head
);
595 free (CPP_OPTION (pfile
, pending
));
596 free (pfile
->op_stack
);
598 while (CPP_BUFFER (pfile
) != NULL
)
599 _cpp_pop_buffer (pfile
);
602 free (pfile
->out
.base
);
604 if (pfile
->macro_buffer
)
606 free ((PTR
) pfile
->macro_buffer
);
607 pfile
->macro_buffer
= NULL
;
608 pfile
->macro_buffer_len
= 0;
612 deps_free (pfile
->deps
);
613 obstack_free (&pfile
->buffer_ob
, 0);
615 _cpp_destroy_hashtable (pfile
);
616 _cpp_cleanup_includes (pfile
);
618 _cpp_free_buff (pfile
->a_buff
);
619 _cpp_free_buff (pfile
->u_buff
);
620 _cpp_free_buff (pfile
->free_buffs
);
622 for (run
= &pfile
->base_run
; run
; run
= runn
)
626 if (run
!= &pfile
->base_run
)
630 for (dir
= CPP_OPTION (pfile
, quote_include
); dir
; dir
= dirn
)
633 free ((PTR
) dir
->name
);
637 for (context
= pfile
->base_context
.next
; context
; context
= contextn
)
639 contextn
= context
->next
;
643 free_line_maps (&pfile
->line_maps
);
647 /* This structure defines one built-in identifier. A node will be
648 entered in the hash table under the name NAME, with value VALUE.
650 There are two tables of these. builtin_array holds all the
651 "builtin" macros: these are handled by builtin_macro() in
652 cppmacro.c. Builtin is somewhat of a misnomer -- the property of
653 interest is that these macros require special code to compute their
654 expansions. The value is a "builtin_type" enumerator.
656 operator_array holds the C++ named operators. These are keywords
657 which act as aliases for punctuators. In C++, they cannot be
658 altered through #define, and #if recognizes them as operators. In
659 C, these are not entered into the hash table at all (but see
660 <iso646.h>). The value is a token-type enumerator. */
665 unsigned short value
;
668 #define B(n, t) { DSC(n), t }
669 static const struct builtin builtin_array
[] =
671 B("__TIME__", BT_TIME
),
672 B("__DATE__", BT_DATE
),
673 B("__FILE__", BT_FILE
),
674 B("__BASE_FILE__", BT_BASE_FILE
),
675 B("__LINE__", BT_SPECLINE
),
676 B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL
),
677 /* Keep builtins not used for -traditional-cpp at the end, and
678 update init_builtins() if any more are added. */
679 B("_Pragma", BT_PRAGMA
),
680 B("__STDC__", BT_STDC
),
683 static const struct builtin operator_array
[] =
685 B("and", CPP_AND_AND
),
686 B("and_eq", CPP_AND_EQ
),
687 B("bitand", CPP_AND
),
689 B("compl", CPP_COMPL
),
691 B("not_eq", CPP_NOT_EQ
),
693 B("or_eq", CPP_OR_EQ
),
695 B("xor_eq", CPP_XOR_EQ
)
699 /* Mark the C++ named operators in the hash table. */
701 mark_named_operators (pfile
)
704 const struct builtin
*b
;
706 for (b
= operator_array
;
707 b
< (operator_array
+ ARRAY_SIZE (operator_array
));
710 cpp_hashnode
*hp
= cpp_lookup (pfile
, b
->name
, b
->len
);
711 hp
->flags
|= NODE_OPERATOR
;
712 hp
->is_directive
= 0;
713 hp
->directive_index
= b
->value
;
717 /* Subroutine of cpp_read_main_file; reads the builtins table above and
718 enters them, and language-specific macros, into the hash table. */
720 init_builtins (pfile
)
723 const struct builtin
*b
;
724 size_t n
= ARRAY_SIZE (builtin_array
);
726 if (CPP_OPTION (pfile
, traditional
))
729 for(b
= builtin_array
; b
< builtin_array
+ n
; b
++)
731 cpp_hashnode
*hp
= cpp_lookup (pfile
, b
->name
, b
->len
);
733 hp
->flags
|= NODE_BUILTIN
| NODE_WARN
;
734 hp
->value
.builtin
= b
->value
;
737 if (CPP_OPTION (pfile
, cplusplus
))
738 _cpp_define_builtin (pfile
, "__cplusplus 1");
739 else if (CPP_OPTION (pfile
, lang
) == CLK_ASM
)
740 _cpp_define_builtin (pfile
, "__ASSEMBLER__ 1");
741 else if (CPP_OPTION (pfile
, lang
) == CLK_STDC94
)
742 _cpp_define_builtin (pfile
, "__STDC_VERSION__ 199409L");
743 else if (CPP_OPTION (pfile
, c99
))
744 _cpp_define_builtin (pfile
, "__STDC_VERSION__ 199901L");
746 if (CPP_OPTION (pfile
, objc
))
747 _cpp_define_builtin (pfile
, "__OBJC__ 1");
749 if (pfile
->cb
.register_builtins
)
750 (*pfile
->cb
.register_builtins
) (pfile
);
753 /* And another subroutine. This one sets up the standard include path. */
755 init_standard_includes (pfile
)
758 const struct default_include
*p
;
759 const char *specd_prefix
= CPP_OPTION (pfile
, include_prefix
);
761 /* Search "translated" versions of GNU directories.
762 These have /usr/local/lib/gcc... replaced by specd_prefix. */
763 if (specd_prefix
!= 0 && cpp_GCC_INCLUDE_DIR_len
)
765 /* Remove the `include' from /usr/local/lib/gcc.../include.
766 GCC_INCLUDE_DIR will always end in /include. */
767 int default_len
= cpp_GCC_INCLUDE_DIR_len
;
768 char *default_prefix
= (char *) alloca (default_len
+ 1);
769 int specd_len
= strlen (specd_prefix
);
771 memcpy (default_prefix
, cpp_GCC_INCLUDE_DIR
, default_len
);
772 default_prefix
[default_len
] = '\0';
774 for (p
= cpp_include_defaults
; p
->fname
; p
++)
776 /* Some standard dirs are only for C++. */
778 || (CPP_OPTION (pfile
, cplusplus
)
779 && !CPP_OPTION (pfile
, no_standard_cplusplus_includes
)))
781 /* Does this dir start with the prefix? */
782 if (!strncmp (p
->fname
, default_prefix
, default_len
))
784 /* Yes; change prefix and add to search list. */
785 int flen
= strlen (p
->fname
);
786 int this_len
= specd_len
+ flen
- default_len
;
787 char *str
= (char *) xmalloc (this_len
+ 1);
788 memcpy (str
, specd_prefix
, specd_len
);
789 memcpy (str
+ specd_len
,
790 p
->fname
+ default_len
,
791 flen
- default_len
+ 1);
793 append_include_chain (pfile
, str
, SYSTEM
, p
->cxx_aware
);
799 /* Search ordinary names for GNU include directories. */
800 for (p
= cpp_include_defaults
; p
->fname
; p
++)
802 /* Some standard dirs are only for C++. */
804 || (CPP_OPTION (pfile
, cplusplus
)
805 && !CPP_OPTION (pfile
, no_standard_cplusplus_includes
)))
807 char *str
= update_path (p
->fname
, p
->component
);
808 append_include_chain (pfile
, str
, SYSTEM
, p
->cxx_aware
);
813 /* Pushes a command line -imacro and -include file indicated by P onto
814 the buffer stack. Returns nonzero if successful. */
816 push_include (pfile
, p
)
818 struct pending_option
*p
;
822 /* Later: maybe update this to use the #include "" search path
823 if cpp_read_file fails. */
824 header
.type
= CPP_STRING
;
825 header
.val
.str
.text
= (const unsigned char *) p
->arg
;
826 header
.val
.str
.len
= strlen (p
->arg
);
827 /* Make the command line directive take up a line. */
830 return _cpp_execute_include (pfile
, &header
, IT_CMDLINE
);
833 /* Frees a pending_option chain. */
836 struct pending_option
*head
;
838 struct pending_option
*next
;
848 /* Sanity-checks are dependent on command-line options, so it is
849 called as a subroutine of cpp_read_main_file (). */
851 static void sanity_checks
PARAMS ((cpp_reader
*));
852 static void sanity_checks (pfile
)
856 size_t max_precision
= 2 * CHAR_BIT
* sizeof (cpp_num_part
);
858 /* Sanity checks for assumptions about CPP arithmetic and target
859 type precisions made by cpplib. */
862 cpp_error (pfile
, DL_ICE
, "cppchar_t must be an unsigned type");
864 if (CPP_OPTION (pfile
, precision
) > max_precision
)
865 cpp_error (pfile
, DL_ICE
,
866 "preprocessor arithmetic has maximum precision of %lu bits; target requires %lu bits",
867 (unsigned long) max_precision
,
868 (unsigned long) CPP_OPTION (pfile
, precision
));
870 if (CPP_OPTION (pfile
, precision
) < CPP_OPTION (pfile
, int_precision
))
871 cpp_error (pfile
, DL_ICE
,
872 "CPP arithmetic must be at least as precise as a target int");
874 if (CPP_OPTION (pfile
, char_precision
) < 8)
875 cpp_error (pfile
, DL_ICE
, "target char is less than 8 bits wide");
877 if (CPP_OPTION (pfile
, wchar_precision
) < CPP_OPTION (pfile
, char_precision
))
878 cpp_error (pfile
, DL_ICE
,
879 "target wchar_t is narrower than target char");
881 if (CPP_OPTION (pfile
, int_precision
) < CPP_OPTION (pfile
, char_precision
))
882 cpp_error (pfile
, DL_ICE
,
883 "target int is narrower than target char");
885 /* This is assumed in eval_token() and could be fixed if necessary. */
886 if (sizeof (cppchar_t
) > sizeof (cpp_num_part
))
887 cpp_error (pfile
, DL_ICE
, "CPP half-integer narrower than CPP character");
889 if (CPP_OPTION (pfile
, wchar_precision
) > BITS_PER_CPPCHAR_T
)
890 cpp_error (pfile
, DL_ICE
,
891 "CPP on this host cannot handle wide character constants over %lu bits, but the target requires %lu bits",
892 (unsigned long) BITS_PER_CPPCHAR_T
,
893 (unsigned long) CPP_OPTION (pfile
, wchar_precision
));
896 # define sanity_checks(PFILE)
899 /* Add a dependency target. Can be called any number of times before
900 cpp_read_main_file(). If no targets have been added before
901 cpp_read_main_file(), then the default target is used. */
903 cpp_add_dependency_target (pfile
, target
, quote
)
909 pfile
->deps
= deps_init ();
911 deps_add_target (pfile
->deps
, target
, quote
);
914 /* This is called after options have been parsed, and partially
915 processed. Setup for processing input from the file named FNAME,
916 or stdin if it is the empty string. Return the original filename
917 on success (e.g. foo.i->foo.c), or NULL on failure. */
919 cpp_read_main_file (pfile
, fname
, table
)
924 static const char *const lang_env_vars
[] =
925 { "C_INCLUDE_PATH", "CPLUS_INCLUDE_PATH",
926 "OBJC_INCLUDE_PATH", "OBJCPLUS_INCLUDE_PATH" };
929 sanity_checks (pfile
);
931 post_options (pfile
);
933 /* The front ends don't set up the hash table until they have
934 finished processing the command line options, so initializing the
935 hashtable is deferred until now. */
936 _cpp_init_hashtable (pfile
, table
);
938 /* Several environment variables may add to the include search path.
939 CPATH specifies an additional list of directories to be searched
940 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
941 etc. specify an additional list of directories to be searched as
942 if specified with -isystem, for the language indicated. */
943 path_include (pfile
, "CPATH", BRACKET
);
944 lang
= (CPP_OPTION (pfile
, objc
) << 1) + CPP_OPTION (pfile
, cplusplus
);
945 path_include (pfile
, lang_env_vars
[lang
], SYSTEM
);
947 /* Set up the include search path now. */
948 if (! CPP_OPTION (pfile
, no_standard_includes
))
949 init_standard_includes (pfile
);
951 merge_include_chains (pfile
);
953 /* With -v, print the list of dirs to search. */
954 if (CPP_OPTION (pfile
, verbose
))
956 struct search_path
*l
;
957 fprintf (stderr
, _("#include \"...\" search starts here:\n"));
958 for (l
= CPP_OPTION (pfile
, quote_include
); l
; l
= l
->next
)
960 if (l
== CPP_OPTION (pfile
, bracket_include
))
961 fprintf (stderr
, _("#include <...> search starts here:\n"));
962 fprintf (stderr
, " %s\n", l
->name
);
964 fprintf (stderr
, _("End of search list.\n"));
967 if (CPP_OPTION (pfile
, deps
.style
) != DEPS_NONE
)
970 pfile
->deps
= deps_init ();
972 /* Set the default target (if there is none already). */
973 deps_add_default_target (pfile
->deps
, fname
);
976 /* Open the main input file. */
977 if (!_cpp_read_file (pfile
, fname
))
980 /* Set this here so the client can change the option if it wishes,
981 and after stacking the main file so we don't trace the main
983 pfile
->line_maps
.trace_includes
= CPP_OPTION (pfile
, print_include_names
);
985 /* For foo.i, read the original filename foo.c now, for the benefit
986 of the front ends. */
987 if (CPP_OPTION (pfile
, preprocessed
))
988 read_original_filename (pfile
);
990 return pfile
->map
->to_file
;
993 /* For preprocessed files, if the first tokens are of the form # NUM.
994 handle the directive so we know the original file name. This will
995 generate file_change callbacks, which the front ends must handle
996 appropriately given their state of initialization. */
998 read_original_filename (pfile
)
1001 const cpp_token
*token
, *token1
;
1003 /* Lex ahead; if the first tokens are of the form # NUM, then
1004 process the directive, otherwise back up. */
1005 token
= _cpp_lex_direct (pfile
);
1006 if (token
->type
== CPP_HASH
)
1008 token1
= _cpp_lex_direct (pfile
);
1009 _cpp_backup_tokens (pfile
, 1);
1011 /* If it's a #line directive, handle it. */
1012 if (token1
->type
== CPP_NUMBER
)
1014 _cpp_handle_directive (pfile
, token
->flags
& PREV_WHITE
);
1019 /* Backup as if nothing happened. */
1020 _cpp_backup_tokens (pfile
, 1);
1023 /* Handle pending command line options: -D, -U, -A, -imacros and
1024 -include. This should be called after debugging has been properly
1025 set up in the front ends. */
1027 cpp_finish_options (pfile
)
1030 /* Mark named operators before handling command line macros. */
1031 if (CPP_OPTION (pfile
, cplusplus
) && CPP_OPTION (pfile
, operator_names
))
1032 mark_named_operators (pfile
);
1034 /* Install builtins and process command line macros etc. in the order
1035 they appeared, but only if not already preprocessed. */
1036 if (! CPP_OPTION (pfile
, preprocessed
))
1038 struct pending_option
*p
;
1040 _cpp_do_file_change (pfile
, LC_RENAME
, _("<built-in>"), 1, 0);
1041 init_builtins (pfile
);
1042 _cpp_do_file_change (pfile
, LC_RENAME
, _("<command line>"), 1, 0);
1043 for (p
= CPP_OPTION (pfile
, pending
)->directive_head
; p
; p
= p
->next
)
1044 (*p
->handler
) (pfile
, p
->arg
);
1046 /* Scan -imacros files after -D, -U, but before -include.
1047 pfile->next_include_file is NULL, so _cpp_pop_buffer does not
1048 push -include files. */
1049 for (p
= CPP_OPTION (pfile
, pending
)->imacros_head
; p
; p
= p
->next
)
1050 if (push_include (pfile
, p
))
1051 cpp_scan_nooutput (pfile
);
1053 pfile
->next_include_file
= &CPP_OPTION (pfile
, pending
)->include_head
;
1054 _cpp_maybe_push_include_file (pfile
);
1057 pfile
->first_unused_line
= pfile
->line
;
1059 free_chain (CPP_OPTION (pfile
, pending
)->imacros_head
);
1060 free_chain (CPP_OPTION (pfile
, pending
)->directive_head
);
1063 /* Push the next buffer on the stack given by -include, if any. */
1065 _cpp_maybe_push_include_file (pfile
)
1068 if (pfile
->next_include_file
)
1070 struct pending_option
*head
= *pfile
->next_include_file
;
1072 while (head
&& !push_include (pfile
, head
))
1076 pfile
->next_include_file
= &head
->next
;
1079 /* All done; restore the line map from <command line>. */
1080 _cpp_do_file_change (pfile
, LC_RENAME
,
1081 pfile
->line_maps
.maps
[0].to_file
, 1, 0);
1082 /* Don't come back here again. */
1083 pfile
->next_include_file
= NULL
;
1088 /* This is called at the end of preprocessing. It pops the last
1089 buffer and writes dependency output, and returns the number of
1092 Maybe it should also reset state, such that you could call
1093 cpp_start_read with a new filename to restart processing. */
1095 cpp_finish (pfile
, deps_stream
)
1099 /* Warn about unused macros before popping the final buffer. */
1100 if (CPP_OPTION (pfile
, warn_unused_macros
))
1101 cpp_forall_identifiers (pfile
, _cpp_warn_if_unused_macro
, NULL
);
1103 /* cpplex.c leaves the final buffer on the stack. This it so that
1104 it returns an unending stream of CPP_EOFs to the client. If we
1105 popped the buffer, we'd dereference a NULL buffer pointer and
1106 segfault. It's nice to allow the client to do worry-free excess
1107 cpp_get_token calls. */
1108 while (pfile
->buffer
)
1109 _cpp_pop_buffer (pfile
);
1111 /* Don't write the deps file if there are errors. */
1112 if (CPP_OPTION (pfile
, deps
.style
) != DEPS_NONE
1113 && deps_stream
&& pfile
->errors
== 0)
1115 deps_write (pfile
->deps
, deps_stream
, 72);
1117 if (CPP_OPTION (pfile
, deps
.phony_targets
))
1118 deps_phony_targets (pfile
->deps
, deps_stream
);
1121 /* Report on headers that could use multiple include guards. */
1122 if (CPP_OPTION (pfile
, print_include_names
))
1123 _cpp_report_missing_guards (pfile
);
1125 return pfile
->errors
;
1128 /* Add a directive to be handled later in the initialization phase. */
1130 new_pending_directive (pend
, text
, handler
)
1131 struct cpp_pending
*pend
;
1133 cl_directive_handler handler
;
1135 struct pending_option
*o
= (struct pending_option
*)
1136 xmalloc (sizeof (struct pending_option
));
1140 o
->handler
= handler
;
1141 APPEND (pend
, directive
, o
);
1144 /* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
1145 I.e. a const string initializer with parens around it. That is
1146 what N_("string") resolves to, so we make no_* be macros instead. */
1147 #define no_ass N_("assertion missing after %s")
1148 #define no_dir N_("directory name missing after %s")
1149 #define no_fil N_("file name missing after %s")
1150 #define no_mac N_("macro name missing after %s")
1151 #define no_pth N_("path name missing after %s")
1153 /* This is the list of all command line options, with the leading
1154 "-" removed. It must be sorted in ASCII collating order. */
1155 #define COMMAND_LINE_OPTIONS \
1156 DEF_OPT("A", no_ass, OPT_A) \
1157 DEF_OPT("D", no_mac, OPT_D) \
1158 DEF_OPT("I", no_dir, OPT_I) \
1159 DEF_OPT("U", no_mac, OPT_U) \
1160 DEF_OPT("idirafter", no_dir, OPT_idirafter) \
1161 DEF_OPT("imacros", no_fil, OPT_imacros) \
1162 DEF_OPT("include", no_fil, OPT_include) \
1163 DEF_OPT("iprefix", no_pth, OPT_iprefix) \
1164 DEF_OPT("isystem", no_dir, OPT_isystem) \
1165 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix) \
1166 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore)
1168 #define DEF_OPT(text, msg, code) code,
1171 COMMAND_LINE_OPTIONS
1178 const char *opt_text
;
1181 enum opt_code opt_code
;
1184 #define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
1186 static struct cl_option cl_options
[] =
1188 static const struct cl_option cl_options
[] =
1191 COMMAND_LINE_OPTIONS
1194 #undef COMMAND_LINE_OPTIONS
1196 /* Perform a binary search to find which, if any, option the given
1197 command-line matches. Returns its index in the option array,
1198 negative on failure. Complications arise since some options can be
1199 suffixed with an argument, and multiple complete matches can occur,
1200 e.g. -pedantic and -pedantic-errors. */
1202 parse_option (input
)
1205 unsigned int md
, mn
, mx
;
1216 opt_len
= cl_options
[md
].opt_len
;
1217 comp
= strncmp (input
, cl_options
[md
].opt_text
, opt_len
);
1225 if (input
[opt_len
] == '\0')
1227 /* We were passed more text. If the option takes an argument,
1228 we may match a later option or we may have been passed the
1229 argument. The longest possible option match succeeds.
1230 If the option takes no arguments we have not matched and
1231 continue the search (e.g. input="stdc++" match was "stdc"). */
1233 if (cl_options
[md
].msg
)
1235 /* Scan forwards. If we get an exact match, return it.
1236 Otherwise, return the longest option-accepting match.
1237 This loops no more than twice with current options. */
1239 for (; mn
< (unsigned int) N_OPTS
; mn
++)
1241 opt_len
= cl_options
[mn
].opt_len
;
1242 if (strncmp (input
, cl_options
[mn
].opt_text
, opt_len
))
1244 if (input
[opt_len
] == '\0')
1246 if (cl_options
[mn
].msg
)
1257 /* Handle one command-line option in (argc, argv).
1258 Can be called multiple times, to handle multiple sets of options.
1259 Returns number of strings consumed. */
1261 cpp_handle_option (pfile
, argc
, argv
)
1267 struct cpp_pending
*pend
= CPP_OPTION (pfile
, pending
);
1270 enum opt_code opt_code
;
1272 const char *arg
= 0;
1274 /* Skip over '-'. */
1275 opt_index
= parse_option (&argv
[i
][1]);
1279 opt_code
= cl_options
[opt_index
].opt_code
;
1280 if (cl_options
[opt_index
].msg
)
1282 arg
= &argv
[i
][cl_options
[opt_index
].opt_len
+ 1];
1288 cpp_error (pfile
, DL_ERROR
,
1289 cl_options
[opt_index
].msg
, argv
[i
- 1]);
1297 case N_OPTS
: /* Shut GCC up. */
1301 new_pending_directive (pend
, arg
, cpp_define
);
1304 CPP_OPTION (pfile
, include_prefix
) = arg
;
1305 CPP_OPTION (pfile
, include_prefix_len
) = strlen (arg
);
1311 /* -A with an argument beginning with '-' acts as
1312 #unassert on whatever immediately follows the '-'.
1313 If "-" is the whole argument, we eliminate all
1314 predefined macros and assertions, including those
1315 that were specified earlier on the command line.
1316 That way we can get rid of any that were passed
1317 automatically in from GCC. */
1321 free_chain (pend
->directive_head
);
1322 pend
->directive_head
= NULL
;
1323 pend
->directive_tail
= NULL
;
1326 new_pending_directive (pend
, arg
+ 1, cpp_unassert
);
1329 new_pending_directive (pend
, arg
, cpp_assert
);
1332 new_pending_directive (pend
, arg
, cpp_undef
);
1334 case OPT_I
: /* Add directory to path for includes. */
1335 if (!strcmp (arg
, "-"))
1338 Use the preceding -I directories for #include "..."
1339 but not #include <...>.
1340 Don't search the directory of the present file
1341 for #include "...". (Note that -I. -I- is not the same as
1342 the default setup; -I. uses the compiler's working dir.) */
1343 if (! CPP_OPTION (pfile
, ignore_srcdir
))
1345 pend
->quote_head
= pend
->brack_head
;
1346 pend
->quote_tail
= pend
->brack_tail
;
1347 pend
->brack_head
= 0;
1348 pend
->brack_tail
= 0;
1349 CPP_OPTION (pfile
, ignore_srcdir
) = 1;
1353 cpp_error (pfile
, DL_ERROR
, "-I- specified twice");
1358 append_include_chain (pfile
, xstrdup (arg
), BRACKET
, 0);
1361 /* Add directory to beginning of system include path, as a system
1362 include directory. */
1363 append_include_chain (pfile
, xstrdup (arg
), SYSTEM
, 0);
1368 struct pending_option
*o
= (struct pending_option
*)
1369 xmalloc (sizeof (struct pending_option
));
1373 if (opt_code
== OPT_include
)
1374 APPEND (pend
, include
, o
);
1376 APPEND (pend
, imacros
, o
);
1379 case OPT_iwithprefix
:
1380 /* Add directory to end of path for includes,
1381 with the default prefix at the front of its name. */
1383 case OPT_iwithprefixbefore
:
1384 /* Add directory to main path for includes,
1385 with the default prefix at the front of its name. */
1392 if (CPP_OPTION (pfile
, include_prefix
) != 0)
1394 size_t ipl
= CPP_OPTION (pfile
, include_prefix_len
);
1395 fname
= xmalloc (ipl
+ len
+ 1);
1396 memcpy (fname
, CPP_OPTION (pfile
, include_prefix
), ipl
);
1397 memcpy (fname
+ ipl
, arg
, len
+ 1);
1399 else if (cpp_GCC_INCLUDE_DIR_len
)
1401 fname
= xmalloc (cpp_GCC_INCLUDE_DIR_len
+ len
+ 1);
1402 memcpy (fname
, cpp_GCC_INCLUDE_DIR
, cpp_GCC_INCLUDE_DIR_len
);
1403 memcpy (fname
+ cpp_GCC_INCLUDE_DIR_len
, arg
, len
+ 1);
1406 fname
= xstrdup (arg
);
1408 append_include_chain (pfile
, fname
,
1409 opt_code
== OPT_iwithprefix
? SYSTEM
: BRACKET
, 0);
1413 /* Add directory to end of path for includes. */
1414 append_include_chain (pfile
, xstrdup (arg
), AFTER
, 0);
1421 /* Handle command-line options in (argc, argv).
1422 Can be called multiple times, to handle multiple sets of options.
1423 Returns if an unrecognized option is seen.
1424 Returns number of strings consumed. */
1426 cpp_handle_options (pfile
, argc
, argv
)
1432 int strings_processed
;
1434 for (i
= 0; i
< argc
; i
+= strings_processed
)
1436 strings_processed
= cpp_handle_option (pfile
, argc
- i
, argv
+ i
);
1437 if (strings_processed
== 0)
1445 post_options (pfile
)
1448 /* -Wtraditional is not useful in C++ mode. */
1449 if (CPP_OPTION (pfile
, cplusplus
))
1450 CPP_OPTION (pfile
, warn_traditional
) = 0;
1452 /* Permanently disable macro expansion if we are rescanning
1453 preprocessed text. Read preprocesed source in ISO mode. */
1454 if (CPP_OPTION (pfile
, preprocessed
))
1456 pfile
->state
.prevent_expansion
= 1;
1457 CPP_OPTION (pfile
, traditional
) = 0;
1460 /* Traditional CPP does not accurately track column information. */
1461 if (CPP_OPTION (pfile
, traditional
))
1462 CPP_OPTION (pfile
, show_column
) = 0;