OSDN Git Service

* cppmain.c (scan_translation_unit): Don't worry about
[pf3gnuchains/gcc-fork.git] / gcc / cppmain.c
1 /* CPP main program, using CPP Library.
2    Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001
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  In other words, you are welcome to use, share and improve this program.
21  You are forbidden to forbid anyone else to use, share and improve
22  what you give them.   Help stamp out software-hoarding!  */
23
24 #include "config.h"
25 #include "system.h"
26 #include "cpplib.h"
27 #include "intl.h"
28
29 /* Encapsulates state used to convert the stream of tokens coming from
30    cpp_get_token back into a text file.  */
31 struct printer
32 {
33   FILE *outf;                   /* Stream to write to.  */
34   const struct line_map *map;   /* Logical to physical line mappings.  */
35   unsigned int line;            /* Line currently being written.  */
36   unsigned char printed;        /* Nonzero if something output at line.  */
37 };
38
39 int main                PARAMS ((int, char **));
40 static void general_init PARAMS ((const char *));
41 static void do_preprocessing PARAMS ((int, char **));
42 static void setup_callbacks PARAMS ((void));
43
44 /* General output routines.  */
45 static void scan_translation_unit PARAMS ((cpp_reader *));
46 static void check_multiline_token PARAMS ((cpp_string *));
47 static int dump_macro PARAMS ((cpp_reader *, cpp_hashnode *, void *));
48
49 static void print_line PARAMS ((const struct line_map *, unsigned int,
50                                 const char *));
51 static void maybe_print_line PARAMS ((const struct line_map *, unsigned int));
52
53 /* Callback routines for the parser.   Most of these are active only
54    in specific modes.  */
55 static void cb_line_change PARAMS ((cpp_reader *, const cpp_token *, int));
56 static void cb_define   PARAMS ((cpp_reader *, unsigned int, cpp_hashnode *));
57 static void cb_undef    PARAMS ((cpp_reader *, unsigned int, cpp_hashnode *));
58 static void cb_include  PARAMS ((cpp_reader *, unsigned int,
59                                  const unsigned char *, const cpp_token *));
60 static void cb_ident      PARAMS ((cpp_reader *, unsigned int,
61                                    const cpp_string *));
62 static void cb_file_change PARAMS ((cpp_reader *, const struct line_map *));
63 static void cb_def_pragma PARAMS ((cpp_reader *, unsigned int));
64
65 const char *progname;           /* Needs to be global.  */
66 static cpp_reader *pfile;       /* An opaque handle.  */
67 static cpp_options *options;    /* Options of pfile.  */
68 static struct printer print;
69
70 int
71 main (argc, argv)
72      int argc;
73      char **argv;
74 {
75   general_init (argv[0]);
76
77   /* Contruct a reader with default language GNU C89.  */
78   pfile = cpp_create_reader (NULL, CLK_GNUC89);
79   options = cpp_get_options (pfile);
80   
81   do_preprocessing (argc, argv);
82
83   if (cpp_destroy (pfile))
84     return FATAL_EXIT_CODE;
85
86   return SUCCESS_EXIT_CODE;
87 }
88
89 /* Store the program name, and set the locale.  */
90 static void
91 general_init (argv0)
92      const char *argv0;
93 {
94   progname = argv0 + strlen (argv0);
95
96   while (progname != argv0 && ! IS_DIR_SEPARATOR (progname[-1]))
97     --progname;
98
99   xmalloc_set_program_name (progname);
100
101 /* LC_CTYPE determines the character set used by the terminal so it
102    has to be set to output messages correctly.  */
103
104 #ifdef HAVE_LC_MESSAGES
105   setlocale (LC_CTYPE, "");
106   setlocale (LC_MESSAGES, "");
107 #else
108   setlocale (LC_ALL, "");
109 #endif
110
111   (void) bindtextdomain (PACKAGE, localedir);
112   (void) textdomain (PACKAGE);
113 }
114
115 /* Handle switches, preprocess and output.  */
116 static void
117 do_preprocessing (argc, argv)
118      int argc;
119      char **argv;
120 {
121   int argi = 1;  /* Next argument to handle.  */
122
123   argi += cpp_handle_options (pfile, argc - argi , argv + argi);
124   if (CPP_FATAL_ERRORS (pfile))
125     return;
126
127   if (argi < argc)
128     cpp_fatal (pfile, "Invalid option %s", argv[argi]);
129   else
130     cpp_post_options (pfile);
131
132   if (CPP_FATAL_ERRORS (pfile))
133     return;
134
135   /* If cpp_handle_options saw --help or --version on the command
136      line, it will have set pfile->help_only to indicate this.  Exit
137      successfully.  [The library does not exit itself, because
138      e.g. cc1 needs to print its own --help message at this point.]  */
139   if (options->help_only)
140     return;
141
142   /* Initialize the printer structure.  Setting print.line to -1 here
143      is a trick to guarantee that the first token of the file will
144      cause a linemarker to be output by maybe_print_line.  */
145   print.line = (unsigned int) -1;
146   print.printed = 0;
147   print.map = 0;
148   
149   /* Open the output now.  We must do so even if no_output is on,
150      because there may be other output than from the actual
151      preprocessing (e.g. from -dM).  */
152   if (options->out_fname[0] == '\0')
153     print.outf = stdout;
154   else
155     {
156       print.outf = fopen (options->out_fname, "w");
157       if (print.outf == NULL)
158         {
159           cpp_notice_from_errno (pfile, options->out_fname);
160           return;
161         }
162     }
163
164   setup_callbacks ();
165
166   if (cpp_start_read (pfile, options->in_fname))
167     {
168       /* A successful cpp_start_read guarantees that we can call
169          cpp_scan_nooutput or cpp_get_token next.  */
170       if (options->no_output)
171         cpp_scan_nooutput (pfile);
172       else
173         scan_translation_unit (pfile);
174
175       /* -dM command line option.  Should this be in cpp_finish?  */
176       if (options->dump_macros == dump_only)
177         cpp_forall_identifiers (pfile, dump_macro, NULL);
178
179       cpp_finish (pfile);
180     }
181
182   /* Flush any pending output.  */
183   if (print.printed)
184     putc ('\n', print.outf);
185
186   if (ferror (print.outf) || fclose (print.outf))
187     cpp_notice_from_errno (pfile, options->out_fname);
188 }
189
190 /* Set up the callbacks as appropriate.  */
191 static void
192 setup_callbacks ()
193 {
194   cpp_callbacks *cb = cpp_get_callbacks (pfile);
195
196   if (! options->no_output)
197     {
198       cb->line_change = cb_line_change;
199       cb->ident      = cb_ident;
200       cb->def_pragma = cb_def_pragma;
201       if (! options->no_line_commands)
202         cb->file_change = cb_file_change;
203     }
204
205   if (options->dump_includes)
206     cb->include  = cb_include;
207
208   if (options->dump_macros == dump_names
209       || options->dump_macros == dump_definitions)
210     {
211       cb->define = cb_define;
212       cb->undef  = cb_undef;
213     }
214 }
215
216 /* Writes out the preprocessed file.  Alternates between two tokens,
217    so that we can avoid accidental token pasting.  */
218 static void
219 scan_translation_unit (pfile)
220      cpp_reader *pfile;
221 {
222   unsigned int index;
223   cpp_token tokens[2], *token;
224
225   for (index = 0;; index = 1 - index)
226     {
227       token = &tokens[index];
228       cpp_get_token (pfile, token);
229
230       if (token->type == CPP_EOF)
231         break;
232
233       if ((token->flags & (PREV_WHITE | AVOID_LPASTE | BOL)) == AVOID_LPASTE
234           && cpp_avoid_paste (pfile, &tokens[1 - index], token))
235         token->flags |= PREV_WHITE;
236
237       cpp_output_token (token, print.outf);
238       if (token->type == CPP_STRING || token->type == CPP_WSTRING
239           || token->type == CPP_COMMENT)
240         check_multiline_token (&token->val.str);
241     }
242 }
243
244 /* Adjust print.line for newlines embedded in tokens.  */
245 static void
246 check_multiline_token (str)
247      cpp_string *str;
248 {
249   unsigned int i;
250
251   for (i = 0; i < str->len; i++)
252     if (str->text[i] == '\n')
253       print.line++;
254 }
255
256 /* If the token read on logical line LINE needs to be output on a
257    different line to the current one, output the required newlines or
258    a line marker, and return 1.  Otherwise return 0.  */
259
260 static void
261 maybe_print_line (map, line)
262      const struct line_map *map;
263      unsigned int line;
264 {
265   /* End the previous line of text.  */
266   if (print.printed)
267     {
268       putc ('\n', print.outf);
269       print.line++;
270       print.printed = 0;
271     }
272
273   if (line >= print.line && line < print.line + 8)
274     {
275       while (line > print.line)
276         {
277           putc ('\n', print.outf);
278           print.line++;
279         }
280     }
281   else
282     print_line (map, line, "");
283 }
284
285 /* Output a line marker for logical line LINE.  Special flags are "1"
286    or "2" indicating entering or leaving a file.  */
287 static void
288 print_line (map, line, special_flags)
289      const struct line_map *map;
290      unsigned int line;
291      const char *special_flags;
292 {
293   /* End any previous line of text.  */
294   if (print.printed)
295     putc ('\n', print.outf);
296   print.printed = 0;
297
298   print.line = line;
299   if (! options->no_line_commands)
300     {
301       fprintf (print.outf, "# %u \"%s\"%s",
302                SOURCE_LINE (map, print.line), map->to_file, special_flags);
303
304       if (map->sysp == 2)
305         fputs (" 3 4", print.outf);
306       else if (map->sysp == 1)
307         fputs (" 3", print.outf);
308
309       putc ('\n', print.outf);
310     }
311 }
312
313 /* Called when a line of output is started.  TOKEN is the first token
314    of the line, and maybe be CPP_EOF.  */
315
316 static void
317 cb_line_change (pfile, token, parsing_args)
318      cpp_reader *pfile ATTRIBUTE_UNUSED;
319      const cpp_token *token;
320      int parsing_args;
321 {
322   if (token->type == CPP_EOF || parsing_args)
323     return;
324
325   maybe_print_line (print.map, token->line);
326   print.printed = 1;
327
328   /* Supply enough spaces to put this token in its original column,
329      one space per column greater than 2, since scan_translation_unit
330      will provide a space if PREV_WHITE.  Don't bother trying to
331      reconstruct tabs; we can't get it right in general, and nothing
332      ought to care.  Some things do care; the fault lies with them.  */
333   if (token->col > 2)
334     {
335       unsigned int spaces = token->col - 2;
336
337       while (spaces--)
338         putc (' ', print.outf);
339     }
340 }
341
342 static void
343 cb_ident (pfile, line, str)
344      cpp_reader *pfile ATTRIBUTE_UNUSED;
345      unsigned int line;
346      const cpp_string * str;
347 {
348   maybe_print_line (print.map, line);
349   fprintf (print.outf, "#ident \"%s\"\n", str->text);
350   print.line++;
351 }
352
353 static void
354 cb_define (pfile, line, node)
355      cpp_reader *pfile;
356      unsigned int line;
357      cpp_hashnode *node;
358 {
359   maybe_print_line (print.map, line);
360   fputs ("#define ", print.outf);
361
362   /* -dD command line option.  */
363   if (options->dump_macros == dump_definitions)
364     fputs ((const char *) cpp_macro_definition (pfile, node), print.outf);
365   else
366     fputs ((const char *) NODE_NAME (node), print.outf);
367
368   putc ('\n', print.outf);
369   print.line++;
370 }
371
372 static void
373 cb_undef (pfile, line, node)
374      cpp_reader *pfile ATTRIBUTE_UNUSED;
375      unsigned int line;
376      cpp_hashnode *node;
377 {
378   maybe_print_line (print.map, line);
379   fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
380   print.line++;
381 }
382
383 static void
384 cb_include (pfile, line, dir, header)
385      cpp_reader *pfile;
386      unsigned int line;
387      const unsigned char *dir;
388      const cpp_token *header;
389 {
390   maybe_print_line (print.map, line);
391   fprintf (print.outf, "#%s %s\n", dir, cpp_token_as_text (pfile, header));
392   print.line++;
393 }
394
395 /* The file name, line number or system header flags have changed, as
396    described in MAP.  From this point on, the old print.map might be
397    pointing to freed memory, and so must not be dereferenced.  */
398
399 static void
400 cb_file_change (pfile, map)
401      cpp_reader *pfile ATTRIBUTE_UNUSED;
402      const struct line_map *map;
403 {
404   const char *flags = "";
405
406   /* First time?  */
407   if (print.map == NULL)
408     {
409       /* Avoid printing foo.i when the main file is foo.c.  */
410       if (!options->preprocessed)
411         print_line (map, map->from_line, flags);
412     }
413   else
414     {
415       /* Bring current file to correct line when entering a new file.  */
416       if (map->reason == LC_ENTER)
417         maybe_print_line (map - 1, map->from_line - 1);
418
419       if (map->reason == LC_ENTER)
420         flags = " 1";
421       else if (map->reason == LC_LEAVE)
422         flags = " 2";
423       print_line (map, map->from_line, flags);
424     }
425
426   print.map = map;
427 }
428
429 /* Copy a #pragma directive to the preprocessed output.  */
430 static void
431 cb_def_pragma (pfile, line)
432      cpp_reader *pfile;
433      unsigned int line;
434 {
435   maybe_print_line (print.map, line);
436   fputs ("#pragma ", print.outf);
437   cpp_output_line (pfile, print.outf);
438   print.line++;
439 }
440
441 /* Dump out the hash table.  */
442 static int
443 dump_macro (pfile, node, v)
444      cpp_reader *pfile;
445      cpp_hashnode *node;
446      void *v ATTRIBUTE_UNUSED;
447 {
448   if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
449     {
450       fputs ("#define ", print.outf);
451       fputs ((const char *) cpp_macro_definition (pfile, node), print.outf);
452       putc ('\n', print.outf);
453       print.line++;
454     }
455
456   return 1;
457 }