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