* gcc.dg/cpp/separate-1.c: Adjust line of error. Test for correct
[official-gcc.git] / gcc / c-ppoutput.c
blob68b45d2f517de4f8c37ad88758f96f03dd36218d
1 /* Preprocess only, using cpplib.
2 Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003
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
9 later version.
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 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "cpplib.h"
25 #include "cpphash.h"
26 #include "tree.h"
27 #include "c-common.h" /* For flags. */
28 #include "c-pragma.h" /* For parse_in. */
30 /* Encapsulates state used to convert a stream of tokens into a text
31 file. */
32 static struct
34 FILE *outf; /* Stream to write to. */
35 const struct line_map *map; /* Logical to physical line mappings. */
36 const cpp_token *prev; /* Previous token. */
37 const cpp_token *source; /* Source token for spacing. */
38 unsigned int line; /* Line currently being written. */
39 unsigned char printed; /* Nonzero if something output at line. */
40 } print;
42 /* General output routines. */
43 static void scan_translation_unit (cpp_reader *);
44 static void scan_translation_unit_trad (cpp_reader *);
45 static void account_for_newlines (const unsigned char *, size_t);
46 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
48 static void print_line (const struct line_map *, unsigned int,
49 const char *);
50 static void maybe_print_line (const struct line_map *, unsigned int);
52 /* Callback routines for the parser. Most of these are active only
53 in specific modes. */
54 static void cb_line_change (cpp_reader *, const cpp_token *, int);
55 static void cb_define (cpp_reader *, unsigned int, cpp_hashnode *);
56 static void cb_undef (cpp_reader *, unsigned int, cpp_hashnode *);
57 static void cb_include (cpp_reader *, unsigned int, const unsigned char *,
58 const char *, int);
59 static void cb_ident (cpp_reader *, unsigned int, const cpp_string *);
60 static void cb_def_pragma (cpp_reader *, unsigned int);
62 /* Preprocess and output. */
63 void
64 preprocess_file (cpp_reader *pfile)
66 /* A successful cpp_read_main_file guarantees that we can call
67 cpp_scan_nooutput or cpp_get_token next. */
68 if (flag_no_output)
70 /* Scan -included buffers, then the main file. */
71 while (pfile->buffer->prev)
72 cpp_scan_nooutput (pfile);
73 cpp_scan_nooutput (pfile);
75 else if (cpp_get_options (pfile)->traditional)
76 scan_translation_unit_trad (pfile);
77 else
78 scan_translation_unit (pfile);
80 /* -dM command line option. Should this be elsewhere? */
81 if (flag_dump_macros == 'M')
82 cpp_forall_identifiers (pfile, dump_macro, NULL);
84 /* Flush any pending output. */
85 if (print.printed)
86 putc ('\n', print.outf);
89 /* Set up the callbacks as appropriate. */
90 void
91 init_pp_output (FILE *out_stream)
93 cpp_callbacks *cb = cpp_get_callbacks (parse_in);
95 if (!flag_no_output)
97 cb->line_change = cb_line_change;
98 /* Don't emit #pragma or #ident directives if we are processing
99 assembly language; the assembler may choke on them. */
100 if (cpp_get_options (parse_in)->lang != CLK_ASM)
102 cb->ident = cb_ident;
103 cb->def_pragma = cb_def_pragma;
107 if (flag_dump_includes)
108 cb->include = cb_include;
110 if (flag_dump_macros == 'N' || flag_dump_macros == 'D')
112 cb->define = cb_define;
113 cb->undef = cb_undef;
116 /* Initialize the print structure. Setting print.line to -1 here is
117 a trick to guarantee that the first token of the file will cause
118 a linemarker to be output by maybe_print_line. */
119 print.line = (unsigned int) -1;
120 print.printed = 0;
121 print.prev = 0;
122 print.map = 0;
123 print.outf = out_stream;
126 /* Writes out the preprocessed file, handling spacing and paste
127 avoidance issues. */
128 static void
129 scan_translation_unit (cpp_reader *pfile)
131 bool avoid_paste = false;
133 print.source = NULL;
134 for (;;)
136 const cpp_token *token = cpp_get_token (pfile);
138 if (token->type == CPP_PADDING)
140 avoid_paste = true;
141 if (print.source == NULL
142 || (!(print.source->flags & PREV_WHITE)
143 && token->val.source == NULL))
144 print.source = token->val.source;
145 continue;
148 if (token->type == CPP_EOF)
149 break;
151 /* Subtle logic to output a space if and only if necessary. */
152 if (avoid_paste)
154 if (print.source == NULL)
155 print.source = token;
156 if (print.source->flags & PREV_WHITE
157 || (print.prev
158 && cpp_avoid_paste (pfile, print.prev, token))
159 || (print.prev == NULL && token->type == CPP_HASH))
160 putc (' ', print.outf);
162 else if (token->flags & PREV_WHITE)
163 putc (' ', print.outf);
165 avoid_paste = false;
166 print.source = NULL;
167 print.prev = token;
168 cpp_output_token (token, print.outf);
170 if (token->type == CPP_COMMENT)
171 account_for_newlines (token->val.str.text, token->val.str.len);
175 /* Adjust print.line for newlines embedded in output. */
176 static void
177 account_for_newlines (const unsigned char *str, size_t len)
179 while (len--)
180 if (*str++ == '\n')
181 print.line++;
184 /* Writes out a traditionally preprocessed file. */
185 static void
186 scan_translation_unit_trad (cpp_reader *pfile)
188 while (_cpp_read_logical_line_trad (pfile))
190 size_t len = pfile->out.cur - pfile->out.base;
191 maybe_print_line (print.map, pfile->out.first_line);
192 fwrite (pfile->out.base, 1, len, print.outf);
193 print.printed = 1;
194 if (!CPP_OPTION (pfile, discard_comments))
195 account_for_newlines (pfile->out.base, len);
199 /* If the token read on logical line LINE needs to be output on a
200 different line to the current one, output the required newlines or
201 a line marker, and return 1. Otherwise return 0. */
202 static void
203 maybe_print_line (const struct line_map *map, unsigned int line)
205 /* End the previous line of text. */
206 if (print.printed)
208 putc ('\n', print.outf);
209 print.line++;
210 print.printed = 0;
213 if (line >= print.line && line < print.line + 8)
215 while (line > print.line)
217 putc ('\n', print.outf);
218 print.line++;
221 else
222 print_line (map, line, "");
225 /* Output a line marker for logical line LINE. Special flags are "1"
226 or "2" indicating entering or leaving a file. */
227 static void
228 print_line (const struct line_map *map, unsigned int line, const char *special_flags)
230 /* End any previous line of text. */
231 if (print.printed)
232 putc ('\n', print.outf);
233 print.printed = 0;
235 print.line = line;
236 if (!flag_no_line_commands)
238 size_t to_file_len = strlen (map->to_file);
239 unsigned char *to_file_quoted = alloca (to_file_len * 4 + 1);
240 unsigned char *p;
242 /* cpp_quote_string does not nul-terminate, so we have to do it
243 ourselves. */
244 p = cpp_quote_string (to_file_quoted,
245 (unsigned char *)map->to_file, to_file_len);
246 *p = '\0';
247 fprintf (print.outf, "# %u \"%s\"%s",
248 SOURCE_LINE (map, print.line),
249 to_file_quoted, special_flags);
251 if (map->sysp == 2)
252 fputs (" 3 4", print.outf);
253 else if (map->sysp == 1)
254 fputs (" 3", print.outf);
256 putc ('\n', print.outf);
260 /* Called when a line of output is started. TOKEN is the first token
261 of the line, and at end of file will be CPP_EOF. */
262 static void
263 cb_line_change (cpp_reader *pfile, const cpp_token *token,
264 int parsing_args ATTRIBUTE_UNUSED)
266 if (token->type == CPP_EOF)
267 return;
269 maybe_print_line (print.map, token->line);
270 print.prev = 0;
271 print.source = 0;
273 /* Supply enough spaces to put this token in its original column,
274 one space per column greater than 2, since scan_translation_unit
275 will provide a space if PREV_WHITE. Don't bother trying to
276 reconstruct tabs; we can't get it right in general, and nothing
277 ought to care. Some things do care; the fault lies with them. */
278 if (!CPP_OPTION (pfile, traditional))
280 print.printed = 1;
281 if (token->col > 2)
283 unsigned int spaces = token->col - 2;
285 while (spaces--)
286 putc (' ', print.outf);
291 static void
292 cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, unsigned int line,
293 const cpp_string *str)
295 maybe_print_line (print.map, line);
296 fprintf (print.outf, "#ident \"%s\"\n", str->text);
297 print.line++;
300 static void
301 cb_define (cpp_reader *pfile, unsigned int line, cpp_hashnode *node)
303 maybe_print_line (print.map, line);
304 fputs ("#define ", print.outf);
306 /* 'D' is whole definition; 'N' is name only. */
307 if (flag_dump_macros == 'D')
308 fputs ((const char *) cpp_macro_definition (pfile, node),
309 print.outf);
310 else
311 fputs ((const char *) NODE_NAME (node), print.outf);
313 putc ('\n', print.outf);
314 print.line++;
317 static void
318 cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, unsigned int line,
319 cpp_hashnode *node)
321 maybe_print_line (print.map, line);
322 fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
323 print.line++;
326 static void
327 cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, unsigned int line,
328 const unsigned char *dir, const char *header, int angle_brackets)
330 maybe_print_line (print.map, line);
331 if (angle_brackets)
332 fprintf (print.outf, "#%s <%s>\n", dir, header);
333 else
334 fprintf (print.outf, "#%s \"%s\"\n", dir, header);
335 print.line++;
338 /* The file name, line number or system header flags have changed, as
339 described in MAP. From this point on, the old print.map might be
340 pointing to freed memory, and so must not be dereferenced. */
342 void
343 pp_file_change (const struct line_map *map)
345 const char *flags = "";
347 if (flag_no_line_commands || flag_no_output)
348 return;
350 /* First time? */
351 if (print.map == NULL)
353 /* Avoid printing foo.i when the main file is foo.c. */
354 if (!cpp_get_options (parse_in)->preprocessed)
355 print_line (map, map->from_line, flags);
357 else
359 /* Bring current file to correct line when entering a new file. */
360 if (map->reason == LC_ENTER)
361 maybe_print_line (map - 1, map->from_line - 1);
363 if (map->reason == LC_ENTER)
364 flags = " 1";
365 else if (map->reason == LC_LEAVE)
366 flags = " 2";
367 print_line (map, map->from_line, flags);
370 print.map = map;
373 /* Copy a #pragma directive to the preprocessed output. */
374 static void
375 cb_def_pragma (cpp_reader *pfile, unsigned int line)
377 maybe_print_line (print.map, line);
378 fputs ("#pragma ", print.outf);
379 cpp_output_line (pfile, print.outf);
380 print.line++;
383 /* Dump out the hash table. */
384 static int
385 dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
387 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
389 fputs ("#define ", print.outf);
390 fputs ((const char *) cpp_macro_definition (pfile, node),
391 print.outf);
392 putc ('\n', print.outf);
393 print.line++;
396 return 1;