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.
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
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.
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.
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! */
29 /* Encapsulates state used to convert the stream of tokens coming from
30 cpp_get_token back into a text file. */
33 FILE *outf; /* Stream to write to. */
34 const char *syshdr_flags; /* System header flags, if any. */
35 struct line_map *map; /* Logical to physical line mappings. */
36 unsigned int line; /* Line currently being written. */
37 unsigned char printed; /* Nonzero if something output at line. */
40 int main PARAMS ((int, char **));
41 static void general_init PARAMS ((const char *));
42 static void do_preprocessing PARAMS ((int, char **));
43 static void setup_callbacks PARAMS ((void));
45 /* General output routines. */
46 static void scan_translation_unit PARAMS ((cpp_reader *));
47 static void check_multiline_token PARAMS ((cpp_string *));
48 static int dump_macro PARAMS ((cpp_reader *, cpp_hashnode *, void *));
50 static void print_line PARAMS ((struct line_map *, unsigned int,
52 static void maybe_print_line PARAMS ((struct line_map *, unsigned int));
54 /* Callback routines for the parser. Most of these are active only
56 static void cb_define PARAMS ((cpp_reader *, unsigned int, cpp_hashnode *));
57 static void cb_undef PARAMS ((cpp_reader *, unsigned int, cpp_hashnode *));
58 static void cb_include PARAMS ((cpp_reader *, unsigned int,
59 const unsigned char *, const cpp_token *));
60 static void cb_ident PARAMS ((cpp_reader *, unsigned int,
62 static void cb_file_change PARAMS ((cpp_reader *, const cpp_file_change *));
63 static void cb_def_pragma PARAMS ((cpp_reader *, unsigned int));
65 const char *progname; /* Needs to be global. */
66 static cpp_reader *pfile; /* An opaque handle. */
67 static cpp_options *options; /* Options of pfile. */
68 static struct printer print;
75 general_init (argv[0]);
77 /* Contruct a reader with default language GNU C89. */
78 pfile = cpp_create_reader (NULL, CLK_GNUC89);
79 options = cpp_get_options (pfile);
81 do_preprocessing (argc, argv);
83 if (cpp_destroy (pfile))
84 return FATAL_EXIT_CODE;
86 return SUCCESS_EXIT_CODE;
89 /* Store the program name, and set the locale. */
94 progname = argv0 + strlen (argv0);
96 while (progname != argv0 && ! IS_DIR_SEPARATOR (progname[-1]))
99 xmalloc_set_program_name (progname);
101 /* LC_CTYPE determines the character set used by the terminal so it
102 has to be set to output messages correctly. */
104 #ifdef HAVE_LC_MESSAGES
105 setlocale (LC_CTYPE, "");
106 setlocale (LC_MESSAGES, "");
108 setlocale (LC_ALL, "");
111 (void) bindtextdomain (PACKAGE, localedir);
112 (void) textdomain (PACKAGE);
115 /* Handle switches, preprocess and output. */
117 do_preprocessing (argc, argv)
121 int argi = 1; /* Next argument to handle. */
123 argi += cpp_handle_options (pfile, argc - argi , argv + argi);
124 if (CPP_FATAL_ERRORS (pfile))
128 cpp_fatal (pfile, "Invalid option %s", argv[argi]);
130 cpp_post_options (pfile);
132 if (CPP_FATAL_ERRORS (pfile))
135 /* If cpp_handle_options saw --help or --version on the command
136 line, it will have set pfile->help_only to indicate this. Exit
137 successfully. [The library does not exit itself, because
138 e.g. cc1 needs to print its own --help message at this point.] */
139 if (options->help_only)
142 /* Initialize the printer structure. Setting print.line to -1 here
143 is a trick to guarantee that the first token of the file will
144 cause a linemarker to be output by maybe_print_line. */
145 print.line = (unsigned int) -1;
149 /* Open the output now. We must do so even if no_output is on,
150 because there may be other output than from the actual
151 preprocessing (e.g. from -dM). */
152 if (options->out_fname[0] == '\0')
156 print.outf = fopen (options->out_fname, "w");
157 if (print.outf == NULL)
159 cpp_notice_from_errno (pfile, options->out_fname);
166 if (cpp_start_read (pfile, options->in_fname))
168 /* A successful cpp_start_read guarantees that we can call
169 cpp_scan_nooutput or cpp_get_token next. */
170 if (options->no_output)
171 cpp_scan_nooutput (pfile);
173 scan_translation_unit (pfile);
175 /* -dM command line option. Should this be in cpp_finish? */
176 if (options->dump_macros == dump_only)
177 cpp_forall_identifiers (pfile, dump_macro, NULL);
182 /* Flush any pending output. */
184 putc ('\n', print.outf);
186 if (ferror (print.outf) || fclose (print.outf))
187 cpp_notice_from_errno (pfile, options->out_fname);
190 /* Set up the callbacks as appropriate. */
194 cpp_callbacks *cb = cpp_get_callbacks (pfile);
196 if (! options->no_output)
198 cb->ident = cb_ident;
199 cb->def_pragma = cb_def_pragma;
200 if (! options->no_line_commands)
201 cb->file_change = cb_file_change;
204 if (options->dump_includes)
205 cb->include = cb_include;
207 if (options->dump_macros == dump_names
208 || options->dump_macros == dump_definitions)
210 cb->define = cb_define;
211 cb->undef = cb_undef;
215 /* Writes out the preprocessed file. Alternates between two tokens,
216 so that we can avoid accidental token pasting. */
218 scan_translation_unit (pfile)
221 unsigned int index, line;
222 cpp_token tokens[2], *token;
224 for (index = 0;; index = 1 - index)
226 token = &tokens[index];
227 cpp_get_token (pfile, token);
229 if (token->type == CPP_EOF)
232 line = cpp_get_line (pfile)->output_line;
233 if (print.line != line)
235 unsigned int col = cpp_get_line (pfile)->col;
237 /* Supply enough whitespace to put this token in its original
238 column. Don't bother trying to reconstruct tabs; we can't
239 get it right in general, and nothing ought to care. (Yes,
240 some things do care; the fault lies with them.) */
241 maybe_print_line (print.map, line);
244 if (token->flags & PREV_WHITE)
247 putc (' ', print.outf);
250 else if ((token->flags & (PREV_WHITE | AVOID_LPASTE))
252 && cpp_avoid_paste (pfile, &tokens[1 - index], token))
253 token->flags |= PREV_WHITE;
254 /* Special case '# <directive name>': insert a space between
255 the # and the token. This will prevent it from being
256 treated as a directive when this code is re-preprocessed.
257 XXX Should do this only at the beginning of a line, but how? */
258 else if (token->type == CPP_NAME && token->val.node->directive_index
259 && tokens[1 - index].type == CPP_HASH)
260 token->flags |= PREV_WHITE;
262 cpp_output_token (token, print.outf);
264 if (token->type == CPP_STRING || token->type == CPP_WSTRING
265 || token->type == CPP_COMMENT)
266 check_multiline_token (&token->val.str);
270 /* Adjust print.line for newlines embedded in tokens. */
272 check_multiline_token (str)
277 for (i = 0; i < str->len; i++)
278 if (str->text[i] == '\n')
282 /* If the token read on logical line LINE needs to be output on a
283 different line to the current one, output the required newlines or
284 a line marker, and return 1. Otherwise return 0. */
287 maybe_print_line (map, line)
288 struct line_map *map;
291 /* End the previous line of text. */
294 putc ('\n', print.outf);
299 if (line >= print.line && line < print.line + 8)
301 while (line > print.line)
303 putc ('\n', print.outf);
308 print_line (map, line, "");
312 print_line (map, line, special_flags)
313 struct line_map *map;
315 const char *special_flags;
317 /* End any previous line of text. */
319 putc ('\n', print.outf);
323 if (! options->no_line_commands)
324 fprintf (print.outf, "# %u \"%s\"%s%s\n",
325 SOURCE_LINE (map, print.line), map->to_file,
326 special_flags, print.syshdr_flags);
332 cb_ident (pfile, line, str)
333 cpp_reader *pfile ATTRIBUTE_UNUSED;
335 const cpp_string * str;
337 maybe_print_line (print.map, line);
338 fprintf (print.outf, "#ident \"%s\"\n", str->text);
343 cb_define (pfile, line, node)
348 maybe_print_line (print.map, line);
349 fputs ("#define ", print.outf);
351 /* -dD command line option. */
352 if (options->dump_macros == dump_definitions)
353 fputs ((const char *) cpp_macro_definition (pfile, node), print.outf);
355 fputs ((const char *) NODE_NAME (node), print.outf);
357 putc ('\n', print.outf);
362 cb_undef (pfile, line, node)
363 cpp_reader *pfile ATTRIBUTE_UNUSED;
367 maybe_print_line (print.map, line);
368 fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
373 cb_include (pfile, line, dir, header)
376 const unsigned char *dir;
377 const cpp_token *header;
379 maybe_print_line (print.map, line);
380 fprintf (print.outf, "#%s %s\n", dir, cpp_token_as_text (pfile, header));
384 /* The file name, line number or system header flags have changed, as
385 described in FC. NB: the old print.map must be considered invalid. */
388 cb_file_change (pfile, fc)
389 cpp_reader *pfile ATTRIBUTE_UNUSED;
390 const cpp_file_change *fc;
392 bool first_time = print.map == NULL;
394 /* Bring current file to correct line. We handle the first file
395 change callback specially, so that a first line of "# 1 "foo.c"
396 in file foo.i outputs just the foo.c line, and not a foo.i line. */
397 if (fc->reason == LC_ENTER && !first_time)
398 maybe_print_line (fc->map - 1, fc->line - 1);
402 print.syshdr_flags = " 3 4";
404 print.syshdr_flags = " 3";
406 print.syshdr_flags = "";
410 const char *flags = "";
412 if (fc->reason == LC_ENTER)
414 else if (fc->reason == LC_LEAVE)
417 print_line (print.map, fc->line, flags);
421 /* Copy a #pragma directive to the preprocessed output. LINE is the
422 line of the current source file, not the logical line. */
424 cb_def_pragma (pfile, line)
428 maybe_print_line (print.map, line);
429 fputs ("#pragma ", print.outf);
430 cpp_output_line (pfile, print.outf);
434 /* Dump out the hash table. */
436 dump_macro (pfile, node, v)
439 void *v ATTRIBUTE_UNUSED;
441 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
443 fputs ("#define ", print.outf);
444 fputs ((const char *) cpp_macro_definition (pfile, node), print.outf);
445 putc ('\n', print.outf);