d575f9fba51e96249576f4b2e35c532eda6f11d2
[gcc.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 *last_fname; /* previous file name. */
35 const char *syshdr_flags; /* system header flags, if any. */
36 unsigned int lineno; /* line currently being written. */
37 unsigned char printed; /* nonzero if something output at lineno. */
38 };
39
40 int main PARAMS ((int, char **));
41 static void general_init PARAMS ((const char *));
42 static void setup_callbacks PARAMS ((void));
43
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 *));
49
50 static void print_line PARAMS ((const char *));
51 static void maybe_print_line PARAMS ((unsigned int));
52
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
63 const char *progname; /* Needs to be global. */
64 static cpp_reader *pfile; /* An opaque handle. */
65 static cpp_options *options; /* Options of pfile. */
66 static cpp_callbacks *cb; /* Callbacks of pfile. */
67 static struct printer print;
68
69 int
70 main (argc, argv)
71 int argc;
72 char **argv;
73 {
74 int argi = 1; /* Next argument to handle. */
75
76 general_init (argv[0]);
77 /* Default language is GNU C89. */
78 pfile = cpp_create_reader (CLK_GNUC89);
79 options = cpp_get_options (pfile);
80 cb = cpp_get_callbacks (pfile);
81
82 argi += cpp_handle_options (pfile, argc - argi , argv + argi);
83 if (argi < argc && ! CPP_FATAL_ERRORS (pfile))
84 cpp_fatal (pfile, "Invalid option %s", argv[argi]);
85 cpp_post_options (pfile);
86 if (CPP_FATAL_ERRORS (pfile))
87 return (FATAL_EXIT_CODE);
88
89 /* If cpp_handle_options saw --help or --version on the command
90 line, it will have set pfile->help_only to indicate this. Exit
91 successfully. [The library does not exit itself, because
92 e.g. cc1 needs to print its own --help message at this point.] */
93 if (options->help_only)
94 return (SUCCESS_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 setup_callbacks ();
103
104 if (! cpp_start_read (pfile, options->in_fname))
105 return (FATAL_EXIT_CODE);
106
107 /* A successful cpp_start_read guarantees that we can call
108 cpp_scan_buffer_nooutput or cpp_get_token next. */
109 if (options->no_output)
110 cpp_scan_buffer_nooutput (pfile, 1);
111 else
112 scan_buffer (pfile);
113
114 /* -dM command line option. */
115 if (options->dump_macros == dump_only)
116 cpp_forall_identifiers (pfile, dump_macro, NULL);
117
118 cpp_finish (pfile);
119 cpp_cleanup (pfile);
120
121 /* Flush any pending output. */
122 if (print.printed)
123 putc ('\n', print.outf);
124 if (ferror (print.outf) || fclose (print.outf))
125 cpp_notice_from_errno (pfile, options->out_fname);
126
127 if (cpp_errors (pfile))
128 return FATAL_EXIT_CODE;
129
130 return SUCCESS_EXIT_CODE;
131 }
132
133 /* Store the program name, and set the locale. */
134 static void
135 general_init (const char *argv0)
136 {
137 progname = argv0 + strlen (argv0);
138
139 while (progname != argv0 && ! IS_DIR_SEPARATOR (progname[-1]))
140 --progname;
141
142 xmalloc_set_program_name (progname);
143
144 /* LC_CTYPE determines the character set used by the terminal so it has be set
145 to output messages correctly. */
146
147 #ifdef HAVE_LC_MESSAGES
148 setlocale (LC_CTYPE, "");
149 setlocale (LC_MESSAGES, "");
150 #else
151 setlocale (LC_ALL, "");
152 #endif
153
154 (void) bindtextdomain (PACKAGE, localedir);
155 (void) textdomain (PACKAGE);
156 }
157
158 /* Set up the callbacks as appropriate. */
159 static void
160 setup_callbacks ()
161 {
162 if (! options->no_output)
163 {
164 cb->ident = cb_ident;
165 cb->def_pragma = cb_def_pragma;
166 if (! options->no_line_commands)
167 cb->file_change = cb_file_change;
168 }
169
170 if (options->dump_includes)
171 cb->include = cb_include;
172
173 if (options->dump_macros == dump_names
174 || options->dump_macros == dump_definitions)
175 {
176 cb->define = cb_define;
177 cb->undef = cb_undef;
178 cb->poison = cb_def_pragma;
179 }
180 }
181
182 /* Writes out the preprocessed file. Alternates between two tokens,
183 so that we can avoid accidental token pasting. */
184 static void
185 scan_buffer (pfile)
186 cpp_reader *pfile;
187 {
188 unsigned int index, line;
189 cpp_token tokens[2], *token;
190
191 do
192 {
193 for (index = 0;; index = 1 - index)
194 {
195 token = &tokens[index];
196 cpp_get_token (pfile, token);
197
198 if (token->type == CPP_EOF)
199 break;
200
201 line = cpp_get_line (pfile)->output_line;
202 if (print.lineno != line)
203 {
204 unsigned int col = cpp_get_line (pfile)->col;
205
206 /* Supply enough whitespace to put this token in its original
207 column. Don't bother trying to reconstruct tabs; we can't
208 get it right in general, and nothing ought to care. (Yes,
209 some things do care; the fault lies with them.) */
210 maybe_print_line (line);
211 if (col > 1)
212 {
213 if (token->flags & PREV_WHITE)
214 col--;
215 while (--col)
216 putc (' ', print.outf);
217 }
218 }
219 else if (print.printed
220 && ! (token->flags & PREV_WHITE)
221 && options->lang != CLK_ASM
222 && cpp_avoid_paste (pfile, &tokens[1 - index], token))
223 token->flags |= PREV_WHITE;
224
225 cpp_output_token (token, print.outf);
226 print.printed = 1;
227 if (token->type == CPP_STRING || token->type == CPP_WSTRING
228 || token->type == CPP_COMMENT)
229 check_multiline_token (&token->val.str);
230 }
231 }
232 while (cpp_pop_buffer (pfile) != 0);
233 }
234
235 /* Adjust print.lineno for newlines embedded in tokens. */
236 static void
237 check_multiline_token (str)
238 cpp_string *str;
239 {
240 unsigned int i;
241
242 for (i = 0; i < str->len; i++)
243 if (str->text[i] == '\n')
244 print.lineno++;
245 }
246
247 /* Initialize a cpp_printer structure. As a side effect, open the
248 output file. */
249 static int
250 printer_init (pfile)
251 cpp_reader *pfile;
252 {
253 print.last_fname = 0;
254 print.lineno = 0;
255 print.printed = 0;
256
257 if (options->out_fname == NULL)
258 options->out_fname = "";
259
260 if (options->out_fname[0] == '\0')
261 print.outf = stdout;
262 else
263 {
264 print.outf = fopen (options->out_fname, "w");
265 if (! print.outf)
266 {
267 cpp_notice_from_errno (pfile, options-> out_fname);
268 return 1;
269 }
270 }
271 return 0;
272 }
273
274 /* Newline-terminate any output line currently in progress. If
275 appropriate, write the current line number to the output, or pad
276 with newlines so the output line matches the current line. */
277 static void
278 maybe_print_line (line)
279 unsigned int line;
280 {
281 /* End the previous line of text (probably only needed until we get
282 multi-line tokens fixed). */
283 if (print.printed)
284 {
285 putc ('\n', print.outf);
286 print.lineno++;
287 print.printed = 0;
288 }
289
290 if (options->no_line_commands)
291 {
292 print.lineno = line;
293 return;
294 }
295
296 /* print.lineno is zero if this is the first token of the file. We
297 handle this specially, so that a first line of "# 1 "foo.c" in
298 file foo.i outputs just the foo.c line, and not a foo.i line. */
299 if (line >= print.lineno && line < print.lineno + 8 && print.lineno)
300 {
301 while (line > print.lineno)
302 {
303 putc ('\n', print.outf);
304 print.lineno++;
305 }
306 }
307 else
308 {
309 print.lineno = line;
310 print_line ("");
311 }
312 }
313
314 static void
315 print_line (special_flags)
316 const char *special_flags;
317 {
318 /* End any previous line of text. */
319 if (print.printed)
320 putc ('\n', print.outf);
321 print.printed = 0;
322
323 fprintf (print.outf, "# %u \"%s\"%s%s\n",
324 print.lineno, print.last_fname, special_flags, print.syshdr_flags);
325 }
326
327 /* Callbacks */
328
329 static void
330 cb_ident (pfile, str)
331 cpp_reader *pfile ATTRIBUTE_UNUSED;
332 const cpp_string * str;
333 {
334 maybe_print_line (cpp_get_line (pfile)->output_line);
335 fprintf (print.outf, "#ident \"%.*s\"\n", (int) str->len, str->text);
336 print.lineno++;
337 }
338
339 static void
340 cb_define (pfile, node)
341 cpp_reader *pfile;
342 cpp_hashnode *node;
343 {
344 maybe_print_line (cpp_get_line (pfile)->output_line);
345 fprintf (print.outf, "#define %s", node->name);
346
347 /* -dD command line option. */
348 if (options->dump_macros == dump_definitions)
349 fputs ((const char *) cpp_macro_definition (pfile, node), print.outf);
350
351 putc ('\n', print.outf);
352 print.lineno++;
353 }
354
355 static void
356 cb_undef (pfile, node)
357 cpp_reader *pfile;
358 cpp_hashnode *node;
359 {
360 maybe_print_line (cpp_get_line (pfile)->output_line);
361 fprintf (print.outf, "#undef %s\n", node->name);
362 print.lineno++;
363 }
364
365 static void
366 cb_include (pfile, dir, header)
367 cpp_reader *pfile ATTRIBUTE_UNUSED;
368 const unsigned char *dir;
369 const cpp_token *header;
370 {
371 maybe_print_line (cpp_get_line (pfile)->output_line);
372 fprintf (print.outf, "#%s %s\n", dir, cpp_token_as_text (pfile, header));
373 print.lineno++;
374 }
375
376 static void
377 cb_file_change (pfile, fc)
378 cpp_reader *pfile ATTRIBUTE_UNUSED;
379 const cpp_file_change *fc;
380 {
381 /* Bring current file to correct line (except first file). */
382 if (fc->reason == FC_ENTER && fc->from.filename)
383 maybe_print_line (fc->from.lineno);
384
385 print.last_fname = fc->to.filename;
386 if (fc->externc)
387 print.syshdr_flags = " 3 4";
388 else if (fc->sysp)
389 print.syshdr_flags = " 3";
390 else
391 print.syshdr_flags = "";
392
393 if (print.lineno)
394 {
395 const char *flags = "";
396
397 print.lineno = fc->to.lineno;
398 if (fc->reason == FC_ENTER)
399 flags = " 1";
400 else if (fc->reason == FC_LEAVE)
401 flags = " 2";
402
403 if (! options->no_line_commands)
404 print_line (flags);
405 }
406 }
407
408 static void
409 cb_def_pragma (pfile)
410 cpp_reader *pfile;
411 {
412 maybe_print_line (cpp_get_line (pfile)->output_line);
413 fputs ("#pragma ", print.outf);
414 cpp_output_line (pfile, print.outf);
415 print.lineno++;
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 }