1 /* Preprocess only, using cpplib.
2    Copyright (C) 1995-2018 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.  */
29 static 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.  */
44 struct 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 };
51 static macro_queue *define_queue, *undef_queue;
52 
53 /* General output routines.  */
54 static void scan_translation_unit (cpp_reader *);
55 static void print_lines_directives_only (int, const void *, size_t);
56 static void scan_translation_unit_directives_only (cpp_reader *);
57 static void scan_translation_unit_trad (cpp_reader *);
58 static void account_for_newlines (const unsigned char *, size_t);
59 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
60 static void dump_queued_macros (cpp_reader *);
61 
62 static bool print_line_1 (source_location, const char*, FILE *);
63 static bool print_line (source_location, const char *);
64 static bool maybe_print_line_1 (source_location, FILE *);
65 static bool maybe_print_line (source_location);
66 static bool do_line_change (cpp_reader *, const cpp_token *,
67 			    source_location, int);
68 
69 /* Callback routines for the parser.   Most of these are active only
70    in specific modes.  */
71 static void cb_line_change (cpp_reader *, const cpp_token *, int);
72 static void cb_define (cpp_reader *, source_location, cpp_hashnode *);
73 static void cb_undef (cpp_reader *, source_location, cpp_hashnode *);
74 static void cb_used_define (cpp_reader *, source_location, cpp_hashnode *);
75 static void cb_used_undef (cpp_reader *, source_location, cpp_hashnode *);
76 static void cb_include (cpp_reader *, source_location, const unsigned char *,
77 			const char *, int, const cpp_token **);
78 static void cb_ident (cpp_reader *, source_location, const cpp_string *);
79 static void cb_def_pragma (cpp_reader *, source_location);
80 static void cb_read_pch (cpp_reader *pfile, const char *name,
81 			 int fd, const char *orig_name);
82 
83 /* Preprocess and output.  */
84 void
85 preprocess_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.  */
114 void
115 init_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->get_source_date_epoch = cb_get_source_date_epoch;
155   cb->remap_filename = remap_macro_filename;
156 
157   /* Initialize the print structure.  */
158   print.src_line = 1;
159   print.printed = false;
160   print.prev = 0;
161   print.outf = out_stream;
162   print.first_time = 1;
163   print.src_file = "";
164   print.prev_was_system_token = false;
165 }
166 
167 /* Writes out the preprocessed file, handling spacing and paste
168    avoidance issues.  */
169 static void
170 scan_translation_unit (cpp_reader *pfile)
171 {
172   bool avoid_paste = false;
173   bool do_line_adjustments
174     = cpp_get_options (parse_in)->lang != CLK_ASM
175       && !flag_no_line_commands;
176   bool in_pragma = false;
177   bool line_marker_emitted = false;
178 
179   print.source = NULL;
180   for (;;)
181     {
182       source_location loc;
183       const cpp_token *token = cpp_get_token_with_location (pfile, &loc);
184 
185       if (token->type == CPP_PADDING)
186 	{
187 	  avoid_paste = true;
188 	  if (print.source == NULL
189 	      || (!(print.source->flags & PREV_WHITE)
190 		  && token->val.source == NULL))
191 	    print.source = token->val.source;
192 	  continue;
193 	}
194 
195       if (token->type == CPP_EOF)
196 	break;
197 
198       /* Subtle logic to output a space if and only if necessary.  */
199       if (avoid_paste)
200 	{
201 	  int src_line = LOCATION_LINE (loc);
202 
203 	  if (print.source == NULL)
204 	    print.source = token;
205 
206 	  if (src_line != print.src_line
207 	      && do_line_adjustments
208 	      && !in_pragma)
209 	    {
210 	      line_marker_emitted = do_line_change (pfile, token, loc, false);
211 	      putc (' ', print.outf);
212 	      print.printed = true;
213 	    }
214 	  else if (print.source->flags & PREV_WHITE
215 		   || (print.prev
216 		       && cpp_avoid_paste (pfile, print.prev, token))
217 		   || (print.prev == NULL && token->type == CPP_HASH))
218 	    {
219 	      putc (' ', print.outf);
220 	      print.printed = true;
221 	    }
222 	}
223       else if (token->flags & PREV_WHITE)
224 	{
225 	  int src_line = LOCATION_LINE (loc);
226 
227 	  if (src_line != print.src_line
228 	      && do_line_adjustments
229 	      && !in_pragma)
230 	    line_marker_emitted = do_line_change (pfile, token, loc, false);
231 	  putc (' ', print.outf);
232 	  print.printed = true;
233 	}
234 
235       avoid_paste = false;
236       print.source = NULL;
237       print.prev = token;
238       if (token->type == CPP_PRAGMA)
239 	{
240 	  const char *space;
241 	  const char *name;
242 
243 	  line_marker_emitted = maybe_print_line (token->src_loc);
244 	  fputs ("#pragma ", print.outf);
245 	  c_pp_lookup_pragma (token->val.pragma, &space, &name);
246 	  if (space)
247 	    fprintf (print.outf, "%s %s", space, name);
248 	  else
249 	    fprintf (print.outf, "%s", name);
250 	  print.printed = true;
251 	  in_pragma = true;
252 	}
253       else if (token->type == CPP_PRAGMA_EOL)
254 	{
255 	  maybe_print_line (token->src_loc);
256 	  in_pragma = false;
257 	}
258       else
259 	{
260 	  if (cpp_get_options (parse_in)->debug)
261 	    linemap_dump_location (line_table, token->src_loc, print.outf);
262 
263 	  if (do_line_adjustments
264 	      && !in_pragma
265 	      && !line_marker_emitted
266 	      && print.prev_was_system_token != !!in_system_header_at (loc)
267 	      && !is_location_from_builtin_token (loc))
268 	    /* The system-ness of this token is different from the one
269 	       of the previous token.  Let's emit a line change to
270 	       mark the new system-ness before we emit the token.  */
271 	    {
272 	      do_line_change (pfile, token, loc, false);
273 	      print.prev_was_system_token = !!in_system_header_at (loc);
274 	    }
275 	  cpp_output_token (token, print.outf);
276 	  line_marker_emitted = false;
277 	  print.printed = true;
278 	}
279 
280       /* CPP_COMMENT tokens and raw-string literal tokens can
281 	 have embedded new-line characters.  Rather than enumerating
282 	 all the possible token types just check if token uses
283 	 val.str union member.  */
284       if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
285 	account_for_newlines (token->val.str.text, token->val.str.len);
286     }
287 }
288 
289 static void
290 print_lines_directives_only (int lines, const void *buf, size_t size)
291 {
292   print.src_line += lines;
293   fwrite (buf, 1, size, print.outf);
294 }
295 
296 /* Writes out the preprocessed file, handling spacing and paste
297    avoidance issues.  */
298 static void
299 scan_translation_unit_directives_only (cpp_reader *pfile)
300 {
301   struct _cpp_dir_only_callbacks cb;
302 
303   cb.print_lines = print_lines_directives_only;
304   cb.maybe_print_line = maybe_print_line;
305 
306   _cpp_preprocess_dir_only (pfile, &cb);
307 }
308 
309 /* Adjust print.src_line for newlines embedded in output.  */
310 static void
311 account_for_newlines (const unsigned char *str, size_t len)
312 {
313   while (len--)
314     if (*str++ == '\n')
315       print.src_line++;
316 }
317 
318 /* Writes out a traditionally preprocessed file.  */
319 static void
320 scan_translation_unit_trad (cpp_reader *pfile)
321 {
322   while (_cpp_read_logical_line_trad (pfile))
323     {
324       size_t len = pfile->out.cur - pfile->out.base;
325       maybe_print_line (pfile->out.first_line);
326       fwrite (pfile->out.base, 1, len, print.outf);
327       print.printed = true;
328       if (!CPP_OPTION (pfile, discard_comments))
329 	account_for_newlines (pfile->out.base, len);
330     }
331 }
332 
333 /* If the token read on logical line LINE needs to be output on a
334    different line to the current one, output the required newlines or
335    a line marker.  If a line marker was emitted, return TRUE otherwise
336    return FALSE.  */
337 
338 static bool
339 maybe_print_line_1 (source_location src_loc, FILE *stream)
340 {
341   bool emitted_line_marker = false;
342   int src_line = LOCATION_LINE (src_loc);
343   const char *src_file = LOCATION_FILE (src_loc);
344 
345   /* End the previous line of text.  */
346   if (print.printed)
347     {
348       putc ('\n', stream);
349       print.src_line++;
350       print.printed = false;
351     }
352 
353   if (!flag_no_line_commands
354       && src_line >= print.src_line
355       && src_line < print.src_line + 8
356       && strcmp (src_file, print.src_file) == 0)
357     {
358       while (src_line > print.src_line)
359 	{
360 	  putc ('\n', stream);
361 	  print.src_line++;
362 	}
363     }
364   else
365     emitted_line_marker = print_line_1 (src_loc, "", stream);
366 
367   return emitted_line_marker;
368 }
369 
370 /* If the token read on logical line LINE needs to be output on a
371    different line to the current one, output the required newlines or
372    a line marker.  If a line marker was emitted, return TRUE otherwise
373    return FALSE.  */
374 
375 static bool
376 maybe_print_line (source_location src_loc)
377 {
378   if (cpp_get_options (parse_in)->debug)
379     linemap_dump_location (line_table, src_loc,
380 			   print.outf);
381   return maybe_print_line_1 (src_loc, print.outf);
382 }
383 
384 /* Output a line marker for logical line LINE.  Special flags are "1"
385    or "2" indicating entering or leaving a file.  If the line marker
386    was effectively emitted, return TRUE otherwise return FALSE.  */
387 
388 static bool
389 print_line_1 (source_location src_loc, const char *special_flags, FILE *stream)
390 {
391   bool emitted_line_marker = false;
392 
393   /* End any previous line of text.  */
394   if (print.printed)
395     putc ('\n', stream);
396   print.printed = false;
397 
398   if (!flag_no_line_commands)
399     {
400       const char *file_path = LOCATION_FILE (src_loc);
401       int sysp;
402       size_t to_file_len = strlen (file_path);
403       unsigned char *to_file_quoted =
404          (unsigned char *) alloca (to_file_len * 4 + 1);
405       unsigned char *p;
406 
407       print.src_line = LOCATION_LINE (src_loc);
408       print.src_file = file_path;
409 
410       /* cpp_quote_string does not nul-terminate, so we have to do it
411 	 ourselves.  */
412       p = cpp_quote_string (to_file_quoted,
413 			    (const unsigned char *) file_path,
414 			    to_file_len);
415       *p = '\0';
416       fprintf (stream, "# %u \"%s\"%s",
417 	       print.src_line == 0 ? 1 : print.src_line,
418 	       to_file_quoted, special_flags);
419 
420       sysp = in_system_header_at (src_loc);
421       if (sysp == 2)
422 	fputs (" 3 4", stream);
423       else if (sysp == 1)
424 	fputs (" 3", stream);
425 
426       putc ('\n', stream);
427       emitted_line_marker = true;
428     }
429 
430   return emitted_line_marker;
431 }
432 
433 /* Output a line marker for logical line LINE.  Special flags are "1"
434    or "2" indicating entering or leaving a file.  Return TRUE if a
435    line marker was effectively emitted, FALSE otherwise.  */
436 
437 static bool
438 print_line (source_location src_loc, const char *special_flags)
439 {
440     if (cpp_get_options (parse_in)->debug)
441       linemap_dump_location (line_table, src_loc,
442 			     print.outf);
443     return print_line_1 (src_loc, special_flags, print.outf);
444 }
445 
446 /* Helper function for cb_line_change and scan_translation_unit.
447    Return TRUE if a line marker is emitted, FALSE otherwise.  */
448 static bool
449 do_line_change (cpp_reader *pfile, const cpp_token *token,
450 		source_location src_loc, int parsing_args)
451 {
452   bool emitted_line_marker = false;
453   if (define_queue || undef_queue)
454     dump_queued_macros (pfile);
455 
456   if (token->type == CPP_EOF || parsing_args)
457     return false;
458 
459   emitted_line_marker = maybe_print_line (src_loc);
460   print.prev = 0;
461   print.source = 0;
462 
463   /* Supply enough spaces to put this token in its original column,
464      one space per column greater than 2, since scan_translation_unit
465      will provide a space if PREV_WHITE.  Don't bother trying to
466      reconstruct tabs; we can't get it right in general, and nothing
467      ought to care.  Some things do care; the fault lies with them.  */
468   if (!CPP_OPTION (pfile, traditional))
469     {
470       int spaces = LOCATION_COLUMN (src_loc) - 2;
471       print.printed = true;
472 
473       while (-- spaces >= 0)
474 	putc (' ', print.outf);
475     }
476 
477   return emitted_line_marker;
478 }
479 
480 /* Called when a line of output is started.  TOKEN is the first token
481    of the line, and at end of file will be CPP_EOF.  */
482 static void
483 cb_line_change (cpp_reader *pfile, const cpp_token *token,
484 		int parsing_args)
485 {
486   do_line_change (pfile, token, token->src_loc, parsing_args);
487 }
488 
489 static void
490 cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
491 	  const cpp_string *str)
492 {
493   maybe_print_line (line);
494   fprintf (print.outf, "#ident %s\n", str->text);
495   print.src_line++;
496 }
497 
498 static void
499 cb_define (cpp_reader *pfile, source_location line, cpp_hashnode *node)
500 {
501   const line_map_ordinary *map;
502 
503   maybe_print_line (line);
504   fputs ("#define ", print.outf);
505 
506   /* 'D' is whole definition; 'N' is name only.  */
507   if (flag_dump_macros == 'D')
508     fputs ((const char *) cpp_macro_definition (pfile, node),
509 	   print.outf);
510   else
511     fputs ((const char *) NODE_NAME (node), print.outf);
512 
513   putc ('\n', print.outf);
514   print.printed = false;
515   linemap_resolve_location (line_table, line,
516 			    LRK_MACRO_DEFINITION_LOCATION,
517 			    &map);
518   if (LINEMAP_LINE (map) != 0)
519     print.src_line++;
520 }
521 
522 static void
523 cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
524 	  cpp_hashnode *node)
525 {
526   maybe_print_line (line);
527   fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
528   print.src_line++;
529 }
530 
531 static void
532 cb_used_define (cpp_reader *pfile, source_location line ATTRIBUTE_UNUSED,
533 		cpp_hashnode *node)
534 {
535   macro_queue *q;
536   if (node->flags & NODE_BUILTIN)
537     return;
538   q = XNEW (macro_queue);
539   q->macro = xstrdup ((const char *) cpp_macro_definition (pfile, node));
540   q->next = define_queue;
541   define_queue = q;
542 }
543 
544 static void
545 cb_used_undef (cpp_reader *pfile ATTRIBUTE_UNUSED,
546 	       source_location line ATTRIBUTE_UNUSED,
547 	       cpp_hashnode *node)
548 {
549   macro_queue *q;
550   q = XNEW (macro_queue);
551   q->macro = xstrdup ((const char *) NODE_NAME (node));
552   q->next = undef_queue;
553   undef_queue = q;
554 }
555 
556 static void
557 dump_queued_macros (cpp_reader *pfile ATTRIBUTE_UNUSED)
558 {
559   macro_queue *q;
560 
561   /* End the previous line of text.  */
562   if (print.printed)
563     {
564       putc ('\n', print.outf);
565       print.src_line++;
566       print.printed = false;
567     }
568 
569   for (q = define_queue; q;)
570     {
571       macro_queue *oq;
572       fputs ("#define ", print.outf);
573       fputs (q->macro, print.outf);
574       putc ('\n', print.outf);
575       print.printed = false;
576       print.src_line++;
577       oq = q;
578       q = q->next;
579       free (oq->macro);
580       free (oq);
581     }
582   define_queue = NULL;
583   for (q = undef_queue; q;)
584     {
585       macro_queue *oq;
586       fprintf (print.outf, "#undef %s\n", q->macro);
587       print.src_line++;
588       oq = q;
589       q = q->next;
590       free (oq->macro);
591       free (oq);
592     }
593   undef_queue = NULL;
594 }
595 
596 static void
597 cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
598 	    const unsigned char *dir, const char *header, int angle_brackets,
599 	    const cpp_token **comments)
600 {
601   maybe_print_line (line);
602   if (angle_brackets)
603     fprintf (print.outf, "#%s <%s>", dir, header);
604   else
605     fprintf (print.outf, "#%s \"%s\"", dir, header);
606 
607   if (comments != NULL)
608     {
609       while (*comments != NULL)
610 	{
611 	  if ((*comments)->flags & PREV_WHITE)
612 	    putc (' ', print.outf);
613 	  cpp_output_token (*comments, print.outf);
614 	  ++comments;
615 	}
616     }
617 
618   putc ('\n', print.outf);
619   print.printed = false;
620   print.src_line++;
621 }
622 
623 /* Callback called when -fworking-director and -E to emit working
624    directory in cpp output file.  */
625 
626 void
627 pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
628 {
629   size_t to_file_len = strlen (dir);
630   unsigned char *to_file_quoted =
631      (unsigned char *) alloca (to_file_len * 4 + 1);
632   unsigned char *p;
633 
634   /* cpp_quote_string does not nul-terminate, so we have to do it ourselves.  */
635   p = cpp_quote_string (to_file_quoted, (const unsigned char *) dir, to_file_len);
636   *p = '\0';
637   fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
638 }
639 
640 /* The file name, line number or system header flags have changed, as
641    described in MAP.  */
642 
643 void
644 pp_file_change (const line_map_ordinary *map)
645 {
646   const char *flags = "";
647 
648   if (flag_no_line_commands)
649     return;
650 
651   if (map != NULL)
652     {
653       input_location = map->start_location;
654       if (print.first_time)
655 	{
656 	  /* Avoid printing foo.i when the main file is foo.c.  */
657 	  if (!cpp_get_options (parse_in)->preprocessed)
658 	    print_line (map->start_location, flags);
659 	  print.first_time = 0;
660 	}
661       else
662 	{
663 	  /* Bring current file to correct line when entering a new file.  */
664 	  if (map->reason == LC_ENTER)
665 	    {
666 	      const line_map_ordinary *from = INCLUDED_FROM (line_table, map);
667 	      maybe_print_line (LAST_SOURCE_LINE_LOCATION (from));
668 	    }
669 	  if (map->reason == LC_ENTER)
670 	    flags = " 1";
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.  */
679 static void
680 cb_def_pragma (cpp_reader *pfile, source_location 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.  */
690 static int
691 dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
692 {
693   if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
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 
710 static void
711 cb_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