]>
Commit | Line | Data |
---|---|---|
e14c5993 | 1 | /* CPP Library. (Directive handling.) |
00059bc7 | 2 | Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, |
927b511f | 3 | 1999, 2000, 2001, 2002, 2003, 2004, 2005, |
cde59b72 | 4 | 2007, 2008 Free Software Foundation, Inc. |
1f99c786 | 5 | Contributed by Per Bothner, 1994-95. |
0cb8d9ae | 6 | Based on CCCP program by Paul Rubin, June 1986 |
1d6fa33f | 7 | Adapted to ANSI C, Richard Stallman, Jan 1987 |
8 | ||
9 | This program is free software; you can redistribute it and/or modify it | |
10 | under the terms of the GNU General Public License as published by the | |
11 | Free Software Foundation; either version 2, or (at your option) any | |
12 | later version. | |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with this program; if not, write to the Free Software | |
2656917a | 21 | Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
1d6fa33f | 22 | |
ad87de1e | 23 | #include "config.h" |
1486870d | 24 | #include "system.h" |
ad87de1e | 25 | #include "cpplib.h" |
d856c8a6 | 26 | #include "internal.h" |
3eb3f293 | 27 | #include "mkdeps.h" |
f51c2148 | 28 | #include "obstack.h" |
1d6fa33f | 29 | |
1e8b9746 | 30 | /* Stack of conditionals currently in progress |
31 | (including both successful and failing conditionals). */ | |
1e8b9746 | 32 | struct if_stack |
33 | { | |
34 | struct if_stack *next; | |
729d2022 | 35 | unsigned int line; /* Line where condition started. */ |
79bd622b | 36 | const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */ |
5e878517 | 37 | bool skip_elses; /* Can future #else / #elif be skipped? */ |
38 | bool was_skipping; /* If were skipping on entry. */ | |
f7fdd7a1 | 39 | int type; /* Most recent conditional for diagnostics. */ |
1e8b9746 | 40 | }; |
1e8b9746 | 41 | |
c6f14ce5 | 42 | /* Contains a registered pragma or pragma namespace. */ |
f7fdd7a1 | 43 | typedef void (*pragma_cb) (cpp_reader *); |
c6f14ce5 | 44 | struct pragma_entry |
45 | { | |
46 | struct pragma_entry *next; | |
5c43d212 | 47 | const cpp_hashnode *pragma; /* Name and length. */ |
d6d3c909 | 48 | bool is_nspace; |
49 | bool is_internal; | |
b75b98aa | 50 | bool is_deferred; |
51 | bool allow_expansion; | |
c6f14ce5 | 52 | union { |
53 | pragma_cb handler; | |
54 | struct pragma_entry *space; | |
b75b98aa | 55 | unsigned int ident; |
c6f14ce5 | 56 | } u; |
57 | }; | |
58 | ||
79bd622b | 59 | /* Values for the origin field of struct directive. KANDR directives |
60 | come from traditional (K&R) C. STDC89 directives come from the | |
61 | 1989 C standard. EXTENSION directives are extensions. */ | |
62 | #define KANDR 0 | |
63 | #define STDC89 1 | |
64 | #define EXTENSION 2 | |
65 | ||
66 | /* Values for the flags field of struct directive. COND indicates a | |
67 | conditional; IF_COND an opening conditional. INCL means to treat | |
68 | "..." and <...> as q-char and h-char sequences respectively. IN_I | |
69 | means this directive should be handled even if -fpreprocessed is in | |
fb83e0d6 | 70 | effect (these are the directives with callback hooks). |
71 | ||
bab5e68f | 72 | EXPAND is set on directives that are always macro-expanded. */ |
79bd622b | 73 | #define COND (1 << 0) |
74 | #define IF_COND (1 << 1) | |
75 | #define INCL (1 << 2) | |
76 | #define IN_I (1 << 3) | |
fb83e0d6 | 77 | #define EXPAND (1 << 4) |
79bd622b | 78 | |
79 | /* Defines one #-directive, including how to handle it. */ | |
f7fdd7a1 | 80 | typedef void (*directive_handler) (cpp_reader *); |
79bd622b | 81 | typedef struct directive directive; |
82 | struct directive | |
83 | { | |
84 | directive_handler handler; /* Function to handle directive. */ | |
b6d18b0a | 85 | const uchar *name; /* Name of directive. */ |
79bd622b | 86 | unsigned short length; /* Length of name. */ |
87 | unsigned char origin; /* Origin of directive. */ | |
88 | unsigned char flags; /* Flags describing this directive. */ | |
89 | }; | |
90 | ||
08c74ec4 | 91 | /* Forward declarations. */ |
92 | ||
f7fdd7a1 | 93 | static void skip_rest_of_line (cpp_reader *); |
94 | static void check_eol (cpp_reader *); | |
95 | static void start_directive (cpp_reader *); | |
96 | static void prepare_directive_trad (cpp_reader *); | |
97 | static void end_directive (cpp_reader *, int); | |
98 | static void directive_diagnostics (cpp_reader *, const directive *, int); | |
99 | static void run_directive (cpp_reader *, int, const char *, size_t); | |
100 | static char *glue_header_name (cpp_reader *); | |
f789fe3e | 101 | static const char *parse_include (cpp_reader *, int *, const cpp_token ***); |
f7fdd7a1 | 102 | static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *); |
103 | static unsigned int read_flag (cpp_reader *, unsigned int); | |
f7fdd7a1 | 104 | static int strtoul_for_line (const uchar *, unsigned int, unsigned long *); |
105 | static void do_diagnostic (cpp_reader *, int, int); | |
3e2a04b5 | 106 | static cpp_hashnode *lex_macro_node (cpp_reader *, bool); |
40109983 | 107 | static int undefine_macros (cpp_reader *, cpp_hashnode *, void *); |
f7fdd7a1 | 108 | static void do_include_common (cpp_reader *, enum include_type); |
109 | static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *, | |
110 | const cpp_hashnode *); | |
f7fdd7a1 | 111 | static int count_registered_pragmas (struct pragma_entry *); |
112 | static char ** save_registered_pragmas (struct pragma_entry *, char **); | |
113 | static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *, | |
114 | char **); | |
115 | static void do_pragma_once (cpp_reader *); | |
116 | static void do_pragma_poison (cpp_reader *); | |
117 | static void do_pragma_system_header (cpp_reader *); | |
118 | static void do_pragma_dependency (cpp_reader *); | |
119 | static void do_linemarker (cpp_reader *); | |
120 | static const cpp_token *get_token_no_padding (cpp_reader *); | |
121 | static const cpp_token *get__Pragma_string (cpp_reader *); | |
122 | static void destringize_and_run (cpp_reader *, const cpp_string *); | |
123 | static int parse_answer (cpp_reader *, struct answer **, int); | |
124 | static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int); | |
125 | static struct answer ** find_answer (cpp_hashnode *, const struct answer *); | |
126 | static void handle_assertion (cpp_reader *, const char *, int); | |
a4b4a940 | 127 | |
4bf559f2 | 128 | /* This is the table of directive handlers. It is ordered by |
129 | frequency of occurrence; the numbers at the end are directive | |
130 | counts from all the source code I have lying around (egcs and libc | |
131 | CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and | |
79bd622b | 132 | pcmcia-cs-3.0.9). This is no longer important as directive lookup |
133 | is now O(1). All extensions other than #warning and #include_next | |
134 | are deprecated. The name is where the extension appears to have | |
135 | come from. */ | |
4bf559f2 | 136 | |
79bd622b | 137 | #define DIRECTIVE_TABLE \ |
138 | D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \ | |
bab5e68f | 139 | D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \ |
79bd622b | 140 | D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \ |
141 | D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \ | |
fb83e0d6 | 142 | D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \ |
79bd622b | 143 | D(else, T_ELSE, KANDR, COND) /* 9863 */ \ |
144 | D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \ | |
145 | D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \ | |
fb83e0d6 | 146 | D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \ |
147 | D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \ | |
79bd622b | 148 | D(error, T_ERROR, STDC89, 0) /* 475 */ \ |
149 | D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \ | |
150 | D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \ | |
bab5e68f | 151 | D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \ |
79bd622b | 152 | D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \ |
bab5e68f | 153 | D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \ |
79bd622b | 154 | D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \ |
155 | D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \ | |
81e19b31 | 156 | D(sccs, T_SCCS, EXTENSION, IN_I) /* 0 SVR4? */ |
157 | ||
158 | /* #sccs is synonymous with #ident. */ | |
159 | #define do_sccs do_ident | |
ef33b55c | 160 | |
4bf559f2 | 161 | /* Use the table to generate a series of prototypes, an enum for the |
162 | directive names, and an array of directive handlers. */ | |
163 | ||
f7fdd7a1 | 164 | #define D(name, t, o, f) static void do_##name (cpp_reader *); |
4bf559f2 | 165 | DIRECTIVE_TABLE |
166 | #undef D | |
167 | ||
f80e83a9 | 168 | #define D(n, tag, o, f) tag, |
4bf559f2 | 169 | enum |
170 | { | |
171 | DIRECTIVE_TABLE | |
172 | N_DIRECTIVES | |
1d6fa33f | 173 | }; |
4bf559f2 | 174 | #undef D |
175 | ||
f80e83a9 | 176 | #define D(name, t, origin, flags) \ |
18e43155 | 177 | { do_##name, (const uchar *) #name, \ |
178 | sizeof #name - 1, origin, flags }, | |
79bd622b | 179 | static const directive dtable[] = |
4bf559f2 | 180 | { |
181 | DIRECTIVE_TABLE | |
182 | }; | |
183 | #undef D | |
184 | #undef DIRECTIVE_TABLE | |
1d6fa33f | 185 | |
25266990 | 186 | /* Wrapper struct directive for linemarkers. |
187 | The origin is more or less true - the original K+R cpp | |
188 | did use this notation in its preprocessed output. */ | |
189 | static const directive linemarker_dir = | |
190 | { | |
191 | do_linemarker, U"#", 1, KANDR, IN_I | |
192 | }; | |
193 | ||
fb83e0d6 | 194 | #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF) |
1ea7ed21 | 195 | |
79bd622b | 196 | /* Skip any remaining tokens in a directive. */ |
197 | static void | |
f7fdd7a1 | 198 | skip_rest_of_line (cpp_reader *pfile) |
6060326b | 199 | { |
79bd622b | 200 | /* Discard all stacked contexts. */ |
6e5ef531 | 201 | while (pfile->context->prev) |
79bd622b | 202 | _cpp_pop_context (pfile); |
203 | ||
920b5d41 | 204 | /* Sweep up all tokens remaining on the line. */ |
c00e481c | 205 | if (! SEEN_EOL ()) |
206 | while (_cpp_lex_token (pfile)->type != CPP_EOF) | |
207 | ; | |
79bd622b | 208 | } |
6060326b | 209 | |
79bd622b | 210 | /* Ensure there are no stray tokens at the end of a directive. */ |
211 | static void | |
f7fdd7a1 | 212 | check_eol (cpp_reader *pfile) |
79bd622b | 213 | { |
c00e481c | 214 | if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF) |
d80d2074 | 215 | cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive", |
73328dce | 216 | pfile->directive->name); |
79bd622b | 217 | } |
218 | ||
f789fe3e | 219 | /* Ensure there are no stray tokens other than comments at the end of |
220 | a directive, and gather the comments. */ | |
221 | static const cpp_token ** | |
222 | check_eol_return_comments (cpp_reader *pfile) | |
223 | { | |
224 | size_t c; | |
225 | size_t capacity = 8; | |
226 | const cpp_token **buf; | |
227 | ||
228 | buf = XNEWVEC (const cpp_token *, capacity); | |
229 | c = 0; | |
230 | if (! SEEN_EOL ()) | |
231 | { | |
232 | while (1) | |
233 | { | |
234 | const cpp_token *tok; | |
235 | ||
236 | tok = _cpp_lex_token (pfile); | |
237 | if (tok->type == CPP_EOF) | |
238 | break; | |
239 | if (tok->type != CPP_COMMENT) | |
240 | cpp_error (pfile, CPP_DL_PEDWARN, | |
241 | "extra tokens at end of #%s directive", | |
242 | pfile->directive->name); | |
243 | else | |
244 | { | |
245 | if (c + 1 >= capacity) | |
246 | { | |
247 | capacity *= 2; | |
248 | buf = XRESIZEVEC (const cpp_token *, buf, capacity); | |
249 | } | |
250 | buf[c] = tok; | |
251 | ++c; | |
252 | } | |
253 | } | |
254 | } | |
255 | buf[c] = NULL; | |
256 | return buf; | |
257 | } | |
258 | ||
9677c711 | 259 | /* Called when entering a directive, _Pragma or command-line directive. */ |
260 | static void | |
f7fdd7a1 | 261 | start_directive (cpp_reader *pfile) |
79bd622b | 262 | { |
343fd982 | 263 | /* Setup in-directive state. */ |
264 | pfile->state.in_directive = 1; | |
265 | pfile->state.save_comments = 0; | |
d6d3c909 | 266 | pfile->directive_result.type = CPP_PADDING; |
343fd982 | 267 | |
79bd622b | 268 | /* Some handlers need the position of the # for diagnostics. */ |
dbddc569 | 269 | pfile->directive_line = pfile->line_table->highest_line; |
9677c711 | 270 | } |
271 | ||
272 | /* Called when leaving a directive, _Pragma or command-line directive. */ | |
273 | static void | |
f7fdd7a1 | 274 | end_directive (cpp_reader *pfile, int skip_line) |
9677c711 | 275 | { |
b75b98aa | 276 | if (pfile->state.in_deferred_pragma) |
277 | ; | |
278 | else if (CPP_OPTION (pfile, traditional)) | |
fb83e0d6 | 279 | { |
bab5e68f | 280 | /* Revert change of prepare_directive_trad. */ |
281 | pfile->state.prevent_expansion--; | |
282 | ||
c455fac3 | 283 | if (pfile->directive != &dtable[T_DEFINE]) |
fb83e0d6 | 284 | _cpp_remove_overlay (pfile); |
285 | } | |
9677c711 | 286 | /* We don't skip for an assembler #. */ |
c455fac3 | 287 | else if (skip_line) |
1ea7ed21 | 288 | { |
289 | skip_rest_of_line (pfile); | |
fb5ab82c | 290 | if (!pfile->keep_tokens) |
291 | { | |
292 | pfile->cur_run = &pfile->base_run; | |
293 | pfile->cur_token = pfile->base_run.base; | |
294 | } | |
1ea7ed21 | 295 | } |
9677c711 | 296 | |
297 | /* Restore state. */ | |
9677c711 | 298 | pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments); |
299 | pfile->state.in_directive = 0; | |
bab5e68f | 300 | pfile->state.in_expression = 0; |
9677c711 | 301 | pfile->state.angled_headers = 0; |
302 | pfile->directive = 0; | |
303 | } | |
304 | ||
fb83e0d6 | 305 | /* Prepare to handle the directive in pfile->directive. */ |
306 | static void | |
f7fdd7a1 | 307 | prepare_directive_trad (cpp_reader *pfile) |
fb83e0d6 | 308 | { |
cbe337b6 | 309 | if (pfile->directive != &dtable[T_DEFINE]) |
fb83e0d6 | 310 | { |
c455fac3 | 311 | bool no_expand = (pfile->directive |
312 | && ! (pfile->directive->flags & EXPAND)); | |
01628c3c | 313 | bool was_skipping = pfile->state.skipping; |
fb83e0d6 | 314 | |
bab5e68f | 315 | pfile->state.in_expression = (pfile->directive == &dtable[T_IF] |
316 | || pfile->directive == &dtable[T_ELIF]); | |
8b837dff | 317 | if (pfile->state.in_expression) |
318 | pfile->state.skipping = false; | |
319 | ||
fb83e0d6 | 320 | if (no_expand) |
321 | pfile->state.prevent_expansion++; | |
69edc0b3 | 322 | _cpp_scan_out_logical_line (pfile, NULL); |
fb83e0d6 | 323 | if (no_expand) |
324 | pfile->state.prevent_expansion--; | |
8b837dff | 325 | |
01628c3c | 326 | pfile->state.skipping = was_skipping; |
fb83e0d6 | 327 | _cpp_overlay_buffer (pfile, pfile->out.base, |
328 | pfile->out.cur - pfile->out.base); | |
329 | } | |
bab5e68f | 330 | |
331 | /* Stop ISO C from expanding anything. */ | |
332 | pfile->state.prevent_expansion++; | |
fb83e0d6 | 333 | } |
334 | ||
d10cfa8d | 335 | /* Output diagnostics for a directive DIR. INDENTED is nonzero if |
d0d481d7 | 336 | the '#' was indented. */ |
d0d481d7 | 337 | static void |
f7fdd7a1 | 338 | directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented) |
d0d481d7 | 339 | { |
25266990 | 340 | /* Issue -pedantic warnings for extensions. */ |
341 | if (CPP_PEDANTIC (pfile) | |
342 | && ! pfile->state.skipping | |
343 | && dir->origin == EXTENSION) | |
d80d2074 | 344 | cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name); |
25266990 | 345 | |
346 | /* Traditionally, a directive is ignored unless its # is in | |
347 | column 1. Therefore in code intended to work with K+R | |
348 | compilers, directives added by C89 must have their # | |
349 | indented, and directives present in traditional C must not. | |
350 | This is true even of directives in skipped conditional | |
351 | blocks. #elif cannot be used at all. */ | |
352 | if (CPP_WTRADITIONAL (pfile)) | |
d0d481d7 | 353 | { |
25266990 | 354 | if (dir == &dtable[T_ELIF]) |
d80d2074 | 355 | cpp_error (pfile, CPP_DL_WARNING, |
73328dce | 356 | "suggest not using #elif in traditional C"); |
25266990 | 357 | else if (indented && dir->origin == KANDR) |
d80d2074 | 358 | cpp_error (pfile, CPP_DL_WARNING, |
73328dce | 359 | "traditional C ignores #%s with the # indented", |
360 | dir->name); | |
25266990 | 361 | else if (!indented && dir->origin != KANDR) |
d80d2074 | 362 | cpp_error (pfile, CPP_DL_WARNING, |
73328dce | 363 | "suggest hiding #%s from traditional C with an indented #", |
364 | dir->name); | |
d0d481d7 | 365 | } |
366 | } | |
367 | ||
d10cfa8d | 368 | /* Check if we have a known directive. INDENTED is nonzero if the |
d0d481d7 | 369 | '#' of the directive was indented. This function is in this file |
f03668bd | 370 | to save unnecessarily exporting dtable etc. to lex.c. Returns |
d10cfa8d | 371 | nonzero if the line of tokens has been handled, zero if we should |
d0d481d7 | 372 | continue processing the line. */ |
9677c711 | 373 | int |
f7fdd7a1 | 374 | _cpp_handle_directive (cpp_reader *pfile, int indented) |
9677c711 | 375 | { |
9677c711 | 376 | const directive *dir = 0; |
c00e481c | 377 | const cpp_token *dname; |
d6af0368 | 378 | bool was_parsing_args = pfile->state.parsing_args; |
3eb3f293 | 379 | bool was_discarding_output = pfile->state.discarding_output; |
9677c711 | 380 | int skip = 1; |
381 | ||
3eb3f293 | 382 | if (was_discarding_output) |
383 | pfile->state.prevent_expansion = 0; | |
384 | ||
d6af0368 | 385 | if (was_parsing_args) |
386 | { | |
387 | if (CPP_OPTION (pfile, pedantic)) | |
d80d2074 | 388 | cpp_error (pfile, CPP_DL_PEDWARN, |
d6af0368 | 389 | "embedding a directive within macro arguments is not portable"); |
390 | pfile->state.parsing_args = 0; | |
391 | pfile->state.prevent_expansion = 0; | |
392 | } | |
9677c711 | 393 | start_directive (pfile); |
c00e481c | 394 | dname = _cpp_lex_token (pfile); |
338fa5f7 | 395 | |
c00e481c | 396 | if (dname->type == CPP_NAME) |
338fa5f7 | 397 | { |
805e22b2 | 398 | if (dname->val.node->is_directive) |
399 | dir = &dtable[dname->val.node->directive_index]; | |
338fa5f7 | 400 | } |
a2f10574 | 401 | /* We do not recognize the # followed by a number extension in |
d0d481d7 | 402 | assembler code. */ |
c00e481c | 403 | else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM) |
338fa5f7 | 404 | { |
25266990 | 405 | dir = &linemarker_dir; |
406 | if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed) | |
407 | && ! pfile->state.skipping) | |
d80d2074 | 408 | cpp_error (pfile, CPP_DL_PEDWARN, |
73328dce | 409 | "style of line directive is a GCC extension"); |
79bd622b | 410 | } |
338fa5f7 | 411 | |
79bd622b | 412 | if (dir) |
413 | { | |
d0d481d7 | 414 | /* If we have a directive that is not an opening conditional, |
415 | invalidate any control macro. */ | |
416 | if (! (dir->flags & IF_COND)) | |
417 | pfile->mi_valid = false; | |
418 | ||
419 | /* Kluge alert. In order to be sure that code like this | |
420 | ||
421 | #define HASH # | |
422 | HASH define foo bar | |
423 | ||
424 | does not cause '#define foo bar' to get executed when | |
425 | compiled with -save-temps, we recognize directives in | |
f03668bd | 426 | -fpreprocessed mode only if the # is in column 1. macro.c |
fcde64dc | 427 | puts a space in front of any '#' at the start of a macro. |
428 | ||
429 | We exclude the -fdirectives-only case because macro expansion | |
430 | has not been performed yet, and block comments can cause spaces | |
431 | to preceed the directive. */ | |
d0d481d7 | 432 | if (CPP_OPTION (pfile, preprocessed) |
fcde64dc | 433 | && !CPP_OPTION (pfile, directives_only) |
d0d481d7 | 434 | && (indented || !(dir->flags & IN_I))) |
946ce1b7 | 435 | { |
d0d481d7 | 436 | skip = 0; |
437 | dir = 0; | |
946ce1b7 | 438 | } |
439 | else | |
79bd622b | 440 | { |
d0d481d7 | 441 | /* In failed conditional groups, all non-conditional |
442 | directives are ignored. Before doing that, whether | |
443 | skipping or not, we should lex angle-bracketed headers | |
444 | correctly, and maybe output some diagnostics. */ | |
445 | pfile->state.angled_headers = dir->flags & INCL; | |
7825551f | 446 | pfile->state.directive_wants_padding = dir->flags & INCL; |
d0d481d7 | 447 | if (! CPP_OPTION (pfile, preprocessed)) |
448 | directive_diagnostics (pfile, dir, indented); | |
449 | if (pfile->state.skipping && !(dir->flags & COND)) | |
450 | dir = 0; | |
79bd622b | 451 | } |
452 | } | |
c00e481c | 453 | else if (dname->type == CPP_EOF) |
d0d481d7 | 454 | ; /* CPP_EOF is the "null directive". */ |
455 | else | |
79bd622b | 456 | { |
457 | /* An unknown directive. Don't complain about it in assembly | |
458 | source: we don't know where the comments are, and # may | |
459 | introduce assembler pseudo-ops. Don't complain about invalid | |
460 | directives in skipped conditional groups (6.10 p4). */ | |
5db5d057 | 461 | if (CPP_OPTION (pfile, lang) == CLK_ASM) |
d0d481d7 | 462 | skip = 0; |
463 | else if (!pfile->state.skipping) | |
d80d2074 | 464 | cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s", |
c00e481c | 465 | cpp_token_as_text (pfile, dname)); |
338fa5f7 | 466 | } |
467 | ||
94fed299 | 468 | pfile->directive = dir; |
469 | if (CPP_OPTION (pfile, traditional)) | |
470 | prepare_directive_trad (pfile); | |
471 | ||
d0d481d7 | 472 | if (dir) |
f7fdd7a1 | 473 | pfile->directive->handler (pfile); |
d0d481d7 | 474 | else if (skip == 0) |
475 | _cpp_backup_tokens (pfile, 1); | |
476 | ||
477 | end_directive (pfile, skip); | |
45f9f140 | 478 | if (was_parsing_args && !pfile->state.in_deferred_pragma) |
d6af0368 | 479 | { |
480 | /* Restore state when within macro args. */ | |
481 | pfile->state.parsing_args = 2; | |
482 | pfile->state.prevent_expansion = 1; | |
d6af0368 | 483 | } |
3eb3f293 | 484 | if (was_discarding_output) |
485 | pfile->state.prevent_expansion = 1; | |
9677c711 | 486 | return skip; |
f80e83a9 | 487 | } |
1d6fa33f | 488 | |
79bd622b | 489 | /* Directive handler wrapper used by the command line option |
a54e0bf8 | 490 | processor. BUF is \n terminated. */ |
79bd622b | 491 | static void |
f7fdd7a1 | 492 | run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count) |
e3630c4b | 493 | { |
b6d18b0a | 494 | cpp_push_buffer (pfile, (const uchar *) buf, count, |
9eb74666 | 495 | /* from_stage3 */ true); |
6cee4464 | 496 | start_directive (pfile); |
a54e0bf8 | 497 | |
498 | /* This is a short-term fix to prevent a leading '#' being | |
499 | interpreted as a directive. */ | |
500 | _cpp_clean_line (pfile); | |
501 | ||
7b5cc295 | 502 | pfile->directive = &dtable[dir_no]; |
fb83e0d6 | 503 | if (CPP_OPTION (pfile, traditional)) |
504 | prepare_directive_trad (pfile); | |
f7fdd7a1 | 505 | pfile->directive->handler (pfile); |
6cee4464 | 506 | end_directive (pfile, 1); |
4dfe8b74 | 507 | _cpp_pop_buffer (pfile); |
79bd622b | 508 | } |
509 | ||
510 | /* Checks for validity the macro name in #define, #undef, #ifdef and | |
3e2a04b5 | 511 | #ifndef directives. IS_DEF_OR_UNDEF is true if this call is |
512 | processing a #define or #undefine directive, and false | |
513 | otherwise. */ | |
f80e83a9 | 514 | static cpp_hashnode * |
3e2a04b5 | 515 | lex_macro_node (cpp_reader *pfile, bool is_def_or_undef) |
f80e83a9 | 516 | { |
fb83e0d6 | 517 | const cpp_token *token = _cpp_lex_token (pfile); |
c4357c92 | 518 | |
31674461 | 519 | /* The token immediately after #define must be an identifier. That |
f17c0828 | 520 | identifier may not be "defined", per C99 6.10.8p4. |
521 | In C++, it may not be any of the "named operators" either, | |
522 | per C++98 [lex.digraph], [lex.key]. | |
523 | Finally, the identifier may not have been poisoned. (In that case | |
9a26b86f | 524 | the lexer has issued the error message for us.) */ |
f15f6c8d | 525 | |
fb83e0d6 | 526 | if (token->type == CPP_NAME) |
527 | { | |
528 | cpp_hashnode *node = token->val.node; | |
31674461 | 529 | |
3e2a04b5 | 530 | if (is_def_or_undef && node == pfile->spec_nodes.n_defined) |
d80d2074 | 531 | cpp_error (pfile, CPP_DL_ERROR, |
fb83e0d6 | 532 | "\"defined\" cannot be used as a macro name"); |
533 | else if (! (node->flags & NODE_POISONED)) | |
534 | return node; | |
71aa9da4 | 535 | } |
fb83e0d6 | 536 | else if (token->flags & NAMED_OP) |
d80d2074 | 537 | cpp_error (pfile, CPP_DL_ERROR, |
f15f6c8d | 538 | "\"%s\" cannot be used as a macro name as it is an operator in C++", |
fb83e0d6 | 539 | NODE_NAME (token->val.node)); |
540 | else if (token->type == CPP_EOF) | |
d80d2074 | 541 | cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive", |
fb83e0d6 | 542 | pfile->directive->name); |
543 | else | |
d80d2074 | 544 | cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers"); |
ef33b55c | 545 | |
f15f6c8d | 546 | return NULL; |
1d6fa33f | 547 | } |
1d6fa33f | 548 | |
f03668bd | 549 | /* Process a #define directive. Most work is done in macro.c. */ |
69461e0d | 550 | static void |
f7fdd7a1 | 551 | do_define (cpp_reader *pfile) |
1d6fa33f | 552 | { |
3e2a04b5 | 553 | cpp_hashnode *node = lex_macro_node (pfile, true); |
c030ed73 | 554 | |
79bd622b | 555 | if (node) |
556 | { | |
9a26b86f | 557 | /* If we have been requested to expand comments into macros, |
558 | then re-enable saving of comments. */ | |
559 | pfile->state.save_comments = | |
560 | ! CPP_OPTION (pfile, discard_comments_in_macro_exp); | |
561 | ||
79bd622b | 562 | if (_cpp_create_definition (pfile, node)) |
563 | if (pfile->cb.define) | |
f7fdd7a1 | 564 | pfile->cb.define (pfile, pfile->directive_line, node); |
79bd622b | 565 | } |
f80e83a9 | 566 | } |
567 | ||
e484a1cc | 568 | /* Handle #undef. Mark the identifier NT_VOID in the hash table. */ |
69461e0d | 569 | static void |
f7fdd7a1 | 570 | do_undef (cpp_reader *pfile) |
f80e83a9 | 571 | { |
3e2a04b5 | 572 | cpp_hashnode *node = lex_macro_node (pfile, true); |
2c63d6c8 | 573 | |
8b837dff | 574 | if (node) |
2c63d6c8 | 575 | { |
6cae2504 | 576 | if (pfile->cb.undef) |
f7fdd7a1 | 577 | pfile->cb.undef (pfile, pfile->directive_line, node); |
2c63d6c8 | 578 | |
8b837dff | 579 | /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified |
580 | identifier is not currently defined as a macro name. */ | |
581 | if (node->type == NT_MACRO) | |
582 | { | |
583 | if (node->flags & NODE_WARN) | |
584 | cpp_error (pfile, CPP_DL_WARNING, | |
585 | "undefining \"%s\"", NODE_NAME (node)); | |
2c63d6c8 | 586 | |
8b837dff | 587 | if (CPP_OPTION (pfile, warn_unused_macros)) |
588 | _cpp_warn_if_unused_macro (pfile, node, NULL); | |
71a7c282 | 589 | |
8b837dff | 590 | _cpp_free_definition (node); |
591 | } | |
2c63d6c8 | 592 | } |
8b837dff | 593 | |
79bd622b | 594 | check_eol (pfile); |
1d6fa33f | 595 | } |
596 | ||
40109983 | 597 | /* Undefine a single macro/assertion/whatever. */ |
598 | ||
599 | static int | |
3eb3f293 | 600 | undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h, |
40109983 | 601 | void *data_p ATTRIBUTE_UNUSED) |
602 | { | |
3eb3f293 | 603 | /* Body of _cpp_free_definition inlined here for speed. |
604 | Macros and assertions no longer have anything to free. */ | |
605 | h->type = NT_VOID; | |
606 | h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED); | |
40109983 | 607 | return 1; |
608 | } | |
609 | ||
610 | /* Undefine all macros and assertions. */ | |
611 | ||
612 | void | |
613 | cpp_undef_all (cpp_reader *pfile) | |
614 | { | |
615 | cpp_forall_identifiers (pfile, undefine_macros, NULL); | |
616 | } | |
617 | ||
618 | ||
79bd622b | 619 | /* Helper routine used by parse_include. Reinterpret the current line |
620 | as an h-char-sequence (< ... >); we are looking at the first token | |
58efbd5a | 621 | after the <. Returns a malloced filename. */ |
622 | static char * | |
f7fdd7a1 | 623 | glue_header_name (cpp_reader *pfile) |
79bd622b | 624 | { |
f9b5f742 | 625 | const cpp_token *token; |
58efbd5a | 626 | char *buffer; |
92b1c81a | 627 | size_t len, total_len = 0, capacity = 1024; |
79bd622b | 628 | |
629 | /* To avoid lexed tokens overwriting our glued name, we can only | |
630 | allocate from the string pool once we've lexed everything. */ | |
720aca92 | 631 | buffer = XNEWVEC (char, capacity); |
79bd622b | 632 | for (;;) |
633 | { | |
7825551f | 634 | token = get_token_no_padding (pfile); |
79bd622b | 635 | |
58efbd5a | 636 | if (token->type == CPP_GREATER) |
79bd622b | 637 | break; |
58efbd5a | 638 | if (token->type == CPP_EOF) |
639 | { | |
d80d2074 | 640 | cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character"); |
58efbd5a | 641 | break; |
642 | } | |
79bd622b | 643 | |
b1280514 | 644 | len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */ |
92b1c81a | 645 | if (total_len + len > capacity) |
79bd622b | 646 | { |
92b1c81a | 647 | capacity = (capacity + len) * 2; |
720aca92 | 648 | buffer = XRESIZEVEC (char, buffer, capacity); |
79bd622b | 649 | } |
650 | ||
f9b5f742 | 651 | if (token->flags & PREV_WHITE) |
92b1c81a | 652 | buffer[total_len++] = ' '; |
79bd622b | 653 | |
bb1fa6bb | 654 | total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len], |
655 | true) | |
58efbd5a | 656 | - (uchar *) buffer); |
79bd622b | 657 | } |
f80e83a9 | 658 | |
58efbd5a | 659 | buffer[total_len] = '\0'; |
660 | return buffer; | |
79bd622b | 661 | } |
1d6fa33f | 662 | |
58efbd5a | 663 | /* Returns the file name of #include, #include_next, #import and |
664 | #pragma dependency. The string is malloced and the caller should | |
665 | free it. Returns NULL on error. */ | |
666 | static const char * | |
f789fe3e | 667 | parse_include (cpp_reader *pfile, int *pangle_brackets, |
668 | const cpp_token ***buf) | |
1d6fa33f | 669 | { |
58efbd5a | 670 | char *fname; |
f9b5f742 | 671 | const cpp_token *header; |
1d6fa33f | 672 | |
79bd622b | 673 | /* Allow macro expansion. */ |
7825551f | 674 | header = get_token_no_padding (pfile); |
58efbd5a | 675 | if (header->type == CPP_STRING || header->type == CPP_HEADER_NAME) |
1d6fa33f | 676 | { |
720aca92 | 677 | fname = XNEWVEC (char, header->val.str.len - 1); |
4970d4c2 | 678 | memcpy (fname, header->val.str.text + 1, header->val.str.len - 2); |
679 | fname[header->val.str.len - 2] = '\0'; | |
58efbd5a | 680 | *pangle_brackets = header->type == CPP_HEADER_NAME; |
1d6fa33f | 681 | } |
58efbd5a | 682 | else if (header->type == CPP_LESS) |
1d6fa33f | 683 | { |
58efbd5a | 684 | fname = glue_header_name (pfile); |
685 | *pangle_brackets = 1; | |
686 | } | |
687 | else | |
688 | { | |
689 | const unsigned char *dir; | |
690 | ||
691 | if (pfile->directive == &dtable[T_PRAGMA]) | |
692 | dir = U"pragma dependency"; | |
693 | else | |
694 | dir = pfile->directive->name; | |
d80d2074 | 695 | cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>", |
58efbd5a | 696 | dir); |
697 | ||
f9b5f742 | 698 | return NULL; |
1d6fa33f | 699 | } |
1d6fa33f | 700 | |
36ec4203 | 701 | if (pfile->directive == &dtable[T_PRAGMA]) |
702 | { | |
703 | /* This pragma allows extra tokens after the file name. */ | |
704 | } | |
705 | else if (buf == NULL || CPP_OPTION (pfile, discard_comments)) | |
f789fe3e | 706 | check_eol (pfile); |
707 | else | |
708 | { | |
709 | /* If we are not discarding comments, then gather them while | |
710 | doing the eol check. */ | |
711 | *buf = check_eol_return_comments (pfile); | |
712 | } | |
713 | ||
58efbd5a | 714 | return fname; |
4bf559f2 | 715 | } |
d453a374 | 716 | |
0d6d8dc0 | 717 | /* Handle #include, #include_next and #import. */ |
69461e0d | 718 | static void |
f7fdd7a1 | 719 | do_include_common (cpp_reader *pfile, enum include_type type) |
4bf559f2 | 720 | { |
58efbd5a | 721 | const char *fname; |
722 | int angle_brackets; | |
f789fe3e | 723 | const cpp_token **buf = NULL; |
724 | ||
725 | /* Re-enable saving of comments if requested, so that the include | |
726 | callback can dump comments which follow #include. */ | |
727 | pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments); | |
58efbd5a | 728 | |
f789fe3e | 729 | fname = parse_include (pfile, &angle_brackets, &buf); |
58efbd5a | 730 | if (!fname) |
f789fe3e | 731 | { |
732 | if (buf) | |
733 | XDELETEVEC (buf); | |
734 | return; | |
735 | } | |
1d6fa33f | 736 | |
bd9e37a6 | 737 | if (!*fname) |
738 | { | |
739 | cpp_error (pfile, CPP_DL_ERROR, "empty filename in #%s", | |
740 | pfile->directive->name); | |
f789fe3e | 741 | XDELETEVEC (fname); |
742 | if (buf) | |
743 | XDELETEVEC (buf); | |
bd9e37a6 | 744 | return; |
745 | } | |
746 | ||
f4578baa | 747 | /* Prevent #include recursion. */ |
ceec9c13 | 748 | if (pfile->line_table->depth >= CPP_STACK_MAX) |
d80d2074 | 749 | cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply"); |
58efbd5a | 750 | else |
968e3f9d | 751 | { |
58efbd5a | 752 | /* Get out of macro context, if we are. */ |
753 | skip_rest_of_line (pfile); | |
968e3f9d | 754 | |
58efbd5a | 755 | if (pfile->cb.include) |
f7fdd7a1 | 756 | pfile->cb.include (pfile, pfile->directive_line, |
f789fe3e | 757 | pfile->directive->name, fname, angle_brackets, |
758 | buf); | |
f4578baa | 759 | |
1824e2bd | 760 | _cpp_stack_include (pfile, fname, angle_brackets, type); |
58efbd5a | 761 | } |
f4578baa | 762 | |
f789fe3e | 763 | XDELETEVEC (fname); |
764 | if (buf) | |
765 | XDELETEVEC (buf); | |
4bf559f2 | 766 | } |
c4e0ec82 | 767 | |
69461e0d | 768 | static void |
f7fdd7a1 | 769 | do_include (cpp_reader *pfile) |
4bf559f2 | 770 | { |
0d6d8dc0 | 771 | do_include_common (pfile, IT_INCLUDE); |
772 | } | |
4bf559f2 | 773 | |
0d6d8dc0 | 774 | static void |
f7fdd7a1 | 775 | do_import (cpp_reader *pfile) |
0d6d8dc0 | 776 | { |
0d6d8dc0 | 777 | do_include_common (pfile, IT_IMPORT); |
4bf559f2 | 778 | } |
1d6fa33f | 779 | |
69461e0d | 780 | static void |
f7fdd7a1 | 781 | do_include_next (cpp_reader *pfile) |
4bf559f2 | 782 | { |
f4578baa | 783 | enum include_type type = IT_INCLUDE_NEXT; |
784 | ||
785 | /* If this is the primary source file, warn and use the normal | |
786 | search logic. */ | |
927b511f | 787 | if (cpp_in_primary_file (pfile)) |
f4578baa | 788 | { |
d80d2074 | 789 | cpp_error (pfile, CPP_DL_WARNING, |
f4578baa | 790 | "#include_next in primary source file"); |
791 | type = IT_INCLUDE; | |
792 | } | |
793 | do_include_common (pfile, type); | |
1d6fa33f | 794 | } |
1d6fa33f | 795 | |
25266990 | 796 | /* Subroutine of do_linemarker. Read possible flags after file name. |
797 | LAST is the last flag seen; 0 if this is the first flag. Return the | |
798 | flag if it is valid, 0 at the end of the directive. Otherwise | |
799 | complain. */ | |
2fd3b10b | 800 | static unsigned int |
f7fdd7a1 | 801 | read_flag (cpp_reader *pfile, unsigned int last) |
314ab6d5 | 802 | { |
c00e481c | 803 | const cpp_token *token = _cpp_lex_token (pfile); |
314ab6d5 | 804 | |
c00e481c | 805 | if (token->type == CPP_NUMBER && token->val.str.len == 1) |
314ab6d5 | 806 | { |
c00e481c | 807 | unsigned int flag = token->val.str.text[0] - '0'; |
53c052ca | 808 | |
809 | if (flag > last && flag <= 4 | |
810 | && (flag != 4 || last == 3) | |
811 | && (flag != 2 || last == 0)) | |
812 | return flag; | |
314ab6d5 | 813 | } |
79bd622b | 814 | |
c00e481c | 815 | if (token->type != CPP_EOF) |
d80d2074 | 816 | cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive", |
c00e481c | 817 | cpp_token_as_text (pfile, token)); |
79bd622b | 818 | return 0; |
314ab6d5 | 819 | } |
820 | ||
25266990 | 821 | /* Subroutine of do_line and do_linemarker. Convert a number in STR, |
822 | of length LEN, to binary; store it in NUMP, and return 0 if the | |
823 | number was well-formed, 1 if not. Temporary, hopefully. */ | |
f80e83a9 | 824 | static int |
f7fdd7a1 | 825 | strtoul_for_line (const uchar *str, unsigned int len, long unsigned int *nump) |
f80e83a9 | 826 | { |
827 | unsigned long reg = 0; | |
b6d18b0a | 828 | uchar c; |
f80e83a9 | 829 | while (len--) |
830 | { | |
831 | c = *str++; | |
832 | if (!ISDIGIT (c)) | |
833 | return 1; | |
834 | reg *= 10; | |
835 | reg += c - '0'; | |
836 | } | |
837 | *nump = reg; | |
838 | return 0; | |
839 | } | |
840 | ||
6d71dc85 | 841 | /* Interpret #line command. |
25266990 | 842 | Note that the filename string (if any) is a true string constant |
843 | (escapes are interpreted), unlike in #line. */ | |
69461e0d | 844 | static void |
f7fdd7a1 | 845 | do_line (cpp_reader *pfile) |
1d6fa33f | 846 | { |
dbddc569 | 847 | const struct line_maps *line_table = pfile->line_table; |
848 | const struct line_map *map = &line_table->maps[line_table->used - 1]; | |
b0d0794d | 849 | |
850 | /* skip_rest_of_line() may cause line table to be realloc()ed so note down | |
851 | sysp right now. */ | |
852 | ||
853 | unsigned char map_sysp = map->sysp; | |
f9b5f742 | 854 | const cpp_token *token; |
610625e3 | 855 | const char *new_file = map->to_file; |
5a9f87bb | 856 | unsigned long new_lineno; |
79bd622b | 857 | |
d7503801 | 858 | /* C99 raised the minimum limit on #line numbers. */ |
25266990 | 859 | unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767; |
d0d481d7 | 860 | |
79bd622b | 861 | /* #line commands expand macros. */ |
f9b5f742 | 862 | token = cpp_get_token (pfile); |
863 | if (token->type != CPP_NUMBER | |
864 | || strtoul_for_line (token->val.str.text, token->val.str.len, | |
865 | &new_lineno)) | |
1d6fa33f | 866 | { |
cde59b72 | 867 | if (token->type == CPP_EOF) |
868 | cpp_error (pfile, CPP_DL_ERROR, "unexpected end of file after #line"); | |
869 | else | |
870 | cpp_error (pfile, CPP_DL_ERROR, | |
871 | "\"%s\" after #line is not a positive integer", | |
872 | cpp_token_as_text (pfile, token)); | |
0653b94e | 873 | return; |
b1a9ff83 | 874 | } |
1d6fa33f | 875 | |
25266990 | 876 | if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap)) |
d80d2074 | 877 | cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range"); |
1d6fa33f | 878 | |
f9b5f742 | 879 | token = cpp_get_token (pfile); |
880 | if (token->type == CPP_STRING) | |
a852e3b1 | 881 | { |
ebc03810 | 882 | cpp_string s = { 0, 0 }; |
7bc95b56 | 883 | if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1, |
884 | &s, false)) | |
ebc03810 | 885 | new_file = (const char *)s.text; |
25266990 | 886 | check_eol (pfile); |
887 | } | |
888 | else if (token->type != CPP_EOF) | |
889 | { | |
d80d2074 | 890 | cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename", |
25266990 | 891 | cpp_token_as_text (pfile, token)); |
892 | return; | |
893 | } | |
894 | ||
895 | skip_rest_of_line (pfile); | |
896 | _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno, | |
b0d0794d | 897 | map_sysp); |
25266990 | 898 | } |
899 | ||
900 | /* Interpret the # 44 "file" [flags] notation, which has slightly | |
901 | different syntax and semantics from #line: Flags are allowed, | |
902 | and we never complain about the line number being too big. */ | |
903 | static void | |
f7fdd7a1 | 904 | do_linemarker (cpp_reader *pfile) |
25266990 | 905 | { |
dbddc569 | 906 | const struct line_maps *line_table = pfile->line_table; |
907 | const struct line_map *map = &line_table->maps[line_table->used - 1]; | |
25266990 | 908 | const cpp_token *token; |
610625e3 | 909 | const char *new_file = map->to_file; |
25266990 | 910 | unsigned long new_lineno; |
610625e3 | 911 | unsigned int new_sysp = map->sysp; |
25266990 | 912 | enum lc_reason reason = LC_RENAME; |
913 | int flag; | |
914 | ||
915 | /* Back up so we can get the number again. Putting this in | |
916 | _cpp_handle_directive risks two calls to _cpp_backup_tokens in | |
917 | some circumstances, which can segfault. */ | |
918 | _cpp_backup_tokens (pfile, 1); | |
919 | ||
920 | /* #line commands expand macros. */ | |
921 | token = cpp_get_token (pfile); | |
922 | if (token->type != CPP_NUMBER | |
923 | || strtoul_for_line (token->val.str.text, token->val.str.len, | |
924 | &new_lineno)) | |
925 | { | |
cde59b72 | 926 | /* Unlike #line, there does not seem to be a way to get an EOF |
927 | here. So, it should be safe to always spell the token. */ | |
d80d2074 | 928 | cpp_error (pfile, CPP_DL_ERROR, |
929 | "\"%s\" after # is not a positive integer", | |
25266990 | 930 | cpp_token_as_text (pfile, token)); |
931 | return; | |
b1a9ff83 | 932 | } |
616814f8 | 933 | |
25266990 | 934 | token = cpp_get_token (pfile); |
935 | if (token->type == CPP_STRING) | |
936 | { | |
ebc03810 | 937 | cpp_string s = { 0, 0 }; |
7bc95b56 | 938 | if (cpp_interpret_string_notranslate (pfile, &token->val.str, |
939 | 1, &s, false)) | |
ebc03810 | 940 | new_file = (const char *)s.text; |
787c3d1a | 941 | |
25266990 | 942 | new_sysp = 0; |
943 | flag = read_flag (pfile, 0); | |
944 | if (flag == 1) | |
945 | { | |
946 | reason = LC_ENTER; | |
947 | /* Fake an include for cpp_included (). */ | |
948 | _cpp_fake_include (pfile, new_file); | |
949 | flag = read_flag (pfile, flag); | |
950 | } | |
951 | else if (flag == 2) | |
952 | { | |
953 | reason = LC_LEAVE; | |
954 | flag = read_flag (pfile, flag); | |
955 | } | |
956 | if (flag == 3) | |
79bd622b | 957 | { |
25266990 | 958 | new_sysp = 1; |
959 | flag = read_flag (pfile, flag); | |
960 | if (flag == 4) | |
961 | new_sysp = 2; | |
79bd622b | 962 | } |
5d70627e | 963 | pfile->buffer->sysp = new_sysp; |
25266990 | 964 | |
bd507c05 | 965 | check_eol (pfile); |
f80e83a9 | 966 | } |
f9b5f742 | 967 | else if (token->type != CPP_EOF) |
d7503801 | 968 | { |
d80d2074 | 969 | cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename", |
f9b5f742 | 970 | cpp_token_as_text (pfile, token)); |
d7503801 | 971 | return; |
972 | } | |
1d6fa33f | 973 | |
fb5ab82c | 974 | skip_rest_of_line (pfile); |
5a9f87bb | 975 | _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp); |
d7503801 | 976 | } |
977 | ||
1ea7ed21 | 978 | /* Arrange the file_change callback. pfile->line has changed to |
f85fcf2b | 979 | FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system |
3fb1e43b | 980 | header, 2 for a system header that needs to be extern "C" protected, |
f85fcf2b | 981 | and zero otherwise. */ |
4087613d | 982 | void |
f7fdd7a1 | 983 | _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason, |
984 | const char *to_file, unsigned int file_line, | |
985 | unsigned int sysp) | |
d7503801 | 986 | { |
610625e3 | 987 | const struct line_map *map = linemap_add (pfile->line_table, reason, sysp, |
988 | to_file, file_line); | |
dbddc569 | 989 | if (map != NULL) |
990 | linemap_line_start (pfile->line_table, map->to_line, 127); | |
38692459 | 991 | |
4087613d | 992 | if (pfile->cb.file_change) |
610625e3 | 993 | pfile->cb.file_change (pfile, map); |
1d6fa33f | 994 | } |
616814f8 | 995 | |
e484a1cc | 996 | /* Report a warning or error detected by the program we are |
997 | processing. Use the directive's tokens in the error message. */ | |
69461e0d | 998 | static void |
f7fdd7a1 | 999 | do_diagnostic (cpp_reader *pfile, int code, int print_dir) |
1d6fa33f | 1000 | { |
610625e3 | 1001 | if (_cpp_begin_message (pfile, code, pfile->cur_token[-1].src_loc, 0)) |
6cae2504 | 1002 | { |
def71b06 | 1003 | if (print_dir) |
1004 | fprintf (stderr, "#%s ", pfile->directive->name); | |
79bd622b | 1005 | pfile->state.prevent_expansion++; |
1006 | cpp_output_line (pfile, stderr); | |
1007 | pfile->state.prevent_expansion--; | |
6cae2504 | 1008 | } |
1d6fa33f | 1009 | } |
1010 | ||
f3b83e9b | 1011 | static void |
f7fdd7a1 | 1012 | do_error (cpp_reader *pfile) |
f3b83e9b | 1013 | { |
d80d2074 | 1014 | do_diagnostic (pfile, CPP_DL_ERROR, 1); |
f3b83e9b | 1015 | } |
1d6fa33f | 1016 | |
69461e0d | 1017 | static void |
f7fdd7a1 | 1018 | do_warning (cpp_reader *pfile) |
1d6fa33f | 1019 | { |
2ef5e3d3 | 1020 | /* We want #warning diagnostics to be emitted in system headers too. */ |
d80d2074 | 1021 | do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1); |
1d6fa33f | 1022 | } |
1023 | ||
4e29fcb2 | 1024 | /* Report program identification. */ |
69461e0d | 1025 | static void |
f7fdd7a1 | 1026 | do_ident (cpp_reader *pfile) |
1d6fa33f | 1027 | { |
f9b5f742 | 1028 | const cpp_token *str = cpp_get_token (pfile); |
6cae2504 | 1029 | |
f9b5f742 | 1030 | if (str->type != CPP_STRING) |
81e19b31 | 1031 | cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive", |
1032 | pfile->directive->name); | |
79bd622b | 1033 | else if (pfile->cb.ident) |
f7fdd7a1 | 1034 | pfile->cb.ident (pfile, pfile->directive_line, &str->val.str); |
4e29fcb2 | 1035 | |
79bd622b | 1036 | check_eol (pfile); |
1d6fa33f | 1037 | } |
1038 | ||
c6f14ce5 | 1039 | /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the |
1040 | matching entry, or NULL if none is found. The returned entry could | |
1041 | be the start of a namespace chain, or a pragma. */ | |
1042 | static struct pragma_entry * | |
f7fdd7a1 | 1043 | lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma) |
3f075661 | 1044 | { |
5c43d212 | 1045 | while (chain && chain->pragma != pragma) |
1046 | chain = chain->next; | |
c6f14ce5 | 1047 | |
1048 | return chain; | |
1049 | } | |
1050 | ||
b75b98aa | 1051 | /* Create and insert a blank pragma entry at the beginning of a |
1052 | singly-linked CHAIN. */ | |
c6f14ce5 | 1053 | static struct pragma_entry * |
b75b98aa | 1054 | new_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain) |
3f075661 | 1055 | { |
720aca92 | 1056 | struct pragma_entry *new_entry; |
6cae2504 | 1057 | |
720aca92 | 1058 | new_entry = (struct pragma_entry *) |
e6a5f963 | 1059 | _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry)); |
6cae2504 | 1060 | |
b75b98aa | 1061 | memset (new_entry, 0, sizeof (struct pragma_entry)); |
720aca92 | 1062 | new_entry->next = *chain; |
b75b98aa | 1063 | |
720aca92 | 1064 | *chain = new_entry; |
1065 | return new_entry; | |
6cae2504 | 1066 | } |
3f075661 | 1067 | |
c6f14ce5 | 1068 | /* Register a pragma NAME in namespace SPACE. If SPACE is null, it |
b75b98aa | 1069 | goes in the global namespace. */ |
1070 | static struct pragma_entry * | |
1071 | register_pragma_1 (cpp_reader *pfile, const char *space, const char *name, | |
1072 | bool allow_name_expansion) | |
3f075661 | 1073 | { |
c6f14ce5 | 1074 | struct pragma_entry **chain = &pfile->pragmas; |
1075 | struct pragma_entry *entry; | |
5c43d212 | 1076 | const cpp_hashnode *node; |
c6f14ce5 | 1077 | |
c6f14ce5 | 1078 | if (space) |
6cae2504 | 1079 | { |
5c43d212 | 1080 | node = cpp_lookup (pfile, U space, strlen (space)); |
1081 | entry = lookup_pragma_entry (*chain, node); | |
c6f14ce5 | 1082 | if (!entry) |
b75b98aa | 1083 | { |
1084 | entry = new_pragma_entry (pfile, chain); | |
1085 | entry->pragma = node; | |
1086 | entry->is_nspace = true; | |
1087 | entry->allow_expansion = allow_name_expansion; | |
1088 | } | |
c6f14ce5 | 1089 | else if (!entry->is_nspace) |
1090 | goto clash; | |
b75b98aa | 1091 | else if (entry->allow_expansion != allow_name_expansion) |
1092 | { | |
1093 | cpp_error (pfile, CPP_DL_ICE, | |
1094 | "registering pragmas in namespace \"%s\" with mismatched " | |
1095 | "name expansion", space); | |
1096 | return NULL; | |
1097 | } | |
c6f14ce5 | 1098 | chain = &entry->u.space; |
6cae2504 | 1099 | } |
b75b98aa | 1100 | else if (allow_name_expansion) |
1101 | { | |
1102 | cpp_error (pfile, CPP_DL_ICE, | |
1103 | "registering pragma \"%s\" with name expansion " | |
1104 | "and no namespace", name); | |
1105 | return NULL; | |
1106 | } | |
6cae2504 | 1107 | |
c6f14ce5 | 1108 | /* Check for duplicates. */ |
5c43d212 | 1109 | node = cpp_lookup (pfile, U name, strlen (name)); |
1110 | entry = lookup_pragma_entry (*chain, node); | |
b75b98aa | 1111 | if (entry == NULL) |
c6f14ce5 | 1112 | { |
b75b98aa | 1113 | entry = new_pragma_entry (pfile, chain); |
1114 | entry->pragma = node; | |
1115 | return entry; | |
c6f14ce5 | 1116 | } |
b75b98aa | 1117 | |
1118 | if (entry->is_nspace) | |
1119 | clash: | |
1120 | cpp_error (pfile, CPP_DL_ICE, | |
1121 | "registering \"%s\" as both a pragma and a pragma namespace", | |
1122 | NODE_NAME (node)); | |
1123 | else if (space) | |
1124 | cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered", | |
1125 | space, name); | |
c6f14ce5 | 1126 | else |
b75b98aa | 1127 | cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name); |
1128 | ||
1129 | return NULL; | |
1130 | } | |
1131 | ||
1132 | /* Register a cpplib internal pragma SPACE NAME with HANDLER. */ | |
1133 | static void | |
1134 | register_pragma_internal (cpp_reader *pfile, const char *space, | |
1135 | const char *name, pragma_cb handler) | |
1136 | { | |
1137 | struct pragma_entry *entry; | |
1138 | ||
1139 | entry = register_pragma_1 (pfile, space, name, false); | |
1140 | entry->is_internal = true; | |
1141 | entry->u.handler = handler; | |
d6d3c909 | 1142 | } |
1143 | ||
1144 | /* Register a pragma NAME in namespace SPACE. If SPACE is null, it | |
1145 | goes in the global namespace. HANDLER is the handler it will call, | |
68bf2ad9 | 1146 | which must be non-NULL. If ALLOW_EXPANSION is set, allow macro |
1147 | expansion while parsing pragma NAME. This function is exported | |
1148 | from libcpp. */ | |
d6d3c909 | 1149 | void |
1150 | cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name, | |
68bf2ad9 | 1151 | pragma_cb handler, bool allow_expansion) |
d6d3c909 | 1152 | { |
b75b98aa | 1153 | struct pragma_entry *entry; |
1154 | ||
1155 | if (!handler) | |
1156 | { | |
1157 | cpp_error (pfile, CPP_DL_ICE, "registering pragma with NULL handler"); | |
1158 | return; | |
1159 | } | |
1160 | ||
1161 | entry = register_pragma_1 (pfile, space, name, false); | |
1162 | if (entry) | |
1163 | { | |
1164 | entry->allow_expansion = allow_expansion; | |
1165 | entry->u.handler = handler; | |
1166 | } | |
6cae2504 | 1167 | } |
c6f14ce5 | 1168 | |
b75b98aa | 1169 | /* Similarly, but create mark the pragma for deferred processing. |
1170 | When found, a CPP_PRAGMA token will be insertted into the stream | |
1171 | with IDENT in the token->u.pragma slot. */ | |
1172 | void | |
1173 | cpp_register_deferred_pragma (cpp_reader *pfile, const char *space, | |
1174 | const char *name, unsigned int ident, | |
1175 | bool allow_expansion, bool allow_name_expansion) | |
1176 | { | |
1177 | struct pragma_entry *entry; | |
1178 | ||
1179 | entry = register_pragma_1 (pfile, space, name, allow_name_expansion); | |
1180 | if (entry) | |
1181 | { | |
1182 | entry->is_deferred = true; | |
1183 | entry->allow_expansion = allow_expansion; | |
1184 | entry->u.ident = ident; | |
1185 | } | |
1186 | } | |
1187 | ||
c6f14ce5 | 1188 | /* Register the pragmas the preprocessor itself handles. */ |
6cae2504 | 1189 | void |
f7fdd7a1 | 1190 | _cpp_init_internal_pragmas (cpp_reader *pfile) |
6cae2504 | 1191 | { |
c6f14ce5 | 1192 | /* Pragmas in the global namespace. */ |
b75b98aa | 1193 | register_pragma_internal (pfile, 0, "once", do_pragma_once); |
6cae2504 | 1194 | |
c6f14ce5 | 1195 | /* New GCC-specific pragmas should be put in the GCC namespace. */ |
b75b98aa | 1196 | register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison); |
1197 | register_pragma_internal (pfile, "GCC", "system_header", | |
1198 | do_pragma_system_header); | |
1199 | register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency); | |
3f075661 | 1200 | } |
1d6fa33f | 1201 | |
573aba85 | 1202 | /* Return the number of registered pragmas in PE. */ |
1203 | ||
1204 | static int | |
f7fdd7a1 | 1205 | count_registered_pragmas (struct pragma_entry *pe) |
573aba85 | 1206 | { |
1207 | int ct = 0; | |
1208 | for (; pe != NULL; pe = pe->next) | |
1209 | { | |
1210 | if (pe->is_nspace) | |
1211 | ct += count_registered_pragmas (pe->u.space); | |
1212 | ct++; | |
1213 | } | |
1214 | return ct; | |
1215 | } | |
1216 | ||
1217 | /* Save into SD the names of the registered pragmas referenced by PE, | |
1218 | and return a pointer to the next free space in SD. */ | |
1219 | ||
1220 | static char ** | |
f7fdd7a1 | 1221 | save_registered_pragmas (struct pragma_entry *pe, char **sd) |
573aba85 | 1222 | { |
1223 | for (; pe != NULL; pe = pe->next) | |
1224 | { | |
1225 | if (pe->is_nspace) | |
1226 | sd = save_registered_pragmas (pe->u.space, sd); | |
720aca92 | 1227 | *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident), |
1228 | HT_LEN (&pe->pragma->ident), | |
1229 | HT_LEN (&pe->pragma->ident) + 1); | |
573aba85 | 1230 | } |
1231 | return sd; | |
1232 | } | |
1233 | ||
1234 | /* Return a newly-allocated array which saves the names of the | |
1235 | registered pragmas. */ | |
1236 | ||
1237 | char ** | |
f7fdd7a1 | 1238 | _cpp_save_pragma_names (cpp_reader *pfile) |
573aba85 | 1239 | { |
1240 | int ct = count_registered_pragmas (pfile->pragmas); | |
3b298764 | 1241 | char **result = XNEWVEC (char *, ct); |
573aba85 | 1242 | (void) save_registered_pragmas (pfile->pragmas, result); |
1243 | return result; | |
1244 | } | |
1245 | ||
1246 | /* Restore from SD the names of the registered pragmas referenced by PE, | |
1247 | and return a pointer to the next unused name in SD. */ | |
1248 | ||
1249 | static char ** | |
f7fdd7a1 | 1250 | restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe, |
1251 | char **sd) | |
573aba85 | 1252 | { |
1253 | for (; pe != NULL; pe = pe->next) | |
1254 | { | |
1255 | if (pe->is_nspace) | |
1256 | sd = restore_registered_pragmas (pfile, pe->u.space, sd); | |
1257 | pe->pragma = cpp_lookup (pfile, U *sd, strlen (*sd)); | |
1258 | free (*sd); | |
1259 | sd++; | |
1260 | } | |
1261 | return sd; | |
1262 | } | |
1263 | ||
1264 | /* Restore the names of the registered pragmas from SAVED. */ | |
1265 | ||
1266 | void | |
f7fdd7a1 | 1267 | _cpp_restore_pragma_names (cpp_reader *pfile, char **saved) |
573aba85 | 1268 | { |
1269 | (void) restore_registered_pragmas (pfile, pfile->pragmas, saved); | |
1270 | free (saved); | |
1271 | } | |
1272 | ||
c6f14ce5 | 1273 | /* Pragmata handling. We handle some, and pass the rest on to the |
1274 | front end. C99 defines three pragmas and says that no macro | |
1275 | expansion is to be performed on them; whether or not macro | |
1276 | expansion happens for other pragmas is implementation defined. | |
b75b98aa | 1277 | This implementation allows for a mix of both, since GCC did not |
1278 | traditionally macro expand its (few) pragmas, whereas OpenMP | |
1279 | specifies that macro expansion should happen. */ | |
69461e0d | 1280 | static void |
f7fdd7a1 | 1281 | do_pragma (cpp_reader *pfile) |
1d6fa33f | 1282 | { |
c6f14ce5 | 1283 | const struct pragma_entry *p = NULL; |
9b3d6630 | 1284 | const cpp_token *token, *pragma_token = pfile->cur_token; |
add258d7 | 1285 | cpp_token ns_token; |
c6f14ce5 | 1286 | unsigned int count = 1; |
2e398cc7 | 1287 | |
79bd622b | 1288 | pfile->state.prevent_expansion++; |
6cae2504 | 1289 | |
f9b5f742 | 1290 | token = cpp_get_token (pfile); |
add258d7 | 1291 | ns_token = *token; |
f9b5f742 | 1292 | if (token->type == CPP_NAME) |
c0b823af | 1293 | { |
5c43d212 | 1294 | p = lookup_pragma_entry (pfile->pragmas, token->val.node); |
c6f14ce5 | 1295 | if (p && p->is_nspace) |
6cae2504 | 1296 | { |
b75b98aa | 1297 | bool allow_name_expansion = p->allow_expansion; |
1298 | if (allow_name_expansion) | |
1299 | pfile->state.prevent_expansion--; | |
c6f14ce5 | 1300 | token = cpp_get_token (pfile); |
1301 | if (token->type == CPP_NAME) | |
5c43d212 | 1302 | p = lookup_pragma_entry (p->u.space, token->val.node); |
c6f14ce5 | 1303 | else |
1304 | p = NULL; | |
b75b98aa | 1305 | if (allow_name_expansion) |
1306 | pfile->state.prevent_expansion++; | |
1307 | count = 2; | |
6cae2504 | 1308 | } |
6cae2504 | 1309 | } |
f80e83a9 | 1310 | |
a095c1b5 | 1311 | if (p) |
9b3d6630 | 1312 | { |
b75b98aa | 1313 | if (p->is_deferred) |
1314 | { | |
1315 | pfile->directive_result.src_loc = pragma_token->src_loc; | |
1316 | pfile->directive_result.type = CPP_PRAGMA; | |
1317 | pfile->directive_result.flags = pragma_token->flags; | |
1318 | pfile->directive_result.val.pragma = p->u.ident; | |
1319 | pfile->state.in_deferred_pragma = true; | |
1320 | pfile->state.pragma_allow_expansion = p->allow_expansion; | |
1321 | if (!p->allow_expansion) | |
1322 | pfile->state.prevent_expansion++; | |
1323 | } | |
1324 | else | |
a095c1b5 | 1325 | { |
b75b98aa | 1326 | /* Since the handler below doesn't get the line number, that |
1327 | it might need for diagnostics, make sure it has the right | |
a095c1b5 | 1328 | numbers in place. */ |
1329 | if (pfile->cb.line_change) | |
1330 | (*pfile->cb.line_change) (pfile, pragma_token, false); | |
b75b98aa | 1331 | if (p->allow_expansion) |
68bf2ad9 | 1332 | pfile->state.prevent_expansion--; |
a095c1b5 | 1333 | (*p->u.handler) (pfile); |
b75b98aa | 1334 | if (p->allow_expansion) |
68bf2ad9 | 1335 | pfile->state.prevent_expansion++; |
a095c1b5 | 1336 | } |
d6d3c909 | 1337 | } |
38692459 | 1338 | else if (pfile->cb.def_pragma) |
fb5ab82c | 1339 | { |
add258d7 | 1340 | if (count == 1 || pfile->context->prev == NULL) |
1341 | _cpp_backup_tokens (pfile, count); | |
1342 | else | |
1343 | { | |
1344 | /* Invalid name comes from macro expansion, _cpp_backup_tokens | |
1345 | won't allow backing 2 tokens. */ | |
1346 | /* ??? The token buffer is leaked. Perhaps if def_pragma hook | |
1347 | reads both tokens, we could perhaps free it, but if it doesn't, | |
1348 | we don't know the exact lifespan. */ | |
1349 | cpp_token *toks = XNEWVEC (cpp_token, 2); | |
1350 | toks[0] = ns_token; | |
1351 | toks[0].flags |= NO_EXPAND; | |
1352 | toks[1] = *token; | |
1353 | toks[1].flags |= NO_EXPAND; | |
1354 | _cpp_push_token_context (pfile, NULL, toks, 2); | |
1355 | } | |
f7fdd7a1 | 1356 | pfile->cb.def_pragma (pfile, pfile->directive_line); |
fb5ab82c | 1357 | } |
c6f14ce5 | 1358 | |
5621a364 | 1359 | pfile->state.prevent_expansion--; |
3f075661 | 1360 | } |
1361 | ||
e484a1cc | 1362 | /* Handle #pragma once. */ |
6cae2504 | 1363 | static void |
f7fdd7a1 | 1364 | do_pragma_once (cpp_reader *pfile) |
4e29fcb2 | 1365 | { |
927b511f | 1366 | if (cpp_in_primary_file (pfile)) |
d80d2074 | 1367 | cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file"); |
79bd622b | 1368 | |
1369 | check_eol (pfile); | |
68faebf4 | 1370 | _cpp_mark_file_once_only (pfile, pfile->buffer->file); |
4e29fcb2 | 1371 | } |
c9d838e1 | 1372 | |
8e1db61c | 1373 | /* Handle #pragma GCC poison, to poison one or more identifiers so |
1374 | that the lexer produces a hard error for each subsequent usage. */ | |
6cae2504 | 1375 | static void |
f7fdd7a1 | 1376 | do_pragma_poison (cpp_reader *pfile) |
4e29fcb2 | 1377 | { |
c00e481c | 1378 | const cpp_token *tok; |
c4abf88d | 1379 | cpp_hashnode *hp; |
4e29fcb2 | 1380 | |
79bd622b | 1381 | pfile->state.poisoned_ok = 1; |
4e29fcb2 | 1382 | for (;;) |
1383 | { | |
c00e481c | 1384 | tok = _cpp_lex_token (pfile); |
1385 | if (tok->type == CPP_EOF) | |
4e29fcb2 | 1386 | break; |
c00e481c | 1387 | if (tok->type != CPP_NAME) |
c9d838e1 | 1388 | { |
d80d2074 | 1389 | cpp_error (pfile, CPP_DL_ERROR, |
1390 | "invalid #pragma GCC poison directive"); | |
79bd622b | 1391 | break; |
c9d838e1 | 1392 | } |
1393 | ||
c00e481c | 1394 | hp = tok->val.node; |
79bd622b | 1395 | if (hp->flags & NODE_POISONED) |
1396 | continue; | |
1397 | ||
1398 | if (hp->type == NT_MACRO) | |
d80d2074 | 1399 | cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"", |
73328dce | 1400 | NODE_NAME (hp)); |
79bd622b | 1401 | _cpp_free_definition (hp); |
1402 | hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC; | |
c9d838e1 | 1403 | } |
79bd622b | 1404 | pfile->state.poisoned_ok = 0; |
1d6fa33f | 1405 | } |
e35f919d | 1406 | |
1407 | /* Mark the current header as a system header. This will suppress | |
1408 | some categories of warnings (notably those from -pedantic). It is | |
1409 | intended for use in system libraries that cannot be implemented in | |
1410 | conforming C, but cannot be certain that their headers appear in a | |
1411 | system include directory. To prevent abuse, it is rejected in the | |
1412 | primary source file. */ | |
6cae2504 | 1413 | static void |
f7fdd7a1 | 1414 | do_pragma_system_header (cpp_reader *pfile) |
e35f919d | 1415 | { |
927b511f | 1416 | if (cpp_in_primary_file (pfile)) |
d80d2074 | 1417 | cpp_error (pfile, CPP_DL_WARNING, |
73328dce | 1418 | "#pragma system_header ignored outside include file"); |
e35f919d | 1419 | else |
38692459 | 1420 | { |
1421 | check_eol (pfile); | |
fb5ab82c | 1422 | skip_rest_of_line (pfile); |
38692459 | 1423 | cpp_make_system_header (pfile, 1, 0); |
1424 | } | |
e35f919d | 1425 | } |
e485814b | 1426 | |
1427 | /* Check the modified date of the current include file against a specified | |
1428 | file. Issue a diagnostic, if the specified file is newer. We use this to | |
1429 | determine if a fixed header should be refixed. */ | |
6cae2504 | 1430 | static void |
f7fdd7a1 | 1431 | do_pragma_dependency (cpp_reader *pfile) |
e485814b | 1432 | { |
58efbd5a | 1433 | const char *fname; |
1434 | int angle_brackets, ordering; | |
b1a9ff83 | 1435 | |
f789fe3e | 1436 | fname = parse_include (pfile, &angle_brackets, NULL); |
58efbd5a | 1437 | if (!fname) |
6cae2504 | 1438 | return; |
f80e83a9 | 1439 | |
58efbd5a | 1440 | ordering = _cpp_compare_file_date (pfile, fname, angle_brackets); |
e485814b | 1441 | if (ordering < 0) |
d80d2074 | 1442 | cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname); |
e485814b | 1443 | else if (ordering > 0) |
1444 | { | |
d80d2074 | 1445 | cpp_error (pfile, CPP_DL_WARNING, |
1446 | "current file is older than %s", fname); | |
f9b5f742 | 1447 | if (cpp_get_token (pfile)->type != CPP_EOF) |
fb5ab82c | 1448 | { |
1449 | _cpp_backup_tokens (pfile, 1); | |
d80d2074 | 1450 | do_diagnostic (pfile, CPP_DL_WARNING, 0); |
fb5ab82c | 1451 | } |
e485814b | 1452 | } |
58efbd5a | 1453 | |
b9a7cc69 | 1454 | free ((void *) fname); |
e485814b | 1455 | } |
1456 | ||
f9b5f742 | 1457 | /* Get a token but skip padding. */ |
1458 | static const cpp_token * | |
f7fdd7a1 | 1459 | get_token_no_padding (cpp_reader *pfile) |
396ffa86 | 1460 | { |
f9b5f742 | 1461 | for (;;) |
1462 | { | |
1463 | const cpp_token *result = cpp_get_token (pfile); | |
1464 | if (result->type != CPP_PADDING) | |
1465 | return result; | |
1466 | } | |
1467 | } | |
396ffa86 | 1468 | |
f9b5f742 | 1469 | /* Check syntax is "(string-literal)". Returns the string on success, |
1470 | or NULL on failure. */ | |
1471 | static const cpp_token * | |
f7fdd7a1 | 1472 | get__Pragma_string (cpp_reader *pfile) |
f9b5f742 | 1473 | { |
1474 | const cpp_token *string; | |
2d507e67 | 1475 | const cpp_token *paren; |
396ffa86 | 1476 | |
2d507e67 | 1477 | paren = get_token_no_padding (pfile); |
1478 | if (paren->type == CPP_EOF) | |
1479 | _cpp_backup_tokens (pfile, 1); | |
1480 | if (paren->type != CPP_OPEN_PAREN) | |
f9b5f742 | 1481 | return NULL; |
1482 | ||
1483 | string = get_token_no_padding (pfile); | |
2d507e67 | 1484 | if (string->type == CPP_EOF) |
1485 | _cpp_backup_tokens (pfile, 1); | |
396ffa86 | 1486 | if (string->type != CPP_STRING && string->type != CPP_WSTRING) |
f9b5f742 | 1487 | return NULL; |
1488 | ||
2d507e67 | 1489 | paren = get_token_no_padding (pfile); |
1490 | if (paren->type == CPP_EOF) | |
1491 | _cpp_backup_tokens (pfile, 1); | |
1492 | if (paren->type != CPP_CLOSE_PAREN) | |
f9b5f742 | 1493 | return NULL; |
396ffa86 | 1494 | |
f9b5f742 | 1495 | return string; |
396ffa86 | 1496 | } |
1497 | ||
1e0ef2fd | 1498 | /* Destringize IN into a temporary buffer, by removing the first \ of |
1499 | \" and \\ sequences, and process the result as a #pragma directive. */ | |
1500 | static void | |
f7fdd7a1 | 1501 | destringize_and_run (cpp_reader *pfile, const cpp_string *in) |
396ffa86 | 1502 | { |
1503 | const unsigned char *src, *limit; | |
1e0ef2fd | 1504 | char *dest, *result; |
b75b98aa | 1505 | cpp_context *saved_context; |
1506 | cpp_token *saved_cur_token; | |
1507 | tokenrun *saved_cur_run; | |
1508 | cpp_token *toks; | |
1509 | int count; | |
8302ff7f | 1510 | const struct directive *save_directive; |
396ffa86 | 1511 | |
720aca92 | 1512 | dest = result = (char *) alloca (in->len - 1); |
4970d4c2 | 1513 | src = in->text + 1 + (in->text[0] == 'L'); |
1514 | limit = in->text + in->len - 1; | |
1515 | while (src < limit) | |
396ffa86 | 1516 | { |
1517 | /* We know there is a character following the backslash. */ | |
1518 | if (*src == '\\' && (src[1] == '\\' || src[1] == '"')) | |
1519 | src++; | |
1520 | *dest++ = *src++; | |
1521 | } | |
a54e0bf8 | 1522 | *dest = '\n'; |
396ffa86 | 1523 | |
6e5ef531 | 1524 | /* Ugh; an awful kludge. We are really not set up to be lexing |
1525 | tokens when in the middle of a macro expansion. Use a new | |
1526 | context to force cpp_get_token to lex, and so skip_rest_of_line | |
1527 | doesn't go beyond the end of the text. Also, remember the | |
1528 | current lexing position so we can return to it later. | |
1529 | ||
1530 | Something like line-at-a-time lexing should remove the need for | |
1531 | this. */ | |
b75b98aa | 1532 | saved_context = pfile->context; |
1533 | saved_cur_token = pfile->cur_token; | |
1534 | saved_cur_run = pfile->cur_run; | |
878d9b47 | 1535 | |
b75b98aa | 1536 | pfile->context = XNEW (cpp_context); |
1537 | pfile->context->macro = 0; | |
1538 | pfile->context->prev = 0; | |
add258d7 | 1539 | pfile->context->next = 0; |
878d9b47 | 1540 | |
b75b98aa | 1541 | /* Inline run_directive, since we need to delay the _cpp_pop_buffer |
1542 | until we've read all of the tokens that we want. */ | |
1543 | cpp_push_buffer (pfile, (const uchar *) result, dest - result, | |
1544 | /* from_stage3 */ true); | |
1545 | /* ??? Antique Disgusting Hack. What does this do? */ | |
1546 | if (pfile->buffer->prev) | |
1547 | pfile->buffer->file = pfile->buffer->prev->file; | |
878d9b47 | 1548 | |
b75b98aa | 1549 | start_directive (pfile); |
1550 | _cpp_clean_line (pfile); | |
8302ff7f | 1551 | save_directive = pfile->directive; |
1552 | pfile->directive = &dtable[T_PRAGMA]; | |
b75b98aa | 1553 | do_pragma (pfile); |
1554 | end_directive (pfile, 1); | |
8302ff7f | 1555 | pfile->directive = save_directive; |
878d9b47 | 1556 | |
b75b98aa | 1557 | /* We always insert at least one token, the directive result. It'll |
1558 | either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we | |
1559 | need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */ | |
1560 | ||
1561 | /* If we're not handling the pragma internally, read all of the tokens from | |
1562 | the string buffer now, while the string buffer is still installed. */ | |
1563 | /* ??? Note that the token buffer allocated here is leaked. It's not clear | |
1564 | to me what the true lifespan of the tokens are. It would appear that | |
1565 | the lifespan is the entire parse of the main input stream, in which case | |
1566 | this may not be wrong. */ | |
1567 | if (pfile->directive_result.type == CPP_PRAGMA) | |
1568 | { | |
1569 | int maxcount; | |
1570 | ||
1571 | count = 1; | |
1572 | maxcount = 50; | |
1573 | toks = XNEWVEC (cpp_token, maxcount); | |
1574 | toks[0] = pfile->directive_result; | |
878d9b47 | 1575 | |
b75b98aa | 1576 | do |
1577 | { | |
1578 | if (count == maxcount) | |
1579 | { | |
1580 | maxcount = maxcount * 3 / 2; | |
1581 | toks = XRESIZEVEC (cpp_token, toks, maxcount); | |
1582 | } | |
add258d7 | 1583 | toks[count] = *cpp_get_token (pfile); |
1584 | /* Macros have been already expanded by cpp_get_token | |
1585 | if the pragma allowed expansion. */ | |
1586 | toks[count++].flags |= NO_EXPAND; | |
b75b98aa | 1587 | } |
1588 | while (toks[count-1].type != CPP_PRAGMA_EOL); | |
1589 | } | |
1590 | else | |
1591 | { | |
1592 | count = 1; | |
1593 | toks = XNEW (cpp_token); | |
1594 | toks[0] = pfile->directive_result; | |
1595 | ||
1596 | /* If we handled the entire pragma internally, make sure we get the | |
1597 | line number correct for the next token. */ | |
1598 | if (pfile->cb.line_change) | |
1599 | pfile->cb.line_change (pfile, pfile->cur_token, false); | |
1600 | } | |
1601 | ||
1602 | /* Finish inlining run_directive. */ | |
1603 | pfile->buffer->file = NULL; | |
1604 | _cpp_pop_buffer (pfile); | |
1605 | ||
1606 | /* Reset the old macro state before ... */ | |
1607 | XDELETE (pfile->context); | |
1608 | pfile->context = saved_context; | |
1609 | pfile->cur_token = saved_cur_token; | |
1610 | pfile->cur_run = saved_cur_run; | |
1611 | ||
1612 | /* ... inserting the new tokens we collected. */ | |
1613 | _cpp_push_token_context (pfile, NULL, toks, count); | |
396ffa86 | 1614 | } |
1615 | ||
2d507e67 | 1616 | /* Handle the _Pragma operator. Return 0 on error, 1 if ok. */ |
1617 | int | |
f7fdd7a1 | 1618 | _cpp_do__Pragma (cpp_reader *pfile) |
396ffa86 | 1619 | { |
f9b5f742 | 1620 | const cpp_token *string = get__Pragma_string (pfile); |
d6d3c909 | 1621 | pfile->directive_result.type = CPP_PADDING; |
396ffa86 | 1622 | |
878d9b47 | 1623 | if (string) |
2d507e67 | 1624 | { |
1625 | destringize_and_run (pfile, &string->val.str); | |
1626 | return 1; | |
1627 | } | |
1628 | cpp_error (pfile, CPP_DL_ERROR, | |
1629 | "_Pragma takes a parenthesized string literal"); | |
1630 | return 0; | |
396ffa86 | 1631 | } |
d6d3c909 | 1632 | |
e484a1cc | 1633 | /* Handle #ifdef. */ |
69461e0d | 1634 | static void |
f7fdd7a1 | 1635 | do_ifdef (cpp_reader *pfile) |
4bf559f2 | 1636 | { |
79bd622b | 1637 | int skip = 1; |
f80e83a9 | 1638 | |
5e878517 | 1639 | if (! pfile->state.skipping) |
79bd622b | 1640 | { |
3e2a04b5 | 1641 | const cpp_hashnode *node = lex_macro_node (pfile, false); |
f80e83a9 | 1642 | |
79bd622b | 1643 | if (node) |
71a7c282 | 1644 | { |
1645 | skip = node->type != NT_MACRO; | |
1646 | _cpp_mark_macro_used (node); | |
1647 | check_eol (pfile); | |
1648 | } | |
79bd622b | 1649 | } |
4bf559f2 | 1650 | |
79bd622b | 1651 | push_conditional (pfile, skip, T_IFDEF, 0); |
1652 | } | |
4bf559f2 | 1653 | |
e484a1cc | 1654 | /* Handle #ifndef. */ |
69461e0d | 1655 | static void |
f7fdd7a1 | 1656 | do_ifndef (cpp_reader *pfile) |
4bf559f2 | 1657 | { |
79bd622b | 1658 | int skip = 1; |
76faa4c0 | 1659 | const cpp_hashnode *node = 0; |
4bf559f2 | 1660 | |
5e878517 | 1661 | if (! pfile->state.skipping) |
b970ec42 | 1662 | { |
3e2a04b5 | 1663 | node = lex_macro_node (pfile, false); |
f99f369d | 1664 | |
1665 | if (node) | |
71a7c282 | 1666 | { |
1667 | skip = node->type == NT_MACRO; | |
1668 | _cpp_mark_macro_used (node); | |
1669 | check_eol (pfile); | |
1670 | } | |
b970ec42 | 1671 | } |
f80e83a9 | 1672 | |
79bd622b | 1673 | push_conditional (pfile, skip, T_IFNDEF, node); |
1d6fa33f | 1674 | } |
1675 | ||
fa233610 | 1676 | /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in |
1677 | pfile->mi_ind_cmacro so we can handle multiple-include | |
7ef5b942 | 1678 | optimizations. If macro expansion occurs in the expression, we |
fa233610 | 1679 | cannot treat it as a controlling conditional, since the expansion |
1680 | could change in the future. That is handled by cpp_get_token. */ | |
69461e0d | 1681 | static void |
f7fdd7a1 | 1682 | do_if (cpp_reader *pfile) |
1d6fa33f | 1683 | { |
79bd622b | 1684 | int skip = 1; |
1d6fa33f | 1685 | |
5e878517 | 1686 | if (! pfile->state.skipping) |
5bbf045f | 1687 | skip = _cpp_parse_expr (pfile) == false; |
79bd622b | 1688 | |
fa233610 | 1689 | push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro); |
1d6fa33f | 1690 | } |
1691 | ||
920b5d41 | 1692 | /* Flip skipping state if appropriate and continue without changing |
c4357c92 | 1693 | if_stack; this is so that the error message for missing #endif's |
1694 | etc. will point to the original #if. */ | |
69461e0d | 1695 | static void |
f7fdd7a1 | 1696 | do_else (cpp_reader *pfile) |
8e33b875 | 1697 | { |
920b5d41 | 1698 | cpp_buffer *buffer = pfile->buffer; |
1699 | struct if_stack *ifs = buffer->if_stack; | |
c030ed73 | 1700 | |
c4357c92 | 1701 | if (ifs == NULL) |
d80d2074 | 1702 | cpp_error (pfile, CPP_DL_ERROR, "#else without #if"); |
79bd622b | 1703 | else |
c030ed73 | 1704 | { |
79bd622b | 1705 | if (ifs->type == T_ELSE) |
1706 | { | |
d80d2074 | 1707 | cpp_error (pfile, CPP_DL_ERROR, "#else after #else"); |
1708 | cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, | |
79bd622b | 1709 | "the conditional began here"); |
1710 | } | |
920b5d41 | 1711 | ifs->type = T_ELSE; |
1712 | ||
5e878517 | 1713 | /* Skip any future (erroneous) #elses or #elifs. */ |
1714 | pfile->state.skipping = ifs->skip_elses; | |
1715 | ifs->skip_elses = true; | |
1d6fa33f | 1716 | |
79bd622b | 1717 | /* Invalidate any controlling macro. */ |
1718 | ifs->mi_cmacro = 0; | |
79bd622b | 1719 | |
5e878517 | 1720 | /* Only check EOL if was not originally skipping. */ |
923acdd5 | 1721 | if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels)) |
5e878517 | 1722 | check_eol (pfile); |
1723 | } | |
1d6fa33f | 1724 | } |
1725 | ||
e484a1cc | 1726 | /* Handle a #elif directive by not changing if_stack either. See the |
79bd622b | 1727 | comment above do_else. */ |
69461e0d | 1728 | static void |
f7fdd7a1 | 1729 | do_elif (cpp_reader *pfile) |
1d6fa33f | 1730 | { |
920b5d41 | 1731 | cpp_buffer *buffer = pfile->buffer; |
1732 | struct if_stack *ifs = buffer->if_stack; | |
1d6fa33f | 1733 | |
c4357c92 | 1734 | if (ifs == NULL) |
d80d2074 | 1735 | cpp_error (pfile, CPP_DL_ERROR, "#elif without #if"); |
920b5d41 | 1736 | else |
0b60628c | 1737 | { |
920b5d41 | 1738 | if (ifs->type == T_ELSE) |
1739 | { | |
d80d2074 | 1740 | cpp_error (pfile, CPP_DL_ERROR, "#elif after #else"); |
1741 | cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, | |
920b5d41 | 1742 | "the conditional began here"); |
1743 | } | |
1744 | ifs->type = T_ELIF; | |
79bd622b | 1745 | |
5e878517 | 1746 | /* Only evaluate this if we aren't skipping elses. During |
1747 | evaluation, set skipping to false to get lexer warnings. */ | |
1748 | if (ifs->skip_elses) | |
1749 | pfile->state.skipping = 1; | |
1750 | else | |
920b5d41 | 1751 | { |
5e878517 | 1752 | pfile->state.skipping = 0; |
1753 | pfile->state.skipping = ! _cpp_parse_expr (pfile); | |
1754 | ifs->skip_elses = ! pfile->state.skipping; | |
920b5d41 | 1755 | } |
5e878517 | 1756 | |
1757 | /* Invalidate any controlling macro. */ | |
1758 | ifs->mi_cmacro = 0; | |
0b60628c | 1759 | } |
1d6fa33f | 1760 | } |
1761 | ||
5e878517 | 1762 | /* #endif pops the if stack and resets pfile->state.skipping. */ |
69461e0d | 1763 | static void |
f7fdd7a1 | 1764 | do_endif (cpp_reader *pfile) |
1d6fa33f | 1765 | { |
920b5d41 | 1766 | cpp_buffer *buffer = pfile->buffer; |
1767 | struct if_stack *ifs = buffer->if_stack; | |
c4357c92 | 1768 | |
c4357c92 | 1769 | if (ifs == NULL) |
d80d2074 | 1770 | cpp_error (pfile, CPP_DL_ERROR, "#endif without #if"); |
1d6fa33f | 1771 | else |
1772 | { | |
5e878517 | 1773 | /* Only check EOL if was not originally skipping. */ |
923acdd5 | 1774 | if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels)) |
5e878517 | 1775 | check_eol (pfile); |
1776 | ||
79bd622b | 1777 | /* If potential control macro, we go back outside again. */ |
1778 | if (ifs->next == 0 && ifs->mi_cmacro) | |
1779 | { | |
fa233610 | 1780 | pfile->mi_valid = true; |
79bd622b | 1781 | pfile->mi_cmacro = ifs->mi_cmacro; |
1782 | } | |
1783 | ||
920b5d41 | 1784 | buffer->if_stack = ifs->next; |
5e878517 | 1785 | pfile->state.skipping = ifs->was_skipping; |
0d086e18 | 1786 | obstack_free (&pfile->buffer_ob, ifs); |
1d6fa33f | 1787 | } |
79bd622b | 1788 | } |
f80e83a9 | 1789 | |
e484a1cc | 1790 | /* Push an if_stack entry for a preprocessor conditional, and set |
1791 | pfile->state.skipping to SKIP. If TYPE indicates the conditional | |
1792 | is #if or #ifndef, CMACRO is a potentially controlling macro, and | |
1793 | we need to check here that we are at the top of the file. */ | |
c4357c92 | 1794 | static void |
f7fdd7a1 | 1795 | push_conditional (cpp_reader *pfile, int skip, int type, |
1796 | const cpp_hashnode *cmacro) | |
c4357c92 | 1797 | { |
1798 | struct if_stack *ifs; | |
920b5d41 | 1799 | cpp_buffer *buffer = pfile->buffer; |
c4357c92 | 1800 | |
3b298764 | 1801 | ifs = XOBNEW (&pfile->buffer_ob, struct if_stack); |
729d2022 | 1802 | ifs->line = pfile->directive_line; |
920b5d41 | 1803 | ifs->next = buffer->if_stack; |
5e878517 | 1804 | ifs->skip_elses = pfile->state.skipping || !skip; |
1805 | ifs->was_skipping = pfile->state.skipping; | |
c4357c92 | 1806 | ifs->type = type; |
fa233610 | 1807 | /* This condition is effectively a test for top-of-file. */ |
1808 | if (pfile->mi_valid && pfile->mi_cmacro == 0) | |
79bd622b | 1809 | ifs->mi_cmacro = cmacro; |
1810 | else | |
1811 | ifs->mi_cmacro = 0; | |
c4357c92 | 1812 | |
5e878517 | 1813 | pfile->state.skipping = skip; |
920b5d41 | 1814 | buffer->if_stack = ifs; |
4896abfd | 1815 | } |
69bcbfc6 | 1816 | |
e484a1cc | 1817 | /* Read the tokens of the answer into the macro pool, in a directive |
1818 | of type TYPE. Only commit the memory if we intend it as permanent | |
1819 | storage, i.e. the #assert case. Returns 0 on success, and sets | |
1820 | ANSWERP to point to the answer. */ | |
79bd622b | 1821 | static int |
f7fdd7a1 | 1822 | parse_answer (cpp_reader *pfile, struct answer **answerp, int type) |
1d6fa33f | 1823 | { |
f9b5f742 | 1824 | const cpp_token *paren; |
79bd622b | 1825 | struct answer *answer; |
e6a5f963 | 1826 | unsigned int acount; |
79bd622b | 1827 | |
1828 | /* In a conditional, it is legal to not have an open paren. We | |
1829 | should save the following token in this case. */ | |
f9b5f742 | 1830 | paren = cpp_get_token (pfile); |
79bd622b | 1831 | |
1832 | /* If not a paren, see if we're OK. */ | |
f9b5f742 | 1833 | if (paren->type != CPP_OPEN_PAREN) |
f80e83a9 | 1834 | { |
79bd622b | 1835 | /* In a conditional no answer is a test for any answer. It |
1836 | could be followed by any token. */ | |
1837 | if (type == T_IF) | |
fb5ab82c | 1838 | { |
1839 | _cpp_backup_tokens (pfile, 1); | |
1840 | return 0; | |
1841 | } | |
79bd622b | 1842 | |
1843 | /* #unassert with no answer is valid - it removes all answers. */ | |
f9b5f742 | 1844 | if (type == T_UNASSERT && paren->type == CPP_EOF) |
79bd622b | 1845 | return 0; |
bce8e0c0 | 1846 | |
d80d2074 | 1847 | cpp_error (pfile, CPP_DL_ERROR, "missing '(' after predicate"); |
79bd622b | 1848 | return 1; |
f80e83a9 | 1849 | } |
69bcbfc6 | 1850 | |
e6a5f963 | 1851 | for (acount = 0;; acount++) |
f80e83a9 | 1852 | { |
e6a5f963 | 1853 | size_t room_needed; |
1854 | const cpp_token *token = cpp_get_token (pfile); | |
1855 | cpp_token *dest; | |
79bd622b | 1856 | |
f80e83a9 | 1857 | if (token->type == CPP_CLOSE_PAREN) |
1858 | break; | |
3ba8b497 | 1859 | |
79bd622b | 1860 | if (token->type == CPP_EOF) |
f80e83a9 | 1861 | { |
d80d2074 | 1862 | cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer"); |
79bd622b | 1863 | return 1; |
f80e83a9 | 1864 | } |
e6a5f963 | 1865 | |
1866 | /* struct answer includes the space for one token. */ | |
1867 | room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token)); | |
1868 | ||
1869 | if (BUFF_ROOM (pfile->a_buff) < room_needed) | |
1870 | _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer)); | |
1871 | ||
1872 | dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount]; | |
1873 | *dest = *token; | |
1874 | ||
1875 | /* Drop whitespace at start, for answer equivalence purposes. */ | |
1876 | if (acount == 0) | |
1877 | dest->flags &= ~PREV_WHITE; | |
f80e83a9 | 1878 | } |
bce8e0c0 | 1879 | |
e6a5f963 | 1880 | if (acount == 0) |
69bcbfc6 | 1881 | { |
d80d2074 | 1882 | cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty"); |
79bd622b | 1883 | return 1; |
1d6fa33f | 1884 | } |
f80e83a9 | 1885 | |
e6a5f963 | 1886 | answer = (struct answer *) BUFF_FRONT (pfile->a_buff); |
1887 | answer->count = acount; | |
1888 | answer->next = NULL; | |
79bd622b | 1889 | *answerp = answer; |
f80e83a9 | 1890 | |
79bd622b | 1891 | return 0; |
1892 | } | |
f80e83a9 | 1893 | |
e484a1cc | 1894 | /* Parses an assertion directive of type TYPE, returning a pointer to |
1895 | the hash node of the predicate, or 0 on error. If an answer was | |
1896 | supplied, it is placed in ANSWERP, otherwise it is set to 0. */ | |
79bd622b | 1897 | static cpp_hashnode * |
f7fdd7a1 | 1898 | parse_assertion (cpp_reader *pfile, struct answer **answerp, int type) |
79bd622b | 1899 | { |
1900 | cpp_hashnode *result = 0; | |
f9b5f742 | 1901 | const cpp_token *predicate; |
79bd622b | 1902 | |
1903 | /* We don't expand predicates or answers. */ | |
1904 | pfile->state.prevent_expansion++; | |
1905 | ||
79bd622b | 1906 | *answerp = 0; |
f9b5f742 | 1907 | predicate = cpp_get_token (pfile); |
1908 | if (predicate->type == CPP_EOF) | |
d80d2074 | 1909 | cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate"); |
f9b5f742 | 1910 | else if (predicate->type != CPP_NAME) |
d80d2074 | 1911 | cpp_error (pfile, CPP_DL_ERROR, "predicate must be an identifier"); |
79bd622b | 1912 | else if (parse_answer (pfile, answerp, type) == 0) |
1913 | { | |
f9b5f742 | 1914 | unsigned int len = NODE_LEN (predicate->val.node); |
720aca92 | 1915 | unsigned char *sym = (unsigned char *) alloca (len + 1); |
f80e83a9 | 1916 | |
79bd622b | 1917 | /* Prefix '#' to get it out of macro namespace. */ |
1918 | sym[0] = '#'; | |
f9b5f742 | 1919 | memcpy (sym + 1, NODE_NAME (predicate->val.node), len); |
79bd622b | 1920 | result = cpp_lookup (pfile, sym, len + 1); |
1921 | } | |
69bcbfc6 | 1922 | |
79bd622b | 1923 | pfile->state.prevent_expansion--; |
1924 | return result; | |
1d6fa33f | 1925 | } |
69bcbfc6 | 1926 | |
e484a1cc | 1927 | /* Returns a pointer to the pointer to CANDIDATE in the answer chain, |
f80e83a9 | 1928 | or a pointer to NULL if the answer is not in the chain. */ |
79bd622b | 1929 | static struct answer ** |
f7fdd7a1 | 1930 | find_answer (cpp_hashnode *node, const struct answer *candidate) |
1d6fa33f | 1931 | { |
79bd622b | 1932 | unsigned int i; |
f80e83a9 | 1933 | struct answer **result; |
1d6fa33f | 1934 | |
f80e83a9 | 1935 | for (result = &node->value.answers; *result; result = &(*result)->next) |
79bd622b | 1936 | { |
1937 | struct answer *answer = *result; | |
1938 | ||
1939 | if (answer->count == candidate->count) | |
1940 | { | |
1941 | for (i = 0; i < answer->count; i++) | |
1942 | if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i])) | |
1943 | break; | |
1944 | ||
1945 | if (i == answer->count) | |
1946 | break; | |
1947 | } | |
1948 | } | |
c030ed73 | 1949 | |
f80e83a9 | 1950 | return result; |
1951 | } | |
bce8e0c0 | 1952 | |
79bd622b | 1953 | /* Test an assertion within a preprocessor conditional. Returns |
d10cfa8d | 1954 | nonzero on failure, zero on success. On success, the result of |
9fd28e01 | 1955 | the test is written into VALUE, otherwise the value 0. */ |
79bd622b | 1956 | int |
f7fdd7a1 | 1957 | _cpp_test_assertion (cpp_reader *pfile, unsigned int *value) |
79bd622b | 1958 | { |
1959 | struct answer *answer; | |
1960 | cpp_hashnode *node; | |
1961 | ||
1962 | node = parse_assertion (pfile, &answer, T_IF); | |
9fd28e01 | 1963 | |
1964 | /* For recovery, an erroneous assertion expression is handled as a | |
1965 | failing assertion. */ | |
1966 | *value = 0; | |
1967 | ||
79bd622b | 1968 | if (node) |
1969 | *value = (node->type == NT_ASSERTION && | |
1970 | (answer == 0 || *find_answer (node, answer) != 0)); | |
b444d47a | 1971 | else if (pfile->cur_token[-1].type == CPP_EOF) |
1972 | _cpp_backup_tokens (pfile, 1); | |
79bd622b | 1973 | |
1974 | /* We don't commit the memory for the answer - it's temporary only. */ | |
1975 | return node == 0; | |
1976 | } | |
1977 | ||
e484a1cc | 1978 | /* Handle #assert. */ |
69461e0d | 1979 | static void |
f7fdd7a1 | 1980 | do_assert (cpp_reader *pfile) |
f80e83a9 | 1981 | { |
1982 | struct answer *new_answer; | |
1983 | cpp_hashnode *node; | |
b1a9ff83 | 1984 | |
79bd622b | 1985 | node = parse_assertion (pfile, &new_answer, T_ASSERT); |
f80e83a9 | 1986 | if (node) |
c030ed73 | 1987 | { |
c39ed964 | 1988 | size_t answer_size; |
1989 | ||
79bd622b | 1990 | /* Place the new answer in the answer list. First check there |
1991 | is not a duplicate. */ | |
f80e83a9 | 1992 | new_answer->next = 0; |
79bd622b | 1993 | if (node->type == NT_ASSERTION) |
f80e83a9 | 1994 | { |
79bd622b | 1995 | if (*find_answer (node, new_answer)) |
1996 | { | |
d80d2074 | 1997 | cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted", |
73328dce | 1998 | NODE_NAME (node) + 1); |
79bd622b | 1999 | return; |
2000 | } | |
f80e83a9 | 2001 | new_answer->next = node->value.answers; |
2002 | } | |
e6a5f963 | 2003 | |
c39ed964 | 2004 | answer_size = sizeof (struct answer) + ((new_answer->count - 1) |
2005 | * sizeof (cpp_token)); | |
2006 | /* Commit or allocate storage for the object. */ | |
2007 | if (pfile->hash_table->alloc_subobject) | |
2008 | { | |
2009 | struct answer *temp_answer = new_answer; | |
720aca92 | 2010 | new_answer = (struct answer *) pfile->hash_table->alloc_subobject |
2011 | (answer_size); | |
c39ed964 | 2012 | memcpy (new_answer, temp_answer, answer_size); |
2013 | } | |
2014 | else | |
2015 | BUFF_FRONT (pfile->a_buff) += answer_size; | |
2016 | ||
79bd622b | 2017 | node->type = NT_ASSERTION; |
f80e83a9 | 2018 | node->value.answers = new_answer; |
e6a5f963 | 2019 | check_eol (pfile); |
c030ed73 | 2020 | } |
f80e83a9 | 2021 | } |
bce8e0c0 | 2022 | |
e484a1cc | 2023 | /* Handle #unassert. */ |
69461e0d | 2024 | static void |
f7fdd7a1 | 2025 | do_unassert (cpp_reader *pfile) |
f80e83a9 | 2026 | { |
2027 | cpp_hashnode *node; | |
79bd622b | 2028 | struct answer *answer; |
b1a9ff83 | 2029 | |
79bd622b | 2030 | node = parse_assertion (pfile, &answer, T_UNASSERT); |
2031 | /* It isn't an error to #unassert something that isn't asserted. */ | |
2032 | if (node && node->type == NT_ASSERTION) | |
69bcbfc6 | 2033 | { |
79bd622b | 2034 | if (answer) |
bce8e0c0 | 2035 | { |
79bd622b | 2036 | struct answer **p = find_answer (node, answer), *temp; |
f80e83a9 | 2037 | |
79bd622b | 2038 | /* Remove the answer from the list. */ |
2039 | temp = *p; | |
2040 | if (temp) | |
2041 | *p = temp->next; | |
3ba8b497 | 2042 | |
79bd622b | 2043 | /* Did we free the last answer? */ |
2044 | if (node->value.answers == 0) | |
2045 | node->type = NT_VOID; | |
e6a5f963 | 2046 | |
2047 | check_eol (pfile); | |
79bd622b | 2048 | } |
2049 | else | |
2050 | _cpp_free_definition (node); | |
f80e83a9 | 2051 | } |
79bd622b | 2052 | |
2053 | /* We don't commit the memory for the answer - it's temporary only. */ | |
1d6fa33f | 2054 | } |
1d6fa33f | 2055 | |
0578f103 | 2056 | /* These are for -D, -U, -A. */ |
2057 | ||
2058 | /* Process the string STR as if it appeared as the body of a #define. | |
2059 | If STR is just an identifier, define it with value 1. | |
2060 | If STR has anything after the identifier, then it should | |
2c0e001b | 2061 | be identifier=definition. */ |
9fa36617 | 2062 | void |
f7fdd7a1 | 2063 | cpp_define (cpp_reader *pfile, const char *str) |
9fa36617 | 2064 | { |
0578f103 | 2065 | char *buf, *p; |
2066 | size_t count; | |
2067 | ||
b1a9ff83 | 2068 | /* Copy the entire option so we can modify it. |
0578f103 | 2069 | Change the first "=" in the string to a space. If there is none, |
90dc47e0 | 2070 | tack " 1" on the end. */ |
2071 | ||
90dc47e0 | 2072 | count = strlen (str); |
720aca92 | 2073 | buf = (char *) alloca (count + 3); |
90dc47e0 | 2074 | memcpy (buf, str, count); |
2075 | ||
2076 | p = strchr (str, '='); | |
0578f103 | 2077 | if (p) |
90dc47e0 | 2078 | buf[p - str] = ' '; |
0578f103 | 2079 | else |
2080 | { | |
90dc47e0 | 2081 | buf[count++] = ' '; |
2082 | buf[count++] = '1'; | |
9fa36617 | 2083 | } |
a54e0bf8 | 2084 | buf[count] = '\n'; |
eb3948d1 | 2085 | |
c808d026 | 2086 | run_directive (pfile, T_DEFINE, buf, count); |
a336277c | 2087 | } |
2088 | ||
f0c20935 | 2089 | /* Slight variant of the above for use by initialize_builtins. */ |
a336277c | 2090 | void |
f7fdd7a1 | 2091 | _cpp_define_builtin (cpp_reader *pfile, const char *str) |
a336277c | 2092 | { |
a54e0bf8 | 2093 | size_t len = strlen (str); |
720aca92 | 2094 | char *buf = (char *) alloca (len + 1); |
a54e0bf8 | 2095 | memcpy (buf, str, len); |
2096 | buf[len] = '\n'; | |
2097 | run_directive (pfile, T_DEFINE, buf, len); | |
0578f103 | 2098 | } |
a92771b8 | 2099 | |
0578f103 | 2100 | /* Process MACRO as if it appeared as the body of an #undef. */ |
2101 | void | |
f7fdd7a1 | 2102 | cpp_undef (cpp_reader *pfile, const char *macro) |
1d6fa33f | 2103 | { |
a54e0bf8 | 2104 | size_t len = strlen (macro); |
720aca92 | 2105 | char *buf = (char *) alloca (len + 1); |
a54e0bf8 | 2106 | memcpy (buf, macro, len); |
2107 | buf[len] = '\n'; | |
2108 | run_directive (pfile, T_UNDEF, buf, len); | |
1d6fa33f | 2109 | } |
2110 | ||
3fa3949d | 2111 | /* Like lex_macro_node, but read the input from STR. */ |
2112 | static cpp_hashnode * | |
2113 | lex_macro_node_from_str (cpp_reader *pfile, const char *str) | |
2114 | { | |
2115 | size_t len = strlen (str); | |
f23c2abe | 2116 | uchar *buf = (uchar *) alloca (len + 1); |
3fa3949d | 2117 | cpp_hashnode *node; |
2118 | ||
2119 | memcpy (buf, str, len); | |
2120 | buf[len] = '\n'; | |
2121 | cpp_push_buffer (pfile, buf, len, true); | |
2122 | node = lex_macro_node (pfile, true); | |
2123 | _cpp_pop_buffer (pfile); | |
2124 | ||
2125 | return node; | |
2126 | } | |
2127 | ||
2128 | /* If STR is a defined macro, return its definition node, else return NULL. */ | |
2129 | cpp_macro * | |
2130 | cpp_push_definition (cpp_reader *pfile, const char *str) | |
2131 | { | |
2132 | cpp_hashnode *node = lex_macro_node_from_str (pfile, str); | |
2133 | if (node && node->type == NT_MACRO) | |
2134 | return node->value.macro; | |
2135 | else | |
2136 | return NULL; | |
2137 | } | |
2138 | ||
2139 | /* Replace a previous definition DFN of the macro STR. If DFN is NULL, | |
2140 | then the macro should be undefined. */ | |
2141 | void | |
2142 | cpp_pop_definition (cpp_reader *pfile, const char *str, cpp_macro *dfn) | |
2143 | { | |
2144 | cpp_hashnode *node = lex_macro_node_from_str (pfile, str); | |
2145 | if (node == NULL) | |
2146 | return; | |
2147 | ||
2148 | if (node->type == NT_MACRO) | |
2149 | { | |
2150 | if (pfile->cb.undef) | |
2151 | pfile->cb.undef (pfile, pfile->directive_line, node); | |
2152 | if (CPP_OPTION (pfile, warn_unused_macros)) | |
2153 | _cpp_warn_if_unused_macro (pfile, node, NULL); | |
2154 | } | |
2155 | if (node->type != NT_VOID) | |
2156 | _cpp_free_definition (node); | |
2157 | ||
2158 | if (dfn) | |
2159 | { | |
2160 | node->type = NT_MACRO; | |
2161 | node->value.macro = dfn; | |
2162 | if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_"))) | |
2163 | node->flags |= NODE_WARN; | |
2164 | ||
2165 | if (pfile->cb.define) | |
2166 | pfile->cb.define (pfile, pfile->directive_line, node); | |
2167 | } | |
2168 | } | |
2169 | ||
2c0e001b | 2170 | /* Process the string STR as if it appeared as the body of a #assert. */ |
0578f103 | 2171 | void |
f7fdd7a1 | 2172 | cpp_assert (cpp_reader *pfile, const char *str) |
0578f103 | 2173 | { |
90dc47e0 | 2174 | handle_assertion (pfile, str, T_ASSERT); |
0578f103 | 2175 | } |
1d6fa33f | 2176 | |
2c0e001b | 2177 | /* Process STR as if it appeared as the body of an #unassert. */ |
0578f103 | 2178 | void |
f7fdd7a1 | 2179 | cpp_unassert (cpp_reader *pfile, const char *str) |
1d6fa33f | 2180 | { |
90dc47e0 | 2181 | handle_assertion (pfile, str, T_UNASSERT); |
b1a9ff83 | 2182 | } |
e3630c4b | 2183 | |
90dc47e0 | 2184 | /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */ |
2185 | static void | |
f7fdd7a1 | 2186 | handle_assertion (cpp_reader *pfile, const char *str, int type) |
90dc47e0 | 2187 | { |
2188 | size_t count = strlen (str); | |
2189 | const char *p = strchr (str, '='); | |
2190 | ||
a54e0bf8 | 2191 | /* Copy the entire option so we can modify it. Change the first |
2192 | "=" in the string to a '(', and tack a ')' on the end. */ | |
720aca92 | 2193 | char *buf = (char *) alloca (count + 2); |
a54e0bf8 | 2194 | |
2195 | memcpy (buf, str, count); | |
90dc47e0 | 2196 | if (p) |
2197 | { | |
90dc47e0 | 2198 | buf[p - str] = '('; |
2199 | buf[count++] = ')'; | |
90dc47e0 | 2200 | } |
a54e0bf8 | 2201 | buf[count] = '\n'; |
2202 | str = buf; | |
90dc47e0 | 2203 | |
c808d026 | 2204 | run_directive (pfile, type, str, count); |
90dc47e0 | 2205 | } |
2206 | ||
72b04ca4 | 2207 | /* The number of errors for a given reader. */ |
2208 | unsigned int | |
f7fdd7a1 | 2209 | cpp_errors (cpp_reader *pfile) |
72b04ca4 | 2210 | { |
2211 | return pfile->errors; | |
2212 | } | |
2213 | ||
2214 | /* The options structure. */ | |
2215 | cpp_options * | |
f7fdd7a1 | 2216 | cpp_get_options (cpp_reader *pfile) |
72b04ca4 | 2217 | { |
2218 | return &pfile->opts; | |
2219 | } | |
2220 | ||
2221 | /* The callbacks structure. */ | |
2222 | cpp_callbacks * | |
f7fdd7a1 | 2223 | cpp_get_callbacks (cpp_reader *pfile) |
72b04ca4 | 2224 | { |
2225 | return &pfile->cb; | |
2226 | } | |
2227 | ||
2228 | /* Copy the given callbacks structure to our own. */ | |
2229 | void | |
f7fdd7a1 | 2230 | cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb) |
72b04ca4 | 2231 | { |
2232 | pfile->cb = *cb; | |
2233 | } | |
2234 | ||
3eb3f293 | 2235 | /* The dependencies structure. (Creates one if it hasn't already been.) */ |
2236 | struct deps * | |
2237 | cpp_get_deps (cpp_reader *pfile) | |
2238 | { | |
2239 | if (!pfile->deps) | |
2240 | pfile->deps = deps_init (); | |
2241 | return pfile->deps; | |
2242 | } | |
2243 | ||
4087613d | 2244 | /* Push a new buffer on the buffer stack. Returns the new buffer; it |
2245 | doesn't fail. It does not generate a file change call back; that | |
2246 | is the responsibility of the caller. */ | |
f51c2148 | 2247 | cpp_buffer * |
f7fdd7a1 | 2248 | cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len, |
9eb74666 | 2249 | int from_stage3) |
f51c2148 | 2250 | { |
720aca92 | 2251 | cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer); |
79bd622b | 2252 | |
bd507c05 | 2253 | /* Clears, amongst other things, if_stack and mi_cmacro. */ |
720aca92 | 2254 | memset (new_buffer, 0, sizeof (cpp_buffer)); |
bd507c05 | 2255 | |
720aca92 | 2256 | new_buffer->next_line = new_buffer->buf = buffer; |
2257 | new_buffer->rlimit = buffer + len; | |
2258 | new_buffer->from_stage3 = from_stage3; | |
2259 | new_buffer->prev = pfile->buffer; | |
2260 | new_buffer->need_line = true; | |
6cee4464 | 2261 | |
720aca92 | 2262 | pfile->buffer = new_buffer; |
787c3d1a | 2263 | |
720aca92 | 2264 | return new_buffer; |
f51c2148 | 2265 | } |
2266 | ||
02516fb9 | 2267 | /* Pops a single buffer, with a file change call-back if appropriate. |
2268 | Then pushes the next -include file, if any remain. */ | |
4dfe8b74 | 2269 | void |
f7fdd7a1 | 2270 | _cpp_pop_buffer (cpp_reader *pfile) |
f51c2148 | 2271 | { |
bd507c05 | 2272 | cpp_buffer *buffer = pfile->buffer; |
1824e2bd | 2273 | struct _cpp_file *inc = buffer->file; |
f0c20935 | 2274 | struct if_stack *ifs; |
f51c2148 | 2275 | |
bd507c05 | 2276 | /* Walk back up the conditional stack till we reach its level at |
2277 | entry to this file, issuing error messages. */ | |
2278 | for (ifs = buffer->if_stack; ifs; ifs = ifs->next) | |
d80d2074 | 2279 | cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, |
bd507c05 | 2280 | "unterminated #%s", dtable[ifs->type].name); |
4087613d | 2281 | |
5621a364 | 2282 | /* In case of a missing #endif. */ |
1ea7ed21 | 2283 | pfile->state.skipping = 0; |
c808d026 | 2284 | |
02516fb9 | 2285 | /* _cpp_do_file_change expects pfile->buffer to be the new one. */ |
c808d026 | 2286 | pfile->buffer = buffer->prev; |
2287 | ||
a54e0bf8 | 2288 | free (buffer->notes); |
2289 | ||
02516fb9 | 2290 | /* Free the buffer object now; we may want to push a new buffer |
2291 | in _cpp_push_next_include_file. */ | |
2292 | obstack_free (&pfile->buffer_ob, buffer); | |
c808d026 | 2293 | |
02516fb9 | 2294 | if (inc) |
2295 | { | |
2296 | _cpp_pop_file_buffer (pfile, inc); | |
2297 | ||
9eb74666 | 2298 | _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0); |
02516fb9 | 2299 | } |
f51c2148 | 2300 | } |
2301 | ||
a2f10574 | 2302 | /* Enter all recognized directives in the hash table. */ |
f51c2148 | 2303 | void |
f7fdd7a1 | 2304 | _cpp_init_directives (cpp_reader *pfile) |
f51c2148 | 2305 | { |
87f5a8f0 | 2306 | unsigned int i; |
79bd622b | 2307 | cpp_hashnode *node; |
76faa4c0 | 2308 | |
ee6c4e4b | 2309 | for (i = 0; i < (unsigned int) N_DIRECTIVES; i++) |
79bd622b | 2310 | { |
87f5a8f0 | 2311 | node = cpp_lookup (pfile, dtable[i].name, dtable[i].length); |
805e22b2 | 2312 | node->is_directive = 1; |
2313 | node->directive_index = i; | |
79bd622b | 2314 | } |
f51c2148 | 2315 | } |