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. */
30 #include "cppdefault.h"
32 /* Windows does not natively support inodes, and neither does MSDOS.
33 Cygwin's emulation can generate non-unique inodes, so don't use it.
34 VMS has non-numeric inodes. */
36 # define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
37 # define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
39 # if (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
40 # define INO_T_EQ(A, B) 0
42 # define INO_T_EQ(A, B) ((A) == (B))
44 # define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
47 /* Internal structures and prototypes. */
49 /* A `struct pending_option' remembers one -D, -A, -U, -include, or
51 typedef void (* cl_directive_handler
) PARAMS ((cpp_reader
*, const char *));
54 struct pending_option
*next
;
56 cl_directive_handler handler
;
59 /* The `pending' structure accumulates all the options that are not
60 actually processed until we hit cpp_read_main_file. It consists of
61 several lists, one for each type of option. We keep both head and
62 tail pointers for quick insertion. */
65 struct pending_option
*directive_head
, *directive_tail
;
67 struct search_path
*quote_head
, *quote_tail
;
68 struct search_path
*brack_head
, *brack_tail
;
69 struct search_path
*systm_head
, *systm_tail
;
70 struct search_path
*after_head
, *after_tail
;
72 struct pending_option
*imacros_head
, *imacros_tail
;
73 struct pending_option
*include_head
, *include_tail
;
77 #define APPEND(pend, list, elt) \
78 do { if (!(pend)->list##_head) (pend)->list##_head = (elt); \
79 else (pend)->list##_tail->next = (elt); \
80 (pend)->list##_tail = (elt); \
83 #define APPEND(pend, list, elt) \
84 do { if (!(pend)->list/**/_head) (pend)->list/**/_head = (elt); \
85 else (pend)->list/**/_tail->next = (elt); \
86 (pend)->list/**/_tail = (elt); \
90 static void path_include
PARAMS ((cpp_reader
*,
92 static void init_library
PARAMS ((void));
93 static void init_builtins
PARAMS ((cpp_reader
*));
94 static void mark_named_operators
PARAMS ((cpp_reader
*));
95 static void append_include_chain
PARAMS ((cpp_reader
*,
97 static struct search_path
* remove_dup_dir
PARAMS ((cpp_reader
*,
98 struct search_path
*));
99 static struct search_path
* remove_dup_dirs
PARAMS ((cpp_reader
*,
100 struct search_path
*));
101 static void merge_include_chains
PARAMS ((cpp_reader
*));
102 static bool push_include
PARAMS ((cpp_reader
*,
103 struct pending_option
*));
104 static void free_chain
PARAMS ((struct pending_option
*));
105 static void init_standard_includes
PARAMS ((cpp_reader
*));
106 static void read_original_filename
PARAMS ((cpp_reader
*));
107 static void new_pending_directive
PARAMS ((struct cpp_pending
*,
109 cl_directive_handler
));
110 static int parse_option
PARAMS ((const char *));
112 /* Fourth argument to append_include_chain: chain to use.
113 Note it's never asked to append to the quote chain. */
114 enum { BRACKET
= 0, SYSTEM
, AFTER
};
116 /* If we have designated initializers (GCC >2.7) these tables can be
117 initialized, constant data. Otherwise, they have to be filled in at
119 #if HAVE_DESIGNATED_INITIALIZERS
121 #define init_trigraph_map() /* Nothing. */
122 #define TRIGRAPH_MAP \
123 __extension__ const uchar _cpp_trigraph_map[UCHAR_MAX + 1] = {
126 #define s(p, v) [p] = v,
130 #define TRIGRAPH_MAP uchar _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
131 static void init_trigraph_map PARAMS ((void)) { \
132 unsigned char *x = _cpp_trigraph_map;
135 #define s(p, v) x[p] = v;
140 s('=', '#') s(')', ']') s('!', '|')
141 s('(', '[') s('\'', '^') s('>', '}')
142 s('/', '\\') s('<', '{') s('-', '~')
149 /* Given a colon-separated list of file names PATH,
150 add all the names to the search path for include files. */
152 path_include (pfile
, list
, path
)
163 /* Find the end of this name. */
165 while (*q
!= 0 && *q
!= PATH_SEPARATOR
) q
++;
168 /* An empty name in the path stands for the current directory. */
169 name
= (char *) xmalloc (2);
175 /* Otherwise use the directory that is named. */
176 name
= (char *) xmalloc (q
- p
+ 1);
177 memcpy (name
, p
, q
- p
);
181 append_include_chain (pfile
, name
, path
, 0);
183 /* Advance past this name. */
191 /* Append DIR to include path PATH. DIR must be allocated on the
192 heap; this routine takes responsibility for freeing it. CXX_AWARE
193 is non-zero if the header contains extern "C" guards for C++,
194 otherwise it is zero. */
196 append_include_chain (pfile
, dir
, path
, cxx_aware
)
202 struct cpp_pending
*pend
= CPP_OPTION (pfile
, pending
);
203 struct search_path
*new;
212 _cpp_simplify_pathname (dir
);
216 /* Dirs that don't exist are silently ignored. */
218 cpp_errno (pfile
, DL_ERROR
, dir
);
219 else if (CPP_OPTION (pfile
, verbose
))
220 fprintf (stderr
, _("ignoring nonexistent directory \"%s\"\n"), dir
);
225 if (!S_ISDIR (st
.st_mode
))
227 cpp_error_with_line (pfile
, DL_ERROR
, 0, 0, "%s: Not a directory", dir
);
233 if (len
> pfile
->max_include_len
)
234 pfile
->max_include_len
= len
;
236 new = (struct search_path
*) xmalloc (sizeof (struct search_path
));
239 INO_T_COPY (new->ino
, st
.st_ino
);
240 new->dev
= st
.st_dev
;
241 /* Both systm and after include file lists should be treated as system
242 include files since these two lists are really just a concatenation
243 of one "system" list. */
244 if (path
== SYSTEM
|| path
== AFTER
)
245 new->sysp
= cxx_aware
? 1 : 2;
248 new->name_map
= NULL
;
253 case BRACKET
: APPEND (pend
, brack
, new); break;
254 case SYSTEM
: APPEND (pend
, systm
, new); break;
255 case AFTER
: APPEND (pend
, after
, new); break;
259 /* Handle a duplicated include path. PREV is the link in the chain
260 before the duplicate. The duplicate is removed from the chain and
261 freed. Returns PREV. */
262 static struct search_path
*
263 remove_dup_dir (pfile
, prev
)
265 struct search_path
*prev
;
267 struct search_path
*cur
= prev
->next
;
269 if (CPP_OPTION (pfile
, verbose
))
270 fprintf (stderr
, _("ignoring duplicate directory \"%s\"\n"), cur
->name
);
272 prev
->next
= cur
->next
;
273 free ((PTR
) cur
->name
);
279 /* Remove duplicate directories from a chain. Returns the tail of the
280 chain, or NULL if the chain is empty. This algorithm is quadratic
281 in the number of -I switches, which is acceptable since there
282 aren't usually that many of them. */
283 static struct search_path
*
284 remove_dup_dirs (pfile
, head
)
286 struct search_path
*head
;
288 struct search_path
*prev
= NULL
, *cur
, *other
;
290 for (cur
= head
; cur
; cur
= cur
->next
)
292 for (other
= head
; other
!= cur
; other
= other
->next
)
293 if (INO_T_EQ (cur
->ino
, other
->ino
) && cur
->dev
== other
->dev
)
295 if (cur
->sysp
&& !other
->sysp
)
297 cpp_error (pfile
, DL_WARNING
,
298 "changing search order for system directory \"%s\"",
300 if (strcmp (cur
->name
, other
->name
))
301 cpp_error (pfile
, DL_WARNING
,
302 " as it is the same as non-system directory \"%s\"",
305 cpp_error (pfile
, DL_WARNING
,
306 " as it has already been specified as a non-system directory");
308 cur
= remove_dup_dir (pfile
, prev
);
317 /* Merge the four include chains together in the order quote, bracket,
318 system, after. Remove duplicate dirs (as determined by
319 INO_T_EQ()). The system_include and after_include chains are never
320 referred to again after this function; all access is through the
321 bracket_include path. */
323 merge_include_chains (pfile
)
326 struct search_path
*quote
, *brack
, *systm
, *qtail
;
328 struct cpp_pending
*pend
= CPP_OPTION (pfile
, pending
);
330 quote
= pend
->quote_head
;
331 brack
= pend
->brack_head
;
332 systm
= pend
->systm_head
;
333 qtail
= pend
->quote_tail
;
335 /* Paste together bracket, system, and after include chains. */
337 pend
->systm_tail
->next
= pend
->after_head
;
339 systm
= pend
->after_head
;
342 pend
->brack_tail
->next
= systm
;
346 /* This is a bit tricky. First we drop dupes from the quote-include
347 list. Then we drop dupes from the bracket-include list.
348 Finally, if qtail and brack are the same directory, we cut out
349 brack and move brack up to point to qtail.
351 We can't just merge the lists and then uniquify them because
352 then we may lose directories from the <> search path that should
353 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
354 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
355 -Ibar -I- -Ifoo -Iquux. */
357 remove_dup_dirs (pfile
, brack
);
358 qtail
= remove_dup_dirs (pfile
, quote
);
364 /* If brack == qtail, remove brack as it's simpler. */
365 if (brack
&& INO_T_EQ (qtail
->ino
, brack
->ino
)
366 && qtail
->dev
== brack
->dev
)
367 brack
= remove_dup_dir (pfile
, qtail
);
372 CPP_OPTION (pfile
, quote_include
) = quote
;
373 CPP_OPTION (pfile
, bracket_include
) = brack
;
376 /* A set of booleans indicating what CPP features each source language
382 char extended_numbers
;
384 char dollars_in_ident
;
385 char cplusplus_comments
;
389 /* ??? Enable $ in identifiers in assembly? */
390 static const struct lang_flags lang_defaults
[] =
391 { /* c99 c++ xnum std dollar c++comm digr */
392 /* GNUC89 */ { 0, 0, 1, 0, 1, 1, 1 },
393 /* GNUC99 */ { 1, 0, 1, 0, 1, 1, 1 },
394 /* STDC89 */ { 0, 0, 0, 1, 0, 0, 0 },
395 /* STDC94 */ { 0, 0, 0, 1, 0, 0, 1 },
396 /* STDC99 */ { 1, 0, 1, 1, 0, 1, 1 },
397 /* GNUCXX */ { 0, 1, 1, 0, 1, 1, 1 },
398 /* CXX98 */ { 0, 1, 1, 1, 0, 1, 1 },
399 /* ASM */ { 0, 0, 1, 0, 0, 1, 0 }
402 /* Sets internal flags correctly for a given language. */
404 cpp_set_lang (pfile
, lang
)
408 const struct lang_flags
*l
= &lang_defaults
[(int) lang
];
410 CPP_OPTION (pfile
, lang
) = lang
;
412 CPP_OPTION (pfile
, c99
) = l
->c99
;
413 CPP_OPTION (pfile
, cplusplus
) = l
->cplusplus
;
414 CPP_OPTION (pfile
, extended_numbers
) = l
->extended_numbers
;
415 CPP_OPTION (pfile
, std
) = l
->std
;
416 CPP_OPTION (pfile
, trigraphs
) = l
->std
;
417 CPP_OPTION (pfile
, dollars_in_ident
) = l
->dollars_in_ident
;
418 CPP_OPTION (pfile
, cplusplus_comments
) = l
->cplusplus_comments
;
419 CPP_OPTION (pfile
, digraphs
) = l
->digraphs
;
423 static int opt_comp
PARAMS ((const void *, const void *));
425 /* Run-time sorting of options array. */
430 return strcmp (((struct cl_option
*) p1
)->opt_text
,
431 ((struct cl_option
*) p2
)->opt_text
);
435 /* init initializes library global state. It might not need to
436 do anything depending on the platform and compiler. */
440 static int initialized
= 0;
447 /* For non-ASCII hosts, the cl_options array needs to be sorted at
449 qsort (cl_options
, N_OPTS
, sizeof (struct cl_option
), opt_comp
);
452 /* Set up the trigraph map. This doesn't need to do anything if
453 we were compiled with a compiler that supports C99 designated
455 init_trigraph_map ();
459 /* Initialize a cpp_reader structure. */
461 cpp_create_reader (lang
)
466 /* Initialise this instance of the library if it hasn't been already. */
469 pfile
= (cpp_reader
*) xcalloc (1, sizeof (cpp_reader
));
471 cpp_set_lang (pfile
, lang
);
472 CPP_OPTION (pfile
, warn_import
) = 1;
473 CPP_OPTION (pfile
, warn_multichar
) = 1;
474 CPP_OPTION (pfile
, discard_comments
) = 1;
475 CPP_OPTION (pfile
, discard_comments_in_macro_exp
) = 1;
476 CPP_OPTION (pfile
, show_column
) = 1;
477 CPP_OPTION (pfile
, tabstop
) = 8;
478 CPP_OPTION (pfile
, operator_names
) = 1;
479 CPP_OPTION (pfile
, warn_endif_labels
) = 1;
481 CPP_OPTION (pfile
, pending
) =
482 (struct cpp_pending
*) xcalloc (1, sizeof (struct cpp_pending
));
484 /* Default CPP arithmetic to something sensible for the host for the
485 benefit of dumb users like fix-header. */
486 CPP_OPTION (pfile
, precision
) = CHAR_BIT
* sizeof (long);
487 CPP_OPTION (pfile
, char_precision
) = CHAR_BIT
;
488 CPP_OPTION (pfile
, wchar_precision
) = CHAR_BIT
* sizeof (int);
489 CPP_OPTION (pfile
, int_precision
) = CHAR_BIT
* sizeof (int);
490 CPP_OPTION (pfile
, unsigned_char
) = 0;
491 CPP_OPTION (pfile
, unsigned_wchar
) = 1;
493 /* It's simplest to just create this struct whether or not it will
495 pfile
->deps
= deps_init ();
497 /* Initialise the line map. Start at logical line 1, so we can use
498 a line number of zero for special states. */
499 init_line_maps (&pfile
->line_maps
);
502 /* Initialize lexer state. */
503 pfile
->state
.save_comments
= ! CPP_OPTION (pfile
, discard_comments
);
505 /* Set up static tokens. */
506 pfile
->avoid_paste
.type
= CPP_PADDING
;
507 pfile
->avoid_paste
.val
.source
= NULL
;
508 pfile
->eof
.type
= CPP_EOF
;
509 pfile
->eof
.flags
= 0;
511 /* Create a token buffer for the lexer. */
512 _cpp_init_tokenrun (&pfile
->base_run
, 250);
513 pfile
->cur_run
= &pfile
->base_run
;
514 pfile
->cur_token
= pfile
->base_run
.base
;
516 /* Initialise the base context. */
517 pfile
->context
= &pfile
->base_context
;
518 pfile
->base_context
.macro
= 0;
519 pfile
->base_context
.prev
= pfile
->base_context
.next
= 0;
521 /* Aligned and unaligned storage. */
522 pfile
->a_buff
= _cpp_get_buff (pfile
, 0);
523 pfile
->u_buff
= _cpp_get_buff (pfile
, 0);
525 /* The expression parser stack. */
526 _cpp_expand_op_stack (pfile
);
528 /* Initialise the buffer obstack. */
529 gcc_obstack_init (&pfile
->buffer_ob
);
531 _cpp_init_includes (pfile
);
536 /* Free resources used by PFILE. Accessing PFILE after this function
537 returns leads to undefined behaviour. Returns the error count. */
542 struct search_path
*dir
, *dirn
;
543 cpp_context
*context
, *contextn
;
544 tokenrun
*run
, *runn
;
546 free_chain (CPP_OPTION (pfile
, pending
)->include_head
);
547 free (CPP_OPTION (pfile
, pending
));
548 free (pfile
->op_stack
);
550 while (CPP_BUFFER (pfile
) != NULL
)
551 _cpp_pop_buffer (pfile
);
554 free (pfile
->out
.base
);
556 if (pfile
->macro_buffer
)
558 free ((PTR
) pfile
->macro_buffer
);
559 pfile
->macro_buffer
= NULL
;
560 pfile
->macro_buffer_len
= 0;
563 deps_free (pfile
->deps
);
564 obstack_free (&pfile
->buffer_ob
, 0);
566 _cpp_destroy_hashtable (pfile
);
567 _cpp_cleanup_includes (pfile
);
569 _cpp_free_buff (pfile
->a_buff
);
570 _cpp_free_buff (pfile
->u_buff
);
571 _cpp_free_buff (pfile
->free_buffs
);
573 for (run
= &pfile
->base_run
; run
; run
= runn
)
577 if (run
!= &pfile
->base_run
)
581 for (dir
= CPP_OPTION (pfile
, quote_include
); dir
; dir
= dirn
)
584 free ((PTR
) dir
->name
);
588 for (context
= pfile
->base_context
.next
; context
; context
= contextn
)
590 contextn
= context
->next
;
594 free_line_maps (&pfile
->line_maps
);
598 /* This structure defines one built-in identifier. A node will be
599 entered in the hash table under the name NAME, with value VALUE.
601 There are two tables of these. builtin_array holds all the
602 "builtin" macros: these are handled by builtin_macro() in
603 cppmacro.c. Builtin is somewhat of a misnomer -- the property of
604 interest is that these macros require special code to compute their
605 expansions. The value is a "builtin_type" enumerator.
607 operator_array holds the C++ named operators. These are keywords
608 which act as aliases for punctuators. In C++, they cannot be
609 altered through #define, and #if recognizes them as operators. In
610 C, these are not entered into the hash table at all (but see
611 <iso646.h>). The value is a token-type enumerator. */
616 unsigned short value
;
619 #define B(n, t) { DSC(n), t }
620 static const struct builtin builtin_array
[] =
622 B("__TIME__", BT_TIME
),
623 B("__DATE__", BT_DATE
),
624 B("__FILE__", BT_FILE
),
625 B("__BASE_FILE__", BT_BASE_FILE
),
626 B("__LINE__", BT_SPECLINE
),
627 B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL
),
628 /* Keep builtins not used for -traditional-cpp at the end, and
629 update init_builtins() if any more are added. */
630 B("_Pragma", BT_PRAGMA
),
631 B("__STDC__", BT_STDC
),
634 static const struct builtin operator_array
[] =
636 B("and", CPP_AND_AND
),
637 B("and_eq", CPP_AND_EQ
),
638 B("bitand", CPP_AND
),
640 B("compl", CPP_COMPL
),
642 B("not_eq", CPP_NOT_EQ
),
644 B("or_eq", CPP_OR_EQ
),
646 B("xor_eq", CPP_XOR_EQ
)
650 /* Mark the C++ named operators in the hash table. */
652 mark_named_operators (pfile
)
655 const struct builtin
*b
;
657 for (b
= operator_array
;
658 b
< (operator_array
+ ARRAY_SIZE (operator_array
));
661 cpp_hashnode
*hp
= cpp_lookup (pfile
, b
->name
, b
->len
);
662 hp
->flags
|= NODE_OPERATOR
;
663 hp
->value
.operator = b
->value
;
667 /* Subroutine of cpp_read_main_file; reads the builtins table above and
668 enters them, and language-specific macros, into the hash table. */
670 init_builtins (pfile
)
673 const struct builtin
*b
;
674 size_t n
= ARRAY_SIZE (builtin_array
);
676 if (CPP_OPTION (pfile
, traditional
))
679 for(b
= builtin_array
; b
< builtin_array
+ n
; b
++)
681 cpp_hashnode
*hp
= cpp_lookup (pfile
, b
->name
, b
->len
);
683 hp
->flags
|= NODE_BUILTIN
| NODE_WARN
;
684 hp
->value
.builtin
= b
->value
;
687 if (CPP_OPTION (pfile
, cplusplus
))
688 _cpp_define_builtin (pfile
, "__cplusplus 1");
689 else if (CPP_OPTION (pfile
, lang
) == CLK_ASM
)
690 _cpp_define_builtin (pfile
, "__ASSEMBLER__ 1");
691 else if (CPP_OPTION (pfile
, lang
) == CLK_STDC94
)
692 _cpp_define_builtin (pfile
, "__STDC_VERSION__ 199409L");
693 else if (CPP_OPTION (pfile
, c99
))
694 _cpp_define_builtin (pfile
, "__STDC_VERSION__ 199901L");
696 if (CPP_OPTION (pfile
, objc
))
697 _cpp_define_builtin (pfile
, "__OBJC__ 1");
699 if (pfile
->cb
.register_builtins
)
700 (*pfile
->cb
.register_builtins
) (pfile
);
703 /* And another subroutine. This one sets up the standard include path. */
705 init_standard_includes (pfile
)
709 const struct default_include
*p
;
710 const char *specd_prefix
= CPP_OPTION (pfile
, include_prefix
);
712 /* Several environment variables may add to the include search path.
713 CPATH specifies an additional list of directories to be searched
714 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
715 etc. specify an additional list of directories to be searched as
716 if specified with -isystem, for the language indicated. */
718 GET_ENVIRONMENT (path
, "CPATH");
719 if (path
!= 0 && *path
!= 0)
720 path_include (pfile
, path
, BRACKET
);
722 switch ((CPP_OPTION (pfile
, objc
) << 1) + CPP_OPTION (pfile
, cplusplus
))
725 GET_ENVIRONMENT (path
, "C_INCLUDE_PATH");
728 GET_ENVIRONMENT (path
, "CPLUS_INCLUDE_PATH");
731 GET_ENVIRONMENT (path
, "OBJC_INCLUDE_PATH");
734 GET_ENVIRONMENT (path
, "OBJCPLUS_INCLUDE_PATH");
737 if (path
!= 0 && *path
!= 0)
738 path_include (pfile
, path
, SYSTEM
);
740 /* Search "translated" versions of GNU directories.
741 These have /usr/local/lib/gcc... replaced by specd_prefix. */
742 if (specd_prefix
!= 0 && cpp_GCC_INCLUDE_DIR_len
)
744 /* Remove the `include' from /usr/local/lib/gcc.../include.
745 GCC_INCLUDE_DIR will always end in /include. */
746 int default_len
= cpp_GCC_INCLUDE_DIR_len
;
747 char *default_prefix
= (char *) alloca (default_len
+ 1);
748 int specd_len
= strlen (specd_prefix
);
750 memcpy (default_prefix
, cpp_GCC_INCLUDE_DIR
, default_len
);
751 default_prefix
[default_len
] = '\0';
753 for (p
= cpp_include_defaults
; p
->fname
; p
++)
755 /* Some standard dirs are only for C++. */
757 || (CPP_OPTION (pfile
, cplusplus
)
758 && !CPP_OPTION (pfile
, no_standard_cplusplus_includes
)))
760 /* Does this dir start with the prefix? */
761 if (!memcmp (p
->fname
, default_prefix
, default_len
))
763 /* Yes; change prefix and add to search list. */
764 int flen
= strlen (p
->fname
);
765 int this_len
= specd_len
+ flen
- default_len
;
766 char *str
= (char *) xmalloc (this_len
+ 1);
767 memcpy (str
, specd_prefix
, specd_len
);
768 memcpy (str
+ specd_len
,
769 p
->fname
+ default_len
,
770 flen
- default_len
+ 1);
772 append_include_chain (pfile
, str
, SYSTEM
, p
->cxx_aware
);
778 /* Search ordinary names for GNU include directories. */
779 for (p
= cpp_include_defaults
; p
->fname
; p
++)
781 /* Some standard dirs are only for C++. */
783 || (CPP_OPTION (pfile
, cplusplus
)
784 && !CPP_OPTION (pfile
, no_standard_cplusplus_includes
)))
786 char *str
= update_path (p
->fname
, p
->component
);
787 append_include_chain (pfile
, str
, SYSTEM
, p
->cxx_aware
);
792 /* Pushes a command line -imacro and -include file indicated by P onto
793 the buffer stack. Returns non-zero if successful. */
795 push_include (pfile
, p
)
797 struct pending_option
*p
;
801 /* Later: maybe update this to use the #include "" search path
802 if cpp_read_file fails. */
803 header
.type
= CPP_STRING
;
804 header
.val
.str
.text
= (const unsigned char *) p
->arg
;
805 header
.val
.str
.len
= strlen (p
->arg
);
806 /* Make the command line directive take up a line. */
809 return _cpp_execute_include (pfile
, &header
, IT_CMDLINE
);
812 /* Frees a pending_option chain. */
815 struct pending_option
*head
;
817 struct pending_option
*next
;
827 /* Sanity-checks are dependent on command-line options, so it is
828 called as a subroutine of cpp_read_main_file (). */
830 static void sanity_checks
PARAMS ((cpp_reader
*));
831 static void sanity_checks (pfile
)
835 size_t max_precision
= 2 * CHAR_BIT
* sizeof (cpp_num_part
);
837 /* Sanity checks for assumptions about CPP arithmetic and target
838 type precisions made by cpplib. */
841 cpp_error (pfile
, DL_ICE
, "cppchar_t must be an unsigned type");
843 if (CPP_OPTION (pfile
, precision
) > max_precision
)
844 cpp_error (pfile
, DL_ICE
,
845 "preprocessor arithmetic has maximum precision of %lu bits; target requires %lu bits",
846 (unsigned long) max_precision
,
847 (unsigned long) CPP_OPTION (pfile
, precision
));
849 if (CPP_OPTION (pfile
, precision
) < CPP_OPTION (pfile
, int_precision
))
850 cpp_error (pfile
, DL_ICE
,
851 "CPP arithmetic must be at least as precise as a target int");
853 if (CPP_OPTION (pfile
, char_precision
) < 8)
854 cpp_error (pfile
, DL_ICE
, "target char is less than 8 bits wide");
856 if (CPP_OPTION (pfile
, wchar_precision
) < CPP_OPTION (pfile
, char_precision
))
857 cpp_error (pfile
, DL_ICE
,
858 "target wchar_t is narrower than target char");
860 if (CPP_OPTION (pfile
, int_precision
) < CPP_OPTION (pfile
, char_precision
))
861 cpp_error (pfile
, DL_ICE
,
862 "target int is narrower than target char");
864 /* This is assumed in eval_token() and could be fixed if necessary. */
865 if (sizeof (cppchar_t
) > sizeof (cpp_num_part
))
866 cpp_error (pfile
, DL_ICE
, "CPP half-integer narrower than CPP character");
868 if (CPP_OPTION (pfile
, wchar_precision
) > BITS_PER_CPPCHAR_T
)
869 cpp_error (pfile
, DL_ICE
,
870 "CPP on this host cannot handle wide character constants over %lu bits, but the target requires %lu bits",
871 (unsigned long) BITS_PER_CPPCHAR_T
,
872 (unsigned long) CPP_OPTION (pfile
, wchar_precision
));
875 # define sanity_checks(PFILE)
878 /* Add a dependency target. Can be called any number of times before
879 cpp_read_main_file(). If no targets have been added before
880 cpp_read_main_file(), then the default target is used. */
882 cpp_add_dependency_target (pfile
, target
, quote
)
888 pfile
->deps
= deps_init ();
890 deps_add_target (pfile
->deps
, target
, quote
);
893 /* This is called after options have been parsed, and partially
894 processed. Setup for processing input from the file named FNAME,
895 or stdin if it is the empty string. Return the original filename
896 on success (e.g. foo.i->foo.c), or NULL on failure. */
898 cpp_read_main_file (pfile
, fname
, table
)
903 sanity_checks (pfile
);
905 /* The front ends don't set up the hash table until they have
906 finished processing the command line options, so initializing the
907 hashtable is deferred until now. */
908 _cpp_init_hashtable (pfile
, table
);
910 /* Set up the include search path now. */
911 if (! CPP_OPTION (pfile
, no_standard_includes
))
912 init_standard_includes (pfile
);
914 merge_include_chains (pfile
);
916 /* With -v, print the list of dirs to search. */
917 if (CPP_OPTION (pfile
, verbose
))
919 struct search_path
*l
;
920 fprintf (stderr
, _("#include \"...\" search starts here:\n"));
921 for (l
= CPP_OPTION (pfile
, quote_include
); l
; l
= l
->next
)
923 if (l
== CPP_OPTION (pfile
, bracket_include
))
924 fprintf (stderr
, _("#include <...> search starts here:\n"));
925 fprintf (stderr
, " %s\n", l
->name
);
927 fprintf (stderr
, _("End of search list.\n"));
930 if (CPP_OPTION (pfile
, print_deps
))
931 /* Set the default target (if there is none already). */
932 deps_add_default_target (pfile
->deps
, fname
);
934 /* Open the main input file. */
935 if (!_cpp_read_file (pfile
, fname
))
938 /* Set this after cpp_post_options so the client can change the
939 option if it wishes, and after stacking the main file so we don't
940 trace the main file. */
941 pfile
->line_maps
.trace_includes
= CPP_OPTION (pfile
, print_include_names
);
943 /* For foo.i, read the original filename foo.c now, for the benefit
944 of the front ends. */
945 if (CPP_OPTION (pfile
, preprocessed
))
946 read_original_filename (pfile
);
948 return pfile
->map
->to_file
;
951 /* For preprocessed files, if the first tokens are of the form # NUM.
952 handle the directive so we know the original file name. This will
953 generate file_change callbacks, which the front ends must handle
954 appropriately given their state of initialization. */
956 read_original_filename (pfile
)
959 const cpp_token
*token
, *token1
;
961 /* Lex ahead; if the first tokens are of the form # NUM, then
962 process the directive, otherwise back up. */
963 token
= _cpp_lex_direct (pfile
);
964 if (token
->type
== CPP_HASH
)
966 token1
= _cpp_lex_direct (pfile
);
967 _cpp_backup_tokens (pfile
, 1);
969 /* If it's a #line directive, handle it. */
970 if (token1
->type
== CPP_NUMBER
)
972 _cpp_handle_directive (pfile
, token
->flags
& PREV_WHITE
);
977 /* Backup as if nothing happened. */
978 _cpp_backup_tokens (pfile
, 1);
981 /* Handle pending command line options: -D, -U, -A, -imacros and
982 -include. This should be called after debugging has been properly
983 set up in the front ends. */
985 cpp_finish_options (pfile
)
988 /* Mark named operators before handling command line macros. */
989 if (CPP_OPTION (pfile
, cplusplus
) && CPP_OPTION (pfile
, operator_names
))
990 mark_named_operators (pfile
);
992 /* Install builtins and process command line macros etc. in the order
993 they appeared, but only if not already preprocessed. */
994 if (! CPP_OPTION (pfile
, preprocessed
))
996 struct pending_option
*p
;
998 _cpp_do_file_change (pfile
, LC_RENAME
, _("<built-in>"), 1, 0);
999 init_builtins (pfile
);
1000 _cpp_do_file_change (pfile
, LC_RENAME
, _("<command line>"), 1, 0);
1001 for (p
= CPP_OPTION (pfile
, pending
)->directive_head
; p
; p
= p
->next
)
1002 (*p
->handler
) (pfile
, p
->arg
);
1004 /* Scan -imacros files after -D, -U, but before -include.
1005 pfile->next_include_file is NULL, so _cpp_pop_buffer does not
1006 push -include files. */
1007 for (p
= CPP_OPTION (pfile
, pending
)->imacros_head
; p
; p
= p
->next
)
1008 if (push_include (pfile
, p
))
1009 cpp_scan_nooutput (pfile
);
1011 pfile
->next_include_file
= &CPP_OPTION (pfile
, pending
)->include_head
;
1012 _cpp_maybe_push_include_file (pfile
);
1015 pfile
->first_unused_line
= pfile
->line
;
1017 free_chain (CPP_OPTION (pfile
, pending
)->imacros_head
);
1018 free_chain (CPP_OPTION (pfile
, pending
)->directive_head
);
1021 /* Push the next buffer on the stack given by -include, if any. */
1023 _cpp_maybe_push_include_file (pfile
)
1026 if (pfile
->next_include_file
)
1028 struct pending_option
*head
= *pfile
->next_include_file
;
1030 while (head
&& !push_include (pfile
, head
))
1034 pfile
->next_include_file
= &head
->next
;
1037 /* All done; restore the line map from <command line>. */
1038 _cpp_do_file_change (pfile
, LC_RENAME
,
1039 pfile
->line_maps
.maps
[0].to_file
, 1, 0);
1040 /* Don't come back here again. */
1041 pfile
->next_include_file
= NULL
;
1046 /* This is called at the end of preprocessing. It pops the last
1047 buffer and writes dependency output, and returns the number of
1050 Maybe it should also reset state, such that you could call
1051 cpp_start_read with a new filename to restart processing. */
1053 cpp_finish (pfile
, deps_stream
)
1057 /* Warn about unused macros before popping the final buffer. */
1058 if (CPP_OPTION (pfile
, warn_unused_macros
))
1059 cpp_forall_identifiers (pfile
, _cpp_warn_if_unused_macro
, NULL
);
1061 /* cpplex.c leaves the final buffer on the stack. This it so that
1062 it returns an unending stream of CPP_EOFs to the client. If we
1063 popped the buffer, we'd dereference a NULL buffer pointer and
1064 segfault. It's nice to allow the client to do worry-free excess
1065 cpp_get_token calls. */
1066 while (pfile
->buffer
)
1067 _cpp_pop_buffer (pfile
);
1069 /* Don't write the deps file if there are errors. */
1070 if (deps_stream
&& CPP_OPTION (pfile
, print_deps
) && !pfile
->errors
)
1072 deps_write (pfile
->deps
, deps_stream
, 72);
1074 if (CPP_OPTION (pfile
, deps_phony_targets
))
1075 deps_phony_targets (pfile
->deps
, deps_stream
);
1078 /* Report on headers that could use multiple include guards. */
1079 if (CPP_OPTION (pfile
, print_include_names
))
1080 _cpp_report_missing_guards (pfile
);
1082 return pfile
->errors
;
1085 /* Add a directive to be handled later in the initialization phase. */
1087 new_pending_directive (pend
, text
, handler
)
1088 struct cpp_pending
*pend
;
1090 cl_directive_handler handler
;
1092 struct pending_option
*o
= (struct pending_option
*)
1093 xmalloc (sizeof (struct pending_option
));
1097 o
->handler
= handler
;
1098 APPEND (pend
, directive
, o
);
1101 /* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
1102 I.e. a const string initializer with parens around it. That is
1103 what N_("string") resolves to, so we make no_* be macros instead. */
1104 #define no_ass N_("assertion missing after %s")
1105 #define no_dir N_("directory name missing after %s")
1106 #define no_fil N_("file name missing after %s")
1107 #define no_mac N_("macro name missing after %s")
1108 #define no_pth N_("path name missing after %s")
1109 #define no_tgt N_("target missing after %s")
1111 /* This is the list of all command line options, with the leading
1112 "-" removed. It must be sorted in ASCII collating order. */
1113 #define COMMAND_LINE_OPTIONS \
1114 DEF_OPT("A", no_ass, OPT_A) \
1115 DEF_OPT("D", no_mac, OPT_D) \
1116 DEF_OPT("I", no_dir, OPT_I) \
1117 DEF_OPT("M", 0, OPT_M) \
1118 DEF_OPT("MD", no_fil, OPT_MD) \
1119 DEF_OPT("MF", no_fil, OPT_MF) \
1120 DEF_OPT("MG", 0, OPT_MG) \
1121 DEF_OPT("MM", 0, OPT_MM) \
1122 DEF_OPT("MMD", no_fil, OPT_MMD) \
1123 DEF_OPT("MP", 0, OPT_MP) \
1124 DEF_OPT("MQ", no_tgt, OPT_MQ) \
1125 DEF_OPT("MT", no_tgt, OPT_MT) \
1126 DEF_OPT("U", no_mac, OPT_U) \
1127 DEF_OPT("idirafter", no_dir, OPT_idirafter) \
1128 DEF_OPT("imacros", no_fil, OPT_imacros) \
1129 DEF_OPT("include", no_fil, OPT_include) \
1130 DEF_OPT("iprefix", no_pth, OPT_iprefix) \
1131 DEF_OPT("isystem", no_dir, OPT_isystem) \
1132 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix) \
1133 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore)
1135 #define DEF_OPT(text, msg, code) code,
1138 COMMAND_LINE_OPTIONS
1145 const char *opt_text
;
1148 enum opt_code opt_code
;
1151 #define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
1153 static struct cl_option cl_options
[] =
1155 static const struct cl_option cl_options
[] =
1158 COMMAND_LINE_OPTIONS
1161 #undef COMMAND_LINE_OPTIONS
1163 /* Perform a binary search to find which, if any, option the given
1164 command-line matches. Returns its index in the option array,
1165 negative on failure. Complications arise since some options can be
1166 suffixed with an argument, and multiple complete matches can occur,
1167 e.g. -pedantic and -pedantic-errors. */
1169 parse_option (input
)
1172 unsigned int md
, mn
, mx
;
1183 opt_len
= cl_options
[md
].opt_len
;
1184 comp
= memcmp (input
, cl_options
[md
].opt_text
, opt_len
);
1192 if (input
[opt_len
] == '\0')
1194 /* We were passed more text. If the option takes an argument,
1195 we may match a later option or we may have been passed the
1196 argument. The longest possible option match succeeds.
1197 If the option takes no arguments we have not matched and
1198 continue the search (e.g. input="stdc++" match was "stdc"). */
1200 if (cl_options
[md
].msg
)
1202 /* Scan forwards. If we get an exact match, return it.
1203 Otherwise, return the longest option-accepting match.
1204 This loops no more than twice with current options. */
1206 for (; mn
< (unsigned int) N_OPTS
; mn
++)
1208 opt_len
= cl_options
[mn
].opt_len
;
1209 if (memcmp (input
, cl_options
[mn
].opt_text
, opt_len
))
1211 if (input
[opt_len
] == '\0')
1213 if (cl_options
[mn
].msg
)
1224 /* Handle one command-line option in (argc, argv).
1225 Can be called multiple times, to handle multiple sets of options.
1226 Returns number of strings consumed. */
1228 cpp_handle_option (pfile
, argc
, argv
)
1234 struct cpp_pending
*pend
= CPP_OPTION (pfile
, pending
);
1237 enum opt_code opt_code
;
1239 const char *arg
= 0;
1241 /* Skip over '-'. */
1242 opt_index
= parse_option (&argv
[i
][1]);
1246 opt_code
= cl_options
[opt_index
].opt_code
;
1247 if (cl_options
[opt_index
].msg
)
1249 arg
= &argv
[i
][cl_options
[opt_index
].opt_len
+ 1];
1255 cpp_error (pfile
, DL_ERROR
,
1256 cl_options
[opt_index
].msg
, argv
[i
- 1]);
1264 case N_OPTS
: /* Shut GCC up. */
1268 new_pending_directive (pend
, arg
, cpp_define
);
1271 CPP_OPTION (pfile
, include_prefix
) = arg
;
1272 CPP_OPTION (pfile
, include_prefix_len
) = strlen (arg
);
1276 CPP_OPTION (pfile
, print_deps_missing_files
) = 1;
1279 /* When doing dependencies with -M or -MM, suppress normal
1280 preprocessed output, but still do -dM etc. as software
1281 depends on this. Preprocessed output occurs if -MD, -MMD
1282 or environment var dependency generation is used. */
1283 CPP_OPTION (pfile
, print_deps
) = 2;
1284 CPP_OPTION (pfile
, no_output
) = 1;
1285 CPP_OPTION (pfile
, inhibit_warnings
) = 1;
1288 CPP_OPTION (pfile
, print_deps
) = 1;
1289 CPP_OPTION (pfile
, no_output
) = 1;
1290 CPP_OPTION (pfile
, inhibit_warnings
) = 1;
1293 CPP_OPTION (pfile
, deps_file
) = arg
;
1296 CPP_OPTION (pfile
, deps_phony_targets
) = 1;
1300 /* Add a target. -MQ quotes for Make. */
1301 deps_add_target (pfile
->deps
, arg
, opt_code
== OPT_MQ
);
1305 CPP_OPTION (pfile
, print_deps
) = 2;
1306 CPP_OPTION (pfile
, deps_file
) = arg
;
1309 CPP_OPTION (pfile
, print_deps
) = 1;
1310 CPP_OPTION (pfile
, deps_file
) = arg
;
1316 /* -A with an argument beginning with '-' acts as
1317 #unassert on whatever immediately follows the '-'.
1318 If "-" is the whole argument, we eliminate all
1319 predefined macros and assertions, including those
1320 that were specified earlier on the command line.
1321 That way we can get rid of any that were passed
1322 automatically in from GCC. */
1326 free_chain (pend
->directive_head
);
1327 pend
->directive_head
= NULL
;
1328 pend
->directive_tail
= NULL
;
1331 new_pending_directive (pend
, arg
+ 1, cpp_unassert
);
1334 new_pending_directive (pend
, arg
, cpp_assert
);
1337 new_pending_directive (pend
, arg
, cpp_undef
);
1339 case OPT_I
: /* Add directory to path for includes. */
1340 if (!strcmp (arg
, "-"))
1343 Use the preceding -I directories for #include "..."
1344 but not #include <...>.
1345 Don't search the directory of the present file
1346 for #include "...". (Note that -I. -I- is not the same as
1347 the default setup; -I. uses the compiler's working dir.) */
1348 if (! CPP_OPTION (pfile
, ignore_srcdir
))
1350 pend
->quote_head
= pend
->brack_head
;
1351 pend
->quote_tail
= pend
->brack_tail
;
1352 pend
->brack_head
= 0;
1353 pend
->brack_tail
= 0;
1354 CPP_OPTION (pfile
, ignore_srcdir
) = 1;
1358 cpp_error (pfile
, DL_ERROR
, "-I- specified twice");
1363 append_include_chain (pfile
, xstrdup (arg
), BRACKET
, 0);
1366 /* Add directory to beginning of system include path, as a system
1367 include directory. */
1368 append_include_chain (pfile
, xstrdup (arg
), SYSTEM
, 0);
1373 struct pending_option
*o
= (struct pending_option
*)
1374 xmalloc (sizeof (struct pending_option
));
1378 if (opt_code
== OPT_include
)
1379 APPEND (pend
, include
, o
);
1381 APPEND (pend
, imacros
, o
);
1384 case OPT_iwithprefix
:
1385 /* Add directory to end of path for includes,
1386 with the default prefix at the front of its name. */
1388 case OPT_iwithprefixbefore
:
1389 /* Add directory to main path for includes,
1390 with the default prefix at the front of its name. */
1397 if (CPP_OPTION (pfile
, include_prefix
) != 0)
1399 size_t ipl
= CPP_OPTION (pfile
, include_prefix_len
);
1400 fname
= xmalloc (ipl
+ len
+ 1);
1401 memcpy (fname
, CPP_OPTION (pfile
, include_prefix
), ipl
);
1402 memcpy (fname
+ ipl
, arg
, len
+ 1);
1404 else if (cpp_GCC_INCLUDE_DIR_len
)
1406 fname
= xmalloc (cpp_GCC_INCLUDE_DIR_len
+ len
+ 1);
1407 memcpy (fname
, cpp_GCC_INCLUDE_DIR
, cpp_GCC_INCLUDE_DIR_len
);
1408 memcpy (fname
+ cpp_GCC_INCLUDE_DIR_len
, arg
, len
+ 1);
1411 fname
= xstrdup (arg
);
1413 append_include_chain (pfile
, fname
,
1414 opt_code
== OPT_iwithprefix
? SYSTEM
: BRACKET
, 0);
1418 /* Add directory to end of path for includes. */
1419 append_include_chain (pfile
, xstrdup (arg
), AFTER
, 0);
1426 /* Handle command-line options in (argc, argv).
1427 Can be called multiple times, to handle multiple sets of options.
1428 Returns if an unrecognized option is seen.
1429 Returns number of strings consumed. */
1431 cpp_handle_options (pfile
, argc
, argv
)
1437 int strings_processed
;
1439 for (i
= 0; i
< argc
; i
+= strings_processed
)
1441 strings_processed
= cpp_handle_option (pfile
, argc
- i
, argv
+ i
);
1442 if (strings_processed
== 0)
1449 /* Extra processing when all options are parsed, after all calls to
1450 cpp_handle_option[s]. Consistency checks etc. */
1452 cpp_post_options (pfile
)
1455 /* Canonicalize in_fname and out_fname. We guarantee they are not
1456 NULL, and that the empty string represents stdin / stdout. */
1457 if (CPP_OPTION (pfile
, in_fname
) == NULL
1458 || !strcmp (CPP_OPTION (pfile
, in_fname
), "-"))
1459 CPP_OPTION (pfile
, in_fname
) = "";
1461 /* -Wtraditional is not useful in C++ mode. */
1462 if (CPP_OPTION (pfile
, cplusplus
))
1463 CPP_OPTION (pfile
, warn_traditional
) = 0;
1465 /* The compiler front ends override this, but I think this is the
1466 appropriate setting for the library. */
1467 CPP_OPTION (pfile
, warn_long_long
)
1468 = ((CPP_OPTION (pfile
, pedantic
) && !CPP_OPTION (pfile
, c99
))
1469 || CPP_OPTION (pfile
, warn_traditional
));
1471 /* Permanently disable macro expansion if we are rescanning
1472 preprocessed text. Read preprocesed source in ISO mode. */
1473 if (CPP_OPTION (pfile
, preprocessed
))
1475 pfile
->state
.prevent_expansion
= 1;
1476 CPP_OPTION (pfile
, traditional
) = 0;
1479 /* Traditional CPP does not accurately track column information. */
1480 if (CPP_OPTION (pfile
, traditional
))
1481 CPP_OPTION (pfile
, show_column
) = 0;
1483 /* -dM and dependencies suppress normal output; do it here so that
1484 the last -d[MDN] switch overrides earlier ones. */
1485 if (CPP_OPTION (pfile
, dump_macros
) == dump_only
)
1486 CPP_OPTION (pfile
, no_output
) = 1;
1488 /* Disable -dD, -dN and -dI if normal output is suppressed. Allow
1489 -dM since at least glibc relies on -M -dM to work. */
1490 if (CPP_OPTION (pfile
, no_output
))
1492 if (CPP_OPTION (pfile
, dump_macros
) != dump_only
)
1493 CPP_OPTION (pfile
, dump_macros
) = dump_none
;
1494 CPP_OPTION (pfile
, dump_includes
) = 0;