OSDN Git Service

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