* SDCPP synchronized with GCC CPP release version 3.4.6,
[fw/sdcc] / support / cpp2 / c-ppoutput.c
1 /* Preprocess only, using cpplib.
2    Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3    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 2, 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; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 #include "config.h"
21 #include "system.h"
22 #include "cpplib.h"
23 #include "cpphash.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 struct line_map *map;   /* Logical to physical line mappings.  */
31   const cpp_token *prev;        /* Previous token.  */
32   const cpp_token *source;      /* Source token for spacing.  */
33   fileline line;                /* Line currently being written.  */
34   unsigned char printed;        /* Nonzero if something output at line.  */
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 (const struct line_map *, fileline, const char *);
44 static void maybe_print_line (const struct line_map *, fileline);
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 *, fileline, cpp_hashnode *);
50 static void cb_undef (cpp_reader *, fileline, cpp_hashnode *);
51 static void cb_include (cpp_reader *, fileline, const unsigned char *,
52                         const char *, int);
53 static void cb_ident (cpp_reader *, fileline, const cpp_string *);
54 static void cb_def_pragma (cpp_reader *, fileline);
55
56 /* Preprocess and output.  */
57 void
58 preprocess_file (cpp_reader *pfile)
59 {
60   /* A successful cpp_read_main_file guarantees that we can call
61      cpp_scan_nooutput or cpp_get_token next.  */
62   if (flag_no_output)
63     {
64       /* Scan -included buffers, then the main file.  */
65       while (pfile->buffer->prev)
66         cpp_scan_nooutput (pfile);
67       cpp_scan_nooutput (pfile);
68     }
69   else if (cpp_get_options (pfile)->traditional)
70     scan_translation_unit_trad (pfile);
71   else
72     scan_translation_unit (pfile);
73
74   /* -dM command line option.  Should this be elsewhere?  */
75   if (flag_dump_macros == 'M')
76     cpp_forall_identifiers (pfile, dump_macro, NULL);
77
78   /* Flush any pending output.  */
79   if (print.printed)
80     putc ('\n', print.outf);
81 }
82
83 /* Set up the callbacks as appropriate.  */
84 void
85 init_pp_output (FILE *out_stream)
86 {
87   cpp_callbacks *cb = cpp_get_callbacks (parse_in);
88
89   if (!flag_no_output)
90     {
91       cb->line_change = cb_line_change;
92       /* Don't emit #pragma or #ident directives if we are processing
93          assembly language; the assembler may choke on them.  */
94       if (cpp_get_options (parse_in)->lang != CLK_ASM)
95         {
96           cb->ident      = cb_ident;
97           cb->def_pragma = cb_def_pragma;
98         }
99     }
100
101   if (flag_dump_includes)
102     cb->include  = cb_include;
103
104   if (flag_dump_macros == 'N' || flag_dump_macros == 'D')
105     {
106       cb->define = cb_define;
107       cb->undef  = cb_undef;
108     }
109
110   /* Initialize the print structure.  Setting print.line to -1 here is
111      a trick to guarantee that the first token of the file will cause
112      a linemarker to be output by maybe_print_line.  */
113   print.line = (fileline) -1;
114   print.printed = 0;
115   print.prev = 0;
116   print.map = 0;
117   print.outf = out_stream;
118 }
119
120 /* Writes out the preprocessed file, handling spacing and paste
121    avoidance issues.  */
122 static void
123 scan_translation_unit (cpp_reader *pfile)
124 {
125   bool avoid_paste = false;
126
127   print.source = NULL;
128   for (;;)
129     {
130       const cpp_token *token = cpp_get_token (pfile);
131
132       if (token->type == CPP_PADDING)
133         {
134           avoid_paste = true;
135           if (print.source == NULL
136               || (!(print.source->flags & PREV_WHITE)
137                   && token->val.source == NULL))
138             print.source = token->val.source;
139           continue;
140         }
141
142       if (token->type == CPP_EOF)
143         break;
144
145       /* Subtle logic to output a space if and only if necessary.  */
146       if (avoid_paste)
147         {
148           if (print.source == NULL)
149             print.source = token;
150           if (print.source->flags & PREV_WHITE
151               || (print.prev
152                   && cpp_avoid_paste (pfile, print.prev, token))
153               || (print.prev == NULL && token->type == CPP_HASH))
154             putc (' ', print.outf);
155         }
156       else if (token->flags & PREV_WHITE)
157         putc (' ', print.outf);
158
159       avoid_paste = false;
160       print.source = NULL;
161       print.prev = token;
162       cpp_output_token (token, print.outf);
163
164       if (token->type == CPP_COMMENT)
165         account_for_newlines (token->val.str.text, token->val.str.len);
166     }
167 }
168
169 /* Adjust print.line for newlines embedded in output.  */
170 static void
171 account_for_newlines (const unsigned char *str, size_t len)
172 {
173   while (len--)
174     if (*str++ == '\n')
175       print.line++;
176 }
177
178 /* Writes out a traditionally preprocessed file.  */
179 static void
180 scan_translation_unit_trad (cpp_reader *pfile)
181 {
182   while (_cpp_read_logical_line_trad (pfile))
183     {
184       size_t len = pfile->out.cur - pfile->out.base;
185       maybe_print_line (print.map, pfile->out.first_line);
186       fwrite (pfile->out.base, 1, len, print.outf);
187       print.printed = 1;
188       if (!CPP_OPTION (pfile, discard_comments))
189         account_for_newlines (pfile->out.base, len);
190     }
191 }
192
193 /* If the token read on logical line LINE needs to be output on a
194    different line to the current one, output the required newlines or
195    a line marker, and return 1.  Otherwise return 0.  */
196 static void
197 maybe_print_line (const struct line_map *map, fileline line)
198 {
199   /* End the previous line of text.  */
200   if (print.printed)
201     {
202       putc ('\n', print.outf);
203       print.line++;
204       print.printed = 0;
205     }
206
207   if (line >= print.line && line < print.line + 8)
208     {
209       while (line > print.line)
210         {
211           putc ('\n', print.outf);
212           print.line++;
213         }
214     }
215   else
216     print_line (map, line, "");
217 }
218
219 /* Output a line marker for logical line LINE.  Special flags are "1"
220    or "2" indicating entering or leaving a file.  */
221 static void
222 print_line (const struct line_map *map, fileline line, const char *special_flags)
223 {
224   /* End any previous line of text.  */
225   if (print.printed)
226     putc ('\n', print.outf);
227   print.printed = 0;
228
229   print.line = line;
230   if (!flag_no_line_commands)
231     {
232       size_t to_file_len = strlen (map->to_file);
233       unsigned char *to_file_quoted = alloca (to_file_len * 4 + 1);
234       unsigned char *p;
235
236       /* cpp_quote_string does not nul-terminate, so we have to do it
237          ourselves.  */
238       p = cpp_quote_string (to_file_quoted,
239                             (unsigned char *)map->to_file, to_file_len);
240       *p = '\0';
241       fprintf (print.outf, "# %u \"%s\"%s",
242                SOURCE_LINE (map, print.line),
243                to_file_quoted, special_flags);
244
245       if (map->sysp == 2)
246         fputs (" 3 4", print.outf);
247       else if (map->sysp == 1)
248         fputs (" 3", print.outf);
249
250       putc ('\n', print.outf);
251     }
252 }
253
254 /* Called when a line of output is started.  TOKEN is the first token
255    of the line, and at end of file will be CPP_EOF.  */
256 static void
257 cb_line_change (cpp_reader *pfile, const cpp_token *token,
258                 int parsing_args)
259 {
260   if (token->type == CPP_EOF || parsing_args)
261     return;
262
263   maybe_print_line (print.map, token->line);
264   print.prev = 0;
265   print.source = 0;
266
267   /* Supply enough spaces to put this token in its original column,
268      one space per column greater than 2, since scan_translation_unit
269      will provide a space if PREV_WHITE.  Don't bother trying to
270      reconstruct tabs; we can't get it right in general, and nothing
271      ought to care.  Some things do care; the fault lies with them.  */
272   if (!CPP_OPTION (pfile, traditional))
273     {
274       print.printed = 1;
275       if (token->col > 2)
276         {
277           unsigned int spaces = token->col - 2;
278
279           while (spaces--)
280             putc (' ', print.outf);
281         }
282     }
283 }
284
285 static void
286 cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, fileline line,
287           const cpp_string *str)
288 {
289   maybe_print_line (print.map, line);
290   fprintf (print.outf, "#ident %s\n", str->text);
291   print.line++;
292 }
293
294 static void
295 cb_define (cpp_reader *pfile, fileline line, cpp_hashnode *node)
296 {
297   maybe_print_line (print.map, line);
298   fputs ("#define ", print.outf);
299
300   /* 'D' is whole definition; 'N' is name only.  */
301   if (flag_dump_macros == 'D')
302     fputs ((const char *) cpp_macro_definition (pfile, node),
303            print.outf);
304   else
305     fputs ((const char *) NODE_NAME (node), print.outf);
306
307   putc ('\n', print.outf);
308   print.line++;
309 }
310
311 static void
312 cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, fileline line,
313           cpp_hashnode *node)
314 {
315   maybe_print_line (print.map, line);
316   fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
317   print.line++;
318 }
319
320 static void
321 cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, fileline line,
322             const unsigned char *dir, const char *header, int angle_brackets)
323 {
324   maybe_print_line (print.map, line);
325   if (angle_brackets)
326     fprintf (print.outf, "#%s <%s>\n", dir, header);
327   else
328     fprintf (print.outf, "#%s \"%s\"\n", dir, header);
329   print.line++;
330 }
331
332 /* Callback called when -fworking-director and -E to emit working
333    diretory in cpp output file. */
334
335 void
336 pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
337 {
338   size_t to_file_len = strlen (dir);
339   unsigned char *to_file_quoted = alloca (to_file_len * 4 + 1);
340   unsigned char *p;
341
342   /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
343   p = cpp_quote_string (to_file_quoted, (unsigned char *) dir, to_file_len);
344   *p = '\0';
345   fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
346 }
347
348 /* The file name, line number or system header flags have changed, as
349    described in MAP.  From this point on, the old print.map might be
350    pointing to freed memory, and so must not be dereferenced.  */
351
352 void
353 pp_file_change (const struct line_map *map)
354 {
355   const char *flags = "";
356
357   if (flag_no_line_commands)
358     return;
359
360   if (map != NULL)
361     {
362       /* First time?  */
363       if (print.map == NULL)
364         {
365           /* Avoid printing foo.i when the main file is foo.c.  */
366           if (!cpp_get_options (parse_in)->preprocessed)
367             print_line (map, map->from_line, flags);
368         }
369       else
370         {
371           /* Bring current file to correct line when entering a new file.  */
372           if (map->reason == LC_ENTER)
373             maybe_print_line (map - 1, map->from_line - 1);
374
375           if (map->reason == LC_ENTER)
376             flags = " 1";
377           else if (map->reason == LC_LEAVE)
378             flags = " 2";
379           print_line (map, map->from_line, flags);
380         }
381     }
382
383   print.map = map;
384 }
385
386 /* Copy a #pragma directive to the preprocessed output.  */
387 static void
388 cb_def_pragma (cpp_reader *pfile, fileline line)
389 {
390   maybe_print_line (print.map, line);
391   fputs ("#pragma ", print.outf);
392   cpp_output_line (pfile, print.outf);
393   print.line++;
394 }
395
396 /* Dump out the hash table.  */
397 static int
398 dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
399 {
400   if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
401     {
402       fputs ("#define ", print.outf);
403       fputs ((const char *) cpp_macro_definition (pfile, node),
404              print.outf);
405       putc ('\n', print.outf);
406       print.line++;
407     }
408
409   return 1;
410 }