OSDN Git Service

* c-lex.c (cb_enter_file, cb_leave_file, cb_rename_file):
[pf3gnuchains/gcc-fork.git] / gcc / cppmain.c
1 /* CPP main program, using CPP Library.
2    Copyright (C) 1995, 1997, 1998, 1999, 2000 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 2, 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; if not, write to the Free Software
17 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19  In other words, you are welcome to use, share and improve this program.
20  You are forbidden to forbid anyone else to use, share and improve
21  what you give them.   Help stamp out software-hoarding!  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "cpplib.h"
26 #include "intl.h"
27
28 /* Encapsulates state used to convert the stream of tokens coming from
29    cpp_get_token back into a text file.  */
30 struct printer
31 {
32   FILE *outf;                   /* stream to write to.  */
33   const char *last_fname;       /* previous file name.  */
34   const char *syshdr_flags;     /* system header flags, if any.  */
35   unsigned int lineno;          /* line currently being written.  */
36   unsigned char printed;        /* nonzero if something output at lineno.  */
37   unsigned char no_line_dirs;   /* nonzero to output no line directives.  */
38 };
39
40 int main                PARAMS ((int, char **));
41
42 /* General output routines.  */
43 static void scan_buffer PARAMS ((cpp_reader *));
44 static int printer_init PARAMS ((cpp_reader *));
45 static int dump_macro PARAMS ((cpp_reader *, cpp_hashnode *, void *));
46
47 static void print_line PARAMS ((const char *));
48 static void maybe_print_line PARAMS ((unsigned int));
49
50 /* Callback routines for the parser.   Most of these are active only
51    in specific modes.  */
52 static void cb_define   PARAMS ((cpp_reader *, cpp_hashnode *));
53 static void cb_undef    PARAMS ((cpp_reader *, cpp_hashnode *));
54 static void cb_include  PARAMS ((cpp_reader *, const unsigned char *,
55                                  const cpp_token *));
56 static void cb_ident      PARAMS ((cpp_reader *, const cpp_string *));
57 static void cb_change_file PARAMS ((cpp_reader *, const cpp_file_change *));
58 static void cb_def_pragma PARAMS ((cpp_reader *));
59 static void do_pragma_implementation PARAMS ((cpp_reader *));
60
61 const char *progname;
62 static cpp_reader parse_in;
63 static struct printer print;
64
65 int
66 main (argc, argv)
67      int argc;
68      char **argv;
69 {
70   char *p;
71   cpp_reader *pfile = &parse_in;
72   int argi = 1;  /* Next argument to handle.  */
73
74   p = argv[0] + strlen (argv[0]);
75   while (p != argv[0] && ! IS_DIR_SEPARATOR (p[-1])) --p;
76   progname = p;
77
78   xmalloc_set_program_name (progname);
79
80 #ifdef HAVE_LC_MESSAGES
81   setlocale (LC_MESSAGES, "");
82 #endif
83   (void) bindtextdomain (PACKAGE, localedir);
84   (void) textdomain (PACKAGE);
85
86   cpp_init ();
87   /* Default language is GNU C89.  */
88   cpp_reader_init (pfile, CLK_GNUC89);
89   
90   argi += cpp_handle_options (pfile, argc - argi , argv + argi);
91   if (argi < argc && ! CPP_FATAL_ERRORS (pfile))
92     cpp_fatal (pfile, "Invalid option %s", argv[argi]);
93   if (CPP_FATAL_ERRORS (pfile))
94     return (FATAL_EXIT_CODE);
95
96   /* Open the output now.  We must do so even if no_output is on,
97      because there may be other output than from the actual
98      preprocessing (e.g. from -dM).  */
99   if (printer_init (pfile))
100     return (FATAL_EXIT_CODE);
101
102   /* Set callbacks.  */
103   if (! CPP_OPTION (pfile, no_output))
104     {
105       pfile->cb.ident      = cb_ident;
106       pfile->cb.def_pragma = cb_def_pragma;
107       if (! CPP_OPTION (pfile, no_line_commands))
108         pfile->cb.change_file = cb_change_file;
109     }
110
111   if (CPP_OPTION (pfile, dump_includes))
112     pfile->cb.include  = cb_include;
113
114   if (CPP_OPTION (pfile, debug_output)
115       || CPP_OPTION (pfile, dump_macros) == dump_names
116       || CPP_OPTION (pfile, dump_macros) == dump_definitions)
117     {
118       pfile->cb.define = cb_define;
119       pfile->cb.undef  = cb_undef;
120       pfile->cb.poison = cb_def_pragma;
121     }
122
123   /* Register one #pragma which needs special handling.  */
124   cpp_register_pragma(pfile, 0, "implementation", do_pragma_implementation);
125   cpp_register_pragma(pfile, "GCC", "implementation", do_pragma_implementation);
126
127   if (! cpp_start_read (pfile, CPP_OPTION (pfile, in_fname)))
128     return (FATAL_EXIT_CODE);
129
130   if (CPP_BUFFER (pfile))
131     {
132       if (CPP_OPTION (pfile, no_output))
133         cpp_scan_buffer_nooutput (pfile);
134       else
135         scan_buffer (pfile);
136     }
137
138   /* -dM command line option.  */
139   if (CPP_OPTION (pfile, dump_macros) == dump_only)
140     cpp_forall_identifiers (pfile, dump_macro, NULL);
141
142   cpp_finish (pfile);
143   cpp_cleanup (pfile);
144
145   /* Flush any pending output.  */
146   if (print.printed)
147     putc ('\n', print.outf);
148   if (ferror (print.outf) || fclose (print.outf))
149     cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));
150
151   if (parse_in.errors)
152     return (FATAL_EXIT_CODE);
153   return (SUCCESS_EXIT_CODE);
154 }
155
156 /* Writes out the preprocessed file.  Alternates between two tokens,
157    so that we can avoid accidental token pasting.  */
158 static void
159 scan_buffer (pfile)
160      cpp_reader *pfile;
161 {
162   unsigned int index, line;
163   cpp_token tokens[2], *token;
164
165   do
166     {
167       for (index = 0;; index = 1 - index)
168         {
169           token = &tokens[index];
170           cpp_get_token (pfile, token);
171
172           if (token->type == CPP_EOF)
173             break;
174
175           line = cpp_get_line (pfile)->output_line;
176           if (print.lineno != line)
177             {
178               unsigned int col = cpp_get_line (pfile)->col;
179
180               /* Supply enough whitespace to put this token in its original
181                  column.  Don't bother trying to reconstruct tabs; we can't
182                  get it right in general, and nothing ought to care.  (Yes,
183                  some things do care; the fault lies with them.)  */
184               maybe_print_line (line);
185               if (col > 1)
186                 {
187                   if (token->flags & PREV_WHITE)
188                     col--;
189                   while (--col)
190                     putc (' ', print.outf);
191                 }
192             }
193           else if (print.printed
194                    && ! (token->flags & PREV_WHITE)
195                    && CPP_OPTION (pfile, lang) != CLK_ASM
196                    && cpp_avoid_paste (pfile, &tokens[1 - index], token))
197             token->flags |= PREV_WHITE;
198
199           cpp_output_token (token, print.outf);
200           print.printed = 1;
201         }
202     }
203   while (cpp_pop_buffer (pfile) != 0);
204 }
205
206 /* Initialize a cpp_printer structure.  As a side effect, open the
207    output file.  */
208 static int
209 printer_init (pfile)
210      cpp_reader *pfile;
211 {
212   print.last_fname = 0;
213   print.lineno = 0;
214   print.printed = 0;
215   print.no_line_dirs = CPP_OPTION (pfile, no_line_commands);
216
217   if (CPP_OPTION (pfile, out_fname) == NULL)
218     CPP_OPTION (pfile, out_fname) = "";
219   
220   if (CPP_OPTION (pfile, out_fname)[0] == '\0')
221     print.outf = stdout;
222   else
223     {
224       print.outf = fopen (CPP_OPTION (pfile, out_fname), "w");
225       if (! print.outf)
226         {
227           cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));
228           return 1;
229         }
230     }
231   return 0;
232 }
233
234 /* Newline-terminate any output line currently in progress.  If
235    appropriate, write the current line number to the output, or pad
236    with newlines so the output line matches the current line.  */
237 static void
238 maybe_print_line (line)
239      unsigned int line;
240 {
241   /* End the previous line of text (probably only needed until we get
242      multi-line tokens fixed).  */
243   if (print.printed)
244     {
245       putc ('\n', print.outf);
246       print.lineno++;
247       print.printed = 0;
248     }
249
250   if (print.no_line_dirs)
251     return;
252
253   if (line >= print.lineno && line < print.lineno + 8)
254     {
255       while (line > print.lineno)
256         {
257           putc ('\n', print.outf);
258           print.lineno++;
259         }
260     }
261   else
262     {
263       print.lineno = line;
264       print_line ("");
265     }
266 }
267
268 static void
269 print_line (special_flags)
270   const char *special_flags;
271 {
272   /* End any previous line of text.  */
273   if (print.printed)
274     putc ('\n', print.outf);
275   print.printed = 0;
276
277   fprintf (print.outf, "# %u \"%s\"%s%s\n",
278            print.lineno, print.last_fname, special_flags, print.syshdr_flags);
279 }
280
281 /* Callbacks */
282
283 static void
284 cb_ident (pfile, str)
285      cpp_reader *pfile ATTRIBUTE_UNUSED;
286      const cpp_string * str;
287 {
288   maybe_print_line (cpp_get_line (pfile)->output_line);
289   fprintf (print.outf, "#ident \"%.*s\"\n", (int) str->len, str->text);
290   print.lineno++;
291 }
292
293 static void
294 cb_define (pfile, node)
295      cpp_reader *pfile;
296      cpp_hashnode *node;
297 {
298   if (pfile->done_initializing)
299     {
300       maybe_print_line (cpp_get_line (pfile)->output_line);
301       fprintf (print.outf, "#define %s", node->name);
302
303       /* -dD or -g3 command line options.  */
304       if (CPP_OPTION (pfile, debug_output)
305           || CPP_OPTION (pfile, dump_macros) == dump_definitions)
306         fputs ((const char *) cpp_macro_definition (pfile, node), print.outf);
307
308       putc ('\n', print.outf);
309       print.lineno++;
310     }
311 }
312
313 static void
314 cb_undef (pfile, node)
315      cpp_reader *pfile;
316      cpp_hashnode *node;
317 {
318   if (pfile->done_initializing)
319     {
320       maybe_print_line (cpp_get_line (pfile)->output_line);
321       fprintf (print.outf, "#undef %s\n", node->name);
322       print.lineno++;
323     }
324 }
325
326 static void
327 cb_include (pfile, dir, header)
328      cpp_reader *pfile ATTRIBUTE_UNUSED;
329      const unsigned char *dir;
330      const cpp_token *header;
331 {
332   maybe_print_line (cpp_get_line (pfile)->output_line);
333   fprintf (print.outf, "#%s %s\n", dir, cpp_token_as_text (pfile, header));
334   print.lineno++;
335 }
336
337 static void
338 cb_change_file (pfile, fc)
339      cpp_reader *pfile ATTRIBUTE_UNUSED;
340      const cpp_file_change *fc;
341 {
342   const char *flags;
343
344   /* Bring current file to correct line (except first file).  */
345   if (fc->reason == FC_ENTER && fc->from.filename)
346     maybe_print_line (fc->from.lineno);
347
348   print.lineno = fc->to.lineno;
349   print.last_fname = fc->to.filename;
350   if (fc->externc)
351     print.syshdr_flags = " 3 4";
352   else if (fc->sysp)
353     print.syshdr_flags = " 3";
354   else
355     print.syshdr_flags = "";
356
357   switch (fc->reason)
358     {
359     case FC_ENTER : flags = fc->from.filename ? " 1": ""; break;
360     case FC_LEAVE : flags = " 2"; break;
361     case FC_RENAME: flags = ""; break;
362     }
363
364   print_line (flags);
365 }
366
367 static void
368 cb_def_pragma (pfile)
369      cpp_reader *pfile;
370 {
371   maybe_print_line (cpp_get_line (pfile)->output_line);
372   fputs ("#pragma ", print.outf);
373   cpp_output_line (pfile, print.outf);
374   print.lineno++;
375 }
376
377 static void
378 do_pragma_implementation (pfile)
379      cpp_reader *pfile;
380 {
381   /* Be quiet about `#pragma implementation' for a file only if it hasn't
382      been included yet.  */
383   cpp_token token;
384
385   cpp_start_lookahead (pfile);
386   cpp_get_token (pfile, &token);
387   cpp_stop_lookahead (pfile, 0);
388
389   /* If it's not a string, pass it through and let the front end complain.  */
390   if (token.type == CPP_STRING)
391     {
392      /* Make a NUL-terminated copy of the string.  */
393       char *filename = alloca (token.val.str.len + 1);
394       memcpy (filename, token.val.str.text, token.val.str.len);
395       filename[token.val.str.len] = '\0';
396       if (cpp_included (pfile, filename))
397         cpp_warning (pfile,
398              "#pragma GCC implementation for \"%s\" appears after file is included",
399                      filename);
400     }
401   else if (token.type != CPP_EOF)
402     {
403       cpp_error (pfile, "malformed #pragma GCC implementation");
404       return;
405     }
406
407   /* Output?  This is nasty, but we don't have [GCC] implementation in
408      the buffer.  */
409   if (pfile->cb.def_pragma)
410     {
411       maybe_print_line (cpp_get_line (pfile)->output_line);
412       fputs ("#pragma GCC implementation ", print.outf);
413       cpp_output_line (pfile, print.outf);
414       print.lineno++;
415     }
416 }
417
418 /* Dump out the hash table.  */
419 static int
420 dump_macro (pfile, node, v)
421      cpp_reader *pfile;
422      cpp_hashnode *node;
423      void *v ATTRIBUTE_UNUSED;
424 {
425   if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
426     {
427       fprintf (print.outf, "#define %s", node->name);
428       fputs ((const char *) cpp_macro_definition (pfile, node), print.outf);
429       putc ('\n', print.outf);
430       print.lineno++;
431     }
432
433   return 1;
434 }