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