]>
Commit | Line | Data |
---|---|---|
e14c5993 | 1 | /* CPP Library. (Directive handling.) |
00059bc7 | 2 | Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, |
e484a1cc | 3 | 1999, 2000, 2001, 2002 Free Software Foundation, Inc. |
1f99c786 | 4 | Contributed by Per Bothner, 1994-95. |
0cb8d9ae | 5 | Based on CCCP program by Paul Rubin, June 1986 |
1d6fa33f | 6 | Adapted to ANSI C, Richard Stallman, Jan 1987 |
7 | ||
8 | This program is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by the | |
10 | Free Software Foundation; either version 2, or (at your option) any | |
11 | later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
ad87de1e | 20 | Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
1d6fa33f | 21 | |
ad87de1e | 22 | #include "config.h" |
1486870d | 23 | #include "system.h" |
1d6fa33f | 24 | |
ad87de1e | 25 | #include "cpplib.h" |
26 | #include "cpphash.h" | |
f51c2148 | 27 | #include "obstack.h" |
1d6fa33f | 28 | |
79bd622b | 29 | /* Chained list of answers to an assertion. */ |
30 | struct answer | |
31 | { | |
32 | struct answer *next; | |
33 | unsigned int count; | |
34 | cpp_token first[1]; | |
35 | }; | |
36 | ||
1e8b9746 | 37 | /* Stack of conditionals currently in progress |
38 | (including both successful and failing conditionals). */ | |
1e8b9746 | 39 | struct if_stack |
40 | { | |
41 | struct if_stack *next; | |
729d2022 | 42 | unsigned int line; /* Line where condition started. */ |
79bd622b | 43 | const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */ |
5e878517 | 44 | bool skip_elses; /* Can future #else / #elif be skipped? */ |
45 | bool was_skipping; /* If were skipping on entry. */ | |
46 | int type; /* Most recent conditional, for diagnostics. */ | |
1e8b9746 | 47 | }; |
1e8b9746 | 48 | |
c6f14ce5 | 49 | /* Contains a registered pragma or pragma namespace. */ |
50 | typedef void (*pragma_cb) PARAMS ((cpp_reader *)); | |
51 | struct pragma_entry | |
52 | { | |
53 | struct pragma_entry *next; | |
5c43d212 | 54 | const cpp_hashnode *pragma; /* Name and length. */ |
c6f14ce5 | 55 | int is_nspace; |
56 | union { | |
57 | pragma_cb handler; | |
58 | struct pragma_entry *space; | |
59 | } u; | |
60 | }; | |
61 | ||
79bd622b | 62 | /* Values for the origin field of struct directive. KANDR directives |
63 | come from traditional (K&R) C. STDC89 directives come from the | |
64 | 1989 C standard. EXTENSION directives are extensions. */ | |
65 | #define KANDR 0 | |
66 | #define STDC89 1 | |
67 | #define EXTENSION 2 | |
68 | ||
69 | /* Values for the flags field of struct directive. COND indicates a | |
70 | conditional; IF_COND an opening conditional. INCL means to treat | |
71 | "..." and <...> as q-char and h-char sequences respectively. IN_I | |
72 | means this directive should be handled even if -fpreprocessed is in | |
73 | effect (these are the directives with callback hooks). */ | |
74 | #define COND (1 << 0) | |
75 | #define IF_COND (1 << 1) | |
76 | #define INCL (1 << 2) | |
77 | #define IN_I (1 << 3) | |
78 | ||
79 | /* Defines one #-directive, including how to handle it. */ | |
80 | typedef void (*directive_handler) PARAMS ((cpp_reader *)); | |
81 | typedef struct directive directive; | |
82 | struct directive | |
83 | { | |
84 | directive_handler handler; /* Function to handle directive. */ | |
85 | const U_CHAR *name; /* Name of directive. */ | |
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 | ||
79bd622b | 93 | static void skip_rest_of_line PARAMS ((cpp_reader *)); |
94 | static void check_eol PARAMS ((cpp_reader *)); | |
9677c711 | 95 | static void start_directive PARAMS ((cpp_reader *)); |
96 | static void end_directive PARAMS ((cpp_reader *, int)); | |
d0d481d7 | 97 | static void directive_diagnostics |
98 | PARAMS ((cpp_reader *, const directive *, int)); | |
79bd622b | 99 | static void run_directive PARAMS ((cpp_reader *, int, |
6cee4464 | 100 | const char *, size_t)); |
f9b5f742 | 101 | static const cpp_token *glue_header_name PARAMS ((cpp_reader *)); |
102 | static const cpp_token *parse_include PARAMS ((cpp_reader *)); | |
f80e83a9 | 103 | static void push_conditional PARAMS ((cpp_reader *, int, int, |
104 | const cpp_hashnode *)); | |
53c052ca | 105 | static unsigned int read_flag PARAMS ((cpp_reader *, unsigned int)); |
25266990 | 106 | static U_CHAR *dequote_string PARAMS ((cpp_reader *, const U_CHAR *, |
107 | unsigned int)); | |
f51c2148 | 108 | static int strtoul_for_line PARAMS ((const U_CHAR *, unsigned int, |
f80e83a9 | 109 | unsigned long *)); |
def71b06 | 110 | static void do_diagnostic PARAMS ((cpp_reader *, enum error_type, int)); |
920b5d41 | 111 | static cpp_hashnode *lex_macro_node PARAMS ((cpp_reader *)); |
0d6d8dc0 | 112 | static void do_include_common PARAMS ((cpp_reader *, enum include_type)); |
c6f14ce5 | 113 | static struct pragma_entry *lookup_pragma_entry |
5c43d212 | 114 | PARAMS ((struct pragma_entry *, const cpp_hashnode *pragma)); |
c6f14ce5 | 115 | static struct pragma_entry *insert_pragma_entry |
5c43d212 | 116 | PARAMS ((cpp_reader *, struct pragma_entry **, const cpp_hashnode *, |
117 | pragma_cb)); | |
79bd622b | 118 | static void do_pragma_once PARAMS ((cpp_reader *)); |
119 | static void do_pragma_poison PARAMS ((cpp_reader *)); | |
120 | static void do_pragma_system_header PARAMS ((cpp_reader *)); | |
121 | static void do_pragma_dependency PARAMS ((cpp_reader *)); | |
25266990 | 122 | static void do_linemarker PARAMS ((cpp_reader *)); |
f9b5f742 | 123 | static const cpp_token *get_token_no_padding PARAMS ((cpp_reader *)); |
124 | static const cpp_token *get__Pragma_string PARAMS ((cpp_reader *)); | |
1e0ef2fd | 125 | static void destringize_and_run PARAMS ((cpp_reader *, const cpp_string *)); |
79bd622b | 126 | static int parse_answer PARAMS ((cpp_reader *, struct answer **, int)); |
127 | static cpp_hashnode *parse_assertion PARAMS ((cpp_reader *, struct answer **, | |
128 | int)); | |
129 | static struct answer ** find_answer PARAMS ((cpp_hashnode *, | |
130 | const struct answer *)); | |
90dc47e0 | 131 | static void handle_assertion PARAMS ((cpp_reader *, const char *, int)); |
a4b4a940 | 132 | |
4bf559f2 | 133 | /* This is the table of directive handlers. It is ordered by |
134 | frequency of occurrence; the numbers at the end are directive | |
135 | counts from all the source code I have lying around (egcs and libc | |
136 | CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and | |
79bd622b | 137 | pcmcia-cs-3.0.9). This is no longer important as directive lookup |
138 | is now O(1). All extensions other than #warning and #include_next | |
139 | are deprecated. The name is where the extension appears to have | |
140 | come from. */ | |
4bf559f2 | 141 | |
79bd622b | 142 | #define DIRECTIVE_TABLE \ |
143 | D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \ | |
144 | D(include, T_INCLUDE, KANDR, INCL) /* 52262 */ \ | |
145 | D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \ | |
146 | D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \ | |
147 | D(if, T_IF, KANDR, COND | IF_COND) /* 18162 */ \ | |
148 | D(else, T_ELSE, KANDR, COND) /* 9863 */ \ | |
149 | D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \ | |
150 | D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \ | |
25266990 | 151 | D(line, T_LINE, KANDR, 0) /* 2465 */ \ |
81ada207 | 152 | D(elif, T_ELIF, STDC89, COND) /* 610 */ \ |
79bd622b | 153 | D(error, T_ERROR, STDC89, 0) /* 475 */ \ |
154 | D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \ | |
155 | D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \ | |
156 | D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL) /* 19 */ \ | |
157 | D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \ | |
158 | D(import, T_IMPORT, EXTENSION, INCL) /* 0 ObjC */ \ | |
159 | D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \ | |
160 | D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \ | |
161 | SCCS_ENTRY /* 0 SVR4? */ | |
4bf559f2 | 162 | |
ef33b55c | 163 | /* #sccs is not always recognized. */ |
164 | #ifdef SCCS_DIRECTIVE | |
f80e83a9 | 165 | # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0) |
ef33b55c | 166 | #else |
167 | # define SCCS_ENTRY /* nothing */ | |
168 | #endif | |
169 | ||
4bf559f2 | 170 | /* Use the table to generate a series of prototypes, an enum for the |
171 | directive names, and an array of directive handlers. */ | |
172 | ||
2c0e001b | 173 | /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */ |
69461e0d | 174 | #define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *)); |
4bf559f2 | 175 | DIRECTIVE_TABLE |
176 | #undef D | |
177 | ||
f80e83a9 | 178 | #define D(n, tag, o, f) tag, |
4bf559f2 | 179 | enum |
180 | { | |
181 | DIRECTIVE_TABLE | |
182 | N_DIRECTIVES | |
1d6fa33f | 183 | }; |
4bf559f2 | 184 | #undef D |
185 | ||
2c0e001b | 186 | /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */ |
f80e83a9 | 187 | #define D(name, t, origin, flags) \ |
e057cf7c | 188 | { CONCAT2(do_,name), (const U_CHAR *) STRINGX(name), \ |
f80e83a9 | 189 | sizeof STRINGX(name) - 1, origin, flags }, |
79bd622b | 190 | static const directive dtable[] = |
4bf559f2 | 191 | { |
192 | DIRECTIVE_TABLE | |
193 | }; | |
194 | #undef D | |
195 | #undef DIRECTIVE_TABLE | |
1d6fa33f | 196 | |
25266990 | 197 | /* Wrapper struct directive for linemarkers. |
198 | The origin is more or less true - the original K+R cpp | |
199 | did use this notation in its preprocessed output. */ | |
200 | static const directive linemarker_dir = | |
201 | { | |
202 | do_linemarker, U"#", 1, KANDR, IN_I | |
203 | }; | |
204 | ||
fb5ab82c | 205 | #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF) |
1ea7ed21 | 206 | |
79bd622b | 207 | /* Skip any remaining tokens in a directive. */ |
208 | static void | |
209 | skip_rest_of_line (pfile) | |
f80e83a9 | 210 | cpp_reader *pfile; |
6060326b | 211 | { |
79bd622b | 212 | /* Discard all stacked contexts. */ |
213 | while (pfile->context != &pfile->base_context) | |
214 | _cpp_pop_context (pfile); | |
215 | ||
920b5d41 | 216 | /* Sweep up all tokens remaining on the line. */ |
c00e481c | 217 | if (! SEEN_EOL ()) |
218 | while (_cpp_lex_token (pfile)->type != CPP_EOF) | |
219 | ; | |
79bd622b | 220 | } |
6060326b | 221 | |
79bd622b | 222 | /* Ensure there are no stray tokens at the end of a directive. */ |
223 | static void | |
224 | check_eol (pfile) | |
225 | cpp_reader *pfile; | |
226 | { | |
c00e481c | 227 | if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF) |
228 | cpp_pedwarn (pfile, "extra tokens at end of #%s directive", | |
229 | pfile->directive->name); | |
79bd622b | 230 | } |
231 | ||
9677c711 | 232 | /* Called when entering a directive, _Pragma or command-line directive. */ |
233 | static void | |
234 | start_directive (pfile) | |
79bd622b | 235 | cpp_reader *pfile; |
79bd622b | 236 | { |
343fd982 | 237 | /* Setup in-directive state. */ |
238 | pfile->state.in_directive = 1; | |
239 | pfile->state.save_comments = 0; | |
240 | ||
79bd622b | 241 | /* Some handlers need the position of the # for diagnostics. */ |
f8d396cd | 242 | pfile->directive_line = pfile->line; |
9677c711 | 243 | } |
244 | ||
245 | /* Called when leaving a directive, _Pragma or command-line directive. */ | |
246 | static void | |
247 | end_directive (pfile, skip_line) | |
248 | cpp_reader *pfile; | |
249 | int skip_line; | |
250 | { | |
9677c711 | 251 | /* We don't skip for an assembler #. */ |
252 | if (skip_line) | |
1ea7ed21 | 253 | { |
254 | skip_rest_of_line (pfile); | |
fb5ab82c | 255 | if (!pfile->keep_tokens) |
256 | { | |
257 | pfile->cur_run = &pfile->base_run; | |
258 | pfile->cur_token = pfile->base_run.base; | |
259 | } | |
1ea7ed21 | 260 | } |
9677c711 | 261 | |
262 | /* Restore state. */ | |
9677c711 | 263 | pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments); |
264 | pfile->state.in_directive = 0; | |
265 | pfile->state.angled_headers = 0; | |
266 | pfile->directive = 0; | |
267 | } | |
268 | ||
d0d481d7 | 269 | /* Output diagnostics for a directive DIR. INDENTED is non-zero if |
270 | the '#' was indented. */ | |
d0d481d7 | 271 | static void |
272 | directive_diagnostics (pfile, dir, indented) | |
273 | cpp_reader *pfile; | |
274 | const directive *dir; | |
275 | int indented; | |
276 | { | |
25266990 | 277 | /* Issue -pedantic warnings for extensions. */ |
278 | if (CPP_PEDANTIC (pfile) | |
279 | && ! pfile->state.skipping | |
280 | && dir->origin == EXTENSION) | |
281 | cpp_pedwarn (pfile, "#%s is a GCC extension", dir->name); | |
282 | ||
283 | /* Traditionally, a directive is ignored unless its # is in | |
284 | column 1. Therefore in code intended to work with K+R | |
285 | compilers, directives added by C89 must have their # | |
286 | indented, and directives present in traditional C must not. | |
287 | This is true even of directives in skipped conditional | |
288 | blocks. #elif cannot be used at all. */ | |
289 | if (CPP_WTRADITIONAL (pfile)) | |
d0d481d7 | 290 | { |
25266990 | 291 | if (dir == &dtable[T_ELIF]) |
292 | cpp_warning (pfile, "suggest not using #elif in traditional C"); | |
293 | else if (indented && dir->origin == KANDR) | |
294 | cpp_warning (pfile, | |
295 | "traditional C ignores #%s with the # indented", | |
296 | dir->name); | |
297 | else if (!indented && dir->origin != KANDR) | |
298 | cpp_warning (pfile, | |
299 | "suggest hiding #%s from traditional C with an indented #", | |
300 | dir->name); | |
d0d481d7 | 301 | } |
302 | } | |
303 | ||
304 | /* Check if we have a known directive. INDENTED is non-zero if the | |
305 | '#' of the directive was indented. This function is in this file | |
306 | to save unnecessarily exporting dtable etc. to cpplex.c. Returns | |
307 | non-zero if the line of tokens has been handled, zero if we should | |
308 | continue processing the line. */ | |
9677c711 | 309 | int |
310 | _cpp_handle_directive (pfile, indented) | |
311 | cpp_reader *pfile; | |
312 | int indented; | |
313 | { | |
9677c711 | 314 | const directive *dir = 0; |
c00e481c | 315 | const cpp_token *dname; |
d6af0368 | 316 | bool was_parsing_args = pfile->state.parsing_args; |
9677c711 | 317 | int skip = 1; |
318 | ||
d6af0368 | 319 | if (was_parsing_args) |
320 | { | |
321 | if (CPP_OPTION (pfile, pedantic)) | |
322 | cpp_pedwarn (pfile, | |
323 | "embedding a directive within macro arguments is not portable"); | |
324 | pfile->state.parsing_args = 0; | |
325 | pfile->state.prevent_expansion = 0; | |
326 | } | |
9677c711 | 327 | start_directive (pfile); |
c00e481c | 328 | dname = _cpp_lex_token (pfile); |
338fa5f7 | 329 | |
c00e481c | 330 | if (dname->type == CPP_NAME) |
338fa5f7 | 331 | { |
c00e481c | 332 | if (dname->val.node->directive_index) |
333 | dir = &dtable[dname->val.node->directive_index - 1]; | |
338fa5f7 | 334 | } |
d0d481d7 | 335 | /* We do not recognise the # followed by a number extension in |
336 | assembler code. */ | |
c00e481c | 337 | else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM) |
338fa5f7 | 338 | { |
25266990 | 339 | dir = &linemarker_dir; |
340 | if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed) | |
341 | && ! pfile->state.skipping) | |
342 | cpp_pedwarn (pfile, "style of line directive is a GCC extension"); | |
79bd622b | 343 | } |
338fa5f7 | 344 | |
79bd622b | 345 | if (dir) |
346 | { | |
d0d481d7 | 347 | /* If we have a directive that is not an opening conditional, |
348 | invalidate any control macro. */ | |
349 | if (! (dir->flags & IF_COND)) | |
350 | pfile->mi_valid = false; | |
351 | ||
352 | /* Kluge alert. In order to be sure that code like this | |
353 | ||
354 | #define HASH # | |
355 | HASH define foo bar | |
356 | ||
357 | does not cause '#define foo bar' to get executed when | |
358 | compiled with -save-temps, we recognize directives in | |
359 | -fpreprocessed mode only if the # is in column 1. cppmacro.c | |
3fb1e43b | 360 | puts a space in front of any '#' at the start of a macro. */ |
d0d481d7 | 361 | if (CPP_OPTION (pfile, preprocessed) |
362 | && (indented || !(dir->flags & IN_I))) | |
946ce1b7 | 363 | { |
d0d481d7 | 364 | skip = 0; |
365 | dir = 0; | |
946ce1b7 | 366 | } |
367 | else | |
79bd622b | 368 | { |
d0d481d7 | 369 | /* In failed conditional groups, all non-conditional |
370 | directives are ignored. Before doing that, whether | |
371 | skipping or not, we should lex angle-bracketed headers | |
372 | correctly, and maybe output some diagnostics. */ | |
373 | pfile->state.angled_headers = dir->flags & INCL; | |
374 | if (! CPP_OPTION (pfile, preprocessed)) | |
375 | directive_diagnostics (pfile, dir, indented); | |
376 | if (pfile->state.skipping && !(dir->flags & COND)) | |
377 | dir = 0; | |
79bd622b | 378 | } |
379 | } | |
c00e481c | 380 | else if (dname->type == CPP_EOF) |
d0d481d7 | 381 | ; /* CPP_EOF is the "null directive". */ |
382 | else | |
79bd622b | 383 | { |
384 | /* An unknown directive. Don't complain about it in assembly | |
385 | source: we don't know where the comments are, and # may | |
386 | introduce assembler pseudo-ops. Don't complain about invalid | |
387 | directives in skipped conditional groups (6.10 p4). */ | |
5db5d057 | 388 | if (CPP_OPTION (pfile, lang) == CLK_ASM) |
d0d481d7 | 389 | skip = 0; |
390 | else if (!pfile->state.skipping) | |
79bd622b | 391 | cpp_error (pfile, "invalid preprocessing directive #%s", |
c00e481c | 392 | cpp_token_as_text (pfile, dname)); |
338fa5f7 | 393 | } |
394 | ||
d0d481d7 | 395 | if (dir) |
396 | { | |
d3f7919d | 397 | /* If we are processing a `#define' directive and we have been |
398 | requested to expand comments into macros, then re-enable | |
399 | saving of comments. */ | |
400 | if (dir == &dtable[T_DEFINE]) | |
401 | pfile->state.save_comments = | |
402 | ! CPP_OPTION (pfile, discard_comments_in_macro_exp); | |
403 | ||
d0d481d7 | 404 | pfile->directive = dir; |
405 | (*pfile->directive->handler) (pfile); | |
406 | } | |
407 | else if (skip == 0) | |
408 | _cpp_backup_tokens (pfile, 1); | |
409 | ||
410 | end_directive (pfile, skip); | |
d6af0368 | 411 | if (was_parsing_args) |
412 | { | |
413 | /* Restore state when within macro args. */ | |
414 | pfile->state.parsing_args = 2; | |
415 | pfile->state.prevent_expansion = 1; | |
416 | pfile->buffer->saved_flags |= PREV_WHITE; | |
417 | } | |
9677c711 | 418 | return skip; |
f80e83a9 | 419 | } |
1d6fa33f | 420 | |
79bd622b | 421 | /* Directive handler wrapper used by the command line option |
422 | processor. */ | |
423 | static void | |
c808d026 | 424 | run_directive (pfile, dir_no, buf, count) |
1d6fa33f | 425 | cpp_reader *pfile; |
79bd622b | 426 | int dir_no; |
427 | const char *buf; | |
428 | size_t count; | |
e3630c4b | 429 | { |
c808d026 | 430 | cpp_push_buffer (pfile, (const U_CHAR *) buf, count, |
431 | /* from_stage3 */ true, 1); | |
6cee4464 | 432 | start_directive (pfile); |
d0d481d7 | 433 | /* We don't want a leading # to be interpreted as a directive. */ |
434 | pfile->buffer->saved_flags = 0; | |
7b5cc295 | 435 | pfile->directive = &dtable[dir_no]; |
436 | (void) (*pfile->directive->handler) (pfile); | |
6cee4464 | 437 | end_directive (pfile, 1); |
4dfe8b74 | 438 | _cpp_pop_buffer (pfile); |
79bd622b | 439 | } |
440 | ||
441 | /* Checks for validity the macro name in #define, #undef, #ifdef and | |
442 | #ifndef directives. */ | |
f80e83a9 | 443 | static cpp_hashnode * |
79bd622b | 444 | lex_macro_node (pfile) |
f80e83a9 | 445 | cpp_reader *pfile; |
446 | { | |
f17c0828 | 447 | cpp_hashnode *node; |
c00e481c | 448 | const cpp_token *token = _cpp_lex_token (pfile); |
c4357c92 | 449 | |
31674461 | 450 | /* The token immediately after #define must be an identifier. That |
f17c0828 | 451 | identifier may not be "defined", per C99 6.10.8p4. |
452 | In C++, it may not be any of the "named operators" either, | |
453 | per C++98 [lex.digraph], [lex.key]. | |
454 | Finally, the identifier may not have been poisoned. (In that case | |
d3f7919d | 455 | the lexer has issued the error message for us.) |
456 | ||
457 | Note that if we're copying comments into macro expansions, we | |
458 | could encounter comment tokens here, so eat them all up first. */ | |
459 | ||
460 | if (! CPP_OPTION (pfile, discard_comments_in_macro_exp)) | |
461 | { | |
462 | while (token->type == CPP_COMMENT) | |
463 | token = _cpp_lex_token (pfile); | |
464 | } | |
31674461 | 465 | |
c00e481c | 466 | if (token->type != CPP_NAME) |
1d6fa33f | 467 | { |
c00e481c | 468 | if (token->type == CPP_EOF) |
79bd622b | 469 | cpp_error (pfile, "no macro name given in #%s directive", |
470 | pfile->directive->name); | |
c00e481c | 471 | else if (token->flags & NAMED_OP) |
79bd622b | 472 | cpp_error (pfile, |
f17c0828 | 473 | "\"%s\" cannot be used as a macro name as it is an operator in C++", |
c00e481c | 474 | NODE_NAME (token->val.node)); |
31674461 | 475 | else |
79bd622b | 476 | cpp_error (pfile, "macro names must be identifiers"); |
f17c0828 | 477 | |
478 | return 0; | |
71aa9da4 | 479 | } |
f17c0828 | 480 | |
c00e481c | 481 | node = token->val.node; |
f17c0828 | 482 | if (node->flags & NODE_POISONED) |
483 | return 0; | |
484 | ||
485 | if (node == pfile->spec_nodes.n_defined) | |
ef33b55c | 486 | { |
f17c0828 | 487 | cpp_error (pfile, "\"%s\" cannot be used as a macro name", |
488 | NODE_NAME (node)); | |
489 | return 0; | |
ef33b55c | 490 | } |
491 | ||
f17c0828 | 492 | return node; |
1d6fa33f | 493 | } |
1d6fa33f | 494 | |
79bd622b | 495 | /* Process a #define directive. Most work is done in cppmacro.c. */ |
69461e0d | 496 | static void |
4bf559f2 | 497 | do_define (pfile) |
1d6fa33f | 498 | cpp_reader *pfile; |
1d6fa33f | 499 | { |
79bd622b | 500 | cpp_hashnode *node = lex_macro_node (pfile); |
c030ed73 | 501 | |
79bd622b | 502 | if (node) |
503 | { | |
79bd622b | 504 | if (_cpp_create_definition (pfile, node)) |
505 | if (pfile->cb.define) | |
f8d396cd | 506 | (*pfile->cb.define) (pfile, pfile->directive_line, node); |
79bd622b | 507 | } |
f80e83a9 | 508 | } |
509 | ||
e484a1cc | 510 | /* Handle #undef. Mark the identifier NT_VOID in the hash table. */ |
69461e0d | 511 | static void |
f80e83a9 | 512 | do_undef (pfile) |
513 | cpp_reader *pfile; | |
514 | { | |
79bd622b | 515 | cpp_hashnode *node = lex_macro_node (pfile); |
2c63d6c8 | 516 | |
f80e83a9 | 517 | /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier |
518 | is not currently defined as a macro name. */ | |
79bd622b | 519 | if (node && node->type == NT_MACRO) |
2c63d6c8 | 520 | { |
6cae2504 | 521 | if (pfile->cb.undef) |
f8d396cd | 522 | (*pfile->cb.undef) (pfile, pfile->directive_line, node); |
2c63d6c8 | 523 | |
31ca26b1 | 524 | if (node->flags & NODE_WARN) |
c86dbc5b | 525 | cpp_warning (pfile, "undefining \"%s\"", NODE_NAME (node)); |
2c63d6c8 | 526 | |
f80e83a9 | 527 | _cpp_free_definition (node); |
2c63d6c8 | 528 | } |
79bd622b | 529 | check_eol (pfile); |
1d6fa33f | 530 | } |
531 | ||
79bd622b | 532 | /* Helper routine used by parse_include. Reinterpret the current line |
533 | as an h-char-sequence (< ... >); we are looking at the first token | |
f9b5f742 | 534 | after the <. Returns the header as a token, or NULL on failure. */ |
535 | static const cpp_token * | |
536 | glue_header_name (pfile) | |
79bd622b | 537 | cpp_reader *pfile; |
79bd622b | 538 | { |
f9b5f742 | 539 | cpp_token *header = NULL; |
540 | const cpp_token *token; | |
92b1c81a | 541 | unsigned char *buffer; |
542 | size_t len, total_len = 0, capacity = 1024; | |
79bd622b | 543 | |
544 | /* To avoid lexed tokens overwriting our glued name, we can only | |
545 | allocate from the string pool once we've lexed everything. */ | |
92b1c81a | 546 | buffer = (unsigned char *) xmalloc (capacity); |
79bd622b | 547 | for (;;) |
548 | { | |
f9b5f742 | 549 | token = cpp_get_token (pfile); |
79bd622b | 550 | |
f9b5f742 | 551 | if (token->type == CPP_GREATER || token->type == CPP_EOF) |
79bd622b | 552 | break; |
553 | ||
92b1c81a | 554 | len = cpp_token_len (token); |
555 | if (total_len + len > capacity) | |
79bd622b | 556 | { |
92b1c81a | 557 | capacity = (capacity + len) * 2; |
558 | buffer = (unsigned char *) xrealloc (buffer, capacity); | |
79bd622b | 559 | } |
560 | ||
f9b5f742 | 561 | if (token->flags & PREV_WHITE) |
92b1c81a | 562 | buffer[total_len++] = ' '; |
79bd622b | 563 | |
92b1c81a | 564 | total_len = cpp_spell_token (pfile, token, &buffer[total_len]) - buffer; |
79bd622b | 565 | } |
566 | ||
f9b5f742 | 567 | if (token->type == CPP_EOF) |
79bd622b | 568 | cpp_error (pfile, "missing terminating > character"); |
569 | else | |
570 | { | |
92b1c81a | 571 | unsigned char *token_mem = _cpp_unaligned_alloc (pfile, total_len + 1); |
572 | memcpy (token_mem, buffer, total_len); | |
573 | token_mem[total_len] = '\0'; | |
574 | ||
f9b5f742 | 575 | header = _cpp_temp_token (pfile); |
79bd622b | 576 | header->type = CPP_HEADER_NAME; |
1fdf6039 | 577 | header->flags = 0; |
92b1c81a | 578 | header->val.str.len = total_len; |
579 | header->val.str.text = token_mem; | |
79bd622b | 580 | } |
f80e83a9 | 581 | |
92b1c81a | 582 | free ((PTR) buffer); |
f9b5f742 | 583 | return header; |
79bd622b | 584 | } |
1d6fa33f | 585 | |
f9b5f742 | 586 | /* Returns the header string of #include, #include_next, #import and |
587 | #pragma dependency. Returns NULL on error. */ | |
588 | static const cpp_token * | |
589 | parse_include (pfile) | |
1d6fa33f | 590 | cpp_reader *pfile; |
591 | { | |
79bd622b | 592 | const unsigned char *dir; |
f9b5f742 | 593 | const cpp_token *header; |
1d6fa33f | 594 | |
968e3f9d | 595 | if (pfile->directive == &dtable[T_PRAGMA]) |
79bd622b | 596 | dir = U"pragma dependency"; |
597 | else | |
598 | dir = pfile->directive->name; | |
599 | ||
600 | /* Allow macro expansion. */ | |
f9b5f742 | 601 | header = cpp_get_token (pfile); |
79bd622b | 602 | if (header->type != CPP_STRING && header->type != CPP_HEADER_NAME) |
1d6fa33f | 603 | { |
79bd622b | 604 | if (header->type != CPP_LESS) |
f80e83a9 | 605 | { |
606 | cpp_error (pfile, "#%s expects \"FILENAME\" or <FILENAME>", dir); | |
f9b5f742 | 607 | return NULL; |
f80e83a9 | 608 | } |
f9b5f742 | 609 | |
610 | header = glue_header_name (pfile); | |
611 | if (header == NULL) | |
612 | return header; | |
1d6fa33f | 613 | } |
79bd622b | 614 | |
615 | if (header->val.str.len == 0) | |
1d6fa33f | 616 | { |
f80e83a9 | 617 | cpp_error (pfile, "empty file name in #%s", dir); |
f9b5f742 | 618 | return NULL; |
1d6fa33f | 619 | } |
1d6fa33f | 620 | |
f9b5f742 | 621 | return header; |
4bf559f2 | 622 | } |
d453a374 | 623 | |
0d6d8dc0 | 624 | /* Handle #include, #include_next and #import. */ |
69461e0d | 625 | static void |
0d6d8dc0 | 626 | do_include_common (pfile, type) |
4bf559f2 | 627 | cpp_reader *pfile; |
0d6d8dc0 | 628 | enum include_type type; |
4bf559f2 | 629 | { |
f9b5f742 | 630 | const cpp_token *header; |
1d6fa33f | 631 | |
968e3f9d | 632 | /* For #include_next, if this is the primary source file, warn and |
633 | use the normal search logic. */ | |
634 | if (type == IT_INCLUDE_NEXT && ! pfile->buffer->prev) | |
635 | { | |
636 | cpp_warning (pfile, "#include_next in primary source file"); | |
637 | type = IT_INCLUDE; | |
638 | } | |
639 | else if (type == IT_IMPORT && CPP_OPTION (pfile, warn_import)) | |
640 | { | |
641 | CPP_OPTION (pfile, warn_import) = 0; | |
642 | cpp_warning (pfile, | |
643 | "#import is obsolete, use an #ifndef wrapper in the header file"); | |
644 | } | |
645 | ||
f9b5f742 | 646 | header = parse_include (pfile); |
647 | if (header) | |
0d6d8dc0 | 648 | { |
649 | /* Prevent #include recursion. */ | |
4087823d | 650 | if (pfile->line_maps.depth >= CPP_STACK_MAX) |
0d6d8dc0 | 651 | cpp_fatal (pfile, "#include nested too deeply"); |
0d6d8dc0 | 652 | else |
653 | { | |
968e3f9d | 654 | check_eol (pfile); |
655 | /* Get out of macro context, if we are. */ | |
fb5ab82c | 656 | skip_rest_of_line (pfile); |
968e3f9d | 657 | if (pfile->cb.include) |
f8d396cd | 658 | (*pfile->cb.include) (pfile, pfile->directive_line, |
f9b5f742 | 659 | pfile->directive->name, header); |
0d6d8dc0 | 660 | |
f9b5f742 | 661 | _cpp_execute_include (pfile, header, type); |
0d6d8dc0 | 662 | } |
663 | } | |
4bf559f2 | 664 | } |
c4e0ec82 | 665 | |
69461e0d | 666 | static void |
0d6d8dc0 | 667 | do_include (pfile) |
4bf559f2 | 668 | cpp_reader *pfile; |
669 | { | |
0d6d8dc0 | 670 | do_include_common (pfile, IT_INCLUDE); |
671 | } | |
4bf559f2 | 672 | |
0d6d8dc0 | 673 | static void |
674 | do_import (pfile) | |
675 | cpp_reader *pfile; | |
676 | { | |
0d6d8dc0 | 677 | do_include_common (pfile, IT_IMPORT); |
4bf559f2 | 678 | } |
1d6fa33f | 679 | |
69461e0d | 680 | static void |
4bf559f2 | 681 | do_include_next (pfile) |
682 | cpp_reader *pfile; | |
683 | { | |
0d6d8dc0 | 684 | do_include_common (pfile, IT_INCLUDE_NEXT); |
1d6fa33f | 685 | } |
1d6fa33f | 686 | |
25266990 | 687 | /* Subroutine of do_linemarker. Read possible flags after file name. |
688 | LAST is the last flag seen; 0 if this is the first flag. Return the | |
689 | flag if it is valid, 0 at the end of the directive. Otherwise | |
690 | complain. */ | |
2fd3b10b | 691 | static unsigned int |
53c052ca | 692 | read_flag (pfile, last) |
314ab6d5 | 693 | cpp_reader *pfile; |
53c052ca | 694 | unsigned int last; |
314ab6d5 | 695 | { |
c00e481c | 696 | const cpp_token *token = _cpp_lex_token (pfile); |
314ab6d5 | 697 | |
c00e481c | 698 | if (token->type == CPP_NUMBER && token->val.str.len == 1) |
314ab6d5 | 699 | { |
c00e481c | 700 | unsigned int flag = token->val.str.text[0] - '0'; |
53c052ca | 701 | |
702 | if (flag > last && flag <= 4 | |
703 | && (flag != 4 || last == 3) | |
704 | && (flag != 2 || last == 0)) | |
705 | return flag; | |
314ab6d5 | 706 | } |
79bd622b | 707 | |
c00e481c | 708 | if (token->type != CPP_EOF) |
2fd3b10b | 709 | cpp_error (pfile, "invalid flag \"%s\" in line directive", |
c00e481c | 710 | cpp_token_as_text (pfile, token)); |
79bd622b | 711 | return 0; |
314ab6d5 | 712 | } |
713 | ||
25266990 | 714 | /* Subroutine of do_line and do_linemarker. Returns a version of STR |
715 | which has a NUL terminator and all escape sequences converted to | |
716 | their equivalents. Temporary, hopefully. */ | |
717 | static U_CHAR * | |
718 | dequote_string (pfile, str, len) | |
719 | cpp_reader *pfile; | |
720 | const U_CHAR *str; | |
721 | unsigned int len; | |
722 | { | |
723 | U_CHAR *result = _cpp_unaligned_alloc (pfile, len + 1); | |
724 | U_CHAR *dst = result; | |
725 | const U_CHAR *limit = str + len; | |
726 | unsigned int c; | |
727 | unsigned HOST_WIDE_INT mask; | |
728 | ||
729 | /* We need the mask to match the host's 'unsigned char', not the | |
730 | target's. */ | |
731 | if (CHAR_BIT < HOST_BITS_PER_WIDE_INT) | |
732 | mask = ((unsigned HOST_WIDE_INT) 1 << CHAR_BIT) - 1; | |
733 | else | |
734 | mask = ~(unsigned HOST_WIDE_INT)0; | |
735 | ||
736 | while (str < limit) | |
737 | { | |
738 | c = *str++; | |
739 | if (c != '\\') | |
740 | *dst++ = c; | |
741 | else | |
742 | *dst++ = cpp_parse_escape (pfile, (const U_CHAR **)&str, limit, mask); | |
743 | } | |
744 | *dst++ = '\0'; | |
745 | return result; | |
746 | } | |
747 | ||
748 | /* Subroutine of do_line and do_linemarker. Convert a number in STR, | |
749 | of length LEN, to binary; store it in NUMP, and return 0 if the | |
750 | number was well-formed, 1 if not. Temporary, hopefully. */ | |
f80e83a9 | 751 | static int |
752 | strtoul_for_line (str, len, nump) | |
753 | const U_CHAR *str; | |
754 | unsigned int len; | |
755 | unsigned long *nump; | |
756 | { | |
757 | unsigned long reg = 0; | |
758 | U_CHAR c; | |
759 | while (len--) | |
760 | { | |
761 | c = *str++; | |
762 | if (!ISDIGIT (c)) | |
763 | return 1; | |
764 | reg *= 10; | |
765 | reg += c - '0'; | |
766 | } | |
767 | *nump = reg; | |
768 | return 0; | |
769 | } | |
770 | ||
6d71dc85 | 771 | /* Interpret #line command. |
25266990 | 772 | Note that the filename string (if any) is a true string constant |
773 | (escapes are interpreted), unlike in #line. */ | |
69461e0d | 774 | static void |
4bf559f2 | 775 | do_line (pfile) |
1d6fa33f | 776 | cpp_reader *pfile; |
1d6fa33f | 777 | { |
f9b5f742 | 778 | const cpp_token *token; |
5a9f87bb | 779 | const char *new_file = pfile->map->to_file; |
780 | unsigned long new_lineno; | |
79bd622b | 781 | |
d7503801 | 782 | /* C99 raised the minimum limit on #line numbers. */ |
25266990 | 783 | unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767; |
d0d481d7 | 784 | |
79bd622b | 785 | /* #line commands expand macros. */ |
f9b5f742 | 786 | token = cpp_get_token (pfile); |
787 | if (token->type != CPP_NUMBER | |
788 | || strtoul_for_line (token->val.str.text, token->val.str.len, | |
789 | &new_lineno)) | |
1d6fa33f | 790 | { |
79bd622b | 791 | cpp_error (pfile, "\"%s\" after #line is not a positive integer", |
f9b5f742 | 792 | cpp_token_as_text (pfile, token)); |
0653b94e | 793 | return; |
6d71dc85 | 794 | } |
1d6fa33f | 795 | |
25266990 | 796 | if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap)) |
f80e83a9 | 797 | cpp_pedwarn (pfile, "line number out of range"); |
1d6fa33f | 798 | |
f9b5f742 | 799 | token = cpp_get_token (pfile); |
800 | if (token->type == CPP_STRING) | |
a852e3b1 | 801 | { |
25266990 | 802 | new_file = (const char *) dequote_string (pfile, token->val.str.text, |
803 | token->val.str.len); | |
804 | check_eol (pfile); | |
805 | } | |
806 | else if (token->type != CPP_EOF) | |
807 | { | |
808 | cpp_error (pfile, "\"%s\" is not a valid filename", | |
809 | cpp_token_as_text (pfile, token)); | |
810 | return; | |
811 | } | |
812 | ||
813 | skip_rest_of_line (pfile); | |
814 | _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno, | |
815 | pfile->map->sysp); | |
816 | } | |
817 | ||
818 | /* Interpret the # 44 "file" [flags] notation, which has slightly | |
819 | different syntax and semantics from #line: Flags are allowed, | |
820 | and we never complain about the line number being too big. */ | |
821 | static void | |
822 | do_linemarker (pfile) | |
823 | cpp_reader *pfile; | |
824 | { | |
825 | const cpp_token *token; | |
826 | const char *new_file = pfile->map->to_file; | |
827 | unsigned long new_lineno; | |
828 | unsigned int new_sysp = pfile->map->sysp; | |
829 | enum lc_reason reason = LC_RENAME; | |
830 | int flag; | |
831 | ||
832 | /* Back up so we can get the number again. Putting this in | |
833 | _cpp_handle_directive risks two calls to _cpp_backup_tokens in | |
834 | some circumstances, which can segfault. */ | |
835 | _cpp_backup_tokens (pfile, 1); | |
836 | ||
837 | /* #line commands expand macros. */ | |
838 | token = cpp_get_token (pfile); | |
839 | if (token->type != CPP_NUMBER | |
840 | || strtoul_for_line (token->val.str.text, token->val.str.len, | |
841 | &new_lineno)) | |
842 | { | |
843 | cpp_error (pfile, "\"%s\" after # is not a positive integer", | |
844 | cpp_token_as_text (pfile, token)); | |
845 | return; | |
846 | } | |
616814f8 | 847 | |
25266990 | 848 | token = cpp_get_token (pfile); |
849 | if (token->type == CPP_STRING) | |
850 | { | |
851 | new_file = (const char *) dequote_string (pfile, token->val.str.text, | |
852 | token->val.str.len); | |
853 | new_sysp = 0; | |
854 | flag = read_flag (pfile, 0); | |
855 | if (flag == 1) | |
856 | { | |
857 | reason = LC_ENTER; | |
858 | /* Fake an include for cpp_included (). */ | |
859 | _cpp_fake_include (pfile, new_file); | |
860 | flag = read_flag (pfile, flag); | |
861 | } | |
862 | else if (flag == 2) | |
863 | { | |
864 | reason = LC_LEAVE; | |
865 | flag = read_flag (pfile, flag); | |
866 | } | |
867 | if (flag == 3) | |
79bd622b | 868 | { |
25266990 | 869 | new_sysp = 1; |
870 | flag = read_flag (pfile, flag); | |
871 | if (flag == 4) | |
872 | new_sysp = 2; | |
79bd622b | 873 | } |
25266990 | 874 | |
bd507c05 | 875 | check_eol (pfile); |
f80e83a9 | 876 | } |
f9b5f742 | 877 | else if (token->type != CPP_EOF) |
d7503801 | 878 | { |
879 | cpp_error (pfile, "\"%s\" is not a valid filename", | |
f9b5f742 | 880 | cpp_token_as_text (pfile, token)); |
d7503801 | 881 | return; |
882 | } | |
1d6fa33f | 883 | |
fb5ab82c | 884 | skip_rest_of_line (pfile); |
5a9f87bb | 885 | _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp); |
d7503801 | 886 | } |
887 | ||
1ea7ed21 | 888 | /* Arrange the file_change callback. pfile->line has changed to |
f85fcf2b | 889 | FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system |
3fb1e43b | 890 | header, 2 for a system header that needs to be extern "C" protected, |
f85fcf2b | 891 | and zero otherwise. */ |
4087613d | 892 | void |
f85fcf2b | 893 | _cpp_do_file_change (pfile, reason, to_file, file_line, sysp) |
d7503801 | 894 | cpp_reader *pfile; |
38692459 | 895 | enum lc_reason reason; |
f85fcf2b | 896 | const char *to_file; |
1ea7ed21 | 897 | unsigned int file_line; |
f85fcf2b | 898 | unsigned int sysp; |
d7503801 | 899 | { |
f85fcf2b | 900 | pfile->map = add_line_map (&pfile->line_maps, reason, sysp, |
901 | pfile->line, to_file, file_line); | |
38692459 | 902 | |
4087613d | 903 | if (pfile->cb.file_change) |
f85fcf2b | 904 | (*pfile->cb.file_change) (pfile, pfile->map); |
1d6fa33f | 905 | } |
616814f8 | 906 | |
e484a1cc | 907 | /* Report a warning or error detected by the program we are |
908 | processing. Use the directive's tokens in the error message. */ | |
69461e0d | 909 | static void |
def71b06 | 910 | do_diagnostic (pfile, code, print_dir) |
1d6fa33f | 911 | cpp_reader *pfile; |
f3b83e9b | 912 | enum error_type code; |
def71b06 | 913 | int print_dir; |
1d6fa33f | 914 | { |
5621a364 | 915 | if (_cpp_begin_message (pfile, code, 0, 0)) |
6cae2504 | 916 | { |
def71b06 | 917 | if (print_dir) |
918 | fprintf (stderr, "#%s ", pfile->directive->name); | |
79bd622b | 919 | pfile->state.prevent_expansion++; |
920 | cpp_output_line (pfile, stderr); | |
921 | pfile->state.prevent_expansion--; | |
6cae2504 | 922 | } |
1d6fa33f | 923 | } |
924 | ||
f3b83e9b | 925 | static void |
926 | do_error (pfile) | |
927 | cpp_reader *pfile; | |
928 | { | |
def71b06 | 929 | do_diagnostic (pfile, ERROR, 1); |
f3b83e9b | 930 | } |
1d6fa33f | 931 | |
69461e0d | 932 | static void |
4bf559f2 | 933 | do_warning (pfile) |
1d6fa33f | 934 | cpp_reader *pfile; |
1d6fa33f | 935 | { |
2ef5e3d3 | 936 | /* We want #warning diagnostics to be emitted in system headers too. */ |
937 | do_diagnostic (pfile, WARNING_SYSHDR, 1); | |
1d6fa33f | 938 | } |
939 | ||
4e29fcb2 | 940 | /* Report program identification. */ |
69461e0d | 941 | static void |
4bf559f2 | 942 | do_ident (pfile) |
1d6fa33f | 943 | cpp_reader *pfile; |
1d6fa33f | 944 | { |
f9b5f742 | 945 | const cpp_token *str = cpp_get_token (pfile); |
6cae2504 | 946 | |
f9b5f742 | 947 | if (str->type != CPP_STRING) |
948 | cpp_error (pfile, "invalid #ident directive"); | |
79bd622b | 949 | else if (pfile->cb.ident) |
f9b5f742 | 950 | (*pfile->cb.ident) (pfile, pfile->directive_line, &str->val.str); |
4e29fcb2 | 951 | |
79bd622b | 952 | check_eol (pfile); |
1d6fa33f | 953 | } |
954 | ||
c6f14ce5 | 955 | /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the |
956 | matching entry, or NULL if none is found. The returned entry could | |
957 | be the start of a namespace chain, or a pragma. */ | |
958 | static struct pragma_entry * | |
959 | lookup_pragma_entry (chain, pragma) | |
960 | struct pragma_entry *chain; | |
5c43d212 | 961 | const cpp_hashnode *pragma; |
3f075661 | 962 | { |
5c43d212 | 963 | while (chain && chain->pragma != pragma) |
964 | chain = chain->next; | |
c6f14ce5 | 965 | |
966 | return chain; | |
967 | } | |
968 | ||
969 | /* Create and insert a pragma entry for NAME at the beginning of a | |
970 | singly-linked CHAIN. If handler is NULL, it is a namespace, | |
971 | otherwise it is a pragma and its handler. */ | |
972 | static struct pragma_entry * | |
5c43d212 | 973 | insert_pragma_entry (pfile, chain, pragma, handler) |
6cae2504 | 974 | cpp_reader *pfile; |
c6f14ce5 | 975 | struct pragma_entry **chain; |
5c43d212 | 976 | const cpp_hashnode *pragma; |
38692459 | 977 | pragma_cb handler; |
3f075661 | 978 | { |
c6f14ce5 | 979 | struct pragma_entry *new; |
6cae2504 | 980 | |
162cee98 | 981 | new = (struct pragma_entry *) |
e6a5f963 | 982 | _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry)); |
5c43d212 | 983 | new->pragma = pragma; |
c6f14ce5 | 984 | if (handler) |
985 | { | |
986 | new->is_nspace = 0; | |
987 | new->u.handler = handler; | |
988 | } | |
989 | else | |
990 | { | |
991 | new->is_nspace = 1; | |
992 | new->u.space = NULL; | |
993 | } | |
6cae2504 | 994 | |
c6f14ce5 | 995 | new->next = *chain; |
996 | *chain = new; | |
997 | return new; | |
6cae2504 | 998 | } |
3f075661 | 999 | |
c6f14ce5 | 1000 | /* Register a pragma NAME in namespace SPACE. If SPACE is null, it |
1001 | goes in the global namespace. HANDLER is the handler it will call, | |
1002 | which must be non-NULL. */ | |
6cae2504 | 1003 | void |
c6f14ce5 | 1004 | cpp_register_pragma (pfile, space, name, handler) |
3f075661 | 1005 | cpp_reader *pfile; |
6cae2504 | 1006 | const char *space; |
c6f14ce5 | 1007 | const char *name; |
1008 | pragma_cb handler; | |
3f075661 | 1009 | { |
c6f14ce5 | 1010 | struct pragma_entry **chain = &pfile->pragmas; |
1011 | struct pragma_entry *entry; | |
5c43d212 | 1012 | const cpp_hashnode *node; |
c6f14ce5 | 1013 | |
1014 | if (!handler) | |
1015 | abort (); | |
6cae2504 | 1016 | |
c6f14ce5 | 1017 | if (space) |
6cae2504 | 1018 | { |
5c43d212 | 1019 | node = cpp_lookup (pfile, U space, strlen (space)); |
1020 | entry = lookup_pragma_entry (*chain, node); | |
c6f14ce5 | 1021 | if (!entry) |
5c43d212 | 1022 | entry = insert_pragma_entry (pfile, chain, node, NULL); |
c6f14ce5 | 1023 | else if (!entry->is_nspace) |
1024 | goto clash; | |
1025 | chain = &entry->u.space; | |
6cae2504 | 1026 | } |
1027 | ||
c6f14ce5 | 1028 | /* Check for duplicates. */ |
5c43d212 | 1029 | node = cpp_lookup (pfile, U name, strlen (name)); |
1030 | entry = lookup_pragma_entry (*chain, node); | |
c6f14ce5 | 1031 | if (entry) |
1032 | { | |
1033 | if (entry->is_nspace) | |
1034 | clash: | |
1035 | cpp_ice (pfile, | |
1036 | "registering \"%s\" as both a pragma and a pragma namespace", | |
5c43d212 | 1037 | NODE_NAME (node)); |
c6f14ce5 | 1038 | else if (space) |
1039 | cpp_ice (pfile, "#pragma %s %s is already registered", space, name); | |
1040 | else | |
1041 | cpp_ice (pfile, "#pragma %s is already registered", name); | |
1042 | } | |
1043 | else | |
5c43d212 | 1044 | insert_pragma_entry (pfile, chain, node, handler); |
6cae2504 | 1045 | } |
c6f14ce5 | 1046 | |
1047 | /* Register the pragmas the preprocessor itself handles. */ | |
6cae2504 | 1048 | void |
1049 | _cpp_init_internal_pragmas (pfile) | |
1050 | cpp_reader *pfile; | |
1051 | { | |
c6f14ce5 | 1052 | /* Pragmas in the global namespace. */ |
6cae2504 | 1053 | cpp_register_pragma (pfile, 0, "poison", do_pragma_poison); |
1054 | cpp_register_pragma (pfile, 0, "once", do_pragma_once); | |
1055 | ||
c6f14ce5 | 1056 | /* New GCC-specific pragmas should be put in the GCC namespace. */ |
6cae2504 | 1057 | cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison); |
1058 | cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header); | |
1059 | cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency); | |
3f075661 | 1060 | } |
1d6fa33f | 1061 | |
c6f14ce5 | 1062 | /* Pragmata handling. We handle some, and pass the rest on to the |
1063 | front end. C99 defines three pragmas and says that no macro | |
1064 | expansion is to be performed on them; whether or not macro | |
1065 | expansion happens for other pragmas is implementation defined. | |
1066 | This implementation never macro-expands the text after #pragma. */ | |
69461e0d | 1067 | static void |
4bf559f2 | 1068 | do_pragma (pfile) |
1d6fa33f | 1069 | cpp_reader *pfile; |
1d6fa33f | 1070 | { |
c6f14ce5 | 1071 | const struct pragma_entry *p = NULL; |
f9b5f742 | 1072 | const cpp_token *token; |
c6f14ce5 | 1073 | unsigned int count = 1; |
2e398cc7 | 1074 | |
79bd622b | 1075 | pfile->state.prevent_expansion++; |
6cae2504 | 1076 | |
f9b5f742 | 1077 | token = cpp_get_token (pfile); |
1078 | if (token->type == CPP_NAME) | |
c0b823af | 1079 | { |
5c43d212 | 1080 | p = lookup_pragma_entry (pfile->pragmas, token->val.node); |
c6f14ce5 | 1081 | if (p && p->is_nspace) |
6cae2504 | 1082 | { |
c6f14ce5 | 1083 | count = 2; |
1084 | token = cpp_get_token (pfile); | |
1085 | if (token->type == CPP_NAME) | |
5c43d212 | 1086 | p = lookup_pragma_entry (p->u.space, token->val.node); |
c6f14ce5 | 1087 | else |
1088 | p = NULL; | |
6cae2504 | 1089 | } |
6cae2504 | 1090 | } |
f80e83a9 | 1091 | |
5621a364 | 1092 | /* FIXME. This is an awful kludge to get the front ends to update |
1093 | their notion of line number for diagnostic purposes. The line | |
1094 | number should be passed to the handler and they should do it | |
1095 | themselves. Stand-alone CPP must ignore us, otherwise it will | |
1096 | prefix the directive with spaces, hence the 1. Ugh. */ | |
1097 | if (pfile->cb.line_change) | |
f9b5f742 | 1098 | (*pfile->cb.line_change)(pfile, token, 1); |
5621a364 | 1099 | |
c6f14ce5 | 1100 | if (p) |
1101 | (*p->u.handler) (pfile); | |
38692459 | 1102 | else if (pfile->cb.def_pragma) |
fb5ab82c | 1103 | { |
1104 | _cpp_backup_tokens (pfile, count); | |
1105 | (*pfile->cb.def_pragma) (pfile, pfile->directive_line); | |
1106 | } | |
c6f14ce5 | 1107 | |
5621a364 | 1108 | pfile->state.prevent_expansion--; |
3f075661 | 1109 | } |
1110 | ||
e484a1cc | 1111 | /* Handle #pragma once. */ |
6cae2504 | 1112 | static void |
4e29fcb2 | 1113 | do_pragma_once (pfile) |
1114 | cpp_reader *pfile; | |
1115 | { | |
02c09dab | 1116 | cpp_warning (pfile, "#pragma once is obsolete"); |
1117 | ||
2fd3b10b | 1118 | if (pfile->buffer->prev == NULL) |
79bd622b | 1119 | cpp_warning (pfile, "#pragma once in main file"); |
4e29fcb2 | 1120 | else |
2fd3b10b | 1121 | _cpp_never_reread (pfile->buffer->inc); |
79bd622b | 1122 | |
1123 | check_eol (pfile); | |
4e29fcb2 | 1124 | } |
c9d838e1 | 1125 | |
e484a1cc | 1126 | /* Handle #pragma poison, to poison one or more identifiers so that |
1127 | the lexer produces a hard error for each subsequent usage. */ | |
6cae2504 | 1128 | static void |
4e29fcb2 | 1129 | do_pragma_poison (pfile) |
1130 | cpp_reader *pfile; | |
1131 | { | |
c00e481c | 1132 | const cpp_token *tok; |
c4abf88d | 1133 | cpp_hashnode *hp; |
4e29fcb2 | 1134 | |
79bd622b | 1135 | pfile->state.poisoned_ok = 1; |
4e29fcb2 | 1136 | for (;;) |
1137 | { | |
c00e481c | 1138 | tok = _cpp_lex_token (pfile); |
1139 | if (tok->type == CPP_EOF) | |
4e29fcb2 | 1140 | break; |
c00e481c | 1141 | if (tok->type != CPP_NAME) |
c9d838e1 | 1142 | { |
79bd622b | 1143 | cpp_error (pfile, "invalid #pragma GCC poison directive"); |
1144 | break; | |
c9d838e1 | 1145 | } |
1146 | ||
c00e481c | 1147 | hp = tok->val.node; |
79bd622b | 1148 | if (hp->flags & NODE_POISONED) |
1149 | continue; | |
1150 | ||
1151 | if (hp->type == NT_MACRO) | |
c86dbc5b | 1152 | cpp_warning (pfile, "poisoning existing macro \"%s\"", NODE_NAME (hp)); |
79bd622b | 1153 | _cpp_free_definition (hp); |
1154 | hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC; | |
c9d838e1 | 1155 | } |
79bd622b | 1156 | pfile->state.poisoned_ok = 0; |
1d6fa33f | 1157 | } |
e35f919d | 1158 | |
1159 | /* Mark the current header as a system header. This will suppress | |
1160 | some categories of warnings (notably those from -pedantic). It is | |
1161 | intended for use in system libraries that cannot be implemented in | |
1162 | conforming C, but cannot be certain that their headers appear in a | |
1163 | system include directory. To prevent abuse, it is rejected in the | |
1164 | primary source file. */ | |
6cae2504 | 1165 | static void |
e35f919d | 1166 | do_pragma_system_header (pfile) |
1167 | cpp_reader *pfile; | |
1168 | { | |
fd944c82 | 1169 | cpp_buffer *buffer = pfile->buffer; |
1170 | ||
1171 | if (buffer->prev == 0) | |
1172 | cpp_warning (pfile, "#pragma system_header ignored outside include file"); | |
e35f919d | 1173 | else |
38692459 | 1174 | { |
1175 | check_eol (pfile); | |
fb5ab82c | 1176 | skip_rest_of_line (pfile); |
38692459 | 1177 | cpp_make_system_header (pfile, 1, 0); |
1178 | } | |
e35f919d | 1179 | } |
e485814b | 1180 | |
1181 | /* Check the modified date of the current include file against a specified | |
1182 | file. Issue a diagnostic, if the specified file is newer. We use this to | |
1183 | determine if a fixed header should be refixed. */ | |
6cae2504 | 1184 | static void |
e485814b | 1185 | do_pragma_dependency (pfile) |
1186 | cpp_reader *pfile; | |
1187 | { | |
f9b5f742 | 1188 | const cpp_token *header; |
79bd622b | 1189 | int ordering; |
f80e83a9 | 1190 | |
f9b5f742 | 1191 | header = parse_include (pfile); |
1192 | if (!header) | |
6cae2504 | 1193 | return; |
f80e83a9 | 1194 | |
f9b5f742 | 1195 | ordering = _cpp_compare_file_date (pfile, header); |
e485814b | 1196 | if (ordering < 0) |
79bd622b | 1197 | cpp_warning (pfile, "cannot find source %s", |
f9b5f742 | 1198 | cpp_token_as_text (pfile, header)); |
e485814b | 1199 | else if (ordering > 0) |
1200 | { | |
79bd622b | 1201 | cpp_warning (pfile, "current file is older than %s", |
f9b5f742 | 1202 | cpp_token_as_text (pfile, header)); |
1203 | if (cpp_get_token (pfile)->type != CPP_EOF) | |
fb5ab82c | 1204 | { |
1205 | _cpp_backup_tokens (pfile, 1); | |
1206 | do_diagnostic (pfile, WARNING, 0); | |
1207 | } | |
e485814b | 1208 | } |
e485814b | 1209 | } |
1210 | ||
f9b5f742 | 1211 | /* Get a token but skip padding. */ |
1212 | static const cpp_token * | |
1213 | get_token_no_padding (pfile) | |
396ffa86 | 1214 | cpp_reader *pfile; |
396ffa86 | 1215 | { |
f9b5f742 | 1216 | for (;;) |
1217 | { | |
1218 | const cpp_token *result = cpp_get_token (pfile); | |
1219 | if (result->type != CPP_PADDING) | |
1220 | return result; | |
1221 | } | |
1222 | } | |
396ffa86 | 1223 | |
f9b5f742 | 1224 | /* Check syntax is "(string-literal)". Returns the string on success, |
1225 | or NULL on failure. */ | |
1226 | static const cpp_token * | |
1227 | get__Pragma_string (pfile) | |
1228 | cpp_reader *pfile; | |
1229 | { | |
1230 | const cpp_token *string; | |
396ffa86 | 1231 | |
f9b5f742 | 1232 | if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN) |
1233 | return NULL; | |
1234 | ||
1235 | string = get_token_no_padding (pfile); | |
396ffa86 | 1236 | if (string->type != CPP_STRING && string->type != CPP_WSTRING) |
f9b5f742 | 1237 | return NULL; |
1238 | ||
1239 | if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN) | |
1240 | return NULL; | |
396ffa86 | 1241 | |
f9b5f742 | 1242 | return string; |
396ffa86 | 1243 | } |
1244 | ||
1e0ef2fd | 1245 | /* Destringize IN into a temporary buffer, by removing the first \ of |
1246 | \" and \\ sequences, and process the result as a #pragma directive. */ | |
1247 | static void | |
1248 | destringize_and_run (pfile, in) | |
1249 | cpp_reader *pfile; | |
396ffa86 | 1250 | const cpp_string *in; |
396ffa86 | 1251 | { |
1252 | const unsigned char *src, *limit; | |
1e0ef2fd | 1253 | char *dest, *result; |
396ffa86 | 1254 | |
435fb09b | 1255 | dest = result = alloca (in->len + 1); |
396ffa86 | 1256 | for (src = in->text, limit = src + in->len; src < limit;) |
1257 | { | |
1258 | /* We know there is a character following the backslash. */ | |
1259 | if (*src == '\\' && (src[1] == '\\' || src[1] == '"')) | |
1260 | src++; | |
1261 | *dest++ = *src++; | |
1262 | } | |
435fb09b | 1263 | *dest = '\0'; |
396ffa86 | 1264 | |
1e0ef2fd | 1265 | run_directive (pfile, T_PRAGMA, result, dest - result); |
396ffa86 | 1266 | } |
1267 | ||
1e0ef2fd | 1268 | /* Handle the _Pragma operator. */ |
396ffa86 | 1269 | void |
1270 | _cpp_do__Pragma (pfile) | |
1271 | cpp_reader *pfile; | |
1272 | { | |
f9b5f742 | 1273 | const cpp_token *string = get__Pragma_string (pfile); |
396ffa86 | 1274 | |
f9b5f742 | 1275 | if (!string) |
1ea7ed21 | 1276 | cpp_error (pfile, "_Pragma takes a parenthesized string literal"); |
1277 | else | |
396ffa86 | 1278 | { |
729d2022 | 1279 | /* Ideally, we'd like |
1280 | token1 _Pragma ("foo") token2 | |
1281 | to be output as | |
1282 | token1 | |
1283 | # 7 "file.c" | |
1284 | #pragma foo | |
1285 | # 7 "file.c" | |
1286 | token2 | |
1287 | Getting these correct line markers is a little tricky. */ | |
1288 | ||
1289 | unsigned int orig_line = pfile->line; | |
1e0ef2fd | 1290 | destringize_and_run (pfile, &string->val.str); |
729d2022 | 1291 | pfile->line = orig_line; |
1292 | pfile->buffer->saved_flags = BOL; | |
396ffa86 | 1293 | } |
396ffa86 | 1294 | } |
1295 | ||
1d6fa33f | 1296 | /* Just ignore #sccs, on systems where we define it at all. */ |
ef33b55c | 1297 | #ifdef SCCS_DIRECTIVE |
69461e0d | 1298 | static void |
4bf559f2 | 1299 | do_sccs (pfile) |
f80e83a9 | 1300 | cpp_reader *pfile ATTRIBUTE_UNUSED; |
1d6fa33f | 1301 | { |
1d6fa33f | 1302 | } |
ef33b55c | 1303 | #endif |
20534e99 | 1304 | |
e484a1cc | 1305 | /* Handle #ifdef. */ |
69461e0d | 1306 | static void |
4bf559f2 | 1307 | do_ifdef (pfile) |
1308 | cpp_reader *pfile; | |
1309 | { | |
79bd622b | 1310 | int skip = 1; |
f80e83a9 | 1311 | |
5e878517 | 1312 | if (! pfile->state.skipping) |
79bd622b | 1313 | { |
1314 | const cpp_hashnode *node = lex_macro_node (pfile); | |
f80e83a9 | 1315 | |
79bd622b | 1316 | if (node) |
1317 | skip = node->type != NT_MACRO; | |
f99f369d | 1318 | |
1319 | if (node) | |
1320 | check_eol (pfile); | |
79bd622b | 1321 | } |
4bf559f2 | 1322 | |
79bd622b | 1323 | push_conditional (pfile, skip, T_IFDEF, 0); |
1324 | } | |
4bf559f2 | 1325 | |
e484a1cc | 1326 | /* Handle #ifndef. */ |
69461e0d | 1327 | static void |
4bf559f2 | 1328 | do_ifndef (pfile) |
1329 | cpp_reader *pfile; | |
1330 | { | |
79bd622b | 1331 | int skip = 1; |
76faa4c0 | 1332 | const cpp_hashnode *node = 0; |
4bf559f2 | 1333 | |
5e878517 | 1334 | if (! pfile->state.skipping) |
b970ec42 | 1335 | { |
79bd622b | 1336 | node = lex_macro_node (pfile); |
1337 | if (node) | |
1338 | skip = node->type == NT_MACRO; | |
f99f369d | 1339 | |
1340 | if (node) | |
1341 | check_eol (pfile); | |
b970ec42 | 1342 | } |
f80e83a9 | 1343 | |
79bd622b | 1344 | push_conditional (pfile, skip, T_IFNDEF, node); |
1d6fa33f | 1345 | } |
1346 | ||
fa233610 | 1347 | /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in |
1348 | pfile->mi_ind_cmacro so we can handle multiple-include | |
1349 | optimisations. If macro expansion occurs in the expression, we | |
1350 | cannot treat it as a controlling conditional, since the expansion | |
1351 | could change in the future. That is handled by cpp_get_token. */ | |
69461e0d | 1352 | static void |
c4357c92 | 1353 | do_if (pfile) |
1d6fa33f | 1354 | cpp_reader *pfile; |
1d6fa33f | 1355 | { |
79bd622b | 1356 | int skip = 1; |
1d6fa33f | 1357 | |
5e878517 | 1358 | if (! pfile->state.skipping) |
fa233610 | 1359 | skip = _cpp_parse_expr (pfile) == 0; |
79bd622b | 1360 | |
fa233610 | 1361 | push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro); |
1d6fa33f | 1362 | } |
1363 | ||
920b5d41 | 1364 | /* Flip skipping state if appropriate and continue without changing |
c4357c92 | 1365 | if_stack; this is so that the error message for missing #endif's |
1366 | etc. will point to the original #if. */ | |
69461e0d | 1367 | static void |
c4357c92 | 1368 | do_else (pfile) |
1369 | cpp_reader *pfile; | |
8e33b875 | 1370 | { |
920b5d41 | 1371 | cpp_buffer *buffer = pfile->buffer; |
1372 | struct if_stack *ifs = buffer->if_stack; | |
c030ed73 | 1373 | |
c4357c92 | 1374 | if (ifs == NULL) |
79bd622b | 1375 | cpp_error (pfile, "#else without #if"); |
1376 | else | |
c030ed73 | 1377 | { |
79bd622b | 1378 | if (ifs->type == T_ELSE) |
1379 | { | |
1380 | cpp_error (pfile, "#else after #else"); | |
729d2022 | 1381 | cpp_error_with_line (pfile, ifs->line, 0, |
79bd622b | 1382 | "the conditional began here"); |
1383 | } | |
920b5d41 | 1384 | ifs->type = T_ELSE; |
1385 | ||
5e878517 | 1386 | /* Skip any future (erroneous) #elses or #elifs. */ |
1387 | pfile->state.skipping = ifs->skip_elses; | |
1388 | ifs->skip_elses = true; | |
1d6fa33f | 1389 | |
79bd622b | 1390 | /* Invalidate any controlling macro. */ |
1391 | ifs->mi_cmacro = 0; | |
79bd622b | 1392 | |
5e878517 | 1393 | /* Only check EOL if was not originally skipping. */ |
923acdd5 | 1394 | if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels)) |
5e878517 | 1395 | check_eol (pfile); |
1396 | } | |
1d6fa33f | 1397 | } |
1398 | ||
e484a1cc | 1399 | /* Handle a #elif directive by not changing if_stack either. See the |
79bd622b | 1400 | comment above do_else. */ |
69461e0d | 1401 | static void |
c4357c92 | 1402 | do_elif (pfile) |
1d6fa33f | 1403 | cpp_reader *pfile; |
1d6fa33f | 1404 | { |
920b5d41 | 1405 | cpp_buffer *buffer = pfile->buffer; |
1406 | struct if_stack *ifs = buffer->if_stack; | |
1d6fa33f | 1407 | |
c4357c92 | 1408 | if (ifs == NULL) |
920b5d41 | 1409 | cpp_error (pfile, "#elif without #if"); |
1410 | else | |
0b60628c | 1411 | { |
920b5d41 | 1412 | if (ifs->type == T_ELSE) |
1413 | { | |
1414 | cpp_error (pfile, "#elif after #else"); | |
729d2022 | 1415 | cpp_error_with_line (pfile, ifs->line, 0, |
920b5d41 | 1416 | "the conditional began here"); |
1417 | } | |
1418 | ifs->type = T_ELIF; | |
79bd622b | 1419 | |
5e878517 | 1420 | /* Only evaluate this if we aren't skipping elses. During |
1421 | evaluation, set skipping to false to get lexer warnings. */ | |
1422 | if (ifs->skip_elses) | |
1423 | pfile->state.skipping = 1; | |
1424 | else | |
920b5d41 | 1425 | { |
5e878517 | 1426 | pfile->state.skipping = 0; |
1427 | pfile->state.skipping = ! _cpp_parse_expr (pfile); | |
1428 | ifs->skip_elses = ! pfile->state.skipping; | |
920b5d41 | 1429 | } |
5e878517 | 1430 | |
1431 | /* Invalidate any controlling macro. */ | |
1432 | ifs->mi_cmacro = 0; | |
0b60628c | 1433 | } |
1d6fa33f | 1434 | } |
1435 | ||
5e878517 | 1436 | /* #endif pops the if stack and resets pfile->state.skipping. */ |
69461e0d | 1437 | static void |
4bf559f2 | 1438 | do_endif (pfile) |
1d6fa33f | 1439 | cpp_reader *pfile; |
1d6fa33f | 1440 | { |
920b5d41 | 1441 | cpp_buffer *buffer = pfile->buffer; |
1442 | struct if_stack *ifs = buffer->if_stack; | |
c4357c92 | 1443 | |
c4357c92 | 1444 | if (ifs == NULL) |
1445 | cpp_error (pfile, "#endif without #if"); | |
1d6fa33f | 1446 | else |
1447 | { | |
5e878517 | 1448 | /* Only check EOL if was not originally skipping. */ |
923acdd5 | 1449 | if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels)) |
5e878517 | 1450 | check_eol (pfile); |
1451 | ||
79bd622b | 1452 | /* If potential control macro, we go back outside again. */ |
1453 | if (ifs->next == 0 && ifs->mi_cmacro) | |
1454 | { | |
fa233610 | 1455 | pfile->mi_valid = true; |
79bd622b | 1456 | pfile->mi_cmacro = ifs->mi_cmacro; |
1457 | } | |
1458 | ||
920b5d41 | 1459 | buffer->if_stack = ifs->next; |
5e878517 | 1460 | pfile->state.skipping = ifs->was_skipping; |
0d086e18 | 1461 | obstack_free (&pfile->buffer_ob, ifs); |
1d6fa33f | 1462 | } |
79bd622b | 1463 | } |
f80e83a9 | 1464 | |
e484a1cc | 1465 | /* Push an if_stack entry for a preprocessor conditional, and set |
1466 | pfile->state.skipping to SKIP. If TYPE indicates the conditional | |
1467 | is #if or #ifndef, CMACRO is a potentially controlling macro, and | |
1468 | we need to check here that we are at the top of the file. */ | |
c4357c92 | 1469 | static void |
1470 | push_conditional (pfile, skip, type, cmacro) | |
1471 | cpp_reader *pfile; | |
1472 | int skip; | |
1473 | int type; | |
1474 | const cpp_hashnode *cmacro; | |
1475 | { | |
1476 | struct if_stack *ifs; | |
920b5d41 | 1477 | cpp_buffer *buffer = pfile->buffer; |
c4357c92 | 1478 | |
0d086e18 | 1479 | ifs = xobnew (&pfile->buffer_ob, struct if_stack); |
729d2022 | 1480 | ifs->line = pfile->directive_line; |
920b5d41 | 1481 | ifs->next = buffer->if_stack; |
5e878517 | 1482 | ifs->skip_elses = pfile->state.skipping || !skip; |
1483 | ifs->was_skipping = pfile->state.skipping; | |
c4357c92 | 1484 | ifs->type = type; |
fa233610 | 1485 | /* This condition is effectively a test for top-of-file. */ |
1486 | if (pfile->mi_valid && pfile->mi_cmacro == 0) | |
79bd622b | 1487 | ifs->mi_cmacro = cmacro; |
1488 | else | |
1489 | ifs->mi_cmacro = 0; | |
c4357c92 | 1490 | |
5e878517 | 1491 | pfile->state.skipping = skip; |
920b5d41 | 1492 | buffer->if_stack = ifs; |
4896abfd | 1493 | } |
69bcbfc6 | 1494 | |
e484a1cc | 1495 | /* Read the tokens of the answer into the macro pool, in a directive |
1496 | of type TYPE. Only commit the memory if we intend it as permanent | |
1497 | storage, i.e. the #assert case. Returns 0 on success, and sets | |
1498 | ANSWERP to point to the answer. */ | |
79bd622b | 1499 | static int |
1500 | parse_answer (pfile, answerp, type) | |
1d6fa33f | 1501 | cpp_reader *pfile; |
f80e83a9 | 1502 | struct answer **answerp; |
79bd622b | 1503 | int type; |
1d6fa33f | 1504 | { |
f9b5f742 | 1505 | const cpp_token *paren; |
79bd622b | 1506 | struct answer *answer; |
e6a5f963 | 1507 | unsigned int acount; |
79bd622b | 1508 | |
1509 | /* In a conditional, it is legal to not have an open paren. We | |
1510 | should save the following token in this case. */ | |
f9b5f742 | 1511 | paren = cpp_get_token (pfile); |
79bd622b | 1512 | |
1513 | /* If not a paren, see if we're OK. */ | |
f9b5f742 | 1514 | if (paren->type != CPP_OPEN_PAREN) |
f80e83a9 | 1515 | { |
79bd622b | 1516 | /* In a conditional no answer is a test for any answer. It |
1517 | could be followed by any token. */ | |
1518 | if (type == T_IF) | |
fb5ab82c | 1519 | { |
1520 | _cpp_backup_tokens (pfile, 1); | |
1521 | return 0; | |
1522 | } | |
79bd622b | 1523 | |
1524 | /* #unassert with no answer is valid - it removes all answers. */ | |
f9b5f742 | 1525 | if (type == T_UNASSERT && paren->type == CPP_EOF) |
79bd622b | 1526 | return 0; |
bce8e0c0 | 1527 | |
f80e83a9 | 1528 | cpp_error (pfile, "missing '(' after predicate"); |
79bd622b | 1529 | return 1; |
f80e83a9 | 1530 | } |
69bcbfc6 | 1531 | |
e6a5f963 | 1532 | for (acount = 0;; acount++) |
f80e83a9 | 1533 | { |
e6a5f963 | 1534 | size_t room_needed; |
1535 | const cpp_token *token = cpp_get_token (pfile); | |
1536 | cpp_token *dest; | |
79bd622b | 1537 | |
f80e83a9 | 1538 | if (token->type == CPP_CLOSE_PAREN) |
1539 | break; | |
3ba8b497 | 1540 | |
79bd622b | 1541 | if (token->type == CPP_EOF) |
f80e83a9 | 1542 | { |
79bd622b | 1543 | cpp_error (pfile, "missing ')' to complete answer"); |
1544 | return 1; | |
f80e83a9 | 1545 | } |
e6a5f963 | 1546 | |
1547 | /* struct answer includes the space for one token. */ | |
1548 | room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token)); | |
1549 | ||
1550 | if (BUFF_ROOM (pfile->a_buff) < room_needed) | |
1551 | _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer)); | |
1552 | ||
1553 | dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount]; | |
1554 | *dest = *token; | |
1555 | ||
1556 | /* Drop whitespace at start, for answer equivalence purposes. */ | |
1557 | if (acount == 0) | |
1558 | dest->flags &= ~PREV_WHITE; | |
f80e83a9 | 1559 | } |
bce8e0c0 | 1560 | |
e6a5f963 | 1561 | if (acount == 0) |
69bcbfc6 | 1562 | { |
f80e83a9 | 1563 | cpp_error (pfile, "predicate's answer is empty"); |
79bd622b | 1564 | return 1; |
1d6fa33f | 1565 | } |
f80e83a9 | 1566 | |
e6a5f963 | 1567 | answer = (struct answer *) BUFF_FRONT (pfile->a_buff); |
1568 | answer->count = acount; | |
1569 | answer->next = NULL; | |
79bd622b | 1570 | *answerp = answer; |
f80e83a9 | 1571 | |
79bd622b | 1572 | return 0; |
1573 | } | |
f80e83a9 | 1574 | |
e484a1cc | 1575 | /* Parses an assertion directive of type TYPE, returning a pointer to |
1576 | the hash node of the predicate, or 0 on error. If an answer was | |
1577 | supplied, it is placed in ANSWERP, otherwise it is set to 0. */ | |
79bd622b | 1578 | static cpp_hashnode * |
1579 | parse_assertion (pfile, answerp, type) | |
1580 | cpp_reader *pfile; | |
1581 | struct answer **answerp; | |
1582 | int type; | |
1583 | { | |
1584 | cpp_hashnode *result = 0; | |
f9b5f742 | 1585 | const cpp_token *predicate; |
79bd622b | 1586 | |
1587 | /* We don't expand predicates or answers. */ | |
1588 | pfile->state.prevent_expansion++; | |
1589 | ||
79bd622b | 1590 | *answerp = 0; |
f9b5f742 | 1591 | predicate = cpp_get_token (pfile); |
1592 | if (predicate->type == CPP_EOF) | |
79bd622b | 1593 | cpp_error (pfile, "assertion without predicate"); |
f9b5f742 | 1594 | else if (predicate->type != CPP_NAME) |
79bd622b | 1595 | cpp_error (pfile, "predicate must be an identifier"); |
1596 | else if (parse_answer (pfile, answerp, type) == 0) | |
1597 | { | |
f9b5f742 | 1598 | unsigned int len = NODE_LEN (predicate->val.node); |
79bd622b | 1599 | unsigned char *sym = alloca (len + 1); |
f80e83a9 | 1600 | |
79bd622b | 1601 | /* Prefix '#' to get it out of macro namespace. */ |
1602 | sym[0] = '#'; | |
f9b5f742 | 1603 | memcpy (sym + 1, NODE_NAME (predicate->val.node), len); |
79bd622b | 1604 | result = cpp_lookup (pfile, sym, len + 1); |
1605 | } | |
69bcbfc6 | 1606 | |
79bd622b | 1607 | pfile->state.prevent_expansion--; |
1608 | return result; | |
1d6fa33f | 1609 | } |
69bcbfc6 | 1610 | |
e484a1cc | 1611 | /* Returns a pointer to the pointer to CANDIDATE in the answer chain, |
f80e83a9 | 1612 | or a pointer to NULL if the answer is not in the chain. */ |
79bd622b | 1613 | static struct answer ** |
1614 | find_answer (node, candidate) | |
f80e83a9 | 1615 | cpp_hashnode *node; |
79bd622b | 1616 | const struct answer *candidate; |
1d6fa33f | 1617 | { |
79bd622b | 1618 | unsigned int i; |
f80e83a9 | 1619 | struct answer **result; |
1d6fa33f | 1620 | |
f80e83a9 | 1621 | for (result = &node->value.answers; *result; result = &(*result)->next) |
79bd622b | 1622 | { |
1623 | struct answer *answer = *result; | |
1624 | ||
1625 | if (answer->count == candidate->count) | |
1626 | { | |
1627 | for (i = 0; i < answer->count; i++) | |
1628 | if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i])) | |
1629 | break; | |
1630 | ||
1631 | if (i == answer->count) | |
1632 | break; | |
1633 | } | |
1634 | } | |
c030ed73 | 1635 | |
f80e83a9 | 1636 | return result; |
1637 | } | |
bce8e0c0 | 1638 | |
79bd622b | 1639 | /* Test an assertion within a preprocessor conditional. Returns |
1640 | non-zero on failure, zero on success. On success, the result of | |
1641 | the test is written into VALUE. */ | |
1642 | int | |
1643 | _cpp_test_assertion (pfile, value) | |
1644 | cpp_reader *pfile; | |
1645 | int *value; | |
1646 | { | |
1647 | struct answer *answer; | |
1648 | cpp_hashnode *node; | |
1649 | ||
1650 | node = parse_assertion (pfile, &answer, T_IF); | |
1651 | if (node) | |
1652 | *value = (node->type == NT_ASSERTION && | |
1653 | (answer == 0 || *find_answer (node, answer) != 0)); | |
1654 | ||
1655 | /* We don't commit the memory for the answer - it's temporary only. */ | |
1656 | return node == 0; | |
1657 | } | |
1658 | ||
e484a1cc | 1659 | /* Handle #assert. */ |
69461e0d | 1660 | static void |
f80e83a9 | 1661 | do_assert (pfile) |
1662 | cpp_reader *pfile; | |
1663 | { | |
1664 | struct answer *new_answer; | |
1665 | cpp_hashnode *node; | |
1666 | ||
79bd622b | 1667 | node = parse_assertion (pfile, &new_answer, T_ASSERT); |
f80e83a9 | 1668 | if (node) |
c030ed73 | 1669 | { |
79bd622b | 1670 | /* Place the new answer in the answer list. First check there |
1671 | is not a duplicate. */ | |
f80e83a9 | 1672 | new_answer->next = 0; |
79bd622b | 1673 | if (node->type == NT_ASSERTION) |
f80e83a9 | 1674 | { |
79bd622b | 1675 | if (*find_answer (node, new_answer)) |
1676 | { | |
c86dbc5b | 1677 | cpp_warning (pfile, "\"%s\" re-asserted", NODE_NAME (node) + 1); |
79bd622b | 1678 | return; |
1679 | } | |
f80e83a9 | 1680 | new_answer->next = node->value.answers; |
1681 | } | |
e6a5f963 | 1682 | |
79bd622b | 1683 | node->type = NT_ASSERTION; |
f80e83a9 | 1684 | node->value.answers = new_answer; |
e6a5f963 | 1685 | BUFF_FRONT (pfile->a_buff) += (sizeof (struct answer) |
1686 | + (new_answer->count - 1) | |
1687 | * sizeof (cpp_token)); | |
1688 | check_eol (pfile); | |
c030ed73 | 1689 | } |
f80e83a9 | 1690 | } |
bce8e0c0 | 1691 | |
e484a1cc | 1692 | /* Handle #unassert. */ |
69461e0d | 1693 | static void |
f80e83a9 | 1694 | do_unassert (pfile) |
1695 | cpp_reader *pfile; | |
1696 | { | |
1697 | cpp_hashnode *node; | |
79bd622b | 1698 | struct answer *answer; |
f80e83a9 | 1699 | |
79bd622b | 1700 | node = parse_assertion (pfile, &answer, T_UNASSERT); |
1701 | /* It isn't an error to #unassert something that isn't asserted. */ | |
1702 | if (node && node->type == NT_ASSERTION) | |
69bcbfc6 | 1703 | { |
79bd622b | 1704 | if (answer) |
bce8e0c0 | 1705 | { |
79bd622b | 1706 | struct answer **p = find_answer (node, answer), *temp; |
f80e83a9 | 1707 | |
79bd622b | 1708 | /* Remove the answer from the list. */ |
1709 | temp = *p; | |
1710 | if (temp) | |
1711 | *p = temp->next; | |
3ba8b497 | 1712 | |
79bd622b | 1713 | /* Did we free the last answer? */ |
1714 | if (node->value.answers == 0) | |
1715 | node->type = NT_VOID; | |
e6a5f963 | 1716 | |
1717 | check_eol (pfile); | |
79bd622b | 1718 | } |
1719 | else | |
1720 | _cpp_free_definition (node); | |
f80e83a9 | 1721 | } |
79bd622b | 1722 | |
1723 | /* We don't commit the memory for the answer - it's temporary only. */ | |
1d6fa33f | 1724 | } |
1d6fa33f | 1725 | |
0578f103 | 1726 | /* These are for -D, -U, -A. */ |
1727 | ||
1728 | /* Process the string STR as if it appeared as the body of a #define. | |
1729 | If STR is just an identifier, define it with value 1. | |
1730 | If STR has anything after the identifier, then it should | |
2c0e001b | 1731 | be identifier=definition. */ |
9fa36617 | 1732 | void |
0578f103 | 1733 | cpp_define (pfile, str) |
9fa36617 | 1734 | cpp_reader *pfile; |
8d7a2585 | 1735 | const char *str; |
9fa36617 | 1736 | { |
0578f103 | 1737 | char *buf, *p; |
1738 | size_t count; | |
1739 | ||
0578f103 | 1740 | /* Copy the entire option so we can modify it. |
1741 | Change the first "=" in the string to a space. If there is none, | |
90dc47e0 | 1742 | tack " 1" on the end. */ |
1743 | ||
90dc47e0 | 1744 | count = strlen (str); |
435fb09b | 1745 | buf = (char *) alloca (count + 3); |
90dc47e0 | 1746 | memcpy (buf, str, count); |
1747 | ||
1748 | p = strchr (str, '='); | |
0578f103 | 1749 | if (p) |
90dc47e0 | 1750 | buf[p - str] = ' '; |
0578f103 | 1751 | else |
1752 | { | |
90dc47e0 | 1753 | buf[count++] = ' '; |
1754 | buf[count++] = '1'; | |
9fa36617 | 1755 | } |
435fb09b | 1756 | buf[count] = '\0'; |
eb3948d1 | 1757 | |
c808d026 | 1758 | run_directive (pfile, T_DEFINE, buf, count); |
a336277c | 1759 | } |
1760 | ||
f0c20935 | 1761 | /* Slight variant of the above for use by initialize_builtins. */ |
a336277c | 1762 | void |
1763 | _cpp_define_builtin (pfile, str) | |
1764 | cpp_reader *pfile; | |
1765 | const char *str; | |
1766 | { | |
c808d026 | 1767 | run_directive (pfile, T_DEFINE, str, strlen (str)); |
0578f103 | 1768 | } |
a92771b8 | 1769 | |
0578f103 | 1770 | /* Process MACRO as if it appeared as the body of an #undef. */ |
1771 | void | |
1772 | cpp_undef (pfile, macro) | |
1d6fa33f | 1773 | cpp_reader *pfile; |
0578f103 | 1774 | const char *macro; |
1d6fa33f | 1775 | { |
c808d026 | 1776 | run_directive (pfile, T_UNDEF, macro, strlen (macro)); |
1d6fa33f | 1777 | } |
1778 | ||
2c0e001b | 1779 | /* Process the string STR as if it appeared as the body of a #assert. */ |
0578f103 | 1780 | void |
1781 | cpp_assert (pfile, str) | |
1782 | cpp_reader *pfile; | |
1783 | const char *str; | |
1784 | { | |
90dc47e0 | 1785 | handle_assertion (pfile, str, T_ASSERT); |
0578f103 | 1786 | } |
1d6fa33f | 1787 | |
2c0e001b | 1788 | /* Process STR as if it appeared as the body of an #unassert. */ |
0578f103 | 1789 | void |
1790 | cpp_unassert (pfile, str) | |
1d6fa33f | 1791 | cpp_reader *pfile; |
0578f103 | 1792 | const char *str; |
1d6fa33f | 1793 | { |
90dc47e0 | 1794 | handle_assertion (pfile, str, T_UNASSERT); |
0578f103 | 1795 | } |
e3630c4b | 1796 | |
90dc47e0 | 1797 | /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */ |
1798 | static void | |
1799 | handle_assertion (pfile, str, type) | |
1800 | cpp_reader *pfile; | |
1801 | const char *str; | |
1802 | int type; | |
1803 | { | |
1804 | size_t count = strlen (str); | |
1805 | const char *p = strchr (str, '='); | |
1806 | ||
1807 | if (p) | |
1808 | { | |
1809 | /* Copy the entire option so we can modify it. Change the first | |
1810 | "=" in the string to a '(', and tack a ')' on the end. */ | |
435fb09b | 1811 | char *buf = (char *) alloca (count + 2); |
90dc47e0 | 1812 | |
1813 | memcpy (buf, str, count); | |
1814 | buf[p - str] = '('; | |
1815 | buf[count++] = ')'; | |
435fb09b | 1816 | buf[count] = '\0'; |
90dc47e0 | 1817 | str = buf; |
1818 | } | |
1819 | ||
c808d026 | 1820 | run_directive (pfile, type, str, count); |
90dc47e0 | 1821 | } |
1822 | ||
72b04ca4 | 1823 | /* The number of errors for a given reader. */ |
1824 | unsigned int | |
1825 | cpp_errors (pfile) | |
1826 | cpp_reader *pfile; | |
1827 | { | |
1828 | return pfile->errors; | |
1829 | } | |
1830 | ||
1831 | /* The options structure. */ | |
1832 | cpp_options * | |
1833 | cpp_get_options (pfile) | |
1834 | cpp_reader *pfile; | |
1835 | { | |
1836 | return &pfile->opts; | |
1837 | } | |
1838 | ||
1839 | /* The callbacks structure. */ | |
1840 | cpp_callbacks * | |
1841 | cpp_get_callbacks (pfile) | |
1842 | cpp_reader *pfile; | |
1843 | { | |
1844 | return &pfile->cb; | |
1845 | } | |
1846 | ||
38692459 | 1847 | /* The line map set. */ |
f85fcf2b | 1848 | const struct line_maps * |
38692459 | 1849 | cpp_get_line_maps (pfile) |
1850 | cpp_reader *pfile; | |
1851 | { | |
1852 | return &pfile->line_maps; | |
1853 | } | |
1854 | ||
72b04ca4 | 1855 | /* Copy the given callbacks structure to our own. */ |
1856 | void | |
1857 | cpp_set_callbacks (pfile, cb) | |
1858 | cpp_reader *pfile; | |
1859 | cpp_callbacks *cb; | |
1860 | { | |
1861 | pfile->cb = *cb; | |
1862 | } | |
1863 | ||
4087613d | 1864 | /* Push a new buffer on the buffer stack. Returns the new buffer; it |
1865 | doesn't fail. It does not generate a file change call back; that | |
1866 | is the responsibility of the caller. */ | |
f51c2148 | 1867 | cpp_buffer * |
c808d026 | 1868 | cpp_push_buffer (pfile, buffer, len, from_stage3, return_at_eof) |
f51c2148 | 1869 | cpp_reader *pfile; |
1870 | const U_CHAR *buffer; | |
c4a0fee9 | 1871 | size_t len; |
c808d026 | 1872 | int from_stage3; |
4dfe8b74 | 1873 | int return_at_eof; |
f51c2148 | 1874 | { |
0d086e18 | 1875 | cpp_buffer *new = xobnew (&pfile->buffer_ob, cpp_buffer); |
79bd622b | 1876 | |
bd507c05 | 1877 | /* Clears, amongst other things, if_stack and mi_cmacro. */ |
1878 | memset (new, 0, sizeof (cpp_buffer)); | |
1879 | ||
1880 | new->line_base = new->buf = new->cur = buffer; | |
1881 | new->rlimit = buffer + len; | |
c808d026 | 1882 | new->from_stage3 = from_stage3; |
c4a0fee9 | 1883 | new->prev = pfile->buffer; |
4dfe8b74 | 1884 | new->return_at_eof = return_at_eof; |
fb5ab82c | 1885 | new->saved_flags = BOL; |
6cee4464 | 1886 | |
c4a0fee9 | 1887 | pfile->buffer = new; |
6cee4464 | 1888 | |
f51c2148 | 1889 | return new; |
1890 | } | |
1891 | ||
4087613d | 1892 | /* If called from do_line, pops a single buffer. Otherwise pops all |
1893 | buffers until a real file is reached. Generates appropriate | |
1894 | call-backs. */ | |
4dfe8b74 | 1895 | void |
1896 | _cpp_pop_buffer (pfile) | |
f51c2148 | 1897 | cpp_reader *pfile; |
1898 | { | |
bd507c05 | 1899 | cpp_buffer *buffer = pfile->buffer; |
f0c20935 | 1900 | struct if_stack *ifs; |
54915428 | 1901 | bool pushed = false; |
f51c2148 | 1902 | |
bd507c05 | 1903 | /* Walk back up the conditional stack till we reach its level at |
1904 | entry to this file, issuing error messages. */ | |
1905 | for (ifs = buffer->if_stack; ifs; ifs = ifs->next) | |
729d2022 | 1906 | cpp_error_with_line (pfile, ifs->line, 0, |
bd507c05 | 1907 | "unterminated #%s", dtable[ifs->type].name); |
4087613d | 1908 | |
5621a364 | 1909 | /* In case of a missing #endif. */ |
1ea7ed21 | 1910 | pfile->state.skipping = 0; |
c808d026 | 1911 | |
1912 | /* Update the reader's buffer before _cpp_do_file_change. */ | |
1913 | pfile->buffer = buffer->prev; | |
1914 | ||
1915 | if (buffer->inc) | |
54915428 | 1916 | pushed = _cpp_pop_file_buffer (pfile, buffer->inc); |
c808d026 | 1917 | |
54915428 | 1918 | if (!pushed) |
1919 | obstack_free (&pfile->buffer_ob, buffer); | |
f51c2148 | 1920 | } |
1921 | ||
e484a1cc | 1922 | /* Enter all recognised directives in the hash table. */ |
f51c2148 | 1923 | void |
0d086e18 | 1924 | _cpp_init_directives (pfile) |
f51c2148 | 1925 | cpp_reader *pfile; |
1926 | { | |
87f5a8f0 | 1927 | unsigned int i; |
79bd622b | 1928 | cpp_hashnode *node; |
76faa4c0 | 1929 | |
ee6c4e4b | 1930 | for (i = 0; i < (unsigned int) N_DIRECTIVES; i++) |
79bd622b | 1931 | { |
87f5a8f0 | 1932 | node = cpp_lookup (pfile, dtable[i].name, dtable[i].length); |
1933 | node->directive_index = i + 1; | |
79bd622b | 1934 | } |
f51c2148 | 1935 | } |