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