blob: 04129aaecc6e16d48d33ef76e91a45194900fbe5 [file] [log] [blame]
Zack Weinberg5538ada1999-02-04 06:36:54 -05001/* CPP Library.
Jeff Law5e7b4e22000-02-25 22:59:31 -07002 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
Zack Weinberg5538ada1999-02-04 06:36:54 -05004 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
Zack Weinberg5538ada1999-02-04 06:36:54 -050022#include "config.h"
23#include "system.h"
24
Zack Weinberg6de1e2a1999-02-18 15:35:49 +000025#include "cpplib.h"
26#include "cpphash.h"
27#include "output.h"
28#include "prefix.h"
29#include "intl.h"
Kaveh R. Ghazi9f8f4ef2000-02-15 16:36:35 +000030#include "version.h"
Zack Weinberg49e6c082000-03-04 19:42:04 +000031#include "mkdeps.h"
Zack Weinberg6de1e2a1999-02-18 15:35:49 +000032
33/* Predefined symbols, built-in macros, and the default include path. */
34
35#ifndef GET_ENV_PATH_LIST
36#define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
37#endif
38
Zack Weinberg6de1e2a1999-02-18 15:35:49 +000039#ifndef STANDARD_INCLUDE_DIR
40#define STANDARD_INCLUDE_DIR "/usr/include"
41#endif
42
Zack Weinberg6de1e2a1999-02-18 15:35:49 +000043/* We let tm.h override the types used here, to handle trivial differences
44 such as the choice of unsigned int or long unsigned int for size_t.
45 When machines start needing nontrivial differences in the size type,
46 it would be best to do something here to figure out automatically
47 from other information what type to use. */
48
49/* The string value for __SIZE_TYPE__. */
50
51#ifndef SIZE_TYPE
52#define SIZE_TYPE "long unsigned int"
53#endif
54
55/* The string value for __PTRDIFF_TYPE__. */
56
57#ifndef PTRDIFF_TYPE
58#define PTRDIFF_TYPE "long int"
59#endif
60
61/* The string value for __WCHAR_TYPE__. */
62
63#ifndef WCHAR_TYPE
64#define WCHAR_TYPE "int"
65#endif
Zack Weinberg6de1e2a1999-02-18 15:35:49 +000066
67/* The string value for __USER_LABEL_PREFIX__ */
68
69#ifndef USER_LABEL_PREFIX
70#define USER_LABEL_PREFIX ""
71#endif
72
73/* The string value for __REGISTER_PREFIX__ */
74
75#ifndef REGISTER_PREFIX
76#define REGISTER_PREFIX ""
77#endif
78
Zack Weinberg0b22d651999-03-15 18:42:46 +000079/* This is the default list of directories to search for include files.
80 It may be overridden by the various -I and -ixxx options.
81
82 #include "file" looks in the same directory as the current file,
83 then this list.
84 #include <file> just looks in this list.
85
86 All these directories are treated as `system' include directories
87 (they are not subject to pedantic warnings in some cases). */
88
Zack Weinberg455d2582000-03-04 01:42:56 +000089struct default_include
Zack Weinberg0b22d651999-03-15 18:42:46 +000090{
Kaveh R. Ghazibcc5cac1999-09-07 15:41:26 +000091 const char *fname; /* The name of the directory. */
92 const char *component; /* The component containing the directory
Zack Weinberg0b22d651999-03-15 18:42:46 +000093 (see update_path in prefix.c) */
Zack Weinberg6de1e2a1999-02-18 15:35:49 +000094 int cplusplus; /* Only look here if we're compiling C++. */
95 int cxx_aware; /* Includes in this directory don't need to
96 be wrapped in extern "C" when compiling
Zack Weinbergc45da1c2000-03-02 20:14:32 +000097 C++. */
Zack Weinberg455d2582000-03-04 01:42:56 +000098};
99
100static const struct default_include include_defaults_array[]
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000101#ifdef INCLUDE_DEFAULTS
Zack Weinberg0b22d651999-03-15 18:42:46 +0000102= INCLUDE_DEFAULTS;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000103#else
Zack Weinberg0b22d651999-03-15 18:42:46 +0000104= {
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000105 /* Pick up GNU C++ specific include files. */
106 { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1 },
107#ifdef CROSS_COMPILE
108 /* This is the dir for fixincludes. Put it just before
109 the files that we fix. */
110 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
111 /* For cross-compilation, this dir name is generated
112 automatically in Makefile.in. */
Zack Weinberg0b22d651999-03-15 18:42:46 +0000113 { CROSS_INCLUDE_DIR, "GCC", 0, 0 },
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000114#ifdef TOOL_INCLUDE_DIR
115 /* This is another place that the target system's headers might be. */
116 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
117#endif
118#else /* not CROSS_COMPILE */
119#ifdef LOCAL_INCLUDE_DIR
120 /* This should be /usr/local/include and should come before
121 the fixincludes-fixed header files. */
122 { LOCAL_INCLUDE_DIR, 0, 0, 1 },
123#endif
124#ifdef TOOL_INCLUDE_DIR
125 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
126 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
127 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
128#endif
129 /* This is the dir for fixincludes. Put it just before
130 the files that we fix. */
131 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
132 /* Some systems have an extra dir of include files. */
133#ifdef SYSTEM_INCLUDE_DIR
134 { SYSTEM_INCLUDE_DIR, 0, 0, 0 },
135#endif
136#ifndef STANDARD_INCLUDE_COMPONENT
137#define STANDARD_INCLUDE_COMPONENT 0
138#endif
139 { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0 },
140#endif /* not CROSS_COMPILE */
141 { 0, 0, 0, 0 }
Zack Weinberg0b22d651999-03-15 18:42:46 +0000142 };
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000143#endif /* no INCLUDE_DEFAULTS */
144
145/* Internal structures and prototypes. */
146
Zack Weinberg0b22d651999-03-15 18:42:46 +0000147/* A `struct pending_option' remembers one -D, -A, -U, -include, or -imacros
148 switch. There are four lists: one for -D and -U, one for -A, one
149 for -include, one for -imacros. `undef' is set for -U, clear for
150 -D, ignored for the others.
151 (Future: add an equivalent of -U for -A) */
152struct pending_option
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000153{
Zack Weinberg0b22d651999-03-15 18:42:46 +0000154 struct pending_option *next;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000155 char *arg;
Zack Weinberg0b22d651999-03-15 18:42:46 +0000156 int undef;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000157};
Zack Weinberg0b22d651999-03-15 18:42:46 +0000158
159#ifdef __STDC__
160#define APPEND(pend, list, elt) \
161 do { if (!(pend)->list##_head) (pend)->list##_head = (elt); \
162 else (pend)->list##_tail->next = (elt); \
163 (pend)->list##_tail = (elt); \
164 } while (0)
165#else
166#define APPEND(pend, list, elt) \
167 do { if (!(pend)->list/**/_head) (pend)->list/**/_head = (elt); \
168 else (pend)->list/**/_tail->next = (elt); \
169 (pend)->list/**/_tail = (elt); \
170 } while (0)
171#endif
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000172
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000173static void print_help PARAMS ((void));
Zack Weinberg0b22d651999-03-15 18:42:46 +0000174static void path_include PARAMS ((cpp_reader *,
175 struct cpp_pending *,
176 char *, int));
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000177static void initialize_builtins PARAMS ((cpp_reader *));
Zack Weinberg0b22d651999-03-15 18:42:46 +0000178static void append_include_chain PARAMS ((cpp_reader *,
179 struct cpp_pending *,
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000180 char *, int, int));
Kaveh R. Ghazibcc5cac1999-09-07 15:41:26 +0000181static void dump_special_to_buffer PARAMS ((cpp_reader *, const char *));
182static void initialize_dependency_output PARAMS ((cpp_reader *));
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000183static void initialize_standard_includes PARAMS ((cpp_reader *));
Zack Weinberg40ea76d2000-02-06 07:30:25 +0000184static void new_pending_define PARAMS ((struct cpp_options *,
185 const char *));
Zack Weinberge23c0ba2000-03-07 23:11:06 +0000186#ifdef HOST_EBCDIC
187static int opt_comp PARAMS ((const void *, const void *));
188#endif
189static int parse_option PARAMS ((const char *));
Zack Weinbergb0699da2000-03-07 20:58:47 +0000190static int handle_option PARAMS ((cpp_reader *, int, char **));
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000191
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000192/* Fourth argument to append_include_chain: chain to use */
Zack Weinberg0b22d651999-03-15 18:42:46 +0000193enum { QUOTE = 0, BRACKET, SYSTEM, AFTER };
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000194
Zack Weinberg455d2582000-03-04 01:42:56 +0000195/* If we have designated initializers (GCC >2.7, or C99) this table
196 can be initialized, constant data. Otherwise, it has to be filled
197 in at runtime. */
Zack Weinberga9ae4481999-10-29 04:31:14 +0000198
Zack Weinberg455d2582000-03-04 01:42:56 +0000199#if (GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L)
200#define init_IStable() /* nothing */
201#define ISTABLE const unsigned char _cpp_IStable[256] = {
Zack Weinberga9ae4481999-10-29 04:31:14 +0000202#define END };
Zack Weinberg455d2582000-03-04 01:42:56 +0000203#define s(p, v) [p] = v,
Zack Weinberga9ae4481999-10-29 04:31:14 +0000204#else
Zack Weinberg455d2582000-03-04 01:42:56 +0000205#define ISTABLE unsigned char _cpp_IStable[256] = { 0 }; \
206 static void init_IStable PARAMS ((void)) { \
Zack Weinbergc64912102000-03-04 17:53:04 +0000207 unsigned char *x = _cpp_IStable;
Zack Weinberga9ae4481999-10-29 04:31:14 +0000208#define END }
Zack Weinberg455d2582000-03-04 01:42:56 +0000209#define s(p, v) x[p] = v;
Zack Weinberga9ae4481999-10-29 04:31:14 +0000210#endif
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000211
Zack Weinberga9ae4481999-10-29 04:31:14 +0000212#define A(x) s(x, ISidnum|ISidstart)
213#define N(x) s(x, ISidnum|ISnumstart)
214#define H(x) s(x, IShspace|ISspace)
215#define S(x) s(x, ISspace)
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000216
Zack Weinberg455d2582000-03-04 01:42:56 +0000217ISTABLE
Zack Weinberga9ae4481999-10-29 04:31:14 +0000218 A('_')
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000219
Zack Weinberga9ae4481999-10-29 04:31:14 +0000220 A('a') A('b') A('c') A('d') A('e') A('f') A('g') A('h') A('i')
221 A('j') A('k') A('l') A('m') A('n') A('o') A('p') A('q') A('r')
222 A('s') A('t') A('u') A('v') A('w') A('x') A('y') A('z')
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000223
Zack Weinberga9ae4481999-10-29 04:31:14 +0000224 A('A') A('B') A('C') A('D') A('E') A('F') A('G') A('H') A('I')
225 A('J') A('K') A('L') A('M') A('N') A('O') A('P') A('Q') A('R')
226 A('S') A('T') A('U') A('V') A('W') A('X') A('Y') A('Z')
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000227
Zack Weinberga9ae4481999-10-29 04:31:14 +0000228 N('1') N('2') N('3') N('4') N('5') N('6') N('7') N('8') N('9') N('0')
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000229
Zack Weinberga9ae4481999-10-29 04:31:14 +0000230 H(' ') H('\t') H('\v') H('\f')
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000231
Zack Weinberga9ae4481999-10-29 04:31:14 +0000232 S('\n')
233END
Zack Weinberg5538ada1999-02-04 06:36:54 -0500234
Zack Weinberga9ae4481999-10-29 04:31:14 +0000235#undef A
236#undef N
237#undef H
238#undef S
Zack Weinberga9ae4481999-10-29 04:31:14 +0000239#undef s
Zack Weinberg455d2582000-03-04 01:42:56 +0000240#undef ISTABLE
241#undef END
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000242
243/* Given a colon-separated list of file names PATH,
244 add all the names to the search path for include files. */
245
246static void
Zack Weinberg0b22d651999-03-15 18:42:46 +0000247path_include (pfile, pend, list, path)
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000248 cpp_reader *pfile;
Zack Weinberg0b22d651999-03-15 18:42:46 +0000249 struct cpp_pending *pend;
250 char *list;
251 int path;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000252{
Zack Weinberg0b22d651999-03-15 18:42:46 +0000253 char *p, *q, *name;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000254
Zack Weinberg0b22d651999-03-15 18:42:46 +0000255 p = list;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000256
Zack Weinberg0b22d651999-03-15 18:42:46 +0000257 do
258 {
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000259 /* Find the end of this name. */
Zack Weinberg0b22d651999-03-15 18:42:46 +0000260 q = p;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000261 while (*q != 0 && *q != PATH_SEPARATOR) q++;
Zack Weinberg0b22d651999-03-15 18:42:46 +0000262 if (q == p)
263 {
264 /* An empty name in the path stands for the current directory. */
265 name = (char *) xmalloc (2);
266 name[0] = '.';
267 name[1] = 0;
268 }
269 else
270 {
271 /* Otherwise use the directory that is named. */
272 name = (char *) xmalloc (q - p + 1);
273 memcpy (name, p, q - p);
274 name[q - p] = 0;
275 }
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000276
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000277 append_include_chain (pfile, pend, name, path, 0);
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000278
279 /* Advance past this name. */
Zack Weinberg0b22d651999-03-15 18:42:46 +0000280 if (*q == 0)
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000281 break;
Zack Weinberg0b22d651999-03-15 18:42:46 +0000282 p = q + 1;
283 }
284 while (1);
285}
286
Zack Weinberg0b22d651999-03-15 18:42:46 +0000287/* Append DIR to include path PATH. DIR must be permanently allocated
288 and writable. */
289static void
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000290append_include_chain (pfile, pend, dir, path, cxx_aware)
Zack Weinberg0b22d651999-03-15 18:42:46 +0000291 cpp_reader *pfile;
292 struct cpp_pending *pend;
293 char *dir;
294 int path;
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000295 int cxx_aware;
Zack Weinberg0b22d651999-03-15 18:42:46 +0000296{
297 struct file_name_list *new;
298 struct stat st;
299 unsigned int len;
300
Zack Weinbergb0699da2000-03-07 20:58:47 +0000301 _cpp_simplify_pathname (dir);
Zack Weinberg0b22d651999-03-15 18:42:46 +0000302 if (stat (dir, &st))
303 {
304 /* Dirs that don't exist are silently ignored. */
305 if (errno != ENOENT)
Zack Weinbergc1212d22000-02-06 23:46:18 +0000306 cpp_notice_from_errno (pfile, dir);
Zack Weinberg0b22d651999-03-15 18:42:46 +0000307 else if (CPP_OPTIONS (pfile)->verbose)
Zack Weinbergc1212d22000-02-06 23:46:18 +0000308 fprintf (stderr, _("ignoring nonexistent directory `%s'\n"), dir);
Zack Weinberg0b22d651999-03-15 18:42:46 +0000309 return;
310 }
311
312 if (!S_ISDIR (st.st_mode))
313 {
Zack Weinbergc1212d22000-02-06 23:46:18 +0000314 cpp_notice (pfile, "%s: Not a directory", dir);
Zack Weinberg0b22d651999-03-15 18:42:46 +0000315 return;
316 }
317
318 len = strlen (dir);
319 if (len > pfile->max_include_len)
320 pfile->max_include_len = len;
321
322 new = (struct file_name_list *)xmalloc (sizeof (struct file_name_list));
323 new->name = dir;
324 new->nlen = len;
325 new->ino = st.st_ino;
326 new->dev = st.st_dev;
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000327 if (path == SYSTEM)
328 new->sysp = cxx_aware ? 1 : 2;
329 else
330 new->sysp = 0;
Zack Weinberg0b22d651999-03-15 18:42:46 +0000331 new->name_map = NULL;
Dave Brolley503cb431999-09-13 16:58:44 +0000332 new->next = NULL;
333 new->alloc = NULL;
Zack Weinberg0b22d651999-03-15 18:42:46 +0000334
335 switch (path)
336 {
337 case QUOTE: APPEND (pend, quote, new); break;
338 case BRACKET: APPEND (pend, brack, new); break;
339 case SYSTEM: APPEND (pend, systm, new); break;
340 case AFTER: APPEND (pend, after, new); break;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000341 }
342}
343
Zack Weinberg0b22d651999-03-15 18:42:46 +0000344
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000345/* Write out a #define command for the special named MACRO_NAME
346 to PFILE's token_buffer. */
347
348static void
349dump_special_to_buffer (pfile, macro_name)
350 cpp_reader *pfile;
Kaveh R. Ghazibcc5cac1999-09-07 15:41:26 +0000351 const char *macro_name;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000352{
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000353 static const char define_directive[] = "#define ";
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000354 int macro_name_length = strlen (macro_name);
Zack Weinberg80e9dcb1999-04-19 11:55:04 +0000355 output_line_command (pfile, same_file);
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000356 CPP_RESERVE (pfile, sizeof(define_directive) + macro_name_length);
357 CPP_PUTS_Q (pfile, define_directive, sizeof(define_directive)-1);
358 CPP_PUTS_Q (pfile, macro_name, macro_name_length);
359 CPP_PUTC_Q (pfile, ' ');
360 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
361 CPP_PUTC (pfile, '\n');
362}
363
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000364/* Initialize a cpp_options structure. */
365void
366cpp_options_init (opts)
367 cpp_options *opts;
368{
Zack Weinberg0b22d651999-03-15 18:42:46 +0000369 bzero ((char *) opts, sizeof (struct cpp_options));
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000370
371 opts->dollars_in_ident = 1;
372 opts->cplusplus_comments = 1;
373 opts->warn_import = 1;
Zack Weinberg564ad5f2000-02-10 00:26:47 +0000374 opts->discard_comments = 1;
Zack Weinberg0b22d651999-03-15 18:42:46 +0000375
Kaveh R. Ghaziad852161999-09-07 02:36:41 +0000376 opts->pending =
377 (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000378}
379
380/* Initialize a cpp_reader structure. */
381void
382cpp_reader_init (pfile)
383 cpp_reader *pfile;
384{
385 bzero ((char *) pfile, sizeof (cpp_reader));
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000386
387 pfile->token_buffer_size = 200;
388 pfile->token_buffer = (U_CHAR *) xmalloc (pfile->token_buffer_size);
389 CPP_SET_WRITTEN (pfile, 0);
Zack Weinberg122ae891999-02-25 14:24:40 +0000390
391 pfile->hashtab = (HASHNODE **) xcalloc (HASHSIZE, sizeof (HASHNODE *));
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000392}
393
394/* Free resources used by PFILE.
395 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
396void
397cpp_cleanup (pfile)
398 cpp_reader *pfile;
399{
400 int i;
401 while (CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile))
402 cpp_pop_buffer (pfile);
403
404 if (pfile->token_buffer)
405 {
406 free (pfile->token_buffer);
407 pfile->token_buffer = NULL;
408 }
409
Zack Weinberg2c826211999-05-10 11:24:36 -0400410 if (pfile->input_buffer)
411 {
412 free (pfile->input_buffer);
413 free (pfile->input_speccase);
414 pfile->input_buffer = pfile->input_speccase = NULL;
415 pfile->input_buffer_len = 0;
416 }
417
Zack Weinberg49e6c082000-03-04 19:42:04 +0000418 if (pfile->deps)
419 deps_free (pfile->deps);
420
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000421 while (pfile->if_stack)
422 {
423 IF_STACK_FRAME *temp = pfile->if_stack;
424 pfile->if_stack = temp->next;
425 free (temp);
426 }
427
428 for (i = ALL_INCLUDE_HASHSIZE; --i >= 0; )
429 {
430 struct include_hash *imp = pfile->all_include_files[i];
431 while (imp)
432 {
433 struct include_hash *next = imp->next;
Zack Weinberg49e6c082000-03-04 19:42:04 +0000434
435 free ((PTR) imp->name);
Zack Weinbergb0699da2000-03-07 20:58:47 +0000436 free ((PTR) imp->nshort);
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000437 free (imp);
438 imp = next;
439 }
440 pfile->all_include_files[i] = 0;
441 }
442
Zack Weinberg122ae891999-02-25 14:24:40 +0000443 for (i = HASHSIZE; --i >= 0;)
444 {
445 while (pfile->hashtab[i])
Zack Weinbergb0699da2000-03-07 20:58:47 +0000446 _cpp_delete_macro (pfile->hashtab[i]);
Zack Weinberg122ae891999-02-25 14:24:40 +0000447 }
448 free (pfile->hashtab);
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000449}
450
451
Zack Weinberga9ae4481999-10-29 04:31:14 +0000452/* This structure defines one built-in macro. A node of type TYPE will
453 be entered in the macro hash table under the name NAME, with value
454 VALUE (if any). FLAGS tweaks the behavior a little:
455 DUMP write debug info for this macro
456 STDC define only if not -traditional
Zack Weinberga9ae4481999-10-29 04:31:14 +0000457 ULP value is the global user_label_prefix (which can't be
458 put directly into the table).
459 */
460
461struct builtin
462{
463 const char *name;
464 const char *value;
465 unsigned short type;
466 unsigned short flags;
467};
468#define DUMP 0x01
469#define STDC 0x02
Zack Weinberga9ae4481999-10-29 04:31:14 +0000470#define ULP 0x10
471
472static const struct builtin builtin_array[] =
473{
474 { "__TIME__", 0, T_TIME, DUMP },
475 { "__DATE__", 0, T_DATE, DUMP },
476 { "__FILE__", 0, T_FILE, 0 },
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000477 { "__BASE_FILE__", 0, T_BASE_FILE, 0 },
Zack Weinberga9ae4481999-10-29 04:31:14 +0000478 { "__LINE__", 0, T_SPECLINE, 0 },
479 { "__INCLUDE_LEVEL__", 0, T_INCLUDE_LEVEL, 0 },
480 { "__VERSION__", 0, T_VERSION, DUMP },
481 { "__STDC__", 0, T_STDC, DUMP|STDC },
482
483 { "__USER_LABEL_PREFIX__", 0, T_CONST, ULP },
484 { "__REGISTER_PREFIX__", REGISTER_PREFIX, T_CONST, 0 },
485 { "__HAVE_BUILTIN_SETJMP__", "1", T_CONST, 0 },
486#ifndef NO_BUILTIN_SIZE_TYPE
487 { "__SIZE_TYPE__", SIZE_TYPE, T_CONST, DUMP },
488#endif
489#ifndef NO_BUILTIN_PTRDIFF_TYPE
490 { "__PTRDIFF_TYPE__", PTRDIFF_TYPE, T_CONST, DUMP },
491#endif
Zack Weinberg0209c342000-02-28 21:09:54 +0000492#ifndef NO_BUILTIN_WCHAR_TYPE
Zack Weinberga9ae4481999-10-29 04:31:14 +0000493 { "__WCHAR_TYPE__", WCHAR_TYPE, T_CONST, DUMP },
Zack Weinberg0209c342000-02-28 21:09:54 +0000494#endif
Zack Weinberga9ae4481999-10-29 04:31:14 +0000495 { 0, 0, 0, 0 }
496};
497
498/* Subroutine of cpp_start_read; reads the builtins table above and
499 enters the macros into the hash table. */
500
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000501static void
502initialize_builtins (pfile)
503 cpp_reader *pfile;
504{
Zack Weinberga9ae4481999-10-29 04:31:14 +0000505 int len;
506 const struct builtin *b;
507 const char *val;
508 for(b = builtin_array; b->name; b++)
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000509 {
Zack Weinberga9ae4481999-10-29 04:31:14 +0000510 if ((b->flags & STDC) && CPP_TRADITIONAL (pfile))
511 continue;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000512
Zack Weinberga9ae4481999-10-29 04:31:14 +0000513 val = (b->flags & ULP) ? user_label_prefix : b->value;
514 len = strlen (b->name);
515
Zack Weinbergb0699da2000-03-07 20:58:47 +0000516 _cpp_install (pfile, b->name, len, b->type, val);
Zack Weinberga9ae4481999-10-29 04:31:14 +0000517 if ((b->flags & DUMP) && CPP_OPTIONS (pfile)->debug_output)
518 dump_special_to_buffer (pfile, b->name);
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000519 }
Zack Weinberga9ae4481999-10-29 04:31:14 +0000520
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000521}
Zack Weinberga9ae4481999-10-29 04:31:14 +0000522#undef DUMP
523#undef STDC
Zack Weinberga9ae4481999-10-29 04:31:14 +0000524#undef ULP
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000525
Zack Weinberg0b22d651999-03-15 18:42:46 +0000526/* Another subroutine of cpp_start_read. This one sets up to do
527 dependency-file output. */
528static void
529initialize_dependency_output (pfile)
530 cpp_reader *pfile;
531{
532 cpp_options *opts = CPP_OPTIONS (pfile);
533 char *spec, *s, *output_file;
534
535 /* Either of two environment variables can specify output of deps.
536 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
537 where OUTPUT_FILE is the file to write deps info to
538 and DEPS_TARGET is the target to mention in the deps. */
539
540 if (opts->print_deps == 0)
541 {
542 spec = getenv ("DEPENDENCIES_OUTPUT");
543 if (spec)
544 opts->print_deps = 1;
545 else
546 {
547 spec = getenv ("SUNPRO_DEPENDENCIES");
548 if (spec)
549 opts->print_deps = 2;
550 else
551 return;
552 }
553
554 /* Find the space before the DEPS_TARGET, if there is one. */
555 s = strchr (spec, ' ');
556 if (s)
557 {
558 opts->deps_target = s + 1;
559 output_file = (char *) xmalloc (s - spec + 1);
560 memcpy (output_file, spec, s - spec);
561 output_file[s - spec] = 0;
562 }
563 else
564 {
565 opts->deps_target = 0;
566 output_file = spec;
567 }
568
569 opts->deps_file = output_file;
570 opts->print_deps_append = 1;
571 }
572
Zack Weinberg49e6c082000-03-04 19:42:04 +0000573 pfile->deps = deps_init ();
574
Zack Weinberg0b22d651999-03-15 18:42:46 +0000575 /* Print the expected object file name as the target of this Make-rule. */
Zack Weinberg0b22d651999-03-15 18:42:46 +0000576 if (opts->deps_target)
Zack Weinberg49e6c082000-03-04 19:42:04 +0000577 deps_add_target (pfile->deps, opts->deps_target);
Zack Weinberg0b22d651999-03-15 18:42:46 +0000578 else if (*opts->in_fname == 0)
Zack Weinberg49e6c082000-03-04 19:42:04 +0000579 deps_add_target (pfile->deps, "-");
Zack Weinberg0b22d651999-03-15 18:42:46 +0000580 else
Zack Weinberg49e6c082000-03-04 19:42:04 +0000581 deps_calc_target (pfile->deps, opts->in_fname);
Zack Weinberg0b22d651999-03-15 18:42:46 +0000582
Zack Weinberg49e6c082000-03-04 19:42:04 +0000583 if (opts->in_fname)
584 deps_add_dep (pfile->deps, opts->in_fname);
Zack Weinberg0b22d651999-03-15 18:42:46 +0000585}
586
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000587/* And another subroutine. This one sets up the standard include path. */
588static void
589initialize_standard_includes (pfile)
590 cpp_reader *pfile;
591{
592 cpp_options *opts = CPP_OPTIONS (pfile);
593 char *path;
Zack Weinberg455d2582000-03-04 01:42:56 +0000594 const struct default_include *p;
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000595 char *specd_prefix = opts->include_prefix;
596
597 /* Several environment variables may add to the include search path.
598 CPATH specifies an additional list of directories to be searched
599 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
600 etc. specify an additional list of directories to be searched as
601 if specified with -isystem, for the language indicated. */
602
603 GET_ENV_PATH_LIST (path, "CPATH");
604 if (path != 0 && *path != 0)
605 path_include (pfile, opts->pending, path, BRACKET);
606
607 switch ((opts->objc << 1) + opts->cplusplus)
608 {
609 case 0:
610 GET_ENV_PATH_LIST (path, "C_INCLUDE_PATH");
611 break;
612 case 1:
613 GET_ENV_PATH_LIST (path, "CPLUS_INCLUDE_PATH");
614 break;
615 case 2:
616 GET_ENV_PATH_LIST (path, "OBJC_INCLUDE_PATH");
617 break;
618 case 3:
619 GET_ENV_PATH_LIST (path, "OBJCPLUS_INCLUDE_PATH");
620 break;
621 }
622 if (path != 0 && *path != 0)
623 path_include (pfile, opts->pending, path, SYSTEM);
624
625 /* Search "translated" versions of GNU directories.
626 These have /usr/local/lib/gcc... replaced by specd_prefix. */
627 if (specd_prefix != 0)
628 {
629 char *default_prefix = alloca (sizeof GCC_INCLUDE_DIR - 7);
630 /* Remove the `include' from /usr/local/lib/gcc.../include.
631 GCC_INCLUDE_DIR will always end in /include. */
632 int default_len = sizeof GCC_INCLUDE_DIR - 8;
633 int specd_len = strlen (specd_prefix);
634
635 memcpy (default_prefix, GCC_INCLUDE_DIR, default_len);
636 default_prefix[default_len] = '\0';
637
638 for (p = include_defaults_array; p->fname; p++)
639 {
640 /* Some standard dirs are only for C++. */
641 if (!p->cplusplus
642 || (opts->cplusplus
643 && !opts->no_standard_cplusplus_includes))
644 {
645 /* Does this dir start with the prefix? */
646 if (!strncmp (p->fname, default_prefix, default_len))
647 {
648 /* Yes; change prefix and add to search list. */
649 int flen = strlen (p->fname);
650 int this_len = specd_len + flen - default_len;
651 char *str = (char *) xmalloc (this_len + 1);
652 memcpy (str, specd_prefix, specd_len);
653 memcpy (str + specd_len,
654 p->fname + default_len,
655 flen - default_len + 1);
656
657 append_include_chain (pfile, opts->pending,
658 str, SYSTEM, p->cxx_aware);
659 }
660 }
661 }
662 }
663
664 /* Search ordinary names for GNU include directories. */
665 for (p = include_defaults_array; p->fname; p++)
666 {
667 /* Some standard dirs are only for C++. */
668 if (!p->cplusplus
669 || (opts->cplusplus
670 && !opts->no_standard_cplusplus_includes))
671 {
672 /* XXX Potential memory leak! */
673 char *str = xstrdup (update_path (p->fname, p->component));
674 append_include_chain (pfile, opts->pending, str, SYSTEM,
675 p->cxx_aware);
676 }
677 }
678}
679
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000680/* This is called after options have been processed.
681 * Check options for consistency, and setup for processing input
682 * from the file named FNAME. (Use standard input if FNAME==NULL.)
683 * Return 1 on success, 0 on failure.
684 */
685
686int
687cpp_start_read (pfile, fname)
688 cpp_reader *pfile;
689 char *fname;
690{
691 struct cpp_options *opts = CPP_OPTIONS (pfile);
Zack Weinberg0b22d651999-03-15 18:42:46 +0000692 struct pending_option *p, *q;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000693
Zack Weinberg0b22d651999-03-15 18:42:46 +0000694 /* -MG doesn't select the form of output and must be specified with one of
695 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
696 inhibit compilation. */
697 if (opts->print_deps_missing_files
698 && (opts->print_deps == 0 || !opts->no_output))
699 {
700 cpp_fatal (pfile, "-MG must be specified with one of -M or -MM");
701 return 0;
702 }
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000703
Zack Weinberg0b22d651999-03-15 18:42:46 +0000704 /* Chill should not be used with -trigraphs. */
705 if (opts->chill && opts->trigraphs)
706 {
707 cpp_warning (pfile, "-lang-chill and -trigraphs are mutually exclusive");
708 opts->trigraphs = 0;
709 }
710
711 /* Set this if it hasn't been set already. */
712 if (user_label_prefix == NULL)
713 user_label_prefix = USER_LABEL_PREFIX;
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000714
715 /* Don't bother trying to do macro expansion if we've already done
716 preprocessing. */
717 if (opts->preprocessed)
718 pfile->no_macro_expand++;
Zack Weinberg455d2582000-03-04 01:42:56 +0000719
720 /* Set up the IStable. This doesn't do anything if we were compiled
721 with a compiler that supports C99 designated initializers. */
Zack Weinberga9ae4481999-10-29 04:31:14 +0000722 init_IStable ();
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000723
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000724 /* Set up the include search path now. */
725 if (! opts->no_standard_includes)
726 initialize_standard_includes (pfile);
727
Zack Weinbergb0699da2000-03-07 20:58:47 +0000728 _cpp_merge_include_chains (opts);
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000729
730 /* With -v, print the list of dirs to search. */
731 if (opts->verbose)
732 {
733 struct file_name_list *l;
734 fprintf (stderr, _("#include \"...\" search starts here:\n"));
735 for (l = opts->quote_include; l; l = l->next)
736 {
737 if (l == opts->bracket_include)
738 fprintf (stderr, _("#include <...> search starts here:\n"));
739 fprintf (stderr, " %s\n", l->name);
740 }
741 fprintf (stderr, _("End of search list.\n"));
742 }
743
744 initialize_dependency_output (pfile);
745
746 /* Open the main input file. This must be done before -D processing
747 so we have a buffer to stand on. */
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000748 if (opts->in_fname == NULL || *opts->in_fname == 0)
749 {
750 opts->in_fname = fname;
751 if (opts->in_fname == NULL)
752 opts->in_fname = "";
753 }
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000754
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000755 if (!cpp_read_file (pfile, fname))
756 return 0;
757
758 /* -D and friends may produce output, which should be identified
759 as line 0. */
760
761 CPP_BUFFER (pfile)->lineno = 0;
762
763 /* Install __LINE__, etc. */
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000764 initialize_builtins (pfile);
765
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000766 /* Do -U's, -D's and -A's in the order they were seen. */
Zack Weinberg0b22d651999-03-15 18:42:46 +0000767 p = opts->pending->define_head;
768 while (p)
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000769 {
Zack Weinberg0b22d651999-03-15 18:42:46 +0000770 if (p->undef)
771 cpp_undef (pfile, p->arg);
772 else
773 cpp_define (pfile, p->arg);
774
775 q = p->next;
776 free (p);
777 p = q;
778 }
779
780 p = opts->pending->assert_head;
781 while (p)
782 {
Zack Weinberg0b22d651999-03-15 18:42:46 +0000783 if (p->undef)
784 cpp_unassert (pfile, p->arg);
785 else
786 cpp_assert (pfile, p->arg);
787
788 q = p->next;
789 free (p);
790 p = q;
791 }
792
793 opts->done_initializing = 1;
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000794 CPP_BUFFER (pfile)->lineno = 1;
Zack Weinberg0b22d651999-03-15 18:42:46 +0000795
Jason Merrill3773a461999-07-20 15:13:01 -0400796 if (opts->preprocessed)
797 /* If we've already processed this code, we want to trust the #line
798 directives in the input. But we still need to update our line
799 counter accordingly. */
800 pfile->lineno = CPP_BUFFER (pfile)->lineno;
801 else
802 output_line_command (pfile, same_file);
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000803 pfile->only_seen_white = 2;
804
805 /* The -imacros files can be scanned now, but the -include files
806 have to be pushed onto the include stack and processed later,
Zack Weinberg0b22d651999-03-15 18:42:46 +0000807 in the main loop calling cpp_get_token. */
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000808
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000809 opts->no_output++;
Zack Weinberg0b22d651999-03-15 18:42:46 +0000810 p = opts->pending->imacros_head;
811 while (p)
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000812 {
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000813 if (cpp_read_file (pfile, p->arg))
814 cpp_scan_buffer (pfile);
Zack Weinberg0b22d651999-03-15 18:42:46 +0000815
816 q = p->next;
817 free (p);
818 p = q;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000819 }
Zack Weinberg0b22d651999-03-15 18:42:46 +0000820 opts->no_output--;
821
822 p = opts->pending->include_head;
823 while (p)
824 {
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000825 if (cpp_read_file (pfile, p->arg))
826 output_line_command (pfile, enter_file);
Zack Weinberg0b22d651999-03-15 18:42:46 +0000827
Zack Weinberg0b22d651999-03-15 18:42:46 +0000828 q = p->next;
829 free (p);
830 p = q;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000831 }
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000832
Zack Weinberg0b22d651999-03-15 18:42:46 +0000833 free (opts->pending);
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000834 opts->pending = NULL;
835
836 return 1;
837}
838
839/* This is called at the end of preprocessing. It pops the
840 last buffer and writes dependency output. It should also
841 clear macro definitions, such that you could call cpp_start_read
842 with a new filename to restart processing. */
843void
844cpp_finish (pfile)
845 cpp_reader *pfile;
846{
847 struct cpp_options *opts = CPP_OPTIONS (pfile);
848
849 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) != CPP_NULL_BUFFER (pfile))
Zack Weinbergc1212d22000-02-06 23:46:18 +0000850 cpp_ice (pfile, "buffers still stacked in cpp_finish");
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000851 cpp_pop_buffer (pfile);
Zack Weinbergc1212d22000-02-06 23:46:18 +0000852
Zack Weinberg49e6c082000-03-04 19:42:04 +0000853 /* Don't write the deps file if preprocessing has failed. */
854 if (opts->print_deps && pfile->errors == 0)
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000855 {
856 /* Stream on which to print the dependency information. */
Zack Weinbergc1212d22000-02-06 23:46:18 +0000857 FILE *deps_stream = 0;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000858
Zack Weinberg49e6c082000-03-04 19:42:04 +0000859 const char *deps_mode = opts->print_deps_append ? "a" : "w";
860 if (opts->deps_file == 0)
861 deps_stream = stdout;
862 else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
863 cpp_notice_from_errno (pfile, opts->deps_file);
Zack Weinbergc1212d22000-02-06 23:46:18 +0000864
Zack Weinberg49e6c082000-03-04 19:42:04 +0000865 if (deps_stream)
866 {
867 deps_write (pfile->deps, deps_stream, 72);
868 if (opts->deps_file)
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000869 {
Zack Weinberg49e6c082000-03-04 19:42:04 +0000870 if (ferror (deps_stream) || fclose (deps_stream) != 0)
871 cpp_fatal (pfile, "I/O error on output");
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000872 }
873 }
874 }
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000875
876 if (opts->dump_macros == dump_only)
877 {
878 int i;
879 HASHNODE *h;
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000880 for (i = HASHSIZE; --i >= 0;)
881 {
882 for (h = pfile->hashtab[i]; h; h = h->next)
883 if (h->type == T_MACRO)
884 {
Zack Weinbergb0699da2000-03-07 20:58:47 +0000885 _cpp_dump_definition (pfile, h->name, h->length,
886 h->value.defn);
Zack Weinberg3caee4a1999-04-26 16:41:02 +0000887 CPP_PUTC (pfile, '\n');
888 }
889 }
890 }
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000891}
892
Richard Henderson223dca61999-12-14 08:05:23 -0800893static void
894new_pending_define (opts, text)
895 struct cpp_options *opts;
896 const char *text;
897{
898 struct pending_option *o = (struct pending_option *)
899 xmalloc (sizeof (struct pending_option));
900
Richard Kennerc8d8ed62000-02-19 01:27:00 +0000901 o->arg = (char *) text;
Richard Henderson223dca61999-12-14 08:05:23 -0800902 o->next = NULL;
903 o->undef = 0;
904 APPEND (opts->pending, define, o);
905}
906
Zack Weinberge23c0ba2000-03-07 23:11:06 +0000907enum opt_code
908{
909 OPT_stdin_stdout = 0, OPT_dollar, OPT_plus,
910 OPT__help, OPT__version,
911 OPT_A, OPT_C, OPT_D, OPT_H, OPT_I, OPT_M,
912 OPT_MD, OPT_MG, OPT_MM, OPT_MMD,
913 OPT_P, OPT_U, OPT_W,
914 OPT_d,
915 OPT_fleading_underscore, OPT_fno_leading_underscore,
916 OPT_fpreprocessed, OPT_fno_preprocessed,
917 OPT_g, OPT_h,
918 OPT_idirafter, OPT_imacros, OPT_include,
919 OPT_iprefix, OPT_isystem, OPT_iwithprefix, OPT_iwithprefixbefore,
920 OPT_lang_asm, OPT_lang_c, OPT_lang_cplusplus, OPT_lang_c89,
921 OPT_lang_chill, OPT_lang_fortran, OPT_lang_objc, OPT_lang_objcplusplus,
922 OPT_nostdinc, OPT_nostdincplusplus,
923 OPT_o,
924 OPT_pedantic, OPT_pedantic_errors, OPT_remap,
925 OPT_std_c89, OPT_std_c99, OPT_std_c9x, OPT_std_gnu89, OPT_std_gnu99,
926 OPT_std_gnu9x, OPT_std_iso9899_1990, OPT_std_iso9899_199409,
927 OPT_std_iso9899_1999, OPT_std_iso9899_199x,
928 OPT_traditional, OPT_trigraphs,
929 OPT_v, OPT_w,
930 N_OPTS
931};
932
933struct cl_option
934{
935 const char *opt_text;
936 const char *msg;
937 size_t opt_len;
938 enum opt_code opt_code;
939};
940
941static const char no_arg[] = N_("Argument missing after `%s' option");
942static const char no_ass[] = N_("Assertion missing after `%s' option");
943static const char no_dir[] = N_("Directory name missing after `%s' option");
944static const char no_fil[] = N_("File name missing after `%s' option");
945static const char no_mac[] = N_("Macro name missing after `%s' option");
946static const char no_pth[] = N_("Path name missing after `%s' option");
947
948/* This list must be ASCII sorted. Make enum order above match this. */
949#define DEF_OPT(text, msg, code) {text, msg, sizeof(text) - 1, code}
950
951#ifdef HOST_EBCDIC
952static struct cl_option cl_options[] =
953#else
954static const struct cl_option cl_options[] =
955#endif
956{
957 DEF_OPT("", 0, OPT_stdin_stdout),
958 DEF_OPT("$", 0, OPT_dollar),
959 DEF_OPT("+", 0, OPT_plus),
960 DEF_OPT("-help", 0, OPT__help),
961 DEF_OPT("-version", 0, OPT__version),
962 DEF_OPT("A", no_ass, OPT_A),
963 DEF_OPT("C", 0, OPT_C),
964 DEF_OPT("D", no_mac, OPT_D),
965 DEF_OPT("H", 0, OPT_H),
966 DEF_OPT("I", no_dir, OPT_I),
967 DEF_OPT("M", 0, OPT_M),
968 DEF_OPT("MD", no_fil, OPT_MD),
969 DEF_OPT("MG", 0, OPT_MG),
970 DEF_OPT("MM", 0, OPT_MM),
971 DEF_OPT("MMD", no_fil, OPT_MMD),
972 DEF_OPT("P", 0, OPT_P),
973 DEF_OPT("U", no_mac, OPT_U),
974 /* NB: Immed arg only, and not reqd */
975 DEF_OPT("W", no_arg, OPT_W),
976 DEF_OPT("d", no_arg, OPT_d),
977 DEF_OPT("fleading-underscore", 0, OPT_fleading_underscore),
978 DEF_OPT("fno-leading-underscore", 0, OPT_fno_leading_underscore),
979 DEF_OPT("fpreprocessed", 0, OPT_fpreprocessed),
980 DEF_OPT("fno-preprocessed", 0, OPT_fno_preprocessed),
981 /* NB: Immed arg only, and not reqd */
982 DEF_OPT("g", no_arg, OPT_g),
983 DEF_OPT("h", 0, OPT_h),
984 DEF_OPT("idirafter", no_dir, OPT_idirafter),
985 DEF_OPT("imacros", no_fil, OPT_imacros),
986 DEF_OPT("include", no_fil, OPT_include),
987 DEF_OPT("iprefix", no_pth, OPT_iprefix),
988 DEF_OPT("isystem", no_dir, OPT_isystem),
989 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix),
990 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore),
991 DEF_OPT("lang-asm", 0, OPT_lang_asm),
992 DEF_OPT("lang-c", 0, OPT_lang_c),
993 DEF_OPT("lang-c++", 0, OPT_lang_cplusplus),
994 DEF_OPT("lang-c89", 0, OPT_lang_c89),
995 DEF_OPT("lang-chill", 0, OPT_lang_chill),
996 DEF_OPT("lang-fortran", 0, OPT_lang_fortran),
997 DEF_OPT("lang-objc", 0, OPT_lang_objc),
998 DEF_OPT("lang-objc++", 0, OPT_lang_objcplusplus),
999 DEF_OPT("nostdinc", 0, OPT_nostdinc),
1000 DEF_OPT("nostdinc++", 0, OPT_nostdincplusplus),
1001 DEF_OPT("o", no_fil, OPT_o),
1002 DEF_OPT("pedantic", 0, OPT_pedantic),
1003 DEF_OPT("pedantic-errors", 0, OPT_pedantic_errors),
1004 DEF_OPT("remap", 0, OPT_remap),
1005 DEF_OPT("std=c89", 0, OPT_std_c89),
1006 DEF_OPT("std=c99", 0, OPT_std_c99),
1007 DEF_OPT("std=c9x", 0, OPT_std_c9x),
1008 DEF_OPT("std=gnu89", 0, OPT_std_gnu89),
1009 DEF_OPT("std=gnu99", 0, OPT_std_gnu99),
1010 DEF_OPT("std=gnu9x", 0, OPT_std_gnu9x),
1011 DEF_OPT("std=iso9899:1990", 0, OPT_std_iso9899_1990),
1012 DEF_OPT("std=iso9899:199409", 0, OPT_std_iso9899_199409),
1013 DEF_OPT("std=iso9899:1999", 0, OPT_std_iso9899_1999),
1014 DEF_OPT("std=iso9899:199x", 0, OPT_std_iso9899_199x),
1015 DEF_OPT("traditional", 0, OPT_traditional),
1016 DEF_OPT("trigraphs", 0, OPT_trigraphs),
1017 DEF_OPT("v", 0, OPT_v),
1018 DEF_OPT("w", 0, OPT_w)
1019};
1020#undef DEF_OPT
1021
1022/* Perform a binary search to find which, if any, option the given
1023 command-line matches. Returns its index in the option array,
1024 negative on failure. Complications arise since some options can be
1025 suffixed with an argument, and multiple complete matches can occur,
1026 e.g. -iwithprefix and -iwithprefixbefore. Moreover, we want to
1027 accept options beginning with -g and -W that we do not recognise,
1028 but not to swallow any subsequent command line argument; these are
1029 handled as special cases in cpp_handle_option */
1030static int
1031parse_option (input)
1032 const char *input;
1033{
1034 unsigned int md, mn, mx;
1035 size_t opt_len;
1036 int comp;
1037
1038 mn = 0;
1039 mx = N_OPTS;
1040
1041 while (mx > mn)
1042 {
1043 md = (mn + mx) / 2;
1044
1045 opt_len = cl_options[md].opt_len;
1046 comp = strncmp (input, cl_options[md].opt_text, opt_len);
1047
1048 if (comp > 0)
1049 mn = md + 1;
1050 else if (comp < 0)
1051 mx = md;
1052 else
1053 {
1054 if (input[opt_len] == '\0')
1055 return md;
1056 /* We were passed more text. If the option takes an argument,
1057 we may match a later option or we may have been passed the
1058 argument. The longest possible option match succeeds.
1059 If the option takes no arguments we have not matched and
1060 continue the search (e.g. input="stdc++" match was "stdc") */
1061 mn = md + 1;
1062 if (cl_options[md].msg)
1063 {
1064 /* Scan forwards. If we get an exact match, return it.
1065 Otherwise, return the longest option-accepting match.
1066 This loops no more than twice with current options */
1067 mx = md;
1068 for (; mn < N_OPTS; mn++)
1069 {
1070 opt_len = cl_options[mn].opt_len;
1071 if (strncmp (input, cl_options[mn].opt_text, opt_len))
1072 break;
1073 if (input[opt_len] == '\0')
1074 return mn;
1075 if (cl_options[mn].msg)
1076 mx = mn;
1077 }
1078 return mx;
1079 }
1080 }
1081 }
1082
1083 return -1;
1084}
1085
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001086/* Handle one command-line option in (argc, argv).
1087 Can be called multiple times, to handle multiple sets of options.
1088 Returns number of strings consumed. */
Richard Kennerc8d8ed62000-02-19 01:27:00 +00001089
Zack Weinbergb0699da2000-03-07 20:58:47 +00001090static int
1091handle_option (pfile, argc, argv)
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001092 cpp_reader *pfile;
1093 int argc;
1094 char **argv;
1095{
1096 struct cpp_options *opts = CPP_OPTIONS (pfile);
1097 int i = 0;
1098
Zack Weinberg0b22d651999-03-15 18:42:46 +00001099 if (argv[i][0] != '-')
1100 {
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001101 if (opts->out_fname != NULL)
1102 {
Zack Weinberg0b22d651999-03-15 18:42:46 +00001103 print_help ();
1104 cpp_fatal (pfile, "Too many arguments");
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001105 }
Zack Weinberg0b22d651999-03-15 18:42:46 +00001106 else if (opts->in_fname != NULL)
1107 opts->out_fname = argv[i];
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001108 else
Zack Weinberg0b22d651999-03-15 18:42:46 +00001109 opts->in_fname = argv[i];
1110 }
1111 else
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001112 {
1113 enum opt_code opt_code;
1114 int opt_index;
1115 char *arg = 0;
Zack Weinberg0b22d651999-03-15 18:42:46 +00001116
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001117 /* Skip over '-' */
1118 opt_index = parse_option (&argv[i][1]);
1119 if (opt_index < 0)
1120 return i;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001121
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001122 opt_code = cl_options[opt_index].opt_code;
1123 if (cl_options[opt_index].msg)
Zack Weinberg0b22d651999-03-15 18:42:46 +00001124 {
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001125 arg = &argv[i][cl_options[opt_index].opt_len + 1];
1126
1127 /* Yuk. Special case for -g and -W as they must not swallow
1128 up any following argument. If this becomes common, add
1129 another field to the cl_options table */
1130 if (arg[0] == '\0' && !(opt_code == OPT_g || opt_code == OPT_W))
Zack Weinberg0b22d651999-03-15 18:42:46 +00001131 {
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001132 arg = argv[++i];
1133 if (!arg)
Zack Weinberg0b22d651999-03-15 18:42:46 +00001134 {
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001135 cpp_fatal (pfile, _(cl_options[opt_index].msg), argv[i - 1]);
1136 return argc;
1137 }
1138 }
1139 }
1140
1141 switch (opt_code)
1142 {
1143 case N_OPTS: /* shut GCC up */
1144 break;
1145 case OPT_fleading_underscore:
1146 user_label_prefix = "_";
1147 break;
1148 case OPT_fno_leading_underscore:
1149 user_label_prefix = "";
1150 break;
1151 case OPT_fpreprocessed:
1152 opts->preprocessed = 1;
1153 break;
1154 case OPT_fno_preprocessed:
1155 opts->preprocessed = 0;
1156 break;
1157 case OPT_w:
1158 opts->inhibit_warnings = 1;
1159 break;
1160 case OPT_g: /* Silently ignore anything but -g3 */
1161 if (!strcmp(&argv[i][2], "3"))
1162 opts->debug_output = 1;
1163 break;
1164 case OPT_h:
1165 case OPT__help:
1166 print_help ();
1167 exit (0); /* XXX */
1168 break;
1169 case OPT__version:
1170 fprintf (stderr, _("GNU CPP version %s (cpplib)\n"), version_string);
1171 exit (0); /* XXX */
1172 break;
1173 case OPT_C:
1174 opts->discard_comments = 0;
1175 break;
1176 case OPT_P:
1177 opts->no_line_commands = 1;
1178 break;
1179 case OPT_dollar: /* Don't include $ in identifiers. */
1180 opts->dollars_in_ident = 0;
1181 break;
1182 case OPT_H:
1183 opts->print_include_names = 1;
1184 break;
1185 case OPT_D:
1186 new_pending_define (opts, arg);
1187 break;
1188 case OPT_pedantic_errors:
1189 opts->pedantic_errors = 1;
1190 /* fall through */
1191 case OPT_pedantic:
1192 opts->pedantic = 1;
1193 break;
1194 case OPT_traditional:
1195 opts->traditional = 1;
1196 opts->cplusplus_comments = 0;
1197 opts->trigraphs = 0;
1198 opts->warn_trigraphs = 0;
1199 break;
1200 case OPT_trigraphs:
1201 opts->trigraphs = 1;
1202 break;
1203 case OPT_plus:
1204 opts->cplusplus = 1;
1205 opts->cplusplus_comments = 1;
1206 break;
1207 case OPT_remap:
1208 opts->remap = 1;
1209 break;
1210 case OPT_iprefix:
1211 opts->include_prefix = arg;
1212 opts->include_prefix_len = strlen (arg);
1213 break;
1214 case OPT_lang_c:
1215 opts->cplusplus = 0, opts->cplusplus_comments = 1;
1216 opts->c89 = 0, opts->c99 = 1, opts->objc = 0;
1217 break;
1218 case OPT_lang_c89:
1219 opts->cplusplus = 0, opts->cplusplus_comments = 0;
1220 opts->c89 = 1, opts->c99 = 0, opts->objc = 0;
1221 opts->trigraphs = 1;
1222 new_pending_define (opts, "__STRICT_ANSI__");
1223 break;
1224 case OPT_lang_cplusplus:
1225 opts->cplusplus = 1, opts->cplusplus_comments = 1;
1226 opts->c89 = 0, opts->c99 = 0, opts->objc = 0;
1227 break;
1228 case OPT_lang_objc:
1229 case OPT_lang_objcplusplus:
1230 opts->cplusplus = opt_code == OPT_lang_objcplusplus;
1231 opts->cplusplus_comments = 1;
1232 opts->c89 = 0, opts->c99 = 0, opts->objc = 1;
1233 break;
1234 case OPT_lang_asm:
1235 opts->lang_asm = 1;
1236 break;
1237 case OPT_lang_fortran:
1238 opts->lang_fortran = 1, opts->cplusplus_comments = 0;
1239 break;
1240 case OPT_lang_chill:
1241 opts->objc = 0, opts->cplusplus = 0;
1242 opts->chill = 1, opts->traditional = 1;
1243 break;
1244 case OPT_nostdinc:
1245 /* -nostdinc causes no default include directories.
1246 You must specify all include-file directories with -I. */
1247 opts->no_standard_includes = 1;
1248 break;
1249 case OPT_nostdincplusplus:
1250 /* -nostdinc++ causes no default C++-specific include directories. */
1251 opts->no_standard_cplusplus_includes = 1;
1252 break;
1253 case OPT_std_gnu89:
1254 opts->cplusplus = 0, opts->cplusplus_comments = 1;
1255 opts->c89 = 1, opts->c99 = 0, opts->objc = 0;
1256 break;
1257 case OPT_std_gnu9x:
1258 case OPT_std_gnu99:
1259 opts->cplusplus = 0, opts->cplusplus_comments = 1;
1260 opts->c89 = 0, opts->c99 = 1, opts->objc = 0;
1261 new_pending_define (opts, "__STDC_VERSION__=199901L");
1262 break;
1263 case OPT_std_iso9899_199409:
1264 new_pending_define (opts, "__STDC_VERSION__=199409L");
1265 /* Fall through */
1266 case OPT_std_iso9899_1990:
1267 case OPT_std_c89:
1268 opts->cplusplus = 0, opts->cplusplus_comments = 0;
1269 opts->c89 = 1, opts->c99 = 0, opts->objc = 0;
1270 opts->trigraphs = 1;
1271 new_pending_define (opts, "__STRICT_ANSI__");
1272 break;
1273 case OPT_std_iso9899_199x:
1274 case OPT_std_iso9899_1999:
1275 case OPT_std_c9x:
1276 case OPT_std_c99:
1277 opts->cplusplus = 0, opts->cplusplus_comments = 1;
1278 opts->c89 = 0, opts->c99 = 1, opts->objc = 0;
1279 opts->trigraphs = 1;
1280 new_pending_define (opts, "__STRICT_ANSI__");
1281 new_pending_define (opts, "__STDC_VERSION__=199901L");
1282 break;
1283 case OPT_o:
1284 if (opts->out_fname != NULL)
1285 {
1286 cpp_fatal (pfile, "Output filename specified twice");
1287 return argc;
1288 }
1289 opts->out_fname = arg;
1290 if (!strcmp (opts->out_fname, "-"))
1291 opts->out_fname = "";
1292 break;
1293 case OPT_v:
1294 fprintf (stderr, _("GNU CPP version %s (cpplib)\n"), version_string);
1295#ifdef TARGET_VERSION
1296 TARGET_VERSION;
1297#endif
1298 fputc ('\n', stderr);
1299 opts->verbose = 1;
1300 break;
1301 case OPT_stdin_stdout:
1302 /* JF handle '-' as file name meaning stdin or stdout */
1303 if (opts->in_fname == NULL)
1304 opts->in_fname = "";
1305 else if (opts->out_fname == NULL)
1306 opts->out_fname = "";
1307 break;
1308 case OPT_d:
1309 /* Args to -d specify what parts of macros to dump.
1310 Silently ignore unrecognised options; they may
1311 be aimed at the compiler proper. */
1312 {
1313 char c;
1314
1315 while ((c = *arg++) != '\0')
1316 switch (c)
1317 {
1318 case 'M':
Zack Weinberg0b22d651999-03-15 18:42:46 +00001319 opts->dump_macros = dump_only;
1320 opts->no_output = 1;
1321 break;
1322 case 'N':
1323 opts->dump_macros = dump_names;
1324 break;
1325 case 'D':
1326 opts->dump_macros = dump_definitions;
1327 break;
1328 case 'I':
1329 opts->dump_includes = 1;
1330 break;
1331 }
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001332 }
1333 break;
1334 /* The style of the choices here is a bit mixed.
1335 The chosen scheme is a hybrid of keeping all options in one string
1336 and specifying each option in a separate argument:
1337 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
1338 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1339 -M[M][G][D file]. This is awkward to handle in specs, and is not
1340 as extensible. */
1341 /* ??? -MG must be specified in addition to one of -M or -MM.
1342 This can be relaxed in the future without breaking anything.
1343 The converse isn't true. */
1344
1345 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
1346 case OPT_MG:
1347 opts->print_deps_missing_files = 1;
1348 break;
1349 case OPT_M:
1350 case OPT_MD:
1351 case OPT_MM:
1352 case OPT_MMD:
1353 if (opt_code == OPT_M || opt_code == OPT_MD)
1354 opts->print_deps = 2;
1355 else
1356 opts->print_deps = 1;
Zack Weinberg0b22d651999-03-15 18:42:46 +00001357
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001358 /* For -MD and -MMD options, write deps on file named by next arg */
1359 /* For -M and -MM, write deps on standard output
1360 and suppress the usual output. */
1361 if (opt_code == OPT_MD || opt_code == OPT_MMD)
1362 opts->deps_file = arg;
1363 else
1364 opts->no_output = 1;
1365 break;
1366 case OPT_A:
1367 if (strcmp (arg, "-"))
1368 {
1369 struct pending_option *o = (struct pending_option *)
1370 xmalloc (sizeof (struct pending_option));
1371
1372 o->arg = arg;
1373 o->next = NULL;
1374 o->undef = 0;
1375 APPEND (opts->pending, assert, o);
Zack Weinberg0b22d651999-03-15 18:42:46 +00001376 }
1377 else
1378 {
1379 /* -A- eliminates all predefined macros and assertions.
1380 Let's include also any that were specified earlier
1381 on the command line. That way we can get rid of any
1382 that were passed automatically in from GCC. */
1383 struct pending_option *o1, *o2;
1384
1385 o1 = opts->pending->define_head;
1386 while (o1)
1387 {
1388 o2 = o1->next;
1389 free (o1);
1390 o1 = o2;
1391 }
1392 o1 = opts->pending->assert_head;
1393 while (o1)
1394 {
1395 o2 = o1->next;
1396 free (o1);
1397 o1 = o2;
1398 }
1399 opts->pending->assert_head = NULL;
1400 opts->pending->assert_tail = NULL;
1401 opts->pending->define_head = NULL;
1402 opts->pending->define_tail = NULL;
Zack Weinberg0b22d651999-03-15 18:42:46 +00001403 }
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001404 break;
1405 case OPT_U:
1406 {
1407 struct pending_option *o = (struct pending_option *)
1408 xmalloc (sizeof (struct pending_option));
1409
1410 o->arg = arg;
1411 o->next = NULL;
1412 o->undef = 1;
1413 APPEND (opts->pending, define, o);
1414 }
1415 break;
1416 case OPT_I: /* Add directory to path for includes. */
1417 if (!strcmp (arg, "-"))
1418 {
1419 /* -I- means:
1420 Use the preceding -I directories for #include "..."
1421 but not #include <...>.
1422 Don't search the directory of the present file
1423 for #include "...". (Note that -I. -I- is not the same as
1424 the default setup; -I. uses the compiler's working dir.) */
1425 if (! opts->ignore_srcdir)
1426 {
1427 opts->ignore_srcdir = 1;
1428 opts->pending->quote_head = opts->pending->brack_head;
1429 opts->pending->quote_tail = opts->pending->brack_tail;
1430 opts->pending->brack_head = 0;
1431 opts->pending->brack_tail = 0;
1432 }
1433 else
1434 {
1435 cpp_fatal (pfile, "-I- specified twice");
1436 return argc;
1437 }
1438 }
1439 else
1440 append_include_chain (pfile, opts->pending,
1441 xstrdup (arg), BRACKET, 0);
1442 break;
1443 case OPT_isystem:
1444 /* Add directory to beginning of system include path, as a system
1445 include directory. */
1446 append_include_chain (pfile, opts->pending,
1447 xstrdup (arg), SYSTEM, 0);
1448 break;
1449 case OPT_include:
1450 {
1451 struct pending_option *o = (struct pending_option *)
1452 xmalloc (sizeof (struct pending_option));
1453 o->arg = arg;
1454
1455 /* This list has to be built in reverse order so that
1456 when cpp_start_read pushes all the -include files onto
1457 the buffer stack, they will be scanned in forward order. */
1458 o->next = opts->pending->include_head;
1459 opts->pending->include_head = o;
1460 }
1461 break;
1462 case OPT_imacros:
1463 {
1464 struct pending_option *o = (struct pending_option *)
1465 xmalloc (sizeof (struct pending_option));
1466 o->arg = arg;
1467 o->next = NULL;
1468
1469 APPEND (opts->pending, imacros, o);
1470 }
1471 break;
1472 case OPT_iwithprefix:
1473 /* Add directory to end of path for includes,
1474 with the default prefix at the front of its name. */
1475 /* fall through */
1476 case OPT_iwithprefixbefore:
1477 /* Add directory to main path for includes,
1478 with the default prefix at the front of its name. */
1479 {
1480 char *fname;
1481 int len;
1482
1483 len = strlen (arg);
1484
1485 if (opts->include_prefix != 0)
1486 {
1487 fname = xmalloc (opts->include_prefix_len + len + 1);
1488 memcpy (fname, opts->include_prefix, opts->include_prefix_len);
1489 memcpy (fname + opts->include_prefix_len, arg, len + 1);
1490 }
1491 else
1492 {
1493 fname = xmalloc (sizeof GCC_INCLUDE_DIR - 8 + len);
1494 memcpy (fname, GCC_INCLUDE_DIR, sizeof GCC_INCLUDE_DIR - 9);
1495 memcpy (fname + sizeof GCC_INCLUDE_DIR - 9, arg, len + 1);
1496 }
1497
1498 append_include_chain (pfile, opts->pending, fname,
1499 opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0);
1500 }
1501 break;
1502 case OPT_idirafter:
1503 /* Add directory to end of path for includes. */
1504 append_include_chain (pfile, opts->pending,
1505 xstrdup (arg), AFTER, 0);
1506 break;
1507 case OPT_W:
1508 /* Silently ignore unrecognised options */
1509 if (!strcmp (argv[i], "-Wall"))
Zack Weinberg0b22d651999-03-15 18:42:46 +00001510 {
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001511 opts->warn_trigraphs = 1;
1512 opts->warn_comments = 1;
Zack Weinberg0b22d651999-03-15 18:42:46 +00001513 }
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001514 else if (!strcmp (argv[i], "-Wtraditional"))
1515 opts->warn_stringify = 1;
1516 else if (!strcmp (argv[i], "-Wtrigraphs"))
1517 opts->warn_trigraphs = 1;
1518 else if (!strcmp (argv[i], "-Wcomment"))
1519 opts->warn_comments = 1;
1520 else if (!strcmp (argv[i], "-Wcomments"))
1521 opts->warn_comments = 1;
1522 else if (!strcmp (argv[i], "-Wundef"))
1523 opts->warn_undef = 1;
1524 else if (!strcmp (argv[i], "-Wimport"))
1525 opts->warn_import = 1;
1526 else if (!strcmp (argv[i], "-Werror"))
1527 opts->warnings_are_errors = 1;
1528 else if (!strcmp (argv[i], "-Wno-traditional"))
1529 opts->warn_stringify = 0;
1530 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1531 opts->warn_trigraphs = 0;
1532 else if (!strcmp (argv[i], "-Wno-comment"))
1533 opts->warn_comments = 0;
1534 else if (!strcmp (argv[i], "-Wno-comments"))
1535 opts->warn_comments = 0;
1536 else if (!strcmp (argv[i], "-Wno-undef"))
1537 opts->warn_undef = 0;
1538 else if (!strcmp (argv[i], "-Wno-import"))
1539 opts->warn_import = 0;
1540 else if (!strcmp (argv[i], "-Wno-error"))
1541 opts->warnings_are_errors = 0;
1542 break;
1543 }
1544 }
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001545 return i + 1;
1546}
1547
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001548#ifdef HOST_EBCDIC
1549static int
1550opt_comp (const void *p1, const void *p2)
1551{
1552 return strcmp (((struct cl_option *)p1)->opt_text,
1553 ((struct cl_option *)p2)->opt_text);
1554}
1555#endif
1556
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001557/* Handle command-line options in (argc, argv).
1558 Can be called multiple times, to handle multiple sets of options.
1559 Returns if an unrecognized option is seen.
1560 Returns number of strings consumed. */
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001561int
1562cpp_handle_options (pfile, argc, argv)
1563 cpp_reader *pfile;
1564 int argc;
1565 char **argv;
1566{
1567 int i;
1568 int strings_processed;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001569
1570#ifdef HOST_EBCDIC
1571 static int opts_sorted = 0;
1572
1573 if (!opts_sorted)
1574 {
1575 opts_sorted = 1;
1576 /* For non-ASCII hosts, the array needs to be sorted at runtime */
1577 qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
1578 }
1579#endif
1580
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001581 for (i = 0; i < argc; i += strings_processed)
1582 {
Zack Weinbergb0699da2000-03-07 20:58:47 +00001583 strings_processed = handle_option (pfile, argc - i, argv + i);
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001584 if (strings_processed == 0)
1585 break;
1586 }
1587 return i;
1588}
1589
1590static void
1591print_help ()
1592{
Zack Weinbergc1212d22000-02-06 23:46:18 +00001593 fprintf (stderr, _("Usage: %s [switches] input output\n"), progname);
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001594 fputs (_("\
1595Switches:\n\
1596 -include <file> Include the contents of <file> before other files\n\
1597 -imacros <file> Accept definition of macros in <file>\n\
1598 -iprefix <path> Specify <path> as a prefix for next two options\n\
1599 -iwithprefix <dir> Add <dir> to the end of the system include path\n\
1600 -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\
1601 -isystem <dir> Add <dir> to the start of the system include path\n\
1602 -idirafter <dir> Add <dir> to the end of the system include path\n\
1603 -I <dir> Add <dir> to the end of the main include path\n\
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001604 -I- Fine-grained include path control; see info docs\n\
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001605 -nostdinc Do not search system include directories\n\
1606 (dirs specified with -isystem will still be used)\n\
1607 -nostdinc++ Do not search system include directories for C++\n\
1608 -o <file> Put output into <file>\n\
1609 -pedantic Issue all warnings demanded by strict ANSI C\n\
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001610 -pedantic-errors Issue -pedantic warnings as errors instead\n\
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001611 -traditional Follow K&R pre-processor behaviour\n\
1612 -trigraphs Support ANSI C trigraphs\n\
1613 -lang-c Assume that the input sources are in C\n\
1614 -lang-c89 Assume that the input sources are in C89\n\
1615 -lang-c++ Assume that the input sources are in C++\n\
1616 -lang-objc Assume that the input sources are in ObjectiveC\n\
1617 -lang-objc++ Assume that the input sources are in ObjectiveC++\n\
1618 -lang-asm Assume that the input sources are in assembler\n\
Zack Weinberg40c79d52000-01-12 00:35:36 +00001619 -lang-fortran Assume that the input sources are in Fortran\n\
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001620 -lang-chill Assume that the input sources are in Chill\n\
1621 -std=<std name> Specify the conformance standard; one of:\n\
Ulrich Drepper916269a2000-01-29 19:00:43 +00001622 gnu89, gnu99, c89, c99, iso9899:1990,\n\
1623 iso9899:199409, iso9899:1999\n\
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001624 -+ Allow parsing of C++ style features\n\
1625 -w Inhibit warning messages\n\
1626 -Wtrigraphs Warn if trigraphs are encountered\n\
1627 -Wno-trigraphs Do not warn about trigraphs\n\
1628 -Wcomment{s} Warn if one comment starts inside another\n\
1629 -Wno-comment{s} Do not warn about comments\n\
1630 -Wtraditional Warn if a macro argument is/would be turned into\n\
1631 a string if -traditional is specified\n\
1632 -Wno-traditional Do not warn about stringification\n\
1633 -Wundef Warn if an undefined macro is used by #if\n\
1634 -Wno-undef Do not warn about testing undefined macros\n\
1635 -Wimport Warn about the use of the #import directive\n\
1636 -Wno-import Do not warn about the use of #import\n\
1637 -Werror Treat all warnings as errors\n\
1638 -Wno-error Do not treat warnings as errors\n\
1639 -Wall Enable all preprocessor warnings\n\
1640 -M Generate make dependencies\n\
1641 -MM As -M, but ignore system header files\n\
1642 -MD As -M, but put output in a .d file\n\
1643 -MMD As -MD, but ignore system header files\n\
1644 -MG Treat missing header file as generated files\n\
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001645 -g3 Include #define and #undef directives in the output\n\
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001646 -D<macro> Define a <macro> with string '1' as its value\n\
1647 -D<macro>=<val> Define a <macro> with <val> as its value\n\
1648 -A<question> (<answer>) Assert the <answer> to <question>\n\
1649 -U<macro> Undefine <macro> \n\
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001650 -v Display the version number\n\
1651 -H Print the name of header files as they are used\n\
1652 -C Do not discard comments\n\
1653 -dM Display a list of macro definitions active at end\n\
1654 -dD Preserve macro definitions in output\n\
1655 -dN As -dD except that only the names are preserved\n\
1656 -dI Include #include directives in the output\n\
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001657 -P Do not generate #line directives\n\
1658 -$ Do not allow '$' in identifiers\n\
1659 -remap Remap file names when including files.\n\
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001660 --version Display version information\n\
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001661 -h or --help Display this information\n\
1662"), stdout);
1663}