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