mirror of
git://gcc.gnu.org/git/gcc.git
synced 2025-04-05 08:20:29 +08:00
c-family: token streamer
This is broken out of modules patch 01-langhooks.diff, I realized that this part is independent, and removes some duplicated code -- migrated to the token_streamer class. gcc/c-family/ * c-ppoutput.c (scan_translation_unit): Use token_streamer, remove code duplicating that functionality.
This commit is contained in:
parent
7f87b4ef23
commit
e3b55ce50e
@ -304,120 +304,18 @@ token_streamer::stream (cpp_reader *pfile, const cpp_token *token,
|
||||
static void
|
||||
scan_translation_unit (cpp_reader *pfile)
|
||||
{
|
||||
bool avoid_paste = false;
|
||||
bool do_line_adjustments
|
||||
= cpp_get_options (parse_in)->lang != CLK_ASM
|
||||
&& !flag_no_line_commands;
|
||||
bool in_pragma = false;
|
||||
bool line_marker_emitted = false;
|
||||
token_streamer streamer (pfile);
|
||||
|
||||
print.source = NULL;
|
||||
for (;;)
|
||||
{
|
||||
location_t loc;
|
||||
const cpp_token *token = cpp_get_token_with_location (pfile, &loc);
|
||||
|
||||
if (token->type == CPP_PADDING)
|
||||
{
|
||||
avoid_paste = true;
|
||||
if (print.source == NULL
|
||||
|| (!(print.source->flags & PREV_WHITE)
|
||||
&& token->val.source == NULL))
|
||||
print.source = token->val.source;
|
||||
continue;
|
||||
}
|
||||
location_t spelling_loc;
|
||||
const cpp_token *token
|
||||
= cpp_get_token_with_location (pfile, &spelling_loc);
|
||||
|
||||
streamer.stream (pfile, token, spelling_loc);
|
||||
if (token->type == CPP_EOF)
|
||||
break;
|
||||
|
||||
/* Subtle logic to output a space if and only if necessary. */
|
||||
if (avoid_paste)
|
||||
{
|
||||
int src_line = LOCATION_LINE (loc);
|
||||
|
||||
if (print.source == NULL)
|
||||
print.source = token;
|
||||
|
||||
if (src_line != print.src_line
|
||||
&& do_line_adjustments
|
||||
&& !in_pragma)
|
||||
{
|
||||
line_marker_emitted = do_line_change (pfile, token, loc, false);
|
||||
putc (' ', print.outf);
|
||||
print.printed = true;
|
||||
}
|
||||
else if (print.source->flags & PREV_WHITE
|
||||
|| (print.prev
|
||||
&& cpp_avoid_paste (pfile, print.prev, token))
|
||||
|| (print.prev == NULL && token->type == CPP_HASH))
|
||||
{
|
||||
putc (' ', print.outf);
|
||||
print.printed = true;
|
||||
}
|
||||
}
|
||||
else if (token->flags & PREV_WHITE)
|
||||
{
|
||||
int src_line = LOCATION_LINE (loc);
|
||||
|
||||
if (src_line != print.src_line
|
||||
&& do_line_adjustments
|
||||
&& !in_pragma)
|
||||
line_marker_emitted = do_line_change (pfile, token, loc, false);
|
||||
putc (' ', print.outf);
|
||||
print.printed = true;
|
||||
}
|
||||
|
||||
avoid_paste = false;
|
||||
print.source = NULL;
|
||||
print.prev = token;
|
||||
if (token->type == CPP_PRAGMA)
|
||||
{
|
||||
const char *space;
|
||||
const char *name;
|
||||
|
||||
line_marker_emitted = maybe_print_line (token->src_loc);
|
||||
fputs ("#pragma ", print.outf);
|
||||
c_pp_lookup_pragma (token->val.pragma, &space, &name);
|
||||
if (space)
|
||||
fprintf (print.outf, "%s %s", space, name);
|
||||
else
|
||||
fprintf (print.outf, "%s", name);
|
||||
print.printed = true;
|
||||
in_pragma = true;
|
||||
}
|
||||
else if (token->type == CPP_PRAGMA_EOL)
|
||||
{
|
||||
maybe_print_line (token->src_loc);
|
||||
in_pragma = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (cpp_get_options (parse_in)->debug)
|
||||
linemap_dump_location (line_table, token->src_loc, print.outf);
|
||||
|
||||
if (do_line_adjustments
|
||||
&& !in_pragma
|
||||
&& !line_marker_emitted
|
||||
&& print.prev_was_system_token != !!in_system_header_at (loc)
|
||||
&& !is_location_from_builtin_token (loc))
|
||||
/* The system-ness of this token is different from the one
|
||||
of the previous token. Let's emit a line change to
|
||||
mark the new system-ness before we emit the token. */
|
||||
{
|
||||
do_line_change (pfile, token, loc, false);
|
||||
print.prev_was_system_token = !!in_system_header_at (loc);
|
||||
}
|
||||
cpp_output_token (token, print.outf);
|
||||
line_marker_emitted = false;
|
||||
print.printed = true;
|
||||
}
|
||||
|
||||
/* CPP_COMMENT tokens and raw-string literal tokens can
|
||||
have embedded new-line characters. Rather than enumerating
|
||||
all the possible token types just check if token uses
|
||||
val.str union member. */
|
||||
if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
|
||||
account_for_newlines (token->val.str.text, token->val.str.len);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user