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