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 struct line_map
*map
; /* Logical to physical line mappings. */
35 const cpp_token
*prev
; /* Previous token. */
36 const cpp_token
*source
; /* Source token for spacing. */
37 unsigned int line
; /* Line currently being written. */
38 unsigned char printed
; /* Nonzero if something output at line. */
41 int main
PARAMS ((int, char **));
42 static void general_init
PARAMS ((const char *));
43 static void do_preprocessing
PARAMS ((int, char **));
44 static void setup_callbacks
PARAMS ((void));
46 /* General output routines. */
47 static void scan_translation_unit
PARAMS ((cpp_reader
*));
48 static void check_multiline_token
PARAMS ((const cpp_string
*));
49 static int dump_macro
PARAMS ((cpp_reader
*, cpp_hashnode
*, void *));
51 static void print_line
PARAMS ((const struct line_map
*, unsigned int,
53 static void maybe_print_line
PARAMS ((const struct line_map
*, unsigned int));
55 /* Callback routines for the parser. Most of these are active only
57 static void cb_line_change
PARAMS ((cpp_reader
*, const cpp_token
*, int));
58 static void cb_define
PARAMS ((cpp_reader
*, unsigned int, cpp_hashnode
*));
59 static void cb_undef
PARAMS ((cpp_reader
*, unsigned int, cpp_hashnode
*));
60 static void cb_include
PARAMS ((cpp_reader
*, unsigned int,
61 const unsigned char *, const cpp_token
*));
62 static void cb_ident
PARAMS ((cpp_reader
*, unsigned int,
64 static void cb_file_change
PARAMS ((cpp_reader
*, const struct line_map
*));
65 static void cb_def_pragma
PARAMS ((cpp_reader
*, unsigned int));
67 const char *progname
; /* Needs to be global. */
68 static cpp_reader
*pfile
; /* An opaque handle. */
69 static cpp_options
*options
; /* Options of pfile. */
70 static struct printer print
;
77 general_init (argv
[0]);
79 /* Construct a reader with default language GNU C89. */
80 pfile
= cpp_create_reader (CLK_GNUC89
);
81 options
= cpp_get_options (pfile
);
83 do_preprocessing (argc
, argv
);
85 if (cpp_destroy (pfile
))
86 return FATAL_EXIT_CODE
;
88 return SUCCESS_EXIT_CODE
;
91 /* Store the program name, and set the locale. */
96 progname
= argv0
+ strlen (argv0
);
98 while (progname
!= argv0
&& ! IS_DIR_SEPARATOR (progname
[-1]))
101 xmalloc_set_program_name (progname
);
107 /* Handle switches, preprocess and output. */
109 do_preprocessing (argc
, argv
)
113 int argi
= 1; /* Next argument to handle. */
115 argi
+= cpp_handle_options (pfile
, argc
- argi
, argv
+ argi
);
116 if (CPP_FATAL_ERRORS (pfile
))
121 cpp_fatal (pfile
, "invalid option %s", argv
[argi
]);
125 cpp_post_options (pfile
);
126 if (CPP_FATAL_ERRORS (pfile
))
129 /* If cpp_handle_options saw --help or --version on the command
130 line, it will have set pfile->help_only to indicate this. Exit
131 successfully. [The library does not exit itself, because
132 e.g. cc1 needs to print its own --help message at this point.] */
133 if (options
->help_only
)
136 /* Initialize the printer structure. Setting print.line to -1 here
137 is a trick to guarantee that the first token of the file will
138 cause a linemarker to be output by maybe_print_line. */
139 print
.line
= (unsigned int) -1;
144 /* Open the output now. We must do so even if no_output is on,
145 because there may be other output than from the actual
146 preprocessing (e.g. from -dM). */
147 if (options
->out_fname
[0] == '\0')
151 print
.outf
= fopen (options
->out_fname
, "w");
152 if (print
.outf
== NULL
)
154 cpp_notice_from_errno (pfile
, options
->out_fname
);
161 if (cpp_read_main_file (pfile
, options
->in_fname
, NULL
))
163 cpp_finish_options (pfile
);
165 /* A successful cpp_read_main_file guarantees that we can call
166 cpp_scan_nooutput or cpp_get_token next. */
167 if (options
->no_output
)
168 cpp_scan_nooutput (pfile
);
170 scan_translation_unit (pfile
);
172 /* -dM command line option. Should this be in cpp_finish? */
173 if (options
->dump_macros
== dump_only
)
174 cpp_forall_identifiers (pfile
, dump_macro
, NULL
);
179 /* Flush any pending output. */
181 putc ('\n', print
.outf
);
183 if (ferror (print
.outf
) || fclose (print
.outf
))
184 cpp_notice_from_errno (pfile
, options
->out_fname
);
187 /* Set up the callbacks as appropriate. */
191 cpp_callbacks
*cb
= cpp_get_callbacks (pfile
);
193 if (! options
->no_output
)
195 cb
->line_change
= cb_line_change
;
196 cb
->ident
= cb_ident
;
197 cb
->def_pragma
= cb_def_pragma
;
198 if (! options
->no_line_commands
)
199 cb
->file_change
= cb_file_change
;
202 if (options
->dump_includes
)
203 cb
->include
= cb_include
;
205 if (options
->dump_macros
== dump_names
206 || options
->dump_macros
== dump_definitions
)
208 cb
->define
= cb_define
;
209 cb
->undef
= cb_undef
;
213 /* Writes out the preprocessed file, handling spacing and paste
216 scan_translation_unit (pfile
)
219 bool avoid_paste
= false;
224 const cpp_token
*token
= cpp_get_token (pfile
);
226 if (token
->type
== CPP_PADDING
)
229 if (print
.source
== NULL
230 || (!(print
.source
->flags
& PREV_WHITE
)
231 && token
->val
.source
== NULL
))
232 print
.source
= token
->val
.source
;
236 if (token
->type
== CPP_EOF
)
239 /* Subtle logic to output a space if and only if necessary. */
242 if (print
.source
== NULL
)
243 print
.source
= token
;
244 if (print
.source
->flags
& PREV_WHITE
245 || (print
.prev
&& cpp_avoid_paste (pfile
, print
.prev
, token
))
246 || (print
.prev
== NULL
&& token
->type
== CPP_HASH
))
247 putc (' ', print
.outf
);
249 else if (token
->flags
& PREV_WHITE
)
250 putc (' ', print
.outf
);
255 cpp_output_token (token
, print
.outf
);
257 if (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
258 || token
->type
== CPP_COMMENT
)
259 check_multiline_token (&token
->val
.str
);
263 /* Adjust print.line for newlines embedded in tokens. */
265 check_multiline_token (str
)
266 const cpp_string
*str
;
270 for (i
= 0; i
< str
->len
; i
++)
271 if (str
->text
[i
] == '\n')
275 /* If the token read on logical line LINE needs to be output on a
276 different line to the current one, output the required newlines or
277 a line marker, and return 1. Otherwise return 0. */
279 maybe_print_line (map
, line
)
280 const struct line_map
*map
;
283 /* End the previous line of text. */
286 putc ('\n', print
.outf
);
291 if (line
>= print
.line
&& line
< print
.line
+ 8)
293 while (line
> print
.line
)
295 putc ('\n', print
.outf
);
300 print_line (map
, line
, "");
303 /* Output a line marker for logical line LINE. Special flags are "1"
304 or "2" indicating entering or leaving a file. */
306 print_line (map
, line
, special_flags
)
307 const struct line_map
*map
;
309 const char *special_flags
;
311 /* End any previous line of text. */
313 putc ('\n', print
.outf
);
317 if (! options
->no_line_commands
)
319 fprintf (print
.outf
, "# %u \"%s\"%s",
320 SOURCE_LINE (map
, print
.line
), map
->to_file
, special_flags
);
323 fputs (" 3 4", print
.outf
);
324 else if (map
->sysp
== 1)
325 fputs (" 3", print
.outf
);
327 putc ('\n', print
.outf
);
331 /* Called when a line of output is started. TOKEN is the first token
332 of the line, and at end of file will be CPP_EOF. */
334 cb_line_change (pfile
, token
, parsing_args
)
335 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
336 const cpp_token
*token
;
339 if (token
->type
== CPP_EOF
|| parsing_args
)
342 maybe_print_line (print
.map
, token
->line
);
347 /* Supply enough spaces to put this token in its original column,
348 one space per column greater than 2, since scan_translation_unit
349 will provide a space if PREV_WHITE. Don't bother trying to
350 reconstruct tabs; we can't get it right in general, and nothing
351 ought to care. Some things do care; the fault lies with them. */
354 unsigned int spaces
= token
->col
- 2;
357 putc (' ', print
.outf
);
362 cb_ident (pfile
, line
, str
)
363 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
365 const cpp_string
* str
;
367 maybe_print_line (print
.map
, line
);
368 fprintf (print
.outf
, "#ident \"%s\"\n", str
->text
);
373 cb_define (pfile
, line
, node
)
378 maybe_print_line (print
.map
, line
);
379 fputs ("#define ", print
.outf
);
381 /* -dD command line option. */
382 if (options
->dump_macros
== dump_definitions
)
383 fputs ((const char *) cpp_macro_definition (pfile
, node
), print
.outf
);
385 fputs ((const char *) NODE_NAME (node
), print
.outf
);
387 putc ('\n', print
.outf
);
392 cb_undef (pfile
, line
, node
)
393 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
397 maybe_print_line (print
.map
, line
);
398 fprintf (print
.outf
, "#undef %s\n", NODE_NAME (node
));
403 cb_include (pfile
, line
, dir
, header
)
406 const unsigned char *dir
;
407 const cpp_token
*header
;
409 maybe_print_line (print
.map
, line
);
410 fprintf (print
.outf
, "#%s %s\n", dir
, cpp_token_as_text (pfile
, header
));
414 /* The file name, line number or system header flags have changed, as
415 described in MAP. From this point on, the old print.map might be
416 pointing to freed memory, and so must not be dereferenced. */
419 cb_file_change (pfile
, map
)
420 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
421 const struct line_map
*map
;
423 const char *flags
= "";
426 if (print
.map
== NULL
)
428 /* Avoid printing foo.i when the main file is foo.c. */
429 if (!options
->preprocessed
)
430 print_line (map
, map
->from_line
, flags
);
434 /* Bring current file to correct line when entering a new file. */
435 if (map
->reason
== LC_ENTER
)
436 maybe_print_line (map
- 1, map
->from_line
- 1);
438 if (map
->reason
== LC_ENTER
)
440 else if (map
->reason
== LC_LEAVE
)
442 print_line (map
, map
->from_line
, flags
);
448 /* Copy a #pragma directive to the preprocessed output. */
450 cb_def_pragma (pfile
, line
)
454 maybe_print_line (print
.map
, line
);
455 fputs ("#pragma ", print
.outf
);
456 cpp_output_line (pfile
, print
.outf
);
460 /* Dump out the hash table. */
462 dump_macro (pfile
, node
, v
)
465 void *v ATTRIBUTE_UNUSED
;
467 if (node
->type
== NT_MACRO
&& !(node
->flags
& NODE_BUILTIN
))
469 fputs ("#define ", print
.outf
);
470 fputs ((const char *) cpp_macro_definition (pfile
, node
), print
.outf
);
471 putc ('\n', print
.outf
);