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