OSDN Git Service

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