1169695Skan/* CPP Library.
2169695Skan   Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3169695Skan   1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4169695Skan   Contributed by Per Bothner, 1994-95.
5169695Skan   Based on CCCP program by Paul Rubin, June 1986
6169695Skan   Adapted to ANSI C, Richard Stallman, Jan 1987
7169695Skan
8169695SkanThis program is free software; you can redistribute it and/or modify it
9169695Skanunder the terms of the GNU General Public License as published by the
10169695SkanFree Software Foundation; either version 2, or (at your option) any
11169695Skanlater version.
12169695Skan
13169695SkanThis program is distributed in the hope that it will be useful,
14169695Skanbut WITHOUT ANY WARRANTY; without even the implied warranty of
15169695SkanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16169695SkanGNU General Public License for more details.
17169695Skan
18169695SkanYou should have received a copy of the GNU General Public License
19169695Skanalong with this program; if not, write to the Free Software
20169695SkanFoundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
21169695Skan
22169695Skan#include "config.h"
23169695Skan#include "system.h"
24169695Skan#include "cpplib.h"
25169695Skan#include "internal.h"
26169695Skan#include "mkdeps.h"
27169695Skan#include "localedir.h"
28169695Skan
29169695Skanstatic void init_library (void);
30169695Skanstatic void mark_named_operators (cpp_reader *);
31169695Skanstatic void read_original_filename (cpp_reader *);
32169695Skanstatic void read_original_directory (cpp_reader *);
33169695Skanstatic void post_options (cpp_reader *);
34169695Skan
35169695Skan/* If we have designated initializers (GCC >2.7) these tables can be
36169695Skan   initialized, constant data.  Otherwise, they have to be filled in at
37169695Skan   runtime.  */
38169695Skan#if HAVE_DESIGNATED_INITIALIZERS
39169695Skan
40169695Skan#define init_trigraph_map()  /* Nothing.  */
41169695Skan#define TRIGRAPH_MAP \
42169695Skan__extension__ const uchar _cpp_trigraph_map[UCHAR_MAX + 1] = {
43169695Skan
44169695Skan#define END };
45169695Skan#define s(p, v) [p] = v,
46169695Skan
47169695Skan#else
48169695Skan
49169695Skan#define TRIGRAPH_MAP uchar _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
50169695Skan static void init_trigraph_map (void) { \
51169695Skan unsigned char *x = _cpp_trigraph_map;
52169695Skan
53169695Skan#define END }
54169695Skan#define s(p, v) x[p] = v;
55169695Skan
56169695Skan#endif
57169695Skan
58169695SkanTRIGRAPH_MAP
59169695Skan  s('=', '#')	s(')', ']')	s('!', '|')
60169695Skan  s('(', '[')	s('\'', '^')	s('>', '}')
61169695Skan  s('/', '\\')	s('<', '{')	s('-', '~')
62169695SkanEND
63169695Skan
64169695Skan#undef s
65169695Skan#undef END
66169695Skan#undef TRIGRAPH_MAP
67169695Skan
68169695Skan/* A set of booleans indicating what CPP features each source language
69169695Skan   requires.  */
70169695Skanstruct lang_flags
71169695Skan{
72169695Skan  char c99;
73169695Skan  char cplusplus;
74169695Skan  char extended_numbers;
75169695Skan  char extended_identifiers;
76169695Skan  char std;
77169695Skan  char cplusplus_comments;
78169695Skan  char digraphs;
79169695Skan};
80169695Skan
81169695Skanstatic const struct lang_flags lang_defaults[] =
82169695Skan{ /*              c99 c++ xnum xid std  //   digr  */
83169695Skan  /* GNUC89 */  { 0,  0,  1,   0,  0,   1,   1     },
84169695Skan  /* GNUC99 */  { 1,  0,  1,   0,  0,   1,   1     },
85169695Skan  /* STDC89 */  { 0,  0,  0,   0,  1,   0,   0     },
86169695Skan  /* STDC94 */  { 0,  0,  0,   0,  1,   0,   1     },
87169695Skan  /* STDC99 */  { 1,  0,  1,   0,  1,   1,   1     },
88169695Skan  /* GNUCXX */  { 0,  1,  1,   0,  0,   1,   1     },
89169695Skan  /* CXX98  */  { 0,  1,  1,   0,  1,   1,   1     },
90169695Skan  /* ASM    */  { 0,  0,  1,   0,  0,   1,   0     }
91169695Skan  /* xid should be 1 for GNUC99, STDC99, GNUCXX and CXX98 when no
92169695Skan     longer experimental (when all uses of identifiers in the compiler
93169695Skan     have been audited for correct handling of extended
94169695Skan     identifiers).  */
95169695Skan};
96169695Skan
97169695Skan/* Sets internal flags correctly for a given language.  */
98169695Skanvoid
99169695Skancpp_set_lang (cpp_reader *pfile, enum c_lang lang)
100169695Skan{
101169695Skan  const struct lang_flags *l = &lang_defaults[(int) lang];
102169695Skan
103169695Skan  CPP_OPTION (pfile, lang) = lang;
104169695Skan
105169695Skan  CPP_OPTION (pfile, c99)			 = l->c99;
106169695Skan  CPP_OPTION (pfile, cplusplus)			 = l->cplusplus;
107169695Skan  CPP_OPTION (pfile, extended_numbers)		 = l->extended_numbers;
108169695Skan  CPP_OPTION (pfile, extended_identifiers)	 = l->extended_identifiers;
109169695Skan  CPP_OPTION (pfile, std)			 = l->std;
110169695Skan  CPP_OPTION (pfile, trigraphs)			 = l->std;
111169695Skan  CPP_OPTION (pfile, cplusplus_comments)	 = l->cplusplus_comments;
112169695Skan  CPP_OPTION (pfile, digraphs)			 = l->digraphs;
113169695Skan}
114169695Skan
115169695Skan/* Initialize library global state.  */
116169695Skanstatic void
117169695Skaninit_library (void)
118169695Skan{
119169695Skan  static int initialized = 0;
120169695Skan
121169695Skan  if (! initialized)
122169695Skan    {
123169695Skan      initialized = 1;
124169695Skan
125169695Skan      /* Set up the trigraph map.  This doesn't need to do anything if
126169695Skan	 we were compiled with a compiler that supports C99 designated
127169695Skan	 initializers.  */
128169695Skan      init_trigraph_map ();
129169695Skan
130169695Skan#ifdef ENABLE_NLS
131169695Skan       (void) bindtextdomain (PACKAGE, LOCALEDIR);
132169695Skan#endif
133169695Skan    }
134169695Skan}
135169695Skan
136169695Skan/* Initialize a cpp_reader structure.  */
137169695Skancpp_reader *
138169695Skancpp_create_reader (enum c_lang lang, hash_table *table,
139169695Skan		   struct line_maps *line_table)
140169695Skan{
141169695Skan  cpp_reader *pfile;
142169695Skan
143169695Skan  /* Initialize this instance of the library if it hasn't been already.  */
144169695Skan  init_library ();
145169695Skan
146169695Skan  pfile = XCNEW (cpp_reader);
147169695Skan
148169695Skan  cpp_set_lang (pfile, lang);
149259891Spfg  /* APPLE LOCAL begin -Wnewline-eof 2001-08-23 --sts */
150259891Spfg  /* Suppress warnings about missing newlines at ends of files.  */
151259891Spfg  CPP_OPTION (pfile, warn_newline_at_eof) = 0;
152259891Spfg  /* APPLE LOCAL end -Wnewline-eof 2001-08-23 --sts */
153169695Skan  CPP_OPTION (pfile, warn_multichar) = 1;
154169695Skan  CPP_OPTION (pfile, discard_comments) = 1;
155169695Skan  CPP_OPTION (pfile, discard_comments_in_macro_exp) = 1;
156169695Skan  CPP_OPTION (pfile, show_column) = 1;
157169695Skan  CPP_OPTION (pfile, tabstop) = 8;
158169695Skan  CPP_OPTION (pfile, operator_names) = 1;
159169695Skan  CPP_OPTION (pfile, warn_trigraphs) = 2;
160169695Skan  CPP_OPTION (pfile, warn_endif_labels) = 1;
161169695Skan  CPP_OPTION (pfile, warn_deprecated) = 1;
162169695Skan  CPP_OPTION (pfile, warn_long_long) = !CPP_OPTION (pfile, c99);
163169695Skan  CPP_OPTION (pfile, dollars_in_ident) = 1;
164169695Skan  CPP_OPTION (pfile, warn_dollars) = 1;
165169695Skan  CPP_OPTION (pfile, warn_variadic_macros) = 1;
166169695Skan  CPP_OPTION (pfile, warn_normalize) = normalized_C;
167169695Skan
168169695Skan  /* Default CPP arithmetic to something sensible for the host for the
169169695Skan     benefit of dumb users like fix-header.  */
170169695Skan  CPP_OPTION (pfile, precision) = CHAR_BIT * sizeof (long);
171169695Skan  CPP_OPTION (pfile, char_precision) = CHAR_BIT;
172169695Skan  CPP_OPTION (pfile, wchar_precision) = CHAR_BIT * sizeof (int);
173169695Skan  CPP_OPTION (pfile, int_precision) = CHAR_BIT * sizeof (int);
174169695Skan  CPP_OPTION (pfile, unsigned_char) = 0;
175169695Skan  CPP_OPTION (pfile, unsigned_wchar) = 1;
176169695Skan  CPP_OPTION (pfile, bytes_big_endian) = 1;  /* does not matter */
177169695Skan
178169695Skan  /* Default to no charset conversion.  */
179169695Skan  CPP_OPTION (pfile, narrow_charset) = _cpp_default_encoding ();
180169695Skan  CPP_OPTION (pfile, wide_charset) = 0;
181169695Skan
182169695Skan  /* Default the input character set to UTF-8.  */
183169695Skan  CPP_OPTION (pfile, input_charset) = _cpp_default_encoding ();
184169695Skan
185169695Skan  /* A fake empty "directory" used as the starting point for files
186169695Skan     looked up without a search path.  Name cannot be '/' because we
187169695Skan     don't want to prepend anything at all to filenames using it.  All
188169695Skan     other entries are correct zero-initialized.  */
189169695Skan  pfile->no_search_path.name = (char *) "";
190169695Skan
191169695Skan  /* Initialize the line map.  */
192169695Skan  pfile->line_table = line_table;
193169695Skan
194169695Skan  /* Initialize lexer state.  */
195169695Skan  pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
196169695Skan
197169695Skan  /* Set up static tokens.  */
198169695Skan  pfile->avoid_paste.type = CPP_PADDING;
199169695Skan  pfile->avoid_paste.val.source = NULL;
200169695Skan  pfile->eof.type = CPP_EOF;
201169695Skan  pfile->eof.flags = 0;
202169695Skan
203169695Skan  /* Create a token buffer for the lexer.  */
204169695Skan  _cpp_init_tokenrun (&pfile->base_run, 250);
205169695Skan  pfile->cur_run = &pfile->base_run;
206169695Skan  pfile->cur_token = pfile->base_run.base;
207169695Skan
208169695Skan  /* Initialize the base context.  */
209169695Skan  pfile->context = &pfile->base_context;
210169695Skan  pfile->base_context.macro = 0;
211169695Skan  pfile->base_context.prev = pfile->base_context.next = 0;
212169695Skan
213169695Skan  /* Aligned and unaligned storage.  */
214169695Skan  pfile->a_buff = _cpp_get_buff (pfile, 0);
215169695Skan  pfile->u_buff = _cpp_get_buff (pfile, 0);
216169695Skan
217169695Skan  /* The expression parser stack.  */
218169695Skan  _cpp_expand_op_stack (pfile);
219169695Skan
220169695Skan  /* Initialize the buffer obstack.  */
221169695Skan  _obstack_begin (&pfile->buffer_ob, 0, 0,
222169695Skan		  (void *(*) (long)) xmalloc,
223169695Skan		  (void (*) (void *)) free);
224169695Skan
225169695Skan  _cpp_init_files (pfile);
226169695Skan
227169695Skan  _cpp_init_hashtable (pfile, table);
228169695Skan
229169695Skan  return pfile;
230169695Skan}
231169695Skan
232169695Skan/* Free resources used by PFILE.  Accessing PFILE after this function
233169695Skan   returns leads to undefined behavior.  Returns the error count.  */
234169695Skanvoid
235169695Skancpp_destroy (cpp_reader *pfile)
236169695Skan{
237169695Skan  cpp_context *context, *contextn;
238169695Skan  tokenrun *run, *runn;
239169695Skan
240169695Skan  free (pfile->op_stack);
241169695Skan
242169695Skan  while (CPP_BUFFER (pfile) != NULL)
243169695Skan    _cpp_pop_buffer (pfile);
244169695Skan
245169695Skan  if (pfile->out.base)
246169695Skan    free (pfile->out.base);
247169695Skan
248169695Skan  if (pfile->macro_buffer)
249169695Skan    {
250169695Skan      free (pfile->macro_buffer);
251169695Skan      pfile->macro_buffer = NULL;
252169695Skan      pfile->macro_buffer_len = 0;
253169695Skan    }
254169695Skan
255169695Skan  if (pfile->deps)
256169695Skan    deps_free (pfile->deps);
257169695Skan  obstack_free (&pfile->buffer_ob, 0);
258169695Skan
259169695Skan  _cpp_destroy_hashtable (pfile);
260169695Skan  _cpp_cleanup_files (pfile);
261169695Skan  _cpp_destroy_iconv (pfile);
262169695Skan
263169695Skan  _cpp_free_buff (pfile->a_buff);
264169695Skan  _cpp_free_buff (pfile->u_buff);
265169695Skan  _cpp_free_buff (pfile->free_buffs);
266169695Skan
267169695Skan  for (run = &pfile->base_run; run; run = runn)
268169695Skan    {
269169695Skan      runn = run->next;
270169695Skan      free (run->base);
271169695Skan      if (run != &pfile->base_run)
272169695Skan	free (run);
273169695Skan    }
274169695Skan
275169695Skan  for (context = pfile->base_context.next; context; context = contextn)
276169695Skan    {
277169695Skan      contextn = context->next;
278169695Skan      free (context);
279169695Skan    }
280169695Skan
281169695Skan  free (pfile);
282169695Skan}
283169695Skan
284169695Skan/* This structure defines one built-in identifier.  A node will be
285169695Skan   entered in the hash table under the name NAME, with value VALUE.
286169695Skan
287169695Skan   There are two tables of these.  builtin_array holds all the
288169695Skan   "builtin" macros: these are handled by builtin_macro() in
289169695Skan   macro.c.  Builtin is somewhat of a misnomer -- the property of
290169695Skan   interest is that these macros require special code to compute their
291169695Skan   expansions.  The value is a "builtin_type" enumerator.
292169695Skan
293169695Skan   operator_array holds the C++ named operators.  These are keywords
294169695Skan   which act as aliases for punctuators.  In C++, they cannot be
295169695Skan   altered through #define, and #if recognizes them as operators.  In
296169695Skan   C, these are not entered into the hash table at all (but see
297169695Skan   <iso646.h>).  The value is a token-type enumerator.  */
298169695Skanstruct builtin
299169695Skan{
300169695Skan  const uchar *name;
301169695Skan  unsigned short len;
302169695Skan  unsigned short value;
303169695Skan};
304169695Skan
305169695Skan#define B(n, t)    { DSC(n), t }
306169695Skanstatic const struct builtin builtin_array[] =
307169695Skan{
308169695Skan  B("__TIMESTAMP__",	 BT_TIMESTAMP),
309169695Skan  B("__TIME__",		 BT_TIME),
310169695Skan  B("__DATE__",		 BT_DATE),
311169695Skan  B("__FILE__",		 BT_FILE),
312169695Skan  B("__BASE_FILE__",	 BT_BASE_FILE),
313169695Skan  B("__LINE__",		 BT_SPECLINE),
314169695Skan  B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL),
315231289Sed  B("__COUNTER__",	 BT_COUNTER),
316169695Skan  /* Keep builtins not used for -traditional-cpp at the end, and
317169695Skan     update init_builtins() if any more are added.  */
318169695Skan  B("_Pragma",		 BT_PRAGMA),
319169695Skan  B("__STDC__",		 BT_STDC),
320169695Skan};
321169695Skan
322169695Skanstatic const struct builtin operator_array[] =
323169695Skan{
324169695Skan  B("and",	CPP_AND_AND),
325169695Skan  B("and_eq",	CPP_AND_EQ),
326169695Skan  B("bitand",	CPP_AND),
327169695Skan  B("bitor",	CPP_OR),
328169695Skan  B("compl",	CPP_COMPL),
329169695Skan  B("not",	CPP_NOT),
330169695Skan  B("not_eq",	CPP_NOT_EQ),
331169695Skan  B("or",	CPP_OR_OR),
332169695Skan  B("or_eq",	CPP_OR_EQ),
333169695Skan  B("xor",	CPP_XOR),
334169695Skan  B("xor_eq",	CPP_XOR_EQ)
335169695Skan};
336169695Skan#undef B
337169695Skan
338169695Skan/* Mark the C++ named operators in the hash table.  */
339169695Skanstatic void
340169695Skanmark_named_operators (cpp_reader *pfile)
341169695Skan{
342169695Skan  const struct builtin *b;
343169695Skan
344169695Skan  for (b = operator_array;
345169695Skan       b < (operator_array + ARRAY_SIZE (operator_array));
346169695Skan       b++)
347169695Skan    {
348169695Skan      cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
349169695Skan      hp->flags |= NODE_OPERATOR;
350169695Skan      hp->is_directive = 0;
351169695Skan      hp->directive_index = b->value;
352169695Skan    }
353169695Skan}
354169695Skan
355169695Skanvoid
356259406Spfgcpp_init_special_builtins (cpp_reader *pfile)
357169695Skan{
358169695Skan  const struct builtin *b;
359169695Skan  size_t n = ARRAY_SIZE (builtin_array);
360169695Skan
361169695Skan  if (CPP_OPTION (pfile, traditional))
362169695Skan    n -= 2;
363169695Skan  else if (! CPP_OPTION (pfile, stdc_0_in_system_headers)
364169695Skan	   || CPP_OPTION (pfile, std))
365259406Spfg    n--;
366169695Skan
367169695Skan  for (b = builtin_array; b < builtin_array + n; b++)
368169695Skan    {
369169695Skan      cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
370169695Skan      hp->type = NT_MACRO;
371169695Skan      hp->flags |= NODE_BUILTIN | NODE_WARN;
372169695Skan      hp->value.builtin = (enum builtin_type) b->value;
373169695Skan    }
374259406Spfg}
375169695Skan
376259406Spfg/* Read the builtins table above and enter them, and language-specific
377259406Spfg   macros, into the hash table.  HOSTED is true if this is a hosted
378259406Spfg   environment.  */
379259406Spfgvoid
380259406Spfgcpp_init_builtins (cpp_reader *pfile, int hosted)
381259406Spfg{
382259406Spfg  cpp_init_special_builtins (pfile);
383259406Spfg
384259406Spfg  if (!CPP_OPTION (pfile, traditional)
385259406Spfg      && (! CPP_OPTION (pfile, stdc_0_in_system_headers)
386259406Spfg	  || CPP_OPTION (pfile, std)))
387259406Spfg    _cpp_define_builtin (pfile, "__STDC__ 1");
388259406Spfg
389169695Skan  if (CPP_OPTION (pfile, cplusplus))
390169695Skan    _cpp_define_builtin (pfile, "__cplusplus 1");
391169695Skan  else if (CPP_OPTION (pfile, lang) == CLK_ASM)
392169695Skan    _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
393169695Skan  else if (CPP_OPTION (pfile, lang) == CLK_STDC94)
394169695Skan    _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
395169695Skan  else if (CPP_OPTION (pfile, c99))
396169695Skan    _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
397169695Skan
398169695Skan  if (hosted)
399169695Skan    _cpp_define_builtin (pfile, "__STDC_HOSTED__ 1");
400169695Skan  else
401169695Skan    _cpp_define_builtin (pfile, "__STDC_HOSTED__ 0");
402169695Skan
403169695Skan  if (CPP_OPTION (pfile, objc))
404169695Skan    _cpp_define_builtin (pfile, "__OBJC__ 1");
405169695Skan}
406169695Skan
407169695Skan/* Sanity-checks are dependent on command-line options, so it is
408169695Skan   called as a subroutine of cpp_read_main_file ().  */
409169695Skan#if ENABLE_CHECKING
410169695Skanstatic void sanity_checks (cpp_reader *);
411169695Skanstatic void sanity_checks (cpp_reader *pfile)
412169695Skan{
413169695Skan  cppchar_t test = 0;
414169695Skan  size_t max_precision = 2 * CHAR_BIT * sizeof (cpp_num_part);
415169695Skan
416169695Skan  /* Sanity checks for assumptions about CPP arithmetic and target
417169695Skan     type precisions made by cpplib.  */
418169695Skan  test--;
419169695Skan  if (test < 1)
420169695Skan    cpp_error (pfile, CPP_DL_ICE, "cppchar_t must be an unsigned type");
421169695Skan
422169695Skan  if (CPP_OPTION (pfile, precision) > max_precision)
423169695Skan    cpp_error (pfile, CPP_DL_ICE,
424169695Skan	       "preprocessor arithmetic has maximum precision of %lu bits;"
425169695Skan	       " target requires %lu bits",
426169695Skan	       (unsigned long) max_precision,
427169695Skan	       (unsigned long) CPP_OPTION (pfile, precision));
428169695Skan
429169695Skan  if (CPP_OPTION (pfile, precision) < CPP_OPTION (pfile, int_precision))
430169695Skan    cpp_error (pfile, CPP_DL_ICE,
431169695Skan	       "CPP arithmetic must be at least as precise as a target int");
432169695Skan
433169695Skan  if (CPP_OPTION (pfile, char_precision) < 8)
434169695Skan    cpp_error (pfile, CPP_DL_ICE, "target char is less than 8 bits wide");
435169695Skan
436169695Skan  if (CPP_OPTION (pfile, wchar_precision) < CPP_OPTION (pfile, char_precision))
437169695Skan    cpp_error (pfile, CPP_DL_ICE,
438169695Skan	       "target wchar_t is narrower than target char");
439169695Skan
440169695Skan  if (CPP_OPTION (pfile, int_precision) < CPP_OPTION (pfile, char_precision))
441169695Skan    cpp_error (pfile, CPP_DL_ICE,
442169695Skan	       "target int is narrower than target char");
443169695Skan
444169695Skan  /* This is assumed in eval_token() and could be fixed if necessary.  */
445169695Skan  if (sizeof (cppchar_t) > sizeof (cpp_num_part))
446169695Skan    cpp_error (pfile, CPP_DL_ICE,
447169695Skan	       "CPP half-integer narrower than CPP character");
448169695Skan
449169695Skan  if (CPP_OPTION (pfile, wchar_precision) > BITS_PER_CPPCHAR_T)
450169695Skan    cpp_error (pfile, CPP_DL_ICE,
451169695Skan	       "CPP on this host cannot handle wide character constants over"
452169695Skan	       " %lu bits, but the target requires %lu bits",
453169695Skan	       (unsigned long) BITS_PER_CPPCHAR_T,
454169695Skan	       (unsigned long) CPP_OPTION (pfile, wchar_precision));
455169695Skan}
456169695Skan#else
457169695Skan# define sanity_checks(PFILE)
458169695Skan#endif
459169695Skan
460169695Skan/* This is called after options have been parsed, and partially
461169695Skan   processed.  */
462169695Skanvoid
463169695Skancpp_post_options (cpp_reader *pfile)
464169695Skan{
465169695Skan  sanity_checks (pfile);
466169695Skan
467169695Skan  post_options (pfile);
468169695Skan
469169695Skan  /* Mark named operators before handling command line macros.  */
470169695Skan  if (CPP_OPTION (pfile, cplusplus) && CPP_OPTION (pfile, operator_names))
471169695Skan    mark_named_operators (pfile);
472169695Skan}
473169695Skan
474169695Skan/* Setup for processing input from the file named FNAME, or stdin if
475169695Skan   it is the empty string.  Return the original filename
476169695Skan   on success (e.g. foo.i->foo.c), or NULL on failure.  */
477169695Skanconst char *
478169695Skancpp_read_main_file (cpp_reader *pfile, const char *fname)
479169695Skan{
480169695Skan  if (CPP_OPTION (pfile, deps.style) != DEPS_NONE)
481169695Skan    {
482169695Skan      if (!pfile->deps)
483169695Skan	pfile->deps = deps_init ();
484169695Skan
485169695Skan      /* Set the default target (if there is none already).  */
486169695Skan      deps_add_default_target (pfile->deps, fname);
487169695Skan    }
488169695Skan
489169695Skan  pfile->main_file
490169695Skan    = _cpp_find_file (pfile, fname, &pfile->no_search_path, false, 0);
491169695Skan  if (_cpp_find_failed (pfile->main_file))
492169695Skan    return NULL;
493169695Skan
494169695Skan  _cpp_stack_file (pfile, pfile->main_file, false);
495169695Skan
496169695Skan  /* For foo.i, read the original filename foo.c now, for the benefit
497169695Skan     of the front ends.  */
498169695Skan  if (CPP_OPTION (pfile, preprocessed))
499169695Skan    {
500169695Skan      read_original_filename (pfile);
501169695Skan      fname = pfile->line_table->maps[pfile->line_table->used-1].to_file;
502169695Skan    }
503169695Skan  return fname;
504169695Skan}
505169695Skan
506169695Skan/* For preprocessed files, if the first tokens are of the form # NUM.
507169695Skan   handle the directive so we know the original file name.  This will
508169695Skan   generate file_change callbacks, which the front ends must handle
509169695Skan   appropriately given their state of initialization.  */
510169695Skanstatic void
511169695Skanread_original_filename (cpp_reader *pfile)
512169695Skan{
513169695Skan  const cpp_token *token, *token1;
514169695Skan
515169695Skan  /* Lex ahead; if the first tokens are of the form # NUM, then
516169695Skan     process the directive, otherwise back up.  */
517169695Skan  token = _cpp_lex_direct (pfile);
518169695Skan  if (token->type == CPP_HASH)
519169695Skan    {
520169695Skan      pfile->state.in_directive = 1;
521169695Skan      token1 = _cpp_lex_direct (pfile);
522169695Skan      _cpp_backup_tokens (pfile, 1);
523169695Skan      pfile->state.in_directive = 0;
524169695Skan
525169695Skan      /* If it's a #line directive, handle it.  */
526169695Skan      if (token1->type == CPP_NUMBER)
527169695Skan	{
528169695Skan	  _cpp_handle_directive (pfile, token->flags & PREV_WHITE);
529169695Skan	  read_original_directory (pfile);
530169695Skan	  return;
531169695Skan	}
532169695Skan    }
533169695Skan
534169695Skan  /* Backup as if nothing happened.  */
535169695Skan  _cpp_backup_tokens (pfile, 1);
536169695Skan}
537169695Skan
538169695Skan/* For preprocessed files, if the tokens following the first filename
539169695Skan   line is of the form # <line> "/path/name//", handle the
540169695Skan   directive so we know the original current directory.  */
541169695Skanstatic void
542169695Skanread_original_directory (cpp_reader *pfile)
543169695Skan{
544169695Skan  const cpp_token *hash, *token;
545169695Skan
546169695Skan  /* Lex ahead; if the first tokens are of the form # NUM, then
547169695Skan     process the directive, otherwise back up.  */
548169695Skan  hash = _cpp_lex_direct (pfile);
549169695Skan  if (hash->type != CPP_HASH)
550169695Skan    {
551169695Skan      _cpp_backup_tokens (pfile, 1);
552169695Skan      return;
553169695Skan    }
554169695Skan
555169695Skan  token = _cpp_lex_direct (pfile);
556169695Skan
557169695Skan  if (token->type != CPP_NUMBER)
558169695Skan    {
559169695Skan      _cpp_backup_tokens (pfile, 2);
560169695Skan      return;
561169695Skan    }
562169695Skan
563169695Skan  token = _cpp_lex_direct (pfile);
564169695Skan
565169695Skan  if (token->type != CPP_STRING
566169695Skan      || ! (token->val.str.len >= 5
567169695Skan	    && token->val.str.text[token->val.str.len-2] == '/'
568169695Skan	    && token->val.str.text[token->val.str.len-3] == '/'))
569169695Skan    {
570169695Skan      _cpp_backup_tokens (pfile, 3);
571169695Skan      return;
572169695Skan    }
573169695Skan
574169695Skan  if (pfile->cb.dir_change)
575169695Skan    {
576169695Skan      char *debugdir = (char *) alloca (token->val.str.len - 3);
577169695Skan
578169695Skan      memcpy (debugdir, (const char *) token->val.str.text + 1,
579169695Skan	      token->val.str.len - 4);
580169695Skan      debugdir[token->val.str.len - 4] = '\0';
581169695Skan
582169695Skan      pfile->cb.dir_change (pfile, debugdir);
583169695Skan    }
584169695Skan}
585169695Skan
586169695Skan/* This is called at the end of preprocessing.  It pops the last
587169695Skan   buffer and writes dependency output, and returns the number of
588169695Skan   errors.
589169695Skan
590169695Skan   Maybe it should also reset state, such that you could call
591169695Skan   cpp_start_read with a new filename to restart processing.  */
592169695Skanint
593169695Skancpp_finish (cpp_reader *pfile, FILE *deps_stream)
594169695Skan{
595169695Skan  /* Warn about unused macros before popping the final buffer.  */
596169695Skan  if (CPP_OPTION (pfile, warn_unused_macros))
597169695Skan    cpp_forall_identifiers (pfile, _cpp_warn_if_unused_macro, NULL);
598169695Skan
599169695Skan  /* lex.c leaves the final buffer on the stack.  This it so that
600169695Skan     it returns an unending stream of CPP_EOFs to the client.  If we
601169695Skan     popped the buffer, we'd dereference a NULL buffer pointer and
602169695Skan     segfault.  It's nice to allow the client to do worry-free excess
603169695Skan     cpp_get_token calls.  */
604169695Skan  while (pfile->buffer)
605169695Skan    _cpp_pop_buffer (pfile);
606169695Skan
607169695Skan  /* Don't write the deps file if there are errors.  */
608169695Skan  if (CPP_OPTION (pfile, deps.style) != DEPS_NONE
609169695Skan      && deps_stream && pfile->errors == 0)
610169695Skan    {
611169695Skan      deps_write (pfile->deps, deps_stream, 72);
612169695Skan
613169695Skan      if (CPP_OPTION (pfile, deps.phony_targets))
614169695Skan	deps_phony_targets (pfile->deps, deps_stream);
615169695Skan    }
616169695Skan
617169695Skan  /* Report on headers that could use multiple include guards.  */
618169695Skan  if (CPP_OPTION (pfile, print_include_names))
619169695Skan    _cpp_report_missing_guards (pfile);
620169695Skan
621169695Skan  return pfile->errors;
622169695Skan}
623169695Skan
624169695Skanstatic void
625169695Skanpost_options (cpp_reader *pfile)
626169695Skan{
627169695Skan  /* -Wtraditional is not useful in C++ mode.  */
628169695Skan  if (CPP_OPTION (pfile, cplusplus))
629169695Skan    CPP_OPTION (pfile, warn_traditional) = 0;
630169695Skan
631169695Skan  /* Permanently disable macro expansion if we are rescanning
632169695Skan     preprocessed text.  Read preprocesed source in ISO mode.  */
633169695Skan  if (CPP_OPTION (pfile, preprocessed))
634169695Skan    {
635259406Spfg      if (!CPP_OPTION (pfile, directives_only))
636259406Spfg	pfile->state.prevent_expansion = 1;
637169695Skan      CPP_OPTION (pfile, traditional) = 0;
638169695Skan    }
639169695Skan
640169695Skan  if (CPP_OPTION (pfile, warn_trigraphs) == 2)
641169695Skan    CPP_OPTION (pfile, warn_trigraphs) = !CPP_OPTION (pfile, trigraphs);
642169695Skan
643169695Skan  if (CPP_OPTION (pfile, traditional))
644169695Skan    {
645169695Skan      CPP_OPTION (pfile, cplusplus_comments) = 0;
646169695Skan
647169695Skan      /* Traditional CPP does not accurately track column information.  */
648169695Skan      CPP_OPTION (pfile, show_column) = 0;
649169695Skan      CPP_OPTION (pfile, trigraphs) = 0;
650169695Skan      CPP_OPTION (pfile, warn_trigraphs) = 0;
651169695Skan    }
652169695Skan}
653