OSDN Git Service

* cpperror.c (print_containing_files): Moved to line-map.c.
[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 char *syshdr_flags;     /* System header flags, if any.  */
35   unsigned int line;            /* Line currently being written.  */
36   unsigned char printed;        /* Nonzero if something output at line.  */
37   struct line_map *map;         /* Logical to physical line mappings.  */
38 };
39
40 int main                PARAMS ((int, char **));
41 static void general_init PARAMS ((const char *));
42 static void do_preprocessing PARAMS ((int, char **));
43 static void setup_callbacks PARAMS ((void));
44
45 /* General output routines.  */
46 static void scan_translation_unit PARAMS ((cpp_reader *));
47 static void check_multiline_token PARAMS ((cpp_string *));
48 static void printer_init PARAMS ((void));
49 static int dump_macro PARAMS ((cpp_reader *, cpp_hashnode *, void *));
50
51 static void print_line PARAMS ((unsigned int, const char *));
52 static void maybe_print_line PARAMS ((unsigned int));
53
54 /* Callback routines for the parser.   Most of these are active only
55    in specific modes.  */
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 cpp_file_change *));
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   /* Open the output now.  We must do so even if no_output is on,
143      because there may be other output than from the actual
144      preprocessing (e.g. from -dM).  */
145   printer_init ();
146   if (print.outf == NULL)
147     {
148       cpp_notice_from_errno (pfile, options->out_fname);
149       return;
150     }
151
152   setup_callbacks ();
153
154   if (cpp_start_read (pfile, options->in_fname))
155     {
156       /* A successful cpp_start_read guarantees that we can call
157          cpp_scan_nooutput or cpp_get_token next.  */
158       if (options->no_output)
159         cpp_scan_nooutput (pfile);
160       else
161         scan_translation_unit (pfile);
162
163       /* -dM command line option.  Should this be in cpp_finish?  */
164       if (options->dump_macros == dump_only)
165         cpp_forall_identifiers (pfile, dump_macro, NULL);
166
167       cpp_finish (pfile);
168     }
169
170   /* Flush any pending output.  */
171   if (print.printed)
172     putc ('\n', print.outf);
173
174   if (ferror (print.outf) || fclose (print.outf))
175     cpp_notice_from_errno (pfile, options->out_fname);
176 }
177
178 /* Set up the callbacks as appropriate.  */
179 static void
180 setup_callbacks ()
181 {
182   cpp_callbacks *cb = cpp_get_callbacks (pfile);
183
184   if (! options->no_output)
185     {
186       cb->ident      = cb_ident;
187       cb->def_pragma = cb_def_pragma;
188       if (! options->no_line_commands)
189         cb->file_change = cb_file_change;
190     }
191
192   if (options->dump_includes)
193     cb->include  = cb_include;
194
195   if (options->dump_macros == dump_names
196       || options->dump_macros == dump_definitions)
197     {
198       cb->define = cb_define;
199       cb->undef  = cb_undef;
200     }
201 }
202
203 /* Writes out the preprocessed file.  Alternates between two tokens,
204    so that we can avoid accidental token pasting.  */
205 static void
206 scan_translation_unit (pfile)
207      cpp_reader *pfile;
208 {
209   unsigned int index, line;
210   cpp_token tokens[2], *token;
211
212   for (index = 0;; index = 1 - index)
213     {
214       token = &tokens[index];
215       cpp_get_token (pfile, token);
216
217       if (token->type == CPP_EOF)
218         break;
219
220       line = cpp_get_line (pfile)->output_line;
221       if (print.line != line)
222         {
223           unsigned int col = cpp_get_line (pfile)->col;
224
225           /* Supply enough whitespace to put this token in its original
226              column.  Don't bother trying to reconstruct tabs; we can't
227              get it right in general, and nothing ought to care.  (Yes,
228              some things do care; the fault lies with them.)  */
229           maybe_print_line (line);
230           if (col > 1)
231             {
232               if (token->flags & PREV_WHITE)
233                 col--;
234               while (--col)
235                 putc (' ', print.outf);
236             }
237         }
238       else if ((token->flags & (PREV_WHITE | AVOID_LPASTE))
239                == AVOID_LPASTE
240                && cpp_avoid_paste (pfile, &tokens[1 - index], token))
241         token->flags |= PREV_WHITE;
242       /* Special case '# <directive name>': insert a space between
243          the # and the token.  This will prevent it from being
244          treated as a directive when this code is re-preprocessed.
245          XXX Should do this only at the beginning of a line, but how?  */
246       else if (token->type == CPP_NAME && token->val.node->directive_index
247                && tokens[1 - index].type == CPP_HASH)
248         token->flags |= PREV_WHITE;
249
250       cpp_output_token (token, print.outf);
251       print.printed = 1;
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      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 /* Initialize a cpp_printer structure.  As a side effect, open the
271    output file.  If print.outf is NULL an error occurred.  */
272 static void
273 printer_init ()
274 {
275   /* Setting print.line to -1 here guarantees that the first token of
276      the file will cause a linemarker to be output by maybe_print_line.  */
277   print.line = (unsigned int) -1;
278   print.printed = 0;
279   print.map = 0;
280
281   if (options->out_fname[0] == '\0')
282     print.outf = stdout;
283   else
284     print.outf = fopen (options->out_fname, "w");
285 }
286
287 /* If the token read on logical line LINE needs to be output on a
288    different line to the current one, output the required newlines or
289    a line marker, and return 1.  Otherwise return 0.  */
290
291 static void
292 maybe_print_line (line)
293      unsigned int line;
294 {
295   /* End the previous line of text.  */
296   if (print.printed)
297     {
298       putc ('\n', print.outf);
299       print.line++;
300       print.printed = 0;
301     }
302
303   if (line >= print.line && line < print.line + 8)
304     {
305       while (line > print.line)
306         {
307           putc ('\n', print.outf);
308           print.line++;
309         }
310     }
311   else
312     print_line (line, "");
313 }
314
315 static void
316 print_line (line, special_flags)
317      unsigned int line;
318      const char *special_flags;
319 {
320   /* End any previous line of text.  */
321   if (print.printed)
322     putc ('\n', print.outf);
323   print.printed = 0;
324
325   print.line = line;
326   if (! options->no_line_commands)
327     fprintf (print.outf, "# %u \"%s\"%s%s\n",
328              SOURCE_LINE (print.map, print.line),
329              print.map->to_file, special_flags, print.syshdr_flags);
330 }
331
332 /* Callbacks.  */
333
334 static void
335 cb_ident (pfile, line, str)
336      cpp_reader *pfile ATTRIBUTE_UNUSED;
337      unsigned int line;
338      const cpp_string * str;
339 {
340   maybe_print_line (line);
341   fprintf (print.outf, "#ident \"%s\"\n", str->text);
342   print.line++;
343 }
344
345 static void
346 cb_define (pfile, line, node)
347      cpp_reader *pfile;
348      unsigned int line;
349      cpp_hashnode *node;
350 {
351   maybe_print_line (line);
352   fputs ("#define ", print.outf);
353
354   /* -dD command line option.  */
355   if (options->dump_macros == dump_definitions)
356     fputs ((const char *) cpp_macro_definition (pfile, node), print.outf);
357   else
358     fputs ((const char *) NODE_NAME (node), print.outf);
359
360   putc ('\n', print.outf);
361   print.line++;
362 }
363
364 static void
365 cb_undef (pfile, line, node)
366      cpp_reader *pfile ATTRIBUTE_UNUSED;
367      unsigned int line;
368      cpp_hashnode *node;
369 {
370   maybe_print_line (line);
371   fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
372   print.line++;
373 }
374
375 static void
376 cb_include (pfile, line, dir, header)
377      cpp_reader *pfile;
378      unsigned int line;
379      const unsigned char *dir;
380      const cpp_token *header;
381 {
382   maybe_print_line (line);
383   fprintf (print.outf, "#%s %s\n", dir, cpp_token_as_text (pfile, header));
384   print.line++;
385 }
386
387 static void
388 cb_file_change (pfile, fc)
389      cpp_reader *pfile ATTRIBUTE_UNUSED;
390      const cpp_file_change *fc;
391 {
392   bool first_time = print.map == NULL;
393
394   /* Bring current file to correct line.  We handle the first file
395      change callback specially, so that a first line of "# 1 "foo.c"
396      in file foo.i outputs just the foo.c line, and not a foo.i line.  */
397   if (fc->reason == LC_ENTER && !first_time)
398     maybe_print_line (fc->line - 1);
399
400   print.map = fc->map;
401   if (fc->externc)
402     print.syshdr_flags = " 3 4";
403   else if (fc->sysp)
404     print.syshdr_flags = " 3";
405   else
406     print.syshdr_flags = "";
407
408   if (!first_time)
409     {
410       const char *flags = "";
411
412       if (fc->reason == LC_ENTER)
413         flags = " 1";
414       else if (fc->reason == LC_LEAVE)
415         flags = " 2";
416
417       print_line (fc->line, flags);
418     }
419 }
420
421 /* Copy a #pragma directive to the preprocessed output.  LINE is the
422    line of the current source file, not the logical line.  */
423 static void
424 cb_def_pragma (pfile, line)
425      cpp_reader *pfile;
426      unsigned int line;
427 {
428   maybe_print_line (line);
429   fputs ("#pragma ", print.outf);
430   cpp_output_line (pfile, print.outf);
431   print.line++;
432 }
433
434 /* Dump out the hash table.  */
435 static int
436 dump_macro (pfile, node, v)
437      cpp_reader *pfile;
438      cpp_hashnode *node;
439      void *v ATTRIBUTE_UNUSED;
440 {
441   if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
442     {
443       fputs ("#define ", print.outf);
444       fputs ((const char *) cpp_macro_definition (pfile, node), print.outf);
445       putc ('\n', print.outf);
446       print.line++;
447     }
448
449   return 1;
450 }