* arm.c (arm_split_constant): Don't try to force a constant to
[official-gcc.git] / gcc / cppinit.c
blob551d922a06eb1fef0c20d42ab62cfde9c23efbbc
1 /* CPP Library.
2 Copyright (C) 1986, 87, 89, 92-98, 1999 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994-95.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include "config.h"
22 #include "system.h"
24 #define FAKE_CONST
25 #include "cpplib.h"
26 #include "cpphash.h"
27 #include "output.h"
28 #include "prefix.h"
29 #include "intl.h"
31 /* XXX Should be in a header file. */
32 extern char *version_string;
34 /* Predefined symbols, built-in macros, and the default include path. */
36 #ifndef GET_ENV_PATH_LIST
37 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
38 #endif
40 /* By default, colon separates directories in a path. */
41 #ifndef PATH_SEPARATOR
42 #define PATH_SEPARATOR ':'
43 #endif
45 #ifndef STANDARD_INCLUDE_DIR
46 #define STANDARD_INCLUDE_DIR "/usr/include"
47 #endif
49 /* Symbols to predefine. */
51 #ifdef CPP_PREDEFINES
52 static char *predefs = CPP_PREDEFINES;
53 #else
54 static char *predefs = "";
55 #endif
57 /* We let tm.h override the types used here, to handle trivial differences
58 such as the choice of unsigned int or long unsigned int for size_t.
59 When machines start needing nontrivial differences in the size type,
60 it would be best to do something here to figure out automatically
61 from other information what type to use. */
63 /* The string value for __SIZE_TYPE__. */
65 #ifndef SIZE_TYPE
66 #define SIZE_TYPE "long unsigned int"
67 #endif
69 /* The string value for __PTRDIFF_TYPE__. */
71 #ifndef PTRDIFF_TYPE
72 #define PTRDIFF_TYPE "long int"
73 #endif
75 /* The string value for __WCHAR_TYPE__. */
77 #ifndef WCHAR_TYPE
78 #define WCHAR_TYPE "int"
79 #endif
80 #define CPP_WCHAR_TYPE(PFILE) \
81 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
83 /* The string value for __USER_LABEL_PREFIX__ */
85 #ifndef USER_LABEL_PREFIX
86 #define USER_LABEL_PREFIX ""
87 #endif
89 /* The string value for __REGISTER_PREFIX__ */
91 #ifndef REGISTER_PREFIX
92 #define REGISTER_PREFIX ""
93 #endif
95 /* #include "file" looks in source file dir, then stack. */
96 /* #include <file> just looks in the stack. */
97 /* -I directories are added to the end, then the defaults are added. */
98 /* The */
99 static struct default_include {
100 char *fname; /* The name of the directory. */
101 char *component; /* The component containing the directory */
102 int cplusplus; /* Only look here if we're compiling C++. */
103 int cxx_aware; /* Includes in this directory don't need to
104 be wrapped in extern "C" when compiling
105 C++. */
106 } include_defaults_array[]
107 #ifdef INCLUDE_DEFAULTS
108 = INCLUDE_DEFAULTS;
109 #else
111 /* Pick up GNU C++ specific include files. */
112 { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1 },
113 #ifdef CROSS_COMPILE
114 /* This is the dir for fixincludes. Put it just before
115 the files that we fix. */
116 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
117 /* For cross-compilation, this dir name is generated
118 automatically in Makefile.in. */
119 { CROSS_INCLUDE_DIR, "GCC",0, 0 },
120 #ifdef TOOL_INCLUDE_DIR
121 /* This is another place that the target system's headers might be. */
122 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
123 #endif
124 #else /* not CROSS_COMPILE */
125 #ifdef LOCAL_INCLUDE_DIR
126 /* This should be /usr/local/include and should come before
127 the fixincludes-fixed header files. */
128 { LOCAL_INCLUDE_DIR, 0, 0, 1 },
129 #endif
130 #ifdef TOOL_INCLUDE_DIR
131 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
132 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
133 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
134 #endif
135 /* This is the dir for fixincludes. Put it just before
136 the files that we fix. */
137 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
138 /* Some systems have an extra dir of include files. */
139 #ifdef SYSTEM_INCLUDE_DIR
140 { SYSTEM_INCLUDE_DIR, 0, 0, 0 },
141 #endif
142 #ifndef STANDARD_INCLUDE_COMPONENT
143 #define STANDARD_INCLUDE_COMPONENT 0
144 #endif
145 { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0 },
146 #endif /* not CROSS_COMPILE */
147 { 0, 0, 0, 0 }
149 #endif /* no INCLUDE_DEFAULTS */
151 /* Internal structures and prototypes. */
153 struct cpp_pending
155 struct cpp_pending *next;
156 char *cmd;
157 char *arg;
159 static struct cpp_pending *nreverse_pending PARAMS ((struct cpp_pending *));
161 static void initialize_char_syntax PARAMS ((int));
162 static void print_help PARAMS ((void));
163 static void path_include PARAMS ((cpp_reader *, char *));
164 static void initialize_builtins PARAMS ((cpp_reader *));
167 /* If gcc is in use (stage2/stage3) we can make these tables initialized
168 data. */
169 #if defined __GNUC__ && __GNUC__ >= 2
170 /* Table to tell if a character is legal as the second or later character
171 of a C identifier. */
172 U_CHAR is_idchar[256] =
174 ['a'] = 1, ['b'] = 1, ['c'] = 1, ['d'] = 1, ['e'] = 1, ['f'] = 1,
175 ['g'] = 1, ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, ['l'] = 1,
176 ['m'] = 1, ['n'] = 1, ['o'] = 1, ['p'] = 1, ['q'] = 1, ['r'] = 1,
177 ['s'] = 1, ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, ['x'] = 1,
178 ['y'] = 1, ['z'] = 1,
180 ['A'] = 1, ['B'] = 1, ['C'] = 1, ['D'] = 1, ['E'] = 1, ['F'] = 1,
181 ['G'] = 1, ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, ['L'] = 1,
182 ['M'] = 1, ['N'] = 1, ['O'] = 1, ['P'] = 1, ['Q'] = 1, ['R'] = 1,
183 ['S'] = 1, ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, ['X'] = 1,
184 ['Y'] = 1, ['Z'] = 1,
186 ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, ['5'] = 1, ['6'] = 1,
187 ['7'] = 1, ['8'] = 1, ['9'] = 1, ['0'] = 1,
189 ['_'] = 1,
192 /* Table to tell if a character is legal as the first character of
193 a C identifier. */
194 U_CHAR is_idstart[256] =
196 ['a'] = 1, ['b'] = 1, ['c'] = 1, ['d'] = 1, ['e'] = 1, ['f'] = 1,
197 ['g'] = 1, ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, ['l'] = 1,
198 ['m'] = 1, ['n'] = 1, ['o'] = 1, ['p'] = 1, ['q'] = 1, ['r'] = 1,
199 ['s'] = 1, ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, ['x'] = 1,
200 ['y'] = 1, ['z'] = 1,
202 ['A'] = 1, ['B'] = 1, ['C'] = 1, ['D'] = 1, ['E'] = 1, ['F'] = 1,
203 ['G'] = 1, ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, ['L'] = 1,
204 ['M'] = 1, ['N'] = 1, ['O'] = 1, ['P'] = 1, ['Q'] = 1, ['R'] = 1,
205 ['S'] = 1, ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, ['X'] = 1,
206 ['Y'] = 1, ['Z'] = 1,
208 ['_'] = 1,
211 /* Table to tell if a character is horizontal space. */
212 U_CHAR is_hor_space[256] =
214 [' '] = 1, ['\t'] = 1, ['\v'] = 1, ['\f'] = 1, ['\r'] = 1
216 /* table to tell if a character is horizontal or vertical space. */
217 U_CHAR is_space[256] =
219 [' '] = 1, ['\t'] = 1, ['\v'] = 1, ['\f'] = 1, ['\r'] = 1, ['\n'] = 1,
221 /* Table to handle trigraph conversion, which occurs before all other
222 processing, everywhere in the file. (This is necessary since one
223 of the trigraphs encodes backslash.) Note it's off by default.
225 from to from to from to
226 ?? = # ?? ) ] ?? ! |
227 ?? ( [ ?? ' ^ ?? > }
228 ?? / \ ?? < { ?? - ~
230 There is not a space between the ?? and the third char. I put spaces
231 there to avoid warnings when compiling this file. */
232 U_CHAR trigraph_table[256] =
234 ['='] = '#', [')'] = ']', ['!'] = '|',
235 ['('] = '[', ['\''] = '^', ['>'] = '}',
236 ['/'] = '\\', ['<'] = '{', ['-'] = '~',
239 /* This function will be entirely removed soon. */
240 static inline void
241 initialize_char_syntax (dollar_in_ident)
242 int dollar_in_ident;
244 is_idchar['$'] = dollar_in_ident;
245 is_idstart['$'] = dollar_in_ident;
248 #else /* Not GCC. */
250 U_CHAR is_idchar[256] = { 0 };
251 U_CHAR is_idstart[256] = { 0 };
252 U_CHAR is_hor_space[256] = { 0 };
253 U_CHAR is_space[256] = { 0 };
254 U_CHAR trigraph_table[256] = { 0 };
256 /* Initialize syntactic classifications of characters. */
257 static void
258 initialize_char_syntax (dollar_in_ident)
259 int dollar_in_ident;
261 is_idstart['a'] = 1; is_idstart['b'] = 1; is_idstart['c'] = 1;
262 is_idstart['d'] = 1; is_idstart['e'] = 1; is_idstart['f'] = 1;
263 is_idstart['g'] = 1; is_idstart['h'] = 1; is_idstart['i'] = 1;
264 is_idstart['j'] = 1; is_idstart['k'] = 1; is_idstart['l'] = 1;
265 is_idstart['m'] = 1; is_idstart['n'] = 1; is_idstart['o'] = 1;
266 is_idstart['p'] = 1; is_idstart['q'] = 1; is_idstart['r'] = 1;
267 is_idstart['s'] = 1; is_idstart['t'] = 1; is_idstart['u'] = 1;
268 is_idstart['v'] = 1; is_idstart['w'] = 1; is_idstart['x'] = 1;
269 is_idstart['y'] = 1; is_idstart['z'] = 1;
271 is_idstart['A'] = 1; is_idstart['B'] = 1; is_idstart['C'] = 1;
272 is_idstart['D'] = 1; is_idstart['E'] = 1; is_idstart['F'] = 1;
273 is_idstart['G'] = 1; is_idstart['H'] = 1; is_idstart['I'] = 1;
274 is_idstart['J'] = 1; is_idstart['K'] = 1; is_idstart['L'] = 1;
275 is_idstart['M'] = 1; is_idstart['N'] = 1; is_idstart['O'] = 1;
276 is_idstart['P'] = 1; is_idstart['Q'] = 1; is_idstart['R'] = 1;
277 is_idstart['S'] = 1; is_idstart['T'] = 1; is_idstart['U'] = 1;
278 is_idstart['V'] = 1; is_idstart['W'] = 1; is_idstart['X'] = 1;
279 is_idstart['Y'] = 1; is_idstart['Z'] = 1;
281 is_idstart['_'] = 1;
283 is_idchar['a'] = 1; is_idchar['b'] = 1; is_idchar['c'] = 1;
284 is_idchar['d'] = 1; is_idchar['e'] = 1; is_idchar['f'] = 1;
285 is_idchar['g'] = 1; is_idchar['h'] = 1; is_idchar['i'] = 1;
286 is_idchar['j'] = 1; is_idchar['k'] = 1; is_idchar['l'] = 1;
287 is_idchar['m'] = 1; is_idchar['n'] = 1; is_idchar['o'] = 1;
288 is_idchar['p'] = 1; is_idchar['q'] = 1; is_idchar['r'] = 1;
289 is_idchar['s'] = 1; is_idchar['t'] = 1; is_idchar['u'] = 1;
290 is_idchar['v'] = 1; is_idchar['w'] = 1; is_idchar['x'] = 1;
291 is_idchar['y'] = 1; is_idchar['z'] = 1;
293 is_idchar['A'] = 1; is_idchar['B'] = 1; is_idchar['C'] = 1;
294 is_idchar['D'] = 1; is_idchar['E'] = 1; is_idchar['F'] = 1;
295 is_idchar['G'] = 1; is_idchar['H'] = 1; is_idchar['I'] = 1;
296 is_idchar['J'] = 1; is_idchar['K'] = 1; is_idchar['L'] = 1;
297 is_idchar['M'] = 1; is_idchar['N'] = 1; is_idchar['O'] = 1;
298 is_idchar['P'] = 1; is_idchar['Q'] = 1; is_idchar['R'] = 1;
299 is_idchar['S'] = 1; is_idchar['T'] = 1; is_idchar['U'] = 1;
300 is_idchar['V'] = 1; is_idchar['W'] = 1; is_idchar['X'] = 1;
301 is_idchar['Y'] = 1; is_idchar['Z'] = 1;
303 is_idchar['1'] = 1; is_idchar['2'] = 1; is_idchar['3'] = 1;
304 is_idchar['4'] = 1; is_idchar['5'] = 1; is_idchar['6'] = 1;
305 is_idchar['7'] = 1; is_idchar['8'] = 1; is_idchar['9'] = 1;
306 is_idchar['0'] = 1;
308 is_idchar['_'] = 1;
310 is_idchar['$'] = dollar_in_ident;
311 is_idstart['$'] = dollar_in_ident;
313 /* white space tables */
314 is_hor_space[' '] = 1;
315 is_hor_space['\t'] = 1;
316 is_hor_space['\v'] = 1;
317 is_hor_space['\f'] = 1;
318 is_hor_space['\r'] = 1;
320 is_space[' '] = 1;
321 is_space['\t'] = 1;
322 is_space['\v'] = 1;
323 is_space['\f'] = 1;
324 is_space['\n'] = 1;
325 is_space['\r'] = 1;
327 /* trigraph conversion */
328 trigraph_table['='] = '#'; trigraph_table[')'] = ']';
329 trigraph_table['!'] = '|'; trigraph_table['('] = '[';
330 trigraph_table['\''] = '^'; trigraph_table['>'] = '}';
331 trigraph_table['/'] = '\\'; trigraph_table['<'] = '{';
332 trigraph_table['-'] = '~';
335 #endif /* Not GCC. */
337 /* Given a colon-separated list of file names PATH,
338 add all the names to the search path for include files. */
340 static void
341 path_include (pfile, path)
342 cpp_reader *pfile;
343 char *path;
345 char *p;
347 p = path;
349 if (*p)
350 while (1) {
351 char *q = p;
352 char *name;
354 /* Find the end of this name. */
355 while (*q != 0 && *q != PATH_SEPARATOR) q++;
356 if (p == q) {
357 /* An empty name in the path stands for the current directory. */
358 name = (char *) xmalloc (2);
359 name[0] = '.';
360 name[1] = 0;
361 } else {
362 /* Otherwise use the directory that is named. */
363 name = (char *) xmalloc (q - p + 1);
364 bcopy (p, name, q - p);
365 name[q - p] = 0;
368 append_include_chain (pfile,
369 &(CPP_OPTIONS (pfile)->bracket_include), name, 0);
371 /* Advance past this name. */
372 p = q;
373 if (*p == 0)
374 break;
375 /* Skip the colon. */
376 p++;
380 /* Write out a #define command for the special named MACRO_NAME
381 to PFILE's token_buffer. */
383 static void
384 dump_special_to_buffer (pfile, macro_name)
385 cpp_reader *pfile;
386 char *macro_name;
388 static char define_directive[] = "#define ";
389 int macro_name_length = strlen (macro_name);
390 output_line_command (pfile, 0, same_file);
391 CPP_RESERVE (pfile, sizeof(define_directive) + macro_name_length);
392 CPP_PUTS_Q (pfile, define_directive, sizeof(define_directive)-1);
393 CPP_PUTS_Q (pfile, macro_name, macro_name_length);
394 CPP_PUTC_Q (pfile, ' ');
395 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
396 CPP_PUTC (pfile, '\n');
399 /* Pending-list utility routines. Will go away soon. */
400 static struct cpp_pending *
401 nreverse_pending (list)
402 struct cpp_pending *list;
405 register struct cpp_pending *prev = 0, *next, *pend;
406 for (pend = list; pend; pend = next)
408 next = pend->next;
409 pend->next = prev;
410 prev = pend;
412 return prev;
415 static void
416 push_pending (pfile, cmd, arg)
417 cpp_reader *pfile;
418 char *cmd;
419 char *arg;
421 struct cpp_pending *pend
422 = (struct cpp_pending *) xmalloc (sizeof (struct cpp_pending));
423 pend->cmd = cmd;
424 pend->arg = arg;
425 pend->next = CPP_OPTIONS (pfile)->pending;
426 CPP_OPTIONS (pfile)->pending = pend;
430 /* Initialize a cpp_options structure. */
431 void
432 cpp_options_init (opts)
433 cpp_options *opts;
435 bzero ((char *) opts, sizeof *opts);
437 opts->dollars_in_ident = 1;
438 opts->cplusplus_comments = 1;
439 opts->warn_import = 1;
442 /* Initialize a cpp_reader structure. */
443 void
444 cpp_reader_init (pfile)
445 cpp_reader *pfile;
447 bzero ((char *) pfile, sizeof (cpp_reader));
448 #if 0
449 pfile->get_token = cpp_get_token;
450 #endif
452 pfile->token_buffer_size = 200;
453 pfile->token_buffer = (U_CHAR *) xmalloc (pfile->token_buffer_size);
454 CPP_SET_WRITTEN (pfile, 0);
456 pfile->hashtab = (HASHNODE **) xcalloc (HASHSIZE, sizeof (HASHNODE *));
459 /* Free resources used by PFILE.
460 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
461 void
462 cpp_cleanup (pfile)
463 cpp_reader *pfile;
465 int i;
466 while (CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile))
467 cpp_pop_buffer (pfile);
469 if (pfile->token_buffer)
471 free (pfile->token_buffer);
472 pfile->token_buffer = NULL;
475 if (pfile->deps_buffer)
477 free (pfile->deps_buffer);
478 pfile->deps_buffer = NULL;
479 pfile->deps_allocated_size = 0;
482 while (pfile->if_stack)
484 IF_STACK_FRAME *temp = pfile->if_stack;
485 pfile->if_stack = temp->next;
486 free (temp);
489 for (i = ALL_INCLUDE_HASHSIZE; --i >= 0; )
491 struct include_hash *imp = pfile->all_include_files[i];
492 while (imp)
494 struct include_hash *next = imp->next;
495 #if 0
496 /* This gets freed elsewhere - I think. */
497 free (imp->name);
498 #endif
499 free (imp);
500 imp = next;
502 pfile->all_include_files[i] = 0;
505 for (i = HASHSIZE; --i >= 0;)
507 while (pfile->hashtab[i])
508 delete_macro (pfile->hashtab[i]);
510 free (pfile->hashtab);
514 /* Initialize the built-in macros. */
515 static void
516 initialize_builtins (pfile)
517 cpp_reader *pfile;
519 #define NAME(str) (U_CHAR *)str, sizeof str - 1
520 cpp_install (pfile, NAME("__TIME__"), T_TIME, 0, -1);
521 cpp_install (pfile, NAME("__DATE__"), T_DATE, 0, -1);
522 cpp_install (pfile, NAME("__FILE__"), T_FILE, 0, -1);
523 cpp_install (pfile, NAME("__BASE_FILE__"), T_BASE_FILE, 0, -1);
524 cpp_install (pfile, NAME("__LINE__"), T_SPECLINE, 0, -1);
525 cpp_install (pfile, NAME("__INCLUDE_LEVEL__"), T_INCLUDE_LEVEL, 0, -1);
526 cpp_install (pfile, NAME("__VERSION__"), T_VERSION, 0, -1);
527 #ifndef NO_BUILTIN_SIZE_TYPE
528 cpp_install (pfile, NAME("__SIZE_TYPE__"), T_CONST, SIZE_TYPE, -1);
529 #endif
530 #ifndef NO_BUILTIN_PTRDIFF_TYPE
531 cpp_install (pfile, NAME("__PTRDIFF_TYPE__ "), T_CONST, PTRDIFF_TYPE, -1);
532 #endif
533 cpp_install (pfile, NAME("__WCHAR_TYPE__"), T_CONST, WCHAR_TYPE, -1);
534 cpp_install (pfile, NAME("__USER_LABEL_PREFIX__"), T_CONST, user_label_prefix, -1);
535 cpp_install (pfile, NAME("__REGISTER_PREFIX__"), T_CONST, REGISTER_PREFIX, -1);
536 if (!CPP_TRADITIONAL (pfile))
538 cpp_install (pfile, NAME("__STDC__"), T_STDC, 0, -1);
539 #if 0
540 if (CPP_OPTIONS (pfile)->c9x)
541 cpp_install (pfile, NAME("__STDC_VERSION__"),T_CONST, "199909L", -1);
542 else
543 #endif
544 cpp_install (pfile, NAME("__STDC_VERSION__"),T_CONST, "199409L", -1);
546 #undef NAME
548 if (CPP_OPTIONS (pfile)->debug_output)
550 dump_special_to_buffer (pfile, "__BASE_FILE__");
551 dump_special_to_buffer (pfile, "__VERSION__");
552 #ifndef NO_BUILTIN_SIZE_TYPE
553 dump_special_to_buffer (pfile, "__SIZE_TYPE__");
554 #endif
555 #ifndef NO_BUILTIN_PTRDIFF_TYPE
556 dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__");
557 #endif
558 dump_special_to_buffer (pfile, "__WCHAR_TYPE__");
559 dump_special_to_buffer (pfile, "__DATE__");
560 dump_special_to_buffer (pfile, "__TIME__");
561 if (!CPP_TRADITIONAL (pfile))
562 dump_special_to_buffer (pfile, "__STDC__");
566 /* This is called after options have been processed.
567 * Check options for consistency, and setup for processing input
568 * from the file named FNAME. (Use standard input if FNAME==NULL.)
569 * Return 1 on success, 0 on failure.
573 cpp_start_read (pfile, fname)
574 cpp_reader *pfile;
575 char *fname;
577 struct cpp_options *opts = CPP_OPTIONS (pfile);
578 struct cpp_pending *pend;
579 char *p;
580 int f;
581 cpp_buffer *fp;
582 struct include_hash *ih_fake;
584 /* The code looks at the defaults through this pointer, rather than
585 through the constant structure above. This pointer gets changed
586 if an environment variable specifies other defaults. */
587 struct default_include *include_defaults = include_defaults_array;
589 /* Now that we know dollars_in_ident, we can initialize the syntax
590 tables. */
591 initialize_char_syntax (opts->dollars_in_ident);
593 /* Add dirs from CPATH after dirs from -I. */
594 /* There seems to be confusion about what CPATH should do,
595 so for the moment it is not documented. */
596 /* Some people say that CPATH should replace the standard include
597 dirs, but that seems pointless: it comes before them, so it
598 overrides them anyway. */
599 GET_ENV_PATH_LIST (p, "CPATH");
600 if (p != 0 && ! opts->no_standard_includes)
601 path_include (pfile, p);
603 /* Do partial setup of input buffer for the sake of generating
604 early #line directives (when -g is in effect). */
605 fp = cpp_push_buffer (pfile, NULL, 0);
606 if (!fp)
607 return 0;
608 if (opts->in_fname == NULL || *opts->in_fname == 0)
610 opts->in_fname = fname;
611 if (opts->in_fname == NULL)
612 opts->in_fname = "";
614 fp->nominal_fname = fp->fname = opts->in_fname;
615 fp->lineno = 0;
617 /* Install __LINE__, etc. Must follow initialize_char_syntax
618 and option processing. */
619 initialize_builtins (pfile);
621 /* Do standard #defines and assertions
622 that identify system and machine type. */
624 if (!opts->inhibit_predefs) {
625 char *p = (char *) alloca (strlen (predefs) + 1);
626 strcpy (p, predefs);
627 while (*p) {
628 char *q;
629 while (*p == ' ' || *p == '\t')
630 p++;
631 /* Handle -D options. */
632 if (p[0] == '-' && p[1] == 'D') {
633 q = &p[2];
634 while (*p && *p != ' ' && *p != '\t')
635 p++;
636 if (*p != 0)
637 *p++= 0;
638 if (opts->debug_output)
639 output_line_command (pfile, 0, same_file);
640 cpp_define (pfile, q);
641 while (*p == ' ' || *p == '\t')
642 p++;
643 } else if (p[0] == '-' && p[1] == 'A') {
644 /* Handle -A options (assertions). */
645 char *assertion;
646 char *past_name;
647 char *value;
648 char *past_value;
649 char *termination;
650 int save_char;
652 assertion = &p[2];
653 past_name = assertion;
654 /* Locate end of name. */
655 while (*past_name && *past_name != ' '
656 && *past_name != '\t' && *past_name != '(')
657 past_name++;
658 /* Locate `(' at start of value. */
659 value = past_name;
660 while (*value && (*value == ' ' || *value == '\t'))
661 value++;
662 if (*value++ != '(')
663 abort ();
664 while (*value && (*value == ' ' || *value == '\t'))
665 value++;
666 past_value = value;
667 /* Locate end of value. */
668 while (*past_value && *past_value != ' '
669 && *past_value != '\t' && *past_value != ')')
670 past_value++;
671 termination = past_value;
672 while (*termination && (*termination == ' ' || *termination == '\t'))
673 termination++;
674 if (*termination++ != ')')
675 abort ();
676 if (*termination && *termination != ' ' && *termination != '\t')
677 abort ();
678 /* Temporarily null-terminate the value. */
679 save_char = *termination;
680 *termination = '\0';
681 /* Install the assertion. */
682 cpp_assert (pfile, assertion);
683 *termination = (char) save_char;
684 p = termination;
685 while (*p == ' ' || *p == '\t')
686 p++;
687 } else {
688 abort ();
693 /* Now handle the command line options. */
695 /* Do -U's, -D's and -A's in the order they were seen. */
696 /* First reverse the list. */
697 opts->pending = nreverse_pending (opts->pending);
699 for (pend = opts->pending; pend; pend = pend->next)
701 if (pend->cmd != NULL && pend->cmd[0] == '-')
703 switch (pend->cmd[1])
705 case 'U':
706 if (opts->debug_output)
707 output_line_command (pfile, 0, same_file);
708 cpp_undef (pfile, pend->arg);
709 break;
710 case 'D':
711 if (opts->debug_output)
712 output_line_command (pfile, 0, same_file);
713 cpp_define (pfile, pend->arg);
714 break;
715 case 'A':
716 cpp_assert (pfile, pend->arg);
717 break;
722 opts->done_initializing = 1;
724 { /* Read the appropriate environment variable and if it exists
725 replace include_defaults with the listed path. */
726 char *epath = 0;
727 switch ((opts->objc << 1) + opts->cplusplus)
729 case 0:
730 GET_ENV_PATH_LIST (epath, "C_INCLUDE_PATH");
731 break;
732 case 1:
733 GET_ENV_PATH_LIST (epath, "CPLUS_INCLUDE_PATH");
734 break;
735 case 2:
736 GET_ENV_PATH_LIST (epath, "OBJC_INCLUDE_PATH");
737 break;
738 case 3:
739 GET_ENV_PATH_LIST (epath, "OBJCPLUS_INCLUDE_PATH");
740 break;
742 /* If the environment var for this language is set,
743 add to the default list of include directories. */
744 if (epath) {
745 char *nstore = (char *) alloca (strlen (epath) + 2);
746 int num_dirs;
747 char *startp, *endp;
749 for (num_dirs = 1, startp = epath; *startp; startp++)
750 if (*startp == PATH_SEPARATOR)
751 num_dirs++;
752 include_defaults
753 = (struct default_include *) xmalloc ((num_dirs
754 * sizeof (struct default_include))
755 + sizeof (include_defaults_array));
756 startp = endp = epath;
757 num_dirs = 0;
758 while (1) {
759 /* Handle cases like c:/usr/lib:d:/gcc/lib */
760 if ((*endp == PATH_SEPARATOR)
761 || *endp == 0) {
762 strncpy (nstore, startp, endp-startp);
763 if (endp == startp)
764 strcpy (nstore, ".");
765 else
766 nstore[endp-startp] = '\0';
768 include_defaults[num_dirs].fname = xstrdup (nstore);
769 include_defaults[num_dirs].component = 0;
770 include_defaults[num_dirs].cplusplus = opts->cplusplus;
771 include_defaults[num_dirs].cxx_aware = 1;
772 num_dirs++;
773 if (*endp == '\0')
774 break;
775 endp = startp = endp + 1;
776 } else
777 endp++;
779 /* Put the usual defaults back in at the end. */
780 bcopy ((char *) include_defaults_array,
781 (char *) &include_defaults[num_dirs],
782 sizeof (include_defaults_array));
786 /* Unless -fnostdinc,
787 tack on the standard include file dirs to the specified list */
788 if (!opts->no_standard_includes) {
789 struct default_include *p = include_defaults;
790 char *specd_prefix = opts->include_prefix;
791 char *default_prefix = xstrdup (GCC_INCLUDE_DIR);
792 int default_len = 0;
793 /* Remove the `include' from /usr/local/lib/gcc.../include. */
794 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
795 default_len = strlen (default_prefix) - 7;
796 default_prefix[default_len] = 0;
798 /* Search "translated" versions of GNU directories.
799 These have /usr/local/lib/gcc... replaced by specd_prefix. */
800 if (specd_prefix != 0 && default_len != 0)
801 for (p = include_defaults; p->fname; p++) {
802 /* Some standard dirs are only for C++. */
803 if (!p->cplusplus
804 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
805 /* Does this dir start with the prefix? */
806 if (!strncmp (p->fname, default_prefix, default_len)) {
807 /* Yes; change prefix and add to search list. */
808 int this_len = strlen (specd_prefix)
809 + strlen (p->fname) - default_len;
810 char *str = (char *) xmalloc (this_len + 1);
811 strcpy (str, specd_prefix);
812 strcat (str, p->fname + default_len);
814 append_include_chain (pfile, &opts->system_include,
815 str, !p->cxx_aware);
819 /* Search ordinary names for GNU include directories. */
820 for (p = include_defaults; p->fname; p++) {
821 /* Some standard dirs are only for C++. */
822 if (!p->cplusplus
823 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
824 const char *str = update_path (p->fname, p->component);
825 append_include_chain (pfile, &opts->system_include,
826 str, !p->cxx_aware);
831 merge_include_chains (opts);
833 /* With -v, print the list of dirs to search. */
834 if (opts->verbose) {
835 struct file_name_list *p;
836 cpp_notice ("#include \"...\" search starts here:\n");
837 for (p = opts->quote_include; p; p = p->next) {
838 if (p == opts->bracket_include)
839 cpp_notice ("#include <...> search starts here:\n");
840 fprintf (stderr, " %s\n", p->name);
842 cpp_notice ("End of search list.\n");
845 /* Copy the entire contents of the main input file into
846 the stacked input buffer previously allocated for it. */
847 if (fname == NULL || *fname == 0) {
848 fname = "";
849 f = 0;
850 } else if ((f = open (fname, O_RDONLY|O_NONBLOCK|O_NOCTTY, 0666)) < 0)
851 cpp_pfatal_with_name (pfile, fname);
853 /* -MG doesn't select the form of output and must be specified with one of
854 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
855 inhibit compilation. */
856 if (opts->print_deps_missing_files
857 && (opts->print_deps == 0 || !opts->no_output))
859 cpp_fatal (pfile, "-MG must be specified with one of -M or -MM");
860 return 0;
863 /* Either of two environment variables can specify output of deps.
864 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
865 where OUTPUT_FILE is the file to write deps info to
866 and DEPS_TARGET is the target to mention in the deps. */
868 if (opts->print_deps == 0
869 && (getenv ("SUNPRO_DEPENDENCIES") != 0
870 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
871 char *spec = getenv ("DEPENDENCIES_OUTPUT");
872 char *s;
873 char *output_file;
875 if (spec == 0)
877 spec = getenv ("SUNPRO_DEPENDENCIES");
878 opts->print_deps = 2;
880 else
881 opts->print_deps = 1;
883 s = spec;
884 /* Find the space before the DEPS_TARGET, if there is one. */
885 /* This should use index. (mrs) */
886 while (*s != 0 && *s != ' ') s++;
887 if (*s != 0)
889 opts->deps_target = s + 1;
890 output_file = (char *) xmalloc (s - spec + 1);
891 bcopy (spec, output_file, s - spec);
892 output_file[s - spec] = 0;
894 else
896 opts->deps_target = 0;
897 output_file = spec;
900 opts->deps_file = output_file;
901 opts->print_deps_append = 1;
904 /* For -M, print the expected object file name
905 as the target of this Make-rule. */
906 if (opts->print_deps)
908 pfile->deps_allocated_size = 200;
909 pfile->deps_buffer = (char *) xmalloc (pfile->deps_allocated_size);
910 pfile->deps_buffer[0] = 0;
911 pfile->deps_size = 0;
912 pfile->deps_column = 0;
914 if (opts->deps_target)
915 deps_output (pfile, opts->deps_target, ':');
916 else if (*opts->in_fname == 0)
917 deps_output (pfile, "-", ':');
918 else
920 char *p, *q, *r;
921 int len, x;
922 static char *known_suffixes[] = { ".c", ".C", ".s", ".S", ".m",
923 ".cc", ".cxx", ".cpp", ".cp",
924 ".c++", 0
927 /* Discard all directory prefixes from filename. */
928 if ((q = rindex (opts->in_fname, '/')) != NULL
929 #ifdef DIR_SEPARATOR
930 && (q = rindex (opts->in_fname, DIR_SEPARATOR)) != NULL
931 #endif
933 ++q;
934 else
935 q = opts->in_fname;
937 /* Copy remainder to mungable area. */
938 p = (char *) alloca (strlen(q) + 8);
939 strcpy (p, q);
941 /* Output P, but remove known suffixes. */
942 len = strlen (p);
943 q = p + len;
944 /* Point to the filename suffix. */
945 r = rindex (p, '.');
946 /* Compare against the known suffixes. */
947 x = 0;
948 while (known_suffixes[x] != 0)
950 if (strncmp (known_suffixes[x], r, q - r) == 0)
952 /* Make q point to the bit we're going to overwrite
953 with an object suffix. */
954 q = r;
955 break;
957 x++;
960 /* Supply our own suffix. */
961 #ifndef VMS
962 strcpy (q, ".o");
963 #else
964 strcpy (q, ".obj");
965 #endif
967 deps_output (pfile, p, ':');
968 deps_output (pfile, opts->in_fname, ' ');
972 /* Must call finclude() on the main input before processing
973 -include switches; otherwise the -included text winds up
974 after the main input. */
975 ih_fake = (struct include_hash *) xmalloc (sizeof (struct include_hash));
976 ih_fake->next = 0;
977 ih_fake->next_this_file = 0;
978 ih_fake->foundhere = ABSOLUTE_PATH; /* well sort of ... */
979 ih_fake->name = fname;
980 ih_fake->control_macro = 0;
981 ih_fake->buf = (char *)-1;
982 ih_fake->limit = 0;
983 if (!finclude (pfile, f, ih_fake))
984 return 0;
985 output_line_command (pfile, 0, same_file);
986 pfile->only_seen_white = 2;
988 /* The -imacros files can be scanned now, but the -include files
989 have to be pushed onto the include stack and processed later,
990 in the main loop calling cpp_get_token. That means the -include
991 files have to be processed in reverse order of the pending list,
992 which means the pending list has to be reversed again, which
993 means the -imacros files have to be done separately and first. */
995 pfile->no_record_file++;
996 opts->no_output++;
997 for (pend = opts->pending; pend; pend = pend->next)
999 if (pend->cmd != NULL)
1001 if (strcmp (pend->cmd, "-imacros") == 0)
1003 int fd = open (pend->arg, O_RDONLY|O_NONBLOCK|O_NOCTTY, 0666);
1004 if (fd < 0)
1006 cpp_perror_with_name (pfile, pend->arg);
1007 return 0;
1009 if (!cpp_push_buffer (pfile, NULL, 0))
1010 return 0;
1012 ih_fake = (struct include_hash *)
1013 xmalloc (sizeof (struct include_hash));
1014 ih_fake->next = 0;
1015 ih_fake->next_this_file = 0;
1016 ih_fake->foundhere = ABSOLUTE_PATH; /* well sort of ... */
1017 ih_fake->name = pend->arg;
1018 ih_fake->control_macro = 0;
1019 ih_fake->buf = (char *)-1;
1020 ih_fake->limit = 0;
1021 if (!finclude (pfile, fd, ih_fake))
1022 cpp_scan_buffer (pfile);
1023 free (ih_fake);
1027 opts->no_output--;
1028 opts->pending = nreverse_pending (opts->pending);
1029 for (pend = opts->pending; pend; pend = pend->next)
1031 if (pend->cmd != NULL)
1033 if (strcmp (pend->cmd, "-include") == 0)
1035 int fd = open (pend->arg, O_RDONLY|O_NONBLOCK|O_NOCTTY, 0666);
1036 if (fd < 0)
1038 cpp_perror_with_name (pfile, pend->arg);
1039 return 0;
1041 if (!cpp_push_buffer (pfile, NULL, 0))
1042 return 0;
1044 ih_fake = (struct include_hash *)
1045 xmalloc (sizeof (struct include_hash));
1046 ih_fake->next = 0;
1047 ih_fake->next_this_file = 0;
1048 ih_fake->foundhere = ABSOLUTE_PATH; /* well sort of ... */
1049 ih_fake->name = pend->arg;
1050 ih_fake->control_macro = 0;
1051 ih_fake->buf = (char *)-1;
1052 ih_fake->limit = 0;
1053 if (finclude (pfile, fd, ih_fake))
1054 output_line_command (pfile, 0, enter_file);
1058 pfile->no_record_file--;
1060 /* Free the pending list. */
1061 for (pend = opts->pending; pend; )
1063 struct cpp_pending *next = pend->next;
1064 free (pend);
1065 pend = next;
1067 opts->pending = NULL;
1069 return 1;
1072 /* This is called at the end of preprocessing. It pops the
1073 last buffer and writes dependency output. It should also
1074 clear macro definitions, such that you could call cpp_start_read
1075 with a new filename to restart processing. */
1076 void
1077 cpp_finish (pfile)
1078 cpp_reader *pfile;
1080 struct cpp_options *opts = CPP_OPTIONS (pfile);
1082 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) != CPP_NULL_BUFFER (pfile))
1083 cpp_fatal (pfile,
1084 "cpplib internal error: buffers still stacked in cpp_finish");
1085 cpp_pop_buffer (pfile);
1087 if (opts->print_deps)
1089 /* Stream on which to print the dependency information. */
1090 FILE *deps_stream;
1092 /* Don't actually write the deps file if compilation has failed. */
1093 if (pfile->errors == 0)
1095 char *deps_mode = opts->print_deps_append ? "a" : "w";
1096 if (opts->deps_file == 0)
1097 deps_stream = stdout;
1098 else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
1099 cpp_pfatal_with_name (pfile, opts->deps_file);
1100 fputs (pfile->deps_buffer, deps_stream);
1101 putc ('\n', deps_stream);
1102 if (opts->deps_file)
1104 if (ferror (deps_stream) || fclose (deps_stream) != 0)
1105 cpp_fatal (pfile, "I/O error on output");
1111 /* Handle one command-line option in (argc, argv).
1112 Can be called multiple times, to handle multiple sets of options.
1113 Returns number of strings consumed. */
1115 cpp_handle_option (pfile, argc, argv)
1116 cpp_reader *pfile;
1117 int argc;
1118 char **argv;
1120 struct cpp_options *opts = CPP_OPTIONS (pfile);
1121 int i = 0;
1123 if (user_label_prefix == NULL)
1124 user_label_prefix = USER_LABEL_PREFIX;
1126 if (argv[i][0] != '-') {
1127 if (opts->out_fname != NULL)
1129 print_help ();
1130 cpp_fatal (pfile, "Too many arguments");
1132 else if (opts->in_fname != NULL)
1133 opts->out_fname = argv[i];
1134 else
1135 opts->in_fname = argv[i];
1136 } else {
1137 switch (argv[i][1]) {
1139 missing_filename:
1140 cpp_fatal (pfile, "Filename missing after `%s' option", argv[i]);
1141 return argc;
1142 missing_dirname:
1143 cpp_fatal (pfile, "Directory name missing after `%s' option", argv[i]);
1144 return argc;
1146 case 'f':
1147 if (!strcmp (argv[i], "-fleading-underscore"))
1148 user_label_prefix = "_";
1149 else if (!strcmp (argv[i], "-fno-leading-underscore"))
1150 user_label_prefix = "";
1151 break;
1153 case 'I': /* Add directory to path for includes. */
1154 if (!strcmp (argv[i] + 2, "-"))
1156 if (! opts->ignore_srcdir)
1158 opts->ignore_srcdir = 1;
1159 /* Don't use any preceding -I directories for #include <...>. */
1160 opts->quote_include = opts->bracket_include;
1161 opts->bracket_include = 0;
1164 else
1166 char *fname;
1167 if (argv[i][2] != 0)
1168 fname = argv[i] + 2;
1169 else if (i + 1 == argc)
1170 goto missing_dirname;
1171 else
1172 fname = argv[++i];
1173 append_include_chain (pfile, &opts->bracket_include, fname, 0);
1175 break;
1177 case 'i':
1178 /* Add directory to beginning of system include path, as a system
1179 include directory. */
1180 if (!strcmp (argv[i], "-isystem"))
1182 if (i + 1 == argc)
1183 goto missing_filename;
1184 append_include_chain (pfile, &opts->system_include, argv[++i], 1);
1186 /* Add directory to end of path for includes,
1187 with the default prefix at the front of its name. */
1188 else if (!strcmp (argv[i], "-iwithprefix"))
1190 char *fname;
1191 if (i + 1 == argc)
1192 goto missing_dirname;
1193 ++i;
1195 if (opts->include_prefix != 0)
1197 fname = xmalloc (strlen (opts->include_prefix)
1198 + strlen (argv[i]) + 1);
1199 strcpy (fname, opts->include_prefix);
1200 strcat (fname, argv[i]);
1202 else
1204 fname = xmalloc (strlen (GCC_INCLUDE_DIR)
1205 + strlen (argv[i]) + 1);
1206 strcpy (fname, GCC_INCLUDE_DIR);
1207 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1208 if (!strcmp (fname + strlen (fname) - 8, "/include"))
1209 fname[strlen (fname) - 7] = 0;
1210 strcat (fname, argv[i]);
1213 append_include_chain (pfile, &opts->system_include, fname, 0);
1215 /* Add directory to main path for includes,
1216 with the default prefix at the front of its name. */
1217 else if (!strcmp (argv[i], "-iwithprefix"))
1219 char *fname;
1220 if (i + 1 == argc)
1221 goto missing_dirname;
1222 ++i;
1224 if (opts->include_prefix != 0)
1226 fname = xmalloc (strlen (opts->include_prefix)
1227 + strlen (argv[i]) + 1);
1228 strcpy (fname, opts->include_prefix);
1229 strcat (fname, argv[i]);
1231 else
1233 fname = xmalloc (strlen (GCC_INCLUDE_DIR)
1234 + strlen (argv[i]) + 1);
1235 strcpy (fname, GCC_INCLUDE_DIR);
1236 /* Remove the `include' from /usr/local/lib/gcc.../include. */
1237 if (!strcmp (fname + strlen (fname) - 8, "/include"))
1238 fname[strlen (fname) - 7] = 0;
1239 strcat (fname, argv[i]);
1242 append_include_chain (pfile, &opts->bracket_include, fname, 0);
1244 /* Add directory to end of path for includes. */
1245 else if (!strcmp (argv[i], "-idirafter"))
1247 if (i + 1 == argc)
1248 goto missing_dirname;
1249 append_include_chain (pfile, &opts->after_include, argv[++i], 0);
1251 else if (!strcmp (argv[i], "-include") || !strcmp (argv[i], "-imacros"))
1253 if (i + 1 == argc)
1254 goto missing_filename;
1255 else
1256 push_pending (pfile, argv[i], argv[i+1]), i++;
1258 else if (!strcmp (argv[i], "-iprefix"))
1260 if (i + 1 == argc)
1261 goto missing_filename;
1262 else
1263 opts->include_prefix = argv[++i];
1265 else if (!strcmp (argv[i], "-ifoutput"))
1266 opts->output_conditionals = 1;
1268 break;
1270 case 'o':
1271 if (opts->out_fname != NULL)
1273 cpp_fatal (pfile, "Output filename specified twice");
1274 return argc;
1276 if (i + 1 == argc)
1277 goto missing_filename;
1278 opts->out_fname = argv[++i];
1279 if (!strcmp (opts->out_fname, "-"))
1280 opts->out_fname = "";
1281 break;
1283 case 'p':
1284 if (!strcmp (argv[i], "-pedantic"))
1285 CPP_PEDANTIC (pfile) = 1;
1286 else if (!strcmp (argv[i], "-pedantic-errors")) {
1287 CPP_PEDANTIC (pfile) = 1;
1288 opts->pedantic_errors = 1;
1290 #if 0
1291 else if (!strcmp (argv[i], "-pcp")) {
1292 char *pcp_fname = argv[++i];
1293 pcp_outfile = ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
1294 ? fopen (pcp_fname, "w")
1295 : fdopen (dup (fileno (stdout)), "w"));
1296 if (pcp_outfile == 0)
1297 cpp_pfatal_with_name (pfile, pcp_fname);
1298 no_precomp = 1;
1300 #endif
1301 break;
1303 case 't':
1304 if (!strcmp (argv[i], "-traditional")) {
1305 opts->traditional = 1;
1306 opts->cplusplus_comments = 0;
1307 } else if (!strcmp (argv[i], "-trigraphs")) {
1308 if (!opts->chill)
1309 opts->trigraphs = 1;
1311 break;
1313 case 'l':
1314 if (! strcmp (argv[i], "-lang-c"))
1315 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
1316 opts->c9x = 1, opts->objc = 0;
1317 if (! strcmp (argv[i], "-lang-c89"))
1318 opts->cplusplus = 0, opts->cplusplus_comments = 0, opts->c89 = 1,
1319 opts->c9x = 0, opts->objc = 0;
1320 if (! strcmp (argv[i], "-lang-c++"))
1321 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
1322 opts->c9x = 0, opts->objc = 0;
1323 if (! strcmp (argv[i], "-lang-objc"))
1324 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
1325 opts->c9x = 0, opts->objc = 1;
1326 if (! strcmp (argv[i], "-lang-objc++"))
1327 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
1328 opts->c9x = 0, opts->objc = 1;
1329 if (! strcmp (argv[i], "-lang-asm"))
1330 opts->lang_asm = 1;
1331 if (! strcmp (argv[i], "-lint"))
1332 opts->for_lint = 1;
1333 if (! strcmp (argv[i], "-lang-chill"))
1334 opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
1335 opts->traditional = 1, opts->trigraphs = 0;
1336 break;
1338 case '+':
1339 opts->cplusplus = 1, opts->cplusplus_comments = 1;
1340 break;
1342 case 's':
1343 if (!strcmp (argv[i], "-std=iso9899:1990")
1344 || !strcmp (argv[i], "-std=iso9899:199409")
1345 || !strcmp (argv[i], "-std=c89")
1346 || !strcmp (argv[i], "-std=gnu89"))
1347 opts->cplusplus = 0, opts->cplusplus_comments = 0,
1348 opts->c89 = 1, opts->c9x = 0, opts->objc = 0;
1349 else if (!strcmp (argv[i], "-std=iso9899:199x")
1350 || !strcmp (argv[i], "-std=c9x")
1351 || !strcmp (argv[i], "-std=gnu9x"))
1352 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
1353 opts->c9x = 1, opts->objc = 0;
1354 break;
1356 case 'w':
1357 opts->inhibit_warnings = 1;
1358 break;
1360 case 'W':
1361 if (!strcmp (argv[i], "-Wtrigraphs"))
1362 opts->warn_trigraphs = 1;
1363 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1364 opts->warn_trigraphs = 0;
1365 else if (!strcmp (argv[i], "-Wcomment"))
1366 opts->warn_comments = 1;
1367 else if (!strcmp (argv[i], "-Wno-comment"))
1368 opts->warn_comments = 0;
1369 else if (!strcmp (argv[i], "-Wcomments"))
1370 opts->warn_comments = 1;
1371 else if (!strcmp (argv[i], "-Wno-comments"))
1372 opts->warn_comments = 0;
1373 else if (!strcmp (argv[i], "-Wtraditional"))
1374 opts->warn_stringify = 1;
1375 else if (!strcmp (argv[i], "-Wno-traditional"))
1376 opts->warn_stringify = 0;
1377 else if (!strcmp (argv[i], "-Wundef"))
1378 opts->warn_undef = 1;
1379 else if (!strcmp (argv[i], "-Wno-undef"))
1380 opts->warn_undef = 0;
1381 else if (!strcmp (argv[i], "-Wimport"))
1382 opts->warn_import = 1;
1383 else if (!strcmp (argv[i], "-Wno-import"))
1384 opts->warn_import = 0;
1385 else if (!strcmp (argv[i], "-Werror"))
1386 opts->warnings_are_errors = 1;
1387 else if (!strcmp (argv[i], "-Wno-error"))
1388 opts->warnings_are_errors = 0;
1389 else if (!strcmp (argv[i], "-Wall"))
1391 opts->warn_trigraphs = 1;
1392 opts->warn_comments = 1;
1394 break;
1396 case 'M':
1397 /* The style of the choices here is a bit mixed.
1398 The chosen scheme is a hybrid of keeping all options in one string
1399 and specifying each option in a separate argument:
1400 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
1401 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1402 -M[M][G][D file]. This is awkward to handle in specs, and is not
1403 as extensible. */
1404 /* ??? -MG must be specified in addition to one of -M or -MM.
1405 This can be relaxed in the future without breaking anything.
1406 The converse isn't true. */
1408 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
1409 if (!strcmp (argv[i], "-MG"))
1411 opts->print_deps_missing_files = 1;
1412 break;
1414 if (!strcmp (argv[i], "-M"))
1415 opts->print_deps = 2;
1416 else if (!strcmp (argv[i], "-MM"))
1417 opts->print_deps = 1;
1418 else if (!strcmp (argv[i], "-MD"))
1419 opts->print_deps = 2;
1420 else if (!strcmp (argv[i], "-MMD"))
1421 opts->print_deps = 1;
1422 /* For -MD and -MMD options, write deps on file named by next arg. */
1423 if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD"))
1425 if (i+1 == argc)
1426 goto missing_filename;
1427 opts->deps_file = argv[++i];
1429 else
1431 /* For -M and -MM, write deps on standard output
1432 and suppress the usual output. */
1433 opts->no_output = 1;
1435 break;
1437 case 'd':
1439 char *p = argv[i] + 2;
1440 char c;
1441 while ((c = *p++) != 0) {
1442 /* Arg to -d specifies what parts of macros to dump */
1443 switch (c) {
1444 case 'M':
1445 opts->dump_macros = dump_only;
1446 opts->no_output = 1;
1447 break;
1448 case 'N':
1449 opts->dump_macros = dump_names;
1450 break;
1451 case 'D':
1452 opts->dump_macros = dump_definitions;
1453 break;
1454 case 'I':
1455 opts->dump_includes = 1;
1456 break;
1460 break;
1462 case 'g':
1463 if (argv[i][2] == '3')
1464 opts->debug_output = 1;
1465 break;
1467 case '-':
1468 if (strcmp (argv[i], "--help") != 0)
1469 return i;
1470 print_help ();
1471 break;
1473 case 'v':
1474 cpp_notice ("GNU CPP version %s", version_string);
1475 #ifdef TARGET_VERSION
1476 TARGET_VERSION;
1477 #endif
1478 fprintf (stderr, "\n");
1479 opts->verbose = 1;
1480 break;
1482 case 'H':
1483 opts->print_include_names = 1;
1484 break;
1486 case 'D':
1487 if (argv[i][2] != 0)
1488 push_pending (pfile, "-D", argv[i] + 2);
1489 else if (i + 1 == argc)
1491 cpp_fatal (pfile, "Macro name missing after -D option");
1492 return argc;
1494 else
1495 i++, push_pending (pfile, "-D", argv[i]);
1496 break;
1498 case 'A':
1500 char *p;
1502 if (argv[i][2] != 0)
1503 p = argv[i] + 2;
1504 else if (i + 1 == argc)
1506 cpp_fatal (pfile, "Assertion missing after -A option");
1507 return argc;
1509 else
1510 p = argv[++i];
1512 if (!strcmp (p, "-")) {
1513 struct cpp_pending **ptr;
1514 /* -A- eliminates all predefined macros and assertions.
1515 Let's include also any that were specified earlier
1516 on the command line. That way we can get rid of any
1517 that were passed automatically in from GCC. */
1518 opts->inhibit_predefs = 1;
1519 for (ptr = &opts->pending; *ptr != NULL; )
1521 struct cpp_pending *pend = *ptr;
1522 if (pend->cmd && pend->cmd[0] == '-'
1523 && (pend->cmd[1] == 'D' || pend->cmd[1] == 'A'))
1525 *ptr = pend->next;
1526 free (pend);
1528 else
1529 ptr = &pend->next;
1531 } else {
1532 push_pending (pfile, "-A", p);
1535 break;
1537 case 'U': /* JF #undef something */
1538 if (argv[i][2] != 0)
1539 push_pending (pfile, "-U", argv[i] + 2);
1540 else if (i + 1 == argc)
1542 cpp_fatal (pfile, "Macro name missing after -U option");
1543 return argc;
1545 else
1546 push_pending (pfile, "-U", argv[i+1]), i++;
1547 break;
1549 case 'C':
1550 opts->put_out_comments = 1;
1551 break;
1553 case 'E': /* -E comes from cc -E; ignore it. */
1554 break;
1556 case 'P':
1557 opts->no_line_commands = 1;
1558 break;
1560 case '$': /* Don't include $ in identifiers. */
1561 opts->dollars_in_ident = 0;
1562 break;
1564 case 'n':
1565 if (!strcmp (argv[i], "-nostdinc"))
1566 /* -nostdinc causes no default include directories.
1567 You must specify all include-file directories with -I. */
1568 opts->no_standard_includes = 1;
1569 else if (!strcmp (argv[i], "-nostdinc++"))
1570 /* -nostdinc++ causes no default C++-specific include directories. */
1571 opts->no_standard_cplusplus_includes = 1;
1572 #if 0
1573 else if (!strcmp (argv[i], "-noprecomp"))
1574 no_precomp = 1;
1575 #endif
1576 break;
1578 case 'r':
1579 if (!strcmp (argv[i], "-remap"))
1580 opts->remap = 1;
1581 break;
1583 case 'u':
1584 /* Sun compiler passes undocumented switch "-undef".
1585 Let's assume it means to inhibit the predefined symbols. */
1586 opts->inhibit_predefs = 1;
1587 break;
1589 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
1590 if (opts->in_fname == NULL) {
1591 opts->in_fname = "";
1592 break;
1593 } else if (opts->out_fname == NULL) {
1594 opts->out_fname = "";
1595 break;
1596 } /* else fall through into error */
1598 default:
1599 return i;
1603 return i + 1;
1606 /* Handle command-line options in (argc, argv).
1607 Can be called multiple times, to handle multiple sets of options.
1608 Returns if an unrecognized option is seen.
1609 Returns number of strings consumed. */
1612 cpp_handle_options (pfile, argc, argv)
1613 cpp_reader *pfile;
1614 int argc;
1615 char **argv;
1617 int i;
1618 int strings_processed;
1619 for (i = 0; i < argc; i += strings_processed)
1621 strings_processed = cpp_handle_option (pfile, argc - i, argv + i);
1622 if (strings_processed == 0)
1623 break;
1625 return i;
1628 static void
1629 print_help ()
1631 cpp_notice ("Usage: %s [switches] input output\n", progname);
1632 fputs (_("\
1633 Switches:\n\
1634 -include <file> Include the contents of <file> before other files\n\
1635 -imacros <file> Accept definition of macros in <file>\n\
1636 -iprefix <path> Specify <path> as a prefix for next two options\n\
1637 -iwithprefix <dir> Add <dir> to the end of the system include path\n\
1638 -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\
1639 -isystem <dir> Add <dir> to the start of the system include path\n\
1640 -idirafter <dir> Add <dir> to the end of the system include path\n\
1641 -I <dir> Add <dir> to the end of the main include path\n\
1642 -nostdinc Do not search system include directories\n\
1643 (dirs specified with -isystem will still be used)\n\
1644 -nostdinc++ Do not search system include directories for C++\n\
1645 -o <file> Put output into <file>\n\
1646 -pedantic Issue all warnings demanded by strict ANSI C\n\
1647 -traditional Follow K&R pre-processor behaviour\n\
1648 -trigraphs Support ANSI C trigraphs\n\
1649 -lang-c Assume that the input sources are in C\n\
1650 -lang-c89 Assume that the input sources are in C89\n\
1651 -lang-c++ Assume that the input sources are in C++\n\
1652 -lang-objc Assume that the input sources are in ObjectiveC\n\
1653 -lang-objc++ Assume that the input sources are in ObjectiveC++\n\
1654 -lang-asm Assume that the input sources are in assembler\n\
1655 -lang-chill Assume that the input sources are in Chill\n\
1656 -std=<std name> Specify the conformance standard; one of:\n\
1657 gnu89, gnu9x, c89, c9x, iso9899:1990,\n\
1658 iso9899:199409, iso9899:199x\n\
1659 -+ Allow parsing of C++ style features\n\
1660 -w Inhibit warning messages\n\
1661 -Wtrigraphs Warn if trigraphs are encountered\n\
1662 -Wno-trigraphs Do not warn about trigraphs\n\
1663 -Wcomment{s} Warn if one comment starts inside another\n\
1664 -Wno-comment{s} Do not warn about comments\n\
1665 -Wtraditional Warn if a macro argument is/would be turned into\n\
1666 a string if -traditional is specified\n\
1667 -Wno-traditional Do not warn about stringification\n\
1668 -Wundef Warn if an undefined macro is used by #if\n\
1669 -Wno-undef Do not warn about testing undefined macros\n\
1670 -Wimport Warn about the use of the #import directive\n\
1671 -Wno-import Do not warn about the use of #import\n\
1672 -Werror Treat all warnings as errors\n\
1673 -Wno-error Do not treat warnings as errors\n\
1674 -Wall Enable all preprocessor warnings\n\
1675 -M Generate make dependencies\n\
1676 -MM As -M, but ignore system header files\n\
1677 -MD As -M, but put output in a .d file\n\
1678 -MMD As -MD, but ignore system header files\n\
1679 -MG Treat missing header file as generated files\n\
1680 -g Include #define and #undef directives in the output\n\
1681 -D<macro> Define a <macro> with string '1' as its value\n\
1682 -D<macro>=<val> Define a <macro> with <val> as its value\n\
1683 -A<question> (<answer>) Assert the <answer> to <question>\n\
1684 -U<macro> Undefine <macro> \n\
1685 -u or -undef Do not predefine any macros\n\
1686 -v Display the version number\n\
1687 -H Print the name of header files as they are used\n\
1688 -C Do not discard comments\n\
1689 -dM Display a list of macro definitions active at end\n\
1690 -dD Preserve macro definitions in output\n\
1691 -dN As -dD except that only the names are preserved\n\
1692 -dI Include #include directives in the output\n\
1693 -ifoutput Describe skipped code blocks in output \n\
1694 -P Do not generate #line directives\n\
1695 -$ Do not allow '$' in identifiers\n\
1696 -remap Remap file names when including files.\n\
1697 -h or --help Display this information\n\
1698 "), stdout);