/* CPP main program, using CPP Library.
- Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001
+ Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2002
Free Software Foundation, Inc.
Written by Per Bothner, 1994-95.
{
FILE *outf; /* Stream to write to. */
const struct line_map *map; /* Logical to physical line mappings. */
+ const cpp_token *prev; /* Previous token. */
+ const cpp_token *source; /* Source token for spacing. */
unsigned int line; /* Line currently being written. */
unsigned char printed; /* Nonzero if something output at line. */
};
/* General output routines. */
static void scan_translation_unit PARAMS ((cpp_reader *));
-static void check_multiline_token PARAMS ((cpp_string *));
+static void check_multiline_token PARAMS ((const cpp_string *));
static int dump_macro PARAMS ((cpp_reader *, cpp_hashnode *, void *));
static void print_line PARAMS ((const struct line_map *, unsigned int,
{
general_init (argv[0]);
- /* Contruct a reader with default language GNU C89. */
- pfile = cpp_create_reader (NULL, CLK_GNUC89);
+ /* Construct a reader with default language GNU C89. */
+ pfile = cpp_create_reader (CLK_GNUC89);
options = cpp_get_options (pfile);
do_preprocessing (argc, argv);
xmalloc_set_program_name (progname);
-/* LC_CTYPE determines the character set used by the terminal so it
- has to be set to output messages correctly. */
-
-#ifdef HAVE_LC_MESSAGES
- setlocale (LC_CTYPE, "");
- setlocale (LC_MESSAGES, "");
-#else
- setlocale (LC_ALL, "");
-#endif
-
- (void) bindtextdomain (PACKAGE, localedir);
- (void) textdomain (PACKAGE);
+ hex_init ();
+ gcc_init_libintl ();
}
/* Handle switches, preprocess and output. */
return;
if (argi < argc)
- cpp_fatal (pfile, "Invalid option %s", argv[argi]);
- else
- cpp_post_options (pfile);
+ {
+ cpp_fatal (pfile, "invalid option %s", argv[argi]);
+ return;
+ }
+ cpp_post_options (pfile);
if (CPP_FATAL_ERRORS (pfile))
return;
cause a linemarker to be output by maybe_print_line. */
print.line = (unsigned int) -1;
print.printed = 0;
+ print.prev = 0;
print.map = 0;
/* Open the output now. We must do so even if no_output is on,
setup_callbacks ();
- if (cpp_start_read (pfile, options->in_fname))
+ if (cpp_read_main_file (pfile, options->in_fname, NULL))
{
- /* A successful cpp_start_read guarantees that we can call
+ cpp_finish_options (pfile);
+
+ /* A successful cpp_read_main_file guarantees that we can call
cpp_scan_nooutput or cpp_get_token next. */
if (options->no_output)
cpp_scan_nooutput (pfile);
}
}
-/* Writes out the preprocessed file. Alternates between two tokens,
- so that we can avoid accidental token pasting. */
+/* Writes out the preprocessed file, handling spacing and paste
+ avoidance issues. */
static void
scan_translation_unit (pfile)
cpp_reader *pfile;
{
- unsigned int index;
- cpp_token tokens[2], *token;
+ bool avoid_paste = false;
- for (index = 0;; index = 1 - index)
+ print.source = NULL;
+ for (;;)
{
- token = &tokens[index];
- cpp_get_token (pfile, token);
+ const cpp_token *token = cpp_get_token (pfile);
+
+ 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;
+ }
if (token->type == CPP_EOF)
break;
- if ((token->flags & (PREV_WHITE | AVOID_LPASTE | BOL)) == AVOID_LPASTE
- && cpp_avoid_paste (pfile, &tokens[1 - index], token))
- token->flags |= PREV_WHITE;
- /* Special case '# <directive name>': insert a space between
- the # and the token. This will prevent it from being
- treated as a directive when this code is re-preprocessed.
- XXX Should do this only at the beginning of a line, but how? */
- else if (token->type == CPP_NAME && token->val.node->directive_index
- && tokens[1 - index].type == CPP_HASH)
- token->flags |= PREV_WHITE;
+ /* Subtle logic to output a space if and only if necessary. */
+ if (avoid_paste)
+ {
+ if (print.source == NULL)
+ print.source = token;
+ 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);
+ }
+ else if (token->flags & PREV_WHITE)
+ putc (' ', print.outf);
+ avoid_paste = false;
+ print.source = NULL;
+ print.prev = token;
cpp_output_token (token, print.outf);
+
if (token->type == CPP_STRING || token->type == CPP_WSTRING
|| token->type == CPP_COMMENT)
check_multiline_token (&token->val.str);
/* Adjust print.line for newlines embedded in tokens. */
static void
check_multiline_token (str)
- cpp_string *str;
+ const cpp_string *str;
{
unsigned int i;
/* If the token read on logical line LINE needs to be output on a
different line to the current one, output the required newlines or
a line marker, and return 1. Otherwise return 0. */
-
static void
maybe_print_line (map, line)
const struct line_map *map;
}
/* Called when a line of output is started. TOKEN is the first token
- of the line, and maybe be CPP_EOF. */
-
+ of the line, and at end of file will be CPP_EOF. */
static void
cb_line_change (pfile, token, parsing_args)
cpp_reader *pfile ATTRIBUTE_UNUSED;
maybe_print_line (print.map, token->line);
print.printed = 1;
+ print.prev = 0;
+ print.source = 0;
/* Supply enough spaces to put this token in its original column,
one space per column greater than 2, since scan_translation_unit