Represent column numbers using line-map's source_location.
[gcc.git] / gcc / c-ppoutput.c
1 /* Preprocess only, using cpplib.
2 Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
4 Written by Per Bothner, 1994-95.
5
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.
10
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.
15
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. */
19
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. */
29
30 /* Encapsulates state used to convert a stream of tokens into a text
31 file. */
32 static struct
33 {
34 FILE *outf; /* Stream to write to. */
35 const cpp_token *prev; /* Previous token. */
36 const cpp_token *source; /* Source token for spacing. */
37 int src_line; /* Line number currently being written. */
38 unsigned char printed; /* Nonzero if something output at line. */
39 } print;
40
41 /* General output routines. */
42 static void scan_translation_unit (cpp_reader *);
43 static void scan_translation_unit_trad (cpp_reader *);
44 static void account_for_newlines (const unsigned char *, size_t);
45 static int dump_macro (cpp_reader *, cpp_hashnode *, void *);
46
47 static void print_line (source_location, const char *);
48 static void maybe_print_line (source_location);
49
50 /* Callback routines for the parser. Most of these are active only
51 in specific modes. */
52 static void cb_line_change (cpp_reader *, const cpp_token *, int);
53 static void cb_define (cpp_reader *, fileline, cpp_hashnode *);
54 static void cb_undef (cpp_reader *, fileline, cpp_hashnode *);
55 static void cb_include (cpp_reader *, fileline, const unsigned char *,
56 const char *, int);
57 static void cb_ident (cpp_reader *, fileline, const cpp_string *);
58 static void cb_def_pragma (cpp_reader *, fileline);
59
60 /* Preprocess and output. */
61 void
62 preprocess_file (cpp_reader *pfile)
63 {
64 /* A successful cpp_read_main_file guarantees that we can call
65 cpp_scan_nooutput or cpp_get_token next. */
66 if (flag_no_output)
67 {
68 /* Scan -included buffers, then the main file. */
69 while (pfile->buffer->prev)
70 cpp_scan_nooutput (pfile);
71 cpp_scan_nooutput (pfile);
72 }
73 else if (cpp_get_options (pfile)->traditional)
74 scan_translation_unit_trad (pfile);
75 else
76 scan_translation_unit (pfile);
77
78 /* -dM command line option. Should this be elsewhere? */
79 if (flag_dump_macros == 'M')
80 cpp_forall_identifiers (pfile, dump_macro, NULL);
81
82 /* Flush any pending output. */
83 if (print.printed)
84 putc ('\n', print.outf);
85 }
86
87 /* Set up the callbacks as appropriate. */
88 void
89 init_pp_output (FILE *out_stream)
90 {
91 cpp_callbacks *cb = cpp_get_callbacks (parse_in);
92
93 if (!flag_no_output)
94 {
95 cb->line_change = cb_line_change;
96 /* Don't emit #pragma or #ident directives if we are processing
97 assembly language; the assembler may choke on them. */
98 if (cpp_get_options (parse_in)->lang != CLK_ASM)
99 {
100 cb->ident = cb_ident;
101 cb->def_pragma = cb_def_pragma;
102 }
103 }
104
105 if (flag_dump_includes)
106 cb->include = cb_include;
107
108 if (flag_dump_macros == 'N' || flag_dump_macros == 'D')
109 {
110 cb->define = cb_define;
111 cb->undef = cb_undef;
112 }
113
114 /* Initialize the print structure. Setting print.src_line to -1 here is
115 a trick to guarantee that the first token of the file will cause
116 a linemarker to be output by maybe_print_line. */
117 print.src_line = -1;
118 print.printed = 0;
119 print.prev = 0;
120 print.outf = out_stream;
121 }
122
123 /* Writes out the preprocessed file, handling spacing and paste
124 avoidance issues. */
125 static void
126 scan_translation_unit (cpp_reader *pfile)
127 {
128 bool avoid_paste = false;
129
130 print.source = NULL;
131 for (;;)
132 {
133 const cpp_token *token = cpp_get_token (pfile);
134
135 if (token->type == CPP_PADDING)
136 {
137 avoid_paste = true;
138 if (print.source == NULL
139 || (!(print.source->flags & PREV_WHITE)
140 && token->val.source == NULL))
141 print.source = token->val.source;
142 continue;
143 }
144
145 if (token->type == CPP_EOF)
146 break;
147
148 /* Subtle logic to output a space if and only if necessary. */
149 if (avoid_paste)
150 {
151 if (print.source == NULL)
152 print.source = token;
153 if (print.source->flags & PREV_WHITE
154 || (print.prev
155 && cpp_avoid_paste (pfile, print.prev, token))
156 || (print.prev == NULL && token->type == CPP_HASH))
157 putc (' ', print.outf);
158 }
159 else if (token->flags & PREV_WHITE)
160 putc (' ', print.outf);
161
162 avoid_paste = false;
163 print.source = NULL;
164 print.prev = token;
165 cpp_output_token (token, print.outf);
166
167 if (token->type == CPP_COMMENT)
168 account_for_newlines (token->val.str.text, token->val.str.len);
169 }
170 }
171
172 /* Adjust print.src_line for newlines embedded in output. */
173 static void
174 account_for_newlines (const unsigned char *str, size_t len)
175 {
176 while (len--)
177 if (*str++ == '\n')
178 print.src_line++;
179 }
180
181 /* Writes out a traditionally preprocessed file. */
182 static void
183 scan_translation_unit_trad (cpp_reader *pfile)
184 {
185 while (_cpp_read_logical_line_trad (pfile))
186 {
187 size_t len = pfile->out.cur - pfile->out.base;
188 maybe_print_line (pfile->out.first_line);
189 fwrite (pfile->out.base, 1, len, print.outf);
190 print.printed = 1;
191 if (!CPP_OPTION (pfile, discard_comments))
192 account_for_newlines (pfile->out.base, len);
193 }
194 }
195
196 /* If the token read on logical line LINE needs to be output on a
197 different line to the current one, output the required newlines or
198 a line marker, and return 1. Otherwise return 0. */
199 static void
200 maybe_print_line (source_location src_loc)
201 {
202 const struct line_map *map = linemap_lookup (&line_table, src_loc);
203 int src_line = SOURCE_LINE (map, src_loc);
204 /* End the previous line of text. */
205 if (print.printed)
206 {
207 putc ('\n', print.outf);
208 print.src_line++;
209 print.printed = 0;
210 }
211
212 if (src_line >= print.src_line && src_line < print.src_line + 8)
213 {
214 while (src_line > print.src_line)
215 {
216 putc ('\n', print.outf);
217 print.src_line++;
218 }
219 }
220 else
221 print_line (src_loc, "");
222 }
223
224 /* Output a line marker for logical line LINE. Special flags are "1"
225 or "2" indicating entering or leaving a file. */
226 static void
227 print_line (source_location src_loc, const char *special_flags)
228 {
229 /* End any previous line of text. */
230 if (print.printed)
231 putc ('\n', print.outf);
232 print.printed = 0;
233
234 if (!flag_no_line_commands)
235 {
236 const struct line_map *map = linemap_lookup (&line_table, src_loc);
237
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;
241
242 print.src_line = SOURCE_LINE (map, src_loc);
243
244 /* cpp_quote_string does not nul-terminate, so we have to do it
245 ourselves. */
246 p = cpp_quote_string (to_file_quoted,
247 (unsigned char *)map->to_file, to_file_len);
248 *p = '\0';
249 fprintf (print.outf, "# %u \"%s\"%s", print.src_line,
250 to_file_quoted, special_flags);
251
252 if (map->sysp == 2)
253 fputs (" 3 4", print.outf);
254 else if (map->sysp == 1)
255 fputs (" 3", print.outf);
256
257 putc ('\n', print.outf);
258 }
259 }
260
261 /* Called when a line of output is started. TOKEN is the first token
262 of the line, and at end of file will be CPP_EOF. */
263 static void
264 cb_line_change (cpp_reader *pfile, const cpp_token *token,
265 int parsing_args)
266 {
267 source_location src_loc = token->src_loc;
268
269 if (token->type == CPP_EOF || parsing_args)
270 return;
271
272 maybe_print_line (src_loc);
273 print.prev = 0;
274 print.source = 0;
275
276 /* Supply enough spaces to put this token in its original column,
277 one space per column greater than 2, since scan_translation_unit
278 will provide a space if PREV_WHITE. Don't bother trying to
279 reconstruct tabs; we can't get it right in general, and nothing
280 ought to care. Some things do care; the fault lies with them. */
281 if (!CPP_OPTION (pfile, traditional))
282 {
283 const struct line_map *map = linemap_lookup (&line_table, src_loc);
284 int spaces = SOURCE_COLUMN (map, src_loc) - 2;
285 print.printed = 1;
286
287 while (-- spaces >= 0)
288 putc (' ', print.outf);
289 }
290 }
291
292 static void
293 cb_ident (cpp_reader *pfile ATTRIBUTE_UNUSED, fileline line,
294 const cpp_string *str)
295 {
296 maybe_print_line (line);
297 fprintf (print.outf, "#ident \"%s\"\n", str->text);
298 print.src_line++;
299 }
300
301 static void
302 cb_define (cpp_reader *pfile, fileline line, cpp_hashnode *node)
303 {
304 maybe_print_line (line);
305 fputs ("#define ", print.outf);
306
307 /* 'D' is whole definition; 'N' is name only. */
308 if (flag_dump_macros == 'D')
309 fputs ((const char *) cpp_macro_definition (pfile, node),
310 print.outf);
311 else
312 fputs ((const char *) NODE_NAME (node), print.outf);
313
314 putc ('\n', print.outf);
315 print.src_line++;
316 }
317
318 static void
319 cb_undef (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
320 cpp_hashnode *node)
321 {
322 maybe_print_line (line);
323 fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
324 print.src_line++;
325 }
326
327 static void
328 cb_include (cpp_reader *pfile ATTRIBUTE_UNUSED, source_location line,
329 const unsigned char *dir, const char *header, int angle_brackets)
330 {
331 maybe_print_line (line);
332 if (angle_brackets)
333 fprintf (print.outf, "#%s <%s>\n", dir, header);
334 else
335 fprintf (print.outf, "#%s \"%s\"\n", dir, header);
336 print.src_line++;
337 }
338
339 /* Callback called when -fworking-director and -E to emit working
340 directory in cpp output file. */
341
342 void
343 pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
344 {
345 size_t to_file_len = strlen (dir);
346 unsigned char *to_file_quoted = alloca (to_file_len * 4 + 1);
347 unsigned char *p;
348
349 /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
350 p = cpp_quote_string (to_file_quoted, (unsigned char *) dir, to_file_len);
351 *p = '\0';
352 fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
353 }
354
355 /* The file name, line number or system header flags have changed, as
356 described in MAP. */
357
358 void
359 pp_file_change (const struct line_map *map)
360 {
361 const char *flags = "";
362
363 if (flag_no_line_commands || flag_no_output)
364 return;
365
366 if (map != NULL)
367 {
368 /* First time? */
369 if (MAIN_FILE_P (map))
370 {
371 /* Avoid printing foo.i when the main file is foo.c. */
372 if (!cpp_get_options (parse_in)->preprocessed)
373 print_line (map->start_location, flags);
374 }
375 else
376 {
377 /* Bring current file to correct line when entering a new file. */
378 if (map->reason == LC_ENTER)
379 {
380 const struct line_map *from = INCLUDED_FROM (&line_table, map);
381 maybe_print_line (LAST_SOURCE_LINE_LOCATION (from));
382 }
383 if (map->reason == LC_ENTER)
384 flags = " 1";
385 else if (map->reason == LC_LEAVE)
386 flags = " 2";
387 print_line (map->start_location, flags);
388 }
389 }
390 }
391
392 /* Copy a #pragma directive to the preprocessed output. */
393 static void
394 cb_def_pragma (cpp_reader *pfile, fileline line)
395 {
396 maybe_print_line (line);
397 fputs ("#pragma ", print.outf);
398 cpp_output_line (pfile, print.outf);
399 print.src_line++;
400 }
401
402 /* Dump out the hash table. */
403 static int
404 dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
405 {
406 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
407 {
408 fputs ("#define ", print.outf);
409 fputs ((const char *) cpp_macro_definition (pfile, node),
410 print.outf);
411 putc ('\n', print.outf);
412 print.src_line++;
413 }
414
415 return 1;
416 }