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