00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "config.h"
00025 #include "system.h"
00026 #include "cpplib.h"
00027 #include "cpphash.h"
00028
00029 static void setup_callbacks PARAMS ((cpp_reader *));
00030
00031
00032 static void scan_translation_unit PARAMS ((cpp_reader *));
00033 static void scan_translation_unit_trad PARAMS ((cpp_reader *));
00034 static void account_for_newlines PARAMS ((cpp_reader *, const uchar *,
00035 size_t));
00036 static int dump_macro PARAMS ((cpp_reader *, cpp_hashnode *, void *));
00037
00038 static void print_line PARAMS ((cpp_reader *, const struct line_map *,
00039 unsigned int, const char *));
00040 static void maybe_print_line PARAMS ((cpp_reader *, const struct line_map *,
00041 unsigned int));
00042
00043
00044
00045 static void cb_line_change PARAMS ((cpp_reader *, const cpp_token *, int));
00046 static void cb_define PARAMS ((cpp_reader *, unsigned int, cpp_hashnode *));
00047 static void cb_undef PARAMS ((cpp_reader *, unsigned int, cpp_hashnode *));
00048 static void cb_include PARAMS ((cpp_reader *, unsigned int,
00049 const unsigned char *, const cpp_token *));
00050 static void cb_ident PARAMS ((cpp_reader *, unsigned int,
00051 const cpp_string *));
00052 static void cb_file_change PARAMS ((cpp_reader *, const struct line_map *));
00053 static void cb_def_pragma PARAMS ((cpp_reader *, unsigned int));
00054
00055
00056 void
00057 cpp_preprocess_file (pfile, in_fname, out_stream)
00058 cpp_reader *pfile;
00059 const char *in_fname;
00060 FILE *out_stream;
00061 {
00062
00063
00064
00065 pfile->print.line = (unsigned int) -1;
00066 pfile->print.printed = 0;
00067 pfile->print.prev = 0;
00068 pfile->print.map = 0;
00069 pfile->print.outf = out_stream;
00070
00071 setup_callbacks (pfile);
00072
00073 if (cpp_read_main_file (pfile, in_fname, NULL))
00074 {
00075 cpp_options *options = &pfile->opts;
00076 cpp_finish_options (pfile);
00077
00078
00079
00080 if (options->no_output)
00081 {
00082
00083 while (pfile->buffer->prev)
00084 cpp_scan_nooutput (pfile);
00085 cpp_scan_nooutput (pfile);
00086 }
00087 else if (options->traditional)
00088 scan_translation_unit_trad (pfile);
00089 else
00090 scan_translation_unit (pfile);
00091
00092
00093 if (options->dump_macros == dump_only)
00094 cpp_forall_identifiers (pfile, dump_macro, NULL);
00095 }
00096
00097
00098 if (pfile->print.printed)
00099 putc ('\n', pfile->print.outf);
00100 }
00101
00102
00103 static void
00104 setup_callbacks (pfile)
00105 cpp_reader *pfile;
00106 {
00107 cpp_options *options = &pfile->opts;
00108 cpp_callbacks *cb = cpp_get_callbacks (pfile);
00109
00110 if (! options->no_output)
00111 {
00112 cb->line_change = cb_line_change;
00113
00114
00115 if (options->lang != CLK_ASM)
00116 {
00117 cb->ident = cb_ident;
00118 cb->def_pragma = cb_def_pragma;
00119 }
00120 if (! options->no_line_commands)
00121 cb->file_change = cb_file_change;
00122 }
00123
00124 if (options->dump_includes)
00125 cb->include = cb_include;
00126
00127 if (options->dump_macros == dump_names
00128 || options->dump_macros == dump_definitions)
00129 {
00130 cb->define = cb_define;
00131 cb->undef = cb_undef;
00132 }
00133 }
00134
00135
00136
00137 static void
00138 scan_translation_unit (pfile)
00139 cpp_reader *pfile;
00140 {
00141 bool avoid_paste = false;
00142
00143 pfile->print.source = NULL;
00144 for (;;)
00145 {
00146 const cpp_token *token = cpp_get_token (pfile);
00147
00148 if (token->type == CPP_PADDING)
00149 {
00150 avoid_paste = true;
00151 if (pfile->print.source == NULL
00152 || (!(pfile->print.source->flags & PREV_WHITE)
00153 && token->val.source == NULL))
00154 pfile->print.source = token->val.source;
00155 continue;
00156 }
00157
00158 if (token->type == CPP_EOF)
00159 break;
00160
00161
00162 if (avoid_paste)
00163 {
00164 if (pfile->print.source == NULL)
00165 pfile->print.source = token;
00166 if (pfile->print.source->flags & PREV_WHITE
00167 || (pfile->print.prev
00168 && cpp_avoid_paste (pfile, pfile->print.prev, token))
00169 || (pfile->print.prev == NULL && token->type == CPP_HASH))
00170 putc (' ', pfile->print.outf);
00171 }
00172 else if (token->flags & PREV_WHITE)
00173 putc (' ', pfile->print.outf);
00174
00175 avoid_paste = false;
00176 pfile->print.source = NULL;
00177 pfile->print.prev = token;
00178 cpp_output_token (token, pfile->print.outf);
00179
00180 if (token->type == CPP_COMMENT)
00181 account_for_newlines (pfile, token->val.str.text, token->val.str.len);
00182 }
00183 }
00184
00185
00186 static void
00187 account_for_newlines (pfile, str, len)
00188 cpp_reader *pfile;
00189 const uchar *str;
00190 size_t len;
00191 {
00192 while (len--)
00193 if (*str++ == '\n')
00194 pfile->print.line++;
00195 }
00196
00197
00198 static void
00199 scan_translation_unit_trad (pfile)
00200 cpp_reader *pfile;
00201 {
00202 while (_cpp_read_logical_line_trad (pfile))
00203 {
00204 size_t len = pfile->out.cur - pfile->out.base;
00205 maybe_print_line (pfile, pfile->print.map, pfile->out.first_line);
00206 fwrite (pfile->out.base, 1, len, pfile->print.outf);
00207 pfile->print.printed = 1;
00208 if (!CPP_OPTION (pfile, discard_comments))
00209 account_for_newlines (pfile, pfile->out.base, len);
00210 }
00211 }
00212
00213
00214
00215
00216 static void
00217 maybe_print_line (pfile, map, line)
00218 cpp_reader *pfile;
00219 const struct line_map *map;
00220 unsigned int line;
00221 {
00222
00223 if (pfile->print.printed)
00224 {
00225 putc ('\n', pfile->print.outf);
00226 pfile->print.line++;
00227 pfile->print.printed = 0;
00228 }
00229
00230 if (line >= pfile->print.line && line < pfile->print.line + 8)
00231 {
00232 while (line > pfile->print.line)
00233 {
00234 putc ('\n', pfile->print.outf);
00235 pfile->print.line++;
00236 }
00237 }
00238 else
00239 print_line (pfile, map, line, "");
00240 }
00241
00242
00243
00244 static void
00245 print_line (pfile, map, line, special_flags)
00246 cpp_reader *pfile;
00247 const struct line_map *map;
00248 unsigned int line;
00249 const char *special_flags;
00250 {
00251
00252 if (pfile->print.printed)
00253 putc ('\n', pfile->print.outf);
00254 pfile->print.printed = 0;
00255
00256 pfile->print.line = line;
00257 if (! CPP_OPTION (pfile, no_line_commands))
00258 {
00259 size_t to_file_len = strlen (map->to_file);
00260 unsigned char *to_file_quoted = alloca (to_file_len * 4 + 1);
00261 unsigned char *p;
00262
00263
00264
00265 p = cpp_quote_string (to_file_quoted,
00266 (unsigned char *)map->to_file, to_file_len);
00267 *p = '\0';
00268 fprintf (pfile->print.outf, "# %u \"%s\"%s",
00269 SOURCE_LINE (map, pfile->print.line),
00270 to_file_quoted, special_flags);
00271
00272 if (map->sysp == 2)
00273 fputs (" 3 4", pfile->print.outf);
00274 else if (map->sysp == 1)
00275 fputs (" 3", pfile->print.outf);
00276
00277 putc ('\n', pfile->print.outf);
00278 }
00279 }
00280
00281
00282
00283 static void
00284 cb_line_change (pfile, token, parsing_args)
00285 cpp_reader *pfile;
00286 const cpp_token *token;
00287 int parsing_args;
00288 {
00289 if (token->type == CPP_EOF || parsing_args)
00290 return;
00291
00292 maybe_print_line (pfile, pfile->print.map, token->line);
00293 pfile->print.prev = 0;
00294 pfile->print.source = 0;
00295
00296
00297
00298
00299
00300
00301 if (!CPP_OPTION (pfile, traditional))
00302 {
00303 pfile->print.printed = 1;
00304 if (token->col > 2)
00305 {
00306 unsigned int spaces = token->col - 2;
00307
00308 while (spaces--)
00309 putc (' ', pfile->print.outf);
00310 }
00311 }
00312 }
00313
00314 static void
00315 cb_ident (pfile, line, str)
00316 cpp_reader *pfile;
00317 unsigned int line;
00318 const cpp_string * str;
00319 {
00320 maybe_print_line (pfile, pfile->print.map, line);
00321 fprintf (pfile->print.outf, "#ident \"%s\"\n", str->text);
00322 pfile->print.line++;
00323 }
00324
00325 static void
00326 cb_define (pfile, line, node)
00327 cpp_reader *pfile;
00328 unsigned int line;
00329 cpp_hashnode *node;
00330 {
00331 maybe_print_line (pfile, pfile->print.map, line);
00332 fputs ("#define ", pfile->print.outf);
00333
00334
00335 if (CPP_OPTION (pfile, dump_macros) == dump_definitions)
00336 fputs ((const char *) cpp_macro_definition (pfile, node),
00337 pfile->print.outf);
00338 else
00339 fputs ((const char *) NODE_NAME (node), pfile->print.outf);
00340
00341 putc ('\n', pfile->print.outf);
00342 pfile->print.line++;
00343 }
00344
00345 static void
00346 cb_undef (pfile, line, node)
00347 cpp_reader *pfile;
00348 unsigned int line;
00349 cpp_hashnode *node;
00350 {
00351 maybe_print_line (pfile, pfile->print.map, line);
00352 fprintf (pfile->print.outf, "#undef %s\n", NODE_NAME (node));
00353 pfile->print.line++;
00354 }
00355
00356 static void
00357 cb_include (pfile, line, dir, header)
00358 cpp_reader *pfile;
00359 unsigned int line;
00360 const unsigned char *dir;
00361 const cpp_token *header;
00362 {
00363 maybe_print_line (pfile, pfile->print.map, line);
00364 fprintf (pfile->print.outf, "#%s %s\n", dir,
00365 cpp_token_as_text (pfile, header));
00366 pfile->print.line++;
00367 }
00368
00369
00370
00371
00372
00373 static void
00374 cb_file_change (pfile, map)
00375 cpp_reader *pfile;
00376 const struct line_map *map;
00377 {
00378 const char *flags = "";
00379
00380
00381 if (pfile->print.map == NULL)
00382 {
00383
00384 if (!CPP_OPTION (pfile, preprocessed))
00385 print_line (pfile, map, map->from_line, flags);
00386 }
00387 else
00388 {
00389
00390 if (map->reason == LC_ENTER)
00391 maybe_print_line (pfile, map - 1, map->from_line - 1);
00392
00393 if (map->reason == LC_ENTER)
00394 flags = " 1";
00395 else if (map->reason == LC_LEAVE)
00396 flags = " 2";
00397 print_line (pfile, map, map->from_line, flags);
00398 }
00399
00400 pfile->print.map = map;
00401 }
00402
00403
00404 static void
00405 cb_def_pragma (pfile, line)
00406 cpp_reader *pfile;
00407 unsigned int line;
00408 {
00409 maybe_print_line (pfile, pfile->print.map, line);
00410 fputs ("#pragma ", pfile->print.outf);
00411 cpp_output_line (pfile, pfile->print.outf);
00412 pfile->print.line++;
00413 }
00414
00415
00416 static int
00417 dump_macro (pfile, node, v)
00418 cpp_reader *pfile;
00419 cpp_hashnode *node;
00420 void *v ATTRIBUTE_UNUSED;
00421 {
00422 if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
00423 {
00424 fputs ("#define ", pfile->print.outf);
00425 fputs ((const char *) cpp_macro_definition (pfile, node),
00426 pfile->print.outf);
00427 putc ('\n', pfile->print.outf);
00428 pfile->print.line++;
00429 }
00430
00431 return 1;
00432 }