* calls.c (calls_function_1, case CONSTRUCTOR): New case.
[official-gcc.git] / gcc / cppmain.c
blobace260ca8cae3a3d28db7eaa05ec89353dfcde29
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.
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.
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.
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.
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! */
23 #include "config.h"
24 #include "system.h"
25 #include "cpplib.h"
26 #include "intl.h"
28 /* Encapsulates state used to convert the stream of tokens coming from
29 cpp_get_token back into a text file. */
30 struct printer
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. */
40 int main PARAMS ((int, char **));
41 static void general_init PARAMS ((const char *));
42 static void setup_callbacks PARAMS ((void));
44 /* General output routines. */
45 static void scan_buffer PARAMS ((cpp_reader *));
46 static void check_multiline_token PARAMS ((cpp_string *));
47 static int printer_init PARAMS ((cpp_reader *));
48 static int dump_macro PARAMS ((cpp_reader *, cpp_hashnode *, void *));
50 static void print_line PARAMS ((const char *));
51 static void maybe_print_line PARAMS ((unsigned int));
53 /* Callback routines for the parser. Most of these are active only
54 in specific modes. */
55 static void cb_define PARAMS ((cpp_reader *, cpp_hashnode *));
56 static void cb_undef PARAMS ((cpp_reader *, cpp_hashnode *));
57 static void cb_include PARAMS ((cpp_reader *, const unsigned char *,
58 const cpp_token *));
59 static void cb_ident PARAMS ((cpp_reader *, const cpp_string *));
60 static void cb_file_change PARAMS ((cpp_reader *, const cpp_file_change *));
61 static void cb_def_pragma PARAMS ((cpp_reader *));
62 static void do_pragma_implementation PARAMS ((cpp_reader *));
64 const char *progname; /* Needs to be global. */
65 static cpp_reader *pfile;
66 static struct printer print;
68 int
69 main (argc, argv)
70 int argc;
71 char **argv;
73 int argi = 1; /* Next argument to handle. */
75 general_init (argv[0]);
76 /* Default language is GNU C89. */
77 pfile = cpp_create_reader (CLK_GNUC89);
79 argi += cpp_handle_options (pfile, argc - argi , argv + argi);
80 if (argi < argc && ! CPP_FATAL_ERRORS (pfile))
81 cpp_fatal (pfile, "Invalid option %s", argv[argi]);
82 if (CPP_FATAL_ERRORS (pfile))
83 return (FATAL_EXIT_CODE);
85 /* Open the output now. We must do so even if no_output is on,
86 because there may be other output than from the actual
87 preprocessing (e.g. from -dM). */
88 if (printer_init (pfile))
89 return (FATAL_EXIT_CODE);
91 setup_callbacks ();
93 if (! cpp_start_read (pfile, CPP_OPTION (pfile, in_fname)))
94 return (FATAL_EXIT_CODE);
96 if (CPP_BUFFER (pfile))
98 if (CPP_OPTION (pfile, no_output))
99 cpp_scan_buffer_nooutput (pfile, 1);
100 else
101 scan_buffer (pfile);
104 /* -dM command line option. */
105 if (CPP_OPTION (pfile, dump_macros) == dump_only)
106 cpp_forall_identifiers (pfile, dump_macro, NULL);
108 cpp_finish (pfile);
109 cpp_cleanup (pfile);
111 /* Flush any pending output. */
112 if (print.printed)
113 putc ('\n', print.outf);
114 if (ferror (print.outf) || fclose (print.outf))
115 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));
117 if (pfile->errors)
118 return (FATAL_EXIT_CODE);
119 return (SUCCESS_EXIT_CODE);
122 /* Store the program name, and set the locale. */
123 static void
124 general_init (const char *argv0)
126 progname = argv0 + strlen (argv0);
128 while (progname != argv0 && ! IS_DIR_SEPARATOR (progname[-1]))
129 --progname;
131 xmalloc_set_program_name (progname);
133 /* LC_CTYPE determines the character set used by the terminal so it has be set
134 to output messages correctly. */
136 #ifdef HAVE_LC_MESSAGES
137 setlocale (LC_CTYPE, "");
138 setlocale (LC_MESSAGES, "");
139 #else
140 setlocale (LC_ALL, "");
141 #endif
143 (void) bindtextdomain (PACKAGE, localedir);
144 (void) textdomain (PACKAGE);
147 /* Set up the callbacks and register the pragmas we handle. */
148 static void
149 setup_callbacks ()
151 /* Set callbacks. */
152 if (! CPP_OPTION (pfile, no_output))
154 pfile->cb.ident = cb_ident;
155 pfile->cb.def_pragma = cb_def_pragma;
156 if (! CPP_OPTION (pfile, no_line_commands))
157 pfile->cb.file_change = cb_file_change;
160 if (CPP_OPTION (pfile, dump_includes))
161 pfile->cb.include = cb_include;
163 if (CPP_OPTION (pfile, debug_output)
164 || CPP_OPTION (pfile, dump_macros) == dump_names
165 || CPP_OPTION (pfile, dump_macros) == dump_definitions)
167 pfile->cb.define = cb_define;
168 pfile->cb.undef = cb_undef;
169 pfile->cb.poison = cb_def_pragma;
172 /* Register one #pragma which needs special handling. */
173 cpp_register_pragma(pfile, 0, "implementation", do_pragma_implementation);
174 cpp_register_pragma(pfile, "GCC", "implementation", do_pragma_implementation);
177 /* Writes out the preprocessed file. Alternates between two tokens,
178 so that we can avoid accidental token pasting. */
179 static void
180 scan_buffer (pfile)
181 cpp_reader *pfile;
183 unsigned int index, line;
184 cpp_token tokens[2], *token;
188 for (index = 0;; index = 1 - index)
190 token = &tokens[index];
191 cpp_get_token (pfile, token);
193 if (token->type == CPP_EOF)
194 break;
196 line = cpp_get_line (pfile)->output_line;
197 if (print.lineno != line)
199 unsigned int col = cpp_get_line (pfile)->col;
201 /* Supply enough whitespace to put this token in its original
202 column. Don't bother trying to reconstruct tabs; we can't
203 get it right in general, and nothing ought to care. (Yes,
204 some things do care; the fault lies with them.) */
205 maybe_print_line (line);
206 if (col > 1)
208 if (token->flags & PREV_WHITE)
209 col--;
210 while (--col)
211 putc (' ', print.outf);
214 else if (print.printed
215 && ! (token->flags & PREV_WHITE)
216 && CPP_OPTION (pfile, lang) != CLK_ASM
217 && cpp_avoid_paste (pfile, &tokens[1 - index], token))
218 token->flags |= PREV_WHITE;
220 cpp_output_token (token, print.outf);
221 print.printed = 1;
222 if (token->type == CPP_STRING || token->type == CPP_WSTRING
223 || token->type == CPP_COMMENT)
224 check_multiline_token (&token->val.str);
227 while (cpp_pop_buffer (pfile) != 0);
230 /* Adjust print.lineno for newlines embedded in tokens. */
231 static void
232 check_multiline_token (str)
233 cpp_string *str;
235 unsigned int i;
237 for (i = 0; i < str->len; i++)
238 if (str->text[i] == '\n')
239 print.lineno++;
242 /* Initialize a cpp_printer structure. As a side effect, open the
243 output file. */
244 static int
245 printer_init (pfile)
246 cpp_reader *pfile;
248 print.last_fname = 0;
249 print.lineno = 0;
250 print.printed = 0;
251 print.no_line_dirs = CPP_OPTION (pfile, no_line_commands);
253 if (CPP_OPTION (pfile, out_fname) == NULL)
254 CPP_OPTION (pfile, out_fname) = "";
256 if (CPP_OPTION (pfile, out_fname)[0] == '\0')
257 print.outf = stdout;
258 else
260 print.outf = fopen (CPP_OPTION (pfile, out_fname), "w");
261 if (! print.outf)
263 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));
264 return 1;
267 return 0;
270 /* Newline-terminate any output line currently in progress. If
271 appropriate, write the current line number to the output, or pad
272 with newlines so the output line matches the current line. */
273 static void
274 maybe_print_line (line)
275 unsigned int line;
277 /* End the previous line of text (probably only needed until we get
278 multi-line tokens fixed). */
279 if (print.printed)
281 putc ('\n', print.outf);
282 print.lineno++;
283 print.printed = 0;
286 if (print.no_line_dirs)
288 print.lineno = line;
289 return;
292 /* print.lineno is zero if this is the first token of the file. We
293 handle this specially, so that a first line of "# 1 "foo.c" in
294 file foo.i outputs just the foo.c line, and not a foo.i line. */
295 if (line >= print.lineno && line < print.lineno + 8 && print.lineno)
297 while (line > print.lineno)
299 putc ('\n', print.outf);
300 print.lineno++;
303 else
305 print.lineno = line;
306 print_line ("");
310 static void
311 print_line (special_flags)
312 const char *special_flags;
314 /* End any previous line of text. */
315 if (print.printed)
316 putc ('\n', print.outf);
317 print.printed = 0;
319 fprintf (print.outf, "# %u \"%s\"%s%s\n",
320 print.lineno, print.last_fname, special_flags, print.syshdr_flags);
323 /* Callbacks */
325 static void
326 cb_ident (pfile, str)
327 cpp_reader *pfile ATTRIBUTE_UNUSED;
328 const cpp_string * str;
330 maybe_print_line (cpp_get_line (pfile)->output_line);
331 fprintf (print.outf, "#ident \"%.*s\"\n", (int) str->len, str->text);
332 print.lineno++;
335 static void
336 cb_define (pfile, node)
337 cpp_reader *pfile;
338 cpp_hashnode *node;
340 if (pfile->done_initializing)
342 maybe_print_line (cpp_get_line (pfile)->output_line);
343 fprintf (print.outf, "#define %s", node->name);
345 /* -dD or -g3 command line options. */
346 if (CPP_OPTION (pfile, debug_output)
347 || CPP_OPTION (pfile, dump_macros) == dump_definitions)
348 fputs ((const char *) cpp_macro_definition (pfile, node), print.outf);
350 putc ('\n', print.outf);
351 print.lineno++;
355 static void
356 cb_undef (pfile, node)
357 cpp_reader *pfile;
358 cpp_hashnode *node;
360 if (pfile->done_initializing)
362 maybe_print_line (cpp_get_line (pfile)->output_line);
363 fprintf (print.outf, "#undef %s\n", node->name);
364 print.lineno++;
368 static void
369 cb_include (pfile, dir, header)
370 cpp_reader *pfile ATTRIBUTE_UNUSED;
371 const unsigned char *dir;
372 const cpp_token *header;
374 maybe_print_line (cpp_get_line (pfile)->output_line);
375 fprintf (print.outf, "#%s %s\n", dir, cpp_token_as_text (pfile, header));
376 print.lineno++;
379 static void
380 cb_file_change (pfile, fc)
381 cpp_reader *pfile ATTRIBUTE_UNUSED;
382 const cpp_file_change *fc;
384 /* Bring current file to correct line (except first file). */
385 if (fc->reason == FC_ENTER && fc->from.filename)
386 maybe_print_line (fc->from.lineno);
388 print.last_fname = fc->to.filename;
389 if (fc->externc)
390 print.syshdr_flags = " 3 4";
391 else if (fc->sysp)
392 print.syshdr_flags = " 3";
393 else
394 print.syshdr_flags = "";
396 if (print.lineno)
398 const char *flags = "";
400 print.lineno = fc->to.lineno;
401 if (fc->reason == FC_ENTER)
402 flags = " 1";
403 else if (fc->reason == FC_LEAVE)
404 flags = " 2";
405 print_line (flags);
409 static void
410 cb_def_pragma (pfile)
411 cpp_reader *pfile;
413 maybe_print_line (cpp_get_line (pfile)->output_line);
414 fputs ("#pragma ", print.outf);
415 cpp_output_line (pfile, print.outf);
416 print.lineno++;
419 static void
420 do_pragma_implementation (pfile)
421 cpp_reader *pfile;
423 /* Be quiet about `#pragma implementation' for a file only if it hasn't
424 been included yet. */
425 cpp_token token;
427 cpp_start_lookahead (pfile);
428 cpp_get_token (pfile, &token);
429 cpp_stop_lookahead (pfile, 0);
431 /* If it's not a string, pass it through and let the front end complain. */
432 if (token.type == CPP_STRING)
434 /* Make a NUL-terminated copy of the string. */
435 char *filename = alloca (token.val.str.len + 1);
436 memcpy (filename, token.val.str.text, token.val.str.len);
437 filename[token.val.str.len] = '\0';
438 if (cpp_included (pfile, filename))
439 cpp_warning (pfile,
440 "#pragma GCC implementation for \"%s\" appears after file is included",
441 filename);
443 else if (token.type != CPP_EOF)
445 cpp_error (pfile, "malformed #pragma GCC implementation");
446 return;
449 /* Output? This is nasty, but we don't have [GCC] implementation in
450 the buffer. */
451 if (pfile->cb.def_pragma)
453 maybe_print_line (cpp_get_line (pfile)->output_line);
454 fputs ("#pragma GCC implementation ", print.outf);
455 cpp_output_line (pfile, print.outf);
456 print.lineno++;
460 /* Dump out the hash table. */
461 static int
462 dump_macro (pfile, node, v)
463 cpp_reader *pfile;
464 cpp_hashnode *node;
465 void *v ATTRIBUTE_UNUSED;
467 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
469 fprintf (print.outf, "#define %s", node->name);
470 fputs ((const char *) cpp_macro_definition (pfile, node), print.outf);
471 putc ('\n', print.outf);
472 print.lineno++;
475 return 1;