* support/cpp/output.h, support/cpp/opts-common.c,
[fw/sdcc] / support / cpp / c-ppoutput.c
1 /* Preprocess only, using cpplib.
2    Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2007,
3    2008 Free Software Foundation, Inc.
4    Written by Per Bothner, 1994-95.
5
6    This program is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by the
8    Free Software Foundation; either version 3, or (at your option) any
9    later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; see the file COPYING3.  If not see
18    <http://www.gnu.org/licenses/>.  */
19
20 #include "config.h"
21 #include "system.h"
22 #include "cpplib.h"
23 #include "../libcpp/internal.h"
24
25 /* Encapsulates state used to convert a stream of tokens into a text
26    file.  */
27 static struct
28 {
29   FILE *outf;                   /* Stream to write to.  */
30   const cpp_token *prev;        /* Previous token.  */
31   const cpp_token *source;      /* Source token for spacing.  */
32   int src_line;                 /* Line number currently being written.  */
33   unsigned char printed;        /* Nonzero if something output at line.  */
34   bool first_time;              /* pp_file_change hasn't been called yet.  */
35 } print;
36
37 /* General output routines.  */
38 static void scan_translation_unit (cpp_reader *);
39 static void scan_translation_unit_trad (cpp_reader *);
40 static void account_for_newlines (const unsigned char *, size_t);
41 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
42
43 static void print_line (source_location, const char *);
44 static void maybe_print_line (source_location);
45
46 /* Callback routines for the parser.   Most of these are active only
47    in specific modes.  */
48 static void cb_line_change (cpp_reader *, const cpp_token *, int);
49 static void cb_define (cpp_reader *, source_location, cpp_hashnode *);
50 static void cb_undef (cpp_reader *, source_location, cpp_hashnode *);
51 static void cb_include (cpp_reader *, source_location, const unsigned char *,
52                         const char *, int, const cpp_token **);
53 static void cb_ident (cpp_reader *, source_location, const cpp_string *);
54 static void cb_def_pragma (cpp_reader *, source_location);
55 #if 0
56 static void cb_read_pch (cpp_reader *pfile, const char *name,
57                          int fd, const char *orig_name);
58 #endif
59
60 /* Preprocess and output.  */
61 void
62 preprocess_file (cpp_reader *pfile)
63 {
64   /* A successful cpp_read_main_file guarantees that we can call
65      cpp_scan_nooutput or cpp_get_token next.  */
66   if (flag_no_output)
67     {
68       /* Scan -included buffers, then the main file.  */
69       while (pfile->buffer->prev)
70         cpp_scan_nooutput (pfile);
71       cpp_scan_nooutput (pfile);
72     }
73   else if (cpp_get_options (pfile)->traditional)
74     scan_translation_unit_trad (pfile);
75   else
76     scan_translation_unit (pfile);
77
78   /* -dM command line option.  Should this be elsewhere?  */
79   if (flag_dump_macros == 'M')
80     cpp_forall_identifiers (pfile, dump_macro, NULL);
81
82   /* Flush any pending output.  */
83   if (print.printed)
84     putc ('\n', print.outf);
85 }
86
87 /* Set up the callbacks as appropriate.  */
88 void
89 init_pp_output (FILE *out_stream)
90 {
91   cpp_callbacks *cb = cpp_get_callbacks (parse_in);
92
93   if (!flag_no_output)
94     {
95       cb->line_change = cb_line_change;
96       /* Don't emit #pragma or #ident directives if we are processing
97          assembly language; the assembler may choke on them.  */
98       if (cpp_get_options (parse_in)->lang != CLK_ASM)
99         {
100           cb->ident      = cb_ident;
101           cb->def_pragma = cb_def_pragma;
102         }
103     }
104
105   if (flag_dump_includes)
106     cb->include  = cb_include;
107
108 #if 0
109   if (flag_pch_preprocess)
110     {
111       cb->valid_pch = c_common_valid_pch;
112       cb->read_pch = cb_read_pch;
113     }
114 #endif
115
116   if (flag_dump_macros == 'N' || flag_dump_macros == 'D')
117     {
118       cb->define = cb_define;
119       cb->undef  = cb_undef;
120     }
121
122   /* Initialize the print structure.  Setting print.src_line to -1 here is
123      a trick to guarantee that the first token of the file will cause
124      a linemarker to be output by maybe_print_line.  */
125   print.src_line = -1;
126   print.printed = 0;
127   print.prev = 0;
128   print.outf = out_stream;
129   print.first_time = 1;
130 }
131
132 /* Writes out the preprocessed file, handling spacing and paste
133    avoidance issues.  */
134 static void
135 scan_translation_unit (cpp_reader *pfile)
136 {
137   bool avoid_paste = false;
138
139   print.source = NULL;
140   for (;;)
141     {
142       const cpp_token *token = cpp_get_token (pfile);
143
144       if (token->type == CPP_PADDING)
145         {
146           avoid_paste = true;
147           if (print.source == NULL
148               || (!(print.source->flags & PREV_WHITE)
149                   && token->val.source == NULL))
150             print.source = token->val.source;
151           continue;
152         }
153
154       if (token->type == CPP_EOF)
155         break;
156
157       /* Subtle logic to output a space if and only if necessary.  */
158       if (avoid_paste)
159         {
160           if (print.source == NULL)
161             print.source = token;
162           if (print.source->flags & PREV_WHITE
163               || (print.prev
164                   && cpp_avoid_paste (pfile, print.prev, token))
165               || (print.prev == NULL && token->type == CPP_HASH))
166             putc (' ', print.outf);
167         }
168       else if (token->flags & PREV_WHITE)
169         putc (' ', print.outf);
170
171       avoid_paste = false;
172       print.source = NULL;
173       print.prev = token;
174       cpp_output_token (token, print.outf);
175
176       if (token->type == CPP_COMMENT)
177         account_for_newlines (token->val.str.text, token->val.str.len);
178     }
179 }
180
181 /* Adjust print.src_line for newlines embedded in output.  */
182 static void
183 account_for_newlines (const unsigned char *str, size_t len)
184 {
185   while (len--)
186     if (*str++ == '\n')
187       print.src_line++;
188 }
189
190 /* Writes out a traditionally preprocessed file.  */
191 static void
192 scan_translation_unit_trad (cpp_reader *pfile)
193 {
194   while (_cpp_read_logical_line_trad (pfile))
195     {
196       size_t len = pfile->out.cur - pfile->out.base;
197       maybe_print_line (pfile->out.first_line);
198       fwrite (pfile->out.base, 1, len, print.outf);
199       print.printed = 1;
200       if (!CPP_OPTION (pfile, discard_comments))
201         account_for_newlines (pfile->out.base, len);
202     }
203 }
204
205 /* If the token read on logical line LINE needs to be output on a
206    different line to the current one, output the required newlines or
207    a line marker, and return 1.  Otherwise return 0.  */
208 static void
209 maybe_print_line (source_location src_loc)
210 {
211   const struct line_map *map = linemap_lookup (line_table, src_loc);
212   int src_line = SOURCE_LINE (map, src_loc);
213   /* End the previous line of text.  */
214   if (print.printed)
215     {
216       putc ('\n', print.outf);
217       print.src_line++;
218       print.printed = 0;
219     }
220
221   if (src_line >= print.src_line && src_line < print.src_line + 8)
222     {
223       while (src_line > print.src_line)
224         {
225           putc ('\n', print.outf);
226           print.src_line++;
227         }
228     }
229   else
230     print_line (src_loc, "");
231 }
232
233 /* Output a line marker for logical line LINE.  Special flags are "1"
234    or "2" indicating entering or leaving a file.  */
235 static void
236 print_line (source_location src_loc, const char *special_flags)
237 {
238   /* End any previous line of text.  */
239   if (print.printed)
240     putc ('\n', print.outf);
241   print.printed = 0;
242
243   if (!flag_no_line_commands)
244     {
245       const struct line_map *map = linemap_lookup (line_table, src_loc);
246
247       size_t to_file_len = strlen (map->to_file);
248       unsigned char *to_file_quoted =
249          (unsigned char *) alloca (to_file_len * 4 + 1);
250       unsigned char *p;
251
252       print.src_line = SOURCE_LINE (map, src_loc);
253
254       /* cpp_quote_string does not nul-terminate, so we have to do it
255          ourselves.  */
256       p = cpp_quote_string (to_file_quoted,
257                             (unsigned char *) map->to_file, to_file_len);
258       *p = '\0';
259       fprintf (print.outf, "# %u \"%s\"%s",
260                print.src_line == 0 ? 1 : print.src_line,
261                to_file_quoted, special_flags);
262
263       if (map->sysp == 2)
264         fputs (" 3 4", print.outf);
265       else if (map->sysp == 1)
266         fputs (" 3", print.outf);
267
268       putc ('\n', print.outf);
269     }
270 }
271
272 /* Called when a line of output is started.  TOKEN is the first token
273    of the line, and at end of file will be CPP_EOF.  */
274 static void
275 cb_line_change (cpp_reader *pfile, const cpp_token *token,
276                 int parsing_args)
277 {
278   source_location src_loc = token->src_loc;
279
280   if (token->type == CPP_EOF || parsing_args)
281     return;
282
283   maybe_print_line (src_loc);
284   print.prev = 0;
285   print.source = 0;
286
287   /* Supply enough spaces to put this token in its original column,
288      one space per column greater than 2, since scan_translation_unit
289      will provide a space if PREV_WHITE.  Don't bother trying to
290      reconstruct tabs; we can't get it right in general, and nothing
291      ought to care.  Some things do care; the fault lies with them.  */
292   if (!CPP_OPTION (pfile, traditional))
293     {
294       const struct line_map *map = linemap_lookup (line_table, src_loc);
295       int spaces = SOURCE_COLUMN (map, src_loc) - 2;
296       print.printed = 1;
297
298       while (-- spaces >= 0)
299         putc (' ', print.outf);
300     }
301 }
302
303 static void
304 cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
305           const cpp_string *str)
306 {
307   maybe_print_line (line);
308   fprintf (print.outf, "#ident %s\n", str->text);
309   print.src_line++;
310 }
311
312 static void
313 cb_define (cpp_reader *pfile, source_location line, cpp_hashnode *node)
314 {
315   maybe_print_line (line);
316   fputs ("#define ", print.outf);
317
318   /* 'D' is whole definition; 'N' is name only.  */
319   if (flag_dump_macros == 'D')
320     fputs ((const char *) cpp_macro_definition (pfile, node),
321            print.outf);
322   else
323     fputs ((const char *) NODE_NAME (node), print.outf);
324
325   putc ('\n', print.outf);
326   if (linemap_lookup (line_table, line)->to_line != 0)
327     print.src_line++;
328 }
329
330 static void
331 cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
332           cpp_hashnode *node)
333 {
334   maybe_print_line (line);
335   fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
336   print.src_line++;
337 }
338
339 static void
340 cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
341             const unsigned char *dir, const char *header, int angle_brackets,
342             const cpp_token **comments)
343 {
344   maybe_print_line (line);
345   if (angle_brackets)
346     fprintf (print.outf, "#%s <%s>", dir, header);
347   else
348     fprintf (print.outf, "#%s \"%s\"", dir, header);
349
350   if (comments != NULL)
351     {
352       while (*comments != NULL)
353         {
354           if ((*comments)->flags & PREV_WHITE)
355             putc (' ', print.outf);
356           cpp_output_token (*comments, print.outf);
357           ++comments;
358         }
359     }
360
361   putc ('\n', print.outf);
362   print.src_line++;
363 }
364
365 /* Callback called when -fworking-director and -E to emit working
366    directory in cpp output file.  */
367
368 void
369 pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
370 {
371   size_t to_file_len = strlen (dir);
372   unsigned char *to_file_quoted =
373      (unsigned char *) alloca (to_file_len * 4 + 1);
374   unsigned char *p;
375
376   /* cpp_quote_string does not nul-terminate, so we have to do it ourselves.  */
377   p = cpp_quote_string (to_file_quoted, (unsigned char *) dir, to_file_len);
378   *p = '\0';
379   fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
380 }
381
382 /* The file name, line number or system header flags have changed, as
383    described in MAP.  */
384
385 void
386 pp_file_change (const struct line_map *map)
387 {
388   const char *flags = "";
389
390   if (flag_no_line_commands)
391     return;
392
393   if (map != NULL)
394     {
395       if (print.first_time)
396         {
397           /* Avoid printing foo.i when the main file is foo.c.  */
398           if (!cpp_get_options (parse_in)->preprocessed)
399             print_line (map->start_location, flags);
400           print.first_time = 0;
401         }
402       else
403         {
404           /* Bring current file to correct line when entering a new file.  */
405           if (map->reason == LC_ENTER)
406             {
407               const struct line_map *from = INCLUDED_FROM (line_table, map);
408               maybe_print_line (LAST_SOURCE_LINE_LOCATION (from));
409             }
410           if (map->reason == LC_ENTER)
411             flags = " 1";
412           else if (map->reason == LC_LEAVE)
413             flags = " 2";
414           print_line (map->start_location, flags);
415         }
416     }
417 }
418
419 /* Copy a #pragma directive to the preprocessed output.  */
420 static void
421 cb_def_pragma (cpp_reader *pfile, source_location line)
422 {
423   maybe_print_line (line);
424   fputs ("#pragma ", print.outf);
425   cpp_output_line (pfile, print.outf);
426   print.src_line++;
427 }
428
429 /* Dump out the hash table.  */
430 static int
431 dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
432 {
433   if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
434     {
435       fputs ("#define ", print.outf);
436       fputs ((const char *) cpp_macro_definition (pfile, node),
437              print.outf);
438       putc ('\n', print.outf);
439       print.src_line++;
440     }
441
442   return 1;
443 }
444
445 #if 0
446 /* Load in the PCH file NAME, open on FD.  It was originally searched for
447    by ORIG_NAME.  Also, print out a #include command so that the PCH
448    file can be loaded when the preprocessed output is compiled.  */
449
450 static void
451 cb_read_pch (cpp_reader *pfile, const char *name,
452              int fd, const char *orig_name ATTRIBUTE_UNUSED)
453 {
454   c_common_read_pch (pfile, name, fd, orig_name);
455
456   fprintf (print.outf, "#pragma GCC pch_preprocess \"%s\"\n", name);
457   print.src_line++;
458 }
459 #endif