blob: dad255aca8ce9d612d9bdc89236bef48160c5a4b [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,
Graham Stott745b26b2002-01-03 03:55:19 +00003 1999, 2000, 2001, 2002 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"
Zack Weinberg6de1e2a1999-02-18 15:35:49 +000024#include "cpplib.h"
25#include "cpphash.h"
Zack Weinberg6de1e2a1999-02-18 15:35:49 +000026#include "prefix.h"
27#include "intl.h"
Kaveh R. Ghazi9f8f4ef2000-02-15 16:36:35 +000028#include "version.h"
Zack Weinberg49e6c082000-03-04 19:42:04 +000029#include "mkdeps.h"
Zack Weinberg60893f42000-07-06 22:52:03 +000030#include "cppdefault.h"
Zack Weinberg6de1e2a1999-02-18 15:35:49 +000031
Neil Booth4a58aab2000-11-18 12:18:09 +000032/* Predefined symbols, built-in macros, and the default include path. */
Zack Weinberg6de1e2a1999-02-18 15:35:49 +000033
34#ifndef GET_ENV_PATH_LIST
35#define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
36#endif
37
Zack Weinberg88ae23e2000-03-08 23:35:19 +000038/* Windows does not natively support inodes, and neither does MSDOS.
39 Cygwin's emulation can generate non-unique inodes, so don't use it.
Kazu Hirataec5c56d2001-08-01 17:57:27 +000040 VMS has non-numeric inodes. */
Zack Weinberg88ae23e2000-03-08 23:35:19 +000041#ifdef VMS
Douglas B Ruppca47c892001-11-21 16:55:36 -050042# define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
43# define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
Zack Weinberg88ae23e2000-03-08 23:35:19 +000044#else
Michael Sokolov3943e752001-01-21 02:26:27 +000045# if (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
Douglas B Ruppca47c892001-11-21 16:55:36 -050046# define INO_T_EQ(A, B) 0
Michael Sokolov3943e752001-01-21 02:26:27 +000047# else
Douglas B Ruppca47c892001-11-21 16:55:36 -050048# define INO_T_EQ(A, B) ((A) == (B))
Michael Sokolov3943e752001-01-21 02:26:27 +000049# endif
Douglas B Ruppca47c892001-11-21 16:55:36 -050050# define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
Zack Weinberg88ae23e2000-03-08 23:35:19 +000051#endif
52
Neil Booth4a58aab2000-11-18 12:18:09 +000053/* Internal structures and prototypes. */
Zack Weinberg6de1e2a1999-02-18 15:35:49 +000054
Neil Booth4a58aab2000-11-18 12:18:09 +000055/* A `struct pending_option' remembers one -D, -A, -U, -include, or
56 -imacros switch. */
Kaveh R. Ghazi8be1ddc2000-03-12 13:55:52 +000057typedef void (* cl_directive_handler) PARAMS ((cpp_reader *, const char *));
Zack Weinberg0b22d651999-03-15 18:42:46 +000058struct pending_option
Zack Weinberg6de1e2a1999-02-18 15:35:49 +000059{
Zack Weinberg0b22d651999-03-15 18:42:46 +000060 struct pending_option *next;
Neil Booth7ceb3592000-03-11 00:49:44 +000061 const char *arg;
Neil Booth40eac642000-03-11 09:13:00 +000062 cl_directive_handler handler;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +000063};
Zack Weinberg0b22d651999-03-15 18:42:46 +000064
Zack Weinberg88ae23e2000-03-08 23:35:19 +000065/* The `pending' structure accumulates all the options that are not
Neil Boothf5e99452001-11-15 10:01:10 +000066 actually processed until we hit cpp_read_main_file. It consists of
Zack Weinberg88ae23e2000-03-08 23:35:19 +000067 several lists, one for each type of option. We keep both head and
Neil Booth4a58aab2000-11-18 12:18:09 +000068 tail pointers for quick insertion. */
Zack Weinberg88ae23e2000-03-08 23:35:19 +000069struct cpp_pending
70{
Neil Booth40eac642000-03-11 09:13:00 +000071 struct pending_option *directive_head, *directive_tail;
Zack Weinberg88ae23e2000-03-08 23:35:19 +000072
Neil Booth591e15a2001-03-02 07:35:12 +000073 struct search_path *quote_head, *quote_tail;
74 struct search_path *brack_head, *brack_tail;
75 struct search_path *systm_head, *systm_tail;
76 struct search_path *after_head, *after_tail;
Zack Weinberg88ae23e2000-03-08 23:35:19 +000077
78 struct pending_option *imacros_head, *imacros_tail;
79 struct pending_option *include_head, *include_tail;
80};
81
Zack Weinberg0b22d651999-03-15 18:42:46 +000082#ifdef __STDC__
83#define APPEND(pend, list, elt) \
84 do { if (!(pend)->list##_head) (pend)->list##_head = (elt); \
85 else (pend)->list##_tail->next = (elt); \
86 (pend)->list##_tail = (elt); \
87 } while (0)
88#else
89#define APPEND(pend, list, elt) \
90 do { if (!(pend)->list/**/_head) (pend)->list/**/_head = (elt); \
91 else (pend)->list/**/_tail->next = (elt); \
92 (pend)->list/**/_tail = (elt); \
93 } while (0)
94#endif
Zack Weinberg6de1e2a1999-02-18 15:35:49 +000095
Zack Weinberg6de1e2a1999-02-18 15:35:49 +000096static void print_help PARAMS ((void));
Zack Weinberg0b22d651999-03-15 18:42:46 +000097static void path_include PARAMS ((cpp_reader *,
Zack Weinberg0b22d651999-03-15 18:42:46 +000098 char *, int));
Neil Booth674c3b42001-01-08 18:52:09 +000099static void init_library PARAMS ((void));
Neil Booth7ca3d2b2001-01-07 11:15:13 +0000100static void init_builtins PARAMS ((cpp_reader *));
Zack Weinberg0b22d651999-03-15 18:42:46 +0000101static void append_include_chain PARAMS ((cpp_reader *,
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000102 char *, int, int));
Nathan Sidwell002ee642001-07-20 10:30:47 +0000103static struct search_path * remove_dup_dir PARAMS ((cpp_reader *,
Neil Booth591e15a2001-03-02 07:35:12 +0000104 struct search_path *));
Nathan Sidwell002ee642001-07-20 10:30:47 +0000105static struct search_path * remove_dup_dirs PARAMS ((cpp_reader *,
Neil Booth591e15a2001-03-02 07:35:12 +0000106 struct search_path *));
Zack Weinbergae796972000-03-31 23:16:11 +0000107static void merge_include_chains PARAMS ((cpp_reader *));
Neil Boothd7bc7a92001-08-21 06:20:18 +0000108static bool push_include PARAMS ((cpp_reader *,
109 struct pending_option *));
110static void free_chain PARAMS ((struct pending_option *));
Neil Boothdd07b882000-11-20 18:27:32 +0000111static void set_lang PARAMS ((cpp_reader *, enum c_lang));
Neil Booth7ca3d2b2001-01-07 11:15:13 +0000112static void init_dependency_output PARAMS ((cpp_reader *));
113static void init_standard_includes PARAMS ((cpp_reader *));
Neil Boothf5e99452001-11-15 10:01:10 +0000114static void read_original_filename PARAMS ((cpp_reader *));
Zack Weinberg2c0accc2000-07-15 19:29:14 +0000115static void new_pending_directive PARAMS ((struct cpp_pending *,
Neil Booth40eac642000-03-11 09:13:00 +0000116 const char *,
117 cl_directive_handler));
Neil Booth7ca3d2b2001-01-07 11:15:13 +0000118static void output_deps PARAMS ((cpp_reader *));
Zack Weinberge23c0ba2000-03-07 23:11:06 +0000119static int parse_option PARAMS ((const char *));
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000120
Zack Weinbergcb773842001-03-02 00:42:28 +0000121/* Fourth argument to append_include_chain: chain to use.
122 Note it's never asked to append to the quote chain. */
123enum { BRACKET = 0, SYSTEM, AFTER };
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000124
Neil Booth61d03462000-08-18 17:35:58 +0000125/* If we have designated initializers (GCC >2.7) these tables can be
126 initialized, constant data. Otherwise, they have to be filled in at
Zack Weinberg12cf91f2000-05-04 04:38:01 +0000127 runtime. */
Neil Booth61d03462000-08-18 17:35:58 +0000128#if HAVE_DESIGNATED_INITIALIZERS
Zack Weinberga9ae4481999-10-29 04:31:14 +0000129
Neil Booth4a58aab2000-11-18 12:18:09 +0000130#define init_trigraph_map() /* Nothing. */
Neil Booth61d03462000-08-18 17:35:58 +0000131#define TRIGRAPH_MAP \
Neil Booth562a5c22002-04-21 18:46:42 +0000132__extension__ const uchar _cpp_trigraph_map[UCHAR_MAX + 1] = {
Neil Booth61d03462000-08-18 17:35:58 +0000133
Zack Weinberga9ae4481999-10-29 04:31:14 +0000134#define END };
Zack Weinberg455d2582000-03-04 01:42:56 +0000135#define s(p, v) [p] = v,
Neil Booth61d03462000-08-18 17:35:58 +0000136
Zack Weinberga9ae4481999-10-29 04:31:14 +0000137#else
Neil Booth61d03462000-08-18 17:35:58 +0000138
Neil Booth562a5c22002-04-21 18:46:42 +0000139#define TRIGRAPH_MAP uchar _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
Neil Booth61d03462000-08-18 17:35:58 +0000140 static void init_trigraph_map PARAMS ((void)) { \
141 unsigned char *x = _cpp_trigraph_map;
142
Zack Weinbergae796972000-03-31 23:16:11 +0000143#define END }
Zack Weinberg455d2582000-03-04 01:42:56 +0000144#define s(p, v) x[p] = v;
Neil Booth61d03462000-08-18 17:35:58 +0000145
Zack Weinberga9ae4481999-10-29 04:31:14 +0000146#endif
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000147
Neil Booth61d03462000-08-18 17:35:58 +0000148TRIGRAPH_MAP
149 s('=', '#') s(')', ']') s('!', '|')
150 s('(', '[') s('\'', '^') s('>', '}')
151 s('/', '\\') s('<', '{') s('-', '~')
152END
153
Zack Weinberga9ae4481999-10-29 04:31:14 +0000154#undef s
Zack Weinberg455d2582000-03-04 01:42:56 +0000155#undef END
Neil Booth61d03462000-08-18 17:35:58 +0000156#undef TRIGRAPH_MAP
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000157
158/* Given a colon-separated list of file names PATH,
159 add all the names to the search path for include files. */
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000160static void
Neil Boothe33f6252000-08-17 17:58:24 +0000161path_include (pfile, list, path)
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000162 cpp_reader *pfile;
Zack Weinberg0b22d651999-03-15 18:42:46 +0000163 char *list;
164 int path;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000165{
Zack Weinberg0b22d651999-03-15 18:42:46 +0000166 char *p, *q, *name;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000167
Zack Weinberg0b22d651999-03-15 18:42:46 +0000168 p = list;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000169
Zack Weinberg0b22d651999-03-15 18:42:46 +0000170 do
171 {
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000172 /* Find the end of this name. */
Zack Weinberg0b22d651999-03-15 18:42:46 +0000173 q = p;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000174 while (*q != 0 && *q != PATH_SEPARATOR) q++;
Zack Weinberg0b22d651999-03-15 18:42:46 +0000175 if (q == p)
176 {
177 /* An empty name in the path stands for the current directory. */
178 name = (char *) xmalloc (2);
179 name[0] = '.';
180 name[1] = 0;
181 }
182 else
183 {
184 /* Otherwise use the directory that is named. */
185 name = (char *) xmalloc (q - p + 1);
186 memcpy (name, p, q - p);
187 name[q - p] = 0;
188 }
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000189
Neil Boothe33f6252000-08-17 17:58:24 +0000190 append_include_chain (pfile, name, path, 0);
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000191
192 /* Advance past this name. */
Zack Weinberg0b22d651999-03-15 18:42:46 +0000193 if (*q == 0)
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000194 break;
Zack Weinberg0b22d651999-03-15 18:42:46 +0000195 p = q + 1;
196 }
197 while (1);
198}
199
Neil Boothbef985f2001-08-11 12:37:19 +0000200/* Append DIR to include path PATH. DIR must be allocated on the
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000201 heap; this routine takes responsibility for freeing it. CXX_AWARE
202 is non-zero if the header contains extern "C" guards for C++,
203 otherwise it is zero. */
Zack Weinberg0b22d651999-03-15 18:42:46 +0000204static void
Neil Boothe33f6252000-08-17 17:58:24 +0000205append_include_chain (pfile, dir, path, cxx_aware)
Zack Weinberg0b22d651999-03-15 18:42:46 +0000206 cpp_reader *pfile;
Zack Weinberg0b22d651999-03-15 18:42:46 +0000207 char *dir;
208 int path;
Zack Weinberg7d4918a2001-02-07 18:32:42 +0000209 int cxx_aware ATTRIBUTE_UNUSED;
Zack Weinberg0b22d651999-03-15 18:42:46 +0000210{
Neil Boothe33f6252000-08-17 17:58:24 +0000211 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
Neil Booth591e15a2001-03-02 07:35:12 +0000212 struct search_path *new;
Zack Weinberg0b22d651999-03-15 18:42:46 +0000213 struct stat st;
214 unsigned int len;
215
Neil Boothf9200da2001-04-06 07:22:01 +0000216 if (*dir == '\0')
Neil Boothbef985f2001-08-11 12:37:19 +0000217 {
218 free (dir);
219 dir = xstrdup (".");
220 }
Zack Weinbergb0699da2000-03-07 20:58:47 +0000221 _cpp_simplify_pathname (dir);
Neil Boothbef985f2001-08-11 12:37:19 +0000222
Zack Weinberg0b22d651999-03-15 18:42:46 +0000223 if (stat (dir, &st))
224 {
Neil Boothf9200da2001-04-06 07:22:01 +0000225 /* Dirs that don't exist are silently ignored. */
Zack Weinberg0b22d651999-03-15 18:42:46 +0000226 if (errno != ENOENT)
Neil Boothebef4e82002-04-14 18:42:47 +0000227 cpp_errno (pfile, DL_ERROR, dir);
Zack Weinbergae796972000-03-31 23:16:11 +0000228 else if (CPP_OPTION (pfile, verbose))
Zack Weinberg041c3192000-07-04 01:58:21 +0000229 fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"), dir);
Neil Boothbef985f2001-08-11 12:37:19 +0000230 free (dir);
Zack Weinberg0b22d651999-03-15 18:42:46 +0000231 return;
232 }
233
234 if (!S_ISDIR (st.st_mode))
235 {
Neil Boothebef4e82002-04-14 18:42:47 +0000236 cpp_error_with_line (pfile, DL_ERROR, 0, 0, "%s: Not a directory", dir);
Neil Boothbef985f2001-08-11 12:37:19 +0000237 free (dir);
Zack Weinberg0b22d651999-03-15 18:42:46 +0000238 return;
239 }
240
241 len = strlen (dir);
242 if (len > pfile->max_include_len)
243 pfile->max_include_len = len;
Zack Weinbergae796972000-03-31 23:16:11 +0000244
Neil Booth591e15a2001-03-02 07:35:12 +0000245 new = (struct search_path *) xmalloc (sizeof (struct search_path));
Zack Weinberg0b22d651999-03-15 18:42:46 +0000246 new->name = dir;
Neil Booth591e15a2001-03-02 07:35:12 +0000247 new->len = len;
Douglas B Ruppca47c892001-11-21 16:55:36 -0500248 INO_T_COPY (new->ino, st.st_ino);
Zack Weinberg0b22d651999-03-15 18:42:46 +0000249 new->dev = st.st_dev;
Christopher Faylor4737b272001-03-02 17:20:30 +0000250 /* Both systm and after include file lists should be treated as system
251 include files since these two lists are really just a concatenation
Kazu Hirataec5c56d2001-08-01 17:57:27 +0000252 of one "system" list. */
Christopher Faylor4737b272001-03-02 17:20:30 +0000253 if (path == SYSTEM || path == AFTER)
Jakub Jelinek52b357e2001-02-03 21:48:32 +0100254#ifdef NO_IMPLICIT_EXTERN_C
255 new->sysp = 1;
256#else
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000257 new->sysp = cxx_aware ? 1 : 2;
Jakub Jelinek52b357e2001-02-03 21:48:32 +0100258#endif
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000259 else
260 new->sysp = 0;
Zack Weinberg0b22d651999-03-15 18:42:46 +0000261 new->name_map = NULL;
Dave Brolley503cb431999-09-13 16:58:44 +0000262 new->next = NULL;
Zack Weinberg0b22d651999-03-15 18:42:46 +0000263
264 switch (path)
265 {
Zack Weinberg0b22d651999-03-15 18:42:46 +0000266 case BRACKET: APPEND (pend, brack, new); break;
267 case SYSTEM: APPEND (pend, systm, new); break;
268 case AFTER: APPEND (pend, after, new); break;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000269 }
270}
271
Neil Boothdd69c712000-08-17 18:03:59 +0000272/* Handle a duplicated include path. PREV is the link in the chain
273 before the duplicate. The duplicate is removed from the chain and
274 freed. Returns PREV. */
Nathan Sidwell002ee642001-07-20 10:30:47 +0000275static struct search_path *
Neil Boothdd69c712000-08-17 18:03:59 +0000276remove_dup_dir (pfile, prev)
277 cpp_reader *pfile;
Neil Booth591e15a2001-03-02 07:35:12 +0000278 struct search_path *prev;
Neil Boothdd69c712000-08-17 18:03:59 +0000279{
Neil Booth591e15a2001-03-02 07:35:12 +0000280 struct search_path *cur = prev->next;
Neil Boothdd69c712000-08-17 18:03:59 +0000281
282 if (CPP_OPTION (pfile, verbose))
283 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), cur->name);
284
285 prev->next = cur->next;
Neil Booth591e15a2001-03-02 07:35:12 +0000286 free ((PTR) cur->name);
Neil Boothdd69c712000-08-17 18:03:59 +0000287 free (cur);
288
289 return prev;
290}
291
292/* Remove duplicate directories from a chain. Returns the tail of the
293 chain, or NULL if the chain is empty. This algorithm is quadratic
294 in the number of -I switches, which is acceptable since there
295 aren't usually that many of them. */
Nathan Sidwell002ee642001-07-20 10:30:47 +0000296static struct search_path *
Neil Boothdd69c712000-08-17 18:03:59 +0000297remove_dup_dirs (pfile, head)
298 cpp_reader *pfile;
Neil Booth591e15a2001-03-02 07:35:12 +0000299 struct search_path *head;
Neil Boothdd69c712000-08-17 18:03:59 +0000300{
Neil Booth591e15a2001-03-02 07:35:12 +0000301 struct search_path *prev = NULL, *cur, *other;
Neil Boothdd69c712000-08-17 18:03:59 +0000302
303 for (cur = head; cur; cur = cur->next)
304 {
305 for (other = head; other != cur; other = other->next)
306 if (INO_T_EQ (cur->ino, other->ino) && cur->dev == other->dev)
307 {
Nathan Sidwell002ee642001-07-20 10:30:47 +0000308 if (cur->sysp && !other->sysp)
Nathan Sidwelldbead492001-07-04 20:06:27 +0000309 {
Neil Boothebef4e82002-04-14 18:42:47 +0000310 cpp_error (pfile, DL_WARNING,
311 "changing search order for system directory \"%s\"",
312 cur->name);
Nathan Sidwelldbead492001-07-04 20:06:27 +0000313 if (strcmp (cur->name, other->name))
Neil Boothebef4e82002-04-14 18:42:47 +0000314 cpp_error (pfile, DL_WARNING,
315 " as it is the same as non-system directory \"%s\"",
316 other->name);
Nathan Sidwelldbead492001-07-04 20:06:27 +0000317 else
Neil Boothebef4e82002-04-14 18:42:47 +0000318 cpp_error (pfile, DL_WARNING,
319 " as it has already been specified as a non-system directory");
Nathan Sidwelldbead492001-07-04 20:06:27 +0000320 }
Neil Boothdd69c712000-08-17 18:03:59 +0000321 cur = remove_dup_dir (pfile, prev);
322 break;
323 }
324 prev = cur;
325 }
326
327 return prev;
328}
329
Zack Weinberg88ae23e2000-03-08 23:35:19 +0000330/* Merge the four include chains together in the order quote, bracket,
331 system, after. Remove duplicate dirs (as determined by
332 INO_T_EQ()). The system_include and after_include chains are never
333 referred to again after this function; all access is through the
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000334 bracket_include path. */
Zack Weinberg88ae23e2000-03-08 23:35:19 +0000335static void
Zack Weinbergae796972000-03-31 23:16:11 +0000336merge_include_chains (pfile)
337 cpp_reader *pfile;
Zack Weinberg88ae23e2000-03-08 23:35:19 +0000338{
Neil Booth591e15a2001-03-02 07:35:12 +0000339 struct search_path *quote, *brack, *systm, *qtail;
Zack Weinberg88ae23e2000-03-08 23:35:19 +0000340
Zack Weinbergae796972000-03-31 23:16:11 +0000341 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
Zack Weinberg88ae23e2000-03-08 23:35:19 +0000342
Zack Weinbergae796972000-03-31 23:16:11 +0000343 quote = pend->quote_head;
344 brack = pend->brack_head;
345 systm = pend->systm_head;
Neil Boothdd69c712000-08-17 18:03:59 +0000346 qtail = pend->quote_tail;
Zack Weinberg88ae23e2000-03-08 23:35:19 +0000347
Neil Boothdd69c712000-08-17 18:03:59 +0000348 /* Paste together bracket, system, and after include chains. */
349 if (systm)
350 pend->systm_tail->next = pend->after_head;
Zack Weinberg88ae23e2000-03-08 23:35:19 +0000351 else
Neil Boothdd69c712000-08-17 18:03:59 +0000352 systm = pend->after_head;
353
354 if (brack)
355 pend->brack_tail->next = systm;
Zack Weinberg88ae23e2000-03-08 23:35:19 +0000356 else
357 brack = systm;
358
Neil Boothdd69c712000-08-17 18:03:59 +0000359 /* This is a bit tricky. First we drop dupes from the quote-include
360 list. Then we drop dupes from the bracket-include list.
361 Finally, if qtail and brack are the same directory, we cut out
Zack Weinbergcb773842001-03-02 00:42:28 +0000362 brack and move brack up to point to qtail.
Zack Weinberg88ae23e2000-03-08 23:35:19 +0000363
364 We can't just merge the lists and then uniquify them because
365 then we may lose directories from the <> search path that should
366 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
367 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
Neil Boothdd69c712000-08-17 18:03:59 +0000368 -Ibar -I- -Ifoo -Iquux. */
Zack Weinberg88ae23e2000-03-08 23:35:19 +0000369
Neil Boothdd69c712000-08-17 18:03:59 +0000370 remove_dup_dirs (pfile, brack);
371 qtail = remove_dup_dirs (pfile, quote);
Zack Weinberg88ae23e2000-03-08 23:35:19 +0000372
373 if (quote)
374 {
Neil Boothdd69c712000-08-17 18:03:59 +0000375 qtail->next = brack;
376
377 /* If brack == qtail, remove brack as it's simpler. */
Zack Weinbergafb58282002-02-17 02:54:20 +0000378 if (brack && INO_T_EQ (qtail->ino, brack->ino)
379 && qtail->dev == brack->dev)
Neil Boothdd69c712000-08-17 18:03:59 +0000380 brack = remove_dup_dir (pfile, qtail);
Zack Weinberg88ae23e2000-03-08 23:35:19 +0000381 }
382 else
Neil Boothbef985f2001-08-11 12:37:19 +0000383 quote = brack;
Zack Weinberg88ae23e2000-03-08 23:35:19 +0000384
Zack Weinbergae796972000-03-31 23:16:11 +0000385 CPP_OPTION (pfile, quote_include) = quote;
386 CPP_OPTION (pfile, bracket_include) = brack;
Zack Weinberg88ae23e2000-03-08 23:35:19 +0000387}
388
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000389/* A set of booleans indicating what CPP features each source language
390 requires. */
Zack Weinberga01eb542001-07-02 18:55:35 +0000391struct lang_flags
392{
393 char c99;
Zack Weinberga01eb542001-07-02 18:55:35 +0000394 char cplusplus;
395 char extended_numbers;
396 char trigraphs;
397 char dollars_in_ident;
398 char cplusplus_comments;
399 char digraphs;
400};
401
402/* ??? Enable $ in identifiers in assembly? */
403static const struct lang_flags lang_defaults[] =
Neil Boothbdcae022002-05-17 19:37:43 +0000404{ /* c99 c++ xnum trig dollar c++comm digr */
405 /* GNUC89 */ { 0, 0, 1, 0, 1, 1, 1 },
406 /* GNUC99 */ { 1, 0, 1, 0, 1, 1, 1 },
407 /* STDC89 */ { 0, 0, 0, 1, 0, 0, 0 },
408 /* STDC94 */ { 0, 0, 0, 1, 0, 0, 1 },
409 /* STDC99 */ { 1, 0, 1, 1, 0, 1, 1 },
410 /* GNUCXX */ { 0, 1, 1, 0, 1, 1, 1 },
411 /* CXX98 */ { 0, 1, 1, 1, 0, 1, 1 },
412 /* ASM */ { 0, 0, 1, 0, 0, 1, 0 }
Zack Weinberga01eb542001-07-02 18:55:35 +0000413};
414
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000415/* Sets internal flags correctly for a given language. */
Neil Boothdd07b882000-11-20 18:27:32 +0000416static void
417set_lang (pfile, lang)
418 cpp_reader *pfile;
419 enum c_lang lang;
420{
Zack Weinberga01eb542001-07-02 18:55:35 +0000421 const struct lang_flags *l = &lang_defaults[(int) lang];
422
Neil Boothbdb05a72000-11-26 17:31:13 +0000423 CPP_OPTION (pfile, lang) = lang;
Neil Boothdd07b882000-11-20 18:27:32 +0000424
Zack Weinberga01eb542001-07-02 18:55:35 +0000425 CPP_OPTION (pfile, c99) = l->c99;
Zack Weinberga01eb542001-07-02 18:55:35 +0000426 CPP_OPTION (pfile, cplusplus) = l->cplusplus;
427 CPP_OPTION (pfile, extended_numbers) = l->extended_numbers;
428 CPP_OPTION (pfile, trigraphs) = l->trigraphs;
429 CPP_OPTION (pfile, dollars_in_ident) = l->dollars_in_ident;
430 CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments;
431 CPP_OPTION (pfile, digraphs) = l->digraphs;
Neil Boothdd07b882000-11-20 18:27:32 +0000432}
433
Neil Boothcf44ea52000-11-28 21:13:35 +0000434#ifdef HOST_EBCDIC
Neil Booth7ca3d2b2001-01-07 11:15:13 +0000435static int opt_comp PARAMS ((const void *, const void *));
436
437/* Run-time sorting of options array. */
438static int
439opt_comp (p1, p2)
440 const void *p1, *p2;
441{
442 return strcmp (((struct cl_option *) p1)->opt_text,
443 ((struct cl_option *) p2)->opt_text);
444}
Neil Boothcf44ea52000-11-28 21:13:35 +0000445#endif
446
Neil Booth7ca3d2b2001-01-07 11:15:13 +0000447/* init initializes library global state. It might not need to
448 do anything depending on the platform and compiler. */
Neil Booth7ca3d2b2001-01-07 11:15:13 +0000449static void
Neil Booth674c3b42001-01-08 18:52:09 +0000450init_library ()
Neil Booth7ca3d2b2001-01-07 11:15:13 +0000451{
452 static int initialized = 0;
453
454 if (! initialized)
455 {
456 initialized = 1;
457
458#ifdef HOST_EBCDIC
459 /* For non-ASCII hosts, the cl_options array needs to be sorted at
460 runtime. */
461 qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
462#endif
463
464 /* Set up the trigraph map. This doesn't need to do anything if
465 we were compiled with a compiler that supports C99 designated
466 initializers. */
467 init_trigraph_map ();
468 }
Neil Boothcf44ea52000-11-28 21:13:35 +0000469}
470
Kazu Hirataec5c56d2001-08-01 17:57:27 +0000471/* Initialize a cpp_reader structure. */
Neil Boothcf44ea52000-11-28 21:13:35 +0000472cpp_reader *
Neil Boothf5e99452001-11-15 10:01:10 +0000473cpp_create_reader (lang)
Neil Boothdd07b882000-11-20 18:27:32 +0000474 enum c_lang lang;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000475{
Neil Booth7ca3d2b2001-01-07 11:15:13 +0000476 cpp_reader *pfile;
Neil Booth93c803682000-10-28 17:59:06 +0000477
Neil Boothcf44ea52000-11-28 21:13:35 +0000478 /* Initialise this instance of the library if it hasn't been already. */
Neil Booth674c3b42001-01-08 18:52:09 +0000479 init_library ();
Neil Booth7ca3d2b2001-01-07 11:15:13 +0000480
481 pfile = (cpp_reader *) xcalloc (1, sizeof (cpp_reader));
Neil Booth93c803682000-10-28 17:59:06 +0000482
Neil Boothc740cee2001-02-20 22:52:11 +0000483 set_lang (pfile, lang);
Zack Weinbergae796972000-03-31 23:16:11 +0000484 CPP_OPTION (pfile, warn_import) = 1;
Neil Bootha5a49442002-05-06 22:53:10 +0000485 CPP_OPTION (pfile, warn_multichar) = 1;
Zack Weinbergae796972000-03-31 23:16:11 +0000486 CPP_OPTION (pfile, discard_comments) = 1;
Jason Thorpe477cdac2002-04-07 03:12:23 +0000487 CPP_OPTION (pfile, discard_comments_in_macro_exp) = 1;
Zack Weinbergae796972000-03-31 23:16:11 +0000488 CPP_OPTION (pfile, show_column) = 1;
Neil Booth6ab3e7d2000-05-18 11:09:27 +0000489 CPP_OPTION (pfile, tabstop) = 8;
Jakub Jelinekbe768052000-12-15 16:49:28 +0100490 CPP_OPTION (pfile, operator_names) = 1;
Phil Edwards909de5d2002-03-22 21:59:04 +0000491 CPP_OPTION (pfile, warn_endif_labels) = 1;
Zack Weinbergae796972000-03-31 23:16:11 +0000492
493 CPP_OPTION (pfile, pending) =
494 (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
495
Neil Booth2443d4e2002-05-05 17:05:09 +0000496 /* Default CPP arithmetic to something sensible for the host for the
497 benefit of dumb users like fix-header. */
Neil Booth4268e8b2002-05-04 07:30:32 +0000498#define BITS_PER_HOST_WIDEST_INT (CHAR_BIT * sizeof (HOST_WIDEST_INT))
499 CPP_OPTION (pfile, precision) = BITS_PER_HOST_WIDEST_INT;
Neil Booth2443d4e2002-05-05 17:05:09 +0000500 CPP_OPTION (pfile, char_precision) = CHAR_BIT;
501 CPP_OPTION (pfile, wchar_precision) = CHAR_BIT * sizeof (int);
502 CPP_OPTION (pfile, int_precision) = CHAR_BIT * sizeof (int);
Neil Booth44a147a2002-05-07 21:07:24 +0000503 CPP_OPTION (pfile, unsigned_char) = !DEFAULT_SIGNED_CHAR;
504 CPP_OPTION (pfile, unsigned_wchar) = 1;
Neil Booth4268e8b2002-05-04 07:30:32 +0000505
Neil Boothf7114e12001-01-06 00:15:29 +0000506 /* It's simplest to just create this struct whether or not it will
507 be needed. */
508 pfile->deps = deps_init ();
509
Neil Booth50410422001-09-15 10:18:03 +0000510 /* Initialise the line map. Start at logical line 1, so we can use
511 a line number of zero for special states. */
Neil Boothd82fc102001-08-02 23:03:31 +0000512 init_line_maps (&pfile->line_maps);
Neil Booth004cb262002-05-17 20:16:48 +0000513 pfile->trad_line = pfile->line = 1;
Neil Boothd82fc102001-08-02 23:03:31 +0000514
Neil Booth4a58aab2000-11-18 12:18:09 +0000515 /* Initialize lexer state. */
Neil Booth93c803682000-10-28 17:59:06 +0000516 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
Zack Weinberg3cb553b2000-08-20 21:36:18 +0000517
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000518 /* Set up static tokens. */
Neil Booth93c803682000-10-28 17:59:06 +0000519 pfile->date.type = CPP_EOF;
Neil Booth4ed5bcf2001-09-24 22:53:12 +0000520 pfile->avoid_paste.type = CPP_PADDING;
521 pfile->avoid_paste.val.source = NULL;
522 pfile->eof.type = CPP_EOF;
523 pfile->eof.flags = 0;
Neil Booth93c803682000-10-28 17:59:06 +0000524
Neil Booth5fddcff2001-09-11 07:00:12 +0000525 /* Create a token buffer for the lexer. */
526 _cpp_init_tokenrun (&pfile->base_run, 250);
527 pfile->cur_run = &pfile->base_run;
528 pfile->cur_token = pfile->base_run.base;
Neil Booth5fddcff2001-09-11 07:00:12 +0000529
Neil Booth93c803682000-10-28 17:59:06 +0000530 /* Initialise the base context. */
531 pfile->context = &pfile->base_context;
532 pfile->base_context.macro = 0;
533 pfile->base_context.prev = pfile->base_context.next = 0;
534
Neil Booth8c3b2692001-09-30 10:03:11 +0000535 /* Aligned and unaligned storage. */
536 pfile->a_buff = _cpp_get_buff (pfile, 0);
Neil Boothece54d52001-09-28 09:40:22 +0000537 pfile->u_buff = _cpp_get_buff (pfile, 0);
Neil Booth93c803682000-10-28 17:59:06 +0000538
Neil Booth87ed1092002-04-28 19:42:54 +0000539 /* The expression parser stack. */
540 _cpp_expand_op_stack (pfile);
541
Neil Booth2a967f32001-05-20 06:26:45 +0000542 /* Initialise the buffer obstack. */
543 gcc_obstack_init (&pfile->buffer_ob);
544
Zack Weinbergc71f8352000-07-05 05:33:57 +0000545 _cpp_init_includes (pfile);
Neil Boothcf44ea52000-11-28 21:13:35 +0000546
547 return pfile;
Zack Weinbergf2d5f0c2000-04-14 23:29:45 +0000548}
549
Neil Booth400023a2001-01-14 22:00:20 +0000550/* Free resources used by PFILE. Accessing PFILE after this function
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000551 returns leads to undefined behaviour. Returns the error count. */
Neil Booth400023a2001-01-14 22:00:20 +0000552int
553cpp_destroy (pfile)
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000554 cpp_reader *pfile;
555{
Neil Booth400023a2001-01-14 22:00:20 +0000556 int result;
Neil Booth591e15a2001-03-02 07:35:12 +0000557 struct search_path *dir, *dirn;
Neil Booth93c803682000-10-28 17:59:06 +0000558 cpp_context *context, *contextn;
Neil Booth50410422001-09-15 10:18:03 +0000559 tokenrun *run, *runn;
Neil Booth709e9e52000-08-17 18:01:43 +0000560
Neil Boothaf0d16c2002-04-22 17:48:02 +0000561 free_chain (CPP_OPTION (pfile, pending)->include_head);
562 free (CPP_OPTION (pfile, pending));
Neil Booth87ed1092002-04-28 19:42:54 +0000563 free (pfile->op_stack);
Neil Boothaf0d16c2002-04-22 17:48:02 +0000564
Zack Weinberg38b24ee2000-03-08 20:37:23 +0000565 while (CPP_BUFFER (pfile) != NULL)
Neil Boothef6e9582001-08-04 12:01:59 +0000566 _cpp_pop_buffer (pfile);
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000567
Neil Booth004cb262002-05-17 20:16:48 +0000568 if (pfile->trad_out_base)
569 free (pfile->trad_out_base);
570
Neil Booth93c803682000-10-28 17:59:06 +0000571 if (pfile->macro_buffer)
Alexandre Oliva4b49c362001-01-09 09:30:43 +0000572 {
573 free ((PTR) pfile->macro_buffer);
574 pfile->macro_buffer = NULL;
575 pfile->macro_buffer_len = 0;
576 }
Neil Booth93c803682000-10-28 17:59:06 +0000577
Neil Boothf7114e12001-01-06 00:15:29 +0000578 deps_free (pfile->deps);
Neil Booth2a967f32001-05-20 06:26:45 +0000579 obstack_free (&pfile->buffer_ob, 0);
Zack Weinberg49e6c082000-03-04 19:42:04 +0000580
Neil Booth2a967f32001-05-20 06:26:45 +0000581 _cpp_destroy_hashtable (pfile);
Zack Weinbergbfb9dc72000-07-08 19:00:39 +0000582 _cpp_cleanup_includes (pfile);
Neil Booth93c803682000-10-28 17:59:06 +0000583
Neil Booth8c3b2692001-09-30 10:03:11 +0000584 _cpp_free_buff (pfile->a_buff);
Neil Boothece54d52001-09-28 09:40:22 +0000585 _cpp_free_buff (pfile->u_buff);
Neil Boothb8af0ca2001-09-26 17:52:50 +0000586 _cpp_free_buff (pfile->free_buffs);
Neil Booth93c803682000-10-28 17:59:06 +0000587
Neil Booth50410422001-09-15 10:18:03 +0000588 for (run = &pfile->base_run; run; run = runn)
589 {
590 runn = run->next;
591 free (run->base);
592 if (run != &pfile->base_run)
593 free (run);
594 }
595
Neil Booth93c803682000-10-28 17:59:06 +0000596 for (dir = CPP_OPTION (pfile, quote_include); dir; dir = dirn)
Neil Booth709e9e52000-08-17 18:01:43 +0000597 {
Neil Booth93c803682000-10-28 17:59:06 +0000598 dirn = dir->next;
Neil Booth591e15a2001-03-02 07:35:12 +0000599 free ((PTR) dir->name);
Neil Booth709e9e52000-08-17 18:01:43 +0000600 free (dir);
601 }
Neil Booth93c803682000-10-28 17:59:06 +0000602
603 for (context = pfile->base_context.next; context; context = contextn)
604 {
605 contextn = context->next;
606 free (context);
607 }
Neil Booth400023a2001-01-14 22:00:20 +0000608
Neil Boothd82fc102001-08-02 23:03:31 +0000609 free_line_maps (&pfile->line_maps);
610
Neil Booth400023a2001-01-14 22:00:20 +0000611 result = pfile->errors;
612 free (pfile);
613
614 return result;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000615}
616
Neil Booth93c803682000-10-28 17:59:06 +0000617/* This structure defines one built-in identifier. A node will be
Zack Weinbergf24a1532002-05-18 00:43:13 +0000618 entered in the hash table under the name NAME, with value VALUE.
Zack Weinberg92936ec2000-07-19 20:18:08 +0000619
Zack Weinbergf24a1532002-05-18 00:43:13 +0000620 There are two tables of these. builtin_array holds all the
621 "builtin" macros: these are handled by builtin_macro() in
622 cppmacro.c. Builtin is somewhat of a misnomer -- the property of
623 interest is that these macros require special code to compute their
624 expansions. The value is a "builtin_type" enumerator.
625
626 operator_array holds the C++ named operators. These are keywords
627 which act as aliases for punctuators. In C++, they cannot be
628 altered through #define, and #if recognizes them as operators. In
629 C, these are not entered into the hash table at all (but see
630 <iso646.h>). The value is a token-type enumerator. */
Zack Weinberga9ae4481999-10-29 04:31:14 +0000631struct builtin
632{
Neil Booth562a5c22002-04-21 18:46:42 +0000633 const uchar *name;
Neil Booth93c803682000-10-28 17:59:06 +0000634 unsigned short len;
Zack Weinbergf24a1532002-05-18 00:43:13 +0000635 unsigned short value;
Zack Weinberga9ae4481999-10-29 04:31:14 +0000636};
Zack Weinberga9ae4481999-10-29 04:31:14 +0000637
Zack Weinbergf24a1532002-05-18 00:43:13 +0000638#define B(n, t) { DSC(n), t }
Zack Weinberga9ae4481999-10-29 04:31:14 +0000639static const struct builtin builtin_array[] =
640{
Neil Booth93c803682000-10-28 17:59:06 +0000641 B("__TIME__", BT_TIME),
642 B("__DATE__", BT_DATE),
643 B("__FILE__", BT_FILE),
644 B("__BASE_FILE__", BT_BASE_FILE),
645 B("__LINE__", BT_SPECLINE),
646 B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL),
Neil Booth644edda2001-10-02 12:57:24 +0000647 B("_Pragma", BT_PRAGMA),
Neil Booth618cdda2001-02-25 09:43:03 +0000648 B("__STDC__", BT_STDC),
Zack Weinbergf24a1532002-05-18 00:43:13 +0000649};
Zack Weinberg92936ec2000-07-19 20:18:08 +0000650
Zack Weinbergf24a1532002-05-18 00:43:13 +0000651static const struct builtin operator_array[] =
652{
653 B("and", CPP_AND_AND),
654 B("and_eq", CPP_AND_EQ),
655 B("bitand", CPP_AND),
656 B("bitor", CPP_OR),
657 B("compl", CPP_COMPL),
658 B("not", CPP_NOT),
659 B("not_eq", CPP_NOT_EQ),
660 B("or", CPP_OR_OR),
661 B("or_eq", CPP_OR_EQ),
662 B("xor", CPP_XOR),
663 B("xor_eq", CPP_XOR_EQ)
Zack Weinberga9ae4481999-10-29 04:31:14 +0000664};
Zack Weinberg12cf91f2000-05-04 04:38:01 +0000665#undef B
Zack Weinberga9ae4481999-10-29 04:31:14 +0000666
Neil Boothf5e99452001-11-15 10:01:10 +0000667/* Subroutine of cpp_read_main_file; reads the builtins table above and
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000668 enters them, and language-specific macros, into the hash table. */
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000669static void
Neil Booth7ca3d2b2001-01-07 11:15:13 +0000670init_builtins (pfile)
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000671 cpp_reader *pfile;
672{
Zack Weinberga9ae4481999-10-29 04:31:14 +0000673 const struct builtin *b;
Neil Booth771c4df2000-09-07 20:31:06 +0000674
Zack Weinbergf24a1532002-05-18 00:43:13 +0000675 for(b = builtin_array;
676 b < (builtin_array + ARRAY_SIZE (builtin_array));
677 b++)
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000678 {
Zack Weinbergf24a1532002-05-18 00:43:13 +0000679 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
680 hp->type = NT_MACRO;
681 hp->flags |= NODE_BUILTIN | NODE_WARN;
682 hp->value.builtin = b->value;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000683 }
Neil Boothc740cee2001-02-20 22:52:11 +0000684
Zack Weinbergf24a1532002-05-18 00:43:13 +0000685 if (CPP_OPTION (pfile, cplusplus) && CPP_OPTION (pfile, operator_names))
686 for (b = operator_array;
687 b < (operator_array + ARRAY_SIZE (operator_array));
688 b++)
689 {
690 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
691 hp->flags |= NODE_OPERATOR;
692 hp->value.operator = b->value;
693 }
694
Neil Boothc740cee2001-02-20 22:52:11 +0000695 if (CPP_OPTION (pfile, cplusplus))
Neil Booth3d90d292002-05-04 20:15:00 +0000696 _cpp_define_builtin (pfile, "__cplusplus 1");
697
Neil Boothc740cee2001-02-20 22:52:11 +0000698 if (CPP_OPTION (pfile, objc))
699 _cpp_define_builtin (pfile, "__OBJC__ 1");
700
701 if (CPP_OPTION (pfile, lang) == CLK_STDC94)
702 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
703 else if (CPP_OPTION (pfile, c99))
704 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
705
Neil Booth44a147a2002-05-07 21:07:24 +0000706 if (CPP_OPTION (pfile, unsigned_char))
Neil Booth0fef3fd2002-02-02 18:56:37 +0000707 _cpp_define_builtin (pfile, "__CHAR_UNSIGNED__ 1");
708
Neil Boothc740cee2001-02-20 22:52:11 +0000709 if (CPP_OPTION (pfile, lang) == CLK_STDC89
710 || CPP_OPTION (pfile, lang) == CLK_STDC94
711 || CPP_OPTION (pfile, lang) == CLK_STDC99)
712 _cpp_define_builtin (pfile, "__STRICT_ANSI__ 1");
713 else if (CPP_OPTION (pfile, lang) == CLK_ASM)
714 _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
Neil Booth3d90d292002-05-04 20:15:00 +0000715
716 if (pfile->cb.register_builtins)
717 (*pfile->cb.register_builtins) (pfile);
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000718}
719
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000720/* And another subroutine. This one sets up the standard include path. */
721static void
Neil Booth7ca3d2b2001-01-07 11:15:13 +0000722init_standard_includes (pfile)
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000723 cpp_reader *pfile;
724{
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000725 char *path;
Zack Weinberg455d2582000-03-04 01:42:56 +0000726 const struct default_include *p;
Zack Weinbergae796972000-03-31 23:16:11 +0000727 const char *specd_prefix = CPP_OPTION (pfile, include_prefix);
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000728
729 /* Several environment variables may add to the include search path.
730 CPATH specifies an additional list of directories to be searched
731 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
732 etc. specify an additional list of directories to be searched as
733 if specified with -isystem, for the language indicated. */
734
735 GET_ENV_PATH_LIST (path, "CPATH");
736 if (path != 0 && *path != 0)
Neil Boothe33f6252000-08-17 17:58:24 +0000737 path_include (pfile, path, BRACKET);
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000738
Zack Weinbergae796972000-03-31 23:16:11 +0000739 switch ((CPP_OPTION (pfile, objc) << 1) + CPP_OPTION (pfile, cplusplus))
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000740 {
741 case 0:
742 GET_ENV_PATH_LIST (path, "C_INCLUDE_PATH");
743 break;
744 case 1:
745 GET_ENV_PATH_LIST (path, "CPLUS_INCLUDE_PATH");
746 break;
747 case 2:
748 GET_ENV_PATH_LIST (path, "OBJC_INCLUDE_PATH");
749 break;
750 case 3:
751 GET_ENV_PATH_LIST (path, "OBJCPLUS_INCLUDE_PATH");
752 break;
753 }
754 if (path != 0 && *path != 0)
Neil Boothe33f6252000-08-17 17:58:24 +0000755 path_include (pfile, path, SYSTEM);
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000756
757 /* Search "translated" versions of GNU directories.
758 These have /usr/local/lib/gcc... replaced by specd_prefix. */
Zack Weinberg60893f42000-07-06 22:52:03 +0000759 if (specd_prefix != 0 && cpp_GCC_INCLUDE_DIR_len)
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000760 {
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000761 /* Remove the `include' from /usr/local/lib/gcc.../include.
Kazu Hirataec5c56d2001-08-01 17:57:27 +0000762 GCC_INCLUDE_DIR will always end in /include. */
Zack Weinberg60893f42000-07-06 22:52:03 +0000763 int default_len = cpp_GCC_INCLUDE_DIR_len;
764 char *default_prefix = (char *) alloca (default_len + 1);
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000765 int specd_len = strlen (specd_prefix);
766
Zack Weinberg60893f42000-07-06 22:52:03 +0000767 memcpy (default_prefix, cpp_GCC_INCLUDE_DIR, default_len);
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000768 default_prefix[default_len] = '\0';
769
Zack Weinberg60893f42000-07-06 22:52:03 +0000770 for (p = cpp_include_defaults; p->fname; p++)
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000771 {
772 /* Some standard dirs are only for C++. */
773 if (!p->cplusplus
Zack Weinbergae796972000-03-31 23:16:11 +0000774 || (CPP_OPTION (pfile, cplusplus)
775 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000776 {
777 /* Does this dir start with the prefix? */
Neil Booth61d03462000-08-18 17:35:58 +0000778 if (!memcmp (p->fname, default_prefix, default_len))
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000779 {
780 /* Yes; change prefix and add to search list. */
781 int flen = strlen (p->fname);
782 int this_len = specd_len + flen - default_len;
783 char *str = (char *) xmalloc (this_len + 1);
784 memcpy (str, specd_prefix, specd_len);
785 memcpy (str + specd_len,
786 p->fname + default_len,
787 flen - default_len + 1);
788
Neil Boothe33f6252000-08-17 17:58:24 +0000789 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000790 }
791 }
792 }
793 }
794
795 /* Search ordinary names for GNU include directories. */
Zack Weinberg60893f42000-07-06 22:52:03 +0000796 for (p = cpp_include_defaults; p->fname; p++)
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000797 {
798 /* Some standard dirs are only for C++. */
799 if (!p->cplusplus
Zack Weinbergae796972000-03-31 23:16:11 +0000800 || (CPP_OPTION (pfile, cplusplus)
801 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000802 {
Neil Booth51c04252001-08-20 06:14:53 +0000803 char *str = update_path (p->fname, p->component);
Neil Boothe33f6252000-08-17 17:58:24 +0000804 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000805 }
806 }
807}
808
Neil Booth5d8ebbd2002-01-03 21:43:09 +0000809/* Pushes a command line -imacro and -include file indicated by P onto
Neil Boothd7bc7a92001-08-21 06:20:18 +0000810 the buffer stack. Returns non-zero if successful. */
811static bool
812push_include (pfile, p)
Neil Booth4a58aab2000-11-18 12:18:09 +0000813 cpp_reader *pfile;
814 struct pending_option *p;
Neil Booth4a58aab2000-11-18 12:18:09 +0000815{
Neil Boothd7bc7a92001-08-21 06:20:18 +0000816 cpp_token header;
Neil Booth4a58aab2000-11-18 12:18:09 +0000817
Neil Boothd7bc7a92001-08-21 06:20:18 +0000818 /* Later: maybe update this to use the #include "" search path
819 if cpp_read_file fails. */
820 header.type = CPP_STRING;
821 header.val.str.text = (const unsigned char *) p->arg;
822 header.val.str.len = strlen (p->arg);
823 /* Make the command line directive take up a line. */
Neil Booth50410422001-09-15 10:18:03 +0000824 pfile->line++;
Neil Boothd7bc7a92001-08-21 06:20:18 +0000825
826 return _cpp_execute_include (pfile, &header, IT_CMDLINE);
827}
828
829/* Frees a pending_option chain. */
830static void
831free_chain (head)
832 struct pending_option *head;
833{
834 struct pending_option *next;
835
836 while (head)
837 {
838 next = head->next;
839 free (head);
840 head = next;
Neil Booth4a58aab2000-11-18 12:18:09 +0000841 }
842}
843
Neil Booth2443d4e2002-05-05 17:05:09 +0000844/* Sanity-checks are dependent on command-line options, so it is
845 called as a subroutine of cpp_read_main_file (). */
846#if ENABLE_CHECKING
847static void sanity_checks PARAMS ((cpp_reader *));
848static void sanity_checks (pfile)
849 cpp_reader *pfile;
850{
851 cppchar_t test = 0;
852
853 /* Sanity checks for assumptions about CPP arithmetic and target
854 type precisions made by cpplib. */
855 test--;
856 if (test < 1)
857 cpp_error (pfile, DL_FATAL, "cppchar_t must be an unsigned type");
858
859 if (CPP_OPTION (pfile, precision) > BITS_PER_HOST_WIDEST_INT)
860 cpp_error (pfile, DL_FATAL,
861 "preprocessor arithmetic has maximum precision of %u bits; target requires %u bits",
862 BITS_PER_HOST_WIDEST_INT, CPP_OPTION (pfile, precision));
863
864 if (CPP_OPTION (pfile, precision) < CPP_OPTION (pfile, int_precision))
865 cpp_error (pfile, DL_FATAL,
866 "CPP arithmetic must be at least as precise as a target int");
867
868 if (CPP_OPTION (pfile, char_precision) < 8)
869 cpp_error (pfile, DL_FATAL, "target char is less than 8 bits wide");
870
871 if (CPP_OPTION (pfile, wchar_precision) < CPP_OPTION (pfile, char_precision))
872 cpp_error (pfile, DL_FATAL,
873 "target wchar_t is narrower than target char");
874
875 if (CPP_OPTION (pfile, int_precision) < CPP_OPTION (pfile, char_precision))
876 cpp_error (pfile, DL_FATAL,
877 "target int is narrower than target char");
878
879 if (CPP_OPTION (pfile, wchar_precision) > BITS_PER_CPPCHAR_T)
880 cpp_error (pfile, DL_FATAL,
881 "CPP on this host cannot handle wide character constants over %u bits, but the target requires %u bits",
882 BITS_PER_CPPCHAR_T, CPP_OPTION (pfile, wchar_precision));
883}
884#else
885# define sanity_checks(PFILE)
886#endif
887
Neil Boothf5e99452001-11-15 10:01:10 +0000888/* This is called after options have been parsed, and partially
889 processed. Setup for processing input from the file named FNAME,
890 or stdin if it is the empty string. Return the original filename
891 on success (e.g. foo.i->foo.c), or NULL on failure. */
892const char *
893cpp_read_main_file (pfile, fname, table)
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000894 cpp_reader *pfile;
Neil Booth7ceb3592000-03-11 00:49:44 +0000895 const char *fname;
Neil Boothf5e99452001-11-15 10:01:10 +0000896 hash_table *table;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000897{
Neil Booth2443d4e2002-05-05 17:05:09 +0000898 sanity_checks (pfile);
899
Neil Boothf5e99452001-11-15 10:01:10 +0000900 /* The front ends don't set up the hash table until they have
901 finished processing the command line options, so initializing the
902 hashtable is deferred until now. */
903 _cpp_init_hashtable (pfile, table);
904
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000905 /* Set up the include search path now. */
Zack Weinbergae796972000-03-31 23:16:11 +0000906 if (! CPP_OPTION (pfile, no_standard_includes))
Neil Booth7ca3d2b2001-01-07 11:15:13 +0000907 init_standard_includes (pfile);
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000908
Zack Weinbergae796972000-03-31 23:16:11 +0000909 merge_include_chains (pfile);
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000910
911 /* With -v, print the list of dirs to search. */
Zack Weinbergae796972000-03-31 23:16:11 +0000912 if (CPP_OPTION (pfile, verbose))
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000913 {
Neil Booth591e15a2001-03-02 07:35:12 +0000914 struct search_path *l;
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000915 fprintf (stderr, _("#include \"...\" search starts here:\n"));
Zack Weinbergae796972000-03-31 23:16:11 +0000916 for (l = CPP_OPTION (pfile, quote_include); l; l = l->next)
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000917 {
Zack Weinbergae796972000-03-31 23:16:11 +0000918 if (l == CPP_OPTION (pfile, bracket_include))
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000919 fprintf (stderr, _("#include <...> search starts here:\n"));
920 fprintf (stderr, " %s\n", l->name);
921 }
922 fprintf (stderr, _("End of search list.\n"));
923 }
924
Neil Booth96302432001-01-07 15:17:07 +0000925 if (CPP_OPTION (pfile, print_deps))
Jakub Jelinek7855db72001-01-24 19:44:40 +0100926 /* Set the default target (if there is none already). */
Neil Booth373e2172001-02-21 07:29:56 +0000927 deps_add_default_target (pfile->deps, fname);
Neil Booth96302432001-01-07 15:17:07 +0000928
Neil Boothf5e99452001-11-15 10:01:10 +0000929 /* Open the main input file. */
Neil Booth614c7d32000-12-04 07:32:04 +0000930 if (!_cpp_read_file (pfile, fname))
Neil Boothf5e99452001-11-15 10:01:10 +0000931 return NULL;
Zack Weinbergc45da1c2000-03-02 20:14:32 +0000932
Neil Booth59930192001-08-21 21:17:48 +0000933 /* Set this after cpp_post_options so the client can change the
934 option if it wishes, and after stacking the main file so we don't
935 trace the main file. */
936 pfile->line_maps.trace_includes = CPP_OPTION (pfile, print_include_names);
937
Neil Boothf5e99452001-11-15 10:01:10 +0000938 /* For foo.i, read the original filename foo.c now, for the benefit
939 of the front ends. */
940 if (CPP_OPTION (pfile, preprocessed))
941 read_original_filename (pfile);
Neil Booth004cb262002-05-17 20:16:48 +0000942 /* Overlay an empty buffer to seed traditional preprocessing. */
943 else if (CPP_OPTION (pfile, traditional))
944 _cpp_overlay_buffer (pfile, U"", 0);
Neil Boothf5e99452001-11-15 10:01:10 +0000945
946 return pfile->map->to_file;
947}
948
949/* For preprocessed files, if the first tokens are of the form # NUM.
950 handle the directive so we know the original file name. This will
951 generate file_change callbacks, which the front ends must handle
952 appropriately given their state of initialization. */
953static void
954read_original_filename (pfile)
955 cpp_reader *pfile;
956{
957 const cpp_token *token, *token1;
958
959 /* Lex ahead; if the first tokens are of the form # NUM, then
960 process the directive, otherwise back up. */
961 token = _cpp_lex_direct (pfile);
962 if (token->type == CPP_HASH)
963 {
964 token1 = _cpp_lex_direct (pfile);
965 _cpp_backup_tokens (pfile, 1);
966
967 /* If it's a #line directive, handle it. */
968 if (token1->type == CPP_NUMBER)
969 {
970 _cpp_handle_directive (pfile, token->flags & PREV_WHITE);
971 return;
972 }
973 }
974
975 /* Backup as if nothing happened. */
976 _cpp_backup_tokens (pfile, 1);
977}
978
979/* Handle pending command line options: -D, -U, -A, -imacros and
980 -include. This should be called after debugging has been properly
981 set up in the front ends. */
982void
983cpp_finish_options (pfile)
984 cpp_reader *pfile;
985{
Neil Boothd7bc7a92001-08-21 06:20:18 +0000986 /* Install builtins and process command line macros etc. in the order
987 they appeared, but only if not already preprocessed. */
Neil Booth05e81722001-01-11 21:30:16 +0000988 if (! CPP_OPTION (pfile, preprocessed))
Zack Weinberg6de1e2a1999-02-18 15:35:49 +0000989 {
Neil Boothd7bc7a92001-08-21 06:20:18 +0000990 struct pending_option *p;
991
Joseph Myersb0287a92001-12-15 20:31:07 +0000992 _cpp_do_file_change (pfile, LC_RENAME, _("<built-in>"), 1, 0);
Neil Booth004cb262002-05-17 20:16:48 +0000993 if (!CPP_OPTION (pfile, traditional) /* REMOVEME */)
994 init_builtins (pfile);
Neil Boothd7bc7a92001-08-21 06:20:18 +0000995 _cpp_do_file_change (pfile, LC_RENAME, _("<command line>"), 1, 0);
Neil Booth004cb262002-05-17 20:16:48 +0000996 if (!CPP_OPTION (pfile, traditional) /* REMOVEME */)
997 for (p = CPP_OPTION (pfile, pending)->directive_head; p; p = p->next)
998 (*p->handler) (pfile, p->arg);
Neil Boothd7bc7a92001-08-21 06:20:18 +0000999
Neil Boothaf0d16c2002-04-22 17:48:02 +00001000 /* Scan -imacros files after -D, -U, but before -include.
1001 pfile->next_include_file is NULL, so _cpp_pop_buffer does not
1002 push -include files. */
1003 for (p = CPP_OPTION (pfile, pending)->imacros_head; p; p = p->next)
1004 if (push_include (pfile, p))
1005 cpp_scan_nooutput (pfile);
1006
1007 pfile->next_include_file = &CPP_OPTION (pfile, pending)->include_head;
1008 _cpp_maybe_push_include_file (pfile);
Zack Weinberg0b22d651999-03-15 18:42:46 +00001009 }
Neil Booth05e81722001-01-11 21:30:16 +00001010
Neil Boothaf0d16c2002-04-22 17:48:02 +00001011 free_chain (CPP_OPTION (pfile, pending)->imacros_head);
Neil Boothd7bc7a92001-08-21 06:20:18 +00001012 free_chain (CPP_OPTION (pfile, pending)->directive_head);
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001013}
1014
Neil Boothaf0d16c2002-04-22 17:48:02 +00001015/* Push the next buffer on the stack given by -include, if any. */
1016void
1017_cpp_maybe_push_include_file (pfile)
Neil Boothd7bc7a92001-08-21 06:20:18 +00001018 cpp_reader *pfile;
1019{
Neil Boothaf0d16c2002-04-22 17:48:02 +00001020 if (pfile->next_include_file)
Neil Boothd7bc7a92001-08-21 06:20:18 +00001021 {
Neil Boothaf0d16c2002-04-22 17:48:02 +00001022 struct pending_option *head = *pfile->next_include_file;
1023
1024 while (head && !push_include (pfile, head))
1025 head = head->next;
1026
1027 if (head)
1028 pfile->next_include_file = &head->next;
1029 else
Neil Boothd7bc7a92001-08-21 06:20:18 +00001030 {
Neil Boothaf0d16c2002-04-22 17:48:02 +00001031 /* All done; restore the line map from <command line>. */
1032 _cpp_do_file_change (pfile, LC_RENAME,
1033 pfile->line_maps.maps[0].to_file, 1, 0);
1034 /* Don't come back here again. */
1035 pfile->next_include_file = NULL;
Neil Boothd7bc7a92001-08-21 06:20:18 +00001036 }
1037 }
Neil Boothd7bc7a92001-08-21 06:20:18 +00001038}
1039
Neil Booth2f638f92001-01-10 23:28:00 +00001040/* Use mkdeps.c to output dependency information. */
Neil Booth7ca3d2b2001-01-07 11:15:13 +00001041static void
1042output_deps (pfile)
1043 cpp_reader *pfile;
1044{
1045 /* Stream on which to print the dependency information. */
1046 FILE *deps_stream = 0;
Kaveh R. Ghazi27c38fb2001-09-12 17:18:03 +00001047 const char *const deps_mode =
1048 CPP_OPTION (pfile, print_deps_append) ? "a" : "w";
Neil Booth7ca3d2b2001-01-07 11:15:13 +00001049
Neil Booth56cd5b92002-02-20 07:24:10 +00001050 if (CPP_OPTION (pfile, deps_file)[0] == '\0')
Neil Booth7ca3d2b2001-01-07 11:15:13 +00001051 deps_stream = stdout;
1052 else
1053 {
1054 deps_stream = fopen (CPP_OPTION (pfile, deps_file), deps_mode);
1055 if (deps_stream == 0)
1056 {
Neil Boothebef4e82002-04-14 18:42:47 +00001057 cpp_errno (pfile, DL_ERROR, CPP_OPTION (pfile, deps_file));
Neil Booth7ca3d2b2001-01-07 11:15:13 +00001058 return;
1059 }
1060 }
1061
1062 deps_write (pfile->deps, deps_stream, 72);
1063
1064 if (CPP_OPTION (pfile, deps_phony_targets))
1065 deps_phony_targets (pfile->deps, deps_stream);
1066
1067 /* Don't close stdout. */
Neil Boothab8e2222002-02-23 13:42:40 +00001068 if (deps_stream != stdout)
Neil Booth7ca3d2b2001-01-07 11:15:13 +00001069 {
1070 if (ferror (deps_stream) || fclose (deps_stream) != 0)
Neil Boothebef4e82002-04-14 18:42:47 +00001071 cpp_error (pfile, DL_FATAL, "I/O error on output");
Neil Booth7ca3d2b2001-01-07 11:15:13 +00001072 }
1073}
1074
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001075/* This is called at the end of preprocessing. It pops the
1076 last buffer and writes dependency output. It should also
1077 clear macro definitions, such that you could call cpp_start_read
Neil Booth4a58aab2000-11-18 12:18:09 +00001078 with a new filename to restart processing. */
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001079void
Neil Booth93c803682000-10-28 17:59:06 +00001080cpp_finish (pfile)
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001081 cpp_reader *pfile;
1082{
Neil Booth7364fdd2001-08-07 20:37:26 +00001083 /* cpplex.c leaves the final buffer on the stack. This it so that
1084 it returns an unending stream of CPP_EOFs to the client. If we
Joseph Myersa1f300c2001-11-23 02:05:19 +00001085 popped the buffer, we'd dereference a NULL buffer pointer and
Neil Booth7364fdd2001-08-07 20:37:26 +00001086 segfault. It's nice to allow the client to do worry-free excess
1087 cpp_get_token calls. */
1088 while (pfile->buffer)
1089 _cpp_pop_buffer (pfile);
Zack Weinbergc1212d22000-02-06 23:46:18 +00001090
Zack Weinberg49e6c082000-03-04 19:42:04 +00001091 /* Don't write the deps file if preprocessing has failed. */
Zack Weinbergae796972000-03-31 23:16:11 +00001092 if (CPP_OPTION (pfile, print_deps) && pfile->errors == 0)
Neil Booth7ca3d2b2001-01-07 11:15:13 +00001093 output_deps (pfile);
Zack Weinberg3caee4a1999-04-26 16:41:02 +00001094
Zack Weinbergd4506962000-06-28 19:03:08 +00001095 /* Report on headers that could use multiple include guards. */
1096 if (CPP_OPTION (pfile, print_include_names))
Zack Weinbergc71f8352000-07-05 05:33:57 +00001097 _cpp_report_missing_guards (pfile);
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001098}
1099
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001100/* Add a directive to be handled later in the initialization phase. */
Richard Henderson223dca61999-12-14 08:05:23 -08001101static void
Zack Weinbergae796972000-03-31 23:16:11 +00001102new_pending_directive (pend, text, handler)
1103 struct cpp_pending *pend;
Richard Henderson223dca61999-12-14 08:05:23 -08001104 const char *text;
Neil Booth40eac642000-03-11 09:13:00 +00001105 cl_directive_handler handler;
Richard Henderson223dca61999-12-14 08:05:23 -08001106{
1107 struct pending_option *o = (struct pending_option *)
1108 xmalloc (sizeof (struct pending_option));
1109
Neil Booth7ceb3592000-03-11 00:49:44 +00001110 o->arg = text;
Richard Henderson223dca61999-12-14 08:05:23 -08001111 o->next = NULL;
Neil Booth40eac642000-03-11 09:13:00 +00001112 o->handler = handler;
Zack Weinbergae796972000-03-31 23:16:11 +00001113 APPEND (pend, directive, o);
Richard Henderson223dca61999-12-14 08:05:23 -08001114}
1115
Zack Weinbergae796972000-03-31 23:16:11 +00001116/* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
1117 I.e. a const string initializer with parens around it. That is
1118 what N_("string") resolves to, so we make no_* be macros instead. */
Neil Boothc725bd72001-12-03 19:15:19 +00001119#define no_arg N_("argument missing after %s")
1120#define no_ass N_("assertion missing after %s")
1121#define no_dir N_("directory name missing after %s")
1122#define no_fil N_("file name missing after %s")
1123#define no_mac N_("macro name missing after %s")
1124#define no_pth N_("path name missing after %s")
1125#define no_num N_("number missing after %s")
1126#define no_tgt N_("target missing after %s")
Zack Weinbergae796972000-03-31 23:16:11 +00001127
1128/* This is the list of all command line options, with the leading
1129 "-" removed. It must be sorted in ASCII collating order. */
1130#define COMMAND_LINE_OPTIONS \
Zack Weinbergae796972000-03-31 23:16:11 +00001131 DEF_OPT("$", 0, OPT_dollar) \
Zack Weinbergae796972000-03-31 23:16:11 +00001132 DEF_OPT("-help", 0, OPT__help) \
Chandra Chavva91606ce2000-11-14 12:06:06 -05001133 DEF_OPT("-target-help", 0, OPT_target__help) \
Zack Weinbergae796972000-03-31 23:16:11 +00001134 DEF_OPT("-version", 0, OPT__version) \
1135 DEF_OPT("A", no_ass, OPT_A) \
1136 DEF_OPT("C", 0, OPT_C) \
Jason Thorpe477cdac2002-04-07 03:12:23 +00001137 DEF_OPT("CC", 0, OPT_CC) \
Zack Weinbergae796972000-03-31 23:16:11 +00001138 DEF_OPT("D", no_mac, OPT_D) \
1139 DEF_OPT("H", 0, OPT_H) \
1140 DEF_OPT("I", no_dir, OPT_I) \
1141 DEF_OPT("M", 0, OPT_M) \
Neil Boothb3124fa2002-03-16 14:03:36 +00001142 DEF_OPT("MD", no_fil, OPT_MD) \
Neil Booth96302432001-01-07 15:17:07 +00001143 DEF_OPT("MF", no_fil, OPT_MF) \
Zack Weinbergae796972000-03-31 23:16:11 +00001144 DEF_OPT("MG", 0, OPT_MG) \
1145 DEF_OPT("MM", 0, OPT_MM) \
Neil Boothb3124fa2002-03-16 14:03:36 +00001146 DEF_OPT("MMD", no_fil, OPT_MMD) \
Neil Bootha5a4ce32001-01-05 07:50:24 +00001147 DEF_OPT("MP", 0, OPT_MP) \
Neil Boothf7114e12001-01-06 00:15:29 +00001148 DEF_OPT("MQ", no_tgt, OPT_MQ) \
Neil Booth03b9ab42001-01-04 10:25:55 +00001149 DEF_OPT("MT", no_tgt, OPT_MT) \
Zack Weinbergae796972000-03-31 23:16:11 +00001150 DEF_OPT("P", 0, OPT_P) \
1151 DEF_OPT("U", no_mac, OPT_U) \
1152 DEF_OPT("W", no_arg, OPT_W) /* arg optional */ \
1153 DEF_OPT("d", no_arg, OPT_d) \
Jakub Jelinekbe768052000-12-15 16:49:28 +01001154 DEF_OPT("fno-operator-names", 0, OPT_fno_operator_names) \
Zack Weinbergae796972000-03-31 23:16:11 +00001155 DEF_OPT("fno-preprocessed", 0, OPT_fno_preprocessed) \
1156 DEF_OPT("fno-show-column", 0, OPT_fno_show_column) \
1157 DEF_OPT("fpreprocessed", 0, OPT_fpreprocessed) \
1158 DEF_OPT("fshow-column", 0, OPT_fshow_column) \
Neil Booth0fef3fd2002-02-02 18:56:37 +00001159 DEF_OPT("fsigned-char", 0, OPT_fsigned_char) \
Neil Booth6ab3e7d2000-05-18 11:09:27 +00001160 DEF_OPT("ftabstop=", no_num, OPT_ftabstop) \
Neil Booth0fef3fd2002-02-02 18:56:37 +00001161 DEF_OPT("funsigned-char", 0, OPT_funsigned_char) \
Zack Weinbergae796972000-03-31 23:16:11 +00001162 DEF_OPT("h", 0, OPT_h) \
1163 DEF_OPT("idirafter", no_dir, OPT_idirafter) \
1164 DEF_OPT("imacros", no_fil, OPT_imacros) \
1165 DEF_OPT("include", no_fil, OPT_include) \
1166 DEF_OPT("iprefix", no_pth, OPT_iprefix) \
1167 DEF_OPT("isystem", no_dir, OPT_isystem) \
1168 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix) \
1169 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore) \
1170 DEF_OPT("lang-asm", 0, OPT_lang_asm) \
1171 DEF_OPT("lang-c", 0, OPT_lang_c) \
1172 DEF_OPT("lang-c++", 0, OPT_lang_cplusplus) \
1173 DEF_OPT("lang-c89", 0, OPT_lang_c89) \
Zack Weinbergae796972000-03-31 23:16:11 +00001174 DEF_OPT("lang-objc", 0, OPT_lang_objc) \
Zack Weinbergae796972000-03-31 23:16:11 +00001175 DEF_OPT("nostdinc", 0, OPT_nostdinc) \
1176 DEF_OPT("nostdinc++", 0, OPT_nostdincplusplus) \
1177 DEF_OPT("o", no_fil, OPT_o) \
1178 DEF_OPT("pedantic", 0, OPT_pedantic) \
1179 DEF_OPT("pedantic-errors", 0, OPT_pedantic_errors) \
1180 DEF_OPT("remap", 0, OPT_remap) \
Neil Boothdd07b882000-11-20 18:27:32 +00001181 DEF_OPT("std=c++98", 0, OPT_std_cplusplus98) \
Zack Weinbergae796972000-03-31 23:16:11 +00001182 DEF_OPT("std=c89", 0, OPT_std_c89) \
1183 DEF_OPT("std=c99", 0, OPT_std_c99) \
1184 DEF_OPT("std=c9x", 0, OPT_std_c9x) \
1185 DEF_OPT("std=gnu89", 0, OPT_std_gnu89) \
1186 DEF_OPT("std=gnu99", 0, OPT_std_gnu99) \
1187 DEF_OPT("std=gnu9x", 0, OPT_std_gnu9x) \
1188 DEF_OPT("std=iso9899:1990", 0, OPT_std_iso9899_1990) \
1189 DEF_OPT("std=iso9899:199409", 0, OPT_std_iso9899_199409) \
1190 DEF_OPT("std=iso9899:1999", 0, OPT_std_iso9899_1999) \
1191 DEF_OPT("std=iso9899:199x", 0, OPT_std_iso9899_199x) \
Neil Booth004cb262002-05-17 20:16:48 +00001192 DEF_OPT("traditional-cpp", 0, OPT_traditional_cpp) \
Zack Weinbergae796972000-03-31 23:16:11 +00001193 DEF_OPT("trigraphs", 0, OPT_trigraphs) \
1194 DEF_OPT("v", 0, OPT_v) \
Zack Weinbergf4cdc362001-01-05 23:41:00 +00001195 DEF_OPT("version", 0, OPT_version) \
Zack Weinbergae796972000-03-31 23:16:11 +00001196 DEF_OPT("w", 0, OPT_w)
1197
1198#define DEF_OPT(text, msg, code) code,
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001199enum opt_code
1200{
Zack Weinbergae796972000-03-31 23:16:11 +00001201 COMMAND_LINE_OPTIONS
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001202 N_OPTS
1203};
Zack Weinbergae796972000-03-31 23:16:11 +00001204#undef DEF_OPT
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001205
1206struct cl_option
1207{
1208 const char *opt_text;
1209 const char *msg;
1210 size_t opt_len;
1211 enum opt_code opt_code;
1212};
1213
Zack Weinbergae796972000-03-31 23:16:11 +00001214#define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001215#ifdef HOST_EBCDIC
1216static struct cl_option cl_options[] =
1217#else
1218static const struct cl_option cl_options[] =
1219#endif
1220{
Zack Weinbergae796972000-03-31 23:16:11 +00001221 COMMAND_LINE_OPTIONS
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001222};
1223#undef DEF_OPT
Zack Weinbergae796972000-03-31 23:16:11 +00001224#undef COMMAND_LINE_OPTIONS
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001225
1226/* Perform a binary search to find which, if any, option the given
1227 command-line matches. Returns its index in the option array,
1228 negative on failure. Complications arise since some options can be
1229 suffixed with an argument, and multiple complete matches can occur,
Neil Booth05e81722001-01-11 21:30:16 +00001230 e.g. -iwithprefix and -iwithprefixbefore. Moreover, we need to
1231 accept options beginning with -W that we do not recognise, but not
1232 to swallow any subsequent command line argument; this is handled as
1233 special cases in cpp_handle_option. */
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001234static int
1235parse_option (input)
1236 const char *input;
1237{
1238 unsigned int md, mn, mx;
1239 size_t opt_len;
1240 int comp;
1241
1242 mn = 0;
1243 mx = N_OPTS;
1244
1245 while (mx > mn)
1246 {
1247 md = (mn + mx) / 2;
Zack Weinbergae796972000-03-31 23:16:11 +00001248
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001249 opt_len = cl_options[md].opt_len;
Neil Booth61d03462000-08-18 17:35:58 +00001250 comp = memcmp (input, cl_options[md].opt_text, opt_len);
Zack Weinbergae796972000-03-31 23:16:11 +00001251
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001252 if (comp > 0)
1253 mn = md + 1;
1254 else if (comp < 0)
1255 mx = md;
1256 else
1257 {
1258 if (input[opt_len] == '\0')
1259 return md;
1260 /* We were passed more text. If the option takes an argument,
1261 we may match a later option or we may have been passed the
1262 argument. The longest possible option match succeeds.
1263 If the option takes no arguments we have not matched and
Neil Booth4a58aab2000-11-18 12:18:09 +00001264 continue the search (e.g. input="stdc++" match was "stdc"). */
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001265 mn = md + 1;
1266 if (cl_options[md].msg)
1267 {
1268 /* Scan forwards. If we get an exact match, return it.
1269 Otherwise, return the longest option-accepting match.
Neil Booth4a58aab2000-11-18 12:18:09 +00001270 This loops no more than twice with current options. */
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001271 mx = md;
John David Anglin37b85242001-03-02 01:11:50 +00001272 for (; mn < (unsigned int) N_OPTS; mn++)
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001273 {
1274 opt_len = cl_options[mn].opt_len;
Neil Booth61d03462000-08-18 17:35:58 +00001275 if (memcmp (input, cl_options[mn].opt_text, opt_len))
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001276 break;
1277 if (input[opt_len] == '\0')
1278 return mn;
1279 if (cl_options[mn].msg)
1280 mx = mn;
1281 }
1282 return mx;
1283 }
1284 }
1285 }
1286
1287 return -1;
1288}
1289
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001290/* Handle one command-line option in (argc, argv).
1291 Can be called multiple times, to handle multiple sets of options.
Jakub Jelinekffdeea42002-01-29 13:09:37 +01001292 If ignore is non-zero, this will ignore unrecognized -W* options.
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001293 Returns number of strings consumed. */
Zack Weinberg2c0accc2000-07-15 19:29:14 +00001294int
Jakub Jelinekffdeea42002-01-29 13:09:37 +01001295cpp_handle_option (pfile, argc, argv, ignore)
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001296 cpp_reader *pfile;
1297 int argc;
1298 char **argv;
Jakub Jelinekffdeea42002-01-29 13:09:37 +01001299 int ignore;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001300{
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001301 int i = 0;
Zack Weinbergf8f769e2000-05-28 05:56:38 +00001302 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001303
Neil Booth373e2172001-02-21 07:29:56 +00001304 /* Interpret "-" or a non-option as a file name. */
1305 if (argv[i][0] != '-' || argv[i][1] == '\0')
Zack Weinberg0b22d651999-03-15 18:42:46 +00001306 {
Neil Booth373e2172001-02-21 07:29:56 +00001307 if (CPP_OPTION (pfile, in_fname) == NULL)
1308 CPP_OPTION (pfile, in_fname) = argv[i];
1309 else if (CPP_OPTION (pfile, out_fname) == NULL)
Zack Weinbergae796972000-03-31 23:16:11 +00001310 CPP_OPTION (pfile, out_fname) = argv[i];
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001311 else
Neil Boothebef4e82002-04-14 18:42:47 +00001312 cpp_error (pfile, DL_FATAL,
1313 "too many filenames. Type %s --help for usage info",
Neil Booth373e2172001-02-21 07:29:56 +00001314 progname);
Zack Weinberg0b22d651999-03-15 18:42:46 +00001315 }
1316 else
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001317 {
1318 enum opt_code opt_code;
1319 int opt_index;
Neil Booth7ceb3592000-03-11 00:49:44 +00001320 const char *arg = 0;
Zack Weinberg0b22d651999-03-15 18:42:46 +00001321
Neil Booth4a58aab2000-11-18 12:18:09 +00001322 /* Skip over '-'. */
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001323 opt_index = parse_option (&argv[i][1]);
1324 if (opt_index < 0)
1325 return i;
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001326
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001327 opt_code = cl_options[opt_index].opt_code;
1328 if (cl_options[opt_index].msg)
Zack Weinberg0b22d651999-03-15 18:42:46 +00001329 {
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001330 arg = &argv[i][cl_options[opt_index].opt_len + 1];
1331
Neil Booth5c5d1ea2001-01-10 21:32:15 +00001332 /* Yuk. Special case for -W as it must not swallow
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001333 up any following argument. If this becomes common, add
Neil Booth4a58aab2000-11-18 12:18:09 +00001334 another field to the cl_options table. */
Neil Booth5c5d1ea2001-01-10 21:32:15 +00001335 if (arg[0] == '\0' && opt_code != OPT_W)
Zack Weinberg0b22d651999-03-15 18:42:46 +00001336 {
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001337 arg = argv[++i];
1338 if (!arg)
Zack Weinberg0b22d651999-03-15 18:42:46 +00001339 {
Neil Boothebef4e82002-04-14 18:42:47 +00001340 cpp_error (pfile, DL_FATAL,
1341 cl_options[opt_index].msg, argv[i - 1]);
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001342 return argc;
1343 }
1344 }
1345 }
Zack Weinbergae796972000-03-31 23:16:11 +00001346
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001347 switch (opt_code)
1348 {
Neil Booth4a58aab2000-11-18 12:18:09 +00001349 case N_OPTS: /* Shut GCC up. */
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001350 break;
Jakub Jelinekbe768052000-12-15 16:49:28 +01001351 case OPT_fno_operator_names:
1352 CPP_OPTION (pfile, operator_names) = 0;
1353 break;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001354 case OPT_fpreprocessed:
Zack Weinbergae796972000-03-31 23:16:11 +00001355 CPP_OPTION (pfile, preprocessed) = 1;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001356 break;
1357 case OPT_fno_preprocessed:
Zack Weinbergae796972000-03-31 23:16:11 +00001358 CPP_OPTION (pfile, preprocessed) = 0;
1359 break;
1360 case OPT_fshow_column:
1361 CPP_OPTION (pfile, show_column) = 1;
1362 break;
1363 case OPT_fno_show_column:
1364 CPP_OPTION (pfile, show_column) = 0;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001365 break;
Neil Booth0fef3fd2002-02-02 18:56:37 +00001366 case OPT_fsigned_char:
Neil Booth44a147a2002-05-07 21:07:24 +00001367 CPP_OPTION (pfile, unsigned_char) = 0;
Neil Booth0fef3fd2002-02-02 18:56:37 +00001368 break;
1369 case OPT_funsigned_char:
Neil Booth44a147a2002-05-07 21:07:24 +00001370 CPP_OPTION (pfile, unsigned_char) = 1;
Neil Booth0fef3fd2002-02-02 18:56:37 +00001371 break;
Neil Booth6ab3e7d2000-05-18 11:09:27 +00001372 case OPT_ftabstop:
1373 /* Silently ignore empty string, non-longs and silly values. */
1374 if (arg[0] != '\0')
1375 {
1376 char *endptr;
1377 long tabstop = strtol (arg, &endptr, 10);
1378 if (*endptr == '\0' && tabstop >= 1 && tabstop <= 100)
1379 CPP_OPTION (pfile, tabstop) = tabstop;
1380 }
1381 break;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001382 case OPT_w:
Zack Weinbergae796972000-03-31 23:16:11 +00001383 CPP_OPTION (pfile, inhibit_warnings) = 1;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001384 break;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001385 case OPT_h:
1386 case OPT__help:
1387 print_help ();
Neil Booth7e96d762001-01-13 01:00:01 +00001388 CPP_OPTION (pfile, help_only) = 1;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001389 break;
Chandra Chavva91606ce2000-11-14 12:06:06 -05001390 case OPT_target__help:
Zack Weinbergf4cdc362001-01-05 23:41:00 +00001391 /* Print if any target specific options. cpplib has none, but
1392 make sure help_only gets set. */
Neil Booth7e96d762001-01-13 01:00:01 +00001393 CPP_OPTION (pfile, help_only) = 1;
Chandra Chavva91606ce2000-11-14 12:06:06 -05001394 break;
Zack Weinbergf4cdc362001-01-05 23:41:00 +00001395
1396 /* --version inhibits compilation, -version doesn't. -v means
1397 verbose and -version. Historical reasons, don't ask. */
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001398 case OPT__version:
Neil Booth7e96d762001-01-13 01:00:01 +00001399 CPP_OPTION (pfile, help_only) = 1;
Zack Weinbergcb773842001-03-02 00:42:28 +00001400 pfile->print_version = 1;
1401 break;
Zack Weinbergf4cdc362001-01-05 23:41:00 +00001402 case OPT_v:
1403 CPP_OPTION (pfile, verbose) = 1;
Zack Weinbergcb773842001-03-02 00:42:28 +00001404 pfile->print_version = 1;
1405 break;
Zack Weinbergf4cdc362001-01-05 23:41:00 +00001406 case OPT_version:
Zack Weinbergcb773842001-03-02 00:42:28 +00001407 pfile->print_version = 1;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001408 break;
Zack Weinbergf4cdc362001-01-05 23:41:00 +00001409
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001410 case OPT_C:
Zack Weinbergae796972000-03-31 23:16:11 +00001411 CPP_OPTION (pfile, discard_comments) = 0;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001412 break;
Jason Thorpe477cdac2002-04-07 03:12:23 +00001413 case OPT_CC:
1414 CPP_OPTION (pfile, discard_comments) = 0;
1415 CPP_OPTION (pfile, discard_comments_in_macro_exp) = 0;
1416 break;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001417 case OPT_P:
Zack Weinbergae796972000-03-31 23:16:11 +00001418 CPP_OPTION (pfile, no_line_commands) = 1;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001419 break;
Neil Booth4a58aab2000-11-18 12:18:09 +00001420 case OPT_dollar: /* Don't include $ in identifiers. */
Zack Weinbergae796972000-03-31 23:16:11 +00001421 CPP_OPTION (pfile, dollars_in_ident) = 0;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001422 break;
1423 case OPT_H:
Zack Weinbergae796972000-03-31 23:16:11 +00001424 CPP_OPTION (pfile, print_include_names) = 1;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001425 break;
1426 case OPT_D:
Zack Weinbergf8f769e2000-05-28 05:56:38 +00001427 new_pending_directive (pend, arg, cpp_define);
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001428 break;
1429 case OPT_pedantic_errors:
Zack Weinbergae796972000-03-31 23:16:11 +00001430 CPP_OPTION (pfile, pedantic_errors) = 1;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001431 /* fall through */
1432 case OPT_pedantic:
Zack Weinbergae796972000-03-31 23:16:11 +00001433 CPP_OPTION (pfile, pedantic) = 1;
Neil Booth27845282002-03-24 12:52:28 +00001434 CPP_OPTION (pfile, warn_endif_labels) = 1;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001435 break;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001436 case OPT_trigraphs:
Zack Weinbergae796972000-03-31 23:16:11 +00001437 CPP_OPTION (pfile, trigraphs) = 1;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001438 break;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001439 case OPT_remap:
Zack Weinbergae796972000-03-31 23:16:11 +00001440 CPP_OPTION (pfile, remap) = 1;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001441 break;
Neil Booth004cb262002-05-17 20:16:48 +00001442 case OPT_traditional_cpp:
1443 CPP_OPTION (pfile, traditional) = 1;
1444 break;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001445 case OPT_iprefix:
Zack Weinbergae796972000-03-31 23:16:11 +00001446 CPP_OPTION (pfile, include_prefix) = arg;
1447 CPP_OPTION (pfile, include_prefix_len) = strlen (arg);
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001448 break;
1449 case OPT_lang_c:
Neil Boothdd07b882000-11-20 18:27:32 +00001450 set_lang (pfile, CLK_GNUC89);
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001451 break;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001452 case OPT_lang_cplusplus:
Neil Boothdd07b882000-11-20 18:27:32 +00001453 set_lang (pfile, CLK_GNUCXX);
1454 break;
1455 case OPT_lang_objc:
Neil Boothbdcae022002-05-17 19:37:43 +00001456 CPP_OPTION (pfile, objc) = 1;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001457 break;
1458 case OPT_lang_asm:
Neil Boothdd07b882000-11-20 18:27:32 +00001459 set_lang (pfile, CLK_ASM);
1460 break;
1461 case OPT_std_cplusplus98:
1462 set_lang (pfile, CLK_CXX98);
1463 break;
1464 case OPT_std_gnu89:
1465 set_lang (pfile, CLK_GNUC89);
1466 break;
1467 case OPT_std_gnu9x:
1468 case OPT_std_gnu99:
1469 set_lang (pfile, CLK_GNUC99);
1470 break;
1471 case OPT_std_iso9899_199409:
1472 set_lang (pfile, CLK_STDC94);
1473 break;
1474 case OPT_std_iso9899_1990:
1475 case OPT_std_c89:
1476 case OPT_lang_c89:
1477 set_lang (pfile, CLK_STDC89);
1478 break;
1479 case OPT_std_iso9899_199x:
1480 case OPT_std_iso9899_1999:
1481 case OPT_std_c9x:
1482 case OPT_std_c99:
1483 set_lang (pfile, CLK_STDC99);
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001484 break;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001485 case OPT_nostdinc:
1486 /* -nostdinc causes no default include directories.
1487 You must specify all include-file directories with -I. */
Zack Weinbergae796972000-03-31 23:16:11 +00001488 CPP_OPTION (pfile, no_standard_includes) = 1;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001489 break;
1490 case OPT_nostdincplusplus:
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001491 /* -nostdinc++ causes no default C++-specific include directories. */
Zack Weinbergae796972000-03-31 23:16:11 +00001492 CPP_OPTION (pfile, no_standard_cplusplus_includes) = 1;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001493 break;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001494 case OPT_o:
Neil Booth373e2172001-02-21 07:29:56 +00001495 if (CPP_OPTION (pfile, out_fname) == NULL)
1496 CPP_OPTION (pfile, out_fname) = arg;
1497 else
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001498 {
Neil Boothebef4e82002-04-14 18:42:47 +00001499 cpp_error (pfile, DL_FATAL, "output filename specified twice");
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001500 return argc;
1501 }
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001502 break;
1503 case OPT_d:
1504 /* Args to -d specify what parts of macros to dump.
1505 Silently ignore unrecognised options; they may
Neil Booth4a58aab2000-11-18 12:18:09 +00001506 be aimed at the compiler proper. */
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001507 {
1508 char c;
Zack Weinbergae796972000-03-31 23:16:11 +00001509
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001510 while ((c = *arg++) != '\0')
1511 switch (c)
1512 {
1513 case 'M':
Zack Weinbergae796972000-03-31 23:16:11 +00001514 CPP_OPTION (pfile, dump_macros) = dump_only;
Zack Weinberg0b22d651999-03-15 18:42:46 +00001515 break;
1516 case 'N':
Zack Weinbergae796972000-03-31 23:16:11 +00001517 CPP_OPTION (pfile, dump_macros) = dump_names;
Zack Weinberg0b22d651999-03-15 18:42:46 +00001518 break;
1519 case 'D':
Zack Weinbergae796972000-03-31 23:16:11 +00001520 CPP_OPTION (pfile, dump_macros) = dump_definitions;
Zack Weinberg0b22d651999-03-15 18:42:46 +00001521 break;
1522 case 'I':
Zack Weinbergae796972000-03-31 23:16:11 +00001523 CPP_OPTION (pfile, dump_includes) = 1;
Zack Weinberg0b22d651999-03-15 18:42:46 +00001524 break;
1525 }
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001526 }
1527 break;
Zack Weinbergae796972000-03-31 23:16:11 +00001528
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001529 case OPT_MG:
Zack Weinbergae796972000-03-31 23:16:11 +00001530 CPP_OPTION (pfile, print_deps_missing_files) = 1;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001531 break;
1532 case OPT_M:
Neil Booth3ddbb8a2002-03-24 21:01:00 +00001533 /* When doing dependencies with -M or -MM, suppress normal
1534 preprocessed output, but still do -dM etc. as software
1535 depends on this. Preprocessed output occurs if -MD, -MMD
1536 or environment var dependency generation is used. */
Neil Booth96302432001-01-07 15:17:07 +00001537 CPP_OPTION (pfile, print_deps) = 2;
Neil Booth3ddbb8a2002-03-24 21:01:00 +00001538 CPP_OPTION (pfile, no_output) = 1;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001539 break;
Neil Booth96302432001-01-07 15:17:07 +00001540 case OPT_MM:
1541 CPP_OPTION (pfile, print_deps) = 1;
Neil Booth3ddbb8a2002-03-24 21:01:00 +00001542 CPP_OPTION (pfile, no_output) = 1;
Neil Booth96302432001-01-07 15:17:07 +00001543 break;
1544 case OPT_MF:
1545 CPP_OPTION (pfile, deps_file) = arg;
1546 break;
1547 case OPT_MP:
Neil Bootha5a4ce32001-01-05 07:50:24 +00001548 CPP_OPTION (pfile, deps_phony_targets) = 1;
1549 break;
Neil Boothf7114e12001-01-06 00:15:29 +00001550 case OPT_MQ:
Neil Booth03b9ab42001-01-04 10:25:55 +00001551 case OPT_MT:
Neil Boothf7114e12001-01-06 00:15:29 +00001552 /* Add a target. -MQ quotes for Make. */
1553 deps_add_target (pfile->deps, arg, opt_code == OPT_MQ);
Neil Booth03b9ab42001-01-04 10:25:55 +00001554 break;
1555
Neil Boothb3124fa2002-03-16 14:03:36 +00001556 case OPT_MD:
1557 CPP_OPTION (pfile, print_deps) = 2;
1558 CPP_OPTION (pfile, deps_file) = arg;
1559 break;
1560 case OPT_MMD:
1561 CPP_OPTION (pfile, print_deps) = 1;
1562 CPP_OPTION (pfile, deps_file) = arg;
1563 break;
1564
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001565 case OPT_A:
Neil Boothe1e97c42000-03-16 14:15:17 +00001566 if (arg[0] == '-')
Zack Weinberg0b22d651999-03-15 18:42:46 +00001567 {
Neil Boothe1e97c42000-03-16 14:15:17 +00001568 /* -A with an argument beginning with '-' acts as
1569 #unassert on whatever immediately follows the '-'.
1570 If "-" is the whole argument, we eliminate all
1571 predefined macros and assertions, including those
1572 that were specified earlier on the command line.
1573 That way we can get rid of any that were passed
1574 automatically in from GCC. */
Zack Weinberg0b22d651999-03-15 18:42:46 +00001575
Neil Boothe1e97c42000-03-16 14:15:17 +00001576 if (arg[1] == '\0')
Zack Weinberg0b22d651999-03-15 18:42:46 +00001577 {
Neil Booth29401c32001-08-22 20:37:20 +00001578 free_chain (pend->directive_head);
Neil Boothe33f6252000-08-17 17:58:24 +00001579 pend->directive_head = NULL;
1580 pend->directive_tail = NULL;
Zack Weinberg0b22d651999-03-15 18:42:46 +00001581 }
Neil Boothe1e97c42000-03-16 14:15:17 +00001582 else
Neil Boothe33f6252000-08-17 17:58:24 +00001583 new_pending_directive (pend, arg + 1, cpp_unassert);
Zack Weinberg0b22d651999-03-15 18:42:46 +00001584 }
Neil Boothe1e97c42000-03-16 14:15:17 +00001585 else
Neil Boothe33f6252000-08-17 17:58:24 +00001586 new_pending_directive (pend, arg, cpp_assert);
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001587 break;
1588 case OPT_U:
Neil Boothe33f6252000-08-17 17:58:24 +00001589 new_pending_directive (pend, arg, cpp_undef);
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001590 break;
1591 case OPT_I: /* Add directory to path for includes. */
1592 if (!strcmp (arg, "-"))
1593 {
1594 /* -I- means:
1595 Use the preceding -I directories for #include "..."
1596 but not #include <...>.
1597 Don't search the directory of the present file
1598 for #include "...". (Note that -I. -I- is not the same as
1599 the default setup; -I. uses the compiler's working dir.) */
Zack Weinbergae796972000-03-31 23:16:11 +00001600 if (! CPP_OPTION (pfile, ignore_srcdir))
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001601 {
Zack Weinbergae796972000-03-31 23:16:11 +00001602 pend->quote_head = pend->brack_head;
1603 pend->quote_tail = pend->brack_tail;
1604 pend->brack_head = 0;
1605 pend->brack_tail = 0;
1606 CPP_OPTION (pfile, ignore_srcdir) = 1;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001607 }
1608 else
1609 {
Neil Boothebef4e82002-04-14 18:42:47 +00001610 cpp_error (pfile, DL_FATAL, "-I- specified twice");
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001611 return argc;
1612 }
1613 }
1614 else
Neil Boothe33f6252000-08-17 17:58:24 +00001615 append_include_chain (pfile, xstrdup (arg), BRACKET, 0);
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001616 break;
1617 case OPT_isystem:
1618 /* Add directory to beginning of system include path, as a system
Neil Booth4a58aab2000-11-18 12:18:09 +00001619 include directory. */
Neil Boothe33f6252000-08-17 17:58:24 +00001620 append_include_chain (pfile, xstrdup (arg), SYSTEM, 0);
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001621 break;
1622 case OPT_include:
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001623 case OPT_imacros:
1624 {
1625 struct pending_option *o = (struct pending_option *)
1626 xmalloc (sizeof (struct pending_option));
1627 o->arg = arg;
1628 o->next = NULL;
Zack Weinbergae796972000-03-31 23:16:11 +00001629
Neil Boothd7bc7a92001-08-21 06:20:18 +00001630 if (opt_code == OPT_include)
1631 APPEND (pend, include, o);
1632 else
1633 APPEND (pend, imacros, o);
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001634 }
1635 break;
1636 case OPT_iwithprefix:
1637 /* Add directory to end of path for includes,
1638 with the default prefix at the front of its name. */
1639 /* fall through */
1640 case OPT_iwithprefixbefore:
1641 /* Add directory to main path for includes,
1642 with the default prefix at the front of its name. */
1643 {
1644 char *fname;
1645 int len;
Zack Weinbergae796972000-03-31 23:16:11 +00001646
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001647 len = strlen (arg);
Zack Weinbergae796972000-03-31 23:16:11 +00001648
1649 if (CPP_OPTION (pfile, include_prefix) != 0)
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001650 {
Zack Weinbergae796972000-03-31 23:16:11 +00001651 size_t ipl = CPP_OPTION (pfile, include_prefix_len);
1652 fname = xmalloc (ipl + len + 1);
1653 memcpy (fname, CPP_OPTION (pfile, include_prefix), ipl);
1654 memcpy (fname + ipl, arg, len + 1);
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001655 }
Zack Weinberg60893f42000-07-06 22:52:03 +00001656 else if (cpp_GCC_INCLUDE_DIR_len)
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001657 {
Zack Weinberg60893f42000-07-06 22:52:03 +00001658 fname = xmalloc (cpp_GCC_INCLUDE_DIR_len + len + 1);
1659 memcpy (fname, cpp_GCC_INCLUDE_DIR, cpp_GCC_INCLUDE_DIR_len);
1660 memcpy (fname + cpp_GCC_INCLUDE_DIR_len, arg, len + 1);
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001661 }
Zack Weinberg60893f42000-07-06 22:52:03 +00001662 else
1663 fname = xstrdup (arg);
Zack Weinbergae796972000-03-31 23:16:11 +00001664
Neil Boothe33f6252000-08-17 17:58:24 +00001665 append_include_chain (pfile, fname,
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001666 opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0);
1667 }
1668 break;
1669 case OPT_idirafter:
1670 /* Add directory to end of path for includes. */
Neil Boothe33f6252000-08-17 17:58:24 +00001671 append_include_chain (pfile, xstrdup (arg), AFTER, 0);
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001672 break;
1673 case OPT_W:
Neil Booth4a58aab2000-11-18 12:18:09 +00001674 /* Silently ignore unrecognised options. */
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001675 if (!strcmp (argv[i], "-Wall"))
Zack Weinberg0b22d651999-03-15 18:42:46 +00001676 {
Zack Weinbergae796972000-03-31 23:16:11 +00001677 CPP_OPTION (pfile, warn_trigraphs) = 1;
1678 CPP_OPTION (pfile, warn_comments) = 1;
Zack Weinberg0b22d651999-03-15 18:42:46 +00001679 }
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001680 else if (!strcmp (argv[i], "-Wtraditional"))
Zack Weinberg07aa0b02000-04-01 22:55:25 +00001681 CPP_OPTION (pfile, warn_traditional) = 1;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001682 else if (!strcmp (argv[i], "-Wtrigraphs"))
Zack Weinbergae796972000-03-31 23:16:11 +00001683 CPP_OPTION (pfile, warn_trigraphs) = 1;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001684 else if (!strcmp (argv[i], "-Wcomment"))
Zack Weinbergae796972000-03-31 23:16:11 +00001685 CPP_OPTION (pfile, warn_comments) = 1;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001686 else if (!strcmp (argv[i], "-Wcomments"))
Zack Weinbergae796972000-03-31 23:16:11 +00001687 CPP_OPTION (pfile, warn_comments) = 1;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001688 else if (!strcmp (argv[i], "-Wundef"))
Zack Weinbergae796972000-03-31 23:16:11 +00001689 CPP_OPTION (pfile, warn_undef) = 1;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001690 else if (!strcmp (argv[i], "-Wimport"))
Zack Weinbergae796972000-03-31 23:16:11 +00001691 CPP_OPTION (pfile, warn_import) = 1;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001692 else if (!strcmp (argv[i], "-Werror"))
Zack Weinbergae796972000-03-31 23:16:11 +00001693 CPP_OPTION (pfile, warnings_are_errors) = 1;
Branko Cibej317639a2000-09-26 00:54:04 +02001694 else if (!strcmp (argv[i], "-Wsystem-headers"))
1695 CPP_OPTION (pfile, warn_system_headers) = 1;
Phil Edwards909de5d2002-03-22 21:59:04 +00001696 else if (!strcmp (argv[i], "-Wendif-labels"))
1697 CPP_OPTION (pfile, warn_endif_labels) = 1;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001698 else if (!strcmp (argv[i], "-Wno-traditional"))
Zack Weinberg07aa0b02000-04-01 22:55:25 +00001699 CPP_OPTION (pfile, warn_traditional) = 0;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001700 else if (!strcmp (argv[i], "-Wno-trigraphs"))
Zack Weinbergae796972000-03-31 23:16:11 +00001701 CPP_OPTION (pfile, warn_trigraphs) = 0;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001702 else if (!strcmp (argv[i], "-Wno-comment"))
Zack Weinbergae796972000-03-31 23:16:11 +00001703 CPP_OPTION (pfile, warn_comments) = 0;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001704 else if (!strcmp (argv[i], "-Wno-comments"))
Zack Weinbergae796972000-03-31 23:16:11 +00001705 CPP_OPTION (pfile, warn_comments) = 0;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001706 else if (!strcmp (argv[i], "-Wno-undef"))
Zack Weinbergae796972000-03-31 23:16:11 +00001707 CPP_OPTION (pfile, warn_undef) = 0;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001708 else if (!strcmp (argv[i], "-Wno-import"))
Zack Weinbergae796972000-03-31 23:16:11 +00001709 CPP_OPTION (pfile, warn_import) = 0;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001710 else if (!strcmp (argv[i], "-Wno-error"))
Zack Weinbergae796972000-03-31 23:16:11 +00001711 CPP_OPTION (pfile, warnings_are_errors) = 0;
Branko Cibej317639a2000-09-26 00:54:04 +02001712 else if (!strcmp (argv[i], "-Wno-system-headers"))
1713 CPP_OPTION (pfile, warn_system_headers) = 0;
Phil Edwards909de5d2002-03-22 21:59:04 +00001714 else if (!strcmp (argv[i], "-Wno-endif-labels"))
1715 CPP_OPTION (pfile, warn_endif_labels) = 0;
Jakub Jelinekffdeea42002-01-29 13:09:37 +01001716 else if (! ignore)
1717 return i;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001718 break;
1719 }
1720 }
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001721 return i + 1;
1722}
1723
1724/* Handle command-line options in (argc, argv).
1725 Can be called multiple times, to handle multiple sets of options.
1726 Returns if an unrecognized option is seen.
1727 Returns number of strings consumed. */
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001728int
1729cpp_handle_options (pfile, argc, argv)
1730 cpp_reader *pfile;
1731 int argc;
1732 char **argv;
1733{
1734 int i;
1735 int strings_processed;
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001736
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001737 for (i = 0; i < argc; i += strings_processed)
1738 {
Jakub Jelinekffdeea42002-01-29 13:09:37 +01001739 strings_processed = cpp_handle_option (pfile, argc - i, argv + i, 1);
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001740 if (strings_processed == 0)
1741 break;
1742 }
Neil Booth96302432001-01-07 15:17:07 +00001743
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001744 return i;
1745}
1746
Neil Booth96302432001-01-07 15:17:07 +00001747/* Extra processing when all options are parsed, after all calls to
1748 cpp_handle_option[s]. Consistency checks etc. */
1749void
1750cpp_post_options (pfile)
1751 cpp_reader *pfile;
1752{
Zack Weinbergcb773842001-03-02 00:42:28 +00001753 if (pfile->print_version)
1754 {
1755 fprintf (stderr, _("GNU CPP version %s (cpplib)"), version_string);
1756#ifdef TARGET_VERSION
1757 TARGET_VERSION;
1758#endif
1759 fputc ('\n', stderr);
1760 }
1761
Neil Booth373e2172001-02-21 07:29:56 +00001762 /* Canonicalize in_fname and out_fname. We guarantee they are not
1763 NULL, and that the empty string represents stdin / stdout. */
1764 if (CPP_OPTION (pfile, in_fname) == NULL
1765 || !strcmp (CPP_OPTION (pfile, in_fname), "-"))
1766 CPP_OPTION (pfile, in_fname) = "";
1767
1768 if (CPP_OPTION (pfile, out_fname) == NULL
1769 || !strcmp (CPP_OPTION (pfile, out_fname), "-"))
1770 CPP_OPTION (pfile, out_fname) = "";
1771
Neil Booth96302432001-01-07 15:17:07 +00001772 /* -Wtraditional is not useful in C++ mode. */
1773 if (CPP_OPTION (pfile, cplusplus))
1774 CPP_OPTION (pfile, warn_traditional) = 0;
1775
Zack Weinberg6d4587f2001-05-10 00:07:23 +00001776 /* Permanently disable macro expansion if we are rescanning
Neil Booth43612ff2002-05-18 08:23:20 +00001777 preprocessed text. Read preprocesed source in ISO mode. */
Zack Weinberg6d4587f2001-05-10 00:07:23 +00001778 if (CPP_OPTION (pfile, preprocessed))
Neil Booth43612ff2002-05-18 08:23:20 +00001779 {
1780 pfile->state.prevent_expansion = 1;
1781 CPP_OPTION (pfile, traditional) = 0;
1782 }
1783
1784 /* Traditional CPP does not accurately track column information. */
1785 if (CPP_OPTION (pfile, traditional))
1786 CPP_OPTION (pfile, show_column) = 0;
Zack Weinberg6d4587f2001-05-10 00:07:23 +00001787
Jakub Jelinek1651cc92002-05-01 22:07:36 +02001788 /* -dM makes no normal output. This is set here so that -dM -dD
1789 works as expected. */
1790 if (CPP_OPTION (pfile, dump_macros) == dump_only)
1791 CPP_OPTION (pfile, no_output) = 1;
1792
1793 /* Disable -dD, -dN and -dI if we should make no normal output
1794 (such as with -M). Allow -M -dM since some software relies on
1795 this. */
1796 if (CPP_OPTION (pfile, no_output))
1797 {
1798 if (CPP_OPTION (pfile, dump_macros) != dump_only)
1799 CPP_OPTION (pfile, dump_macros) = dump_none;
1800 CPP_OPTION (pfile, dump_includes) = 0;
1801 }
1802
Neil Booth96302432001-01-07 15:17:07 +00001803 /* We need to do this after option processing and before
1804 cpp_start_read, as cppmain.c relies on the options->no_output to
1805 set its callbacks correctly before calling cpp_start_read. */
1806 init_dependency_output (pfile);
1807
Neil Boothe5822482001-01-09 14:45:44 +00001808 /* After checking the environment variables, check if -M or -MM has
1809 not been specified, but other -M options have. */
1810 if (CPP_OPTION (pfile, print_deps) == 0 &&
1811 (CPP_OPTION (pfile, print_deps_missing_files)
1812 || CPP_OPTION (pfile, deps_file)
1813 || CPP_OPTION (pfile, deps_phony_targets)))
Neil Boothebef4e82002-04-14 18:42:47 +00001814 cpp_error (pfile, DL_FATAL,
1815 "you must additionally specify either -M or -MM");
Neil Booth96302432001-01-07 15:17:07 +00001816}
1817
Neil Booth56cd5b92002-02-20 07:24:10 +00001818/* Set up dependency-file output. On exit, if print_deps is non-zero
1819 then deps_file is not NULL; stdout is the empty string. */
Neil Booth7ca3d2b2001-01-07 11:15:13 +00001820static void
1821init_dependency_output (pfile)
1822 cpp_reader *pfile;
1823{
1824 char *spec, *s, *output_file;
1825
1826 /* Either of two environment variables can specify output of deps.
1827 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1828 where OUTPUT_FILE is the file to write deps info to
1829 and DEPS_TARGET is the target to mention in the deps. */
1830
1831 if (CPP_OPTION (pfile, print_deps) == 0)
1832 {
1833 spec = getenv ("DEPENDENCIES_OUTPUT");
1834 if (spec)
1835 CPP_OPTION (pfile, print_deps) = 1;
1836 else
1837 {
1838 spec = getenv ("SUNPRO_DEPENDENCIES");
1839 if (spec)
1840 CPP_OPTION (pfile, print_deps) = 2;
1841 else
1842 return;
1843 }
1844
1845 /* Find the space before the DEPS_TARGET, if there is one. */
1846 s = strchr (spec, ' ');
1847 if (s)
1848 {
1849 /* Let the caller perform MAKE quoting. */
1850 deps_add_target (pfile->deps, s + 1, 0);
1851 output_file = (char *) xmalloc (s - spec + 1);
1852 memcpy (output_file, spec, s - spec);
1853 output_file[s - spec] = 0;
1854 }
1855 else
1856 output_file = spec;
1857
Neil Boothab8e2222002-02-23 13:42:40 +00001858 /* Command line -MF overrides environment variables and default. */
1859 if (CPP_OPTION (pfile, deps_file) == 0)
1860 CPP_OPTION (pfile, deps_file) = output_file;
1861
Neil Booth7ca3d2b2001-01-07 11:15:13 +00001862 CPP_OPTION (pfile, print_deps_append) = 1;
1863 }
Neil Boothab8e2222002-02-23 13:42:40 +00001864 else if (CPP_OPTION (pfile, deps_file) == 0)
Neil Booth32810ba2002-03-10 21:10:21 +00001865 /* If -M or -MM was seen without -MF, default output to wherever
1866 was specified with -o. out_fname is non-NULL here. */
Neil Boothab8e2222002-02-23 13:42:40 +00001867 CPP_OPTION (pfile, deps_file) = CPP_OPTION (pfile, out_fname);
Neil Booth7ca3d2b2001-01-07 11:15:13 +00001868}
1869
Neil Booth5d8ebbd2002-01-03 21:43:09 +00001870/* Handle --help output. */
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001871static void
1872print_help ()
1873{
Donn Terryaaaf7842000-07-08 06:21:13 +00001874 /* To keep the lines from getting too long for some compilers, limit
Kazu Hirataec5c56d2001-08-01 17:57:27 +00001875 to about 500 characters (6 lines) per chunk. */
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001876 fputs (_("\
1877Switches:\n\
1878 -include <file> Include the contents of <file> before other files\n\
1879 -imacros <file> Accept definition of macros in <file>\n\
1880 -iprefix <path> Specify <path> as a prefix for next two options\n\
1881 -iwithprefix <dir> Add <dir> to the end of the system include path\n\
1882 -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\
1883 -isystem <dir> Add <dir> to the start of the system include path\n\
Donn Terryaaaf7842000-07-08 06:21:13 +00001884"), stdout);
1885 fputs (_("\
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001886 -idirafter <dir> Add <dir> to the end of the system include path\n\
1887 -I <dir> Add <dir> to the end of the main include path\n\
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001888 -I- Fine-grained include path control; see info docs\n\
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001889 -nostdinc Do not search system include directories\n\
1890 (dirs specified with -isystem will still be used)\n\
1891 -nostdinc++ Do not search system include directories for C++\n\
1892 -o <file> Put output into <file>\n\
Donn Terryaaaf7842000-07-08 06:21:13 +00001893"), stdout);
1894 fputs (_("\
Zack Weinberg041c3192000-07-04 01:58:21 +00001895 -pedantic Issue all warnings demanded by strict ISO C\n\
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001896 -pedantic-errors Issue -pedantic warnings as errors instead\n\
Zack Weinberg041c3192000-07-04 01:58:21 +00001897 -trigraphs Support ISO C trigraphs\n\
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001898 -lang-c Assume that the input sources are in C\n\
1899 -lang-c89 Assume that the input sources are in C89\n\
Donn Terryaaaf7842000-07-08 06:21:13 +00001900"), stdout);
1901 fputs (_("\
Zack Weinbergf9a0e962000-07-13 02:32:41 +00001902 -lang-c++ Assume that the input sources are in C++\n\
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001903 -lang-objc Assume that the input sources are in ObjectiveC\n\
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001904 -lang-asm Assume that the input sources are in assembler\n\
Donn Terryaaaf7842000-07-08 06:21:13 +00001905"), stdout);
1906 fputs (_("\
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001907 -std=<std name> Specify the conformance standard; one of:\n\
Ulrich Drepper916269a2000-01-29 19:00:43 +00001908 gnu89, gnu99, c89, c99, iso9899:1990,\n\
1909 iso9899:199409, iso9899:1999\n\
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001910 -w Inhibit warning messages\n\
1911 -Wtrigraphs Warn if trigraphs are encountered\n\
1912 -Wno-trigraphs Do not warn about trigraphs\n\
1913 -Wcomment{s} Warn if one comment starts inside another\n\
Donn Terryaaaf7842000-07-08 06:21:13 +00001914"), stdout);
1915 fputs (_("\
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001916 -Wno-comment{s} Do not warn about comments\n\
Zack Weinbergf9a0e962000-07-13 02:32:41 +00001917 -Wtraditional Warn about features not present in traditional C\n\
1918 -Wno-traditional Do not warn about traditional C\n\
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001919 -Wundef Warn if an undefined macro is used by #if\n\
1920 -Wno-undef Do not warn about testing undefined macros\n\
1921 -Wimport Warn about the use of the #import directive\n\
Donn Terryaaaf7842000-07-08 06:21:13 +00001922"), stdout);
1923 fputs (_("\
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001924 -Wno-import Do not warn about the use of #import\n\
1925 -Werror Treat all warnings as errors\n\
1926 -Wno-error Do not treat warnings as errors\n\
Branko Cibej317639a2000-09-26 00:54:04 +02001927 -Wsystem-headers Do not suppress warnings from system headers\n\
1928 -Wno-system-headers Suppress warnings from system headers\n\
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001929 -Wall Enable all preprocessor warnings\n\
Donn Terryaaaf7842000-07-08 06:21:13 +00001930"), stdout);
1931 fputs (_("\
Branko Cibej317639a2000-09-26 00:54:04 +02001932 -M Generate make dependencies\n\
1933 -MM As -M, but ignore system header files\n\
Neil Booth576786b2002-03-16 10:57:28 +00001934 -MD Generate make dependencies and compile\n\
1935 -MMD As -MD, but ignore system header files\n\
Neil Booth96302432001-01-07 15:17:07 +00001936 -MF <file> Write dependency output to the given file\n\
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001937 -MG Treat missing header file as generated files\n\
Neil Booth96302432001-01-07 15:17:07 +00001938"), stdout);
1939 fputs (_("\
1940 -MP Generate phony targets for all headers\n\
1941 -MQ <target> Add a MAKE-quoted target\n\
1942 -MT <target> Add an unquoted target\n\
Donn Terryaaaf7842000-07-08 06:21:13 +00001943"), stdout);
1944 fputs (_("\
Branko Cibej317639a2000-09-26 00:54:04 +02001945 -D<macro> Define a <macro> with string '1' as its value\n\
1946 -D<macro>=<val> Define a <macro> with <val> as its value\n\
Neil Booth576786b2002-03-16 10:57:28 +00001947 -A<question>=<answer> Assert the <answer> to <question>\n\
1948 -A-<question>=<answer> Disable the <answer> to <question>\n\
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001949 -U<macro> Undefine <macro> \n\
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001950 -v Display the version number\n\
Donn Terryaaaf7842000-07-08 06:21:13 +00001951"), stdout);
1952 fputs (_("\
Branko Cibej317639a2000-09-26 00:54:04 +02001953 -H Print the name of header files as they are used\n\
1954 -C Do not discard comments\n\
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001955 -dM Display a list of macro definitions active at end\n\
1956 -dD Preserve macro definitions in output\n\
1957 -dN As -dD except that only the names are preserved\n\
1958 -dI Include #include directives in the output\n\
Branko Cibej317639a2000-09-26 00:54:04 +02001959"), stdout);
1960 fputs (_("\
Neil Booth3bce8a02001-06-09 22:55:49 +00001961 -fpreprocessed Treat the input file as already preprocessed\n\
Neil Booth6ab3e7d2000-05-18 11:09:27 +00001962 -ftabstop=<number> Distance between tab stops for column reporting\n\
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001963 -P Do not generate #line directives\n\
1964 -$ Do not allow '$' in identifiers\n\
Neil Booth576786b2002-03-16 10:57:28 +00001965 -remap Remap file names when including files\n\
Zack Weinberge23c0ba2000-03-07 23:11:06 +00001966 --version Display version information\n\
Zack Weinberg6de1e2a1999-02-18 15:35:49 +00001967 -h or --help Display this information\n\
1968"), stdout);
1969}