xref: /openbsd-src/gnu/gcc/libcpp/init.c (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert /* CPP Library.
2*404b540aSrobert    Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3*404b540aSrobert    1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4*404b540aSrobert    Contributed by Per Bothner, 1994-95.
5*404b540aSrobert    Based on CCCP program by Paul Rubin, June 1986
6*404b540aSrobert    Adapted to ANSI C, Richard Stallman, Jan 1987
7*404b540aSrobert 
8*404b540aSrobert This program is free software; you can redistribute it and/or modify it
9*404b540aSrobert under the terms of the GNU General Public License as published by the
10*404b540aSrobert Free Software Foundation; either version 2, or (at your option) any
11*404b540aSrobert later version.
12*404b540aSrobert 
13*404b540aSrobert This program is distributed in the hope that it will be useful,
14*404b540aSrobert but WITHOUT ANY WARRANTY; without even the implied warranty of
15*404b540aSrobert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*404b540aSrobert GNU General Public License for more details.
17*404b540aSrobert 
18*404b540aSrobert You should have received a copy of the GNU General Public License
19*404b540aSrobert along with this program; if not, write to the Free Software
20*404b540aSrobert Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
21*404b540aSrobert 
22*404b540aSrobert #include "config.h"
23*404b540aSrobert #include "system.h"
24*404b540aSrobert #include "cpplib.h"
25*404b540aSrobert #include "internal.h"
26*404b540aSrobert #include "mkdeps.h"
27*404b540aSrobert #include "localedir.h"
28*404b540aSrobert 
29*404b540aSrobert static void init_library (void);
30*404b540aSrobert static void mark_named_operators (cpp_reader *);
31*404b540aSrobert static void read_original_filename (cpp_reader *);
32*404b540aSrobert static void read_original_directory (cpp_reader *);
33*404b540aSrobert static void post_options (cpp_reader *);
34*404b540aSrobert 
35*404b540aSrobert /* If we have designated initializers (GCC >2.7) these tables can be
36*404b540aSrobert    initialized, constant data.  Otherwise, they have to be filled in at
37*404b540aSrobert    runtime.  */
38*404b540aSrobert #if HAVE_DESIGNATED_INITIALIZERS
39*404b540aSrobert 
40*404b540aSrobert #define init_trigraph_map()  /* Nothing.  */
41*404b540aSrobert #define TRIGRAPH_MAP \
42*404b540aSrobert __extension__ const uchar _cpp_trigraph_map[UCHAR_MAX + 1] = {
43*404b540aSrobert 
44*404b540aSrobert #define END };
45*404b540aSrobert #define s(p, v) [p] = v,
46*404b540aSrobert 
47*404b540aSrobert #else
48*404b540aSrobert 
49*404b540aSrobert #define TRIGRAPH_MAP uchar _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
50*404b540aSrobert  static void init_trigraph_map (void) { \
51*404b540aSrobert  unsigned char *x = _cpp_trigraph_map;
52*404b540aSrobert 
53*404b540aSrobert #define END }
54*404b540aSrobert #define s(p, v) x[p] = v;
55*404b540aSrobert 
56*404b540aSrobert #endif
57*404b540aSrobert 
58*404b540aSrobert TRIGRAPH_MAP
59*404b540aSrobert   s('=', '#')	s(')', ']')	s('!', '|')
60*404b540aSrobert   s('(', '[')	s('\'', '^')	s('>', '}')
61*404b540aSrobert   s('/', '\\')	s('<', '{')	s('-', '~')
62*404b540aSrobert END
63*404b540aSrobert 
64*404b540aSrobert #undef s
65*404b540aSrobert #undef END
66*404b540aSrobert #undef TRIGRAPH_MAP
67*404b540aSrobert 
68*404b540aSrobert /* A set of booleans indicating what CPP features each source language
69*404b540aSrobert    requires.  */
70*404b540aSrobert struct lang_flags
71*404b540aSrobert {
72*404b540aSrobert   char c99;
73*404b540aSrobert   char cplusplus;
74*404b540aSrobert   char extended_numbers;
75*404b540aSrobert   char extended_identifiers;
76*404b540aSrobert   char std;
77*404b540aSrobert   char cplusplus_comments;
78*404b540aSrobert   char digraphs;
79*404b540aSrobert };
80*404b540aSrobert 
81*404b540aSrobert static const struct lang_flags lang_defaults[] =
82*404b540aSrobert { /*              c99 c++ xnum xid std  //   digr  */
83*404b540aSrobert   /* GNUC89 */  { 0,  0,  1,   0,  0,   1,   1     },
84*404b540aSrobert   /* GNUC99 */  { 1,  0,  1,   0,  0,   1,   1     },
85*404b540aSrobert   /* STDC89 */  { 0,  0,  0,   0,  1,   0,   0     },
86*404b540aSrobert   /* STDC94 */  { 0,  0,  0,   0,  1,   0,   1     },
87*404b540aSrobert   /* STDC99 */  { 1,  0,  1,   0,  1,   1,   1     },
88*404b540aSrobert   /* GNUCXX */  { 0,  1,  1,   0,  0,   1,   1     },
89*404b540aSrobert   /* CXX98  */  { 0,  1,  1,   0,  1,   1,   1     },
90*404b540aSrobert   /* ASM    */  { 0,  0,  1,   0,  0,   1,   0     }
91*404b540aSrobert   /* xid should be 1 for GNUC99, STDC99, GNUCXX and CXX98 when no
92*404b540aSrobert      longer experimental (when all uses of identifiers in the compiler
93*404b540aSrobert      have been audited for correct handling of extended
94*404b540aSrobert      identifiers).  */
95*404b540aSrobert };
96*404b540aSrobert 
97*404b540aSrobert /* Sets internal flags correctly for a given language.  */
98*404b540aSrobert void
cpp_set_lang(cpp_reader * pfile,enum c_lang lang)99*404b540aSrobert cpp_set_lang (cpp_reader *pfile, enum c_lang lang)
100*404b540aSrobert {
101*404b540aSrobert   const struct lang_flags *l = &lang_defaults[(int) lang];
102*404b540aSrobert 
103*404b540aSrobert   CPP_OPTION (pfile, lang) = lang;
104*404b540aSrobert 
105*404b540aSrobert   CPP_OPTION (pfile, c99)			 = l->c99;
106*404b540aSrobert   CPP_OPTION (pfile, cplusplus)			 = l->cplusplus;
107*404b540aSrobert   CPP_OPTION (pfile, extended_numbers)		 = l->extended_numbers;
108*404b540aSrobert   CPP_OPTION (pfile, extended_identifiers)	 = l->extended_identifiers;
109*404b540aSrobert   CPP_OPTION (pfile, std)			 = l->std;
110*404b540aSrobert   CPP_OPTION (pfile, trigraphs)			 = l->std;
111*404b540aSrobert   CPP_OPTION (pfile, cplusplus_comments)	 = l->cplusplus_comments;
112*404b540aSrobert   CPP_OPTION (pfile, digraphs)			 = l->digraphs;
113*404b540aSrobert }
114*404b540aSrobert 
115*404b540aSrobert /* Initialize library global state.  */
116*404b540aSrobert static void
init_library(void)117*404b540aSrobert init_library (void)
118*404b540aSrobert {
119*404b540aSrobert   static int initialized = 0;
120*404b540aSrobert 
121*404b540aSrobert   if (! initialized)
122*404b540aSrobert     {
123*404b540aSrobert       initialized = 1;
124*404b540aSrobert 
125*404b540aSrobert       /* Set up the trigraph map.  This doesn't need to do anything if
126*404b540aSrobert 	 we were compiled with a compiler that supports C99 designated
127*404b540aSrobert 	 initializers.  */
128*404b540aSrobert       init_trigraph_map ();
129*404b540aSrobert 
130*404b540aSrobert #ifdef ENABLE_NLS
131*404b540aSrobert        (void) bindtextdomain (PACKAGE, LOCALEDIR);
132*404b540aSrobert #endif
133*404b540aSrobert     }
134*404b540aSrobert }
135*404b540aSrobert 
136*404b540aSrobert /* Initialize a cpp_reader structure.  */
137*404b540aSrobert cpp_reader *
cpp_create_reader(enum c_lang lang,hash_table * table,struct line_maps * line_table)138*404b540aSrobert cpp_create_reader (enum c_lang lang, hash_table *table,
139*404b540aSrobert 		   struct line_maps *line_table)
140*404b540aSrobert {
141*404b540aSrobert   cpp_reader *pfile;
142*404b540aSrobert 
143*404b540aSrobert   /* Initialize this instance of the library if it hasn't been already.  */
144*404b540aSrobert   init_library ();
145*404b540aSrobert 
146*404b540aSrobert   pfile = XCNEW (cpp_reader);
147*404b540aSrobert 
148*404b540aSrobert   cpp_set_lang (pfile, lang);
149*404b540aSrobert   CPP_OPTION (pfile, warn_multichar) = 1;
150*404b540aSrobert   CPP_OPTION (pfile, discard_comments) = 1;
151*404b540aSrobert   CPP_OPTION (pfile, discard_comments_in_macro_exp) = 1;
152*404b540aSrobert   CPP_OPTION (pfile, show_column) = 1;
153*404b540aSrobert   CPP_OPTION (pfile, tabstop) = 8;
154*404b540aSrobert   CPP_OPTION (pfile, operator_names) = 1;
155*404b540aSrobert   CPP_OPTION (pfile, warn_trigraphs) = 2;
156*404b540aSrobert   CPP_OPTION (pfile, warn_endif_labels) = 1;
157*404b540aSrobert   CPP_OPTION (pfile, warn_deprecated) = 1;
158*404b540aSrobert   CPP_OPTION (pfile, warn_long_long) = !CPP_OPTION (pfile, c99);
159*404b540aSrobert   CPP_OPTION (pfile, dollars_in_ident) = 1;
160*404b540aSrobert   CPP_OPTION (pfile, warn_dollars) = 1;
161*404b540aSrobert   CPP_OPTION (pfile, warn_variadic_macros) = 1;
162*404b540aSrobert   CPP_OPTION (pfile, warn_normalize) = normalized_C;
163*404b540aSrobert 
164*404b540aSrobert   /* Default CPP arithmetic to something sensible for the host for the
165*404b540aSrobert      benefit of dumb users like fix-header.  */
166*404b540aSrobert   CPP_OPTION (pfile, precision) = CHAR_BIT * sizeof (long);
167*404b540aSrobert   CPP_OPTION (pfile, char_precision) = CHAR_BIT;
168*404b540aSrobert   CPP_OPTION (pfile, wchar_precision) = CHAR_BIT * sizeof (int);
169*404b540aSrobert   CPP_OPTION (pfile, int_precision) = CHAR_BIT * sizeof (int);
170*404b540aSrobert   CPP_OPTION (pfile, unsigned_char) = 0;
171*404b540aSrobert   CPP_OPTION (pfile, unsigned_wchar) = 1;
172*404b540aSrobert   CPP_OPTION (pfile, bytes_big_endian) = 1;  /* does not matter */
173*404b540aSrobert 
174*404b540aSrobert   /* Default to no charset conversion.  */
175*404b540aSrobert   CPP_OPTION (pfile, narrow_charset) = _cpp_default_encoding ();
176*404b540aSrobert   CPP_OPTION (pfile, wide_charset) = 0;
177*404b540aSrobert 
178*404b540aSrobert   /* Default the input character set to UTF-8.  */
179*404b540aSrobert   CPP_OPTION (pfile, input_charset) = _cpp_default_encoding ();
180*404b540aSrobert 
181*404b540aSrobert   /* A fake empty "directory" used as the starting point for files
182*404b540aSrobert      looked up without a search path.  Name cannot be '/' because we
183*404b540aSrobert      don't want to prepend anything at all to filenames using it.  All
184*404b540aSrobert      other entries are correct zero-initialized.  */
185*404b540aSrobert   pfile->no_search_path.name = (char *) "";
186*404b540aSrobert 
187*404b540aSrobert   /* Initialize the line map.  */
188*404b540aSrobert   pfile->line_table = line_table;
189*404b540aSrobert 
190*404b540aSrobert   /* Initialize lexer state.  */
191*404b540aSrobert   pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
192*404b540aSrobert 
193*404b540aSrobert   /* Set up static tokens.  */
194*404b540aSrobert   pfile->avoid_paste.type = CPP_PADDING;
195*404b540aSrobert   pfile->avoid_paste.val.source = NULL;
196*404b540aSrobert   pfile->eof.type = CPP_EOF;
197*404b540aSrobert   pfile->eof.flags = 0;
198*404b540aSrobert 
199*404b540aSrobert   /* Create a token buffer for the lexer.  */
200*404b540aSrobert   _cpp_init_tokenrun (&pfile->base_run, 250);
201*404b540aSrobert   pfile->cur_run = &pfile->base_run;
202*404b540aSrobert   pfile->cur_token = pfile->base_run.base;
203*404b540aSrobert 
204*404b540aSrobert   /* Initialize the base context.  */
205*404b540aSrobert   pfile->context = &pfile->base_context;
206*404b540aSrobert   pfile->base_context.macro = 0;
207*404b540aSrobert   pfile->base_context.prev = pfile->base_context.next = 0;
208*404b540aSrobert 
209*404b540aSrobert   /* Aligned and unaligned storage.  */
210*404b540aSrobert   pfile->a_buff = _cpp_get_buff (pfile, 0);
211*404b540aSrobert   pfile->u_buff = _cpp_get_buff (pfile, 0);
212*404b540aSrobert 
213*404b540aSrobert   /* The expression parser stack.  */
214*404b540aSrobert   _cpp_expand_op_stack (pfile);
215*404b540aSrobert 
216*404b540aSrobert   /* Initialize the buffer obstack.  */
217*404b540aSrobert   _obstack_begin (&pfile->buffer_ob, 0, 0,
218*404b540aSrobert 		  (void *(*) (long)) xmalloc,
219*404b540aSrobert 		  (void (*) (void *)) free);
220*404b540aSrobert 
221*404b540aSrobert   _cpp_init_files (pfile);
222*404b540aSrobert 
223*404b540aSrobert   _cpp_init_hashtable (pfile, table);
224*404b540aSrobert 
225*404b540aSrobert   return pfile;
226*404b540aSrobert }
227*404b540aSrobert 
228*404b540aSrobert /* Free resources used by PFILE.  Accessing PFILE after this function
229*404b540aSrobert    returns leads to undefined behavior.  Returns the error count.  */
230*404b540aSrobert void
cpp_destroy(cpp_reader * pfile)231*404b540aSrobert cpp_destroy (cpp_reader *pfile)
232*404b540aSrobert {
233*404b540aSrobert   cpp_context *context, *contextn;
234*404b540aSrobert   tokenrun *run, *runn;
235*404b540aSrobert 
236*404b540aSrobert   free (pfile->op_stack);
237*404b540aSrobert 
238*404b540aSrobert   while (CPP_BUFFER (pfile) != NULL)
239*404b540aSrobert     _cpp_pop_buffer (pfile);
240*404b540aSrobert 
241*404b540aSrobert   if (pfile->out.base)
242*404b540aSrobert     free (pfile->out.base);
243*404b540aSrobert 
244*404b540aSrobert   if (pfile->macro_buffer)
245*404b540aSrobert     {
246*404b540aSrobert       free (pfile->macro_buffer);
247*404b540aSrobert       pfile->macro_buffer = NULL;
248*404b540aSrobert       pfile->macro_buffer_len = 0;
249*404b540aSrobert     }
250*404b540aSrobert 
251*404b540aSrobert   if (pfile->deps)
252*404b540aSrobert     deps_free (pfile->deps);
253*404b540aSrobert   obstack_free (&pfile->buffer_ob, 0);
254*404b540aSrobert 
255*404b540aSrobert   _cpp_destroy_hashtable (pfile);
256*404b540aSrobert   _cpp_cleanup_files (pfile);
257*404b540aSrobert   _cpp_destroy_iconv (pfile);
258*404b540aSrobert 
259*404b540aSrobert   _cpp_free_buff (pfile->a_buff);
260*404b540aSrobert   _cpp_free_buff (pfile->u_buff);
261*404b540aSrobert   _cpp_free_buff (pfile->free_buffs);
262*404b540aSrobert 
263*404b540aSrobert   for (run = &pfile->base_run; run; run = runn)
264*404b540aSrobert     {
265*404b540aSrobert       runn = run->next;
266*404b540aSrobert       free (run->base);
267*404b540aSrobert       if (run != &pfile->base_run)
268*404b540aSrobert 	free (run);
269*404b540aSrobert     }
270*404b540aSrobert 
271*404b540aSrobert   for (context = pfile->base_context.next; context; context = contextn)
272*404b540aSrobert     {
273*404b540aSrobert       contextn = context->next;
274*404b540aSrobert       free (context);
275*404b540aSrobert     }
276*404b540aSrobert 
277*404b540aSrobert   free (pfile);
278*404b540aSrobert }
279*404b540aSrobert 
280*404b540aSrobert /* This structure defines one built-in identifier.  A node will be
281*404b540aSrobert    entered in the hash table under the name NAME, with value VALUE.
282*404b540aSrobert 
283*404b540aSrobert    There are two tables of these.  builtin_array holds all the
284*404b540aSrobert    "builtin" macros: these are handled by builtin_macro() in
285*404b540aSrobert    macro.c.  Builtin is somewhat of a misnomer -- the property of
286*404b540aSrobert    interest is that these macros require special code to compute their
287*404b540aSrobert    expansions.  The value is a "builtin_type" enumerator.
288*404b540aSrobert 
289*404b540aSrobert    operator_array holds the C++ named operators.  These are keywords
290*404b540aSrobert    which act as aliases for punctuators.  In C++, they cannot be
291*404b540aSrobert    altered through #define, and #if recognizes them as operators.  In
292*404b540aSrobert    C, these are not entered into the hash table at all (but see
293*404b540aSrobert    <iso646.h>).  The value is a token-type enumerator.  */
294*404b540aSrobert struct builtin
295*404b540aSrobert {
296*404b540aSrobert   const uchar *name;
297*404b540aSrobert   unsigned short len;
298*404b540aSrobert   unsigned short value;
299*404b540aSrobert };
300*404b540aSrobert 
301*404b540aSrobert #define B(n, t)    { DSC(n), t }
302*404b540aSrobert static const struct builtin builtin_array[] =
303*404b540aSrobert {
304*404b540aSrobert   B("__TIMESTAMP__",	 BT_TIMESTAMP),
305*404b540aSrobert   B("__TIME__",		 BT_TIME),
306*404b540aSrobert   B("__DATE__",		 BT_DATE),
307*404b540aSrobert   B("__FILE__",		 BT_FILE),
308*404b540aSrobert   B("__BASE_FILE__",	 BT_BASE_FILE),
309*404b540aSrobert   B("__LINE__",		 BT_SPECLINE),
310*404b540aSrobert   B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL),
311*404b540aSrobert   /* Keep builtins not used for -traditional-cpp at the end, and
312*404b540aSrobert      update init_builtins() if any more are added.  */
313*404b540aSrobert   B("_Pragma",		 BT_PRAGMA),
314*404b540aSrobert   B("__STDC__",		 BT_STDC),
315*404b540aSrobert };
316*404b540aSrobert 
317*404b540aSrobert static const struct builtin operator_array[] =
318*404b540aSrobert {
319*404b540aSrobert   B("and",	CPP_AND_AND),
320*404b540aSrobert   B("and_eq",	CPP_AND_EQ),
321*404b540aSrobert   B("bitand",	CPP_AND),
322*404b540aSrobert   B("bitor",	CPP_OR),
323*404b540aSrobert   B("compl",	CPP_COMPL),
324*404b540aSrobert   B("not",	CPP_NOT),
325*404b540aSrobert   B("not_eq",	CPP_NOT_EQ),
326*404b540aSrobert   B("or",	CPP_OR_OR),
327*404b540aSrobert   B("or_eq",	CPP_OR_EQ),
328*404b540aSrobert   B("xor",	CPP_XOR),
329*404b540aSrobert   B("xor_eq",	CPP_XOR_EQ)
330*404b540aSrobert };
331*404b540aSrobert #undef B
332*404b540aSrobert 
333*404b540aSrobert /* Mark the C++ named operators in the hash table.  */
334*404b540aSrobert static void
mark_named_operators(cpp_reader * pfile)335*404b540aSrobert mark_named_operators (cpp_reader *pfile)
336*404b540aSrobert {
337*404b540aSrobert   const struct builtin *b;
338*404b540aSrobert 
339*404b540aSrobert   for (b = operator_array;
340*404b540aSrobert        b < (operator_array + ARRAY_SIZE (operator_array));
341*404b540aSrobert        b++)
342*404b540aSrobert     {
343*404b540aSrobert       cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
344*404b540aSrobert       hp->flags |= NODE_OPERATOR;
345*404b540aSrobert       hp->is_directive = 0;
346*404b540aSrobert       hp->directive_index = b->value;
347*404b540aSrobert     }
348*404b540aSrobert }
349*404b540aSrobert 
350*404b540aSrobert /* Read the builtins table above and enter them, and language-specific
351*404b540aSrobert    macros, into the hash table.  HOSTED is true if this is a hosted
352*404b540aSrobert    environment.  */
353*404b540aSrobert void
cpp_init_builtins(cpp_reader * pfile,int hosted)354*404b540aSrobert cpp_init_builtins (cpp_reader *pfile, int hosted)
355*404b540aSrobert {
356*404b540aSrobert   const struct builtin *b;
357*404b540aSrobert   size_t n = ARRAY_SIZE (builtin_array);
358*404b540aSrobert 
359*404b540aSrobert   if (CPP_OPTION (pfile, traditional))
360*404b540aSrobert     n -= 2;
361*404b540aSrobert   else if (! CPP_OPTION (pfile, stdc_0_in_system_headers)
362*404b540aSrobert 	   || CPP_OPTION (pfile, std))
363*404b540aSrobert     {
364*404b540aSrobert       n--;
365*404b540aSrobert       _cpp_define_builtin (pfile, "__STDC__ 1");
366*404b540aSrobert     }
367*404b540aSrobert 
368*404b540aSrobert   for (b = builtin_array; b < builtin_array + n; b++)
369*404b540aSrobert     {
370*404b540aSrobert       cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
371*404b540aSrobert       hp->type = NT_MACRO;
372*404b540aSrobert       hp->flags |= NODE_BUILTIN | NODE_WARN;
373*404b540aSrobert       hp->value.builtin = (enum builtin_type) b->value;
374*404b540aSrobert     }
375*404b540aSrobert 
376*404b540aSrobert   if (CPP_OPTION (pfile, cplusplus))
377*404b540aSrobert     _cpp_define_builtin (pfile, "__cplusplus 1");
378*404b540aSrobert   else if (CPP_OPTION (pfile, lang) == CLK_ASM)
379*404b540aSrobert     _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
380*404b540aSrobert   else if (CPP_OPTION (pfile, lang) == CLK_STDC94)
381*404b540aSrobert     _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
382*404b540aSrobert   else if (CPP_OPTION (pfile, c99))
383*404b540aSrobert     _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
384*404b540aSrobert 
385*404b540aSrobert   if (hosted)
386*404b540aSrobert     _cpp_define_builtin (pfile, "__STDC_HOSTED__ 1");
387*404b540aSrobert   else
388*404b540aSrobert     _cpp_define_builtin (pfile, "__STDC_HOSTED__ 0");
389*404b540aSrobert 
390*404b540aSrobert   if (CPP_OPTION (pfile, objc))
391*404b540aSrobert     _cpp_define_builtin (pfile, "__OBJC__ 1");
392*404b540aSrobert }
393*404b540aSrobert 
394*404b540aSrobert /* Sanity-checks are dependent on command-line options, so it is
395*404b540aSrobert    called as a subroutine of cpp_read_main_file ().  */
396*404b540aSrobert #if ENABLE_CHECKING
397*404b540aSrobert static void sanity_checks (cpp_reader *);
sanity_checks(cpp_reader * pfile)398*404b540aSrobert static void sanity_checks (cpp_reader *pfile)
399*404b540aSrobert {
400*404b540aSrobert   cppchar_t test = 0;
401*404b540aSrobert   size_t max_precision = 2 * CHAR_BIT * sizeof (cpp_num_part);
402*404b540aSrobert 
403*404b540aSrobert   /* Sanity checks for assumptions about CPP arithmetic and target
404*404b540aSrobert      type precisions made by cpplib.  */
405*404b540aSrobert   test--;
406*404b540aSrobert   if (test < 1)
407*404b540aSrobert     cpp_error (pfile, CPP_DL_ICE, "cppchar_t must be an unsigned type");
408*404b540aSrobert 
409*404b540aSrobert   if (CPP_OPTION (pfile, precision) > max_precision)
410*404b540aSrobert     cpp_error (pfile, CPP_DL_ICE,
411*404b540aSrobert 	       "preprocessor arithmetic has maximum precision of %lu bits;"
412*404b540aSrobert 	       " target requires %lu bits",
413*404b540aSrobert 	       (unsigned long) max_precision,
414*404b540aSrobert 	       (unsigned long) CPP_OPTION (pfile, precision));
415*404b540aSrobert 
416*404b540aSrobert   if (CPP_OPTION (pfile, precision) < CPP_OPTION (pfile, int_precision))
417*404b540aSrobert     cpp_error (pfile, CPP_DL_ICE,
418*404b540aSrobert 	       "CPP arithmetic must be at least as precise as a target int");
419*404b540aSrobert 
420*404b540aSrobert   if (CPP_OPTION (pfile, char_precision) < 8)
421*404b540aSrobert     cpp_error (pfile, CPP_DL_ICE, "target char is less than 8 bits wide");
422*404b540aSrobert 
423*404b540aSrobert   if (CPP_OPTION (pfile, wchar_precision) < CPP_OPTION (pfile, char_precision))
424*404b540aSrobert     cpp_error (pfile, CPP_DL_ICE,
425*404b540aSrobert 	       "target wchar_t is narrower than target char");
426*404b540aSrobert 
427*404b540aSrobert   if (CPP_OPTION (pfile, int_precision) < CPP_OPTION (pfile, char_precision))
428*404b540aSrobert     cpp_error (pfile, CPP_DL_ICE,
429*404b540aSrobert 	       "target int is narrower than target char");
430*404b540aSrobert 
431*404b540aSrobert   /* This is assumed in eval_token() and could be fixed if necessary.  */
432*404b540aSrobert   if (sizeof (cppchar_t) > sizeof (cpp_num_part))
433*404b540aSrobert     cpp_error (pfile, CPP_DL_ICE,
434*404b540aSrobert 	       "CPP half-integer narrower than CPP character");
435*404b540aSrobert 
436*404b540aSrobert   if (CPP_OPTION (pfile, wchar_precision) > BITS_PER_CPPCHAR_T)
437*404b540aSrobert     cpp_error (pfile, CPP_DL_ICE,
438*404b540aSrobert 	       "CPP on this host cannot handle wide character constants over"
439*404b540aSrobert 	       " %lu bits, but the target requires %lu bits",
440*404b540aSrobert 	       (unsigned long) BITS_PER_CPPCHAR_T,
441*404b540aSrobert 	       (unsigned long) CPP_OPTION (pfile, wchar_precision));
442*404b540aSrobert }
443*404b540aSrobert #else
444*404b540aSrobert # define sanity_checks(PFILE)
445*404b540aSrobert #endif
446*404b540aSrobert 
447*404b540aSrobert /* This is called after options have been parsed, and partially
448*404b540aSrobert    processed.  */
449*404b540aSrobert void
cpp_post_options(cpp_reader * pfile)450*404b540aSrobert cpp_post_options (cpp_reader *pfile)
451*404b540aSrobert {
452*404b540aSrobert   sanity_checks (pfile);
453*404b540aSrobert 
454*404b540aSrobert   post_options (pfile);
455*404b540aSrobert 
456*404b540aSrobert   /* Mark named operators before handling command line macros.  */
457*404b540aSrobert   if (CPP_OPTION (pfile, cplusplus) && CPP_OPTION (pfile, operator_names))
458*404b540aSrobert     mark_named_operators (pfile);
459*404b540aSrobert }
460*404b540aSrobert 
461*404b540aSrobert /* Setup for processing input from the file named FNAME, or stdin if
462*404b540aSrobert    it is the empty string.  Return the original filename
463*404b540aSrobert    on success (e.g. foo.i->foo.c), or NULL on failure.  */
464*404b540aSrobert const char *
cpp_read_main_file(cpp_reader * pfile,const char * fname)465*404b540aSrobert cpp_read_main_file (cpp_reader *pfile, const char *fname)
466*404b540aSrobert {
467*404b540aSrobert   if (CPP_OPTION (pfile, deps.style) != DEPS_NONE)
468*404b540aSrobert     {
469*404b540aSrobert       if (!pfile->deps)
470*404b540aSrobert 	pfile->deps = deps_init ();
471*404b540aSrobert 
472*404b540aSrobert       /* Set the default target (if there is none already).  */
473*404b540aSrobert       deps_add_default_target (pfile->deps, fname);
474*404b540aSrobert     }
475*404b540aSrobert 
476*404b540aSrobert   pfile->main_file
477*404b540aSrobert     = _cpp_find_file (pfile, fname, &pfile->no_search_path, false, 0);
478*404b540aSrobert   if (_cpp_find_failed (pfile->main_file))
479*404b540aSrobert     return NULL;
480*404b540aSrobert 
481*404b540aSrobert   _cpp_stack_file (pfile, pfile->main_file, false);
482*404b540aSrobert 
483*404b540aSrobert   /* For foo.i, read the original filename foo.c now, for the benefit
484*404b540aSrobert      of the front ends.  */
485*404b540aSrobert   if (CPP_OPTION (pfile, preprocessed))
486*404b540aSrobert     {
487*404b540aSrobert       read_original_filename (pfile);
488*404b540aSrobert       fname = pfile->line_table->maps[pfile->line_table->used-1].to_file;
489*404b540aSrobert     }
490*404b540aSrobert   return fname;
491*404b540aSrobert }
492*404b540aSrobert 
493*404b540aSrobert /* For preprocessed files, if the first tokens are of the form # NUM.
494*404b540aSrobert    handle the directive so we know the original file name.  This will
495*404b540aSrobert    generate file_change callbacks, which the front ends must handle
496*404b540aSrobert    appropriately given their state of initialization.  */
497*404b540aSrobert static void
read_original_filename(cpp_reader * pfile)498*404b540aSrobert read_original_filename (cpp_reader *pfile)
499*404b540aSrobert {
500*404b540aSrobert   const cpp_token *token, *token1;
501*404b540aSrobert 
502*404b540aSrobert   /* Lex ahead; if the first tokens are of the form # NUM, then
503*404b540aSrobert      process the directive, otherwise back up.  */
504*404b540aSrobert   token = _cpp_lex_direct (pfile);
505*404b540aSrobert   if (token->type == CPP_HASH)
506*404b540aSrobert     {
507*404b540aSrobert       pfile->state.in_directive = 1;
508*404b540aSrobert       token1 = _cpp_lex_direct (pfile);
509*404b540aSrobert       _cpp_backup_tokens (pfile, 1);
510*404b540aSrobert       pfile->state.in_directive = 0;
511*404b540aSrobert 
512*404b540aSrobert       /* If it's a #line directive, handle it.  */
513*404b540aSrobert       if (token1->type == CPP_NUMBER)
514*404b540aSrobert 	{
515*404b540aSrobert 	  _cpp_handle_directive (pfile, token->flags & PREV_WHITE);
516*404b540aSrobert 	  read_original_directory (pfile);
517*404b540aSrobert 	  return;
518*404b540aSrobert 	}
519*404b540aSrobert     }
520*404b540aSrobert 
521*404b540aSrobert   /* Backup as if nothing happened.  */
522*404b540aSrobert   _cpp_backup_tokens (pfile, 1);
523*404b540aSrobert }
524*404b540aSrobert 
525*404b540aSrobert /* For preprocessed files, if the tokens following the first filename
526*404b540aSrobert    line is of the form # <line> "/path/name//", handle the
527*404b540aSrobert    directive so we know the original current directory.  */
528*404b540aSrobert static void
read_original_directory(cpp_reader * pfile)529*404b540aSrobert read_original_directory (cpp_reader *pfile)
530*404b540aSrobert {
531*404b540aSrobert   const cpp_token *hash, *token;
532*404b540aSrobert 
533*404b540aSrobert   /* Lex ahead; if the first tokens are of the form # NUM, then
534*404b540aSrobert      process the directive, otherwise back up.  */
535*404b540aSrobert   hash = _cpp_lex_direct (pfile);
536*404b540aSrobert   if (hash->type != CPP_HASH)
537*404b540aSrobert     {
538*404b540aSrobert       _cpp_backup_tokens (pfile, 1);
539*404b540aSrobert       return;
540*404b540aSrobert     }
541*404b540aSrobert 
542*404b540aSrobert   token = _cpp_lex_direct (pfile);
543*404b540aSrobert 
544*404b540aSrobert   if (token->type != CPP_NUMBER)
545*404b540aSrobert     {
546*404b540aSrobert       _cpp_backup_tokens (pfile, 2);
547*404b540aSrobert       return;
548*404b540aSrobert     }
549*404b540aSrobert 
550*404b540aSrobert   token = _cpp_lex_direct (pfile);
551*404b540aSrobert 
552*404b540aSrobert   if (token->type != CPP_STRING
553*404b540aSrobert       || ! (token->val.str.len >= 5
554*404b540aSrobert 	    && token->val.str.text[token->val.str.len-2] == '/'
555*404b540aSrobert 	    && token->val.str.text[token->val.str.len-3] == '/'))
556*404b540aSrobert     {
557*404b540aSrobert       _cpp_backup_tokens (pfile, 3);
558*404b540aSrobert       return;
559*404b540aSrobert     }
560*404b540aSrobert 
561*404b540aSrobert   if (pfile->cb.dir_change)
562*404b540aSrobert     {
563*404b540aSrobert       char *debugdir = (char *) alloca (token->val.str.len - 3);
564*404b540aSrobert 
565*404b540aSrobert       memcpy (debugdir, (const char *) token->val.str.text + 1,
566*404b540aSrobert 	      token->val.str.len - 4);
567*404b540aSrobert       debugdir[token->val.str.len - 4] = '\0';
568*404b540aSrobert 
569*404b540aSrobert       pfile->cb.dir_change (pfile, debugdir);
570*404b540aSrobert     }
571*404b540aSrobert }
572*404b540aSrobert 
573*404b540aSrobert /* This is called at the end of preprocessing.  It pops the last
574*404b540aSrobert    buffer and writes dependency output, and returns the number of
575*404b540aSrobert    errors.
576*404b540aSrobert 
577*404b540aSrobert    Maybe it should also reset state, such that you could call
578*404b540aSrobert    cpp_start_read with a new filename to restart processing.  */
579*404b540aSrobert int
cpp_finish(cpp_reader * pfile,FILE * deps_stream)580*404b540aSrobert cpp_finish (cpp_reader *pfile, FILE *deps_stream)
581*404b540aSrobert {
582*404b540aSrobert   /* Warn about unused macros before popping the final buffer.  */
583*404b540aSrobert   if (CPP_OPTION (pfile, warn_unused_macros))
584*404b540aSrobert     cpp_forall_identifiers (pfile, _cpp_warn_if_unused_macro, NULL);
585*404b540aSrobert 
586*404b540aSrobert   /* lex.c leaves the final buffer on the stack.  This it so that
587*404b540aSrobert      it returns an unending stream of CPP_EOFs to the client.  If we
588*404b540aSrobert      popped the buffer, we'd dereference a NULL buffer pointer and
589*404b540aSrobert      segfault.  It's nice to allow the client to do worry-free excess
590*404b540aSrobert      cpp_get_token calls.  */
591*404b540aSrobert   while (pfile->buffer)
592*404b540aSrobert     _cpp_pop_buffer (pfile);
593*404b540aSrobert 
594*404b540aSrobert   /* Don't write the deps file if there are errors.  */
595*404b540aSrobert   if (CPP_OPTION (pfile, deps.style) != DEPS_NONE
596*404b540aSrobert       && deps_stream && pfile->errors == 0)
597*404b540aSrobert     {
598*404b540aSrobert       deps_write (pfile->deps, deps_stream, 72);
599*404b540aSrobert 
600*404b540aSrobert       if (CPP_OPTION (pfile, deps.phony_targets))
601*404b540aSrobert 	deps_phony_targets (pfile->deps, deps_stream);
602*404b540aSrobert     }
603*404b540aSrobert 
604*404b540aSrobert   /* Report on headers that could use multiple include guards.  */
605*404b540aSrobert   if (CPP_OPTION (pfile, print_include_names))
606*404b540aSrobert     _cpp_report_missing_guards (pfile);
607*404b540aSrobert 
608*404b540aSrobert   return pfile->errors;
609*404b540aSrobert }
610*404b540aSrobert 
611*404b540aSrobert static void
post_options(cpp_reader * pfile)612*404b540aSrobert post_options (cpp_reader *pfile)
613*404b540aSrobert {
614*404b540aSrobert   /* -Wtraditional is not useful in C++ mode.  */
615*404b540aSrobert   if (CPP_OPTION (pfile, cplusplus))
616*404b540aSrobert     CPP_OPTION (pfile, warn_traditional) = 0;
617*404b540aSrobert 
618*404b540aSrobert   /* Permanently disable macro expansion if we are rescanning
619*404b540aSrobert      preprocessed text.  Read preprocesed source in ISO mode.  */
620*404b540aSrobert   if (CPP_OPTION (pfile, preprocessed))
621*404b540aSrobert     {
622*404b540aSrobert       pfile->state.prevent_expansion = 1;
623*404b540aSrobert       CPP_OPTION (pfile, traditional) = 0;
624*404b540aSrobert     }
625*404b540aSrobert 
626*404b540aSrobert   if (CPP_OPTION (pfile, warn_trigraphs) == 2)
627*404b540aSrobert     CPP_OPTION (pfile, warn_trigraphs) = !CPP_OPTION (pfile, trigraphs);
628*404b540aSrobert 
629*404b540aSrobert   if (CPP_OPTION (pfile, traditional))
630*404b540aSrobert     {
631*404b540aSrobert       CPP_OPTION (pfile, cplusplus_comments) = 0;
632*404b540aSrobert 
633*404b540aSrobert       /* Traditional CPP does not accurately track column information.  */
634*404b540aSrobert       CPP_OPTION (pfile, show_column) = 0;
635*404b540aSrobert       CPP_OPTION (pfile, trigraphs) = 0;
636*404b540aSrobert       CPP_OPTION (pfile, warn_trigraphs) = 0;
637*404b540aSrobert     }
638*404b540aSrobert }
639