2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 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. */
33 /* Predefined symbols, built-in macros, and the default include path. */
35 #ifndef GET_ENV_PATH_LIST
36 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
39 /* Windows does not natively support inodes, and neither does MSDOS.
40 Cygwin's emulation can generate non-unique inodes, so don't use it.
41 VMS has non-numeric inodes. */
43 #define INO_T_EQ(a, b) (!memcmp (&(a), &(b), sizeof (a)))
44 #elif (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
45 #define INO_T_EQ(a, b) 0
47 #define INO_T_EQ(a, b) ((a) == (b))
50 #ifndef STANDARD_INCLUDE_DIR
51 #define STANDARD_INCLUDE_DIR "/usr/include"
54 /* We let tm.h override the types used here, to handle trivial differences
55 such as the choice of unsigned int or long unsigned int for size_t.
56 When machines start needing nontrivial differences in the size type,
57 it would be best to do something here to figure out automatically
58 from other information what type to use. */
60 /* The string value for __SIZE_TYPE__. */
63 #define SIZE_TYPE "long unsigned int"
66 /* The string value for __PTRDIFF_TYPE__. */
69 #define PTRDIFF_TYPE "long int"
72 /* The string value for __WCHAR_TYPE__. */
75 #define WCHAR_TYPE "int"
78 /* The string value for __USER_LABEL_PREFIX__ */
80 #ifndef USER_LABEL_PREFIX
81 #define USER_LABEL_PREFIX ""
84 /* The string value for __REGISTER_PREFIX__ */
86 #ifndef REGISTER_PREFIX
87 #define REGISTER_PREFIX ""
90 /* This is the default list of directories to search for include files.
91 It may be overridden by the various -I and -ixxx options.
93 #include "file" looks in the same directory as the current file,
95 #include <file> just looks in this list.
97 All these directories are treated as `system' include directories
98 (they are not subject to pedantic warnings in some cases). */
100 struct default_include
102 const char *fname
; /* The name of the directory. */
103 const char *component
; /* The component containing the directory
104 (see update_path in prefix.c) */
105 int cplusplus
; /* Only look here if we're compiling C++. */
106 int cxx_aware
; /* Includes in this directory don't need to
107 be wrapped in extern "C" when compiling
111 #ifndef STANDARD_INCLUDE_COMPONENT
112 #define STANDARD_INCLUDE_COMPONENT 0
116 #undef LOCAL_INCLUDE_DIR
117 #undef SYSTEM_INCLUDE_DIR
118 #undef STANDARD_INCLUDE_DIR
120 #undef CROSS_INCLUDE_DIR
123 static const struct default_include include_defaults_array
[]
124 #ifdef INCLUDE_DEFAULTS
128 #ifdef GPLUSPLUS_INCLUDE_DIR
129 /* Pick up GNU C++ specific include files. */
130 { GPLUSPLUS_INCLUDE_DIR
, "G++", 1, 1 },
132 #ifdef LOCAL_INCLUDE_DIR
133 /* /usr/local/include comes before the fixincluded header files. */
134 { LOCAL_INCLUDE_DIR
, 0, 0, 1 },
136 #ifdef GCC_INCLUDE_DIR
137 /* This is the dir for fixincludes and for gcc's private headers. */
138 { GCC_INCLUDE_DIR
, "GCC", 0, 0 },
140 #ifdef CROSS_INCLUDE_DIR
141 /* One place the target system's headers might be. */
142 { CROSS_INCLUDE_DIR
, "GCC", 0, 0 },
144 #ifdef TOOL_INCLUDE_DIR
145 /* Another place the target system's headers might be. */
146 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 1 },
148 #ifdef SYSTEM_INCLUDE_DIR
149 /* Some systems have an extra dir of include files. */
150 { SYSTEM_INCLUDE_DIR
, 0, 0, 0 },
152 #ifdef STANDARD_INCLUDE_DIR
153 /* /usr/include comes dead last. */
154 { STANDARD_INCLUDE_DIR
, STANDARD_INCLUDE_COMPONENT
, 0, 0 },
158 #endif /* no INCLUDE_DEFAULTS */
160 /* Internal structures and prototypes. */
162 /* A `struct pending_option' remembers one -D, -A, -U, -include, or -imacros
163 switch. There are four lists: one for -D and -U, one for -A, one
164 for -include, one for -imacros. `undef' is set for -U, clear for
165 -D, ignored for the others.
166 (Future: add an equivalent of -U for -A) */
168 typedef void (* cl_directive_handler
) PARAMS ((cpp_reader
*, const char *));
169 struct pending_option
171 struct pending_option
*next
;
173 cl_directive_handler handler
;
176 /* The `pending' structure accumulates all the options that are not
177 actually processed until we hit cpp_start_read. It consists of
178 several lists, one for each type of option. We keep both head and
179 tail pointers for quick insertion. */
182 struct pending_option
*directive_head
, *directive_tail
;
184 struct file_name_list
*quote_head
, *quote_tail
;
185 struct file_name_list
*brack_head
, *brack_tail
;
186 struct file_name_list
*systm_head
, *systm_tail
;
187 struct file_name_list
*after_head
, *after_tail
;
189 struct pending_option
*imacros_head
, *imacros_tail
;
190 struct pending_option
*include_head
, *include_tail
;
194 #define APPEND(pend, list, elt) \
195 do { if (!(pend)->list##_head) (pend)->list##_head = (elt); \
196 else (pend)->list##_tail->next = (elt); \
197 (pend)->list##_tail = (elt); \
200 #define APPEND(pend, list, elt) \
201 do { if (!(pend)->list/**/_head) (pend)->list/**/_head = (elt); \
202 else (pend)->list/**/_tail->next = (elt); \
203 (pend)->list/**/_tail = (elt); \
207 static void print_help
PARAMS ((void));
208 static void path_include
PARAMS ((cpp_reader
*,
209 struct cpp_pending
*,
211 static void initialize_builtins
PARAMS ((cpp_reader
*));
212 static void append_include_chain
PARAMS ((cpp_reader
*,
213 struct cpp_pending
*,
215 static void merge_include_chains
PARAMS ((cpp_reader
*));
217 static void dump_special_to_buffer
PARAMS ((cpp_reader
*, const char *));
218 static void initialize_dependency_output
PARAMS ((cpp_reader
*));
219 static void initialize_standard_includes
PARAMS ((cpp_reader
*));
220 static void new_pending_directive
PARAMS ((struct cpp_pending
*,
222 cl_directive_handler
));
224 static int opt_comp
PARAMS ((const void *, const void *));
226 static int parse_option
PARAMS ((const char *));
227 static int handle_option
PARAMS ((cpp_reader
*, int, char **));
229 /* Fourth argument to append_include_chain: chain to use */
230 enum { QUOTE
= 0, BRACKET
, SYSTEM
, AFTER
};
232 /* If we have designated initializers (GCC >2.7, or C99) this table
233 can be initialized, constant data. Otherwise, it has to be filled
236 #if (GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L)
237 #define init_IStable() /* nothing */
238 #define ISTABLE const unsigned char _cpp_IStable[256] = {
240 #define s(p, v) [p] = v,
242 #define ISTABLE unsigned char _cpp_IStable[256] = { 0 }; \
243 static void init_IStable PARAMS ((void)) { \
244 unsigned char *x = _cpp_IStable;
246 #define s(p, v) x[p] = v;
249 #define A(x) s(x, ISidnum|ISidstart)
250 #define N(x) s(x, ISidnum|ISnumstart)
251 #define H(x) s(x, IShspace|ISspace)
252 #define S(x) s(x, ISspace)
257 A('a') A('b') A('c') A('d') A('e') A('f') A('g') A('h') A('i')
258 A('j') A('k') A('l') A('m') A('n') A('o') A('p') A('q') A('r')
259 A('s') A('t') A('u') A('v') A('w') A('x') A('y') A('z')
261 A('A') A('B') A('C') A('D') A('E') A('F') A('G') A('H') A('I')
262 A('J') A('K') A('L') A('M') A('N') A('O') A('P') A('Q') A('R')
263 A('S') A('T') A('U') A('V') A('W') A('X') A('Y') A('Z')
265 N('1') N('2') N('3') N('4') N('5') N('6') N('7') N('8') N('9') N('0')
267 H('\0') H(' ') H('\t') H('\v') H('\f')
280 /* Given a colon-separated list of file names PATH,
281 add all the names to the search path for include files. */
284 path_include (pfile
, pend
, list
, path
)
286 struct cpp_pending
*pend
;
296 /* Find the end of this name. */
298 while (*q
!= 0 && *q
!= PATH_SEPARATOR
) q
++;
301 /* An empty name in the path stands for the current directory. */
302 name
= (char *) xmalloc (2);
308 /* Otherwise use the directory that is named. */
309 name
= (char *) xmalloc (q
- p
+ 1);
310 memcpy (name
, p
, q
- p
);
314 append_include_chain (pfile
, pend
, name
, path
, 0);
316 /* Advance past this name. */
324 /* Append DIR to include path PATH. DIR must be permanently allocated
327 append_include_chain (pfile
, pend
, dir
, path
, cxx_aware
)
329 struct cpp_pending
*pend
;
334 struct file_name_list
*new;
338 _cpp_simplify_pathname (dir
);
341 /* Dirs that don't exist are silently ignored. */
343 cpp_notice_from_errno (pfile
, dir
);
344 else if (CPP_OPTION (pfile
, verbose
))
345 fprintf (stderr
, _("ignoring nonexistent directory `%s'\n"), dir
);
349 if (!S_ISDIR (st
.st_mode
))
351 cpp_notice (pfile
, "%s: Not a directory", dir
);
356 if (len
> pfile
->max_include_len
)
357 pfile
->max_include_len
= len
;
359 new = (struct file_name_list
*) xmalloc (sizeof (struct file_name_list
));
362 new->ino
= st
.st_ino
;
363 new->dev
= st
.st_dev
;
365 new->sysp
= cxx_aware
? 1 : 2;
368 new->name_map
= NULL
;
374 case QUOTE
: APPEND (pend
, quote
, new); break;
375 case BRACKET
: APPEND (pend
, brack
, new); break;
376 case SYSTEM
: APPEND (pend
, systm
, new); break;
377 case AFTER
: APPEND (pend
, after
, new); break;
381 /* Merge the four include chains together in the order quote, bracket,
382 system, after. Remove duplicate dirs (as determined by
383 INO_T_EQ()). The system_include and after_include chains are never
384 referred to again after this function; all access is through the
385 bracket_include path.
387 For the future: Check if the directory is empty (but
388 how?) and possibly preload the include hash. */
391 merge_include_chains (pfile
)
394 struct file_name_list
*prev
, *cur
, *other
;
395 struct file_name_list
*quote
, *brack
, *systm
, *after
;
396 struct file_name_list
*qtail
, *btail
, *stail
, *atail
;
398 struct cpp_pending
*pend
= CPP_OPTION (pfile
, pending
);
400 qtail
= pend
->quote_tail
;
401 btail
= pend
->brack_tail
;
402 stail
= pend
->systm_tail
;
403 atail
= pend
->after_tail
;
405 quote
= pend
->quote_head
;
406 brack
= pend
->brack_head
;
407 systm
= pend
->systm_head
;
408 after
= pend
->after_head
;
410 /* Paste together bracket, system, and after include chains. */
420 /* This is a bit tricky.
421 First we drop dupes from the quote-include list.
422 Then we drop dupes from the bracket-include list.
423 Finally, if qtail and brack are the same directory,
426 We can't just merge the lists and then uniquify them because
427 then we may lose directories from the <> search path that should
428 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
429 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
430 -Ibar -I- -Ifoo -Iquux.
432 Note that this algorithm is quadratic in the number of -I switches,
433 which is acceptable since there aren't usually that many of them. */
435 for (cur
= quote
, prev
= NULL
; cur
; cur
= cur
->next
)
437 for (other
= quote
; other
!= cur
; other
= other
->next
)
438 if (INO_T_EQ (cur
->ino
, other
->ino
)
439 && cur
->dev
== other
->dev
)
441 if (CPP_OPTION (pfile
, verbose
))
442 fprintf (stderr
, _("ignoring duplicate directory `%s'\n"),
445 prev
->next
= cur
->next
;
455 for (cur
= brack
; cur
; cur
= cur
->next
)
457 for (other
= brack
; other
!= cur
; other
= other
->next
)
458 if (INO_T_EQ (cur
->ino
, other
->ino
)
459 && cur
->dev
== other
->dev
)
461 if (CPP_OPTION (pfile
, verbose
))
462 fprintf (stderr
, _("ignoring duplicate directory `%s'\n"),
465 prev
->next
= cur
->next
;
476 if (INO_T_EQ (qtail
->ino
, brack
->ino
) && qtail
->dev
== brack
->dev
)
480 if (CPP_OPTION (pfile
, verbose
))
481 fprintf (stderr
, _("ignoring duplicate directory `%s'\n"),
491 while (cur
->next
!= qtail
)
494 if (CPP_OPTION (pfile
, verbose
))
495 fprintf (stderr
, _("ignoring duplicate directory `%s'\n"),
508 CPP_OPTION (pfile
, quote_include
) = quote
;
509 CPP_OPTION (pfile
, bracket_include
) = brack
;
513 /* Write out a #define command for the special named MACRO_NAME
514 to PFILE's token_buffer. */
517 dump_special_to_buffer (pfile
, macro_name
)
519 const char *macro_name
;
521 static const char define_directive
[] = "#define ";
522 int macro_name_length
= strlen (macro_name
);
523 CPP_RESERVE (pfile
, sizeof(define_directive
) + macro_name_length
);
524 CPP_PUTS_Q (pfile
, define_directive
, sizeof(define_directive
)-1);
525 CPP_PUTS_Q (pfile
, macro_name
, macro_name_length
);
526 CPP_PUTC_Q (pfile
, ' ');
527 _cpp_expand_to_buffer (pfile
, macro_name
, macro_name_length
);
528 CPP_PUTC (pfile
, '\n');
531 /* Initialize a cpp_reader structure. */
533 cpp_reader_init (pfile
)
536 memset ((char *) pfile
, 0, sizeof (cpp_reader
));
538 pfile
->token_buffer_size
= 200;
539 pfile
->token_buffer
= (U_CHAR
*) xmalloc (pfile
->token_buffer_size
);
540 CPP_SET_WRITTEN (pfile
, 0);
542 CPP_OPTION (pfile
, dollars_in_ident
) = 1;
543 CPP_OPTION (pfile
, cplusplus_comments
) = 1;
544 CPP_OPTION (pfile
, warn_import
) = 1;
545 CPP_OPTION (pfile
, discard_comments
) = 1;
546 CPP_OPTION (pfile
, show_column
) = 1;
548 CPP_OPTION (pfile
, pending
) =
549 (struct cpp_pending
*) xcalloc (1, sizeof (struct cpp_pending
));
551 _cpp_init_macro_hash (pfile
);
552 _cpp_init_include_hash (pfile
);
555 /* Initialize a cpp_printer structure. As a side effect, open the
558 cpp_printer_init (pfile
, print
)
562 memset (print
, '\0', sizeof (cpp_printer
));
563 if (CPP_OPTION (pfile
, out_fname
) == NULL
)
564 CPP_OPTION (pfile
, out_fname
) = "";
566 if (CPP_OPTION (pfile
, out_fname
)[0] == '\0')
567 print
->outf
= stdout
;
570 print
->outf
= fopen (CPP_OPTION (pfile
, out_fname
), "w");
573 cpp_notice_from_errno (pfile
, CPP_OPTION (pfile
, out_fname
));
580 /* Free resources used by PFILE.
581 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
586 while (CPP_BUFFER (pfile
) != NULL
)
587 cpp_pop_buffer (pfile
);
589 if (pfile
->token_buffer
)
591 free (pfile
->token_buffer
);
592 pfile
->token_buffer
= NULL
;
595 if (pfile
->input_buffer
)
597 free (pfile
->input_buffer
);
598 pfile
->input_buffer
= NULL
;
599 pfile
->input_buffer_len
= 0;
603 deps_free (pfile
->deps
);
605 htab_delete (pfile
->hashtab
);
606 htab_delete (pfile
->all_include_files
);
610 /* This structure defines one built-in macro. A node of type TYPE will
611 be entered in the macro hash table under the name NAME, with value
612 VALUE (if any). FLAGS tweaks the behavior a little:
613 DUMP write debug info for this macro
614 VERS value is the global version_string, quoted
615 ULP value is the global user_label_prefix
623 unsigned short flags
;
629 static const struct builtin builtin_array
[] =
631 { "__TIME__", 0, T_TIME
, 0 },
632 { "__DATE__", 0, T_DATE
, 0 },
633 { "__FILE__", 0, T_FILE
, 0 },
634 { "__BASE_FILE__", 0, T_BASE_FILE
, 0 },
635 { "__LINE__", 0, T_SPECLINE
, 0 },
636 { "__INCLUDE_LEVEL__", 0, T_INCLUDE_LEVEL
, 0 },
638 { "__VERSION__", 0, T_XCONST
, DUMP
|VERS
},
639 { "__USER_LABEL_PREFIX__", 0, T_CONST
, DUMP
|ULP
},
640 { "__STDC__", "1", T_STDC
, DUMP
},
641 { "__REGISTER_PREFIX__", REGISTER_PREFIX
, T_CONST
, DUMP
},
642 { "__HAVE_BUILTIN_SETJMP__", "1", T_CONST
, DUMP
},
643 #ifndef NO_BUILTIN_SIZE_TYPE
644 { "__SIZE_TYPE__", SIZE_TYPE
, T_CONST
, DUMP
},
646 #ifndef NO_BUILTIN_PTRDIFF_TYPE
647 { "__PTRDIFF_TYPE__", PTRDIFF_TYPE
, T_CONST
, DUMP
},
649 #ifndef NO_BUILTIN_WCHAR_TYPE
650 { "__WCHAR_TYPE__", WCHAR_TYPE
, T_CONST
, DUMP
},
653 #define builtin_array_end \
654 builtin_array + sizeof(builtin_array)/sizeof(struct builtin)
656 /* Subroutine of cpp_start_read; reads the builtins table above and
657 enters the macros into the hash table. */
659 initialize_builtins (pfile
)
663 const struct builtin
*b
;
666 for(b
= builtin_array
; b
< builtin_array_end
; b
++)
668 if (b
->type
== T_STDC
&& CPP_TRADITIONAL (pfile
))
672 val
= user_label_prefix
;
673 else if (b
->flags
& VERS
)
675 val
= xmalloc (strlen (version_string
) + 3);
676 sprintf ((char *)val
, "\"%s\"", version_string
);
681 len
= strlen (b
->name
);
682 hp
= _cpp_lookup (pfile
, b
->name
, len
);
683 hp
->value
.cpval
= val
;
686 if ((b
->flags
& DUMP
) && CPP_OPTION (pfile
, debug_output
))
687 dump_special_to_buffer (pfile
, b
->name
);
695 /* Another subroutine of cpp_start_read. This one sets up to do
696 dependency-file output. */
698 initialize_dependency_output (pfile
)
701 char *spec
, *s
, *output_file
;
703 /* Either of two environment variables can specify output of deps.
704 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
705 where OUTPUT_FILE is the file to write deps info to
706 and DEPS_TARGET is the target to mention in the deps. */
708 if (CPP_OPTION (pfile
, print_deps
) == 0)
710 spec
= getenv ("DEPENDENCIES_OUTPUT");
712 CPP_OPTION (pfile
, print_deps
) = 1;
715 spec
= getenv ("SUNPRO_DEPENDENCIES");
717 CPP_OPTION (pfile
, print_deps
) = 2;
722 /* Find the space before the DEPS_TARGET, if there is one. */
723 s
= strchr (spec
, ' ');
726 CPP_OPTION (pfile
, deps_target
) = s
+ 1;
727 output_file
= (char *) xmalloc (s
- spec
+ 1);
728 memcpy (output_file
, spec
, s
- spec
);
729 output_file
[s
- spec
] = 0;
733 CPP_OPTION (pfile
, deps_target
) = 0;
737 CPP_OPTION (pfile
, deps_file
) = output_file
;
738 CPP_OPTION (pfile
, print_deps_append
) = 1;
741 pfile
->deps
= deps_init ();
743 /* Print the expected object file name as the target of this Make-rule. */
744 if (CPP_OPTION (pfile
, deps_target
))
745 deps_add_target (pfile
->deps
, CPP_OPTION (pfile
, deps_target
));
746 else if (*CPP_OPTION (pfile
, in_fname
) == 0)
747 deps_add_target (pfile
->deps
, "-");
749 deps_calc_target (pfile
->deps
, CPP_OPTION (pfile
, in_fname
));
751 if (CPP_OPTION (pfile
, in_fname
))
752 deps_add_dep (pfile
->deps
, CPP_OPTION (pfile
, in_fname
));
755 /* And another subroutine. This one sets up the standard include path. */
757 initialize_standard_includes (pfile
)
761 const struct default_include
*p
;
762 const char *specd_prefix
= CPP_OPTION (pfile
, include_prefix
);
764 /* Several environment variables may add to the include search path.
765 CPATH specifies an additional list of directories to be searched
766 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
767 etc. specify an additional list of directories to be searched as
768 if specified with -isystem, for the language indicated. */
770 GET_ENV_PATH_LIST (path
, "CPATH");
771 if (path
!= 0 && *path
!= 0)
772 path_include (pfile
, CPP_OPTION (pfile
, pending
), path
, BRACKET
);
774 switch ((CPP_OPTION (pfile
, objc
) << 1) + CPP_OPTION (pfile
, cplusplus
))
777 GET_ENV_PATH_LIST (path
, "C_INCLUDE_PATH");
780 GET_ENV_PATH_LIST (path
, "CPLUS_INCLUDE_PATH");
783 GET_ENV_PATH_LIST (path
, "OBJC_INCLUDE_PATH");
786 GET_ENV_PATH_LIST (path
, "OBJCPLUS_INCLUDE_PATH");
789 if (path
!= 0 && *path
!= 0)
790 path_include (pfile
, CPP_OPTION (pfile
, pending
), path
, SYSTEM
);
792 /* Search "translated" versions of GNU directories.
793 These have /usr/local/lib/gcc... replaced by specd_prefix. */
794 if (specd_prefix
!= 0)
796 char *default_prefix
= (char *) alloca (sizeof GCC_INCLUDE_DIR
- 7);
797 /* Remove the `include' from /usr/local/lib/gcc.../include.
798 GCC_INCLUDE_DIR will always end in /include. */
799 int default_len
= sizeof GCC_INCLUDE_DIR
- 8;
800 int specd_len
= strlen (specd_prefix
);
802 memcpy (default_prefix
, GCC_INCLUDE_DIR
, default_len
);
803 default_prefix
[default_len
] = '\0';
805 for (p
= include_defaults_array
; p
->fname
; p
++)
807 /* Some standard dirs are only for C++. */
809 || (CPP_OPTION (pfile
, cplusplus
)
810 && !CPP_OPTION (pfile
, no_standard_cplusplus_includes
)))
812 /* Does this dir start with the prefix? */
813 if (!strncmp (p
->fname
, default_prefix
, default_len
))
815 /* Yes; change prefix and add to search list. */
816 int flen
= strlen (p
->fname
);
817 int this_len
= specd_len
+ flen
- default_len
;
818 char *str
= (char *) xmalloc (this_len
+ 1);
819 memcpy (str
, specd_prefix
, specd_len
);
820 memcpy (str
+ specd_len
,
821 p
->fname
+ default_len
,
822 flen
- default_len
+ 1);
824 append_include_chain (pfile
, CPP_OPTION (pfile
, pending
),
825 str
, SYSTEM
, p
->cxx_aware
);
831 /* Search ordinary names for GNU include directories. */
832 for (p
= include_defaults_array
; p
->fname
; p
++)
834 /* Some standard dirs are only for C++. */
836 || (CPP_OPTION (pfile
, cplusplus
)
837 && !CPP_OPTION (pfile
, no_standard_cplusplus_includes
)))
839 /* XXX Potential memory leak! */
840 char *str
= xstrdup (update_path (p
->fname
, p
->component
));
841 append_include_chain (pfile
, CPP_OPTION (pfile
, pending
),
842 str
, SYSTEM
, p
->cxx_aware
);
847 /* This is called after options have been processed.
848 * Check options for consistency, and setup for processing input
849 * from the file named FNAME. (Use standard input if FNAME==NULL.)
850 * Return 1 on success, 0 on failure.
854 cpp_start_read (pfile
, print
, fname
)
859 struct pending_option
*p
, *q
;
861 /* -MG doesn't select the form of output and must be specified with one of
862 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
863 inhibit compilation. */
864 if (CPP_OPTION (pfile
, print_deps_missing_files
)
865 && (CPP_OPTION (pfile
, print_deps
) == 0
866 || !CPP_OPTION (pfile
, no_output
)))
868 cpp_fatal (pfile
, "-MG must be specified with one of -M or -MM");
872 /* Chill should not be used with -trigraphs. */
873 if (CPP_OPTION (pfile
, chill
) && CPP_OPTION (pfile
, trigraphs
))
875 cpp_warning (pfile
, "-lang-chill and -trigraphs are mutually exclusive");
876 CPP_OPTION (pfile
, trigraphs
) = 0;
879 /* -Wtraditional is not useful in C++ mode. */
880 if (CPP_OPTION (pfile
, cplusplus
))
881 CPP_OPTION (pfile
, warn_traditional
) = 0;
883 /* Set this if it hasn't been set already. */
884 if (user_label_prefix
== NULL
)
885 user_label_prefix
= USER_LABEL_PREFIX
;
887 /* Don't bother trying to do macro expansion if we've already done
889 if (CPP_OPTION (pfile
, preprocessed
))
890 pfile
->no_macro_expand
++;
892 /* Set up the IStable. This doesn't do anything if we were compiled
893 with a compiler that supports C99 designated initializers. */
896 /* Set up the tables used by read_and_prescan. */
897 _cpp_init_input_buffer (pfile
);
899 /* Set up the include search path now. */
900 if (! CPP_OPTION (pfile
, no_standard_includes
))
901 initialize_standard_includes (pfile
);
903 merge_include_chains (pfile
);
905 /* With -v, print the list of dirs to search. */
906 if (CPP_OPTION (pfile
, verbose
))
908 struct file_name_list
*l
;
909 fprintf (stderr
, _("#include \"...\" search starts here:\n"));
910 for (l
= CPP_OPTION (pfile
, quote_include
); l
; l
= l
->next
)
912 if (l
== CPP_OPTION (pfile
, bracket_include
))
913 fprintf (stderr
, _("#include <...> search starts here:\n"));
914 fprintf (stderr
, " %s\n", l
->name
);
916 fprintf (stderr
, _("End of search list.\n"));
919 /* Open the main input file. This must be done early, so we have a
920 buffer to stand on. */
921 if (CPP_OPTION (pfile
, in_fname
) == NULL
922 || *CPP_OPTION (pfile
, in_fname
) == 0)
924 CPP_OPTION (pfile
, in_fname
) = fname
;
925 if (CPP_OPTION (pfile
, in_fname
) == NULL
)
926 CPP_OPTION (pfile
, in_fname
) = "";
928 if (CPP_OPTION (pfile
, out_fname
) == NULL
)
929 CPP_OPTION (pfile
, out_fname
) = "";
931 if (!cpp_read_file (pfile
, fname
))
934 initialize_dependency_output (pfile
);
936 /* -D and friends may produce output, which should be identified
939 CPP_BUFFER (pfile
)->lineno
= 0;
944 print
->last_fname
= CPP_BUFFER (pfile
)->nominal_fname
;
945 print
->last_bsd
= pfile
->buffer_stack_depth
;
946 print
->written
= CPP_WRITTEN (pfile
);
949 /* Install __LINE__, etc. */
950 initialize_builtins (pfile
);
952 /* Do -U's, -D's and -A's in the order they were seen. */
953 p
= CPP_OPTION (pfile
, pending
)->directive_head
;
956 (*p
->handler
) (pfile
, p
->arg
);
961 pfile
->done_initializing
= 1;
962 pfile
->only_seen_white
= 2;
963 CPP_BUFFER (pfile
)->lineno
= 1;
964 if (print
&& ! CPP_OPTION (pfile
, no_output
))
965 cpp_output_tokens (pfile
, print
);
967 /* The -imacros files can be scanned now, but the -include files
968 have to be pushed onto the include stack and processed later,
969 in the main loop calling cpp_get_token. */
971 p
= CPP_OPTION (pfile
, pending
)->imacros_head
;
974 if (cpp_read_file (pfile
, p
->arg
))
975 cpp_scan_buffer_nooutput (pfile
);
981 p
= CPP_OPTION (pfile
, pending
)->include_head
;
984 if (cpp_read_file (pfile
, p
->arg
)
985 && print
&& ! CPP_OPTION (pfile
, no_output
))
986 cpp_output_tokens (pfile
, print
);
992 free (CPP_OPTION (pfile
, pending
));
993 CPP_OPTION (pfile
, pending
) = NULL
;
998 /* This is called at the end of preprocessing. It pops the
999 last buffer and writes dependency output. It should also
1000 clear macro definitions, such that you could call cpp_start_read
1001 with a new filename to restart processing. */
1003 cpp_finish (pfile
, print
)
1007 if (CPP_BUFFER (pfile
))
1009 cpp_ice (pfile
, "buffers still stacked in cpp_finish");
1010 while (CPP_BUFFER (pfile
))
1011 cpp_pop_buffer (pfile
);
1014 /* Don't write the deps file if preprocessing has failed. */
1015 if (CPP_OPTION (pfile
, print_deps
) && pfile
->errors
== 0)
1017 /* Stream on which to print the dependency information. */
1018 FILE *deps_stream
= 0;
1019 const char *deps_mode
1020 = CPP_OPTION (pfile
, print_deps_append
) ? "a" : "w";
1021 if (CPP_OPTION (pfile
, deps_file
) == 0)
1022 deps_stream
= stdout
;
1025 deps_stream
= fopen (CPP_OPTION (pfile
, deps_file
), deps_mode
);
1026 if (deps_stream
== 0)
1027 cpp_notice_from_errno (pfile
, CPP_OPTION (pfile
, deps_file
));
1031 deps_write (pfile
->deps
, deps_stream
, 72);
1032 if (CPP_OPTION (pfile
, deps_file
))
1034 if (ferror (deps_stream
) || fclose (deps_stream
) != 0)
1035 cpp_fatal (pfile
, "I/O error on output");
1040 if (CPP_OPTION (pfile
, dump_macros
) == dump_only
)
1041 _cpp_dump_macro_hash (pfile
);
1043 /* Flush any pending output. */
1046 cpp_output_tokens (pfile
, print
);
1047 if (ferror (print
->outf
) || fclose (print
->outf
))
1048 cpp_notice_from_errno (pfile
, CPP_OPTION (pfile
, out_fname
));
1053 new_pending_directive (pend
, text
, handler
)
1054 struct cpp_pending
*pend
;
1056 cl_directive_handler handler
;
1058 struct pending_option
*o
= (struct pending_option
*)
1059 xmalloc (sizeof (struct pending_option
));
1063 o
->handler
= handler
;
1064 APPEND (pend
, directive
, o
);
1067 /* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
1068 I.e. a const string initializer with parens around it. That is
1069 what N_("string") resolves to, so we make no_* be macros instead. */
1070 #define no_arg N_("Argument missing after %s")
1071 #define no_ass N_("Assertion missing after %s")
1072 #define no_dir N_("Directory name missing after %s")
1073 #define no_fil N_("File name missing after %s")
1074 #define no_mac N_("Macro name missing after %s")
1075 #define no_pth N_("Path name missing after %s")
1077 /* This is the list of all command line options, with the leading
1078 "-" removed. It must be sorted in ASCII collating order. */
1079 #define COMMAND_LINE_OPTIONS \
1080 DEF_OPT("", 0, OPT_stdin_stdout) \
1081 DEF_OPT("$", 0, OPT_dollar) \
1082 DEF_OPT("+", 0, OPT_plus) \
1083 DEF_OPT("-help", 0, OPT__help) \
1084 DEF_OPT("-version", 0, OPT__version) \
1085 DEF_OPT("A", no_ass, OPT_A) \
1086 DEF_OPT("C", 0, OPT_C) \
1087 DEF_OPT("D", no_mac, OPT_D) \
1088 DEF_OPT("H", 0, OPT_H) \
1089 DEF_OPT("I", no_dir, OPT_I) \
1090 DEF_OPT("M", 0, OPT_M) \
1091 DEF_OPT("MD", no_fil, OPT_MD) \
1092 DEF_OPT("MG", 0, OPT_MG) \
1093 DEF_OPT("MM", 0, OPT_MM) \
1094 DEF_OPT("MMD", no_fil, OPT_MMD) \
1095 DEF_OPT("P", 0, OPT_P) \
1096 DEF_OPT("U", no_mac, OPT_U) \
1097 DEF_OPT("W", no_arg, OPT_W) /* arg optional */ \
1098 DEF_OPT("d", no_arg, OPT_d) \
1099 DEF_OPT("fleading-underscore", 0, OPT_fleading_underscore) \
1100 DEF_OPT("fno-leading-underscore", 0, OPT_fno_leading_underscore) \
1101 DEF_OPT("fno-preprocessed", 0, OPT_fno_preprocessed) \
1102 DEF_OPT("fno-show-column", 0, OPT_fno_show_column) \
1103 DEF_OPT("fpreprocessed", 0, OPT_fpreprocessed) \
1104 DEF_OPT("fshow-column", 0, OPT_fshow_column) \
1105 DEF_OPT("g", no_arg, OPT_g) /* arg optional */ \
1106 DEF_OPT("h", 0, OPT_h) \
1107 DEF_OPT("idirafter", no_dir, OPT_idirafter) \
1108 DEF_OPT("imacros", no_fil, OPT_imacros) \
1109 DEF_OPT("include", no_fil, OPT_include) \
1110 DEF_OPT("iprefix", no_pth, OPT_iprefix) \
1111 DEF_OPT("isystem", no_dir, OPT_isystem) \
1112 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix) \
1113 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore) \
1114 DEF_OPT("lang-asm", 0, OPT_lang_asm) \
1115 DEF_OPT("lang-c", 0, OPT_lang_c) \
1116 DEF_OPT("lang-c++", 0, OPT_lang_cplusplus) \
1117 DEF_OPT("lang-c89", 0, OPT_lang_c89) \
1118 DEF_OPT("lang-chill", 0, OPT_lang_chill) \
1119 DEF_OPT("lang-fortran", 0, OPT_lang_fortran) \
1120 DEF_OPT("lang-objc", 0, OPT_lang_objc) \
1121 DEF_OPT("lang-objc++", 0, OPT_lang_objcplusplus) \
1122 DEF_OPT("nostdinc", 0, OPT_nostdinc) \
1123 DEF_OPT("nostdinc++", 0, OPT_nostdincplusplus) \
1124 DEF_OPT("o", no_fil, OPT_o) \
1125 DEF_OPT("pedantic", 0, OPT_pedantic) \
1126 DEF_OPT("pedantic-errors", 0, OPT_pedantic_errors) \
1127 DEF_OPT("remap", 0, OPT_remap) \
1128 DEF_OPT("std=c89", 0, OPT_std_c89) \
1129 DEF_OPT("std=c99", 0, OPT_std_c99) \
1130 DEF_OPT("std=c9x", 0, OPT_std_c9x) \
1131 DEF_OPT("std=gnu89", 0, OPT_std_gnu89) \
1132 DEF_OPT("std=gnu99", 0, OPT_std_gnu99) \
1133 DEF_OPT("std=gnu9x", 0, OPT_std_gnu9x) \
1134 DEF_OPT("std=iso9899:1990", 0, OPT_std_iso9899_1990) \
1135 DEF_OPT("std=iso9899:199409", 0, OPT_std_iso9899_199409) \
1136 DEF_OPT("std=iso9899:1999", 0, OPT_std_iso9899_1999) \
1137 DEF_OPT("std=iso9899:199x", 0, OPT_std_iso9899_199x) \
1138 DEF_OPT("traditional", 0, OPT_traditional) \
1139 DEF_OPT("trigraphs", 0, OPT_trigraphs) \
1140 DEF_OPT("v", 0, OPT_v) \
1141 DEF_OPT("w", 0, OPT_w)
1143 #define DEF_OPT(text, msg, code) code,
1146 COMMAND_LINE_OPTIONS
1153 const char *opt_text
;
1156 enum opt_code opt_code
;
1159 #define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
1161 static struct cl_option cl_options
[] =
1163 static const struct cl_option cl_options
[] =
1166 COMMAND_LINE_OPTIONS
1169 #undef COMMAND_LINE_OPTIONS
1171 /* Perform a binary search to find which, if any, option the given
1172 command-line matches. Returns its index in the option array,
1173 negative on failure. Complications arise since some options can be
1174 suffixed with an argument, and multiple complete matches can occur,
1175 e.g. -iwithprefix and -iwithprefixbefore. Moreover, we want to
1176 accept options beginning with -g and -W that we do not recognise,
1177 but not to swallow any subsequent command line argument; these are
1178 handled as special cases in cpp_handle_option */
1180 parse_option (input
)
1183 unsigned int md
, mn
, mx
;
1194 opt_len
= cl_options
[md
].opt_len
;
1195 comp
= strncmp (input
, cl_options
[md
].opt_text
, opt_len
);
1203 if (input
[opt_len
] == '\0')
1205 /* We were passed more text. If the option takes an argument,
1206 we may match a later option or we may have been passed the
1207 argument. The longest possible option match succeeds.
1208 If the option takes no arguments we have not matched and
1209 continue the search (e.g. input="stdc++" match was "stdc") */
1211 if (cl_options
[md
].msg
)
1213 /* Scan forwards. If we get an exact match, return it.
1214 Otherwise, return the longest option-accepting match.
1215 This loops no more than twice with current options */
1217 for (; mn
< N_OPTS
; mn
++)
1219 opt_len
= cl_options
[mn
].opt_len
;
1220 if (strncmp (input
, cl_options
[mn
].opt_text
, opt_len
))
1222 if (input
[opt_len
] == '\0')
1224 if (cl_options
[mn
].msg
)
1235 /* Handle one command-line option in (argc, argv).
1236 Can be called multiple times, to handle multiple sets of options.
1237 Returns number of strings consumed. */
1240 handle_option (pfile
, argc
, argv
)
1247 if (argv
[i
][0] != '-')
1249 if (CPP_OPTION (pfile
, out_fname
) != NULL
)
1250 cpp_fatal (pfile
, "Too many arguments. Type %s --help for usage info",
1252 else if (CPP_OPTION (pfile
, in_fname
) != NULL
)
1253 CPP_OPTION (pfile
, out_fname
) = argv
[i
];
1255 CPP_OPTION (pfile
, in_fname
) = argv
[i
];
1259 enum opt_code opt_code
;
1261 const char *arg
= 0;
1264 opt_index
= parse_option (&argv
[i
][1]);
1268 opt_code
= cl_options
[opt_index
].opt_code
;
1269 if (cl_options
[opt_index
].msg
)
1271 arg
= &argv
[i
][cl_options
[opt_index
].opt_len
+ 1];
1273 /* Yuk. Special case for -g and -W as they must not swallow
1274 up any following argument. If this becomes common, add
1275 another field to the cl_options table */
1276 if (arg
[0] == '\0' && !(opt_code
== OPT_g
|| opt_code
== OPT_W
))
1281 cpp_fatal (pfile
, cl_options
[opt_index
].msg
, argv
[i
- 1]);
1289 case N_OPTS
: /* shut GCC up */
1291 case OPT_fleading_underscore
:
1292 user_label_prefix
= "_";
1294 case OPT_fno_leading_underscore
:
1295 user_label_prefix
= "";
1297 case OPT_fpreprocessed
:
1298 CPP_OPTION (pfile
, preprocessed
) = 1;
1300 case OPT_fno_preprocessed
:
1301 CPP_OPTION (pfile
, preprocessed
) = 0;
1303 case OPT_fshow_column
:
1304 CPP_OPTION (pfile
, show_column
) = 1;
1306 case OPT_fno_show_column
:
1307 CPP_OPTION (pfile
, show_column
) = 0;
1310 CPP_OPTION (pfile
, inhibit_warnings
) = 1;
1312 case OPT_g
: /* Silently ignore anything but -g3 */
1313 if (!strcmp(&argv
[i
][2], "3"))
1314 CPP_OPTION (pfile
, debug_output
) = 1;
1322 fprintf (stderr
, _("GNU CPP version %s (cpplib)\n"), version_string
);
1326 CPP_OPTION (pfile
, discard_comments
) = 0;
1329 CPP_OPTION (pfile
, no_line_commands
) = 1;
1331 case OPT_dollar
: /* Don't include $ in identifiers. */
1332 CPP_OPTION (pfile
, dollars_in_ident
) = 0;
1335 CPP_OPTION (pfile
, print_include_names
) = 1;
1338 new_pending_directive (CPP_OPTION (pfile
, pending
), arg
, cpp_define
);
1340 case OPT_pedantic_errors
:
1341 CPP_OPTION (pfile
, pedantic_errors
) = 1;
1344 CPP_OPTION (pfile
, pedantic
) = 1;
1346 case OPT_traditional
:
1347 CPP_OPTION (pfile
, traditional
) = 1;
1348 CPP_OPTION (pfile
, cplusplus_comments
) = 0;
1349 CPP_OPTION (pfile
, trigraphs
) = 0;
1350 CPP_OPTION (pfile
, warn_trigraphs
) = 0;
1353 CPP_OPTION (pfile
, trigraphs
) = 1;
1356 CPP_OPTION (pfile
, cplusplus
) = 1;
1357 CPP_OPTION (pfile
, cplusplus_comments
) = 1;
1360 CPP_OPTION (pfile
, remap
) = 1;
1363 CPP_OPTION (pfile
, include_prefix
) = arg
;
1364 CPP_OPTION (pfile
, include_prefix_len
) = strlen (arg
);
1367 CPP_OPTION (pfile
, cplusplus
) = 0;
1368 CPP_OPTION (pfile
, cplusplus_comments
) = 1;
1369 CPP_OPTION (pfile
, c89
) = 0;
1370 CPP_OPTION (pfile
, c99
) = 1;
1371 CPP_OPTION (pfile
, objc
) = 0;
1374 CPP_OPTION (pfile
, cplusplus
) = 0;
1375 CPP_OPTION (pfile
, cplusplus_comments
) = 0;
1376 CPP_OPTION (pfile
, c89
) = 1;
1377 CPP_OPTION (pfile
, c99
) = 0;
1378 CPP_OPTION (pfile
, objc
) = 0;
1379 CPP_OPTION (pfile
, trigraphs
) = 1;
1380 new_pending_directive (CPP_OPTION (pfile
, pending
),
1381 "__STRICT_ANSI__", cpp_define
);
1383 case OPT_lang_cplusplus
:
1384 CPP_OPTION (pfile
, cplusplus
) = 1;
1385 CPP_OPTION (pfile
, cplusplus_comments
) = 1;
1386 CPP_OPTION (pfile
, c89
) = 0;
1387 CPP_OPTION (pfile
, c99
) = 0;
1388 CPP_OPTION (pfile
, objc
) = 0;
1391 case OPT_lang_objcplusplus
:
1392 CPP_OPTION (pfile
, cplusplus
) = opt_code
== OPT_lang_objcplusplus
;
1393 CPP_OPTION (pfile
, cplusplus_comments
) = 1;
1394 CPP_OPTION (pfile
, c89
) = 0;
1395 CPP_OPTION (pfile
, c99
) = 0;
1396 CPP_OPTION (pfile
, objc
) = 1;
1399 CPP_OPTION (pfile
, lang_asm
) = 1;
1401 case OPT_lang_fortran
:
1402 CPP_OPTION (pfile
, lang_fortran
) = 1;
1403 CPP_OPTION (pfile
, cplusplus_comments
) = 0;
1405 case OPT_lang_chill
:
1406 CPP_OPTION (pfile
, objc
) = 0;
1407 CPP_OPTION (pfile
, cplusplus
) = 0;
1408 CPP_OPTION (pfile
, chill
) = 1;
1409 CPP_OPTION (pfile
, traditional
) = 1;
1412 /* -nostdinc causes no default include directories.
1413 You must specify all include-file directories with -I. */
1414 CPP_OPTION (pfile
, no_standard_includes
) = 1;
1416 case OPT_nostdincplusplus
:
1417 /* -nostdinc++ causes no default C++-specific include directories. */
1418 CPP_OPTION (pfile
, no_standard_cplusplus_includes
) = 1;
1421 CPP_OPTION (pfile
, cplusplus
) = 0;
1422 CPP_OPTION (pfile
, cplusplus_comments
) = 1;
1423 CPP_OPTION (pfile
, c89
) = 1;
1424 CPP_OPTION (pfile
, c99
) = 0;
1425 CPP_OPTION (pfile
, objc
) = 0;
1429 CPP_OPTION (pfile
, cplusplus
) = 0;
1430 CPP_OPTION (pfile
, cplusplus_comments
) = 1;
1431 CPP_OPTION (pfile
, c89
) = 0;
1432 CPP_OPTION (pfile
, c99
) = 1;
1433 CPP_OPTION (pfile
, objc
) = 0;
1434 new_pending_directive (CPP_OPTION (pfile
, pending
),
1435 "__STDC_VERSION__=199901L", cpp_define
);
1437 case OPT_std_iso9899_199409
:
1438 new_pending_directive (CPP_OPTION (pfile
, pending
),
1439 "__STDC_VERSION__=199409L", cpp_define
);
1441 case OPT_std_iso9899_1990
:
1443 CPP_OPTION (pfile
, cplusplus
) = 0;
1444 CPP_OPTION (pfile
, cplusplus_comments
) = 0;
1445 CPP_OPTION (pfile
, c89
) = 1;
1446 CPP_OPTION (pfile
, c99
) = 0;
1447 CPP_OPTION (pfile
, objc
) = 0;
1448 CPP_OPTION (pfile
, trigraphs
) = 1;
1449 new_pending_directive (CPP_OPTION (pfile
, pending
),
1450 "__STRICT_ANSI__", cpp_define
);
1452 case OPT_std_iso9899_199x
:
1453 case OPT_std_iso9899_1999
:
1456 CPP_OPTION (pfile
, cplusplus
) = 0;
1457 CPP_OPTION (pfile
, cplusplus_comments
) = 1;
1458 CPP_OPTION (pfile
, c89
) = 0;
1459 CPP_OPTION (pfile
, c99
) = 1;
1460 CPP_OPTION (pfile
, objc
) = 0;
1461 CPP_OPTION (pfile
, trigraphs
) = 1;
1462 new_pending_directive (CPP_OPTION (pfile
, pending
),
1463 "__STRICT_ANSI__", cpp_define
);
1464 new_pending_directive (CPP_OPTION (pfile
, pending
),
1465 "__STDC_VERSION__=199901L", cpp_define
);
1468 if (CPP_OPTION (pfile
, out_fname
) != NULL
)
1470 cpp_fatal (pfile
, "Output filename specified twice");
1473 CPP_OPTION (pfile
, out_fname
) = arg
;
1474 if (!strcmp (CPP_OPTION (pfile
, out_fname
), "-"))
1475 CPP_OPTION (pfile
, out_fname
) = "";
1478 fprintf (stderr
, _("GNU CPP version %s (cpplib)\n"), version_string
);
1479 #ifdef TARGET_VERSION
1482 fputc ('\n', stderr
);
1483 CPP_OPTION (pfile
, verbose
) = 1;
1485 case OPT_stdin_stdout
:
1486 /* JF handle '-' as file name meaning stdin or stdout */
1487 if (CPP_OPTION (pfile
, in_fname
) == NULL
)
1488 CPP_OPTION (pfile
, in_fname
) = "";
1489 else if (CPP_OPTION (pfile
, out_fname
) == NULL
)
1490 CPP_OPTION (pfile
, out_fname
) = "";
1493 /* Args to -d specify what parts of macros to dump.
1494 Silently ignore unrecognised options; they may
1495 be aimed at the compiler proper. */
1499 while ((c
= *arg
++) != '\0')
1503 CPP_OPTION (pfile
, dump_macros
) = dump_only
;
1504 CPP_OPTION (pfile
, no_output
) = 1;
1507 CPP_OPTION (pfile
, dump_macros
) = dump_names
;
1510 CPP_OPTION (pfile
, dump_macros
) = dump_definitions
;
1513 CPP_OPTION (pfile
, dump_includes
) = 1;
1518 /* The style of the choices here is a bit mixed.
1519 The chosen scheme is a hybrid of keeping all options in one string
1520 and specifying each option in a separate argument:
1521 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
1522 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1523 -M[M][G][D file]. This is awkward to handle in specs, and is not
1525 /* ??? -MG must be specified in addition to one of -M or -MM.
1526 This can be relaxed in the future without breaking anything.
1527 The converse isn't true. */
1529 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
1531 CPP_OPTION (pfile
, print_deps_missing_files
) = 1;
1537 if (opt_code
== OPT_M
|| opt_code
== OPT_MD
)
1538 CPP_OPTION (pfile
, print_deps
) = 2;
1540 CPP_OPTION (pfile
, print_deps
) = 1;
1542 /* For -MD and -MMD options, write deps on file named by next arg */
1543 /* For -M and -MM, write deps on standard output
1544 and suppress the usual output. */
1545 if (opt_code
== OPT_MD
|| opt_code
== OPT_MMD
)
1546 CPP_OPTION (pfile
, deps_file
) = arg
;
1548 CPP_OPTION (pfile
, no_output
) = 1;
1553 /* -A with an argument beginning with '-' acts as
1554 #unassert on whatever immediately follows the '-'.
1555 If "-" is the whole argument, we eliminate all
1556 predefined macros and assertions, including those
1557 that were specified earlier on the command line.
1558 That way we can get rid of any that were passed
1559 automatically in from GCC. */
1563 struct pending_option
*o1
, *o2
;
1565 o1
= CPP_OPTION (pfile
, pending
)->directive_head
;
1572 CPP_OPTION (pfile
, pending
)->directive_head
= NULL
;
1573 CPP_OPTION (pfile
, pending
)->directive_tail
= NULL
;
1576 new_pending_directive (CPP_OPTION (pfile
, pending
),
1577 arg
+ 1, cpp_unassert
);
1580 new_pending_directive (CPP_OPTION (pfile
, pending
),
1584 new_pending_directive (CPP_OPTION (pfile
, pending
), arg
, cpp_undef
);
1586 case OPT_I
: /* Add directory to path for includes. */
1587 if (!strcmp (arg
, "-"))
1590 Use the preceding -I directories for #include "..."
1591 but not #include <...>.
1592 Don't search the directory of the present file
1593 for #include "...". (Note that -I. -I- is not the same as
1594 the default setup; -I. uses the compiler's working dir.) */
1595 if (! CPP_OPTION (pfile
, ignore_srcdir
))
1597 struct cpp_pending
*pend
= CPP_OPTION (pfile
, pending
);
1598 pend
->quote_head
= pend
->brack_head
;
1599 pend
->quote_tail
= pend
->brack_tail
;
1600 pend
->brack_head
= 0;
1601 pend
->brack_tail
= 0;
1602 CPP_OPTION (pfile
, ignore_srcdir
) = 1;
1606 cpp_fatal (pfile
, "-I- specified twice");
1611 append_include_chain (pfile
, CPP_OPTION (pfile
, pending
),
1612 xstrdup (arg
), BRACKET
, 0);
1615 /* Add directory to beginning of system include path, as a system
1616 include directory. */
1617 append_include_chain (pfile
, CPP_OPTION (pfile
, pending
),
1618 xstrdup (arg
), SYSTEM
, 0);
1622 struct pending_option
*o
= (struct pending_option
*)
1623 xmalloc (sizeof (struct pending_option
));
1626 /* This list has to be built in reverse order so that
1627 when cpp_start_read pushes all the -include files onto
1628 the buffer stack, they will be scanned in forward order. */
1629 o
->next
= CPP_OPTION (pfile
, pending
)->include_head
;
1630 CPP_OPTION (pfile
, pending
)->include_head
= o
;
1635 struct pending_option
*o
= (struct pending_option
*)
1636 xmalloc (sizeof (struct pending_option
));
1640 APPEND (CPP_OPTION (pfile
, pending
), imacros
, o
);
1643 case OPT_iwithprefix
:
1644 /* Add directory to end of path for includes,
1645 with the default prefix at the front of its name. */
1647 case OPT_iwithprefixbefore
:
1648 /* Add directory to main path for includes,
1649 with the default prefix at the front of its name. */
1656 if (CPP_OPTION (pfile
, include_prefix
) != 0)
1658 size_t ipl
= CPP_OPTION (pfile
, include_prefix_len
);
1659 fname
= xmalloc (ipl
+ len
+ 1);
1660 memcpy (fname
, CPP_OPTION (pfile
, include_prefix
), ipl
);
1661 memcpy (fname
+ ipl
, arg
, len
+ 1);
1665 fname
= xmalloc (sizeof GCC_INCLUDE_DIR
- 8 + len
);
1666 memcpy (fname
, GCC_INCLUDE_DIR
, sizeof GCC_INCLUDE_DIR
- 9);
1667 memcpy (fname
+ sizeof GCC_INCLUDE_DIR
- 9, arg
, len
+ 1);
1670 append_include_chain (pfile
, CPP_OPTION (pfile
, pending
), fname
,
1671 opt_code
== OPT_iwithprefix
? SYSTEM
: BRACKET
, 0);
1675 /* Add directory to end of path for includes. */
1676 append_include_chain (pfile
, CPP_OPTION (pfile
, pending
),
1677 xstrdup (arg
), AFTER
, 0);
1680 /* Silently ignore unrecognised options */
1681 if (!strcmp (argv
[i
], "-Wall"))
1683 CPP_OPTION (pfile
, warn_trigraphs
) = 1;
1684 CPP_OPTION (pfile
, warn_comments
) = 1;
1686 else if (!strcmp (argv
[i
], "-Wtraditional"))
1687 CPP_OPTION (pfile
, warn_traditional
) = 1;
1688 else if (!strcmp (argv
[i
], "-Wtrigraphs"))
1689 CPP_OPTION (pfile
, warn_trigraphs
) = 1;
1690 else if (!strcmp (argv
[i
], "-Wcomment"))
1691 CPP_OPTION (pfile
, warn_comments
) = 1;
1692 else if (!strcmp (argv
[i
], "-Wcomments"))
1693 CPP_OPTION (pfile
, warn_comments
) = 1;
1694 else if (!strcmp (argv
[i
], "-Wundef"))
1695 CPP_OPTION (pfile
, warn_undef
) = 1;
1696 else if (!strcmp (argv
[i
], "-Wimport"))
1697 CPP_OPTION (pfile
, warn_import
) = 1;
1698 else if (!strcmp (argv
[i
], "-Werror"))
1699 CPP_OPTION (pfile
, warnings_are_errors
) = 1;
1700 else if (!strcmp (argv
[i
], "-Wno-traditional"))
1701 CPP_OPTION (pfile
, warn_traditional
) = 0;
1702 else if (!strcmp (argv
[i
], "-Wno-trigraphs"))
1703 CPP_OPTION (pfile
, warn_trigraphs
) = 0;
1704 else if (!strcmp (argv
[i
], "-Wno-comment"))
1705 CPP_OPTION (pfile
, warn_comments
) = 0;
1706 else if (!strcmp (argv
[i
], "-Wno-comments"))
1707 CPP_OPTION (pfile
, warn_comments
) = 0;
1708 else if (!strcmp (argv
[i
], "-Wno-undef"))
1709 CPP_OPTION (pfile
, warn_undef
) = 0;
1710 else if (!strcmp (argv
[i
], "-Wno-import"))
1711 CPP_OPTION (pfile
, warn_import
) = 0;
1712 else if (!strcmp (argv
[i
], "-Wno-error"))
1713 CPP_OPTION (pfile
, warnings_are_errors
) = 0;
1722 opt_comp (const void *p1
, const void *p2
)
1724 return strcmp (((struct cl_option
*)p1
)->opt_text
,
1725 ((struct cl_option
*)p2
)->opt_text
);
1729 /* Handle command-line options in (argc, argv).
1730 Can be called multiple times, to handle multiple sets of options.
1731 Returns if an unrecognized option is seen.
1732 Returns number of strings consumed. */
1734 cpp_handle_options (pfile
, argc
, argv
)
1740 int strings_processed
;
1743 static int opts_sorted
= 0;
1748 /* For non-ASCII hosts, the array needs to be sorted at runtime */
1749 qsort (cl_options
, N_OPTS
, sizeof (struct cl_option
), opt_comp
);
1753 for (i
= 0; i
< argc
; i
+= strings_processed
)
1755 strings_processed
= handle_option (pfile
, argc
- i
, argv
+ i
);
1756 if (strings_processed
== 0)
1765 fprintf (stderr
, _("Usage: %s [switches] input output\n"), progname
);
1768 -include <file> Include the contents of <file> before other files\n\
1769 -imacros <file> Accept definition of macros in <file>\n\
1770 -iprefix <path> Specify <path> as a prefix for next two options\n\
1771 -iwithprefix <dir> Add <dir> to the end of the system include path\n\
1772 -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\
1773 -isystem <dir> Add <dir> to the start of the system include path\n\
1774 -idirafter <dir> Add <dir> to the end of the system include path\n\
1775 -I <dir> Add <dir> to the end of the main include path\n\
1776 -I- Fine-grained include path control; see info docs\n\
1777 -nostdinc Do not search system include directories\n\
1778 (dirs specified with -isystem will still be used)\n\
1779 -nostdinc++ Do not search system include directories for C++\n\
1780 -o <file> Put output into <file>\n\
1781 -pedantic Issue all warnings demanded by strict ANSI C\n\
1782 -pedantic-errors Issue -pedantic warnings as errors instead\n\
1783 -traditional Follow K&R pre-processor behaviour\n\
1784 -trigraphs Support ANSI C trigraphs\n\
1785 -lang-c Assume that the input sources are in C\n\
1786 -lang-c89 Assume that the input sources are in C89\n\
1787 -lang-c++ Assume that the input sources are in C++\n\
1788 -lang-objc Assume that the input sources are in ObjectiveC\n\
1789 -lang-objc++ Assume that the input sources are in ObjectiveC++\n\
1790 -lang-asm Assume that the input sources are in assembler\n\
1791 -lang-fortran Assume that the input sources are in Fortran\n\
1792 -lang-chill Assume that the input sources are in Chill\n\
1793 -std=<std name> Specify the conformance standard; one of:\n\
1794 gnu89, gnu99, c89, c99, iso9899:1990,\n\
1795 iso9899:199409, iso9899:1999\n\
1796 -+ Allow parsing of C++ style features\n\
1797 -w Inhibit warning messages\n\
1798 -Wtrigraphs Warn if trigraphs are encountered\n\
1799 -Wno-trigraphs Do not warn about trigraphs\n\
1800 -Wcomment{s} Warn if one comment starts inside another\n\
1801 -Wno-comment{s} Do not warn about comments\n\
1802 -Wtraditional Warn if a macro argument is/would be turned into\n\
1803 a string if -traditional is specified\n\
1804 -Wno-traditional Do not warn about stringification\n\
1805 -Wundef Warn if an undefined macro is used by #if\n\
1806 -Wno-undef Do not warn about testing undefined macros\n\
1807 -Wimport Warn about the use of the #import directive\n\
1808 -Wno-import Do not warn about the use of #import\n\
1809 -Werror Treat all warnings as errors\n\
1810 -Wno-error Do not treat warnings as errors\n\
1811 -Wall Enable all preprocessor warnings\n\
1812 -M Generate make dependencies\n\
1813 -MM As -M, but ignore system header files\n\
1814 -MD As -M, but put output in a .d file\n\
1815 -MMD As -MD, but ignore system header files\n\
1816 -MG Treat missing header file as generated files\n\
1817 -g3 Include #define and #undef directives in the output\n\
1818 -D<macro> Define a <macro> with string '1' as its value\n\
1819 -D<macro>=<val> Define a <macro> with <val> as its value\n\
1820 -A<question> (<answer>) Assert the <answer> to <question>\n\
1821 -A-<question> (<answer>) Disable the <answer> to <question>\n\
1822 -U<macro> Undefine <macro> \n\
1823 -v Display the version number\n\
1824 -H Print the name of header files as they are used\n\
1825 -C Do not discard comments\n\
1826 -dM Display a list of macro definitions active at end\n\
1827 -dD Preserve macro definitions in output\n\
1828 -dN As -dD except that only the names are preserved\n\
1829 -dI Include #include directives in the output\n\
1830 -P Do not generate #line directives\n\
1831 -$ Do not allow '$' in identifiers\n\
1832 -remap Remap file names when including files.\n\
1833 --version Display version information\n\
1834 -h or --help Display this information\n\