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
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! */
28 /* Encapsulates state used to convert the stream of tokens coming from
29 cpp_get_token back into a text file. */
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 **));
42 /* General output routines. */
43 static void scan_buffer PARAMS ((cpp_reader *));
44 static int printer_init PARAMS ((cpp_reader *));
45 static int dump_macro PARAMS ((cpp_reader *, cpp_hashnode *, void *));
47 static void print_line PARAMS ((const char *));
48 static void maybe_print_line PARAMS ((unsigned int));
50 /* Callback routines for the parser. Most of these are active only
52 static void cb_define PARAMS ((cpp_reader *, cpp_hashnode *));
53 static void cb_undef PARAMS ((cpp_reader *, cpp_hashnode *));
54 static void cb_include PARAMS ((cpp_reader *, const unsigned char *,
56 static void cb_ident PARAMS ((cpp_reader *, const cpp_string *));
57 static void cb_change_file PARAMS ((cpp_reader *, const cpp_file_change *));
58 static void cb_def_pragma PARAMS ((cpp_reader *));
59 static void do_pragma_implementation PARAMS ((cpp_reader *));
62 static cpp_reader parse_in;
63 static struct printer print;
71 cpp_reader *pfile = &parse_in;
72 int argi = 1; /* Next argument to handle. */
74 p = argv[0] + strlen (argv[0]);
75 while (p != argv[0] && ! IS_DIR_SEPARATOR (p[-1])) --p;
78 xmalloc_set_program_name (progname);
80 #ifdef HAVE_LC_MESSAGES
81 setlocale (LC_MESSAGES, "");
83 (void) bindtextdomain (PACKAGE, localedir);
84 (void) textdomain (PACKAGE);
87 /* Default language is GNU C89. */
88 cpp_reader_init (pfile, CLK_GNUC89);
90 argi += cpp_handle_options (pfile, argc - argi , argv + argi);
91 if (argi < argc && ! CPP_FATAL_ERRORS (pfile))
92 cpp_fatal (pfile, "Invalid option %s", argv[argi]);
93 if (CPP_FATAL_ERRORS (pfile))
94 return (FATAL_EXIT_CODE);
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);
103 if (! CPP_OPTION (pfile, no_output))
105 pfile->cb.ident = cb_ident;
106 pfile->cb.def_pragma = cb_def_pragma;
107 if (! CPP_OPTION (pfile, no_line_commands))
108 pfile->cb.change_file = cb_change_file;
111 if (CPP_OPTION (pfile, dump_includes))
112 pfile->cb.include = cb_include;
114 if (CPP_OPTION (pfile, debug_output)
115 || CPP_OPTION (pfile, dump_macros) == dump_names
116 || CPP_OPTION (pfile, dump_macros) == dump_definitions)
118 pfile->cb.define = cb_define;
119 pfile->cb.undef = cb_undef;
120 pfile->cb.poison = cb_def_pragma;
123 /* Register one #pragma which needs special handling. */
124 cpp_register_pragma(pfile, 0, "implementation", do_pragma_implementation);
125 cpp_register_pragma(pfile, "GCC", "implementation", do_pragma_implementation);
127 if (! cpp_start_read (pfile, CPP_OPTION (pfile, in_fname)))
128 return (FATAL_EXIT_CODE);
130 if (CPP_BUFFER (pfile))
132 if (CPP_OPTION (pfile, no_output))
133 cpp_scan_buffer_nooutput (pfile);
138 /* -dM command line option. */
139 if (CPP_OPTION (pfile, dump_macros) == dump_only)
140 cpp_forall_identifiers (pfile, dump_macro, NULL);
145 /* Flush any pending output. */
147 putc ('\n', print.outf);
148 if (ferror (print.outf) || fclose (print.outf))
149 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));
152 return (FATAL_EXIT_CODE);
153 return (SUCCESS_EXIT_CODE);
156 /* Writes out the preprocessed file. Alternates between two tokens,
157 so that we can avoid accidental token pasting. */
162 unsigned int index, line;
163 cpp_token tokens[2], *token;
167 for (index = 0;; index = 1 - index)
169 token = &tokens[index];
170 cpp_get_token (pfile, token);
172 if (token->type == CPP_EOF)
175 line = cpp_get_line (pfile)->output_line;
176 if (print.lineno != line)
178 unsigned int col = cpp_get_line (pfile)->col;
180 /* Supply enough whitespace to put this token in its original
181 column. Don't bother trying to reconstruct tabs; we can't
182 get it right in general, and nothing ought to care. (Yes,
183 some things do care; the fault lies with them.) */
184 maybe_print_line (line);
187 if (token->flags & PREV_WHITE)
190 putc (' ', print.outf);
193 else if (print.printed
194 && ! (token->flags & PREV_WHITE)
195 && CPP_OPTION (pfile, lang) != CLK_ASM
196 && cpp_avoid_paste (pfile, &tokens[1 - index], token))
197 token->flags |= PREV_WHITE;
199 cpp_output_token (token, print.outf);
203 while (cpp_pop_buffer (pfile) != 0);
206 /* Initialize a cpp_printer structure. As a side effect, open the
212 print.last_fname = 0;
215 print.no_line_dirs = CPP_OPTION (pfile, no_line_commands);
217 if (CPP_OPTION (pfile, out_fname) == NULL)
218 CPP_OPTION (pfile, out_fname) = "";
220 if (CPP_OPTION (pfile, out_fname)[0] == '\0')
224 print.outf = fopen (CPP_OPTION (pfile, out_fname), "w");
227 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));
234 /* Newline-terminate any output line currently in progress. If
235 appropriate, write the current line number to the output, or pad
236 with newlines so the output line matches the current line. */
238 maybe_print_line (line)
241 /* End the previous line of text (probably only needed until we get
242 multi-line tokens fixed). */
245 putc ('\n', print.outf);
250 if (print.no_line_dirs)
253 if (line >= print.lineno && line < print.lineno + 8)
255 while (line > print.lineno)
257 putc ('\n', print.outf);
269 print_line (special_flags)
270 const char *special_flags;
272 /* End any previous line of text. */
274 putc ('\n', print.outf);
277 fprintf (print.outf, "# %u \"%s\"%s%s\n",
278 print.lineno, print.last_fname, special_flags, print.syshdr_flags);
284 cb_ident (pfile, str)
285 cpp_reader *pfile ATTRIBUTE_UNUSED;
286 const cpp_string * str;
288 maybe_print_line (cpp_get_line (pfile)->output_line);
289 fprintf (print.outf, "#ident \"%.*s\"\n", (int) str->len, str->text);
294 cb_define (pfile, node)
298 if (pfile->done_initializing)
300 maybe_print_line (cpp_get_line (pfile)->output_line);
301 fprintf (print.outf, "#define %s", node->name);
303 /* -dD or -g3 command line options. */
304 if (CPP_OPTION (pfile, debug_output)
305 || CPP_OPTION (pfile, dump_macros) == dump_definitions)
306 fputs ((const char *) cpp_macro_definition (pfile, node), print.outf);
308 putc ('\n', print.outf);
314 cb_undef (pfile, node)
318 if (pfile->done_initializing)
320 maybe_print_line (cpp_get_line (pfile)->output_line);
321 fprintf (print.outf, "#undef %s\n", node->name);
327 cb_include (pfile, dir, header)
328 cpp_reader *pfile ATTRIBUTE_UNUSED;
329 const unsigned char *dir;
330 const cpp_token *header;
332 maybe_print_line (cpp_get_line (pfile)->output_line);
333 fprintf (print.outf, "#%s %s\n", dir, cpp_token_as_text (pfile, header));
338 cb_change_file (pfile, fc)
339 cpp_reader *pfile ATTRIBUTE_UNUSED;
340 const cpp_file_change *fc;
344 /* Bring current file to correct line (except first file). */
345 if (fc->reason == FC_ENTER && fc->from.filename)
346 maybe_print_line (fc->from.lineno);
348 print.lineno = fc->to.lineno;
349 print.last_fname = fc->to.filename;
351 print.syshdr_flags = " 3 4";
353 print.syshdr_flags = " 3";
355 print.syshdr_flags = "";
359 case FC_ENTER : flags = fc->from.filename ? " 1": ""; break;
360 case FC_LEAVE : flags = " 2"; break;
361 case FC_RENAME: flags = ""; break;
368 cb_def_pragma (pfile)
371 maybe_print_line (cpp_get_line (pfile)->output_line);
372 fputs ("#pragma ", print.outf);
373 cpp_output_line (pfile, print.outf);
378 do_pragma_implementation (pfile)
381 /* Be quiet about `#pragma implementation' for a file only if it hasn't
382 been included yet. */
385 cpp_start_lookahead (pfile);
386 cpp_get_token (pfile, &token);
387 cpp_stop_lookahead (pfile, 0);
389 /* If it's not a string, pass it through and let the front end complain. */
390 if (token.type == CPP_STRING)
392 /* Make a NUL-terminated copy of the string. */
393 char *filename = alloca (token.val.str.len + 1);
394 memcpy (filename, token.val.str.text, token.val.str.len);
395 filename[token.val.str.len] = '\0';
396 if (cpp_included (pfile, filename))
398 "#pragma GCC implementation for \"%s\" appears after file is included",
401 else if (token.type != CPP_EOF)
403 cpp_error (pfile, "malformed #pragma GCC implementation");
407 /* Output? This is nasty, but we don't have [GCC] implementation in
409 if (pfile->cb.def_pragma)
411 maybe_print_line (cpp_get_line (pfile)->output_line);
412 fputs ("#pragma GCC implementation ", print.outf);
413 cpp_output_line (pfile, print.outf);
418 /* Dump out the hash table. */
420 dump_macro (pfile, node, v)
423 void *v ATTRIBUTE_UNUSED;
425 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
427 fprintf (print.outf, "#define %s", node->name);
428 fputs ((const char *) cpp_macro_definition (pfile, node), print.outf);
429 putc ('\n', print.outf);