1/* Preprocess only, using cpplib.
2   Copyright (C) 1995-2015 Free Software Foundation, Inc.
3   Written by Per Bothner, 1994-95.
4
5   This program is free software; you can redistribute it and/or modify it
6   under the terms of the GNU General Public License as published by the
7   Free Software Foundation; either version 3, or (at your option) any
8   later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program; see the file COPYING3.  If not see
17   <http://www.gnu.org/licenses/>.  */
18
19#include "config.h"
20#include "system.h"
21#include "coretypes.h"
22#include "cpplib.h"
23#include "../libcpp/internal.h"
24#include "hash-set.h"
25#include "machmode.h"
26#include "vec.h"
27#include "double-int.h"
28#include "input.h"
29#include "alias.h"
30#include "symtab.h"
31#include "options.h"
32#include "wide-int.h"
33#include "inchash.h"
34#include "tree.h"
35#include "c-common.h"		/* For flags.  */
36#include "c-pragma.h"		/* For parse_in.  */
37
38/* Encapsulates state used to convert a stream of tokens into a text
39   file.  */
40static struct
41{
42  FILE *outf;			/* Stream to write to.  */
43  const cpp_token *prev;	/* Previous token.  */
44  const cpp_token *source;	/* Source token for spacing.  */
45  int src_line;			/* Line number currently being written.  */
46  bool printed;			/* True if something output at line.  */
47  bool first_time;		/* pp_file_change hasn't been called yet.  */
48  bool prev_was_system_token;	/* True if the previous token was a
49				   system token.*/
50  const char *src_file;		/* Current source file.  */
51} print;
52
53/* Defined and undefined macros being queued for output with -dU at
54   the next newline.  */
55typedef struct macro_queue
56{
57  struct macro_queue *next;	/* Next macro in the list.  */
58  char *macro;			/* The name of the macro if not
59				   defined, the full definition if
60				   defined.  */
61} macro_queue;
62static macro_queue *define_queue, *undef_queue;
63
64/* General output routines.  */
65static void scan_translation_unit (cpp_reader *);
66static void print_lines_directives_only (int, const void *, size_t);
67static void scan_translation_unit_directives_only (cpp_reader *);
68static void scan_translation_unit_trad (cpp_reader *);
69static void account_for_newlines (const unsigned char *, size_t);
70static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
71static void dump_queued_macros (cpp_reader *);
72
73static bool print_line_1 (source_location, const char*, FILE *);
74static bool print_line (source_location, const char *);
75static bool maybe_print_line_1 (source_location, FILE *);
76static bool maybe_print_line (source_location);
77static bool do_line_change (cpp_reader *, const cpp_token *,
78			    source_location, int);
79
80/* Callback routines for the parser.   Most of these are active only
81   in specific modes.  */
82static void cb_line_change (cpp_reader *, const cpp_token *, int);
83static void cb_define (cpp_reader *, source_location, cpp_hashnode *);
84static void cb_undef (cpp_reader *, source_location, cpp_hashnode *);
85static void cb_used_define (cpp_reader *, source_location, cpp_hashnode *);
86static void cb_used_undef (cpp_reader *, source_location, cpp_hashnode *);
87static void cb_include (cpp_reader *, source_location, const unsigned char *,
88			const char *, int, const cpp_token **);
89static void cb_ident (cpp_reader *, source_location, const cpp_string *);
90static void cb_def_pragma (cpp_reader *, source_location);
91static void cb_read_pch (cpp_reader *pfile, const char *name,
92			 int fd, const char *orig_name);
93
94/* Preprocess and output.  */
95void
96preprocess_file (cpp_reader *pfile)
97{
98  /* A successful cpp_read_main_file guarantees that we can call
99     cpp_scan_nooutput or cpp_get_token next.  */
100  if (flag_no_output && pfile->buffer)
101    {
102      /* Scan -included buffers, then the main file.  */
103      while (pfile->buffer->prev)
104	cpp_scan_nooutput (pfile);
105      cpp_scan_nooutput (pfile);
106    }
107  else if (cpp_get_options (pfile)->traditional)
108    scan_translation_unit_trad (pfile);
109  else if (cpp_get_options (pfile)->directives_only
110	   && !cpp_get_options (pfile)->preprocessed)
111    scan_translation_unit_directives_only (pfile);
112  else
113    scan_translation_unit (pfile);
114
115  /* -dM command line option.  Should this be elsewhere?  */
116  if (flag_dump_macros == 'M')
117    cpp_forall_identifiers (pfile, dump_macro, NULL);
118
119  /* Flush any pending output.  */
120  if (print.printed)
121    putc ('\n', print.outf);
122}
123
124/* Set up the callbacks as appropriate.  */
125void
126init_pp_output (FILE *out_stream)
127{
128  cpp_callbacks *cb = cpp_get_callbacks (parse_in);
129
130  if (!flag_no_output)
131    {
132      cb->line_change = cb_line_change;
133      /* Don't emit #pragma or #ident directives if we are processing
134	 assembly language; the assembler may choke on them.  */
135      if (cpp_get_options (parse_in)->lang != CLK_ASM)
136	{
137	  cb->ident      = cb_ident;
138	  cb->def_pragma = cb_def_pragma;
139	}
140    }
141
142  if (flag_dump_includes)
143    cb->include  = cb_include;
144
145  if (flag_pch_preprocess)
146    {
147      cb->valid_pch = c_common_valid_pch;
148      cb->read_pch = cb_read_pch;
149    }
150
151  if (flag_dump_macros == 'N' || flag_dump_macros == 'D')
152    {
153      cb->define = cb_define;
154      cb->undef  = cb_undef;
155    }
156
157  if (flag_dump_macros == 'U')
158    {
159      cb->before_define = dump_queued_macros;
160      cb->used_define = cb_used_define;
161      cb->used_undef = cb_used_undef;
162    }
163
164  cb->has_attribute = c_common_has_attribute;
165
166  /* Initialize the print structure.  */
167  print.src_line = 1;
168  print.printed = false;
169  print.prev = 0;
170  print.outf = out_stream;
171  print.first_time = 1;
172  print.src_file = "";
173  print.prev_was_system_token = false;
174}
175
176/* Writes out the preprocessed file, handling spacing and paste
177   avoidance issues.  */
178static void
179scan_translation_unit (cpp_reader *pfile)
180{
181  bool avoid_paste = false;
182  bool do_line_adjustments
183    = cpp_get_options (parse_in)->lang != CLK_ASM
184      && !flag_no_line_commands;
185  bool in_pragma = false;
186  bool line_marker_emitted = false;
187
188  print.source = NULL;
189  for (;;)
190    {
191      source_location loc;
192      const cpp_token *token = cpp_get_token_with_location (pfile, &loc);
193
194      if (token->type == CPP_PADDING)
195	{
196	  avoid_paste = true;
197	  if (print.source == NULL
198	      || (!(print.source->flags & PREV_WHITE)
199		  && token->val.source == NULL))
200	    print.source = token->val.source;
201	  continue;
202	}
203
204      if (token->type == CPP_EOF)
205	break;
206
207      /* Subtle logic to output a space if and only if necessary.  */
208      if (avoid_paste)
209	{
210	  int src_line = LOCATION_LINE (loc);
211
212	  if (print.source == NULL)
213	    print.source = token;
214
215	  if (src_line != print.src_line
216	      && do_line_adjustments
217	      && !in_pragma)
218	    {
219	      line_marker_emitted = do_line_change (pfile, token, loc, false);
220	      putc (' ', print.outf);
221	      print.printed = true;
222	    }
223	  else if (print.source->flags & PREV_WHITE
224		   || (print.prev
225		       && cpp_avoid_paste (pfile, print.prev, token))
226		   || (print.prev == NULL && token->type == CPP_HASH))
227	    {
228	      putc (' ', print.outf);
229	      print.printed = true;
230	    }
231	}
232      else if (token->flags & PREV_WHITE)
233	{
234	  int src_line = LOCATION_LINE (loc);
235
236	  if (src_line != print.src_line
237	      && do_line_adjustments
238	      && !in_pragma)
239	    line_marker_emitted = do_line_change (pfile, token, loc, false);
240	  putc (' ', print.outf);
241	  print.printed = true;
242	}
243
244      avoid_paste = false;
245      print.source = NULL;
246      print.prev = token;
247      if (token->type == CPP_PRAGMA)
248	{
249	  const char *space;
250	  const char *name;
251
252	  line_marker_emitted = maybe_print_line (token->src_loc);
253	  fputs ("#pragma ", print.outf);
254	  c_pp_lookup_pragma (token->val.pragma, &space, &name);
255	  if (space)
256	    fprintf (print.outf, "%s %s", space, name);
257	  else
258	    fprintf (print.outf, "%s", name);
259	  print.printed = true;
260	  in_pragma = true;
261	}
262      else if (token->type == CPP_PRAGMA_EOL)
263	{
264	  maybe_print_line (token->src_loc);
265	  in_pragma = false;
266	}
267      else
268	{
269	  if (cpp_get_options (parse_in)->debug)
270	    linemap_dump_location (line_table, token->src_loc, print.outf);
271
272	  if (do_line_adjustments
273	      && !in_pragma
274	      && !line_marker_emitted
275	      && print.prev_was_system_token != !!in_system_header_at (loc)
276	      && !is_location_from_builtin_token (loc))
277	    /* The system-ness of this token is different from the one
278	       of the previous token.  Let's emit a line change to
279	       mark the new system-ness before we emit the token.  */
280	    {
281	      do_line_change (pfile, token, loc, false);
282	      print.prev_was_system_token = !!in_system_header_at (loc);
283	    }
284	  cpp_output_token (token, print.outf);
285	  line_marker_emitted = false;
286	  print.printed = true;
287	}
288
289      /* CPP_COMMENT tokens and raw-string literal tokens can
290	 have embedded new-line characters.  Rather than enumerating
291	 all the possible token types just check if token uses
292	 val.str union member.  */
293      if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
294	account_for_newlines (token->val.str.text, token->val.str.len);
295    }
296}
297
298static void
299print_lines_directives_only (int lines, const void *buf, size_t size)
300{
301  print.src_line += lines;
302  fwrite (buf, 1, size, print.outf);
303}
304
305/* Writes out the preprocessed file, handling spacing and paste
306   avoidance issues.  */
307static void
308scan_translation_unit_directives_only (cpp_reader *pfile)
309{
310  struct _cpp_dir_only_callbacks cb;
311
312  cb.print_lines = print_lines_directives_only;
313  cb.maybe_print_line = (void (*) (source_location)) maybe_print_line;
314
315  _cpp_preprocess_dir_only (pfile, &cb);
316}
317
318/* Adjust print.src_line for newlines embedded in output.  */
319static void
320account_for_newlines (const unsigned char *str, size_t len)
321{
322  while (len--)
323    if (*str++ == '\n')
324      print.src_line++;
325}
326
327/* Writes out a traditionally preprocessed file.  */
328static void
329scan_translation_unit_trad (cpp_reader *pfile)
330{
331  while (_cpp_read_logical_line_trad (pfile))
332    {
333      size_t len = pfile->out.cur - pfile->out.base;
334      maybe_print_line (pfile->out.first_line);
335      fwrite (pfile->out.base, 1, len, print.outf);
336      print.printed = true;
337      if (!CPP_OPTION (pfile, discard_comments))
338	account_for_newlines (pfile->out.base, len);
339    }
340}
341
342/* If the token read on logical line LINE needs to be output on a
343   different line to the current one, output the required newlines or
344   a line marker.  If a line marker was emitted, return TRUE otherwise
345   return FALSE.  */
346
347static bool
348maybe_print_line_1 (source_location src_loc, FILE *stream)
349{
350  bool emitted_line_marker = false;
351  int src_line = LOCATION_LINE (src_loc);
352  const char *src_file = LOCATION_FILE (src_loc);
353
354  /* End the previous line of text.  */
355  if (print.printed)
356    {
357      putc ('\n', stream);
358      print.src_line++;
359      print.printed = false;
360    }
361
362  if (!flag_no_line_commands
363      && src_line >= print.src_line
364      && src_line < print.src_line + 8
365      && strcmp (src_file, print.src_file) == 0)
366    {
367      while (src_line > print.src_line)
368	{
369	  putc ('\n', stream);
370	  print.src_line++;
371	}
372    }
373  else
374    emitted_line_marker = print_line_1 (src_loc, "", stream);
375
376  return emitted_line_marker;
377}
378
379/* If the token read on logical line LINE needs to be output on a
380   different line to the current one, output the required newlines or
381   a line marker.  If a line marker was emitted, return TRUE otherwise
382   return FALSE.  */
383
384static bool
385maybe_print_line (source_location src_loc)
386{
387  if (cpp_get_options (parse_in)->debug)
388    linemap_dump_location (line_table, src_loc,
389			   print.outf);
390  return maybe_print_line_1 (src_loc, print.outf);
391}
392
393/* Output a line marker for logical line LINE.  Special flags are "1"
394   or "2" indicating entering or leaving a file.  If the line marker
395   was effectively emitted, return TRUE otherwise return FALSE.  */
396
397static bool
398print_line_1 (source_location src_loc, const char *special_flags, FILE *stream)
399{
400  bool emitted_line_marker = false;
401
402  /* End any previous line of text.  */
403  if (print.printed)
404    putc ('\n', stream);
405  print.printed = false;
406
407  if (!flag_no_line_commands)
408    {
409      const char *file_path = LOCATION_FILE (src_loc);
410      int sysp;
411      size_t to_file_len = strlen (file_path);
412      unsigned char *to_file_quoted =
413         (unsigned char *) alloca (to_file_len * 4 + 1);
414      unsigned char *p;
415
416      print.src_line = LOCATION_LINE (src_loc);
417      print.src_file = file_path;
418
419      /* cpp_quote_string does not nul-terminate, so we have to do it
420	 ourselves.  */
421      p = cpp_quote_string (to_file_quoted,
422			    (const unsigned char *) file_path,
423			    to_file_len);
424      *p = '\0';
425      fprintf (stream, "# %u \"%s\"%s",
426	       print.src_line == 0 ? 1 : print.src_line,
427	       to_file_quoted, special_flags);
428
429      sysp = in_system_header_at (src_loc);
430      if (sysp == 2)
431	fputs (" 3 4", stream);
432      else if (sysp == 1)
433	fputs (" 3", stream);
434
435      putc ('\n', stream);
436      emitted_line_marker = true;
437    }
438
439  return emitted_line_marker;
440}
441
442/* Output a line marker for logical line LINE.  Special flags are "1"
443   or "2" indicating entering or leaving a file.  Return TRUE if a
444   line marker was effectively emitted, FALSE otherwise.  */
445
446static bool
447print_line (source_location src_loc, const char *special_flags)
448{
449    if (cpp_get_options (parse_in)->debug)
450      linemap_dump_location (line_table, src_loc,
451			     print.outf);
452    return print_line_1 (src_loc, special_flags, print.outf);
453}
454
455/* Helper function for cb_line_change and scan_translation_unit.
456   Return TRUE if a line marker is emitted, FALSE otherwise.  */
457static bool
458do_line_change (cpp_reader *pfile, const cpp_token *token,
459		source_location src_loc, int parsing_args)
460{
461  bool emitted_line_marker = false;
462  if (define_queue || undef_queue)
463    dump_queued_macros (pfile);
464
465  if (token->type == CPP_EOF || parsing_args)
466    return false;
467
468  emitted_line_marker = maybe_print_line (src_loc);
469  print.prev = 0;
470  print.source = 0;
471
472  /* Supply enough spaces to put this token in its original column,
473     one space per column greater than 2, since scan_translation_unit
474     will provide a space if PREV_WHITE.  Don't bother trying to
475     reconstruct tabs; we can't get it right in general, and nothing
476     ought to care.  Some things do care; the fault lies with them.  */
477  if (!CPP_OPTION (pfile, traditional))
478    {
479      int spaces = LOCATION_COLUMN (src_loc) - 2;
480      print.printed = true;
481
482      while (-- spaces >= 0)
483	putc (' ', print.outf);
484    }
485
486  return emitted_line_marker;
487}
488
489/* Called when a line of output is started.  TOKEN is the first token
490   of the line, and at end of file will be CPP_EOF.  */
491static void
492cb_line_change (cpp_reader *pfile, const cpp_token *token,
493		int parsing_args)
494{
495  do_line_change (pfile, token, token->src_loc, parsing_args);
496}
497
498static void
499cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
500	  const cpp_string *str)
501{
502  maybe_print_line (line);
503  fprintf (print.outf, "#ident %s\n", str->text);
504  print.src_line++;
505}
506
507static void
508cb_define (cpp_reader *pfile, source_location line, cpp_hashnode *node)
509{
510  const struct line_map *map;
511
512  maybe_print_line (line);
513  fputs ("#define ", print.outf);
514
515  /* 'D' is whole definition; 'N' is name only.  */
516  if (flag_dump_macros == 'D')
517    fputs ((const char *) cpp_macro_definition (pfile, node),
518	   print.outf);
519  else
520    fputs ((const char *) NODE_NAME (node), print.outf);
521
522  putc ('\n', print.outf);
523  print.printed = false;
524  linemap_resolve_location (line_table, line,
525			    LRK_MACRO_DEFINITION_LOCATION,
526			    &map);
527  if (LINEMAP_LINE (map) != 0)
528    print.src_line++;
529}
530
531static void
532cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
533	  cpp_hashnode *node)
534{
535  maybe_print_line (line);
536  fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
537  print.src_line++;
538}
539
540static void
541cb_used_define (cpp_reader *pfile, source_location line ATTRIBUTE_UNUSED,
542		cpp_hashnode *node)
543{
544  macro_queue *q;
545  if (node->flags & NODE_BUILTIN)
546    return;
547  q = XNEW (macro_queue);
548  q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node));
549  q->next = define_queue;
550  define_queue = q;
551}
552
553static void
554cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED,
555	       source_location line ATTRIBUTE_UNUSED,
556	       cpp_hashnode *node)
557{
558  macro_queue *q;
559  q = XNEW (macro_queue);
560  q->macro = xstrdup ((const char *) NODE_NAME (node));
561  q->next = undef_queue;
562  undef_queue = q;
563}
564
565static void
566dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED)
567{
568  macro_queue *q;
569
570  /* End the previous line of text.  */
571  if (print.printed)
572    {
573      putc ('\n', print.outf);
574      print.src_line++;
575      print.printed = false;
576    }
577
578  for (q = define_queue; q;)
579    {
580      macro_queue *oq;
581      fputs ("#define ", print.outf);
582      fputs (q->macro, print.outf);
583      putc ('\n', print.outf);
584      print.printed = false;
585      print.src_line++;
586      oq = q;
587      q = q->next;
588      free (oq->macro);
589      free (oq);
590    }
591  define_queue = NULL;
592  for (q = undef_queue; q;)
593    {
594      macro_queue *oq;
595      fprintf (print.outf, "#undef %s\n", q->macro);
596      print.src_line++;
597      oq = q;
598      q = q->next;
599      free (oq->macro);
600      free (oq);
601    }
602  undef_queue = NULL;
603}
604
605static void
606cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
607	    const unsigned char *dir, const char *header, int angle_brackets,
608	    const cpp_token **comments)
609{
610  maybe_print_line (line);
611  if (angle_brackets)
612    fprintf (print.outf, "#%s <%s>", dir, header);
613  else
614    fprintf (print.outf, "#%s \"%s\"", dir, header);
615
616  if (comments != NULL)
617    {
618      while (*comments != NULL)
619	{
620	  if ((*comments)->flags & PREV_WHITE)
621	    putc (' ', print.outf);
622	  cpp_output_token (*comments, print.outf);
623	  ++comments;
624	}
625    }
626
627  putc ('\n', print.outf);
628  print.printed = false;
629  print.src_line++;
630}
631
632/* Callback called when -fworking-director and -E to emit working
633   directory in cpp output file.  */
634
635void
636pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
637{
638  size_t to_file_len = strlen (dir);
639  unsigned char *to_file_quoted =
640     (unsigned char *) alloca (to_file_len * 4 + 1);
641  unsigned char *p;
642
643  /* cpp_quote_string does not nul-terminate, so we have to do it ourselves.  */
644  p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len);
645  *p = '\0';
646  fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
647}
648
649/* The file name, line number or system header flags have changed, as
650   described in MAP.  */
651
652void
653pp_file_change (const struct line_map *map)
654{
655  const char *flags = "";
656
657  if (flag_no_line_commands)
658    return;
659
660  if (map != NULL)
661    {
662      input_location = map->start_location;
663      if (print.first_time)
664	{
665	  /* Avoid printing foo.i when the main file is foo.c.  */
666	  if (!cpp_get_options (parse_in)->preprocessed)
667	    print_line (map->start_location, flags);
668	  print.first_time = 0;
669	}
670      else
671	{
672	  /* Bring current file to correct line when entering a new file.  */
673	  if (map->reason == LC_ENTER)
674	    {
675	      const struct line_map *from = INCLUDED_FROM (line_table, map);
676	      maybe_print_line (LAST_SOURCE_LINE_LOCATION (from));
677	    }
678	  if (map->reason == LC_ENTER)
679	    flags = " 1";
680	  else if (map->reason == LC_LEAVE)
681	    flags = " 2";
682	  print_line (map->start_location, flags);
683	}
684    }
685}
686
687/* Copy a #pragma directive to the preprocessed output.  */
688static void
689cb_def_pragma (cpp_reader *pfile, source_location line)
690{
691  maybe_print_line (line);
692  fputs ("#pragma ", print.outf);
693  cpp_output_line (pfile, print.outf);
694  print.printed = false;
695  print.src_line++;
696}
697
698/* Dump out the hash table.  */
699static int
700dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
701{
702  if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
703    {
704      fputs ("#define ", print.outf);
705      fputs ((const char *) cpp_macro_definition (pfile, node),
706	     print.outf);
707      putc ('\n', print.outf);
708      print.printed = false;
709      print.src_line++;
710    }
711
712  return 1;
713}
714
715/* Load in the PCH file NAME, open on FD.  It was originally searched for
716   by ORIG_NAME.  Also, print out a #include command so that the PCH
717   file can be loaded when the preprocessed output is compiled.  */
718
719static void
720cb_read_pch (cpp_reader *pfile, const char *name,
721	     int fd, const char *orig_name ATTRIBUTE_UNUSED)
722{
723  c_common_read_pch (pfile, name, fd, orig_name);
724
725  fprintf (print.outf, "#pragma GCC pch_preprocess \"%s\"\n", name);
726  print.src_line++;
727}
728