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