d575f9fba51e96249576f4b2e35c532eda6f11d2
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 *last_fname
; /* previous file name. */
35 const char *syshdr_flags
; /* system header flags, if any. */
36 unsigned int lineno
; /* line currently being written. */
37 unsigned char printed
; /* nonzero if something output at lineno. */
40 int main
PARAMS ((int, char **));
41 static void general_init
PARAMS ((const char *));
42 static void setup_callbacks
PARAMS ((void));
44 /* General output routines. */
45 static void scan_buffer
PARAMS ((cpp_reader
*));
46 static void check_multiline_token
PARAMS ((cpp_string
*));
47 static int printer_init
PARAMS ((cpp_reader
*));
48 static int dump_macro
PARAMS ((cpp_reader
*, cpp_hashnode
*, void *));
50 static void print_line
PARAMS ((const char *));
51 static void maybe_print_line
PARAMS ((unsigned int));
53 /* Callback routines for the parser. Most of these are active only
55 static void cb_define
PARAMS ((cpp_reader
*, cpp_hashnode
*));
56 static void cb_undef
PARAMS ((cpp_reader
*, cpp_hashnode
*));
57 static void cb_include
PARAMS ((cpp_reader
*, const unsigned char *,
59 static void cb_ident
PARAMS ((cpp_reader
*, const cpp_string
*));
60 static void cb_file_change
PARAMS ((cpp_reader
*, const cpp_file_change
*));
61 static void cb_def_pragma
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
;
182 /* Writes out the preprocessed file. Alternates between two tokens,
183 so that we can avoid accidental token pasting. */
188 unsigned int index
, line
;
189 cpp_token tokens
[2], *token
;
193 for (index
= 0;; index
= 1 - index
)
195 token
= &tokens
[index
];
196 cpp_get_token (pfile
, token
);
198 if (token
->type
== CPP_EOF
)
201 line
= cpp_get_line (pfile
)->output_line
;
202 if (print
.lineno
!= line
)
204 unsigned int col
= cpp_get_line (pfile
)->col
;
206 /* Supply enough whitespace to put this token in its original
207 column. Don't bother trying to reconstruct tabs; we can't
208 get it right in general, and nothing ought to care. (Yes,
209 some things do care; the fault lies with them.) */
210 maybe_print_line (line
);
213 if (token
->flags
& PREV_WHITE
)
216 putc (' ', print
.outf
);
219 else if (print
.printed
220 && ! (token
->flags
& PREV_WHITE
)
221 && options
->lang
!= CLK_ASM
222 && cpp_avoid_paste (pfile
, &tokens
[1 - index
], token
))
223 token
->flags
|= PREV_WHITE
;
225 cpp_output_token (token
, print
.outf
);
227 if (token
->type
== CPP_STRING
|| token
->type
== CPP_WSTRING
228 || token
->type
== CPP_COMMENT
)
229 check_multiline_token (&token
->val
.str
);
232 while (cpp_pop_buffer (pfile
) != 0);
235 /* Adjust print.lineno for newlines embedded in tokens. */
237 check_multiline_token (str
)
242 for (i
= 0; i
< str
->len
; i
++)
243 if (str
->text
[i
] == '\n')
247 /* Initialize a cpp_printer structure. As a side effect, open the
253 print
.last_fname
= 0;
257 if (options
->out_fname
== NULL
)
258 options
->out_fname
= "";
260 if (options
->out_fname
[0] == '\0')
264 print
.outf
= fopen (options
->out_fname
, "w");
267 cpp_notice_from_errno (pfile
, options
-> out_fname
);
274 /* Newline-terminate any output line currently in progress. If
275 appropriate, write the current line number to the output, or pad
276 with newlines so the output line matches the current line. */
278 maybe_print_line (line
)
281 /* End the previous line of text (probably only needed until we get
282 multi-line tokens fixed). */
285 putc ('\n', print
.outf
);
290 if (options
->no_line_commands
)
296 /* print.lineno is zero if this is the first token of the file. We
297 handle this specially, so that a first line of "# 1 "foo.c" in
298 file foo.i outputs just the foo.c line, and not a foo.i line. */
299 if (line
>= print
.lineno
&& line
< print
.lineno
+ 8 && print
.lineno
)
301 while (line
> print
.lineno
)
303 putc ('\n', print
.outf
);
315 print_line (special_flags
)
316 const char *special_flags
;
318 /* End any previous line of text. */
320 putc ('\n', print
.outf
);
323 fprintf (print
.outf
, "# %u \"%s\"%s%s\n",
324 print
.lineno
, print
.last_fname
, special_flags
, print
.syshdr_flags
);
330 cb_ident (pfile
, str
)
331 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
332 const cpp_string
* str
;
334 maybe_print_line (cpp_get_line (pfile
)->output_line
);
335 fprintf (print
.outf
, "#ident \"%.*s\"\n", (int) str
->len
, str
->text
);
340 cb_define (pfile
, node
)
344 maybe_print_line (cpp_get_line (pfile
)->output_line
);
345 fprintf (print
.outf
, "#define %s", node
->name
);
347 /* -dD command line option. */
348 if (options
->dump_macros
== dump_definitions
)
349 fputs ((const char *) cpp_macro_definition (pfile
, node
), print
.outf
);
351 putc ('\n', print
.outf
);
356 cb_undef (pfile
, node
)
360 maybe_print_line (cpp_get_line (pfile
)->output_line
);
361 fprintf (print
.outf
, "#undef %s\n", node
->name
);
366 cb_include (pfile
, dir
, header
)
367 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
368 const unsigned char *dir
;
369 const cpp_token
*header
;
371 maybe_print_line (cpp_get_line (pfile
)->output_line
);
372 fprintf (print
.outf
, "#%s %s\n", dir
, cpp_token_as_text (pfile
, header
));
377 cb_file_change (pfile
, fc
)
378 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
379 const cpp_file_change
*fc
;
381 /* Bring current file to correct line (except first file). */
382 if (fc
->reason
== FC_ENTER
&& fc
->from
.filename
)
383 maybe_print_line (fc
->from
.lineno
);
385 print
.last_fname
= fc
->to
.filename
;
387 print
.syshdr_flags
= " 3 4";
389 print
.syshdr_flags
= " 3";
391 print
.syshdr_flags
= "";
395 const char *flags
= "";
397 print
.lineno
= fc
->to
.lineno
;
398 if (fc
->reason
== FC_ENTER
)
400 else if (fc
->reason
== FC_LEAVE
)
403 if (! options
->no_line_commands
)
409 cb_def_pragma (pfile
)
412 maybe_print_line (cpp_get_line (pfile
)->output_line
);
413 fputs ("#pragma ", print
.outf
);
414 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
);