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. */
39 int main PARAMS ((int, char **));
40 static void general_init PARAMS ((const char *));
41 static void setup_callbacks PARAMS ((void));
43 /* General output routines. */
44 static void scan_buffer PARAMS ((cpp_reader *));
45 static void check_multiline_token PARAMS ((cpp_string *));
46 static int printer_init PARAMS ((cpp_reader *));
47 static int dump_macro PARAMS ((cpp_reader *, cpp_hashnode *, void *));
49 static void print_line PARAMS ((const char *));
50 static void maybe_print_line PARAMS ((unsigned int));
52 /* Callback routines for the parser. Most of these are active only
54 static void cb_define PARAMS ((cpp_reader *, cpp_hashnode *));
55 static void cb_undef PARAMS ((cpp_reader *, cpp_hashnode *));
56 static void cb_include PARAMS ((cpp_reader *, const unsigned char *,
58 static void cb_ident PARAMS ((cpp_reader *, const cpp_string *));
59 static void cb_file_change PARAMS ((cpp_reader *, const cpp_file_change *));
60 static void cb_def_pragma PARAMS ((cpp_reader *));
61 static void do_pragma_implementation PARAMS ((cpp_reader *));
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;
74 int argi = 1; /* Next argument to handle. */
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);
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);
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);
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);
104 if (! cpp_start_read (pfile, options->in_fname))
105 return (FATAL_EXIT_CODE);
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);
114 /* -dM command line option. */
115 if (options->dump_macros == dump_only)
116 cpp_forall_identifiers (pfile, dump_macro, NULL);
121 /* Flush any pending output. */
123 putc ('\n', print.outf);
124 if (ferror (print.outf) || fclose (print.outf))
125 cpp_notice_from_errno (pfile, options->out_fname);
127 if (cpp_errors (pfile))
128 return FATAL_EXIT_CODE;
130 return SUCCESS_EXIT_CODE;
133 /* Store the program name, and set the locale. */
135 general_init (const char *argv0)
137 progname = argv0 + strlen (argv0);
139 while (progname != argv0 && ! IS_DIR_SEPARATOR (progname[-1]))
142 xmalloc_set_program_name (progname);
144 /* LC_CTYPE determines the character set used by the terminal so it has be set
145 to output messages correctly. */
147 #ifdef HAVE_LC_MESSAGES
148 setlocale (LC_CTYPE, "");
149 setlocale (LC_MESSAGES, "");
151 setlocale (LC_ALL, "");
154 (void) bindtextdomain (PACKAGE, localedir);
155 (void) textdomain (PACKAGE);
158 /* Set up the callbacks as appropriate. */
162 if (! options->no_output)
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;
170 if (options->dump_includes)
171 cb->include = cb_include;
173 if (options->dump_macros == dump_names
174 || options->dump_macros == dump_definitions)
176 cb->define = cb_define;
177 cb->undef = cb_undef;
178 cb->poison = cb_def_pragma;
181 /* Register one #pragma which needs special handling. */
182 cpp_register_pragma(pfile, 0, "implementation", do_pragma_implementation);
183 cpp_register_pragma(pfile, "GCC", "implementation", do_pragma_implementation);
186 /* Writes out the preprocessed file. Alternates between two tokens,
187 so that we can avoid accidental token pasting. */
192 unsigned int index, line;
193 cpp_token tokens[2], *token;
197 for (index = 0;; index = 1 - index)
199 token = &tokens[index];
200 cpp_get_token (pfile, token);
202 if (token->type == CPP_EOF)
205 line = cpp_get_line (pfile)->output_line;
206 if (print.lineno != line)
208 unsigned int col = cpp_get_line (pfile)->col;
210 /* Supply enough whitespace to put this token in its original
211 column. Don't bother trying to reconstruct tabs; we can't
212 get it right in general, and nothing ought to care. (Yes,
213 some things do care; the fault lies with them.) */
214 maybe_print_line (line);
217 if (token->flags & PREV_WHITE)
220 putc (' ', print.outf);
223 else if (print.printed
224 && ! (token->flags & PREV_WHITE)
225 && options->lang != CLK_ASM
226 && cpp_avoid_paste (pfile, &tokens[1 - index], token))
227 token->flags |= PREV_WHITE;
229 cpp_output_token (token, print.outf);
231 if (token->type == CPP_STRING || token->type == CPP_WSTRING
232 || token->type == CPP_COMMENT)
233 check_multiline_token (&token->val.str);
236 while (cpp_pop_buffer (pfile) != 0);
239 /* Adjust print.lineno for newlines embedded in tokens. */
241 check_multiline_token (str)
246 for (i = 0; i < str->len; i++)
247 if (str->text[i] == '\n')
251 /* Initialize a cpp_printer structure. As a side effect, open the
257 print.last_fname = 0;
261 if (options->out_fname == NULL)
262 options->out_fname = "";
264 if (options->out_fname[0] == '\0')
268 print.outf = fopen (options->out_fname, "w");
271 cpp_notice_from_errno (pfile, options-> out_fname);
278 /* Newline-terminate any output line currently in progress. If
279 appropriate, write the current line number to the output, or pad
280 with newlines so the output line matches the current line. */
282 maybe_print_line (line)
285 /* End the previous line of text (probably only needed until we get
286 multi-line tokens fixed). */
289 putc ('\n', print.outf);
294 if (options->no_line_commands)
300 /* print.lineno is zero if this is the first token of the file. We
301 handle this specially, so that a first line of "# 1 "foo.c" in
302 file foo.i outputs just the foo.c line, and not a foo.i line. */
303 if (line >= print.lineno && line < print.lineno + 8 && print.lineno)
305 while (line > print.lineno)
307 putc ('\n', print.outf);
319 print_line (special_flags)
320 const char *special_flags;
322 /* End any previous line of text. */
324 putc ('\n', print.outf);
327 fprintf (print.outf, "# %u \"%s\"%s%s\n",
328 print.lineno, print.last_fname, special_flags, print.syshdr_flags);
334 cb_ident (pfile, str)
335 cpp_reader *pfile ATTRIBUTE_UNUSED;
336 const cpp_string * str;
338 maybe_print_line (cpp_get_line (pfile)->output_line);
339 fprintf (print.outf, "#ident \"%.*s\"\n", (int) str->len, str->text);
344 cb_define (pfile, node)
348 maybe_print_line (cpp_get_line (pfile)->output_line);
349 fprintf (print.outf, "#define %s", node->name);
351 /* -dD command line option. */
352 if (options->dump_macros == dump_definitions)
353 fputs ((const char *) cpp_macro_definition (pfile, node), print.outf);
355 putc ('\n', print.outf);
360 cb_undef (pfile, node)
364 maybe_print_line (cpp_get_line (pfile)->output_line);
365 fprintf (print.outf, "#undef %s\n", node->name);
370 cb_include (pfile, dir, header)
371 cpp_reader *pfile ATTRIBUTE_UNUSED;
372 const unsigned char *dir;
373 const cpp_token *header;
375 maybe_print_line (cpp_get_line (pfile)->output_line);
376 fprintf (print.outf, "#%s %s\n", dir, cpp_token_as_text (pfile, header));
381 cb_file_change (pfile, fc)
382 cpp_reader *pfile ATTRIBUTE_UNUSED;
383 const cpp_file_change *fc;
385 /* Bring current file to correct line (except first file). */
386 if (fc->reason == FC_ENTER && fc->from.filename)
387 maybe_print_line (fc->from.lineno);
389 print.last_fname = fc->to.filename;
391 print.syshdr_flags = " 3 4";
393 print.syshdr_flags = " 3";
395 print.syshdr_flags = "";
399 const char *flags = "";
401 print.lineno = fc->to.lineno;
402 if (fc->reason == FC_ENTER)
404 else if (fc->reason == FC_LEAVE)
407 if (! options->no_line_commands)
413 cb_def_pragma (pfile)
416 maybe_print_line (cpp_get_line (pfile)->output_line);
417 fputs ("#pragma ", print.outf);
418 cpp_output_line (pfile, print.outf);
423 do_pragma_implementation (pfile)
426 /* Be quiet about `#pragma implementation' for a file only if it hasn't
427 been included yet. */
430 cpp_start_lookahead (pfile);
431 cpp_get_token (pfile, &token);
432 cpp_stop_lookahead (pfile, 0);
434 /* If it's not a string, pass it through and let the front end complain. */
435 if (token.type == CPP_STRING)
437 /* Make a NUL-terminated copy of the string. */
438 char *filename = alloca (token.val.str.len + 1);
439 memcpy (filename, token.val.str.text, token.val.str.len);
440 filename[token.val.str.len] = '\0';
441 if (cpp_included (pfile, filename))
443 "#pragma GCC implementation for \"%s\" appears after file is included",
446 else if (token.type != CPP_EOF)
448 cpp_error (pfile, "malformed #pragma GCC implementation");
452 /* Output? This is nasty, but we don't have [GCC] implementation in
456 maybe_print_line (cpp_get_line (pfile)->output_line);
457 fputs ("#pragma GCC implementation ", print.outf);
458 cpp_output_line (pfile, print.outf);
463 /* Dump out the hash table. */
465 dump_macro (pfile, node, v)
468 void *v ATTRIBUTE_UNUSED;
470 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
472 fprintf (print.outf, "#define %s", node->name);
473 fputs ((const char *) cpp_macro_definition (pfile, node), print.outf);
474 putc ('\n', print.outf);