]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cppinit.c
* .cvsignore: Remove files that are present in CVS.
[thirdparty/gcc.git] / gcc / cppinit.c
CommitLineData
5538ada6 1/* CPP Library.
5e7b4e25 2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
745b26b3 3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
5538ada6
ZW
4 Contributed by Per Bothner, 1994-95.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
7
8This program is free software; you can redistribute it and/or modify it
9under the terms of the GNU General Public License as published by the
10Free Software Foundation; either version 2, or (at your option) any
11later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
5538ada6
ZW
22#include "config.h"
23#include "system.h"
6de1e2a9
ZW
24#include "cpplib.h"
25#include "cpphash.h"
6de1e2a9
ZW
26#include "prefix.h"
27#include "intl.h"
9f8f4efe 28#include "version.h"
49e6c08e 29#include "mkdeps.h"
60893f43 30#include "cppdefault.h"
0d24f4d1 31#include "except.h" /* for USING_SJLJ_EXCEPTIONS */
6de1e2a9 32
4a58aab6 33/* Predefined symbols, built-in macros, and the default include path. */
6de1e2a9
ZW
34
35#ifndef GET_ENV_PATH_LIST
36#define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
37#endif
38
88ae23e7
ZW
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.
ec5c56db 41 VMS has non-numeric inodes. */
88ae23e7 42#ifdef VMS
ca47c89e
DR
43# define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
44# define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
88ae23e7 45#else
3943e756 46# if (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
ca47c89e 47# define INO_T_EQ(A, B) 0
3943e756 48# else
ca47c89e 49# define INO_T_EQ(A, B) ((A) == (B))
3943e756 50# endif
ca47c89e 51# define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
88ae23e7
ZW
52#endif
53
4a58aab6 54/* Internal structures and prototypes. */
6de1e2a9 55
4a58aab6
NB
56/* A `struct pending_option' remembers one -D, -A, -U, -include, or
57 -imacros switch. */
8be1ddca 58typedef void (* cl_directive_handler) PARAMS ((cpp_reader *, const char *));
0b22d65c 59struct pending_option
6de1e2a9 60{
0b22d65c 61 struct pending_option *next;
7ceb3598 62 const char *arg;
40eac643 63 cl_directive_handler handler;
6de1e2a9 64};
0b22d65c 65
88ae23e7 66/* The `pending' structure accumulates all the options that are not
f5e99456 67 actually processed until we hit cpp_read_main_file. It consists of
88ae23e7 68 several lists, one for each type of option. We keep both head and
4a58aab6 69 tail pointers for quick insertion. */
88ae23e7
ZW
70struct cpp_pending
71{
40eac643 72 struct pending_option *directive_head, *directive_tail;
88ae23e7 73
591e15a1
NB
74 struct search_path *quote_head, *quote_tail;
75 struct search_path *brack_head, *brack_tail;
76 struct search_path *systm_head, *systm_tail;
77 struct search_path *after_head, *after_tail;
88ae23e7
ZW
78
79 struct pending_option *imacros_head, *imacros_tail;
80 struct pending_option *include_head, *include_tail;
81};
82
0b22d65c
ZW
83#ifdef __STDC__
84#define APPEND(pend, list, elt) \
85 do { if (!(pend)->list##_head) (pend)->list##_head = (elt); \
86 else (pend)->list##_tail->next = (elt); \
87 (pend)->list##_tail = (elt); \
88 } while (0)
89#else
90#define APPEND(pend, list, elt) \
91 do { if (!(pend)->list/**/_head) (pend)->list/**/_head = (elt); \
92 else (pend)->list/**/_tail->next = (elt); \
93 (pend)->list/**/_tail = (elt); \
94 } while (0)
95#endif
6de1e2a9 96
6de1e2a9 97static void print_help PARAMS ((void));
0b22d65c 98static void path_include PARAMS ((cpp_reader *,
0b22d65c 99 char *, int));
674c3b40 100static void init_library PARAMS ((void));
7ca3d2b1 101static void init_builtins PARAMS ((cpp_reader *));
0b22d65c 102static void append_include_chain PARAMS ((cpp_reader *,
c45da1ca 103 char *, int, int));
002ee64f 104static struct search_path * remove_dup_dir PARAMS ((cpp_reader *,
591e15a1 105 struct search_path *));
002ee64f 106static struct search_path * remove_dup_dirs PARAMS ((cpp_reader *,
591e15a1 107 struct search_path *));
ae79697b 108static void merge_include_chains PARAMS ((cpp_reader *));
d7bc7a98
NB
109static bool push_include PARAMS ((cpp_reader *,
110 struct pending_option *));
111static void free_chain PARAMS ((struct pending_option *));
dd07b884 112static void set_lang PARAMS ((cpp_reader *, enum c_lang));
7ca3d2b1
NB
113static void init_dependency_output PARAMS ((cpp_reader *));
114static void init_standard_includes PARAMS ((cpp_reader *));
f5e99456 115static void read_original_filename PARAMS ((cpp_reader *));
2c0accc9 116static void new_pending_directive PARAMS ((struct cpp_pending *,
40eac643
NB
117 const char *,
118 cl_directive_handler));
7ca3d2b1 119static void output_deps PARAMS ((cpp_reader *));
e23c0ba3 120static int parse_option PARAMS ((const char *));
6de1e2a9 121
cb773845
ZW
122/* Fourth argument to append_include_chain: chain to use.
123 Note it's never asked to append to the quote chain. */
124enum { BRACKET = 0, SYSTEM, AFTER };
6de1e2a9 125
61d0346d
NB
126/* If we have designated initializers (GCC >2.7) these tables can be
127 initialized, constant data. Otherwise, they have to be filled in at
12cf91fe 128 runtime. */
61d0346d 129#if HAVE_DESIGNATED_INITIALIZERS
a9ae4483 130
4a58aab6 131#define init_trigraph_map() /* Nothing. */
61d0346d 132#define TRIGRAPH_MAP \
562a5c27 133__extension__ const uchar _cpp_trigraph_map[UCHAR_MAX + 1] = {
61d0346d 134
a9ae4483 135#define END };
455d2586 136#define s(p, v) [p] = v,
61d0346d 137
a9ae4483 138#else
61d0346d 139
562a5c27 140#define TRIGRAPH_MAP uchar _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
61d0346d
NB
141 static void init_trigraph_map PARAMS ((void)) { \
142 unsigned char *x = _cpp_trigraph_map;
143
ae79697b 144#define END }
455d2586 145#define s(p, v) x[p] = v;
61d0346d 146
a9ae4483 147#endif
6de1e2a9 148
61d0346d
NB
149TRIGRAPH_MAP
150 s('=', '#') s(')', ']') s('!', '|')
151 s('(', '[') s('\'', '^') s('>', '}')
152 s('/', '\\') s('<', '{') s('-', '~')
153END
154
a9ae4483 155#undef s
455d2586 156#undef END
61d0346d 157#undef TRIGRAPH_MAP
6de1e2a9
ZW
158
159/* Given a colon-separated list of file names PATH,
160 add all the names to the search path for include files. */
6de1e2a9 161static void
e33f6253 162path_include (pfile, list, path)
6de1e2a9 163 cpp_reader *pfile;
0b22d65c
ZW
164 char *list;
165 int path;
6de1e2a9 166{
0b22d65c 167 char *p, *q, *name;
6de1e2a9 168
0b22d65c 169 p = list;
6de1e2a9 170
0b22d65c
ZW
171 do
172 {
6de1e2a9 173 /* Find the end of this name. */
0b22d65c 174 q = p;
6de1e2a9 175 while (*q != 0 && *q != PATH_SEPARATOR) q++;
0b22d65c
ZW
176 if (q == p)
177 {
178 /* An empty name in the path stands for the current directory. */
179 name = (char *) xmalloc (2);
180 name[0] = '.';
181 name[1] = 0;
182 }
183 else
184 {
185 /* Otherwise use the directory that is named. */
186 name = (char *) xmalloc (q - p + 1);
187 memcpy (name, p, q - p);
188 name[q - p] = 0;
189 }
6de1e2a9 190
e33f6253 191 append_include_chain (pfile, name, path, 0);
6de1e2a9
ZW
192
193 /* Advance past this name. */
0b22d65c 194 if (*q == 0)
6de1e2a9 195 break;
0b22d65c
ZW
196 p = q + 1;
197 }
198 while (1);
199}
200
bef985f3 201/* Append DIR to include path PATH. DIR must be allocated on the
5d8ebbd8
NB
202 heap; this routine takes responsibility for freeing it. CXX_AWARE
203 is non-zero if the header contains extern "C" guards for C++,
204 otherwise it is zero. */
0b22d65c 205static void
e33f6253 206append_include_chain (pfile, dir, path, cxx_aware)
0b22d65c 207 cpp_reader *pfile;
0b22d65c
ZW
208 char *dir;
209 int path;
7d4918a2 210 int cxx_aware ATTRIBUTE_UNUSED;
0b22d65c 211{
e33f6253 212 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
591e15a1 213 struct search_path *new;
0b22d65c
ZW
214 struct stat st;
215 unsigned int len;
216
f9200da2 217 if (*dir == '\0')
bef985f3
NB
218 {
219 free (dir);
220 dir = xstrdup (".");
221 }
b0699dad 222 _cpp_simplify_pathname (dir);
bef985f3 223
0b22d65c
ZW
224 if (stat (dir, &st))
225 {
f9200da2 226 /* Dirs that don't exist are silently ignored. */
0b22d65c 227 if (errno != ENOENT)
ebef4e8c 228 cpp_errno (pfile, DL_ERROR, dir);
ae79697b 229 else if (CPP_OPTION (pfile, verbose))
041c3194 230 fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"), dir);
bef985f3 231 free (dir);
0b22d65c
ZW
232 return;
233 }
234
235 if (!S_ISDIR (st.st_mode))
236 {
ebef4e8c 237 cpp_error_with_line (pfile, DL_ERROR, 0, 0, "%s: Not a directory", dir);
bef985f3 238 free (dir);
0b22d65c
ZW
239 return;
240 }
241
242 len = strlen (dir);
243 if (len > pfile->max_include_len)
244 pfile->max_include_len = len;
ae79697b 245
591e15a1 246 new = (struct search_path *) xmalloc (sizeof (struct search_path));
0b22d65c 247 new->name = dir;
591e15a1 248 new->len = len;
ca47c89e 249 INO_T_COPY (new->ino, st.st_ino);
0b22d65c 250 new->dev = st.st_dev;
4737b274
CF
251 /* Both systm and after include file lists should be treated as system
252 include files since these two lists are really just a concatenation
ec5c56db 253 of one "system" list. */
4737b274 254 if (path == SYSTEM || path == AFTER)
52b357ea
JJ
255#ifdef NO_IMPLICIT_EXTERN_C
256 new->sysp = 1;
257#else
c45da1ca 258 new->sysp = cxx_aware ? 1 : 2;
52b357ea 259#endif
c45da1ca
ZW
260 else
261 new->sysp = 0;
0b22d65c 262 new->name_map = NULL;
503cb436 263 new->next = NULL;
0b22d65c
ZW
264
265 switch (path)
266 {
0b22d65c
ZW
267 case BRACKET: APPEND (pend, brack, new); break;
268 case SYSTEM: APPEND (pend, systm, new); break;
269 case AFTER: APPEND (pend, after, new); break;
6de1e2a9
ZW
270 }
271}
272
dd69c71b
NB
273/* Handle a duplicated include path. PREV is the link in the chain
274 before the duplicate. The duplicate is removed from the chain and
275 freed. Returns PREV. */
002ee64f 276static struct search_path *
dd69c71b
NB
277remove_dup_dir (pfile, prev)
278 cpp_reader *pfile;
591e15a1 279 struct search_path *prev;
dd69c71b 280{
591e15a1 281 struct search_path *cur = prev->next;
dd69c71b
NB
282
283 if (CPP_OPTION (pfile, verbose))
284 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), cur->name);
285
286 prev->next = cur->next;
591e15a1 287 free ((PTR) cur->name);
dd69c71b
NB
288 free (cur);
289
290 return prev;
291}
292
293/* Remove duplicate directories from a chain. Returns the tail of the
294 chain, or NULL if the chain is empty. This algorithm is quadratic
295 in the number of -I switches, which is acceptable since there
296 aren't usually that many of them. */
002ee64f 297static struct search_path *
dd69c71b
NB
298remove_dup_dirs (pfile, head)
299 cpp_reader *pfile;
591e15a1 300 struct search_path *head;
dd69c71b 301{
591e15a1 302 struct search_path *prev = NULL, *cur, *other;
dd69c71b
NB
303
304 for (cur = head; cur; cur = cur->next)
305 {
306 for (other = head; other != cur; other = other->next)
307 if (INO_T_EQ (cur->ino, other->ino) && cur->dev == other->dev)
308 {
002ee64f 309 if (cur->sysp && !other->sysp)
dbead49c 310 {
ebef4e8c
NB
311 cpp_error (pfile, DL_WARNING,
312 "changing search order for system directory \"%s\"",
313 cur->name);
dbead49c 314 if (strcmp (cur->name, other->name))
ebef4e8c
NB
315 cpp_error (pfile, DL_WARNING,
316 " as it is the same as non-system directory \"%s\"",
317 other->name);
dbead49c 318 else
ebef4e8c
NB
319 cpp_error (pfile, DL_WARNING,
320 " as it has already been specified as a non-system directory");
dbead49c 321 }
dd69c71b
NB
322 cur = remove_dup_dir (pfile, prev);
323 break;
324 }
325 prev = cur;
326 }
327
328 return prev;
329}
330
88ae23e7
ZW
331/* Merge the four include chains together in the order quote, bracket,
332 system, after. Remove duplicate dirs (as determined by
333 INO_T_EQ()). The system_include and after_include chains are never
334 referred to again after this function; all access is through the
5d8ebbd8 335 bracket_include path. */
88ae23e7 336static void
ae79697b
ZW
337merge_include_chains (pfile)
338 cpp_reader *pfile;
88ae23e7 339{
591e15a1 340 struct search_path *quote, *brack, *systm, *qtail;
88ae23e7 341
ae79697b 342 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
88ae23e7 343
ae79697b
ZW
344 quote = pend->quote_head;
345 brack = pend->brack_head;
346 systm = pend->systm_head;
dd69c71b 347 qtail = pend->quote_tail;
88ae23e7 348
dd69c71b
NB
349 /* Paste together bracket, system, and after include chains. */
350 if (systm)
351 pend->systm_tail->next = pend->after_head;
88ae23e7 352 else
dd69c71b
NB
353 systm = pend->after_head;
354
355 if (brack)
356 pend->brack_tail->next = systm;
88ae23e7
ZW
357 else
358 brack = systm;
359
dd69c71b
NB
360 /* This is a bit tricky. First we drop dupes from the quote-include
361 list. Then we drop dupes from the bracket-include list.
362 Finally, if qtail and brack are the same directory, we cut out
cb773845 363 brack and move brack up to point to qtail.
88ae23e7
ZW
364
365 We can't just merge the lists and then uniquify them because
366 then we may lose directories from the <> search path that should
367 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
368 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
dd69c71b 369 -Ibar -I- -Ifoo -Iquux. */
88ae23e7 370
dd69c71b
NB
371 remove_dup_dirs (pfile, brack);
372 qtail = remove_dup_dirs (pfile, quote);
88ae23e7
ZW
373
374 if (quote)
375 {
dd69c71b
NB
376 qtail->next = brack;
377
378 /* If brack == qtail, remove brack as it's simpler. */
afb58288
ZW
379 if (brack && INO_T_EQ (qtail->ino, brack->ino)
380 && qtail->dev == brack->dev)
dd69c71b 381 brack = remove_dup_dir (pfile, qtail);
88ae23e7
ZW
382 }
383 else
bef985f3 384 quote = brack;
88ae23e7 385
ae79697b
ZW
386 CPP_OPTION (pfile, quote_include) = quote;
387 CPP_OPTION (pfile, bracket_include) = brack;
88ae23e7
ZW
388}
389
5d8ebbd8
NB
390/* A set of booleans indicating what CPP features each source language
391 requires. */
a01eb545
ZW
392struct lang_flags
393{
394 char c99;
395 char objc;
396 char cplusplus;
397 char extended_numbers;
398 char trigraphs;
399 char dollars_in_ident;
400 char cplusplus_comments;
401 char digraphs;
402};
403
404/* ??? Enable $ in identifiers in assembly? */
405static const struct lang_flags lang_defaults[] =
406{ /* c99 objc c++ xnum trig dollar c++comm digr */
407 /* GNUC89 */ { 0, 0, 0, 1, 0, 1, 1, 1 },
408 /* GNUC99 */ { 1, 0, 0, 1, 0, 1, 1, 1 },
409 /* STDC89 */ { 0, 0, 0, 0, 1, 0, 0, 0 },
410 /* STDC94 */ { 0, 0, 0, 0, 1, 0, 0, 1 },
411 /* STDC99 */ { 1, 0, 0, 1, 1, 0, 1, 1 },
412 /* GNUCXX */ { 0, 0, 1, 1, 0, 1, 1, 1 },
413 /* CXX98 */ { 0, 0, 1, 1, 1, 0, 1, 1 },
414 /* OBJC */ { 0, 1, 0, 1, 0, 1, 1, 1 },
415 /* OBJCXX */ { 0, 1, 1, 1, 0, 1, 1, 1 },
416 /* ASM */ { 0, 0, 0, 1, 0, 0, 1, 0 }
417};
418
5d8ebbd8 419/* Sets internal flags correctly for a given language. */
dd07b884
NB
420static void
421set_lang (pfile, lang)
422 cpp_reader *pfile;
423 enum c_lang lang;
424{
a01eb545
ZW
425 const struct lang_flags *l = &lang_defaults[(int) lang];
426
bdb05a7b 427 CPP_OPTION (pfile, lang) = lang;
dd07b884 428
a01eb545
ZW
429 CPP_OPTION (pfile, c99) = l->c99;
430 CPP_OPTION (pfile, objc) = l->objc;
431 CPP_OPTION (pfile, cplusplus) = l->cplusplus;
432 CPP_OPTION (pfile, extended_numbers) = l->extended_numbers;
433 CPP_OPTION (pfile, trigraphs) = l->trigraphs;
434 CPP_OPTION (pfile, dollars_in_ident) = l->dollars_in_ident;
435 CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments;
436 CPP_OPTION (pfile, digraphs) = l->digraphs;
dd07b884
NB
437}
438
7ca3d2b1
NB
439#ifdef HOST_EBCDIC
440static int opt_comp PARAMS ((const void *, const void *));
441
442/* Run-time sorting of options array. */
443static int
444opt_comp (p1, p2)
445 const void *p1, *p2;
446{
447 return strcmp (((struct cl_option *) p1)->opt_text,
448 ((struct cl_option *) p2)->opt_text);
449}
450#endif
cf44ea52 451
7ca3d2b1
NB
452/* init initializes library global state. It might not need to
453 do anything depending on the platform and compiler. */
cf44ea52 454static void
674c3b40 455init_library ()
cf44ea52 456{
7ca3d2b1
NB
457 static int initialized = 0;
458
459 if (! initialized)
460 {
461 initialized = 1;
462
cf44ea52 463#ifdef HOST_EBCDIC
7ca3d2b1
NB
464 /* For non-ASCII hosts, the cl_options array needs to be sorted at
465 runtime. */
466 qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
cf44ea52
NB
467#endif
468
7ca3d2b1
NB
469 /* Set up the trigraph map. This doesn't need to do anything if
470 we were compiled with a compiler that supports C99 designated
471 initializers. */
472 init_trigraph_map ();
473 }
cf44ea52
NB
474}
475
ec5c56db 476/* Initialize a cpp_reader structure. */
cf44ea52 477cpp_reader *
f5e99456 478cpp_create_reader (lang)
dd07b884 479 enum c_lang lang;
6de1e2a9 480{
7ca3d2b1 481 cpp_reader *pfile;
93c80368 482
cf44ea52 483 /* Initialise this instance of the library if it hasn't been already. */
674c3b40 484 init_library ();
7ca3d2b1
NB
485
486 pfile = (cpp_reader *) xcalloc (1, sizeof (cpp_reader));
93c80368 487
c740cee2 488 set_lang (pfile, lang);
ae79697b
ZW
489 CPP_OPTION (pfile, warn_import) = 1;
490 CPP_OPTION (pfile, discard_comments) = 1;
477cdac7 491 CPP_OPTION (pfile, discard_comments_in_macro_exp) = 1;
ae79697b 492 CPP_OPTION (pfile, show_column) = 1;
6ab3e7dd 493 CPP_OPTION (pfile, tabstop) = 8;
be768055 494 CPP_OPTION (pfile, operator_names) = 1;
909de5da 495 CPP_OPTION (pfile, warn_endif_labels) = 1;
0fef3fd0
NB
496#if DEFAULT_SIGNED_CHAR
497 CPP_OPTION (pfile, signed_char) = 1;
498#else
499 CPP_OPTION (pfile, signed_char) = 0;
500#endif
ae79697b
ZW
501
502 CPP_OPTION (pfile, pending) =
503 (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
504
f7114e17
NB
505 /* It's simplest to just create this struct whether or not it will
506 be needed. */
507 pfile->deps = deps_init ();
508
50410426
NB
509 /* Initialise the line map. Start at logical line 1, so we can use
510 a line number of zero for special states. */
d82fc108 511 init_line_maps (&pfile->line_maps);
50410426 512 pfile->line = 1;
d82fc108 513
4a58aab6 514 /* Initialize lexer state. */
93c80368
NB
515 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
516
4ed5bcfb 517 /* Set up static tokens. */
93c80368 518 pfile->date.type = CPP_EOF;
4ed5bcfb
NB
519 pfile->avoid_paste.type = CPP_PADDING;
520 pfile->avoid_paste.val.source = NULL;
521 pfile->eof.type = CPP_EOF;
522 pfile->eof.flags = 0;
93c80368 523
5fddcffc
NB
524 /* Create a token buffer for the lexer. */
525 _cpp_init_tokenrun (&pfile->base_run, 250);
526 pfile->cur_run = &pfile->base_run;
527 pfile->cur_token = pfile->base_run.base;
5fddcffc 528
93c80368
NB
529 /* Initialise the base context. */
530 pfile->context = &pfile->base_context;
531 pfile->base_context.macro = 0;
532 pfile->base_context.prev = pfile->base_context.next = 0;
533
8c3b2693
NB
534 /* Aligned and unaligned storage. */
535 pfile->a_buff = _cpp_get_buff (pfile, 0);
ece54d54 536 pfile->u_buff = _cpp_get_buff (pfile, 0);
93c80368 537
2a967f3d
NB
538 /* Initialise the buffer obstack. */
539 gcc_obstack_init (&pfile->buffer_ob);
540
c71f835b 541 _cpp_init_includes (pfile);
cf44ea52
NB
542
543 return pfile;
f2d5f0cc
ZW
544}
545
400023a3 546/* Free resources used by PFILE. Accessing PFILE after this function
5d8ebbd8 547 returns leads to undefined behaviour. Returns the error count. */
400023a3
NB
548int
549cpp_destroy (pfile)
6de1e2a9
ZW
550 cpp_reader *pfile;
551{
400023a3 552 int result;
591e15a1 553 struct search_path *dir, *dirn;
93c80368 554 cpp_context *context, *contextn;
50410426 555 tokenrun *run, *runn;
709e9e50 556
af0d16cd
NB
557 free_chain (CPP_OPTION (pfile, pending)->include_head);
558 free (CPP_OPTION (pfile, pending));
559
38b24ee2 560 while (CPP_BUFFER (pfile) != NULL)
ef6e958a 561 _cpp_pop_buffer (pfile);
6de1e2a9 562
93c80368 563 if (pfile->macro_buffer)
4b49c365
AO
564 {
565 free ((PTR) pfile->macro_buffer);
566 pfile->macro_buffer = NULL;
567 pfile->macro_buffer_len = 0;
568 }
93c80368 569
f7114e17 570 deps_free (pfile->deps);
2a967f3d 571 obstack_free (&pfile->buffer_ob, 0);
49e6c08e 572
2a967f3d 573 _cpp_destroy_hashtable (pfile);
bfb9dc7f 574 _cpp_cleanup_includes (pfile);
709e9e50 575
8c3b2693 576 _cpp_free_buff (pfile->a_buff);
ece54d54 577 _cpp_free_buff (pfile->u_buff);
b8af0ca5 578 _cpp_free_buff (pfile->free_buffs);
93c80368 579
50410426
NB
580 for (run = &pfile->base_run; run; run = runn)
581 {
582 runn = run->next;
583 free (run->base);
584 if (run != &pfile->base_run)
585 free (run);
586 }
587
93c80368 588 for (dir = CPP_OPTION (pfile, quote_include); dir; dir = dirn)
709e9e50 589 {
93c80368 590 dirn = dir->next;
591e15a1 591 free ((PTR) dir->name);
709e9e50
NB
592 free (dir);
593 }
93c80368
NB
594
595 for (context = pfile->base_context.next; context; context = contextn)
596 {
597 contextn = context->next;
598 free (context);
599 }
400023a3 600
d82fc108
NB
601 free_line_maps (&pfile->line_maps);
602
400023a3
NB
603 result = pfile->errors;
604 free (pfile);
605
606 return result;
6de1e2a9
ZW
607}
608
609
93c80368
NB
610/* This structure defines one built-in identifier. A node will be
611 entered in the hash table under the name NAME, with value VALUE (if
612 any). If flags has OPERATOR, the node's operator field is used; if
618cdda7
NB
613 flags has BUILTIN the node's builtin field is used. Macros that are
614 known at build time should not be flagged BUILTIN, as then they do
615 not appear in macro dumps with e.g. -dM or -dD.
92936ecf
ZW
616
617 Two values are not compile time constants, so we tag
041c3194 618 them in the FLAGS field instead:
8c389f84
ZW
619 VERS value is the global version_string, quoted
620 ULP value is the global user_label_prefix
92936ecf 621
93c80368 622 Also, macros with CPLUS set in the flags field are entered only for C++. */
a9ae4483
ZW
623struct builtin
624{
562a5c27 625 const uchar *name;
041c3194 626 const char *value;
93c80368
NB
627 unsigned char builtin;
628 unsigned char operator;
a9ae4483 629 unsigned short flags;
93c80368 630 unsigned short len;
a9ae4483 631};
93c80368
NB
632#define VERS 0x01
633#define ULP 0x02
634#define CPLUS 0x04
635#define BUILTIN 0x08
636#define OPERATOR 0x10
637
638#define B(n, t) { U n, 0, t, 0, BUILTIN, sizeof n - 1 }
639#define C(n, v) { U n, v, 0, 0, 0, sizeof n - 1 }
640#define X(n, f) { U n, 0, 0, 0, f, sizeof n - 1 }
641#define O(n, c, f) { U n, 0, 0, c, OPERATOR | f, sizeof n - 1 }
a9ae4483 642static const struct builtin builtin_array[] =
6de1e2a9 643{
93c80368
NB
644 B("__TIME__", BT_TIME),
645 B("__DATE__", BT_DATE),
646 B("__FILE__", BT_FILE),
647 B("__BASE_FILE__", BT_BASE_FILE),
648 B("__LINE__", BT_SPECLINE),
649 B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL),
644eddaa 650 B("_Pragma", BT_PRAGMA),
12cf91fe 651
041c3194
ZW
652 X("__VERSION__", VERS),
653 X("__USER_LABEL_PREFIX__", ULP),
12cf91fe
ZW
654 C("__REGISTER_PREFIX__", REGISTER_PREFIX),
655 C("__HAVE_BUILTIN_SETJMP__", "1"),
0d24f4d1
ZW
656#if USING_SJLJ_EXCEPTIONS
657 /* libgcc needs to know this. */
658 C("__USING_SJLJ_EXCEPTIONS__","1"),
659#endif
6de1e2a9 660#ifndef NO_BUILTIN_SIZE_TYPE
12cf91fe 661 C("__SIZE_TYPE__", SIZE_TYPE),
6de1e2a9
ZW
662#endif
663#ifndef NO_BUILTIN_PTRDIFF_TYPE
12cf91fe 664 C("__PTRDIFF_TYPE__", PTRDIFF_TYPE),
6de1e2a9 665#endif
0209c340 666#ifndef NO_BUILTIN_WCHAR_TYPE
12cf91fe 667 C("__WCHAR_TYPE__", WCHAR_TYPE),
0209c340 668#endif
d6777972
JM
669#ifndef NO_BUILTIN_WINT_TYPE
670 C("__WINT_TYPE__", WINT_TYPE),
671#endif
618cdda7
NB
672#ifdef STDC_0_IN_SYSTEM_HEADERS
673 B("__STDC__", BT_STDC),
674#else
675 C("__STDC__", "1"),
676#endif
92936ecf
ZW
677
678 /* Named operators known to the preprocessor. These cannot be #defined
679 and always have their stated meaning. They are treated like normal
93c80368 680 identifiers except for the type code and the meaning. Most of them
92936ecf 681 are only for C++ (but see iso646.h). */
92936ecf
ZW
682 O("and", CPP_AND_AND, CPLUS),
683 O("and_eq", CPP_AND_EQ, CPLUS),
684 O("bitand", CPP_AND, CPLUS),
685 O("bitor", CPP_OR, CPLUS),
686 O("compl", CPP_COMPL, CPLUS),
687 O("not", CPP_NOT, CPLUS),
688 O("not_eq", CPP_NOT_EQ, CPLUS),
689 O("or", CPP_OR_OR, CPLUS),
690 O("or_eq", CPP_OR_EQ, CPLUS),
691 O("xor", CPP_XOR, CPLUS),
93c80368 692 O("xor_eq", CPP_XOR_EQ, CPLUS)
a9ae4483 693};
12cf91fe
ZW
694#undef B
695#undef C
696#undef X
be768055 697#undef O
ca7558fc 698#define builtin_array_end (builtin_array + ARRAY_SIZE (builtin_array))
a9ae4483 699
f5e99456 700/* Subroutine of cpp_read_main_file; reads the builtins table above and
5d8ebbd8 701 enters them, and language-specific macros, into the hash table. */
a9ae4483 702static void
7ca3d2b1 703init_builtins (pfile)
a9ae4483
ZW
704 cpp_reader *pfile;
705{
a9ae4483 706 const struct builtin *b;
771c4df3 707
8c389f84 708 for(b = builtin_array; b < builtin_array_end; b++)
6de1e2a9 709 {
93c80368 710 if ((b->flags & CPLUS) && ! CPP_OPTION (pfile, cplusplus))
92936ecf
ZW
711 continue;
712
be768055
JJ
713 if ((b->flags & OPERATOR) && ! CPP_OPTION (pfile, operator_names))
714 continue;
715
93c80368
NB
716 if (b->flags & (OPERATOR | BUILTIN))
717 {
718 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
719 if (b->flags & OPERATOR)
720 {
721 hp->flags |= NODE_OPERATOR;
722 hp->value.operator = b->operator;
723 }
724 else
725 {
726 hp->type = NT_MACRO;
618cdda7 727 hp->flags |= NODE_BUILTIN | NODE_WARN;
93c80368
NB
728 hp->value.builtin = b->builtin;
729 }
730 }
731 else /* A standard macro of some kind. */
8c389f84 732 {
041c3194
ZW
733 const char *val;
734 char *str;
735
736 if (b->flags & VERS)
737 {
2c8f0515
ZW
738 /* Allocate enough space for 'name "value"\n\0'. */
739 str = alloca (b->len + strlen (version_string) + 5);
740 sprintf (str, "%s \"%s\"\n", b->name, version_string);
041c3194
ZW
741 }
742 else
743 {
744 if (b->flags & ULP)
771c4df3 745 val = CPP_OPTION (pfile, user_label_prefix);
041c3194
ZW
746 else
747 val = b->value;
748
2c8f0515
ZW
749 /* Allocate enough space for "name value\n\0". */
750 str = alloca (b->len + strlen (val) + 3);
751 sprintf(str, "%s %s\n", b->name, val);
041c3194 752 }
c154ba66 753
2c8f0515 754 _cpp_define_builtin (pfile, str);
8c389f84 755 }
6de1e2a9 756 }
c740cee2
NB
757
758 if (CPP_OPTION (pfile, cplusplus))
618cdda7
NB
759 {
760 _cpp_define_builtin (pfile, "__cplusplus 1");
761 if (SUPPORTS_ONE_ONLY)
762 _cpp_define_builtin (pfile, "__GXX_WEAK__ 1");
763 else
764 _cpp_define_builtin (pfile, "__GXX_WEAK__ 0");
765 }
c740cee2
NB
766 if (CPP_OPTION (pfile, objc))
767 _cpp_define_builtin (pfile, "__OBJC__ 1");
768
769 if (CPP_OPTION (pfile, lang) == CLK_STDC94)
770 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
771 else if (CPP_OPTION (pfile, c99))
772 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
773
0fef3fd0
NB
774 if (CPP_OPTION (pfile, signed_char) == 0)
775 _cpp_define_builtin (pfile, "__CHAR_UNSIGNED__ 1");
776
c740cee2
NB
777 if (CPP_OPTION (pfile, lang) == CLK_STDC89
778 || CPP_OPTION (pfile, lang) == CLK_STDC94
779 || CPP_OPTION (pfile, lang) == CLK_STDC99)
780 _cpp_define_builtin (pfile, "__STRICT_ANSI__ 1");
781 else if (CPP_OPTION (pfile, lang) == CLK_ASM)
782 _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
6de1e2a9 783}
93c80368
NB
784#undef BUILTIN
785#undef OPERATOR
6feb7728 786#undef VERS
a9ae4483 787#undef ULP
93c80368 788#undef CPLUS
041c3194 789#undef builtin_array_end
6de1e2a9 790
c45da1ca
ZW
791/* And another subroutine. This one sets up the standard include path. */
792static void
7ca3d2b1 793init_standard_includes (pfile)
c45da1ca
ZW
794 cpp_reader *pfile;
795{
c45da1ca 796 char *path;
455d2586 797 const struct default_include *p;
ae79697b 798 const char *specd_prefix = CPP_OPTION (pfile, include_prefix);
c45da1ca
ZW
799
800 /* Several environment variables may add to the include search path.
801 CPATH specifies an additional list of directories to be searched
802 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
803 etc. specify an additional list of directories to be searched as
804 if specified with -isystem, for the language indicated. */
805
806 GET_ENV_PATH_LIST (path, "CPATH");
807 if (path != 0 && *path != 0)
e33f6253 808 path_include (pfile, path, BRACKET);
c45da1ca 809
ae79697b 810 switch ((CPP_OPTION (pfile, objc) << 1) + CPP_OPTION (pfile, cplusplus))
c45da1ca
ZW
811 {
812 case 0:
813 GET_ENV_PATH_LIST (path, "C_INCLUDE_PATH");
814 break;
815 case 1:
816 GET_ENV_PATH_LIST (path, "CPLUS_INCLUDE_PATH");
817 break;
818 case 2:
819 GET_ENV_PATH_LIST (path, "OBJC_INCLUDE_PATH");
820 break;
821 case 3:
822 GET_ENV_PATH_LIST (path, "OBJCPLUS_INCLUDE_PATH");
823 break;
824 }
825 if (path != 0 && *path != 0)
e33f6253 826 path_include (pfile, path, SYSTEM);
c45da1ca
ZW
827
828 /* Search "translated" versions of GNU directories.
829 These have /usr/local/lib/gcc... replaced by specd_prefix. */
60893f43 830 if (specd_prefix != 0 && cpp_GCC_INCLUDE_DIR_len)
c45da1ca 831 {
c45da1ca 832 /* Remove the `include' from /usr/local/lib/gcc.../include.
ec5c56db 833 GCC_INCLUDE_DIR will always end in /include. */
60893f43
ZW
834 int default_len = cpp_GCC_INCLUDE_DIR_len;
835 char *default_prefix = (char *) alloca (default_len + 1);
c45da1ca
ZW
836 int specd_len = strlen (specd_prefix);
837
60893f43 838 memcpy (default_prefix, cpp_GCC_INCLUDE_DIR, default_len);
c45da1ca
ZW
839 default_prefix[default_len] = '\0';
840
60893f43 841 for (p = cpp_include_defaults; p->fname; p++)
c45da1ca
ZW
842 {
843 /* Some standard dirs are only for C++. */
844 if (!p->cplusplus
ae79697b
ZW
845 || (CPP_OPTION (pfile, cplusplus)
846 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
c45da1ca
ZW
847 {
848 /* Does this dir start with the prefix? */
61d0346d 849 if (!memcmp (p->fname, default_prefix, default_len))
c45da1ca
ZW
850 {
851 /* Yes; change prefix and add to search list. */
852 int flen = strlen (p->fname);
853 int this_len = specd_len + flen - default_len;
854 char *str = (char *) xmalloc (this_len + 1);
855 memcpy (str, specd_prefix, specd_len);
856 memcpy (str + specd_len,
857 p->fname + default_len,
858 flen - default_len + 1);
859
e33f6253 860 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
c45da1ca
ZW
861 }
862 }
863 }
864 }
865
866 /* Search ordinary names for GNU include directories. */
60893f43 867 for (p = cpp_include_defaults; p->fname; p++)
c45da1ca
ZW
868 {
869 /* Some standard dirs are only for C++. */
870 if (!p->cplusplus
ae79697b
ZW
871 || (CPP_OPTION (pfile, cplusplus)
872 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
c45da1ca 873 {
51c04256 874 char *str = update_path (p->fname, p->component);
e33f6253 875 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
c45da1ca
ZW
876 }
877 }
878}
879
5d8ebbd8 880/* Pushes a command line -imacro and -include file indicated by P onto
d7bc7a98
NB
881 the buffer stack. Returns non-zero if successful. */
882static bool
883push_include (pfile, p)
4a58aab6
NB
884 cpp_reader *pfile;
885 struct pending_option *p;
4a58aab6 886{
d7bc7a98 887 cpp_token header;
4a58aab6 888
d7bc7a98
NB
889 /* Later: maybe update this to use the #include "" search path
890 if cpp_read_file fails. */
891 header.type = CPP_STRING;
892 header.val.str.text = (const unsigned char *) p->arg;
893 header.val.str.len = strlen (p->arg);
894 /* Make the command line directive take up a line. */
50410426 895 pfile->line++;
d7bc7a98
NB
896
897 return _cpp_execute_include (pfile, &header, IT_CMDLINE);
898}
899
900/* Frees a pending_option chain. */
901static void
902free_chain (head)
903 struct pending_option *head;
904{
905 struct pending_option *next;
906
907 while (head)
908 {
909 next = head->next;
910 free (head);
911 head = next;
4a58aab6
NB
912 }
913}
914
f5e99456
NB
915/* This is called after options have been parsed, and partially
916 processed. Setup for processing input from the file named FNAME,
917 or stdin if it is the empty string. Return the original filename
918 on success (e.g. foo.i->foo.c), or NULL on failure. */
919const char *
920cpp_read_main_file (pfile, fname, table)
6de1e2a9 921 cpp_reader *pfile;
7ceb3598 922 const char *fname;
f5e99456 923 hash_table *table;
6de1e2a9 924{
f5e99456
NB
925 /* The front ends don't set up the hash table until they have
926 finished processing the command line options, so initializing the
927 hashtable is deferred until now. */
928 _cpp_init_hashtable (pfile, table);
929
c45da1ca 930 /* Set up the include search path now. */
ae79697b 931 if (! CPP_OPTION (pfile, no_standard_includes))
7ca3d2b1 932 init_standard_includes (pfile);
c45da1ca 933
ae79697b 934 merge_include_chains (pfile);
c45da1ca
ZW
935
936 /* With -v, print the list of dirs to search. */
ae79697b 937 if (CPP_OPTION (pfile, verbose))
c45da1ca 938 {
591e15a1 939 struct search_path *l;
c45da1ca 940 fprintf (stderr, _("#include \"...\" search starts here:\n"));
ae79697b 941 for (l = CPP_OPTION (pfile, quote_include); l; l = l->next)
c45da1ca 942 {
ae79697b 943 if (l == CPP_OPTION (pfile, bracket_include))
c45da1ca
ZW
944 fprintf (stderr, _("#include <...> search starts here:\n"));
945 fprintf (stderr, " %s\n", l->name);
946 }
947 fprintf (stderr, _("End of search list.\n"));
948 }
949
96302433 950 if (CPP_OPTION (pfile, print_deps))
7855db7c 951 /* Set the default target (if there is none already). */
373e2177 952 deps_add_default_target (pfile->deps, fname);
96302433 953
f5e99456 954 /* Open the main input file. */
614c7d37 955 if (!_cpp_read_file (pfile, fname))
f5e99456 956 return NULL;
c45da1ca 957
5993019d
NB
958 /* Set this after cpp_post_options so the client can change the
959 option if it wishes, and after stacking the main file so we don't
960 trace the main file. */
961 pfile->line_maps.trace_includes = CPP_OPTION (pfile, print_include_names);
962
f5e99456
NB
963 /* For foo.i, read the original filename foo.c now, for the benefit
964 of the front ends. */
965 if (CPP_OPTION (pfile, preprocessed))
966 read_original_filename (pfile);
967
968 return pfile->map->to_file;
969}
970
971/* For preprocessed files, if the first tokens are of the form # NUM.
972 handle the directive so we know the original file name. This will
973 generate file_change callbacks, which the front ends must handle
974 appropriately given their state of initialization. */
975static void
976read_original_filename (pfile)
977 cpp_reader *pfile;
978{
979 const cpp_token *token, *token1;
980
981 /* Lex ahead; if the first tokens are of the form # NUM, then
982 process the directive, otherwise back up. */
983 token = _cpp_lex_direct (pfile);
984 if (token->type == CPP_HASH)
985 {
986 token1 = _cpp_lex_direct (pfile);
987 _cpp_backup_tokens (pfile, 1);
988
989 /* If it's a #line directive, handle it. */
990 if (token1->type == CPP_NUMBER)
991 {
992 _cpp_handle_directive (pfile, token->flags & PREV_WHITE);
993 return;
994 }
995 }
996
997 /* Backup as if nothing happened. */
998 _cpp_backup_tokens (pfile, 1);
999}
1000
1001/* Handle pending command line options: -D, -U, -A, -imacros and
1002 -include. This should be called after debugging has been properly
1003 set up in the front ends. */
1004void
1005cpp_finish_options (pfile)
1006 cpp_reader *pfile;
1007{
d7bc7a98
NB
1008 /* Install builtins and process command line macros etc. in the order
1009 they appeared, but only if not already preprocessed. */
05e81724 1010 if (! CPP_OPTION (pfile, preprocessed))
6de1e2a9 1011 {
d7bc7a98
NB
1012 struct pending_option *p;
1013
b0287a90 1014 _cpp_do_file_change (pfile, LC_RENAME, _("<built-in>"), 1, 0);
d7bc7a98
NB
1015 init_builtins (pfile);
1016 _cpp_do_file_change (pfile, LC_RENAME, _("<command line>"), 1, 0);
1017 for (p = CPP_OPTION (pfile, pending)->directive_head; p; p = p->next)
05e81724 1018 (*p->handler) (pfile, p->arg);
d7bc7a98 1019
af0d16cd
NB
1020 /* Scan -imacros files after -D, -U, but before -include.
1021 pfile->next_include_file is NULL, so _cpp_pop_buffer does not
1022 push -include files. */
1023 for (p = CPP_OPTION (pfile, pending)->imacros_head; p; p = p->next)
1024 if (push_include (pfile, p))
1025 cpp_scan_nooutput (pfile);
1026
1027 pfile->next_include_file = &CPP_OPTION (pfile, pending)->include_head;
1028 _cpp_maybe_push_include_file (pfile);
6de1e2a9 1029 }
05e81724 1030
af0d16cd 1031 free_chain (CPP_OPTION (pfile, pending)->imacros_head);
d7bc7a98 1032 free_chain (CPP_OPTION (pfile, pending)->directive_head);
d7bc7a98 1033}
6de1e2a9 1034
af0d16cd
NB
1035/* Push the next buffer on the stack given by -include, if any. */
1036void
1037_cpp_maybe_push_include_file (pfile)
d7bc7a98
NB
1038 cpp_reader *pfile;
1039{
af0d16cd 1040 if (pfile->next_include_file)
d7bc7a98 1041 {
af0d16cd
NB
1042 struct pending_option *head = *pfile->next_include_file;
1043
1044 while (head && !push_include (pfile, head))
1045 head = head->next;
d7bc7a98 1046
af0d16cd
NB
1047 if (head)
1048 pfile->next_include_file = &head->next;
1049 else
d7bc7a98 1050 {
af0d16cd
NB
1051 /* All done; restore the line map from <command line>. */
1052 _cpp_do_file_change (pfile, LC_RENAME,
1053 pfile->line_maps.maps[0].to_file, 1, 0);
1054 /* Don't come back here again. */
1055 pfile->next_include_file = NULL;
d7bc7a98
NB
1056 }
1057 }
6de1e2a9
ZW
1058}
1059
2f638f96 1060/* Use mkdeps.c to output dependency information. */
7ca3d2b1
NB
1061static void
1062output_deps (pfile)
1063 cpp_reader *pfile;
1064{
1065 /* Stream on which to print the dependency information. */
1066 FILE *deps_stream = 0;
27c38fbe
KG
1067 const char *const deps_mode =
1068 CPP_OPTION (pfile, print_deps_append) ? "a" : "w";
7ca3d2b1 1069
56cd5b95 1070 if (CPP_OPTION (pfile, deps_file)[0] == '\0')
7ca3d2b1
NB
1071 deps_stream = stdout;
1072 else
1073 {
1074 deps_stream = fopen (CPP_OPTION (pfile, deps_file), deps_mode);
1075 if (deps_stream == 0)
1076 {
ebef4e8c 1077 cpp_errno (pfile, DL_ERROR, CPP_OPTION (pfile, deps_file));
7ca3d2b1
NB
1078 return;
1079 }
1080 }
1081
1082 deps_write (pfile->deps, deps_stream, 72);
1083
1084 if (CPP_OPTION (pfile, deps_phony_targets))
1085 deps_phony_targets (pfile->deps, deps_stream);
1086
1087 /* Don't close stdout. */
ab8e2228 1088 if (deps_stream != stdout)
7ca3d2b1
NB
1089 {
1090 if (ferror (deps_stream) || fclose (deps_stream) != 0)
ebef4e8c 1091 cpp_error (pfile, DL_FATAL, "I/O error on output");
7ca3d2b1
NB
1092 }
1093}
1094
6de1e2a9
ZW
1095/* This is called at the end of preprocessing. It pops the
1096 last buffer and writes dependency output. It should also
1097 clear macro definitions, such that you could call cpp_start_read
4a58aab6 1098 with a new filename to restart processing. */
6de1e2a9 1099void
93c80368 1100cpp_finish (pfile)
6de1e2a9
ZW
1101 cpp_reader *pfile;
1102{
7364fdd8
NB
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
a1f300c0 1105 popped the buffer, we'd dereference a NULL buffer pointer and
7364fdd8
NB
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);
c1212d2f 1110
49e6c08e 1111 /* Don't write the deps file if preprocessing has failed. */
ae79697b 1112 if (CPP_OPTION (pfile, print_deps) && pfile->errors == 0)
7ca3d2b1 1113 output_deps (pfile);
3caee4a8 1114
d4506961
ZW
1115 /* Report on headers that could use multiple include guards. */
1116 if (CPP_OPTION (pfile, print_include_names))
c71f835b 1117 _cpp_report_missing_guards (pfile);
6de1e2a9
ZW
1118}
1119
5d8ebbd8 1120/* Add a directive to be handled later in the initialization phase. */
223dca6a 1121static void
ae79697b
ZW
1122new_pending_directive (pend, text, handler)
1123 struct cpp_pending *pend;
223dca6a 1124 const char *text;
40eac643 1125 cl_directive_handler handler;
223dca6a
RH
1126{
1127 struct pending_option *o = (struct pending_option *)
1128 xmalloc (sizeof (struct pending_option));
1129
7ceb3598 1130 o->arg = text;
223dca6a 1131 o->next = NULL;
40eac643 1132 o->handler = handler;
ae79697b 1133 APPEND (pend, directive, o);
223dca6a
RH
1134}
1135
ae79697b
ZW
1136/* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
1137 I.e. a const string initializer with parens around it. That is
1138 what N_("string") resolves to, so we make no_* be macros instead. */
c725bd79
NB
1139#define no_arg N_("argument missing after %s")
1140#define no_ass N_("assertion missing after %s")
1141#define no_dir N_("directory name missing after %s")
1142#define no_fil N_("file name missing after %s")
1143#define no_mac N_("macro name missing after %s")
1144#define no_pth N_("path name missing after %s")
1145#define no_num N_("number missing after %s")
1146#define no_tgt N_("target missing after %s")
ae79697b
ZW
1147
1148/* This is the list of all command line options, with the leading
1149 "-" removed. It must be sorted in ASCII collating order. */
1150#define COMMAND_LINE_OPTIONS \
ae79697b
ZW
1151 DEF_OPT("$", 0, OPT_dollar) \
1152 DEF_OPT("+", 0, OPT_plus) \
1153 DEF_OPT("-help", 0, OPT__help) \
91606ce2 1154 DEF_OPT("-target-help", 0, OPT_target__help) \
ae79697b
ZW
1155 DEF_OPT("-version", 0, OPT__version) \
1156 DEF_OPT("A", no_ass, OPT_A) \
1157 DEF_OPT("C", 0, OPT_C) \
477cdac7 1158 DEF_OPT("CC", 0, OPT_CC) \
ae79697b
ZW
1159 DEF_OPT("D", no_mac, OPT_D) \
1160 DEF_OPT("H", 0, OPT_H) \
1161 DEF_OPT("I", no_dir, OPT_I) \
1162 DEF_OPT("M", 0, OPT_M) \
b3124fac 1163 DEF_OPT("MD", no_fil, OPT_MD) \
96302433 1164 DEF_OPT("MF", no_fil, OPT_MF) \
ae79697b
ZW
1165 DEF_OPT("MG", 0, OPT_MG) \
1166 DEF_OPT("MM", 0, OPT_MM) \
b3124fac 1167 DEF_OPT("MMD", no_fil, OPT_MMD) \
a5a4ce3c 1168 DEF_OPT("MP", 0, OPT_MP) \
f7114e17 1169 DEF_OPT("MQ", no_tgt, OPT_MQ) \
03b9ab42 1170 DEF_OPT("MT", no_tgt, OPT_MT) \
ae79697b
ZW
1171 DEF_OPT("P", 0, OPT_P) \
1172 DEF_OPT("U", no_mac, OPT_U) \
1173 DEF_OPT("W", no_arg, OPT_W) /* arg optional */ \
1174 DEF_OPT("d", no_arg, OPT_d) \
1175 DEF_OPT("fleading-underscore", 0, OPT_fleading_underscore) \
1176 DEF_OPT("fno-leading-underscore", 0, OPT_fno_leading_underscore) \
be768055 1177 DEF_OPT("fno-operator-names", 0, OPT_fno_operator_names) \
ae79697b
ZW
1178 DEF_OPT("fno-preprocessed", 0, OPT_fno_preprocessed) \
1179 DEF_OPT("fno-show-column", 0, OPT_fno_show_column) \
1180 DEF_OPT("fpreprocessed", 0, OPT_fpreprocessed) \
1181 DEF_OPT("fshow-column", 0, OPT_fshow_column) \
0fef3fd0 1182 DEF_OPT("fsigned-char", 0, OPT_fsigned_char) \
6ab3e7dd 1183 DEF_OPT("ftabstop=", no_num, OPT_ftabstop) \
0fef3fd0 1184 DEF_OPT("funsigned-char", 0, OPT_funsigned_char) \
ae79697b
ZW
1185 DEF_OPT("h", 0, OPT_h) \
1186 DEF_OPT("idirafter", no_dir, OPT_idirafter) \
1187 DEF_OPT("imacros", no_fil, OPT_imacros) \
1188 DEF_OPT("include", no_fil, OPT_include) \
1189 DEF_OPT("iprefix", no_pth, OPT_iprefix) \
1190 DEF_OPT("isystem", no_dir, OPT_isystem) \
1191 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix) \
1192 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore) \
1193 DEF_OPT("lang-asm", 0, OPT_lang_asm) \
1194 DEF_OPT("lang-c", 0, OPT_lang_c) \
1195 DEF_OPT("lang-c++", 0, OPT_lang_cplusplus) \
1196 DEF_OPT("lang-c89", 0, OPT_lang_c89) \
ae79697b
ZW
1197 DEF_OPT("lang-objc", 0, OPT_lang_objc) \
1198 DEF_OPT("lang-objc++", 0, OPT_lang_objcplusplus) \
1199 DEF_OPT("nostdinc", 0, OPT_nostdinc) \
1200 DEF_OPT("nostdinc++", 0, OPT_nostdincplusplus) \
1201 DEF_OPT("o", no_fil, OPT_o) \
1202 DEF_OPT("pedantic", 0, OPT_pedantic) \
1203 DEF_OPT("pedantic-errors", 0, OPT_pedantic_errors) \
1204 DEF_OPT("remap", 0, OPT_remap) \
dd07b884 1205 DEF_OPT("std=c++98", 0, OPT_std_cplusplus98) \
ae79697b
ZW
1206 DEF_OPT("std=c89", 0, OPT_std_c89) \
1207 DEF_OPT("std=c99", 0, OPT_std_c99) \
1208 DEF_OPT("std=c9x", 0, OPT_std_c9x) \
1209 DEF_OPT("std=gnu89", 0, OPT_std_gnu89) \
1210 DEF_OPT("std=gnu99", 0, OPT_std_gnu99) \
1211 DEF_OPT("std=gnu9x", 0, OPT_std_gnu9x) \
1212 DEF_OPT("std=iso9899:1990", 0, OPT_std_iso9899_1990) \
1213 DEF_OPT("std=iso9899:199409", 0, OPT_std_iso9899_199409) \
1214 DEF_OPT("std=iso9899:1999", 0, OPT_std_iso9899_1999) \
1215 DEF_OPT("std=iso9899:199x", 0, OPT_std_iso9899_199x) \
ae79697b
ZW
1216 DEF_OPT("trigraphs", 0, OPT_trigraphs) \
1217 DEF_OPT("v", 0, OPT_v) \
f4cdc368 1218 DEF_OPT("version", 0, OPT_version) \
ae79697b
ZW
1219 DEF_OPT("w", 0, OPT_w)
1220
1221#define DEF_OPT(text, msg, code) code,
e23c0ba3
ZW
1222enum opt_code
1223{
ae79697b 1224 COMMAND_LINE_OPTIONS
e23c0ba3
ZW
1225 N_OPTS
1226};
ae79697b 1227#undef DEF_OPT
e23c0ba3
ZW
1228
1229struct cl_option
1230{
1231 const char *opt_text;
1232 const char *msg;
1233 size_t opt_len;
1234 enum opt_code opt_code;
1235};
1236
ae79697b 1237#define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
e23c0ba3
ZW
1238#ifdef HOST_EBCDIC
1239static struct cl_option cl_options[] =
1240#else
1241static const struct cl_option cl_options[] =
1242#endif
1243{
ae79697b 1244 COMMAND_LINE_OPTIONS
e23c0ba3
ZW
1245};
1246#undef DEF_OPT
ae79697b 1247#undef COMMAND_LINE_OPTIONS
e23c0ba3
ZW
1248
1249/* Perform a binary search to find which, if any, option the given
1250 command-line matches. Returns its index in the option array,
1251 negative on failure. Complications arise since some options can be
1252 suffixed with an argument, and multiple complete matches can occur,
05e81724
NB
1253 e.g. -iwithprefix and -iwithprefixbefore. Moreover, we need to
1254 accept options beginning with -W that we do not recognise, but not
1255 to swallow any subsequent command line argument; this is handled as
1256 special cases in cpp_handle_option. */
e23c0ba3
ZW
1257static int
1258parse_option (input)
1259 const char *input;
1260{
1261 unsigned int md, mn, mx;
1262 size_t opt_len;
1263 int comp;
1264
1265 mn = 0;
1266 mx = N_OPTS;
1267
1268 while (mx > mn)
1269 {
1270 md = (mn + mx) / 2;
ae79697b 1271
e23c0ba3 1272 opt_len = cl_options[md].opt_len;
61d0346d 1273 comp = memcmp (input, cl_options[md].opt_text, opt_len);
ae79697b 1274
e23c0ba3
ZW
1275 if (comp > 0)
1276 mn = md + 1;
1277 else if (comp < 0)
1278 mx = md;
1279 else
1280 {
1281 if (input[opt_len] == '\0')
1282 return md;
1283 /* We were passed more text. If the option takes an argument,
1284 we may match a later option or we may have been passed the
1285 argument. The longest possible option match succeeds.
1286 If the option takes no arguments we have not matched and
4a58aab6 1287 continue the search (e.g. input="stdc++" match was "stdc"). */
e23c0ba3
ZW
1288 mn = md + 1;
1289 if (cl_options[md].msg)
1290 {
1291 /* Scan forwards. If we get an exact match, return it.
1292 Otherwise, return the longest option-accepting match.
4a58aab6 1293 This loops no more than twice with current options. */
e23c0ba3 1294 mx = md;
37b8524c 1295 for (; mn < (unsigned int) N_OPTS; mn++)
e23c0ba3
ZW
1296 {
1297 opt_len = cl_options[mn].opt_len;
61d0346d 1298 if (memcmp (input, cl_options[mn].opt_text, opt_len))
e23c0ba3
ZW
1299 break;
1300 if (input[opt_len] == '\0')
1301 return mn;
1302 if (cl_options[mn].msg)
1303 mx = mn;
1304 }
1305 return mx;
1306 }
1307 }
1308 }
1309
1310 return -1;
1311}
1312
6de1e2a9
ZW
1313/* Handle one command-line option in (argc, argv).
1314 Can be called multiple times, to handle multiple sets of options.
ffdeea47 1315 If ignore is non-zero, this will ignore unrecognized -W* options.
6de1e2a9 1316 Returns number of strings consumed. */
2c0accc9 1317int
ffdeea47 1318cpp_handle_option (pfile, argc, argv, ignore)
6de1e2a9
ZW
1319 cpp_reader *pfile;
1320 int argc;
1321 char **argv;
ffdeea47 1322 int ignore;
6de1e2a9 1323{
6de1e2a9 1324 int i = 0;
f8f769ea 1325 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
6de1e2a9 1326
373e2177
NB
1327 /* Interpret "-" or a non-option as a file name. */
1328 if (argv[i][0] != '-' || argv[i][1] == '\0')
0b22d65c 1329 {
373e2177
NB
1330 if (CPP_OPTION (pfile, in_fname) == NULL)
1331 CPP_OPTION (pfile, in_fname) = argv[i];
1332 else if (CPP_OPTION (pfile, out_fname) == NULL)
ae79697b 1333 CPP_OPTION (pfile, out_fname) = argv[i];
0b22d65c 1334 else
ebef4e8c
NB
1335 cpp_error (pfile, DL_FATAL,
1336 "too many filenames. Type %s --help for usage info",
373e2177 1337 progname);
0b22d65c
ZW
1338 }
1339 else
e23c0ba3
ZW
1340 {
1341 enum opt_code opt_code;
1342 int opt_index;
7ceb3598 1343 const char *arg = 0;
e23c0ba3 1344
4a58aab6 1345 /* Skip over '-'. */
e23c0ba3
ZW
1346 opt_index = parse_option (&argv[i][1]);
1347 if (opt_index < 0)
1348 return i;
1349
1350 opt_code = cl_options[opt_index].opt_code;
1351 if (cl_options[opt_index].msg)
1352 {
1353 arg = &argv[i][cl_options[opt_index].opt_len + 1];
1354
5c5d1ea0 1355 /* Yuk. Special case for -W as it must not swallow
e23c0ba3 1356 up any following argument. If this becomes common, add
4a58aab6 1357 another field to the cl_options table. */
5c5d1ea0 1358 if (arg[0] == '\0' && opt_code != OPT_W)
e23c0ba3
ZW
1359 {
1360 arg = argv[++i];
1361 if (!arg)
1362 {
ebef4e8c
NB
1363 cpp_error (pfile, DL_FATAL,
1364 cl_options[opt_index].msg, argv[i - 1]);
e23c0ba3
ZW
1365 return argc;
1366 }
1367 }
1368 }
ae79697b 1369
e23c0ba3
ZW
1370 switch (opt_code)
1371 {
4a58aab6 1372 case N_OPTS: /* Shut GCC up. */
e23c0ba3
ZW
1373 break;
1374 case OPT_fleading_underscore:
771c4df3 1375 CPP_OPTION (pfile, user_label_prefix) = "_";
e23c0ba3
ZW
1376 break;
1377 case OPT_fno_leading_underscore:
771c4df3 1378 CPP_OPTION (pfile, user_label_prefix) = "";
e23c0ba3 1379 break;
be768055
JJ
1380 case OPT_fno_operator_names:
1381 CPP_OPTION (pfile, operator_names) = 0;
1382 break;
e23c0ba3 1383 case OPT_fpreprocessed:
ae79697b 1384 CPP_OPTION (pfile, preprocessed) = 1;
e23c0ba3
ZW
1385 break;
1386 case OPT_fno_preprocessed:
ae79697b
ZW
1387 CPP_OPTION (pfile, preprocessed) = 0;
1388 break;
1389 case OPT_fshow_column:
1390 CPP_OPTION (pfile, show_column) = 1;
1391 break;
1392 case OPT_fno_show_column:
1393 CPP_OPTION (pfile, show_column) = 0;
e23c0ba3 1394 break;
0fef3fd0
NB
1395 case OPT_fsigned_char:
1396 CPP_OPTION (pfile, signed_char) = 1;
1397 break;
1398 case OPT_funsigned_char:
1399 CPP_OPTION (pfile, signed_char) = 0;
1400 break;
6ab3e7dd
NB
1401 case OPT_ftabstop:
1402 /* Silently ignore empty string, non-longs and silly values. */
1403 if (arg[0] != '\0')
1404 {
1405 char *endptr;
1406 long tabstop = strtol (arg, &endptr, 10);
1407 if (*endptr == '\0' && tabstop >= 1 && tabstop <= 100)
1408 CPP_OPTION (pfile, tabstop) = tabstop;
1409 }
1410 break;
e23c0ba3 1411 case OPT_w:
ae79697b 1412 CPP_OPTION (pfile, inhibit_warnings) = 1;
e23c0ba3 1413 break;
e23c0ba3
ZW
1414 case OPT_h:
1415 case OPT__help:
1416 print_help ();
7e96d768 1417 CPP_OPTION (pfile, help_only) = 1;
e23c0ba3 1418 break;
91606ce2 1419 case OPT_target__help:
f4cdc368
ZW
1420 /* Print if any target specific options. cpplib has none, but
1421 make sure help_only gets set. */
7e96d768 1422 CPP_OPTION (pfile, help_only) = 1;
91606ce2 1423 break;
f4cdc368
ZW
1424
1425 /* --version inhibits compilation, -version doesn't. -v means
1426 verbose and -version. Historical reasons, don't ask. */
e23c0ba3 1427 case OPT__version:
7e96d768 1428 CPP_OPTION (pfile, help_only) = 1;
cb773845
ZW
1429 pfile->print_version = 1;
1430 break;
f4cdc368
ZW
1431 case OPT_v:
1432 CPP_OPTION (pfile, verbose) = 1;
cb773845
ZW
1433 pfile->print_version = 1;
1434 break;
f4cdc368 1435 case OPT_version:
cb773845 1436 pfile->print_version = 1;
e23c0ba3 1437 break;
f4cdc368 1438
e23c0ba3 1439 case OPT_C:
ae79697b 1440 CPP_OPTION (pfile, discard_comments) = 0;
e23c0ba3 1441 break;
477cdac7
JT
1442 case OPT_CC:
1443 CPP_OPTION (pfile, discard_comments) = 0;
1444 CPP_OPTION (pfile, discard_comments_in_macro_exp) = 0;
1445 break;
e23c0ba3 1446 case OPT_P:
ae79697b 1447 CPP_OPTION (pfile, no_line_commands) = 1;
e23c0ba3 1448 break;
4a58aab6 1449 case OPT_dollar: /* Don't include $ in identifiers. */
ae79697b 1450 CPP_OPTION (pfile, dollars_in_ident) = 0;
e23c0ba3
ZW
1451 break;
1452 case OPT_H:
ae79697b 1453 CPP_OPTION (pfile, print_include_names) = 1;
e23c0ba3
ZW
1454 break;
1455 case OPT_D:
f8f769ea 1456 new_pending_directive (pend, arg, cpp_define);
e23c0ba3
ZW
1457 break;
1458 case OPT_pedantic_errors:
ae79697b 1459 CPP_OPTION (pfile, pedantic_errors) = 1;
e23c0ba3
ZW
1460 /* fall through */
1461 case OPT_pedantic:
ae79697b 1462 CPP_OPTION (pfile, pedantic) = 1;
2784528c 1463 CPP_OPTION (pfile, warn_endif_labels) = 1;
e23c0ba3 1464 break;
e23c0ba3 1465 case OPT_trigraphs:
ae79697b 1466 CPP_OPTION (pfile, trigraphs) = 1;
e23c0ba3
ZW
1467 break;
1468 case OPT_plus:
ae79697b
ZW
1469 CPP_OPTION (pfile, cplusplus) = 1;
1470 CPP_OPTION (pfile, cplusplus_comments) = 1;
e23c0ba3
ZW
1471 break;
1472 case OPT_remap:
ae79697b 1473 CPP_OPTION (pfile, remap) = 1;
e23c0ba3
ZW
1474 break;
1475 case OPT_iprefix:
ae79697b
ZW
1476 CPP_OPTION (pfile, include_prefix) = arg;
1477 CPP_OPTION (pfile, include_prefix_len) = strlen (arg);
e23c0ba3
ZW
1478 break;
1479 case OPT_lang_c:
dd07b884 1480 set_lang (pfile, CLK_GNUC89);
e23c0ba3 1481 break;
e23c0ba3 1482 case OPT_lang_cplusplus:
dd07b884 1483 set_lang (pfile, CLK_GNUCXX);
e23c0ba3 1484 break;
f8f769ea 1485 case OPT_lang_objc:
dd07b884 1486 set_lang (pfile, CLK_OBJC);
e23c0ba3 1487 break;
dd07b884
NB
1488 case OPT_lang_objcplusplus:
1489 set_lang (pfile, CLK_OBJCXX);
e23c0ba3 1490 break;
dd07b884
NB
1491 case OPT_lang_asm:
1492 set_lang (pfile, CLK_ASM);
e23c0ba3 1493 break;
dd07b884
NB
1494 case OPT_std_cplusplus98:
1495 set_lang (pfile, CLK_CXX98);
e23c0ba3
ZW
1496 break;
1497 case OPT_std_gnu89:
dd07b884 1498 set_lang (pfile, CLK_GNUC89);
e23c0ba3
ZW
1499 break;
1500 case OPT_std_gnu9x:
1501 case OPT_std_gnu99:
dd07b884 1502 set_lang (pfile, CLK_GNUC99);
e23c0ba3
ZW
1503 break;
1504 case OPT_std_iso9899_199409:
dd07b884
NB
1505 set_lang (pfile, CLK_STDC94);
1506 break;
e23c0ba3
ZW
1507 case OPT_std_iso9899_1990:
1508 case OPT_std_c89:
9b55f29a 1509 case OPT_lang_c89:
dd07b884 1510 set_lang (pfile, CLK_STDC89);
e23c0ba3
ZW
1511 break;
1512 case OPT_std_iso9899_199x:
1513 case OPT_std_iso9899_1999:
1514 case OPT_std_c9x:
1515 case OPT_std_c99:
dd07b884
NB
1516 set_lang (pfile, CLK_STDC99);
1517 break;
1518 case OPT_nostdinc:
1519 /* -nostdinc causes no default include directories.
1520 You must specify all include-file directories with -I. */
1521 CPP_OPTION (pfile, no_standard_includes) = 1;
1522 break;
1523 case OPT_nostdincplusplus:
ec5c56db 1524 /* -nostdinc++ causes no default C++-specific include directories. */
dd07b884 1525 CPP_OPTION (pfile, no_standard_cplusplus_includes) = 1;
e23c0ba3
ZW
1526 break;
1527 case OPT_o:
373e2177
NB
1528 if (CPP_OPTION (pfile, out_fname) == NULL)
1529 CPP_OPTION (pfile, out_fname) = arg;
1530 else
0b22d65c 1531 {
ebef4e8c 1532 cpp_error (pfile, DL_FATAL, "output filename specified twice");
e23c0ba3
ZW
1533 return argc;
1534 }
e23c0ba3
ZW
1535 break;
1536 case OPT_d:
1537 /* Args to -d specify what parts of macros to dump.
1538 Silently ignore unrecognised options; they may
4a58aab6 1539 be aimed at the compiler proper. */
e23c0ba3
ZW
1540 {
1541 char c;
ae79697b 1542
e23c0ba3
ZW
1543 while ((c = *arg++) != '\0')
1544 switch (c)
1545 {
1546 case 'M':
ae79697b
ZW
1547 CPP_OPTION (pfile, dump_macros) = dump_only;
1548 CPP_OPTION (pfile, no_output) = 1;
0b22d65c
ZW
1549 break;
1550 case 'N':
ae79697b 1551 CPP_OPTION (pfile, dump_macros) = dump_names;
0b22d65c
ZW
1552 break;
1553 case 'D':
ae79697b 1554 CPP_OPTION (pfile, dump_macros) = dump_definitions;
0b22d65c
ZW
1555 break;
1556 case 'I':
ae79697b 1557 CPP_OPTION (pfile, dump_includes) = 1;
0b22d65c
ZW
1558 break;
1559 }
e23c0ba3
ZW
1560 }
1561 break;
96302433 1562
e23c0ba3 1563 case OPT_MG:
ae79697b 1564 CPP_OPTION (pfile, print_deps_missing_files) = 1;
e23c0ba3
ZW
1565 break;
1566 case OPT_M:
3ddbb8a9
NB
1567 /* When doing dependencies with -M or -MM, suppress normal
1568 preprocessed output, but still do -dM etc. as software
1569 depends on this. Preprocessed output occurs if -MD, -MMD
1570 or environment var dependency generation is used. */
96302433 1571 CPP_OPTION (pfile, print_deps) = 2;
3ddbb8a9 1572 CPP_OPTION (pfile, no_output) = 1;
96302433 1573 break;
e23c0ba3 1574 case OPT_MM:
96302433 1575 CPP_OPTION (pfile, print_deps) = 1;
3ddbb8a9 1576 CPP_OPTION (pfile, no_output) = 1;
e23c0ba3 1577 break;
96302433
NB
1578 case OPT_MF:
1579 CPP_OPTION (pfile, deps_file) = arg;
1580 break;
1581 case OPT_MP:
a5a4ce3c
NB
1582 CPP_OPTION (pfile, deps_phony_targets) = 1;
1583 break;
f7114e17 1584 case OPT_MQ:
03b9ab42 1585 case OPT_MT:
f7114e17
NB
1586 /* Add a target. -MQ quotes for Make. */
1587 deps_add_target (pfile->deps, arg, opt_code == OPT_MQ);
03b9ab42
NB
1588 break;
1589
b3124fac
NB
1590 case OPT_MD:
1591 CPP_OPTION (pfile, print_deps) = 2;
1592 CPP_OPTION (pfile, deps_file) = arg;
1593 break;
1594 case OPT_MMD:
1595 CPP_OPTION (pfile, print_deps) = 1;
1596 CPP_OPTION (pfile, deps_file) = arg;
1597 break;
1598
e23c0ba3 1599 case OPT_A:
e1e97c4f 1600 if (arg[0] == '-')
0b22d65c 1601 {
e1e97c4f
NB
1602 /* -A with an argument beginning with '-' acts as
1603 #unassert on whatever immediately follows the '-'.
1604 If "-" is the whole argument, we eliminate all
1605 predefined macros and assertions, including those
1606 that were specified earlier on the command line.
1607 That way we can get rid of any that were passed
1608 automatically in from GCC. */
1609
1610 if (arg[1] == '\0')
0b22d65c 1611 {
29401c30 1612 free_chain (pend->directive_head);
e33f6253
NB
1613 pend->directive_head = NULL;
1614 pend->directive_tail = NULL;
0b22d65c 1615 }
e1e97c4f 1616 else
e33f6253 1617 new_pending_directive (pend, arg + 1, cpp_unassert);
6de1e2a9 1618 }
e1e97c4f 1619 else
e33f6253 1620 new_pending_directive (pend, arg, cpp_assert);
e23c0ba3
ZW
1621 break;
1622 case OPT_U:
e33f6253 1623 new_pending_directive (pend, arg, cpp_undef);
e23c0ba3
ZW
1624 break;
1625 case OPT_I: /* Add directory to path for includes. */
1626 if (!strcmp (arg, "-"))
1627 {
1628 /* -I- means:
1629 Use the preceding -I directories for #include "..."
1630 but not #include <...>.
1631 Don't search the directory of the present file
1632 for #include "...". (Note that -I. -I- is not the same as
1633 the default setup; -I. uses the compiler's working dir.) */
ae79697b 1634 if (! CPP_OPTION (pfile, ignore_srcdir))
e23c0ba3 1635 {
ae79697b
ZW
1636 pend->quote_head = pend->brack_head;
1637 pend->quote_tail = pend->brack_tail;
1638 pend->brack_head = 0;
1639 pend->brack_tail = 0;
1640 CPP_OPTION (pfile, ignore_srcdir) = 1;
e23c0ba3
ZW
1641 }
1642 else
1643 {
ebef4e8c 1644 cpp_error (pfile, DL_FATAL, "-I- specified twice");
e23c0ba3
ZW
1645 return argc;
1646 }
1647 }
1648 else
e33f6253 1649 append_include_chain (pfile, xstrdup (arg), BRACKET, 0);
e23c0ba3
ZW
1650 break;
1651 case OPT_isystem:
1652 /* Add directory to beginning of system include path, as a system
4a58aab6 1653 include directory. */
e33f6253 1654 append_include_chain (pfile, xstrdup (arg), SYSTEM, 0);
e23c0ba3
ZW
1655 break;
1656 case OPT_include:
e23c0ba3
ZW
1657 case OPT_imacros:
1658 {
1659 struct pending_option *o = (struct pending_option *)
1660 xmalloc (sizeof (struct pending_option));
1661 o->arg = arg;
1662 o->next = NULL;
ae79697b 1663
d7bc7a98
NB
1664 if (opt_code == OPT_include)
1665 APPEND (pend, include, o);
1666 else
1667 APPEND (pend, imacros, o);
e23c0ba3
ZW
1668 }
1669 break;
1670 case OPT_iwithprefix:
1671 /* Add directory to end of path for includes,
1672 with the default prefix at the front of its name. */
1673 /* fall through */
1674 case OPT_iwithprefixbefore:
1675 /* Add directory to main path for includes,
1676 with the default prefix at the front of its name. */
1677 {
1678 char *fname;
1679 int len;
ae79697b 1680
e23c0ba3 1681 len = strlen (arg);
ae79697b
ZW
1682
1683 if (CPP_OPTION (pfile, include_prefix) != 0)
e23c0ba3 1684 {
ae79697b
ZW
1685 size_t ipl = CPP_OPTION (pfile, include_prefix_len);
1686 fname = xmalloc (ipl + len + 1);
1687 memcpy (fname, CPP_OPTION (pfile, include_prefix), ipl);
1688 memcpy (fname + ipl, arg, len + 1);
e23c0ba3 1689 }
60893f43 1690 else if (cpp_GCC_INCLUDE_DIR_len)
e23c0ba3 1691 {
60893f43
ZW
1692 fname = xmalloc (cpp_GCC_INCLUDE_DIR_len + len + 1);
1693 memcpy (fname, cpp_GCC_INCLUDE_DIR, cpp_GCC_INCLUDE_DIR_len);
1694 memcpy (fname + cpp_GCC_INCLUDE_DIR_len, arg, len + 1);
e23c0ba3 1695 }
60893f43
ZW
1696 else
1697 fname = xstrdup (arg);
ae79697b 1698
e33f6253 1699 append_include_chain (pfile, fname,
e23c0ba3
ZW
1700 opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0);
1701 }
1702 break;
1703 case OPT_idirafter:
1704 /* Add directory to end of path for includes. */
e33f6253 1705 append_include_chain (pfile, xstrdup (arg), AFTER, 0);
e23c0ba3
ZW
1706 break;
1707 case OPT_W:
4a58aab6 1708 /* Silently ignore unrecognised options. */
e23c0ba3 1709 if (!strcmp (argv[i], "-Wall"))
0b22d65c 1710 {
ae79697b
ZW
1711 CPP_OPTION (pfile, warn_trigraphs) = 1;
1712 CPP_OPTION (pfile, warn_comments) = 1;
0b22d65c 1713 }
e23c0ba3 1714 else if (!strcmp (argv[i], "-Wtraditional"))
07aa0b04 1715 CPP_OPTION (pfile, warn_traditional) = 1;
e23c0ba3 1716 else if (!strcmp (argv[i], "-Wtrigraphs"))
ae79697b 1717 CPP_OPTION (pfile, warn_trigraphs) = 1;
e23c0ba3 1718 else if (!strcmp (argv[i], "-Wcomment"))
ae79697b 1719 CPP_OPTION (pfile, warn_comments) = 1;
e23c0ba3 1720 else if (!strcmp (argv[i], "-Wcomments"))
ae79697b 1721 CPP_OPTION (pfile, warn_comments) = 1;
e23c0ba3 1722 else if (!strcmp (argv[i], "-Wundef"))
ae79697b 1723 CPP_OPTION (pfile, warn_undef) = 1;
e23c0ba3 1724 else if (!strcmp (argv[i], "-Wimport"))
ae79697b 1725 CPP_OPTION (pfile, warn_import) = 1;
e23c0ba3 1726 else if (!strcmp (argv[i], "-Werror"))
ae79697b 1727 CPP_OPTION (pfile, warnings_are_errors) = 1;
317639a8
BC
1728 else if (!strcmp (argv[i], "-Wsystem-headers"))
1729 CPP_OPTION (pfile, warn_system_headers) = 1;
909de5da
PE
1730 else if (!strcmp (argv[i], "-Wendif-labels"))
1731 CPP_OPTION (pfile, warn_endif_labels) = 1;
e23c0ba3 1732 else if (!strcmp (argv[i], "-Wno-traditional"))
07aa0b04 1733 CPP_OPTION (pfile, warn_traditional) = 0;
e23c0ba3 1734 else if (!strcmp (argv[i], "-Wno-trigraphs"))
ae79697b 1735 CPP_OPTION (pfile, warn_trigraphs) = 0;
e23c0ba3 1736 else if (!strcmp (argv[i], "-Wno-comment"))
ae79697b 1737 CPP_OPTION (pfile, warn_comments) = 0;
e23c0ba3 1738 else if (!strcmp (argv[i], "-Wno-comments"))
ae79697b 1739 CPP_OPTION (pfile, warn_comments) = 0;
e23c0ba3 1740 else if (!strcmp (argv[i], "-Wno-undef"))
ae79697b 1741 CPP_OPTION (pfile, warn_undef) = 0;
e23c0ba3 1742 else if (!strcmp (argv[i], "-Wno-import"))
ae79697b 1743 CPP_OPTION (pfile, warn_import) = 0;
e23c0ba3 1744 else if (!strcmp (argv[i], "-Wno-error"))
ae79697b 1745 CPP_OPTION (pfile, warnings_are_errors) = 0;
317639a8
BC
1746 else if (!strcmp (argv[i], "-Wno-system-headers"))
1747 CPP_OPTION (pfile, warn_system_headers) = 0;
909de5da
PE
1748 else if (!strcmp (argv[i], "-Wno-endif-labels"))
1749 CPP_OPTION (pfile, warn_endif_labels) = 0;
ffdeea47
JJ
1750 else if (! ignore)
1751 return i;
e23c0ba3
ZW
1752 break;
1753 }
1754 }
6de1e2a9 1755 return i + 1;
e23c0ba3 1756}
0b22d65c 1757
6de1e2a9
ZW
1758/* Handle command-line options in (argc, argv).
1759 Can be called multiple times, to handle multiple sets of options.
1760 Returns if an unrecognized option is seen.
1761 Returns number of strings consumed. */
6de1e2a9
ZW
1762int
1763cpp_handle_options (pfile, argc, argv)
1764 cpp_reader *pfile;
1765 int argc;
1766 char **argv;
1767{
1768 int i;
1769 int strings_processed;
e23c0ba3 1770
6de1e2a9
ZW
1771 for (i = 0; i < argc; i += strings_processed)
1772 {
ffdeea47 1773 strings_processed = cpp_handle_option (pfile, argc - i, argv + i, 1);
6de1e2a9
ZW
1774 if (strings_processed == 0)
1775 break;
1776 }
96302433 1777
6de1e2a9
ZW
1778 return i;
1779}
1780
96302433
NB
1781/* Extra processing when all options are parsed, after all calls to
1782 cpp_handle_option[s]. Consistency checks etc. */
1783void
1784cpp_post_options (pfile)
1785 cpp_reader *pfile;
1786{
cb773845
ZW
1787 if (pfile->print_version)
1788 {
1789 fprintf (stderr, _("GNU CPP version %s (cpplib)"), version_string);
1790#ifdef TARGET_VERSION
1791 TARGET_VERSION;
1792#endif
1793 fputc ('\n', stderr);
1794 }
1795
373e2177
NB
1796 /* Canonicalize in_fname and out_fname. We guarantee they are not
1797 NULL, and that the empty string represents stdin / stdout. */
1798 if (CPP_OPTION (pfile, in_fname) == NULL
1799 || !strcmp (CPP_OPTION (pfile, in_fname), "-"))
1800 CPP_OPTION (pfile, in_fname) = "";
1801
1802 if (CPP_OPTION (pfile, out_fname) == NULL
1803 || !strcmp (CPP_OPTION (pfile, out_fname), "-"))
1804 CPP_OPTION (pfile, out_fname) = "";
1805
96302433
NB
1806 /* -Wtraditional is not useful in C++ mode. */
1807 if (CPP_OPTION (pfile, cplusplus))
1808 CPP_OPTION (pfile, warn_traditional) = 0;
1809
ec5c56db 1810 /* Set this if it hasn't been set already. */
96302433
NB
1811 if (CPP_OPTION (pfile, user_label_prefix) == NULL)
1812 CPP_OPTION (pfile, user_label_prefix) = USER_LABEL_PREFIX;
1813
6d4587f7
ZW
1814 /* Permanently disable macro expansion if we are rescanning
1815 preprocessed text. */
1816 if (CPP_OPTION (pfile, preprocessed))
1817 pfile->state.prevent_expansion = 1;
1818
96302433
NB
1819 /* We need to do this after option processing and before
1820 cpp_start_read, as cppmain.c relies on the options->no_output to
1821 set its callbacks correctly before calling cpp_start_read. */
1822 init_dependency_output (pfile);
1823
e582248c
NB
1824 /* After checking the environment variables, check if -M or -MM has
1825 not been specified, but other -M options have. */
1826 if (CPP_OPTION (pfile, print_deps) == 0 &&
1827 (CPP_OPTION (pfile, print_deps_missing_files)
1828 || CPP_OPTION (pfile, deps_file)
1829 || CPP_OPTION (pfile, deps_phony_targets)))
ebef4e8c
NB
1830 cpp_error (pfile, DL_FATAL,
1831 "you must additionally specify either -M or -MM");
96302433
NB
1832}
1833
56cd5b95
NB
1834/* Set up dependency-file output. On exit, if print_deps is non-zero
1835 then deps_file is not NULL; stdout is the empty string. */
7ca3d2b1
NB
1836static void
1837init_dependency_output (pfile)
1838 cpp_reader *pfile;
1839{
1840 char *spec, *s, *output_file;
1841
1842 /* Either of two environment variables can specify output of deps.
1843 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1844 where OUTPUT_FILE is the file to write deps info to
1845 and DEPS_TARGET is the target to mention in the deps. */
1846
1847 if (CPP_OPTION (pfile, print_deps) == 0)
1848 {
1849 spec = getenv ("DEPENDENCIES_OUTPUT");
1850 if (spec)
1851 CPP_OPTION (pfile, print_deps) = 1;
1852 else
1853 {
1854 spec = getenv ("SUNPRO_DEPENDENCIES");
1855 if (spec)
1856 CPP_OPTION (pfile, print_deps) = 2;
1857 else
1858 return;
1859 }
1860
1861 /* Find the space before the DEPS_TARGET, if there is one. */
1862 s = strchr (spec, ' ');
1863 if (s)
1864 {
1865 /* Let the caller perform MAKE quoting. */
1866 deps_add_target (pfile->deps, s + 1, 0);
1867 output_file = (char *) xmalloc (s - spec + 1);
1868 memcpy (output_file, spec, s - spec);
1869 output_file[s - spec] = 0;
1870 }
1871 else
1872 output_file = spec;
1873
ab8e2228
NB
1874 /* Command line -MF overrides environment variables and default. */
1875 if (CPP_OPTION (pfile, deps_file) == 0)
1876 CPP_OPTION (pfile, deps_file) = output_file;
1877
7ca3d2b1
NB
1878 CPP_OPTION (pfile, print_deps_append) = 1;
1879 }
ab8e2228 1880 else if (CPP_OPTION (pfile, deps_file) == 0)
32810ba3
NB
1881 /* If -M or -MM was seen without -MF, default output to wherever
1882 was specified with -o. out_fname is non-NULL here. */
ab8e2228 1883 CPP_OPTION (pfile, deps_file) = CPP_OPTION (pfile, out_fname);
7ca3d2b1
NB
1884}
1885
5d8ebbd8 1886/* Handle --help output. */
6de1e2a9
ZW
1887static void
1888print_help ()
1889{
aaaf7848 1890 /* To keep the lines from getting too long for some compilers, limit
ec5c56db 1891 to about 500 characters (6 lines) per chunk. */
6de1e2a9
ZW
1892 fputs (_("\
1893Switches:\n\
1894 -include <file> Include the contents of <file> before other files\n\
1895 -imacros <file> Accept definition of macros in <file>\n\
1896 -iprefix <path> Specify <path> as a prefix for next two options\n\
1897 -iwithprefix <dir> Add <dir> to the end of the system include path\n\
1898 -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\
1899 -isystem <dir> Add <dir> to the start of the system include path\n\
aaaf7848
DT
1900"), stdout);
1901 fputs (_("\
6de1e2a9
ZW
1902 -idirafter <dir> Add <dir> to the end of the system include path\n\
1903 -I <dir> Add <dir> to the end of the main include path\n\
e23c0ba3 1904 -I- Fine-grained include path control; see info docs\n\
6de1e2a9
ZW
1905 -nostdinc Do not search system include directories\n\
1906 (dirs specified with -isystem will still be used)\n\
1907 -nostdinc++ Do not search system include directories for C++\n\
1908 -o <file> Put output into <file>\n\
aaaf7848
DT
1909"), stdout);
1910 fputs (_("\
041c3194 1911 -pedantic Issue all warnings demanded by strict ISO C\n\
e23c0ba3 1912 -pedantic-errors Issue -pedantic warnings as errors instead\n\
041c3194 1913 -trigraphs Support ISO C trigraphs\n\
6de1e2a9
ZW
1914 -lang-c Assume that the input sources are in C\n\
1915 -lang-c89 Assume that the input sources are in C89\n\
aaaf7848
DT
1916"), stdout);
1917 fputs (_("\
f9a0e96c 1918 -lang-c++ Assume that the input sources are in C++\n\
6de1e2a9
ZW
1919 -lang-objc Assume that the input sources are in ObjectiveC\n\
1920 -lang-objc++ Assume that the input sources are in ObjectiveC++\n\
1921 -lang-asm Assume that the input sources are in assembler\n\
aaaf7848
DT
1922"), stdout);
1923 fputs (_("\
6de1e2a9 1924 -std=<std name> Specify the conformance standard; one of:\n\
916269ab
UD
1925 gnu89, gnu99, c89, c99, iso9899:1990,\n\
1926 iso9899:199409, iso9899:1999\n\
6de1e2a9
ZW
1927 -+ Allow parsing of C++ style features\n\
1928 -w Inhibit warning messages\n\
1929 -Wtrigraphs Warn if trigraphs are encountered\n\
1930 -Wno-trigraphs Do not warn about trigraphs\n\
1931 -Wcomment{s} Warn if one comment starts inside another\n\
aaaf7848
DT
1932"), stdout);
1933 fputs (_("\
6de1e2a9 1934 -Wno-comment{s} Do not warn about comments\n\
f9a0e96c
ZW
1935 -Wtraditional Warn about features not present in traditional C\n\
1936 -Wno-traditional Do not warn about traditional C\n\
6de1e2a9
ZW
1937 -Wundef Warn if an undefined macro is used by #if\n\
1938 -Wno-undef Do not warn about testing undefined macros\n\
1939 -Wimport Warn about the use of the #import directive\n\
aaaf7848
DT
1940"), stdout);
1941 fputs (_("\
6de1e2a9
ZW
1942 -Wno-import Do not warn about the use of #import\n\
1943 -Werror Treat all warnings as errors\n\
1944 -Wno-error Do not treat warnings as errors\n\
317639a8
BC
1945 -Wsystem-headers Do not suppress warnings from system headers\n\
1946 -Wno-system-headers Suppress warnings from system headers\n\
6de1e2a9 1947 -Wall Enable all preprocessor warnings\n\
aaaf7848
DT
1948"), stdout);
1949 fputs (_("\
317639a8
BC
1950 -M Generate make dependencies\n\
1951 -MM As -M, but ignore system header files\n\
576786b0
NB
1952 -MD Generate make dependencies and compile\n\
1953 -MMD As -MD, but ignore system header files\n\
96302433 1954 -MF <file> Write dependency output to the given file\n\
6de1e2a9 1955 -MG Treat missing header file as generated files\n\
96302433
NB
1956"), stdout);
1957 fputs (_("\
1958 -MP Generate phony targets for all headers\n\
1959 -MQ <target> Add a MAKE-quoted target\n\
1960 -MT <target> Add an unquoted target\n\
aaaf7848
DT
1961"), stdout);
1962 fputs (_("\
317639a8
BC
1963 -D<macro> Define a <macro> with string '1' as its value\n\
1964 -D<macro>=<val> Define a <macro> with <val> as its value\n\
576786b0
NB
1965 -A<question>=<answer> Assert the <answer> to <question>\n\
1966 -A-<question>=<answer> Disable the <answer> to <question>\n\
6de1e2a9 1967 -U<macro> Undefine <macro> \n\
6de1e2a9 1968 -v Display the version number\n\
aaaf7848
DT
1969"), stdout);
1970 fputs (_("\
317639a8
BC
1971 -H Print the name of header files as they are used\n\
1972 -C Do not discard comments\n\
6de1e2a9
ZW
1973 -dM Display a list of macro definitions active at end\n\
1974 -dD Preserve macro definitions in output\n\
1975 -dN As -dD except that only the names are preserved\n\
1976 -dI Include #include directives in the output\n\
317639a8
BC
1977"), stdout);
1978 fputs (_("\
3bce8a01 1979 -fpreprocessed Treat the input file as already preprocessed\n\
6ab3e7dd 1980 -ftabstop=<number> Distance between tab stops for column reporting\n\
6de1e2a9
ZW
1981 -P Do not generate #line directives\n\
1982 -$ Do not allow '$' in identifiers\n\
576786b0 1983 -remap Remap file names when including files\n\
e23c0ba3 1984 --version Display version information\n\
6de1e2a9
ZW
1985 -h or --help Display this information\n\
1986"), stdout);
1987}