]> gcc.gnu.org Git - gcc.git/blame - gcc/cppinit.c
Makefile.in (c-opts.o): Update
[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"
6de1e2a9 31
88ae23e7
ZW
32/* Windows does not natively support inodes, and neither does MSDOS.
33 Cygwin's emulation can generate non-unique inodes, so don't use it.
ec5c56db 34 VMS has non-numeric inodes. */
88ae23e7 35#ifdef VMS
ca47c89e
DR
36# define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
37# define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
88ae23e7 38#else
3943e756 39# if (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
ca47c89e 40# define INO_T_EQ(A, B) 0
3943e756 41# else
ca47c89e 42# define INO_T_EQ(A, B) ((A) == (B))
3943e756 43# endif
ca47c89e 44# define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
88ae23e7
ZW
45#endif
46
4a58aab6 47/* Internal structures and prototypes. */
6de1e2a9 48
4a58aab6
NB
49/* A `struct pending_option' remembers one -D, -A, -U, -include, or
50 -imacros switch. */
8be1ddca 51typedef void (* cl_directive_handler) PARAMS ((cpp_reader *, const char *));
0b22d65c 52struct pending_option
6de1e2a9 53{
0b22d65c 54 struct pending_option *next;
7ceb3598 55 const char *arg;
40eac643 56 cl_directive_handler handler;
6de1e2a9 57};
0b22d65c 58
88ae23e7 59/* The `pending' structure accumulates all the options that are not
f5e99456 60 actually processed until we hit cpp_read_main_file. It consists of
88ae23e7 61 several lists, one for each type of option. We keep both head and
4a58aab6 62 tail pointers for quick insertion. */
88ae23e7
ZW
63struct cpp_pending
64{
40eac643 65 struct pending_option *directive_head, *directive_tail;
88ae23e7 66
591e15a1
NB
67 struct search_path *quote_head, *quote_tail;
68 struct search_path *brack_head, *brack_tail;
69 struct search_path *systm_head, *systm_tail;
70 struct search_path *after_head, *after_tail;
88ae23e7
ZW
71
72 struct pending_option *imacros_head, *imacros_tail;
73 struct pending_option *include_head, *include_tail;
74};
75
0b22d65c
ZW
76#ifdef __STDC__
77#define APPEND(pend, list, elt) \
78 do { if (!(pend)->list##_head) (pend)->list##_head = (elt); \
79 else (pend)->list##_tail->next = (elt); \
80 (pend)->list##_tail = (elt); \
81 } while (0)
82#else
83#define APPEND(pend, list, elt) \
84 do { if (!(pend)->list/**/_head) (pend)->list/**/_head = (elt); \
85 else (pend)->list/**/_tail->next = (elt); \
86 (pend)->list/**/_tail = (elt); \
87 } while (0)
88#endif
6de1e2a9 89
0b22d65c 90static void path_include PARAMS ((cpp_reader *,
0b22d65c 91 char *, int));
674c3b40 92static void init_library PARAMS ((void));
7ca3d2b1 93static void init_builtins PARAMS ((cpp_reader *));
17645b15 94static void mark_named_operators PARAMS ((cpp_reader *));
0b22d65c 95static void append_include_chain PARAMS ((cpp_reader *,
c45da1ca 96 char *, int, int));
002ee64f 97static struct search_path * remove_dup_dir PARAMS ((cpp_reader *,
591e15a1 98 struct search_path *));
002ee64f 99static struct search_path * remove_dup_dirs PARAMS ((cpp_reader *,
591e15a1 100 struct search_path *));
ae79697b 101static void merge_include_chains PARAMS ((cpp_reader *));
d7bc7a98
NB
102static bool push_include PARAMS ((cpp_reader *,
103 struct pending_option *));
104static void free_chain PARAMS ((struct pending_option *));
dd07b884 105static void set_lang PARAMS ((cpp_reader *, enum c_lang));
7ca3d2b1
NB
106static void init_dependency_output PARAMS ((cpp_reader *));
107static void init_standard_includes PARAMS ((cpp_reader *));
f5e99456 108static void read_original_filename PARAMS ((cpp_reader *));
2c0accc9 109static void new_pending_directive PARAMS ((struct cpp_pending *,
40eac643
NB
110 const char *,
111 cl_directive_handler));
7ca3d2b1 112static void output_deps PARAMS ((cpp_reader *));
e23c0ba3 113static int parse_option PARAMS ((const char *));
6de1e2a9 114
cb773845
ZW
115/* Fourth argument to append_include_chain: chain to use.
116 Note it's never asked to append to the quote chain. */
117enum { BRACKET = 0, SYSTEM, AFTER };
6de1e2a9 118
61d0346d
NB
119/* If we have designated initializers (GCC >2.7) these tables can be
120 initialized, constant data. Otherwise, they have to be filled in at
12cf91fe 121 runtime. */
61d0346d 122#if HAVE_DESIGNATED_INITIALIZERS
a9ae4483 123
4a58aab6 124#define init_trigraph_map() /* Nothing. */
61d0346d 125#define TRIGRAPH_MAP \
562a5c27 126__extension__ const uchar _cpp_trigraph_map[UCHAR_MAX + 1] = {
61d0346d 127
a9ae4483 128#define END };
455d2586 129#define s(p, v) [p] = v,
61d0346d 130
a9ae4483 131#else
61d0346d 132
562a5c27 133#define TRIGRAPH_MAP uchar _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
61d0346d
NB
134 static void init_trigraph_map PARAMS ((void)) { \
135 unsigned char *x = _cpp_trigraph_map;
136
ae79697b 137#define END }
455d2586 138#define s(p, v) x[p] = v;
61d0346d 139
a9ae4483 140#endif
6de1e2a9 141
61d0346d
NB
142TRIGRAPH_MAP
143 s('=', '#') s(')', ']') s('!', '|')
144 s('(', '[') s('\'', '^') s('>', '}')
145 s('/', '\\') s('<', '{') s('-', '~')
146END
147
a9ae4483 148#undef s
455d2586 149#undef END
61d0346d 150#undef TRIGRAPH_MAP
6de1e2a9
ZW
151
152/* Given a colon-separated list of file names PATH,
153 add all the names to the search path for include files. */
6de1e2a9 154static void
e33f6253 155path_include (pfile, list, path)
6de1e2a9 156 cpp_reader *pfile;
0b22d65c
ZW
157 char *list;
158 int path;
6de1e2a9 159{
0b22d65c 160 char *p, *q, *name;
6de1e2a9 161
0b22d65c 162 p = list;
6de1e2a9 163
0b22d65c
ZW
164 do
165 {
6de1e2a9 166 /* Find the end of this name. */
0b22d65c 167 q = p;
6de1e2a9 168 while (*q != 0 && *q != PATH_SEPARATOR) q++;
0b22d65c
ZW
169 if (q == p)
170 {
171 /* An empty name in the path stands for the current directory. */
172 name = (char *) xmalloc (2);
173 name[0] = '.';
174 name[1] = 0;
175 }
176 else
177 {
178 /* Otherwise use the directory that is named. */
179 name = (char *) xmalloc (q - p + 1);
180 memcpy (name, p, q - p);
181 name[q - p] = 0;
182 }
6de1e2a9 183
e33f6253 184 append_include_chain (pfile, name, path, 0);
6de1e2a9
ZW
185
186 /* Advance past this name. */
0b22d65c 187 if (*q == 0)
6de1e2a9 188 break;
0b22d65c
ZW
189 p = q + 1;
190 }
191 while (1);
192}
193
bef985f3 194/* Append DIR to include path PATH. DIR must be allocated on the
5d8ebbd8
NB
195 heap; this routine takes responsibility for freeing it. CXX_AWARE
196 is non-zero if the header contains extern "C" guards for C++,
197 otherwise it is zero. */
0b22d65c 198static void
e33f6253 199append_include_chain (pfile, dir, path, cxx_aware)
0b22d65c 200 cpp_reader *pfile;
0b22d65c
ZW
201 char *dir;
202 int path;
39e5db1a 203 int cxx_aware;
0b22d65c 204{
e33f6253 205 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
591e15a1 206 struct search_path *new;
0b22d65c
ZW
207 struct stat st;
208 unsigned int len;
209
f9200da2 210 if (*dir == '\0')
bef985f3
NB
211 {
212 free (dir);
213 dir = xstrdup (".");
214 }
b0699dad 215 _cpp_simplify_pathname (dir);
bef985f3 216
0b22d65c
ZW
217 if (stat (dir, &st))
218 {
f9200da2 219 /* Dirs that don't exist are silently ignored. */
0b22d65c 220 if (errno != ENOENT)
ebef4e8c 221 cpp_errno (pfile, DL_ERROR, dir);
ae79697b 222 else if (CPP_OPTION (pfile, verbose))
041c3194 223 fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"), dir);
bef985f3 224 free (dir);
0b22d65c
ZW
225 return;
226 }
227
228 if (!S_ISDIR (st.st_mode))
229 {
ebef4e8c 230 cpp_error_with_line (pfile, DL_ERROR, 0, 0, "%s: Not a directory", dir);
bef985f3 231 free (dir);
0b22d65c
ZW
232 return;
233 }
234
235 len = strlen (dir);
236 if (len > pfile->max_include_len)
237 pfile->max_include_len = len;
ae79697b 238
591e15a1 239 new = (struct search_path *) xmalloc (sizeof (struct search_path));
0b22d65c 240 new->name = dir;
591e15a1 241 new->len = len;
ca47c89e 242 INO_T_COPY (new->ino, st.st_ino);
0b22d65c 243 new->dev = st.st_dev;
4737b274
CF
244 /* Both systm and after include file lists should be treated as system
245 include files since these two lists are really just a concatenation
ec5c56db 246 of one "system" list. */
4737b274 247 if (path == SYSTEM || path == AFTER)
c45da1ca
ZW
248 new->sysp = cxx_aware ? 1 : 2;
249 else
250 new->sysp = 0;
0b22d65c 251 new->name_map = NULL;
503cb436 252 new->next = NULL;
0b22d65c
ZW
253
254 switch (path)
255 {
0b22d65c
ZW
256 case BRACKET: APPEND (pend, brack, new); break;
257 case SYSTEM: APPEND (pend, systm, new); break;
258 case AFTER: APPEND (pend, after, new); break;
6de1e2a9
ZW
259 }
260}
261
dd69c71b
NB
262/* Handle a duplicated include path. PREV is the link in the chain
263 before the duplicate. The duplicate is removed from the chain and
264 freed. Returns PREV. */
002ee64f 265static struct search_path *
dd69c71b
NB
266remove_dup_dir (pfile, prev)
267 cpp_reader *pfile;
591e15a1 268 struct search_path *prev;
dd69c71b 269{
591e15a1 270 struct search_path *cur = prev->next;
dd69c71b
NB
271
272 if (CPP_OPTION (pfile, verbose))
273 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), cur->name);
274
275 prev->next = cur->next;
591e15a1 276 free ((PTR) cur->name);
dd69c71b
NB
277 free (cur);
278
279 return prev;
280}
281
282/* Remove duplicate directories from a chain. Returns the tail of the
283 chain, or NULL if the chain is empty. This algorithm is quadratic
284 in the number of -I switches, which is acceptable since there
285 aren't usually that many of them. */
002ee64f 286static struct search_path *
dd69c71b
NB
287remove_dup_dirs (pfile, head)
288 cpp_reader *pfile;
591e15a1 289 struct search_path *head;
dd69c71b 290{
591e15a1 291 struct search_path *prev = NULL, *cur, *other;
dd69c71b
NB
292
293 for (cur = head; cur; cur = cur->next)
294 {
295 for (other = head; other != cur; other = other->next)
df383483 296 if (INO_T_EQ (cur->ino, other->ino) && cur->dev == other->dev)
dd69c71b 297 {
002ee64f 298 if (cur->sysp && !other->sysp)
dbead49c 299 {
ebef4e8c
NB
300 cpp_error (pfile, DL_WARNING,
301 "changing search order for system directory \"%s\"",
302 cur->name);
dbead49c 303 if (strcmp (cur->name, other->name))
ebef4e8c
NB
304 cpp_error (pfile, DL_WARNING,
305 " as it is the same as non-system directory \"%s\"",
306 other->name);
dbead49c 307 else
ebef4e8c
NB
308 cpp_error (pfile, DL_WARNING,
309 " as it has already been specified as a non-system directory");
dbead49c 310 }
dd69c71b
NB
311 cur = remove_dup_dir (pfile, prev);
312 break;
313 }
314 prev = cur;
315 }
316
317 return prev;
318}
319
88ae23e7
ZW
320/* Merge the four include chains together in the order quote, bracket,
321 system, after. Remove duplicate dirs (as determined by
322 INO_T_EQ()). The system_include and after_include chains are never
323 referred to again after this function; all access is through the
5d8ebbd8 324 bracket_include path. */
88ae23e7 325static void
ae79697b
ZW
326merge_include_chains (pfile)
327 cpp_reader *pfile;
88ae23e7 328{
591e15a1 329 struct search_path *quote, *brack, *systm, *qtail;
88ae23e7 330
ae79697b 331 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
88ae23e7 332
ae79697b
ZW
333 quote = pend->quote_head;
334 brack = pend->brack_head;
335 systm = pend->systm_head;
dd69c71b 336 qtail = pend->quote_tail;
88ae23e7 337
dd69c71b
NB
338 /* Paste together bracket, system, and after include chains. */
339 if (systm)
340 pend->systm_tail->next = pend->after_head;
88ae23e7 341 else
dd69c71b
NB
342 systm = pend->after_head;
343
344 if (brack)
345 pend->brack_tail->next = systm;
88ae23e7
ZW
346 else
347 brack = systm;
348
dd69c71b
NB
349 /* This is a bit tricky. First we drop dupes from the quote-include
350 list. Then we drop dupes from the bracket-include list.
351 Finally, if qtail and brack are the same directory, we cut out
cb773845 352 brack and move brack up to point to qtail.
88ae23e7
ZW
353
354 We can't just merge the lists and then uniquify them because
355 then we may lose directories from the <> search path that should
356 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
357 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
dd69c71b 358 -Ibar -I- -Ifoo -Iquux. */
88ae23e7 359
dd69c71b
NB
360 remove_dup_dirs (pfile, brack);
361 qtail = remove_dup_dirs (pfile, quote);
88ae23e7
ZW
362
363 if (quote)
364 {
dd69c71b
NB
365 qtail->next = brack;
366
367 /* If brack == qtail, remove brack as it's simpler. */
afb58288
ZW
368 if (brack && INO_T_EQ (qtail->ino, brack->ino)
369 && qtail->dev == brack->dev)
dd69c71b 370 brack = remove_dup_dir (pfile, qtail);
88ae23e7
ZW
371 }
372 else
bef985f3 373 quote = brack;
88ae23e7 374
ae79697b
ZW
375 CPP_OPTION (pfile, quote_include) = quote;
376 CPP_OPTION (pfile, bracket_include) = brack;
88ae23e7
ZW
377}
378
5d8ebbd8
NB
379/* A set of booleans indicating what CPP features each source language
380 requires. */
a01eb545
ZW
381struct lang_flags
382{
383 char c99;
a01eb545
ZW
384 char cplusplus;
385 char extended_numbers;
58551c23 386 char std;
a01eb545
ZW
387 char dollars_in_ident;
388 char cplusplus_comments;
389 char digraphs;
390};
391
392/* ??? Enable $ in identifiers in assembly? */
393static const struct lang_flags lang_defaults[] =
58551c23 394{ /* c99 c++ xnum std dollar c++comm digr */
bdcae02b
NB
395 /* GNUC89 */ { 0, 0, 1, 0, 1, 1, 1 },
396 /* GNUC99 */ { 1, 0, 1, 0, 1, 1, 1 },
397 /* STDC89 */ { 0, 0, 0, 1, 0, 0, 0 },
398 /* STDC94 */ { 0, 0, 0, 1, 0, 0, 1 },
399 /* STDC99 */ { 1, 0, 1, 1, 0, 1, 1 },
400 /* GNUCXX */ { 0, 1, 1, 0, 1, 1, 1 },
401 /* CXX98 */ { 0, 1, 1, 1, 0, 1, 1 },
402 /* ASM */ { 0, 0, 1, 0, 0, 1, 0 }
a01eb545
ZW
403};
404
5d8ebbd8 405/* Sets internal flags correctly for a given language. */
dd07b884
NB
406static void
407set_lang (pfile, lang)
408 cpp_reader *pfile;
409 enum c_lang lang;
410{
a01eb545 411 const struct lang_flags *l = &lang_defaults[(int) lang];
df383483 412
bdb05a7b 413 CPP_OPTION (pfile, lang) = lang;
dd07b884 414
a01eb545 415 CPP_OPTION (pfile, c99) = l->c99;
a01eb545
ZW
416 CPP_OPTION (pfile, cplusplus) = l->cplusplus;
417 CPP_OPTION (pfile, extended_numbers) = l->extended_numbers;
58551c23
NB
418 CPP_OPTION (pfile, std) = l->std;
419 CPP_OPTION (pfile, trigraphs) = l->std;
a01eb545
ZW
420 CPP_OPTION (pfile, dollars_in_ident) = l->dollars_in_ident;
421 CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments;
422 CPP_OPTION (pfile, digraphs) = l->digraphs;
dd07b884
NB
423}
424
7ca3d2b1
NB
425#ifdef HOST_EBCDIC
426static int opt_comp PARAMS ((const void *, const void *));
427
428/* Run-time sorting of options array. */
429static int
430opt_comp (p1, p2)
431 const void *p1, *p2;
432{
433 return strcmp (((struct cl_option *) p1)->opt_text,
434 ((struct cl_option *) p2)->opt_text);
435}
436#endif
cf44ea52 437
7ca3d2b1
NB
438/* init initializes library global state. It might not need to
439 do anything depending on the platform and compiler. */
cf44ea52 440static void
674c3b40 441init_library ()
cf44ea52 442{
7ca3d2b1
NB
443 static int initialized = 0;
444
445 if (! initialized)
446 {
447 initialized = 1;
448
cf44ea52 449#ifdef HOST_EBCDIC
7ca3d2b1
NB
450 /* For non-ASCII hosts, the cl_options array needs to be sorted at
451 runtime. */
452 qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
cf44ea52
NB
453#endif
454
7ca3d2b1
NB
455 /* Set up the trigraph map. This doesn't need to do anything if
456 we were compiled with a compiler that supports C99 designated
457 initializers. */
458 init_trigraph_map ();
459 }
cf44ea52
NB
460}
461
ec5c56db 462/* Initialize a cpp_reader structure. */
cf44ea52 463cpp_reader *
f5e99456 464cpp_create_reader (lang)
dd07b884 465 enum c_lang lang;
6de1e2a9 466{
7ca3d2b1 467 cpp_reader *pfile;
93c80368 468
cf44ea52 469 /* Initialise this instance of the library if it hasn't been already. */
674c3b40 470 init_library ();
7ca3d2b1
NB
471
472 pfile = (cpp_reader *) xcalloc (1, sizeof (cpp_reader));
93c80368 473
c740cee2 474 set_lang (pfile, lang);
ae79697b 475 CPP_OPTION (pfile, warn_import) = 1;
a5a49440 476 CPP_OPTION (pfile, warn_multichar) = 1;
ae79697b 477 CPP_OPTION (pfile, discard_comments) = 1;
477cdac7 478 CPP_OPTION (pfile, discard_comments_in_macro_exp) = 1;
ae79697b 479 CPP_OPTION (pfile, show_column) = 1;
6ab3e7dd 480 CPP_OPTION (pfile, tabstop) = 8;
be768055 481 CPP_OPTION (pfile, operator_names) = 1;
909de5da 482 CPP_OPTION (pfile, warn_endif_labels) = 1;
ae79697b
ZW
483
484 CPP_OPTION (pfile, pending) =
485 (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
486
2443d4e1
NB
487 /* Default CPP arithmetic to something sensible for the host for the
488 benefit of dumb users like fix-header. */
c9220e3a 489 CPP_OPTION (pfile, precision) = CHAR_BIT * sizeof (long);
2443d4e1
NB
490 CPP_OPTION (pfile, char_precision) = CHAR_BIT;
491 CPP_OPTION (pfile, wchar_precision) = CHAR_BIT * sizeof (int);
492 CPP_OPTION (pfile, int_precision) = CHAR_BIT * sizeof (int);
2a1dc0d8 493 CPP_OPTION (pfile, unsigned_char) = 0;
44a147ad 494 CPP_OPTION (pfile, unsigned_wchar) = 1;
4268e8bb 495
f7114e17
NB
496 /* It's simplest to just create this struct whether or not it will
497 be needed. */
498 pfile->deps = deps_init ();
499
50410426
NB
500 /* Initialise the line map. Start at logical line 1, so we can use
501 a line number of zero for special states. */
d82fc108 502 init_line_maps (&pfile->line_maps);
1a76916c 503 pfile->line = 1;
d82fc108 504
4a58aab6 505 /* Initialize lexer state. */
93c80368
NB
506 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
507
4ed5bcfb 508 /* Set up static tokens. */
4ed5bcfb
NB
509 pfile->avoid_paste.type = CPP_PADDING;
510 pfile->avoid_paste.val.source = NULL;
511 pfile->eof.type = CPP_EOF;
512 pfile->eof.flags = 0;
93c80368 513
5fddcffc
NB
514 /* Create a token buffer for the lexer. */
515 _cpp_init_tokenrun (&pfile->base_run, 250);
516 pfile->cur_run = &pfile->base_run;
517 pfile->cur_token = pfile->base_run.base;
5fddcffc 518
93c80368
NB
519 /* Initialise the base context. */
520 pfile->context = &pfile->base_context;
521 pfile->base_context.macro = 0;
522 pfile->base_context.prev = pfile->base_context.next = 0;
523
8c3b2693
NB
524 /* Aligned and unaligned storage. */
525 pfile->a_buff = _cpp_get_buff (pfile, 0);
ece54d54 526 pfile->u_buff = _cpp_get_buff (pfile, 0);
93c80368 527
87ed109f
NB
528 /* The expression parser stack. */
529 _cpp_expand_op_stack (pfile);
530
2a967f3d
NB
531 /* Initialise the buffer obstack. */
532 gcc_obstack_init (&pfile->buffer_ob);
533
c71f835b 534 _cpp_init_includes (pfile);
cf44ea52
NB
535
536 return pfile;
f2d5f0cc
ZW
537}
538
400023a3 539/* Free resources used by PFILE. Accessing PFILE after this function
5d8ebbd8 540 returns leads to undefined behaviour. Returns the error count. */
400023a3
NB
541int
542cpp_destroy (pfile)
6de1e2a9
ZW
543 cpp_reader *pfile;
544{
400023a3 545 int result;
591e15a1 546 struct search_path *dir, *dirn;
93c80368 547 cpp_context *context, *contextn;
50410426 548 tokenrun *run, *runn;
709e9e50 549
af0d16cd
NB
550 free_chain (CPP_OPTION (pfile, pending)->include_head);
551 free (CPP_OPTION (pfile, pending));
87ed109f 552 free (pfile->op_stack);
af0d16cd 553
38b24ee2 554 while (CPP_BUFFER (pfile) != NULL)
ef6e958a 555 _cpp_pop_buffer (pfile);
6de1e2a9 556
1a76916c
NB
557 if (pfile->out.base)
558 free (pfile->out.base);
004cb263 559
93c80368 560 if (pfile->macro_buffer)
4b49c365
AO
561 {
562 free ((PTR) pfile->macro_buffer);
563 pfile->macro_buffer = NULL;
564 pfile->macro_buffer_len = 0;
565 }
93c80368 566
f7114e17 567 deps_free (pfile->deps);
2a967f3d 568 obstack_free (&pfile->buffer_ob, 0);
49e6c08e 569
2a967f3d 570 _cpp_destroy_hashtable (pfile);
bfb9dc7f 571 _cpp_cleanup_includes (pfile);
709e9e50 572
8c3b2693 573 _cpp_free_buff (pfile->a_buff);
ece54d54 574 _cpp_free_buff (pfile->u_buff);
b8af0ca5 575 _cpp_free_buff (pfile->free_buffs);
93c80368 576
50410426
NB
577 for (run = &pfile->base_run; run; run = runn)
578 {
579 runn = run->next;
580 free (run->base);
581 if (run != &pfile->base_run)
582 free (run);
583 }
584
93c80368 585 for (dir = CPP_OPTION (pfile, quote_include); dir; dir = dirn)
709e9e50 586 {
93c80368 587 dirn = dir->next;
591e15a1 588 free ((PTR) dir->name);
709e9e50
NB
589 free (dir);
590 }
93c80368
NB
591
592 for (context = pfile->base_context.next; context; context = contextn)
593 {
594 contextn = context->next;
595 free (context);
596 }
400023a3 597
d82fc108
NB
598 free_line_maps (&pfile->line_maps);
599
400023a3
NB
600 result = pfile->errors;
601 free (pfile);
602
603 return result;
6de1e2a9
ZW
604}
605
93c80368 606/* This structure defines one built-in identifier. A node will be
f24a153a
ZW
607 entered in the hash table under the name NAME, with value VALUE.
608
609 There are two tables of these. builtin_array holds all the
610 "builtin" macros: these are handled by builtin_macro() in
611 cppmacro.c. Builtin is somewhat of a misnomer -- the property of
612 interest is that these macros require special code to compute their
613 expansions. The value is a "builtin_type" enumerator.
614
615 operator_array holds the C++ named operators. These are keywords
616 which act as aliases for punctuators. In C++, they cannot be
617 altered through #define, and #if recognizes them as operators. In
618 C, these are not entered into the hash table at all (but see
619 <iso646.h>). The value is a token-type enumerator. */
a9ae4483
ZW
620struct builtin
621{
562a5c27 622 const uchar *name;
93c80368 623 unsigned short len;
f24a153a 624 unsigned short value;
a9ae4483 625};
93c80368 626
f24a153a 627#define B(n, t) { DSC(n), t }
a9ae4483 628static const struct builtin builtin_array[] =
6de1e2a9 629{
93c80368
NB
630 B("__TIME__", BT_TIME),
631 B("__DATE__", BT_DATE),
632 B("__FILE__", BT_FILE),
633 B("__BASE_FILE__", BT_BASE_FILE),
634 B("__LINE__", BT_SPECLINE),
635 B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL),
278c4662
NB
636 /* Keep builtins not used for -traditional-cpp at the end, and
637 update init_builtins() if any more are added. */
644eddaa 638 B("_Pragma", BT_PRAGMA),
618cdda7 639 B("__STDC__", BT_STDC),
f24a153a 640};
92936ecf 641
f24a153a
ZW
642static const struct builtin operator_array[] =
643{
644 B("and", CPP_AND_AND),
645 B("and_eq", CPP_AND_EQ),
646 B("bitand", CPP_AND),
647 B("bitor", CPP_OR),
648 B("compl", CPP_COMPL),
649 B("not", CPP_NOT),
650 B("not_eq", CPP_NOT_EQ),
651 B("or", CPP_OR_OR),
652 B("or_eq", CPP_OR_EQ),
653 B("xor", CPP_XOR),
654 B("xor_eq", CPP_XOR_EQ)
a9ae4483 655};
12cf91fe 656#undef B
a9ae4483 657
17645b15
NB
658/* Mark the C++ named operators in the hash table. */
659static void
660mark_named_operators (pfile)
661 cpp_reader *pfile;
662{
663 const struct builtin *b;
664
665 for (b = operator_array;
666 b < (operator_array + ARRAY_SIZE (operator_array));
667 b++)
668 {
669 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
670 hp->flags |= NODE_OPERATOR;
671 hp->value.operator = b->value;
672 }
673}
674
f5e99456 675/* Subroutine of cpp_read_main_file; reads the builtins table above and
5d8ebbd8 676 enters them, and language-specific macros, into the hash table. */
a9ae4483 677static void
7ca3d2b1 678init_builtins (pfile)
a9ae4483
ZW
679 cpp_reader *pfile;
680{
a9ae4483 681 const struct builtin *b;
278c4662 682 size_t n = ARRAY_SIZE (builtin_array);
771c4df3 683
278c4662
NB
684 if (CPP_OPTION (pfile, traditional))
685 n -= 2;
686
687 for(b = builtin_array; b < builtin_array + n; b++)
6de1e2a9 688 {
f24a153a
ZW
689 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
690 hp->type = NT_MACRO;
691 hp->flags |= NODE_BUILTIN | NODE_WARN;
692 hp->value.builtin = b->value;
6de1e2a9 693 }
c740cee2
NB
694
695 if (CPP_OPTION (pfile, cplusplus))
3d90d290 696 _cpp_define_builtin (pfile, "__cplusplus 1");
2a1dc0d8 697 else if (CPP_OPTION (pfile, objc))
c740cee2 698 _cpp_define_builtin (pfile, "__OBJC__ 1");
2a1dc0d8
ZW
699 else if (CPP_OPTION (pfile, lang) == CLK_ASM)
700 _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
c740cee2
NB
701
702 if (CPP_OPTION (pfile, lang) == CLK_STDC94)
703 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
704 else if (CPP_OPTION (pfile, c99))
705 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
706
3d90d290
NB
707 if (pfile->cb.register_builtins)
708 (*pfile->cb.register_builtins) (pfile);
6de1e2a9
ZW
709}
710
c45da1ca
ZW
711/* And another subroutine. This one sets up the standard include path. */
712static void
7ca3d2b1 713init_standard_includes (pfile)
c45da1ca
ZW
714 cpp_reader *pfile;
715{
c45da1ca 716 char *path;
455d2586 717 const struct default_include *p;
ae79697b 718 const char *specd_prefix = CPP_OPTION (pfile, include_prefix);
c45da1ca
ZW
719
720 /* Several environment variables may add to the include search path.
721 CPATH specifies an additional list of directories to be searched
722 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
723 etc. specify an additional list of directories to be searched as
724 if specified with -isystem, for the language indicated. */
725
2f8dd115 726 GET_ENVIRONMENT (path, "CPATH");
c45da1ca 727 if (path != 0 && *path != 0)
e33f6253 728 path_include (pfile, path, BRACKET);
c45da1ca 729
ae79697b 730 switch ((CPP_OPTION (pfile, objc) << 1) + CPP_OPTION (pfile, cplusplus))
c45da1ca
ZW
731 {
732 case 0:
2f8dd115 733 GET_ENVIRONMENT (path, "C_INCLUDE_PATH");
c45da1ca
ZW
734 break;
735 case 1:
2f8dd115 736 GET_ENVIRONMENT (path, "CPLUS_INCLUDE_PATH");
c45da1ca
ZW
737 break;
738 case 2:
2f8dd115 739 GET_ENVIRONMENT (path, "OBJC_INCLUDE_PATH");
c45da1ca
ZW
740 break;
741 case 3:
2f8dd115 742 GET_ENVIRONMENT (path, "OBJCPLUS_INCLUDE_PATH");
c45da1ca
ZW
743 break;
744 }
745 if (path != 0 && *path != 0)
e33f6253 746 path_include (pfile, path, SYSTEM);
c45da1ca
ZW
747
748 /* Search "translated" versions of GNU directories.
749 These have /usr/local/lib/gcc... replaced by specd_prefix. */
60893f43 750 if (specd_prefix != 0 && cpp_GCC_INCLUDE_DIR_len)
c45da1ca 751 {
c45da1ca 752 /* Remove the `include' from /usr/local/lib/gcc.../include.
ec5c56db 753 GCC_INCLUDE_DIR will always end in /include. */
60893f43
ZW
754 int default_len = cpp_GCC_INCLUDE_DIR_len;
755 char *default_prefix = (char *) alloca (default_len + 1);
c45da1ca
ZW
756 int specd_len = strlen (specd_prefix);
757
60893f43 758 memcpy (default_prefix, cpp_GCC_INCLUDE_DIR, default_len);
c45da1ca
ZW
759 default_prefix[default_len] = '\0';
760
60893f43 761 for (p = cpp_include_defaults; p->fname; p++)
c45da1ca
ZW
762 {
763 /* Some standard dirs are only for C++. */
764 if (!p->cplusplus
ae79697b
ZW
765 || (CPP_OPTION (pfile, cplusplus)
766 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
c45da1ca
ZW
767 {
768 /* Does this dir start with the prefix? */
61d0346d 769 if (!memcmp (p->fname, default_prefix, default_len))
c45da1ca
ZW
770 {
771 /* Yes; change prefix and add to search list. */
772 int flen = strlen (p->fname);
773 int this_len = specd_len + flen - default_len;
774 char *str = (char *) xmalloc (this_len + 1);
775 memcpy (str, specd_prefix, specd_len);
776 memcpy (str + specd_len,
777 p->fname + default_len,
778 flen - default_len + 1);
779
e33f6253 780 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
c45da1ca
ZW
781 }
782 }
783 }
784 }
785
786 /* Search ordinary names for GNU include directories. */
60893f43 787 for (p = cpp_include_defaults; p->fname; p++)
c45da1ca
ZW
788 {
789 /* Some standard dirs are only for C++. */
790 if (!p->cplusplus
ae79697b
ZW
791 || (CPP_OPTION (pfile, cplusplus)
792 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
c45da1ca 793 {
51c04256 794 char *str = update_path (p->fname, p->component);
e33f6253 795 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
c45da1ca
ZW
796 }
797 }
798}
799
5d8ebbd8 800/* Pushes a command line -imacro and -include file indicated by P onto
d7bc7a98
NB
801 the buffer stack. Returns non-zero if successful. */
802static bool
803push_include (pfile, p)
4a58aab6
NB
804 cpp_reader *pfile;
805 struct pending_option *p;
4a58aab6 806{
d7bc7a98 807 cpp_token header;
4a58aab6 808
d7bc7a98
NB
809 /* Later: maybe update this to use the #include "" search path
810 if cpp_read_file fails. */
811 header.type = CPP_STRING;
812 header.val.str.text = (const unsigned char *) p->arg;
813 header.val.str.len = strlen (p->arg);
814 /* Make the command line directive take up a line. */
50410426 815 pfile->line++;
d7bc7a98
NB
816
817 return _cpp_execute_include (pfile, &header, IT_CMDLINE);
818}
819
820/* Frees a pending_option chain. */
821static void
822free_chain (head)
823 struct pending_option *head;
824{
825 struct pending_option *next;
826
827 while (head)
828 {
829 next = head->next;
830 free (head);
831 head = next;
4a58aab6
NB
832 }
833}
834
2443d4e1
NB
835/* Sanity-checks are dependent on command-line options, so it is
836 called as a subroutine of cpp_read_main_file (). */
837#if ENABLE_CHECKING
838static void sanity_checks PARAMS ((cpp_reader *));
839static void sanity_checks (pfile)
840 cpp_reader *pfile;
841{
842 cppchar_t test = 0;
c9220e3a 843 size_t max_precision = 2 * CHAR_BIT * sizeof (cpp_num_part);
2443d4e1
NB
844
845 /* Sanity checks for assumptions about CPP arithmetic and target
846 type precisions made by cpplib. */
847 test--;
848 if (test < 1)
bdee42b1 849 cpp_error (pfile, DL_ICE, "cppchar_t must be an unsigned type");
2443d4e1 850
c9220e3a 851 if (CPP_OPTION (pfile, precision) > max_precision)
bdee42b1 852 cpp_error (pfile, DL_ICE,
68bd6dd6 853 "preprocessor arithmetic has maximum precision of %lu bits; target requires %lu bits",
c9220e3a
NB
854 (unsigned long) max_precision,
855 (unsigned long) CPP_OPTION (pfile, precision));
2443d4e1
NB
856
857 if (CPP_OPTION (pfile, precision) < CPP_OPTION (pfile, int_precision))
bdee42b1 858 cpp_error (pfile, DL_ICE,
2443d4e1
NB
859 "CPP arithmetic must be at least as precise as a target int");
860
861 if (CPP_OPTION (pfile, char_precision) < 8)
bdee42b1 862 cpp_error (pfile, DL_ICE, "target char is less than 8 bits wide");
2443d4e1
NB
863
864 if (CPP_OPTION (pfile, wchar_precision) < CPP_OPTION (pfile, char_precision))
bdee42b1 865 cpp_error (pfile, DL_ICE,
2443d4e1
NB
866 "target wchar_t is narrower than target char");
867
868 if (CPP_OPTION (pfile, int_precision) < CPP_OPTION (pfile, char_precision))
bdee42b1 869 cpp_error (pfile, DL_ICE,
2443d4e1
NB
870 "target int is narrower than target char");
871
c9220e3a
NB
872 /* This is assumed in eval_token() and could be fixed if necessary. */
873 if (sizeof (cppchar_t) > sizeof (cpp_num_part))
874 cpp_error (pfile, DL_ICE, "CPP half-integer narrower than CPP character");
875
2443d4e1 876 if (CPP_OPTION (pfile, wchar_precision) > BITS_PER_CPPCHAR_T)
bdee42b1 877 cpp_error (pfile, DL_ICE,
68bd6dd6 878 "CPP on this host cannot handle wide character constants over %lu bits, but the target requires %lu bits",
c9220e3a
NB
879 (unsigned long) BITS_PER_CPPCHAR_T,
880 (unsigned long) CPP_OPTION (pfile, wchar_precision));
2443d4e1
NB
881}
882#else
883# define sanity_checks(PFILE)
884#endif
885
f5e99456
NB
886/* This is called after options have been parsed, and partially
887 processed. Setup for processing input from the file named FNAME,
888 or stdin if it is the empty string. Return the original filename
889 on success (e.g. foo.i->foo.c), or NULL on failure. */
890const char *
891cpp_read_main_file (pfile, fname, table)
6de1e2a9 892 cpp_reader *pfile;
7ceb3598 893 const char *fname;
f5e99456 894 hash_table *table;
6de1e2a9 895{
2443d4e1
NB
896 sanity_checks (pfile);
897
f5e99456
NB
898 /* The front ends don't set up the hash table until they have
899 finished processing the command line options, so initializing the
900 hashtable is deferred until now. */
901 _cpp_init_hashtable (pfile, table);
902
c45da1ca 903 /* Set up the include search path now. */
ae79697b 904 if (! CPP_OPTION (pfile, no_standard_includes))
7ca3d2b1 905 init_standard_includes (pfile);
c45da1ca 906
ae79697b 907 merge_include_chains (pfile);
c45da1ca
ZW
908
909 /* With -v, print the list of dirs to search. */
ae79697b 910 if (CPP_OPTION (pfile, verbose))
c45da1ca 911 {
591e15a1 912 struct search_path *l;
c45da1ca 913 fprintf (stderr, _("#include \"...\" search starts here:\n"));
ae79697b 914 for (l = CPP_OPTION (pfile, quote_include); l; l = l->next)
c45da1ca 915 {
ae79697b 916 if (l == CPP_OPTION (pfile, bracket_include))
c45da1ca
ZW
917 fprintf (stderr, _("#include <...> search starts here:\n"));
918 fprintf (stderr, " %s\n", l->name);
919 }
920 fprintf (stderr, _("End of search list.\n"));
921 }
922
96302433 923 if (CPP_OPTION (pfile, print_deps))
7855db7c 924 /* Set the default target (if there is none already). */
373e2177 925 deps_add_default_target (pfile->deps, fname);
96302433 926
f5e99456 927 /* Open the main input file. */
614c7d37 928 if (!_cpp_read_file (pfile, fname))
f5e99456 929 return NULL;
c45da1ca 930
5993019d
NB
931 /* Set this after cpp_post_options so the client can change the
932 option if it wishes, and after stacking the main file so we don't
933 trace the main file. */
934 pfile->line_maps.trace_includes = CPP_OPTION (pfile, print_include_names);
935
f5e99456
NB
936 /* For foo.i, read the original filename foo.c now, for the benefit
937 of the front ends. */
938 if (CPP_OPTION (pfile, preprocessed))
939 read_original_filename (pfile);
004cb263 940 /* Overlay an empty buffer to seed traditional preprocessing. */
1a76916c
NB
941 else if (CPP_OPTION (pfile, traditional)
942 && !CPP_OPTION (pfile, preprocess_only))
004cb263 943 _cpp_overlay_buffer (pfile, U"", 0);
f5e99456
NB
944
945 return pfile->map->to_file;
946}
947
948/* For preprocessed files, if the first tokens are of the form # NUM.
949 handle the directive so we know the original file name. This will
950 generate file_change callbacks, which the front ends must handle
951 appropriately given their state of initialization. */
952static void
953read_original_filename (pfile)
954 cpp_reader *pfile;
955{
956 const cpp_token *token, *token1;
957
958 /* Lex ahead; if the first tokens are of the form # NUM, then
959 process the directive, otherwise back up. */
960 token = _cpp_lex_direct (pfile);
961 if (token->type == CPP_HASH)
962 {
963 token1 = _cpp_lex_direct (pfile);
964 _cpp_backup_tokens (pfile, 1);
965
966 /* If it's a #line directive, handle it. */
967 if (token1->type == CPP_NUMBER)
968 {
969 _cpp_handle_directive (pfile, token->flags & PREV_WHITE);
970 return;
971 }
972 }
973
974 /* Backup as if nothing happened. */
975 _cpp_backup_tokens (pfile, 1);
976}
977
978/* Handle pending command line options: -D, -U, -A, -imacros and
979 -include. This should be called after debugging has been properly
980 set up in the front ends. */
981void
982cpp_finish_options (pfile)
983 cpp_reader *pfile;
984{
17645b15
NB
985 /* Mark named operators before handling command line macros. */
986 if (CPP_OPTION (pfile, cplusplus) && CPP_OPTION (pfile, operator_names))
987 mark_named_operators (pfile);
988
d7bc7a98
NB
989 /* Install builtins and process command line macros etc. in the order
990 they appeared, but only if not already preprocessed. */
05e81724 991 if (! CPP_OPTION (pfile, preprocessed))
6de1e2a9 992 {
d7bc7a98
NB
993 struct pending_option *p;
994
b0287a90 995 _cpp_do_file_change (pfile, LC_RENAME, _("<built-in>"), 1, 0);
cbc69f84 996 init_builtins (pfile);
d7bc7a98 997 _cpp_do_file_change (pfile, LC_RENAME, _("<command line>"), 1, 0);
cbc69f84
NB
998 for (p = CPP_OPTION (pfile, pending)->directive_head; p; p = p->next)
999 (*p->handler) (pfile, p->arg);
d7bc7a98 1000
af0d16cd
NB
1001 /* Scan -imacros files after -D, -U, but before -include.
1002 pfile->next_include_file is NULL, so _cpp_pop_buffer does not
1003 push -include files. */
1004 for (p = CPP_OPTION (pfile, pending)->imacros_head; p; p = p->next)
1005 if (push_include (pfile, p))
1006 cpp_scan_nooutput (pfile);
1007
1008 pfile->next_include_file = &CPP_OPTION (pfile, pending)->include_head;
1009 _cpp_maybe_push_include_file (pfile);
6de1e2a9 1010 }
05e81724 1011
a69cbaac
NB
1012 pfile->first_unused_line = pfile->line;
1013
af0d16cd 1014 free_chain (CPP_OPTION (pfile, pending)->imacros_head);
d7bc7a98 1015 free_chain (CPP_OPTION (pfile, pending)->directive_head);
d7bc7a98 1016}
6de1e2a9 1017
af0d16cd
NB
1018/* Push the next buffer on the stack given by -include, if any. */
1019void
1020_cpp_maybe_push_include_file (pfile)
d7bc7a98
NB
1021 cpp_reader *pfile;
1022{
af0d16cd 1023 if (pfile->next_include_file)
d7bc7a98 1024 {
af0d16cd 1025 struct pending_option *head = *pfile->next_include_file;
df383483 1026
af0d16cd
NB
1027 while (head && !push_include (pfile, head))
1028 head = head->next;
d7bc7a98 1029
af0d16cd
NB
1030 if (head)
1031 pfile->next_include_file = &head->next;
1032 else
d7bc7a98 1033 {
af0d16cd
NB
1034 /* All done; restore the line map from <command line>. */
1035 _cpp_do_file_change (pfile, LC_RENAME,
1036 pfile->line_maps.maps[0].to_file, 1, 0);
1037 /* Don't come back here again. */
1038 pfile->next_include_file = NULL;
d7bc7a98
NB
1039 }
1040 }
6de1e2a9
ZW
1041}
1042
2f638f96 1043/* Use mkdeps.c to output dependency information. */
7ca3d2b1
NB
1044static void
1045output_deps (pfile)
1046 cpp_reader *pfile;
1047{
1048 /* Stream on which to print the dependency information. */
1049 FILE *deps_stream = 0;
27c38fbe
KG
1050 const char *const deps_mode =
1051 CPP_OPTION (pfile, print_deps_append) ? "a" : "w";
7ca3d2b1 1052
56cd5b95 1053 if (CPP_OPTION (pfile, deps_file)[0] == '\0')
7ca3d2b1
NB
1054 deps_stream = stdout;
1055 else
1056 {
1057 deps_stream = fopen (CPP_OPTION (pfile, deps_file), deps_mode);
1058 if (deps_stream == 0)
1059 {
ebef4e8c 1060 cpp_errno (pfile, DL_ERROR, CPP_OPTION (pfile, deps_file));
7ca3d2b1
NB
1061 return;
1062 }
1063 }
1064
1065 deps_write (pfile->deps, deps_stream, 72);
1066
1067 if (CPP_OPTION (pfile, deps_phony_targets))
1068 deps_phony_targets (pfile->deps, deps_stream);
1069
1070 /* Don't close stdout. */
ab8e2228 1071 if (deps_stream != stdout)
7ca3d2b1
NB
1072 {
1073 if (ferror (deps_stream) || fclose (deps_stream) != 0)
bdee42b1 1074 cpp_error (pfile, DL_ERROR, "I/O error on output");
7ca3d2b1
NB
1075 }
1076}
1077
6de1e2a9
ZW
1078/* This is called at the end of preprocessing. It pops the
1079 last buffer and writes dependency output. It should also
1080 clear macro definitions, such that you could call cpp_start_read
4a58aab6 1081 with a new filename to restart processing. */
6de1e2a9 1082void
93c80368 1083cpp_finish (pfile)
6de1e2a9
ZW
1084 cpp_reader *pfile;
1085{
a69cbaac
NB
1086 /* Warn about unused macros before popping the final buffer. */
1087 if (CPP_OPTION (pfile, warn_unused_macros))
1088 cpp_forall_identifiers (pfile, _cpp_warn_if_unused_macro, NULL);
1089
7364fdd8
NB
1090 /* cpplex.c leaves the final buffer on the stack. This it so that
1091 it returns an unending stream of CPP_EOFs to the client. If we
a1f300c0 1092 popped the buffer, we'd dereference a NULL buffer pointer and
7364fdd8
NB
1093 segfault. It's nice to allow the client to do worry-free excess
1094 cpp_get_token calls. */
1095 while (pfile->buffer)
1096 _cpp_pop_buffer (pfile);
c1212d2f 1097
49e6c08e 1098 /* Don't write the deps file if preprocessing has failed. */
ae79697b 1099 if (CPP_OPTION (pfile, print_deps) && pfile->errors == 0)
7ca3d2b1 1100 output_deps (pfile);
3caee4a8 1101
d4506961
ZW
1102 /* Report on headers that could use multiple include guards. */
1103 if (CPP_OPTION (pfile, print_include_names))
c71f835b 1104 _cpp_report_missing_guards (pfile);
6de1e2a9
ZW
1105}
1106
5d8ebbd8 1107/* Add a directive to be handled later in the initialization phase. */
223dca6a 1108static void
ae79697b
ZW
1109new_pending_directive (pend, text, handler)
1110 struct cpp_pending *pend;
223dca6a 1111 const char *text;
40eac643 1112 cl_directive_handler handler;
223dca6a
RH
1113{
1114 struct pending_option *o = (struct pending_option *)
1115 xmalloc (sizeof (struct pending_option));
1116
7ceb3598 1117 o->arg = text;
223dca6a 1118 o->next = NULL;
40eac643 1119 o->handler = handler;
ae79697b 1120 APPEND (pend, directive, o);
223dca6a
RH
1121}
1122
ae79697b
ZW
1123/* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
1124 I.e. a const string initializer with parens around it. That is
1125 what N_("string") resolves to, so we make no_* be macros instead. */
c725bd79
NB
1126#define no_arg N_("argument missing after %s")
1127#define no_ass N_("assertion missing after %s")
1128#define no_dir N_("directory name missing after %s")
1129#define no_fil N_("file name missing after %s")
1130#define no_mac N_("macro name missing after %s")
1131#define no_pth N_("path name missing after %s")
1132#define no_num N_("number missing after %s")
1133#define no_tgt N_("target missing after %s")
ae79697b
ZW
1134
1135/* This is the list of all command line options, with the leading
1136 "-" removed. It must be sorted in ASCII collating order. */
1137#define COMMAND_LINE_OPTIONS \
ae79697b 1138 DEF_OPT("A", no_ass, OPT_A) \
ae79697b 1139 DEF_OPT("D", no_mac, OPT_D) \
ae79697b
ZW
1140 DEF_OPT("I", no_dir, OPT_I) \
1141 DEF_OPT("M", 0, OPT_M) \
b3124fac 1142 DEF_OPT("MD", no_fil, OPT_MD) \
96302433 1143 DEF_OPT("MF", no_fil, OPT_MF) \
ae79697b
ZW
1144 DEF_OPT("MG", 0, OPT_MG) \
1145 DEF_OPT("MM", 0, OPT_MM) \
b3124fac 1146 DEF_OPT("MMD", no_fil, OPT_MMD) \
a5a4ce3c 1147 DEF_OPT("MP", 0, OPT_MP) \
f7114e17 1148 DEF_OPT("MQ", no_tgt, OPT_MQ) \
03b9ab42 1149 DEF_OPT("MT", no_tgt, OPT_MT) \
ae79697b 1150 DEF_OPT("U", no_mac, OPT_U) \
ae79697b
ZW
1151 DEF_OPT("idirafter", no_dir, OPT_idirafter) \
1152 DEF_OPT("imacros", no_fil, OPT_imacros) \
1153 DEF_OPT("include", no_fil, OPT_include) \
1154 DEF_OPT("iprefix", no_pth, OPT_iprefix) \
1155 DEF_OPT("isystem", no_dir, OPT_isystem) \
1156 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix) \
1157 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore) \
1158 DEF_OPT("lang-asm", 0, OPT_lang_asm) \
1159 DEF_OPT("lang-c", 0, OPT_lang_c) \
1160 DEF_OPT("lang-c++", 0, OPT_lang_cplusplus) \
1161 DEF_OPT("lang-c89", 0, OPT_lang_c89) \
ae79697b 1162 DEF_OPT("lang-objc", 0, OPT_lang_objc) \
ae79697b 1163 DEF_OPT("o", no_fil, OPT_o) \
ae79697b 1164 DEF_OPT("remap", 0, OPT_remap) \
dd07b884 1165 DEF_OPT("std=c++98", 0, OPT_std_cplusplus98) \
ae79697b
ZW
1166 DEF_OPT("std=c89", 0, OPT_std_c89) \
1167 DEF_OPT("std=c99", 0, OPT_std_c99) \
1168 DEF_OPT("std=c9x", 0, OPT_std_c9x) \
1169 DEF_OPT("std=gnu89", 0, OPT_std_gnu89) \
1170 DEF_OPT("std=gnu99", 0, OPT_std_gnu99) \
1171 DEF_OPT("std=gnu9x", 0, OPT_std_gnu9x) \
1172 DEF_OPT("std=iso9899:1990", 0, OPT_std_iso9899_1990) \
1173 DEF_OPT("std=iso9899:199409", 0, OPT_std_iso9899_199409) \
1174 DEF_OPT("std=iso9899:1999", 0, OPT_std_iso9899_1999) \
4b7091eb 1175 DEF_OPT("std=iso9899:199x", 0, OPT_std_iso9899_199x)
ae79697b
ZW
1176
1177#define DEF_OPT(text, msg, code) code,
e23c0ba3
ZW
1178enum opt_code
1179{
ae79697b 1180 COMMAND_LINE_OPTIONS
e23c0ba3
ZW
1181 N_OPTS
1182};
ae79697b 1183#undef DEF_OPT
e23c0ba3
ZW
1184
1185struct cl_option
1186{
1187 const char *opt_text;
1188 const char *msg;
1189 size_t opt_len;
1190 enum opt_code opt_code;
1191};
1192
ae79697b 1193#define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
e23c0ba3
ZW
1194#ifdef HOST_EBCDIC
1195static struct cl_option cl_options[] =
1196#else
1197static const struct cl_option cl_options[] =
1198#endif
1199{
ae79697b 1200 COMMAND_LINE_OPTIONS
e23c0ba3
ZW
1201};
1202#undef DEF_OPT
ae79697b 1203#undef COMMAND_LINE_OPTIONS
e23c0ba3
ZW
1204
1205/* Perform a binary search to find which, if any, option the given
1206 command-line matches. Returns its index in the option array,
1207 negative on failure. Complications arise since some options can be
1208 suffixed with an argument, and multiple complete matches can occur,
09e77dee 1209 e.g. -pedantic and -pedantic-errors. */
e23c0ba3
ZW
1210static int
1211parse_option (input)
1212 const char *input;
1213{
1214 unsigned int md, mn, mx;
1215 size_t opt_len;
1216 int comp;
1217
1218 mn = 0;
1219 mx = N_OPTS;
1220
1221 while (mx > mn)
1222 {
1223 md = (mn + mx) / 2;
ae79697b 1224
e23c0ba3 1225 opt_len = cl_options[md].opt_len;
61d0346d 1226 comp = memcmp (input, cl_options[md].opt_text, opt_len);
ae79697b 1227
e23c0ba3
ZW
1228 if (comp > 0)
1229 mn = md + 1;
1230 else if (comp < 0)
1231 mx = md;
1232 else
1233 {
1234 if (input[opt_len] == '\0')
1235 return md;
1236 /* We were passed more text. If the option takes an argument,
1237 we may match a later option or we may have been passed the
1238 argument. The longest possible option match succeeds.
1239 If the option takes no arguments we have not matched and
4a58aab6 1240 continue the search (e.g. input="stdc++" match was "stdc"). */
e23c0ba3
ZW
1241 mn = md + 1;
1242 if (cl_options[md].msg)
1243 {
1244 /* Scan forwards. If we get an exact match, return it.
1245 Otherwise, return the longest option-accepting match.
4a58aab6 1246 This loops no more than twice with current options. */
e23c0ba3 1247 mx = md;
37b8524c 1248 for (; mn < (unsigned int) N_OPTS; mn++)
e23c0ba3
ZW
1249 {
1250 opt_len = cl_options[mn].opt_len;
61d0346d 1251 if (memcmp (input, cl_options[mn].opt_text, opt_len))
e23c0ba3
ZW
1252 break;
1253 if (input[opt_len] == '\0')
1254 return mn;
1255 if (cl_options[mn].msg)
1256 mx = mn;
1257 }
1258 return mx;
1259 }
1260 }
1261 }
1262
1263 return -1;
1264}
1265
6de1e2a9
ZW
1266/* Handle one command-line option in (argc, argv).
1267 Can be called multiple times, to handle multiple sets of options.
1268 Returns number of strings consumed. */
2c0accc9 1269int
09e77dee 1270cpp_handle_option (pfile, argc, argv)
6de1e2a9
ZW
1271 cpp_reader *pfile;
1272 int argc;
1273 char **argv;
1274{
6de1e2a9 1275 int i = 0;
f8f769ea 1276 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
6de1e2a9 1277
373e2177
NB
1278 /* Interpret "-" or a non-option as a file name. */
1279 if (argv[i][0] != '-' || argv[i][1] == '\0')
0b22d65c 1280 {
373e2177
NB
1281 if (CPP_OPTION (pfile, in_fname) == NULL)
1282 CPP_OPTION (pfile, in_fname) = argv[i];
1283 else if (CPP_OPTION (pfile, out_fname) == NULL)
ae79697b 1284 CPP_OPTION (pfile, out_fname) = argv[i];
0b22d65c 1285 else
bdee42b1 1286 cpp_error (pfile, DL_ERROR,
ebef4e8c 1287 "too many filenames. Type %s --help for usage info",
373e2177 1288 progname);
0b22d65c
ZW
1289 }
1290 else
e23c0ba3
ZW
1291 {
1292 enum opt_code opt_code;
1293 int opt_index;
7ceb3598 1294 const char *arg = 0;
e23c0ba3 1295
4a58aab6 1296 /* Skip over '-'. */
e23c0ba3
ZW
1297 opt_index = parse_option (&argv[i][1]);
1298 if (opt_index < 0)
1299 return i;
1300
1301 opt_code = cl_options[opt_index].opt_code;
1302 if (cl_options[opt_index].msg)
1303 {
1304 arg = &argv[i][cl_options[opt_index].opt_len + 1];
09e77dee 1305 if (arg[0] == '\0')
e23c0ba3
ZW
1306 {
1307 arg = argv[++i];
1308 if (!arg)
1309 {
bdee42b1 1310 cpp_error (pfile, DL_ERROR,
ebef4e8c 1311 cl_options[opt_index].msg, argv[i - 1]);
e23c0ba3
ZW
1312 return argc;
1313 }
1314 }
1315 }
ae79697b 1316
e23c0ba3
ZW
1317 switch (opt_code)
1318 {
4a58aab6 1319 case N_OPTS: /* Shut GCC up. */
e23c0ba3 1320 break;
f4cdc368 1321
e23c0ba3 1322 case OPT_D:
f8f769ea 1323 new_pending_directive (pend, arg, cpp_define);
e23c0ba3 1324 break;
e23c0ba3 1325 case OPT_remap:
ae79697b 1326 CPP_OPTION (pfile, remap) = 1;
e23c0ba3
ZW
1327 break;
1328 case OPT_iprefix:
ae79697b
ZW
1329 CPP_OPTION (pfile, include_prefix) = arg;
1330 CPP_OPTION (pfile, include_prefix_len) = strlen (arg);
e23c0ba3
ZW
1331 break;
1332 case OPT_lang_c:
dd07b884 1333 set_lang (pfile, CLK_GNUC89);
e23c0ba3 1334 break;
e23c0ba3 1335 case OPT_lang_cplusplus:
dd07b884 1336 set_lang (pfile, CLK_GNUCXX);
e23c0ba3 1337 break;
f8f769ea 1338 case OPT_lang_objc:
bdcae02b 1339 CPP_OPTION (pfile, objc) = 1;
e23c0ba3 1340 break;
dd07b884
NB
1341 case OPT_lang_asm:
1342 set_lang (pfile, CLK_ASM);
e23c0ba3 1343 break;
dd07b884
NB
1344 case OPT_std_cplusplus98:
1345 set_lang (pfile, CLK_CXX98);
e23c0ba3
ZW
1346 break;
1347 case OPT_std_gnu89:
dd07b884 1348 set_lang (pfile, CLK_GNUC89);
e23c0ba3
ZW
1349 break;
1350 case OPT_std_gnu9x:
1351 case OPT_std_gnu99:
dd07b884 1352 set_lang (pfile, CLK_GNUC99);
e23c0ba3
ZW
1353 break;
1354 case OPT_std_iso9899_199409:
dd07b884
NB
1355 set_lang (pfile, CLK_STDC94);
1356 break;
e23c0ba3
ZW
1357 case OPT_std_iso9899_1990:
1358 case OPT_std_c89:
9b55f29a 1359 case OPT_lang_c89:
dd07b884 1360 set_lang (pfile, CLK_STDC89);
e23c0ba3
ZW
1361 break;
1362 case OPT_std_iso9899_199x:
1363 case OPT_std_iso9899_1999:
1364 case OPT_std_c9x:
1365 case OPT_std_c99:
dd07b884
NB
1366 set_lang (pfile, CLK_STDC99);
1367 break;
e23c0ba3 1368 case OPT_o:
373e2177
NB
1369 if (CPP_OPTION (pfile, out_fname) == NULL)
1370 CPP_OPTION (pfile, out_fname) = arg;
1371 else
0b22d65c 1372 {
bdee42b1 1373 cpp_error (pfile, DL_ERROR, "output filename specified twice");
e23c0ba3
ZW
1374 return argc;
1375 }
e23c0ba3 1376 break;
96302433 1377
e23c0ba3 1378 case OPT_MG:
ae79697b 1379 CPP_OPTION (pfile, print_deps_missing_files) = 1;
e23c0ba3
ZW
1380 break;
1381 case OPT_M:
3ddbb8a9
NB
1382 /* When doing dependencies with -M or -MM, suppress normal
1383 preprocessed output, but still do -dM etc. as software
1384 depends on this. Preprocessed output occurs if -MD, -MMD
1385 or environment var dependency generation is used. */
96302433 1386 CPP_OPTION (pfile, print_deps) = 2;
3ddbb8a9 1387 CPP_OPTION (pfile, no_output) = 1;
ba57a9c0 1388 CPP_OPTION (pfile, inhibit_warnings) = 1;
96302433 1389 break;
e23c0ba3 1390 case OPT_MM:
96302433 1391 CPP_OPTION (pfile, print_deps) = 1;
3ddbb8a9 1392 CPP_OPTION (pfile, no_output) = 1;
ba57a9c0 1393 CPP_OPTION (pfile, inhibit_warnings) = 1;
e23c0ba3 1394 break;
96302433
NB
1395 case OPT_MF:
1396 CPP_OPTION (pfile, deps_file) = arg;
1397 break;
df383483 1398 case OPT_MP:
a5a4ce3c
NB
1399 CPP_OPTION (pfile, deps_phony_targets) = 1;
1400 break;
f7114e17 1401 case OPT_MQ:
03b9ab42 1402 case OPT_MT:
f7114e17
NB
1403 /* Add a target. -MQ quotes for Make. */
1404 deps_add_target (pfile->deps, arg, opt_code == OPT_MQ);
03b9ab42
NB
1405 break;
1406
b3124fac
NB
1407 case OPT_MD:
1408 CPP_OPTION (pfile, print_deps) = 2;
1409 CPP_OPTION (pfile, deps_file) = arg;
1410 break;
1411 case OPT_MMD:
1412 CPP_OPTION (pfile, print_deps) = 1;
1413 CPP_OPTION (pfile, deps_file) = arg;
1414 break;
1415
e23c0ba3 1416 case OPT_A:
e1e97c4f 1417 if (arg[0] == '-')
0b22d65c 1418 {
e1e97c4f
NB
1419 /* -A with an argument beginning with '-' acts as
1420 #unassert on whatever immediately follows the '-'.
1421 If "-" is the whole argument, we eliminate all
1422 predefined macros and assertions, including those
1423 that were specified earlier on the command line.
1424 That way we can get rid of any that were passed
1425 automatically in from GCC. */
1426
1427 if (arg[1] == '\0')
0b22d65c 1428 {
29401c30 1429 free_chain (pend->directive_head);
e33f6253
NB
1430 pend->directive_head = NULL;
1431 pend->directive_tail = NULL;
0b22d65c 1432 }
e1e97c4f 1433 else
e33f6253 1434 new_pending_directive (pend, arg + 1, cpp_unassert);
6de1e2a9 1435 }
e1e97c4f 1436 else
e33f6253 1437 new_pending_directive (pend, arg, cpp_assert);
e23c0ba3
ZW
1438 break;
1439 case OPT_U:
e33f6253 1440 new_pending_directive (pend, arg, cpp_undef);
e23c0ba3
ZW
1441 break;
1442 case OPT_I: /* Add directory to path for includes. */
1443 if (!strcmp (arg, "-"))
df383483 1444 {
e23c0ba3
ZW
1445 /* -I- means:
1446 Use the preceding -I directories for #include "..."
1447 but not #include <...>.
1448 Don't search the directory of the present file
1449 for #include "...". (Note that -I. -I- is not the same as
1450 the default setup; -I. uses the compiler's working dir.) */
ae79697b 1451 if (! CPP_OPTION (pfile, ignore_srcdir))
e23c0ba3 1452 {
ae79697b
ZW
1453 pend->quote_head = pend->brack_head;
1454 pend->quote_tail = pend->brack_tail;
1455 pend->brack_head = 0;
1456 pend->brack_tail = 0;
1457 CPP_OPTION (pfile, ignore_srcdir) = 1;
e23c0ba3
ZW
1458 }
1459 else
1460 {
bdee42b1 1461 cpp_error (pfile, DL_ERROR, "-I- specified twice");
e23c0ba3
ZW
1462 return argc;
1463 }
df383483
KH
1464 }
1465 else
e33f6253 1466 append_include_chain (pfile, xstrdup (arg), BRACKET, 0);
e23c0ba3
ZW
1467 break;
1468 case OPT_isystem:
1469 /* Add directory to beginning of system include path, as a system
4a58aab6 1470 include directory. */
e33f6253 1471 append_include_chain (pfile, xstrdup (arg), SYSTEM, 0);
e23c0ba3
ZW
1472 break;
1473 case OPT_include:
e23c0ba3
ZW
1474 case OPT_imacros:
1475 {
1476 struct pending_option *o = (struct pending_option *)
1477 xmalloc (sizeof (struct pending_option));
1478 o->arg = arg;
1479 o->next = NULL;
ae79697b 1480
d7bc7a98
NB
1481 if (opt_code == OPT_include)
1482 APPEND (pend, include, o);
1483 else
1484 APPEND (pend, imacros, o);
e23c0ba3
ZW
1485 }
1486 break;
1487 case OPT_iwithprefix:
1488 /* Add directory to end of path for includes,
1489 with the default prefix at the front of its name. */
1490 /* fall through */
1491 case OPT_iwithprefixbefore:
1492 /* Add directory to main path for includes,
1493 with the default prefix at the front of its name. */
1494 {
1495 char *fname;
1496 int len;
ae79697b 1497
e23c0ba3 1498 len = strlen (arg);
ae79697b
ZW
1499
1500 if (CPP_OPTION (pfile, include_prefix) != 0)
e23c0ba3 1501 {
ae79697b
ZW
1502 size_t ipl = CPP_OPTION (pfile, include_prefix_len);
1503 fname = xmalloc (ipl + len + 1);
1504 memcpy (fname, CPP_OPTION (pfile, include_prefix), ipl);
1505 memcpy (fname + ipl, arg, len + 1);
e23c0ba3 1506 }
60893f43 1507 else if (cpp_GCC_INCLUDE_DIR_len)
e23c0ba3 1508 {
60893f43
ZW
1509 fname = xmalloc (cpp_GCC_INCLUDE_DIR_len + len + 1);
1510 memcpy (fname, cpp_GCC_INCLUDE_DIR, cpp_GCC_INCLUDE_DIR_len);
1511 memcpy (fname + cpp_GCC_INCLUDE_DIR_len, arg, len + 1);
e23c0ba3 1512 }
60893f43
ZW
1513 else
1514 fname = xstrdup (arg);
ae79697b 1515
e33f6253 1516 append_include_chain (pfile, fname,
e23c0ba3
ZW
1517 opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0);
1518 }
1519 break;
1520 case OPT_idirafter:
1521 /* Add directory to end of path for includes. */
e33f6253 1522 append_include_chain (pfile, xstrdup (arg), AFTER, 0);
e23c0ba3 1523 break;
df383483 1524 }
e23c0ba3 1525 }
6de1e2a9 1526 return i + 1;
e23c0ba3 1527}
0b22d65c 1528
6de1e2a9
ZW
1529/* Handle command-line options in (argc, argv).
1530 Can be called multiple times, to handle multiple sets of options.
1531 Returns if an unrecognized option is seen.
1532 Returns number of strings consumed. */
6de1e2a9
ZW
1533int
1534cpp_handle_options (pfile, argc, argv)
1535 cpp_reader *pfile;
1536 int argc;
1537 char **argv;
1538{
1539 int i;
1540 int strings_processed;
e23c0ba3 1541
6de1e2a9
ZW
1542 for (i = 0; i < argc; i += strings_processed)
1543 {
09e77dee 1544 strings_processed = cpp_handle_option (pfile, argc - i, argv + i);
6de1e2a9
ZW
1545 if (strings_processed == 0)
1546 break;
1547 }
96302433 1548
6de1e2a9
ZW
1549 return i;
1550}
1551
96302433
NB
1552/* Extra processing when all options are parsed, after all calls to
1553 cpp_handle_option[s]. Consistency checks etc. */
1554void
1555cpp_post_options (pfile)
1556 cpp_reader *pfile;
1557{
373e2177
NB
1558 /* Canonicalize in_fname and out_fname. We guarantee they are not
1559 NULL, and that the empty string represents stdin / stdout. */
1560 if (CPP_OPTION (pfile, in_fname) == NULL
1561 || !strcmp (CPP_OPTION (pfile, in_fname), "-"))
1562 CPP_OPTION (pfile, in_fname) = "";
1563
1564 if (CPP_OPTION (pfile, out_fname) == NULL
1565 || !strcmp (CPP_OPTION (pfile, out_fname), "-"))
1566 CPP_OPTION (pfile, out_fname) = "";
1567
96302433
NB
1568 /* -Wtraditional is not useful in C++ mode. */
1569 if (CPP_OPTION (pfile, cplusplus))
1570 CPP_OPTION (pfile, warn_traditional) = 0;
1571
ceeedfc1
NB
1572 /* The compiler front ends override this, but I think this is the
1573 appropriate setting for the library. */
56da7207
ZW
1574 CPP_OPTION (pfile, warn_long_long)
1575 = ((CPP_OPTION (pfile, pedantic) && !CPP_OPTION (pfile, c99))
1576 || CPP_OPTION (pfile, warn_traditional));
cd7ab83f 1577
6d4587f7 1578 /* Permanently disable macro expansion if we are rescanning
43612ffb 1579 preprocessed text. Read preprocesed source in ISO mode. */
6d4587f7 1580 if (CPP_OPTION (pfile, preprocessed))
43612ffb
NB
1581 {
1582 pfile->state.prevent_expansion = 1;
1583 CPP_OPTION (pfile, traditional) = 0;
1584 }
1585
1586 /* Traditional CPP does not accurately track column information. */
1587 if (CPP_OPTION (pfile, traditional))
1588 CPP_OPTION (pfile, show_column) = 0;
6d4587f7 1589
ba57a9c0
NB
1590 /* -dM and dependencies suppress normal output; do it here so that
1591 the last -d[MDN] switch overrides earlier ones. */
1651cc96
JJ
1592 if (CPP_OPTION (pfile, dump_macros) == dump_only)
1593 CPP_OPTION (pfile, no_output) = 1;
1594
ba57a9c0
NB
1595 /* Disable -dD, -dN and -dI if normal output is suppressed. Allow
1596 -dM since at least glibc relies on -M -dM to work. */
1651cc96
JJ
1597 if (CPP_OPTION (pfile, no_output))
1598 {
1599 if (CPP_OPTION (pfile, dump_macros) != dump_only)
1600 CPP_OPTION (pfile, dump_macros) = dump_none;
1601 CPP_OPTION (pfile, dump_includes) = 0;
1602 }
1603
ba57a9c0
NB
1604 /* Intialize, and check environment variables for, dependency
1605 output. */
96302433
NB
1606 init_dependency_output (pfile);
1607
ba57a9c0
NB
1608 /* If we're not outputting dependencies, complain if other -M
1609 options have been given. */
1610 if (!CPP_OPTION (pfile, print_deps)
1611 && (CPP_OPTION (pfile, print_deps_missing_files)
1612 || CPP_OPTION (pfile, deps_file)
1613 || CPP_OPTION (pfile, deps_phony_targets)))
bdee42b1 1614 cpp_error (pfile, DL_ERROR,
ebef4e8c 1615 "you must additionally specify either -M or -MM");
96302433
NB
1616}
1617
56cd5b95
NB
1618/* Set up dependency-file output. On exit, if print_deps is non-zero
1619 then deps_file is not NULL; stdout is the empty string. */
7ca3d2b1
NB
1620static void
1621init_dependency_output (pfile)
1622 cpp_reader *pfile;
1623{
1624 char *spec, *s, *output_file;
1625
1626 /* Either of two environment variables can specify output of deps.
1627 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1628 where OUTPUT_FILE is the file to write deps info to
1629 and DEPS_TARGET is the target to mention in the deps. */
1630
1631 if (CPP_OPTION (pfile, print_deps) == 0)
1632 {
1633 spec = getenv ("DEPENDENCIES_OUTPUT");
1634 if (spec)
1635 CPP_OPTION (pfile, print_deps) = 1;
1636 else
1637 {
1638 spec = getenv ("SUNPRO_DEPENDENCIES");
1639 if (spec)
1640 CPP_OPTION (pfile, print_deps) = 2;
1641 else
1642 return;
1643 }
1644
1645 /* Find the space before the DEPS_TARGET, if there is one. */
1646 s = strchr (spec, ' ');
1647 if (s)
1648 {
1649 /* Let the caller perform MAKE quoting. */
1650 deps_add_target (pfile->deps, s + 1, 0);
1651 output_file = (char *) xmalloc (s - spec + 1);
1652 memcpy (output_file, spec, s - spec);
1653 output_file[s - spec] = 0;
1654 }
1655 else
1656 output_file = spec;
1657
ab8e2228
NB
1658 /* Command line -MF overrides environment variables and default. */
1659 if (CPP_OPTION (pfile, deps_file) == 0)
1660 CPP_OPTION (pfile, deps_file) = output_file;
1661
7ca3d2b1
NB
1662 CPP_OPTION (pfile, print_deps_append) = 1;
1663 }
ab8e2228 1664 else if (CPP_OPTION (pfile, deps_file) == 0)
32810ba3
NB
1665 /* If -M or -MM was seen without -MF, default output to wherever
1666 was specified with -o. out_fname is non-NULL here. */
ab8e2228 1667 CPP_OPTION (pfile, deps_file) = CPP_OPTION (pfile, out_fname);
7ca3d2b1 1668}
This page took 0.923511 seconds and 5 git commands to generate.