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