Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 1 | /* CPP Library. |
Jeff Law | 5e7b4e2 | 2000-02-25 22:59:31 -0700 | [diff] [blame] | 2 | Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, |
| 3 | 1999, 2000 Free Software Foundation, Inc. |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 4 | Contributed by Per Bothner, 1994-95. |
| 5 | Based on CCCP program by Paul Rubin, June 1986 |
| 6 | Adapted to ANSI C, Richard Stallman, Jan 1987 |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify it |
| 9 | under the terms of the GNU General Public License as published by the |
| 10 | Free Software Foundation; either version 2, or (at your option) any |
| 11 | later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 21 | |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 22 | #include "config.h" |
| 23 | #include "system.h" |
| 24 | |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 25 | #include "cpplib.h" |
| 26 | #include "cpphash.h" |
| 27 | #include "output.h" |
| 28 | #include "prefix.h" |
| 29 | #include "intl.h" |
Kaveh R. Ghazi | 9f8f4ef | 2000-02-15 16:36:35 +0000 | [diff] [blame] | 30 | #include "version.h" |
Zack Weinberg | 49e6c08 | 2000-03-04 19:42:04 +0000 | [diff] [blame] | 31 | #include "mkdeps.h" |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 32 | |
| 33 | /* Predefined symbols, built-in macros, and the default include path. */ |
| 34 | |
| 35 | #ifndef GET_ENV_PATH_LIST |
| 36 | #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0) |
| 37 | #endif |
| 38 | |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 39 | #ifndef STANDARD_INCLUDE_DIR |
| 40 | #define STANDARD_INCLUDE_DIR "/usr/include" |
| 41 | #endif |
| 42 | |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 43 | /* We let tm.h override the types used here, to handle trivial differences |
| 44 | such as the choice of unsigned int or long unsigned int for size_t. |
| 45 | When machines start needing nontrivial differences in the size type, |
| 46 | it would be best to do something here to figure out automatically |
| 47 | from other information what type to use. */ |
| 48 | |
| 49 | /* The string value for __SIZE_TYPE__. */ |
| 50 | |
| 51 | #ifndef SIZE_TYPE |
| 52 | #define SIZE_TYPE "long unsigned int" |
| 53 | #endif |
| 54 | |
| 55 | /* The string value for __PTRDIFF_TYPE__. */ |
| 56 | |
| 57 | #ifndef PTRDIFF_TYPE |
| 58 | #define PTRDIFF_TYPE "long int" |
| 59 | #endif |
| 60 | |
| 61 | /* The string value for __WCHAR_TYPE__. */ |
| 62 | |
| 63 | #ifndef WCHAR_TYPE |
| 64 | #define WCHAR_TYPE "int" |
| 65 | #endif |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 66 | |
| 67 | /* The string value for __USER_LABEL_PREFIX__ */ |
| 68 | |
| 69 | #ifndef USER_LABEL_PREFIX |
| 70 | #define USER_LABEL_PREFIX "" |
| 71 | #endif |
| 72 | |
| 73 | /* The string value for __REGISTER_PREFIX__ */ |
| 74 | |
| 75 | #ifndef REGISTER_PREFIX |
| 76 | #define REGISTER_PREFIX "" |
| 77 | #endif |
| 78 | |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 79 | /* This is the default list of directories to search for include files. |
| 80 | It may be overridden by the various -I and -ixxx options. |
| 81 | |
| 82 | #include "file" looks in the same directory as the current file, |
| 83 | then this list. |
| 84 | #include <file> just looks in this list. |
| 85 | |
| 86 | All these directories are treated as `system' include directories |
| 87 | (they are not subject to pedantic warnings in some cases). */ |
| 88 | |
Zack Weinberg | 455d258 | 2000-03-04 01:42:56 +0000 | [diff] [blame] | 89 | struct default_include |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 90 | { |
Kaveh R. Ghazi | bcc5cac | 1999-09-07 15:41:26 +0000 | [diff] [blame] | 91 | const char *fname; /* The name of the directory. */ |
| 92 | const char *component; /* The component containing the directory |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 93 | (see update_path in prefix.c) */ |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 94 | int cplusplus; /* Only look here if we're compiling C++. */ |
| 95 | int cxx_aware; /* Includes in this directory don't need to |
| 96 | be wrapped in extern "C" when compiling |
Zack Weinberg | c45da1c | 2000-03-02 20:14:32 +0000 | [diff] [blame] | 97 | C++. */ |
Zack Weinberg | 455d258 | 2000-03-04 01:42:56 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | static const struct default_include include_defaults_array[] |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 101 | #ifdef INCLUDE_DEFAULTS |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 102 | = INCLUDE_DEFAULTS; |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 103 | #else |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 104 | = { |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 105 | /* Pick up GNU C++ specific include files. */ |
| 106 | { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1 }, |
| 107 | #ifdef CROSS_COMPILE |
| 108 | /* This is the dir for fixincludes. Put it just before |
| 109 | the files that we fix. */ |
| 110 | { GCC_INCLUDE_DIR, "GCC", 0, 0 }, |
| 111 | /* For cross-compilation, this dir name is generated |
| 112 | automatically in Makefile.in. */ |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 113 | { CROSS_INCLUDE_DIR, "GCC", 0, 0 }, |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 114 | #ifdef TOOL_INCLUDE_DIR |
| 115 | /* This is another place that the target system's headers might be. */ |
| 116 | { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 }, |
| 117 | #endif |
| 118 | #else /* not CROSS_COMPILE */ |
| 119 | #ifdef LOCAL_INCLUDE_DIR |
| 120 | /* This should be /usr/local/include and should come before |
| 121 | the fixincludes-fixed header files. */ |
| 122 | { LOCAL_INCLUDE_DIR, 0, 0, 1 }, |
| 123 | #endif |
| 124 | #ifdef TOOL_INCLUDE_DIR |
| 125 | /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here. |
| 126 | Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */ |
| 127 | { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 }, |
| 128 | #endif |
| 129 | /* This is the dir for fixincludes. Put it just before |
| 130 | the files that we fix. */ |
| 131 | { GCC_INCLUDE_DIR, "GCC", 0, 0 }, |
| 132 | /* Some systems have an extra dir of include files. */ |
| 133 | #ifdef SYSTEM_INCLUDE_DIR |
| 134 | { SYSTEM_INCLUDE_DIR, 0, 0, 0 }, |
| 135 | #endif |
| 136 | #ifndef STANDARD_INCLUDE_COMPONENT |
| 137 | #define STANDARD_INCLUDE_COMPONENT 0 |
| 138 | #endif |
| 139 | { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0 }, |
| 140 | #endif /* not CROSS_COMPILE */ |
| 141 | { 0, 0, 0, 0 } |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 142 | }; |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 143 | #endif /* no INCLUDE_DEFAULTS */ |
| 144 | |
| 145 | /* Internal structures and prototypes. */ |
| 146 | |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 147 | /* A `struct pending_option' remembers one -D, -A, -U, -include, or -imacros |
| 148 | switch. There are four lists: one for -D and -U, one for -A, one |
| 149 | for -include, one for -imacros. `undef' is set for -U, clear for |
| 150 | -D, ignored for the others. |
| 151 | (Future: add an equivalent of -U for -A) */ |
| 152 | struct pending_option |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 153 | { |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 154 | struct pending_option *next; |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 155 | char *arg; |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 156 | int undef; |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 157 | }; |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 158 | |
| 159 | #ifdef __STDC__ |
| 160 | #define APPEND(pend, list, elt) \ |
| 161 | do { if (!(pend)->list##_head) (pend)->list##_head = (elt); \ |
| 162 | else (pend)->list##_tail->next = (elt); \ |
| 163 | (pend)->list##_tail = (elt); \ |
| 164 | } while (0) |
| 165 | #else |
| 166 | #define APPEND(pend, list, elt) \ |
| 167 | do { if (!(pend)->list/**/_head) (pend)->list/**/_head = (elt); \ |
| 168 | else (pend)->list/**/_tail->next = (elt); \ |
| 169 | (pend)->list/**/_tail = (elt); \ |
| 170 | } while (0) |
| 171 | #endif |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 172 | |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 173 | static void print_help PARAMS ((void)); |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 174 | static void path_include PARAMS ((cpp_reader *, |
| 175 | struct cpp_pending *, |
| 176 | char *, int)); |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 177 | static void initialize_builtins PARAMS ((cpp_reader *)); |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 178 | static void append_include_chain PARAMS ((cpp_reader *, |
| 179 | struct cpp_pending *, |
Zack Weinberg | c45da1c | 2000-03-02 20:14:32 +0000 | [diff] [blame] | 180 | char *, int, int)); |
Kaveh R. Ghazi | bcc5cac | 1999-09-07 15:41:26 +0000 | [diff] [blame] | 181 | static void dump_special_to_buffer PARAMS ((cpp_reader *, const char *)); |
| 182 | static void initialize_dependency_output PARAMS ((cpp_reader *)); |
Zack Weinberg | c45da1c | 2000-03-02 20:14:32 +0000 | [diff] [blame] | 183 | static void initialize_standard_includes PARAMS ((cpp_reader *)); |
Zack Weinberg | 40ea76d | 2000-02-06 07:30:25 +0000 | [diff] [blame] | 184 | static void new_pending_define PARAMS ((struct cpp_options *, |
| 185 | const char *)); |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame^] | 186 | #ifdef HOST_EBCDIC |
| 187 | static int opt_comp PARAMS ((const void *, const void *)); |
| 188 | #endif |
| 189 | static int parse_option PARAMS ((const char *)); |
Zack Weinberg | b0699da | 2000-03-07 20:58:47 +0000 | [diff] [blame] | 190 | static int handle_option PARAMS ((cpp_reader *, int, char **)); |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 191 | |
Zack Weinberg | c45da1c | 2000-03-02 20:14:32 +0000 | [diff] [blame] | 192 | /* Fourth argument to append_include_chain: chain to use */ |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 193 | enum { QUOTE = 0, BRACKET, SYSTEM, AFTER }; |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 194 | |
Zack Weinberg | 455d258 | 2000-03-04 01:42:56 +0000 | [diff] [blame] | 195 | /* If we have designated initializers (GCC >2.7, or C99) this table |
| 196 | can be initialized, constant data. Otherwise, it has to be filled |
| 197 | in at runtime. */ |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 198 | |
Zack Weinberg | 455d258 | 2000-03-04 01:42:56 +0000 | [diff] [blame] | 199 | #if (GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L) |
| 200 | #define init_IStable() /* nothing */ |
| 201 | #define ISTABLE const unsigned char _cpp_IStable[256] = { |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 202 | #define END }; |
Zack Weinberg | 455d258 | 2000-03-04 01:42:56 +0000 | [diff] [blame] | 203 | #define s(p, v) [p] = v, |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 204 | #else |
Zack Weinberg | 455d258 | 2000-03-04 01:42:56 +0000 | [diff] [blame] | 205 | #define ISTABLE unsigned char _cpp_IStable[256] = { 0 }; \ |
| 206 | static void init_IStable PARAMS ((void)) { \ |
Zack Weinberg | c6491210 | 2000-03-04 17:53:04 +0000 | [diff] [blame] | 207 | unsigned char *x = _cpp_IStable; |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 208 | #define END } |
Zack Weinberg | 455d258 | 2000-03-04 01:42:56 +0000 | [diff] [blame] | 209 | #define s(p, v) x[p] = v; |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 210 | #endif |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 211 | |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 212 | #define A(x) s(x, ISidnum|ISidstart) |
| 213 | #define N(x) s(x, ISidnum|ISnumstart) |
| 214 | #define H(x) s(x, IShspace|ISspace) |
| 215 | #define S(x) s(x, ISspace) |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 216 | |
Zack Weinberg | 455d258 | 2000-03-04 01:42:56 +0000 | [diff] [blame] | 217 | ISTABLE |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 218 | A('_') |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 219 | |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 220 | A('a') A('b') A('c') A('d') A('e') A('f') A('g') A('h') A('i') |
| 221 | A('j') A('k') A('l') A('m') A('n') A('o') A('p') A('q') A('r') |
| 222 | A('s') A('t') A('u') A('v') A('w') A('x') A('y') A('z') |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 223 | |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 224 | A('A') A('B') A('C') A('D') A('E') A('F') A('G') A('H') A('I') |
| 225 | A('J') A('K') A('L') A('M') A('N') A('O') A('P') A('Q') A('R') |
| 226 | A('S') A('T') A('U') A('V') A('W') A('X') A('Y') A('Z') |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 227 | |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 228 | N('1') N('2') N('3') N('4') N('5') N('6') N('7') N('8') N('9') N('0') |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 229 | |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 230 | H(' ') H('\t') H('\v') H('\f') |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 231 | |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 232 | S('\n') |
| 233 | END |
Zack Weinberg | 5538ada | 1999-02-04 06:36:54 -0500 | [diff] [blame] | 234 | |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 235 | #undef A |
| 236 | #undef N |
| 237 | #undef H |
| 238 | #undef S |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 239 | #undef s |
Zack Weinberg | 455d258 | 2000-03-04 01:42:56 +0000 | [diff] [blame] | 240 | #undef ISTABLE |
| 241 | #undef END |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 242 | |
| 243 | /* Given a colon-separated list of file names PATH, |
| 244 | add all the names to the search path for include files. */ |
| 245 | |
| 246 | static void |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 247 | path_include (pfile, pend, list, path) |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 248 | cpp_reader *pfile; |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 249 | struct cpp_pending *pend; |
| 250 | char *list; |
| 251 | int path; |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 252 | { |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 253 | char *p, *q, *name; |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 254 | |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 255 | p = list; |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 256 | |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 257 | do |
| 258 | { |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 259 | /* Find the end of this name. */ |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 260 | q = p; |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 261 | while (*q != 0 && *q != PATH_SEPARATOR) q++; |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 262 | if (q == p) |
| 263 | { |
| 264 | /* An empty name in the path stands for the current directory. */ |
| 265 | name = (char *) xmalloc (2); |
| 266 | name[0] = '.'; |
| 267 | name[1] = 0; |
| 268 | } |
| 269 | else |
| 270 | { |
| 271 | /* Otherwise use the directory that is named. */ |
| 272 | name = (char *) xmalloc (q - p + 1); |
| 273 | memcpy (name, p, q - p); |
| 274 | name[q - p] = 0; |
| 275 | } |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 276 | |
Zack Weinberg | c45da1c | 2000-03-02 20:14:32 +0000 | [diff] [blame] | 277 | append_include_chain (pfile, pend, name, path, 0); |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 278 | |
| 279 | /* Advance past this name. */ |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 280 | if (*q == 0) |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 281 | break; |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 282 | p = q + 1; |
| 283 | } |
| 284 | while (1); |
| 285 | } |
| 286 | |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 287 | /* Append DIR to include path PATH. DIR must be permanently allocated |
| 288 | and writable. */ |
| 289 | static void |
Zack Weinberg | c45da1c | 2000-03-02 20:14:32 +0000 | [diff] [blame] | 290 | append_include_chain (pfile, pend, dir, path, cxx_aware) |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 291 | cpp_reader *pfile; |
| 292 | struct cpp_pending *pend; |
| 293 | char *dir; |
| 294 | int path; |
Zack Weinberg | c45da1c | 2000-03-02 20:14:32 +0000 | [diff] [blame] | 295 | int cxx_aware; |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 296 | { |
| 297 | struct file_name_list *new; |
| 298 | struct stat st; |
| 299 | unsigned int len; |
| 300 | |
Zack Weinberg | b0699da | 2000-03-07 20:58:47 +0000 | [diff] [blame] | 301 | _cpp_simplify_pathname (dir); |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 302 | if (stat (dir, &st)) |
| 303 | { |
| 304 | /* Dirs that don't exist are silently ignored. */ |
| 305 | if (errno != ENOENT) |
Zack Weinberg | c1212d2 | 2000-02-06 23:46:18 +0000 | [diff] [blame] | 306 | cpp_notice_from_errno (pfile, dir); |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 307 | else if (CPP_OPTIONS (pfile)->verbose) |
Zack Weinberg | c1212d2 | 2000-02-06 23:46:18 +0000 | [diff] [blame] | 308 | fprintf (stderr, _("ignoring nonexistent directory `%s'\n"), dir); |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 309 | return; |
| 310 | } |
| 311 | |
| 312 | if (!S_ISDIR (st.st_mode)) |
| 313 | { |
Zack Weinberg | c1212d2 | 2000-02-06 23:46:18 +0000 | [diff] [blame] | 314 | cpp_notice (pfile, "%s: Not a directory", dir); |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 315 | return; |
| 316 | } |
| 317 | |
| 318 | len = strlen (dir); |
| 319 | if (len > pfile->max_include_len) |
| 320 | pfile->max_include_len = len; |
| 321 | |
| 322 | new = (struct file_name_list *)xmalloc (sizeof (struct file_name_list)); |
| 323 | new->name = dir; |
| 324 | new->nlen = len; |
| 325 | new->ino = st.st_ino; |
| 326 | new->dev = st.st_dev; |
Zack Weinberg | c45da1c | 2000-03-02 20:14:32 +0000 | [diff] [blame] | 327 | if (path == SYSTEM) |
| 328 | new->sysp = cxx_aware ? 1 : 2; |
| 329 | else |
| 330 | new->sysp = 0; |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 331 | new->name_map = NULL; |
Dave Brolley | 503cb43 | 1999-09-13 16:58:44 +0000 | [diff] [blame] | 332 | new->next = NULL; |
| 333 | new->alloc = NULL; |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 334 | |
| 335 | switch (path) |
| 336 | { |
| 337 | case QUOTE: APPEND (pend, quote, new); break; |
| 338 | case BRACKET: APPEND (pend, brack, new); break; |
| 339 | case SYSTEM: APPEND (pend, systm, new); break; |
| 340 | case AFTER: APPEND (pend, after, new); break; |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 344 | |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 345 | /* Write out a #define command for the special named MACRO_NAME |
| 346 | to PFILE's token_buffer. */ |
| 347 | |
| 348 | static void |
| 349 | dump_special_to_buffer (pfile, macro_name) |
| 350 | cpp_reader *pfile; |
Kaveh R. Ghazi | bcc5cac | 1999-09-07 15:41:26 +0000 | [diff] [blame] | 351 | const char *macro_name; |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 352 | { |
Zack Weinberg | c45da1c | 2000-03-02 20:14:32 +0000 | [diff] [blame] | 353 | static const char define_directive[] = "#define "; |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 354 | int macro_name_length = strlen (macro_name); |
Zack Weinberg | 80e9dcb | 1999-04-19 11:55:04 +0000 | [diff] [blame] | 355 | output_line_command (pfile, same_file); |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 356 | CPP_RESERVE (pfile, sizeof(define_directive) + macro_name_length); |
| 357 | CPP_PUTS_Q (pfile, define_directive, sizeof(define_directive)-1); |
| 358 | CPP_PUTS_Q (pfile, macro_name, macro_name_length); |
| 359 | CPP_PUTC_Q (pfile, ' '); |
| 360 | cpp_expand_to_buffer (pfile, macro_name, macro_name_length); |
| 361 | CPP_PUTC (pfile, '\n'); |
| 362 | } |
| 363 | |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 364 | /* Initialize a cpp_options structure. */ |
| 365 | void |
| 366 | cpp_options_init (opts) |
| 367 | cpp_options *opts; |
| 368 | { |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 369 | bzero ((char *) opts, sizeof (struct cpp_options)); |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 370 | |
| 371 | opts->dollars_in_ident = 1; |
| 372 | opts->cplusplus_comments = 1; |
| 373 | opts->warn_import = 1; |
Zack Weinberg | 564ad5f | 2000-02-10 00:26:47 +0000 | [diff] [blame] | 374 | opts->discard_comments = 1; |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 375 | |
Kaveh R. Ghazi | ad85216 | 1999-09-07 02:36:41 +0000 | [diff] [blame] | 376 | opts->pending = |
| 377 | (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending)); |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | /* Initialize a cpp_reader structure. */ |
| 381 | void |
| 382 | cpp_reader_init (pfile) |
| 383 | cpp_reader *pfile; |
| 384 | { |
| 385 | bzero ((char *) pfile, sizeof (cpp_reader)); |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 386 | |
| 387 | pfile->token_buffer_size = 200; |
| 388 | pfile->token_buffer = (U_CHAR *) xmalloc (pfile->token_buffer_size); |
| 389 | CPP_SET_WRITTEN (pfile, 0); |
Zack Weinberg | 122ae89 | 1999-02-25 14:24:40 +0000 | [diff] [blame] | 390 | |
| 391 | pfile->hashtab = (HASHNODE **) xcalloc (HASHSIZE, sizeof (HASHNODE *)); |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /* Free resources used by PFILE. |
| 395 | This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */ |
| 396 | void |
| 397 | cpp_cleanup (pfile) |
| 398 | cpp_reader *pfile; |
| 399 | { |
| 400 | int i; |
| 401 | while (CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile)) |
| 402 | cpp_pop_buffer (pfile); |
| 403 | |
| 404 | if (pfile->token_buffer) |
| 405 | { |
| 406 | free (pfile->token_buffer); |
| 407 | pfile->token_buffer = NULL; |
| 408 | } |
| 409 | |
Zack Weinberg | 2c82621 | 1999-05-10 11:24:36 -0400 | [diff] [blame] | 410 | if (pfile->input_buffer) |
| 411 | { |
| 412 | free (pfile->input_buffer); |
| 413 | free (pfile->input_speccase); |
| 414 | pfile->input_buffer = pfile->input_speccase = NULL; |
| 415 | pfile->input_buffer_len = 0; |
| 416 | } |
| 417 | |
Zack Weinberg | 49e6c08 | 2000-03-04 19:42:04 +0000 | [diff] [blame] | 418 | if (pfile->deps) |
| 419 | deps_free (pfile->deps); |
| 420 | |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 421 | while (pfile->if_stack) |
| 422 | { |
| 423 | IF_STACK_FRAME *temp = pfile->if_stack; |
| 424 | pfile->if_stack = temp->next; |
| 425 | free (temp); |
| 426 | } |
| 427 | |
| 428 | for (i = ALL_INCLUDE_HASHSIZE; --i >= 0; ) |
| 429 | { |
| 430 | struct include_hash *imp = pfile->all_include_files[i]; |
| 431 | while (imp) |
| 432 | { |
| 433 | struct include_hash *next = imp->next; |
Zack Weinberg | 49e6c08 | 2000-03-04 19:42:04 +0000 | [diff] [blame] | 434 | |
| 435 | free ((PTR) imp->name); |
Zack Weinberg | b0699da | 2000-03-07 20:58:47 +0000 | [diff] [blame] | 436 | free ((PTR) imp->nshort); |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 437 | free (imp); |
| 438 | imp = next; |
| 439 | } |
| 440 | pfile->all_include_files[i] = 0; |
| 441 | } |
| 442 | |
Zack Weinberg | 122ae89 | 1999-02-25 14:24:40 +0000 | [diff] [blame] | 443 | for (i = HASHSIZE; --i >= 0;) |
| 444 | { |
| 445 | while (pfile->hashtab[i]) |
Zack Weinberg | b0699da | 2000-03-07 20:58:47 +0000 | [diff] [blame] | 446 | _cpp_delete_macro (pfile->hashtab[i]); |
Zack Weinberg | 122ae89 | 1999-02-25 14:24:40 +0000 | [diff] [blame] | 447 | } |
| 448 | free (pfile->hashtab); |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 452 | /* This structure defines one built-in macro. A node of type TYPE will |
| 453 | be entered in the macro hash table under the name NAME, with value |
| 454 | VALUE (if any). FLAGS tweaks the behavior a little: |
| 455 | DUMP write debug info for this macro |
| 456 | STDC define only if not -traditional |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 457 | ULP value is the global user_label_prefix (which can't be |
| 458 | put directly into the table). |
| 459 | */ |
| 460 | |
| 461 | struct builtin |
| 462 | { |
| 463 | const char *name; |
| 464 | const char *value; |
| 465 | unsigned short type; |
| 466 | unsigned short flags; |
| 467 | }; |
| 468 | #define DUMP 0x01 |
| 469 | #define STDC 0x02 |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 470 | #define ULP 0x10 |
| 471 | |
| 472 | static const struct builtin builtin_array[] = |
| 473 | { |
| 474 | { "__TIME__", 0, T_TIME, DUMP }, |
| 475 | { "__DATE__", 0, T_DATE, DUMP }, |
| 476 | { "__FILE__", 0, T_FILE, 0 }, |
Zack Weinberg | c45da1c | 2000-03-02 20:14:32 +0000 | [diff] [blame] | 477 | { "__BASE_FILE__", 0, T_BASE_FILE, 0 }, |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 478 | { "__LINE__", 0, T_SPECLINE, 0 }, |
| 479 | { "__INCLUDE_LEVEL__", 0, T_INCLUDE_LEVEL, 0 }, |
| 480 | { "__VERSION__", 0, T_VERSION, DUMP }, |
| 481 | { "__STDC__", 0, T_STDC, DUMP|STDC }, |
| 482 | |
| 483 | { "__USER_LABEL_PREFIX__", 0, T_CONST, ULP }, |
| 484 | { "__REGISTER_PREFIX__", REGISTER_PREFIX, T_CONST, 0 }, |
| 485 | { "__HAVE_BUILTIN_SETJMP__", "1", T_CONST, 0 }, |
| 486 | #ifndef NO_BUILTIN_SIZE_TYPE |
| 487 | { "__SIZE_TYPE__", SIZE_TYPE, T_CONST, DUMP }, |
| 488 | #endif |
| 489 | #ifndef NO_BUILTIN_PTRDIFF_TYPE |
| 490 | { "__PTRDIFF_TYPE__", PTRDIFF_TYPE, T_CONST, DUMP }, |
| 491 | #endif |
Zack Weinberg | 0209c34 | 2000-02-28 21:09:54 +0000 | [diff] [blame] | 492 | #ifndef NO_BUILTIN_WCHAR_TYPE |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 493 | { "__WCHAR_TYPE__", WCHAR_TYPE, T_CONST, DUMP }, |
Zack Weinberg | 0209c34 | 2000-02-28 21:09:54 +0000 | [diff] [blame] | 494 | #endif |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 495 | { 0, 0, 0, 0 } |
| 496 | }; |
| 497 | |
| 498 | /* Subroutine of cpp_start_read; reads the builtins table above and |
| 499 | enters the macros into the hash table. */ |
| 500 | |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 501 | static void |
| 502 | initialize_builtins (pfile) |
| 503 | cpp_reader *pfile; |
| 504 | { |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 505 | int len; |
| 506 | const struct builtin *b; |
| 507 | const char *val; |
| 508 | for(b = builtin_array; b->name; b++) |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 509 | { |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 510 | if ((b->flags & STDC) && CPP_TRADITIONAL (pfile)) |
| 511 | continue; |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 512 | |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 513 | val = (b->flags & ULP) ? user_label_prefix : b->value; |
| 514 | len = strlen (b->name); |
| 515 | |
Zack Weinberg | b0699da | 2000-03-07 20:58:47 +0000 | [diff] [blame] | 516 | _cpp_install (pfile, b->name, len, b->type, val); |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 517 | if ((b->flags & DUMP) && CPP_OPTIONS (pfile)->debug_output) |
| 518 | dump_special_to_buffer (pfile, b->name); |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 519 | } |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 520 | |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 521 | } |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 522 | #undef DUMP |
| 523 | #undef STDC |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 524 | #undef ULP |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 525 | |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 526 | /* Another subroutine of cpp_start_read. This one sets up to do |
| 527 | dependency-file output. */ |
| 528 | static void |
| 529 | initialize_dependency_output (pfile) |
| 530 | cpp_reader *pfile; |
| 531 | { |
| 532 | cpp_options *opts = CPP_OPTIONS (pfile); |
| 533 | char *spec, *s, *output_file; |
| 534 | |
| 535 | /* Either of two environment variables can specify output of deps. |
| 536 | Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET", |
| 537 | where OUTPUT_FILE is the file to write deps info to |
| 538 | and DEPS_TARGET is the target to mention in the deps. */ |
| 539 | |
| 540 | if (opts->print_deps == 0) |
| 541 | { |
| 542 | spec = getenv ("DEPENDENCIES_OUTPUT"); |
| 543 | if (spec) |
| 544 | opts->print_deps = 1; |
| 545 | else |
| 546 | { |
| 547 | spec = getenv ("SUNPRO_DEPENDENCIES"); |
| 548 | if (spec) |
| 549 | opts->print_deps = 2; |
| 550 | else |
| 551 | return; |
| 552 | } |
| 553 | |
| 554 | /* Find the space before the DEPS_TARGET, if there is one. */ |
| 555 | s = strchr (spec, ' '); |
| 556 | if (s) |
| 557 | { |
| 558 | opts->deps_target = s + 1; |
| 559 | output_file = (char *) xmalloc (s - spec + 1); |
| 560 | memcpy (output_file, spec, s - spec); |
| 561 | output_file[s - spec] = 0; |
| 562 | } |
| 563 | else |
| 564 | { |
| 565 | opts->deps_target = 0; |
| 566 | output_file = spec; |
| 567 | } |
| 568 | |
| 569 | opts->deps_file = output_file; |
| 570 | opts->print_deps_append = 1; |
| 571 | } |
| 572 | |
Zack Weinberg | 49e6c08 | 2000-03-04 19:42:04 +0000 | [diff] [blame] | 573 | pfile->deps = deps_init (); |
| 574 | |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 575 | /* Print the expected object file name as the target of this Make-rule. */ |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 576 | if (opts->deps_target) |
Zack Weinberg | 49e6c08 | 2000-03-04 19:42:04 +0000 | [diff] [blame] | 577 | deps_add_target (pfile->deps, opts->deps_target); |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 578 | else if (*opts->in_fname == 0) |
Zack Weinberg | 49e6c08 | 2000-03-04 19:42:04 +0000 | [diff] [blame] | 579 | deps_add_target (pfile->deps, "-"); |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 580 | else |
Zack Weinberg | 49e6c08 | 2000-03-04 19:42:04 +0000 | [diff] [blame] | 581 | deps_calc_target (pfile->deps, opts->in_fname); |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 582 | |
Zack Weinberg | 49e6c08 | 2000-03-04 19:42:04 +0000 | [diff] [blame] | 583 | if (opts->in_fname) |
| 584 | deps_add_dep (pfile->deps, opts->in_fname); |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Zack Weinberg | c45da1c | 2000-03-02 20:14:32 +0000 | [diff] [blame] | 587 | /* And another subroutine. This one sets up the standard include path. */ |
| 588 | static void |
| 589 | initialize_standard_includes (pfile) |
| 590 | cpp_reader *pfile; |
| 591 | { |
| 592 | cpp_options *opts = CPP_OPTIONS (pfile); |
| 593 | char *path; |
Zack Weinberg | 455d258 | 2000-03-04 01:42:56 +0000 | [diff] [blame] | 594 | const struct default_include *p; |
Zack Weinberg | c45da1c | 2000-03-02 20:14:32 +0000 | [diff] [blame] | 595 | char *specd_prefix = opts->include_prefix; |
| 596 | |
| 597 | /* Several environment variables may add to the include search path. |
| 598 | CPATH specifies an additional list of directories to be searched |
| 599 | as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH, |
| 600 | etc. specify an additional list of directories to be searched as |
| 601 | if specified with -isystem, for the language indicated. */ |
| 602 | |
| 603 | GET_ENV_PATH_LIST (path, "CPATH"); |
| 604 | if (path != 0 && *path != 0) |
| 605 | path_include (pfile, opts->pending, path, BRACKET); |
| 606 | |
| 607 | switch ((opts->objc << 1) + opts->cplusplus) |
| 608 | { |
| 609 | case 0: |
| 610 | GET_ENV_PATH_LIST (path, "C_INCLUDE_PATH"); |
| 611 | break; |
| 612 | case 1: |
| 613 | GET_ENV_PATH_LIST (path, "CPLUS_INCLUDE_PATH"); |
| 614 | break; |
| 615 | case 2: |
| 616 | GET_ENV_PATH_LIST (path, "OBJC_INCLUDE_PATH"); |
| 617 | break; |
| 618 | case 3: |
| 619 | GET_ENV_PATH_LIST (path, "OBJCPLUS_INCLUDE_PATH"); |
| 620 | break; |
| 621 | } |
| 622 | if (path != 0 && *path != 0) |
| 623 | path_include (pfile, opts->pending, path, SYSTEM); |
| 624 | |
| 625 | /* Search "translated" versions of GNU directories. |
| 626 | These have /usr/local/lib/gcc... replaced by specd_prefix. */ |
| 627 | if (specd_prefix != 0) |
| 628 | { |
| 629 | char *default_prefix = alloca (sizeof GCC_INCLUDE_DIR - 7); |
| 630 | /* Remove the `include' from /usr/local/lib/gcc.../include. |
| 631 | GCC_INCLUDE_DIR will always end in /include. */ |
| 632 | int default_len = sizeof GCC_INCLUDE_DIR - 8; |
| 633 | int specd_len = strlen (specd_prefix); |
| 634 | |
| 635 | memcpy (default_prefix, GCC_INCLUDE_DIR, default_len); |
| 636 | default_prefix[default_len] = '\0'; |
| 637 | |
| 638 | for (p = include_defaults_array; p->fname; p++) |
| 639 | { |
| 640 | /* Some standard dirs are only for C++. */ |
| 641 | if (!p->cplusplus |
| 642 | || (opts->cplusplus |
| 643 | && !opts->no_standard_cplusplus_includes)) |
| 644 | { |
| 645 | /* Does this dir start with the prefix? */ |
| 646 | if (!strncmp (p->fname, default_prefix, default_len)) |
| 647 | { |
| 648 | /* Yes; change prefix and add to search list. */ |
| 649 | int flen = strlen (p->fname); |
| 650 | int this_len = specd_len + flen - default_len; |
| 651 | char *str = (char *) xmalloc (this_len + 1); |
| 652 | memcpy (str, specd_prefix, specd_len); |
| 653 | memcpy (str + specd_len, |
| 654 | p->fname + default_len, |
| 655 | flen - default_len + 1); |
| 656 | |
| 657 | append_include_chain (pfile, opts->pending, |
| 658 | str, SYSTEM, p->cxx_aware); |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | /* Search ordinary names for GNU include directories. */ |
| 665 | for (p = include_defaults_array; p->fname; p++) |
| 666 | { |
| 667 | /* Some standard dirs are only for C++. */ |
| 668 | if (!p->cplusplus |
| 669 | || (opts->cplusplus |
| 670 | && !opts->no_standard_cplusplus_includes)) |
| 671 | { |
| 672 | /* XXX Potential memory leak! */ |
| 673 | char *str = xstrdup (update_path (p->fname, p->component)); |
| 674 | append_include_chain (pfile, opts->pending, str, SYSTEM, |
| 675 | p->cxx_aware); |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 680 | /* This is called after options have been processed. |
| 681 | * Check options for consistency, and setup for processing input |
| 682 | * from the file named FNAME. (Use standard input if FNAME==NULL.) |
| 683 | * Return 1 on success, 0 on failure. |
| 684 | */ |
| 685 | |
| 686 | int |
| 687 | cpp_start_read (pfile, fname) |
| 688 | cpp_reader *pfile; |
| 689 | char *fname; |
| 690 | { |
| 691 | struct cpp_options *opts = CPP_OPTIONS (pfile); |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 692 | struct pending_option *p, *q; |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 693 | |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 694 | /* -MG doesn't select the form of output and must be specified with one of |
| 695 | -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't |
| 696 | inhibit compilation. */ |
| 697 | if (opts->print_deps_missing_files |
| 698 | && (opts->print_deps == 0 || !opts->no_output)) |
| 699 | { |
| 700 | cpp_fatal (pfile, "-MG must be specified with one of -M or -MM"); |
| 701 | return 0; |
| 702 | } |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 703 | |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 704 | /* Chill should not be used with -trigraphs. */ |
| 705 | if (opts->chill && opts->trigraphs) |
| 706 | { |
| 707 | cpp_warning (pfile, "-lang-chill and -trigraphs are mutually exclusive"); |
| 708 | opts->trigraphs = 0; |
| 709 | } |
| 710 | |
| 711 | /* Set this if it hasn't been set already. */ |
| 712 | if (user_label_prefix == NULL) |
| 713 | user_label_prefix = USER_LABEL_PREFIX; |
Zack Weinberg | c45da1c | 2000-03-02 20:14:32 +0000 | [diff] [blame] | 714 | |
| 715 | /* Don't bother trying to do macro expansion if we've already done |
| 716 | preprocessing. */ |
| 717 | if (opts->preprocessed) |
| 718 | pfile->no_macro_expand++; |
Zack Weinberg | 455d258 | 2000-03-04 01:42:56 +0000 | [diff] [blame] | 719 | |
| 720 | /* Set up the IStable. This doesn't do anything if we were compiled |
| 721 | with a compiler that supports C99 designated initializers. */ |
Zack Weinberg | a9ae448 | 1999-10-29 04:31:14 +0000 | [diff] [blame] | 722 | init_IStable (); |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 723 | |
Zack Weinberg | c45da1c | 2000-03-02 20:14:32 +0000 | [diff] [blame] | 724 | /* Set up the include search path now. */ |
| 725 | if (! opts->no_standard_includes) |
| 726 | initialize_standard_includes (pfile); |
| 727 | |
Zack Weinberg | b0699da | 2000-03-07 20:58:47 +0000 | [diff] [blame] | 728 | _cpp_merge_include_chains (opts); |
Zack Weinberg | c45da1c | 2000-03-02 20:14:32 +0000 | [diff] [blame] | 729 | |
| 730 | /* With -v, print the list of dirs to search. */ |
| 731 | if (opts->verbose) |
| 732 | { |
| 733 | struct file_name_list *l; |
| 734 | fprintf (stderr, _("#include \"...\" search starts here:\n")); |
| 735 | for (l = opts->quote_include; l; l = l->next) |
| 736 | { |
| 737 | if (l == opts->bracket_include) |
| 738 | fprintf (stderr, _("#include <...> search starts here:\n")); |
| 739 | fprintf (stderr, " %s\n", l->name); |
| 740 | } |
| 741 | fprintf (stderr, _("End of search list.\n")); |
| 742 | } |
| 743 | |
| 744 | initialize_dependency_output (pfile); |
| 745 | |
| 746 | /* Open the main input file. This must be done before -D processing |
| 747 | so we have a buffer to stand on. */ |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 748 | if (opts->in_fname == NULL || *opts->in_fname == 0) |
| 749 | { |
| 750 | opts->in_fname = fname; |
| 751 | if (opts->in_fname == NULL) |
| 752 | opts->in_fname = ""; |
| 753 | } |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 754 | |
Zack Weinberg | c45da1c | 2000-03-02 20:14:32 +0000 | [diff] [blame] | 755 | if (!cpp_read_file (pfile, fname)) |
| 756 | return 0; |
| 757 | |
| 758 | /* -D and friends may produce output, which should be identified |
| 759 | as line 0. */ |
| 760 | |
| 761 | CPP_BUFFER (pfile)->lineno = 0; |
| 762 | |
| 763 | /* Install __LINE__, etc. */ |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 764 | initialize_builtins (pfile); |
| 765 | |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 766 | /* Do -U's, -D's and -A's in the order they were seen. */ |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 767 | p = opts->pending->define_head; |
| 768 | while (p) |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 769 | { |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 770 | if (p->undef) |
| 771 | cpp_undef (pfile, p->arg); |
| 772 | else |
| 773 | cpp_define (pfile, p->arg); |
| 774 | |
| 775 | q = p->next; |
| 776 | free (p); |
| 777 | p = q; |
| 778 | } |
| 779 | |
| 780 | p = opts->pending->assert_head; |
| 781 | while (p) |
| 782 | { |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 783 | if (p->undef) |
| 784 | cpp_unassert (pfile, p->arg); |
| 785 | else |
| 786 | cpp_assert (pfile, p->arg); |
| 787 | |
| 788 | q = p->next; |
| 789 | free (p); |
| 790 | p = q; |
| 791 | } |
| 792 | |
| 793 | opts->done_initializing = 1; |
Zack Weinberg | c45da1c | 2000-03-02 20:14:32 +0000 | [diff] [blame] | 794 | CPP_BUFFER (pfile)->lineno = 1; |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 795 | |
Jason Merrill | 3773a46 | 1999-07-20 15:13:01 -0400 | [diff] [blame] | 796 | if (opts->preprocessed) |
| 797 | /* If we've already processed this code, we want to trust the #line |
| 798 | directives in the input. But we still need to update our line |
| 799 | counter accordingly. */ |
| 800 | pfile->lineno = CPP_BUFFER (pfile)->lineno; |
| 801 | else |
| 802 | output_line_command (pfile, same_file); |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 803 | pfile->only_seen_white = 2; |
| 804 | |
| 805 | /* The -imacros files can be scanned now, but the -include files |
| 806 | have to be pushed onto the include stack and processed later, |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 807 | in the main loop calling cpp_get_token. */ |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 808 | |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 809 | opts->no_output++; |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 810 | p = opts->pending->imacros_head; |
| 811 | while (p) |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 812 | { |
Zack Weinberg | c45da1c | 2000-03-02 20:14:32 +0000 | [diff] [blame] | 813 | if (cpp_read_file (pfile, p->arg)) |
| 814 | cpp_scan_buffer (pfile); |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 815 | |
| 816 | q = p->next; |
| 817 | free (p); |
| 818 | p = q; |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 819 | } |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 820 | opts->no_output--; |
| 821 | |
| 822 | p = opts->pending->include_head; |
| 823 | while (p) |
| 824 | { |
Zack Weinberg | c45da1c | 2000-03-02 20:14:32 +0000 | [diff] [blame] | 825 | if (cpp_read_file (pfile, p->arg)) |
| 826 | output_line_command (pfile, enter_file); |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 827 | |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 828 | q = p->next; |
| 829 | free (p); |
| 830 | p = q; |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 831 | } |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 832 | |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 833 | free (opts->pending); |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 834 | opts->pending = NULL; |
| 835 | |
| 836 | return 1; |
| 837 | } |
| 838 | |
| 839 | /* This is called at the end of preprocessing. It pops the |
| 840 | last buffer and writes dependency output. It should also |
| 841 | clear macro definitions, such that you could call cpp_start_read |
| 842 | with a new filename to restart processing. */ |
| 843 | void |
| 844 | cpp_finish (pfile) |
| 845 | cpp_reader *pfile; |
| 846 | { |
| 847 | struct cpp_options *opts = CPP_OPTIONS (pfile); |
| 848 | |
| 849 | if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) != CPP_NULL_BUFFER (pfile)) |
Zack Weinberg | c1212d2 | 2000-02-06 23:46:18 +0000 | [diff] [blame] | 850 | cpp_ice (pfile, "buffers still stacked in cpp_finish"); |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 851 | cpp_pop_buffer (pfile); |
Zack Weinberg | c1212d2 | 2000-02-06 23:46:18 +0000 | [diff] [blame] | 852 | |
Zack Weinberg | 49e6c08 | 2000-03-04 19:42:04 +0000 | [diff] [blame] | 853 | /* Don't write the deps file if preprocessing has failed. */ |
| 854 | if (opts->print_deps && pfile->errors == 0) |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 855 | { |
| 856 | /* Stream on which to print the dependency information. */ |
Zack Weinberg | c1212d2 | 2000-02-06 23:46:18 +0000 | [diff] [blame] | 857 | FILE *deps_stream = 0; |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 858 | |
Zack Weinberg | 49e6c08 | 2000-03-04 19:42:04 +0000 | [diff] [blame] | 859 | const char *deps_mode = opts->print_deps_append ? "a" : "w"; |
| 860 | if (opts->deps_file == 0) |
| 861 | deps_stream = stdout; |
| 862 | else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0) |
| 863 | cpp_notice_from_errno (pfile, opts->deps_file); |
Zack Weinberg | c1212d2 | 2000-02-06 23:46:18 +0000 | [diff] [blame] | 864 | |
Zack Weinberg | 49e6c08 | 2000-03-04 19:42:04 +0000 | [diff] [blame] | 865 | if (deps_stream) |
| 866 | { |
| 867 | deps_write (pfile->deps, deps_stream, 72); |
| 868 | if (opts->deps_file) |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 869 | { |
Zack Weinberg | 49e6c08 | 2000-03-04 19:42:04 +0000 | [diff] [blame] | 870 | if (ferror (deps_stream) || fclose (deps_stream) != 0) |
| 871 | cpp_fatal (pfile, "I/O error on output"); |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | } |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 875 | |
| 876 | if (opts->dump_macros == dump_only) |
| 877 | { |
| 878 | int i; |
| 879 | HASHNODE *h; |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 880 | for (i = HASHSIZE; --i >= 0;) |
| 881 | { |
| 882 | for (h = pfile->hashtab[i]; h; h = h->next) |
| 883 | if (h->type == T_MACRO) |
| 884 | { |
Zack Weinberg | b0699da | 2000-03-07 20:58:47 +0000 | [diff] [blame] | 885 | _cpp_dump_definition (pfile, h->name, h->length, |
| 886 | h->value.defn); |
Zack Weinberg | 3caee4a | 1999-04-26 16:41:02 +0000 | [diff] [blame] | 887 | CPP_PUTC (pfile, '\n'); |
| 888 | } |
| 889 | } |
| 890 | } |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Richard Henderson | 223dca6 | 1999-12-14 08:05:23 -0800 | [diff] [blame] | 893 | static void |
| 894 | new_pending_define (opts, text) |
| 895 | struct cpp_options *opts; |
| 896 | const char *text; |
| 897 | { |
| 898 | struct pending_option *o = (struct pending_option *) |
| 899 | xmalloc (sizeof (struct pending_option)); |
| 900 | |
Richard Kenner | c8d8ed6 | 2000-02-19 01:27:00 +0000 | [diff] [blame] | 901 | o->arg = (char *) text; |
Richard Henderson | 223dca6 | 1999-12-14 08:05:23 -0800 | [diff] [blame] | 902 | o->next = NULL; |
| 903 | o->undef = 0; |
| 904 | APPEND (opts->pending, define, o); |
| 905 | } |
| 906 | |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame^] | 907 | enum opt_code |
| 908 | { |
| 909 | OPT_stdin_stdout = 0, OPT_dollar, OPT_plus, |
| 910 | OPT__help, OPT__version, |
| 911 | OPT_A, OPT_C, OPT_D, OPT_H, OPT_I, OPT_M, |
| 912 | OPT_MD, OPT_MG, OPT_MM, OPT_MMD, |
| 913 | OPT_P, OPT_U, OPT_W, |
| 914 | OPT_d, |
| 915 | OPT_fleading_underscore, OPT_fno_leading_underscore, |
| 916 | OPT_fpreprocessed, OPT_fno_preprocessed, |
| 917 | OPT_g, OPT_h, |
| 918 | OPT_idirafter, OPT_imacros, OPT_include, |
| 919 | OPT_iprefix, OPT_isystem, OPT_iwithprefix, OPT_iwithprefixbefore, |
| 920 | OPT_lang_asm, OPT_lang_c, OPT_lang_cplusplus, OPT_lang_c89, |
| 921 | OPT_lang_chill, OPT_lang_fortran, OPT_lang_objc, OPT_lang_objcplusplus, |
| 922 | OPT_nostdinc, OPT_nostdincplusplus, |
| 923 | OPT_o, |
| 924 | OPT_pedantic, OPT_pedantic_errors, OPT_remap, |
| 925 | OPT_std_c89, OPT_std_c99, OPT_std_c9x, OPT_std_gnu89, OPT_std_gnu99, |
| 926 | OPT_std_gnu9x, OPT_std_iso9899_1990, OPT_std_iso9899_199409, |
| 927 | OPT_std_iso9899_1999, OPT_std_iso9899_199x, |
| 928 | OPT_traditional, OPT_trigraphs, |
| 929 | OPT_v, OPT_w, |
| 930 | N_OPTS |
| 931 | }; |
| 932 | |
| 933 | struct cl_option |
| 934 | { |
| 935 | const char *opt_text; |
| 936 | const char *msg; |
| 937 | size_t opt_len; |
| 938 | enum opt_code opt_code; |
| 939 | }; |
| 940 | |
| 941 | static const char no_arg[] = N_("Argument missing after `%s' option"); |
| 942 | static const char no_ass[] = N_("Assertion missing after `%s' option"); |
| 943 | static const char no_dir[] = N_("Directory name missing after `%s' option"); |
| 944 | static const char no_fil[] = N_("File name missing after `%s' option"); |
| 945 | static const char no_mac[] = N_("Macro name missing after `%s' option"); |
| 946 | static const char no_pth[] = N_("Path name missing after `%s' option"); |
| 947 | |
| 948 | /* This list must be ASCII sorted. Make enum order above match this. */ |
| 949 | #define DEF_OPT(text, msg, code) {text, msg, sizeof(text) - 1, code} |
| 950 | |
| 951 | #ifdef HOST_EBCDIC |
| 952 | static struct cl_option cl_options[] = |
| 953 | #else |
| 954 | static const struct cl_option cl_options[] = |
| 955 | #endif |
| 956 | { |
| 957 | DEF_OPT("", 0, OPT_stdin_stdout), |
| 958 | DEF_OPT("$", 0, OPT_dollar), |
| 959 | DEF_OPT("+", 0, OPT_plus), |
| 960 | DEF_OPT("-help", 0, OPT__help), |
| 961 | DEF_OPT("-version", 0, OPT__version), |
| 962 | DEF_OPT("A", no_ass, OPT_A), |
| 963 | DEF_OPT("C", 0, OPT_C), |
| 964 | DEF_OPT("D", no_mac, OPT_D), |
| 965 | DEF_OPT("H", 0, OPT_H), |
| 966 | DEF_OPT("I", no_dir, OPT_I), |
| 967 | DEF_OPT("M", 0, OPT_M), |
| 968 | DEF_OPT("MD", no_fil, OPT_MD), |
| 969 | DEF_OPT("MG", 0, OPT_MG), |
| 970 | DEF_OPT("MM", 0, OPT_MM), |
| 971 | DEF_OPT("MMD", no_fil, OPT_MMD), |
| 972 | DEF_OPT("P", 0, OPT_P), |
| 973 | DEF_OPT("U", no_mac, OPT_U), |
| 974 | /* NB: Immed arg only, and not reqd */ |
| 975 | DEF_OPT("W", no_arg, OPT_W), |
| 976 | DEF_OPT("d", no_arg, OPT_d), |
| 977 | DEF_OPT("fleading-underscore", 0, OPT_fleading_underscore), |
| 978 | DEF_OPT("fno-leading-underscore", 0, OPT_fno_leading_underscore), |
| 979 | DEF_OPT("fpreprocessed", 0, OPT_fpreprocessed), |
| 980 | DEF_OPT("fno-preprocessed", 0, OPT_fno_preprocessed), |
| 981 | /* NB: Immed arg only, and not reqd */ |
| 982 | DEF_OPT("g", no_arg, OPT_g), |
| 983 | DEF_OPT("h", 0, OPT_h), |
| 984 | DEF_OPT("idirafter", no_dir, OPT_idirafter), |
| 985 | DEF_OPT("imacros", no_fil, OPT_imacros), |
| 986 | DEF_OPT("include", no_fil, OPT_include), |
| 987 | DEF_OPT("iprefix", no_pth, OPT_iprefix), |
| 988 | DEF_OPT("isystem", no_dir, OPT_isystem), |
| 989 | DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix), |
| 990 | DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore), |
| 991 | DEF_OPT("lang-asm", 0, OPT_lang_asm), |
| 992 | DEF_OPT("lang-c", 0, OPT_lang_c), |
| 993 | DEF_OPT("lang-c++", 0, OPT_lang_cplusplus), |
| 994 | DEF_OPT("lang-c89", 0, OPT_lang_c89), |
| 995 | DEF_OPT("lang-chill", 0, OPT_lang_chill), |
| 996 | DEF_OPT("lang-fortran", 0, OPT_lang_fortran), |
| 997 | DEF_OPT("lang-objc", 0, OPT_lang_objc), |
| 998 | DEF_OPT("lang-objc++", 0, OPT_lang_objcplusplus), |
| 999 | DEF_OPT("nostdinc", 0, OPT_nostdinc), |
| 1000 | DEF_OPT("nostdinc++", 0, OPT_nostdincplusplus), |
| 1001 | DEF_OPT("o", no_fil, OPT_o), |
| 1002 | DEF_OPT("pedantic", 0, OPT_pedantic), |
| 1003 | DEF_OPT("pedantic-errors", 0, OPT_pedantic_errors), |
| 1004 | DEF_OPT("remap", 0, OPT_remap), |
| 1005 | DEF_OPT("std=c89", 0, OPT_std_c89), |
| 1006 | DEF_OPT("std=c99", 0, OPT_std_c99), |
| 1007 | DEF_OPT("std=c9x", 0, OPT_std_c9x), |
| 1008 | DEF_OPT("std=gnu89", 0, OPT_std_gnu89), |
| 1009 | DEF_OPT("std=gnu99", 0, OPT_std_gnu99), |
| 1010 | DEF_OPT("std=gnu9x", 0, OPT_std_gnu9x), |
| 1011 | DEF_OPT("std=iso9899:1990", 0, OPT_std_iso9899_1990), |
| 1012 | DEF_OPT("std=iso9899:199409", 0, OPT_std_iso9899_199409), |
| 1013 | DEF_OPT("std=iso9899:1999", 0, OPT_std_iso9899_1999), |
| 1014 | DEF_OPT("std=iso9899:199x", 0, OPT_std_iso9899_199x), |
| 1015 | DEF_OPT("traditional", 0, OPT_traditional), |
| 1016 | DEF_OPT("trigraphs", 0, OPT_trigraphs), |
| 1017 | DEF_OPT("v", 0, OPT_v), |
| 1018 | DEF_OPT("w", 0, OPT_w) |
| 1019 | }; |
| 1020 | #undef DEF_OPT |
| 1021 | |
| 1022 | /* Perform a binary search to find which, if any, option the given |
| 1023 | command-line matches. Returns its index in the option array, |
| 1024 | negative on failure. Complications arise since some options can be |
| 1025 | suffixed with an argument, and multiple complete matches can occur, |
| 1026 | e.g. -iwithprefix and -iwithprefixbefore. Moreover, we want to |
| 1027 | accept options beginning with -g and -W that we do not recognise, |
| 1028 | but not to swallow any subsequent command line argument; these are |
| 1029 | handled as special cases in cpp_handle_option */ |
| 1030 | static int |
| 1031 | parse_option (input) |
| 1032 | const char *input; |
| 1033 | { |
| 1034 | unsigned int md, mn, mx; |
| 1035 | size_t opt_len; |
| 1036 | int comp; |
| 1037 | |
| 1038 | mn = 0; |
| 1039 | mx = N_OPTS; |
| 1040 | |
| 1041 | while (mx > mn) |
| 1042 | { |
| 1043 | md = (mn + mx) / 2; |
| 1044 | |
| 1045 | opt_len = cl_options[md].opt_len; |
| 1046 | comp = strncmp (input, cl_options[md].opt_text, opt_len); |
| 1047 | |
| 1048 | if (comp > 0) |
| 1049 | mn = md + 1; |
| 1050 | else if (comp < 0) |
| 1051 | mx = md; |
| 1052 | else |
| 1053 | { |
| 1054 | if (input[opt_len] == '\0') |
| 1055 | return md; |
| 1056 | /* We were passed more text. If the option takes an argument, |
| 1057 | we may match a later option or we may have been passed the |
| 1058 | argument. The longest possible option match succeeds. |
| 1059 | If the option takes no arguments we have not matched and |
| 1060 | continue the search (e.g. input="stdc++" match was "stdc") */ |
| 1061 | mn = md + 1; |
| 1062 | if (cl_options[md].msg) |
| 1063 | { |
| 1064 | /* Scan forwards. If we get an exact match, return it. |
| 1065 | Otherwise, return the longest option-accepting match. |
| 1066 | This loops no more than twice with current options */ |
| 1067 | mx = md; |
| 1068 | for (; mn < N_OPTS; mn++) |
| 1069 | { |
| 1070 | opt_len = cl_options[mn].opt_len; |
| 1071 | if (strncmp (input, cl_options[mn].opt_text, opt_len)) |
| 1072 | break; |
| 1073 | if (input[opt_len] == '\0') |
| 1074 | return mn; |
| 1075 | if (cl_options[mn].msg) |
| 1076 | mx = mn; |
| 1077 | } |
| 1078 | return mx; |
| 1079 | } |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | return -1; |
| 1084 | } |
| 1085 | |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 1086 | /* Handle one command-line option in (argc, argv). |
| 1087 | Can be called multiple times, to handle multiple sets of options. |
| 1088 | Returns number of strings consumed. */ |
Richard Kenner | c8d8ed6 | 2000-02-19 01:27:00 +0000 | [diff] [blame] | 1089 | |
Zack Weinberg | b0699da | 2000-03-07 20:58:47 +0000 | [diff] [blame] | 1090 | static int |
| 1091 | handle_option (pfile, argc, argv) |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 1092 | cpp_reader *pfile; |
| 1093 | int argc; |
| 1094 | char **argv; |
| 1095 | { |
| 1096 | struct cpp_options *opts = CPP_OPTIONS (pfile); |
| 1097 | int i = 0; |
| 1098 | |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 1099 | if (argv[i][0] != '-') |
| 1100 | { |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 1101 | if (opts->out_fname != NULL) |
| 1102 | { |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 1103 | print_help (); |
| 1104 | cpp_fatal (pfile, "Too many arguments"); |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 1105 | } |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 1106 | else if (opts->in_fname != NULL) |
| 1107 | opts->out_fname = argv[i]; |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 1108 | else |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 1109 | opts->in_fname = argv[i]; |
| 1110 | } |
| 1111 | else |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame^] | 1112 | { |
| 1113 | enum opt_code opt_code; |
| 1114 | int opt_index; |
| 1115 | char *arg = 0; |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 1116 | |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame^] | 1117 | /* Skip over '-' */ |
| 1118 | opt_index = parse_option (&argv[i][1]); |
| 1119 | if (opt_index < 0) |
| 1120 | return i; |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 1121 | |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame^] | 1122 | opt_code = cl_options[opt_index].opt_code; |
| 1123 | if (cl_options[opt_index].msg) |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 1124 | { |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame^] | 1125 | arg = &argv[i][cl_options[opt_index].opt_len + 1]; |
| 1126 | |
| 1127 | /* Yuk. Special case for -g and -W as they must not swallow |
| 1128 | up any following argument. If this becomes common, add |
| 1129 | another field to the cl_options table */ |
| 1130 | if (arg[0] == '\0' && !(opt_code == OPT_g || opt_code == OPT_W)) |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 1131 | { |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame^] | 1132 | arg = argv[++i]; |
| 1133 | if (!arg) |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 1134 | { |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame^] | 1135 | cpp_fatal (pfile, _(cl_options[opt_index].msg), argv[i - 1]); |
| 1136 | return argc; |
| 1137 | } |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | switch (opt_code) |
| 1142 | { |
| 1143 | case N_OPTS: /* shut GCC up */ |
| 1144 | break; |
| 1145 | case OPT_fleading_underscore: |
| 1146 | user_label_prefix = "_"; |
| 1147 | break; |
| 1148 | case OPT_fno_leading_underscore: |
| 1149 | user_label_prefix = ""; |
| 1150 | break; |
| 1151 | case OPT_fpreprocessed: |
| 1152 | opts->preprocessed = 1; |
| 1153 | break; |
| 1154 | case OPT_fno_preprocessed: |
| 1155 | opts->preprocessed = 0; |
| 1156 | break; |
| 1157 | case OPT_w: |
| 1158 | opts->inhibit_warnings = 1; |
| 1159 | break; |
| 1160 | case OPT_g: /* Silently ignore anything but -g3 */ |
| 1161 | if (!strcmp(&argv[i][2], "3")) |
| 1162 | opts->debug_output = 1; |
| 1163 | break; |
| 1164 | case OPT_h: |
| 1165 | case OPT__help: |
| 1166 | print_help (); |
| 1167 | exit (0); /* XXX */ |
| 1168 | break; |
| 1169 | case OPT__version: |
| 1170 | fprintf (stderr, _("GNU CPP version %s (cpplib)\n"), version_string); |
| 1171 | exit (0); /* XXX */ |
| 1172 | break; |
| 1173 | case OPT_C: |
| 1174 | opts->discard_comments = 0; |
| 1175 | break; |
| 1176 | case OPT_P: |
| 1177 | opts->no_line_commands = 1; |
| 1178 | break; |
| 1179 | case OPT_dollar: /* Don't include $ in identifiers. */ |
| 1180 | opts->dollars_in_ident = 0; |
| 1181 | break; |
| 1182 | case OPT_H: |
| 1183 | opts->print_include_names = 1; |
| 1184 | break; |
| 1185 | case OPT_D: |
| 1186 | new_pending_define (opts, arg); |
| 1187 | break; |
| 1188 | case OPT_pedantic_errors: |
| 1189 | opts->pedantic_errors = 1; |
| 1190 | /* fall through */ |
| 1191 | case OPT_pedantic: |
| 1192 | opts->pedantic = 1; |
| 1193 | break; |
| 1194 | case OPT_traditional: |
| 1195 | opts->traditional = 1; |
| 1196 | opts->cplusplus_comments = 0; |
| 1197 | opts->trigraphs = 0; |
| 1198 | opts->warn_trigraphs = 0; |
| 1199 | break; |
| 1200 | case OPT_trigraphs: |
| 1201 | opts->trigraphs = 1; |
| 1202 | break; |
| 1203 | case OPT_plus: |
| 1204 | opts->cplusplus = 1; |
| 1205 | opts->cplusplus_comments = 1; |
| 1206 | break; |
| 1207 | case OPT_remap: |
| 1208 | opts->remap = 1; |
| 1209 | break; |
| 1210 | case OPT_iprefix: |
| 1211 | opts->include_prefix = arg; |
| 1212 | opts->include_prefix_len = strlen (arg); |
| 1213 | break; |
| 1214 | case OPT_lang_c: |
| 1215 | opts->cplusplus = 0, opts->cplusplus_comments = 1; |
| 1216 | opts->c89 = 0, opts->c99 = 1, opts->objc = 0; |
| 1217 | break; |
| 1218 | case OPT_lang_c89: |
| 1219 | opts->cplusplus = 0, opts->cplusplus_comments = 0; |
| 1220 | opts->c89 = 1, opts->c99 = 0, opts->objc = 0; |
| 1221 | opts->trigraphs = 1; |
| 1222 | new_pending_define (opts, "__STRICT_ANSI__"); |
| 1223 | break; |
| 1224 | case OPT_lang_cplusplus: |
| 1225 | opts->cplusplus = 1, opts->cplusplus_comments = 1; |
| 1226 | opts->c89 = 0, opts->c99 = 0, opts->objc = 0; |
| 1227 | break; |
| 1228 | case OPT_lang_objc: |
| 1229 | case OPT_lang_objcplusplus: |
| 1230 | opts->cplusplus = opt_code == OPT_lang_objcplusplus; |
| 1231 | opts->cplusplus_comments = 1; |
| 1232 | opts->c89 = 0, opts->c99 = 0, opts->objc = 1; |
| 1233 | break; |
| 1234 | case OPT_lang_asm: |
| 1235 | opts->lang_asm = 1; |
| 1236 | break; |
| 1237 | case OPT_lang_fortran: |
| 1238 | opts->lang_fortran = 1, opts->cplusplus_comments = 0; |
| 1239 | break; |
| 1240 | case OPT_lang_chill: |
| 1241 | opts->objc = 0, opts->cplusplus = 0; |
| 1242 | opts->chill = 1, opts->traditional = 1; |
| 1243 | break; |
| 1244 | case OPT_nostdinc: |
| 1245 | /* -nostdinc causes no default include directories. |
| 1246 | You must specify all include-file directories with -I. */ |
| 1247 | opts->no_standard_includes = 1; |
| 1248 | break; |
| 1249 | case OPT_nostdincplusplus: |
| 1250 | /* -nostdinc++ causes no default C++-specific include directories. */ |
| 1251 | opts->no_standard_cplusplus_includes = 1; |
| 1252 | break; |
| 1253 | case OPT_std_gnu89: |
| 1254 | opts->cplusplus = 0, opts->cplusplus_comments = 1; |
| 1255 | opts->c89 = 1, opts->c99 = 0, opts->objc = 0; |
| 1256 | break; |
| 1257 | case OPT_std_gnu9x: |
| 1258 | case OPT_std_gnu99: |
| 1259 | opts->cplusplus = 0, opts->cplusplus_comments = 1; |
| 1260 | opts->c89 = 0, opts->c99 = 1, opts->objc = 0; |
| 1261 | new_pending_define (opts, "__STDC_VERSION__=199901L"); |
| 1262 | break; |
| 1263 | case OPT_std_iso9899_199409: |
| 1264 | new_pending_define (opts, "__STDC_VERSION__=199409L"); |
| 1265 | /* Fall through */ |
| 1266 | case OPT_std_iso9899_1990: |
| 1267 | case OPT_std_c89: |
| 1268 | opts->cplusplus = 0, opts->cplusplus_comments = 0; |
| 1269 | opts->c89 = 1, opts->c99 = 0, opts->objc = 0; |
| 1270 | opts->trigraphs = 1; |
| 1271 | new_pending_define (opts, "__STRICT_ANSI__"); |
| 1272 | break; |
| 1273 | case OPT_std_iso9899_199x: |
| 1274 | case OPT_std_iso9899_1999: |
| 1275 | case OPT_std_c9x: |
| 1276 | case OPT_std_c99: |
| 1277 | opts->cplusplus = 0, opts->cplusplus_comments = 1; |
| 1278 | opts->c89 = 0, opts->c99 = 1, opts->objc = 0; |
| 1279 | opts->trigraphs = 1; |
| 1280 | new_pending_define (opts, "__STRICT_ANSI__"); |
| 1281 | new_pending_define (opts, "__STDC_VERSION__=199901L"); |
| 1282 | break; |
| 1283 | case OPT_o: |
| 1284 | if (opts->out_fname != NULL) |
| 1285 | { |
| 1286 | cpp_fatal (pfile, "Output filename specified twice"); |
| 1287 | return argc; |
| 1288 | } |
| 1289 | opts->out_fname = arg; |
| 1290 | if (!strcmp (opts->out_fname, "-")) |
| 1291 | opts->out_fname = ""; |
| 1292 | break; |
| 1293 | case OPT_v: |
| 1294 | fprintf (stderr, _("GNU CPP version %s (cpplib)\n"), version_string); |
| 1295 | #ifdef TARGET_VERSION |
| 1296 | TARGET_VERSION; |
| 1297 | #endif |
| 1298 | fputc ('\n', stderr); |
| 1299 | opts->verbose = 1; |
| 1300 | break; |
| 1301 | case OPT_stdin_stdout: |
| 1302 | /* JF handle '-' as file name meaning stdin or stdout */ |
| 1303 | if (opts->in_fname == NULL) |
| 1304 | opts->in_fname = ""; |
| 1305 | else if (opts->out_fname == NULL) |
| 1306 | opts->out_fname = ""; |
| 1307 | break; |
| 1308 | case OPT_d: |
| 1309 | /* Args to -d specify what parts of macros to dump. |
| 1310 | Silently ignore unrecognised options; they may |
| 1311 | be aimed at the compiler proper. */ |
| 1312 | { |
| 1313 | char c; |
| 1314 | |
| 1315 | while ((c = *arg++) != '\0') |
| 1316 | switch (c) |
| 1317 | { |
| 1318 | case 'M': |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 1319 | opts->dump_macros = dump_only; |
| 1320 | opts->no_output = 1; |
| 1321 | break; |
| 1322 | case 'N': |
| 1323 | opts->dump_macros = dump_names; |
| 1324 | break; |
| 1325 | case 'D': |
| 1326 | opts->dump_macros = dump_definitions; |
| 1327 | break; |
| 1328 | case 'I': |
| 1329 | opts->dump_includes = 1; |
| 1330 | break; |
| 1331 | } |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame^] | 1332 | } |
| 1333 | break; |
| 1334 | /* The style of the choices here is a bit mixed. |
| 1335 | The chosen scheme is a hybrid of keeping all options in one string |
| 1336 | and specifying each option in a separate argument: |
| 1337 | -M|-MM|-MD file|-MMD file [-MG]. An alternative is: |
| 1338 | -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely: |
| 1339 | -M[M][G][D file]. This is awkward to handle in specs, and is not |
| 1340 | as extensible. */ |
| 1341 | /* ??? -MG must be specified in addition to one of -M or -MM. |
| 1342 | This can be relaxed in the future without breaking anything. |
| 1343 | The converse isn't true. */ |
| 1344 | |
| 1345 | /* -MG isn't valid with -MD or -MMD. This is checked for later. */ |
| 1346 | case OPT_MG: |
| 1347 | opts->print_deps_missing_files = 1; |
| 1348 | break; |
| 1349 | case OPT_M: |
| 1350 | case OPT_MD: |
| 1351 | case OPT_MM: |
| 1352 | case OPT_MMD: |
| 1353 | if (opt_code == OPT_M || opt_code == OPT_MD) |
| 1354 | opts->print_deps = 2; |
| 1355 | else |
| 1356 | opts->print_deps = 1; |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 1357 | |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame^] | 1358 | /* For -MD and -MMD options, write deps on file named by next arg */ |
| 1359 | /* For -M and -MM, write deps on standard output |
| 1360 | and suppress the usual output. */ |
| 1361 | if (opt_code == OPT_MD || opt_code == OPT_MMD) |
| 1362 | opts->deps_file = arg; |
| 1363 | else |
| 1364 | opts->no_output = 1; |
| 1365 | break; |
| 1366 | case OPT_A: |
| 1367 | if (strcmp (arg, "-")) |
| 1368 | { |
| 1369 | struct pending_option *o = (struct pending_option *) |
| 1370 | xmalloc (sizeof (struct pending_option)); |
| 1371 | |
| 1372 | o->arg = arg; |
| 1373 | o->next = NULL; |
| 1374 | o->undef = 0; |
| 1375 | APPEND (opts->pending, assert, o); |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 1376 | } |
| 1377 | else |
| 1378 | { |
| 1379 | /* -A- eliminates all predefined macros and assertions. |
| 1380 | Let's include also any that were specified earlier |
| 1381 | on the command line. That way we can get rid of any |
| 1382 | that were passed automatically in from GCC. */ |
| 1383 | struct pending_option *o1, *o2; |
| 1384 | |
| 1385 | o1 = opts->pending->define_head; |
| 1386 | while (o1) |
| 1387 | { |
| 1388 | o2 = o1->next; |
| 1389 | free (o1); |
| 1390 | o1 = o2; |
| 1391 | } |
| 1392 | o1 = opts->pending->assert_head; |
| 1393 | while (o1) |
| 1394 | { |
| 1395 | o2 = o1->next; |
| 1396 | free (o1); |
| 1397 | o1 = o2; |
| 1398 | } |
| 1399 | opts->pending->assert_head = NULL; |
| 1400 | opts->pending->assert_tail = NULL; |
| 1401 | opts->pending->define_head = NULL; |
| 1402 | opts->pending->define_tail = NULL; |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 1403 | } |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame^] | 1404 | break; |
| 1405 | case OPT_U: |
| 1406 | { |
| 1407 | struct pending_option *o = (struct pending_option *) |
| 1408 | xmalloc (sizeof (struct pending_option)); |
| 1409 | |
| 1410 | o->arg = arg; |
| 1411 | o->next = NULL; |
| 1412 | o->undef = 1; |
| 1413 | APPEND (opts->pending, define, o); |
| 1414 | } |
| 1415 | break; |
| 1416 | case OPT_I: /* Add directory to path for includes. */ |
| 1417 | if (!strcmp (arg, "-")) |
| 1418 | { |
| 1419 | /* -I- means: |
| 1420 | Use the preceding -I directories for #include "..." |
| 1421 | but not #include <...>. |
| 1422 | Don't search the directory of the present file |
| 1423 | for #include "...". (Note that -I. -I- is not the same as |
| 1424 | the default setup; -I. uses the compiler's working dir.) */ |
| 1425 | if (! opts->ignore_srcdir) |
| 1426 | { |
| 1427 | opts->ignore_srcdir = 1; |
| 1428 | opts->pending->quote_head = opts->pending->brack_head; |
| 1429 | opts->pending->quote_tail = opts->pending->brack_tail; |
| 1430 | opts->pending->brack_head = 0; |
| 1431 | opts->pending->brack_tail = 0; |
| 1432 | } |
| 1433 | else |
| 1434 | { |
| 1435 | cpp_fatal (pfile, "-I- specified twice"); |
| 1436 | return argc; |
| 1437 | } |
| 1438 | } |
| 1439 | else |
| 1440 | append_include_chain (pfile, opts->pending, |
| 1441 | xstrdup (arg), BRACKET, 0); |
| 1442 | break; |
| 1443 | case OPT_isystem: |
| 1444 | /* Add directory to beginning of system include path, as a system |
| 1445 | include directory. */ |
| 1446 | append_include_chain (pfile, opts->pending, |
| 1447 | xstrdup (arg), SYSTEM, 0); |
| 1448 | break; |
| 1449 | case OPT_include: |
| 1450 | { |
| 1451 | struct pending_option *o = (struct pending_option *) |
| 1452 | xmalloc (sizeof (struct pending_option)); |
| 1453 | o->arg = arg; |
| 1454 | |
| 1455 | /* This list has to be built in reverse order so that |
| 1456 | when cpp_start_read pushes all the -include files onto |
| 1457 | the buffer stack, they will be scanned in forward order. */ |
| 1458 | o->next = opts->pending->include_head; |
| 1459 | opts->pending->include_head = o; |
| 1460 | } |
| 1461 | break; |
| 1462 | case OPT_imacros: |
| 1463 | { |
| 1464 | struct pending_option *o = (struct pending_option *) |
| 1465 | xmalloc (sizeof (struct pending_option)); |
| 1466 | o->arg = arg; |
| 1467 | o->next = NULL; |
| 1468 | |
| 1469 | APPEND (opts->pending, imacros, o); |
| 1470 | } |
| 1471 | break; |
| 1472 | case OPT_iwithprefix: |
| 1473 | /* Add directory to end of path for includes, |
| 1474 | with the default prefix at the front of its name. */ |
| 1475 | /* fall through */ |
| 1476 | case OPT_iwithprefixbefore: |
| 1477 | /* Add directory to main path for includes, |
| 1478 | with the default prefix at the front of its name. */ |
| 1479 | { |
| 1480 | char *fname; |
| 1481 | int len; |
| 1482 | |
| 1483 | len = strlen (arg); |
| 1484 | |
| 1485 | if (opts->include_prefix != 0) |
| 1486 | { |
| 1487 | fname = xmalloc (opts->include_prefix_len + len + 1); |
| 1488 | memcpy (fname, opts->include_prefix, opts->include_prefix_len); |
| 1489 | memcpy (fname + opts->include_prefix_len, arg, len + 1); |
| 1490 | } |
| 1491 | else |
| 1492 | { |
| 1493 | fname = xmalloc (sizeof GCC_INCLUDE_DIR - 8 + len); |
| 1494 | memcpy (fname, GCC_INCLUDE_DIR, sizeof GCC_INCLUDE_DIR - 9); |
| 1495 | memcpy (fname + sizeof GCC_INCLUDE_DIR - 9, arg, len + 1); |
| 1496 | } |
| 1497 | |
| 1498 | append_include_chain (pfile, opts->pending, fname, |
| 1499 | opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0); |
| 1500 | } |
| 1501 | break; |
| 1502 | case OPT_idirafter: |
| 1503 | /* Add directory to end of path for includes. */ |
| 1504 | append_include_chain (pfile, opts->pending, |
| 1505 | xstrdup (arg), AFTER, 0); |
| 1506 | break; |
| 1507 | case OPT_W: |
| 1508 | /* Silently ignore unrecognised options */ |
| 1509 | if (!strcmp (argv[i], "-Wall")) |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 1510 | { |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame^] | 1511 | opts->warn_trigraphs = 1; |
| 1512 | opts->warn_comments = 1; |
Zack Weinberg | 0b22d65 | 1999-03-15 18:42:46 +0000 | [diff] [blame] | 1513 | } |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame^] | 1514 | else if (!strcmp (argv[i], "-Wtraditional")) |
| 1515 | opts->warn_stringify = 1; |
| 1516 | else if (!strcmp (argv[i], "-Wtrigraphs")) |
| 1517 | opts->warn_trigraphs = 1; |
| 1518 | else if (!strcmp (argv[i], "-Wcomment")) |
| 1519 | opts->warn_comments = 1; |
| 1520 | else if (!strcmp (argv[i], "-Wcomments")) |
| 1521 | opts->warn_comments = 1; |
| 1522 | else if (!strcmp (argv[i], "-Wundef")) |
| 1523 | opts->warn_undef = 1; |
| 1524 | else if (!strcmp (argv[i], "-Wimport")) |
| 1525 | opts->warn_import = 1; |
| 1526 | else if (!strcmp (argv[i], "-Werror")) |
| 1527 | opts->warnings_are_errors = 1; |
| 1528 | else if (!strcmp (argv[i], "-Wno-traditional")) |
| 1529 | opts->warn_stringify = 0; |
| 1530 | else if (!strcmp (argv[i], "-Wno-trigraphs")) |
| 1531 | opts->warn_trigraphs = 0; |
| 1532 | else if (!strcmp (argv[i], "-Wno-comment")) |
| 1533 | opts->warn_comments = 0; |
| 1534 | else if (!strcmp (argv[i], "-Wno-comments")) |
| 1535 | opts->warn_comments = 0; |
| 1536 | else if (!strcmp (argv[i], "-Wno-undef")) |
| 1537 | opts->warn_undef = 0; |
| 1538 | else if (!strcmp (argv[i], "-Wno-import")) |
| 1539 | opts->warn_import = 0; |
| 1540 | else if (!strcmp (argv[i], "-Wno-error")) |
| 1541 | opts->warnings_are_errors = 0; |
| 1542 | break; |
| 1543 | } |
| 1544 | } |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 1545 | return i + 1; |
| 1546 | } |
| 1547 | |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame^] | 1548 | #ifdef HOST_EBCDIC |
| 1549 | static int |
| 1550 | opt_comp (const void *p1, const void *p2) |
| 1551 | { |
| 1552 | return strcmp (((struct cl_option *)p1)->opt_text, |
| 1553 | ((struct cl_option *)p2)->opt_text); |
| 1554 | } |
| 1555 | #endif |
| 1556 | |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 1557 | /* Handle command-line options in (argc, argv). |
| 1558 | Can be called multiple times, to handle multiple sets of options. |
| 1559 | Returns if an unrecognized option is seen. |
| 1560 | Returns number of strings consumed. */ |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 1561 | int |
| 1562 | cpp_handle_options (pfile, argc, argv) |
| 1563 | cpp_reader *pfile; |
| 1564 | int argc; |
| 1565 | char **argv; |
| 1566 | { |
| 1567 | int i; |
| 1568 | int strings_processed; |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame^] | 1569 | |
| 1570 | #ifdef HOST_EBCDIC |
| 1571 | static int opts_sorted = 0; |
| 1572 | |
| 1573 | if (!opts_sorted) |
| 1574 | { |
| 1575 | opts_sorted = 1; |
| 1576 | /* For non-ASCII hosts, the array needs to be sorted at runtime */ |
| 1577 | qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp); |
| 1578 | } |
| 1579 | #endif |
| 1580 | |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 1581 | for (i = 0; i < argc; i += strings_processed) |
| 1582 | { |
Zack Weinberg | b0699da | 2000-03-07 20:58:47 +0000 | [diff] [blame] | 1583 | strings_processed = handle_option (pfile, argc - i, argv + i); |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 1584 | if (strings_processed == 0) |
| 1585 | break; |
| 1586 | } |
| 1587 | return i; |
| 1588 | } |
| 1589 | |
| 1590 | static void |
| 1591 | print_help () |
| 1592 | { |
Zack Weinberg | c1212d2 | 2000-02-06 23:46:18 +0000 | [diff] [blame] | 1593 | fprintf (stderr, _("Usage: %s [switches] input output\n"), progname); |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 1594 | fputs (_("\ |
| 1595 | Switches:\n\ |
| 1596 | -include <file> Include the contents of <file> before other files\n\ |
| 1597 | -imacros <file> Accept definition of macros in <file>\n\ |
| 1598 | -iprefix <path> Specify <path> as a prefix for next two options\n\ |
| 1599 | -iwithprefix <dir> Add <dir> to the end of the system include path\n\ |
| 1600 | -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\ |
| 1601 | -isystem <dir> Add <dir> to the start of the system include path\n\ |
| 1602 | -idirafter <dir> Add <dir> to the end of the system include path\n\ |
| 1603 | -I <dir> Add <dir> to the end of the main include path\n\ |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame^] | 1604 | -I- Fine-grained include path control; see info docs\n\ |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 1605 | -nostdinc Do not search system include directories\n\ |
| 1606 | (dirs specified with -isystem will still be used)\n\ |
| 1607 | -nostdinc++ Do not search system include directories for C++\n\ |
| 1608 | -o <file> Put output into <file>\n\ |
| 1609 | -pedantic Issue all warnings demanded by strict ANSI C\n\ |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame^] | 1610 | -pedantic-errors Issue -pedantic warnings as errors instead\n\ |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 1611 | -traditional Follow K&R pre-processor behaviour\n\ |
| 1612 | -trigraphs Support ANSI C trigraphs\n\ |
| 1613 | -lang-c Assume that the input sources are in C\n\ |
| 1614 | -lang-c89 Assume that the input sources are in C89\n\ |
| 1615 | -lang-c++ Assume that the input sources are in C++\n\ |
| 1616 | -lang-objc Assume that the input sources are in ObjectiveC\n\ |
| 1617 | -lang-objc++ Assume that the input sources are in ObjectiveC++\n\ |
| 1618 | -lang-asm Assume that the input sources are in assembler\n\ |
Zack Weinberg | 40c79d5 | 2000-01-12 00:35:36 +0000 | [diff] [blame] | 1619 | -lang-fortran Assume that the input sources are in Fortran\n\ |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 1620 | -lang-chill Assume that the input sources are in Chill\n\ |
| 1621 | -std=<std name> Specify the conformance standard; one of:\n\ |
Ulrich Drepper | 916269a | 2000-01-29 19:00:43 +0000 | [diff] [blame] | 1622 | gnu89, gnu99, c89, c99, iso9899:1990,\n\ |
| 1623 | iso9899:199409, iso9899:1999\n\ |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 1624 | -+ Allow parsing of C++ style features\n\ |
| 1625 | -w Inhibit warning messages\n\ |
| 1626 | -Wtrigraphs Warn if trigraphs are encountered\n\ |
| 1627 | -Wno-trigraphs Do not warn about trigraphs\n\ |
| 1628 | -Wcomment{s} Warn if one comment starts inside another\n\ |
| 1629 | -Wno-comment{s} Do not warn about comments\n\ |
| 1630 | -Wtraditional Warn if a macro argument is/would be turned into\n\ |
| 1631 | a string if -traditional is specified\n\ |
| 1632 | -Wno-traditional Do not warn about stringification\n\ |
| 1633 | -Wundef Warn if an undefined macro is used by #if\n\ |
| 1634 | -Wno-undef Do not warn about testing undefined macros\n\ |
| 1635 | -Wimport Warn about the use of the #import directive\n\ |
| 1636 | -Wno-import Do not warn about the use of #import\n\ |
| 1637 | -Werror Treat all warnings as errors\n\ |
| 1638 | -Wno-error Do not treat warnings as errors\n\ |
| 1639 | -Wall Enable all preprocessor warnings\n\ |
| 1640 | -M Generate make dependencies\n\ |
| 1641 | -MM As -M, but ignore system header files\n\ |
| 1642 | -MD As -M, but put output in a .d file\n\ |
| 1643 | -MMD As -MD, but ignore system header files\n\ |
| 1644 | -MG Treat missing header file as generated files\n\ |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame^] | 1645 | -g3 Include #define and #undef directives in the output\n\ |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 1646 | -D<macro> Define a <macro> with string '1' as its value\n\ |
| 1647 | -D<macro>=<val> Define a <macro> with <val> as its value\n\ |
| 1648 | -A<question> (<answer>) Assert the <answer> to <question>\n\ |
| 1649 | -U<macro> Undefine <macro> \n\ |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 1650 | -v Display the version number\n\ |
| 1651 | -H Print the name of header files as they are used\n\ |
| 1652 | -C Do not discard comments\n\ |
| 1653 | -dM Display a list of macro definitions active at end\n\ |
| 1654 | -dD Preserve macro definitions in output\n\ |
| 1655 | -dN As -dD except that only the names are preserved\n\ |
| 1656 | -dI Include #include directives in the output\n\ |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 1657 | -P Do not generate #line directives\n\ |
| 1658 | -$ Do not allow '$' in identifiers\n\ |
| 1659 | -remap Remap file names when including files.\n\ |
Zack Weinberg | e23c0ba | 2000-03-07 23:11:06 +0000 | [diff] [blame^] | 1660 | --version Display version information\n\ |
Zack Weinberg | 6de1e2a | 1999-02-18 15:35:49 +0000 | [diff] [blame] | 1661 | -h or --help Display this information\n\ |
| 1662 | "), stdout); |
| 1663 | } |